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