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 | 4766a24 | 2000-07-17 06:17:09 -0500 | [diff] [blame^] | 4 | * libpng 1.0.8rc1 - July 17, 2000 |
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 |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 6 | * Copyright (c) 1998, 1999, 2000 Glenn Randers-Pehrson |
Glenn Randers-Pehrson | d436672 | 2000-06-04 14:29:29 -0500 | [diff] [blame] | 7 | * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) |
| 8 | * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 9 | * |
Glenn Randers-Pehrson | 8686fff | 1998-05-21 09:27:50 -0500 | [diff] [blame] | 10 | * This file contains routines that are only called from within |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 11 | * libpng itself during the course of reading an image. |
| 12 | */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 13 | |
| 14 | #define PNG_INTERNAL |
| 15 | #include "png.h" |
| 16 | |
Glenn Randers-Pehrson | 316f97a | 2000-07-08 13:19:41 -0500 | [diff] [blame] | 17 | #if defined(_WIN32_WCE) |
| 18 | /* strtod() function is not supported on WindowsCE */ |
| 19 | # ifdef PNG_FLOATING_POINT_SUPPORTED |
| 20 | __inline double strtod(const char *nptr, char **endptr) |
| 21 | { |
| 22 | double result = 0; |
| 23 | int len; |
| 24 | wchar_t *str, *end; |
| 25 | |
| 26 | len = MultiByteToWideChar(CP_ACP, 0, nptr, -1, NULL, 0); |
| 27 | str = (wchar_t *)malloc(len * sizeof(wchar_t)); |
| 28 | if ( NULL != str ) |
| 29 | { |
| 30 | MultiByteToWideChar(CP_ACP, 0, nptr, -1, str, len); |
| 31 | result = wcstod(str, &end); |
| 32 | len = WideCharToMultiByte(CP_ACP, 0, end, -1, NULL, 0, NULL, NULL); |
| 33 | *endptr = (char *)nptr + (strlen(nptr) - len + 1); |
| 34 | free(str); |
| 35 | } |
| 36 | return result; |
| 37 | } |
| 38 | # endif |
| 39 | #endif |
| 40 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 41 | #ifndef PNG_READ_BIG_ENDIAN_SUPPORTED |
Glenn Randers-Pehrson | 8686fff | 1998-05-21 09:27:50 -0500 | [diff] [blame] | 42 | /* Grab an unsigned 32-bit integer from a buffer in big-endian format. */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 43 | png_uint_32 /* PRIVATE */ |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 44 | png_get_uint_32(png_bytep buf) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 45 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 46 | png_uint_32 i = ((png_uint_32)(*buf) << 24) + |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 47 | ((png_uint_32)(*(buf + 1)) << 16) + |
| 48 | ((png_uint_32)(*(buf + 2)) << 8) + |
| 49 | (png_uint_32)(*(buf + 3)); |
| 50 | |
Glenn Randers-Pehrson | b212002 | 1998-01-31 20:07:59 -0600 | [diff] [blame] | 51 | return (i); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 52 | } |
| 53 | |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 54 | #if defined(PNG_READ_pCAL_SUPPORTED) || defined(PNG_READ_oFFs_SUPPORTED) |
Glenn Randers-Pehrson | 8686fff | 1998-05-21 09:27:50 -0500 | [diff] [blame] | 55 | /* Grab a signed 32-bit integer from a buffer in big-endian format. The |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 56 | * data is stored in the PNG file in two's complement format, and it is |
| 57 | * assumed that the machine format for signed integers is the same. */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 58 | png_int_32 /* PRIVATE */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 59 | png_get_int_32(png_bytep buf) |
| 60 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 61 | png_int_32 i = ((png_int_32)(*buf) << 24) + |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 62 | ((png_int_32)(*(buf + 1)) << 16) + |
| 63 | ((png_int_32)(*(buf + 2)) << 8) + |
| 64 | (png_int_32)(*(buf + 3)); |
| 65 | |
Glenn Randers-Pehrson | b212002 | 1998-01-31 20:07:59 -0600 | [diff] [blame] | 66 | return (i); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 67 | } |
| 68 | #endif /* PNG_READ_pCAL_SUPPORTED */ |
| 69 | |
Glenn Randers-Pehrson | 8686fff | 1998-05-21 09:27:50 -0500 | [diff] [blame] | 70 | /* Grab an unsigned 16-bit integer from a buffer in big-endian format. */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 71 | png_uint_16 /* PRIVATE */ |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 72 | png_get_uint_16(png_bytep buf) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 73 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 74 | png_uint_16 i = (png_uint_16)(((png_uint_16)(*buf) << 8) + |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 75 | (png_uint_16)(*(buf + 1))); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 76 | |
Glenn Randers-Pehrson | b212002 | 1998-01-31 20:07:59 -0600 | [diff] [blame] | 77 | return (i); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 78 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 79 | #endif /* PNG_READ_BIG_ENDIAN_SUPPORTED */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 80 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 81 | /* Read data, and (optionally) run it through the CRC. */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 82 | void /* PRIVATE */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 83 | 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] | 84 | { |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 85 | png_read_data(png_ptr, buf, length); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 86 | png_calculate_crc(png_ptr, buf, length); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 87 | } |
| 88 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 89 | /* Optionally skip data and then check the CRC. Depending on whether we |
| 90 | 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] | 91 | things up, we may calculate the CRC on the data and print a message. |
| 92 | Returns '1' if there was a CRC error, '0' otherwise. */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 93 | int /* PRIVATE */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 94 | png_crc_finish(png_structp png_ptr, png_uint_32 skip) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 95 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 96 | png_size_t i; |
| 97 | png_size_t istop = png_ptr->zbuf_size; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 98 | |
Glenn Randers-Pehrson | d0dce40 | 1998-05-09 10:02:29 -0500 | [diff] [blame] | 99 | for (i = (png_size_t)skip; i > istop; i -= istop) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 100 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 101 | png_crc_read(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 102 | } |
| 103 | if (i) |
| 104 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 105 | png_crc_read(png_ptr, png_ptr->zbuf, i); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 106 | } |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 107 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 108 | if (png_crc_error(png_ptr)) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 109 | { |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 110 | if (((png_ptr->chunk_name[0] & 0x20) && /* Ancillary */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 111 | !(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN)) || |
| 112 | (!(png_ptr->chunk_name[0] & 0x20) && /* Critical */ |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 113 | (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_USE))) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 114 | { |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 115 | png_chunk_warning(png_ptr, "CRC error"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 116 | } |
| 117 | else |
| 118 | { |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 119 | png_chunk_error(png_ptr, "CRC error"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 120 | } |
Glenn Randers-Pehrson | b212002 | 1998-01-31 20:07:59 -0600 | [diff] [blame] | 121 | return (1); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 122 | } |
| 123 | |
Glenn Randers-Pehrson | b212002 | 1998-01-31 20:07:59 -0600 | [diff] [blame] | 124 | return (0); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 125 | } |
| 126 | |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 127 | /* 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] | 128 | the data it has read thus far. */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 129 | int /* PRIVATE */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 130 | png_crc_error(png_structp png_ptr) |
| 131 | { |
| 132 | png_byte crc_bytes[4]; |
| 133 | png_uint_32 crc; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 134 | int need_crc = 1; |
| 135 | |
| 136 | if (png_ptr->chunk_name[0] & 0x20) /* ancillary */ |
| 137 | { |
| 138 | if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_MASK) == |
| 139 | (PNG_FLAG_CRC_ANCILLARY_USE | PNG_FLAG_CRC_ANCILLARY_NOWARN)) |
| 140 | need_crc = 0; |
| 141 | } |
| 142 | else /* critical */ |
| 143 | { |
| 144 | if (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_IGNORE) |
| 145 | need_crc = 0; |
| 146 | } |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 147 | |
| 148 | png_read_data(png_ptr, crc_bytes, 4); |
| 149 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 150 | if (need_crc) |
| 151 | { |
| 152 | crc = png_get_uint_32(crc_bytes); |
Glenn Randers-Pehrson | b212002 | 1998-01-31 20:07:59 -0600 | [diff] [blame] | 153 | return ((int)(crc != png_ptr->crc)); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 154 | } |
| 155 | else |
Glenn Randers-Pehrson | b212002 | 1998-01-31 20:07:59 -0600 | [diff] [blame] | 156 | return (0); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 157 | } |
| 158 | |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 159 | #if defined(PNG_READ_zTXt_SUPPORTED) || defined(PNG_READ_iTXt_SUPPORTED) || \ |
Glenn Randers-Pehrson | 4accabb | 2000-04-14 14:20:47 -0500 | [diff] [blame] | 160 | defined(PNG_READ_iCCP_SUPPORTED) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 161 | /* |
| 162 | * Decompress trailing data in a chunk. The assumption is that chunkdata |
| 163 | * points at an allocated area holding the contents of a chunk with a |
| 164 | * trailing compressed part. What we get back is an allocated area |
| 165 | * holding the original prefix part and an uncompressed version of the |
| 166 | * trailing part (the malloc area passed in is freed). |
| 167 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 168 | png_charp /* PRIVATE */ |
| 169 | png_decompress_chunk(png_structp png_ptr, int comp_type, |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 170 | png_charp chunkdata, png_size_t chunklength, |
Glenn Randers-Pehrson | 68ea243 | 2000-04-01 21:10:05 -0600 | [diff] [blame] | 171 | png_size_t prefix_size, png_size_t *newlength) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 172 | { |
| 173 | static char msg[] = "Error decoding compressed text"; |
| 174 | png_charp text = NULL; |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 175 | png_size_t text_size; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 176 | |
| 177 | if (comp_type == PNG_TEXT_COMPRESSION_zTXt) |
| 178 | { |
Glenn Randers-Pehrson | d436672 | 2000-06-04 14:29:29 -0500 | [diff] [blame] | 179 | int ret = Z_OK; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 180 | png_ptr->zstream.next_in = (png_bytep)(chunkdata + prefix_size); |
| 181 | png_ptr->zstream.avail_in = (uInt)(chunklength - prefix_size); |
| 182 | png_ptr->zstream.next_out = png_ptr->zbuf; |
| 183 | png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; |
| 184 | |
| 185 | text_size = 0; |
| 186 | text = NULL; |
| 187 | |
| 188 | while (png_ptr->zstream.avail_in) |
| 189 | { |
Glenn Randers-Pehrson | d436672 | 2000-06-04 14:29:29 -0500 | [diff] [blame] | 190 | ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 191 | if (ret != Z_OK && ret != Z_STREAM_END) |
| 192 | { |
| 193 | if (png_ptr->zstream.msg != NULL) |
| 194 | png_warning(png_ptr, png_ptr->zstream.msg); |
| 195 | else |
| 196 | png_warning(png_ptr, msg); |
| 197 | inflateReset(&png_ptr->zstream); |
| 198 | png_ptr->zstream.avail_in = 0; |
| 199 | |
| 200 | if (text == NULL) |
| 201 | { |
| 202 | text_size = prefix_size + sizeof(msg) + 1; |
| 203 | text = (png_charp)png_malloc(png_ptr, text_size); |
| 204 | png_memcpy(text, chunkdata, prefix_size); |
| 205 | } |
| 206 | |
| 207 | text[text_size - 1] = 0x00; |
| 208 | |
| 209 | /* Copy what we can of the error message into the text chunk */ |
| 210 | text_size = (png_size_t)(chunklength - (text - chunkdata) - 1); |
| 211 | text_size = sizeof(msg) > text_size ? text_size : sizeof(msg); |
| 212 | png_memcpy(text + prefix_size, msg, text_size + 1); |
| 213 | break; |
| 214 | } |
| 215 | if (!png_ptr->zstream.avail_out || ret == Z_STREAM_END) |
| 216 | { |
| 217 | if (text == NULL) |
| 218 | { |
| 219 | text_size = prefix_size + |
| 220 | png_ptr->zbuf_size - png_ptr->zstream.avail_out; |
| 221 | text = (png_charp)png_malloc(png_ptr, text_size + 1); |
| 222 | png_memcpy(text + prefix_size, png_ptr->zbuf, |
| 223 | text_size - prefix_size); |
| 224 | png_memcpy(text, chunkdata, prefix_size); |
| 225 | *(text + text_size) = 0x00; |
| 226 | } |
| 227 | else |
| 228 | { |
| 229 | png_charp tmp; |
| 230 | |
| 231 | tmp = text; |
| 232 | text = (png_charp)png_malloc(png_ptr, (png_uint_32)(text_size + |
| 233 | png_ptr->zbuf_size - png_ptr->zstream.avail_out + 1)); |
| 234 | png_memcpy(text, tmp, text_size); |
| 235 | png_free(png_ptr, tmp); |
| 236 | png_memcpy(text + text_size, png_ptr->zbuf, |
| 237 | (png_ptr->zbuf_size - png_ptr->zstream.avail_out)); |
| 238 | text_size += png_ptr->zbuf_size - png_ptr->zstream.avail_out; |
| 239 | *(text + text_size) = 0x00; |
| 240 | } |
| 241 | if (ret == Z_STREAM_END) |
| 242 | break; |
| 243 | else |
| 244 | { |
| 245 | png_ptr->zstream.next_out = png_ptr->zbuf; |
| 246 | png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; |
| 247 | } |
| 248 | } |
| 249 | } |
Glenn Randers-Pehrson | d436672 | 2000-06-04 14:29:29 -0500 | [diff] [blame] | 250 | if (ret != Z_STREAM_END) |
Glenn Randers-Pehrson | 1394480 | 2000-06-24 07:42:42 -0500 | [diff] [blame] | 251 | { |
Glenn Randers-Pehrson | 316f97a | 2000-07-08 13:19:41 -0500 | [diff] [blame] | 252 | #if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE) |
Glenn Randers-Pehrson | 1394480 | 2000-06-24 07:42:42 -0500 | [diff] [blame] | 253 | char umsg[50]; |
| 254 | |
Glenn Randers-Pehrson | 316f97a | 2000-07-08 13:19:41 -0500 | [diff] [blame] | 255 | if (ret == Z_BUF_ERROR) |
| 256 | sprintf(umsg,"Buffer error in compressed datastream in %s chunk", |
| 257 | png_ptr->chunk_name); |
| 258 | else if (ret == Z_DATA_ERROR) |
| 259 | sprintf(umsg,"Data error in compressed datastream in %s chunk", |
| 260 | png_ptr->chunk_name); |
| 261 | else |
| 262 | sprintf(umsg,"Incomplete compressed datastream in %s chunk", |
| 263 | png_ptr->chunk_name); |
Glenn Randers-Pehrson | 1394480 | 2000-06-24 07:42:42 -0500 | [diff] [blame] | 264 | png_warning(png_ptr, umsg); |
| 265 | #else |
Glenn Randers-Pehrson | d436672 | 2000-06-04 14:29:29 -0500 | [diff] [blame] | 266 | png_warning(png_ptr, |
Glenn Randers-Pehrson | 1394480 | 2000-06-24 07:42:42 -0500 | [diff] [blame] | 267 | "Incomplete compressed datastream in chunk other than IDAT"); |
| 268 | #endif |
| 269 | } |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 270 | |
| 271 | inflateReset(&png_ptr->zstream); |
| 272 | png_ptr->zstream.avail_in = 0; |
| 273 | |
| 274 | png_free(png_ptr, chunkdata); |
| 275 | chunkdata = text; |
Glenn Randers-Pehrson | 68ea243 | 2000-04-01 21:10:05 -0600 | [diff] [blame] | 276 | *newlength=text_size; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 277 | } |
Glenn Randers-Pehrson | 1394480 | 2000-06-24 07:42:42 -0500 | [diff] [blame] | 278 | else /* if (comp_type != PNG_TEXT_COMPRESSION_zTXt) */ |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 279 | { |
Glenn Randers-Pehrson | 316f97a | 2000-07-08 13:19:41 -0500 | [diff] [blame] | 280 | #if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 281 | char umsg[50]; |
| 282 | |
| 283 | sprintf(umsg, "Unknown zTXt compression type %d", comp_type); |
| 284 | png_warning(png_ptr, umsg); |
| 285 | #else |
| 286 | png_warning(png_ptr, "Unknown zTXt compression type"); |
| 287 | #endif |
| 288 | |
| 289 | /* Copy what we can of the error message into the text chunk */ |
| 290 | text_size = (png_size_t)(chunklength - (text - chunkdata)); |
| 291 | text_size = sizeof(msg) > text_size ? text_size : sizeof(msg); |
| 292 | png_memcpy(text, msg, text_size); |
| 293 | } |
| 294 | |
| 295 | return chunkdata; |
| 296 | } |
| 297 | #endif |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 298 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 299 | /* read and check the IDHR chunk */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 300 | void /* PRIVATE */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 301 | 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] | 302 | { |
| 303 | png_byte buf[13]; |
| 304 | png_uint_32 width, height; |
| 305 | int bit_depth, color_type, compression_type, filter_type; |
| 306 | int interlace_type; |
| 307 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 308 | png_debug(1, "in png_handle_IHDR\n"); |
| 309 | |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 310 | if (png_ptr->mode & PNG_HAVE_IHDR) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 311 | png_error(png_ptr, "Out of place IHDR"); |
| 312 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 313 | /* check the length */ |
| 314 | if (length != 13) |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 315 | png_error(png_ptr, "Invalid IHDR chunk"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 316 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 317 | png_ptr->mode |= PNG_HAVE_IHDR; |
| 318 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 319 | png_crc_read(png_ptr, buf, 13); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 320 | png_crc_finish(png_ptr, 0); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 321 | |
| 322 | width = png_get_uint_32(buf); |
| 323 | height = png_get_uint_32(buf + 4); |
| 324 | bit_depth = buf[8]; |
| 325 | color_type = buf[9]; |
| 326 | compression_type = buf[10]; |
| 327 | filter_type = buf[11]; |
| 328 | interlace_type = buf[12]; |
| 329 | |
| 330 | /* check for width and height valid values */ |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 331 | if (width == 0 || width > PNG_MAX_UINT || height == 0 || |
| 332 | height > PNG_MAX_UINT) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 333 | png_error(png_ptr, "Invalid image size in IHDR"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 334 | |
| 335 | /* check other values */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 336 | if (bit_depth != 1 && bit_depth != 2 && bit_depth != 4 && |
| 337 | bit_depth != 8 && bit_depth != 16) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 338 | png_error(png_ptr, "Invalid bit depth in IHDR"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 339 | |
| 340 | if (color_type < 0 || color_type == 1 || |
| 341 | color_type == 5 || color_type > 6) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 342 | png_error(png_ptr, "Invalid color type in IHDR"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 343 | |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 344 | if (((color_type == PNG_COLOR_TYPE_PALETTE) && bit_depth > 8) || |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 345 | ((color_type == PNG_COLOR_TYPE_RGB || |
| 346 | color_type == PNG_COLOR_TYPE_GRAY_ALPHA || |
| 347 | color_type == PNG_COLOR_TYPE_RGB_ALPHA) && bit_depth < 8)) |
| 348 | png_error(png_ptr, "Invalid color type/bit depth combination in IHDR"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 349 | |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 350 | if (interlace_type >= PNG_INTERLACE_LAST) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 351 | png_error(png_ptr, "Unknown interlace method in IHDR"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 352 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 353 | if (compression_type != PNG_COMPRESSION_TYPE_BASE) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 354 | png_error(png_ptr, "Unknown compression method in IHDR"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 355 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 356 | if (filter_type != PNG_FILTER_TYPE_BASE) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 357 | png_error(png_ptr, "Unknown filter method in IHDR"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 358 | |
| 359 | /* set internal variables */ |
| 360 | png_ptr->width = width; |
| 361 | png_ptr->height = height; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 362 | png_ptr->bit_depth = (png_byte)bit_depth; |
| 363 | png_ptr->interlaced = (png_byte)interlace_type; |
| 364 | png_ptr->color_type = (png_byte)color_type; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 365 | |
| 366 | /* find number of channels */ |
| 367 | switch (png_ptr->color_type) |
| 368 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 369 | case PNG_COLOR_TYPE_GRAY: |
| 370 | case PNG_COLOR_TYPE_PALETTE: |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 371 | png_ptr->channels = 1; |
| 372 | break; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 373 | case PNG_COLOR_TYPE_RGB: |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 374 | png_ptr->channels = 3; |
| 375 | break; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 376 | case PNG_COLOR_TYPE_GRAY_ALPHA: |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 377 | png_ptr->channels = 2; |
| 378 | break; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 379 | case PNG_COLOR_TYPE_RGB_ALPHA: |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 380 | png_ptr->channels = 4; |
| 381 | break; |
| 382 | } |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 383 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 384 | /* set up other useful info */ |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 385 | png_ptr->pixel_depth = (png_byte)(png_ptr->bit_depth * |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame] | 386 | png_ptr->channels); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 387 | png_ptr->rowbytes = ((png_ptr->width * |
| 388 | (png_uint_32)png_ptr->pixel_depth + 7) >> 3); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 389 | png_debug1(3,"bit_depth = %d\n", png_ptr->bit_depth); |
| 390 | png_debug1(3,"channels = %d\n", png_ptr->channels); |
Glenn Randers-Pehrson | 4766a24 | 2000-07-17 06:17:09 -0500 | [diff] [blame^] | 391 | png_debug1(3,"rowbytes = %lu\n", png_ptr->rowbytes); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 392 | png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth, |
| 393 | color_type, interlace_type, compression_type, filter_type); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | /* read and check the palette */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 397 | void /* PRIVATE */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 398 | 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] | 399 | { |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 400 | png_colorp palette; |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 401 | int num, i; |
Glenn Randers-Pehrson | d436672 | 2000-06-04 14:29:29 -0500 | [diff] [blame] | 402 | #ifndef PNG_NO_POINTER_INDEXING |
| 403 | png_colorp pal_ptr; |
| 404 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 405 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 406 | png_debug(1, "in png_handle_PLTE\n"); |
| 407 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 408 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
| 409 | png_error(png_ptr, "Missing IHDR before PLTE"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 410 | else if (png_ptr->mode & PNG_HAVE_IDAT) |
| 411 | { |
| 412 | png_warning(png_ptr, "Invalid PLTE after IDAT"); |
| 413 | png_crc_finish(png_ptr, length); |
| 414 | return; |
| 415 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 416 | else if (png_ptr->mode & PNG_HAVE_PLTE) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 417 | png_error(png_ptr, "Duplicate PLTE chunk"); |
| 418 | |
| 419 | png_ptr->mode |= PNG_HAVE_PLTE; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 420 | |
| 421 | #if !defined(PNG_READ_OPT_PLTE_SUPPORTED) |
| 422 | if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE) |
| 423 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 424 | png_crc_finish(png_ptr, length); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 425 | return; |
| 426 | } |
| 427 | #endif |
| 428 | |
Glenn Randers-Pehrson | d436672 | 2000-06-04 14:29:29 -0500 | [diff] [blame] | 429 | if (length > 768 || length % 3) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 430 | { |
| 431 | if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE) |
| 432 | { |
| 433 | png_warning(png_ptr, "Invalid palette chunk"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 434 | png_crc_finish(png_ptr, length); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 435 | return; |
| 436 | } |
| 437 | else |
| 438 | { |
| 439 | png_error(png_ptr, "Invalid palette chunk"); |
| 440 | } |
| 441 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 442 | |
| 443 | num = (int)length / 3; |
Glenn Randers-Pehrson | 38e6e77 | 2000-04-09 19:06:13 -0500 | [diff] [blame] | 444 | |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 445 | palette = (png_colorp)png_zalloc(png_ptr, (uInt)num, sizeof (png_color)); |
Glenn Randers-Pehrson | 38e6e77 | 2000-04-09 19:06:13 -0500 | [diff] [blame] | 446 | |
Glenn Randers-Pehrson | d436672 | 2000-06-04 14:29:29 -0500 | [diff] [blame] | 447 | #ifndef PNG_NO_POINTER_INDEXING |
| 448 | for (i = 0, pal_ptr = palette; i < num; i++, pal_ptr++) |
| 449 | { |
| 450 | png_byte buf[3]; |
| 451 | |
| 452 | png_crc_read(png_ptr, buf, 3); |
| 453 | pal_ptr->red = buf[0]; |
| 454 | pal_ptr->green = buf[1]; |
| 455 | pal_ptr->blue = buf[2]; |
| 456 | } |
| 457 | #else |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 458 | for (i = 0; i < num; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 459 | { |
| 460 | png_byte buf[3]; |
| 461 | |
| 462 | png_crc_read(png_ptr, buf, 3); |
| 463 | /* don't depend upon png_color being any order */ |
| 464 | palette[i].red = buf[0]; |
| 465 | palette[i].green = buf[1]; |
| 466 | palette[i].blue = buf[2]; |
| 467 | } |
Glenn Randers-Pehrson | d436672 | 2000-06-04 14:29:29 -0500 | [diff] [blame] | 468 | #endif |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 469 | |
| 470 | /* If we actually NEED the PLTE chunk (ie for a paletted image), we do |
| 471 | whatever the normal CRC configuration tells us. However, if we |
| 472 | have an RGB image, the PLTE can be considered ancillary, so |
| 473 | we will act as though it is. */ |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 474 | #if !defined(PNG_READ_OPT_PLTE_SUPPORTED) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 475 | if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 476 | #endif |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 477 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 478 | png_crc_finish(png_ptr, 0); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 479 | } |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 480 | #if !defined(PNG_READ_OPT_PLTE_SUPPORTED) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 481 | else if (png_crc_error(png_ptr)) /* Only if we have a CRC error */ |
| 482 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 483 | /* If we don't want to use the data from an ancillary chunk, |
| 484 | we have two options: an error abort, or a warning and we |
| 485 | ignore the data in this chunk (which should be OK, since |
| 486 | it's considered ancillary for a RGB or RGBA image). */ |
| 487 | if (!(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_USE)) |
| 488 | { |
| 489 | if (png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN) |
| 490 | { |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 491 | png_chunk_error(png_ptr, "CRC error"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 492 | } |
| 493 | else |
| 494 | { |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 495 | png_chunk_warning(png_ptr, "CRC error"); |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 496 | png_zfree(png_ptr, palette); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 497 | return; |
| 498 | } |
| 499 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 500 | /* Otherwise, we (optionally) emit a warning and use the chunk. */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 501 | else if (!(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN)) |
| 502 | { |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 503 | png_chunk_warning(png_ptr, "CRC error"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 504 | } |
| 505 | } |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 506 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 507 | png_ptr->palette = palette; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 508 | png_ptr->num_palette = (png_uint_16)num; |
Glenn Randers-Pehrson | 38e6e77 | 2000-04-09 19:06:13 -0500 | [diff] [blame] | 509 | |
Glenn Randers-Pehrson | 6942d53 | 2000-05-01 09:31:54 -0500 | [diff] [blame] | 510 | #ifdef PNG_FREE_ME_SUPPORTED |
Glenn Randers-Pehrson | 38e6e77 | 2000-04-09 19:06:13 -0500 | [diff] [blame] | 511 | png_free_data(png_ptr, info_ptr, PNG_FREE_PLTE, 0); |
| 512 | png_ptr->free_me |= PNG_FREE_PLTE; |
Glenn Randers-Pehrson | 6942d53 | 2000-05-01 09:31:54 -0500 | [diff] [blame] | 513 | #else |
| 514 | png_ptr->flags |= PNG_FLAG_FREE_PLTE; |
| 515 | #endif |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 516 | png_set_PLTE(png_ptr, info_ptr, palette, num); |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 517 | |
Glenn Randers-Pehrson | 6942d53 | 2000-05-01 09:31:54 -0500 | [diff] [blame] | 518 | #if defined(PNG_READ_tRNS_SUPPORTED) |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 519 | if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) |
| 520 | { |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 521 | if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS)) |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 522 | { |
Glenn Randers-Pehrson | 38e6e77 | 2000-04-09 19:06:13 -0500 | [diff] [blame] | 523 | if (png_ptr->num_trans > (png_uint_16)num) |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 524 | { |
| 525 | png_warning(png_ptr, "Truncating incorrect tRNS chunk length"); |
Glenn Randers-Pehrson | 38e6e77 | 2000-04-09 19:06:13 -0500 | [diff] [blame] | 526 | png_ptr->num_trans = (png_uint_16)num; |
| 527 | } |
| 528 | if (info_ptr->num_trans > (png_uint_16)num) |
| 529 | { |
| 530 | png_warning(png_ptr, "Truncating incorrect info tRNS chunk length"); |
| 531 | info_ptr->num_trans = (png_uint_16)num; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 532 | } |
| 533 | } |
| 534 | } |
| 535 | #endif |
| 536 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 537 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 538 | |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 539 | void /* PRIVATE */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 540 | png_handle_IEND(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) |
| 541 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 542 | png_debug(1, "in png_handle_IEND\n"); |
| 543 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 544 | if (!(png_ptr->mode & PNG_HAVE_IHDR) || !(png_ptr->mode & PNG_HAVE_IDAT)) |
| 545 | { |
| 546 | png_error(png_ptr, "No image in file"); |
Glenn Randers-Pehrson | a357b99 | 1998-02-08 20:56:40 -0600 | [diff] [blame] | 547 | |
| 548 | /* to quiet compiler warnings about unused info_ptr */ |
| 549 | if (info_ptr == NULL) |
| 550 | return; |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 551 | } |
| 552 | |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 553 | png_ptr->mode |= (PNG_AFTER_IDAT | PNG_HAVE_IEND); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 554 | |
| 555 | if (length != 0) |
| 556 | { |
| 557 | png_warning(png_ptr, "Incorrect IEND chunk length"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 558 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 559 | png_crc_finish(png_ptr, length); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 560 | } |
| 561 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 562 | #if defined(PNG_READ_gAMA_SUPPORTED) |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 563 | void /* PRIVATE */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 564 | 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] | 565 | { |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 566 | png_fixed_point igamma; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 567 | #ifdef PNG_FLOATING_POINT_SUPPORTED |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 568 | float file_gamma; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 569 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 570 | png_byte buf[4]; |
| 571 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 572 | png_debug(1, "in png_handle_gAMA\n"); |
| 573 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 574 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
| 575 | png_error(png_ptr, "Missing IHDR before gAMA"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 576 | else if (png_ptr->mode & PNG_HAVE_IDAT) |
| 577 | { |
| 578 | png_warning(png_ptr, "Invalid gAMA after IDAT"); |
| 579 | png_crc_finish(png_ptr, length); |
| 580 | return; |
| 581 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 582 | else if (png_ptr->mode & PNG_HAVE_PLTE) |
| 583 | /* Should be an error, but we can cope with it */ |
| 584 | png_warning(png_ptr, "Out of place gAMA chunk"); |
Glenn Randers-Pehrson | 2687fcc | 1998-01-07 20:54:20 -0600 | [diff] [blame] | 585 | |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 586 | else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_gAMA) |
Glenn Randers-Pehrson | 2687fcc | 1998-01-07 20:54:20 -0600 | [diff] [blame] | 587 | #if defined(PNG_READ_sRGB_SUPPORTED) |
| 588 | && !(info_ptr->valid & PNG_INFO_sRGB) |
| 589 | #endif |
| 590 | ) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 591 | { |
| 592 | png_warning(png_ptr, "Duplicate gAMA chunk"); |
| 593 | png_crc_finish(png_ptr, length); |
| 594 | return; |
| 595 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 596 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 597 | if (length != 4) |
| 598 | { |
Guy Schalnat | 69b1448 | 1996-01-10 02:56:49 -0600 | [diff] [blame] | 599 | png_warning(png_ptr, "Incorrect gAMA chunk length"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 600 | png_crc_finish(png_ptr, length); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 601 | return; |
| 602 | } |
| 603 | |
| 604 | png_crc_read(png_ptr, buf, 4); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 605 | if (png_crc_finish(png_ptr, 0)) |
| 606 | return; |
| 607 | |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 608 | igamma = (png_fixed_point)png_get_uint_32(buf); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 609 | /* check for zero gamma */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 610 | if (igamma == 0) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 611 | return; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 612 | |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 613 | #if defined(PNG_READ_sRGB_SUPPORTED) |
Glenn Randers-Pehrson | 2687fcc | 1998-01-07 20:54:20 -0600 | [diff] [blame] | 614 | if (info_ptr->valid & PNG_INFO_sRGB) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 615 | if(igamma < 45000L || igamma > 46000L) |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 616 | { |
| 617 | png_warning(png_ptr, |
| 618 | "Ignoring incorrect gAMA value when sRGB is also present"); |
Glenn Randers-Pehrson | 316f97a | 2000-07-08 13:19:41 -0500 | [diff] [blame] | 619 | #ifndef PNG_NO_CONSOLE_IO |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 620 | fprintf(stderr, "gamma = (%d/100000)\n", (int)igamma); |
Glenn Randers-Pehrson | 2687fcc | 1998-01-07 20:54:20 -0600 | [diff] [blame] | 621 | #endif |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 622 | return; |
| 623 | } |
| 624 | #endif /* PNG_READ_sRGB_SUPPORTED */ |
| 625 | |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 626 | #ifdef PNG_FLOATING_POINT_SUPPORTED |
Glenn Randers-Pehrson | 397100e | 1998-03-07 19:45:37 -0600 | [diff] [blame] | 627 | file_gamma = (float)igamma / (float)100000.0; |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 628 | # ifdef PNG_READ_GAMMA_SUPPORTED |
| 629 | png_ptr->gamma = file_gamma; |
| 630 | # endif |
| 631 | png_set_gAMA(png_ptr, info_ptr, file_gamma); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 632 | #endif |
| 633 | #ifdef PNG_FIXED_POINT_SUPPORTED |
| 634 | png_set_gAMA_fixed(png_ptr, info_ptr, igamma); |
| 635 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 636 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 637 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 638 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 639 | #if defined(PNG_READ_sBIT_SUPPORTED) |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 640 | void /* PRIVATE */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 641 | 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] | 642 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 643 | png_size_t truelen; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 644 | png_byte buf[4]; |
Guy Schalnat | 69b1448 | 1996-01-10 02:56:49 -0600 | [diff] [blame] | 645 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 646 | png_debug(1, "in png_handle_sBIT\n"); |
| 647 | |
Guy Schalnat | 69b1448 | 1996-01-10 02:56:49 -0600 | [diff] [blame] | 648 | buf[0] = buf[1] = buf[2] = buf[3] = 0; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 649 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 650 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
| 651 | png_error(png_ptr, "Missing IHDR before sBIT"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 652 | else if (png_ptr->mode & PNG_HAVE_IDAT) |
| 653 | { |
| 654 | png_warning(png_ptr, "Invalid sBIT after IDAT"); |
| 655 | png_crc_finish(png_ptr, length); |
| 656 | return; |
| 657 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 658 | else if (png_ptr->mode & PNG_HAVE_PLTE) |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 659 | { |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 660 | /* Should be an error, but we can cope with it */ |
| 661 | png_warning(png_ptr, "Out of place sBIT chunk"); |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 662 | } |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 663 | else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sBIT)) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 664 | { |
| 665 | png_warning(png_ptr, "Duplicate sBIT chunk"); |
| 666 | png_crc_finish(png_ptr, length); |
| 667 | return; |
| 668 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 669 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 670 | if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 671 | truelen = 3; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 672 | else |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 673 | truelen = (png_size_t)png_ptr->channels; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 674 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 675 | if (length != truelen) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 676 | { |
Guy Schalnat | 69b1448 | 1996-01-10 02:56:49 -0600 | [diff] [blame] | 677 | png_warning(png_ptr, "Incorrect sBIT chunk length"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 678 | png_crc_finish(png_ptr, length); |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 679 | return; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 680 | } |
| 681 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 682 | png_crc_read(png_ptr, buf, truelen); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 683 | if (png_crc_finish(png_ptr, 0)) |
| 684 | return; |
| 685 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 686 | if (png_ptr->color_type & PNG_COLOR_MASK_COLOR) |
| 687 | { |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 688 | png_ptr->sig_bit.red = buf[0]; |
| 689 | png_ptr->sig_bit.green = buf[1]; |
| 690 | png_ptr->sig_bit.blue = buf[2]; |
| 691 | png_ptr->sig_bit.alpha = buf[3]; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 692 | } |
| 693 | else |
| 694 | { |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 695 | png_ptr->sig_bit.gray = buf[0]; |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 696 | png_ptr->sig_bit.red = buf[0]; |
| 697 | png_ptr->sig_bit.green = buf[0]; |
| 698 | png_ptr->sig_bit.blue = buf[0]; |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 699 | png_ptr->sig_bit.alpha = buf[1]; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 700 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 701 | png_set_sBIT(png_ptr, info_ptr, &(png_ptr->sig_bit)); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 702 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 703 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 704 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 705 | #if defined(PNG_READ_cHRM_SUPPORTED) |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 706 | void /* PRIVATE */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 707 | 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] | 708 | { |
| 709 | png_byte buf[4]; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 710 | #ifdef PNG_FLOATING_POINT_SUPPORTED |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 711 | float white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 712 | #endif |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 713 | png_fixed_point int_x_white, int_y_white, int_x_red, int_y_red, int_x_green, |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 714 | int_y_green, int_x_blue, int_y_blue; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 715 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 716 | png_debug(1, "in png_handle_cHRM\n"); |
| 717 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 718 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 719 | png_error(png_ptr, "Missing IHDR before cHRM"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 720 | else if (png_ptr->mode & PNG_HAVE_IDAT) |
| 721 | { |
| 722 | png_warning(png_ptr, "Invalid cHRM after IDAT"); |
| 723 | png_crc_finish(png_ptr, length); |
| 724 | return; |
| 725 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 726 | else if (png_ptr->mode & PNG_HAVE_PLTE) |
| 727 | /* Should be an error, but we can cope with it */ |
| 728 | png_warning(png_ptr, "Missing PLTE before cHRM"); |
Glenn Randers-Pehrson | 2687fcc | 1998-01-07 20:54:20 -0600 | [diff] [blame] | 729 | |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 730 | else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_cHRM) |
Glenn Randers-Pehrson | 2687fcc | 1998-01-07 20:54:20 -0600 | [diff] [blame] | 731 | #if defined(PNG_READ_sRGB_SUPPORTED) |
| 732 | && !(info_ptr->valid & PNG_INFO_sRGB) |
| 733 | #endif |
| 734 | ) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 735 | { |
| 736 | png_warning(png_ptr, "Duplicate cHRM chunk"); |
| 737 | png_crc_finish(png_ptr, length); |
| 738 | return; |
| 739 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 740 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 741 | if (length != 32) |
| 742 | { |
Guy Schalnat | 69b1448 | 1996-01-10 02:56:49 -0600 | [diff] [blame] | 743 | png_warning(png_ptr, "Incorrect cHRM chunk length"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 744 | png_crc_finish(png_ptr, length); |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 745 | return; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 746 | } |
| 747 | |
| 748 | png_crc_read(png_ptr, buf, 4); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 749 | int_x_white = (png_fixed_point)png_get_uint_32(buf); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 750 | |
| 751 | png_crc_read(png_ptr, buf, 4); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 752 | int_y_white = (png_fixed_point)png_get_uint_32(buf); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 753 | |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 754 | if (int_x_white > 80000L || int_y_white > 80000L || |
| 755 | int_x_white + int_y_white > 100000L) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 756 | { |
| 757 | png_warning(png_ptr, "Invalid cHRM white point"); |
| 758 | png_crc_finish(png_ptr, 24); |
| 759 | return; |
| 760 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 761 | |
| 762 | png_crc_read(png_ptr, buf, 4); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 763 | int_x_red = (png_fixed_point)png_get_uint_32(buf); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 764 | |
| 765 | png_crc_read(png_ptr, buf, 4); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 766 | int_y_red = (png_fixed_point)png_get_uint_32(buf); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 767 | |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 768 | if (int_x_red > 80000L || int_y_red > 80000L || |
| 769 | int_x_red + int_y_red > 100000L) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 770 | { |
| 771 | png_warning(png_ptr, "Invalid cHRM red point"); |
| 772 | png_crc_finish(png_ptr, 16); |
| 773 | return; |
| 774 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 775 | |
| 776 | png_crc_read(png_ptr, buf, 4); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 777 | int_x_green = (png_fixed_point)png_get_uint_32(buf); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 778 | |
| 779 | png_crc_read(png_ptr, buf, 4); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 780 | int_y_green = (png_fixed_point)png_get_uint_32(buf); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 781 | |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 782 | if (int_x_green > 80000L || int_y_green > 80000L || |
| 783 | int_x_green + int_y_green > 100000L) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 784 | { |
| 785 | png_warning(png_ptr, "Invalid cHRM green point"); |
| 786 | png_crc_finish(png_ptr, 8); |
| 787 | return; |
| 788 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 789 | |
| 790 | png_crc_read(png_ptr, buf, 4); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 791 | int_x_blue = (png_fixed_point)png_get_uint_32(buf); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 792 | |
| 793 | png_crc_read(png_ptr, buf, 4); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 794 | int_y_blue = (png_fixed_point)png_get_uint_32(buf); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 795 | |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 796 | if (int_x_blue > 80000L || int_y_blue > 80000L || |
| 797 | int_x_blue + int_y_blue > 100000L) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 798 | { |
| 799 | png_warning(png_ptr, "Invalid cHRM blue point"); |
| 800 | png_crc_finish(png_ptr, 0); |
| 801 | return; |
| 802 | } |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 803 | #ifdef PNG_FLOATING_POINT_SUPPORTED |
| 804 | white_x = (float)int_x_white / (float)100000.0; |
| 805 | white_y = (float)int_y_white / (float)100000.0; |
| 806 | red_x = (float)int_x_red / (float)100000.0; |
| 807 | red_y = (float)int_y_red / (float)100000.0; |
| 808 | green_x = (float)int_x_green / (float)100000.0; |
| 809 | green_y = (float)int_y_green / (float)100000.0; |
| 810 | blue_x = (float)int_x_blue / (float)100000.0; |
| 811 | blue_y = (float)int_y_blue / (float)100000.0; |
| 812 | #endif |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 813 | |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 814 | #if defined(PNG_READ_sRGB_SUPPORTED) |
Glenn Randers-Pehrson | 2687fcc | 1998-01-07 20:54:20 -0600 | [diff] [blame] | 815 | if (info_ptr->valid & PNG_INFO_sRGB) |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 816 | { |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 817 | if (abs(int_x_white - 31270L) > 1000 || |
| 818 | abs(int_y_white - 32900L) > 1000 || |
| 819 | abs( int_x_red - 64000L) > 1000 || |
| 820 | abs( int_y_red - 33000L) > 1000 || |
| 821 | abs(int_x_green - 30000L) > 1000 || |
| 822 | abs(int_y_green - 60000L) > 1000 || |
| 823 | abs( int_x_blue - 15000L) > 1000 || |
| 824 | abs( int_y_blue - 6000L) > 1000) |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 825 | { |
Glenn Randers-Pehrson | 2687fcc | 1998-01-07 20:54:20 -0600 | [diff] [blame] | 826 | |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 827 | png_warning(png_ptr, |
| 828 | "Ignoring incorrect cHRM value when sRGB is also present"); |
Glenn Randers-Pehrson | 316f97a | 2000-07-08 13:19:41 -0500 | [diff] [blame] | 829 | #ifndef PNG_NO_CONSOLE_IO |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 830 | #ifdef PNG_FLOATING_POINT_SUPPORTED |
Glenn Randers-Pehrson | 2687fcc | 1998-01-07 20:54:20 -0600 | [diff] [blame] | 831 | fprintf(stderr,"wx=%f, wy=%f, rx=%f, ry=%f\n", |
| 832 | white_x, white_y, red_x, red_y); |
| 833 | fprintf(stderr,"gx=%f, gy=%f, bx=%f, by=%f\n", |
| 834 | green_x, green_y, blue_x, blue_y); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 835 | #else |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 836 | fprintf(stderr,"wx=%ld, wy=%ld, rx=%ld, ry=%ld\n", |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 837 | int_x_white, int_y_white, int_x_red, int_y_red); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 838 | fprintf(stderr,"gx=%ld, gy=%ld, bx=%ld, by=%ld\n", |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 839 | int_x_green, int_y_green, int_x_blue, int_y_blue); |
Glenn Randers-Pehrson | 2687fcc | 1998-01-07 20:54:20 -0600 | [diff] [blame] | 840 | #endif |
Glenn Randers-Pehrson | 316f97a | 2000-07-08 13:19:41 -0500 | [diff] [blame] | 841 | #endif /* PNG_NO_CONSOLE_IO */ |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 842 | } |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 843 | png_crc_finish(png_ptr, 0); |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 844 | return; |
| 845 | } |
| 846 | #endif /* PNG_READ_sRGB_SUPPORTED */ |
| 847 | |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 848 | #ifdef PNG_FLOATING_POINT_SUPPORTED |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 849 | png_set_cHRM(png_ptr, info_ptr, |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 850 | white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 851 | #endif |
| 852 | #ifdef PNG_FIXED_POINT_SUPPORTED |
| 853 | png_set_cHRM_fixed(png_ptr, info_ptr, |
| 854 | int_x_white, int_y_white, int_x_red, int_y_red, int_x_green, |
| 855 | int_y_green, int_x_blue, int_y_blue); |
| 856 | #endif |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 857 | if (png_crc_finish(png_ptr, 0)) |
| 858 | return; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 859 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 860 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 861 | |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 862 | #if defined(PNG_READ_sRGB_SUPPORTED) |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 863 | void /* PRIVATE */ |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 864 | png_handle_sRGB(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) |
| 865 | { |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 866 | int intent; |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 867 | png_byte buf[1]; |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 868 | |
| 869 | png_debug(1, "in png_handle_sRGB\n"); |
| 870 | |
| 871 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
| 872 | png_error(png_ptr, "Missing IHDR before sRGB"); |
| 873 | else if (png_ptr->mode & PNG_HAVE_IDAT) |
| 874 | { |
| 875 | png_warning(png_ptr, "Invalid sRGB after IDAT"); |
| 876 | png_crc_finish(png_ptr, length); |
| 877 | return; |
| 878 | } |
| 879 | else if (png_ptr->mode & PNG_HAVE_PLTE) |
| 880 | /* Should be an error, but we can cope with it */ |
| 881 | png_warning(png_ptr, "Out of place sRGB chunk"); |
Glenn Randers-Pehrson | 2687fcc | 1998-01-07 20:54:20 -0600 | [diff] [blame] | 882 | |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 883 | else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sRGB)) |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 884 | { |
| 885 | png_warning(png_ptr, "Duplicate sRGB chunk"); |
| 886 | png_crc_finish(png_ptr, length); |
| 887 | return; |
| 888 | } |
| 889 | |
| 890 | if (length != 1) |
| 891 | { |
| 892 | png_warning(png_ptr, "Incorrect sRGB chunk length"); |
| 893 | png_crc_finish(png_ptr, length); |
| 894 | return; |
| 895 | } |
| 896 | |
| 897 | png_crc_read(png_ptr, buf, 1); |
| 898 | if (png_crc_finish(png_ptr, 0)) |
| 899 | return; |
| 900 | |
| 901 | intent = buf[0]; |
| 902 | /* check for bad intent */ |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 903 | if (intent >= PNG_sRGB_INTENT_LAST) |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 904 | { |
| 905 | png_warning(png_ptr, "Unknown sRGB intent"); |
| 906 | return; |
| 907 | } |
| 908 | |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 909 | #if defined(PNG_READ_gAMA_SUPPORTED) && defined(PNG_READ_GAMMA_SUPPORTED) |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 910 | if ((info_ptr->valid & PNG_INFO_gAMA)) |
| 911 | { |
| 912 | int igamma; |
| 913 | #ifdef PNG_FIXED_POINT_SUPPORTED |
| 914 | igamma=(int)info_ptr->int_gamma; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 915 | #else |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 916 | # ifdef PNG_FLOATING_POINT_SUPPORTED |
Glenn Randers-Pehrson | 6942d53 | 2000-05-01 09:31:54 -0500 | [diff] [blame] | 917 | igamma=(int)(info_ptr->gamma * 100000.); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 918 | # endif |
| 919 | #endif |
Glenn Randers-Pehrson | 6942d53 | 2000-05-01 09:31:54 -0500 | [diff] [blame] | 920 | #if 0 && defined(PNG_cHRM_SUPPORTED) && !defined(PNG_FIXED_POINT_SUPPORTED) |
| 921 | /* We need to define these here because they aren't in png.h */ |
| 922 | png_fixed_point int_x_white; |
| 923 | png_fixed_point int_y_white; |
| 924 | png_fixed_point int_x_red; |
| 925 | png_fixed_point int_y_red; |
| 926 | png_fixed_point int_x_green; |
| 927 | png_fixed_point int_y_green; |
| 928 | png_fixed_point int_x_blue; |
| 929 | png_fixed_point int_y_blue; |
| 930 | #endif |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 931 | if(igamma < 45000L || igamma > 46000L) |
Glenn Randers-Pehrson | 2687fcc | 1998-01-07 20:54:20 -0600 | [diff] [blame] | 932 | { |
| 933 | png_warning(png_ptr, |
| 934 | "Ignoring incorrect gAMA value when sRGB is also present"); |
Glenn Randers-Pehrson | 316f97a | 2000-07-08 13:19:41 -0500 | [diff] [blame] | 935 | #ifndef PNG_NO_CONSOLE_IO |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 936 | # ifdef PNG_FIXED_POINT_SUPPORTED |
| 937 | fprintf(stderr,"incorrect gamma=(%d/100000)\n",(int)png_ptr->int_gamma); |
| 938 | # else |
| 939 | # ifdef PNG_FLOATING_POINT_SUPPORTED |
| 940 | fprintf(stderr,"incorrect gamma=%f\n",png_ptr->gamma); |
| 941 | # endif |
| 942 | # endif |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 943 | #endif |
Glenn Randers-Pehrson | 2687fcc | 1998-01-07 20:54:20 -0600 | [diff] [blame] | 944 | } |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 945 | } |
Glenn Randers-Pehrson | 2687fcc | 1998-01-07 20:54:20 -0600 | [diff] [blame] | 946 | #endif /* PNG_READ_gAMA_SUPPORTED */ |
| 947 | |
| 948 | #ifdef PNG_READ_cHRM_SUPPORTED |
Glenn Randers-Pehrson | 6942d53 | 2000-05-01 09:31:54 -0500 | [diff] [blame] | 949 | #ifdef PNG_FIXED_POINT_SUPPORTED |
Glenn Randers-Pehrson | 2687fcc | 1998-01-07 20:54:20 -0600 | [diff] [blame] | 950 | if (info_ptr->valid & PNG_INFO_cHRM) |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 951 | if (abs(info_ptr->int_x_white - 31270L) > 1000 || |
| 952 | abs(info_ptr->int_y_white - 32900L) > 1000 || |
| 953 | abs( info_ptr->int_x_red - 64000L) > 1000 || |
| 954 | abs( info_ptr->int_y_red - 33000L) > 1000 || |
| 955 | abs(info_ptr->int_x_green - 30000L) > 1000 || |
| 956 | abs(info_ptr->int_y_green - 60000L) > 1000 || |
| 957 | abs( info_ptr->int_x_blue - 15000L) > 1000 || |
| 958 | abs( info_ptr->int_y_blue - 6000L) > 1000) |
Glenn Randers-Pehrson | 2687fcc | 1998-01-07 20:54:20 -0600 | [diff] [blame] | 959 | { |
| 960 | png_warning(png_ptr, |
| 961 | "Ignoring incorrect cHRM value when sRGB is also present"); |
| 962 | } |
Glenn Randers-Pehrson | 6942d53 | 2000-05-01 09:31:54 -0500 | [diff] [blame] | 963 | #endif /* PNG_FIXED_POINT_SUPPORTED */ |
Glenn Randers-Pehrson | 2687fcc | 1998-01-07 20:54:20 -0600 | [diff] [blame] | 964 | #endif /* PNG_READ_cHRM_SUPPORTED */ |
| 965 | |
| 966 | png_set_sRGB_gAMA_and_cHRM(png_ptr, info_ptr, intent); |
| 967 | } |
| 968 | #endif /* PNG_READ_sRGB_SUPPORTED */ |
| 969 | |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 970 | #if defined(PNG_READ_iCCP_SUPPORTED) |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 971 | void /* PRIVATE */ |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 972 | png_handle_iCCP(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) |
| 973 | /* Note: this does not properly handle chunks that are > 64K under DOS */ |
| 974 | { |
| 975 | png_charp chunkdata; |
| 976 | png_byte compression_type; |
| 977 | png_charp profile; |
| 978 | png_uint_32 skip = 0; |
Glenn Randers-Pehrson | 68ea243 | 2000-04-01 21:10:05 -0600 | [diff] [blame] | 979 | png_size_t slength, prefix_length, data_length; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 980 | |
| 981 | png_debug(1, "in png_handle_iCCP\n"); |
| 982 | |
| 983 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
| 984 | png_error(png_ptr, "Missing IHDR before iCCP"); |
| 985 | else if (png_ptr->mode & PNG_HAVE_IDAT) |
| 986 | { |
| 987 | png_warning(png_ptr, "Invalid iCCP after IDAT"); |
| 988 | png_crc_finish(png_ptr, length); |
| 989 | return; |
| 990 | } |
| 991 | else if (png_ptr->mode & PNG_HAVE_PLTE) |
| 992 | /* Should be an error, but we can cope with it */ |
| 993 | png_warning(png_ptr, "Out of place iCCP chunk"); |
| 994 | |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 995 | else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_iCCP)) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 996 | { |
| 997 | png_warning(png_ptr, "Duplicate iCCP chunk"); |
| 998 | png_crc_finish(png_ptr, length); |
| 999 | return; |
| 1000 | } |
| 1001 | |
| 1002 | #ifdef PNG_MAX_MALLOC_64K |
| 1003 | if (length > (png_uint_32)65535L) |
| 1004 | { |
| 1005 | png_warning(png_ptr, "iCCP chunk too large to fit in memory"); |
| 1006 | skip = length - (png_uint_32)65535L; |
| 1007 | length = (png_uint_32)65535L; |
| 1008 | } |
| 1009 | #endif |
| 1010 | |
| 1011 | chunkdata = (png_charp)png_malloc(png_ptr, length + 1); |
| 1012 | slength = (png_size_t)length; |
| 1013 | png_crc_read(png_ptr, (png_bytep)chunkdata, slength); |
| 1014 | |
| 1015 | if (png_crc_finish(png_ptr, skip)) |
| 1016 | { |
| 1017 | png_free(png_ptr, chunkdata); |
| 1018 | return; |
| 1019 | } |
| 1020 | |
| 1021 | chunkdata[slength] = 0x00; |
| 1022 | |
| 1023 | for (profile = chunkdata; *profile; profile++) |
| 1024 | /* empty loop to find end of name */ ; |
Glenn Randers-Pehrson | d436672 | 2000-06-04 14:29:29 -0500 | [diff] [blame] | 1025 | |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1026 | ++profile; |
| 1027 | |
Glenn Randers-Pehrson | d436672 | 2000-06-04 14:29:29 -0500 | [diff] [blame] | 1028 | /* there should be at least one zero (the compression type byte) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1029 | following the separator, and we should be on it */ |
Glenn Randers-Pehrson | d436672 | 2000-06-04 14:29:29 -0500 | [diff] [blame] | 1030 | if (*profile || profile >= chunkdata + slength) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1031 | { |
| 1032 | png_free(png_ptr, chunkdata); |
Glenn Randers-Pehrson | 4accabb | 2000-04-14 14:20:47 -0500 | [diff] [blame] | 1033 | png_warning(png_ptr, "malformed iCCP chunk"); |
| 1034 | return; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1035 | } |
| 1036 | |
Glenn Randers-Pehrson | d436672 | 2000-06-04 14:29:29 -0500 | [diff] [blame] | 1037 | /* compression_type should always be zero */ |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1038 | compression_type = *profile++; |
| 1039 | |
| 1040 | prefix_length = profile - chunkdata; |
| 1041 | chunkdata = png_decompress_chunk(png_ptr, compression_type, chunkdata, |
Glenn Randers-Pehrson | 68ea243 | 2000-04-01 21:10:05 -0600 | [diff] [blame] | 1042 | slength, prefix_length, &data_length); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1043 | |
| 1044 | png_set_iCCP(png_ptr, info_ptr, chunkdata, compression_type, |
Glenn Randers-Pehrson | 68ea243 | 2000-04-01 21:10:05 -0600 | [diff] [blame] | 1045 | chunkdata + prefix_length, data_length); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1046 | png_free(png_ptr, chunkdata); |
| 1047 | } |
| 1048 | #endif /* PNG_READ_iCCP_SUPPORTED */ |
| 1049 | |
| 1050 | #if defined(PNG_READ_sPLT_SUPPORTED) |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 1051 | void /* PRIVATE */ |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1052 | png_handle_sPLT(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) |
| 1053 | /* Note: this does not properly handle chunks that are > 64K under DOS */ |
| 1054 | { |
| 1055 | png_bytep chunkdata; |
| 1056 | png_bytep entry_start; |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 1057 | png_sPLT_t new_palette; |
Glenn Randers-Pehrson | d436672 | 2000-06-04 14:29:29 -0500 | [diff] [blame] | 1058 | #ifdef PNG_NO_POINTER_INDEXING |
| 1059 | png_sPLT_entryp pp; |
| 1060 | #endif |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1061 | int data_length, entry_size, i; |
| 1062 | png_uint_32 skip = 0; |
| 1063 | png_size_t slength; |
| 1064 | |
| 1065 | png_debug(1, "in png_handle_sPLT\n"); |
| 1066 | |
| 1067 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
| 1068 | png_error(png_ptr, "Missing IHDR before sPLT"); |
| 1069 | else if (png_ptr->mode & PNG_HAVE_IDAT) |
| 1070 | { |
| 1071 | png_warning(png_ptr, "Invalid sPLT after IDAT"); |
| 1072 | png_crc_finish(png_ptr, length); |
| 1073 | return; |
| 1074 | } |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1075 | |
| 1076 | #ifdef PNG_MAX_MALLOC_64K |
| 1077 | if (length > (png_uint_32)65535L) |
| 1078 | { |
| 1079 | png_warning(png_ptr, "sPLT chunk too large to fit in memory"); |
| 1080 | skip = length - (png_uint_32)65535L; |
| 1081 | length = (png_uint_32)65535L; |
| 1082 | } |
| 1083 | #endif |
| 1084 | |
| 1085 | chunkdata = (png_bytep)png_malloc(png_ptr, length + 1); |
| 1086 | slength = (png_size_t)length; |
| 1087 | png_crc_read(png_ptr, chunkdata, slength); |
| 1088 | |
| 1089 | if (png_crc_finish(png_ptr, skip)) |
| 1090 | { |
| 1091 | png_free(png_ptr, chunkdata); |
| 1092 | return; |
| 1093 | } |
| 1094 | |
| 1095 | chunkdata[slength] = 0x00; |
| 1096 | |
| 1097 | for (entry_start = chunkdata; *entry_start; entry_start++) |
| 1098 | /* empty loop to find end of name */ ; |
| 1099 | ++entry_start; |
| 1100 | |
| 1101 | /* a sample depth should follow the separator, and we should be on it */ |
| 1102 | if (entry_start > chunkdata + slength) |
| 1103 | { |
| 1104 | png_free(png_ptr, chunkdata); |
Glenn Randers-Pehrson | 4accabb | 2000-04-14 14:20:47 -0500 | [diff] [blame] | 1105 | png_warning(png_ptr, "malformed sPLT chunk"); |
| 1106 | return; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1107 | } |
| 1108 | |
| 1109 | new_palette.depth = *entry_start++; |
| 1110 | entry_size = (new_palette.depth == 8 ? 6 : 10); |
| 1111 | data_length = (slength - (entry_start - chunkdata)); |
| 1112 | |
| 1113 | /* integrity-check the data length */ |
| 1114 | if (data_length % entry_size) |
| 1115 | { |
| 1116 | png_free(png_ptr, chunkdata); |
| 1117 | png_error(png_ptr, "sPLT chunk has bad length"); |
| 1118 | } |
| 1119 | |
| 1120 | new_palette.nentries = data_length / entry_size; |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 1121 | new_palette.entries = (png_sPLT_entryp)png_malloc( |
| 1122 | png_ptr, new_palette.nentries * sizeof(png_sPLT_entry)); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1123 | |
Glenn Randers-Pehrson | d436672 | 2000-06-04 14:29:29 -0500 | [diff] [blame] | 1124 | #ifndef PNG_NO_POINTER_INDEXING |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1125 | for (i = 0; i < new_palette.nentries; i++) |
| 1126 | { |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 1127 | png_sPLT_entryp pp = new_palette.entries + i; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1128 | |
| 1129 | if (new_palette.depth == 8) |
| 1130 | { |
| 1131 | pp->red = *entry_start++; |
| 1132 | pp->green = *entry_start++; |
| 1133 | pp->blue = *entry_start++; |
| 1134 | pp->alpha = *entry_start++; |
| 1135 | } |
| 1136 | else |
| 1137 | { |
| 1138 | pp->red = png_get_uint_16(entry_start); entry_start += 2; |
| 1139 | pp->green = png_get_uint_16(entry_start); entry_start += 2; |
| 1140 | pp->blue = png_get_uint_16(entry_start); entry_start += 2; |
| 1141 | pp->alpha = png_get_uint_16(entry_start); entry_start += 2; |
| 1142 | } |
| 1143 | pp->frequency = png_get_uint_16(entry_start); entry_start += 2; |
| 1144 | } |
Glenn Randers-Pehrson | d436672 | 2000-06-04 14:29:29 -0500 | [diff] [blame] | 1145 | #else |
| 1146 | pp = new_palette.entries; |
| 1147 | for (i = 0; i < new_palette.nentries; i++) |
| 1148 | { |
| 1149 | |
| 1150 | if (new_palette.depth == 8) |
| 1151 | { |
| 1152 | pp[i].red = *entry_start++; |
| 1153 | pp[i].green = *entry_start++; |
| 1154 | pp[i].blue = *entry_start++; |
| 1155 | pp[i].alpha = *entry_start++; |
| 1156 | } |
| 1157 | else |
| 1158 | { |
| 1159 | pp[i].red = png_get_uint_16(entry_start); entry_start += 2; |
| 1160 | pp[i].green = png_get_uint_16(entry_start); entry_start += 2; |
| 1161 | pp[i].blue = png_get_uint_16(entry_start); entry_start += 2; |
| 1162 | pp[i].alpha = png_get_uint_16(entry_start); entry_start += 2; |
| 1163 | } |
| 1164 | pp->frequency = png_get_uint_16(entry_start); entry_start += 2; |
| 1165 | } |
| 1166 | #endif |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1167 | |
| 1168 | /* discard all chunk data except the name and stash that */ |
| 1169 | new_palette.name = (png_charp)chunkdata; |
| 1170 | |
Glenn Randers-Pehrson | a77ef62 | 2000-02-18 13:48:52 -0600 | [diff] [blame] | 1171 | png_set_sPLT(png_ptr, info_ptr, &new_palette, 1); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1172 | |
| 1173 | png_free(png_ptr, chunkdata); |
| 1174 | png_free(png_ptr, new_palette.entries); |
| 1175 | } |
| 1176 | #endif /* PNG_READ_sPLT_SUPPORTED */ |
| 1177 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1178 | #if defined(PNG_READ_tRNS_SUPPORTED) |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 1179 | void /* PRIVATE */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1180 | 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] | 1181 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1182 | png_debug(1, "in png_handle_tRNS\n"); |
| 1183 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1184 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
| 1185 | png_error(png_ptr, "Missing IHDR before tRNS"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1186 | else if (png_ptr->mode & PNG_HAVE_IDAT) |
| 1187 | { |
| 1188 | png_warning(png_ptr, "Invalid tRNS after IDAT"); |
| 1189 | png_crc_finish(png_ptr, length); |
| 1190 | return; |
| 1191 | } |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1192 | else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS)) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1193 | { |
Glenn Randers-Pehrson | a357b99 | 1998-02-08 20:56:40 -0600 | [diff] [blame] | 1194 | png_warning(png_ptr, "Duplicate tRNS chunk"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1195 | png_crc_finish(png_ptr, length); |
| 1196 | return; |
| 1197 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1198 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1199 | if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) |
| 1200 | { |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1201 | if (!(png_ptr->mode & PNG_HAVE_PLTE)) |
| 1202 | { |
| 1203 | /* Should be an error, but we can cope with it */ |
| 1204 | png_warning(png_ptr, "Missing PLTE before tRNS"); |
| 1205 | } |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 1206 | else if (length > (png_uint_32)png_ptr->num_palette) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1207 | { |
Guy Schalnat | 69b1448 | 1996-01-10 02:56:49 -0600 | [diff] [blame] | 1208 | png_warning(png_ptr, "Incorrect tRNS chunk length"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1209 | png_crc_finish(png_ptr, length); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1210 | return; |
| 1211 | } |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1212 | if (length == 0) |
| 1213 | { |
| 1214 | png_warning(png_ptr, "Zero length tRNS chunk"); |
| 1215 | png_crc_finish(png_ptr, length); |
| 1216 | return; |
| 1217 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1218 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1219 | png_ptr->trans = (png_bytep)png_malloc(png_ptr, length); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1220 | png_crc_read(png_ptr, png_ptr->trans, (png_size_t)length); |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1221 | png_ptr->num_trans = (png_uint_16)length; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1222 | } |
| 1223 | else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB) |
| 1224 | { |
| 1225 | png_byte buf[6]; |
| 1226 | |
| 1227 | if (length != 6) |
| 1228 | { |
Guy Schalnat | 69b1448 | 1996-01-10 02:56:49 -0600 | [diff] [blame] | 1229 | png_warning(png_ptr, "Incorrect tRNS chunk length"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1230 | png_crc_finish(png_ptr, length); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1231 | return; |
| 1232 | } |
| 1233 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1234 | png_crc_read(png_ptr, buf, (png_size_t)length); |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 1235 | png_ptr->num_trans = 1; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1236 | png_ptr->trans_values.red = png_get_uint_16(buf); |
| 1237 | png_ptr->trans_values.green = png_get_uint_16(buf + 2); |
| 1238 | png_ptr->trans_values.blue = png_get_uint_16(buf + 4); |
| 1239 | } |
| 1240 | else if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY) |
| 1241 | { |
| 1242 | png_byte buf[6]; |
| 1243 | |
| 1244 | if (length != 2) |
| 1245 | { |
Guy Schalnat | 69b1448 | 1996-01-10 02:56:49 -0600 | [diff] [blame] | 1246 | png_warning(png_ptr, "Incorrect tRNS chunk length"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1247 | png_crc_finish(png_ptr, length); |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1248 | return; |
| 1249 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1250 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1251 | png_crc_read(png_ptr, buf, 2); |
| 1252 | png_ptr->num_trans = 1; |
| 1253 | png_ptr->trans_values.gray = png_get_uint_16(buf); |
| 1254 | } |
| 1255 | else |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1256 | { |
| 1257 | png_warning(png_ptr, "tRNS chunk not allowed with alpha channel"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1258 | png_crc_finish(png_ptr, length); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1259 | return; |
| 1260 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1261 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1262 | if (png_crc_finish(png_ptr, 0)) |
| 1263 | return; |
| 1264 | |
Glenn Randers-Pehrson | 6942d53 | 2000-05-01 09:31:54 -0500 | [diff] [blame] | 1265 | #ifdef PNG_FREE_ME_SUPPORTED |
Glenn Randers-Pehrson | 38e6e77 | 2000-04-09 19:06:13 -0500 | [diff] [blame] | 1266 | png_free_data(png_ptr, info_ptr, PNG_FREE_TRNS, 0); |
Glenn Randers-Pehrson | ff9c947 | 2000-07-11 07:12:36 -0500 | [diff] [blame] | 1267 | if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) |
| 1268 | png_ptr->free_me |= PNG_FREE_TRNS; |
Glenn Randers-Pehrson | 6942d53 | 2000-05-01 09:31:54 -0500 | [diff] [blame] | 1269 | #else |
Glenn Randers-Pehrson | ff9c947 | 2000-07-11 07:12:36 -0500 | [diff] [blame] | 1270 | if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) |
| 1271 | png_ptr->flags |= PNG_FLAG_FREE_TRNS; |
Glenn Randers-Pehrson | 6942d53 | 2000-05-01 09:31:54 -0500 | [diff] [blame] | 1272 | #endif |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1273 | 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] | 1274 | &(png_ptr->trans_values)); |
| 1275 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1276 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1277 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1278 | #if defined(PNG_READ_bKGD_SUPPORTED) |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 1279 | void /* PRIVATE */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1280 | 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] | 1281 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1282 | png_size_t truelen; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1283 | png_byte buf[6]; |
| 1284 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1285 | png_debug(1, "in png_handle_bKGD\n"); |
| 1286 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1287 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
| 1288 | png_error(png_ptr, "Missing IHDR before bKGD"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1289 | else if (png_ptr->mode & PNG_HAVE_IDAT) |
| 1290 | { |
| 1291 | png_warning(png_ptr, "Invalid bKGD after IDAT"); |
| 1292 | png_crc_finish(png_ptr, length); |
| 1293 | return; |
| 1294 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1295 | else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE && |
| 1296 | !(png_ptr->mode & PNG_HAVE_PLTE)) |
| 1297 | { |
| 1298 | png_warning(png_ptr, "Missing PLTE before bKGD"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1299 | png_crc_finish(png_ptr, length); |
| 1300 | return; |
| 1301 | } |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1302 | else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_bKGD)) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1303 | { |
| 1304 | png_warning(png_ptr, "Duplicate bKGD chunk"); |
| 1305 | png_crc_finish(png_ptr, length); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1306 | return; |
| 1307 | } |
| 1308 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1309 | if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) |
| 1310 | truelen = 1; |
| 1311 | else if (png_ptr->color_type & PNG_COLOR_MASK_COLOR) |
| 1312 | truelen = 6; |
| 1313 | else |
| 1314 | truelen = 2; |
| 1315 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1316 | if (length != truelen) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1317 | { |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1318 | png_warning(png_ptr, "Incorrect bKGD chunk length"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1319 | png_crc_finish(png_ptr, length); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1320 | return; |
| 1321 | } |
| 1322 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1323 | png_crc_read(png_ptr, buf, truelen); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1324 | if (png_crc_finish(png_ptr, 0)) |
| 1325 | return; |
| 1326 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1327 | /* We convert the index value into RGB components so that we can allow |
| 1328 | * arbitrary RGB values for background when we have transparency, and |
| 1329 | * 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] | 1330 | * from the info_ptr struct. */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1331 | if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1332 | { |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1333 | png_ptr->background.index = buf[0]; |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 1334 | if(info_ptr->num_palette) |
| 1335 | { |
| 1336 | if(buf[0] > info_ptr->num_palette) |
| 1337 | { |
| 1338 | png_warning(png_ptr, "Incorrect bKGD chunk index value"); |
| 1339 | png_crc_finish(png_ptr, length); |
| 1340 | return; |
| 1341 | } |
| 1342 | png_ptr->background.red = |
| 1343 | (png_uint_16)png_ptr->palette[buf[0]].red; |
| 1344 | png_ptr->background.green = |
| 1345 | (png_uint_16)png_ptr->palette[buf[0]].green; |
| 1346 | png_ptr->background.blue = |
| 1347 | (png_uint_16)png_ptr->palette[buf[0]].blue; |
| 1348 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1349 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1350 | else if (!(png_ptr->color_type & PNG_COLOR_MASK_COLOR)) /* GRAY */ |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1351 | { |
| 1352 | png_ptr->background.red = |
| 1353 | png_ptr->background.green = |
| 1354 | png_ptr->background.blue = |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1355 | png_ptr->background.gray = png_get_uint_16(buf); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1356 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1357 | else |
| 1358 | { |
| 1359 | png_ptr->background.red = png_get_uint_16(buf); |
| 1360 | png_ptr->background.green = png_get_uint_16(buf + 2); |
| 1361 | png_ptr->background.blue = png_get_uint_16(buf + 4); |
| 1362 | } |
| 1363 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1364 | png_set_bKGD(png_ptr, info_ptr, &(png_ptr->background)); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1365 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1366 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1367 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1368 | #if defined(PNG_READ_hIST_SUPPORTED) |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 1369 | void /* PRIVATE */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1370 | 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] | 1371 | { |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1372 | int num, i; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1373 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1374 | png_debug(1, "in png_handle_hIST\n"); |
| 1375 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1376 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
| 1377 | png_error(png_ptr, "Missing IHDR before hIST"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1378 | else if (png_ptr->mode & PNG_HAVE_IDAT) |
| 1379 | { |
| 1380 | png_warning(png_ptr, "Invalid hIST after IDAT"); |
| 1381 | png_crc_finish(png_ptr, length); |
| 1382 | return; |
| 1383 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1384 | else if (!(png_ptr->mode & PNG_HAVE_PLTE)) |
| 1385 | { |
| 1386 | png_warning(png_ptr, "Missing PLTE before hIST"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1387 | png_crc_finish(png_ptr, length); |
| 1388 | return; |
| 1389 | } |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1390 | else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_hIST)) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1391 | { |
| 1392 | png_warning(png_ptr, "Duplicate hIST chunk"); |
| 1393 | png_crc_finish(png_ptr, length); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1394 | return; |
| 1395 | } |
| 1396 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1397 | if (length != (png_uint_32)(2 * png_ptr->num_palette)) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1398 | { |
| 1399 | png_warning(png_ptr, "Incorrect hIST chunk length"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1400 | png_crc_finish(png_ptr, length); |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1401 | return; |
| 1402 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1403 | |
Glenn Randers-Pehrson | bcfd15d | 1999-10-01 14:22:25 -0500 | [diff] [blame] | 1404 | num = (int)length / 2 ; |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1405 | png_ptr->hist = (png_uint_16p)png_malloc(png_ptr, |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame] | 1406 | (png_uint_32)(num * sizeof (png_uint_16))); |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1407 | for (i = 0; i < num; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1408 | { |
| 1409 | png_byte buf[2]; |
| 1410 | |
| 1411 | png_crc_read(png_ptr, buf, 2); |
| 1412 | png_ptr->hist[i] = png_get_uint_16(buf); |
| 1413 | } |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1414 | |
| 1415 | if (png_crc_finish(png_ptr, 0)) |
| 1416 | return; |
| 1417 | |
Glenn Randers-Pehrson | 6942d53 | 2000-05-01 09:31:54 -0500 | [diff] [blame] | 1418 | #ifdef PNG_FREE_ME_SUPPORTED |
Glenn Randers-Pehrson | 38e6e77 | 2000-04-09 19:06:13 -0500 | [diff] [blame] | 1419 | png_free_data(png_ptr, info_ptr, PNG_FREE_HIST, 0); |
| 1420 | png_ptr->free_me |= PNG_FREE_HIST; |
Glenn Randers-Pehrson | 6942d53 | 2000-05-01 09:31:54 -0500 | [diff] [blame] | 1421 | #else |
| 1422 | png_ptr->flags |= PNG_FLAG_FREE_HIST; |
| 1423 | #endif |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1424 | png_set_hIST(png_ptr, info_ptr, png_ptr->hist); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1425 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1426 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1427 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1428 | #if defined(PNG_READ_pHYs_SUPPORTED) |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 1429 | void /* PRIVATE */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1430 | 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] | 1431 | { |
| 1432 | png_byte buf[9]; |
| 1433 | png_uint_32 res_x, res_y; |
| 1434 | int unit_type; |
| 1435 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1436 | png_debug(1, "in png_handle_pHYs\n"); |
| 1437 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1438 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
Glenn Randers-Pehrson | 104622b | 2000-05-29 08:58:03 -0500 | [diff] [blame] | 1439 | png_error(png_ptr, "Missing IHDR before pHYs"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1440 | else if (png_ptr->mode & PNG_HAVE_IDAT) |
| 1441 | { |
Glenn Randers-Pehrson | 104622b | 2000-05-29 08:58:03 -0500 | [diff] [blame] | 1442 | png_warning(png_ptr, "Invalid pHYs after IDAT"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1443 | png_crc_finish(png_ptr, length); |
| 1444 | return; |
| 1445 | } |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1446 | else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pHYs)) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1447 | { |
Glenn Randers-Pehrson | 104622b | 2000-05-29 08:58:03 -0500 | [diff] [blame] | 1448 | png_warning(png_ptr, "Duplicate pHYs chunk"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1449 | png_crc_finish(png_ptr, length); |
| 1450 | return; |
| 1451 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1452 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1453 | if (length != 9) |
| 1454 | { |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1455 | png_warning(png_ptr, "Incorrect pHYs chunk length"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1456 | png_crc_finish(png_ptr, length); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1457 | return; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1458 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1459 | |
| 1460 | png_crc_read(png_ptr, buf, 9); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1461 | if (png_crc_finish(png_ptr, 0)) |
| 1462 | return; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1463 | |
| 1464 | res_x = png_get_uint_32(buf); |
| 1465 | res_y = png_get_uint_32(buf + 4); |
| 1466 | unit_type = buf[8]; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1467 | 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] | 1468 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1469 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1470 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1471 | #if defined(PNG_READ_oFFs_SUPPORTED) |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 1472 | void /* PRIVATE */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1473 | 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] | 1474 | { |
| 1475 | png_byte buf[9]; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1476 | png_int_32 offset_x, offset_y; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1477 | int unit_type; |
| 1478 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1479 | png_debug(1, "in png_handle_oFFs\n"); |
| 1480 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1481 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
| 1482 | png_error(png_ptr, "Missing IHDR before oFFs"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1483 | else if (png_ptr->mode & PNG_HAVE_IDAT) |
| 1484 | { |
| 1485 | png_warning(png_ptr, "Invalid oFFs after IDAT"); |
| 1486 | png_crc_finish(png_ptr, length); |
| 1487 | return; |
| 1488 | } |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1489 | else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_oFFs)) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1490 | { |
| 1491 | png_warning(png_ptr, "Duplicate oFFs chunk"); |
| 1492 | png_crc_finish(png_ptr, length); |
| 1493 | return; |
| 1494 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1495 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1496 | if (length != 9) |
| 1497 | { |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1498 | png_warning(png_ptr, "Incorrect oFFs chunk length"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1499 | png_crc_finish(png_ptr, length); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1500 | return; |
| 1501 | } |
| 1502 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1503 | png_crc_read(png_ptr, buf, 9); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1504 | if (png_crc_finish(png_ptr, 0)) |
| 1505 | return; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1506 | |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1507 | offset_x = png_get_int_32(buf); |
| 1508 | offset_y = png_get_int_32(buf + 4); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1509 | unit_type = buf[8]; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1510 | png_set_oFFs(png_ptr, info_ptr, offset_x, offset_y, unit_type); |
| 1511 | } |
| 1512 | #endif |
| 1513 | |
| 1514 | #if defined(PNG_READ_pCAL_SUPPORTED) |
Glenn Randers-Pehrson | ff9c947 | 2000-07-11 07:12:36 -0500 | [diff] [blame] | 1515 | /* read the pCAL chunk (described in the PNG Extensions document) */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 1516 | void /* PRIVATE */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1517 | png_handle_pCAL(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) |
| 1518 | { |
| 1519 | png_charp purpose; |
| 1520 | png_int_32 X0, X1; |
| 1521 | png_byte type, nparams; |
| 1522 | png_charp buf, units, endptr; |
| 1523 | png_charpp params; |
Glenn Randers-Pehrson | a357b99 | 1998-02-08 20:56:40 -0600 | [diff] [blame] | 1524 | png_size_t slength; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1525 | int i; |
| 1526 | |
| 1527 | png_debug(1, "in png_handle_pCAL\n"); |
| 1528 | |
| 1529 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
| 1530 | png_error(png_ptr, "Missing IHDR before pCAL"); |
| 1531 | else if (png_ptr->mode & PNG_HAVE_IDAT) |
| 1532 | { |
| 1533 | png_warning(png_ptr, "Invalid pCAL after IDAT"); |
| 1534 | png_crc_finish(png_ptr, length); |
| 1535 | return; |
| 1536 | } |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1537 | else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pCAL)) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1538 | { |
| 1539 | png_warning(png_ptr, "Duplicate pCAL chunk"); |
| 1540 | png_crc_finish(png_ptr, length); |
| 1541 | return; |
| 1542 | } |
| 1543 | |
Glenn Randers-Pehrson | 4766a24 | 2000-07-17 06:17:09 -0500 | [diff] [blame^] | 1544 | png_debug1(2, "Allocating and reading pCAL chunk data (%lu bytes)\n", |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1545 | length + 1); |
| 1546 | purpose = (png_charp)png_malloc(png_ptr, length + 1); |
Glenn Randers-Pehrson | a357b99 | 1998-02-08 20:56:40 -0600 | [diff] [blame] | 1547 | slength = (png_size_t)length; |
| 1548 | png_crc_read(png_ptr, (png_bytep)purpose, slength); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1549 | |
| 1550 | if (png_crc_finish(png_ptr, 0)) |
| 1551 | { |
| 1552 | png_free(png_ptr, purpose); |
| 1553 | return; |
| 1554 | } |
| 1555 | |
Glenn Randers-Pehrson | a357b99 | 1998-02-08 20:56:40 -0600 | [diff] [blame] | 1556 | purpose[slength] = 0x00; /* null terminate the last string */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1557 | |
| 1558 | png_debug(3, "Finding end of pCAL purpose string\n"); |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1559 | for (buf = purpose; *buf; buf++) |
Glenn Randers-Pehrson | d12aa50 | 1998-03-13 07:39:39 -0600 | [diff] [blame] | 1560 | /* empty loop */ ; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1561 | |
Glenn Randers-Pehrson | a357b99 | 1998-02-08 20:56:40 -0600 | [diff] [blame] | 1562 | endptr = purpose + slength; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1563 | |
| 1564 | /* We need to have at least 12 bytes after the purpose string |
| 1565 | in order to get the parameter information. */ |
| 1566 | if (endptr <= buf + 12) |
| 1567 | { |
| 1568 | png_warning(png_ptr, "Invalid pCAL data"); |
| 1569 | png_free(png_ptr, purpose); |
| 1570 | return; |
| 1571 | } |
| 1572 | |
| 1573 | png_debug(3, "Reading pCAL X0, X1, type, nparams, and units\n"); |
| 1574 | X0 = png_get_int_32((png_bytep)buf+1); |
| 1575 | X1 = png_get_int_32((png_bytep)buf+5); |
| 1576 | type = buf[9]; |
| 1577 | nparams = buf[10]; |
| 1578 | units = buf + 11; |
| 1579 | |
| 1580 | png_debug(3, "Checking pCAL equation type and number of parameters\n"); |
| 1581 | /* Check that we have the right number of parameters for known |
| 1582 | equation types. */ |
| 1583 | if ((type == PNG_EQUATION_LINEAR && nparams != 2) || |
| 1584 | (type == PNG_EQUATION_BASE_E && nparams != 3) || |
| 1585 | (type == PNG_EQUATION_ARBITRARY && nparams != 3) || |
| 1586 | (type == PNG_EQUATION_HYPERBOLIC && nparams != 4)) |
| 1587 | { |
| 1588 | png_warning(png_ptr, "Invalid pCAL parameters for equation type"); |
| 1589 | png_free(png_ptr, purpose); |
| 1590 | return; |
| 1591 | } |
| 1592 | else if (type >= PNG_EQUATION_LAST) |
| 1593 | { |
| 1594 | png_warning(png_ptr, "Unrecognized equation type for pCAL chunk"); |
| 1595 | } |
| 1596 | |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1597 | for (buf = units; *buf; buf++) |
Glenn Randers-Pehrson | f9f2fe0 | 1998-03-15 18:20:23 -0600 | [diff] [blame] | 1598 | /* Empty loop to move past the units string. */ ; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1599 | |
| 1600 | png_debug(3, "Allocating pCAL parameters array\n"); |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame] | 1601 | params = (png_charpp)png_malloc(png_ptr, (png_uint_32)(nparams |
| 1602 | *sizeof(png_charp))) ; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1603 | |
| 1604 | /* Get pointers to the start of each parameter string. */ |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame] | 1605 | for (i = 0; i < (int)nparams; i++) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1606 | { |
| 1607 | buf++; /* Skip the null string terminator from previous parameter. */ |
| 1608 | |
| 1609 | png_debug1(3, "Reading pCAL parameter %d\n", i); |
Glenn Randers-Pehrson | d12aa50 | 1998-03-13 07:39:39 -0600 | [diff] [blame] | 1610 | for (params[i] = buf; *buf != 0x00 && buf <= endptr; buf++) |
Glenn Randers-Pehrson | f9f2fe0 | 1998-03-15 18:20:23 -0600 | [diff] [blame] | 1611 | /* Empty loop to move past each parameter string */ ; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1612 | |
| 1613 | /* Make sure we haven't run out of data yet */ |
| 1614 | if (buf > endptr) |
| 1615 | { |
| 1616 | png_warning(png_ptr, "Invalid pCAL data"); |
| 1617 | png_free(png_ptr, purpose); |
| 1618 | png_free(png_ptr, params); |
| 1619 | return; |
| 1620 | } |
| 1621 | } |
| 1622 | |
| 1623 | png_set_pCAL(png_ptr, info_ptr, purpose, X0, X1, type, nparams, |
| 1624 | units, params); |
| 1625 | |
| 1626 | png_free(png_ptr, purpose); |
| 1627 | png_free(png_ptr, params); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1628 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1629 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1630 | |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1631 | #if defined(PNG_READ_sCAL_SUPPORTED) |
| 1632 | /* read the sCAL chunk */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 1633 | void /* PRIVATE */ |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1634 | png_handle_sCAL(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) |
| 1635 | { |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1636 | png_charp buffer, ep; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1637 | #ifdef PNG_FLOATING_POINT_SUPPORTED |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 1638 | double width, height; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1639 | png_charp vp; |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1640 | #else |
| 1641 | #ifdef PNG_FIXED_POINT_SUPPORTED |
| 1642 | png_charp swidth, sheight; |
| 1643 | #endif |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1644 | #endif |
| 1645 | png_size_t slength; |
| 1646 | |
| 1647 | png_debug(1, "in png_handle_sCAL\n"); |
| 1648 | |
| 1649 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
| 1650 | png_error(png_ptr, "Missing IHDR before sCAL"); |
| 1651 | else if (png_ptr->mode & PNG_HAVE_IDAT) |
| 1652 | { |
| 1653 | png_warning(png_ptr, "Invalid sCAL after IDAT"); |
| 1654 | png_crc_finish(png_ptr, length); |
| 1655 | return; |
| 1656 | } |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1657 | else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sCAL)) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1658 | { |
| 1659 | png_warning(png_ptr, "Duplicate sCAL chunk"); |
| 1660 | png_crc_finish(png_ptr, length); |
| 1661 | return; |
| 1662 | } |
| 1663 | |
Glenn Randers-Pehrson | 4766a24 | 2000-07-17 06:17:09 -0500 | [diff] [blame^] | 1664 | png_debug1(2, "Allocating and reading sCAL chunk data (%lu bytes)\n", |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1665 | length + 1); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1666 | buffer = (png_charp)png_malloc(png_ptr, length + 1); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1667 | slength = (png_size_t)length; |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1668 | png_crc_read(png_ptr, (png_bytep)buffer, slength); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1669 | |
| 1670 | if (png_crc_finish(png_ptr, 0)) |
| 1671 | { |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1672 | png_free(png_ptr, buffer); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1673 | return; |
| 1674 | } |
| 1675 | |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1676 | buffer[slength] = 0x00; /* null terminate the last string */ |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1677 | |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1678 | ep = buffer + 1; /* skip unit byte */ |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1679 | |
| 1680 | #ifdef PNG_FLOATING_POINT_SUPPORTED |
| 1681 | width = strtod(ep, &vp); |
| 1682 | if (*vp) |
Glenn Randers-Pehrson | 4accabb | 2000-04-14 14:20:47 -0500 | [diff] [blame] | 1683 | { |
| 1684 | png_warning(png_ptr, "malformed width string in sCAL chunk"); |
| 1685 | return; |
| 1686 | } |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1687 | #else |
| 1688 | #ifdef PNG_FIXED_POINT_SUPPORTED |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1689 | swidth = (png_charp)png_malloc(png_ptr, strlen(ep) + 1); |
| 1690 | png_memcpy(swidth, ep, (png_size_t)strlen(ep)); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1691 | #endif |
| 1692 | #endif |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1693 | |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1694 | for (ep = buffer; *ep; ep++) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1695 | /* empty loop */ ; |
| 1696 | ep++; |
| 1697 | |
| 1698 | #ifdef PNG_FLOATING_POINT_SUPPORTED |
| 1699 | height = strtod(ep, &vp); |
| 1700 | if (*vp) |
Glenn Randers-Pehrson | 4accabb | 2000-04-14 14:20:47 -0500 | [diff] [blame] | 1701 | { |
| 1702 | png_warning(png_ptr, "malformed height string in sCAL chunk"); |
| 1703 | return; |
| 1704 | } |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1705 | #else |
| 1706 | #ifdef PNG_FIXED_POINT_SUPPORTED |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1707 | sheight = (png_charp)png_malloc(png_ptr, strlen(ep) + 1); |
| 1708 | png_memcpy(sheight, ep, (png_size_t)strlen(ep)); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1709 | #endif |
| 1710 | #endif |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1711 | |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1712 | if (buffer + slength < ep |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1713 | #ifdef PNG_FLOATING_POINT_SUPPORTED |
| 1714 | || width <= 0. || height <= 0. |
| 1715 | #endif |
| 1716 | ) |
| 1717 | { |
| 1718 | png_warning(png_ptr, "Invalid sCAL data"); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1719 | png_free(png_ptr, buffer); |
| 1720 | #if defined(PNG_FIXED_POINT_SUPPORTED) && !defined(PNG_FLOATING_POINT_SUPPORTED) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1721 | png_free(png_ptr, swidth); |
| 1722 | png_free(png_ptr, sheight); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1723 | #endif |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1724 | return; |
| 1725 | } |
| 1726 | |
| 1727 | |
| 1728 | #ifdef PNG_FLOATING_POINT_SUPPORTED |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1729 | png_set_sCAL(png_ptr, info_ptr, buffer[0], width, height); |
| 1730 | #else |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1731 | #ifdef PNG_FIXED_POINT_SUPPORTED |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1732 | png_set_sCAL_s(png_ptr, info_ptr, buffer[0], swidth, sheight); |
| 1733 | #endif |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1734 | #endif |
| 1735 | |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1736 | png_free(png_ptr, buffer); |
| 1737 | #if defined(PNG_FIXED_POINT_SUPPORTED) && !defined(PNG_FLOATING_POINT_SUPPORTED) |
| 1738 | png_free(png_ptr, swidth); |
| 1739 | png_free(png_ptr, sheight); |
| 1740 | #endif |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1741 | } |
| 1742 | #endif |
| 1743 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1744 | #if defined(PNG_READ_tIME_SUPPORTED) |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 1745 | void /* PRIVATE */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1746 | 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] | 1747 | { |
| 1748 | png_byte buf[7]; |
| 1749 | png_time mod_time; |
| 1750 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1751 | png_debug(1, "in png_handle_tIME\n"); |
| 1752 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1753 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1754 | png_error(png_ptr, "Out of place tIME chunk"); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1755 | else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tIME)) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1756 | { |
| 1757 | png_warning(png_ptr, "Duplicate tIME chunk"); |
| 1758 | png_crc_finish(png_ptr, length); |
| 1759 | return; |
| 1760 | } |
| 1761 | |
| 1762 | if (png_ptr->mode & PNG_HAVE_IDAT) |
| 1763 | png_ptr->mode |= PNG_AFTER_IDAT; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1764 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1765 | if (length != 7) |
| 1766 | { |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1767 | png_warning(png_ptr, "Incorrect tIME chunk length"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1768 | png_crc_finish(png_ptr, length); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1769 | return; |
| 1770 | } |
| 1771 | |
| 1772 | png_crc_read(png_ptr, buf, 7); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1773 | if (png_crc_finish(png_ptr, 0)) |
| 1774 | return; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1775 | |
| 1776 | mod_time.second = buf[6]; |
| 1777 | mod_time.minute = buf[5]; |
| 1778 | mod_time.hour = buf[4]; |
| 1779 | mod_time.day = buf[3]; |
| 1780 | mod_time.month = buf[2]; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1781 | mod_time.year = png_get_uint_16(buf); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1782 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1783 | png_set_tIME(png_ptr, info_ptr, &mod_time); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1784 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1785 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1786 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1787 | #if defined(PNG_READ_tEXt_SUPPORTED) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1788 | /* Note: this does not properly handle chunks that are > 64K under DOS */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 1789 | void /* PRIVATE */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1790 | 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] | 1791 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1792 | png_textp text_ptr; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1793 | png_charp key; |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 1794 | png_charp text; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1795 | png_uint_32 skip = 0; |
Glenn Randers-Pehrson | a357b99 | 1998-02-08 20:56:40 -0600 | [diff] [blame] | 1796 | png_size_t slength; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1797 | |
| 1798 | png_debug(1, "in png_handle_tEXt\n"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1799 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1800 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
| 1801 | png_error(png_ptr, "Missing IHDR before tEXt"); |
| 1802 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1803 | if (png_ptr->mode & PNG_HAVE_IDAT) |
| 1804 | png_ptr->mode |= PNG_AFTER_IDAT; |
| 1805 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1806 | #ifdef PNG_MAX_MALLOC_64K |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 1807 | if (length > (png_uint_32)65535L) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1808 | { |
| 1809 | 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] | 1810 | skip = length - (png_uint_32)65535L; |
| 1811 | length = (png_uint_32)65535L; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1812 | } |
| 1813 | #endif |
| 1814 | |
| 1815 | key = (png_charp)png_malloc(png_ptr, length + 1); |
Glenn Randers-Pehrson | a357b99 | 1998-02-08 20:56:40 -0600 | [diff] [blame] | 1816 | slength = (png_size_t)length; |
| 1817 | png_crc_read(png_ptr, (png_bytep)key, slength); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1818 | |
| 1819 | if (png_crc_finish(png_ptr, skip)) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1820 | { |
| 1821 | png_free(png_ptr, key); |
| 1822 | return; |
| 1823 | } |
| 1824 | |
Glenn Randers-Pehrson | a357b99 | 1998-02-08 20:56:40 -0600 | [diff] [blame] | 1825 | key[slength] = 0x00; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1826 | |
| 1827 | for (text = key; *text; text++) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1828 | /* empty loop to find end of key */ ; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1829 | |
Glenn Randers-Pehrson | a357b99 | 1998-02-08 20:56:40 -0600 | [diff] [blame] | 1830 | if (text != key + slength) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1831 | text++; |
| 1832 | |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame] | 1833 | 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] | 1834 | text_ptr->compression = PNG_TEXT_COMPRESSION_NONE; |
| 1835 | text_ptr->key = key; |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 1836 | #ifdef PNG_iTXt_SUPPORTED |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1837 | text_ptr->lang = NULL; |
| 1838 | text_ptr->lang_key = NULL; |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 1839 | text_ptr->itxt_length = 0; |
| 1840 | #endif |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1841 | text_ptr->text = text; |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1842 | text_ptr->text_length = png_strlen(text); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1843 | |
| 1844 | png_set_text(png_ptr, info_ptr, text_ptr, 1); |
| 1845 | |
Glenn Randers-Pehrson | 6d8f3b0 | 1999-10-23 08:39:18 -0500 | [diff] [blame] | 1846 | png_free(png_ptr, key); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1847 | png_free(png_ptr, text_ptr); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1848 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1849 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1850 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1851 | #if defined(PNG_READ_zTXt_SUPPORTED) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1852 | /* note: this does not correctly handle chunks that are > 64K under DOS */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 1853 | void /* PRIVATE */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1854 | 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] | 1855 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1856 | png_textp text_ptr; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1857 | png_charp chunkdata; |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 1858 | png_charp text; |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 1859 | int comp_type; |
Glenn Randers-Pehrson | 68ea243 | 2000-04-01 21:10:05 -0600 | [diff] [blame] | 1860 | png_size_t slength, prefix_len, data_len; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1861 | |
| 1862 | png_debug(1, "in png_handle_zTXt\n"); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1863 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
| 1864 | png_error(png_ptr, "Missing IHDR before zTXt"); |
| 1865 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1866 | if (png_ptr->mode & PNG_HAVE_IDAT) |
| 1867 | png_ptr->mode |= PNG_AFTER_IDAT; |
| 1868 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1869 | #ifdef PNG_MAX_MALLOC_64K |
| 1870 | /* We will no doubt have problems with chunks even half this size, but |
| 1871 | 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] | 1872 | if (length > (png_uint_32)65535L) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1873 | { |
| 1874 | png_warning(png_ptr,"zTXt chunk too large to fit in memory"); |
| 1875 | png_crc_finish(png_ptr, length); |
| 1876 | return; |
| 1877 | } |
| 1878 | #endif |
| 1879 | |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1880 | chunkdata = (png_charp)png_malloc(png_ptr, length + 1); |
Glenn Randers-Pehrson | a357b99 | 1998-02-08 20:56:40 -0600 | [diff] [blame] | 1881 | slength = (png_size_t)length; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1882 | png_crc_read(png_ptr, (png_bytep)chunkdata, slength); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1883 | if (png_crc_finish(png_ptr, 0)) |
| 1884 | { |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1885 | png_free(png_ptr, chunkdata); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1886 | return; |
| 1887 | } |
| 1888 | |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1889 | chunkdata[slength] = 0x00; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1890 | |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1891 | for (text = chunkdata; *text; text++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1892 | /* empty loop */ ; |
| 1893 | |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1894 | /* zTXt must have some text after the chunkdataword */ |
| 1895 | if (text == chunkdata + slength) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1896 | { |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1897 | comp_type = PNG_TEXT_COMPRESSION_NONE; |
Guy Schalnat | 69b1448 | 1996-01-10 02:56:49 -0600 | [diff] [blame] | 1898 | png_warning(png_ptr, "Zero length zTXt chunk"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1899 | } |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1900 | else |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1901 | { |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1902 | comp_type = *(++text); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1903 | text++; /* skip the compression_method byte */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1904 | } |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1905 | prefix_len = text - chunkdata; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1906 | |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1907 | chunkdata = (png_charp)png_decompress_chunk(png_ptr, comp_type, chunkdata, |
Glenn Randers-Pehrson | 68ea243 | 2000-04-01 21:10:05 -0600 | [diff] [blame] | 1908 | (png_size_t)length, prefix_len, &data_len); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1909 | |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame] | 1910 | 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] | 1911 | text_ptr->compression = comp_type; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1912 | text_ptr->key = chunkdata; |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 1913 | #ifdef PNG_iTXt_SUPPORTED |
| 1914 | text_ptr->lang = NULL; |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1915 | text_ptr->lang_key = NULL; |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 1916 | text_ptr->itxt_length = 0; |
| 1917 | #endif |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1918 | text_ptr->text = chunkdata + prefix_len; |
Glenn Randers-Pehrson | 68ea243 | 2000-04-01 21:10:05 -0600 | [diff] [blame] | 1919 | text_ptr->text_length = data_len; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1920 | |
| 1921 | png_set_text(png_ptr, info_ptr, text_ptr, 1); |
| 1922 | |
| 1923 | png_free(png_ptr, text_ptr); |
| 1924 | png_free(png_ptr, chunkdata); |
| 1925 | } |
| 1926 | #endif |
| 1927 | |
| 1928 | #if defined(PNG_READ_iTXt_SUPPORTED) |
| 1929 | /* note: this does not correctly handle chunks that are > 64K under DOS */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 1930 | void /* PRIVATE */ |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1931 | png_handle_iTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) |
| 1932 | { |
| 1933 | png_textp text_ptr; |
| 1934 | png_charp chunkdata; |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1935 | png_charp key, lang, text, lang_key; |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 1936 | int comp_flag; |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1937 | int comp_type = 0; |
Glenn Randers-Pehrson | 68ea243 | 2000-04-01 21:10:05 -0600 | [diff] [blame] | 1938 | png_size_t slength, prefix_len, data_len; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1939 | |
| 1940 | png_debug(1, "in png_handle_iTXt\n"); |
| 1941 | |
| 1942 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
| 1943 | png_error(png_ptr, "Missing IHDR before iTXt"); |
| 1944 | |
| 1945 | if (png_ptr->mode & PNG_HAVE_IDAT) |
| 1946 | png_ptr->mode |= PNG_AFTER_IDAT; |
| 1947 | |
| 1948 | #ifdef PNG_MAX_MALLOC_64K |
| 1949 | /* We will no doubt have problems with chunks even half this size, but |
| 1950 | there is no hard and fast rule to tell us where to stop. */ |
| 1951 | if (length > (png_uint_32)65535L) |
| 1952 | { |
| 1953 | png_warning(png_ptr,"iTXt chunk too large to fit in memory"); |
| 1954 | png_crc_finish(png_ptr, length); |
| 1955 | return; |
| 1956 | } |
| 1957 | #endif |
| 1958 | |
| 1959 | chunkdata = (png_charp)png_malloc(png_ptr, length + 1); |
| 1960 | slength = (png_size_t)length; |
| 1961 | png_crc_read(png_ptr, (png_bytep)chunkdata, slength); |
| 1962 | if (png_crc_finish(png_ptr, 0)) |
| 1963 | { |
| 1964 | png_free(png_ptr, chunkdata); |
| 1965 | return; |
| 1966 | } |
| 1967 | |
| 1968 | chunkdata[slength] = 0x00; |
| 1969 | |
| 1970 | for (lang = chunkdata; *lang; lang++) |
| 1971 | /* empty loop */ ; |
| 1972 | lang++; /* skip NUL separator */ |
| 1973 | |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1974 | /* iTXt must have a language tag (possibly empty), two compression bytes, |
| 1975 | translated keyword (possibly empty), and possibly some text after the |
| 1976 | keyword */ |
| 1977 | |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1978 | if (lang >= chunkdata + slength) |
| 1979 | { |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1980 | comp_flag = PNG_TEXT_COMPRESSION_NONE; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1981 | png_warning(png_ptr, "Zero length iTXt chunk"); |
| 1982 | } |
| 1983 | else |
| 1984 | { |
| 1985 | comp_flag = *lang++; |
| 1986 | comp_type = *lang++; |
| 1987 | } |
| 1988 | |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1989 | for (lang_key = lang; *lang_key; lang_key++) |
| 1990 | /* empty loop */ ; |
| 1991 | lang_key++; /* skip NUL separator */ |
| 1992 | |
| 1993 | for (text = lang_key; *text; text++) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1994 | /* empty loop */ ; |
| 1995 | text++; /* skip NUL separator */ |
| 1996 | |
| 1997 | prefix_len = text - chunkdata; |
| 1998 | |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1999 | key=chunkdata; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2000 | if (comp_flag) |
| 2001 | chunkdata = png_decompress_chunk(png_ptr, comp_type, chunkdata, |
Glenn Randers-Pehrson | 68ea243 | 2000-04-01 21:10:05 -0600 | [diff] [blame] | 2002 | (size_t)length, prefix_len, &data_len); |
| 2003 | else |
| 2004 | data_len=png_strlen(chunkdata + prefix_len); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2005 | text_ptr = (png_textp)png_malloc(png_ptr, (png_uint_32)sizeof(png_text)); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 2006 | text_ptr->compression = (int)comp_flag + 1; |
| 2007 | text_ptr->lang_key = chunkdata+(lang_key-key); |
| 2008 | text_ptr->lang = chunkdata+(lang-key); |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 2009 | text_ptr->itxt_length = data_len; |
| 2010 | text_ptr->text_length = 0; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2011 | text_ptr->key = chunkdata; |
| 2012 | text_ptr->text = chunkdata + prefix_len; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2013 | |
| 2014 | png_set_text(png_ptr, info_ptr, text_ptr, 1); |
| 2015 | |
| 2016 | png_free(png_ptr, text_ptr); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2017 | png_free(png_ptr, chunkdata); |
| 2018 | } |
| 2019 | #endif |
| 2020 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2021 | /* This function is called when we haven't found a handler for a |
| 2022 | chunk. If there isn't a problem with the chunk itself (ie bad |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2023 | chunk name, CRC, or a critical chunk), the chunk is silently ignored |
| 2024 | -- unless the PNG_FLAG_UNKNOWN_CHUNKS_SUPPORTED flag is on in which |
| 2025 | case it will be saved away to be written out later. */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 2026 | void /* PRIVATE */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2027 | png_handle_unknown(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) |
| 2028 | { |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2029 | png_uint_32 skip = 0; |
| 2030 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2031 | png_debug(1, "in png_handle_unknown\n"); |
| 2032 | |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2033 | if (png_ptr->mode & PNG_HAVE_IDAT) |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 2034 | { |
| 2035 | #ifdef PNG_USE_LOCAL_ARRAYS |
| 2036 | PNG_IDAT; |
| 2037 | #endif |
| 2038 | if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4)) /* not an IDAT */ |
| 2039 | png_ptr->mode |= PNG_AFTER_IDAT; |
| 2040 | } |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2041 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2042 | png_check_chunk_name(png_ptr, png_ptr->chunk_name); |
| 2043 | |
| 2044 | if (!(png_ptr->chunk_name[0] & 0x20)) |
| 2045 | { |
Glenn Randers-Pehrson | 6942d53 | 2000-05-01 09:31:54 -0500 | [diff] [blame] | 2046 | #if defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED) |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 2047 | if(png_handle_as_unknown(png_ptr, png_ptr->chunk_name) != |
| 2048 | HANDLE_CHUNK_ALWAYS |
| 2049 | #if defined(PNG_READ_USER_CHUNKS_SUPPORTED) |
| 2050 | && png_ptr->read_user_chunk_fn == (png_user_chunk_ptr)NULL |
| 2051 | #endif |
| 2052 | ) |
Glenn Randers-Pehrson | 6942d53 | 2000-05-01 09:31:54 -0500 | [diff] [blame] | 2053 | #endif |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 2054 | png_chunk_error(png_ptr, "unknown critical chunk"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2055 | } |
| 2056 | |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2057 | #if defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED) |
| 2058 | if (png_ptr->flags & PNG_FLAG_KEEP_UNKNOWN_CHUNKS) |
| 2059 | { |
| 2060 | png_unknown_chunk chunk; |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2061 | |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2062 | #ifdef PNG_MAX_MALLOC_64K |
| 2063 | if (length > (png_uint_32)65535L) |
| 2064 | { |
| 2065 | png_warning(png_ptr, "unknown chunk too large to fit in memory"); |
| 2066 | skip = length - (png_uint_32)65535L; |
| 2067 | length = (png_uint_32)65535L; |
| 2068 | } |
| 2069 | #endif |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 2070 | strcpy((png_charp)chunk.name, (png_charp)png_ptr->chunk_name); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2071 | chunk.data = (png_bytep)png_malloc(png_ptr, length); |
| 2072 | png_crc_read(png_ptr, chunk.data, length); |
| 2073 | chunk.size = length; |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 2074 | #if defined(PNG_READ_USER_CHUNKS_SUPPORTED) |
| 2075 | if(png_ptr->read_user_chunk_fn != (png_user_chunk_ptr)NULL) |
| 2076 | { |
| 2077 | /* callback to user unknown chunk handler */ |
| 2078 | if ((*(png_ptr->read_user_chunk_fn)) (png_ptr, &chunk) <= 0) |
| 2079 | { |
| 2080 | if (!(png_ptr->chunk_name[0] & 0x20)) |
| 2081 | if(png_handle_as_unknown(png_ptr, png_ptr->chunk_name) != |
| 2082 | HANDLE_CHUNK_ALWAYS) |
| 2083 | png_chunk_error(png_ptr, "unknown critical chunk"); |
| 2084 | png_set_unknown_chunks(png_ptr, info_ptr, &chunk, 1); |
| 2085 | } |
| 2086 | } |
| 2087 | else |
| 2088 | #endif |
| 2089 | png_set_unknown_chunks(png_ptr, info_ptr, &chunk, 1); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2090 | png_free(png_ptr, chunk.data); |
| 2091 | } |
| 2092 | else |
| 2093 | #endif |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 2094 | skip = length; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2095 | |
| 2096 | png_crc_finish(png_ptr, skip); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 2097 | |
| 2098 | #if !defined(PNG_READ_USER_CHUNKS_SUPPORTED) |
| 2099 | if (info_ptr == NULL) |
| 2100 | /* quiet compiler warnings about unused info_ptr */ ; |
| 2101 | #endif |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2102 | } |
| 2103 | |
| 2104 | /* This function is called to verify that a chunk name is valid. |
| 2105 | This function can't have the "critical chunk check" incorporated |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2106 | 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] | 2107 | functions to handle unknown critical chunks after we check that |
| 2108 | the chunk name itself is valid. */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2109 | |
| 2110 | #define isnonalpha(c) ((c) < 41 || (c) > 122 || ((c) > 90 && (c) < 97)) |
| 2111 | |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 2112 | void /* PRIVATE */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2113 | png_check_chunk_name(png_structp png_ptr, png_bytep chunk_name) |
| 2114 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2115 | png_debug(1, "in png_check_chunk_name\n"); |
| 2116 | if (isnonalpha(chunk_name[0]) || isnonalpha(chunk_name[1]) || |
| 2117 | isnonalpha(chunk_name[2]) || isnonalpha(chunk_name[3])) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2118 | { |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 2119 | png_chunk_error(png_ptr, "invalid chunk type"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2120 | } |
| 2121 | } |
| 2122 | |
Glenn Randers-Pehrson | 345bc27 | 1998-06-14 14:43:31 -0500 | [diff] [blame] | 2123 | /* Combines the row recently read in with the existing pixels in the |
| 2124 | row. This routine takes care of alpha and transparency if requested. |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2125 | This routine also handles the two methods of progressive display |
| 2126 | of interlaced images, depending on the mask value. |
| 2127 | The mask value describes which pixels are to be combined with |
| 2128 | the row. The pattern always repeats every 8 pixels, so just 8 |
Glenn Randers-Pehrson | 345bc27 | 1998-06-14 14:43:31 -0500 | [diff] [blame] | 2129 | bits are needed. A one indicates the pixel is to be combined, |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2130 | a zero indicates the pixel is to be skipped. This is in addition |
| 2131 | to any alpha or transparency value associated with the pixel. If |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2132 | you want all pixels to be combined, pass 0xff (255) in mask. */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 2133 | void /* PRIVATE */ |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 2134 | #ifdef PNG_HAVE_ASSEMBLER_COMBINE_ROW |
Glenn Randers-Pehrson | 1ef65b6 | 2000-05-12 06:19:53 -0500 | [diff] [blame] | 2135 | png_combine_row_c |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 2136 | #else |
Glenn Randers-Pehrson | 1ef65b6 | 2000-05-12 06:19:53 -0500 | [diff] [blame] | 2137 | png_combine_row |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 2138 | #endif /* PNG_HAVE_ASSEMBLER_COMBINE_ROW */ |
| 2139 | (png_structp png_ptr, png_bytep row, int mask) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2140 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2141 | png_debug(1,"in png_combine_row\n"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2142 | if (mask == 0xff) |
| 2143 | { |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 2144 | png_memcpy(row, png_ptr->row_buf + 1, |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2145 | (png_size_t)((png_ptr->width * |
| 2146 | png_ptr->row_info.pixel_depth + 7) >> 3)); |
| 2147 | } |
| 2148 | else |
| 2149 | { |
| 2150 | switch (png_ptr->row_info.pixel_depth) |
| 2151 | { |
| 2152 | case 1: |
| 2153 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2154 | png_bytep sp = png_ptr->row_buf + 1; |
| 2155 | png_bytep dp = row; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2156 | int s_inc, s_start, s_end; |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2157 | int m = 0x80; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2158 | int shift; |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2159 | png_uint_32 i; |
| 2160 | png_uint_32 row_width = png_ptr->width; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2161 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2162 | #if defined(PNG_READ_PACKSWAP_SUPPORTED) |
| 2163 | if (png_ptr->transformations & PNG_PACKSWAP) |
| 2164 | { |
| 2165 | s_start = 0; |
| 2166 | s_end = 7; |
| 2167 | s_inc = 1; |
| 2168 | } |
| 2169 | else |
| 2170 | #endif |
| 2171 | { |
| 2172 | s_start = 7; |
| 2173 | s_end = 0; |
| 2174 | s_inc = -1; |
| 2175 | } |
| 2176 | |
| 2177 | shift = s_start; |
| 2178 | |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2179 | for (i = 0; i < row_width; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2180 | { |
| 2181 | if (m & mask) |
| 2182 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2183 | int value; |
| 2184 | |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 2185 | value = (*sp >> shift) & 0x01; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2186 | *dp &= (png_byte)((0x7f7f >> (7 - shift)) & 0xff); |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 2187 | *dp |= (png_byte)(value << shift); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2188 | } |
| 2189 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2190 | if (shift == s_end) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2191 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2192 | shift = s_start; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2193 | sp++; |
| 2194 | dp++; |
| 2195 | } |
| 2196 | else |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2197 | shift += s_inc; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2198 | |
| 2199 | if (m == 1) |
| 2200 | m = 0x80; |
| 2201 | else |
| 2202 | m >>= 1; |
| 2203 | } |
| 2204 | break; |
| 2205 | } |
| 2206 | case 2: |
| 2207 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2208 | png_bytep sp = png_ptr->row_buf + 1; |
| 2209 | png_bytep dp = row; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2210 | int s_start, s_end, s_inc; |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2211 | int m = 0x80; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2212 | int shift; |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2213 | png_uint_32 i; |
| 2214 | png_uint_32 row_width = png_ptr->width; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2215 | int value; |
| 2216 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2217 | #if defined(PNG_READ_PACKSWAP_SUPPORTED) |
| 2218 | if (png_ptr->transformations & PNG_PACKSWAP) |
| 2219 | { |
| 2220 | s_start = 0; |
| 2221 | s_end = 6; |
| 2222 | s_inc = 2; |
| 2223 | } |
| 2224 | else |
| 2225 | #endif |
| 2226 | { |
| 2227 | s_start = 6; |
| 2228 | s_end = 0; |
| 2229 | s_inc = -2; |
| 2230 | } |
| 2231 | |
| 2232 | shift = s_start; |
| 2233 | |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2234 | for (i = 0; i < row_width; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2235 | { |
| 2236 | if (m & mask) |
| 2237 | { |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 2238 | value = (*sp >> shift) & 0x03; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2239 | *dp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff); |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 2240 | *dp |= (png_byte)(value << shift); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2241 | } |
| 2242 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2243 | if (shift == s_end) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2244 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2245 | shift = s_start; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2246 | sp++; |
| 2247 | dp++; |
| 2248 | } |
| 2249 | else |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2250 | shift += s_inc; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2251 | if (m == 1) |
| 2252 | m = 0x80; |
| 2253 | else |
| 2254 | m >>= 1; |
| 2255 | } |
| 2256 | break; |
| 2257 | } |
| 2258 | case 4: |
| 2259 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2260 | png_bytep sp = png_ptr->row_buf + 1; |
| 2261 | png_bytep dp = row; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2262 | int s_start, s_end, s_inc; |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2263 | int m = 0x80; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2264 | int shift; |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2265 | png_uint_32 i; |
| 2266 | png_uint_32 row_width = png_ptr->width; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2267 | int value; |
| 2268 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2269 | #if defined(PNG_READ_PACKSWAP_SUPPORTED) |
| 2270 | if (png_ptr->transformations & PNG_PACKSWAP) |
| 2271 | { |
| 2272 | s_start = 0; |
| 2273 | s_end = 4; |
| 2274 | s_inc = 4; |
| 2275 | } |
| 2276 | else |
| 2277 | #endif |
| 2278 | { |
| 2279 | s_start = 4; |
| 2280 | s_end = 0; |
| 2281 | s_inc = -4; |
| 2282 | } |
| 2283 | shift = s_start; |
| 2284 | |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2285 | for (i = 0; i < row_width; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2286 | { |
| 2287 | if (m & mask) |
| 2288 | { |
| 2289 | value = (*sp >> shift) & 0xf; |
| 2290 | *dp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff); |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 2291 | *dp |= (png_byte)(value << shift); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2292 | } |
| 2293 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2294 | if (shift == s_end) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2295 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2296 | shift = s_start; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2297 | sp++; |
| 2298 | dp++; |
| 2299 | } |
| 2300 | else |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2301 | shift += s_inc; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2302 | if (m == 1) |
| 2303 | m = 0x80; |
| 2304 | else |
| 2305 | m >>= 1; |
| 2306 | } |
| 2307 | break; |
| 2308 | } |
| 2309 | default: |
| 2310 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2311 | png_bytep sp = png_ptr->row_buf + 1; |
| 2312 | png_bytep dp = row; |
| 2313 | png_size_t pixel_bytes = (png_ptr->row_info.pixel_depth >> 3); |
| 2314 | png_uint_32 i; |
| 2315 | png_uint_32 row_width = png_ptr->width; |
| 2316 | png_byte m = 0x80; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2317 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2318 | |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2319 | for (i = 0; i < row_width; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2320 | { |
| 2321 | if (m & mask) |
| 2322 | { |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 2323 | png_memcpy(dp, sp, pixel_bytes); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2324 | } |
| 2325 | |
| 2326 | sp += pixel_bytes; |
| 2327 | dp += pixel_bytes; |
| 2328 | |
| 2329 | if (m == 1) |
| 2330 | m = 0x80; |
| 2331 | else |
| 2332 | m >>= 1; |
| 2333 | } |
| 2334 | break; |
| 2335 | } |
| 2336 | } |
| 2337 | } |
| 2338 | } |
| 2339 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 2340 | #if defined(PNG_READ_INTERLACING_SUPPORTED) |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 2341 | void /* PRIVATE */ |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 2342 | #ifdef PNG_HAVE_ASSEMBLER_READ_INTERLACE |
| 2343 | png_do_read_interlace_c |
| 2344 | #else |
| 2345 | png_do_read_interlace |
| 2346 | #endif /* PNG_HAVE_ASSEMBLER_READ_INTERLACE */ |
| 2347 | (png_row_infop row_info, png_bytep row, int pass, |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2348 | png_uint_32 transformations) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2349 | { |
Glenn Randers-Pehrson | 074af5e | 1999-11-28 23:32:18 -0600 | [diff] [blame] | 2350 | #ifdef PNG_USE_LOCAL_ARRAYS |
Glenn Randers-Pehrson | 5379b24 | 1999-11-27 10:22:33 -0600 | [diff] [blame] | 2351 | /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */ |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2352 | |
Glenn Randers-Pehrson | 5379b24 | 1999-11-27 10:22:33 -0600 | [diff] [blame] | 2353 | /* offset to next interlace block */ |
| 2354 | const int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1}; |
Glenn Randers-Pehrson | 074af5e | 1999-11-28 23:32:18 -0600 | [diff] [blame] | 2355 | #endif |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2356 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2357 | png_debug(1,"in png_do_read_interlace\n"); |
| 2358 | if (row != NULL && row_info != NULL) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2359 | { |
| 2360 | png_uint_32 final_width; |
| 2361 | |
| 2362 | final_width = row_info->width * png_pass_inc[pass]; |
| 2363 | |
| 2364 | switch (row_info->pixel_depth) |
| 2365 | { |
| 2366 | case 1: |
| 2367 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2368 | png_bytep sp = row + (png_size_t)((row_info->width - 1) >> 3); |
| 2369 | png_bytep dp = row + (png_size_t)((final_width - 1) >> 3); |
Glenn Randers-Pehrson | 983ec16 | 1998-03-07 11:24:03 -0600 | [diff] [blame] | 2370 | int sshift, dshift; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2371 | int s_start, s_end, s_inc; |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2372 | int jstop = png_pass_inc[pass]; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2373 | png_byte v; |
| 2374 | png_uint_32 i; |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2375 | int j; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2376 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2377 | #if defined(PNG_READ_PACKSWAP_SUPPORTED) |
| 2378 | if (transformations & PNG_PACKSWAP) |
| 2379 | { |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 2380 | sshift = (int)((row_info->width + 7) & 0x07); |
| 2381 | dshift = (int)((final_width + 7) & 0x07); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2382 | s_start = 7; |
| 2383 | s_end = 0; |
| 2384 | s_inc = -1; |
| 2385 | } |
| 2386 | else |
| 2387 | #endif |
| 2388 | { |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 2389 | sshift = 7 - (int)((row_info->width + 7) & 0x07); |
| 2390 | dshift = 7 - (int)((final_width + 7) & 0x07); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2391 | s_start = 0; |
| 2392 | s_end = 7; |
| 2393 | s_inc = 1; |
| 2394 | } |
| 2395 | |
Glenn Randers-Pehrson | 8686fff | 1998-05-21 09:27:50 -0500 | [diff] [blame] | 2396 | for (i = 0; i < row_info->width; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2397 | { |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 2398 | v = (png_byte)((*sp >> sshift) & 0x01); |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2399 | for (j = 0; j < jstop; j++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2400 | { |
| 2401 | *dp &= (png_byte)((0x7f7f >> (7 - dshift)) & 0xff); |
| 2402 | *dp |= (png_byte)(v << dshift); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2403 | if (dshift == s_end) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2404 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2405 | dshift = s_start; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2406 | dp--; |
| 2407 | } |
| 2408 | else |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2409 | dshift += s_inc; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2410 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2411 | if (sshift == s_end) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2412 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2413 | sshift = s_start; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2414 | sp--; |
| 2415 | } |
| 2416 | else |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2417 | sshift += s_inc; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2418 | } |
| 2419 | break; |
| 2420 | } |
| 2421 | case 2: |
| 2422 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2423 | png_bytep sp = row + (png_uint_32)((row_info->width - 1) >> 2); |
| 2424 | png_bytep dp = row + (png_uint_32)((final_width - 1) >> 2); |
Glenn Randers-Pehrson | 983ec16 | 1998-03-07 11:24:03 -0600 | [diff] [blame] | 2425 | int sshift, dshift; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2426 | int s_start, s_end, s_inc; |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2427 | int jstop = png_pass_inc[pass]; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2428 | png_uint_32 i; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2429 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2430 | #if defined(PNG_READ_PACKSWAP_SUPPORTED) |
| 2431 | if (transformations & PNG_PACKSWAP) |
| 2432 | { |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 2433 | sshift = (int)(((row_info->width + 3) & 0x03) << 1); |
| 2434 | dshift = (int)(((final_width + 3) & 0x03) << 1); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2435 | s_start = 6; |
| 2436 | s_end = 0; |
| 2437 | s_inc = -2; |
| 2438 | } |
| 2439 | else |
| 2440 | #endif |
| 2441 | { |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 2442 | sshift = (int)((3 - ((row_info->width + 3) & 0x03)) << 1); |
| 2443 | dshift = (int)((3 - ((final_width + 3) & 0x03)) << 1); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2444 | s_start = 0; |
| 2445 | s_end = 6; |
| 2446 | s_inc = 2; |
| 2447 | } |
| 2448 | |
Glenn Randers-Pehrson | 8686fff | 1998-05-21 09:27:50 -0500 | [diff] [blame] | 2449 | for (i = 0; i < row_info->width; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2450 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2451 | png_byte v; |
| 2452 | int j; |
| 2453 | |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 2454 | v = (png_byte)((*sp >> sshift) & 0x03); |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2455 | for (j = 0; j < jstop; j++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2456 | { |
| 2457 | *dp &= (png_byte)((0x3f3f >> (6 - dshift)) & 0xff); |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 2458 | *dp |= (png_byte)(v << dshift); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2459 | if (dshift == s_end) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2460 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2461 | dshift = s_start; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2462 | dp--; |
| 2463 | } |
| 2464 | else |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2465 | dshift += s_inc; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2466 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2467 | if (sshift == s_end) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2468 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2469 | sshift = s_start; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2470 | sp--; |
| 2471 | } |
| 2472 | else |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2473 | sshift += s_inc; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2474 | } |
| 2475 | break; |
| 2476 | } |
| 2477 | case 4: |
| 2478 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2479 | png_bytep sp = row + (png_size_t)((row_info->width - 1) >> 1); |
| 2480 | png_bytep dp = row + (png_size_t)((final_width - 1) >> 1); |
Glenn Randers-Pehrson | 983ec16 | 1998-03-07 11:24:03 -0600 | [diff] [blame] | 2481 | int sshift, dshift; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2482 | int s_start, s_end, s_inc; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2483 | png_uint_32 i; |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2484 | int jstop = png_pass_inc[pass]; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2485 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2486 | #if defined(PNG_READ_PACKSWAP_SUPPORTED) |
| 2487 | if (transformations & PNG_PACKSWAP) |
| 2488 | { |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 2489 | sshift = (int)(((row_info->width + 1) & 0x01) << 2); |
| 2490 | dshift = (int)(((final_width + 1) & 0x01) << 2); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2491 | s_start = 4; |
| 2492 | s_end = 0; |
| 2493 | s_inc = -4; |
| 2494 | } |
| 2495 | else |
| 2496 | #endif |
| 2497 | { |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 2498 | sshift = (int)((1 - ((row_info->width + 1) & 0x01)) << 2); |
| 2499 | dshift = (int)((1 - ((final_width + 1) & 0x01)) << 2); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2500 | s_start = 0; |
| 2501 | s_end = 4; |
| 2502 | s_inc = 4; |
| 2503 | } |
| 2504 | |
Glenn Randers-Pehrson | 8686fff | 1998-05-21 09:27:50 -0500 | [diff] [blame] | 2505 | for (i = 0; i < row_info->width; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2506 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2507 | png_byte v = (png_byte)((*sp >> sshift) & 0xf); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2508 | int j; |
| 2509 | |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2510 | for (j = 0; j < jstop; j++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2511 | { |
| 2512 | *dp &= (png_byte)((0xf0f >> (4 - dshift)) & 0xff); |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 2513 | *dp |= (png_byte)(v << dshift); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2514 | if (dshift == s_end) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2515 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2516 | dshift = s_start; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2517 | dp--; |
| 2518 | } |
| 2519 | else |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2520 | dshift += s_inc; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2521 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2522 | if (sshift == s_end) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2523 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2524 | sshift = s_start; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2525 | sp--; |
| 2526 | } |
| 2527 | else |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2528 | sshift += s_inc; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2529 | } |
| 2530 | break; |
| 2531 | } |
| 2532 | default: |
| 2533 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2534 | png_size_t pixel_bytes = (row_info->pixel_depth >> 3); |
| 2535 | png_bytep sp = row + (png_size_t)(row_info->width - 1) * pixel_bytes; |
| 2536 | png_bytep dp = row + (png_size_t)(final_width - 1) * pixel_bytes; |
Glenn Randers-Pehrson | bcfd15d | 1999-10-01 14:22:25 -0500 | [diff] [blame] | 2537 | |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2538 | int jstop = png_pass_inc[pass]; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2539 | png_uint_32 i; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2540 | |
Glenn Randers-Pehrson | 8686fff | 1998-05-21 09:27:50 -0500 | [diff] [blame] | 2541 | for (i = 0; i < row_info->width; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2542 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2543 | png_byte v[8]; |
| 2544 | int j; |
| 2545 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 2546 | png_memcpy(v, sp, pixel_bytes); |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2547 | for (j = 0; j < jstop; j++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2548 | { |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 2549 | png_memcpy(dp, v, pixel_bytes); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2550 | dp -= pixel_bytes; |
| 2551 | } |
| 2552 | sp -= pixel_bytes; |
| 2553 | } |
| 2554 | break; |
| 2555 | } |
| 2556 | } |
| 2557 | row_info->width = final_width; |
| 2558 | row_info->rowbytes = ((final_width * |
| 2559 | (png_uint_32)row_info->pixel_depth + 7) >> 3); |
| 2560 | } |
Glenn Randers-Pehrson | 104622b | 2000-05-29 08:58:03 -0500 | [diff] [blame] | 2561 | #if !defined(PNG_READ_PACKSWAP_SUPPORTED) |
| 2562 | /* silence compiler warning */ |
| 2563 | if (transformations) |
| 2564 | return; |
| 2565 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2566 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 2567 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2568 | |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 2569 | void /* PRIVATE */ |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 2570 | #ifdef PNG_HAVE_ASSEMBLER_READ_FILTER_ROW |
| 2571 | png_read_filter_row_c |
| 2572 | #else |
| 2573 | png_read_filter_row |
| 2574 | #endif /* PNG_HAVE_ASSEMBLER_READ_FILTER_ROW */ |
| 2575 | (png_structp png_ptr, png_row_infop row_info, png_bytep row, |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2576 | png_bytep prev_row, int filter) |
| 2577 | { |
| 2578 | png_debug(1, "in png_read_filter_row\n"); |
Glenn Randers-Pehrson | 4766a24 | 2000-07-17 06:17:09 -0500 | [diff] [blame^] | 2579 | png_debug2(2,"row = %lu, filter = %d\n", png_ptr->row_number, filter); |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2580 | switch (filter) |
| 2581 | { |
| 2582 | case PNG_FILTER_VALUE_NONE: |
| 2583 | break; |
| 2584 | case PNG_FILTER_VALUE_SUB: |
| 2585 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2586 | png_uint_32 i; |
| 2587 | png_uint_32 istop = row_info->rowbytes; |
Glenn Randers-Pehrson | bcfd15d | 1999-10-01 14:22:25 -0500 | [diff] [blame] | 2588 | png_uint_32 bpp = (row_info->pixel_depth + 7) >> 3; |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2589 | png_bytep rp = row + bpp; |
| 2590 | png_bytep lp = row; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2591 | |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2592 | for (i = bpp; i < istop; i++) |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2593 | { |
Glenn Randers-Pehrson | 8686fff | 1998-05-21 09:27:50 -0500 | [diff] [blame] | 2594 | *rp = (png_byte)(((int)(*rp) + (int)(*lp++)) & 0xff); |
| 2595 | rp++; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2596 | } |
| 2597 | break; |
| 2598 | } |
| 2599 | case PNG_FILTER_VALUE_UP: |
| 2600 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2601 | png_uint_32 i; |
| 2602 | png_uint_32 istop = row_info->rowbytes; |
| 2603 | png_bytep rp = row; |
| 2604 | png_bytep pp = prev_row; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2605 | |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2606 | for (i = 0; i < istop; i++) |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2607 | { |
Glenn Randers-Pehrson | 8686fff | 1998-05-21 09:27:50 -0500 | [diff] [blame] | 2608 | *rp = (png_byte)(((int)(*rp) + (int)(*pp++)) & 0xff); |
| 2609 | rp++; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2610 | } |
| 2611 | break; |
| 2612 | } |
| 2613 | case PNG_FILTER_VALUE_AVG: |
| 2614 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2615 | png_uint_32 i; |
| 2616 | png_bytep rp = row; |
| 2617 | png_bytep pp = prev_row; |
| 2618 | png_bytep lp = row; |
Glenn Randers-Pehrson | bcfd15d | 1999-10-01 14:22:25 -0500 | [diff] [blame] | 2619 | png_uint_32 bpp = (row_info->pixel_depth + 7) >> 3; |
Glenn Randers-Pehrson | 8686fff | 1998-05-21 09:27:50 -0500 | [diff] [blame] | 2620 | png_uint_32 istop = row_info->rowbytes - bpp; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2621 | |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2622 | for (i = 0; i < bpp; i++) |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2623 | { |
Glenn Randers-Pehrson | 8686fff | 1998-05-21 09:27:50 -0500 | [diff] [blame] | 2624 | *rp = (png_byte)(((int)(*rp) + |
Glenn Randers-Pehrson | bcfd15d | 1999-10-01 14:22:25 -0500 | [diff] [blame] | 2625 | ((int)(*pp++) / 2 )) & 0xff); |
Glenn Randers-Pehrson | 8686fff | 1998-05-21 09:27:50 -0500 | [diff] [blame] | 2626 | rp++; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2627 | } |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 2628 | |
Glenn Randers-Pehrson | 8686fff | 1998-05-21 09:27:50 -0500 | [diff] [blame] | 2629 | for (i = 0; i < istop; i++) |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2630 | { |
Glenn Randers-Pehrson | 8686fff | 1998-05-21 09:27:50 -0500 | [diff] [blame] | 2631 | *rp = (png_byte)(((int)(*rp) + |
Glenn Randers-Pehrson | bcfd15d | 1999-10-01 14:22:25 -0500 | [diff] [blame] | 2632 | (int)(*pp++ + *lp++) / 2 ) & 0xff); |
Glenn Randers-Pehrson | 8686fff | 1998-05-21 09:27:50 -0500 | [diff] [blame] | 2633 | rp++; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2634 | } |
| 2635 | break; |
| 2636 | } |
| 2637 | case PNG_FILTER_VALUE_PAETH: |
| 2638 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2639 | png_uint_32 i; |
| 2640 | png_bytep rp = row; |
| 2641 | png_bytep pp = prev_row; |
| 2642 | png_bytep lp = row; |
| 2643 | png_bytep cp = prev_row; |
Glenn Randers-Pehrson | bcfd15d | 1999-10-01 14:22:25 -0500 | [diff] [blame] | 2644 | png_uint_32 bpp = (row_info->pixel_depth + 7) >> 3; |
Glenn Randers-Pehrson | 8686fff | 1998-05-21 09:27:50 -0500 | [diff] [blame] | 2645 | png_uint_32 istop=row_info->rowbytes - bpp; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2646 | |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2647 | for (i = 0; i < bpp; i++) |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2648 | { |
Glenn Randers-Pehrson | 8686fff | 1998-05-21 09:27:50 -0500 | [diff] [blame] | 2649 | *rp = (png_byte)(((int)(*rp) + (int)(*pp++)) & 0xff); |
| 2650 | rp++; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2651 | } |
| 2652 | |
Glenn Randers-Pehrson | 8686fff | 1998-05-21 09:27:50 -0500 | [diff] [blame] | 2653 | for (i = 0; i < istop; i++) /* use leftover rp,pp */ |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2654 | { |
| 2655 | int a, b, c, pa, pb, pc, p; |
| 2656 | |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2657 | a = *lp++; |
| 2658 | b = *pp++; |
| 2659 | c = *cp++; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2660 | |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2661 | p = b - c; |
| 2662 | pc = a - c; |
| 2663 | |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2664 | #ifdef PNG_USE_ABS |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2665 | pa = abs(p); |
| 2666 | pb = abs(pc); |
| 2667 | pc = abs(p + pc); |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2668 | #else |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2669 | pa = p < 0 ? -p : p; |
| 2670 | pb = pc < 0 ? -pc : pc; |
| 2671 | pc = (p + pc) < 0 ? -(p + pc) : p + pc; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2672 | #endif |
| 2673 | |
| 2674 | /* |
| 2675 | if (pa <= pb && pa <= pc) |
| 2676 | p = a; |
| 2677 | else if (pb <= pc) |
| 2678 | p = b; |
| 2679 | else |
| 2680 | p = c; |
| 2681 | */ |
| 2682 | |
| 2683 | p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c; |
| 2684 | |
Glenn Randers-Pehrson | 8686fff | 1998-05-21 09:27:50 -0500 | [diff] [blame] | 2685 | *rp = (png_byte)(((int)(*rp) + p) & 0xff); |
| 2686 | rp++; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2687 | } |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2688 | break; |
| 2689 | } |
| 2690 | default: |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 2691 | png_warning(png_ptr, "Ignoring bad adaptive filter type"); |
| 2692 | *row=0; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2693 | break; |
| 2694 | } |
| 2695 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2696 | |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 2697 | void /* PRIVATE */ |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 2698 | png_read_finish_row(png_structp png_ptr) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2699 | { |
Glenn Randers-Pehrson | 074af5e | 1999-11-28 23:32:18 -0600 | [diff] [blame] | 2700 | #ifdef PNG_USE_LOCAL_ARRAYS |
Glenn Randers-Pehrson | 5379b24 | 1999-11-27 10:22:33 -0600 | [diff] [blame] | 2701 | /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */ |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2702 | |
Glenn Randers-Pehrson | 5379b24 | 1999-11-27 10:22:33 -0600 | [diff] [blame] | 2703 | /* start of interlace block */ |
| 2704 | const int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0}; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2705 | |
Glenn Randers-Pehrson | 5379b24 | 1999-11-27 10:22:33 -0600 | [diff] [blame] | 2706 | /* offset to next interlace block */ |
| 2707 | const int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1}; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2708 | |
Glenn Randers-Pehrson | 5379b24 | 1999-11-27 10:22:33 -0600 | [diff] [blame] | 2709 | /* start of interlace block in the y direction */ |
| 2710 | const int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1}; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2711 | |
Glenn Randers-Pehrson | 5379b24 | 1999-11-27 10:22:33 -0600 | [diff] [blame] | 2712 | /* offset to next interlace block in the y direction */ |
| 2713 | const int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2}; |
Glenn Randers-Pehrson | 074af5e | 1999-11-28 23:32:18 -0600 | [diff] [blame] | 2714 | #endif |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2715 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2716 | png_debug(1, "in png_read_finish_row\n"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2717 | png_ptr->row_number++; |
| 2718 | if (png_ptr->row_number < png_ptr->num_rows) |
| 2719 | return; |
| 2720 | |
| 2721 | if (png_ptr->interlaced) |
| 2722 | { |
| 2723 | png_ptr->row_number = 0; |
Glenn Randers-Pehrson | a357b99 | 1998-02-08 20:56:40 -0600 | [diff] [blame] | 2724 | 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] | 2725 | do |
| 2726 | { |
| 2727 | png_ptr->pass++; |
| 2728 | if (png_ptr->pass >= 7) |
| 2729 | break; |
| 2730 | png_ptr->iwidth = (png_ptr->width + |
| 2731 | png_pass_inc[png_ptr->pass] - 1 - |
| 2732 | png_pass_start[png_ptr->pass]) / |
| 2733 | png_pass_inc[png_ptr->pass]; |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame] | 2734 | png_ptr->irowbytes = ((png_ptr->iwidth * |
| 2735 | (png_uint_32)png_ptr->pixel_depth + 7) >> 3) +1; |
| 2736 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2737 | if (!(png_ptr->transformations & PNG_INTERLACE)) |
| 2738 | { |
| 2739 | png_ptr->num_rows = (png_ptr->height + |
| 2740 | png_pass_yinc[png_ptr->pass] - 1 - |
| 2741 | png_pass_ystart[png_ptr->pass]) / |
| 2742 | png_pass_yinc[png_ptr->pass]; |
| 2743 | if (!(png_ptr->num_rows)) |
| 2744 | continue; |
| 2745 | } |
Glenn Randers-Pehrson | 345bc27 | 1998-06-14 14:43:31 -0500 | [diff] [blame] | 2746 | else /* if (png_ptr->transformations & PNG_INTERLACE) */ |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 2747 | break; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2748 | } while (png_ptr->iwidth == 0); |
| 2749 | |
| 2750 | if (png_ptr->pass < 7) |
| 2751 | return; |
| 2752 | } |
| 2753 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2754 | if (!(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED)) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2755 | { |
Glenn Randers-Pehrson | 074af5e | 1999-11-28 23:32:18 -0600 | [diff] [blame] | 2756 | #ifdef PNG_USE_LOCAL_ARRAYS |
| 2757 | PNG_IDAT; |
| 2758 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2759 | char extra; |
| 2760 | int ret; |
| 2761 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2762 | png_ptr->zstream.next_out = (Byte *)&extra; |
| 2763 | png_ptr->zstream.avail_out = (uInt)1; |
Glenn Randers-Pehrson | ea3bcd7 | 1998-03-07 14:33:00 -0600 | [diff] [blame] | 2764 | for(;;) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2765 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2766 | if (!(png_ptr->zstream.avail_in)) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2767 | { |
| 2768 | while (!png_ptr->idat_size) |
| 2769 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2770 | png_byte chunk_length[4]; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2771 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2772 | png_crc_finish(png_ptr, 0); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2773 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2774 | png_read_data(png_ptr, chunk_length, 4); |
| 2775 | png_ptr->idat_size = png_get_uint_32(chunk_length); |
| 2776 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2777 | png_reset_crc(png_ptr); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2778 | png_crc_read(png_ptr, png_ptr->chunk_name, 4); |
Glenn Randers-Pehrson | 5379b24 | 1999-11-27 10:22:33 -0600 | [diff] [blame] | 2779 | if (png_memcmp(png_ptr->chunk_name, (png_bytep)png_IDAT, 4)) |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 2780 | png_error(png_ptr, "Not enough image data"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2781 | |
| 2782 | } |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2783 | png_ptr->zstream.avail_in = (uInt)png_ptr->zbuf_size; |
| 2784 | png_ptr->zstream.next_in = png_ptr->zbuf; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2785 | if (png_ptr->zbuf_size > png_ptr->idat_size) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2786 | png_ptr->zstream.avail_in = (uInt)png_ptr->idat_size; |
| 2787 | png_crc_read(png_ptr, png_ptr->zbuf, png_ptr->zstream.avail_in); |
| 2788 | png_ptr->idat_size -= png_ptr->zstream.avail_in; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2789 | } |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2790 | ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2791 | if (ret == Z_STREAM_END) |
| 2792 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2793 | if (!(png_ptr->zstream.avail_out) || png_ptr->zstream.avail_in || |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2794 | png_ptr->idat_size) |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 2795 | png_error(png_ptr, "Extra compressed data"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2796 | png_ptr->mode |= PNG_AFTER_IDAT; |
| 2797 | png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2798 | break; |
| 2799 | } |
| 2800 | if (ret != Z_OK) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2801 | png_error(png_ptr, png_ptr->zstream.msg ? png_ptr->zstream.msg : |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2802 | "Decompression Error"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2803 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2804 | if (!(png_ptr->zstream.avail_out)) |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 2805 | png_error(png_ptr, "Extra compressed data"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2806 | |
Glenn Randers-Pehrson | ea3bcd7 | 1998-03-07 14:33:00 -0600 | [diff] [blame] | 2807 | } |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2808 | png_ptr->zstream.avail_out = 0; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2809 | } |
| 2810 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2811 | if (png_ptr->idat_size || png_ptr->zstream.avail_in) |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 2812 | png_error(png_ptr, "Extra compression data"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2813 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2814 | inflateReset(&png_ptr->zstream); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2815 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2816 | png_ptr->mode |= PNG_AFTER_IDAT; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2817 | } |
| 2818 | |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 2819 | void /* PRIVATE */ |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 2820 | png_read_start_row(png_structp png_ptr) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2821 | { |
Glenn Randers-Pehrson | 074af5e | 1999-11-28 23:32:18 -0600 | [diff] [blame] | 2822 | #ifdef PNG_USE_LOCAL_ARRAYS |
Glenn Randers-Pehrson | 5379b24 | 1999-11-27 10:22:33 -0600 | [diff] [blame] | 2823 | /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */ |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2824 | |
Glenn Randers-Pehrson | 5379b24 | 1999-11-27 10:22:33 -0600 | [diff] [blame] | 2825 | /* start of interlace block */ |
| 2826 | const int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0}; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2827 | |
Glenn Randers-Pehrson | 5379b24 | 1999-11-27 10:22:33 -0600 | [diff] [blame] | 2828 | /* offset to next interlace block */ |
| 2829 | const int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1}; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2830 | |
Glenn Randers-Pehrson | 5379b24 | 1999-11-27 10:22:33 -0600 | [diff] [blame] | 2831 | /* start of interlace block in the y direction */ |
| 2832 | const int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1}; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2833 | |
Glenn Randers-Pehrson | 5379b24 | 1999-11-27 10:22:33 -0600 | [diff] [blame] | 2834 | /* offset to next interlace block in the y direction */ |
| 2835 | const int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2}; |
Glenn Randers-Pehrson | 074af5e | 1999-11-28 23:32:18 -0600 | [diff] [blame] | 2836 | #endif |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2837 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2838 | int max_pixel_depth; |
Glenn Randers-Pehrson | d0dce40 | 1998-05-09 10:02:29 -0500 | [diff] [blame] | 2839 | png_uint_32 row_bytes; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2840 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2841 | png_debug(1, "in png_read_start_row\n"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2842 | png_ptr->zstream.avail_in = 0; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2843 | png_init_read_transformations(png_ptr); |
| 2844 | if (png_ptr->interlaced) |
| 2845 | { |
| 2846 | if (!(png_ptr->transformations & PNG_INTERLACE)) |
| 2847 | png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 - |
| 2848 | png_pass_ystart[0]) / png_pass_yinc[0]; |
| 2849 | else |
| 2850 | png_ptr->num_rows = png_ptr->height; |
| 2851 | |
| 2852 | png_ptr->iwidth = (png_ptr->width + |
| 2853 | png_pass_inc[png_ptr->pass] - 1 - |
| 2854 | png_pass_start[png_ptr->pass]) / |
| 2855 | png_pass_inc[png_ptr->pass]; |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame] | 2856 | |
Glenn Randers-Pehrson | d0dce40 | 1998-05-09 10:02:29 -0500 | [diff] [blame] | 2857 | row_bytes = ((png_ptr->iwidth * |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame] | 2858 | (png_uint_32)png_ptr->pixel_depth + 7) >> 3) +1; |
Glenn Randers-Pehrson | d0dce40 | 1998-05-09 10:02:29 -0500 | [diff] [blame] | 2859 | png_ptr->irowbytes = (png_size_t)row_bytes; |
| 2860 | if((png_uint_32)png_ptr->irowbytes != row_bytes) |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame] | 2861 | png_error(png_ptr, "Rowbytes overflow in png_read_start_row"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2862 | } |
| 2863 | else |
| 2864 | { |
| 2865 | png_ptr->num_rows = png_ptr->height; |
| 2866 | png_ptr->iwidth = png_ptr->width; |
| 2867 | png_ptr->irowbytes = png_ptr->rowbytes + 1; |
| 2868 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2869 | max_pixel_depth = png_ptr->pixel_depth; |
| 2870 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 2871 | #if defined(PNG_READ_PACK_SUPPORTED) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2872 | if ((png_ptr->transformations & PNG_PACK) && png_ptr->bit_depth < 8) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2873 | max_pixel_depth = 8; |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 2874 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2875 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2876 | #if defined(PNG_READ_EXPAND_SUPPORTED) |
| 2877 | if (png_ptr->transformations & PNG_EXPAND) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2878 | { |
| 2879 | if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) |
| 2880 | { |
| 2881 | if (png_ptr->num_trans) |
| 2882 | max_pixel_depth = 32; |
| 2883 | else |
| 2884 | max_pixel_depth = 24; |
| 2885 | } |
| 2886 | else if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY) |
| 2887 | { |
| 2888 | if (max_pixel_depth < 8) |
| 2889 | max_pixel_depth = 8; |
| 2890 | if (png_ptr->num_trans) |
| 2891 | max_pixel_depth *= 2; |
| 2892 | } |
| 2893 | else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB) |
| 2894 | { |
| 2895 | if (png_ptr->num_trans) |
| 2896 | { |
| 2897 | max_pixel_depth *= 4; |
| 2898 | max_pixel_depth /= 3; |
| 2899 | } |
| 2900 | } |
| 2901 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 2902 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2903 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 2904 | #if defined(PNG_READ_FILLER_SUPPORTED) |
| 2905 | if (png_ptr->transformations & (PNG_FILLER)) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2906 | { |
Glenn Randers-Pehrson | d0dce40 | 1998-05-09 10:02:29 -0500 | [diff] [blame] | 2907 | if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) |
| 2908 | max_pixel_depth = 32; |
| 2909 | else if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY) |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2910 | { |
| 2911 | if (max_pixel_depth <= 8) |
| 2912 | max_pixel_depth = 16; |
| 2913 | else |
| 2914 | max_pixel_depth = 32; |
| 2915 | } |
| 2916 | else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB) |
| 2917 | { |
| 2918 | if (max_pixel_depth <= 32) |
| 2919 | max_pixel_depth = 32; |
| 2920 | else |
| 2921 | max_pixel_depth = 64; |
| 2922 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2923 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 2924 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2925 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 2926 | #if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2927 | if (png_ptr->transformations & PNG_GRAY_TO_RGB) |
| 2928 | { |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 2929 | if ( |
| 2930 | #if defined(PNG_READ_EXPAND_SUPPORTED) |
| 2931 | (png_ptr->num_trans && (png_ptr->transformations & PNG_EXPAND)) || |
| 2932 | #endif |
| 2933 | #if defined(PNG_READ_FILLER_SUPPORTED) |
| 2934 | (png_ptr->transformations & (PNG_FILLER)) || |
| 2935 | #endif |
| 2936 | png_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2937 | { |
| 2938 | if (max_pixel_depth <= 16) |
| 2939 | max_pixel_depth = 32; |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 2940 | else |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2941 | max_pixel_depth = 64; |
| 2942 | } |
| 2943 | else |
| 2944 | { |
| 2945 | if (max_pixel_depth <= 8) |
Glenn Randers-Pehrson | a77ef62 | 2000-02-18 13:48:52 -0600 | [diff] [blame] | 2946 | { |
| 2947 | if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA) |
| 2948 | max_pixel_depth = 32; |
| 2949 | else |
| 2950 | max_pixel_depth = 24; |
| 2951 | } |
| 2952 | else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA) |
| 2953 | max_pixel_depth = 64; |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 2954 | else |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2955 | max_pixel_depth = 48; |
| 2956 | } |
| 2957 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 2958 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2959 | |
Glenn Randers-Pehrson | 6942d53 | 2000-05-01 09:31:54 -0500 | [diff] [blame] | 2960 | #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) && \ |
| 2961 | defined(PNG_USER_TRANSFORM_PTR_SUPPORTED) |
Glenn Randers-Pehrson | bcfd15d | 1999-10-01 14:22:25 -0500 | [diff] [blame] | 2962 | if(png_ptr->transformations & PNG_USER_TRANSFORM) |
| 2963 | { |
| 2964 | int user_pixel_depth=png_ptr->user_transform_depth* |
| 2965 | png_ptr->user_transform_channels; |
| 2966 | if(user_pixel_depth > max_pixel_depth) |
| 2967 | max_pixel_depth=user_pixel_depth; |
| 2968 | } |
| 2969 | #endif |
| 2970 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2971 | /* align the width on the next larger 8 pixels. Mainly used |
| 2972 | for interlacing */ |
Glenn Randers-Pehrson | d0dce40 | 1998-05-09 10:02:29 -0500 | [diff] [blame] | 2973 | row_bytes = ((png_ptr->width + 7) & ~((png_uint_32)7)); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2974 | /* calculate the maximum bytes needed, adding a byte and a pixel |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2975 | for safety's sake */ |
Glenn Randers-Pehrson | d0dce40 | 1998-05-09 10:02:29 -0500 | [diff] [blame] | 2976 | row_bytes = ((row_bytes * (png_uint_32)max_pixel_depth + 7) >> 3) + |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2977 | 1 + ((max_pixel_depth + 7) >> 3); |
| 2978 | #ifdef PNG_MAX_MALLOC_64K |
Glenn Randers-Pehrson | d0dce40 | 1998-05-09 10:02:29 -0500 | [diff] [blame] | 2979 | if (row_bytes > (png_uint_32)65536L) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2980 | png_error(png_ptr, "This image requires a row greater than 64KB"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2981 | #endif |
Glenn Randers-Pehrson | d0dce40 | 1998-05-09 10:02:29 -0500 | [diff] [blame] | 2982 | png_ptr->row_buf = (png_bytep)png_malloc(png_ptr, row_bytes); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2983 | |
| 2984 | #ifdef PNG_MAX_MALLOC_64K |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 2985 | if ((png_uint_32)png_ptr->rowbytes + 1 > (png_uint_32)65536L) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2986 | png_error(png_ptr, "This image requires a row greater than 64KB"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2987 | #endif |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame] | 2988 | png_ptr->prev_row = (png_bytep)png_malloc(png_ptr, (png_uint_32)( |
| 2989 | png_ptr->rowbytes + 1)); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2990 | |
Glenn Randers-Pehrson | a357b99 | 1998-02-08 20:56:40 -0600 | [diff] [blame] | 2991 | 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] | 2992 | |
Glenn Randers-Pehrson | 4766a24 | 2000-07-17 06:17:09 -0500 | [diff] [blame^] | 2993 | png_debug1(3, "width = %lu,\n", png_ptr->width); |
| 2994 | png_debug1(3, "height = %lu,\n", png_ptr->height); |
| 2995 | png_debug1(3, "iwidth = %lu,\n", png_ptr->iwidth); |
| 2996 | png_debug1(3, "num_rows = %lu\n", png_ptr->num_rows); |
| 2997 | png_debug1(3, "rowbytes = %lu,\n", png_ptr->rowbytes); |
| 2998 | png_debug1(3, "irowbytes = %lu,\n", png_ptr->irowbytes); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2999 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 3000 | png_ptr->flags |= PNG_FLAG_ROW_INIT; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3001 | } |