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