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 | /* pngrutil.c - utilities to read a PNG file |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 3 | * |
Glenn Randers-Pehrson | 397100e | 1998-03-07 19:45:37 -0600 | [diff] [blame^] | 4 | * libpng 0.99 |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 5 | * For conditions of distribution and use, see copyright notice in png.h |
| 6 | * Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc. |
| 7 | * Copyright (c) 1996, 1997 Andreas Dilger |
Glenn Randers-Pehrson | 2687fcc | 1998-01-07 20:54:20 -0600 | [diff] [blame] | 8 | * Copyright (c) 1998, Glenn Randers-Pehrson |
Glenn Randers-Pehrson | 397100e | 1998-03-07 19:45:37 -0600 | [diff] [blame^] | 9 | * March 7, 1998 |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 10 | * |
| 11 | * This file contains routines which are only called from within |
| 12 | * libpng itself during the course of reading an image. |
| 13 | */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 14 | |
| 15 | #define PNG_INTERNAL |
| 16 | #include "png.h" |
| 17 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 18 | #ifndef PNG_READ_BIG_ENDIAN_SUPPORTED |
| 19 | /* Grab an unsigned 32-bit integer from a buffer in big endian format. */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 20 | png_uint_32 |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 21 | png_get_uint_32(png_bytep buf) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 22 | { |
| 23 | png_uint_32 i; |
| 24 | |
| 25 | i = ((png_uint_32)(*buf) << 24) + |
| 26 | ((png_uint_32)(*(buf + 1)) << 16) + |
| 27 | ((png_uint_32)(*(buf + 2)) << 8) + |
| 28 | (png_uint_32)(*(buf + 3)); |
| 29 | |
Glenn Randers-Pehrson | b212002 | 1998-01-31 20:07:59 -0600 | [diff] [blame] | 30 | return (i); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 31 | } |
| 32 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 33 | #if defined(PNG_READ_pCAL_SUPPORTED) |
| 34 | /* Grab a signed 32-bit integer from a buffer in big endian format. The |
| 35 | * data is stored in the PNG file in two's complement format, and it is |
| 36 | * assumed that the machine format for signed integers is the same. */ |
| 37 | png_int_32 |
| 38 | png_get_int_32(png_bytep buf) |
| 39 | { |
| 40 | png_int_32 i; |
| 41 | |
| 42 | i = ((png_int_32)(*buf) << 24) + |
| 43 | ((png_int_32)(*(buf + 1)) << 16) + |
| 44 | ((png_int_32)(*(buf + 2)) << 8) + |
| 45 | (png_int_32)(*(buf + 3)); |
| 46 | |
Glenn Randers-Pehrson | b212002 | 1998-01-31 20:07:59 -0600 | [diff] [blame] | 47 | return (i); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 48 | } |
| 49 | #endif /* PNG_READ_pCAL_SUPPORTED */ |
| 50 | |
| 51 | /* Grab an unsigned 16-bit integer from a buffer in big endian format. */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 52 | png_uint_16 |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 53 | png_get_uint_16(png_bytep buf) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 54 | { |
| 55 | png_uint_16 i; |
| 56 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 57 | i = (png_uint_16)(((png_uint_16)(*buf) << 8) + |
| 58 | (png_uint_16)(*(buf + 1))); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 59 | |
Glenn Randers-Pehrson | b212002 | 1998-01-31 20:07:59 -0600 | [diff] [blame] | 60 | return (i); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 61 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 62 | #endif /* PNG_READ_BIG_ENDIAN_SUPPORTED */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 63 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 64 | /* Read data, and (optionally) run it through the CRC. */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 65 | void |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 66 | png_crc_read(png_structp png_ptr, png_bytep buf, png_size_t length) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 67 | { |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 68 | png_read_data(png_ptr, buf, length); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 69 | png_calculate_crc(png_ptr, buf, length); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 70 | } |
| 71 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 72 | /* Optionally skip data and then check the CRC. Depending on whether we |
| 73 | are reading a ancillary or critical chunk, and how the program has set |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 74 | things up, we may calculate the CRC on the data and print a message. |
| 75 | Returns '1' if there was a CRC error, '0' otherwise. */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 76 | int |
| 77 | png_crc_finish(png_structp png_ptr, png_uint_32 skip) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 78 | { |
| 79 | png_uint_32 i; |
| 80 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 81 | for (i = skip; i > (png_uint_32)png_ptr->zbuf_size; i -= png_ptr->zbuf_size) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 82 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 83 | png_crc_read(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 84 | } |
| 85 | if (i) |
| 86 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 87 | png_crc_read(png_ptr, png_ptr->zbuf, (png_size_t)i); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 88 | } |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 89 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 90 | if (png_crc_error(png_ptr)) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 91 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 92 | if ((png_ptr->chunk_name[0] & 0x20 && /* Ancillary */ |
| 93 | !(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN)) || |
| 94 | (!(png_ptr->chunk_name[0] & 0x20) && /* Critical */ |
| 95 | png_ptr->flags & PNG_FLAG_CRC_CRITICAL_USE)) |
| 96 | { |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 97 | png_chunk_warning(png_ptr, "CRC error"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 98 | } |
| 99 | else |
| 100 | { |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 101 | png_chunk_error(png_ptr, "CRC error"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 102 | } |
Glenn Randers-Pehrson | b212002 | 1998-01-31 20:07:59 -0600 | [diff] [blame] | 103 | return (1); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 104 | } |
| 105 | |
Glenn Randers-Pehrson | b212002 | 1998-01-31 20:07:59 -0600 | [diff] [blame] | 106 | return (0); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 107 | } |
| 108 | |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 109 | /* Compare the CRC stored in the PNG file with that calculated by libpng from |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 110 | the data it has read thus far. */ |
| 111 | int |
| 112 | png_crc_error(png_structp png_ptr) |
| 113 | { |
| 114 | png_byte crc_bytes[4]; |
| 115 | png_uint_32 crc; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 116 | int need_crc = 1; |
| 117 | |
| 118 | if (png_ptr->chunk_name[0] & 0x20) /* ancillary */ |
| 119 | { |
| 120 | if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_MASK) == |
| 121 | (PNG_FLAG_CRC_ANCILLARY_USE | PNG_FLAG_CRC_ANCILLARY_NOWARN)) |
| 122 | need_crc = 0; |
| 123 | } |
| 124 | else /* critical */ |
| 125 | { |
| 126 | if (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_IGNORE) |
| 127 | need_crc = 0; |
| 128 | } |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 129 | |
| 130 | png_read_data(png_ptr, crc_bytes, 4); |
| 131 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 132 | if (need_crc) |
| 133 | { |
| 134 | crc = png_get_uint_32(crc_bytes); |
Glenn Randers-Pehrson | b212002 | 1998-01-31 20:07:59 -0600 | [diff] [blame] | 135 | return ((int)(crc != png_ptr->crc)); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 136 | } |
| 137 | else |
Glenn Randers-Pehrson | b212002 | 1998-01-31 20:07:59 -0600 | [diff] [blame] | 138 | return (0); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 142 | /* read and check the IDHR chunk */ |
| 143 | void |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 144 | png_handle_IHDR(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 145 | { |
| 146 | png_byte buf[13]; |
| 147 | png_uint_32 width, height; |
| 148 | int bit_depth, color_type, compression_type, filter_type; |
| 149 | int interlace_type; |
| 150 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 151 | png_debug(1, "in png_handle_IHDR\n"); |
| 152 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 153 | if (png_ptr->mode != PNG_BEFORE_IHDR) |
| 154 | png_error(png_ptr, "Out of place IHDR"); |
| 155 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 156 | /* check the length */ |
| 157 | if (length != 13) |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 158 | png_error(png_ptr, "Invalid IHDR chunk"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 159 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 160 | png_ptr->mode |= PNG_HAVE_IHDR; |
| 161 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 162 | png_crc_read(png_ptr, buf, 13); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 163 | png_crc_finish(png_ptr, 0); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 164 | |
| 165 | width = png_get_uint_32(buf); |
| 166 | height = png_get_uint_32(buf + 4); |
| 167 | bit_depth = buf[8]; |
| 168 | color_type = buf[9]; |
| 169 | compression_type = buf[10]; |
| 170 | filter_type = buf[11]; |
| 171 | interlace_type = buf[12]; |
| 172 | |
| 173 | /* check for width and height valid values */ |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 174 | if (width == 0 || width > (png_uint_32)2147483647L || height == 0 || |
| 175 | height > (png_uint_32)2147483647L) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 176 | png_error(png_ptr, "Invalid image size in IHDR"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 177 | |
| 178 | /* check other values */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 179 | if (bit_depth != 1 && bit_depth != 2 && bit_depth != 4 && |
| 180 | bit_depth != 8 && bit_depth != 16) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 181 | png_error(png_ptr, "Invalid bit depth in IHDR"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 182 | |
| 183 | if (color_type < 0 || color_type == 1 || |
| 184 | color_type == 5 || color_type > 6) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 185 | png_error(png_ptr, "Invalid color type in IHDR"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 186 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 187 | if ((color_type == PNG_COLOR_TYPE_PALETTE && bit_depth) > 8 || |
| 188 | ((color_type == PNG_COLOR_TYPE_RGB || |
| 189 | color_type == PNG_COLOR_TYPE_GRAY_ALPHA || |
| 190 | color_type == PNG_COLOR_TYPE_RGB_ALPHA) && bit_depth < 8)) |
| 191 | png_error(png_ptr, "Invalid color type/bit depth combination in IHDR"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 192 | |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 193 | if (interlace_type >= PNG_INTERLACE_LAST) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 194 | png_error(png_ptr, "Unknown interlace method in IHDR"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 195 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 196 | if (compression_type != PNG_COMPRESSION_TYPE_BASE) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 197 | png_error(png_ptr, "Unknown compression method in IHDR"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 198 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 199 | if (filter_type != PNG_FILTER_TYPE_BASE) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 200 | png_error(png_ptr, "Unknown filter method in IHDR"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 201 | |
| 202 | /* set internal variables */ |
| 203 | png_ptr->width = width; |
| 204 | png_ptr->height = height; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 205 | png_ptr->bit_depth = (png_byte)bit_depth; |
| 206 | png_ptr->interlaced = (png_byte)interlace_type; |
| 207 | png_ptr->color_type = (png_byte)color_type; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 208 | |
| 209 | /* find number of channels */ |
| 210 | switch (png_ptr->color_type) |
| 211 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 212 | case PNG_COLOR_TYPE_GRAY: |
| 213 | case PNG_COLOR_TYPE_PALETTE: |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 214 | png_ptr->channels = 1; |
| 215 | break; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 216 | case PNG_COLOR_TYPE_RGB: |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 217 | png_ptr->channels = 3; |
| 218 | break; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 219 | case PNG_COLOR_TYPE_GRAY_ALPHA: |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 220 | png_ptr->channels = 2; |
| 221 | break; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 222 | case PNG_COLOR_TYPE_RGB_ALPHA: |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 223 | png_ptr->channels = 4; |
| 224 | break; |
| 225 | } |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 226 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 227 | /* set up other useful info */ |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 228 | png_ptr->pixel_depth = (png_byte)(png_ptr->bit_depth * |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame] | 229 | png_ptr->channels); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 230 | png_ptr->rowbytes = ((png_ptr->width * |
| 231 | (png_uint_32)png_ptr->pixel_depth + 7) >> 3); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 232 | png_debug1(3,"bit_depth = %d\n", png_ptr->bit_depth); |
| 233 | png_debug1(3,"channels = %d\n", png_ptr->channels); |
| 234 | png_debug1(3,"rowbytes = %d\n", png_ptr->rowbytes); |
| 235 | png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth, |
| 236 | color_type, interlace_type, compression_type, filter_type); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | /* read and check the palette */ |
| 240 | void |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 241 | png_handle_PLTE(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 242 | { |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 243 | png_colorp palette; |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 244 | int num, i; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 245 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 246 | png_debug(1, "in png_handle_PLTE\n"); |
| 247 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 248 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
| 249 | png_error(png_ptr, "Missing IHDR before PLTE"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 250 | else if (png_ptr->mode & PNG_HAVE_IDAT) |
| 251 | { |
| 252 | png_warning(png_ptr, "Invalid PLTE after IDAT"); |
| 253 | png_crc_finish(png_ptr, length); |
| 254 | return; |
| 255 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 256 | else if (png_ptr->mode & PNG_HAVE_PLTE) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 257 | png_error(png_ptr, "Duplicate PLTE chunk"); |
| 258 | |
| 259 | png_ptr->mode |= PNG_HAVE_PLTE; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 260 | |
Glenn Randers-Pehrson | cbe52d8 | 1998-02-28 07:00:24 -0600 | [diff] [blame] | 261 | #if defined (PNG_READ_tRNS_SUPPORTED) |
| 262 | if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) |
| 263 | { |
| 264 | if (info_ptr != NULL && info_ptr->valid & PNG_INFO_tRNS) |
| 265 | { |
| 266 | if (png_ptr->num_trans > png_ptr->num_palette) |
| 267 | { |
| 268 | png_warning(png_ptr, "Truncating incorrect tRNS chunk length"); |
| 269 | png_ptr->num_trans = png_ptr->num_palette; |
| 270 | } |
| 271 | } |
| 272 | } |
| 273 | #endif |
| 274 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 275 | #if !defined(PNG_READ_OPT_PLTE_SUPPORTED) |
| 276 | if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE) |
| 277 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 278 | png_crc_finish(png_ptr, length); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 279 | return; |
| 280 | } |
| 281 | #endif |
| 282 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 283 | if (length % 3) |
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_warning(png_ptr, "Invalid palette chunk"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 288 | png_crc_finish(png_ptr, length); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 289 | return; |
| 290 | } |
| 291 | else |
| 292 | { |
| 293 | png_error(png_ptr, "Invalid palette chunk"); |
| 294 | } |
| 295 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 296 | |
| 297 | num = (int)length / 3; |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 298 | palette = (png_colorp)png_zalloc(png_ptr, (uInt)num, sizeof (png_color)); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 299 | png_ptr->flags |= PNG_FLAG_FREE_PALETTE; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 300 | for (i = 0; i < num; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 301 | { |
| 302 | png_byte buf[3]; |
| 303 | |
| 304 | png_crc_read(png_ptr, buf, 3); |
| 305 | /* don't depend upon png_color being any order */ |
| 306 | palette[i].red = buf[0]; |
| 307 | palette[i].green = buf[1]; |
| 308 | palette[i].blue = buf[2]; |
| 309 | } |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 310 | |
| 311 | /* If we actually NEED the PLTE chunk (ie for a paletted image), we do |
| 312 | whatever the normal CRC configuration tells us. However, if we |
| 313 | have an RGB image, the PLTE can be considered ancillary, so |
| 314 | we will act as though it is. */ |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 315 | #if !defined(PNG_READ_OPT_PLTE_SUPPORTED) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 316 | if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 317 | #endif |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 318 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 319 | png_crc_finish(png_ptr, 0); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 320 | } |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 321 | #if !defined(PNG_READ_OPT_PLTE_SUPPORTED) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 322 | else if (png_crc_error(png_ptr)) /* Only if we have a CRC error */ |
| 323 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 324 | /* If we don't want to use the data from an ancillary chunk, |
| 325 | we have two options: an error abort, or a warning and we |
| 326 | ignore the data in this chunk (which should be OK, since |
| 327 | it's considered ancillary for a RGB or RGBA image). */ |
| 328 | if (!(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_USE)) |
| 329 | { |
| 330 | if (png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN) |
| 331 | { |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 332 | png_chunk_error(png_ptr, "CRC error"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 333 | } |
| 334 | else |
| 335 | { |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 336 | png_chunk_warning(png_ptr, "CRC error"); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 337 | png_ptr->flags &= ~PNG_FLAG_FREE_PALETTE; |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 338 | png_zfree(png_ptr, palette); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 339 | return; |
| 340 | } |
| 341 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 342 | /* Otherwise, we (optionally) emit a warning and use the chunk. */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 343 | else if (!(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN)) |
| 344 | { |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 345 | png_chunk_warning(png_ptr, "CRC error"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 346 | } |
| 347 | } |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 348 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 349 | png_ptr->palette = palette; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 350 | png_ptr->num_palette = (png_uint_16)num; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 351 | png_set_PLTE(png_ptr, info_ptr, palette, num); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 352 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 353 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 354 | void |
| 355 | png_handle_IEND(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) |
| 356 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 357 | png_debug(1, "in png_handle_IEND\n"); |
| 358 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 359 | if (!(png_ptr->mode & PNG_HAVE_IHDR) || !(png_ptr->mode & PNG_HAVE_IDAT)) |
| 360 | { |
| 361 | png_error(png_ptr, "No image in file"); |
Glenn Randers-Pehrson | a357b99 | 1998-02-08 20:56:40 -0600 | [diff] [blame] | 362 | |
| 363 | /* to quiet compiler warnings about unused info_ptr */ |
| 364 | if (info_ptr == NULL) |
| 365 | return; |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | png_ptr->mode |= PNG_AFTER_IDAT | PNG_HAVE_IEND; |
| 369 | |
| 370 | if (length != 0) |
| 371 | { |
| 372 | png_warning(png_ptr, "Incorrect IEND chunk length"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 373 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 374 | png_crc_finish(png_ptr, length); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 375 | } |
| 376 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 377 | #if defined(PNG_READ_gAMA_SUPPORTED) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 378 | void |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 379 | png_handle_gAMA(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 380 | { |
| 381 | png_uint_32 igamma; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 382 | float file_gamma; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 383 | png_byte buf[4]; |
| 384 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 385 | png_debug(1, "in png_handle_gAMA\n"); |
| 386 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 387 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
| 388 | png_error(png_ptr, "Missing IHDR before gAMA"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 389 | else if (png_ptr->mode & PNG_HAVE_IDAT) |
| 390 | { |
| 391 | png_warning(png_ptr, "Invalid gAMA after IDAT"); |
| 392 | png_crc_finish(png_ptr, length); |
| 393 | return; |
| 394 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 395 | else if (png_ptr->mode & PNG_HAVE_PLTE) |
| 396 | /* Should be an error, but we can cope with it */ |
| 397 | png_warning(png_ptr, "Out of place gAMA chunk"); |
Glenn Randers-Pehrson | 2687fcc | 1998-01-07 20:54:20 -0600 | [diff] [blame] | 398 | |
| 399 | else if (info_ptr != NULL && info_ptr->valid & PNG_INFO_gAMA |
| 400 | #if defined(PNG_READ_sRGB_SUPPORTED) |
| 401 | && !(info_ptr->valid & PNG_INFO_sRGB) |
| 402 | #endif |
| 403 | ) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 404 | { |
| 405 | png_warning(png_ptr, "Duplicate gAMA chunk"); |
| 406 | png_crc_finish(png_ptr, length); |
| 407 | return; |
| 408 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 409 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 410 | if (length != 4) |
| 411 | { |
Guy Schalnat | 69b1448 | 1996-01-10 02:56:49 -0600 | [diff] [blame] | 412 | png_warning(png_ptr, "Incorrect gAMA chunk length"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 413 | png_crc_finish(png_ptr, length); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 414 | return; |
| 415 | } |
| 416 | |
| 417 | png_crc_read(png_ptr, buf, 4); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 418 | if (png_crc_finish(png_ptr, 0)) |
| 419 | return; |
| 420 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 421 | igamma = png_get_uint_32(buf); |
| 422 | /* check for zero gamma */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 423 | if (igamma == 0) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 424 | return; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 425 | |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 426 | #if defined(PNG_READ_sRGB_SUPPORTED) |
Glenn Randers-Pehrson | 2687fcc | 1998-01-07 20:54:20 -0600 | [diff] [blame] | 427 | if (info_ptr->valid & PNG_INFO_sRGB) |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 428 | if(igamma != (png_uint_32)45000L) |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 429 | { |
| 430 | png_warning(png_ptr, |
| 431 | "Ignoring incorrect gAMA value when sRGB is also present"); |
Glenn Randers-Pehrson | 2687fcc | 1998-01-07 20:54:20 -0600 | [diff] [blame] | 432 | #ifndef PNG_NO_STDIO |
| 433 | fprintf(stderr, "igamma = %lu\n", igamma); |
| 434 | #endif |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 435 | return; |
| 436 | } |
| 437 | #endif /* PNG_READ_sRGB_SUPPORTED */ |
| 438 | |
Glenn Randers-Pehrson | 397100e | 1998-03-07 19:45:37 -0600 | [diff] [blame^] | 439 | file_gamma = (float)igamma / (float)100000.0; |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 440 | #ifdef PNG_READ_GAMMA_SUPPORTED |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 441 | png_ptr->gamma = file_gamma; |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 442 | #endif |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 443 | png_set_gAMA(png_ptr, info_ptr, file_gamma); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 444 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 445 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 446 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 447 | #if defined(PNG_READ_sBIT_SUPPORTED) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 448 | void |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 449 | png_handle_sBIT(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 450 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 451 | png_size_t truelen; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 452 | png_byte buf[4]; |
Guy Schalnat | 69b1448 | 1996-01-10 02:56:49 -0600 | [diff] [blame] | 453 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 454 | png_debug(1, "in png_handle_sBIT\n"); |
| 455 | |
Guy Schalnat | 69b1448 | 1996-01-10 02:56:49 -0600 | [diff] [blame] | 456 | buf[0] = buf[1] = buf[2] = buf[3] = 0; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 457 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 458 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
| 459 | png_error(png_ptr, "Missing IHDR before sBIT"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 460 | else if (png_ptr->mode & PNG_HAVE_IDAT) |
| 461 | { |
| 462 | png_warning(png_ptr, "Invalid sBIT after IDAT"); |
| 463 | png_crc_finish(png_ptr, length); |
| 464 | return; |
| 465 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 466 | else if (png_ptr->mode & PNG_HAVE_PLTE) |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 467 | { |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 468 | /* Should be an error, but we can cope with it */ |
| 469 | png_warning(png_ptr, "Out of place sBIT chunk"); |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 470 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 471 | else if (info_ptr != NULL && info_ptr->valid & PNG_INFO_sBIT) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 472 | { |
| 473 | png_warning(png_ptr, "Duplicate sBIT chunk"); |
| 474 | png_crc_finish(png_ptr, length); |
| 475 | return; |
| 476 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 477 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 478 | if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 479 | truelen = 3; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 480 | else |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 481 | truelen = (png_size_t)png_ptr->channels; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 482 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 483 | if (length != truelen) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 484 | { |
Guy Schalnat | 69b1448 | 1996-01-10 02:56:49 -0600 | [diff] [blame] | 485 | png_warning(png_ptr, "Incorrect sBIT chunk length"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 486 | png_crc_finish(png_ptr, length); |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 487 | return; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 488 | } |
| 489 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 490 | png_crc_read(png_ptr, buf, truelen); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 491 | if (png_crc_finish(png_ptr, 0)) |
| 492 | return; |
| 493 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 494 | if (png_ptr->color_type & PNG_COLOR_MASK_COLOR) |
| 495 | { |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 496 | png_ptr->sig_bit.red = buf[0]; |
| 497 | png_ptr->sig_bit.green = buf[1]; |
| 498 | png_ptr->sig_bit.blue = buf[2]; |
| 499 | png_ptr->sig_bit.alpha = buf[3]; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 500 | } |
| 501 | else |
| 502 | { |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 503 | png_ptr->sig_bit.gray = buf[0]; |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 504 | png_ptr->sig_bit.alpha = buf[1]; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 505 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 506 | png_set_sBIT(png_ptr, info_ptr, &(png_ptr->sig_bit)); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 507 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 508 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 509 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 510 | #if defined(PNG_READ_cHRM_SUPPORTED) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 511 | void |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 512 | png_handle_cHRM(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 513 | { |
| 514 | png_byte buf[4]; |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 515 | png_uint_32 val; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 516 | float white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y; |
| 517 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 518 | png_debug(1, "in png_handle_cHRM\n"); |
| 519 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 520 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
| 521 | png_error(png_ptr, "Missing IHDR before sBIT"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 522 | else if (png_ptr->mode & PNG_HAVE_IDAT) |
| 523 | { |
| 524 | png_warning(png_ptr, "Invalid cHRM after IDAT"); |
| 525 | png_crc_finish(png_ptr, length); |
| 526 | return; |
| 527 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 528 | else if (png_ptr->mode & PNG_HAVE_PLTE) |
| 529 | /* Should be an error, but we can cope with it */ |
| 530 | png_warning(png_ptr, "Missing PLTE before cHRM"); |
Glenn Randers-Pehrson | 2687fcc | 1998-01-07 20:54:20 -0600 | [diff] [blame] | 531 | |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 532 | else if (info_ptr != NULL && info_ptr->valid & PNG_INFO_cHRM |
Glenn Randers-Pehrson | 2687fcc | 1998-01-07 20:54:20 -0600 | [diff] [blame] | 533 | #if defined(PNG_READ_sRGB_SUPPORTED) |
| 534 | && !(info_ptr->valid & PNG_INFO_sRGB) |
| 535 | #endif |
| 536 | ) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 537 | { |
| 538 | png_warning(png_ptr, "Duplicate cHRM chunk"); |
| 539 | png_crc_finish(png_ptr, length); |
| 540 | return; |
| 541 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 542 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 543 | if (length != 32) |
| 544 | { |
Guy Schalnat | 69b1448 | 1996-01-10 02:56:49 -0600 | [diff] [blame] | 545 | png_warning(png_ptr, "Incorrect cHRM chunk length"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 546 | png_crc_finish(png_ptr, length); |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 547 | return; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | png_crc_read(png_ptr, buf, 4); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 551 | val = png_get_uint_32(buf); |
Glenn Randers-Pehrson | 397100e | 1998-03-07 19:45:37 -0600 | [diff] [blame^] | 552 | white_x = (float)val / (float)100000.0; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 553 | |
| 554 | png_crc_read(png_ptr, buf, 4); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 555 | val = png_get_uint_32(buf); |
Glenn Randers-Pehrson | 397100e | 1998-03-07 19:45:37 -0600 | [diff] [blame^] | 556 | white_y = (float)val / (float)100000.0; |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 557 | |
| 558 | if (white_x < 0 || white_x > 0.8 || white_y < 0 || white_y > 0.8 || |
| 559 | white_x + white_y > 1.0) |
| 560 | { |
| 561 | png_warning(png_ptr, "Invalid cHRM white point"); |
| 562 | png_crc_finish(png_ptr, 24); |
| 563 | return; |
| 564 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 565 | |
| 566 | png_crc_read(png_ptr, buf, 4); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 567 | val = png_get_uint_32(buf); |
Glenn Randers-Pehrson | 397100e | 1998-03-07 19:45:37 -0600 | [diff] [blame^] | 568 | red_x = (float)val / (float)100000.0; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 569 | |
| 570 | png_crc_read(png_ptr, buf, 4); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 571 | val = png_get_uint_32(buf); |
Glenn Randers-Pehrson | 397100e | 1998-03-07 19:45:37 -0600 | [diff] [blame^] | 572 | red_y = (float)val / (float)100000.0; |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 573 | |
| 574 | if (red_x < 0 || red_x > 0.8 || red_y < 0 || red_y > 0.8 || |
| 575 | red_x + red_y > 1.0) |
| 576 | { |
| 577 | png_warning(png_ptr, "Invalid cHRM red point"); |
| 578 | png_crc_finish(png_ptr, 16); |
| 579 | return; |
| 580 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 581 | |
| 582 | png_crc_read(png_ptr, buf, 4); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 583 | val = png_get_uint_32(buf); |
Glenn Randers-Pehrson | 397100e | 1998-03-07 19:45:37 -0600 | [diff] [blame^] | 584 | green_x = (float)val / (float)100000.0; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 585 | |
| 586 | png_crc_read(png_ptr, buf, 4); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 587 | val = png_get_uint_32(buf); |
Glenn Randers-Pehrson | 397100e | 1998-03-07 19:45:37 -0600 | [diff] [blame^] | 588 | green_y = (float)val / (float)100000.0; |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 589 | |
| 590 | if (green_x < 0 || green_x > 0.8 || green_y < 0 || green_y > 0.8 || |
| 591 | green_x + green_y > 1.0) |
| 592 | { |
| 593 | png_warning(png_ptr, "Invalid cHRM green point"); |
| 594 | png_crc_finish(png_ptr, 8); |
| 595 | return; |
| 596 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 597 | |
| 598 | png_crc_read(png_ptr, buf, 4); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 599 | val = png_get_uint_32(buf); |
Glenn Randers-Pehrson | 397100e | 1998-03-07 19:45:37 -0600 | [diff] [blame^] | 600 | blue_x = (float)val / (float)100000.0; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 601 | |
| 602 | png_crc_read(png_ptr, buf, 4); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 603 | val = png_get_uint_32(buf); |
Glenn Randers-Pehrson | 397100e | 1998-03-07 19:45:37 -0600 | [diff] [blame^] | 604 | blue_y = (float)val / (float)100000.0; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 605 | |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 606 | if (blue_x < (float)0 || blue_x > (float)0.8 || blue_y < (float)0 || |
| 607 | blue_y > (float)0.8 || blue_x + blue_y > (float)1.0) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 608 | { |
| 609 | png_warning(png_ptr, "Invalid cHRM blue point"); |
| 610 | png_crc_finish(png_ptr, 0); |
| 611 | return; |
| 612 | } |
| 613 | |
| 614 | if (png_crc_finish(png_ptr, 0)) |
| 615 | return; |
| 616 | |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 617 | #if defined(PNG_READ_sRGB_SUPPORTED) |
Glenn Randers-Pehrson | 2687fcc | 1998-01-07 20:54:20 -0600 | [diff] [blame] | 618 | if (info_ptr->valid & PNG_INFO_sRGB) |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 619 | { |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 620 | if (fabs(white_x - (float).3127) > (float).001 || |
| 621 | fabs(white_y - (float).3290) > (float).001 || |
| 622 | fabs( red_x - (float).6400) > (float).001 || |
| 623 | fabs( red_y - (float).3300) > (float).001 || |
| 624 | fabs(green_x - (float).3000) > (float).001 || |
| 625 | fabs(green_y - (float).6000) > (float).001 || |
| 626 | fabs( blue_x - (float).1500) > (float).001 || |
Glenn Randers-Pehrson | 2687fcc | 1998-01-07 20:54:20 -0600 | [diff] [blame] | 627 | fabs( blue_y - (float).0600) > (float).001) |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 628 | { |
Glenn Randers-Pehrson | 2687fcc | 1998-01-07 20:54:20 -0600 | [diff] [blame] | 629 | |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 630 | png_warning(png_ptr, |
| 631 | "Ignoring incorrect cHRM value when sRGB is also present"); |
Glenn Randers-Pehrson | 2687fcc | 1998-01-07 20:54:20 -0600 | [diff] [blame] | 632 | #ifndef PNG_NO_STDIO |
| 633 | fprintf(stderr,"wx=%f, wy=%f, rx=%f, ry=%f\n", |
| 634 | white_x, white_y, red_x, red_y); |
| 635 | fprintf(stderr,"gx=%f, gy=%f, bx=%f, by=%f\n", |
| 636 | green_x, green_y, blue_x, blue_y); |
| 637 | #endif |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 638 | } |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 639 | return; |
| 640 | } |
| 641 | #endif /* PNG_READ_sRGB_SUPPORTED */ |
| 642 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 643 | png_set_cHRM(png_ptr, info_ptr, |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 644 | white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 645 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 646 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 647 | |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 648 | #if defined(PNG_READ_sRGB_SUPPORTED) |
| 649 | void |
| 650 | png_handle_sRGB(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) |
| 651 | { |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 652 | int intent; |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 653 | png_byte buf[1]; |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 654 | |
| 655 | png_debug(1, "in png_handle_sRGB\n"); |
| 656 | |
| 657 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
| 658 | png_error(png_ptr, "Missing IHDR before sRGB"); |
| 659 | else if (png_ptr->mode & PNG_HAVE_IDAT) |
| 660 | { |
| 661 | png_warning(png_ptr, "Invalid sRGB after IDAT"); |
| 662 | png_crc_finish(png_ptr, length); |
| 663 | return; |
| 664 | } |
| 665 | else if (png_ptr->mode & PNG_HAVE_PLTE) |
| 666 | /* Should be an error, but we can cope with it */ |
| 667 | png_warning(png_ptr, "Out of place sRGB chunk"); |
Glenn Randers-Pehrson | 2687fcc | 1998-01-07 20:54:20 -0600 | [diff] [blame] | 668 | |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 669 | else if (info_ptr != NULL && info_ptr->valid & PNG_INFO_sRGB) |
| 670 | { |
| 671 | png_warning(png_ptr, "Duplicate sRGB chunk"); |
| 672 | png_crc_finish(png_ptr, length); |
| 673 | return; |
| 674 | } |
| 675 | |
| 676 | if (length != 1) |
| 677 | { |
| 678 | png_warning(png_ptr, "Incorrect sRGB chunk length"); |
| 679 | png_crc_finish(png_ptr, length); |
| 680 | return; |
| 681 | } |
| 682 | |
| 683 | png_crc_read(png_ptr, buf, 1); |
| 684 | if (png_crc_finish(png_ptr, 0)) |
| 685 | return; |
| 686 | |
| 687 | intent = buf[0]; |
| 688 | /* check for bad intent */ |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 689 | if (intent >= PNG_sRGB_INTENT_LAST) |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 690 | { |
| 691 | png_warning(png_ptr, "Unknown sRGB intent"); |
| 692 | return; |
| 693 | } |
| 694 | |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 695 | #if defined(PNG_READ_gAMA_SUPPORTED) && defined(PNG_READ_GAMMA_SUPPORTED) |
Glenn Randers-Pehrson | 2687fcc | 1998-01-07 20:54:20 -0600 | [diff] [blame] | 696 | if ((info_ptr->valid & PNG_INFO_gAMA)) |
Glenn Randers-Pehrson | 397100e | 1998-03-07 19:45:37 -0600 | [diff] [blame^] | 697 | if((png_uint_32)(png_ptr->gamma*(float)100000.+.5) != (png_uint_32)45000L) |
Glenn Randers-Pehrson | 2687fcc | 1998-01-07 20:54:20 -0600 | [diff] [blame] | 698 | { |
| 699 | png_warning(png_ptr, |
| 700 | "Ignoring incorrect gAMA value when sRGB is also present"); |
| 701 | #ifndef PNG_NO_STDIO |
| 702 | fprintf(stderr,"gamma=%f\n",png_ptr->gamma); |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 703 | #endif |
Glenn Randers-Pehrson | 2687fcc | 1998-01-07 20:54:20 -0600 | [diff] [blame] | 704 | } |
| 705 | #endif /* PNG_READ_gAMA_SUPPORTED */ |
| 706 | |
| 707 | #ifdef PNG_READ_cHRM_SUPPORTED |
| 708 | if (info_ptr->valid & PNG_INFO_cHRM) |
| 709 | if (fabs(info_ptr->x_white - (float).3127) > (float).001 || |
| 710 | fabs(info_ptr->y_white - (float).3290) > (float).001 || |
| 711 | fabs( info_ptr->x_red - (float).6400) > (float).001 || |
| 712 | fabs( info_ptr->y_red - (float).3300) > (float).001 || |
| 713 | fabs(info_ptr->x_green - (float).3000) > (float).001 || |
| 714 | fabs(info_ptr->y_green - (float).6000) > (float).001 || |
| 715 | fabs( info_ptr->x_blue - (float).1500) > (float).001 || |
| 716 | fabs( info_ptr->y_blue - (float).0600) > (float).001) |
| 717 | { |
| 718 | png_warning(png_ptr, |
| 719 | "Ignoring incorrect cHRM value when sRGB is also present"); |
| 720 | } |
| 721 | #endif /* PNG_READ_cHRM_SUPPORTED */ |
| 722 | |
| 723 | png_set_sRGB_gAMA_and_cHRM(png_ptr, info_ptr, intent); |
| 724 | } |
| 725 | #endif /* PNG_READ_sRGB_SUPPORTED */ |
| 726 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 727 | #if defined(PNG_READ_tRNS_SUPPORTED) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 728 | void |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 729 | png_handle_tRNS(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 730 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 731 | png_debug(1, "in png_handle_tRNS\n"); |
| 732 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 733 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
| 734 | png_error(png_ptr, "Missing IHDR before tRNS"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 735 | else if (png_ptr->mode & PNG_HAVE_IDAT) |
| 736 | { |
| 737 | png_warning(png_ptr, "Invalid tRNS after IDAT"); |
| 738 | png_crc_finish(png_ptr, length); |
| 739 | return; |
| 740 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 741 | else if (info_ptr != NULL && info_ptr->valid & PNG_INFO_tRNS) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 742 | { |
Glenn Randers-Pehrson | a357b99 | 1998-02-08 20:56:40 -0600 | [diff] [blame] | 743 | png_warning(png_ptr, "Duplicate tRNS chunk"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 744 | png_crc_finish(png_ptr, length); |
| 745 | return; |
| 746 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 747 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 748 | if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) |
| 749 | { |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 750 | if (!(png_ptr->mode & PNG_HAVE_PLTE)) |
| 751 | { |
| 752 | /* Should be an error, but we can cope with it */ |
| 753 | png_warning(png_ptr, "Missing PLTE before tRNS"); |
| 754 | } |
| 755 | else if (length > png_ptr->num_palette) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 756 | { |
Guy Schalnat | 69b1448 | 1996-01-10 02:56:49 -0600 | [diff] [blame] | 757 | png_warning(png_ptr, "Incorrect tRNS chunk length"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 758 | png_crc_finish(png_ptr, length); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 759 | return; |
| 760 | } |
| 761 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 762 | png_ptr->trans = (png_bytep)png_malloc(png_ptr, length); |
| 763 | png_ptr->flags |= PNG_FLAG_FREE_TRANS; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 764 | png_crc_read(png_ptr, png_ptr->trans, (png_size_t)length); |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 765 | png_ptr->num_trans = (png_uint_16)length; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 766 | } |
| 767 | else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB) |
| 768 | { |
| 769 | png_byte buf[6]; |
| 770 | |
| 771 | if (length != 6) |
| 772 | { |
Guy Schalnat | 69b1448 | 1996-01-10 02:56:49 -0600 | [diff] [blame] | 773 | png_warning(png_ptr, "Incorrect tRNS chunk length"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 774 | png_crc_finish(png_ptr, length); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 775 | return; |
| 776 | } |
| 777 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 778 | png_crc_read(png_ptr, buf, (png_size_t)length); |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 779 | png_ptr->num_trans = 1; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 780 | png_ptr->trans_values.red = png_get_uint_16(buf); |
| 781 | png_ptr->trans_values.green = png_get_uint_16(buf + 2); |
| 782 | png_ptr->trans_values.blue = png_get_uint_16(buf + 4); |
| 783 | } |
| 784 | else if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY) |
| 785 | { |
| 786 | png_byte buf[6]; |
| 787 | |
| 788 | if (length != 2) |
| 789 | { |
Guy Schalnat | 69b1448 | 1996-01-10 02:56:49 -0600 | [diff] [blame] | 790 | png_warning(png_ptr, "Incorrect tRNS chunk length"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 791 | png_crc_finish(png_ptr, length); |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 792 | return; |
| 793 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 794 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 795 | png_crc_read(png_ptr, buf, 2); |
| 796 | png_ptr->num_trans = 1; |
| 797 | png_ptr->trans_values.gray = png_get_uint_16(buf); |
| 798 | } |
| 799 | else |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 800 | { |
| 801 | png_warning(png_ptr, "tRNS chunk not allowed with alpha channel"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 802 | png_crc_finish(png_ptr, length); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 803 | return; |
| 804 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 805 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 806 | if (png_crc_finish(png_ptr, 0)) |
| 807 | return; |
| 808 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 809 | png_set_tRNS(png_ptr, info_ptr, png_ptr->trans, png_ptr->num_trans, |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 810 | &(png_ptr->trans_values)); |
| 811 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 812 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 813 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 814 | #if defined(PNG_READ_bKGD_SUPPORTED) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 815 | void |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 816 | png_handle_bKGD(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 817 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 818 | png_size_t truelen; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 819 | png_byte buf[6]; |
| 820 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 821 | png_debug(1, "in png_handle_bKGD\n"); |
| 822 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 823 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
| 824 | png_error(png_ptr, "Missing IHDR before bKGD"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 825 | else if (png_ptr->mode & PNG_HAVE_IDAT) |
| 826 | { |
| 827 | png_warning(png_ptr, "Invalid bKGD after IDAT"); |
| 828 | png_crc_finish(png_ptr, length); |
| 829 | return; |
| 830 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 831 | else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE && |
| 832 | !(png_ptr->mode & PNG_HAVE_PLTE)) |
| 833 | { |
| 834 | png_warning(png_ptr, "Missing PLTE before bKGD"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 835 | png_crc_finish(png_ptr, length); |
| 836 | return; |
| 837 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 838 | else if (info_ptr != NULL && info_ptr->valid & PNG_INFO_bKGD) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 839 | { |
| 840 | png_warning(png_ptr, "Duplicate bKGD chunk"); |
| 841 | png_crc_finish(png_ptr, length); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 842 | return; |
| 843 | } |
| 844 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 845 | if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) |
| 846 | truelen = 1; |
| 847 | else if (png_ptr->color_type & PNG_COLOR_MASK_COLOR) |
| 848 | truelen = 6; |
| 849 | else |
| 850 | truelen = 2; |
| 851 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 852 | if (length != truelen) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 853 | { |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 854 | png_warning(png_ptr, "Incorrect bKGD chunk length"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 855 | png_crc_finish(png_ptr, length); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 856 | return; |
| 857 | } |
| 858 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 859 | png_crc_read(png_ptr, buf, truelen); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 860 | if (png_crc_finish(png_ptr, 0)) |
| 861 | return; |
| 862 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 863 | /* We convert the index value into RGB components so that we can allow |
| 864 | * arbitrary RGB values for background when we have transparency, and |
| 865 | * so it is easy to determine the RGB values of the background color |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 866 | * from the info_ptr struct. */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 867 | if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 868 | { |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 869 | png_ptr->background.index = buf[0]; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 870 | png_ptr->background.red = (png_uint_16)png_ptr->palette[buf[0]].red; |
| 871 | png_ptr->background.green = (png_uint_16)png_ptr->palette[buf[0]].green; |
| 872 | png_ptr->background.blue = (png_uint_16)png_ptr->palette[buf[0]].blue; |
| 873 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 874 | else if (!(png_ptr->color_type & PNG_COLOR_MASK_COLOR)) /* GRAY */ |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 875 | { |
| 876 | png_ptr->background.red = |
| 877 | png_ptr->background.green = |
| 878 | png_ptr->background.blue = |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 879 | png_ptr->background.gray = png_get_uint_16(buf); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 880 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 881 | else |
| 882 | { |
| 883 | png_ptr->background.red = png_get_uint_16(buf); |
| 884 | png_ptr->background.green = png_get_uint_16(buf + 2); |
| 885 | png_ptr->background.blue = png_get_uint_16(buf + 4); |
| 886 | } |
| 887 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 888 | png_set_bKGD(png_ptr, info_ptr, &(png_ptr->background)); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 889 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 890 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 891 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 892 | #if defined(PNG_READ_hIST_SUPPORTED) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 893 | void |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 894 | png_handle_hIST(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 895 | { |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 896 | int num, i; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 897 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 898 | png_debug(1, "in png_handle_hIST\n"); |
| 899 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 900 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
| 901 | png_error(png_ptr, "Missing IHDR before hIST"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 902 | else if (png_ptr->mode & PNG_HAVE_IDAT) |
| 903 | { |
| 904 | png_warning(png_ptr, "Invalid hIST after IDAT"); |
| 905 | png_crc_finish(png_ptr, length); |
| 906 | return; |
| 907 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 908 | else if (!(png_ptr->mode & PNG_HAVE_PLTE)) |
| 909 | { |
| 910 | png_warning(png_ptr, "Missing PLTE before hIST"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 911 | png_crc_finish(png_ptr, length); |
| 912 | return; |
| 913 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 914 | else if (info_ptr != NULL && info_ptr->valid & PNG_INFO_hIST) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 915 | { |
| 916 | png_warning(png_ptr, "Duplicate hIST chunk"); |
| 917 | png_crc_finish(png_ptr, length); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 918 | return; |
| 919 | } |
| 920 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 921 | if (length != (png_uint_32)(2 * png_ptr->num_palette)) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 922 | { |
| 923 | png_warning(png_ptr, "Incorrect hIST chunk length"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 924 | png_crc_finish(png_ptr, length); |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 925 | return; |
| 926 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 927 | |
| 928 | num = (int)length / 2; |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 929 | png_ptr->hist = (png_uint_16p)png_malloc(png_ptr, |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame] | 930 | (png_uint_32)(num * sizeof (png_uint_16))); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 931 | png_ptr->flags |= PNG_FLAG_FREE_HIST; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 932 | for (i = 0; i < num; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 933 | { |
| 934 | png_byte buf[2]; |
| 935 | |
| 936 | png_crc_read(png_ptr, buf, 2); |
| 937 | png_ptr->hist[i] = png_get_uint_16(buf); |
| 938 | } |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 939 | |
| 940 | if (png_crc_finish(png_ptr, 0)) |
| 941 | return; |
| 942 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 943 | png_set_hIST(png_ptr, info_ptr, png_ptr->hist); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 944 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 945 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 946 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 947 | #if defined(PNG_READ_pHYs_SUPPORTED) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 948 | void |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 949 | png_handle_pHYs(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 950 | { |
| 951 | png_byte buf[9]; |
| 952 | png_uint_32 res_x, res_y; |
| 953 | int unit_type; |
| 954 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 955 | png_debug(1, "in png_handle_pHYs\n"); |
| 956 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 957 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
| 958 | png_error(png_ptr, "Missing IHDR before pHYS"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 959 | else if (png_ptr->mode & PNG_HAVE_IDAT) |
| 960 | { |
| 961 | png_warning(png_ptr, "Invalid pHYS after IDAT"); |
| 962 | png_crc_finish(png_ptr, length); |
| 963 | return; |
| 964 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 965 | else if (info_ptr != NULL && info_ptr->valid & PNG_INFO_pHYs) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 966 | { |
| 967 | png_warning(png_ptr, "Duplicate pHYS chunk"); |
| 968 | png_crc_finish(png_ptr, length); |
| 969 | return; |
| 970 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 971 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 972 | if (length != 9) |
| 973 | { |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 974 | png_warning(png_ptr, "Incorrect pHYs chunk length"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 975 | png_crc_finish(png_ptr, length); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 976 | return; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 977 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 978 | |
| 979 | png_crc_read(png_ptr, buf, 9); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 980 | if (png_crc_finish(png_ptr, 0)) |
| 981 | return; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 982 | |
| 983 | res_x = png_get_uint_32(buf); |
| 984 | res_y = png_get_uint_32(buf + 4); |
| 985 | unit_type = buf[8]; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 986 | png_set_pHYs(png_ptr, info_ptr, res_x, res_y, unit_type); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 987 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 988 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 989 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 990 | #if defined(PNG_READ_oFFs_SUPPORTED) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 991 | void |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 992 | png_handle_oFFs(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 993 | { |
| 994 | png_byte buf[9]; |
| 995 | png_uint_32 offset_x, offset_y; |
| 996 | int unit_type; |
| 997 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 998 | png_debug(1, "in png_handle_oFFs\n"); |
| 999 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1000 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
| 1001 | png_error(png_ptr, "Missing IHDR before oFFs"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1002 | else if (png_ptr->mode & PNG_HAVE_IDAT) |
| 1003 | { |
| 1004 | png_warning(png_ptr, "Invalid oFFs after IDAT"); |
| 1005 | png_crc_finish(png_ptr, length); |
| 1006 | return; |
| 1007 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1008 | else if (info_ptr != NULL && info_ptr->valid & PNG_INFO_oFFs) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1009 | { |
| 1010 | png_warning(png_ptr, "Duplicate oFFs chunk"); |
| 1011 | png_crc_finish(png_ptr, length); |
| 1012 | return; |
| 1013 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1014 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1015 | if (length != 9) |
| 1016 | { |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1017 | png_warning(png_ptr, "Incorrect oFFs chunk length"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1018 | png_crc_finish(png_ptr, length); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1019 | return; |
| 1020 | } |
| 1021 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1022 | png_crc_read(png_ptr, buf, 9); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1023 | if (png_crc_finish(png_ptr, 0)) |
| 1024 | return; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1025 | |
| 1026 | offset_x = png_get_uint_32(buf); |
| 1027 | offset_y = png_get_uint_32(buf + 4); |
| 1028 | unit_type = buf[8]; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1029 | png_set_oFFs(png_ptr, info_ptr, offset_x, offset_y, unit_type); |
| 1030 | } |
| 1031 | #endif |
| 1032 | |
| 1033 | #if defined(PNG_READ_pCAL_SUPPORTED) |
| 1034 | /* read the pCAL chunk (png-scivis-19970203) */ |
| 1035 | void |
| 1036 | png_handle_pCAL(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) |
| 1037 | { |
| 1038 | png_charp purpose; |
| 1039 | png_int_32 X0, X1; |
| 1040 | png_byte type, nparams; |
| 1041 | png_charp buf, units, endptr; |
| 1042 | png_charpp params; |
Glenn Randers-Pehrson | a357b99 | 1998-02-08 20:56:40 -0600 | [diff] [blame] | 1043 | png_size_t slength; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1044 | int i; |
| 1045 | |
| 1046 | png_debug(1, "in png_handle_pCAL\n"); |
| 1047 | |
| 1048 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
| 1049 | png_error(png_ptr, "Missing IHDR before pCAL"); |
| 1050 | else if (png_ptr->mode & PNG_HAVE_IDAT) |
| 1051 | { |
| 1052 | png_warning(png_ptr, "Invalid pCAL after IDAT"); |
| 1053 | png_crc_finish(png_ptr, length); |
| 1054 | return; |
| 1055 | } |
| 1056 | else if (info_ptr != NULL && info_ptr->valid & PNG_INFO_pCAL) |
| 1057 | { |
| 1058 | png_warning(png_ptr, "Duplicate pCAL chunk"); |
| 1059 | png_crc_finish(png_ptr, length); |
| 1060 | return; |
| 1061 | } |
| 1062 | |
| 1063 | png_debug1(2, "Allocating and reading pCAL chunk data (%d bytes)\n", |
| 1064 | length + 1); |
| 1065 | purpose = (png_charp)png_malloc(png_ptr, length + 1); |
Glenn Randers-Pehrson | a357b99 | 1998-02-08 20:56:40 -0600 | [diff] [blame] | 1066 | slength = (png_size_t)length; |
| 1067 | png_crc_read(png_ptr, (png_bytep)purpose, slength); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1068 | |
| 1069 | if (png_crc_finish(png_ptr, 0)) |
| 1070 | { |
| 1071 | png_free(png_ptr, purpose); |
| 1072 | return; |
| 1073 | } |
| 1074 | |
Glenn Randers-Pehrson | a357b99 | 1998-02-08 20:56:40 -0600 | [diff] [blame] | 1075 | purpose[slength] = 0x00; /* null terminate the last string */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1076 | |
| 1077 | png_debug(3, "Finding end of pCAL purpose string\n"); |
| 1078 | for (buf = purpose; *buf != '\0'; buf++) |
| 1079 | /* empty loop */; |
| 1080 | |
Glenn Randers-Pehrson | a357b99 | 1998-02-08 20:56:40 -0600 | [diff] [blame] | 1081 | endptr = purpose + slength; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1082 | |
| 1083 | /* We need to have at least 12 bytes after the purpose string |
| 1084 | in order to get the parameter information. */ |
| 1085 | if (endptr <= buf + 12) |
| 1086 | { |
| 1087 | png_warning(png_ptr, "Invalid pCAL data"); |
| 1088 | png_free(png_ptr, purpose); |
| 1089 | return; |
| 1090 | } |
| 1091 | |
| 1092 | png_debug(3, "Reading pCAL X0, X1, type, nparams, and units\n"); |
| 1093 | X0 = png_get_int_32((png_bytep)buf+1); |
| 1094 | X1 = png_get_int_32((png_bytep)buf+5); |
| 1095 | type = buf[9]; |
| 1096 | nparams = buf[10]; |
| 1097 | units = buf + 11; |
| 1098 | |
| 1099 | png_debug(3, "Checking pCAL equation type and number of parameters\n"); |
| 1100 | /* Check that we have the right number of parameters for known |
| 1101 | equation types. */ |
| 1102 | if ((type == PNG_EQUATION_LINEAR && nparams != 2) || |
| 1103 | (type == PNG_EQUATION_BASE_E && nparams != 3) || |
| 1104 | (type == PNG_EQUATION_ARBITRARY && nparams != 3) || |
| 1105 | (type == PNG_EQUATION_HYPERBOLIC && nparams != 4)) |
| 1106 | { |
| 1107 | png_warning(png_ptr, "Invalid pCAL parameters for equation type"); |
| 1108 | png_free(png_ptr, purpose); |
| 1109 | return; |
| 1110 | } |
| 1111 | else if (type >= PNG_EQUATION_LAST) |
| 1112 | { |
| 1113 | png_warning(png_ptr, "Unrecognized equation type for pCAL chunk"); |
| 1114 | } |
| 1115 | |
| 1116 | /* Empty loop to move past the units string. */ |
Glenn Randers-Pehrson | a357b99 | 1998-02-08 20:56:40 -0600 | [diff] [blame] | 1117 | for (buf = units; *buf != 0x00; buf++); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1118 | |
| 1119 | png_debug(3, "Allocating pCAL parameters array\n"); |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame] | 1120 | params = (png_charpp)png_malloc(png_ptr, (png_uint_32)(nparams |
| 1121 | *sizeof(png_charp))) ; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1122 | |
| 1123 | /* Get pointers to the start of each parameter string. */ |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame] | 1124 | for (i = 0; i < (int)nparams; i++) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1125 | { |
| 1126 | buf++; /* Skip the null string terminator from previous parameter. */ |
| 1127 | |
| 1128 | png_debug1(3, "Reading pCAL parameter %d\n", i); |
| 1129 | /* Empty loop to move past each paramter string */ |
Glenn Randers-Pehrson | a357b99 | 1998-02-08 20:56:40 -0600 | [diff] [blame] | 1130 | for (params[i] = buf; *buf != 0x00 && buf <= endptr; buf++); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1131 | |
| 1132 | /* Make sure we haven't run out of data yet */ |
| 1133 | if (buf > endptr) |
| 1134 | { |
| 1135 | png_warning(png_ptr, "Invalid pCAL data"); |
| 1136 | png_free(png_ptr, purpose); |
| 1137 | png_free(png_ptr, params); |
| 1138 | return; |
| 1139 | } |
| 1140 | } |
| 1141 | |
| 1142 | png_set_pCAL(png_ptr, info_ptr, purpose, X0, X1, type, nparams, |
| 1143 | units, params); |
| 1144 | |
| 1145 | png_free(png_ptr, purpose); |
| 1146 | png_free(png_ptr, params); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1147 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1148 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1149 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1150 | #if defined(PNG_READ_tIME_SUPPORTED) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1151 | void |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1152 | png_handle_tIME(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1153 | { |
| 1154 | png_byte buf[7]; |
| 1155 | png_time mod_time; |
| 1156 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1157 | png_debug(1, "in png_handle_tIME\n"); |
| 1158 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1159 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1160 | png_error(png_ptr, "Out of place tIME chunk"); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1161 | else if (info_ptr != NULL && info_ptr->valid & PNG_INFO_tIME) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1162 | { |
| 1163 | png_warning(png_ptr, "Duplicate tIME chunk"); |
| 1164 | png_crc_finish(png_ptr, length); |
| 1165 | return; |
| 1166 | } |
| 1167 | |
| 1168 | if (png_ptr->mode & PNG_HAVE_IDAT) |
| 1169 | png_ptr->mode |= PNG_AFTER_IDAT; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1170 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1171 | if (length != 7) |
| 1172 | { |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1173 | png_warning(png_ptr, "Incorrect tIME chunk length"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1174 | png_crc_finish(png_ptr, length); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1175 | return; |
| 1176 | } |
| 1177 | |
| 1178 | png_crc_read(png_ptr, buf, 7); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1179 | if (png_crc_finish(png_ptr, 0)) |
| 1180 | return; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1181 | |
| 1182 | mod_time.second = buf[6]; |
| 1183 | mod_time.minute = buf[5]; |
| 1184 | mod_time.hour = buf[4]; |
| 1185 | mod_time.day = buf[3]; |
| 1186 | mod_time.month = buf[2]; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1187 | mod_time.year = png_get_uint_16(buf); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1188 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1189 | png_set_tIME(png_ptr, info_ptr, &mod_time); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1190 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1191 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1192 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1193 | #if defined(PNG_READ_tEXt_SUPPORTED) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1194 | /* Note: this does not properly handle chunks that are > 64K under DOS */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1195 | void |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1196 | png_handle_tEXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1197 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1198 | png_textp text_ptr; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1199 | png_charp key; |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 1200 | png_charp text; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1201 | png_uint_32 skip = 0; |
Glenn Randers-Pehrson | a357b99 | 1998-02-08 20:56:40 -0600 | [diff] [blame] | 1202 | png_size_t slength; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1203 | |
| 1204 | png_debug(1, "in png_handle_tEXt\n"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1205 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1206 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
| 1207 | png_error(png_ptr, "Missing IHDR before tEXt"); |
| 1208 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1209 | if (png_ptr->mode & PNG_HAVE_IDAT) |
| 1210 | png_ptr->mode |= PNG_AFTER_IDAT; |
| 1211 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1212 | #ifdef PNG_MAX_MALLOC_64K |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 1213 | if (length > (png_uint_32)65535L) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1214 | { |
| 1215 | png_warning(png_ptr, "tEXt chunk too large to fit in memory"); |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 1216 | skip = length - (png_uint_32)65535L; |
| 1217 | length = (png_uint_32)65535L; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1218 | } |
| 1219 | #endif |
| 1220 | |
| 1221 | key = (png_charp)png_malloc(png_ptr, length + 1); |
Glenn Randers-Pehrson | a357b99 | 1998-02-08 20:56:40 -0600 | [diff] [blame] | 1222 | slength = (png_size_t)length; |
| 1223 | png_crc_read(png_ptr, (png_bytep)key, slength); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1224 | |
| 1225 | if (png_crc_finish(png_ptr, skip)) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1226 | { |
| 1227 | png_free(png_ptr, key); |
| 1228 | return; |
| 1229 | } |
| 1230 | |
Glenn Randers-Pehrson | a357b99 | 1998-02-08 20:56:40 -0600 | [diff] [blame] | 1231 | key[slength] = 0x00; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1232 | |
| 1233 | for (text = key; *text; text++) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1234 | /* empty loop to find end of key */ ; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1235 | |
Glenn Randers-Pehrson | a357b99 | 1998-02-08 20:56:40 -0600 | [diff] [blame] | 1236 | if (text != key + slength) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1237 | text++; |
| 1238 | |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame] | 1239 | text_ptr = (png_textp)png_malloc(png_ptr, (png_uint_32)sizeof(png_text)); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1240 | text_ptr->compression = PNG_TEXT_COMPRESSION_NONE; |
| 1241 | text_ptr->key = key; |
| 1242 | text_ptr->text = text; |
| 1243 | |
| 1244 | png_set_text(png_ptr, info_ptr, text_ptr, 1); |
| 1245 | |
| 1246 | png_free(png_ptr, text_ptr); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1247 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1248 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1249 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1250 | #if defined(PNG_READ_zTXt_SUPPORTED) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1251 | /* note: this does not correctly handle chunks that are > 64K under DOS */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1252 | void |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1253 | png_handle_zTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1254 | { |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1255 | static char msg[] = "Error decoding zTXt chunk"; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1256 | png_textp text_ptr; |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 1257 | png_charp key; |
| 1258 | png_charp text; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1259 | int comp_type = PNG_TEXT_COMPRESSION_NONE; |
Glenn Randers-Pehrson | a357b99 | 1998-02-08 20:56:40 -0600 | [diff] [blame] | 1260 | png_size_t slength; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1261 | |
| 1262 | png_debug(1, "in png_handle_zTXt\n"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1263 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1264 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
| 1265 | png_error(png_ptr, "Missing IHDR before zTXt"); |
| 1266 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1267 | if (png_ptr->mode & PNG_HAVE_IDAT) |
| 1268 | png_ptr->mode |= PNG_AFTER_IDAT; |
| 1269 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1270 | #ifdef PNG_MAX_MALLOC_64K |
| 1271 | /* We will no doubt have problems with chunks even half this size, but |
| 1272 | there is no hard and fast rule to tell us where to stop. */ |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 1273 | if (length > (png_uint_32)65535L) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1274 | { |
| 1275 | png_warning(png_ptr,"zTXt chunk too large to fit in memory"); |
| 1276 | png_crc_finish(png_ptr, length); |
| 1277 | return; |
| 1278 | } |
| 1279 | #endif |
| 1280 | |
| 1281 | key = (png_charp)png_malloc(png_ptr, length + 1); |
Glenn Randers-Pehrson | a357b99 | 1998-02-08 20:56:40 -0600 | [diff] [blame] | 1282 | slength = (png_size_t)length; |
| 1283 | png_crc_read(png_ptr, (png_bytep)key, slength); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1284 | if (png_crc_finish(png_ptr, 0)) |
| 1285 | { |
| 1286 | png_free(png_ptr, key); |
| 1287 | return; |
| 1288 | } |
| 1289 | |
Glenn Randers-Pehrson | a357b99 | 1998-02-08 20:56:40 -0600 | [diff] [blame] | 1290 | key[slength] = 0x00; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1291 | |
| 1292 | for (text = key; *text; text++) |
| 1293 | /* empty loop */ ; |
| 1294 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1295 | /* zTXt must have some text after the keyword */ |
Glenn Randers-Pehrson | a357b99 | 1998-02-08 20:56:40 -0600 | [diff] [blame] | 1296 | if (text == key + slength) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1297 | { |
Guy Schalnat | 69b1448 | 1996-01-10 02:56:49 -0600 | [diff] [blame] | 1298 | png_warning(png_ptr, "Zero length zTXt chunk"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1299 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1300 | else if ((comp_type = *(++text)) == PNG_TEXT_COMPRESSION_zTXt) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1301 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1302 | png_size_t text_size, key_size; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1303 | text++; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1304 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1305 | png_ptr->zstream.next_in = (png_bytep)text; |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1306 | png_ptr->zstream.avail_in = (uInt)(length - (text - key)); |
| 1307 | png_ptr->zstream.next_out = png_ptr->zbuf; |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 1308 | png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1309 | |
Glenn Randers-Pehrson | a357b99 | 1998-02-08 20:56:40 -0600 | [diff] [blame] | 1310 | key_size = (png_size_t)(text - key); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1311 | text_size = 0; |
| 1312 | text = NULL; |
| 1313 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1314 | while (png_ptr->zstream.avail_in) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1315 | { |
| 1316 | int ret; |
| 1317 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1318 | ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1319 | if (ret != Z_OK && ret != Z_STREAM_END) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1320 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1321 | if (png_ptr->zstream.msg != NULL) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1322 | png_warning(png_ptr, png_ptr->zstream.msg); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1323 | else |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1324 | png_warning(png_ptr, msg); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1325 | inflateReset(&png_ptr->zstream); |
| 1326 | png_ptr->zstream.avail_in = 0; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1327 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1328 | if (text == NULL) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1329 | { |
| 1330 | text_size = key_size + sizeof(msg) + 1; |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame] | 1331 | text = (png_charp)png_malloc(png_ptr, (png_uint_32)text_size); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1332 | png_memcpy(text, key, key_size); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1333 | } |
| 1334 | |
Glenn Randers-Pehrson | a357b99 | 1998-02-08 20:56:40 -0600 | [diff] [blame] | 1335 | text[text_size - 1] = 0x00; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1336 | |
| 1337 | /* Copy what we can of the error message into the text chunk */ |
Glenn Randers-Pehrson | a357b99 | 1998-02-08 20:56:40 -0600 | [diff] [blame] | 1338 | text_size = (png_size_t)(slength - (text - key) - 1); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1339 | text_size = sizeof(msg) > text_size ? text_size : sizeof(msg); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1340 | png_memcpy(text + key_size, msg, text_size + 1); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1341 | break; |
| 1342 | } |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1343 | if (!png_ptr->zstream.avail_out || ret == Z_STREAM_END) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1344 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1345 | if (text == NULL) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1346 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1347 | text = (png_charp)png_malloc(png_ptr, |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame] | 1348 | (png_uint_32)(png_ptr->zbuf_size - png_ptr->zstream.avail_out |
| 1349 | + key_size + 1)); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1350 | png_memcpy(text + key_size, png_ptr->zbuf, |
| 1351 | png_ptr->zbuf_size - png_ptr->zstream.avail_out); |
| 1352 | png_memcpy(text, key, key_size); |
| 1353 | text_size = key_size + png_ptr->zbuf_size - |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1354 | png_ptr->zstream.avail_out; |
Glenn Randers-Pehrson | a357b99 | 1998-02-08 20:56:40 -0600 | [diff] [blame] | 1355 | *(text + text_size) = 0x00; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1356 | } |
| 1357 | else |
| 1358 | { |
| 1359 | png_charp tmp; |
| 1360 | |
| 1361 | tmp = text; |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame] | 1362 | text = (png_charp)png_malloc(png_ptr, (png_uint_32)(text_size + |
| 1363 | png_ptr->zbuf_size - png_ptr->zstream.avail_out + 1)); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1364 | png_memcpy(text, tmp, text_size); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1365 | png_free(png_ptr, tmp); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1366 | png_memcpy(text + text_size, png_ptr->zbuf, |
| 1367 | (png_ptr->zbuf_size - png_ptr->zstream.avail_out)); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1368 | text_size += png_ptr->zbuf_size - png_ptr->zstream.avail_out; |
Glenn Randers-Pehrson | a357b99 | 1998-02-08 20:56:40 -0600 | [diff] [blame] | 1369 | *(text + text_size) = 0x00; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1370 | } |
| 1371 | if (ret != Z_STREAM_END) |
| 1372 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1373 | png_ptr->zstream.next_out = png_ptr->zbuf; |
| 1374 | png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1375 | } |
| 1376 | else |
| 1377 | { |
| 1378 | break; |
| 1379 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1380 | } |
| 1381 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1382 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1383 | inflateReset(&png_ptr->zstream); |
| 1384 | png_ptr->zstream.avail_in = 0; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1385 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1386 | png_free(png_ptr, key); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1387 | key = text; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1388 | text += key_size; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1389 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1390 | else /* if (comp_type >= PNG_TEXT_COMPRESSION_LAST) */ |
| 1391 | { |
| 1392 | png_size_t text_size; |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 1393 | #if !defined(PNG_NO_STDIO) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1394 | char umsg[50]; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1395 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1396 | sprintf(umsg, "Unknown zTXt compression type %d", comp_type); |
| 1397 | png_warning(png_ptr, umsg); |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 1398 | #else |
| 1399 | png_warning(png_ptr, "Unknown zTXt compression type"); |
| 1400 | #endif |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1401 | |
| 1402 | /* Copy what we can of the error message into the text chunk */ |
Glenn Randers-Pehrson | a357b99 | 1998-02-08 20:56:40 -0600 | [diff] [blame] | 1403 | text_size = (png_size_t)(slength - (text - key) - 1); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1404 | text_size = sizeof(msg) > text_size ? text_size : sizeof(msg); |
| 1405 | png_memcpy(text, msg, text_size + 1); |
| 1406 | } |
| 1407 | |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame] | 1408 | text_ptr = (png_textp)png_malloc(png_ptr, (png_uint_32)sizeof(png_text)); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1409 | text_ptr->compression = comp_type; |
| 1410 | text_ptr->key = key; |
| 1411 | text_ptr->text = text; |
| 1412 | |
| 1413 | png_set_text(png_ptr, info_ptr, text_ptr, 1); |
| 1414 | |
| 1415 | png_free(png_ptr, text_ptr); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1416 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1417 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1418 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1419 | /* This function is called when we haven't found a handler for a |
| 1420 | chunk. If there isn't a problem with the chunk itself (ie bad |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1421 | chunk name, CRC, or a critical chunk), the chunk is silently ignored. */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1422 | void |
| 1423 | png_handle_unknown(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) |
| 1424 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1425 | png_debug(1, "in png_handle_unknown\n"); |
| 1426 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1427 | /* In the future we can have code here that calls user-supplied |
| 1428 | * callback functions for unknown chunks before they are ignored or |
| 1429 | * cause an error. |
| 1430 | */ |
| 1431 | png_check_chunk_name(png_ptr, png_ptr->chunk_name); |
| 1432 | |
| 1433 | if (!(png_ptr->chunk_name[0] & 0x20)) |
| 1434 | { |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 1435 | png_chunk_error(png_ptr, "unknown critical chunk"); |
Glenn Randers-Pehrson | a357b99 | 1998-02-08 20:56:40 -0600 | [diff] [blame] | 1436 | |
Glenn Randers-Pehrson | cbe52d8 | 1998-02-28 07:00:24 -0600 | [diff] [blame] | 1437 | /* to quiet compiler warnings about unused info_ptr */ |
Glenn Randers-Pehrson | a357b99 | 1998-02-08 20:56:40 -0600 | [diff] [blame] | 1438 | if (info_ptr == NULL) |
| 1439 | return; |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1440 | } |
| 1441 | |
| 1442 | if (png_ptr->mode & PNG_HAVE_IDAT) |
| 1443 | png_ptr->mode |= PNG_AFTER_IDAT; |
| 1444 | |
| 1445 | png_crc_finish(png_ptr, length); |
Glenn Randers-Pehrson | a357b99 | 1998-02-08 20:56:40 -0600 | [diff] [blame] | 1446 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1447 | } |
| 1448 | |
| 1449 | /* This function is called to verify that a chunk name is valid. |
| 1450 | This function can't have the "critical chunk check" incorporated |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1451 | into it, since in the future we will need to be able to call user |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1452 | functions to handle unknown critical chunks after we check that |
| 1453 | the chunk name itself is valid. */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1454 | |
| 1455 | #define isnonalpha(c) ((c) < 41 || (c) > 122 || ((c) > 90 && (c) < 97)) |
| 1456 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1457 | void |
| 1458 | png_check_chunk_name(png_structp png_ptr, png_bytep chunk_name) |
| 1459 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1460 | png_debug(1, "in png_check_chunk_name\n"); |
| 1461 | if (isnonalpha(chunk_name[0]) || isnonalpha(chunk_name[1]) || |
| 1462 | isnonalpha(chunk_name[2]) || isnonalpha(chunk_name[3])) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1463 | { |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 1464 | png_chunk_error(png_ptr, "invalid chunk type"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1465 | } |
| 1466 | } |
| 1467 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1468 | /* Combines the row recently read in with the previous row. |
| 1469 | This routine takes care of alpha and transparency if requested. |
| 1470 | This routine also handles the two methods of progressive display |
| 1471 | of interlaced images, depending on the mask value. |
| 1472 | The mask value describes which pixels are to be combined with |
| 1473 | the row. The pattern always repeats every 8 pixels, so just 8 |
| 1474 | bits are needed. A one indicates the pixels is to be combined, |
| 1475 | a zero indicates the pixel is to be skipped. This is in addition |
| 1476 | to any alpha or transparency value associated with the pixel. If |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1477 | you want all pixels to be combined, pass 0xff (255) in mask. */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1478 | void |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 1479 | png_combine_row(png_structp png_ptr, png_bytep row, |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1480 | int mask) |
| 1481 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1482 | png_debug(1,"in png_combine_row\n"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1483 | if (mask == 0xff) |
| 1484 | { |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1485 | png_memcpy(row, png_ptr->row_buf + 1, |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1486 | (png_size_t)((png_ptr->width * |
| 1487 | png_ptr->row_info.pixel_depth + 7) >> 3)); |
| 1488 | } |
| 1489 | else |
| 1490 | { |
| 1491 | switch (png_ptr->row_info.pixel_depth) |
| 1492 | { |
| 1493 | case 1: |
| 1494 | { |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 1495 | png_bytep sp; |
| 1496 | png_bytep dp; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1497 | int s_inc, s_start, s_end; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1498 | int m; |
| 1499 | int shift; |
| 1500 | png_uint_32 i; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1501 | |
| 1502 | sp = png_ptr->row_buf + 1; |
| 1503 | dp = row; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1504 | m = 0x80; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1505 | #if defined(PNG_READ_PACKSWAP_SUPPORTED) |
| 1506 | if (png_ptr->transformations & PNG_PACKSWAP) |
| 1507 | { |
| 1508 | s_start = 0; |
| 1509 | s_end = 7; |
| 1510 | s_inc = 1; |
| 1511 | } |
| 1512 | else |
| 1513 | #endif |
| 1514 | { |
| 1515 | s_start = 7; |
| 1516 | s_end = 0; |
| 1517 | s_inc = -1; |
| 1518 | } |
| 1519 | |
| 1520 | shift = s_start; |
| 1521 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1522 | for (i = 0; i < png_ptr->width; i++) |
| 1523 | { |
| 1524 | if (m & mask) |
| 1525 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1526 | int value; |
| 1527 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1528 | value = (*sp >> shift) & 0x1; |
| 1529 | *dp &= (png_byte)((0x7f7f >> (7 - shift)) & 0xff); |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1530 | *dp |= (png_byte)(value << shift); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1531 | } |
| 1532 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1533 | if (shift == s_end) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1534 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1535 | shift = s_start; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1536 | sp++; |
| 1537 | dp++; |
| 1538 | } |
| 1539 | else |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1540 | shift += s_inc; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1541 | |
| 1542 | if (m == 1) |
| 1543 | m = 0x80; |
| 1544 | else |
| 1545 | m >>= 1; |
| 1546 | } |
| 1547 | break; |
| 1548 | } |
| 1549 | case 2: |
| 1550 | { |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 1551 | png_bytep sp; |
| 1552 | png_bytep dp; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1553 | int s_start, s_end, s_inc; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1554 | int m; |
| 1555 | int shift; |
| 1556 | png_uint_32 i; |
| 1557 | int value; |
| 1558 | |
| 1559 | sp = png_ptr->row_buf + 1; |
| 1560 | dp = row; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1561 | m = 0x80; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1562 | #if defined(PNG_READ_PACKSWAP_SUPPORTED) |
| 1563 | if (png_ptr->transformations & PNG_PACKSWAP) |
| 1564 | { |
| 1565 | s_start = 0; |
| 1566 | s_end = 6; |
| 1567 | s_inc = 2; |
| 1568 | } |
| 1569 | else |
| 1570 | #endif |
| 1571 | { |
| 1572 | s_start = 6; |
| 1573 | s_end = 0; |
| 1574 | s_inc = -2; |
| 1575 | } |
| 1576 | |
| 1577 | shift = s_start; |
| 1578 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1579 | for (i = 0; i < png_ptr->width; i++) |
| 1580 | { |
| 1581 | if (m & mask) |
| 1582 | { |
| 1583 | value = (*sp >> shift) & 0x3; |
| 1584 | *dp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff); |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1585 | *dp |= (png_byte)(value << shift); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1586 | } |
| 1587 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1588 | if (shift == s_end) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1589 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1590 | shift = s_start; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1591 | sp++; |
| 1592 | dp++; |
| 1593 | } |
| 1594 | else |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1595 | shift += s_inc; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1596 | if (m == 1) |
| 1597 | m = 0x80; |
| 1598 | else |
| 1599 | m >>= 1; |
| 1600 | } |
| 1601 | break; |
| 1602 | } |
| 1603 | case 4: |
| 1604 | { |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 1605 | png_bytep sp; |
| 1606 | png_bytep dp; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1607 | int s_start, s_end, s_inc; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1608 | int m; |
| 1609 | int shift; |
| 1610 | png_uint_32 i; |
| 1611 | int value; |
| 1612 | |
| 1613 | sp = png_ptr->row_buf + 1; |
| 1614 | dp = row; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1615 | m = 0x80; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1616 | #if defined(PNG_READ_PACKSWAP_SUPPORTED) |
| 1617 | if (png_ptr->transformations & PNG_PACKSWAP) |
| 1618 | { |
| 1619 | s_start = 0; |
| 1620 | s_end = 4; |
| 1621 | s_inc = 4; |
| 1622 | } |
| 1623 | else |
| 1624 | #endif |
| 1625 | { |
| 1626 | s_start = 4; |
| 1627 | s_end = 0; |
| 1628 | s_inc = -4; |
| 1629 | } |
| 1630 | shift = s_start; |
| 1631 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1632 | for (i = 0; i < png_ptr->width; i++) |
| 1633 | { |
| 1634 | if (m & mask) |
| 1635 | { |
| 1636 | value = (*sp >> shift) & 0xf; |
| 1637 | *dp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff); |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1638 | *dp |= (png_byte)(value << shift); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1639 | } |
| 1640 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1641 | if (shift == s_end) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1642 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1643 | shift = s_start; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1644 | sp++; |
| 1645 | dp++; |
| 1646 | } |
| 1647 | else |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1648 | shift += s_inc; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1649 | if (m == 1) |
| 1650 | m = 0x80; |
| 1651 | else |
| 1652 | m >>= 1; |
| 1653 | } |
| 1654 | break; |
| 1655 | } |
| 1656 | default: |
| 1657 | { |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 1658 | png_bytep sp; |
| 1659 | png_bytep dp; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1660 | png_size_t pixel_bytes; |
| 1661 | png_uint_32 i; |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1662 | png_byte m; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1663 | |
| 1664 | pixel_bytes = (png_ptr->row_info.pixel_depth >> 3); |
| 1665 | |
| 1666 | sp = png_ptr->row_buf + 1; |
| 1667 | dp = row; |
| 1668 | m = 0x80; |
| 1669 | for (i = 0; i < png_ptr->width; i++) |
| 1670 | { |
| 1671 | if (m & mask) |
| 1672 | { |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1673 | png_memcpy(dp, sp, pixel_bytes); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1674 | } |
| 1675 | |
| 1676 | sp += pixel_bytes; |
| 1677 | dp += pixel_bytes; |
| 1678 | |
| 1679 | if (m == 1) |
| 1680 | m = 0x80; |
| 1681 | else |
| 1682 | m >>= 1; |
| 1683 | } |
| 1684 | break; |
| 1685 | } |
| 1686 | } |
| 1687 | } |
| 1688 | } |
| 1689 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1690 | #if defined(PNG_READ_INTERLACING_SUPPORTED) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1691 | void |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1692 | png_do_read_interlace(png_row_infop row_info, png_bytep row, int pass, |
| 1693 | png_uint_32 transformations) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1694 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1695 | png_debug(1,"in png_do_read_interlace\n"); |
| 1696 | if (row != NULL && row_info != NULL) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1697 | { |
| 1698 | png_uint_32 final_width; |
| 1699 | |
| 1700 | final_width = row_info->width * png_pass_inc[pass]; |
| 1701 | |
| 1702 | switch (row_info->pixel_depth) |
| 1703 | { |
| 1704 | case 1: |
| 1705 | { |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 1706 | png_bytep sp, dp; |
Glenn Randers-Pehrson | 983ec16 | 1998-03-07 11:24:03 -0600 | [diff] [blame] | 1707 | int sshift, dshift; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1708 | int s_start, s_end, s_inc; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1709 | png_byte v; |
| 1710 | png_uint_32 i; |
| 1711 | int j; |
| 1712 | |
| 1713 | sp = row + (png_size_t)((row_info->width - 1) >> 3); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1714 | dp = row + (png_size_t)((final_width - 1) >> 3); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1715 | #if defined(PNG_READ_PACKSWAP_SUPPORTED) |
| 1716 | if (transformations & PNG_PACKSWAP) |
| 1717 | { |
Glenn Randers-Pehrson | 983ec16 | 1998-03-07 11:24:03 -0600 | [diff] [blame] | 1718 | sshift = (int)((row_info->width + 7) & 7); |
| 1719 | dshift = (int)((final_width + 7) & 7); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1720 | s_start = 7; |
| 1721 | s_end = 0; |
| 1722 | s_inc = -1; |
| 1723 | } |
| 1724 | else |
| 1725 | #endif |
| 1726 | { |
Glenn Randers-Pehrson | 983ec16 | 1998-03-07 11:24:03 -0600 | [diff] [blame] | 1727 | sshift = 7 - (int)((row_info->width + 7) & 7); |
| 1728 | dshift = 7 - (int)((final_width + 7) & 7); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1729 | s_start = 0; |
| 1730 | s_end = 7; |
| 1731 | s_inc = 1; |
| 1732 | } |
| 1733 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1734 | for (i = row_info->width; i; i--) |
| 1735 | { |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1736 | v = (png_byte)((*sp >> sshift) & 0x1); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1737 | for (j = 0; j < png_pass_inc[pass]; j++) |
| 1738 | { |
| 1739 | *dp &= (png_byte)((0x7f7f >> (7 - dshift)) & 0xff); |
| 1740 | *dp |= (png_byte)(v << dshift); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1741 | if (dshift == s_end) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1742 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1743 | dshift = s_start; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1744 | dp--; |
| 1745 | } |
| 1746 | else |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1747 | dshift += s_inc; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1748 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1749 | if (sshift == s_end) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1750 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1751 | sshift = s_start; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1752 | sp--; |
| 1753 | } |
| 1754 | else |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1755 | sshift += s_inc; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1756 | } |
| 1757 | break; |
| 1758 | } |
| 1759 | case 2: |
| 1760 | { |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 1761 | png_bytep sp, dp; |
Glenn Randers-Pehrson | 983ec16 | 1998-03-07 11:24:03 -0600 | [diff] [blame] | 1762 | int sshift, dshift; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1763 | int s_start, s_end, s_inc; |
| 1764 | png_uint_32 i; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1765 | |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 1766 | sp = row + (png_uint_32)((row_info->width - 1) >> 2); |
| 1767 | dp = row + (png_uint_32)((final_width - 1) >> 2); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1768 | #if defined(PNG_READ_PACKSWAP_SUPPORTED) |
| 1769 | if (transformations & PNG_PACKSWAP) |
| 1770 | { |
Glenn Randers-Pehrson | 983ec16 | 1998-03-07 11:24:03 -0600 | [diff] [blame] | 1771 | sshift = (int)(((row_info->width + 3) & 3) << 1); |
| 1772 | dshift = (int)(((final_width + 3) & 3) << 1); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1773 | s_start = 6; |
| 1774 | s_end = 0; |
| 1775 | s_inc = -2; |
| 1776 | } |
| 1777 | else |
| 1778 | #endif |
| 1779 | { |
Glenn Randers-Pehrson | 983ec16 | 1998-03-07 11:24:03 -0600 | [diff] [blame] | 1780 | sshift = (int)((3 - ((row_info->width + 3) & 3)) << 1); |
| 1781 | dshift = (int)((3 - ((final_width + 3) & 3)) << 1); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1782 | s_start = 0; |
| 1783 | s_end = 6; |
| 1784 | s_inc = 2; |
| 1785 | } |
| 1786 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1787 | for (i = row_info->width; i; i--) |
| 1788 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1789 | png_byte v; |
| 1790 | int j; |
| 1791 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1792 | v = (png_byte)((*sp >> sshift) & 0x3); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1793 | for (j = 0; j < png_pass_inc[pass]; j++) |
| 1794 | { |
| 1795 | *dp &= (png_byte)((0x3f3f >> (6 - dshift)) & 0xff); |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1796 | *dp |= (png_byte)(v << dshift); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1797 | if (dshift == s_end) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1798 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1799 | dshift = s_start; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1800 | dp--; |
| 1801 | } |
| 1802 | else |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1803 | dshift += s_inc; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1804 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1805 | if (sshift == s_end) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1806 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1807 | sshift = s_start; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1808 | sp--; |
| 1809 | } |
| 1810 | else |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1811 | sshift += s_inc; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1812 | } |
| 1813 | break; |
| 1814 | } |
| 1815 | case 4: |
| 1816 | { |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 1817 | png_bytep sp, dp; |
Glenn Randers-Pehrson | 983ec16 | 1998-03-07 11:24:03 -0600 | [diff] [blame] | 1818 | int sshift, dshift; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1819 | int s_start, s_end, s_inc; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1820 | png_uint_32 i; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1821 | |
| 1822 | sp = row + (png_size_t)((row_info->width - 1) >> 1); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1823 | dp = row + (png_size_t)((final_width - 1) >> 1); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1824 | #if defined(PNG_READ_PACKSWAP_SUPPORTED) |
| 1825 | if (transformations & PNG_PACKSWAP) |
| 1826 | { |
Glenn Randers-Pehrson | 983ec16 | 1998-03-07 11:24:03 -0600 | [diff] [blame] | 1827 | sshift = (int)(((row_info->width + 1) & 1) << 2); |
| 1828 | dshift = (int)(((final_width + 1) & 1) << 2); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1829 | s_start = 4; |
| 1830 | s_end = 0; |
| 1831 | s_inc = -4; |
| 1832 | } |
| 1833 | else |
| 1834 | #endif |
| 1835 | { |
Glenn Randers-Pehrson | 983ec16 | 1998-03-07 11:24:03 -0600 | [diff] [blame] | 1836 | sshift = (int)((1 - ((row_info->width + 1) & 1)) << 2); |
| 1837 | dshift = (int)((1 - ((final_width + 1) & 1)) << 2); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1838 | s_start = 0; |
| 1839 | s_end = 4; |
| 1840 | s_inc = 4; |
| 1841 | } |
| 1842 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1843 | for (i = row_info->width; i; i--) |
| 1844 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1845 | png_byte v; |
| 1846 | int j; |
| 1847 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1848 | v = (png_byte)((*sp >> sshift) & 0xf); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1849 | for (j = 0; j < png_pass_inc[pass]; j++) |
| 1850 | { |
| 1851 | *dp &= (png_byte)((0xf0f >> (4 - dshift)) & 0xff); |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 1852 | *dp |= (png_byte)(v << dshift); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1853 | if (dshift == s_end) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1854 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1855 | dshift = s_start; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1856 | dp--; |
| 1857 | } |
| 1858 | else |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1859 | dshift += s_inc; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1860 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1861 | if (sshift == s_end) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1862 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1863 | sshift = s_start; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1864 | sp--; |
| 1865 | } |
| 1866 | else |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1867 | sshift += s_inc; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1868 | } |
| 1869 | break; |
| 1870 | } |
| 1871 | default: |
| 1872 | { |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 1873 | png_bytep sp, dp; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1874 | png_uint_32 i; |
| 1875 | png_size_t pixel_bytes; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1876 | |
| 1877 | pixel_bytes = (row_info->pixel_depth >> 3); |
| 1878 | |
Glenn Randers-Pehrson | a357b99 | 1998-02-08 20:56:40 -0600 | [diff] [blame] | 1879 | sp = row + (png_size_t)(row_info->width - 1) * pixel_bytes; |
| 1880 | dp = row + (png_size_t)(final_width - 1) * pixel_bytes; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1881 | for (i = row_info->width; i; i--) |
| 1882 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1883 | png_byte v[8]; |
| 1884 | int j; |
| 1885 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1886 | png_memcpy(v, sp, pixel_bytes); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1887 | for (j = 0; j < png_pass_inc[pass]; j++) |
| 1888 | { |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1889 | png_memcpy(dp, v, pixel_bytes); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1890 | dp -= pixel_bytes; |
| 1891 | } |
| 1892 | sp -= pixel_bytes; |
| 1893 | } |
| 1894 | break; |
| 1895 | } |
| 1896 | } |
| 1897 | row_info->width = final_width; |
| 1898 | row_info->rowbytes = ((final_width * |
| 1899 | (png_uint_32)row_info->pixel_depth + 7) >> 3); |
| 1900 | } |
| 1901 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1902 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1903 | |
| 1904 | void |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1905 | png_read_filter_row(png_structp png_ptr, png_row_infop row_info, png_bytep row, |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 1906 | png_bytep prev_row, int filter) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1907 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1908 | png_debug(1, "in png_read_filter_row\n"); |
| 1909 | png_debug2(2,"row = %d, filter = %d\n", png_ptr->row_number, filter); |
| 1910 | |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame] | 1911 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1912 | switch (filter) |
| 1913 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1914 | case PNG_FILTER_VALUE_NONE: |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1915 | break; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1916 | case PNG_FILTER_VALUE_SUB: |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1917 | { |
| 1918 | png_uint_32 i; |
| 1919 | int bpp; |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 1920 | png_bytep rp; |
| 1921 | png_bytep lp; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1922 | |
| 1923 | bpp = (row_info->pixel_depth + 7) / 8; |
| 1924 | for (i = (png_uint_32)bpp, rp = row + bpp, lp = row; |
| 1925 | i < row_info->rowbytes; i++, rp++, lp++) |
| 1926 | { |
| 1927 | *rp = (png_byte)(((int)(*rp) + (int)(*lp)) & 0xff); |
| 1928 | } |
| 1929 | break; |
| 1930 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1931 | case PNG_FILTER_VALUE_UP: |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1932 | { |
| 1933 | png_uint_32 i; |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 1934 | png_bytep rp; |
| 1935 | png_bytep pp; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1936 | |
| 1937 | for (i = 0, rp = row, pp = prev_row; |
| 1938 | i < row_info->rowbytes; i++, rp++, pp++) |
| 1939 | { |
| 1940 | *rp = (png_byte)(((int)(*rp) + (int)(*pp)) & 0xff); |
| 1941 | } |
| 1942 | break; |
| 1943 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1944 | case PNG_FILTER_VALUE_AVG: |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1945 | { |
| 1946 | png_uint_32 i; |
| 1947 | int bpp; |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 1948 | png_bytep rp; |
| 1949 | png_bytep pp; |
| 1950 | png_bytep lp; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1951 | |
| 1952 | bpp = (row_info->pixel_depth + 7) / 8; |
| 1953 | for (i = 0, rp = row, pp = prev_row; |
| 1954 | i < (png_uint_32)bpp; i++, rp++, pp++) |
| 1955 | { |
| 1956 | *rp = (png_byte)(((int)(*rp) + |
| 1957 | ((int)(*pp) / 2)) & 0xff); |
| 1958 | } |
| 1959 | for (lp = row; i < row_info->rowbytes; i++, rp++, lp++, pp++) |
| 1960 | { |
| 1961 | *rp = (png_byte)(((int)(*rp) + |
| 1962 | (int)(*pp + *lp) / 2) & 0xff); |
| 1963 | } |
| 1964 | break; |
| 1965 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1966 | case PNG_FILTER_VALUE_PAETH: |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1967 | { |
| 1968 | int bpp; |
| 1969 | png_uint_32 i; |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 1970 | png_bytep rp; |
| 1971 | png_bytep pp; |
| 1972 | png_bytep lp; |
| 1973 | png_bytep cp; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1974 | |
| 1975 | bpp = (row_info->pixel_depth + 7) / 8; |
| 1976 | for (i = 0, rp = row, pp = prev_row, |
| 1977 | lp = row - bpp, cp = prev_row - bpp; |
| 1978 | i < row_info->rowbytes; i++, rp++, pp++, lp++, cp++) |
| 1979 | { |
| 1980 | int a, b, c, pa, pb, pc, p; |
| 1981 | |
| 1982 | b = *pp; |
| 1983 | if (i >= (png_uint_32)bpp) |
| 1984 | { |
| 1985 | c = *cp; |
| 1986 | a = *lp; |
| 1987 | } |
| 1988 | else |
| 1989 | { |
| 1990 | a = c = 0; |
| 1991 | } |
| 1992 | p = a + b - c; |
| 1993 | pa = abs(p - a); |
| 1994 | pb = abs(p - b); |
| 1995 | pc = abs(p - c); |
| 1996 | |
| 1997 | if (pa <= pb && pa <= pc) |
| 1998 | p = a; |
| 1999 | else if (pb <= pc) |
| 2000 | p = b; |
| 2001 | else |
| 2002 | p = c; |
| 2003 | |
| 2004 | *rp = (png_byte)(((int)(*rp) + p) & 0xff); |
| 2005 | } |
| 2006 | break; |
| 2007 | } |
| 2008 | default: |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2009 | png_error(png_ptr, "Bad adaptive filter type"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2010 | break; |
| 2011 | } |
| 2012 | } |
| 2013 | |
| 2014 | void |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 2015 | png_read_finish_row(png_structp png_ptr) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2016 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2017 | png_debug(1, "in png_read_finish_row\n"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2018 | png_ptr->row_number++; |
| 2019 | if (png_ptr->row_number < png_ptr->num_rows) |
| 2020 | return; |
| 2021 | |
| 2022 | if (png_ptr->interlaced) |
| 2023 | { |
| 2024 | png_ptr->row_number = 0; |
Glenn Randers-Pehrson | a357b99 | 1998-02-08 20:56:40 -0600 | [diff] [blame] | 2025 | png_memset_check(png_ptr, png_ptr->prev_row, 0, png_ptr->rowbytes + 1); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2026 | do |
| 2027 | { |
| 2028 | png_ptr->pass++; |
| 2029 | if (png_ptr->pass >= 7) |
| 2030 | break; |
| 2031 | png_ptr->iwidth = (png_ptr->width + |
| 2032 | png_pass_inc[png_ptr->pass] - 1 - |
| 2033 | png_pass_start[png_ptr->pass]) / |
| 2034 | png_pass_inc[png_ptr->pass]; |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame] | 2035 | png_ptr->irowbytes = ((png_ptr->iwidth * |
| 2036 | (png_uint_32)png_ptr->pixel_depth + 7) >> 3) +1; |
| 2037 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2038 | if (!(png_ptr->transformations & PNG_INTERLACE)) |
| 2039 | { |
| 2040 | png_ptr->num_rows = (png_ptr->height + |
| 2041 | png_pass_yinc[png_ptr->pass] - 1 - |
| 2042 | png_pass_ystart[png_ptr->pass]) / |
| 2043 | png_pass_yinc[png_ptr->pass]; |
| 2044 | if (!(png_ptr->num_rows)) |
| 2045 | continue; |
| 2046 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 2047 | if (png_ptr->transformations & PNG_INTERLACE) |
| 2048 | break; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2049 | } while (png_ptr->iwidth == 0); |
| 2050 | |
| 2051 | if (png_ptr->pass < 7) |
| 2052 | return; |
| 2053 | } |
| 2054 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2055 | if (!(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED)) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2056 | { |
| 2057 | char extra; |
| 2058 | int ret; |
| 2059 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2060 | png_ptr->zstream.next_out = (Byte *)&extra; |
| 2061 | png_ptr->zstream.avail_out = (uInt)1; |
Glenn Randers-Pehrson | ea3bcd7 | 1998-03-07 14:33:00 -0600 | [diff] [blame] | 2062 | for(;;) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2063 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2064 | if (!(png_ptr->zstream.avail_in)) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2065 | { |
| 2066 | while (!png_ptr->idat_size) |
| 2067 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2068 | png_byte chunk_length[4]; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2069 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2070 | png_crc_finish(png_ptr, 0); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2071 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2072 | png_read_data(png_ptr, chunk_length, 4); |
| 2073 | png_ptr->idat_size = png_get_uint_32(chunk_length); |
| 2074 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2075 | png_reset_crc(png_ptr); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2076 | png_crc_read(png_ptr, png_ptr->chunk_name, 4); |
| 2077 | if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4)) |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 2078 | png_error(png_ptr, "Not enough image data"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2079 | |
| 2080 | } |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2081 | png_ptr->zstream.avail_in = (uInt)png_ptr->zbuf_size; |
| 2082 | png_ptr->zstream.next_in = png_ptr->zbuf; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2083 | if (png_ptr->zbuf_size > png_ptr->idat_size) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2084 | png_ptr->zstream.avail_in = (uInt)png_ptr->idat_size; |
| 2085 | png_crc_read(png_ptr, png_ptr->zbuf, png_ptr->zstream.avail_in); |
| 2086 | png_ptr->idat_size -= png_ptr->zstream.avail_in; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2087 | } |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2088 | ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2089 | if (ret == Z_STREAM_END) |
| 2090 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2091 | if (!(png_ptr->zstream.avail_out) || png_ptr->zstream.avail_in || |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2092 | png_ptr->idat_size) |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 2093 | png_error(png_ptr, "Extra compressed data"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2094 | png_ptr->mode |= PNG_AFTER_IDAT; |
| 2095 | png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2096 | break; |
| 2097 | } |
| 2098 | if (ret != Z_OK) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2099 | png_error(png_ptr, png_ptr->zstream.msg ? png_ptr->zstream.msg : |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2100 | "Decompression Error"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2101 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2102 | if (!(png_ptr->zstream.avail_out)) |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 2103 | png_error(png_ptr, "Extra compressed data"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2104 | |
Glenn Randers-Pehrson | ea3bcd7 | 1998-03-07 14:33:00 -0600 | [diff] [blame] | 2105 | } |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2106 | png_ptr->zstream.avail_out = 0; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2107 | } |
| 2108 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2109 | if (png_ptr->idat_size || png_ptr->zstream.avail_in) |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 2110 | png_error(png_ptr, "Extra compression data"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2111 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2112 | inflateReset(&png_ptr->zstream); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2113 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2114 | png_ptr->mode |= PNG_AFTER_IDAT; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2115 | } |
| 2116 | |
| 2117 | void |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 2118 | png_read_start_row(png_structp png_ptr) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2119 | { |
| 2120 | int max_pixel_depth; |
| 2121 | png_uint_32 rowbytes; |
| 2122 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2123 | png_debug(1, "in png_read_start_row\n"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2124 | png_ptr->zstream.avail_in = 0; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2125 | png_init_read_transformations(png_ptr); |
| 2126 | if (png_ptr->interlaced) |
| 2127 | { |
| 2128 | if (!(png_ptr->transformations & PNG_INTERLACE)) |
| 2129 | png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 - |
| 2130 | png_pass_ystart[0]) / png_pass_yinc[0]; |
| 2131 | else |
| 2132 | png_ptr->num_rows = png_ptr->height; |
| 2133 | |
| 2134 | png_ptr->iwidth = (png_ptr->width + |
| 2135 | png_pass_inc[png_ptr->pass] - 1 - |
| 2136 | png_pass_start[png_ptr->pass]) / |
| 2137 | png_pass_inc[png_ptr->pass]; |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame] | 2138 | |
| 2139 | rowbytes = ((png_ptr->iwidth * |
| 2140 | (png_uint_32)png_ptr->pixel_depth + 7) >> 3) +1; |
| 2141 | png_ptr->irowbytes = (png_size_t)rowbytes; |
| 2142 | if((png_uint_32)png_ptr->irowbytes != rowbytes) |
| 2143 | png_error(png_ptr, "Rowbytes overflow in png_read_start_row"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2144 | } |
| 2145 | else |
| 2146 | { |
| 2147 | png_ptr->num_rows = png_ptr->height; |
| 2148 | png_ptr->iwidth = png_ptr->width; |
| 2149 | png_ptr->irowbytes = png_ptr->rowbytes + 1; |
| 2150 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2151 | max_pixel_depth = png_ptr->pixel_depth; |
| 2152 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 2153 | #if defined(PNG_READ_PACK_SUPPORTED) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2154 | if ((png_ptr->transformations & PNG_PACK) && png_ptr->bit_depth < 8) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2155 | max_pixel_depth = 8; |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 2156 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2157 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2158 | #if defined(PNG_READ_EXPAND_SUPPORTED) |
| 2159 | if (png_ptr->transformations & PNG_EXPAND) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2160 | { |
| 2161 | if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) |
| 2162 | { |
| 2163 | if (png_ptr->num_trans) |
| 2164 | max_pixel_depth = 32; |
| 2165 | else |
| 2166 | max_pixel_depth = 24; |
| 2167 | } |
| 2168 | else if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY) |
| 2169 | { |
| 2170 | if (max_pixel_depth < 8) |
| 2171 | max_pixel_depth = 8; |
| 2172 | if (png_ptr->num_trans) |
| 2173 | max_pixel_depth *= 2; |
| 2174 | } |
| 2175 | else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB) |
| 2176 | { |
| 2177 | if (png_ptr->num_trans) |
| 2178 | { |
| 2179 | max_pixel_depth *= 4; |
| 2180 | max_pixel_depth /= 3; |
| 2181 | } |
| 2182 | } |
| 2183 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 2184 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2185 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 2186 | #if defined(PNG_READ_FILLER_SUPPORTED) |
| 2187 | if (png_ptr->transformations & (PNG_FILLER)) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2188 | { |
| 2189 | if (max_pixel_depth < 32) |
| 2190 | max_pixel_depth = 32; |
| 2191 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 2192 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2193 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 2194 | #if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2195 | if (png_ptr->transformations & PNG_GRAY_TO_RGB) |
| 2196 | { |
| 2197 | if ((png_ptr->num_trans && (png_ptr->transformations & PNG_EXPAND)) || |
| 2198 | png_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) |
| 2199 | { |
| 2200 | if (max_pixel_depth <= 16) |
| 2201 | max_pixel_depth = 32; |
| 2202 | else if (max_pixel_depth <= 32) |
| 2203 | max_pixel_depth = 64; |
| 2204 | } |
| 2205 | else |
| 2206 | { |
| 2207 | if (max_pixel_depth <= 8) |
| 2208 | max_pixel_depth = 24; |
| 2209 | else if (max_pixel_depth <= 16) |
| 2210 | max_pixel_depth = 48; |
| 2211 | } |
| 2212 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 2213 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2214 | |
| 2215 | /* align the width on the next larger 8 pixels. Mainly used |
| 2216 | for interlacing */ |
| 2217 | rowbytes = ((png_ptr->width + 7) & ~((png_uint_32)7)); |
| 2218 | /* calculate the maximum bytes needed, adding a byte and a pixel |
| 2219 | for safety sake */ |
| 2220 | rowbytes = ((rowbytes * (png_uint_32)max_pixel_depth + 7) >> 3) + |
| 2221 | 1 + ((max_pixel_depth + 7) >> 3); |
| 2222 | #ifdef PNG_MAX_MALLOC_64K |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 2223 | if (rowbytes > (png_uint_32)65536L) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2224 | png_error(png_ptr, "This image requires a row greater than 64KB"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2225 | #endif |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2226 | png_ptr->row_buf = (png_bytep)png_malloc(png_ptr, rowbytes); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2227 | |
| 2228 | #ifdef PNG_MAX_MALLOC_64K |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 2229 | if ((png_uint_32)png_ptr->rowbytes + 1 > (png_uint_32)65536L) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2230 | png_error(png_ptr, "This image requires a row greater than 64KB"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2231 | #endif |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame] | 2232 | png_ptr->prev_row = (png_bytep)png_malloc(png_ptr, (png_uint_32)( |
| 2233 | png_ptr->rowbytes + 1)); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2234 | |
Glenn Randers-Pehrson | a357b99 | 1998-02-08 20:56:40 -0600 | [diff] [blame] | 2235 | png_memset_check(png_ptr, png_ptr->prev_row, 0, png_ptr->rowbytes + 1); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2236 | |
| 2237 | png_debug1(3, "width = %d,\n", png_ptr->width); |
| 2238 | png_debug1(3, "height = %d,\n", png_ptr->height); |
| 2239 | png_debug1(3, "iwidth = %d,\n", png_ptr->iwidth); |
| 2240 | png_debug1(3, "num_rows = %d\n", png_ptr->num_rows); |
| 2241 | png_debug1(3, "rowbytes = %d,\n", png_ptr->rowbytes); |
| 2242 | png_debug1(3, "irowbytes = %d,\n", png_ptr->irowbytes); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2243 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2244 | png_ptr->flags |= PNG_FLAG_ROW_INIT; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2245 | } |