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 | /* pngread.c - read a PNG file |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 3 | * |
Glenn Randers-Pehrson | 13831bc | 2011-12-21 08:28:28 -0600 | [diff] [blame] | 4 | * Last changed in libpng 1.6.0 [(PENDING RELEASE)] |
Glenn Randers-Pehrson | 1531bd6 | 2012-01-01 14:45:04 -0600 | [diff] [blame^] | 5 | * Copyright (c) 1998-2012 Glenn Randers-Pehrson |
Glenn Randers-Pehrson | d436672 | 2000-06-04 14:29:29 -0500 | [diff] [blame] | 6 | * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) |
| 7 | * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 8 | * |
Glenn Randers-Pehrson | bfbf865 | 2009-06-26 21:46:52 -0500 | [diff] [blame] | 9 | * This code is released under the libpng license. |
Glenn Randers-Pehrson | c332bbc | 2009-06-25 13:43:50 -0500 | [diff] [blame] | 10 | * For conditions of distribution and use, see the disclaimer |
Glenn Randers-Pehrson | 037023b | 2009-06-24 10:27:36 -0500 | [diff] [blame] | 11 | * and license in png.h |
Glenn Randers-Pehrson | 3e61d79 | 2009-06-24 09:31:28 -0500 | [diff] [blame] | 12 | * |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 13 | * This file contains routines that an application calls directly to |
| 14 | * read a PNG file or stream. |
| 15 | */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 16 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 17 | #include "pngpriv.h" |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 18 | #if defined PNG_SIMPLIFIED_READ_SUPPORTED && defined PNG_STDIO_SUPPORTED |
| 19 | # include <errno.h> |
| 20 | #endif |
Glenn Randers-Pehrson | 9c3ab68 | 2006-02-20 22:09:05 -0600 | [diff] [blame] | 21 | |
Glenn Randers-Pehrson | c3cd22b | 2010-03-08 21:10:25 -0600 | [diff] [blame] | 22 | #ifdef PNG_READ_SUPPORTED |
Glenn Randers-Pehrson | a93c942 | 2009-04-13 11:41:33 -0500 | [diff] [blame] | 23 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 24 | /* Create a PNG structure for reading, and allocate any memory needed. */ |
Glenn Randers-Pehrson | 77396b6 | 2010-08-02 08:00:10 -0500 | [diff] [blame] | 25 | PNG_FUNCTION(png_structp,PNGAPI |
| 26 | png_create_read_struct,(png_const_charp user_png_ver, png_voidp error_ptr, |
| 27 | png_error_ptr error_fn, png_error_ptr warn_fn),PNG_ALLOCATED) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 28 | { |
John Bowler | 1d7f56a | 2011-12-21 20:14:23 -0600 | [diff] [blame] | 29 | #ifndef PNG_USER_MEM_SUPPORTED |
| 30 | png_structp png_ptr = png_create_png_struct(user_png_ver, error_ptr, |
| 31 | error_fn, warn_fn, NULL, NULL, NULL); |
| 32 | #else |
| 33 | return png_create_read_struct_2(user_png_ver, error_ptr, error_fn, |
| 34 | warn_fn, NULL, NULL, NULL); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 35 | } |
| 36 | |
Glenn Randers-Pehrson | e3f3c4e | 2010-02-07 18:08:50 -0600 | [diff] [blame] | 37 | /* Alternate create PNG structure for reading, and allocate any memory |
| 38 | * needed. |
| 39 | */ |
Glenn Randers-Pehrson | 77396b6 | 2010-08-02 08:00:10 -0500 | [diff] [blame] | 40 | PNG_FUNCTION(png_structp,PNGAPI |
| 41 | png_create_read_struct_2,(png_const_charp user_png_ver, png_voidp error_ptr, |
Glenn Randers-Pehrson | 262d0ff | 2010-03-03 07:06:54 -0600 | [diff] [blame] | 42 | png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr, |
Glenn Randers-Pehrson | 77396b6 | 2010-08-02 08:00:10 -0500 | [diff] [blame] | 43 | png_malloc_ptr malloc_fn, png_free_ptr free_fn),PNG_ALLOCATED) |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 44 | { |
John Bowler | 1d7f56a | 2011-12-21 20:14:23 -0600 | [diff] [blame] | 45 | png_structp png_ptr = png_create_png_struct(user_png_ver, error_ptr, |
| 46 | error_fn, warn_fn, mem_ptr, malloc_fn, free_fn); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 47 | #endif /* PNG_USER_MEM_SUPPORTED */ |
| 48 | |
John Bowler | 1d7f56a | 2011-12-21 20:14:23 -0600 | [diff] [blame] | 49 | if (png_ptr != NULL) |
Glenn Randers-Pehrson | a93c942 | 2009-04-13 11:41:33 -0500 | [diff] [blame] | 50 | { |
John Bowler | 1d7f56a | 2011-12-21 20:14:23 -0600 | [diff] [blame] | 51 | int ok = 0; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 52 | |
John Bowler | 1d7f56a | 2011-12-21 20:14:23 -0600 | [diff] [blame] | 53 | /* TODO: why does this happen here on read, but in png_write_IHDR on |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 54 | * write? If it happened there then there would be no error handling case |
| 55 | * here and png_ptr could be a png_structrp. |
John Bowler | 1d7f56a | 2011-12-21 20:14:23 -0600 | [diff] [blame] | 56 | */ |
| 57 | png_ptr->zstream.zalloc = png_zalloc; |
| 58 | png_ptr->zstream.zfree = png_zfree; |
| 59 | png_ptr->zstream.opaque = png_ptr; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 60 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 61 | switch (inflateInit(&png_ptr->zstream)) |
| 62 | { |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 63 | case Z_OK: |
John Bowler | 1d7f56a | 2011-12-21 20:14:23 -0600 | [diff] [blame] | 64 | ok = 1; |
| 65 | break; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 66 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 67 | case Z_MEM_ERROR: |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 68 | png_warning(png_ptr, "zlib memory error"); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 69 | break; |
| 70 | |
| 71 | case Z_STREAM_ERROR: |
| 72 | png_warning(png_ptr, "zlib stream error"); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 73 | break; |
| 74 | |
| 75 | case Z_VERSION_ERROR: |
| 76 | png_warning(png_ptr, "zlib version error"); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 77 | break; |
| 78 | |
John Bowler | 1d7f56a | 2011-12-21 20:14:23 -0600 | [diff] [blame] | 79 | default: |
| 80 | png_warning(png_ptr, "Unknown zlib error"); |
| 81 | break; |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 82 | } |
John Bowler | 1d7f56a | 2011-12-21 20:14:23 -0600 | [diff] [blame] | 83 | |
| 84 | if (ok) |
| 85 | { |
| 86 | png_ptr->zstream.next_out = png_ptr->zbuf; |
| 87 | png_ptr->zstream.avail_out = png_ptr->zbuf_size; |
| 88 | |
| 89 | /* TODO: delay this, it can be done in png_init_io (if the app doesn't |
| 90 | * do it itself) avoiding setting the default function if it is not |
| 91 | * required. |
| 92 | */ |
| 93 | png_set_read_fn(png_ptr, NULL, NULL); |
| 94 | |
| 95 | return png_ptr; |
| 96 | } |
| 97 | |
| 98 | /* Else something went wrong in the zlib initialization above; it would |
| 99 | * much simplify this code if the creation of the zlib stuff was to be |
| 100 | * delayed until it is needed. |
| 101 | */ |
| 102 | png_destroy_read_struct(&png_ptr, NULL, NULL); |
Glenn Randers-Pehrson | a93c942 | 2009-04-13 11:41:33 -0500 | [diff] [blame] | 103 | } |
| 104 | |
John Bowler | 1d7f56a | 2011-12-21 20:14:23 -0600 | [diff] [blame] | 105 | return NULL; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 106 | } |
| 107 | |
Glenn Randers-Pehrson | 8b6a889 | 2001-05-18 04:54:50 -0500 | [diff] [blame] | 108 | |
Glenn Randers-Pehrson | dbd4014 | 2009-08-31 08:42:02 -0500 | [diff] [blame] | 109 | #ifdef PNG_SEQUENTIAL_READ_SUPPORTED |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 110 | /* Read the information before the actual image data. This has been |
Glenn Randers-Pehrson | f9f2fe0 | 1998-03-15 18:20:23 -0600 | [diff] [blame] | 111 | * changed in v0.90 to allow reading a file that already has the magic |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 112 | * bytes read from the stream. You can tell libpng how many bytes have |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 113 | * been read from the beginning of the stream (up to the maximum of 8) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 114 | * via png_set_sig_bytes(), and we will only check the remaining bytes |
| 115 | * here. The application can then have access to the signature bytes we |
| 116 | * read if it is determined that this isn't a valid PNG file. |
| 117 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 118 | void PNGAPI |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 119 | png_read_info(png_structrp png_ptr, png_inforp info_ptr) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 120 | { |
Glenn Randers-Pehrson | c81bb8a | 2009-08-15 22:02:26 -0500 | [diff] [blame] | 121 | png_debug(1, "in png_read_info"); |
Glenn Randers-Pehrson | 821b710 | 2010-06-24 16:16:32 -0500 | [diff] [blame] | 122 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 123 | if (png_ptr == NULL || info_ptr == NULL) |
| 124 | return; |
Glenn Randers-Pehrson | 821b710 | 2010-06-24 16:16:32 -0500 | [diff] [blame] | 125 | |
Glenn Randers-Pehrson | a581556 | 2010-11-20 21:48:29 -0600 | [diff] [blame] | 126 | /* Read and check the PNG file signature. */ |
| 127 | png_read_sig(png_ptr, info_ptr); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 128 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 129 | for (;;) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 130 | { |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 131 | png_uint_32 length = png_read_chunk_header(png_ptr); |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 132 | png_uint_32 chunk_name = png_ptr->chunk_name; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 133 | |
| 134 | /* This should be a binary subdivision search or a hash for |
| 135 | * matching the chunk name rather than a linear search. |
| 136 | */ |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 137 | if (chunk_name == png_IDAT) |
Glenn Randers-Pehrson | 262d0ff | 2010-03-03 07:06:54 -0600 | [diff] [blame] | 138 | if (png_ptr->mode & PNG_AFTER_IDAT) |
| 139 | png_ptr->mode |= PNG_HAVE_CHUNK_AFTER_IDAT; |
Glenn Randers-Pehrson | 6bc53be | 2006-06-16 07:52:03 -0500 | [diff] [blame] | 140 | |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 141 | if (chunk_name == png_IHDR) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 142 | png_handle_IHDR(png_ptr, info_ptr, length); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 143 | |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 144 | else if (chunk_name == png_IEND) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 145 | png_handle_IEND(png_ptr, info_ptr, length); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 146 | |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 147 | #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 148 | else if (png_chunk_unknown_handling(png_ptr, chunk_name) != |
| 149 | PNG_HANDLE_CHUNK_AS_DEFAULT) |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 150 | { |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 151 | if (chunk_name == png_IDAT) |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 152 | png_ptr->mode |= PNG_HAVE_IDAT; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 153 | |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 154 | png_handle_unknown(png_ptr, info_ptr, length); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 155 | |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 156 | if (chunk_name == png_PLTE) |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 157 | png_ptr->mode |= PNG_HAVE_PLTE; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 158 | |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 159 | else if (chunk_name == png_IDAT) |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 160 | { |
| 161 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
| 162 | png_error(png_ptr, "Missing IHDR before IDAT"); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 163 | |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 164 | else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE && |
Glenn Randers-Pehrson | 262d0ff | 2010-03-03 07:06:54 -0600 | [diff] [blame] | 165 | !(png_ptr->mode & PNG_HAVE_PLTE)) |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 166 | png_error(png_ptr, "Missing PLTE before IDAT"); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 167 | |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 168 | break; |
| 169 | } |
| 170 | } |
| 171 | #endif |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 172 | else if (chunk_name == png_PLTE) |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 173 | png_handle_PLTE(png_ptr, info_ptr, length); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 174 | |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 175 | else if (chunk_name == png_IDAT) |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 176 | { |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 177 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
| 178 | png_error(png_ptr, "Missing IHDR before IDAT"); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 179 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 180 | else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE && |
Glenn Randers-Pehrson | 262d0ff | 2010-03-03 07:06:54 -0600 | [diff] [blame] | 181 | !(png_ptr->mode & PNG_HAVE_PLTE)) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 182 | png_error(png_ptr, "Missing PLTE before IDAT"); |
| 183 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 184 | png_ptr->idat_size = length; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 185 | png_ptr->mode |= PNG_HAVE_IDAT; |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 186 | break; |
| 187 | } |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 188 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 189 | #ifdef PNG_READ_bKGD_SUPPORTED |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 190 | else if (chunk_name == png_bKGD) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 191 | png_handle_bKGD(png_ptr, info_ptr, length); |
| 192 | #endif |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 193 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 194 | #ifdef PNG_READ_cHRM_SUPPORTED |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 195 | else if (chunk_name == png_cHRM) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 196 | png_handle_cHRM(png_ptr, info_ptr, length); |
| 197 | #endif |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 198 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 199 | #ifdef PNG_READ_gAMA_SUPPORTED |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 200 | else if (chunk_name == png_gAMA) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 201 | png_handle_gAMA(png_ptr, info_ptr, length); |
| 202 | #endif |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 203 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 204 | #ifdef PNG_READ_hIST_SUPPORTED |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 205 | else if (chunk_name == png_hIST) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 206 | png_handle_hIST(png_ptr, info_ptr, length); |
| 207 | #endif |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 208 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 209 | #ifdef PNG_READ_oFFs_SUPPORTED |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 210 | else if (chunk_name == png_oFFs) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 211 | png_handle_oFFs(png_ptr, info_ptr, length); |
| 212 | #endif |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 213 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 214 | #ifdef PNG_READ_pCAL_SUPPORTED |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 215 | else if (chunk_name == png_pCAL) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 216 | png_handle_pCAL(png_ptr, info_ptr, length); |
| 217 | #endif |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 218 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 219 | #ifdef PNG_READ_sCAL_SUPPORTED |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 220 | else if (chunk_name == png_sCAL) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 221 | png_handle_sCAL(png_ptr, info_ptr, length); |
| 222 | #endif |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 223 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 224 | #ifdef PNG_READ_pHYs_SUPPORTED |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 225 | else if (chunk_name == png_pHYs) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 226 | png_handle_pHYs(png_ptr, info_ptr, length); |
| 227 | #endif |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 228 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 229 | #ifdef PNG_READ_sBIT_SUPPORTED |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 230 | else if (chunk_name == png_sBIT) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 231 | png_handle_sBIT(png_ptr, info_ptr, length); |
| 232 | #endif |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 233 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 234 | #ifdef PNG_READ_sRGB_SUPPORTED |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 235 | else if (chunk_name == png_sRGB) |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 236 | png_handle_sRGB(png_ptr, info_ptr, length); |
| 237 | #endif |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 238 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 239 | #ifdef PNG_READ_iCCP_SUPPORTED |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 240 | else if (chunk_name == png_iCCP) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 241 | png_handle_iCCP(png_ptr, info_ptr, length); |
| 242 | #endif |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 243 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 244 | #ifdef PNG_READ_sPLT_SUPPORTED |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 245 | else if (chunk_name == png_sPLT) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 246 | png_handle_sPLT(png_ptr, info_ptr, length); |
| 247 | #endif |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 248 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 249 | #ifdef PNG_READ_tEXt_SUPPORTED |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 250 | else if (chunk_name == png_tEXt) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 251 | png_handle_tEXt(png_ptr, info_ptr, length); |
| 252 | #endif |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 253 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 254 | #ifdef PNG_READ_tIME_SUPPORTED |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 255 | else if (chunk_name == png_tIME) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 256 | png_handle_tIME(png_ptr, info_ptr, length); |
| 257 | #endif |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 258 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 259 | #ifdef PNG_READ_tRNS_SUPPORTED |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 260 | else if (chunk_name == png_tRNS) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 261 | png_handle_tRNS(png_ptr, info_ptr, length); |
| 262 | #endif |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 263 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 264 | #ifdef PNG_READ_zTXt_SUPPORTED |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 265 | else if (chunk_name == png_zTXt) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 266 | png_handle_zTXt(png_ptr, info_ptr, length); |
| 267 | #endif |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 268 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 269 | #ifdef PNG_READ_iTXt_SUPPORTED |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 270 | else if (chunk_name == png_iTXt) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 271 | png_handle_iTXt(png_ptr, info_ptr, length); |
| 272 | #endif |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 273 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 274 | else |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 275 | png_handle_unknown(png_ptr, info_ptr, length); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 276 | } |
| 277 | } |
Glenn Randers-Pehrson | dbd4014 | 2009-08-31 08:42:02 -0500 | [diff] [blame] | 278 | #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 279 | |
Glenn Randers-Pehrson | d8eb62f | 2009-05-30 20:19:20 -0500 | [diff] [blame] | 280 | /* Optional call to update the users info_ptr structure */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 281 | void PNGAPI |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 282 | png_read_update_info(png_structrp png_ptr, png_inforp info_ptr) |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 283 | { |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 284 | png_debug(1, "in png_read_update_info"); |
Glenn Randers-Pehrson | 821b710 | 2010-06-24 16:16:32 -0500 | [diff] [blame] | 285 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 286 | if (png_ptr == NULL) |
| 287 | return; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 288 | |
Glenn Randers-Pehrson | 3dbfd30 | 2011-10-13 17:24:36 -0500 | [diff] [blame] | 289 | png_read_start_row(png_ptr); |
Glenn Randers-Pehrson | 4cfdb3c | 2009-11-26 11:49:37 -0600 | [diff] [blame] | 290 | |
John Bowler | 4a12f4a | 2011-04-17 18:34:22 -0500 | [diff] [blame] | 291 | #ifdef PNG_READ_TRANSFORMS_SUPPORTED |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 292 | png_read_transform_info(png_ptr, info_ptr); |
John Bowler | 4a12f4a | 2011-04-17 18:34:22 -0500 | [diff] [blame] | 293 | #else |
| 294 | PNG_UNUSED(info_ptr) |
| 295 | #endif |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 296 | } |
| 297 | |
Glenn Randers-Pehrson | dbd4014 | 2009-08-31 08:42:02 -0500 | [diff] [blame] | 298 | #ifdef PNG_SEQUENTIAL_READ_SUPPORTED |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 299 | /* Initialize palette, background, etc, after transformations |
| 300 | * are set, but before any reading takes place. This allows |
Glenn Randers-Pehrson | 345bc27 | 1998-06-14 14:43:31 -0500 | [diff] [blame] | 301 | * the user to obtain a gamma-corrected palette, for example. |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 302 | * If the user doesn't call this, we will do it ourselves. |
| 303 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 304 | void PNGAPI |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 305 | png_start_read_image(png_structrp png_ptr) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 306 | { |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 307 | png_debug(1, "in png_start_read_image"); |
Glenn Randers-Pehrson | 821b710 | 2010-06-24 16:16:32 -0500 | [diff] [blame] | 308 | |
Glenn Randers-Pehrson | 3dbfd30 | 2011-10-13 17:24:36 -0500 | [diff] [blame] | 309 | if (png_ptr != NULL) |
| 310 | png_read_start_row(png_ptr); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 311 | } |
Glenn Randers-Pehrson | dbd4014 | 2009-08-31 08:42:02 -0500 | [diff] [blame] | 312 | #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 313 | |
Glenn Randers-Pehrson | dbd4014 | 2009-08-31 08:42:02 -0500 | [diff] [blame] | 314 | #ifdef PNG_SEQUENTIAL_READ_SUPPORTED |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 315 | void PNGAPI |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 316 | png_read_row(png_structrp png_ptr, png_bytep row, png_bytep dsp_row) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 317 | { |
| 318 | int ret; |
Glenn Randers-Pehrson | 821b710 | 2010-06-24 16:16:32 -0500 | [diff] [blame] | 319 | |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 320 | png_row_info row_info; |
| 321 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 322 | if (png_ptr == NULL) |
| 323 | return; |
Glenn Randers-Pehrson | 821b710 | 2010-06-24 16:16:32 -0500 | [diff] [blame] | 324 | |
Glenn Randers-Pehrson | 6d75d0c | 2009-08-22 08:45:09 -0500 | [diff] [blame] | 325 | png_debug2(1, "in png_read_row (row %lu, pass %d)", |
Glenn Randers-Pehrson | d233287 | 2010-10-12 19:19:28 -0500 | [diff] [blame] | 326 | (unsigned long)png_ptr->row_number, png_ptr->pass); |
Glenn Randers-Pehrson | 6d75d0c | 2009-08-22 08:45:09 -0500 | [diff] [blame] | 327 | |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 328 | /* png_read_start_row sets the information (in particular iwidth) for this |
| 329 | * interlace pass. |
| 330 | */ |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 331 | if (!(png_ptr->flags & PNG_FLAG_ROW_INIT)) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 332 | png_read_start_row(png_ptr); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 333 | |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 334 | /* 1.5.6: row_info moved out of png_struct to a local here. */ |
| 335 | row_info.width = png_ptr->iwidth; /* NOTE: width of current interlaced row */ |
| 336 | row_info.color_type = png_ptr->color_type; |
| 337 | row_info.bit_depth = png_ptr->bit_depth; |
| 338 | row_info.channels = png_ptr->channels; |
| 339 | row_info.pixel_depth = png_ptr->pixel_depth; |
| 340 | row_info.rowbytes = PNG_ROWBYTES(row_info.pixel_depth, row_info.width); |
| 341 | |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 342 | if (png_ptr->row_number == 0 && png_ptr->pass == 0) |
| 343 | { |
Glenn Randers-Pehrson | d8eb62f | 2009-05-30 20:19:20 -0500 | [diff] [blame] | 344 | /* Check for transforms that have been set but were defined out */ |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 345 | #if defined(PNG_WRITE_INVERT_SUPPORTED) && !defined(PNG_READ_INVERT_SUPPORTED) |
| 346 | if (png_ptr->transformations & PNG_INVERT_MONO) |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 347 | png_warning(png_ptr, "PNG_READ_INVERT_SUPPORTED is not defined"); |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 348 | #endif |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 349 | |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 350 | #if defined(PNG_WRITE_FILLER_SUPPORTED) && !defined(PNG_READ_FILLER_SUPPORTED) |
| 351 | if (png_ptr->transformations & PNG_FILLER) |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 352 | png_warning(png_ptr, "PNG_READ_FILLER_SUPPORTED is not defined"); |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 353 | #endif |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 354 | |
Glenn Randers-Pehrson | e3f3c4e | 2010-02-07 18:08:50 -0600 | [diff] [blame] | 355 | #if defined(PNG_WRITE_PACKSWAP_SUPPORTED) && \ |
| 356 | !defined(PNG_READ_PACKSWAP_SUPPORTED) |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 357 | if (png_ptr->transformations & PNG_PACKSWAP) |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 358 | png_warning(png_ptr, "PNG_READ_PACKSWAP_SUPPORTED is not defined"); |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 359 | #endif |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 360 | |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 361 | #if defined(PNG_WRITE_PACK_SUPPORTED) && !defined(PNG_READ_PACK_SUPPORTED) |
| 362 | if (png_ptr->transformations & PNG_PACK) |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 363 | png_warning(png_ptr, "PNG_READ_PACK_SUPPORTED is not defined"); |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 364 | #endif |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 365 | |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 366 | #if defined(PNG_WRITE_SHIFT_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED) |
| 367 | if (png_ptr->transformations & PNG_SHIFT) |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 368 | png_warning(png_ptr, "PNG_READ_SHIFT_SUPPORTED is not defined"); |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 369 | #endif |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 370 | |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 371 | #if defined(PNG_WRITE_BGR_SUPPORTED) && !defined(PNG_READ_BGR_SUPPORTED) |
| 372 | if (png_ptr->transformations & PNG_BGR) |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 373 | png_warning(png_ptr, "PNG_READ_BGR_SUPPORTED is not defined"); |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 374 | #endif |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 375 | |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 376 | #if defined(PNG_WRITE_SWAP_SUPPORTED) && !defined(PNG_READ_SWAP_SUPPORTED) |
| 377 | if (png_ptr->transformations & PNG_SWAP_BYTES) |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 378 | png_warning(png_ptr, "PNG_READ_SWAP_SUPPORTED is not defined"); |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 379 | #endif |
| 380 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 381 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 382 | #ifdef PNG_READ_INTERLACING_SUPPORTED |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 383 | /* If interlaced and we do not need a new row, combine row and return. |
| 384 | * Notice that the pixels we have from previous rows have been transformed |
| 385 | * already; we can only combine like with like (transformed or |
| 386 | * untransformed) and, because of the libpng API for interlaced images, this |
| 387 | * means we must transform before de-interlacing. |
| 388 | */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 389 | if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE)) |
| 390 | { |
| 391 | switch (png_ptr->pass) |
| 392 | { |
| 393 | case 0: |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 394 | if (png_ptr->row_number & 0x07) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 395 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 396 | if (dsp_row != NULL) |
Glenn Randers-Pehrson | 65c0339 | 2011-10-06 21:54:17 -0500 | [diff] [blame] | 397 | png_combine_row(png_ptr, dsp_row, 1/*display*/); |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 398 | png_read_finish_row(png_ptr); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 399 | return; |
| 400 | } |
| 401 | break; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 402 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 403 | case 1: |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 404 | if ((png_ptr->row_number & 0x07) || png_ptr->width < 5) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 405 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 406 | if (dsp_row != NULL) |
Glenn Randers-Pehrson | 65c0339 | 2011-10-06 21:54:17 -0500 | [diff] [blame] | 407 | png_combine_row(png_ptr, dsp_row, 1/*display*/); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 408 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 409 | png_read_finish_row(png_ptr); |
| 410 | return; |
| 411 | } |
| 412 | break; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 413 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 414 | case 2: |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 415 | if ((png_ptr->row_number & 0x07) != 4) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 416 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 417 | if (dsp_row != NULL && (png_ptr->row_number & 4)) |
Glenn Randers-Pehrson | 65c0339 | 2011-10-06 21:54:17 -0500 | [diff] [blame] | 418 | png_combine_row(png_ptr, dsp_row, 1/*display*/); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 419 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 420 | png_read_finish_row(png_ptr); |
| 421 | return; |
| 422 | } |
| 423 | break; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 424 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 425 | case 3: |
| 426 | if ((png_ptr->row_number & 3) || png_ptr->width < 3) |
| 427 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 428 | if (dsp_row != NULL) |
Glenn Randers-Pehrson | 65c0339 | 2011-10-06 21:54:17 -0500 | [diff] [blame] | 429 | png_combine_row(png_ptr, dsp_row, 1/*display*/); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 430 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 431 | png_read_finish_row(png_ptr); |
| 432 | return; |
| 433 | } |
| 434 | break; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 435 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 436 | case 4: |
| 437 | if ((png_ptr->row_number & 3) != 2) |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 438 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 439 | if (dsp_row != NULL && (png_ptr->row_number & 2)) |
Glenn Randers-Pehrson | 65c0339 | 2011-10-06 21:54:17 -0500 | [diff] [blame] | 440 | png_combine_row(png_ptr, dsp_row, 1/*display*/); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 441 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 442 | png_read_finish_row(png_ptr); |
| 443 | return; |
| 444 | } |
| 445 | break; |
| 446 | case 5: |
| 447 | if ((png_ptr->row_number & 1) || png_ptr->width < 2) |
| 448 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 449 | if (dsp_row != NULL) |
Glenn Randers-Pehrson | 65c0339 | 2011-10-06 21:54:17 -0500 | [diff] [blame] | 450 | png_combine_row(png_ptr, dsp_row, 1/*display*/); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 451 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 452 | png_read_finish_row(png_ptr); |
| 453 | return; |
| 454 | } |
| 455 | break; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 456 | |
Glenn Randers-Pehrson | b3edc73 | 2010-11-21 14:06:41 -0600 | [diff] [blame] | 457 | default: |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 458 | case 6: |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 459 | if (!(png_ptr->row_number & 1)) |
| 460 | { |
| 461 | png_read_finish_row(png_ptr); |
| 462 | return; |
| 463 | } |
| 464 | break; |
| 465 | } |
| 466 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 467 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 468 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 469 | if (!(png_ptr->mode & PNG_HAVE_IDAT)) |
| 470 | png_error(png_ptr, "Invalid attempt to read row data"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 471 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 472 | png_ptr->zstream.next_out = png_ptr->row_buf; |
Glenn Randers-Pehrson | e3f3c4e | 2010-02-07 18:08:50 -0600 | [diff] [blame] | 473 | png_ptr->zstream.avail_out = |
| 474 | (uInt)(PNG_ROWBYTES(png_ptr->pixel_depth, |
| 475 | png_ptr->iwidth) + 1); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 476 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 477 | do |
| 478 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 479 | if (!(png_ptr->zstream.avail_in)) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 480 | { |
| 481 | while (!png_ptr->idat_size) |
| 482 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 483 | png_crc_finish(png_ptr, 0); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 484 | |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 485 | png_ptr->idat_size = png_read_chunk_header(png_ptr); |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 486 | if (png_ptr->chunk_name != png_IDAT) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 487 | png_error(png_ptr, "Not enough image data"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 488 | } |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 489 | png_ptr->zstream.avail_in = (uInt)png_ptr->zbuf_size; |
| 490 | png_ptr->zstream.next_in = png_ptr->zbuf; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 491 | if (png_ptr->zbuf_size > png_ptr->idat_size) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 492 | png_ptr->zstream.avail_in = (uInt)png_ptr->idat_size; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 493 | png_crc_read(png_ptr, png_ptr->zbuf, |
Glenn Randers-Pehrson | 262d0ff | 2010-03-03 07:06:54 -0600 | [diff] [blame] | 494 | (png_size_t)png_ptr->zstream.avail_in); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 495 | png_ptr->idat_size -= png_ptr->zstream.avail_in; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 496 | } |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 497 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 498 | ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 499 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 500 | if (ret == Z_STREAM_END) |
| 501 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 502 | if (png_ptr->zstream.avail_out || png_ptr->zstream.avail_in || |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 503 | png_ptr->idat_size) |
Glenn Randers-Pehrson | 6bc53be | 2006-06-16 07:52:03 -0500 | [diff] [blame] | 504 | png_benign_error(png_ptr, "Extra compressed data"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 505 | png_ptr->mode |= PNG_AFTER_IDAT; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 506 | png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED; |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 507 | break; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 508 | } |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 509 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 510 | if (ret != Z_OK) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 511 | png_error(png_ptr, png_ptr->zstream.msg ? png_ptr->zstream.msg : |
Glenn Randers-Pehrson | 262d0ff | 2010-03-03 07:06:54 -0600 | [diff] [blame] | 512 | "Decompression error"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 513 | |
| 514 | } while (png_ptr->zstream.avail_out); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 515 | |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 516 | if (png_ptr->row_buf[0] > PNG_FILTER_VALUE_NONE) |
| 517 | { |
| 518 | if (png_ptr->row_buf[0] < PNG_FILTER_VALUE_LAST) |
Mans Rullgard | d3a9480 | 2011-11-03 00:47:55 -0500 | [diff] [blame] | 519 | png_read_filter_row(png_ptr, &row_info, png_ptr->row_buf + 1, |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 520 | png_ptr->prev_row + 1, png_ptr->row_buf[0]); |
| 521 | else |
| 522 | png_error(png_ptr, "bad adaptive filter value"); |
| 523 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 524 | |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 525 | /* libpng 1.5.6: the following line was copying png_ptr->rowbytes before |
| 526 | * 1.5.6, while the buffer really is this big in current versions of libpng |
| 527 | * it may not be in the future, so this was changed just to copy the |
| 528 | * interlaced count: |
| 529 | */ |
| 530 | png_memcpy(png_ptr->prev_row, png_ptr->row_buf, row_info.rowbytes + 1); |
Glenn Randers-Pehrson | e647462 | 2006-03-04 16:50:47 -0600 | [diff] [blame] | 531 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 532 | #ifdef PNG_MNG_FEATURES_SUPPORTED |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 533 | if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) && |
Glenn Randers-Pehrson | 262d0ff | 2010-03-03 07:06:54 -0600 | [diff] [blame] | 534 | (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING)) |
Glenn Randers-Pehrson | 2ad31ae | 2000-12-15 08:54:42 -0600 | [diff] [blame] | 535 | { |
| 536 | /* Intrapixel differencing */ |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 537 | png_do_read_intrapixel(&row_info, png_ptr->row_buf + 1); |
Glenn Randers-Pehrson | 2ad31ae | 2000-12-15 08:54:42 -0600 | [diff] [blame] | 538 | } |
| 539 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 540 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 541 | |
John Bowler | 4a12f4a | 2011-04-17 18:34:22 -0500 | [diff] [blame] | 542 | #ifdef PNG_READ_TRANSFORMS_SUPPORTED |
John Bowler | 9b872f4 | 2011-02-12 09:00:16 -0600 | [diff] [blame] | 543 | if (png_ptr->transformations) |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 544 | png_do_read_transformations(png_ptr, &row_info); |
John Bowler | 4a12f4a | 2011-04-17 18:34:22 -0500 | [diff] [blame] | 545 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 546 | |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 547 | /* The transformed pixel depth should match the depth now in row_info. */ |
| 548 | if (png_ptr->transformed_pixel_depth == 0) |
| 549 | { |
| 550 | png_ptr->transformed_pixel_depth = row_info.pixel_depth; |
| 551 | if (row_info.pixel_depth > png_ptr->maximum_pixel_depth) |
| 552 | png_error(png_ptr, "sequential row overflow"); |
| 553 | } |
| 554 | |
| 555 | else if (png_ptr->transformed_pixel_depth != row_info.pixel_depth) |
| 556 | png_error(png_ptr, "internal sequential row size calculation error"); |
| 557 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 558 | #ifdef PNG_READ_INTERLACING_SUPPORTED |
Glenn Randers-Pehrson | d8eb62f | 2009-05-30 20:19:20 -0500 | [diff] [blame] | 559 | /* Blow up interlaced rows to full size */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 560 | if (png_ptr->interlaced && |
| 561 | (png_ptr->transformations & PNG_INTERLACE)) |
| 562 | { |
| 563 | if (png_ptr->pass < 6) |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 564 | png_do_read_interlace(&row_info, png_ptr->row_buf + 1, png_ptr->pass, |
| 565 | png_ptr->transformations); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 566 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 567 | if (dsp_row != NULL) |
Glenn Randers-Pehrson | 65c0339 | 2011-10-06 21:54:17 -0500 | [diff] [blame] | 568 | png_combine_row(png_ptr, dsp_row, 1/*display*/); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 569 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 570 | if (row != NULL) |
Glenn Randers-Pehrson | 65c0339 | 2011-10-06 21:54:17 -0500 | [diff] [blame] | 571 | png_combine_row(png_ptr, row, 0/*row*/); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 572 | } |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 573 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 574 | else |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 575 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 576 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 577 | if (row != NULL) |
Glenn Randers-Pehrson | 65c0339 | 2011-10-06 21:54:17 -0500 | [diff] [blame] | 578 | png_combine_row(png_ptr, row, -1/*ignored*/); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 579 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 580 | if (dsp_row != NULL) |
Glenn Randers-Pehrson | 65c0339 | 2011-10-06 21:54:17 -0500 | [diff] [blame] | 581 | png_combine_row(png_ptr, dsp_row, -1/*ignored*/); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 582 | } |
| 583 | png_read_finish_row(png_ptr); |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 584 | |
| 585 | if (png_ptr->read_row_fn != NULL) |
| 586 | (*(png_ptr->read_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 587 | } |
Glenn Randers-Pehrson | dbd4014 | 2009-08-31 08:42:02 -0500 | [diff] [blame] | 588 | #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 589 | |
Glenn Randers-Pehrson | dbd4014 | 2009-08-31 08:42:02 -0500 | [diff] [blame] | 590 | #ifdef PNG_SEQUENTIAL_READ_SUPPORTED |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 591 | /* Read one or more rows of image data. If the image is interlaced, |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 592 | * and png_set_interlace_handling() has been called, the rows need to |
| 593 | * contain the contents of the rows from the previous pass. If the |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 594 | * image has alpha or transparency, and png_handle_alpha()[*] has been |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 595 | * called, the rows contents must be initialized to the contents of the |
| 596 | * screen. |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 597 | * |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 598 | * "row" holds the actual image, and pixels are placed in it |
| 599 | * as they arrive. If the image is displayed after each pass, it will |
| 600 | * appear to "sparkle" in. "display_row" can be used to display a |
| 601 | * "chunky" progressive image, with finer detail added as it becomes |
| 602 | * available. If you do not want this "chunky" display, you may pass |
| 603 | * NULL for display_row. If you do not want the sparkle display, and |
| 604 | * you have not called png_handle_alpha(), you may pass NULL for rows. |
| 605 | * If you have called png_handle_alpha(), and the image has either an |
| 606 | * alpha channel or a transparency chunk, you must provide a buffer for |
| 607 | * rows. In this case, you do not have to provide a display_row buffer |
| 608 | * also, but you may. If the image is not interlaced, or if you have |
| 609 | * not called png_set_interlace_handling(), the display_row buffer will |
| 610 | * be ignored, so pass NULL to it. |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 611 | * |
Glenn Randers-Pehrson | c3d51c1 | 2006-03-02 07:23:18 -0600 | [diff] [blame] | 612 | * [*] png_handle_alpha() does not exist yet, as of this version of libpng |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 613 | */ |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 614 | |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 615 | void PNGAPI |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 616 | png_read_rows(png_structrp png_ptr, png_bytepp row, |
Glenn Randers-Pehrson | 262d0ff | 2010-03-03 07:06:54 -0600 | [diff] [blame] | 617 | png_bytepp display_row, png_uint_32 num_rows) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 618 | { |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 619 | png_uint_32 i; |
| 620 | png_bytepp rp; |
| 621 | png_bytepp dp; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 622 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 623 | png_debug(1, "in png_read_rows"); |
Glenn Randers-Pehrson | 821b710 | 2010-06-24 16:16:32 -0500 | [diff] [blame] | 624 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 625 | if (png_ptr == NULL) |
| 626 | return; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 627 | |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 628 | rp = row; |
| 629 | dp = display_row; |
Glenn Randers-Pehrson | 8686fff | 1998-05-21 09:27:50 -0500 | [diff] [blame] | 630 | if (rp != NULL && dp != NULL) |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 631 | for (i = 0; i < num_rows; i++) |
| 632 | { |
| 633 | png_bytep rptr = *rp++; |
| 634 | png_bytep dptr = *dp++; |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 635 | |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 636 | png_read_row(png_ptr, rptr, dptr); |
| 637 | } |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 638 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 639 | else if (rp != NULL) |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 640 | for (i = 0; i < num_rows; i++) |
| 641 | { |
Glenn Randers-Pehrson | 8686fff | 1998-05-21 09:27:50 -0500 | [diff] [blame] | 642 | png_bytep rptr = *rp; |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 643 | png_read_row(png_ptr, rptr, NULL); |
Glenn Randers-Pehrson | 8686fff | 1998-05-21 09:27:50 -0500 | [diff] [blame] | 644 | rp++; |
| 645 | } |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 646 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 647 | else if (dp != NULL) |
Glenn Randers-Pehrson | 8686fff | 1998-05-21 09:27:50 -0500 | [diff] [blame] | 648 | for (i = 0; i < num_rows; i++) |
| 649 | { |
| 650 | png_bytep dptr = *dp; |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 651 | png_read_row(png_ptr, NULL, dptr); |
Glenn Randers-Pehrson | 8686fff | 1998-05-21 09:27:50 -0500 | [diff] [blame] | 652 | dp++; |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 653 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 654 | } |
Glenn Randers-Pehrson | dbd4014 | 2009-08-31 08:42:02 -0500 | [diff] [blame] | 655 | #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 656 | |
Glenn Randers-Pehrson | dbd4014 | 2009-08-31 08:42:02 -0500 | [diff] [blame] | 657 | #ifdef PNG_SEQUENTIAL_READ_SUPPORTED |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 658 | /* Read the entire image. If the image has an alpha channel or a tRNS |
Glenn Randers-Pehrson | 345bc27 | 1998-06-14 14:43:31 -0500 | [diff] [blame] | 659 | * chunk, and you have called png_handle_alpha()[*], you will need to |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 660 | * initialize the image to the current image that PNG will be overlaying. |
| 661 | * We set the num_rows again here, in case it was incorrectly set in |
| 662 | * png_read_start_row() by a call to png_read_update_info() or |
| 663 | * png_start_read_image() if png_set_interlace_handling() wasn't called |
| 664 | * prior to either of these functions like it should have been. You can |
| 665 | * only call this function once. If you desire to have an image for |
| 666 | * each pass of a interlaced image, use png_read_rows() instead. |
Glenn Randers-Pehrson | 345bc27 | 1998-06-14 14:43:31 -0500 | [diff] [blame] | 667 | * |
Glenn Randers-Pehrson | c3d51c1 | 2006-03-02 07:23:18 -0600 | [diff] [blame] | 668 | * [*] png_handle_alpha() does not exist yet, as of this version of libpng |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 669 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 670 | void PNGAPI |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 671 | png_read_image(png_structrp png_ptr, png_bytepp image) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 672 | { |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 673 | png_uint_32 i, image_height; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 674 | int pass, j; |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 675 | png_bytepp rp; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 676 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 677 | png_debug(1, "in png_read_image"); |
Glenn Randers-Pehrson | 821b710 | 2010-06-24 16:16:32 -0500 | [diff] [blame] | 678 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 679 | if (png_ptr == NULL) |
| 680 | return; |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 681 | |
| 682 | #ifdef PNG_READ_INTERLACING_SUPPORTED |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 683 | if (!(png_ptr->flags & PNG_FLAG_ROW_INIT)) |
| 684 | { |
| 685 | pass = png_set_interlace_handling(png_ptr); |
| 686 | /* And make sure transforms are initialized. */ |
| 687 | png_start_read_image(png_ptr); |
| 688 | } |
| 689 | else |
| 690 | { |
Glenn Randers-Pehrson | e15a96b | 2011-01-14 15:47:37 -0600 | [diff] [blame] | 691 | if (png_ptr->interlaced && !(png_ptr->transformations & PNG_INTERLACE)) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 692 | { |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 693 | /* Caller called png_start_read_image or png_read_update_info without |
| 694 | * first turning on the PNG_INTERLACE transform. We can fix this here, |
| 695 | * but the caller should do it! |
| 696 | */ |
| 697 | png_warning(png_ptr, "Interlace handling should be turned on when " |
| 698 | "using png_read_image"); |
| 699 | /* Make sure this is set correctly */ |
| 700 | png_ptr->num_rows = png_ptr->height; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 701 | } |
| 702 | |
| 703 | /* Obtain the pass number, which also turns on the PNG_INTERLACE flag in |
| 704 | * the above error case. |
| 705 | */ |
| 706 | pass = png_set_interlace_handling(png_ptr); |
| 707 | } |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 708 | #else |
| 709 | if (png_ptr->interlaced) |
| 710 | png_error(png_ptr, |
Glenn Randers-Pehrson | 262d0ff | 2010-03-03 07:06:54 -0600 | [diff] [blame] | 711 | "Cannot read interlaced image -- interlace handler disabled"); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 712 | |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 713 | pass = 1; |
| 714 | #endif |
| 715 | |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 716 | image_height=png_ptr->height; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 717 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 718 | for (j = 0; j < pass; j++) |
| 719 | { |
| 720 | rp = image; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 721 | for (i = 0; i < image_height; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 722 | { |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 723 | png_read_row(png_ptr, *rp, NULL); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 724 | rp++; |
| 725 | } |
| 726 | } |
| 727 | } |
Glenn Randers-Pehrson | dbd4014 | 2009-08-31 08:42:02 -0500 | [diff] [blame] | 728 | #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 729 | |
Glenn Randers-Pehrson | dbd4014 | 2009-08-31 08:42:02 -0500 | [diff] [blame] | 730 | #ifdef PNG_SEQUENTIAL_READ_SUPPORTED |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 731 | /* Read the end of the PNG file. Will not read past the end of the |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 732 | * file, will verify the end is accurate, and will read any comments |
| 733 | * or time information at the end of the file, if info is not NULL. |
| 734 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 735 | void PNGAPI |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 736 | png_read_end(png_structrp png_ptr, png_inforp info_ptr) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 737 | { |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 738 | png_debug(1, "in png_read_end"); |
Glenn Randers-Pehrson | 821b710 | 2010-06-24 16:16:32 -0500 | [diff] [blame] | 739 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 740 | if (png_ptr == NULL) |
| 741 | return; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 742 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 743 | png_crc_finish(png_ptr, 0); /* Finish off CRC from last IDAT chunk */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 744 | |
| 745 | do |
| 746 | { |
Glenn Randers-Pehrson | 895a9c9 | 2008-07-25 08:51:18 -0500 | [diff] [blame] | 747 | png_uint_32 length = png_read_chunk_header(png_ptr); |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 748 | png_uint_32 chunk_name = png_ptr->chunk_name; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 749 | |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 750 | if (chunk_name == png_IHDR) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 751 | png_handle_IHDR(png_ptr, info_ptr, length); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 752 | |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 753 | else if (chunk_name == png_IEND) |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 754 | png_handle_IEND(png_ptr, info_ptr, length); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 755 | |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 756 | #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 757 | else if (png_chunk_unknown_handling(png_ptr, chunk_name) != |
| 758 | PNG_HANDLE_CHUNK_AS_DEFAULT) |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 759 | { |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 760 | if (chunk_name == png_IDAT) |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 761 | { |
Glenn Randers-Pehrson | 6bc53be | 2006-06-16 07:52:03 -0500 | [diff] [blame] | 762 | if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT)) |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 763 | png_benign_error(png_ptr, "Too many IDATs found"); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 764 | } |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 765 | png_handle_unknown(png_ptr, info_ptr, length); |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 766 | if (chunk_name == png_PLTE) |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 767 | png_ptr->mode |= PNG_HAVE_PLTE; |
| 768 | } |
| 769 | #endif |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 770 | |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 771 | else if (chunk_name == png_IDAT) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 772 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 773 | /* Zero length IDATs are legal after the last IDAT has been |
| 774 | * read, but not after other chunks have been read. |
| 775 | */ |
Glenn Randers-Pehrson | 6bc53be | 2006-06-16 07:52:03 -0500 | [diff] [blame] | 776 | if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT)) |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 777 | png_benign_error(png_ptr, "Too many IDATs found"); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 778 | |
Glenn Randers-Pehrson | 4accabb | 2000-04-14 14:20:47 -0500 | [diff] [blame] | 779 | png_crc_finish(png_ptr, length); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 780 | } |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 781 | else if (chunk_name == png_PLTE) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 782 | png_handle_PLTE(png_ptr, info_ptr, length); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 783 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 784 | #ifdef PNG_READ_bKGD_SUPPORTED |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 785 | else if (chunk_name == png_bKGD) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 786 | png_handle_bKGD(png_ptr, info_ptr, length); |
| 787 | #endif |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 788 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 789 | #ifdef PNG_READ_cHRM_SUPPORTED |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 790 | else if (chunk_name == png_cHRM) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 791 | png_handle_cHRM(png_ptr, info_ptr, length); |
| 792 | #endif |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 793 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 794 | #ifdef PNG_READ_gAMA_SUPPORTED |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 795 | else if (chunk_name == png_gAMA) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 796 | png_handle_gAMA(png_ptr, info_ptr, length); |
| 797 | #endif |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 798 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 799 | #ifdef PNG_READ_hIST_SUPPORTED |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 800 | else if (chunk_name == png_hIST) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 801 | png_handle_hIST(png_ptr, info_ptr, length); |
| 802 | #endif |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 803 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 804 | #ifdef PNG_READ_oFFs_SUPPORTED |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 805 | else if (chunk_name == png_oFFs) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 806 | png_handle_oFFs(png_ptr, info_ptr, length); |
| 807 | #endif |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 808 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 809 | #ifdef PNG_READ_pCAL_SUPPORTED |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 810 | else if (chunk_name == png_pCAL) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 811 | png_handle_pCAL(png_ptr, info_ptr, length); |
| 812 | #endif |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 813 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 814 | #ifdef PNG_READ_sCAL_SUPPORTED |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 815 | else if (chunk_name == png_sCAL) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 816 | png_handle_sCAL(png_ptr, info_ptr, length); |
| 817 | #endif |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 818 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 819 | #ifdef PNG_READ_pHYs_SUPPORTED |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 820 | else if (chunk_name == png_pHYs) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 821 | png_handle_pHYs(png_ptr, info_ptr, length); |
| 822 | #endif |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 823 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 824 | #ifdef PNG_READ_sBIT_SUPPORTED |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 825 | else if (chunk_name == png_sBIT) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 826 | png_handle_sBIT(png_ptr, info_ptr, length); |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 827 | #endif |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 828 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 829 | #ifdef PNG_READ_sRGB_SUPPORTED |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 830 | else if (chunk_name == png_sRGB) |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 831 | png_handle_sRGB(png_ptr, info_ptr, length); |
| 832 | #endif |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 833 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 834 | #ifdef PNG_READ_iCCP_SUPPORTED |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 835 | else if (chunk_name == png_iCCP) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 836 | png_handle_iCCP(png_ptr, info_ptr, length); |
| 837 | #endif |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 838 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 839 | #ifdef PNG_READ_sPLT_SUPPORTED |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 840 | else if (chunk_name == png_sPLT) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 841 | png_handle_sPLT(png_ptr, info_ptr, length); |
| 842 | #endif |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 843 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 844 | #ifdef PNG_READ_tEXt_SUPPORTED |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 845 | else if (chunk_name == png_tEXt) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 846 | png_handle_tEXt(png_ptr, info_ptr, length); |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 847 | #endif |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 848 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 849 | #ifdef PNG_READ_tIME_SUPPORTED |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 850 | else if (chunk_name == png_tIME) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 851 | png_handle_tIME(png_ptr, info_ptr, length); |
| 852 | #endif |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 853 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 854 | #ifdef PNG_READ_tRNS_SUPPORTED |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 855 | else if (chunk_name == png_tRNS) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 856 | png_handle_tRNS(png_ptr, info_ptr, length); |
| 857 | #endif |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 858 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 859 | #ifdef PNG_READ_zTXt_SUPPORTED |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 860 | else if (chunk_name == png_zTXt) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 861 | png_handle_zTXt(png_ptr, info_ptr, length); |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 862 | #endif |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 863 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 864 | #ifdef PNG_READ_iTXt_SUPPORTED |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 865 | else if (chunk_name == png_iTXt) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 866 | png_handle_iTXt(png_ptr, info_ptr, length); |
| 867 | #endif |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 868 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 869 | else |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 870 | png_handle_unknown(png_ptr, info_ptr, length); |
| 871 | } while (!(png_ptr->mode & PNG_HAVE_IEND)); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 872 | } |
Glenn Randers-Pehrson | dbd4014 | 2009-08-31 08:42:02 -0500 | [diff] [blame] | 873 | #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 874 | |
John Bowler | 1d7f56a | 2011-12-21 20:14:23 -0600 | [diff] [blame] | 875 | /* Free all memory used in the read struct */ |
| 876 | static void |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 877 | png_read_destroy(png_structrp png_ptr) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 878 | { |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 879 | png_debug(1, "in png_read_destroy"); |
Glenn Randers-Pehrson | 821b710 | 2010-06-24 16:16:32 -0500 | [diff] [blame] | 880 | |
John Bowler | 07772cb | 2011-10-14 18:19:47 -0500 | [diff] [blame] | 881 | #ifdef PNG_READ_GAMMA_SUPPORTED |
| 882 | png_destroy_gamma_table(png_ptr); |
| 883 | #endif |
| 884 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 885 | png_free(png_ptr, png_ptr->zbuf); |
Glenn Randers-Pehrson | 1b8e567 | 2001-08-25 06:46:06 -0500 | [diff] [blame] | 886 | png_free(png_ptr, png_ptr->big_row_buf); |
Mans Rullgard | 1c42276 | 2011-10-17 16:52:19 -0500 | [diff] [blame] | 887 | png_free(png_ptr, png_ptr->big_prev_row); |
Glenn Randers-Pehrson | b3ff968 | 2008-07-21 08:05:57 -0500 | [diff] [blame] | 888 | png_free(png_ptr, png_ptr->chunkdata); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 889 | |
Glenn Randers-Pehrson | 3cd7cff | 2010-04-16 19:27:08 -0500 | [diff] [blame] | 890 | #ifdef PNG_READ_QUANTIZE_SUPPORTED |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 891 | png_free(png_ptr, png_ptr->palette_lookup); |
Glenn Randers-Pehrson | 3cd7cff | 2010-04-16 19:27:08 -0500 | [diff] [blame] | 892 | png_free(png_ptr, png_ptr->quantize_index); |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 893 | #endif |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 894 | |
Glenn Randers-Pehrson | a77ef62 | 2000-02-18 13:48:52 -0600 | [diff] [blame] | 895 | if (png_ptr->free_me & PNG_FREE_PLTE) |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 896 | png_zfree(png_ptr, png_ptr->palette); |
Glenn Randers-Pehrson | a77ef62 | 2000-02-18 13:48:52 -0600 | [diff] [blame] | 897 | png_ptr->free_me &= ~PNG_FREE_PLTE; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 898 | |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 899 | #if defined(PNG_tRNS_SUPPORTED) || \ |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 900 | defined(PNG_READ_EXPAND_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED) |
Glenn Randers-Pehrson | a77ef62 | 2000-02-18 13:48:52 -0600 | [diff] [blame] | 901 | if (png_ptr->free_me & PNG_FREE_TRNS) |
Glenn Randers-Pehrson | 6abea75 | 2009-08-08 16:52:06 -0500 | [diff] [blame] | 902 | png_free(png_ptr, png_ptr->trans_alpha); |
Glenn Randers-Pehrson | a77ef62 | 2000-02-18 13:48:52 -0600 | [diff] [blame] | 903 | png_ptr->free_me &= ~PNG_FREE_TRNS; |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 904 | #endif |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 905 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 906 | inflateEnd(&png_ptr->zstream); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 907 | |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 908 | #ifdef PNG_PROGRESSIVE_READ_SUPPORTED |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 909 | png_free(png_ptr, png_ptr->save_buffer); |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 910 | #endif |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 911 | |
Glenn Randers-Pehrson | d1e8c86 | 2002-06-20 06:54:34 -0500 | [diff] [blame] | 912 | #ifdef PNG_PROGRESSIVE_READ_SUPPORTED |
| 913 | #ifdef PNG_TEXT_SUPPORTED |
| 914 | png_free(png_ptr, png_ptr->current_text); |
| 915 | #endif /* PNG_TEXT_SUPPORTED */ |
| 916 | #endif /* PNG_PROGRESSIVE_READ_SUPPORTED */ |
| 917 | |
John Bowler | 40b2603 | 2011-12-22 08:09:15 -0600 | [diff] [blame] | 918 | #ifdef PNG_UNKNOWN_CHUNKS_SUPPORTED |
| 919 | png_free(png_ptr, png_ptr->unknown_chunk.data); |
| 920 | #endif |
| 921 | |
| 922 | #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED |
| 923 | png_free(png_ptr, png_ptr->chunk_list); |
| 924 | #endif |
| 925 | |
John Bowler | 1d7f56a | 2011-12-21 20:14:23 -0600 | [diff] [blame] | 926 | /* NOTE: the 'setjmp' buffer may still be allocated and the memory and error |
| 927 | * callbacks are still set at this point. They are required to complete the |
| 928 | * destruction of the png_struct itself. |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 929 | */ |
John Bowler | 1d7f56a | 2011-12-21 20:14:23 -0600 | [diff] [blame] | 930 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 931 | |
John Bowler | 1d7f56a | 2011-12-21 20:14:23 -0600 | [diff] [blame] | 932 | /* Free all memory used by the read */ |
| 933 | void PNGAPI |
| 934 | png_destroy_read_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr, |
| 935 | png_infopp end_info_ptr_ptr) |
| 936 | { |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 937 | png_structrp png_ptr = NULL; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 938 | |
John Bowler | 1d7f56a | 2011-12-21 20:14:23 -0600 | [diff] [blame] | 939 | png_debug(1, "in png_destroy_read_struct"); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 940 | |
John Bowler | 1d7f56a | 2011-12-21 20:14:23 -0600 | [diff] [blame] | 941 | if (png_ptr_ptr != NULL) |
| 942 | png_ptr = *png_ptr_ptr; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 943 | |
John Bowler | 1d7f56a | 2011-12-21 20:14:23 -0600 | [diff] [blame] | 944 | if (png_ptr == NULL) |
| 945 | return; |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 946 | |
John Bowler | 1d7f56a | 2011-12-21 20:14:23 -0600 | [diff] [blame] | 947 | /* libpng 1.6.0: use the API to destroy info structs to ensure consistent |
| 948 | * behavior. Prior to 1.6.0 libpng did extra 'info' destruction in this API. |
| 949 | * The extra was, apparently, unnecessary yet this hides memory leak bugs. |
| 950 | */ |
| 951 | png_destroy_info_struct(png_ptr, end_info_ptr_ptr); |
| 952 | png_destroy_info_struct(png_ptr, info_ptr_ptr); |
| 953 | |
| 954 | *png_ptr_ptr = NULL; |
| 955 | png_read_destroy(png_ptr); |
| 956 | png_destroy_png_struct(png_ptr); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 957 | } |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 958 | |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 959 | void PNGAPI |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 960 | png_set_read_status_fn(png_structrp png_ptr, png_read_status_ptr read_row_fn) |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 961 | { |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 962 | if (png_ptr == NULL) |
| 963 | return; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 964 | |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 965 | png_ptr->read_row_fn = read_row_fn; |
| 966 | } |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 967 | |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 968 | |
Glenn Randers-Pehrson | dbd4014 | 2009-08-31 08:42:02 -0500 | [diff] [blame] | 969 | #ifdef PNG_SEQUENTIAL_READ_SUPPORTED |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 970 | #ifdef PNG_INFO_IMAGE_SUPPORTED |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 971 | void PNGAPI |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 972 | png_read_png(png_structrp png_ptr, png_inforp info_ptr, |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 973 | int transforms, |
| 974 | voidp params) |
| 975 | { |
| 976 | int row; |
| 977 | |
John Bowler | 6a6d79f | 2011-02-12 08:56:40 -0600 | [diff] [blame] | 978 | if (png_ptr == NULL || info_ptr == NULL) |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 979 | return; |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 980 | |
Glenn Randers-Pehrson | 272489d | 2004-08-04 06:34:52 -0500 | [diff] [blame] | 981 | /* png_read_info() gives us all of the information from the |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 982 | * PNG file before the first IDAT (image data chunk). |
| 983 | */ |
| 984 | png_read_info(png_ptr, info_ptr); |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 985 | if (info_ptr->height > PNG_UINT_32_MAX/png_sizeof(png_bytep)) |
| 986 | png_error(png_ptr, "Image is too high to process with png_read_png()"); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 987 | |
| 988 | /* -------------- image transformations start here ------------------- */ |
| 989 | |
Glenn Randers-Pehrson | aee83b4 | 2011-06-18 00:19:54 -0500 | [diff] [blame] | 990 | #ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED |
Glenn Randers-Pehrson | 55fbff3 | 2011-05-17 06:49:32 -0500 | [diff] [blame] | 991 | /* Tell libpng to strip 16-bit/color files down to 8 bits per color. |
Glenn Randers-Pehrson | 272489d | 2004-08-04 06:34:52 -0500 | [diff] [blame] | 992 | */ |
Glenn Randers-Pehrson | ab63dd0 | 2011-06-17 20:04:17 -0500 | [diff] [blame] | 993 | if (transforms & PNG_TRANSFORM_SCALE_16) |
Glenn Randers-Pehrson | aee83b4 | 2011-06-18 00:19:54 -0500 | [diff] [blame] | 994 | { |
Glenn Randers-Pehrson | 6da2f2d | 2011-06-17 23:07:16 -0500 | [diff] [blame] | 995 | /* Added at libpng-1.5.4. "strip_16" produces the same result that it |
| 996 | * did in earlier versions, while "scale_16" is now more accurate. |
| 997 | */ |
Glenn Randers-Pehrson | aee83b4 | 2011-06-18 00:19:54 -0500 | [diff] [blame] | 998 | png_set_scale_16(png_ptr); |
| 999 | } |
Glenn Randers-Pehrson | aee83b4 | 2011-06-18 00:19:54 -0500 | [diff] [blame] | 1000 | #endif |
| 1001 | |
| 1002 | #ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED |
John Bowler | 8d26126 | 2011-06-18 13:37:11 -0500 | [diff] [blame] | 1003 | /* If both SCALE and STRIP are required pngrtran will effectively cancel the |
| 1004 | * latter by doing SCALE first. This is ok and allows apps not to check for |
| 1005 | * which is supported to get the right answer. |
| 1006 | */ |
| 1007 | if (transforms & PNG_TRANSFORM_STRIP_16) |
| 1008 | png_set_strip_16(png_ptr); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1009 | #endif |
| 1010 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 1011 | #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED |
Glenn Randers-Pehrson | 272489d | 2004-08-04 06:34:52 -0500 | [diff] [blame] | 1012 | /* Strip alpha bytes from the input data without combining with |
| 1013 | * the background (not recommended). |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1014 | */ |
| 1015 | if (transforms & PNG_TRANSFORM_STRIP_ALPHA) |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 1016 | png_set_strip_alpha(png_ptr); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1017 | #endif |
| 1018 | |
Glenn Randers-Pehrson | a77ef62 | 2000-02-18 13:48:52 -0600 | [diff] [blame] | 1019 | #if defined(PNG_READ_PACK_SUPPORTED) && !defined(PNG_READ_EXPAND_SUPPORTED) |
Glenn Randers-Pehrson | 272489d | 2004-08-04 06:34:52 -0500 | [diff] [blame] | 1020 | /* Extract multiple pixels with bit depths of 1, 2, or 4 from a single |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1021 | * byte into separate bytes (useful for paletted and grayscale images). |
| 1022 | */ |
| 1023 | if (transforms & PNG_TRANSFORM_PACKING) |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 1024 | png_set_packing(png_ptr); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1025 | #endif |
| 1026 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 1027 | #ifdef PNG_READ_PACKSWAP_SUPPORTED |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1028 | /* Change the order of packed pixels to least significant bit first |
Glenn Randers-Pehrson | 272489d | 2004-08-04 06:34:52 -0500 | [diff] [blame] | 1029 | * (not useful if you are using png_set_packing). |
| 1030 | */ |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1031 | if (transforms & PNG_TRANSFORM_PACKSWAP) |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 1032 | png_set_packswap(png_ptr); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1033 | #endif |
| 1034 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 1035 | #ifdef PNG_READ_EXPAND_SUPPORTED |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1036 | /* Expand paletted colors into true RGB triplets |
| 1037 | * Expand grayscale images to full 8 bits from 1, 2, or 4 bits/pixel |
| 1038 | * Expand paletted or RGB images with transparency to full alpha |
| 1039 | * channels so the data will be available as RGBA quartets. |
| 1040 | */ |
| 1041 | if (transforms & PNG_TRANSFORM_EXPAND) |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 1042 | if ((png_ptr->bit_depth < 8) || |
| 1043 | (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) || |
| 1044 | (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))) |
Glenn Randers-Pehrson | a77ef62 | 2000-02-18 13:48:52 -0600 | [diff] [blame] | 1045 | png_set_expand(png_ptr); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1046 | #endif |
| 1047 | |
Glenn Randers-Pehrson | 3cd7cff | 2010-04-16 19:27:08 -0500 | [diff] [blame] | 1048 | /* We don't handle background color or gamma transformation or quantizing. |
Glenn Randers-Pehrson | 272489d | 2004-08-04 06:34:52 -0500 | [diff] [blame] | 1049 | */ |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1050 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 1051 | #ifdef PNG_READ_INVERT_SUPPORTED |
Glenn Randers-Pehrson | d8eb62f | 2009-05-30 20:19:20 -0500 | [diff] [blame] | 1052 | /* Invert monochrome files to have 0 as white and 1 as black |
Glenn Randers-Pehrson | 272489d | 2004-08-04 06:34:52 -0500 | [diff] [blame] | 1053 | */ |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1054 | if (transforms & PNG_TRANSFORM_INVERT_MONO) |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 1055 | png_set_invert_mono(png_ptr); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1056 | #endif |
| 1057 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 1058 | #ifdef PNG_READ_SHIFT_SUPPORTED |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1059 | /* If you want to shift the pixel values from the range [0,255] or |
| 1060 | * [0,65535] to the original [0,7] or [0,31], or whatever range the |
| 1061 | * colors were originally in: |
| 1062 | */ |
| 1063 | if ((transforms & PNG_TRANSFORM_SHIFT) |
| 1064 | && png_get_valid(png_ptr, info_ptr, PNG_INFO_sBIT)) |
| 1065 | { |
| 1066 | png_color_8p sig_bit; |
| 1067 | |
| 1068 | png_get_sBIT(png_ptr, info_ptr, &sig_bit); |
| 1069 | png_set_shift(png_ptr, sig_bit); |
| 1070 | } |
| 1071 | #endif |
| 1072 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 1073 | #ifdef PNG_READ_BGR_SUPPORTED |
Glenn Randers-Pehrson | 262d0ff | 2010-03-03 07:06:54 -0600 | [diff] [blame] | 1074 | /* Flip the RGB pixels to BGR (or RGBA to BGRA) */ |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1075 | if (transforms & PNG_TRANSFORM_BGR) |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 1076 | png_set_bgr(png_ptr); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1077 | #endif |
| 1078 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 1079 | #ifdef PNG_READ_SWAP_ALPHA_SUPPORTED |
Glenn Randers-Pehrson | 262d0ff | 2010-03-03 07:06:54 -0600 | [diff] [blame] | 1080 | /* Swap the RGBA or GA data to ARGB or AG (or BGRA to ABGR) */ |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1081 | if (transforms & PNG_TRANSFORM_SWAP_ALPHA) |
Glenn Randers-Pehrson | 262d0ff | 2010-03-03 07:06:54 -0600 | [diff] [blame] | 1082 | png_set_swap_alpha(png_ptr); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1083 | #endif |
| 1084 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 1085 | #ifdef PNG_READ_SWAP_SUPPORTED |
Glenn Randers-Pehrson | 55fbff3 | 2011-05-17 06:49:32 -0500 | [diff] [blame] | 1086 | /* Swap bytes of 16-bit files to least significant byte first */ |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1087 | if (transforms & PNG_TRANSFORM_SWAP_ENDIAN) |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 1088 | png_set_swap(png_ptr); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1089 | #endif |
| 1090 | |
Glenn Randers-Pehrson | c1a4d64 | 2009-10-29 23:29:24 -0500 | [diff] [blame] | 1091 | /* Added at libpng-1.2.41 */ |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 1092 | #ifdef PNG_READ_INVERT_ALPHA_SUPPORTED |
Glenn Randers-Pehrson | 262d0ff | 2010-03-03 07:06:54 -0600 | [diff] [blame] | 1093 | /* Invert the alpha channel from opacity to transparency */ |
Glenn Randers-Pehrson | 6878eb6 | 2009-06-29 16:45:53 -0500 | [diff] [blame] | 1094 | if (transforms & PNG_TRANSFORM_INVERT_ALPHA) |
Glenn Randers-Pehrson | 262d0ff | 2010-03-03 07:06:54 -0600 | [diff] [blame] | 1095 | png_set_invert_alpha(png_ptr); |
Glenn Randers-Pehrson | 6878eb6 | 2009-06-29 16:45:53 -0500 | [diff] [blame] | 1096 | #endif |
| 1097 | |
Glenn Randers-Pehrson | c1a4d64 | 2009-10-29 23:29:24 -0500 | [diff] [blame] | 1098 | /* Added at libpng-1.2.41 */ |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 1099 | #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED |
Glenn Randers-Pehrson | 262d0ff | 2010-03-03 07:06:54 -0600 | [diff] [blame] | 1100 | /* Expand grayscale image to RGB */ |
Glenn Randers-Pehrson | 99708d5 | 2009-06-29 17:30:00 -0500 | [diff] [blame] | 1101 | if (transforms & PNG_TRANSFORM_GRAY_TO_RGB) |
Glenn Randers-Pehrson | 262d0ff | 2010-03-03 07:06:54 -0600 | [diff] [blame] | 1102 | png_set_gray_to_rgb(png_ptr); |
Glenn Randers-Pehrson | 99708d5 | 2009-06-29 17:30:00 -0500 | [diff] [blame] | 1103 | #endif |
| 1104 | |
Glenn Randers-Pehrson | ef217b7 | 2011-06-15 12:58:27 -0500 | [diff] [blame] | 1105 | /* Added at libpng-1.5.4 */ |
John Bowler | 96cec0e | 2011-05-08 22:48:12 -0500 | [diff] [blame] | 1106 | #ifdef PNG_READ_EXPAND_16_SUPPORTED |
| 1107 | if (transforms & PNG_TRANSFORM_EXPAND_16) |
| 1108 | png_set_expand_16(png_ptr); |
| 1109 | #endif |
| 1110 | |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1111 | /* We don't handle adding filler bytes */ |
| 1112 | |
John Bowler | 6a6d79f | 2011-02-12 08:56:40 -0600 | [diff] [blame] | 1113 | /* We use png_read_image and rely on that for interlace handling, but we also |
| 1114 | * call png_read_update_info therefore must turn on interlace handling now: |
| 1115 | */ |
| 1116 | (void)png_set_interlace_handling(png_ptr); |
| 1117 | |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1118 | /* Optional call to gamma correct and add the background to the palette |
| 1119 | * and update info structure. REQUIRED if you are expecting libpng to |
Glenn Randers-Pehrson | 3097f61 | 2001-05-07 14:52:45 -0500 | [diff] [blame] | 1120 | * update the palette for you (i.e., you selected such a transform above). |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1121 | */ |
| 1122 | png_read_update_info(png_ptr, info_ptr); |
| 1123 | |
| 1124 | /* -------------- image transformations end here ------------------- */ |
| 1125 | |
Glenn Randers-Pehrson | 1ef65b6 | 2000-05-12 06:19:53 -0500 | [diff] [blame] | 1126 | png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0); |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1127 | if (info_ptr->row_pointers == NULL) |
Glenn Randers-Pehrson | a77ef62 | 2000-02-18 13:48:52 -0600 | [diff] [blame] | 1128 | { |
Glenn Randers-Pehrson | 262d0ff | 2010-03-03 07:06:54 -0600 | [diff] [blame] | 1129 | png_uint_32 iptr; |
Glenn Randers-Pehrson | d9f21ee | 2009-08-31 10:52:38 -0500 | [diff] [blame] | 1130 | |
Glenn Randers-Pehrson | 0ffb71a | 2009-02-28 06:08:20 -0600 | [diff] [blame] | 1131 | info_ptr->row_pointers = (png_bytepp)png_malloc(png_ptr, |
Glenn Randers-Pehrson | 262d0ff | 2010-03-03 07:06:54 -0600 | [diff] [blame] | 1132 | info_ptr->height * png_sizeof(png_bytep)); |
Glenn Randers-Pehrson | d9f21ee | 2009-08-31 10:52:38 -0500 | [diff] [blame] | 1133 | for (iptr=0; iptr<info_ptr->height; iptr++) |
| 1134 | info_ptr->row_pointers[iptr] = NULL; |
| 1135 | |
Glenn Randers-Pehrson | a77ef62 | 2000-02-18 13:48:52 -0600 | [diff] [blame] | 1136 | info_ptr->free_me |= PNG_FREE_ROWS; |
Glenn Randers-Pehrson | d9f21ee | 2009-08-31 10:52:38 -0500 | [diff] [blame] | 1137 | |
Glenn Randers-Pehrson | 6942d53 | 2000-05-01 09:31:54 -0500 | [diff] [blame] | 1138 | for (row = 0; row < (int)info_ptr->height; row++) |
Glenn Randers-Pehrson | 3097f61 | 2001-05-07 14:52:45 -0500 | [diff] [blame] | 1139 | info_ptr->row_pointers[row] = (png_bytep)png_malloc(png_ptr, |
Glenn Randers-Pehrson | 6942d53 | 2000-05-01 09:31:54 -0500 | [diff] [blame] | 1140 | png_get_rowbytes(png_ptr, info_ptr)); |
Glenn Randers-Pehrson | a77ef62 | 2000-02-18 13:48:52 -0600 | [diff] [blame] | 1141 | } |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1142 | |
| 1143 | png_read_image(png_ptr, info_ptr->row_pointers); |
| 1144 | info_ptr->valid |= PNG_INFO_IDAT; |
| 1145 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 1146 | /* Read rest of file, and get additional chunks in info_ptr - REQUIRED */ |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1147 | png_read_end(png_ptr, info_ptr); |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 1148 | |
Glenn Randers-Pehrson | d546f43 | 2010-12-04 20:41:36 -0600 | [diff] [blame] | 1149 | PNG_UNUSED(transforms) /* Quiet compiler warnings */ |
| 1150 | PNG_UNUSED(params) |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 1151 | |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1152 | } |
Glenn Randers-Pehrson | 9c3ab68 | 2006-02-20 22:09:05 -0600 | [diff] [blame] | 1153 | #endif /* PNG_INFO_IMAGE_SUPPORTED */ |
Glenn Randers-Pehrson | dbd4014 | 2009-08-31 08:42:02 -0500 | [diff] [blame] | 1154 | #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */ |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1155 | |
| 1156 | #ifdef PNG_SIMPLIFIED_READ_SUPPORTED |
| 1157 | /* SIMPLIFIED READ |
| 1158 | * |
| 1159 | * This code currently relies on the sequential reader, though it could easily |
| 1160 | * be made to work with the progressive one. |
| 1161 | */ |
| 1162 | /* Do all the *safe* initialization - 'safe' means that png_error won't be |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 1163 | * called, so setting up the jmp_buf is not required. This means that anything |
| 1164 | * called from here must *not* call png_malloc - it has to call png_malloc_warn |
| 1165 | * instead so that control is returned safely back to this routine. |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1166 | */ |
| 1167 | static int |
| 1168 | png_image_read_init(png_imagep image) |
| 1169 | { |
| 1170 | png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, image, |
| 1171 | png_safe_error, png_safe_warning); |
| 1172 | |
| 1173 | if (png_ptr != NULL) |
| 1174 | { |
| 1175 | png_infop info_ptr = png_create_info_struct(png_ptr); |
| 1176 | |
| 1177 | if (info_ptr != NULL) |
| 1178 | { |
John Bowler | 4fa96a4 | 2011-11-16 16:39:16 -0600 | [diff] [blame] | 1179 | png_controlp control = png_voidcast(png_controlp, |
| 1180 | png_malloc_warn(png_ptr, sizeof *control)); |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1181 | |
| 1182 | if (control != NULL) |
| 1183 | { |
John Bowler | baeb6d1 | 2011-11-26 18:21:02 -0600 | [diff] [blame] | 1184 | png_memset(control, 0, sizeof *control); |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1185 | |
| 1186 | control->png_ptr = png_ptr; |
| 1187 | control->info_ptr = info_ptr; |
| 1188 | control->for_write = 0; |
| 1189 | |
| 1190 | image->opaque = control; |
| 1191 | return 1; |
| 1192 | } |
| 1193 | |
| 1194 | /* Error clean up */ |
| 1195 | png_destroy_info_struct(png_ptr, &info_ptr); |
| 1196 | } |
| 1197 | |
| 1198 | png_destroy_read_struct(&png_ptr, NULL, NULL); |
| 1199 | } |
| 1200 | |
| 1201 | return png_image_error(image, "png_image_read: out of memory"); |
| 1202 | } |
| 1203 | |
| 1204 | /* Utility to find the base format of a PNG file from a png_struct. */ |
| 1205 | static png_uint_32 |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 1206 | png_image_format(png_structrp png_ptr, png_inforp info_ptr) |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1207 | { |
| 1208 | png_uint_32 format = 0; |
| 1209 | |
| 1210 | if (png_ptr->color_type & PNG_COLOR_MASK_COLOR) |
| 1211 | format |= PNG_FORMAT_FLAG_COLOR; |
| 1212 | |
| 1213 | if (png_ptr->color_type & PNG_COLOR_MASK_ALPHA) |
| 1214 | format |= PNG_FORMAT_FLAG_ALPHA; |
| 1215 | |
| 1216 | else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS)) |
| 1217 | format |= PNG_FORMAT_FLAG_ALPHA; |
| 1218 | |
| 1219 | if (png_ptr->bit_depth == 16) |
| 1220 | format |= PNG_FORMAT_FLAG_LINEAR; |
| 1221 | |
| 1222 | return format; |
| 1223 | } |
| 1224 | |
| 1225 | /* Do the main body of a 'png_image_begin_read' function; read the PNG file |
| 1226 | * header and fill in all the information. This is executed in a safe context, |
| 1227 | * unlike the init routine above. |
| 1228 | */ |
| 1229 | static int |
| 1230 | png_image_read_header(png_voidp argument) |
| 1231 | { |
John Bowler | 4fa96a4 | 2011-11-16 16:39:16 -0600 | [diff] [blame] | 1232 | png_imagep image = png_voidcast(png_imagep, argument); |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 1233 | png_structrp png_ptr = image->opaque->png_ptr; |
| 1234 | png_inforp info_ptr = image->opaque->info_ptr; |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1235 | |
| 1236 | png_read_info(png_ptr, info_ptr); |
| 1237 | |
| 1238 | /* Do this the fast way; just read directly out of png_struct. */ |
| 1239 | image->width = png_ptr->width; |
| 1240 | image->height = png_ptr->height; |
| 1241 | |
| 1242 | { |
| 1243 | png_uint_32 format = png_image_format(png_ptr, info_ptr); |
| 1244 | |
| 1245 | image->format = format; |
| 1246 | image->flags = 0; |
| 1247 | |
| 1248 | /* Now try to work out whether the color data does not match sRGB. */ |
| 1249 | if ((format & PNG_FORMAT_FLAG_COLOR) != 0 && |
| 1250 | (info_ptr->valid & PNG_INFO_sRGB) == 0) |
| 1251 | { |
| 1252 | /* gamma is irrelevant because libpng does gamma correction, what |
| 1253 | * matters is if the cHRM chunk doesn't match or, in the absence of |
Glenn Randers-Pehrson | 84b0da9 | 2011-11-10 06:32:19 -0600 | [diff] [blame] | 1254 | * cRHM, if the iCCP profile appears to have different end points. |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1255 | */ |
| 1256 | if (info_ptr->valid & PNG_INFO_cHRM) |
| 1257 | { |
| 1258 | /* TODO: this is a copy'n'paste from pngrutil.c, make a common |
| 1259 | * checking function. This checks for a 1% error. |
| 1260 | */ |
| 1261 | /* The cHRM chunk is used in preference to iCCP */ |
| 1262 | if (PNG_OUT_OF_RANGE(info_ptr->x_white, 31270, 1000) || |
| 1263 | PNG_OUT_OF_RANGE(info_ptr->y_white, 32900, 1000) || |
| 1264 | PNG_OUT_OF_RANGE(info_ptr->x_red, 64000, 1000) || |
| 1265 | PNG_OUT_OF_RANGE(info_ptr->y_red, 33000, 1000) || |
| 1266 | PNG_OUT_OF_RANGE(info_ptr->x_green, 30000, 1000) || |
| 1267 | PNG_OUT_OF_RANGE(info_ptr->y_green, 60000, 1000) || |
| 1268 | PNG_OUT_OF_RANGE(info_ptr->x_blue, 15000, 1000) || |
| 1269 | PNG_OUT_OF_RANGE(info_ptr->y_blue, 6000, 1000)) |
| 1270 | image->flags |= PNG_IMAGE_FLAG_COLORSPACE_NOT_sRGB; |
| 1271 | } |
| 1272 | |
| 1273 | else if (info_ptr->valid & PNG_INFO_iCCP) |
| 1274 | { |
Glenn Randers-Pehrson | 6d8e090 | 2011-11-11 18:41:59 -0600 | [diff] [blame] | 1275 | # if 0 |
| 1276 | /* TODO: IMPLEMENT THIS! Remember to remove iCCP from |
| 1277 | the chunks_to_ignore list */ |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1278 | /* Here if we just have an iCCP chunk. */ |
| 1279 | if (!png_iCCP_is_sRGB(png_ptr, info_ptr)) |
| 1280 | # endif |
| 1281 | image->flags |= PNG_IMAGE_FLAG_COLORSPACE_NOT_sRGB; |
| 1282 | } |
| 1283 | } |
| 1284 | } |
| 1285 | |
| 1286 | return 1; |
| 1287 | } |
| 1288 | |
| 1289 | #ifdef PNG_STDIO_SUPPORTED |
| 1290 | int PNGAPI |
| 1291 | png_image_begin_read_from_stdio(png_imagep image, FILE* file) |
| 1292 | { |
| 1293 | if (image != NULL) |
| 1294 | { |
| 1295 | if (file != NULL) |
| 1296 | { |
| 1297 | if (png_image_read_init(image)) |
| 1298 | { |
| 1299 | /* This is slightly evil, but png_init_io doesn't do anything other |
| 1300 | * than this and we haven't changed the standard IO functions so |
| 1301 | * this saves a 'safe' function. |
| 1302 | */ |
| 1303 | image->opaque->png_ptr->io_ptr = file; |
| 1304 | return png_safe_execute(image, png_image_read_header, image); |
| 1305 | } |
| 1306 | } |
| 1307 | |
| 1308 | else |
| 1309 | return png_image_error(image, |
| 1310 | "png_image_begin_read_from_stdio: invalid argument"); |
| 1311 | } |
| 1312 | |
| 1313 | return 0; |
| 1314 | } |
| 1315 | |
| 1316 | int PNGAPI |
| 1317 | png_image_begin_read_from_file(png_imagep image, const char *file_name) |
| 1318 | { |
| 1319 | if (image != NULL) |
| 1320 | { |
| 1321 | if (file_name != NULL) |
| 1322 | { |
| 1323 | FILE *fp = fopen(file_name, "rb"); |
| 1324 | |
| 1325 | if (fp != NULL) |
| 1326 | { |
| 1327 | if (png_image_read_init(image)) |
| 1328 | { |
| 1329 | image->opaque->png_ptr->io_ptr = fp; |
| 1330 | image->opaque->owned_file = 1; |
| 1331 | return png_safe_execute(image, png_image_read_header, image); |
| 1332 | } |
| 1333 | |
| 1334 | /* Clean up: just the opened file. */ |
| 1335 | (void)fclose(fp); |
| 1336 | } |
| 1337 | |
| 1338 | else |
| 1339 | return png_image_error(image, strerror(errno)); |
| 1340 | } |
| 1341 | |
| 1342 | else |
| 1343 | return png_image_error(image, |
| 1344 | "png_image_begin_read_from_file: invalid argument"); |
| 1345 | } |
| 1346 | |
| 1347 | return 0; |
| 1348 | } |
| 1349 | #endif /* PNG_STDIO_SUPPORTED */ |
| 1350 | |
| 1351 | static void PNGCBAPI |
| 1352 | png_image_memory_read(png_structp png_ptr, png_bytep out, png_size_t need) |
| 1353 | { |
| 1354 | if (png_ptr != NULL) |
| 1355 | { |
John Bowler | 4fa96a4 | 2011-11-16 16:39:16 -0600 | [diff] [blame] | 1356 | png_imagep image = png_voidcast(png_imagep, png_ptr->io_ptr); |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1357 | if (image != NULL) |
| 1358 | { |
| 1359 | png_controlp cp = image->opaque; |
| 1360 | if (cp != NULL) |
| 1361 | { |
| 1362 | png_const_bytep memory = cp->memory; |
| 1363 | png_size_t size = cp->size; |
| 1364 | |
| 1365 | if (memory != NULL && size >= need) |
| 1366 | { |
John Bowler | baeb6d1 | 2011-11-26 18:21:02 -0600 | [diff] [blame] | 1367 | png_memcpy(out, memory, need); |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1368 | cp->memory = memory + need; |
| 1369 | cp->size = size - need; |
| 1370 | return; |
| 1371 | } |
| 1372 | |
| 1373 | png_error(png_ptr, "read beyond end of data"); |
| 1374 | } |
| 1375 | } |
| 1376 | |
| 1377 | png_error(png_ptr, "invalid memory read"); |
| 1378 | } |
| 1379 | } |
| 1380 | |
| 1381 | int PNGAPI png_image_begin_read_from_memory(png_imagep image, |
| 1382 | png_const_voidp memory, png_size_t size) |
| 1383 | { |
| 1384 | if (image != NULL) |
| 1385 | { |
| 1386 | if (memory != NULL && size > 0) |
| 1387 | { |
| 1388 | if (png_image_read_init(image)) |
| 1389 | { |
| 1390 | /* Now set the IO functions to read from the memory buffer and |
| 1391 | * store it into io_ptr. Again do this in-place to avoid calling a |
| 1392 | * libpng function that requires error handling. |
| 1393 | */ |
John Bowler | 4fa96a4 | 2011-11-16 16:39:16 -0600 | [diff] [blame] | 1394 | image->opaque->memory = png_voidcast(png_const_bytep, memory); |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1395 | image->opaque->size = size; |
| 1396 | image->opaque->png_ptr->io_ptr = image; |
| 1397 | image->opaque->png_ptr->read_data_fn = png_image_memory_read; |
| 1398 | |
| 1399 | return png_safe_execute(image, png_image_read_header, image); |
| 1400 | } |
| 1401 | } |
| 1402 | |
| 1403 | else |
| 1404 | return png_image_error(image, |
| 1405 | "png_image_begin_read_from_memory: invalid argument"); |
| 1406 | } |
| 1407 | |
| 1408 | return 0; |
| 1409 | } |
| 1410 | |
| 1411 | /* Arguments to png_image_finish_read: */ |
| 1412 | typedef struct |
| 1413 | { |
| 1414 | /* Arguments: */ |
| 1415 | png_imagep image; |
| 1416 | png_voidp buffer; |
| 1417 | png_int_32 row_stride; |
| 1418 | png_colorp background; |
| 1419 | /* Local variables: */ |
| 1420 | png_bytep local_row; |
| 1421 | png_bytep first_row; |
| 1422 | ptrdiff_t row_bytes; /* unsigned arithmetic step between rows */ |
| 1423 | } png_image_read_control; |
| 1424 | |
| 1425 | /* Just the row reading part of png_image_read. */ |
| 1426 | static int |
| 1427 | png_image_read_composite(png_voidp argument) |
| 1428 | { |
John Bowler | 4fa96a4 | 2011-11-16 16:39:16 -0600 | [diff] [blame] | 1429 | png_image_read_control *display = png_voidcast(png_image_read_control*, |
| 1430 | argument); |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1431 | png_imagep image = display->image; |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 1432 | png_structrp png_ptr = image->opaque->png_ptr; |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1433 | png_byte interlace_type = png_ptr->interlaced; |
John Bowler | 4f67e40 | 2011-12-28 08:43:37 -0600 | [diff] [blame] | 1434 | int passes; |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1435 | |
| 1436 | switch (interlace_type) |
| 1437 | { |
| 1438 | case PNG_INTERLACE_NONE: |
| 1439 | passes = 1; |
| 1440 | break; |
| 1441 | |
| 1442 | case PNG_INTERLACE_ADAM7: |
| 1443 | passes = PNG_INTERLACE_ADAM7_PASSES; |
| 1444 | break; |
| 1445 | |
| 1446 | default: |
John Bowler | 4f67e40 | 2011-12-28 08:43:37 -0600 | [diff] [blame] | 1447 | passes = 0; |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1448 | png_error(png_ptr, "unknown interlace type"); |
| 1449 | } |
| 1450 | |
| 1451 | { |
| 1452 | png_uint_32 height = image->height; |
| 1453 | png_uint_32 width = image->width; |
| 1454 | unsigned int channels = (image->format & PNG_FORMAT_FLAG_COLOR) ? 3 : 1; |
| 1455 | int pass; |
| 1456 | |
| 1457 | for (pass = 0; pass < passes; ++pass) |
| 1458 | { |
| 1459 | png_bytep row = display->first_row; |
| 1460 | unsigned int startx, stepx, stepy; |
| 1461 | png_uint_32 y; |
| 1462 | |
| 1463 | if (interlace_type == PNG_INTERLACE_ADAM7) |
| 1464 | { |
| 1465 | /* The row may be empty for a short image: */ |
| 1466 | if (PNG_PASS_COLS(width, pass) == 0) |
| 1467 | continue; |
| 1468 | |
| 1469 | startx = PNG_PASS_START_COL(pass); |
| 1470 | stepx = PNG_PASS_COL_OFFSET(pass); |
| 1471 | y = PNG_PASS_START_ROW(pass); |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 1472 | stepy = PNG_PASS_ROW_OFFSET(pass); |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1473 | } |
| 1474 | |
| 1475 | else |
| 1476 | { |
| 1477 | y = 0; |
| 1478 | startx = 0; |
| 1479 | stepx = stepy = 1; |
| 1480 | } |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 1481 | |
| 1482 | /* The following are invariants across all the rows: */ |
| 1483 | startx *= channels; |
| 1484 | stepx *= channels; |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1485 | |
| 1486 | for (; y<height; y += stepy) |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1487 | { |
| 1488 | png_bytep inrow = display->local_row; |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 1489 | png_bytep outrow = row + startx; |
| 1490 | png_const_bytep end_row = row + width * channels; |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1491 | |
| 1492 | /* Read the row, which is packed: */ |
| 1493 | png_read_row(png_ptr, inrow, NULL); |
| 1494 | |
| 1495 | /* Now do the composition on each pixel in this row. */ |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 1496 | for (; outrow < end_row; outrow += stepx) |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1497 | { |
| 1498 | png_byte alpha = inrow[channels]; |
| 1499 | |
| 1500 | if (alpha > 0) /* else no change to the output */ |
| 1501 | { |
| 1502 | unsigned int c; |
| 1503 | |
| 1504 | for (c=0; c<channels; ++c) |
| 1505 | { |
| 1506 | png_uint_32 component = inrow[c]; |
| 1507 | |
| 1508 | if (alpha < 255) /* else just use component */ |
| 1509 | { |
| 1510 | /* This is PNG_OPTIMIZED_ALPHA, the component value |
| 1511 | * is a linear 8-bit value. Combine this with the |
| 1512 | * current outrow[c] value which is sRGB encoded. |
| 1513 | * Arithmetic here is 16-bits to preserve the output |
| 1514 | * values correctly. |
| 1515 | */ |
| 1516 | component *= 257*255; /* =65535 */ |
| 1517 | component += (255-alpha)*png_sRGB_table[outrow[c]]; |
| 1518 | |
| 1519 | /* So 'component' is scaled by 255*65535 and is |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 1520 | * therefore appropriate for the sRGB to linear |
Glenn Randers-Pehrson | 7455cbf | 2011-11-24 14:40:36 -0600 | [diff] [blame] | 1521 | * conversion table. |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1522 | */ |
| 1523 | component = PNG_sRGB_FROM_LINEAR(component); |
| 1524 | } |
| 1525 | |
| 1526 | outrow[c] = (png_byte)component; |
| 1527 | } |
| 1528 | } |
| 1529 | |
| 1530 | inrow += channels+1; /* components and alpha channel */ |
| 1531 | } |
| 1532 | |
| 1533 | row += display->row_bytes; |
| 1534 | } |
| 1535 | } |
| 1536 | } |
| 1537 | |
| 1538 | return 1; |
| 1539 | } |
| 1540 | |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 1541 | /* The do_local_background case; called when all the following transforms are to |
| 1542 | * be done: |
| 1543 | * |
| 1544 | * PNG_RGB_TO_GRAY |
| 1545 | * PNG_COMPOSITE |
| 1546 | * PNG_GAMMA |
| 1547 | * |
| 1548 | * This is a work-round for the fact that both the PNG_RGB_TO_GRAY and |
| 1549 | * PNG_COMPOSITE code performs gamma correction, so we get double gamma |
| 1550 | * correction. The fix-up is to prevent the PNG_COMPOSITE operation happening |
| 1551 | * inside libpng, so this routine sees an 8 or 16-bit gray+alpha row and handles |
| 1552 | * the removal or pre-multiplication of the alpha channel. |
| 1553 | */ |
| 1554 | static int |
| 1555 | png_image_read_background(png_voidp argument) |
| 1556 | { |
John Bowler | 4fa96a4 | 2011-11-16 16:39:16 -0600 | [diff] [blame] | 1557 | png_image_read_control *display = png_voidcast(png_image_read_control*, |
| 1558 | argument); |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 1559 | png_imagep image = display->image; |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 1560 | png_structrp png_ptr = image->opaque->png_ptr; |
| 1561 | png_inforp info_ptr = image->opaque->info_ptr; |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 1562 | png_byte interlace_type = png_ptr->interlaced; |
| 1563 | png_uint_32 height = image->height; |
| 1564 | png_uint_32 width = image->width; |
John Bowler | 4f67e40 | 2011-12-28 08:43:37 -0600 | [diff] [blame] | 1565 | int pass, passes; |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 1566 | |
| 1567 | /* Double check the convoluted logic below. We expect to get here with |
| 1568 | * libpng doing rgb to gray and gamma correction but background processing |
| 1569 | * left to the png_image_read_background function. The rows libpng produce |
| 1570 | * might be 8 or 16-bit but should always have two channels; gray plus alpha. |
| 1571 | */ |
| 1572 | if ((png_ptr->transformations & PNG_RGB_TO_GRAY) == 0) |
| 1573 | png_error(png_ptr, "lost rgb to gray"); |
| 1574 | |
| 1575 | if ((png_ptr->transformations & PNG_COMPOSE) != 0) |
| 1576 | png_error(png_ptr, "unexpected compose"); |
| 1577 | |
| 1578 | /* The palette code zaps PNG_GAMMA in place... */ |
| 1579 | if ((png_ptr->color_type & PNG_COLOR_MASK_PALETTE) == 0 && |
| 1580 | (png_ptr->transformations & PNG_GAMMA) == 0) |
| 1581 | png_error(png_ptr, "lost gamma correction"); |
| 1582 | |
| 1583 | if (png_get_channels(png_ptr, info_ptr) != 2) |
| 1584 | png_error(png_ptr, "lost/gained channels"); |
| 1585 | |
| 1586 | switch (interlace_type) |
| 1587 | { |
| 1588 | case PNG_INTERLACE_NONE: |
| 1589 | passes = 1; |
| 1590 | break; |
| 1591 | |
| 1592 | case PNG_INTERLACE_ADAM7: |
| 1593 | passes = PNG_INTERLACE_ADAM7_PASSES; |
| 1594 | break; |
| 1595 | |
| 1596 | default: |
John Bowler | 4f67e40 | 2011-12-28 08:43:37 -0600 | [diff] [blame] | 1597 | passes = 0; |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 1598 | png_error(png_ptr, "unknown interlace type"); |
| 1599 | } |
| 1600 | |
| 1601 | switch (png_get_bit_depth(png_ptr, info_ptr)) |
| 1602 | { |
| 1603 | default: |
| 1604 | png_error(png_ptr, "unexpected bit depth"); |
| 1605 | break; |
| 1606 | |
| 1607 | case 8: |
| 1608 | /* 8-bit sRGB gray values with an alpha channel; the alpha channel is |
| 1609 | * to be removed by composing on a backgroundi: either the row if |
| 1610 | * display->background is NULL or display->background.green if not. |
| 1611 | * Unlike the code above ALPHA_OPTIMIZED has *not* been done. |
| 1612 | */ |
| 1613 | for (pass = 0; pass < passes; ++pass) |
| 1614 | { |
| 1615 | png_bytep row = display->first_row; |
| 1616 | unsigned int startx, stepx, stepy; |
| 1617 | png_uint_32 y; |
| 1618 | |
| 1619 | if (interlace_type == PNG_INTERLACE_ADAM7) |
| 1620 | { |
| 1621 | /* The row may be empty for a short image: */ |
| 1622 | if (PNG_PASS_COLS(width, pass) == 0) |
| 1623 | continue; |
| 1624 | |
| 1625 | startx = PNG_PASS_START_COL(pass); |
| 1626 | stepx = PNG_PASS_COL_OFFSET(pass); |
| 1627 | y = PNG_PASS_START_ROW(pass); |
| 1628 | stepy = PNG_PASS_ROW_OFFSET(pass); |
| 1629 | } |
| 1630 | |
| 1631 | else |
| 1632 | { |
| 1633 | y = 0; |
| 1634 | startx = 0; |
| 1635 | stepx = stepy = 1; |
| 1636 | } |
| 1637 | |
| 1638 | if (display->background == NULL) |
| 1639 | { |
| 1640 | for (; y<height; y += stepy) |
| 1641 | { |
| 1642 | png_bytep inrow = display->local_row; |
| 1643 | png_bytep outrow = row + startx; |
| 1644 | png_const_bytep end_row = row + width; |
| 1645 | |
| 1646 | /* Read the row, which is packed: */ |
| 1647 | png_read_row(png_ptr, inrow, NULL); |
| 1648 | |
| 1649 | /* Now do the composition on each pixel in this row. */ |
| 1650 | for (; outrow < end_row; outrow += stepx) |
| 1651 | { |
| 1652 | png_byte alpha = inrow[1]; |
| 1653 | |
| 1654 | if (alpha > 0) /* else no change to the output */ |
| 1655 | { |
| 1656 | png_uint_32 component = inrow[0]; |
| 1657 | |
| 1658 | if (alpha < 255) /* else just use component */ |
| 1659 | { |
| 1660 | /* Since PNG_OPTIMIZED_ALPHA was not set it is |
| 1661 | * necessary to invert the sRGB transfer |
| 1662 | * function and multiply the alpha out. |
| 1663 | */ |
| 1664 | component = png_sRGB_table[component] * alpha; |
| 1665 | component += png_sRGB_table[outrow[0]] * |
| 1666 | (255-alpha); |
| 1667 | component = PNG_sRGB_FROM_LINEAR(component); |
| 1668 | } |
| 1669 | |
| 1670 | outrow[0] = (png_byte)component; |
| 1671 | } |
| 1672 | |
| 1673 | inrow += 2; /* gray and alpha channel */ |
| 1674 | } |
| 1675 | |
| 1676 | row += display->row_bytes; |
| 1677 | } |
| 1678 | } |
| 1679 | |
| 1680 | else /* constant background value */ |
| 1681 | { |
| 1682 | png_byte background8 = display->background->green; |
| 1683 | png_uint_16 background = png_sRGB_table[background8]; |
| 1684 | |
| 1685 | for (; y<height; y += stepy) |
| 1686 | { |
| 1687 | png_bytep inrow = display->local_row; |
| 1688 | png_bytep outrow = row + startx; |
| 1689 | png_const_bytep end_row = row + width; |
| 1690 | |
| 1691 | /* Read the row, which is packed: */ |
| 1692 | png_read_row(png_ptr, inrow, NULL); |
| 1693 | |
| 1694 | /* Now do the composition on each pixel in this row. */ |
| 1695 | for (; outrow < end_row; outrow += stepx) |
| 1696 | { |
| 1697 | png_byte alpha = inrow[1]; |
| 1698 | |
| 1699 | if (alpha > 0) /* else use background */ |
| 1700 | { |
| 1701 | png_uint_32 component = inrow[0]; |
| 1702 | |
| 1703 | if (alpha < 255) /* else just use component */ |
| 1704 | { |
| 1705 | component = png_sRGB_table[component] * alpha; |
| 1706 | component += background * (255-alpha); |
| 1707 | component = PNG_sRGB_FROM_LINEAR(component); |
| 1708 | } |
| 1709 | |
| 1710 | outrow[0] = (png_byte)component; |
| 1711 | } |
| 1712 | |
| 1713 | else |
| 1714 | outrow[0] = background8; |
| 1715 | |
| 1716 | inrow += 2; /* gray and alpha channel */ |
| 1717 | } |
| 1718 | |
| 1719 | row += display->row_bytes; |
| 1720 | } |
| 1721 | } |
| 1722 | } |
| 1723 | break; |
| 1724 | |
| 1725 | case 16: |
| 1726 | /* 16-bit linear with pre-multiplied alpha; the pre-multiplication must |
| 1727 | * still be done and, maybe, the alpha channel removed. This code also |
| 1728 | * handles the alpha-first option. |
| 1729 | */ |
| 1730 | { |
| 1731 | unsigned int outchannels = png_get_channels(png_ptr, info_ptr); |
| 1732 | int preserve_alpha = (image->format & PNG_FORMAT_FLAG_ALPHA) != 0; |
| 1733 | int swap_alpha = 0; |
| 1734 | |
| 1735 | if (preserve_alpha && (image->format & PNG_FORMAT_FLAG_AFIRST)) |
| 1736 | swap_alpha = 1; |
| 1737 | |
| 1738 | for (pass = 0; pass < passes; ++pass) |
| 1739 | { |
| 1740 | png_uint_16p row = (png_uint_16p)display->first_row; |
| 1741 | unsigned int startx, stepx, stepy; /* all in pixels */ |
| 1742 | png_uint_32 y; |
| 1743 | |
| 1744 | if (interlace_type == PNG_INTERLACE_ADAM7) |
| 1745 | { |
| 1746 | /* The row may be empty for a short image: */ |
| 1747 | if (PNG_PASS_COLS(width, pass) == 0) |
| 1748 | continue; |
| 1749 | |
| 1750 | startx = PNG_PASS_START_COL(pass); |
| 1751 | stepx = PNG_PASS_COL_OFFSET(pass); |
| 1752 | y = PNG_PASS_START_ROW(pass); |
| 1753 | stepy = PNG_PASS_ROW_OFFSET(pass); |
| 1754 | } |
| 1755 | |
| 1756 | else |
| 1757 | { |
| 1758 | y = 0; |
| 1759 | startx = 0; |
| 1760 | stepx = stepy = 1; |
| 1761 | } |
| 1762 | |
| 1763 | startx *= outchannels; |
| 1764 | stepx *= outchannels; |
| 1765 | |
| 1766 | for (; y<height; y += stepy) |
| 1767 | { |
| 1768 | png_uint_16p inrow; |
| 1769 | png_uint_16p outrow = row + startx; |
| 1770 | png_uint_16p end_row = row + width * outchannels; |
| 1771 | |
| 1772 | /* Read the row, which is packed: */ |
| 1773 | png_read_row(png_ptr, display->local_row, NULL); |
| 1774 | inrow = (png_uint_16p)display->local_row; |
| 1775 | |
| 1776 | /* Now do the pre-multiplication on each pixel in this row. |
| 1777 | */ |
| 1778 | for (; outrow < end_row; outrow += stepx) |
| 1779 | { |
| 1780 | png_uint_32 component = inrow[0]; |
| 1781 | png_uint_16 alpha = inrow[1]; |
| 1782 | |
| 1783 | if (alpha > 0) /* else 0 */ |
| 1784 | { |
| 1785 | if (alpha < 65535) /* else just use component */ |
| 1786 | { |
| 1787 | component *= alpha; |
| 1788 | component += 32767; |
| 1789 | component /= 65535; |
| 1790 | } |
| 1791 | } |
| 1792 | |
| 1793 | else |
| 1794 | component = 0; |
| 1795 | |
| 1796 | outrow[swap_alpha] = (png_uint_16)component; |
| 1797 | if (outchannels > 1) |
| 1798 | outrow[1 ^ swap_alpha] = alpha; |
| 1799 | |
| 1800 | inrow += 2; /* components and alpha channel */ |
| 1801 | } |
| 1802 | |
| 1803 | row += display->row_bytes; |
| 1804 | } |
| 1805 | } |
| 1806 | } |
| 1807 | break; |
| 1808 | } |
| 1809 | |
| 1810 | return 1; |
| 1811 | } |
| 1812 | |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1813 | /* The guts of png_image_finish_read as a png_safe_execute callback. */ |
| 1814 | static int |
| 1815 | png_image_read_end(png_voidp argument) |
| 1816 | { |
John Bowler | 4fa96a4 | 2011-11-16 16:39:16 -0600 | [diff] [blame] | 1817 | png_image_read_control *display = png_voidcast(png_image_read_control*, |
| 1818 | argument); |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1819 | png_imagep image = display->image; |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 1820 | png_structrp png_ptr = image->opaque->png_ptr; |
| 1821 | png_inforp info_ptr = image->opaque->info_ptr; |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1822 | |
| 1823 | png_uint_32 format = image->format; |
| 1824 | int linear = (format & PNG_FORMAT_FLAG_LINEAR) != 0; |
| 1825 | int do_local_compose = 0; |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 1826 | int do_local_background = 0; /* to avoid double gamma correction bug */ |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1827 | int passes = 0; |
| 1828 | |
| 1829 | /* Add transforms to ensure the correct output format is produced then check |
| 1830 | * that the required implementation support is there. Always expand; always |
| 1831 | * need 8 bits minimum, no palette and expanded tRNS. |
| 1832 | */ |
| 1833 | png_set_expand(png_ptr); |
| 1834 | |
| 1835 | /* Now check the format to see if it was modified. */ |
| 1836 | { |
| 1837 | png_uint_32 base_format = png_image_format(png_ptr, info_ptr); |
| 1838 | png_uint_32 change = format ^ base_format; |
John Bowler | 3615d03 | 2011-11-08 10:38:09 -0600 | [diff] [blame] | 1839 | png_fixed_point output_gamma; |
| 1840 | int mode; /* alpha mode */ |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1841 | |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 1842 | /* Do this first so that we have a record if rgb to gray is happening. */ |
| 1843 | if (change & PNG_FORMAT_FLAG_COLOR) |
| 1844 | { |
| 1845 | /* gray<->color transformation required. */ |
| 1846 | if (format & PNG_FORMAT_FLAG_COLOR) |
| 1847 | png_set_gray_to_rgb(png_ptr); |
| 1848 | |
| 1849 | else |
| 1850 | { |
| 1851 | /* libpng can't do both rgb to gray and |
| 1852 | * background/pre-multiplication if there is also significant gamma |
| 1853 | * correction, because both operations require linear colors and |
| 1854 | * the code only supports one transform doing the gamma correction. |
| 1855 | * Handle this by doing the pre-multiplication or background |
| 1856 | * operation in this code, if necessary. |
| 1857 | * |
| 1858 | * TODO: fix this by rewriting pngrtran.c (!) |
| 1859 | * |
| 1860 | * For the moment (given that fixing this in pngrtran.c is an |
| 1861 | * enormous change) 'do_local_background' is used to indicate that |
| 1862 | * the problem exists. |
| 1863 | */ |
| 1864 | if (base_format & PNG_FORMAT_FLAG_ALPHA) |
| 1865 | do_local_background = 1/*maybe*/; |
| 1866 | |
| 1867 | png_set_rgb_to_gray_fixed(png_ptr, PNG_ERROR_ACTION_NONE, |
| 1868 | PNG_RGB_TO_GRAY_DEFAULT, PNG_RGB_TO_GRAY_DEFAULT); |
| 1869 | } |
| 1870 | |
| 1871 | change &= ~PNG_FORMAT_FLAG_COLOR; |
| 1872 | } |
| 1873 | |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1874 | /* Set the gamma appropriately, linear for 16-bit input, sRGB otherwise. |
| 1875 | */ |
| 1876 | { |
John Bowler | 3615d03 | 2011-11-08 10:38:09 -0600 | [diff] [blame] | 1877 | png_fixed_point input_gamma_default; |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1878 | |
| 1879 | if (base_format & PNG_FORMAT_FLAG_LINEAR) |
| 1880 | input_gamma_default = PNG_GAMMA_LINEAR; |
| 1881 | else |
| 1882 | input_gamma_default = PNG_DEFAULT_sRGB; |
| 1883 | |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 1884 | /* Call png_set_alpha_mode to set the default for the input gamma; the |
| 1885 | * output gamma is set by a second call below. |
| 1886 | */ |
| 1887 | png_set_alpha_mode_fixed(png_ptr, PNG_ALPHA_PNG, input_gamma_default); |
| 1888 | } |
| 1889 | |
| 1890 | if (linear) |
| 1891 | { |
| 1892 | /* If there *is* an alpha channel in the input it must be multiplied |
| 1893 | * out; use PNG_ALPHA_STANDARD, otherwise just use PNG_ALPHA_PNG. |
| 1894 | */ |
| 1895 | if (base_format & PNG_FORMAT_FLAG_ALPHA) |
John Bowler | 3615d03 | 2011-11-08 10:38:09 -0600 | [diff] [blame] | 1896 | mode = PNG_ALPHA_STANDARD; /* associated alpha */ |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1897 | |
| 1898 | else |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1899 | mode = PNG_ALPHA_PNG; |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 1900 | |
| 1901 | output_gamma = PNG_GAMMA_LINEAR; |
| 1902 | } |
| 1903 | |
| 1904 | else |
| 1905 | { |
| 1906 | mode = PNG_ALPHA_PNG; |
| 1907 | output_gamma = PNG_DEFAULT_sRGB; |
| 1908 | } |
| 1909 | |
| 1910 | /* If 'do_local_background' is set check for the presence of gamma |
| 1911 | * correction; this is part of the work-round for the libpng bug |
| 1912 | * described above. |
| 1913 | * |
| 1914 | * TODO: fix libpng and remove this. |
| 1915 | */ |
| 1916 | if (do_local_background) |
| 1917 | { |
| 1918 | png_fixed_point gtest; |
| 1919 | |
| 1920 | /* This is 'png_gamma_threshold' from pngrtran.c; the test used for |
| 1921 | * gamma correction, the screen gamma hasn't been set on png_struct |
| 1922 | * yet; it's set below. png_struct::gamma, however, is set to the |
| 1923 | * final value. |
| 1924 | */ |
| 1925 | if (png_muldiv(>est, output_gamma, png_ptr->gamma, PNG_FP_1) && |
| 1926 | !png_gamma_significant(gtest)) |
| 1927 | do_local_background = 0; |
| 1928 | |
| 1929 | else if (mode == PNG_ALPHA_STANDARD) |
| 1930 | { |
| 1931 | do_local_background = 2/*required*/; |
| 1932 | mode = PNG_ALPHA_PNG; /* prevent libpng doing it */ |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1933 | } |
| 1934 | |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 1935 | /* else leave as 1 for the checks below */ |
John Bowler | 3615d03 | 2011-11-08 10:38:09 -0600 | [diff] [blame] | 1936 | } |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1937 | |
John Bowler | 3615d03 | 2011-11-08 10:38:09 -0600 | [diff] [blame] | 1938 | /* If the bit-depth changes then handle that here. */ |
| 1939 | if (change & PNG_FORMAT_FLAG_LINEAR) |
| 1940 | { |
| 1941 | if (linear /*16-bit output*/) |
| 1942 | png_set_expand_16(png_ptr); |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1943 | |
John Bowler | 3615d03 | 2011-11-08 10:38:09 -0600 | [diff] [blame] | 1944 | else /* 8-bit output */ |
| 1945 | png_set_scale_16(png_ptr); |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1946 | |
John Bowler | 3615d03 | 2011-11-08 10:38:09 -0600 | [diff] [blame] | 1947 | change &= ~PNG_FORMAT_FLAG_LINEAR; |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1948 | } |
| 1949 | |
| 1950 | /* Now the background/alpha channel changes. */ |
| 1951 | if (change & PNG_FORMAT_FLAG_ALPHA) |
| 1952 | { |
| 1953 | /* Removing an alpha channel requires composition for the 8-bit |
| 1954 | * formats; for the 16-bit it is already done, above, by the |
| 1955 | * pre-multiplication and the channel just needs to be stripped. |
| 1956 | */ |
| 1957 | if (base_format & PNG_FORMAT_FLAG_ALPHA) |
| 1958 | { |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 1959 | /* If RGB->gray is happening the alpha channel must be left and the |
| 1960 | * operation completed locally. |
| 1961 | * |
| 1962 | * TODO: fix libpng and remove this. |
| 1963 | */ |
| 1964 | if (do_local_background) |
| 1965 | do_local_background = 2/*required*/; |
| 1966 | |
John Bowler | 3615d03 | 2011-11-08 10:38:09 -0600 | [diff] [blame] | 1967 | /* 16-bit output: just remove the channel */ |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 1968 | else if (linear) /* compose on black (well, pre-multiply) */ |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1969 | png_set_strip_alpha(png_ptr); |
| 1970 | |
John Bowler | 3615d03 | 2011-11-08 10:38:09 -0600 | [diff] [blame] | 1971 | /* 8-bit output: do an appropriate compose */ |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1972 | else if (display->background != NULL) |
| 1973 | { |
| 1974 | png_color_16 c; |
| 1975 | |
| 1976 | c.index = 0; /*unused*/ |
| 1977 | c.red = display->background->red; |
| 1978 | c.green = display->background->green; |
| 1979 | c.blue = display->background->blue; |
| 1980 | c.gray = display->background->green; |
| 1981 | |
| 1982 | /* This is always an 8-bit sRGB value, using the 'green' channel |
| 1983 | * for gray is much better than calculating the luminance here; |
| 1984 | * we can get off-by-one errors in that calculation relative to |
| 1985 | * the app expectations and that will show up in transparent |
| 1986 | * pixels. |
| 1987 | */ |
| 1988 | png_set_background_fixed(png_ptr, &c, |
| 1989 | PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/, |
| 1990 | 0/*gamma: not used*/); |
| 1991 | } |
| 1992 | |
| 1993 | else /* compose on row: implemented below. */ |
| 1994 | { |
| 1995 | do_local_compose = 1; |
Glenn Randers-Pehrson | 84b0da9 | 2011-11-10 06:32:19 -0600 | [diff] [blame] | 1996 | /* This leaves the alpha channel in the output, so it has to be |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1997 | * removed by the code below. Set the encoding to the 'OPTIMIZE' |
| 1998 | * one so the code only has to hack on the pixels that require |
| 1999 | * composition. |
| 2000 | */ |
John Bowler | 3615d03 | 2011-11-08 10:38:09 -0600 | [diff] [blame] | 2001 | mode = PNG_ALPHA_OPTIMIZED; |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 2002 | } |
| 2003 | } |
| 2004 | |
| 2005 | else /* output needs an alpha channel */ |
| 2006 | { |
| 2007 | /* This is tricky because it happens before the swap operation has |
Glenn Randers-Pehrson | 84b0da9 | 2011-11-10 06:32:19 -0600 | [diff] [blame] | 2008 | * been accomplished; however, the swap does *not* swap the added |
John Bowler | e6fb691 | 2011-11-08 21:35:16 -0600 | [diff] [blame] | 2009 | * alpha channel (weird API), so it must be added in the correct |
| 2010 | * place. |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 2011 | */ |
John Bowler | 3615d03 | 2011-11-08 10:38:09 -0600 | [diff] [blame] | 2012 | png_uint_32 filler; /* opaque filler */ |
John Bowler | e6fb691 | 2011-11-08 21:35:16 -0600 | [diff] [blame] | 2013 | int where; |
John Bowler | 3615d03 | 2011-11-08 10:38:09 -0600 | [diff] [blame] | 2014 | |
| 2015 | if (linear) |
| 2016 | filler = 65535; |
| 2017 | |
| 2018 | else |
| 2019 | filler = 255; |
| 2020 | |
John Bowler | e6fb691 | 2011-11-08 21:35:16 -0600 | [diff] [blame] | 2021 | # ifdef PNG_FORMAT_AFIRST_SUPPORTED |
| 2022 | if (format & PNG_FORMAT_FLAG_AFIRST) |
| 2023 | { |
| 2024 | where = PNG_FILLER_BEFORE; |
| 2025 | change &= ~PNG_FORMAT_FLAG_AFIRST; |
| 2026 | } |
| 2027 | |
| 2028 | else |
| 2029 | # endif |
| 2030 | where = PNG_FILLER_AFTER; |
| 2031 | |
| 2032 | png_set_add_alpha(png_ptr, filler, where); |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 2033 | } |
| 2034 | |
John Bowler | e6fb691 | 2011-11-08 21:35:16 -0600 | [diff] [blame] | 2035 | /* This stops the (irrelevant) call to swap_alpha below. */ |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 2036 | change &= ~PNG_FORMAT_FLAG_ALPHA; |
| 2037 | } |
| 2038 | |
John Bowler | 3615d03 | 2011-11-08 10:38:09 -0600 | [diff] [blame] | 2039 | /* Now set the alpha mode correctly; this is always done, even if there is |
| 2040 | * no alpha channel in either the input or the output because it correctly |
| 2041 | * sets the output gamma. |
| 2042 | */ |
| 2043 | png_set_alpha_mode_fixed(png_ptr, mode, output_gamma); |
| 2044 | |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 2045 | # ifdef PNG_FORMAT_BGR_SUPPORTED |
John Bowler | e6fb691 | 2011-11-08 21:35:16 -0600 | [diff] [blame] | 2046 | if (change & PNG_FORMAT_FLAG_BGR) |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 2047 | { |
Glenn Randers-Pehrson | 84b0da9 | 2011-11-10 06:32:19 -0600 | [diff] [blame] | 2048 | /* Check only the output format; PNG is never BGR; don't do this if |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 2049 | * the output is gray, but fix up the 'format' value in that case. |
| 2050 | */ |
| 2051 | if (format & PNG_FORMAT_FLAG_COLOR) |
| 2052 | png_set_bgr(png_ptr); |
| 2053 | |
| 2054 | else |
| 2055 | format &= ~PNG_FORMAT_FLAG_BGR; |
| 2056 | |
| 2057 | change &= ~PNG_FORMAT_FLAG_BGR; |
| 2058 | } |
| 2059 | # endif |
| 2060 | |
| 2061 | # ifdef PNG_FORMAT_AFIRST_SUPPORTED |
John Bowler | e6fb691 | 2011-11-08 21:35:16 -0600 | [diff] [blame] | 2062 | if (change & PNG_FORMAT_FLAG_AFIRST) |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 2063 | { |
| 2064 | /* Only relevant if there is an alpha channel - it's particularly |
| 2065 | * important to handle this correctly because do_local_compose may |
| 2066 | * be set above and then libpng will keep the alpha channel for this |
| 2067 | * code to remove. |
| 2068 | */ |
| 2069 | if (format & PNG_FORMAT_FLAG_ALPHA) |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 2070 | { |
| 2071 | /* Disable this if doing a local background, |
| 2072 | * TODO: remove this when local background is no longer required. |
| 2073 | */ |
| 2074 | if (do_local_background != 2) |
| 2075 | png_set_swap_alpha(png_ptr); |
| 2076 | } |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 2077 | |
| 2078 | else |
| 2079 | format &= ~PNG_FORMAT_FLAG_AFIRST; |
| 2080 | |
| 2081 | change &= ~PNG_FORMAT_FLAG_AFIRST; |
| 2082 | } |
| 2083 | # endif |
| 2084 | |
| 2085 | /* If the *output* is 16-bit then we need to check for a byte-swap on this |
| 2086 | * architecture. |
| 2087 | */ |
| 2088 | if (linear) |
| 2089 | { |
| 2090 | PNG_CONST png_uint_16 le = 0x0001; |
| 2091 | |
| 2092 | if (*(png_const_bytep)&le) |
| 2093 | png_set_swap(png_ptr); |
| 2094 | } |
| 2095 | |
| 2096 | /* If change is not now 0 some transformation is missing - error out. */ |
| 2097 | if (change) |
| 2098 | png_error(png_ptr, "png_read_image: unsupported transformation"); |
| 2099 | } |
| 2100 | |
John Bowler | 89c2f84 | 2011-11-16 12:04:39 -0600 | [diff] [blame] | 2101 | # ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED |
| 2102 | /* Prepare the reader to ignore all recognized chunks whose data will not |
| 2103 | * be used, i.e., all chunks recognized by libpng except for those |
| 2104 | * involved in basic image reading: |
| 2105 | * |
| 2106 | * IHDR, PLTE, IDAT, IEND |
| 2107 | * |
| 2108 | * Or image data handling: |
| 2109 | * |
| 2110 | * tRNS, bKGD, gAMA, cHRM, sRGB, iCCP and sBIT. |
| 2111 | * |
| 2112 | * This provides a small performance improvement and eliminates any |
| 2113 | * potential vulnerability to security problems in the unused chunks. |
| 2114 | * |
| 2115 | * TODO: make it so that this is an explicit list to process, not a list |
| 2116 | * to ignore? |
| 2117 | */ |
| 2118 | { |
| 2119 | static PNG_CONST png_byte chunks_to_ignore[] = { |
| 2120 | 104, 73, 83, 84, '\0', /* hIST */ |
| 2121 | 105, 84, 88, 116, '\0', /* iTXt */ |
| 2122 | 111, 70, 70, 115, '\0', /* oFFs */ |
| 2123 | 112, 67, 65, 76, '\0', /* pCAL */ |
| 2124 | 112, 72, 89, 115, '\0', /* pHYs */ |
| 2125 | 115, 67, 65, 76, '\0', /* sCAL */ |
| 2126 | 115, 80, 76, 84, '\0', /* sPLT */ |
| 2127 | 116, 69, 88, 116, '\0', /* tEXt */ |
| 2128 | 116, 73, 77, 69, '\0', /* tIME */ |
| 2129 | 122, 84, 88, 116, '\0' /* zTXt */ |
| 2130 | }; |
| 2131 | |
| 2132 | /* Ignore unknown chunks */ |
| 2133 | png_set_keep_unknown_chunks(png_ptr, 1 /* PNG_HANDLE_CHUNK_NEVER */, |
| 2134 | NULL, 0); |
| 2135 | |
| 2136 | /* Ignore known but unused chunks */ |
| 2137 | png_set_keep_unknown_chunks(png_ptr, 1 /* PNG_HANDLE_CHUNK_NEVER */, |
| 2138 | chunks_to_ignore, (sizeof chunks_to_ignore)/5); |
| 2139 | } |
| 2140 | # endif /* PNG_HANDLE_AS_UNKNOWN_SUPPORTED */ |
| 2141 | |
Glenn Randers-Pehrson | 84b0da9 | 2011-11-10 06:32:19 -0600 | [diff] [blame] | 2142 | /* Update the 'info' structure and make sure the result is as required; first |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 2143 | * make sure to turn on the interlace handling if it will be required |
| 2144 | * (because it can't be turned on *after* the call to png_read_update_info!) |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 2145 | * |
| 2146 | * TODO: remove the do_local_background fixup below. |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 2147 | */ |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 2148 | if (!do_local_compose && do_local_background != 2) |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 2149 | passes = png_set_interlace_handling(png_ptr); |
| 2150 | |
| 2151 | png_read_update_info(png_ptr, info_ptr); |
| 2152 | |
| 2153 | { |
| 2154 | png_uint_32 info_format = 0; |
| 2155 | |
| 2156 | if (info_ptr->color_type & PNG_COLOR_MASK_COLOR) |
| 2157 | info_format |= PNG_FORMAT_FLAG_COLOR; |
| 2158 | |
| 2159 | if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA) |
| 2160 | { |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 2161 | /* do_local_compose removes this channel below. */ |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 2162 | if (!do_local_compose) |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 2163 | { |
| 2164 | /* do_local_background does the same if required. */ |
| 2165 | if (do_local_background != 2 || |
| 2166 | (format & PNG_FORMAT_FLAG_ALPHA) != 0) |
| 2167 | info_format |= PNG_FORMAT_FLAG_ALPHA; |
| 2168 | } |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 2169 | } |
| 2170 | |
| 2171 | else if (do_local_compose) /* internal error */ |
| 2172 | png_error(png_ptr, "png_image_read: alpha channel lost"); |
| 2173 | |
| 2174 | if (info_ptr->bit_depth == 16) |
| 2175 | info_format |= PNG_FORMAT_FLAG_LINEAR; |
| 2176 | |
| 2177 | # ifdef PNG_FORMAT_BGR_SUPPORTED |
| 2178 | if (png_ptr->transformations & PNG_BGR) |
| 2179 | info_format |= PNG_FORMAT_FLAG_BGR; |
| 2180 | # endif |
| 2181 | |
| 2182 | # ifdef PNG_FORMAT_AFIRST_SUPPORTED |
John Bowler | e6fb691 | 2011-11-08 21:35:16 -0600 | [diff] [blame] | 2183 | if (png_ptr->transformations & PNG_SWAP_ALPHA || |
| 2184 | ((png_ptr->transformations & PNG_ADD_ALPHA) != 0 && |
| 2185 | (png_ptr->flags & PNG_FLAG_FILLER_AFTER) == 0)) |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 2186 | info_format |= PNG_FORMAT_FLAG_AFIRST; |
| 2187 | # endif |
| 2188 | |
| 2189 | /* This is actually an internal error. */ |
| 2190 | if (info_format != format) |
| 2191 | png_error(png_ptr, "png_read_image: invalid transformations"); |
| 2192 | } |
| 2193 | |
| 2194 | /* Now read the rows. If do_local_compose is set then it is necessary to use |
| 2195 | * a local row buffer. The output will be GA, RGBA or BGRA and must be |
| 2196 | * converted to G, RGB or BGR as appropriate. The 'local_row' member of the |
| 2197 | * display acts as a flag. |
| 2198 | */ |
| 2199 | { |
John Bowler | 4fa96a4 | 2011-11-16 16:39:16 -0600 | [diff] [blame] | 2200 | png_bytep first_row = png_voidcast(png_bytep, display->buffer); |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 2201 | ptrdiff_t row_bytes = display->row_stride; |
| 2202 | |
| 2203 | if (linear) |
| 2204 | row_bytes *= sizeof (png_uint_16); |
| 2205 | |
| 2206 | /* The following expression is designed to work correctly whether it gives |
| 2207 | * a signed or an unsigned result. |
| 2208 | */ |
| 2209 | if (row_bytes < 0) |
| 2210 | first_row += (image->height-1) * (-row_bytes); |
| 2211 | |
| 2212 | display->first_row = first_row; |
| 2213 | display->row_bytes = row_bytes; |
| 2214 | } |
| 2215 | |
| 2216 | if (do_local_compose) |
| 2217 | { |
| 2218 | int result; |
John Bowler | 4fa96a4 | 2011-11-16 16:39:16 -0600 | [diff] [blame] | 2219 | png_bytep row = png_voidcast(png_bytep, png_malloc(png_ptr, |
| 2220 | png_get_rowbytes(png_ptr, info_ptr))); |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 2221 | |
| 2222 | display->local_row = row; |
| 2223 | result = png_safe_execute(image, png_image_read_composite, display); |
| 2224 | display->local_row = NULL; |
| 2225 | png_free(png_ptr, row); |
| 2226 | |
| 2227 | return result; |
| 2228 | } |
| 2229 | |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 2230 | else if (do_local_background == 2) |
| 2231 | { |
| 2232 | int result; |
John Bowler | 4fa96a4 | 2011-11-16 16:39:16 -0600 | [diff] [blame] | 2233 | png_bytep row = png_voidcast(png_bytep, png_malloc(png_ptr, |
| 2234 | png_get_rowbytes(png_ptr, info_ptr))); |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 2235 | |
| 2236 | display->local_row = row; |
| 2237 | result = png_safe_execute(image, png_image_read_background, display); |
| 2238 | display->local_row = NULL; |
| 2239 | png_free(png_ptr, row); |
| 2240 | |
| 2241 | return result; |
| 2242 | } |
| 2243 | |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 2244 | else |
| 2245 | { |
| 2246 | png_alloc_size_t row_bytes = display->row_bytes; |
| 2247 | |
| 2248 | while (--passes >= 0) |
| 2249 | { |
| 2250 | png_uint_32 y = image->height; |
| 2251 | png_bytep row = display->first_row; |
| 2252 | |
| 2253 | while (y-- > 0) |
| 2254 | { |
| 2255 | png_read_row(png_ptr, row, NULL); |
| 2256 | row += row_bytes; |
| 2257 | } |
| 2258 | } |
| 2259 | |
| 2260 | return 1; |
| 2261 | } |
| 2262 | } |
| 2263 | |
| 2264 | int PNGAPI |
| 2265 | png_image_finish_read(png_imagep image, png_colorp background, void *buffer, |
| 2266 | png_int_32 row_stride) |
| 2267 | { |
| 2268 | if (image != NULL) |
| 2269 | { |
| 2270 | png_uint_32 check; |
| 2271 | |
John Bowler | 3706d73 | 2011-11-21 10:28:06 -0600 | [diff] [blame] | 2272 | if (row_stride == 0) |
| 2273 | row_stride = PNG_IMAGE_ROW_STRIDE(*image); |
| 2274 | |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 2275 | if (row_stride < 0) |
| 2276 | check = -row_stride; |
| 2277 | |
| 2278 | else |
| 2279 | check = row_stride; |
| 2280 | |
| 2281 | if (buffer != NULL && check >= PNG_IMAGE_ROW_STRIDE(*image)) |
| 2282 | { |
| 2283 | int result; |
| 2284 | png_image_read_control display; |
| 2285 | |
John Bowler | baeb6d1 | 2011-11-26 18:21:02 -0600 | [diff] [blame] | 2286 | png_memset(&display, 0, sizeof display); |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 2287 | display.image = image; |
| 2288 | display.buffer = buffer; |
| 2289 | display.row_stride = row_stride; |
| 2290 | display.background = background; |
| 2291 | display.local_row = NULL; |
| 2292 | result = png_safe_execute(image, png_image_read_end, &display); |
| 2293 | png_image_free(image); |
| 2294 | return result; |
| 2295 | } |
| 2296 | |
| 2297 | else |
| 2298 | return png_image_error(image, |
| 2299 | "png_image_finish_read: invalid argument"); |
| 2300 | } |
| 2301 | |
| 2302 | return 0; |
| 2303 | } |
| 2304 | |
| 2305 | #endif /* PNG_SIMPLIFIED_READ_SUPPORTED */ |
Glenn Randers-Pehrson | 9c3ab68 | 2006-02-20 22:09:05 -0600 | [diff] [blame] | 2306 | #endif /* PNG_READ_SUPPORTED */ |