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 | */ |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame^] | 1162 | /* Arguments to png_image_finish_read: */ |
| 1163 | |
| 1164 | /* Encoding of PNG data (used by the color-map code) */ |
| 1165 | # define E_NOTSET 0 /* File encoding not yet known */ |
| 1166 | # define E_sRGB 1 /* 8-bit encoded to sRGB gamma */ |
| 1167 | # define E_LINEAR 2 /* 16-bit linear: not encoded, NOT pre-multiplied! */ |
| 1168 | # define E_FILE 3 /* 8-bit encoded to file gamma, not sRGB or linear */ |
| 1169 | # define E_LINEAR8 4 /* 8-bit linear: only from a file value */ |
| 1170 | |
| 1171 | /* Color-map processing: after libpng has run on the PNG image further |
| 1172 | * processing may be needed to conver the data to color-map indicies. |
| 1173 | */ |
| 1174 | #define PNG_CMAP_NONE 0 |
| 1175 | #define PNG_CMAP_GA 1 /* Process GA data to a color-map with alpha */ |
| 1176 | #define PNG_CMAP_TRANS 2 /* Process GA data to a background index */ |
| 1177 | #define PNG_CMAP_RGB 3 /* Process RGB data */ |
| 1178 | #define PNG_CMAP_RGB_ALPHA 4 /* Process RGBA data */ |
| 1179 | |
| 1180 | /* The following document where the background is for each processing case. */ |
| 1181 | #define PNG_CMAP_NONE_BACKGROUND 256 |
| 1182 | #define PNG_CMAP_GA_BACKGROUND 231 |
| 1183 | #define PNG_CMAP_TRANS_BACKGROUND 254 |
| 1184 | #define PNG_CMAP_RGB_BACKGROUND 256 |
| 1185 | #define PNG_CMAP_RGB_ALPHA_BACKGROUND 216 |
| 1186 | |
| 1187 | typedef struct |
| 1188 | { |
| 1189 | /* Arguments: */ |
| 1190 | png_imagep image; |
| 1191 | png_voidp buffer; |
| 1192 | png_int_32 row_stride; |
| 1193 | png_voidp colormap; |
| 1194 | png_colorp background; |
| 1195 | /* Local variables: */ |
| 1196 | png_bytep local_row; |
| 1197 | png_bytep first_row; |
| 1198 | ptrdiff_t row_bytes; /* step between rows */ |
| 1199 | int file_encoding; /* E_ values above */ |
| 1200 | png_fixed_point gamma_to_linear; /* For E_FILE, reciprocal of gamma */ |
| 1201 | int colormap_processing; /* PNG_CMAP_ values above */ |
| 1202 | } png_image_read_control; |
| 1203 | |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1204 | /* 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] | 1205 | * called, so setting up the jmp_buf is not required. This means that anything |
| 1206 | * called from here must *not* call png_malloc - it has to call png_malloc_warn |
| 1207 | * instead so that control is returned safely back to this routine. |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1208 | */ |
| 1209 | static int |
| 1210 | png_image_read_init(png_imagep image) |
| 1211 | { |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame^] | 1212 | if (image->opaque == NULL) |
| 1213 | { |
| 1214 | png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, image, |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1215 | png_safe_error, png_safe_warning); |
| 1216 | |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame^] | 1217 | /* And set the rest of the structure to NULL to ensure that the various |
| 1218 | * fields are consistent. |
| 1219 | */ |
| 1220 | memset(image, 0, sizeof *image); |
| 1221 | image->version = PNG_IMAGE_VERSION; |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1222 | |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame^] | 1223 | if (png_ptr != NULL) |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1224 | { |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame^] | 1225 | png_infop info_ptr = png_create_info_struct(png_ptr); |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1226 | |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame^] | 1227 | if (info_ptr != NULL) |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1228 | { |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame^] | 1229 | png_controlp control = png_voidcast(png_controlp, |
| 1230 | png_malloc_warn(png_ptr, sizeof *control)); |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1231 | |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame^] | 1232 | if (control != NULL) |
| 1233 | { |
| 1234 | png_memset(control, 0, sizeof *control); |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1235 | |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame^] | 1236 | control->png_ptr = png_ptr; |
| 1237 | control->info_ptr = info_ptr; |
| 1238 | control->for_write = 0; |
| 1239 | |
| 1240 | image->opaque = control; |
| 1241 | return 1; |
| 1242 | } |
| 1243 | |
| 1244 | /* Error clean up */ |
| 1245 | png_destroy_info_struct(png_ptr, &info_ptr); |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1246 | } |
| 1247 | |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame^] | 1248 | png_destroy_read_struct(&png_ptr, NULL, NULL); |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1249 | } |
| 1250 | |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame^] | 1251 | return png_image_error(image, "png_image_read: out of memory"); |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1252 | } |
| 1253 | |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame^] | 1254 | return png_image_error(image, "png_image_read: opaque pointer not NULL"); |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1255 | } |
| 1256 | |
| 1257 | /* Utility to find the base format of a PNG file from a png_struct. */ |
| 1258 | static png_uint_32 |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame^] | 1259 | png_image_format(png_structrp png_ptr) |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1260 | { |
| 1261 | png_uint_32 format = 0; |
| 1262 | |
| 1263 | if (png_ptr->color_type & PNG_COLOR_MASK_COLOR) |
| 1264 | format |= PNG_FORMAT_FLAG_COLOR; |
| 1265 | |
| 1266 | if (png_ptr->color_type & PNG_COLOR_MASK_ALPHA) |
| 1267 | format |= PNG_FORMAT_FLAG_ALPHA; |
| 1268 | |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame^] | 1269 | /* Use png_ptr here, not info_ptr, because by examination png_handle_tRNS |
| 1270 | * sets the png_struct fields; that's all we are interested in here. The |
| 1271 | * precise interaction with an app call to png_set_tRNS and PNG file reading |
| 1272 | * is unclear. |
| 1273 | */ |
| 1274 | else if (png_ptr->num_trans > 0) |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1275 | format |= PNG_FORMAT_FLAG_ALPHA; |
| 1276 | |
| 1277 | if (png_ptr->bit_depth == 16) |
| 1278 | format |= PNG_FORMAT_FLAG_LINEAR; |
| 1279 | |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame^] | 1280 | if (png_ptr->color_type & PNG_COLOR_MASK_PALETTE) |
| 1281 | format |= PNG_FORMAT_FLAG_COLORMAP; |
| 1282 | |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1283 | return format; |
| 1284 | } |
| 1285 | |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame^] | 1286 | /* Is the given gamma significantly different from sRGB? The test is the same |
| 1287 | * one used in pngrtran.c when deciding whether to do gamma correction. The |
| 1288 | * arithmetic optimizes the division by using the fact that the inverse of the |
| 1289 | * file sRGB gamma is 2.2 |
| 1290 | */ |
| 1291 | static int |
| 1292 | png_gamma_not_sRGB(png_fixed_point g) |
| 1293 | { |
| 1294 | if (g < PNG_FP_1) |
| 1295 | { |
| 1296 | /* An uninitialized gamma is assumed to be sRGB for the simplified API. */ |
| 1297 | if (g == 0) |
| 1298 | return 0; |
| 1299 | |
| 1300 | return png_gamma_significant((g * 11 + 2)/5 /* i.e. *2.2, rounded */); |
| 1301 | } |
| 1302 | |
| 1303 | return 1; |
| 1304 | } |
| 1305 | |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1306 | /* Do the main body of a 'png_image_begin_read' function; read the PNG file |
| 1307 | * header and fill in all the information. This is executed in a safe context, |
| 1308 | * unlike the init routine above. |
| 1309 | */ |
| 1310 | static int |
| 1311 | png_image_read_header(png_voidp argument) |
| 1312 | { |
John Bowler | 4fa96a4 | 2011-11-16 16:39:16 -0600 | [diff] [blame] | 1313 | png_imagep image = png_voidcast(png_imagep, argument); |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 1314 | png_structrp png_ptr = image->opaque->png_ptr; |
| 1315 | png_inforp info_ptr = image->opaque->info_ptr; |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1316 | |
| 1317 | png_read_info(png_ptr, info_ptr); |
| 1318 | |
| 1319 | /* Do this the fast way; just read directly out of png_struct. */ |
| 1320 | image->width = png_ptr->width; |
| 1321 | image->height = png_ptr->height; |
| 1322 | |
| 1323 | { |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame^] | 1324 | png_uint_32 format = png_image_format(png_ptr); |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1325 | |
| 1326 | image->format = format; |
John Bowler | 04336ba | 2012-01-16 07:48:36 -0600 | [diff] [blame] | 1327 | |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1328 | /* Now try to work out whether the color data does not match sRGB. */ |
| 1329 | if ((format & PNG_FORMAT_FLAG_COLOR) != 0 && |
| 1330 | (info_ptr->valid & PNG_INFO_sRGB) == 0) |
| 1331 | { |
| 1332 | /* gamma is irrelevant because libpng does gamma correction, what |
| 1333 | * 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] | 1334 | * cRHM, if the iCCP profile appears to have different end points. |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1335 | */ |
| 1336 | if (info_ptr->valid & PNG_INFO_cHRM) |
| 1337 | { |
| 1338 | /* TODO: this is a copy'n'paste from pngrutil.c, make a common |
| 1339 | * checking function. This checks for a 1% error. |
| 1340 | */ |
| 1341 | /* The cHRM chunk is used in preference to iCCP */ |
| 1342 | if (PNG_OUT_OF_RANGE(info_ptr->x_white, 31270, 1000) || |
| 1343 | PNG_OUT_OF_RANGE(info_ptr->y_white, 32900, 1000) || |
| 1344 | PNG_OUT_OF_RANGE(info_ptr->x_red, 64000, 1000) || |
| 1345 | PNG_OUT_OF_RANGE(info_ptr->y_red, 33000, 1000) || |
| 1346 | PNG_OUT_OF_RANGE(info_ptr->x_green, 30000, 1000) || |
| 1347 | PNG_OUT_OF_RANGE(info_ptr->y_green, 60000, 1000) || |
| 1348 | PNG_OUT_OF_RANGE(info_ptr->x_blue, 15000, 1000) || |
| 1349 | PNG_OUT_OF_RANGE(info_ptr->y_blue, 6000, 1000)) |
| 1350 | image->flags |= PNG_IMAGE_FLAG_COLORSPACE_NOT_sRGB; |
| 1351 | } |
| 1352 | |
| 1353 | else if (info_ptr->valid & PNG_INFO_iCCP) |
| 1354 | { |
Glenn Randers-Pehrson | 6d8e090 | 2011-11-11 18:41:59 -0600 | [diff] [blame] | 1355 | # if 0 |
John Bowler | 04336ba | 2012-01-16 07:48:36 -0600 | [diff] [blame] | 1356 | /* TODO: IMPLEMENT THIS! */ |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1357 | /* Here if we just have an iCCP chunk. */ |
| 1358 | if (!png_iCCP_is_sRGB(png_ptr, info_ptr)) |
| 1359 | # endif |
| 1360 | image->flags |= PNG_IMAGE_FLAG_COLORSPACE_NOT_sRGB; |
| 1361 | } |
| 1362 | } |
| 1363 | } |
| 1364 | |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame^] | 1365 | /* We need the maximum number of entries regardless of the format the |
| 1366 | * application sets here. |
| 1367 | */ |
| 1368 | { |
| 1369 | png_uint_32 cmap_entries; |
| 1370 | |
| 1371 | switch (png_ptr->color_type) |
| 1372 | { |
| 1373 | case PNG_COLOR_TYPE_GRAY: |
| 1374 | cmap_entries = 1U << png_ptr->bit_depth; |
| 1375 | break; |
| 1376 | |
| 1377 | case PNG_COLOR_TYPE_PALETTE: |
| 1378 | cmap_entries = png_ptr->num_palette; |
| 1379 | break; |
| 1380 | |
| 1381 | default: |
| 1382 | cmap_entries = 256; |
| 1383 | break; |
| 1384 | } |
| 1385 | |
| 1386 | if (cmap_entries > 256) |
| 1387 | cmap_entries = 256; |
| 1388 | |
| 1389 | image->colormap_entries = cmap_entries; |
| 1390 | } |
| 1391 | |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1392 | return 1; |
| 1393 | } |
| 1394 | |
| 1395 | #ifdef PNG_STDIO_SUPPORTED |
| 1396 | int PNGAPI |
| 1397 | png_image_begin_read_from_stdio(png_imagep image, FILE* file) |
| 1398 | { |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame^] | 1399 | if (image != NULL && image->version == PNG_IMAGE_VERSION) |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1400 | { |
| 1401 | if (file != NULL) |
| 1402 | { |
| 1403 | if (png_image_read_init(image)) |
| 1404 | { |
| 1405 | /* This is slightly evil, but png_init_io doesn't do anything other |
| 1406 | * than this and we haven't changed the standard IO functions so |
| 1407 | * this saves a 'safe' function. |
| 1408 | */ |
| 1409 | image->opaque->png_ptr->io_ptr = file; |
| 1410 | return png_safe_execute(image, png_image_read_header, image); |
| 1411 | } |
| 1412 | } |
| 1413 | |
| 1414 | else |
| 1415 | return png_image_error(image, |
| 1416 | "png_image_begin_read_from_stdio: invalid argument"); |
| 1417 | } |
| 1418 | |
| 1419 | return 0; |
| 1420 | } |
| 1421 | |
| 1422 | int PNGAPI |
| 1423 | png_image_begin_read_from_file(png_imagep image, const char *file_name) |
| 1424 | { |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame^] | 1425 | if (image != NULL && image->version == PNG_IMAGE_VERSION) |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1426 | { |
| 1427 | if (file_name != NULL) |
| 1428 | { |
| 1429 | FILE *fp = fopen(file_name, "rb"); |
| 1430 | |
| 1431 | if (fp != NULL) |
| 1432 | { |
| 1433 | if (png_image_read_init(image)) |
| 1434 | { |
| 1435 | image->opaque->png_ptr->io_ptr = fp; |
| 1436 | image->opaque->owned_file = 1; |
| 1437 | return png_safe_execute(image, png_image_read_header, image); |
| 1438 | } |
| 1439 | |
| 1440 | /* Clean up: just the opened file. */ |
| 1441 | (void)fclose(fp); |
| 1442 | } |
| 1443 | |
| 1444 | else |
| 1445 | return png_image_error(image, strerror(errno)); |
| 1446 | } |
| 1447 | |
| 1448 | else |
| 1449 | return png_image_error(image, |
| 1450 | "png_image_begin_read_from_file: invalid argument"); |
| 1451 | } |
| 1452 | |
| 1453 | return 0; |
| 1454 | } |
| 1455 | #endif /* PNG_STDIO_SUPPORTED */ |
| 1456 | |
| 1457 | static void PNGCBAPI |
| 1458 | png_image_memory_read(png_structp png_ptr, png_bytep out, png_size_t need) |
| 1459 | { |
| 1460 | if (png_ptr != NULL) |
| 1461 | { |
John Bowler | 4fa96a4 | 2011-11-16 16:39:16 -0600 | [diff] [blame] | 1462 | png_imagep image = png_voidcast(png_imagep, png_ptr->io_ptr); |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1463 | if (image != NULL) |
| 1464 | { |
| 1465 | png_controlp cp = image->opaque; |
| 1466 | if (cp != NULL) |
| 1467 | { |
| 1468 | png_const_bytep memory = cp->memory; |
| 1469 | png_size_t size = cp->size; |
| 1470 | |
| 1471 | if (memory != NULL && size >= need) |
| 1472 | { |
John Bowler | baeb6d1 | 2011-11-26 18:21:02 -0600 | [diff] [blame] | 1473 | png_memcpy(out, memory, need); |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1474 | cp->memory = memory + need; |
| 1475 | cp->size = size - need; |
| 1476 | return; |
| 1477 | } |
| 1478 | |
| 1479 | png_error(png_ptr, "read beyond end of data"); |
| 1480 | } |
| 1481 | } |
| 1482 | |
| 1483 | png_error(png_ptr, "invalid memory read"); |
| 1484 | } |
| 1485 | } |
| 1486 | |
| 1487 | int PNGAPI png_image_begin_read_from_memory(png_imagep image, |
| 1488 | png_const_voidp memory, png_size_t size) |
| 1489 | { |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame^] | 1490 | if (image != NULL && image->version == PNG_IMAGE_VERSION) |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1491 | { |
| 1492 | if (memory != NULL && size > 0) |
| 1493 | { |
| 1494 | if (png_image_read_init(image)) |
| 1495 | { |
| 1496 | /* Now set the IO functions to read from the memory buffer and |
| 1497 | * store it into io_ptr. Again do this in-place to avoid calling a |
| 1498 | * libpng function that requires error handling. |
| 1499 | */ |
John Bowler | 4fa96a4 | 2011-11-16 16:39:16 -0600 | [diff] [blame] | 1500 | image->opaque->memory = png_voidcast(png_const_bytep, memory); |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1501 | image->opaque->size = size; |
| 1502 | image->opaque->png_ptr->io_ptr = image; |
| 1503 | image->opaque->png_ptr->read_data_fn = png_image_memory_read; |
| 1504 | |
| 1505 | return png_safe_execute(image, png_image_read_header, image); |
| 1506 | } |
| 1507 | } |
| 1508 | |
| 1509 | else |
| 1510 | return png_image_error(image, |
| 1511 | "png_image_begin_read_from_memory: invalid argument"); |
| 1512 | } |
| 1513 | |
| 1514 | return 0; |
| 1515 | } |
| 1516 | |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame^] | 1517 | /* Utility function to skip chunks that are not used by the simplified image |
| 1518 | * read functions and an appropriate macro to call it. |
| 1519 | */ |
| 1520 | #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED |
| 1521 | static void |
| 1522 | png_image_skip_unused_chunks(png_structrp png_ptr) |
John Bowler | 04336ba | 2012-01-16 07:48:36 -0600 | [diff] [blame] | 1523 | { |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame^] | 1524 | /* Prepare the reader to ignore all recognized chunks whose data will not |
| 1525 | * be used, i.e., all chunks recognized by libpng except for those |
| 1526 | * involved in basic image reading: |
| 1527 | * |
| 1528 | * IHDR, PLTE, IDAT, IEND |
| 1529 | * |
| 1530 | * Or image data handling: |
| 1531 | * |
| 1532 | * tRNS, bKGD, gAMA, cHRM, sRGB, iCCP and sBIT. |
| 1533 | * |
| 1534 | * This provides a small performance improvement and eliminates any |
| 1535 | * potential vulnerability to security problems in the unused chunks. |
| 1536 | * |
| 1537 | * TODO: make it so that this is an explicit list to process, not a list |
| 1538 | * to ignore? |
| 1539 | */ |
John Bowler | 04336ba | 2012-01-16 07:48:36 -0600 | [diff] [blame] | 1540 | { |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame^] | 1541 | static PNG_CONST png_byte chunks_to_ignore[] = { |
| 1542 | 104, 73, 83, 84, '\0', /* hIST */ |
| 1543 | 105, 84, 88, 116, '\0', /* iTXt */ |
| 1544 | 111, 70, 70, 115, '\0', /* oFFs */ |
| 1545 | 112, 67, 65, 76, '\0', /* pCAL */ |
| 1546 | 112, 72, 89, 115, '\0', /* pHYs */ |
| 1547 | 115, 67, 65, 76, '\0', /* sCAL */ |
| 1548 | 115, 80, 76, 84, '\0', /* sPLT */ |
| 1549 | 116, 69, 88, 116, '\0', /* tEXt */ |
| 1550 | 116, 73, 77, 69, '\0', /* tIME */ |
| 1551 | 122, 84, 88, 116, '\0' /* zTXt */ |
| 1552 | }; |
| 1553 | |
| 1554 | /* Ignore unknown chunks */ |
| 1555 | png_set_keep_unknown_chunks(png_ptr, 1 /* PNG_HANDLE_CHUNK_NEVER */, |
| 1556 | NULL, 0); |
| 1557 | |
| 1558 | /* Ignore known but unused chunks */ |
| 1559 | png_set_keep_unknown_chunks(png_ptr, 1 /* PNG_HANDLE_CHUNK_NEVER */, |
| 1560 | chunks_to_ignore, (sizeof chunks_to_ignore)/5); |
| 1561 | } |
| 1562 | } |
| 1563 | |
| 1564 | # define PNG_SKIP_CHUNKS(p) png_image_skip_unused_chunks(p) |
| 1565 | #else |
| 1566 | # define PNG_SKIP_CHUNKS(p) ((void)0) |
| 1567 | #endif /* PNG_HANDLE_AS_UNKNOWN_SUPPORTED */ |
| 1568 | |
| 1569 | /* The following macro gives the exact rounded answer for all values in the |
| 1570 | * range 0..255 (it actually divides by 51.2, but the rounding still generates |
| 1571 | * the correct numbers 0..5 |
| 1572 | */ |
| 1573 | #define PNG_DIV51(v8) (((v8) * 5 + 130) >> 8) |
| 1574 | |
| 1575 | /* Utility functions to make particular color-maps */ |
| 1576 | static void |
| 1577 | set_file_encoding(png_image_read_control *display) |
| 1578 | { |
| 1579 | png_fixed_point g = display->image->opaque->png_ptr->gamma; |
| 1580 | if (png_gamma_significant(g)) |
| 1581 | { |
| 1582 | if (png_gamma_not_sRGB(g)) |
John Bowler | 04336ba | 2012-01-16 07:48:36 -0600 | [diff] [blame] | 1583 | { |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame^] | 1584 | display->file_encoding = E_FILE; |
| 1585 | display->gamma_to_linear = png_reciprocal(g); |
John Bowler | 04336ba | 2012-01-16 07:48:36 -0600 | [diff] [blame] | 1586 | } |
| 1587 | |
| 1588 | else |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame^] | 1589 | display->file_encoding = E_sRGB; |
John Bowler | 04336ba | 2012-01-16 07:48:36 -0600 | [diff] [blame] | 1590 | } |
| 1591 | |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame^] | 1592 | else |
| 1593 | display->file_encoding = E_LINEAR8; |
John Bowler | 04336ba | 2012-01-16 07:48:36 -0600 | [diff] [blame] | 1594 | } |
| 1595 | |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame^] | 1596 | static unsigned int |
| 1597 | decode_gamma(png_image_read_control *display, png_uint_32 value, int encoding) |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1598 | { |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame^] | 1599 | if (encoding == E_FILE) /* double check */ |
| 1600 | encoding = display->file_encoding; |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1601 | |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame^] | 1602 | if (encoding == E_NOTSET) /* must be the file encoding */ |
| 1603 | { |
| 1604 | set_file_encoding(display); |
| 1605 | encoding = display->file_encoding; |
| 1606 | } |
| 1607 | |
| 1608 | switch (encoding) |
| 1609 | { |
| 1610 | case E_FILE: |
| 1611 | value = png_gamma_16bit_correct(value*257, display->gamma_to_linear); |
| 1612 | break; |
| 1613 | |
| 1614 | case E_sRGB: |
| 1615 | value = png_sRGB_table[value]; |
| 1616 | break; |
| 1617 | |
| 1618 | case E_LINEAR: |
| 1619 | break; |
| 1620 | |
| 1621 | case E_LINEAR8: |
| 1622 | value *= 257; |
| 1623 | break; |
| 1624 | |
| 1625 | default: |
| 1626 | png_error(display->image->opaque->png_ptr, |
| 1627 | "unexpected encoding (internal error)"); |
| 1628 | break; |
| 1629 | } |
| 1630 | |
| 1631 | return value; |
| 1632 | } |
| 1633 | |
| 1634 | static png_uint_32 |
| 1635 | png_colormap_compose(png_image_read_control *display, |
| 1636 | png_uint_32 foreground, int foreground_encoding, png_uint_32 alpha, |
| 1637 | png_uint_32 background, int encoding) |
| 1638 | { |
| 1639 | /* The file value is composed on the background, the background has the given |
| 1640 | * encoding and so does the result, the file is encoded with E_FILE and the |
| 1641 | * file and alpha are 8-bit values. The (output) encoding will always be |
| 1642 | * E_LINEAR or E_sRGB. |
| 1643 | */ |
| 1644 | png_uint_32 f = decode_gamma(display, foreground, foreground_encoding); |
| 1645 | png_uint_32 b = decode_gamma(display, background, encoding); |
| 1646 | |
| 1647 | /* The alpha is always an 8-bit value (it comes from the palette), the value |
| 1648 | * scaled by 255 is what PNG_sRGB_FROM_LINEAR requires. |
| 1649 | */ |
| 1650 | f = f * alpha + b * (255-alpha); |
| 1651 | |
| 1652 | if (encoding == E_LINEAR) |
| 1653 | { |
| 1654 | /* Scale to 65535; divide by 255, approximately (in fact this is extremely |
| 1655 | * accurate, it divides by 255.00000005937181414556, with no overflow.) |
| 1656 | */ |
| 1657 | f *= 257; /* Now scaled by 65535 */ |
| 1658 | f += f >> 16; |
| 1659 | f = (f+32768) >> 16; |
| 1660 | } |
| 1661 | |
| 1662 | else /* E_sRGB */ |
| 1663 | f = PNG_sRGB_FROM_LINEAR(f); |
| 1664 | |
| 1665 | return f; |
| 1666 | } |
| 1667 | |
| 1668 | /* NOTE: E_LINEAR values to this routine must be 16-bit, but E_FILE values must |
| 1669 | * be 8-bit. |
| 1670 | */ |
| 1671 | static void |
| 1672 | png_create_colormap_entry(png_image_read_control *display, |
| 1673 | png_uint_32 ip, png_uint_32 red, png_uint_32 green, png_uint_32 blue, |
| 1674 | png_uint_32 alpha, int encoding) |
| 1675 | { |
| 1676 | png_imagep image = display->image; |
| 1677 | const int output_encoding = (image->format & PNG_FORMAT_FLAG_LINEAR) ? |
| 1678 | E_LINEAR : E_sRGB; |
| 1679 | const int convert_to_Y = (image->format & PNG_FORMAT_FLAG_COLOR) == 0 && |
| 1680 | (red != green || green != blue); |
| 1681 | |
| 1682 | if (ip > 255) |
| 1683 | png_error(image->opaque->png_ptr, "color-map index out of range"); |
| 1684 | |
| 1685 | /* Update the cache with whether the file gamma is significantly different |
| 1686 | * from sRGB. |
| 1687 | */ |
| 1688 | if (encoding == E_FILE) |
| 1689 | { |
| 1690 | if (display->file_encoding == E_NOTSET) |
| 1691 | set_file_encoding(display); |
| 1692 | |
| 1693 | /* Note that the cached value may be E_FILE too, but if it is then the |
| 1694 | * gamma_to_linear member has been set. |
| 1695 | */ |
| 1696 | encoding = display->file_encoding; |
| 1697 | } |
| 1698 | |
| 1699 | if (encoding == E_FILE) |
| 1700 | { |
| 1701 | png_fixed_point g = display->gamma_to_linear; |
| 1702 | |
| 1703 | red = png_gamma_16bit_correct(red*257, g); |
| 1704 | green = png_gamma_16bit_correct(green*257, g); |
| 1705 | blue = png_gamma_16bit_correct(blue*257, g); |
| 1706 | |
| 1707 | if (convert_to_Y || output_encoding == E_LINEAR) |
| 1708 | { |
| 1709 | alpha *= 257; |
| 1710 | encoding = E_LINEAR; |
| 1711 | } |
| 1712 | |
| 1713 | else |
| 1714 | { |
| 1715 | red = PNG_sRGB_FROM_LINEAR(red * 255); |
| 1716 | green = PNG_sRGB_FROM_LINEAR(green * 255); |
| 1717 | blue = PNG_sRGB_FROM_LINEAR(blue * 255); |
| 1718 | encoding = E_sRGB; |
| 1719 | } |
| 1720 | } |
| 1721 | |
| 1722 | else if (encoding == E_LINEAR8) |
| 1723 | { |
| 1724 | /* This encoding occurs quite frequently in test cases because PngSuite |
| 1725 | * includes a gAMA 1.0 chunk with most images. |
| 1726 | */ |
| 1727 | red *= 257; |
| 1728 | green *= 257; |
| 1729 | blue *= 257; |
| 1730 | alpha *= 257; |
| 1731 | encoding = E_LINEAR; |
| 1732 | } |
| 1733 | |
| 1734 | else if (encoding == E_sRGB && (convert_to_Y || output_encoding == E_LINEAR)) |
| 1735 | { |
| 1736 | /* The values are 8-bit sRGB values, but must be converted to 16-bit |
| 1737 | * linear. |
| 1738 | */ |
| 1739 | red = png_sRGB_table[red]; |
| 1740 | green = png_sRGB_table[green]; |
| 1741 | blue = png_sRGB_table[blue]; |
| 1742 | alpha *= 257; |
| 1743 | encoding = E_LINEAR; |
| 1744 | } |
| 1745 | |
| 1746 | /* This is set if the color isn't gray but the output is. */ |
| 1747 | if (encoding == E_LINEAR) |
| 1748 | { |
| 1749 | if (convert_to_Y) |
| 1750 | { |
| 1751 | /* NOTE: these values are copied from png_do_rgb_to_gray */ |
| 1752 | png_uint_32 y = (png_uint_32)6968 * red + (png_uint_32)23434 * green + |
| 1753 | (png_uint_32)2366 * blue; |
| 1754 | |
| 1755 | if (output_encoding == E_LINEAR) |
| 1756 | y = (y + 16384) >> 15; |
| 1757 | |
| 1758 | else |
| 1759 | { |
| 1760 | /* y is scaled by 32768, we need it scaled by 255: */ |
| 1761 | y = (y + 128) >> 8; |
| 1762 | y *= 255; |
| 1763 | y = PNG_sRGB_FROM_LINEAR((y + 64) >> 7); |
| 1764 | encoding = E_sRGB; |
| 1765 | } |
| 1766 | |
| 1767 | blue = red = green = y; |
| 1768 | } |
| 1769 | |
| 1770 | else if (output_encoding == E_sRGB) |
| 1771 | { |
| 1772 | red = PNG_sRGB_FROM_LINEAR(red * 255); |
| 1773 | green = PNG_sRGB_FROM_LINEAR(green * 255); |
| 1774 | blue = PNG_sRGB_FROM_LINEAR(blue * 255); |
| 1775 | alpha = PNG_DIV257(alpha); |
| 1776 | encoding = E_sRGB; |
| 1777 | } |
| 1778 | } |
| 1779 | |
| 1780 | if (encoding != output_encoding) |
| 1781 | png_error(image->opaque->png_ptr, "bad encoding (internal error)"); |
| 1782 | |
| 1783 | /* Store the value. */ |
| 1784 | { |
| 1785 | # ifdef PNG_FORMAT_BGR_SUPPORTED |
| 1786 | const int afirst = (image->format & PNG_FORMAT_FLAG_AFIRST) != 0 && |
| 1787 | (image->format & PNG_FORMAT_FLAG_ALPHA) != 0; |
| 1788 | # else |
| 1789 | # define afirst 0 |
| 1790 | # endif |
| 1791 | # ifdef PNG_FORMAT_BGR_SUPPORTED |
| 1792 | const int bgr = (image->format & PNG_FORMAT_FLAG_BGR) ? 2 : 0; |
| 1793 | # else |
| 1794 | # define bgr 0 |
| 1795 | # endif |
| 1796 | |
| 1797 | if (output_encoding == E_LINEAR) |
| 1798 | { |
| 1799 | png_uint_16p entry = png_voidcast(png_uint_16p, display->colormap); |
| 1800 | |
| 1801 | entry += ip * PNG_IMAGE_SAMPLE_CHANNELS(image->format); |
| 1802 | |
| 1803 | /* The linear 16-bit values must be pre-multiplied by the alpha channel |
| 1804 | * value, if less than 65535 (this is, effectively, composite on black |
| 1805 | * if the alpha channel is removed.) |
| 1806 | */ |
| 1807 | switch (PNG_IMAGE_SAMPLE_CHANNELS(image->format)) |
| 1808 | { |
| 1809 | case 4: |
| 1810 | entry[afirst ? 0 : 3] = (png_uint_16)alpha; |
| 1811 | /* FALL THROUGH */ |
| 1812 | |
| 1813 | case 3: |
| 1814 | if (alpha < 65535) |
| 1815 | { |
| 1816 | if (alpha > 0) |
| 1817 | { |
| 1818 | blue = (blue * alpha + 32767U)/65535U; |
| 1819 | green = (green * alpha + 32767U)/65535U; |
| 1820 | red = (red * alpha + 32767U)/65535U; |
| 1821 | } |
| 1822 | |
| 1823 | else |
| 1824 | red = green = blue = 0; |
| 1825 | } |
| 1826 | entry[afirst + (2 ^ bgr)] = (png_uint_16)blue; |
| 1827 | entry[afirst + 1] = (png_uint_16)green; |
| 1828 | entry[afirst + bgr] = (png_uint_16)red; |
| 1829 | break; |
| 1830 | |
| 1831 | case 2: |
| 1832 | entry[1 ^ afirst] = (png_uint_16)alpha; |
| 1833 | /* FALL THROUGH */ |
| 1834 | |
| 1835 | case 1: |
| 1836 | if (alpha < 65535) |
| 1837 | { |
| 1838 | if (alpha > 0) |
| 1839 | green = (green * alpha + 32767U)/65535U; |
| 1840 | |
| 1841 | else |
| 1842 | green = 0; |
| 1843 | } |
| 1844 | entry[afirst] = (png_uint_16)green; |
| 1845 | break; |
| 1846 | |
| 1847 | default: |
| 1848 | break; |
| 1849 | } |
| 1850 | } |
| 1851 | |
| 1852 | else /* output encoding is E_sRGB */ |
| 1853 | { |
| 1854 | png_bytep entry = png_voidcast(png_bytep, display->colormap); |
| 1855 | |
| 1856 | entry += ip * PNG_IMAGE_SAMPLE_CHANNELS(image->format); |
| 1857 | |
| 1858 | switch (PNG_IMAGE_SAMPLE_CHANNELS(image->format)) |
| 1859 | { |
| 1860 | case 4: |
| 1861 | entry[afirst ? 0 : 3] = (png_byte)alpha; |
| 1862 | case 3: |
| 1863 | entry[afirst + (2 ^ bgr)] = (png_byte)blue; |
| 1864 | entry[afirst + 1] = (png_byte)green; |
| 1865 | entry[afirst + bgr] = (png_byte)red; |
| 1866 | break; |
| 1867 | |
| 1868 | case 2: |
| 1869 | entry[1 ^ afirst] = (png_byte)alpha; |
| 1870 | case 1: |
| 1871 | entry[afirst] = (png_byte)green; |
| 1872 | break; |
| 1873 | |
| 1874 | default: |
| 1875 | break; |
| 1876 | } |
| 1877 | } |
| 1878 | |
| 1879 | # ifdef afirst |
| 1880 | # undef afirst |
| 1881 | # endif |
| 1882 | # ifdef bgr |
| 1883 | # undef bgr |
| 1884 | # endif |
| 1885 | } |
| 1886 | } |
| 1887 | |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1888 | static int |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame^] | 1889 | make_gray_colormap(png_image_read_control *display) |
| 1890 | { |
| 1891 | unsigned int i; |
| 1892 | |
| 1893 | for (i=0; i<256; ++i) |
| 1894 | png_create_colormap_entry(display, i, i, i, i, 255, E_sRGB); |
| 1895 | |
| 1896 | return i; |
| 1897 | } |
| 1898 | #define PNG_GRAY_COLORMAP_ENTRIES 256 |
| 1899 | |
| 1900 | static int |
| 1901 | make_ga_colormap(png_image_read_control *display) |
| 1902 | { |
| 1903 | unsigned int i, a; |
| 1904 | |
| 1905 | /* Alpha is retained, the output will be a color-map with entries |
| 1906 | * selected by six levels of alpha. One transparent entry, 6 gray |
| 1907 | * levels for all the intermediate alpha values, leaving 230 entries |
| 1908 | * for the opaque grays. The color-map entries are the six values |
| 1909 | * [0..5]*51, the GA processing uses PNG_DIV51(value) to find the |
| 1910 | * relevant entry. |
| 1911 | * |
| 1912 | * if (alpha > 229) // opaque |
| 1913 | * { |
| 1914 | * // The 231 entries are selected to make the math below work: |
| 1915 | * base = 0; |
| 1916 | * entry = (231 * gray + 128) >> 8; |
| 1917 | * } |
| 1918 | * else if (alpha < 26) // transparent |
| 1919 | * { |
| 1920 | * base = 231; |
| 1921 | * entry = 0; |
| 1922 | * } |
| 1923 | * else // partially opaque |
| 1924 | * { |
| 1925 | * base = 226 + 6 * PNG_DIV51(alpha); |
| 1926 | * entry = PNG_DIV51(gray); |
| 1927 | * } |
| 1928 | */ |
| 1929 | i = 0; |
| 1930 | while (i < 231) |
| 1931 | { |
| 1932 | unsigned int gray = (i * 256 + 115) / 231; |
| 1933 | png_create_colormap_entry(display, i++, gray, gray, gray, 255, E_sRGB); |
| 1934 | } |
| 1935 | |
| 1936 | /* 255 is used here for the component values for consistency with the code |
| 1937 | * that undoes premultiplication in pngwrite.c. |
| 1938 | */ |
| 1939 | png_create_colormap_entry(display, i++, 255, 255, 255, 0, E_sRGB); |
| 1940 | |
| 1941 | for (a=1; a<5; ++a) |
| 1942 | { |
| 1943 | unsigned int g; |
| 1944 | |
| 1945 | for (g=0; g<6; ++g) |
| 1946 | png_create_colormap_entry(display, i++, g*51, g*51, g*51, a*51, |
| 1947 | E_sRGB); |
| 1948 | } |
| 1949 | |
| 1950 | return i; |
| 1951 | } |
| 1952 | |
| 1953 | #define PNG_GA_COLORMAP_ENTRIES 256 |
| 1954 | |
| 1955 | static int |
| 1956 | make_rgb_colormap(png_image_read_control *display) |
| 1957 | { |
| 1958 | unsigned int i, r; |
| 1959 | |
| 1960 | /* Build a 6x6x6 opaque RGB cube */ |
| 1961 | for (i=r=0; r<6; ++r) |
| 1962 | { |
| 1963 | unsigned int g; |
| 1964 | |
| 1965 | for (g=0; g<6; ++g) |
| 1966 | { |
| 1967 | unsigned int b; |
| 1968 | |
| 1969 | for (b=0; b<6; ++b) |
| 1970 | png_create_colormap_entry(display, i++, r*51, g*51, b*51, 255, |
| 1971 | E_sRGB); |
| 1972 | } |
| 1973 | } |
| 1974 | |
| 1975 | return i; |
| 1976 | } |
| 1977 | |
| 1978 | #define PNG_RGB_COLORMAP_ENTRIES 216 |
| 1979 | |
| 1980 | /* Return a palette index to the above palette given three 8-bit sRGB values. */ |
| 1981 | #define PNG_RGB_INDEX(r,g,b) \ |
| 1982 | ((png_byte)(6 * (6 * PNG_DIV51(r) + PNG_DIV51(g)) + PNG_DIV51(b))) |
| 1983 | |
| 1984 | static int |
| 1985 | png_image_read_colormap(png_voidp argument) |
| 1986 | { |
| 1987 | png_image_read_control *display = |
| 1988 | png_voidcast(png_image_read_control*, argument); |
| 1989 | const png_imagep image = display->image; |
| 1990 | |
| 1991 | const png_structrp png_ptr = image->opaque->png_ptr; |
| 1992 | const png_uint_32 output_format = image->format; |
| 1993 | const int output_encoding = (output_format & PNG_FORMAT_FLAG_LINEAR) ? |
| 1994 | E_LINEAR : E_sRGB; |
| 1995 | |
| 1996 | unsigned int cmap_entries; |
| 1997 | unsigned int output_processing; /* Output processing option */ |
| 1998 | unsigned int data_encoding = E_NOTSET; /* Encoding libpng must produce */ |
| 1999 | |
| 2000 | /* Background information; the background color and the index of this color |
| 2001 | * in the color-map if it exists (else 256). |
| 2002 | */ |
| 2003 | unsigned int background_index = 256; |
| 2004 | png_uint_32 back_r, back_g, back_b; |
| 2005 | |
| 2006 | /* Flags to accumulate things that need to be done to the input. */ |
| 2007 | int expand_tRNS = 0; |
| 2008 | |
| 2009 | /* Exclude the NYI feature of compositing onto a color-mapped buffer; it is |
| 2010 | * very difficult to do, the results look awful, and it is difficult to see |
| 2011 | * what possible use it is because the application can't control the |
| 2012 | * color-map. |
| 2013 | */ |
| 2014 | if (((png_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0 || |
| 2015 | png_ptr->num_trans > 0) /* alpha in input */ && |
| 2016 | ((output_format & PNG_FORMAT_FLAG_ALPHA) == 0) /* no alpha in output */) |
| 2017 | { |
| 2018 | if (output_encoding == E_LINEAR) /* compose on black */ |
| 2019 | back_b = back_g = back_r = 0; |
| 2020 | |
| 2021 | else if (display->background == NULL /* no way to remove it */) |
| 2022 | png_error(png_ptr, |
| 2023 | "a background color must be supplied to remove alpha/transparency"); |
| 2024 | |
| 2025 | /* Get a copy of the background color (this avoids repeating the checks |
| 2026 | * below.) The encoding is 8-bit sRGB or 16-bit linear, depending on the |
| 2027 | * output format. |
| 2028 | */ |
| 2029 | else |
| 2030 | { |
| 2031 | back_g = display->background->green; |
| 2032 | if (output_format & PNG_FORMAT_FLAG_COLOR) |
| 2033 | { |
| 2034 | back_r = display->background->red; |
| 2035 | back_b = display->background->blue; |
| 2036 | } |
| 2037 | else |
| 2038 | back_b = back_r = back_g; |
| 2039 | } |
| 2040 | } |
| 2041 | |
| 2042 | else if (output_encoding == E_LINEAR) |
| 2043 | back_b = back_r = back_g = 65535; |
| 2044 | |
| 2045 | else |
| 2046 | back_b = back_r = back_g = 255; |
| 2047 | |
| 2048 | /* Decide what to do based on the PNG color type of the input data. The |
| 2049 | * utility function png_create_colormap_entry deals with most aspects of the |
| 2050 | * output transformations; this code works out how to produce bytes of |
| 2051 | * color-map entries from the original format. |
| 2052 | */ |
| 2053 | switch (png_ptr->color_type) |
| 2054 | { |
| 2055 | case PNG_COLOR_TYPE_GRAY: |
| 2056 | if (png_ptr->bit_depth <= 8) |
| 2057 | { |
| 2058 | /* There at most 256 colors in the output, regardless of |
| 2059 | * transparency. |
| 2060 | */ |
| 2061 | unsigned int step, i, val, trans = 256/*ignore*/, back_alpha = 0; |
| 2062 | |
| 2063 | cmap_entries = 1U << png_ptr->bit_depth; |
| 2064 | if (cmap_entries > image->colormap_entries) |
| 2065 | png_error(png_ptr, "gray[8] color-map: too few entries"); |
| 2066 | |
| 2067 | step = 255 / (cmap_entries - 1); |
| 2068 | output_processing = PNG_CMAP_NONE; |
| 2069 | |
| 2070 | /* If there is a tRNS chunk then this either selects a transparent |
| 2071 | * value or, if the output has no alpha, the background color. |
| 2072 | */ |
| 2073 | if (png_ptr->num_trans > 0) |
| 2074 | { |
| 2075 | trans = png_ptr->trans_color.gray; |
| 2076 | |
| 2077 | if ((output_format & PNG_FORMAT_FLAG_ALPHA) == 0) |
| 2078 | back_alpha = output_encoding == E_LINEAR ? 65535 : 255; |
| 2079 | } |
| 2080 | |
| 2081 | /* png_create_colormap_entry just takes an RGBA and writes the |
| 2082 | * corresponding color-map entry using the format from 'image', |
| 2083 | * including the required convertion to sRGB or linear as |
| 2084 | * appropriate. The input values are always either sRGB (if the |
| 2085 | * gamma correction flag is 0) or 0..255 scaled file encoded values |
| 2086 | * (if the function must gamma correct them). |
| 2087 | */ |
| 2088 | for (i=val=0; i<cmap_entries; ++i, val += step) |
| 2089 | { |
| 2090 | /* 'i' is a file value. While this will result in duplicated |
| 2091 | * entries for 8-bit non-sRGB encoded files it is necessary to |
| 2092 | * have non-gamma corrected values to do tRNS handling. |
| 2093 | */ |
| 2094 | if (i != trans) |
| 2095 | png_create_colormap_entry(display, i, val, val, val, 255, |
| 2096 | E_FILE/*8-bit with file gamma*/); |
| 2097 | |
| 2098 | /* Else this entry is transparent. The colors don't matter if |
| 2099 | * there is an alpha channel (back_alpha == 0), but it does no |
| 2100 | * harm to pass them in; the values are not set above so this |
| 2101 | * passes in white. |
| 2102 | * |
| 2103 | * NOTE: this preserves the full precision of the application |
| 2104 | * supplied background color when it is used. |
| 2105 | */ |
| 2106 | else |
| 2107 | png_create_colormap_entry(display, i, back_r, back_g, back_b, |
| 2108 | back_alpha, output_encoding); |
| 2109 | } |
| 2110 | |
| 2111 | /* We need libpng to preserve the original encoding. */ |
| 2112 | data_encoding = E_FILE; |
| 2113 | |
| 2114 | /* The rows from libpng, while technically gray values, are now also |
| 2115 | * color-map indicies; however, they may need to be expanded to 1 |
| 2116 | * byte per pixel. This is what png_set_packing does (i.e., it |
| 2117 | * unpacks the bit values into bytes.) |
| 2118 | */ |
| 2119 | if (png_ptr->bit_depth < 8) |
| 2120 | png_set_packing(png_ptr); |
| 2121 | } |
| 2122 | |
| 2123 | else /* bit depth is 16 */ |
| 2124 | { |
| 2125 | /* The 16-bit input values can be converted directly to 8-bit gamma |
| 2126 | * encoded values; however, if a tRNS chunk is present 257 color-map |
| 2127 | * entries are required. This means that the extra entry requires |
| 2128 | * special processing; add an alpha channel, sacrifice gray level |
| 2129 | * 254 and convert transparent (alpha==0) entries to that. |
| 2130 | * |
| 2131 | * Use libpng to chop the data to 8 bits. Convert it to sRGB at the |
| 2132 | * same time to minimize quality loss. If a tRNS chunk is present |
| 2133 | * this means libpng must handle it too; otherwise it is impossible |
| 2134 | * to do the exact match on the 16-bit value. |
| 2135 | * |
| 2136 | * If the output has no alpha channel *and* the background color is |
| 2137 | * gray then it is possible to let libpng handle the substitution by |
| 2138 | * ensuring that the corresponding gray level matches the background |
| 2139 | * color exactly. |
| 2140 | */ |
| 2141 | data_encoding = E_sRGB; |
| 2142 | |
| 2143 | if (PNG_GRAY_COLORMAP_ENTRIES > image->colormap_entries) |
| 2144 | png_error(png_ptr, "gray[16] color-map: too few entries"); |
| 2145 | |
| 2146 | cmap_entries = make_gray_colormap(display); |
| 2147 | |
| 2148 | if (png_ptr->num_trans > 0) |
| 2149 | { |
| 2150 | unsigned int back_alpha; |
| 2151 | |
| 2152 | if (output_format & PNG_FORMAT_FLAG_ALPHA) |
| 2153 | back_alpha = 0; |
| 2154 | |
| 2155 | else |
| 2156 | { |
| 2157 | if (back_r == back_g && back_g == back_b) |
| 2158 | { |
| 2159 | /* Background is gray; no special processing will be |
| 2160 | * required. |
| 2161 | */ |
| 2162 | png_color_16 c; |
| 2163 | png_uint_32 gray = back_g; |
| 2164 | |
| 2165 | if (output_encoding == E_LINEAR) |
| 2166 | { |
| 2167 | gray = PNG_sRGB_FROM_LINEAR(gray * 255); |
| 2168 | |
| 2169 | /* And make sure the corresponding palette entry |
| 2170 | * matches. |
| 2171 | */ |
| 2172 | png_create_colormap_entry(display, gray, back_g, back_g, |
| 2173 | back_g, 65535, E_LINEAR); |
| 2174 | } |
| 2175 | |
| 2176 | /* The background passed to libpng, however, must be the |
| 2177 | * sRGB value. |
| 2178 | */ |
| 2179 | c.index = 0; /*unused*/ |
| 2180 | c.gray = c.red = c.green = c.blue = (png_uint_16)gray; |
| 2181 | |
| 2182 | /* NOTE: does this work without expanding tRNS to alpha? |
| 2183 | * It should be the color->gray case below apparently |
| 2184 | * doesn't. |
| 2185 | */ |
| 2186 | png_set_background_fixed(png_ptr, &c, |
| 2187 | PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/, |
| 2188 | 0/*gamma: not used*/); |
| 2189 | |
| 2190 | output_processing = PNG_CMAP_NONE; |
| 2191 | break; |
| 2192 | } |
| 2193 | |
| 2194 | back_alpha = output_encoding == E_LINEAR ? 65535 : 255; |
| 2195 | } |
| 2196 | |
| 2197 | /* output_processing means that the libpng-processed row will be |
| 2198 | * 8-bit GA and it has to be processing to single byte color-map |
| 2199 | * values. Entry 254 is replaced by either a completely |
| 2200 | * transparent entry or by the background color at full |
| 2201 | * precision (and the background color is not a simple gray leve |
| 2202 | * in this case.) |
| 2203 | */ |
| 2204 | expand_tRNS = 1; |
| 2205 | output_processing = PNG_CMAP_TRANS; |
| 2206 | background_index = 254; |
| 2207 | |
| 2208 | /* And set (overwrite) color-map entry 254 to the actual |
| 2209 | * background color at full precision. |
| 2210 | */ |
| 2211 | png_create_colormap_entry(display, 254, back_r, back_g, back_b, |
| 2212 | back_alpha, output_encoding); |
| 2213 | } |
| 2214 | |
| 2215 | else |
| 2216 | output_processing = PNG_CMAP_NONE; |
| 2217 | } |
| 2218 | break; |
| 2219 | |
| 2220 | case PNG_COLOR_TYPE_GRAY_ALPHA: |
| 2221 | /* 8-bit or 16-bit PNG with two channels - gray and alpha. A minimum |
| 2222 | * of 65536 combinations. If, however, the alpha channel is to be |
| 2223 | * removed there are only 256 possibilities if the background is gray. |
| 2224 | * (Otherwise there is a subset of the 65536 possibilities defined by |
| 2225 | * the triangle between black, white and the background color.) |
| 2226 | * |
| 2227 | * Reduce 16-bit files to 8-bit and sRGB encode the result. No need to |
| 2228 | * worry about tRNS matching - tRNS is ignored if there is an alpha |
| 2229 | * channel. |
| 2230 | */ |
| 2231 | data_encoding = E_sRGB; |
| 2232 | |
| 2233 | if (output_format & PNG_FORMAT_FLAG_ALPHA) |
| 2234 | { |
| 2235 | if (PNG_GA_COLORMAP_ENTRIES > image->colormap_entries) |
| 2236 | png_error(png_ptr, "gray+alpha color-map: too few entries"); |
| 2237 | |
| 2238 | cmap_entries = make_ga_colormap(display); |
| 2239 | |
| 2240 | background_index = PNG_CMAP_GA_BACKGROUND; |
| 2241 | output_processing = PNG_CMAP_GA; |
| 2242 | } |
| 2243 | |
| 2244 | else /* alpha is removed */ |
| 2245 | { |
| 2246 | /* Alpha must be removed as the PNG data is processed when the |
| 2247 | * background is a color because the G and A channels are |
| 2248 | * independent and the vector addition (non-parallel vectors) is a |
| 2249 | * 2-D problem. |
| 2250 | * |
| 2251 | * This can be reduced to the same algorithm as above by making a |
| 2252 | * colormap containing gray levels (for the opaque grays), a |
| 2253 | * background entry (for a transparent pixel) and a set of four six |
| 2254 | * level color values, one set for each intermediate alpha value. |
| 2255 | * See the comments in make_ga_colormap for how this works in the |
| 2256 | * per-pixel processing. |
| 2257 | * |
| 2258 | * If the background is gray, however, we only need a 256 entry gray |
| 2259 | * level color map. It is sufficient to make the entry generated |
| 2260 | * for the background color be exactly the color specified. |
| 2261 | */ |
| 2262 | if ((output_format & PNG_FORMAT_FLAG_COLOR) == 0 || |
| 2263 | (back_r == back_g && back_g == back_b)) |
| 2264 | { |
| 2265 | /* Background is gray; no special processing will be required. */ |
| 2266 | png_color_16 c; |
| 2267 | png_uint_32 gray = back_g; |
| 2268 | |
| 2269 | if (PNG_GRAY_COLORMAP_ENTRIES > image->colormap_entries) |
| 2270 | png_error(png_ptr, "gray-alpha color-map: too few entries"); |
| 2271 | |
| 2272 | cmap_entries = make_gray_colormap(display); |
| 2273 | |
| 2274 | if (output_encoding == E_LINEAR) |
| 2275 | { |
| 2276 | gray = PNG_sRGB_FROM_LINEAR(gray * 255); |
| 2277 | |
| 2278 | /* And make sure the corresponding palette entry matches. */ |
| 2279 | png_create_colormap_entry(display, gray, back_g, back_g, |
| 2280 | back_g, 65535, E_LINEAR); |
| 2281 | } |
| 2282 | |
| 2283 | /* The background passed to libpng, however, must be the sRGB |
| 2284 | * value. |
| 2285 | */ |
| 2286 | c.index = 0; /*unused*/ |
| 2287 | c.gray = c.red = c.green = c.blue = (png_uint_16)gray; |
| 2288 | |
| 2289 | png_set_background_fixed(png_ptr, &c, |
| 2290 | PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/, |
| 2291 | 0/*gamma: not used*/); |
| 2292 | |
| 2293 | output_processing = PNG_CMAP_NONE; |
| 2294 | } |
| 2295 | |
| 2296 | else |
| 2297 | { |
| 2298 | png_uint_32 i, a; |
| 2299 | |
| 2300 | /* This is the same as png_make_ga_colormap, above, except that |
| 2301 | * the entries are all opaque. |
| 2302 | */ |
| 2303 | if (PNG_GA_COLORMAP_ENTRIES > image->colormap_entries) |
| 2304 | png_error(png_ptr, "ga-alpha color-map: too few entries"); |
| 2305 | |
| 2306 | i = 0; |
| 2307 | while (i < 231) |
| 2308 | { |
| 2309 | png_uint_32 gray = (i * 256 + 115) / 231; |
| 2310 | png_create_colormap_entry(display, i++, gray, gray, gray, |
| 2311 | 255, E_sRGB); |
| 2312 | } |
| 2313 | |
| 2314 | /* NOTE: this preserves the full precision of the application |
| 2315 | * background color. |
| 2316 | */ |
| 2317 | background_index = i; |
| 2318 | png_create_colormap_entry(display, i++, back_r, back_g, back_b, |
| 2319 | output_encoding == E_LINEAR ? 65535U : 255U, output_encoding); |
| 2320 | |
| 2321 | /* For non-opaque input composite on the sRGB background - this |
| 2322 | * requires inverting the encoding for each component. The input |
| 2323 | * is still converted to the sRGB encoding because this is a |
| 2324 | * reasonable approximate to the logarithmic curve of human |
| 2325 | * visual sensitivity, at least over the narrow range which PNG |
| 2326 | * represents. Consequently 'G' is always sRGB encoded, while |
| 2327 | * 'A' is linear. We need the linear background colors. |
| 2328 | */ |
| 2329 | if (output_encoding == E_sRGB) /* else already linear */ |
| 2330 | { |
| 2331 | /* This may produce a value not exactly matching the |
| 2332 | * background, but that's ok because these numbers are only |
| 2333 | * used when alpha != 0 |
| 2334 | */ |
| 2335 | back_r = png_sRGB_table[back_r]; |
| 2336 | back_g = png_sRGB_table[back_g]; |
| 2337 | back_b = png_sRGB_table[back_b]; |
| 2338 | } |
| 2339 | |
| 2340 | for (a=1; a<5; ++a) |
| 2341 | { |
| 2342 | unsigned int g; |
| 2343 | |
| 2344 | /* PNG_sRGB_FROM_LINEAR expects a 16-bit linear value scaled |
| 2345 | * by an 8-bit alpha value (0..255). |
| 2346 | */ |
| 2347 | png_uint_32 alpha = 51 * a; |
| 2348 | png_uint_32 back_rx = (255-alpha) * back_r; |
| 2349 | png_uint_32 back_gx = (255-alpha) * back_g; |
| 2350 | png_uint_32 back_bx = (255-alpha) * back_b; |
| 2351 | |
| 2352 | for (g=0; g<6; ++g) |
| 2353 | { |
| 2354 | png_uint_32 gray = png_sRGB_table[g*51] * alpha; |
| 2355 | |
| 2356 | png_create_colormap_entry(display, i++, |
| 2357 | PNG_sRGB_FROM_LINEAR(gray + back_rx), |
| 2358 | PNG_sRGB_FROM_LINEAR(gray + back_gx), |
| 2359 | PNG_sRGB_FROM_LINEAR(gray + back_bx), 255, E_sRGB); |
| 2360 | } |
| 2361 | } |
| 2362 | |
| 2363 | cmap_entries = i; |
| 2364 | output_processing = PNG_CMAP_GA; |
| 2365 | } |
| 2366 | } |
| 2367 | break; |
| 2368 | |
| 2369 | case PNG_COLOR_TYPE_RGB: |
| 2370 | case PNG_COLOR_TYPE_RGB_ALPHA: |
| 2371 | /* Exclude the case where the output is gray; we can always handle this |
| 2372 | * with the cases above. |
| 2373 | */ |
| 2374 | if ((output_format & PNG_FORMAT_FLAG_COLOR) == 0) |
| 2375 | { |
| 2376 | /* The color-map will be grayscale, so we may as well convert the |
| 2377 | * input RGB values to a simple grayscale and use the grayscale |
| 2378 | * code above. |
| 2379 | * |
| 2380 | * NOTE: calling this apparently damages the recognition of the |
| 2381 | * transparent color in background color handling; call |
| 2382 | * png_set_tRNS_to_alpha before png_set_background_fixed. |
| 2383 | */ |
| 2384 | png_set_rgb_to_gray_fixed(png_ptr, PNG_ERROR_ACTION_NONE, -1, |
| 2385 | -1); |
| 2386 | data_encoding = E_sRGB; |
| 2387 | |
| 2388 | /* The output will now be one or two 8-bit gray or gray+alpha |
| 2389 | * channels. The more complex case arises when the input has alpha. |
| 2390 | */ |
| 2391 | if ((png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA || |
| 2392 | png_ptr->num_trans > 0) && |
| 2393 | (output_format & PNG_FORMAT_FLAG_ALPHA) != 0) |
| 2394 | { |
| 2395 | /* Both input and output have an alpha channel, so no background |
| 2396 | * processing is required; just map the GA bytes to the right |
| 2397 | * color-map entry. |
| 2398 | */ |
| 2399 | expand_tRNS = 1; |
| 2400 | |
| 2401 | if (PNG_GA_COLORMAP_ENTRIES > image->colormap_entries) |
| 2402 | png_error(png_ptr, "rgb[ga] color-map: too few entries"); |
| 2403 | |
| 2404 | cmap_entries = make_ga_colormap(display); |
| 2405 | background_index = PNG_CMAP_GA_BACKGROUND; |
| 2406 | output_processing = PNG_CMAP_GA; |
| 2407 | } |
| 2408 | |
| 2409 | else |
| 2410 | { |
| 2411 | /* Either the input or the output has no alpha channel, so there |
| 2412 | * will be no non-opaque pixels in the color-map; it will just be |
| 2413 | * grayscale. |
| 2414 | */ |
| 2415 | if (PNG_GRAY_COLORMAP_ENTRIES > image->colormap_entries) |
| 2416 | png_error(png_ptr, "rgb[gray] color-map: too few entries"); |
| 2417 | |
| 2418 | cmap_entries = make_gray_colormap(display); |
| 2419 | |
| 2420 | /* But if the input has alpha or transparency it must be removed |
| 2421 | */ |
| 2422 | if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA || |
| 2423 | png_ptr->num_trans > 0) |
| 2424 | { |
| 2425 | png_color_16 c; |
| 2426 | png_uint_32 gray = back_g; |
| 2427 | |
| 2428 | /* We need to ensure that the application background exists in |
| 2429 | * the colormap and that completely transparent pixels map to |
| 2430 | * it. Achieve this simply by ensuring that the entry |
| 2431 | * selected for the background really is the background color. |
| 2432 | */ |
| 2433 | if (output_encoding == E_LINEAR) |
| 2434 | { |
| 2435 | gray = PNG_sRGB_FROM_LINEAR(gray * 255); |
| 2436 | |
| 2437 | /* And make sure the corresponding palette entry matches. |
| 2438 | */ |
| 2439 | png_create_colormap_entry(display, gray, back_g, back_g, |
| 2440 | back_g, 65535, E_LINEAR); |
| 2441 | } |
| 2442 | |
| 2443 | /* The background passed to libpng, however, must be the sRGB |
| 2444 | * value. |
| 2445 | */ |
| 2446 | c.index = 0; /*unused*/ |
| 2447 | c.gray = c.red = c.green = c.blue = (png_uint_16)gray; |
| 2448 | |
| 2449 | /* NOTE: the following is apparently a bug in libpng. Without |
| 2450 | * it the transparent color recognition in |
| 2451 | * png_set_background_fixed seems to go wrong. |
| 2452 | */ |
| 2453 | expand_tRNS = 1; |
| 2454 | png_set_background_fixed(png_ptr, &c, |
| 2455 | PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/, |
| 2456 | 0/*gamma: not used*/); |
| 2457 | } |
| 2458 | |
| 2459 | output_processing = PNG_CMAP_NONE; |
| 2460 | } |
| 2461 | } |
| 2462 | |
| 2463 | else /* output is color */ |
| 2464 | { |
| 2465 | /* We could use png_quantize here so long as there is no transparent |
| 2466 | * color or alpha; png_quantize ignores alpha. Easier overall just |
| 2467 | * to do it once and using PNG_DIV51 on the 6x6x6 reduced RGB cube. |
| 2468 | * Consequently we always want libpng to produce sRGB data. |
| 2469 | */ |
| 2470 | data_encoding = E_sRGB; |
| 2471 | |
| 2472 | /* Is there any transparency or alpha? */ |
| 2473 | if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA || |
| 2474 | png_ptr->num_trans > 0) |
| 2475 | { |
| 2476 | /* Is there alpha in the output too? If so all four channels are |
| 2477 | * processed into a special RGB cube with alpha support. |
| 2478 | */ |
| 2479 | if (output_format & PNG_FORMAT_FLAG_ALPHA) |
| 2480 | { |
| 2481 | png_uint_32 r; |
| 2482 | |
| 2483 | if (PNG_RGB_COLORMAP_ENTRIES+1+27 > image->colormap_entries) |
| 2484 | png_error(png_ptr, "rgb+alpha color-map: too few entries"); |
| 2485 | |
| 2486 | cmap_entries = make_rgb_colormap(display); |
| 2487 | |
| 2488 | /* Add a transparent entry. */ |
| 2489 | png_create_colormap_entry(display, cmap_entries, 255, 255, |
| 2490 | 255, 0, E_sRGB); |
| 2491 | |
| 2492 | /* This is stored as the background index for the processing |
| 2493 | * algorithm. |
| 2494 | */ |
| 2495 | background_index = cmap_entries++; |
| 2496 | |
| 2497 | /* Add 27 r,g,b entries each with alpha 0.5. */ |
| 2498 | for (r=0; r<256; r = (r << 1) | 0x7f) |
| 2499 | { |
| 2500 | png_uint_32 g; |
| 2501 | |
| 2502 | for (g=0; g<256; g = (g << 1) | 0x7f) |
| 2503 | { |
| 2504 | png_uint_32 b; |
| 2505 | |
| 2506 | /* This generates components with the values 0, 127 and |
| 2507 | * 255 |
| 2508 | */ |
| 2509 | for (b=0; b<256; b = (b << 1) | 0x7f) |
| 2510 | png_create_colormap_entry(display, cmap_entries++, |
| 2511 | r, g, b, 128, E_sRGB); |
| 2512 | } |
| 2513 | } |
| 2514 | |
| 2515 | expand_tRNS = 1; |
| 2516 | output_processing = PNG_CMAP_RGB_ALPHA; |
| 2517 | } |
| 2518 | |
| 2519 | else |
| 2520 | { |
| 2521 | /* Alpha/transparency must be removed. The background must |
| 2522 | * exist in the color map (achieved by setting adding it after |
| 2523 | * the 666 color-map). If the standard processing code will |
| 2524 | * pick up this entry automatically that's all that is |
| 2525 | * required; libpng can be called to do the background |
| 2526 | * processing. |
| 2527 | */ |
| 2528 | unsigned int sample_size = |
| 2529 | PNG_IMAGE_SAMPLE_SIZE(output_format); |
| 2530 | png_uint_32 r, g, b; /* sRGB background */ |
| 2531 | |
| 2532 | if (PNG_RGB_COLORMAP_ENTRIES+1+27 > image->colormap_entries) |
| 2533 | png_error(png_ptr, "rgb-alpha color-map: too few entries"); |
| 2534 | |
| 2535 | cmap_entries = make_rgb_colormap(display); |
| 2536 | |
| 2537 | png_create_colormap_entry(display, cmap_entries, back_r, |
| 2538 | back_g, back_b, 0/*unused*/, output_encoding); |
| 2539 | |
| 2540 | if (output_encoding == E_LINEAR) |
| 2541 | { |
| 2542 | r = PNG_sRGB_FROM_LINEAR(back_r * 255); |
| 2543 | g = PNG_sRGB_FROM_LINEAR(back_g * 255); |
| 2544 | b = PNG_sRGB_FROM_LINEAR(back_b * 255); |
| 2545 | } |
| 2546 | |
| 2547 | else |
| 2548 | { |
| 2549 | r = back_r; |
| 2550 | g = back_g; |
| 2551 | b = back_g; |
| 2552 | } |
| 2553 | |
| 2554 | /* Compare the newly-created color-map entry with the one the |
| 2555 | * PNG_CMAP_RGB algorithm will use. If the two entries don't |
| 2556 | * match, add the new one and set this as the background index. |
| 2557 | */ |
| 2558 | if (memcmp((png_const_bytep)display->colormap + |
| 2559 | sample_size * cmap_entries, |
| 2560 | (png_const_bytep)display->colormap + |
| 2561 | sample_size * PNG_RGB_INDEX(r,g,b), |
| 2562 | sample_size) != 0) |
| 2563 | { |
| 2564 | /* The background color must be added. */ |
| 2565 | background_index = cmap_entries++; |
| 2566 | |
| 2567 | /* Add 27 r,g,b entries each with created by composing with |
| 2568 | * the background at alpha 0.5. |
| 2569 | */ |
| 2570 | for (r=0; r<256; r = (r << 1) | 0x7f) |
| 2571 | { |
| 2572 | for (g=0; g<256; g = (g << 1) | 0x7f) |
| 2573 | { |
| 2574 | /* This generates components with the values 0, 127 |
| 2575 | * and 255 |
| 2576 | */ |
| 2577 | for (b=0; b<256; b = (b << 1) | 0x7f) |
| 2578 | png_create_colormap_entry(display, cmap_entries++, |
| 2579 | png_colormap_compose(display, r, E_sRGB, 128, |
| 2580 | back_r, output_encoding), |
| 2581 | png_colormap_compose(display, g, E_sRGB, 128, |
| 2582 | back_g, output_encoding), |
| 2583 | png_colormap_compose(display, b, E_sRGB, 128, |
| 2584 | back_b, output_encoding), |
| 2585 | 0/*unused*/, output_encoding); |
| 2586 | } |
| 2587 | } |
| 2588 | |
| 2589 | expand_tRNS = 1; |
| 2590 | output_processing = PNG_CMAP_RGB_ALPHA; |
| 2591 | } |
| 2592 | |
| 2593 | else /* background color is in the standard color-map */ |
| 2594 | { |
| 2595 | png_color_16 c; |
| 2596 | |
| 2597 | c.index = 0; /*unused*/ |
| 2598 | c.red = (png_uint_16)back_r; |
| 2599 | c.gray = c.green = (png_uint_16)back_g; |
| 2600 | c.blue = (png_uint_16)back_b; |
| 2601 | |
| 2602 | png_set_background_fixed(png_ptr, &c, |
| 2603 | PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/, |
| 2604 | 0/*gamma: not used*/); |
| 2605 | |
| 2606 | output_processing = PNG_CMAP_RGB; |
| 2607 | } |
| 2608 | } |
| 2609 | } |
| 2610 | |
| 2611 | else /* no alpha or transparency in the input */ |
| 2612 | { |
| 2613 | /* Alpha in the output is irrelevant, simply map the opaque input |
| 2614 | * pixels to the 6x6x6 color-map. |
| 2615 | */ |
| 2616 | if (PNG_RGB_COLORMAP_ENTRIES > image->colormap_entries) |
| 2617 | png_error(png_ptr, "rgb color-map: too few entries"); |
| 2618 | |
| 2619 | cmap_entries = make_rgb_colormap(display); |
| 2620 | output_processing = PNG_CMAP_RGB; |
| 2621 | } |
| 2622 | } |
| 2623 | break; |
| 2624 | |
| 2625 | case PNG_COLOR_TYPE_PALETTE: |
| 2626 | /* It's already got a color-map. It may be necessary to eliminate the |
| 2627 | * tRNS entries though. |
| 2628 | */ |
| 2629 | { |
| 2630 | unsigned int num_trans = png_ptr->num_trans; |
| 2631 | png_const_bytep trans = num_trans > 0 ? png_ptr->trans_alpha : NULL; |
| 2632 | png_const_colorp colormap = png_ptr->palette; |
| 2633 | const int do_background = trans != NULL && |
| 2634 | (output_format & PNG_FORMAT_FLAG_ALPHA) == 0; |
| 2635 | unsigned int i; |
| 2636 | |
| 2637 | /* Just in case: */ |
| 2638 | if (trans == NULL) |
| 2639 | num_trans = 0; |
| 2640 | |
| 2641 | output_processing = PNG_CMAP_NONE; |
| 2642 | data_encoding = E_FILE; /* Don't change from color-map indicies */ |
| 2643 | cmap_entries = png_ptr->num_palette; |
| 2644 | if (cmap_entries > 256) |
| 2645 | cmap_entries = 256; |
| 2646 | |
| 2647 | if (cmap_entries > image->colormap_entries) |
| 2648 | png_error(png_ptr, "palette color-map: too few entries"); |
| 2649 | |
| 2650 | for (i=0; i < cmap_entries; ++i) |
| 2651 | { |
| 2652 | if (do_background && i < num_trans && trans[i] < 255) |
| 2653 | { |
| 2654 | if (trans[i] == 0) |
| 2655 | png_create_colormap_entry(display, i, back_r, back_g, |
| 2656 | back_b, 0, output_encoding); |
| 2657 | |
| 2658 | else |
| 2659 | { |
| 2660 | /* Must compose the PNG file color in the color-map entry |
| 2661 | * on the sRGB color in 'back'. |
| 2662 | */ |
| 2663 | png_create_colormap_entry(display, i, |
| 2664 | png_colormap_compose(display, colormap[i].red, E_FILE, |
| 2665 | trans[i], back_r, output_encoding), |
| 2666 | png_colormap_compose(display, colormap[i].green, E_FILE, |
| 2667 | trans[i], back_g, output_encoding), |
| 2668 | png_colormap_compose(display, colormap[i].blue, E_FILE, |
| 2669 | trans[i], back_b, output_encoding), |
| 2670 | output_encoding == E_LINEAR ? trans[i] * 257U : |
| 2671 | trans[i], |
| 2672 | output_encoding); |
| 2673 | } |
| 2674 | } |
| 2675 | |
| 2676 | else |
| 2677 | png_create_colormap_entry(display, i, colormap[i].red, |
| 2678 | colormap[i].green, colormap[i].blue, |
| 2679 | i < num_trans ? trans[i] : 255U, E_FILE/*8-bit*/); |
| 2680 | } |
| 2681 | |
| 2682 | /* The PNG data may have indicies packed in fewer than 8 bits, it |
| 2683 | * must be expanded if so. |
| 2684 | */ |
| 2685 | if (png_ptr->bit_depth < 8) |
| 2686 | png_set_packing(png_ptr); |
| 2687 | } |
| 2688 | break; |
| 2689 | |
| 2690 | default: |
| 2691 | png_error(png_ptr, "invalid PNG color type"); |
| 2692 | /*NOT REACHED*/ |
| 2693 | break; |
| 2694 | } |
| 2695 | |
| 2696 | /* Now deal with the output processing */ |
| 2697 | if (expand_tRNS && png_ptr->num_trans > 0 && |
| 2698 | (png_ptr->color_type & PNG_COLOR_MASK_ALPHA) == 0) |
| 2699 | png_set_tRNS_to_alpha(png_ptr); |
| 2700 | |
| 2701 | switch (data_encoding) |
| 2702 | { |
| 2703 | default: |
| 2704 | png_error(png_ptr, "bad data option (internal error)"); |
| 2705 | break; |
| 2706 | |
| 2707 | case E_FILE: |
| 2708 | /* Make no changes */ |
| 2709 | break; |
| 2710 | |
| 2711 | case E_sRGB: |
| 2712 | /* Change to 8-bit sRGB */ |
| 2713 | png_set_alpha_mode_fixed(png_ptr, PNG_ALPHA_PNG, PNG_GAMMA_sRGB); |
| 2714 | if (png_ptr->bit_depth > 8) |
| 2715 | png_set_scale_16(png_ptr); |
| 2716 | break; |
| 2717 | } |
| 2718 | |
| 2719 | if (cmap_entries > 256 || cmap_entries > image->colormap_entries) |
| 2720 | png_error(png_ptr, "color map overflow (BAD internal error)"); |
| 2721 | |
| 2722 | image->colormap_entries = cmap_entries; |
| 2723 | |
| 2724 | /* Double check using the recorded background index */ |
| 2725 | switch (output_processing) |
| 2726 | { |
| 2727 | case PNG_CMAP_NONE: |
| 2728 | if (background_index != PNG_CMAP_NONE_BACKGROUND) |
| 2729 | goto bad_background; |
| 2730 | break; |
| 2731 | |
| 2732 | case PNG_CMAP_GA: |
| 2733 | if (background_index != PNG_CMAP_GA_BACKGROUND) |
| 2734 | goto bad_background; |
| 2735 | break; |
| 2736 | |
| 2737 | case PNG_CMAP_TRANS: |
| 2738 | if (background_index >= cmap_entries || |
| 2739 | background_index != PNG_CMAP_TRANS_BACKGROUND) |
| 2740 | goto bad_background; |
| 2741 | break; |
| 2742 | |
| 2743 | case PNG_CMAP_RGB: |
| 2744 | if (background_index != PNG_CMAP_RGB_BACKGROUND) |
| 2745 | goto bad_background; |
| 2746 | break; |
| 2747 | |
| 2748 | case PNG_CMAP_RGB_ALPHA: |
| 2749 | if (background_index != PNG_CMAP_RGB_ALPHA_BACKGROUND) |
| 2750 | goto bad_background; |
| 2751 | break; |
| 2752 | |
| 2753 | default: |
| 2754 | png_error(png_ptr, "bad processing option (internal error)"); |
| 2755 | |
| 2756 | bad_background: |
| 2757 | png_error(png_ptr, "bad background index (internal error)"); |
| 2758 | } |
| 2759 | |
| 2760 | display->colormap_processing = output_processing; |
| 2761 | |
| 2762 | return 1/*ok*/; |
| 2763 | } |
| 2764 | |
| 2765 | /* The final part of the color-map read called from png_image_finish_read. */ |
| 2766 | static int |
| 2767 | png_image_read_and_map(png_voidp argument) |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 2768 | { |
John Bowler | 4fa96a4 | 2011-11-16 16:39:16 -0600 | [diff] [blame] | 2769 | png_image_read_control *display = png_voidcast(png_image_read_control*, |
| 2770 | argument); |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 2771 | png_imagep image = display->image; |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 2772 | png_structrp png_ptr = image->opaque->png_ptr; |
John Bowler | 4f67e40 | 2011-12-28 08:43:37 -0600 | [diff] [blame] | 2773 | int passes; |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 2774 | |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame^] | 2775 | /* Called when the libpng data must be transformed into the color-mapped |
| 2776 | * form. There is a local row buffer in display->local and this routine must |
| 2777 | * do the interlace handling. |
| 2778 | */ |
| 2779 | switch (png_ptr->interlaced) |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 2780 | { |
| 2781 | case PNG_INTERLACE_NONE: |
| 2782 | passes = 1; |
| 2783 | break; |
| 2784 | |
| 2785 | case PNG_INTERLACE_ADAM7: |
| 2786 | passes = PNG_INTERLACE_ADAM7_PASSES; |
| 2787 | break; |
| 2788 | |
| 2789 | default: |
John Bowler | 4f67e40 | 2011-12-28 08:43:37 -0600 | [diff] [blame] | 2790 | passes = 0; |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 2791 | png_error(png_ptr, "unknown interlace type"); |
| 2792 | } |
| 2793 | |
| 2794 | { |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame^] | 2795 | png_uint_32 height = image->height; |
| 2796 | png_uint_32 width = image->width; |
| 2797 | int proc = display->colormap_processing; |
| 2798 | png_bytep first_row = display->first_row; |
| 2799 | ptrdiff_t step_row = display->row_bytes; |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 2800 | int pass; |
| 2801 | |
| 2802 | for (pass = 0; pass < passes; ++pass) |
| 2803 | { |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 2804 | unsigned int startx, stepx, stepy; |
| 2805 | png_uint_32 y; |
| 2806 | |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame^] | 2807 | if (png_ptr->interlaced == PNG_INTERLACE_ADAM7) |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 2808 | { |
| 2809 | /* The row may be empty for a short image: */ |
| 2810 | if (PNG_PASS_COLS(width, pass) == 0) |
| 2811 | continue; |
| 2812 | |
| 2813 | startx = PNG_PASS_START_COL(pass); |
| 2814 | stepx = PNG_PASS_COL_OFFSET(pass); |
| 2815 | y = PNG_PASS_START_ROW(pass); |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 2816 | stepy = PNG_PASS_ROW_OFFSET(pass); |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 2817 | } |
| 2818 | |
| 2819 | else |
| 2820 | { |
| 2821 | y = 0; |
| 2822 | startx = 0; |
| 2823 | stepx = stepy = 1; |
| 2824 | } |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 2825 | |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 2826 | for (; y<height; y += stepy) |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame^] | 2827 | { |
| 2828 | png_bytep inrow = display->local_row; |
| 2829 | png_bytep outrow = first_row + y * step_row; |
| 2830 | png_const_bytep end_row = outrow + width; |
| 2831 | |
| 2832 | /* Read read the libpng data into the temporary buffer. */ |
| 2833 | png_read_row(png_ptr, inrow, NULL); |
| 2834 | |
| 2835 | /* Now process the row according to the processing option, note |
| 2836 | * that the caller verifies that the format of the libpng output |
| 2837 | * data is as required. |
| 2838 | */ |
| 2839 | outrow += startx; |
| 2840 | switch (proc) |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 2841 | { |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame^] | 2842 | case PNG_CMAP_GA: |
| 2843 | for (; outrow < end_row; outrow += stepx) |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 2844 | { |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame^] | 2845 | /* The data is always in the PNG order */ |
| 2846 | unsigned int gray = *inrow++; |
| 2847 | unsigned int alpha = *inrow++; |
| 2848 | unsigned int entry; |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 2849 | |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame^] | 2850 | /* NOTE: this code is copied as a comment in |
| 2851 | * make_ga_colormap above. Please update the |
| 2852 | * comment if you change this code! |
| 2853 | */ |
| 2854 | if (alpha > 229) /* opaque */ |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 2855 | { |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame^] | 2856 | entry = (231 * gray + 128) >> 8; |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 2857 | } |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame^] | 2858 | else if (alpha < 26) /* transparent */ |
| 2859 | { |
| 2860 | entry = 231; |
| 2861 | } |
| 2862 | else /* partially opaque */ |
| 2863 | { |
| 2864 | entry = 226 + 6 * PNG_DIV51(alpha) + PNG_DIV51(gray); |
| 2865 | } |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 2866 | |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame^] | 2867 | *outrow = (png_byte)entry; |
| 2868 | } |
| 2869 | break; |
| 2870 | |
| 2871 | case PNG_CMAP_TRANS: |
| 2872 | for (; outrow < end_row; outrow += stepx) |
| 2873 | { |
| 2874 | png_byte gray = *inrow++; |
| 2875 | png_byte alpha = *inrow++; |
| 2876 | |
| 2877 | if (alpha == 0) |
| 2878 | *outrow = PNG_CMAP_TRANS_BACKGROUND; |
| 2879 | |
| 2880 | else if (gray != PNG_CMAP_TRANS_BACKGROUND) |
| 2881 | *outrow = gray; |
| 2882 | |
| 2883 | else |
| 2884 | *outrow = (png_byte)(PNG_CMAP_TRANS_BACKGROUND+1); |
| 2885 | } |
| 2886 | break; |
| 2887 | |
| 2888 | case PNG_CMAP_RGB: |
| 2889 | for (; outrow < end_row; outrow += stepx) |
| 2890 | { |
| 2891 | *outrow = PNG_RGB_INDEX(inrow[0], inrow[1], inrow[2]); |
| 2892 | inrow += 3; |
| 2893 | } |
| 2894 | break; |
| 2895 | |
| 2896 | case PNG_CMAP_RGB_ALPHA: |
| 2897 | for (; outrow < end_row; outrow += stepx) |
| 2898 | { |
| 2899 | unsigned int alpha = inrow[3]; |
| 2900 | |
| 2901 | /* Because the alpha entries only hold alpha==0.5 values |
| 2902 | * split the processing at alpha==0.25 (64) and 0.75 |
| 2903 | * (196). |
| 2904 | */ |
| 2905 | |
| 2906 | if (alpha >= 196) |
| 2907 | *outrow = PNG_RGB_INDEX(inrow[0], inrow[1], |
| 2908 | inrow[2]); |
| 2909 | |
| 2910 | else if (alpha < 64) |
| 2911 | *outrow = PNG_CMAP_RGB_ALPHA_BACKGROUND; |
| 2912 | |
| 2913 | else |
| 2914 | { |
| 2915 | /* Likewise there are three entries for each of r, g |
| 2916 | * and b. We could select the entry by popcount on |
| 2917 | * the top two bits on those architectures that |
| 2918 | * support it, this is what the code below does, |
| 2919 | * crudely. |
| 2920 | */ |
| 2921 | unsigned int back_i = PNG_CMAP_RGB_ALPHA_BACKGROUND+1; |
| 2922 | |
| 2923 | /* Here are how the values map: |
| 2924 | * |
| 2925 | * 0x00 .. 0x3f -> 0 |
| 2926 | * 0x40 .. 0xbf -> 1 |
| 2927 | * 0xc0 .. 0xff -> 2 |
| 2928 | * |
| 2929 | * So, as above with the explicit alpha checks, the |
| 2930 | * breakpoints are at 64 and 196. |
| 2931 | */ |
| 2932 | if (inrow[0] & 0x80) back_i += 9; /* red */ |
| 2933 | if (inrow[0] & 0x40) back_i += 9; |
| 2934 | if (inrow[0] & 0x80) back_i += 3; /* green */ |
| 2935 | if (inrow[0] & 0x40) back_i += 3; |
| 2936 | if (inrow[0] & 0x80) back_i += 1; /* blue */ |
| 2937 | if (inrow[0] & 0x40) back_i += 1; |
| 2938 | |
| 2939 | *outrow = (png_byte)back_i; |
| 2940 | } |
| 2941 | |
| 2942 | inrow += 4; |
| 2943 | } |
| 2944 | break; |
| 2945 | |
| 2946 | default: |
| 2947 | break; |
| 2948 | } |
| 2949 | } |
| 2950 | } |
| 2951 | } |
| 2952 | |
| 2953 | return 1; |
| 2954 | } |
| 2955 | |
| 2956 | static int |
| 2957 | png_image_read_colormapped(png_voidp argument) |
| 2958 | { |
| 2959 | png_image_read_control *display = png_voidcast(png_image_read_control*, |
| 2960 | argument); |
| 2961 | png_imagep image = display->image; |
| 2962 | png_controlp control = image->opaque; |
| 2963 | png_structrp png_ptr = control->png_ptr; |
| 2964 | png_inforp info_ptr = control->info_ptr; |
| 2965 | |
| 2966 | int passes = 0; /* As a flag */ |
| 2967 | |
| 2968 | PNG_SKIP_CHUNKS(png_ptr); |
| 2969 | |
| 2970 | /* Update the 'info' structure and make sure the result is as required; first |
| 2971 | * make sure to turn on the interlace handling if it will be required |
| 2972 | * (because it can't be turned on *after* the call to png_read_update_info!) |
| 2973 | */ |
| 2974 | if (display->colormap_processing == PNG_CMAP_NONE) |
| 2975 | passes = png_set_interlace_handling(png_ptr); |
| 2976 | |
| 2977 | png_read_update_info(png_ptr, info_ptr); |
| 2978 | |
| 2979 | /* The expected output can be deduced from the colormap_processing option. */ |
| 2980 | switch (display->colormap_processing) |
| 2981 | { |
| 2982 | case PNG_CMAP_NONE: |
| 2983 | /* Output must be one channel and one byte per pixel, the output |
| 2984 | * encoding can be anything. |
| 2985 | */ |
| 2986 | if ((info_ptr->color_type == PNG_COLOR_TYPE_PALETTE || |
| 2987 | info_ptr->color_type == PNG_COLOR_TYPE_GRAY) && |
| 2988 | info_ptr->bit_depth == 8) |
| 2989 | break; |
| 2990 | |
| 2991 | goto bad_output; |
| 2992 | |
| 2993 | case PNG_CMAP_TRANS: |
| 2994 | case PNG_CMAP_GA: |
| 2995 | /* Output must be two channels and the 'G' one must be sRGB, the latter |
| 2996 | * can be checked with an exact number because it should have been set |
| 2997 | * to this number above! |
| 2998 | */ |
| 2999 | if (info_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA && |
| 3000 | info_ptr->bit_depth == 8 && |
| 3001 | png_ptr->screen_gamma == PNG_GAMMA_sRGB && |
| 3002 | image->colormap_entries == 256) |
| 3003 | break; |
| 3004 | |
| 3005 | goto bad_output; |
| 3006 | |
| 3007 | case PNG_CMAP_RGB: |
| 3008 | /* Output must be 8-bit sRGB encoded RGB */ |
| 3009 | if (info_ptr->color_type == PNG_COLOR_TYPE_RGB && |
| 3010 | info_ptr->bit_depth == 8 && |
| 3011 | png_ptr->screen_gamma == PNG_GAMMA_sRGB && |
| 3012 | image->colormap_entries == 216) |
| 3013 | break; |
| 3014 | |
| 3015 | goto bad_output; |
| 3016 | |
| 3017 | case PNG_CMAP_RGB_ALPHA: |
| 3018 | /* Output must be 8-bit sRGB encoded RGBA */ |
| 3019 | if (info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA && |
| 3020 | info_ptr->bit_depth == 8 && |
| 3021 | png_ptr->screen_gamma == PNG_GAMMA_sRGB && |
| 3022 | image->colormap_entries == 244 /* 216 + 1 + 27 */) |
| 3023 | break; |
| 3024 | |
| 3025 | /* goto bad_output; */ |
| 3026 | /* FALL THROUGH */ |
| 3027 | |
| 3028 | default: |
| 3029 | bad_output: |
| 3030 | png_error(png_ptr, "bad color-map processing (internal error)"); |
| 3031 | } |
| 3032 | |
| 3033 | /* Now read the rows. Do this here if it is possible to read directly into |
| 3034 | * the output buffer, otherwise allocate a local row buffer of the maximum |
| 3035 | * size libpng requires and call the relevant processing routine safely. |
| 3036 | */ |
| 3037 | { |
| 3038 | png_bytep first_row = png_voidcast(png_bytep, display->buffer); |
| 3039 | ptrdiff_t row_bytes = display->row_stride; |
| 3040 | |
| 3041 | /* The following expression is designed to work correctly whether it gives |
| 3042 | * a signed or an unsigned result. |
| 3043 | */ |
| 3044 | if (row_bytes < 0) |
| 3045 | first_row += (image->height-1) * (-row_bytes); |
| 3046 | |
| 3047 | display->first_row = first_row; |
| 3048 | display->row_bytes = row_bytes; |
| 3049 | } |
| 3050 | |
| 3051 | if (passes == 0) |
| 3052 | { |
| 3053 | int result; |
| 3054 | png_bytep row = png_voidcast(png_bytep, png_malloc(png_ptr, |
| 3055 | png_get_rowbytes(png_ptr, info_ptr))); |
| 3056 | |
| 3057 | display->local_row = row; |
| 3058 | result = png_safe_execute(image, png_image_read_and_map, display); |
| 3059 | display->local_row = NULL; |
| 3060 | png_free(png_ptr, row); |
| 3061 | |
| 3062 | return result; |
| 3063 | } |
| 3064 | |
| 3065 | else |
| 3066 | { |
| 3067 | png_alloc_size_t row_bytes = display->row_bytes; |
| 3068 | |
| 3069 | while (--passes >= 0) |
| 3070 | { |
| 3071 | png_uint_32 y = image->height; |
| 3072 | png_bytep row = display->first_row; |
| 3073 | |
| 3074 | while (y-- > 0) |
| 3075 | { |
| 3076 | png_read_row(png_ptr, row, NULL); |
| 3077 | row += row_bytes; |
| 3078 | } |
| 3079 | } |
| 3080 | |
| 3081 | return 1; |
| 3082 | } |
| 3083 | } |
| 3084 | |
| 3085 | /* Just the row reading part of png_image_read. */ |
| 3086 | static int |
| 3087 | png_image_read_composite(png_voidp argument) |
| 3088 | { |
| 3089 | png_image_read_control *display = png_voidcast(png_image_read_control*, |
| 3090 | argument); |
| 3091 | png_imagep image = display->image; |
| 3092 | png_structrp png_ptr = image->opaque->png_ptr; |
| 3093 | int passes; |
| 3094 | |
| 3095 | switch (png_ptr->interlaced) |
| 3096 | { |
| 3097 | case PNG_INTERLACE_NONE: |
| 3098 | passes = 1; |
| 3099 | break; |
| 3100 | |
| 3101 | case PNG_INTERLACE_ADAM7: |
| 3102 | passes = PNG_INTERLACE_ADAM7_PASSES; |
| 3103 | break; |
| 3104 | |
| 3105 | default: |
| 3106 | passes = 0; |
| 3107 | png_error(png_ptr, "unknown interlace type"); |
| 3108 | } |
| 3109 | |
| 3110 | { |
| 3111 | png_uint_32 height = image->height; |
| 3112 | png_uint_32 width = image->width; |
| 3113 | png_bytep first_row = display->first_row; |
| 3114 | ptrdiff_t step_row = display->row_bytes; |
| 3115 | unsigned int channels = (image->format & PNG_FORMAT_FLAG_COLOR) ? 3 : 1; |
| 3116 | int pass; |
| 3117 | |
| 3118 | for (pass = 0; pass < passes; ++pass) |
| 3119 | { |
| 3120 | unsigned int startx, stepx, stepy; |
| 3121 | png_uint_32 y; |
| 3122 | |
| 3123 | if (png_ptr->interlaced == PNG_INTERLACE_ADAM7) |
| 3124 | { |
| 3125 | /* The row may be empty for a short image: */ |
| 3126 | if (PNG_PASS_COLS(width, pass) == 0) |
| 3127 | continue; |
| 3128 | |
| 3129 | startx = PNG_PASS_START_COL(pass) * channels; |
| 3130 | stepx = PNG_PASS_COL_OFFSET(pass) * channels; |
| 3131 | y = PNG_PASS_START_ROW(pass); |
| 3132 | stepy = PNG_PASS_ROW_OFFSET(pass); |
| 3133 | } |
| 3134 | |
| 3135 | else |
| 3136 | { |
| 3137 | y = 0; |
| 3138 | startx = 0; |
| 3139 | stepx = channels; |
| 3140 | stepy = 1; |
| 3141 | } |
| 3142 | |
| 3143 | for (; y<height; y += stepy) |
| 3144 | { |
| 3145 | png_bytep inrow = display->local_row; |
| 3146 | png_bytep outrow = first_row + y * step_row; |
| 3147 | png_const_bytep end_row = outrow + width * channels; |
| 3148 | |
| 3149 | /* Read the row, which is packed: */ |
| 3150 | png_read_row(png_ptr, inrow, NULL); |
| 3151 | |
| 3152 | /* Now do the composition on each pixel in this row. */ |
| 3153 | outrow += startx; |
| 3154 | for (; outrow < end_row; outrow += stepx) |
| 3155 | { |
| 3156 | png_byte alpha = inrow[channels]; |
| 3157 | |
| 3158 | if (alpha > 0) /* else no change to the output */ |
| 3159 | { |
| 3160 | unsigned int c; |
| 3161 | |
| 3162 | for (c=0; c<channels; ++c) |
| 3163 | { |
| 3164 | png_uint_32 component = inrow[c]; |
| 3165 | |
| 3166 | if (alpha < 255) /* else just use component */ |
| 3167 | { |
| 3168 | /* This is PNG_OPTIMIZED_ALPHA, the component value |
| 3169 | * is a linear 8-bit value. Combine this with the |
| 3170 | * current outrow[c] value which is sRGB encoded. |
| 3171 | * Arithmetic here is 16-bits to preserve the output |
| 3172 | * values correctly. |
| 3173 | */ |
| 3174 | component *= 257*255; /* =65535 */ |
| 3175 | component += (255-alpha)*png_sRGB_table[outrow[c]]; |
| 3176 | |
| 3177 | /* So 'component' is scaled by 255*65535 and is |
| 3178 | * therefore appropriate for the sRGB to linear |
| 3179 | * conversion table. |
| 3180 | */ |
| 3181 | component = PNG_sRGB_FROM_LINEAR(component); |
| 3182 | } |
| 3183 | |
| 3184 | outrow[c] = (png_byte)component; |
| 3185 | } |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3186 | } |
| 3187 | |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame^] | 3188 | inrow += channels+1; /* components and alpha channel */ |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3189 | } |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame^] | 3190 | } |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3191 | } |
| 3192 | } |
| 3193 | |
| 3194 | return 1; |
| 3195 | } |
| 3196 | |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3197 | /* The do_local_background case; called when all the following transforms are to |
| 3198 | * be done: |
| 3199 | * |
| 3200 | * PNG_RGB_TO_GRAY |
| 3201 | * PNG_COMPOSITE |
| 3202 | * PNG_GAMMA |
| 3203 | * |
| 3204 | * This is a work-round for the fact that both the PNG_RGB_TO_GRAY and |
| 3205 | * PNG_COMPOSITE code performs gamma correction, so we get double gamma |
| 3206 | * correction. The fix-up is to prevent the PNG_COMPOSITE operation happening |
| 3207 | * inside libpng, so this routine sees an 8 or 16-bit gray+alpha row and handles |
| 3208 | * the removal or pre-multiplication of the alpha channel. |
| 3209 | */ |
| 3210 | static int |
| 3211 | png_image_read_background(png_voidp argument) |
| 3212 | { |
John Bowler | 4fa96a4 | 2011-11-16 16:39:16 -0600 | [diff] [blame] | 3213 | png_image_read_control *display = png_voidcast(png_image_read_control*, |
| 3214 | argument); |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3215 | png_imagep image = display->image; |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 3216 | png_structrp png_ptr = image->opaque->png_ptr; |
| 3217 | png_inforp info_ptr = image->opaque->info_ptr; |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3218 | png_uint_32 height = image->height; |
| 3219 | png_uint_32 width = image->width; |
John Bowler | 4f67e40 | 2011-12-28 08:43:37 -0600 | [diff] [blame] | 3220 | int pass, passes; |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3221 | |
| 3222 | /* Double check the convoluted logic below. We expect to get here with |
| 3223 | * libpng doing rgb to gray and gamma correction but background processing |
| 3224 | * left to the png_image_read_background function. The rows libpng produce |
| 3225 | * might be 8 or 16-bit but should always have two channels; gray plus alpha. |
| 3226 | */ |
| 3227 | if ((png_ptr->transformations & PNG_RGB_TO_GRAY) == 0) |
| 3228 | png_error(png_ptr, "lost rgb to gray"); |
| 3229 | |
| 3230 | if ((png_ptr->transformations & PNG_COMPOSE) != 0) |
| 3231 | png_error(png_ptr, "unexpected compose"); |
| 3232 | |
| 3233 | /* The palette code zaps PNG_GAMMA in place... */ |
| 3234 | if ((png_ptr->color_type & PNG_COLOR_MASK_PALETTE) == 0 && |
| 3235 | (png_ptr->transformations & PNG_GAMMA) == 0) |
| 3236 | png_error(png_ptr, "lost gamma correction"); |
| 3237 | |
| 3238 | if (png_get_channels(png_ptr, info_ptr) != 2) |
| 3239 | png_error(png_ptr, "lost/gained channels"); |
| 3240 | |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame^] | 3241 | switch (png_ptr->interlaced) |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3242 | { |
| 3243 | case PNG_INTERLACE_NONE: |
| 3244 | passes = 1; |
| 3245 | break; |
| 3246 | |
| 3247 | case PNG_INTERLACE_ADAM7: |
| 3248 | passes = PNG_INTERLACE_ADAM7_PASSES; |
| 3249 | break; |
| 3250 | |
| 3251 | default: |
John Bowler | 4f67e40 | 2011-12-28 08:43:37 -0600 | [diff] [blame] | 3252 | passes = 0; |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3253 | png_error(png_ptr, "unknown interlace type"); |
| 3254 | } |
| 3255 | |
| 3256 | switch (png_get_bit_depth(png_ptr, info_ptr)) |
| 3257 | { |
| 3258 | default: |
| 3259 | png_error(png_ptr, "unexpected bit depth"); |
| 3260 | break; |
| 3261 | |
| 3262 | case 8: |
| 3263 | /* 8-bit sRGB gray values with an alpha channel; the alpha channel is |
| 3264 | * to be removed by composing on a backgroundi: either the row if |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame^] | 3265 | * display->background is NULL or display->background->green if not. |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3266 | * Unlike the code above ALPHA_OPTIMIZED has *not* been done. |
| 3267 | */ |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3268 | { |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame^] | 3269 | png_bytep first_row = display->first_row; |
| 3270 | ptrdiff_t step_row = display->row_bytes; |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3271 | |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame^] | 3272 | for (pass = 0; pass < passes; ++pass) |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3273 | { |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame^] | 3274 | png_bytep row = display->first_row; |
| 3275 | unsigned int startx, stepx, stepy; |
| 3276 | png_uint_32 y; |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3277 | |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame^] | 3278 | if (png_ptr->interlaced == PNG_INTERLACE_ADAM7) |
| 3279 | { |
| 3280 | /* The row may be empty for a short image: */ |
| 3281 | if (PNG_PASS_COLS(width, pass) == 0) |
| 3282 | continue; |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3283 | |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame^] | 3284 | startx = PNG_PASS_START_COL(pass); |
| 3285 | stepx = PNG_PASS_COL_OFFSET(pass); |
| 3286 | y = PNG_PASS_START_ROW(pass); |
| 3287 | stepy = PNG_PASS_ROW_OFFSET(pass); |
| 3288 | } |
| 3289 | |
| 3290 | else |
| 3291 | { |
| 3292 | y = 0; |
| 3293 | startx = 0; |
| 3294 | stepx = stepy = 1; |
| 3295 | } |
| 3296 | |
| 3297 | if (display->background == NULL) |
| 3298 | { |
| 3299 | for (; y<height; y += stepy) |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3300 | { |
| 3301 | png_bytep inrow = display->local_row; |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame^] | 3302 | png_bytep outrow = first_row + y * step_row; |
| 3303 | png_const_bytep end_row = outrow + width; |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3304 | |
| 3305 | /* Read the row, which is packed: */ |
| 3306 | png_read_row(png_ptr, inrow, NULL); |
| 3307 | |
| 3308 | /* Now do the composition on each pixel in this row. */ |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame^] | 3309 | outrow += startx; |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3310 | for (; outrow < end_row; outrow += stepx) |
| 3311 | { |
| 3312 | png_byte alpha = inrow[1]; |
| 3313 | |
| 3314 | if (alpha > 0) /* else no change to the output */ |
| 3315 | { |
| 3316 | png_uint_32 component = inrow[0]; |
| 3317 | |
| 3318 | if (alpha < 255) /* else just use component */ |
| 3319 | { |
| 3320 | /* Since PNG_OPTIMIZED_ALPHA was not set it is |
| 3321 | * necessary to invert the sRGB transfer |
| 3322 | * function and multiply the alpha out. |
| 3323 | */ |
| 3324 | component = png_sRGB_table[component] * alpha; |
| 3325 | component += png_sRGB_table[outrow[0]] * |
| 3326 | (255-alpha); |
| 3327 | component = PNG_sRGB_FROM_LINEAR(component); |
| 3328 | } |
| 3329 | |
| 3330 | outrow[0] = (png_byte)component; |
| 3331 | } |
| 3332 | |
| 3333 | inrow += 2; /* gray and alpha channel */ |
| 3334 | } |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3335 | } |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame^] | 3336 | } |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3337 | |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame^] | 3338 | else /* constant background value */ |
| 3339 | { |
| 3340 | png_byte background8 = display->background->green; |
| 3341 | png_uint_16 background = png_sRGB_table[background8]; |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3342 | |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame^] | 3343 | for (; y<height; y += stepy) |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3344 | { |
| 3345 | png_bytep inrow = display->local_row; |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame^] | 3346 | png_bytep outrow = first_row + y * step_row; |
| 3347 | png_const_bytep end_row = outrow + width; |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3348 | |
| 3349 | /* Read the row, which is packed: */ |
| 3350 | png_read_row(png_ptr, inrow, NULL); |
| 3351 | |
| 3352 | /* Now do the composition on each pixel in this row. */ |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame^] | 3353 | outrow += startx; |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3354 | for (; outrow < end_row; outrow += stepx) |
| 3355 | { |
| 3356 | png_byte alpha = inrow[1]; |
| 3357 | |
| 3358 | if (alpha > 0) /* else use background */ |
| 3359 | { |
| 3360 | png_uint_32 component = inrow[0]; |
| 3361 | |
| 3362 | if (alpha < 255) /* else just use component */ |
| 3363 | { |
| 3364 | component = png_sRGB_table[component] * alpha; |
| 3365 | component += background * (255-alpha); |
| 3366 | component = PNG_sRGB_FROM_LINEAR(component); |
| 3367 | } |
| 3368 | |
| 3369 | outrow[0] = (png_byte)component; |
| 3370 | } |
| 3371 | |
| 3372 | else |
| 3373 | outrow[0] = background8; |
| 3374 | |
| 3375 | inrow += 2; /* gray and alpha channel */ |
| 3376 | } |
| 3377 | |
| 3378 | row += display->row_bytes; |
| 3379 | } |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame^] | 3380 | } |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3381 | } |
| 3382 | } |
| 3383 | break; |
| 3384 | |
| 3385 | case 16: |
| 3386 | /* 16-bit linear with pre-multiplied alpha; the pre-multiplication must |
| 3387 | * still be done and, maybe, the alpha channel removed. This code also |
| 3388 | * handles the alpha-first option. |
| 3389 | */ |
| 3390 | { |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame^] | 3391 | png_bytep first_row = display->first_row; |
| 3392 | ptrdiff_t step_row = display->row_bytes; |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3393 | unsigned int outchannels = png_get_channels(png_ptr, info_ptr); |
| 3394 | int preserve_alpha = (image->format & PNG_FORMAT_FLAG_ALPHA) != 0; |
| 3395 | int swap_alpha = 0; |
| 3396 | |
| 3397 | if (preserve_alpha && (image->format & PNG_FORMAT_FLAG_AFIRST)) |
| 3398 | swap_alpha = 1; |
| 3399 | |
| 3400 | for (pass = 0; pass < passes; ++pass) |
| 3401 | { |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3402 | unsigned int startx, stepx, stepy; /* all in pixels */ |
| 3403 | png_uint_32 y; |
| 3404 | |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame^] | 3405 | if (png_ptr->interlaced == PNG_INTERLACE_ADAM7) |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3406 | { |
| 3407 | /* The row may be empty for a short image: */ |
| 3408 | if (PNG_PASS_COLS(width, pass) == 0) |
| 3409 | continue; |
| 3410 | |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame^] | 3411 | startx = PNG_PASS_START_COL(pass) * outchannels; |
| 3412 | stepx = PNG_PASS_COL_OFFSET(pass) * outchannels; |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3413 | y = PNG_PASS_START_ROW(pass); |
| 3414 | stepy = PNG_PASS_ROW_OFFSET(pass); |
| 3415 | } |
| 3416 | |
| 3417 | else |
| 3418 | { |
| 3419 | y = 0; |
| 3420 | startx = 0; |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame^] | 3421 | stepx = outchannels; |
| 3422 | stepy = 1; |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3423 | } |
| 3424 | |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3425 | for (; y<height; y += stepy) |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame^] | 3426 | { |
| 3427 | png_const_uint_16p inrow; |
| 3428 | png_uint_16p outrow = (png_uint_16p)(first_row + y*step_row); |
| 3429 | png_uint_16p end_row = outrow + width * outchannels; |
| 3430 | |
| 3431 | /* Read the row, which is packed: */ |
| 3432 | png_read_row(png_ptr, display->local_row, NULL); |
| 3433 | inrow = (png_const_uint_16p)display->local_row; |
| 3434 | |
| 3435 | /* Now do the pre-multiplication on each pixel in this row. |
| 3436 | */ |
| 3437 | outrow += startx; |
| 3438 | for (; outrow < end_row; outrow += stepx) |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3439 | { |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame^] | 3440 | png_uint_32 component = inrow[0]; |
| 3441 | png_uint_16 alpha = inrow[1]; |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3442 | |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame^] | 3443 | if (alpha > 0) /* else 0 */ |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3444 | { |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame^] | 3445 | if (alpha < 65535) /* else just use component */ |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3446 | { |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame^] | 3447 | component *= alpha; |
| 3448 | component += 32767; |
| 3449 | component /= 65535; |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3450 | } |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3451 | } |
| 3452 | |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame^] | 3453 | else |
| 3454 | component = 0; |
| 3455 | |
| 3456 | outrow[swap_alpha] = (png_uint_16)component; |
| 3457 | if (outchannels > 1) |
| 3458 | outrow[1 ^ swap_alpha] = alpha; |
| 3459 | |
| 3460 | inrow += 2; /* components and alpha channel */ |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3461 | } |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame^] | 3462 | } |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3463 | } |
| 3464 | } |
| 3465 | break; |
| 3466 | } |
| 3467 | |
| 3468 | return 1; |
| 3469 | } |
| 3470 | |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3471 | /* The guts of png_image_finish_read as a png_safe_execute callback. */ |
| 3472 | static int |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame^] | 3473 | png_image_read_direct(png_voidp argument) |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3474 | { |
John Bowler | 4fa96a4 | 2011-11-16 16:39:16 -0600 | [diff] [blame] | 3475 | png_image_read_control *display = png_voidcast(png_image_read_control*, |
| 3476 | argument); |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3477 | png_imagep image = display->image; |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 3478 | png_structrp png_ptr = image->opaque->png_ptr; |
| 3479 | png_inforp info_ptr = image->opaque->info_ptr; |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3480 | |
| 3481 | png_uint_32 format = image->format; |
| 3482 | int linear = (format & PNG_FORMAT_FLAG_LINEAR) != 0; |
| 3483 | int do_local_compose = 0; |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3484 | int do_local_background = 0; /* to avoid double gamma correction bug */ |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3485 | int passes = 0; |
| 3486 | |
| 3487 | /* Add transforms to ensure the correct output format is produced then check |
| 3488 | * that the required implementation support is there. Always expand; always |
| 3489 | * need 8 bits minimum, no palette and expanded tRNS. |
| 3490 | */ |
| 3491 | png_set_expand(png_ptr); |
| 3492 | |
| 3493 | /* Now check the format to see if it was modified. */ |
| 3494 | { |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame^] | 3495 | png_uint_32 base_format = png_image_format(png_ptr) & |
| 3496 | ~PNG_FORMAT_FLAG_COLORMAP /* removed by png_set_expand */; |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3497 | png_uint_32 change = format ^ base_format; |
John Bowler | 3615d03 | 2011-11-08 10:38:09 -0600 | [diff] [blame] | 3498 | png_fixed_point output_gamma; |
| 3499 | int mode; /* alpha mode */ |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3500 | |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3501 | /* Do this first so that we have a record if rgb to gray is happening. */ |
| 3502 | if (change & PNG_FORMAT_FLAG_COLOR) |
| 3503 | { |
| 3504 | /* gray<->color transformation required. */ |
| 3505 | if (format & PNG_FORMAT_FLAG_COLOR) |
| 3506 | png_set_gray_to_rgb(png_ptr); |
| 3507 | |
| 3508 | else |
| 3509 | { |
| 3510 | /* libpng can't do both rgb to gray and |
| 3511 | * background/pre-multiplication if there is also significant gamma |
| 3512 | * correction, because both operations require linear colors and |
| 3513 | * the code only supports one transform doing the gamma correction. |
| 3514 | * Handle this by doing the pre-multiplication or background |
| 3515 | * operation in this code, if necessary. |
| 3516 | * |
| 3517 | * TODO: fix this by rewriting pngrtran.c (!) |
| 3518 | * |
| 3519 | * For the moment (given that fixing this in pngrtran.c is an |
| 3520 | * enormous change) 'do_local_background' is used to indicate that |
| 3521 | * the problem exists. |
| 3522 | */ |
| 3523 | if (base_format & PNG_FORMAT_FLAG_ALPHA) |
| 3524 | do_local_background = 1/*maybe*/; |
| 3525 | |
| 3526 | png_set_rgb_to_gray_fixed(png_ptr, PNG_ERROR_ACTION_NONE, |
| 3527 | PNG_RGB_TO_GRAY_DEFAULT, PNG_RGB_TO_GRAY_DEFAULT); |
| 3528 | } |
| 3529 | |
| 3530 | change &= ~PNG_FORMAT_FLAG_COLOR; |
| 3531 | } |
| 3532 | |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3533 | /* Set the gamma appropriately, linear for 16-bit input, sRGB otherwise. |
| 3534 | */ |
| 3535 | { |
John Bowler | 3615d03 | 2011-11-08 10:38:09 -0600 | [diff] [blame] | 3536 | png_fixed_point input_gamma_default; |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3537 | |
| 3538 | if (base_format & PNG_FORMAT_FLAG_LINEAR) |
| 3539 | input_gamma_default = PNG_GAMMA_LINEAR; |
| 3540 | else |
| 3541 | input_gamma_default = PNG_DEFAULT_sRGB; |
| 3542 | |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3543 | /* Call png_set_alpha_mode to set the default for the input gamma; the |
| 3544 | * output gamma is set by a second call below. |
| 3545 | */ |
| 3546 | png_set_alpha_mode_fixed(png_ptr, PNG_ALPHA_PNG, input_gamma_default); |
| 3547 | } |
| 3548 | |
| 3549 | if (linear) |
| 3550 | { |
| 3551 | /* If there *is* an alpha channel in the input it must be multiplied |
| 3552 | * out; use PNG_ALPHA_STANDARD, otherwise just use PNG_ALPHA_PNG. |
| 3553 | */ |
| 3554 | if (base_format & PNG_FORMAT_FLAG_ALPHA) |
John Bowler | 3615d03 | 2011-11-08 10:38:09 -0600 | [diff] [blame] | 3555 | mode = PNG_ALPHA_STANDARD; /* associated alpha */ |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3556 | |
| 3557 | else |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3558 | mode = PNG_ALPHA_PNG; |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3559 | |
| 3560 | output_gamma = PNG_GAMMA_LINEAR; |
| 3561 | } |
| 3562 | |
| 3563 | else |
| 3564 | { |
| 3565 | mode = PNG_ALPHA_PNG; |
| 3566 | output_gamma = PNG_DEFAULT_sRGB; |
| 3567 | } |
| 3568 | |
| 3569 | /* If 'do_local_background' is set check for the presence of gamma |
| 3570 | * correction; this is part of the work-round for the libpng bug |
| 3571 | * described above. |
| 3572 | * |
| 3573 | * TODO: fix libpng and remove this. |
| 3574 | */ |
| 3575 | if (do_local_background) |
| 3576 | { |
| 3577 | png_fixed_point gtest; |
| 3578 | |
| 3579 | /* This is 'png_gamma_threshold' from pngrtran.c; the test used for |
| 3580 | * gamma correction, the screen gamma hasn't been set on png_struct |
| 3581 | * yet; it's set below. png_struct::gamma, however, is set to the |
| 3582 | * final value. |
| 3583 | */ |
| 3584 | if (png_muldiv(>est, output_gamma, png_ptr->gamma, PNG_FP_1) && |
| 3585 | !png_gamma_significant(gtest)) |
| 3586 | do_local_background = 0; |
| 3587 | |
| 3588 | else if (mode == PNG_ALPHA_STANDARD) |
| 3589 | { |
| 3590 | do_local_background = 2/*required*/; |
| 3591 | mode = PNG_ALPHA_PNG; /* prevent libpng doing it */ |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3592 | } |
| 3593 | |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3594 | /* else leave as 1 for the checks below */ |
John Bowler | 3615d03 | 2011-11-08 10:38:09 -0600 | [diff] [blame] | 3595 | } |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3596 | |
John Bowler | 3615d03 | 2011-11-08 10:38:09 -0600 | [diff] [blame] | 3597 | /* If the bit-depth changes then handle that here. */ |
| 3598 | if (change & PNG_FORMAT_FLAG_LINEAR) |
| 3599 | { |
| 3600 | if (linear /*16-bit output*/) |
| 3601 | png_set_expand_16(png_ptr); |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3602 | |
John Bowler | 3615d03 | 2011-11-08 10:38:09 -0600 | [diff] [blame] | 3603 | else /* 8-bit output */ |
| 3604 | png_set_scale_16(png_ptr); |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3605 | |
John Bowler | 3615d03 | 2011-11-08 10:38:09 -0600 | [diff] [blame] | 3606 | change &= ~PNG_FORMAT_FLAG_LINEAR; |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3607 | } |
| 3608 | |
| 3609 | /* Now the background/alpha channel changes. */ |
| 3610 | if (change & PNG_FORMAT_FLAG_ALPHA) |
| 3611 | { |
| 3612 | /* Removing an alpha channel requires composition for the 8-bit |
| 3613 | * formats; for the 16-bit it is already done, above, by the |
| 3614 | * pre-multiplication and the channel just needs to be stripped. |
| 3615 | */ |
| 3616 | if (base_format & PNG_FORMAT_FLAG_ALPHA) |
| 3617 | { |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3618 | /* If RGB->gray is happening the alpha channel must be left and the |
| 3619 | * operation completed locally. |
| 3620 | * |
| 3621 | * TODO: fix libpng and remove this. |
| 3622 | */ |
| 3623 | if (do_local_background) |
| 3624 | do_local_background = 2/*required*/; |
| 3625 | |
John Bowler | 3615d03 | 2011-11-08 10:38:09 -0600 | [diff] [blame] | 3626 | /* 16-bit output: just remove the channel */ |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3627 | else if (linear) /* compose on black (well, pre-multiply) */ |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3628 | png_set_strip_alpha(png_ptr); |
| 3629 | |
John Bowler | 3615d03 | 2011-11-08 10:38:09 -0600 | [diff] [blame] | 3630 | /* 8-bit output: do an appropriate compose */ |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3631 | else if (display->background != NULL) |
| 3632 | { |
| 3633 | png_color_16 c; |
| 3634 | |
| 3635 | c.index = 0; /*unused*/ |
| 3636 | c.red = display->background->red; |
| 3637 | c.green = display->background->green; |
| 3638 | c.blue = display->background->blue; |
| 3639 | c.gray = display->background->green; |
| 3640 | |
| 3641 | /* This is always an 8-bit sRGB value, using the 'green' channel |
| 3642 | * for gray is much better than calculating the luminance here; |
| 3643 | * we can get off-by-one errors in that calculation relative to |
| 3644 | * the app expectations and that will show up in transparent |
| 3645 | * pixels. |
| 3646 | */ |
| 3647 | png_set_background_fixed(png_ptr, &c, |
| 3648 | PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/, |
| 3649 | 0/*gamma: not used*/); |
| 3650 | } |
| 3651 | |
| 3652 | else /* compose on row: implemented below. */ |
| 3653 | { |
| 3654 | do_local_compose = 1; |
Glenn Randers-Pehrson | 84b0da9 | 2011-11-10 06:32:19 -0600 | [diff] [blame] | 3655 | /* 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] | 3656 | * removed by the code below. Set the encoding to the 'OPTIMIZE' |
| 3657 | * one so the code only has to hack on the pixels that require |
| 3658 | * composition. |
| 3659 | */ |
John Bowler | 3615d03 | 2011-11-08 10:38:09 -0600 | [diff] [blame] | 3660 | mode = PNG_ALPHA_OPTIMIZED; |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3661 | } |
| 3662 | } |
| 3663 | |
| 3664 | else /* output needs an alpha channel */ |
| 3665 | { |
| 3666 | /* This is tricky because it happens before the swap operation has |
Glenn Randers-Pehrson | 84b0da9 | 2011-11-10 06:32:19 -0600 | [diff] [blame] | 3667 | * been accomplished; however, the swap does *not* swap the added |
John Bowler | e6fb691 | 2011-11-08 21:35:16 -0600 | [diff] [blame] | 3668 | * alpha channel (weird API), so it must be added in the correct |
| 3669 | * place. |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3670 | */ |
John Bowler | 3615d03 | 2011-11-08 10:38:09 -0600 | [diff] [blame] | 3671 | png_uint_32 filler; /* opaque filler */ |
John Bowler | e6fb691 | 2011-11-08 21:35:16 -0600 | [diff] [blame] | 3672 | int where; |
John Bowler | 3615d03 | 2011-11-08 10:38:09 -0600 | [diff] [blame] | 3673 | |
| 3674 | if (linear) |
| 3675 | filler = 65535; |
| 3676 | |
| 3677 | else |
| 3678 | filler = 255; |
| 3679 | |
John Bowler | e6fb691 | 2011-11-08 21:35:16 -0600 | [diff] [blame] | 3680 | # ifdef PNG_FORMAT_AFIRST_SUPPORTED |
| 3681 | if (format & PNG_FORMAT_FLAG_AFIRST) |
| 3682 | { |
| 3683 | where = PNG_FILLER_BEFORE; |
| 3684 | change &= ~PNG_FORMAT_FLAG_AFIRST; |
| 3685 | } |
| 3686 | |
| 3687 | else |
| 3688 | # endif |
| 3689 | where = PNG_FILLER_AFTER; |
| 3690 | |
| 3691 | png_set_add_alpha(png_ptr, filler, where); |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3692 | } |
| 3693 | |
John Bowler | e6fb691 | 2011-11-08 21:35:16 -0600 | [diff] [blame] | 3694 | /* This stops the (irrelevant) call to swap_alpha below. */ |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3695 | change &= ~PNG_FORMAT_FLAG_ALPHA; |
| 3696 | } |
| 3697 | |
John Bowler | 3615d03 | 2011-11-08 10:38:09 -0600 | [diff] [blame] | 3698 | /* Now set the alpha mode correctly; this is always done, even if there is |
| 3699 | * no alpha channel in either the input or the output because it correctly |
| 3700 | * sets the output gamma. |
| 3701 | */ |
| 3702 | png_set_alpha_mode_fixed(png_ptr, mode, output_gamma); |
| 3703 | |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3704 | # ifdef PNG_FORMAT_BGR_SUPPORTED |
John Bowler | e6fb691 | 2011-11-08 21:35:16 -0600 | [diff] [blame] | 3705 | if (change & PNG_FORMAT_FLAG_BGR) |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3706 | { |
Glenn Randers-Pehrson | 84b0da9 | 2011-11-10 06:32:19 -0600 | [diff] [blame] | 3707 | /* 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] | 3708 | * the output is gray, but fix up the 'format' value in that case. |
| 3709 | */ |
| 3710 | if (format & PNG_FORMAT_FLAG_COLOR) |
| 3711 | png_set_bgr(png_ptr); |
| 3712 | |
| 3713 | else |
| 3714 | format &= ~PNG_FORMAT_FLAG_BGR; |
| 3715 | |
| 3716 | change &= ~PNG_FORMAT_FLAG_BGR; |
| 3717 | } |
| 3718 | # endif |
| 3719 | |
| 3720 | # ifdef PNG_FORMAT_AFIRST_SUPPORTED |
John Bowler | e6fb691 | 2011-11-08 21:35:16 -0600 | [diff] [blame] | 3721 | if (change & PNG_FORMAT_FLAG_AFIRST) |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3722 | { |
| 3723 | /* Only relevant if there is an alpha channel - it's particularly |
| 3724 | * important to handle this correctly because do_local_compose may |
| 3725 | * be set above and then libpng will keep the alpha channel for this |
| 3726 | * code to remove. |
| 3727 | */ |
| 3728 | if (format & PNG_FORMAT_FLAG_ALPHA) |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3729 | { |
| 3730 | /* Disable this if doing a local background, |
| 3731 | * TODO: remove this when local background is no longer required. |
| 3732 | */ |
| 3733 | if (do_local_background != 2) |
| 3734 | png_set_swap_alpha(png_ptr); |
| 3735 | } |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3736 | |
| 3737 | else |
| 3738 | format &= ~PNG_FORMAT_FLAG_AFIRST; |
| 3739 | |
| 3740 | change &= ~PNG_FORMAT_FLAG_AFIRST; |
| 3741 | } |
| 3742 | # endif |
| 3743 | |
| 3744 | /* If the *output* is 16-bit then we need to check for a byte-swap on this |
| 3745 | * architecture. |
| 3746 | */ |
| 3747 | if (linear) |
| 3748 | { |
| 3749 | PNG_CONST png_uint_16 le = 0x0001; |
| 3750 | |
| 3751 | if (*(png_const_bytep)&le) |
| 3752 | png_set_swap(png_ptr); |
| 3753 | } |
| 3754 | |
| 3755 | /* If change is not now 0 some transformation is missing - error out. */ |
| 3756 | if (change) |
| 3757 | png_error(png_ptr, "png_read_image: unsupported transformation"); |
| 3758 | } |
| 3759 | |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame^] | 3760 | PNG_SKIP_CHUNKS(png_ptr); |
John Bowler | 89c2f84 | 2011-11-16 12:04:39 -0600 | [diff] [blame] | 3761 | |
Glenn Randers-Pehrson | 84b0da9 | 2011-11-10 06:32:19 -0600 | [diff] [blame] | 3762 | /* 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] | 3763 | * make sure to turn on the interlace handling if it will be required |
| 3764 | * (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] | 3765 | * |
| 3766 | * TODO: remove the do_local_background fixup below. |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3767 | */ |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3768 | if (!do_local_compose && do_local_background != 2) |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3769 | passes = png_set_interlace_handling(png_ptr); |
| 3770 | |
| 3771 | png_read_update_info(png_ptr, info_ptr); |
| 3772 | |
| 3773 | { |
| 3774 | png_uint_32 info_format = 0; |
| 3775 | |
| 3776 | if (info_ptr->color_type & PNG_COLOR_MASK_COLOR) |
| 3777 | info_format |= PNG_FORMAT_FLAG_COLOR; |
| 3778 | |
| 3779 | if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA) |
| 3780 | { |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3781 | /* do_local_compose removes this channel below. */ |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3782 | if (!do_local_compose) |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3783 | { |
| 3784 | /* do_local_background does the same if required. */ |
| 3785 | if (do_local_background != 2 || |
| 3786 | (format & PNG_FORMAT_FLAG_ALPHA) != 0) |
| 3787 | info_format |= PNG_FORMAT_FLAG_ALPHA; |
| 3788 | } |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3789 | } |
| 3790 | |
| 3791 | else if (do_local_compose) /* internal error */ |
| 3792 | png_error(png_ptr, "png_image_read: alpha channel lost"); |
| 3793 | |
| 3794 | if (info_ptr->bit_depth == 16) |
| 3795 | info_format |= PNG_FORMAT_FLAG_LINEAR; |
| 3796 | |
| 3797 | # ifdef PNG_FORMAT_BGR_SUPPORTED |
| 3798 | if (png_ptr->transformations & PNG_BGR) |
| 3799 | info_format |= PNG_FORMAT_FLAG_BGR; |
| 3800 | # endif |
| 3801 | |
| 3802 | # ifdef PNG_FORMAT_AFIRST_SUPPORTED |
John Bowler | e6fb691 | 2011-11-08 21:35:16 -0600 | [diff] [blame] | 3803 | if (png_ptr->transformations & PNG_SWAP_ALPHA || |
| 3804 | ((png_ptr->transformations & PNG_ADD_ALPHA) != 0 && |
| 3805 | (png_ptr->flags & PNG_FLAG_FILLER_AFTER) == 0)) |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3806 | info_format |= PNG_FORMAT_FLAG_AFIRST; |
| 3807 | # endif |
| 3808 | |
| 3809 | /* This is actually an internal error. */ |
| 3810 | if (info_format != format) |
| 3811 | png_error(png_ptr, "png_read_image: invalid transformations"); |
| 3812 | } |
| 3813 | |
| 3814 | /* Now read the rows. If do_local_compose is set then it is necessary to use |
| 3815 | * a local row buffer. The output will be GA, RGBA or BGRA and must be |
| 3816 | * converted to G, RGB or BGR as appropriate. The 'local_row' member of the |
| 3817 | * display acts as a flag. |
| 3818 | */ |
| 3819 | { |
John Bowler | 4fa96a4 | 2011-11-16 16:39:16 -0600 | [diff] [blame] | 3820 | png_bytep first_row = png_voidcast(png_bytep, display->buffer); |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3821 | ptrdiff_t row_bytes = display->row_stride; |
| 3822 | |
| 3823 | if (linear) |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame^] | 3824 | row_bytes *= 2; |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3825 | |
| 3826 | /* The following expression is designed to work correctly whether it gives |
| 3827 | * a signed or an unsigned result. |
| 3828 | */ |
| 3829 | if (row_bytes < 0) |
| 3830 | first_row += (image->height-1) * (-row_bytes); |
| 3831 | |
| 3832 | display->first_row = first_row; |
| 3833 | display->row_bytes = row_bytes; |
| 3834 | } |
| 3835 | |
| 3836 | if (do_local_compose) |
| 3837 | { |
| 3838 | int result; |
John Bowler | 4fa96a4 | 2011-11-16 16:39:16 -0600 | [diff] [blame] | 3839 | png_bytep row = png_voidcast(png_bytep, png_malloc(png_ptr, |
| 3840 | png_get_rowbytes(png_ptr, info_ptr))); |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3841 | |
| 3842 | display->local_row = row; |
| 3843 | result = png_safe_execute(image, png_image_read_composite, display); |
| 3844 | display->local_row = NULL; |
| 3845 | png_free(png_ptr, row); |
| 3846 | |
| 3847 | return result; |
| 3848 | } |
| 3849 | |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3850 | else if (do_local_background == 2) |
| 3851 | { |
| 3852 | int result; |
John Bowler | 4fa96a4 | 2011-11-16 16:39:16 -0600 | [diff] [blame] | 3853 | png_bytep row = png_voidcast(png_bytep, png_malloc(png_ptr, |
| 3854 | png_get_rowbytes(png_ptr, info_ptr))); |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3855 | |
| 3856 | display->local_row = row; |
| 3857 | result = png_safe_execute(image, png_image_read_background, display); |
| 3858 | display->local_row = NULL; |
| 3859 | png_free(png_ptr, row); |
| 3860 | |
| 3861 | return result; |
| 3862 | } |
| 3863 | |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3864 | else |
| 3865 | { |
| 3866 | png_alloc_size_t row_bytes = display->row_bytes; |
| 3867 | |
| 3868 | while (--passes >= 0) |
| 3869 | { |
| 3870 | png_uint_32 y = image->height; |
| 3871 | png_bytep row = display->first_row; |
| 3872 | |
| 3873 | while (y-- > 0) |
| 3874 | { |
| 3875 | png_read_row(png_ptr, row, NULL); |
| 3876 | row += row_bytes; |
| 3877 | } |
| 3878 | } |
| 3879 | |
| 3880 | return 1; |
| 3881 | } |
| 3882 | } |
| 3883 | |
| 3884 | int PNGAPI |
| 3885 | png_image_finish_read(png_imagep image, png_colorp background, void *buffer, |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame^] | 3886 | png_int_32 row_stride, void *colormap) |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3887 | { |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame^] | 3888 | if (image != NULL && image->version == PNG_IMAGE_VERSION) |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3889 | { |
| 3890 | png_uint_32 check; |
| 3891 | |
John Bowler | 3706d73 | 2011-11-21 10:28:06 -0600 | [diff] [blame] | 3892 | if (row_stride == 0) |
| 3893 | row_stride = PNG_IMAGE_ROW_STRIDE(*image); |
| 3894 | |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3895 | if (row_stride < 0) |
| 3896 | check = -row_stride; |
| 3897 | |
| 3898 | else |
| 3899 | check = row_stride; |
| 3900 | |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame^] | 3901 | if (image->opaque != NULL && buffer != NULL && |
| 3902 | check >= PNG_IMAGE_ROW_STRIDE(*image)) |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3903 | { |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame^] | 3904 | if ((image->format & PNG_FORMAT_FLAG_COLORMAP) == 0 || |
| 3905 | (image->colormap_entries > 0 && colormap != NULL)) |
| 3906 | { |
| 3907 | int result; |
| 3908 | png_image_read_control display; |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3909 | |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame^] | 3910 | png_memset(&display, 0, sizeof display); |
| 3911 | display.image = image; |
| 3912 | display.buffer = buffer; |
| 3913 | display.row_stride = row_stride; |
| 3914 | display.colormap = colormap; |
| 3915 | display.background = background; |
| 3916 | display.local_row = NULL; |
| 3917 | |
| 3918 | /* Choose the correct 'end' routine; for the color-map case all the |
| 3919 | * setup has already been done. |
| 3920 | */ |
| 3921 | if (image->format & PNG_FORMAT_FLAG_COLORMAP) |
| 3922 | result = |
| 3923 | png_safe_execute(image, png_image_read_colormap, &display) && |
| 3924 | png_safe_execute(image, png_image_read_colormapped, &display); |
| 3925 | |
| 3926 | else |
| 3927 | result = |
| 3928 | png_safe_execute(image, png_image_read_direct, &display); |
| 3929 | |
| 3930 | png_image_free(image); |
| 3931 | return result; |
| 3932 | } |
| 3933 | |
| 3934 | else |
| 3935 | return png_image_error(image, |
| 3936 | "png_image_finish_read[color-map]: no color-map"); |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3937 | } |
| 3938 | |
| 3939 | else |
| 3940 | return png_image_error(image, |
| 3941 | "png_image_finish_read: invalid argument"); |
| 3942 | } |
| 3943 | |
| 3944 | return 0; |
| 3945 | } |
| 3946 | |
| 3947 | #endif /* PNG_SIMPLIFIED_READ_SUPPORTED */ |
Glenn Randers-Pehrson | 9c3ab68 | 2006-02-20 22:09:05 -0600 | [diff] [blame] | 3948 | #endif /* PNG_READ_SUPPORTED */ |