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