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 | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 51 | png_ptr->mode = PNG_IS_READ_STRUCT; |
| 52 | |
John Bowler | aa816c4 | 2012-03-16 07:39:49 -0500 | [diff] [blame] | 53 | /* Added in libpng-1.6.0; this can be used to detect a read structure if |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 54 | * required (it will be zero in a write structure.) |
| 55 | */ |
| 56 | # ifdef PNG_SEQUENTIAL_READ_SUPPORTED |
| 57 | png_ptr->IDAT_read_size = PNG_IDAT_READ_SIZE; |
| 58 | # endif |
John Bowler | aa816c4 | 2012-03-16 07:39:49 -0500 | [diff] [blame] | 59 | |
| 60 | # ifdef PNG_BENIGN_READ_ERRORS_SUPPORTED |
| 61 | png_ptr->flags |= PNG_FLAG_BENIGN_ERRORS_WARN; |
John Bowler | 2bc76ff | 2012-03-18 22:37:25 -0500 | [diff] [blame] | 62 | |
| 63 | /* In stable builds only warn if an application error can be completely |
| 64 | * handled. |
| 65 | */ |
John Bowler | 921648a | 2012-03-28 23:36:12 -0500 | [diff] [blame^] | 66 | # if PNG_LIBPNG_BUILD_BASE_TYPE >= PNG_LIBPNG_BUILD_RC |
John Bowler | 2bc76ff | 2012-03-18 22:37:25 -0500 | [diff] [blame] | 67 | png_ptr->flags |= PNG_FLAG_APP_WARNINGS_WARN; |
| 68 | # endif |
John Bowler | aa816c4 | 2012-03-16 07:39:49 -0500 | [diff] [blame] | 69 | # endif |
| 70 | |
John Bowler | 42a2b55 | 2012-03-05 20:57:40 -0600 | [diff] [blame] | 71 | /* TODO: delay this, it can be done in png_init_io (if the app doesn't |
| 72 | * do it itself) avoiding setting the default function if it is not |
| 73 | * required. |
John Bowler | 1d7f56a | 2011-12-21 20:14:23 -0600 | [diff] [blame] | 74 | */ |
John Bowler | 42a2b55 | 2012-03-05 20:57:40 -0600 | [diff] [blame] | 75 | png_set_read_fn(png_ptr, NULL, NULL); |
Glenn Randers-Pehrson | a93c942 | 2009-04-13 11:41:33 -0500 | [diff] [blame] | 76 | } |
| 77 | |
John Bowler | 42a2b55 | 2012-03-05 20:57:40 -0600 | [diff] [blame] | 78 | return png_ptr; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 79 | } |
| 80 | |
Glenn Randers-Pehrson | 8b6a889 | 2001-05-18 04:54:50 -0500 | [diff] [blame] | 81 | |
Glenn Randers-Pehrson | dbd4014 | 2009-08-31 08:42:02 -0500 | [diff] [blame] | 82 | #ifdef PNG_SEQUENTIAL_READ_SUPPORTED |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 83 | /* Read the information before the actual image data. This has been |
Glenn Randers-Pehrson | f9f2fe0 | 1998-03-15 18:20:23 -0600 | [diff] [blame] | 84 | * 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] | 85 | * 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] | 86 | * 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] | 87 | * via png_set_sig_bytes(), and we will only check the remaining bytes |
| 88 | * here. The application can then have access to the signature bytes we |
| 89 | * read if it is determined that this isn't a valid PNG file. |
| 90 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 91 | void PNGAPI |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 92 | png_read_info(png_structrp png_ptr, png_inforp info_ptr) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 93 | { |
Glenn Randers-Pehrson | c81bb8a | 2009-08-15 22:02:26 -0500 | [diff] [blame] | 94 | png_debug(1, "in png_read_info"); |
Glenn Randers-Pehrson | 821b710 | 2010-06-24 16:16:32 -0500 | [diff] [blame] | 95 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 96 | if (png_ptr == NULL || info_ptr == NULL) |
| 97 | return; |
Glenn Randers-Pehrson | 821b710 | 2010-06-24 16:16:32 -0500 | [diff] [blame] | 98 | |
Glenn Randers-Pehrson | a581556 | 2010-11-20 21:48:29 -0600 | [diff] [blame] | 99 | /* Read and check the PNG file signature. */ |
| 100 | png_read_sig(png_ptr, info_ptr); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 101 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 102 | for (;;) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 103 | { |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 104 | png_uint_32 length = png_read_chunk_header(png_ptr); |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 105 | png_uint_32 chunk_name = png_ptr->chunk_name; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 106 | |
| 107 | /* This should be a binary subdivision search or a hash for |
| 108 | * matching the chunk name rather than a linear search. |
| 109 | */ |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 110 | if (chunk_name == png_IDAT) |
Glenn Randers-Pehrson | 262d0ff | 2010-03-03 07:06:54 -0600 | [diff] [blame] | 111 | if (png_ptr->mode & PNG_AFTER_IDAT) |
| 112 | png_ptr->mode |= PNG_HAVE_CHUNK_AFTER_IDAT; |
Glenn Randers-Pehrson | 6bc53be | 2006-06-16 07:52:03 -0500 | [diff] [blame] | 113 | |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 114 | if (chunk_name == png_IHDR) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 115 | png_handle_IHDR(png_ptr, info_ptr, length); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 116 | |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 117 | else if (chunk_name == png_IEND) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 118 | png_handle_IEND(png_ptr, info_ptr, length); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 119 | |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 120 | #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 121 | else if (png_chunk_unknown_handling(png_ptr, chunk_name) != |
| 122 | PNG_HANDLE_CHUNK_AS_DEFAULT) |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 123 | { |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 124 | if (chunk_name == png_IDAT) |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 125 | png_ptr->mode |= PNG_HAVE_IDAT; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 126 | |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 127 | png_handle_unknown(png_ptr, info_ptr, length); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 128 | |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 129 | if (chunk_name == png_PLTE) |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 130 | png_ptr->mode |= PNG_HAVE_PLTE; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 131 | |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 132 | else if (chunk_name == png_IDAT) |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 133 | { |
| 134 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
| 135 | png_error(png_ptr, "Missing IHDR before IDAT"); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 136 | |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 137 | else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE && |
Glenn Randers-Pehrson | 262d0ff | 2010-03-03 07:06:54 -0600 | [diff] [blame] | 138 | !(png_ptr->mode & PNG_HAVE_PLTE)) |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 139 | png_error(png_ptr, "Missing PLTE before IDAT"); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 140 | |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 141 | break; |
| 142 | } |
| 143 | } |
| 144 | #endif |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 145 | else if (chunk_name == png_PLTE) |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 146 | png_handle_PLTE(png_ptr, info_ptr, length); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 147 | |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 148 | else if (chunk_name == png_IDAT) |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 149 | { |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 150 | if (!(png_ptr->mode & PNG_HAVE_IHDR)) |
| 151 | png_error(png_ptr, "Missing IHDR before IDAT"); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 152 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 153 | else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE && |
Glenn Randers-Pehrson | 262d0ff | 2010-03-03 07:06:54 -0600 | [diff] [blame] | 154 | !(png_ptr->mode & PNG_HAVE_PLTE)) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 155 | png_error(png_ptr, "Missing PLTE before IDAT"); |
| 156 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 157 | png_ptr->idat_size = length; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 158 | png_ptr->mode |= PNG_HAVE_IDAT; |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 159 | break; |
| 160 | } |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 161 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 162 | #ifdef PNG_READ_bKGD_SUPPORTED |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 163 | else if (chunk_name == png_bKGD) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 164 | png_handle_bKGD(png_ptr, info_ptr, length); |
| 165 | #endif |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 166 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 167 | #ifdef PNG_READ_cHRM_SUPPORTED |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 168 | else if (chunk_name == png_cHRM) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 169 | png_handle_cHRM(png_ptr, info_ptr, length); |
| 170 | #endif |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 171 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 172 | #ifdef PNG_READ_gAMA_SUPPORTED |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 173 | else if (chunk_name == png_gAMA) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 174 | png_handle_gAMA(png_ptr, info_ptr, length); |
| 175 | #endif |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 176 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 177 | #ifdef PNG_READ_hIST_SUPPORTED |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 178 | else if (chunk_name == png_hIST) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 179 | png_handle_hIST(png_ptr, info_ptr, length); |
| 180 | #endif |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 181 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 182 | #ifdef PNG_READ_oFFs_SUPPORTED |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 183 | else if (chunk_name == png_oFFs) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 184 | png_handle_oFFs(png_ptr, info_ptr, length); |
| 185 | #endif |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 186 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 187 | #ifdef PNG_READ_pCAL_SUPPORTED |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 188 | else if (chunk_name == png_pCAL) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 189 | png_handle_pCAL(png_ptr, info_ptr, length); |
| 190 | #endif |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 191 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 192 | #ifdef PNG_READ_sCAL_SUPPORTED |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 193 | else if (chunk_name == png_sCAL) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 194 | png_handle_sCAL(png_ptr, info_ptr, length); |
| 195 | #endif |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 196 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 197 | #ifdef PNG_READ_pHYs_SUPPORTED |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 198 | else if (chunk_name == png_pHYs) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 199 | png_handle_pHYs(png_ptr, info_ptr, length); |
| 200 | #endif |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 201 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 202 | #ifdef PNG_READ_sBIT_SUPPORTED |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 203 | else if (chunk_name == png_sBIT) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 204 | png_handle_sBIT(png_ptr, info_ptr, length); |
| 205 | #endif |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 206 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 207 | #ifdef PNG_READ_sRGB_SUPPORTED |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 208 | else if (chunk_name == png_sRGB) |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 209 | png_handle_sRGB(png_ptr, info_ptr, length); |
| 210 | #endif |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 211 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 212 | #ifdef PNG_READ_iCCP_SUPPORTED |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 213 | else if (chunk_name == png_iCCP) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 214 | png_handle_iCCP(png_ptr, info_ptr, length); |
| 215 | #endif |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 216 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 217 | #ifdef PNG_READ_sPLT_SUPPORTED |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 218 | else if (chunk_name == png_sPLT) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 219 | png_handle_sPLT(png_ptr, info_ptr, length); |
| 220 | #endif |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 221 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 222 | #ifdef PNG_READ_tEXt_SUPPORTED |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 223 | else if (chunk_name == png_tEXt) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 224 | png_handle_tEXt(png_ptr, info_ptr, length); |
| 225 | #endif |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 226 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 227 | #ifdef PNG_READ_tIME_SUPPORTED |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 228 | else if (chunk_name == png_tIME) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 229 | png_handle_tIME(png_ptr, info_ptr, length); |
| 230 | #endif |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 231 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 232 | #ifdef PNG_READ_tRNS_SUPPORTED |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 233 | else if (chunk_name == png_tRNS) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 234 | png_handle_tRNS(png_ptr, info_ptr, length); |
| 235 | #endif |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 236 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 237 | #ifdef PNG_READ_zTXt_SUPPORTED |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 238 | else if (chunk_name == png_zTXt) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 239 | png_handle_zTXt(png_ptr, info_ptr, length); |
| 240 | #endif |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 241 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 242 | #ifdef PNG_READ_iTXt_SUPPORTED |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 243 | else if (chunk_name == png_iTXt) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 244 | png_handle_iTXt(png_ptr, info_ptr, length); |
| 245 | #endif |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 246 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 247 | else |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 248 | png_handle_unknown(png_ptr, info_ptr, length); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 249 | } |
| 250 | } |
Glenn Randers-Pehrson | dbd4014 | 2009-08-31 08:42:02 -0500 | [diff] [blame] | 251 | #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 252 | |
Glenn Randers-Pehrson | d8eb62f | 2009-05-30 20:19:20 -0500 | [diff] [blame] | 253 | /* Optional call to update the users info_ptr structure */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 254 | void PNGAPI |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 255 | png_read_update_info(png_structrp png_ptr, png_inforp info_ptr) |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 256 | { |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 257 | png_debug(1, "in png_read_update_info"); |
Glenn Randers-Pehrson | 821b710 | 2010-06-24 16:16:32 -0500 | [diff] [blame] | 258 | |
John Bowler | 66efa24 | 2012-03-18 21:10:29 -0500 | [diff] [blame] | 259 | if (png_ptr != NULL) |
| 260 | { |
| 261 | if ((png_ptr->flags & PNG_FLAG_ROW_INIT) == 0) |
| 262 | { |
| 263 | png_read_start_row(png_ptr); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 264 | |
John Bowler | 66efa24 | 2012-03-18 21:10:29 -0500 | [diff] [blame] | 265 | # ifdef PNG_READ_TRANSFORMS_SUPPORTED |
| 266 | png_read_transform_info(png_ptr, info_ptr); |
| 267 | # else |
| 268 | PNG_UNUSED(info_ptr) |
| 269 | # endif |
| 270 | } |
Glenn Randers-Pehrson | 4cfdb3c | 2009-11-26 11:49:37 -0600 | [diff] [blame] | 271 | |
John Bowler | 66efa24 | 2012-03-18 21:10:29 -0500 | [diff] [blame] | 272 | /* New in 1.6.0 this avoids the bug of doing the initializations twice */ |
| 273 | else |
John Bowler | 2bc76ff | 2012-03-18 22:37:25 -0500 | [diff] [blame] | 274 | png_app_error(png_ptr, |
John Bowler | 66efa24 | 2012-03-18 21:10:29 -0500 | [diff] [blame] | 275 | "png_read_update_info/png_start_read_image: duplicate call"); |
| 276 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 277 | } |
| 278 | |
Glenn Randers-Pehrson | dbd4014 | 2009-08-31 08:42:02 -0500 | [diff] [blame] | 279 | #ifdef PNG_SEQUENTIAL_READ_SUPPORTED |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 280 | /* Initialize palette, background, etc, after transformations |
| 281 | * are set, but before any reading takes place. This allows |
Glenn Randers-Pehrson | 345bc27 | 1998-06-14 14:43:31 -0500 | [diff] [blame] | 282 | * the user to obtain a gamma-corrected palette, for example. |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 283 | * If the user doesn't call this, we will do it ourselves. |
| 284 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 285 | void PNGAPI |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 286 | png_start_read_image(png_structrp png_ptr) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 287 | { |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 288 | png_debug(1, "in png_start_read_image"); |
Glenn Randers-Pehrson | 821b710 | 2010-06-24 16:16:32 -0500 | [diff] [blame] | 289 | |
Glenn Randers-Pehrson | 3dbfd30 | 2011-10-13 17:24:36 -0500 | [diff] [blame] | 290 | if (png_ptr != NULL) |
John Bowler | 66efa24 | 2012-03-18 21:10:29 -0500 | [diff] [blame] | 291 | { |
| 292 | if ((png_ptr->flags & PNG_FLAG_ROW_INIT) == 0) |
| 293 | png_read_start_row(png_ptr); |
| 294 | |
| 295 | /* New in 1.6.0 this avoids the bug of doing the initializations twice */ |
| 296 | else |
John Bowler | 2bc76ff | 2012-03-18 22:37:25 -0500 | [diff] [blame] | 297 | png_app_error(png_ptr, |
John Bowler | 66efa24 | 2012-03-18 21:10:29 -0500 | [diff] [blame] | 298 | "png_start_read_image/png_read_update_info: duplicate call"); |
| 299 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 300 | } |
Glenn Randers-Pehrson | dbd4014 | 2009-08-31 08:42:02 -0500 | [diff] [blame] | 301 | #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 302 | |
Glenn Randers-Pehrson | dbd4014 | 2009-08-31 08:42:02 -0500 | [diff] [blame] | 303 | #ifdef PNG_SEQUENTIAL_READ_SUPPORTED |
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_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] | 306 | { |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 307 | png_row_info row_info; |
| 308 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 309 | if (png_ptr == NULL) |
| 310 | return; |
Glenn Randers-Pehrson | 821b710 | 2010-06-24 16:16:32 -0500 | [diff] [blame] | 311 | |
Glenn Randers-Pehrson | 6d75d0c | 2009-08-22 08:45:09 -0500 | [diff] [blame] | 312 | png_debug2(1, "in png_read_row (row %lu, pass %d)", |
Glenn Randers-Pehrson | d233287 | 2010-10-12 19:19:28 -0500 | [diff] [blame] | 313 | (unsigned long)png_ptr->row_number, png_ptr->pass); |
Glenn Randers-Pehrson | 6d75d0c | 2009-08-22 08:45:09 -0500 | [diff] [blame] | 314 | |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 315 | /* png_read_start_row sets the information (in particular iwidth) for this |
| 316 | * interlace pass. |
| 317 | */ |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 318 | if (!(png_ptr->flags & PNG_FLAG_ROW_INIT)) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 319 | png_read_start_row(png_ptr); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 320 | |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 321 | /* 1.5.6: row_info moved out of png_struct to a local here. */ |
| 322 | row_info.width = png_ptr->iwidth; /* NOTE: width of current interlaced row */ |
| 323 | row_info.color_type = png_ptr->color_type; |
| 324 | row_info.bit_depth = png_ptr->bit_depth; |
| 325 | row_info.channels = png_ptr->channels; |
| 326 | row_info.pixel_depth = png_ptr->pixel_depth; |
| 327 | row_info.rowbytes = PNG_ROWBYTES(row_info.pixel_depth, row_info.width); |
| 328 | |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 329 | if (png_ptr->row_number == 0 && png_ptr->pass == 0) |
| 330 | { |
Glenn Randers-Pehrson | d8eb62f | 2009-05-30 20:19:20 -0500 | [diff] [blame] | 331 | /* Check for transforms that have been set but were defined out */ |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 332 | #if defined(PNG_WRITE_INVERT_SUPPORTED) && !defined(PNG_READ_INVERT_SUPPORTED) |
| 333 | if (png_ptr->transformations & PNG_INVERT_MONO) |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 334 | png_warning(png_ptr, "PNG_READ_INVERT_SUPPORTED is not defined"); |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 335 | #endif |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 336 | |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 337 | #if defined(PNG_WRITE_FILLER_SUPPORTED) && !defined(PNG_READ_FILLER_SUPPORTED) |
| 338 | if (png_ptr->transformations & PNG_FILLER) |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 339 | png_warning(png_ptr, "PNG_READ_FILLER_SUPPORTED is not defined"); |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 340 | #endif |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 341 | |
Glenn Randers-Pehrson | e3f3c4e | 2010-02-07 18:08:50 -0600 | [diff] [blame] | 342 | #if defined(PNG_WRITE_PACKSWAP_SUPPORTED) && \ |
| 343 | !defined(PNG_READ_PACKSWAP_SUPPORTED) |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 344 | if (png_ptr->transformations & PNG_PACKSWAP) |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 345 | png_warning(png_ptr, "PNG_READ_PACKSWAP_SUPPORTED is not defined"); |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 346 | #endif |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 347 | |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 348 | #if defined(PNG_WRITE_PACK_SUPPORTED) && !defined(PNG_READ_PACK_SUPPORTED) |
| 349 | if (png_ptr->transformations & PNG_PACK) |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 350 | png_warning(png_ptr, "PNG_READ_PACK_SUPPORTED is not defined"); |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 351 | #endif |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 352 | |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 353 | #if defined(PNG_WRITE_SHIFT_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED) |
| 354 | if (png_ptr->transformations & PNG_SHIFT) |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 355 | png_warning(png_ptr, "PNG_READ_SHIFT_SUPPORTED is not defined"); |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 356 | #endif |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 357 | |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 358 | #if defined(PNG_WRITE_BGR_SUPPORTED) && !defined(PNG_READ_BGR_SUPPORTED) |
| 359 | if (png_ptr->transformations & PNG_BGR) |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 360 | png_warning(png_ptr, "PNG_READ_BGR_SUPPORTED is not defined"); |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 361 | #endif |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 362 | |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 363 | #if defined(PNG_WRITE_SWAP_SUPPORTED) && !defined(PNG_READ_SWAP_SUPPORTED) |
| 364 | if (png_ptr->transformations & PNG_SWAP_BYTES) |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 365 | png_warning(png_ptr, "PNG_READ_SWAP_SUPPORTED is not defined"); |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 366 | #endif |
| 367 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 368 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 369 | #ifdef PNG_READ_INTERLACING_SUPPORTED |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 370 | /* If interlaced and we do not need a new row, combine row and return. |
| 371 | * Notice that the pixels we have from previous rows have been transformed |
| 372 | * already; we can only combine like with like (transformed or |
| 373 | * untransformed) and, because of the libpng API for interlaced images, this |
| 374 | * means we must transform before de-interlacing. |
| 375 | */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 376 | if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE)) |
| 377 | { |
| 378 | switch (png_ptr->pass) |
| 379 | { |
| 380 | case 0: |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 381 | if (png_ptr->row_number & 0x07) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 382 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 383 | if (dsp_row != NULL) |
Glenn Randers-Pehrson | 65c0339 | 2011-10-06 21:54:17 -0500 | [diff] [blame] | 384 | png_combine_row(png_ptr, dsp_row, 1/*display*/); |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 385 | png_read_finish_row(png_ptr); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 386 | return; |
| 387 | } |
| 388 | break; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 389 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 390 | case 1: |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 391 | if ((png_ptr->row_number & 0x07) || png_ptr->width < 5) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 392 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 393 | if (dsp_row != NULL) |
Glenn Randers-Pehrson | 65c0339 | 2011-10-06 21:54:17 -0500 | [diff] [blame] | 394 | png_combine_row(png_ptr, dsp_row, 1/*display*/); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 395 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 396 | png_read_finish_row(png_ptr); |
| 397 | return; |
| 398 | } |
| 399 | break; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 400 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 401 | case 2: |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 402 | if ((png_ptr->row_number & 0x07) != 4) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 403 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 404 | if (dsp_row != NULL && (png_ptr->row_number & 4)) |
Glenn Randers-Pehrson | 65c0339 | 2011-10-06 21:54:17 -0500 | [diff] [blame] | 405 | png_combine_row(png_ptr, dsp_row, 1/*display*/); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 406 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 407 | png_read_finish_row(png_ptr); |
| 408 | return; |
| 409 | } |
| 410 | break; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 411 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 412 | case 3: |
| 413 | if ((png_ptr->row_number & 3) || png_ptr->width < 3) |
| 414 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 415 | if (dsp_row != NULL) |
Glenn Randers-Pehrson | 65c0339 | 2011-10-06 21:54:17 -0500 | [diff] [blame] | 416 | png_combine_row(png_ptr, dsp_row, 1/*display*/); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 417 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 418 | png_read_finish_row(png_ptr); |
| 419 | return; |
| 420 | } |
| 421 | break; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 422 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 423 | case 4: |
| 424 | if ((png_ptr->row_number & 3) != 2) |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 425 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 426 | if (dsp_row != NULL && (png_ptr->row_number & 2)) |
Glenn Randers-Pehrson | 65c0339 | 2011-10-06 21:54:17 -0500 | [diff] [blame] | 427 | png_combine_row(png_ptr, dsp_row, 1/*display*/); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 428 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 429 | png_read_finish_row(png_ptr); |
| 430 | return; |
| 431 | } |
| 432 | break; |
Glenn Randers-Pehrson | 864270e | 2012-02-10 17:13:13 -0600 | [diff] [blame] | 433 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 434 | case 5: |
| 435 | if ((png_ptr->row_number & 1) || png_ptr->width < 2) |
| 436 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 437 | if (dsp_row != NULL) |
Glenn Randers-Pehrson | 65c0339 | 2011-10-06 21:54:17 -0500 | [diff] [blame] | 438 | png_combine_row(png_ptr, dsp_row, 1/*display*/); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 439 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 440 | png_read_finish_row(png_ptr); |
| 441 | return; |
| 442 | } |
| 443 | break; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 444 | |
Glenn Randers-Pehrson | b3edc73 | 2010-11-21 14:06:41 -0600 | [diff] [blame] | 445 | default: |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 446 | case 6: |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 447 | if (!(png_ptr->row_number & 1)) |
| 448 | { |
| 449 | png_read_finish_row(png_ptr); |
| 450 | return; |
| 451 | } |
| 452 | break; |
| 453 | } |
| 454 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 455 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 456 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 457 | if (!(png_ptr->mode & PNG_HAVE_IDAT)) |
| 458 | png_error(png_ptr, "Invalid attempt to read row data"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 459 | |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 460 | /* Fill the row with IDAT data: */ |
| 461 | png_read_IDAT_data(png_ptr, png_ptr->row_buf, row_info.rowbytes + 1); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 462 | |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 463 | if (png_ptr->row_buf[0] > PNG_FILTER_VALUE_NONE) |
| 464 | { |
| 465 | if (png_ptr->row_buf[0] < PNG_FILTER_VALUE_LAST) |
Mans Rullgard | d3a9480 | 2011-11-03 00:47:55 -0500 | [diff] [blame] | 466 | 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] | 467 | png_ptr->prev_row + 1, png_ptr->row_buf[0]); |
| 468 | else |
| 469 | png_error(png_ptr, "bad adaptive filter value"); |
| 470 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 471 | |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 472 | /* libpng 1.5.6: the following line was copying png_ptr->rowbytes before |
| 473 | * 1.5.6, while the buffer really is this big in current versions of libpng |
| 474 | * it may not be in the future, so this was changed just to copy the |
| 475 | * interlaced count: |
| 476 | */ |
| 477 | 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] | 478 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 479 | #ifdef PNG_MNG_FEATURES_SUPPORTED |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 480 | if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) && |
Glenn Randers-Pehrson | 262d0ff | 2010-03-03 07:06:54 -0600 | [diff] [blame] | 481 | (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING)) |
Glenn Randers-Pehrson | 2ad31ae | 2000-12-15 08:54:42 -0600 | [diff] [blame] | 482 | { |
| 483 | /* Intrapixel differencing */ |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 484 | png_do_read_intrapixel(&row_info, png_ptr->row_buf + 1); |
Glenn Randers-Pehrson | 2ad31ae | 2000-12-15 08:54:42 -0600 | [diff] [blame] | 485 | } |
| 486 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 487 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 488 | |
John Bowler | 4a12f4a | 2011-04-17 18:34:22 -0500 | [diff] [blame] | 489 | #ifdef PNG_READ_TRANSFORMS_SUPPORTED |
John Bowler | 9b872f4 | 2011-02-12 09:00:16 -0600 | [diff] [blame] | 490 | if (png_ptr->transformations) |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 491 | png_do_read_transformations(png_ptr, &row_info); |
John Bowler | 4a12f4a | 2011-04-17 18:34:22 -0500 | [diff] [blame] | 492 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 493 | |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 494 | /* The transformed pixel depth should match the depth now in row_info. */ |
| 495 | if (png_ptr->transformed_pixel_depth == 0) |
| 496 | { |
| 497 | png_ptr->transformed_pixel_depth = row_info.pixel_depth; |
| 498 | if (row_info.pixel_depth > png_ptr->maximum_pixel_depth) |
| 499 | png_error(png_ptr, "sequential row overflow"); |
| 500 | } |
| 501 | |
| 502 | else if (png_ptr->transformed_pixel_depth != row_info.pixel_depth) |
| 503 | png_error(png_ptr, "internal sequential row size calculation error"); |
| 504 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 505 | #ifdef PNG_READ_INTERLACING_SUPPORTED |
Glenn Randers-Pehrson | d8eb62f | 2009-05-30 20:19:20 -0500 | [diff] [blame] | 506 | /* Blow up interlaced rows to full size */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 507 | if (png_ptr->interlaced && |
| 508 | (png_ptr->transformations & PNG_INTERLACE)) |
| 509 | { |
| 510 | if (png_ptr->pass < 6) |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 511 | png_do_read_interlace(&row_info, png_ptr->row_buf + 1, png_ptr->pass, |
| 512 | png_ptr->transformations); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 513 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 514 | if (dsp_row != NULL) |
Glenn Randers-Pehrson | 65c0339 | 2011-10-06 21:54:17 -0500 | [diff] [blame] | 515 | png_combine_row(png_ptr, dsp_row, 1/*display*/); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 516 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 517 | if (row != NULL) |
Glenn Randers-Pehrson | 65c0339 | 2011-10-06 21:54:17 -0500 | [diff] [blame] | 518 | png_combine_row(png_ptr, row, 0/*row*/); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 519 | } |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 520 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 521 | else |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 522 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 523 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 524 | if (row != NULL) |
Glenn Randers-Pehrson | 65c0339 | 2011-10-06 21:54:17 -0500 | [diff] [blame] | 525 | png_combine_row(png_ptr, row, -1/*ignored*/); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 526 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 527 | if (dsp_row != NULL) |
Glenn Randers-Pehrson | 65c0339 | 2011-10-06 21:54:17 -0500 | [diff] [blame] | 528 | png_combine_row(png_ptr, dsp_row, -1/*ignored*/); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 529 | } |
| 530 | png_read_finish_row(png_ptr); |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 531 | |
| 532 | if (png_ptr->read_row_fn != NULL) |
| 533 | (*(png_ptr->read_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass); |
Glenn Randers-Pehrson | 67dba43 | 2012-02-16 20:15:13 -0600 | [diff] [blame] | 534 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 535 | } |
Glenn Randers-Pehrson | dbd4014 | 2009-08-31 08:42:02 -0500 | [diff] [blame] | 536 | #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 537 | |
Glenn Randers-Pehrson | dbd4014 | 2009-08-31 08:42:02 -0500 | [diff] [blame] | 538 | #ifdef PNG_SEQUENTIAL_READ_SUPPORTED |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 539 | /* 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] | 540 | * and png_set_interlace_handling() has been called, the rows need to |
| 541 | * 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] | 542 | * image has alpha or transparency, and png_handle_alpha()[*] has been |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 543 | * called, the rows contents must be initialized to the contents of the |
| 544 | * screen. |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 545 | * |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 546 | * "row" holds the actual image, and pixels are placed in it |
| 547 | * as they arrive. If the image is displayed after each pass, it will |
| 548 | * appear to "sparkle" in. "display_row" can be used to display a |
| 549 | * "chunky" progressive image, with finer detail added as it becomes |
| 550 | * available. If you do not want this "chunky" display, you may pass |
| 551 | * NULL for display_row. If you do not want the sparkle display, and |
| 552 | * you have not called png_handle_alpha(), you may pass NULL for rows. |
| 553 | * If you have called png_handle_alpha(), and the image has either an |
| 554 | * alpha channel or a transparency chunk, you must provide a buffer for |
| 555 | * rows. In this case, you do not have to provide a display_row buffer |
| 556 | * also, but you may. If the image is not interlaced, or if you have |
| 557 | * not called png_set_interlace_handling(), the display_row buffer will |
| 558 | * be ignored, so pass NULL to it. |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 559 | * |
Glenn Randers-Pehrson | c3d51c1 | 2006-03-02 07:23:18 -0600 | [diff] [blame] | 560 | * [*] 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] | 561 | */ |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 562 | |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 563 | void PNGAPI |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 564 | png_read_rows(png_structrp png_ptr, png_bytepp row, |
Glenn Randers-Pehrson | 262d0ff | 2010-03-03 07:06:54 -0600 | [diff] [blame] | 565 | png_bytepp display_row, png_uint_32 num_rows) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 566 | { |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 567 | png_uint_32 i; |
| 568 | png_bytepp rp; |
| 569 | png_bytepp dp; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 570 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 571 | png_debug(1, "in png_read_rows"); |
Glenn Randers-Pehrson | 821b710 | 2010-06-24 16:16:32 -0500 | [diff] [blame] | 572 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 573 | if (png_ptr == NULL) |
| 574 | return; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 575 | |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 576 | rp = row; |
| 577 | dp = display_row; |
Glenn Randers-Pehrson | 8686fff | 1998-05-21 09:27:50 -0500 | [diff] [blame] | 578 | if (rp != NULL && dp != NULL) |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 579 | for (i = 0; i < num_rows; i++) |
| 580 | { |
| 581 | png_bytep rptr = *rp++; |
| 582 | png_bytep dptr = *dp++; |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 583 | |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 584 | png_read_row(png_ptr, rptr, dptr); |
| 585 | } |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 586 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 587 | else if (rp != NULL) |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 588 | for (i = 0; i < num_rows; i++) |
| 589 | { |
Glenn Randers-Pehrson | 8686fff | 1998-05-21 09:27:50 -0500 | [diff] [blame] | 590 | png_bytep rptr = *rp; |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 591 | png_read_row(png_ptr, rptr, NULL); |
Glenn Randers-Pehrson | 8686fff | 1998-05-21 09:27:50 -0500 | [diff] [blame] | 592 | rp++; |
| 593 | } |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 594 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 595 | else if (dp != NULL) |
Glenn Randers-Pehrson | 8686fff | 1998-05-21 09:27:50 -0500 | [diff] [blame] | 596 | for (i = 0; i < num_rows; i++) |
| 597 | { |
| 598 | png_bytep dptr = *dp; |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 599 | png_read_row(png_ptr, NULL, dptr); |
Glenn Randers-Pehrson | 8686fff | 1998-05-21 09:27:50 -0500 | [diff] [blame] | 600 | dp++; |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 601 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 602 | } |
Glenn Randers-Pehrson | dbd4014 | 2009-08-31 08:42:02 -0500 | [diff] [blame] | 603 | #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 604 | |
Glenn Randers-Pehrson | dbd4014 | 2009-08-31 08:42:02 -0500 | [diff] [blame] | 605 | #ifdef PNG_SEQUENTIAL_READ_SUPPORTED |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 606 | /* 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] | 607 | * 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] | 608 | * initialize the image to the current image that PNG will be overlaying. |
| 609 | * We set the num_rows again here, in case it was incorrectly set in |
| 610 | * png_read_start_row() by a call to png_read_update_info() or |
| 611 | * png_start_read_image() if png_set_interlace_handling() wasn't called |
| 612 | * prior to either of these functions like it should have been. You can |
| 613 | * only call this function once. If you desire to have an image for |
| 614 | * each pass of a interlaced image, use png_read_rows() instead. |
Glenn Randers-Pehrson | 345bc27 | 1998-06-14 14:43:31 -0500 | [diff] [blame] | 615 | * |
Glenn Randers-Pehrson | c3d51c1 | 2006-03-02 07:23:18 -0600 | [diff] [blame] | 616 | * [*] 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] | 617 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 618 | void PNGAPI |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 619 | png_read_image(png_structrp png_ptr, png_bytepp image) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 620 | { |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 621 | png_uint_32 i, image_height; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 622 | int pass, j; |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 623 | png_bytepp rp; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 624 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 625 | png_debug(1, "in png_read_image"); |
Glenn Randers-Pehrson | 821b710 | 2010-06-24 16:16:32 -0500 | [diff] [blame] | 626 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 627 | if (png_ptr == NULL) |
| 628 | return; |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 629 | |
| 630 | #ifdef PNG_READ_INTERLACING_SUPPORTED |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 631 | if (!(png_ptr->flags & PNG_FLAG_ROW_INIT)) |
| 632 | { |
| 633 | pass = png_set_interlace_handling(png_ptr); |
| 634 | /* And make sure transforms are initialized. */ |
| 635 | png_start_read_image(png_ptr); |
| 636 | } |
| 637 | else |
| 638 | { |
Glenn Randers-Pehrson | e15a96b | 2011-01-14 15:47:37 -0600 | [diff] [blame] | 639 | if (png_ptr->interlaced && !(png_ptr->transformations & PNG_INTERLACE)) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 640 | { |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 641 | /* Caller called png_start_read_image or png_read_update_info without |
| 642 | * first turning on the PNG_INTERLACE transform. We can fix this here, |
| 643 | * but the caller should do it! |
| 644 | */ |
| 645 | png_warning(png_ptr, "Interlace handling should be turned on when " |
| 646 | "using png_read_image"); |
| 647 | /* Make sure this is set correctly */ |
| 648 | png_ptr->num_rows = png_ptr->height; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 649 | } |
| 650 | |
| 651 | /* Obtain the pass number, which also turns on the PNG_INTERLACE flag in |
| 652 | * the above error case. |
| 653 | */ |
| 654 | pass = png_set_interlace_handling(png_ptr); |
| 655 | } |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 656 | #else |
| 657 | if (png_ptr->interlaced) |
| 658 | png_error(png_ptr, |
Glenn Randers-Pehrson | 262d0ff | 2010-03-03 07:06:54 -0600 | [diff] [blame] | 659 | "Cannot read interlaced image -- interlace handler disabled"); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 660 | |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 661 | pass = 1; |
| 662 | #endif |
| 663 | |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 664 | image_height=png_ptr->height; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 665 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 666 | for (j = 0; j < pass; j++) |
| 667 | { |
| 668 | rp = image; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 669 | for (i = 0; i < image_height; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 670 | { |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 671 | png_read_row(png_ptr, *rp, NULL); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 672 | rp++; |
| 673 | } |
| 674 | } |
| 675 | } |
Glenn Randers-Pehrson | dbd4014 | 2009-08-31 08:42:02 -0500 | [diff] [blame] | 676 | #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 677 | |
Glenn Randers-Pehrson | dbd4014 | 2009-08-31 08:42:02 -0500 | [diff] [blame] | 678 | #ifdef PNG_SEQUENTIAL_READ_SUPPORTED |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 679 | /* 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] | 680 | * file, will verify the end is accurate, and will read any comments |
| 681 | * or time information at the end of the file, if info is not NULL. |
| 682 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 683 | void PNGAPI |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 684 | png_read_end(png_structrp png_ptr, png_inforp info_ptr) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 685 | { |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 686 | png_debug(1, "in png_read_end"); |
Glenn Randers-Pehrson | 821b710 | 2010-06-24 16:16:32 -0500 | [diff] [blame] | 687 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 688 | if (png_ptr == NULL) |
| 689 | return; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 690 | |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 691 | /* If png_read_end is called in the middle of reading the rows there may |
| 692 | * still be pending IDAT data and an owned zstream. Deal with this here. |
| 693 | */ |
| 694 | png_read_finish_IDAT(png_ptr); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 695 | |
John Bowler | 6f237b6 | 2012-03-02 13:13:15 -0600 | [diff] [blame] | 696 | #ifdef PNG_READ_CHECK_FOR_INVALID_INDEX_SUPPORTED |
| 697 | /* Report invalid palette index; added at libng-1.5.10 */ |
| 698 | if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE && |
| 699 | png_ptr->num_palette_max > png_ptr->num_palette) |
Glenn Randers-Pehrson | 945cb1f | 2012-03-10 08:48:04 -0600 | [diff] [blame] | 700 | png_benign_error(png_ptr, "Read palette index exceeding num_palette"); |
John Bowler | 6f237b6 | 2012-03-02 13:13:15 -0600 | [diff] [blame] | 701 | #endif |
| 702 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 703 | do |
| 704 | { |
Glenn Randers-Pehrson | 895a9c9 | 2008-07-25 08:51:18 -0500 | [diff] [blame] | 705 | png_uint_32 length = png_read_chunk_header(png_ptr); |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 706 | png_uint_32 chunk_name = png_ptr->chunk_name; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 707 | |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 708 | if (chunk_name == png_IHDR) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 709 | png_handle_IHDR(png_ptr, info_ptr, length); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 710 | |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 711 | else if (chunk_name == png_IEND) |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 712 | png_handle_IEND(png_ptr, info_ptr, length); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 713 | |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 714 | #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 715 | else if (png_chunk_unknown_handling(png_ptr, chunk_name) != |
| 716 | PNG_HANDLE_CHUNK_AS_DEFAULT) |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 717 | { |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 718 | if (chunk_name == png_IDAT) |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 719 | { |
Glenn Randers-Pehrson | 6bc53be | 2006-06-16 07:52:03 -0500 | [diff] [blame] | 720 | if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT)) |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 721 | png_benign_error(png_ptr, "Too many IDATs found"); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 722 | } |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 723 | png_handle_unknown(png_ptr, info_ptr, length); |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 724 | if (chunk_name == png_PLTE) |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 725 | png_ptr->mode |= PNG_HAVE_PLTE; |
| 726 | } |
| 727 | #endif |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 728 | |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 729 | else if (chunk_name == png_IDAT) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 730 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 731 | /* Zero length IDATs are legal after the last IDAT has been |
| 732 | * read, but not after other chunks have been read. |
| 733 | */ |
Glenn Randers-Pehrson | 6bc53be | 2006-06-16 07:52:03 -0500 | [diff] [blame] | 734 | if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT)) |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 735 | png_benign_error(png_ptr, "Too many IDATs found"); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 736 | |
Glenn Randers-Pehrson | 4accabb | 2000-04-14 14:20:47 -0500 | [diff] [blame] | 737 | png_crc_finish(png_ptr, length); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 738 | } |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 739 | else if (chunk_name == png_PLTE) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 740 | png_handle_PLTE(png_ptr, info_ptr, length); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 741 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 742 | #ifdef PNG_READ_bKGD_SUPPORTED |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 743 | else if (chunk_name == png_bKGD) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 744 | png_handle_bKGD(png_ptr, info_ptr, length); |
| 745 | #endif |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 746 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 747 | #ifdef PNG_READ_cHRM_SUPPORTED |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 748 | else if (chunk_name == png_cHRM) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 749 | png_handle_cHRM(png_ptr, info_ptr, length); |
| 750 | #endif |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 751 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 752 | #ifdef PNG_READ_gAMA_SUPPORTED |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 753 | else if (chunk_name == png_gAMA) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 754 | png_handle_gAMA(png_ptr, info_ptr, length); |
| 755 | #endif |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 756 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 757 | #ifdef PNG_READ_hIST_SUPPORTED |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 758 | else if (chunk_name == png_hIST) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 759 | png_handle_hIST(png_ptr, info_ptr, length); |
| 760 | #endif |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 761 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 762 | #ifdef PNG_READ_oFFs_SUPPORTED |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 763 | else if (chunk_name == png_oFFs) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 764 | png_handle_oFFs(png_ptr, info_ptr, length); |
| 765 | #endif |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 766 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 767 | #ifdef PNG_READ_pCAL_SUPPORTED |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 768 | else if (chunk_name == png_pCAL) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 769 | png_handle_pCAL(png_ptr, info_ptr, length); |
| 770 | #endif |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 771 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 772 | #ifdef PNG_READ_sCAL_SUPPORTED |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 773 | else if (chunk_name == png_sCAL) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 774 | png_handle_sCAL(png_ptr, info_ptr, length); |
| 775 | #endif |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 776 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 777 | #ifdef PNG_READ_pHYs_SUPPORTED |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 778 | else if (chunk_name == png_pHYs) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 779 | png_handle_pHYs(png_ptr, info_ptr, length); |
| 780 | #endif |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 781 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 782 | #ifdef PNG_READ_sBIT_SUPPORTED |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 783 | else if (chunk_name == png_sBIT) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 784 | png_handle_sBIT(png_ptr, info_ptr, length); |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 785 | #endif |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 786 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 787 | #ifdef PNG_READ_sRGB_SUPPORTED |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 788 | else if (chunk_name == png_sRGB) |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 789 | png_handle_sRGB(png_ptr, info_ptr, length); |
| 790 | #endif |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 791 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 792 | #ifdef PNG_READ_iCCP_SUPPORTED |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 793 | else if (chunk_name == png_iCCP) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 794 | png_handle_iCCP(png_ptr, info_ptr, length); |
| 795 | #endif |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 796 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 797 | #ifdef PNG_READ_sPLT_SUPPORTED |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 798 | else if (chunk_name == png_sPLT) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 799 | png_handle_sPLT(png_ptr, info_ptr, length); |
| 800 | #endif |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 801 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 802 | #ifdef PNG_READ_tEXt_SUPPORTED |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 803 | else if (chunk_name == png_tEXt) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 804 | png_handle_tEXt(png_ptr, info_ptr, length); |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 805 | #endif |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 806 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 807 | #ifdef PNG_READ_tIME_SUPPORTED |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 808 | else if (chunk_name == png_tIME) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 809 | png_handle_tIME(png_ptr, info_ptr, length); |
| 810 | #endif |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 811 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 812 | #ifdef PNG_READ_tRNS_SUPPORTED |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 813 | else if (chunk_name == png_tRNS) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 814 | png_handle_tRNS(png_ptr, info_ptr, length); |
| 815 | #endif |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 816 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 817 | #ifdef PNG_READ_zTXt_SUPPORTED |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 818 | else if (chunk_name == png_zTXt) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 819 | png_handle_zTXt(png_ptr, info_ptr, length); |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 820 | #endif |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 821 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 822 | #ifdef PNG_READ_iTXt_SUPPORTED |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 823 | else if (chunk_name == png_iTXt) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 824 | png_handle_iTXt(png_ptr, info_ptr, length); |
| 825 | #endif |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 826 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 827 | else |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 828 | png_handle_unknown(png_ptr, info_ptr, length); |
| 829 | } while (!(png_ptr->mode & PNG_HAVE_IEND)); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 830 | } |
Glenn Randers-Pehrson | dbd4014 | 2009-08-31 08:42:02 -0500 | [diff] [blame] | 831 | #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 832 | |
John Bowler | 1d7f56a | 2011-12-21 20:14:23 -0600 | [diff] [blame] | 833 | /* Free all memory used in the read struct */ |
| 834 | static void |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 835 | png_read_destroy(png_structrp png_ptr) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 836 | { |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 837 | png_debug(1, "in png_read_destroy"); |
Glenn Randers-Pehrson | 821b710 | 2010-06-24 16:16:32 -0500 | [diff] [blame] | 838 | |
John Bowler | 07772cb | 2011-10-14 18:19:47 -0500 | [diff] [blame] | 839 | #ifdef PNG_READ_GAMMA_SUPPORTED |
| 840 | png_destroy_gamma_table(png_ptr); |
| 841 | #endif |
| 842 | |
Glenn Randers-Pehrson | 1b8e567 | 2001-08-25 06:46:06 -0500 | [diff] [blame] | 843 | png_free(png_ptr, png_ptr->big_row_buf); |
Mans Rullgard | 1c42276 | 2011-10-17 16:52:19 -0500 | [diff] [blame] | 844 | png_free(png_ptr, png_ptr->big_prev_row); |
John Bowler | b5d0051 | 2012-03-09 09:15:18 -0600 | [diff] [blame] | 845 | png_free(png_ptr, png_ptr->read_buffer); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 846 | |
Glenn Randers-Pehrson | 3cd7cff | 2010-04-16 19:27:08 -0500 | [diff] [blame] | 847 | #ifdef PNG_READ_QUANTIZE_SUPPORTED |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 848 | png_free(png_ptr, png_ptr->palette_lookup); |
Glenn Randers-Pehrson | 3cd7cff | 2010-04-16 19:27:08 -0500 | [diff] [blame] | 849 | png_free(png_ptr, png_ptr->quantize_index); |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 850 | #endif |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 851 | |
Glenn Randers-Pehrson | a77ef62 | 2000-02-18 13:48:52 -0600 | [diff] [blame] | 852 | if (png_ptr->free_me & PNG_FREE_PLTE) |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 853 | png_zfree(png_ptr, png_ptr->palette); |
Glenn Randers-Pehrson | a77ef62 | 2000-02-18 13:48:52 -0600 | [diff] [blame] | 854 | png_ptr->free_me &= ~PNG_FREE_PLTE; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 855 | |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 856 | #if defined(PNG_tRNS_SUPPORTED) || \ |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 857 | defined(PNG_READ_EXPAND_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED) |
Glenn Randers-Pehrson | a77ef62 | 2000-02-18 13:48:52 -0600 | [diff] [blame] | 858 | if (png_ptr->free_me & PNG_FREE_TRNS) |
Glenn Randers-Pehrson | 6abea75 | 2009-08-08 16:52:06 -0500 | [diff] [blame] | 859 | png_free(png_ptr, png_ptr->trans_alpha); |
Glenn Randers-Pehrson | a77ef62 | 2000-02-18 13:48:52 -0600 | [diff] [blame] | 860 | png_ptr->free_me &= ~PNG_FREE_TRNS; |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 861 | #endif |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 862 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 863 | inflateEnd(&png_ptr->zstream); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 864 | |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 865 | #ifdef PNG_PROGRESSIVE_READ_SUPPORTED |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 866 | png_free(png_ptr, png_ptr->save_buffer); |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 867 | #endif |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 868 | |
John Bowler | 40b2603 | 2011-12-22 08:09:15 -0600 | [diff] [blame] | 869 | #ifdef PNG_UNKNOWN_CHUNKS_SUPPORTED |
| 870 | png_free(png_ptr, png_ptr->unknown_chunk.data); |
| 871 | #endif |
| 872 | |
| 873 | #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED |
| 874 | png_free(png_ptr, png_ptr->chunk_list); |
| 875 | #endif |
| 876 | |
John Bowler | 1d7f56a | 2011-12-21 20:14:23 -0600 | [diff] [blame] | 877 | /* NOTE: the 'setjmp' buffer may still be allocated and the memory and error |
| 878 | * callbacks are still set at this point. They are required to complete the |
| 879 | * destruction of the png_struct itself. |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 880 | */ |
John Bowler | 1d7f56a | 2011-12-21 20:14:23 -0600 | [diff] [blame] | 881 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 882 | |
John Bowler | 1d7f56a | 2011-12-21 20:14:23 -0600 | [diff] [blame] | 883 | /* Free all memory used by the read */ |
| 884 | void PNGAPI |
| 885 | png_destroy_read_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr, |
| 886 | png_infopp end_info_ptr_ptr) |
| 887 | { |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 888 | png_structrp png_ptr = NULL; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 889 | |
John Bowler | 1d7f56a | 2011-12-21 20:14:23 -0600 | [diff] [blame] | 890 | png_debug(1, "in png_destroy_read_struct"); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 891 | |
John Bowler | 1d7f56a | 2011-12-21 20:14:23 -0600 | [diff] [blame] | 892 | if (png_ptr_ptr != NULL) |
| 893 | png_ptr = *png_ptr_ptr; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 894 | |
John Bowler | 1d7f56a | 2011-12-21 20:14:23 -0600 | [diff] [blame] | 895 | if (png_ptr == NULL) |
| 896 | return; |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 897 | |
John Bowler | 1d7f56a | 2011-12-21 20:14:23 -0600 | [diff] [blame] | 898 | /* libpng 1.6.0: use the API to destroy info structs to ensure consistent |
| 899 | * behavior. Prior to 1.6.0 libpng did extra 'info' destruction in this API. |
| 900 | * The extra was, apparently, unnecessary yet this hides memory leak bugs. |
| 901 | */ |
| 902 | png_destroy_info_struct(png_ptr, end_info_ptr_ptr); |
| 903 | png_destroy_info_struct(png_ptr, info_ptr_ptr); |
| 904 | |
| 905 | *png_ptr_ptr = NULL; |
| 906 | png_read_destroy(png_ptr); |
| 907 | png_destroy_png_struct(png_ptr); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 908 | } |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 909 | |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 910 | void PNGAPI |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 911 | 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] | 912 | { |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 913 | if (png_ptr == NULL) |
| 914 | return; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 915 | |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 916 | png_ptr->read_row_fn = read_row_fn; |
| 917 | } |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 918 | |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 919 | |
Glenn Randers-Pehrson | dbd4014 | 2009-08-31 08:42:02 -0500 | [diff] [blame] | 920 | #ifdef PNG_SEQUENTIAL_READ_SUPPORTED |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 921 | #ifdef PNG_INFO_IMAGE_SUPPORTED |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 922 | void PNGAPI |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 923 | png_read_png(png_structrp png_ptr, png_inforp info_ptr, |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 924 | int transforms, |
| 925 | voidp params) |
| 926 | { |
| 927 | int row; |
| 928 | |
John Bowler | 6a6d79f | 2011-02-12 08:56:40 -0600 | [diff] [blame] | 929 | if (png_ptr == NULL || info_ptr == NULL) |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 930 | return; |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 931 | |
Glenn Randers-Pehrson | 272489d | 2004-08-04 06:34:52 -0500 | [diff] [blame] | 932 | /* png_read_info() gives us all of the information from the |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 933 | * PNG file before the first IDAT (image data chunk). |
| 934 | */ |
| 935 | png_read_info(png_ptr, info_ptr); |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 936 | if (info_ptr->height > PNG_UINT_32_MAX/png_sizeof(png_bytep)) |
| 937 | 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] | 938 | |
| 939 | /* -------------- image transformations start here ------------------- */ |
| 940 | |
Glenn Randers-Pehrson | aee83b4 | 2011-06-18 00:19:54 -0500 | [diff] [blame] | 941 | #ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED |
Glenn Randers-Pehrson | 55fbff3 | 2011-05-17 06:49:32 -0500 | [diff] [blame] | 942 | /* 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] | 943 | */ |
Glenn Randers-Pehrson | ab63dd0 | 2011-06-17 20:04:17 -0500 | [diff] [blame] | 944 | if (transforms & PNG_TRANSFORM_SCALE_16) |
Glenn Randers-Pehrson | aee83b4 | 2011-06-18 00:19:54 -0500 | [diff] [blame] | 945 | { |
Glenn Randers-Pehrson | 6da2f2d | 2011-06-17 23:07:16 -0500 | [diff] [blame] | 946 | /* Added at libpng-1.5.4. "strip_16" produces the same result that it |
| 947 | * did in earlier versions, while "scale_16" is now more accurate. |
| 948 | */ |
Glenn Randers-Pehrson | aee83b4 | 2011-06-18 00:19:54 -0500 | [diff] [blame] | 949 | png_set_scale_16(png_ptr); |
| 950 | } |
Glenn Randers-Pehrson | aee83b4 | 2011-06-18 00:19:54 -0500 | [diff] [blame] | 951 | #endif |
| 952 | |
| 953 | #ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED |
John Bowler | 8d26126 | 2011-06-18 13:37:11 -0500 | [diff] [blame] | 954 | /* If both SCALE and STRIP are required pngrtran will effectively cancel the |
| 955 | * latter by doing SCALE first. This is ok and allows apps not to check for |
| 956 | * which is supported to get the right answer. |
| 957 | */ |
| 958 | if (transforms & PNG_TRANSFORM_STRIP_16) |
| 959 | png_set_strip_16(png_ptr); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 960 | #endif |
| 961 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 962 | #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED |
Glenn Randers-Pehrson | 272489d | 2004-08-04 06:34:52 -0500 | [diff] [blame] | 963 | /* Strip alpha bytes from the input data without combining with |
| 964 | * the background (not recommended). |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 965 | */ |
| 966 | if (transforms & PNG_TRANSFORM_STRIP_ALPHA) |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 967 | png_set_strip_alpha(png_ptr); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 968 | #endif |
| 969 | |
Glenn Randers-Pehrson | a77ef62 | 2000-02-18 13:48:52 -0600 | [diff] [blame] | 970 | #if defined(PNG_READ_PACK_SUPPORTED) && !defined(PNG_READ_EXPAND_SUPPORTED) |
Glenn Randers-Pehrson | 272489d | 2004-08-04 06:34:52 -0500 | [diff] [blame] | 971 | /* 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] | 972 | * byte into separate bytes (useful for paletted and grayscale images). |
| 973 | */ |
| 974 | if (transforms & PNG_TRANSFORM_PACKING) |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 975 | png_set_packing(png_ptr); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 976 | #endif |
| 977 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 978 | #ifdef PNG_READ_PACKSWAP_SUPPORTED |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 979 | /* Change the order of packed pixels to least significant bit first |
Glenn Randers-Pehrson | 272489d | 2004-08-04 06:34:52 -0500 | [diff] [blame] | 980 | * (not useful if you are using png_set_packing). |
| 981 | */ |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 982 | if (transforms & PNG_TRANSFORM_PACKSWAP) |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 983 | png_set_packswap(png_ptr); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 984 | #endif |
| 985 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 986 | #ifdef PNG_READ_EXPAND_SUPPORTED |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 987 | /* Expand paletted colors into true RGB triplets |
| 988 | * Expand grayscale images to full 8 bits from 1, 2, or 4 bits/pixel |
| 989 | * Expand paletted or RGB images with transparency to full alpha |
| 990 | * channels so the data will be available as RGBA quartets. |
| 991 | */ |
| 992 | if (transforms & PNG_TRANSFORM_EXPAND) |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 993 | if ((png_ptr->bit_depth < 8) || |
| 994 | (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) || |
| 995 | (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))) |
Glenn Randers-Pehrson | a77ef62 | 2000-02-18 13:48:52 -0600 | [diff] [blame] | 996 | png_set_expand(png_ptr); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 997 | #endif |
| 998 | |
Glenn Randers-Pehrson | 3cd7cff | 2010-04-16 19:27:08 -0500 | [diff] [blame] | 999 | /* We don't handle background color or gamma transformation or quantizing. |
Glenn Randers-Pehrson | 272489d | 2004-08-04 06:34:52 -0500 | [diff] [blame] | 1000 | */ |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1001 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 1002 | #ifdef PNG_READ_INVERT_SUPPORTED |
Glenn Randers-Pehrson | d8eb62f | 2009-05-30 20:19:20 -0500 | [diff] [blame] | 1003 | /* 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] | 1004 | */ |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1005 | if (transforms & PNG_TRANSFORM_INVERT_MONO) |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 1006 | png_set_invert_mono(png_ptr); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1007 | #endif |
| 1008 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 1009 | #ifdef PNG_READ_SHIFT_SUPPORTED |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1010 | /* If you want to shift the pixel values from the range [0,255] or |
| 1011 | * [0,65535] to the original [0,7] or [0,31], or whatever range the |
| 1012 | * colors were originally in: |
| 1013 | */ |
| 1014 | if ((transforms & PNG_TRANSFORM_SHIFT) |
| 1015 | && png_get_valid(png_ptr, info_ptr, PNG_INFO_sBIT)) |
| 1016 | { |
| 1017 | png_color_8p sig_bit; |
| 1018 | |
| 1019 | png_get_sBIT(png_ptr, info_ptr, &sig_bit); |
| 1020 | png_set_shift(png_ptr, sig_bit); |
| 1021 | } |
| 1022 | #endif |
| 1023 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 1024 | #ifdef PNG_READ_BGR_SUPPORTED |
Glenn Randers-Pehrson | 262d0ff | 2010-03-03 07:06:54 -0600 | [diff] [blame] | 1025 | /* Flip the RGB pixels to BGR (or RGBA to BGRA) */ |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1026 | if (transforms & PNG_TRANSFORM_BGR) |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 1027 | png_set_bgr(png_ptr); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1028 | #endif |
| 1029 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 1030 | #ifdef PNG_READ_SWAP_ALPHA_SUPPORTED |
Glenn Randers-Pehrson | 262d0ff | 2010-03-03 07:06:54 -0600 | [diff] [blame] | 1031 | /* 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] | 1032 | if (transforms & PNG_TRANSFORM_SWAP_ALPHA) |
Glenn Randers-Pehrson | 262d0ff | 2010-03-03 07:06:54 -0600 | [diff] [blame] | 1033 | png_set_swap_alpha(png_ptr); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1034 | #endif |
| 1035 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 1036 | #ifdef PNG_READ_SWAP_SUPPORTED |
Glenn Randers-Pehrson | 55fbff3 | 2011-05-17 06:49:32 -0500 | [diff] [blame] | 1037 | /* Swap bytes of 16-bit files to least significant byte first */ |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1038 | if (transforms & PNG_TRANSFORM_SWAP_ENDIAN) |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 1039 | png_set_swap(png_ptr); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1040 | #endif |
| 1041 | |
Glenn Randers-Pehrson | c1a4d64 | 2009-10-29 23:29:24 -0500 | [diff] [blame] | 1042 | /* Added at libpng-1.2.41 */ |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 1043 | #ifdef PNG_READ_INVERT_ALPHA_SUPPORTED |
Glenn Randers-Pehrson | 262d0ff | 2010-03-03 07:06:54 -0600 | [diff] [blame] | 1044 | /* Invert the alpha channel from opacity to transparency */ |
Glenn Randers-Pehrson | 6878eb6 | 2009-06-29 16:45:53 -0500 | [diff] [blame] | 1045 | if (transforms & PNG_TRANSFORM_INVERT_ALPHA) |
Glenn Randers-Pehrson | 262d0ff | 2010-03-03 07:06:54 -0600 | [diff] [blame] | 1046 | png_set_invert_alpha(png_ptr); |
Glenn Randers-Pehrson | 6878eb6 | 2009-06-29 16:45:53 -0500 | [diff] [blame] | 1047 | #endif |
| 1048 | |
Glenn Randers-Pehrson | c1a4d64 | 2009-10-29 23:29:24 -0500 | [diff] [blame] | 1049 | /* Added at libpng-1.2.41 */ |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 1050 | #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED |
Glenn Randers-Pehrson | 262d0ff | 2010-03-03 07:06:54 -0600 | [diff] [blame] | 1051 | /* Expand grayscale image to RGB */ |
Glenn Randers-Pehrson | 99708d5 | 2009-06-29 17:30:00 -0500 | [diff] [blame] | 1052 | if (transforms & PNG_TRANSFORM_GRAY_TO_RGB) |
Glenn Randers-Pehrson | 262d0ff | 2010-03-03 07:06:54 -0600 | [diff] [blame] | 1053 | png_set_gray_to_rgb(png_ptr); |
Glenn Randers-Pehrson | 99708d5 | 2009-06-29 17:30:00 -0500 | [diff] [blame] | 1054 | #endif |
| 1055 | |
Glenn Randers-Pehrson | ef217b7 | 2011-06-15 12:58:27 -0500 | [diff] [blame] | 1056 | /* Added at libpng-1.5.4 */ |
John Bowler | 96cec0e | 2011-05-08 22:48:12 -0500 | [diff] [blame] | 1057 | #ifdef PNG_READ_EXPAND_16_SUPPORTED |
| 1058 | if (transforms & PNG_TRANSFORM_EXPAND_16) |
| 1059 | png_set_expand_16(png_ptr); |
| 1060 | #endif |
| 1061 | |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1062 | /* We don't handle adding filler bytes */ |
| 1063 | |
John Bowler | 6a6d79f | 2011-02-12 08:56:40 -0600 | [diff] [blame] | 1064 | /* We use png_read_image and rely on that for interlace handling, but we also |
| 1065 | * call png_read_update_info therefore must turn on interlace handling now: |
| 1066 | */ |
| 1067 | (void)png_set_interlace_handling(png_ptr); |
| 1068 | |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1069 | /* Optional call to gamma correct and add the background to the palette |
| 1070 | * and update info structure. REQUIRED if you are expecting libpng to |
Glenn Randers-Pehrson | 3097f61 | 2001-05-07 14:52:45 -0500 | [diff] [blame] | 1071 | * 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] | 1072 | */ |
| 1073 | png_read_update_info(png_ptr, info_ptr); |
| 1074 | |
| 1075 | /* -------------- image transformations end here ------------------- */ |
| 1076 | |
Glenn Randers-Pehrson | 1ef65b6 | 2000-05-12 06:19:53 -0500 | [diff] [blame] | 1077 | png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0); |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1078 | if (info_ptr->row_pointers == NULL) |
Glenn Randers-Pehrson | a77ef62 | 2000-02-18 13:48:52 -0600 | [diff] [blame] | 1079 | { |
Glenn Randers-Pehrson | 262d0ff | 2010-03-03 07:06:54 -0600 | [diff] [blame] | 1080 | png_uint_32 iptr; |
Glenn Randers-Pehrson | d9f21ee | 2009-08-31 10:52:38 -0500 | [diff] [blame] | 1081 | |
Glenn Randers-Pehrson | 0ffb71a | 2009-02-28 06:08:20 -0600 | [diff] [blame] | 1082 | info_ptr->row_pointers = (png_bytepp)png_malloc(png_ptr, |
Glenn Randers-Pehrson | 262d0ff | 2010-03-03 07:06:54 -0600 | [diff] [blame] | 1083 | info_ptr->height * png_sizeof(png_bytep)); |
Glenn Randers-Pehrson | d9f21ee | 2009-08-31 10:52:38 -0500 | [diff] [blame] | 1084 | for (iptr=0; iptr<info_ptr->height; iptr++) |
| 1085 | info_ptr->row_pointers[iptr] = NULL; |
| 1086 | |
Glenn Randers-Pehrson | a77ef62 | 2000-02-18 13:48:52 -0600 | [diff] [blame] | 1087 | info_ptr->free_me |= PNG_FREE_ROWS; |
Glenn Randers-Pehrson | d9f21ee | 2009-08-31 10:52:38 -0500 | [diff] [blame] | 1088 | |
Glenn Randers-Pehrson | 6942d53 | 2000-05-01 09:31:54 -0500 | [diff] [blame] | 1089 | for (row = 0; row < (int)info_ptr->height; row++) |
Glenn Randers-Pehrson | 3097f61 | 2001-05-07 14:52:45 -0500 | [diff] [blame] | 1090 | info_ptr->row_pointers[row] = (png_bytep)png_malloc(png_ptr, |
Glenn Randers-Pehrson | 6942d53 | 2000-05-01 09:31:54 -0500 | [diff] [blame] | 1091 | png_get_rowbytes(png_ptr, info_ptr)); |
Glenn Randers-Pehrson | a77ef62 | 2000-02-18 13:48:52 -0600 | [diff] [blame] | 1092 | } |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1093 | |
| 1094 | png_read_image(png_ptr, info_ptr->row_pointers); |
| 1095 | info_ptr->valid |= PNG_INFO_IDAT; |
| 1096 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 1097 | /* 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] | 1098 | png_read_end(png_ptr, info_ptr); |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 1099 | |
Glenn Randers-Pehrson | d546f43 | 2010-12-04 20:41:36 -0600 | [diff] [blame] | 1100 | PNG_UNUSED(transforms) /* Quiet compiler warnings */ |
| 1101 | PNG_UNUSED(params) |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 1102 | |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1103 | } |
Glenn Randers-Pehrson | 9c3ab68 | 2006-02-20 22:09:05 -0600 | [diff] [blame] | 1104 | #endif /* PNG_INFO_IMAGE_SUPPORTED */ |
Glenn Randers-Pehrson | dbd4014 | 2009-08-31 08:42:02 -0500 | [diff] [blame] | 1105 | #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */ |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1106 | |
| 1107 | #ifdef PNG_SIMPLIFIED_READ_SUPPORTED |
| 1108 | /* SIMPLIFIED READ |
| 1109 | * |
| 1110 | * This code currently relies on the sequential reader, though it could easily |
| 1111 | * be made to work with the progressive one. |
| 1112 | */ |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame] | 1113 | /* Arguments to png_image_finish_read: */ |
| 1114 | |
| 1115 | /* Encoding of PNG data (used by the color-map code) */ |
John Bowler | aa816c4 | 2012-03-16 07:39:49 -0500 | [diff] [blame] | 1116 | /* TODO: change these, dang, ANSI-C reserves the 'E' namespace. */ |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame] | 1117 | # define E_NOTSET 0 /* File encoding not yet known */ |
| 1118 | # define E_sRGB 1 /* 8-bit encoded to sRGB gamma */ |
| 1119 | # define E_LINEAR 2 /* 16-bit linear: not encoded, NOT pre-multiplied! */ |
| 1120 | # define E_FILE 3 /* 8-bit encoded to file gamma, not sRGB or linear */ |
| 1121 | # define E_LINEAR8 4 /* 8-bit linear: only from a file value */ |
| 1122 | |
| 1123 | /* Color-map processing: after libpng has run on the PNG image further |
| 1124 | * processing may be needed to conver the data to color-map indicies. |
| 1125 | */ |
| 1126 | #define PNG_CMAP_NONE 0 |
| 1127 | #define PNG_CMAP_GA 1 /* Process GA data to a color-map with alpha */ |
| 1128 | #define PNG_CMAP_TRANS 2 /* Process GA data to a background index */ |
| 1129 | #define PNG_CMAP_RGB 3 /* Process RGB data */ |
| 1130 | #define PNG_CMAP_RGB_ALPHA 4 /* Process RGBA data */ |
| 1131 | |
| 1132 | /* The following document where the background is for each processing case. */ |
| 1133 | #define PNG_CMAP_NONE_BACKGROUND 256 |
| 1134 | #define PNG_CMAP_GA_BACKGROUND 231 |
| 1135 | #define PNG_CMAP_TRANS_BACKGROUND 254 |
| 1136 | #define PNG_CMAP_RGB_BACKGROUND 256 |
| 1137 | #define PNG_CMAP_RGB_ALPHA_BACKGROUND 216 |
| 1138 | |
| 1139 | typedef struct |
| 1140 | { |
| 1141 | /* Arguments: */ |
| 1142 | png_imagep image; |
| 1143 | png_voidp buffer; |
| 1144 | png_int_32 row_stride; |
| 1145 | png_voidp colormap; |
John Bowler | 40ca77a | 2012-01-28 23:19:42 -0600 | [diff] [blame] | 1146 | png_const_colorp background; |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame] | 1147 | /* Local variables: */ |
| 1148 | png_bytep local_row; |
| 1149 | png_bytep first_row; |
| 1150 | ptrdiff_t row_bytes; /* step between rows */ |
| 1151 | int file_encoding; /* E_ values above */ |
| 1152 | png_fixed_point gamma_to_linear; /* For E_FILE, reciprocal of gamma */ |
| 1153 | int colormap_processing; /* PNG_CMAP_ values above */ |
| 1154 | } png_image_read_control; |
| 1155 | |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1156 | /* 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] | 1157 | * called, so setting up the jmp_buf is not required. This means that anything |
| 1158 | * called from here must *not* call png_malloc - it has to call png_malloc_warn |
| 1159 | * instead so that control is returned safely back to this routine. |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1160 | */ |
| 1161 | static int |
| 1162 | png_image_read_init(png_imagep image) |
| 1163 | { |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame] | 1164 | if (image->opaque == NULL) |
| 1165 | { |
| 1166 | 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] | 1167 | png_safe_error, png_safe_warning); |
| 1168 | |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame] | 1169 | /* And set the rest of the structure to NULL to ensure that the various |
| 1170 | * fields are consistent. |
| 1171 | */ |
| 1172 | memset(image, 0, sizeof *image); |
| 1173 | image->version = PNG_IMAGE_VERSION; |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1174 | |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame] | 1175 | if (png_ptr != NULL) |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1176 | { |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame] | 1177 | png_infop info_ptr = png_create_info_struct(png_ptr); |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1178 | |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame] | 1179 | if (info_ptr != NULL) |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1180 | { |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame] | 1181 | png_controlp control = png_voidcast(png_controlp, |
| 1182 | png_malloc_warn(png_ptr, sizeof *control)); |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1183 | |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame] | 1184 | if (control != NULL) |
| 1185 | { |
| 1186 | png_memset(control, 0, sizeof *control); |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1187 | |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame] | 1188 | control->png_ptr = png_ptr; |
| 1189 | control->info_ptr = info_ptr; |
| 1190 | control->for_write = 0; |
| 1191 | |
| 1192 | image->opaque = control; |
| 1193 | return 1; |
| 1194 | } |
| 1195 | |
| 1196 | /* Error clean up */ |
| 1197 | png_destroy_info_struct(png_ptr, &info_ptr); |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1198 | } |
| 1199 | |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame] | 1200 | png_destroy_read_struct(&png_ptr, NULL, NULL); |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1201 | } |
| 1202 | |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame] | 1203 | return png_image_error(image, "png_image_read: out of memory"); |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1204 | } |
| 1205 | |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame] | 1206 | return png_image_error(image, "png_image_read: opaque pointer not NULL"); |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1207 | } |
| 1208 | |
| 1209 | /* Utility to find the base format of a PNG file from a png_struct. */ |
| 1210 | static png_uint_32 |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame] | 1211 | png_image_format(png_structrp png_ptr) |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1212 | { |
| 1213 | png_uint_32 format = 0; |
| 1214 | |
| 1215 | if (png_ptr->color_type & PNG_COLOR_MASK_COLOR) |
| 1216 | format |= PNG_FORMAT_FLAG_COLOR; |
| 1217 | |
| 1218 | if (png_ptr->color_type & PNG_COLOR_MASK_ALPHA) |
| 1219 | format |= PNG_FORMAT_FLAG_ALPHA; |
| 1220 | |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame] | 1221 | /* Use png_ptr here, not info_ptr, because by examination png_handle_tRNS |
| 1222 | * sets the png_struct fields; that's all we are interested in here. The |
| 1223 | * precise interaction with an app call to png_set_tRNS and PNG file reading |
| 1224 | * is unclear. |
| 1225 | */ |
| 1226 | else if (png_ptr->num_trans > 0) |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1227 | format |= PNG_FORMAT_FLAG_ALPHA; |
| 1228 | |
| 1229 | if (png_ptr->bit_depth == 16) |
| 1230 | format |= PNG_FORMAT_FLAG_LINEAR; |
| 1231 | |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame] | 1232 | if (png_ptr->color_type & PNG_COLOR_MASK_PALETTE) |
| 1233 | format |= PNG_FORMAT_FLAG_COLORMAP; |
| 1234 | |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1235 | return format; |
| 1236 | } |
| 1237 | |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame] | 1238 | /* Is the given gamma significantly different from sRGB? The test is the same |
| 1239 | * one used in pngrtran.c when deciding whether to do gamma correction. The |
| 1240 | * arithmetic optimizes the division by using the fact that the inverse of the |
| 1241 | * file sRGB gamma is 2.2 |
| 1242 | */ |
| 1243 | static int |
| 1244 | png_gamma_not_sRGB(png_fixed_point g) |
| 1245 | { |
| 1246 | if (g < PNG_FP_1) |
| 1247 | { |
| 1248 | /* An uninitialized gamma is assumed to be sRGB for the simplified API. */ |
| 1249 | if (g == 0) |
| 1250 | return 0; |
| 1251 | |
| 1252 | return png_gamma_significant((g * 11 + 2)/5 /* i.e. *2.2, rounded */); |
| 1253 | } |
| 1254 | |
| 1255 | return 1; |
| 1256 | } |
| 1257 | |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1258 | /* Do the main body of a 'png_image_begin_read' function; read the PNG file |
| 1259 | * header and fill in all the information. This is executed in a safe context, |
| 1260 | * unlike the init routine above. |
| 1261 | */ |
| 1262 | static int |
| 1263 | png_image_read_header(png_voidp argument) |
| 1264 | { |
John Bowler | 4fa96a4 | 2011-11-16 16:39:16 -0600 | [diff] [blame] | 1265 | png_imagep image = png_voidcast(png_imagep, argument); |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 1266 | png_structrp png_ptr = image->opaque->png_ptr; |
| 1267 | png_inforp info_ptr = image->opaque->info_ptr; |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1268 | |
John Bowler | aa816c4 | 2012-03-16 07:39:49 -0500 | [diff] [blame] | 1269 | png_set_benign_errors(png_ptr, 1/*warn*/); |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1270 | png_read_info(png_ptr, info_ptr); |
| 1271 | |
| 1272 | /* Do this the fast way; just read directly out of png_struct. */ |
| 1273 | image->width = png_ptr->width; |
| 1274 | image->height = png_ptr->height; |
| 1275 | |
| 1276 | { |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame] | 1277 | png_uint_32 format = png_image_format(png_ptr); |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1278 | |
| 1279 | image->format = format; |
John Bowler | 04336ba | 2012-01-16 07:48:36 -0600 | [diff] [blame] | 1280 | |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame] | 1281 | #ifdef PNG_COLORSPACE_SUPPORTED |
| 1282 | /* Does the colorspace match sRGB? If there is no color endpoint |
| 1283 | * (colorant) information assume yes, otherwise require the |
| 1284 | * 'ENDPOINTS_MATCHE_sRGB' colorspace flag to have been set. If the |
| 1285 | * colorspace has been determined to be invalid ignore it. |
| 1286 | */ |
| 1287 | if ((format & PNG_FORMAT_FLAG_COLOR) != 0 && ((png_ptr->colorspace.flags |
| 1288 | & (PNG_COLORSPACE_HAVE_ENDPOINTS|PNG_COLORSPACE_ENDPOINTS_MATCH_sRGB| |
| 1289 | PNG_COLORSPACE_INVALID)) == PNG_COLORSPACE_HAVE_ENDPOINTS)) |
| 1290 | image->flags |= PNG_IMAGE_FLAG_COLORSPACE_NOT_sRGB; |
| 1291 | #endif |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1292 | } |
| 1293 | |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame] | 1294 | /* We need the maximum number of entries regardless of the format the |
| 1295 | * application sets here. |
| 1296 | */ |
| 1297 | { |
| 1298 | png_uint_32 cmap_entries; |
| 1299 | |
| 1300 | switch (png_ptr->color_type) |
| 1301 | { |
| 1302 | case PNG_COLOR_TYPE_GRAY: |
| 1303 | cmap_entries = 1U << png_ptr->bit_depth; |
| 1304 | break; |
| 1305 | |
| 1306 | case PNG_COLOR_TYPE_PALETTE: |
| 1307 | cmap_entries = png_ptr->num_palette; |
| 1308 | break; |
| 1309 | |
| 1310 | default: |
| 1311 | cmap_entries = 256; |
| 1312 | break; |
| 1313 | } |
| 1314 | |
| 1315 | if (cmap_entries > 256) |
| 1316 | cmap_entries = 256; |
| 1317 | |
| 1318 | image->colormap_entries = cmap_entries; |
| 1319 | } |
| 1320 | |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1321 | return 1; |
| 1322 | } |
| 1323 | |
| 1324 | #ifdef PNG_STDIO_SUPPORTED |
| 1325 | int PNGAPI |
| 1326 | png_image_begin_read_from_stdio(png_imagep image, FILE* file) |
| 1327 | { |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame] | 1328 | if (image != NULL && image->version == PNG_IMAGE_VERSION) |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1329 | { |
| 1330 | if (file != NULL) |
| 1331 | { |
| 1332 | if (png_image_read_init(image)) |
| 1333 | { |
| 1334 | /* This is slightly evil, but png_init_io doesn't do anything other |
| 1335 | * than this and we haven't changed the standard IO functions so |
| 1336 | * this saves a 'safe' function. |
| 1337 | */ |
| 1338 | image->opaque->png_ptr->io_ptr = file; |
| 1339 | return png_safe_execute(image, png_image_read_header, image); |
| 1340 | } |
| 1341 | } |
| 1342 | |
| 1343 | else |
| 1344 | return png_image_error(image, |
| 1345 | "png_image_begin_read_from_stdio: invalid argument"); |
| 1346 | } |
| 1347 | |
| 1348 | return 0; |
| 1349 | } |
| 1350 | |
| 1351 | int PNGAPI |
| 1352 | png_image_begin_read_from_file(png_imagep image, const char *file_name) |
| 1353 | { |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame] | 1354 | if (image != NULL && image->version == PNG_IMAGE_VERSION) |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1355 | { |
| 1356 | if (file_name != NULL) |
| 1357 | { |
| 1358 | FILE *fp = fopen(file_name, "rb"); |
| 1359 | |
| 1360 | if (fp != NULL) |
| 1361 | { |
| 1362 | if (png_image_read_init(image)) |
| 1363 | { |
| 1364 | image->opaque->png_ptr->io_ptr = fp; |
| 1365 | image->opaque->owned_file = 1; |
| 1366 | return png_safe_execute(image, png_image_read_header, image); |
| 1367 | } |
| 1368 | |
| 1369 | /* Clean up: just the opened file. */ |
| 1370 | (void)fclose(fp); |
| 1371 | } |
| 1372 | |
| 1373 | else |
| 1374 | return png_image_error(image, strerror(errno)); |
| 1375 | } |
| 1376 | |
| 1377 | else |
| 1378 | return png_image_error(image, |
| 1379 | "png_image_begin_read_from_file: invalid argument"); |
| 1380 | } |
| 1381 | |
| 1382 | return 0; |
| 1383 | } |
| 1384 | #endif /* PNG_STDIO_SUPPORTED */ |
| 1385 | |
| 1386 | static void PNGCBAPI |
| 1387 | png_image_memory_read(png_structp png_ptr, png_bytep out, png_size_t need) |
| 1388 | { |
| 1389 | if (png_ptr != NULL) |
| 1390 | { |
John Bowler | 4fa96a4 | 2011-11-16 16:39:16 -0600 | [diff] [blame] | 1391 | png_imagep image = png_voidcast(png_imagep, png_ptr->io_ptr); |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1392 | if (image != NULL) |
| 1393 | { |
| 1394 | png_controlp cp = image->opaque; |
| 1395 | if (cp != NULL) |
| 1396 | { |
| 1397 | png_const_bytep memory = cp->memory; |
| 1398 | png_size_t size = cp->size; |
| 1399 | |
| 1400 | if (memory != NULL && size >= need) |
| 1401 | { |
John Bowler | baeb6d1 | 2011-11-26 18:21:02 -0600 | [diff] [blame] | 1402 | png_memcpy(out, memory, need); |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1403 | cp->memory = memory + need; |
| 1404 | cp->size = size - need; |
| 1405 | return; |
| 1406 | } |
| 1407 | |
| 1408 | png_error(png_ptr, "read beyond end of data"); |
| 1409 | } |
| 1410 | } |
| 1411 | |
| 1412 | png_error(png_ptr, "invalid memory read"); |
| 1413 | } |
| 1414 | } |
| 1415 | |
| 1416 | int PNGAPI png_image_begin_read_from_memory(png_imagep image, |
| 1417 | png_const_voidp memory, png_size_t size) |
| 1418 | { |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame] | 1419 | if (image != NULL && image->version == PNG_IMAGE_VERSION) |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1420 | { |
| 1421 | if (memory != NULL && size > 0) |
| 1422 | { |
| 1423 | if (png_image_read_init(image)) |
| 1424 | { |
| 1425 | /* Now set the IO functions to read from the memory buffer and |
| 1426 | * store it into io_ptr. Again do this in-place to avoid calling a |
| 1427 | * libpng function that requires error handling. |
| 1428 | */ |
John Bowler | 4fa96a4 | 2011-11-16 16:39:16 -0600 | [diff] [blame] | 1429 | image->opaque->memory = png_voidcast(png_const_bytep, memory); |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1430 | image->opaque->size = size; |
| 1431 | image->opaque->png_ptr->io_ptr = image; |
| 1432 | image->opaque->png_ptr->read_data_fn = png_image_memory_read; |
| 1433 | |
| 1434 | return png_safe_execute(image, png_image_read_header, image); |
| 1435 | } |
| 1436 | } |
| 1437 | |
| 1438 | else |
| 1439 | return png_image_error(image, |
| 1440 | "png_image_begin_read_from_memory: invalid argument"); |
| 1441 | } |
| 1442 | |
| 1443 | return 0; |
| 1444 | } |
| 1445 | |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame] | 1446 | /* Utility function to skip chunks that are not used by the simplified image |
| 1447 | * read functions and an appropriate macro to call it. |
| 1448 | */ |
| 1449 | #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED |
| 1450 | static void |
| 1451 | png_image_skip_unused_chunks(png_structrp png_ptr) |
John Bowler | 04336ba | 2012-01-16 07:48:36 -0600 | [diff] [blame] | 1452 | { |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame] | 1453 | /* Prepare the reader to ignore all recognized chunks whose data will not |
| 1454 | * be used, i.e., all chunks recognized by libpng except for those |
| 1455 | * involved in basic image reading: |
| 1456 | * |
| 1457 | * IHDR, PLTE, IDAT, IEND |
| 1458 | * |
| 1459 | * Or image data handling: |
| 1460 | * |
| 1461 | * tRNS, bKGD, gAMA, cHRM, sRGB, iCCP and sBIT. |
| 1462 | * |
| 1463 | * This provides a small performance improvement and eliminates any |
| 1464 | * potential vulnerability to security problems in the unused chunks. |
| 1465 | * |
| 1466 | * TODO: make it so that this is an explicit list to process, not a list |
| 1467 | * to ignore? |
| 1468 | */ |
John Bowler | 04336ba | 2012-01-16 07:48:36 -0600 | [diff] [blame] | 1469 | { |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame] | 1470 | static PNG_CONST png_byte chunks_to_ignore[] = { |
| 1471 | 104, 73, 83, 84, '\0', /* hIST */ |
| 1472 | 105, 84, 88, 116, '\0', /* iTXt */ |
| 1473 | 111, 70, 70, 115, '\0', /* oFFs */ |
| 1474 | 112, 67, 65, 76, '\0', /* pCAL */ |
| 1475 | 112, 72, 89, 115, '\0', /* pHYs */ |
| 1476 | 115, 67, 65, 76, '\0', /* sCAL */ |
| 1477 | 115, 80, 76, 84, '\0', /* sPLT */ |
| 1478 | 116, 69, 88, 116, '\0', /* tEXt */ |
| 1479 | 116, 73, 77, 69, '\0', /* tIME */ |
| 1480 | 122, 84, 88, 116, '\0' /* zTXt */ |
| 1481 | }; |
| 1482 | |
| 1483 | /* Ignore unknown chunks */ |
| 1484 | png_set_keep_unknown_chunks(png_ptr, 1 /* PNG_HANDLE_CHUNK_NEVER */, |
| 1485 | NULL, 0); |
| 1486 | |
| 1487 | /* Ignore known but unused chunks */ |
| 1488 | png_set_keep_unknown_chunks(png_ptr, 1 /* PNG_HANDLE_CHUNK_NEVER */, |
| 1489 | chunks_to_ignore, (sizeof chunks_to_ignore)/5); |
| 1490 | } |
| 1491 | } |
| 1492 | |
| 1493 | # define PNG_SKIP_CHUNKS(p) png_image_skip_unused_chunks(p) |
| 1494 | #else |
| 1495 | # define PNG_SKIP_CHUNKS(p) ((void)0) |
| 1496 | #endif /* PNG_HANDLE_AS_UNKNOWN_SUPPORTED */ |
| 1497 | |
| 1498 | /* The following macro gives the exact rounded answer for all values in the |
| 1499 | * range 0..255 (it actually divides by 51.2, but the rounding still generates |
| 1500 | * the correct numbers 0..5 |
| 1501 | */ |
| 1502 | #define PNG_DIV51(v8) (((v8) * 5 + 130) >> 8) |
| 1503 | |
| 1504 | /* Utility functions to make particular color-maps */ |
| 1505 | static void |
| 1506 | set_file_encoding(png_image_read_control *display) |
| 1507 | { |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame] | 1508 | png_fixed_point g = display->image->opaque->png_ptr->colorspace.gamma; |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame] | 1509 | if (png_gamma_significant(g)) |
| 1510 | { |
| 1511 | if (png_gamma_not_sRGB(g)) |
John Bowler | 04336ba | 2012-01-16 07:48:36 -0600 | [diff] [blame] | 1512 | { |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame] | 1513 | display->file_encoding = E_FILE; |
| 1514 | display->gamma_to_linear = png_reciprocal(g); |
John Bowler | 04336ba | 2012-01-16 07:48:36 -0600 | [diff] [blame] | 1515 | } |
| 1516 | |
| 1517 | else |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame] | 1518 | display->file_encoding = E_sRGB; |
John Bowler | 04336ba | 2012-01-16 07:48:36 -0600 | [diff] [blame] | 1519 | } |
| 1520 | |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame] | 1521 | else |
| 1522 | display->file_encoding = E_LINEAR8; |
John Bowler | 04336ba | 2012-01-16 07:48:36 -0600 | [diff] [blame] | 1523 | } |
| 1524 | |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame] | 1525 | static unsigned int |
| 1526 | 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] | 1527 | { |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame] | 1528 | if (encoding == E_FILE) /* double check */ |
| 1529 | encoding = display->file_encoding; |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1530 | |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame] | 1531 | if (encoding == E_NOTSET) /* must be the file encoding */ |
| 1532 | { |
| 1533 | set_file_encoding(display); |
| 1534 | encoding = display->file_encoding; |
| 1535 | } |
| 1536 | |
| 1537 | switch (encoding) |
| 1538 | { |
| 1539 | case E_FILE: |
| 1540 | value = png_gamma_16bit_correct(value*257, display->gamma_to_linear); |
| 1541 | break; |
| 1542 | |
| 1543 | case E_sRGB: |
| 1544 | value = png_sRGB_table[value]; |
| 1545 | break; |
| 1546 | |
| 1547 | case E_LINEAR: |
| 1548 | break; |
| 1549 | |
| 1550 | case E_LINEAR8: |
| 1551 | value *= 257; |
| 1552 | break; |
| 1553 | |
| 1554 | default: |
| 1555 | png_error(display->image->opaque->png_ptr, |
| 1556 | "unexpected encoding (internal error)"); |
| 1557 | break; |
| 1558 | } |
| 1559 | |
| 1560 | return value; |
| 1561 | } |
| 1562 | |
| 1563 | static png_uint_32 |
| 1564 | png_colormap_compose(png_image_read_control *display, |
| 1565 | png_uint_32 foreground, int foreground_encoding, png_uint_32 alpha, |
| 1566 | png_uint_32 background, int encoding) |
| 1567 | { |
| 1568 | /* The file value is composed on the background, the background has the given |
| 1569 | * encoding and so does the result, the file is encoded with E_FILE and the |
| 1570 | * file and alpha are 8-bit values. The (output) encoding will always be |
| 1571 | * E_LINEAR or E_sRGB. |
| 1572 | */ |
| 1573 | png_uint_32 f = decode_gamma(display, foreground, foreground_encoding); |
| 1574 | png_uint_32 b = decode_gamma(display, background, encoding); |
| 1575 | |
| 1576 | /* The alpha is always an 8-bit value (it comes from the palette), the value |
| 1577 | * scaled by 255 is what PNG_sRGB_FROM_LINEAR requires. |
| 1578 | */ |
| 1579 | f = f * alpha + b * (255-alpha); |
| 1580 | |
| 1581 | if (encoding == E_LINEAR) |
| 1582 | { |
| 1583 | /* Scale to 65535; divide by 255, approximately (in fact this is extremely |
| 1584 | * accurate, it divides by 255.00000005937181414556, with no overflow.) |
| 1585 | */ |
| 1586 | f *= 257; /* Now scaled by 65535 */ |
| 1587 | f += f >> 16; |
| 1588 | f = (f+32768) >> 16; |
| 1589 | } |
| 1590 | |
| 1591 | else /* E_sRGB */ |
| 1592 | f = PNG_sRGB_FROM_LINEAR(f); |
| 1593 | |
| 1594 | return f; |
| 1595 | } |
| 1596 | |
| 1597 | /* NOTE: E_LINEAR values to this routine must be 16-bit, but E_FILE values must |
| 1598 | * be 8-bit. |
| 1599 | */ |
| 1600 | static void |
| 1601 | png_create_colormap_entry(png_image_read_control *display, |
| 1602 | png_uint_32 ip, png_uint_32 red, png_uint_32 green, png_uint_32 blue, |
| 1603 | png_uint_32 alpha, int encoding) |
| 1604 | { |
| 1605 | png_imagep image = display->image; |
| 1606 | const int output_encoding = (image->format & PNG_FORMAT_FLAG_LINEAR) ? |
| 1607 | E_LINEAR : E_sRGB; |
| 1608 | const int convert_to_Y = (image->format & PNG_FORMAT_FLAG_COLOR) == 0 && |
| 1609 | (red != green || green != blue); |
| 1610 | |
| 1611 | if (ip > 255) |
| 1612 | png_error(image->opaque->png_ptr, "color-map index out of range"); |
| 1613 | |
| 1614 | /* Update the cache with whether the file gamma is significantly different |
| 1615 | * from sRGB. |
| 1616 | */ |
| 1617 | if (encoding == E_FILE) |
| 1618 | { |
| 1619 | if (display->file_encoding == E_NOTSET) |
| 1620 | set_file_encoding(display); |
| 1621 | |
| 1622 | /* Note that the cached value may be E_FILE too, but if it is then the |
| 1623 | * gamma_to_linear member has been set. |
| 1624 | */ |
| 1625 | encoding = display->file_encoding; |
| 1626 | } |
| 1627 | |
| 1628 | if (encoding == E_FILE) |
| 1629 | { |
| 1630 | png_fixed_point g = display->gamma_to_linear; |
| 1631 | |
| 1632 | red = png_gamma_16bit_correct(red*257, g); |
| 1633 | green = png_gamma_16bit_correct(green*257, g); |
| 1634 | blue = png_gamma_16bit_correct(blue*257, g); |
| 1635 | |
| 1636 | if (convert_to_Y || output_encoding == E_LINEAR) |
| 1637 | { |
| 1638 | alpha *= 257; |
| 1639 | encoding = E_LINEAR; |
| 1640 | } |
| 1641 | |
| 1642 | else |
| 1643 | { |
| 1644 | red = PNG_sRGB_FROM_LINEAR(red * 255); |
| 1645 | green = PNG_sRGB_FROM_LINEAR(green * 255); |
| 1646 | blue = PNG_sRGB_FROM_LINEAR(blue * 255); |
| 1647 | encoding = E_sRGB; |
| 1648 | } |
| 1649 | } |
| 1650 | |
| 1651 | else if (encoding == E_LINEAR8) |
| 1652 | { |
| 1653 | /* This encoding occurs quite frequently in test cases because PngSuite |
| 1654 | * includes a gAMA 1.0 chunk with most images. |
| 1655 | */ |
| 1656 | red *= 257; |
| 1657 | green *= 257; |
| 1658 | blue *= 257; |
| 1659 | alpha *= 257; |
| 1660 | encoding = E_LINEAR; |
| 1661 | } |
| 1662 | |
| 1663 | else if (encoding == E_sRGB && (convert_to_Y || output_encoding == E_LINEAR)) |
| 1664 | { |
| 1665 | /* The values are 8-bit sRGB values, but must be converted to 16-bit |
| 1666 | * linear. |
| 1667 | */ |
| 1668 | red = png_sRGB_table[red]; |
| 1669 | green = png_sRGB_table[green]; |
| 1670 | blue = png_sRGB_table[blue]; |
| 1671 | alpha *= 257; |
| 1672 | encoding = E_LINEAR; |
| 1673 | } |
| 1674 | |
| 1675 | /* This is set if the color isn't gray but the output is. */ |
| 1676 | if (encoding == E_LINEAR) |
| 1677 | { |
| 1678 | if (convert_to_Y) |
| 1679 | { |
| 1680 | /* NOTE: these values are copied from png_do_rgb_to_gray */ |
| 1681 | png_uint_32 y = (png_uint_32)6968 * red + (png_uint_32)23434 * green + |
| 1682 | (png_uint_32)2366 * blue; |
| 1683 | |
| 1684 | if (output_encoding == E_LINEAR) |
| 1685 | y = (y + 16384) >> 15; |
| 1686 | |
| 1687 | else |
| 1688 | { |
| 1689 | /* y is scaled by 32768, we need it scaled by 255: */ |
| 1690 | y = (y + 128) >> 8; |
| 1691 | y *= 255; |
| 1692 | y = PNG_sRGB_FROM_LINEAR((y + 64) >> 7); |
| 1693 | encoding = E_sRGB; |
| 1694 | } |
| 1695 | |
| 1696 | blue = red = green = y; |
| 1697 | } |
| 1698 | |
| 1699 | else if (output_encoding == E_sRGB) |
| 1700 | { |
| 1701 | red = PNG_sRGB_FROM_LINEAR(red * 255); |
| 1702 | green = PNG_sRGB_FROM_LINEAR(green * 255); |
| 1703 | blue = PNG_sRGB_FROM_LINEAR(blue * 255); |
| 1704 | alpha = PNG_DIV257(alpha); |
| 1705 | encoding = E_sRGB; |
| 1706 | } |
| 1707 | } |
| 1708 | |
| 1709 | if (encoding != output_encoding) |
| 1710 | png_error(image->opaque->png_ptr, "bad encoding (internal error)"); |
| 1711 | |
| 1712 | /* Store the value. */ |
| 1713 | { |
| 1714 | # ifdef PNG_FORMAT_BGR_SUPPORTED |
| 1715 | const int afirst = (image->format & PNG_FORMAT_FLAG_AFIRST) != 0 && |
| 1716 | (image->format & PNG_FORMAT_FLAG_ALPHA) != 0; |
| 1717 | # else |
| 1718 | # define afirst 0 |
| 1719 | # endif |
| 1720 | # ifdef PNG_FORMAT_BGR_SUPPORTED |
| 1721 | const int bgr = (image->format & PNG_FORMAT_FLAG_BGR) ? 2 : 0; |
| 1722 | # else |
| 1723 | # define bgr 0 |
| 1724 | # endif |
| 1725 | |
| 1726 | if (output_encoding == E_LINEAR) |
| 1727 | { |
| 1728 | png_uint_16p entry = png_voidcast(png_uint_16p, display->colormap); |
| 1729 | |
| 1730 | entry += ip * PNG_IMAGE_SAMPLE_CHANNELS(image->format); |
| 1731 | |
| 1732 | /* The linear 16-bit values must be pre-multiplied by the alpha channel |
| 1733 | * value, if less than 65535 (this is, effectively, composite on black |
| 1734 | * if the alpha channel is removed.) |
| 1735 | */ |
| 1736 | switch (PNG_IMAGE_SAMPLE_CHANNELS(image->format)) |
| 1737 | { |
| 1738 | case 4: |
| 1739 | entry[afirst ? 0 : 3] = (png_uint_16)alpha; |
| 1740 | /* FALL THROUGH */ |
| 1741 | |
| 1742 | case 3: |
| 1743 | if (alpha < 65535) |
| 1744 | { |
| 1745 | if (alpha > 0) |
| 1746 | { |
| 1747 | blue = (blue * alpha + 32767U)/65535U; |
| 1748 | green = (green * alpha + 32767U)/65535U; |
| 1749 | red = (red * alpha + 32767U)/65535U; |
| 1750 | } |
| 1751 | |
| 1752 | else |
| 1753 | red = green = blue = 0; |
| 1754 | } |
| 1755 | entry[afirst + (2 ^ bgr)] = (png_uint_16)blue; |
| 1756 | entry[afirst + 1] = (png_uint_16)green; |
| 1757 | entry[afirst + bgr] = (png_uint_16)red; |
| 1758 | break; |
| 1759 | |
| 1760 | case 2: |
| 1761 | entry[1 ^ afirst] = (png_uint_16)alpha; |
| 1762 | /* FALL THROUGH */ |
| 1763 | |
| 1764 | case 1: |
| 1765 | if (alpha < 65535) |
| 1766 | { |
| 1767 | if (alpha > 0) |
| 1768 | green = (green * alpha + 32767U)/65535U; |
| 1769 | |
| 1770 | else |
| 1771 | green = 0; |
| 1772 | } |
| 1773 | entry[afirst] = (png_uint_16)green; |
| 1774 | break; |
| 1775 | |
| 1776 | default: |
| 1777 | break; |
| 1778 | } |
| 1779 | } |
| 1780 | |
| 1781 | else /* output encoding is E_sRGB */ |
| 1782 | { |
| 1783 | png_bytep entry = png_voidcast(png_bytep, display->colormap); |
| 1784 | |
| 1785 | entry += ip * PNG_IMAGE_SAMPLE_CHANNELS(image->format); |
| 1786 | |
| 1787 | switch (PNG_IMAGE_SAMPLE_CHANNELS(image->format)) |
| 1788 | { |
| 1789 | case 4: |
| 1790 | entry[afirst ? 0 : 3] = (png_byte)alpha; |
| 1791 | case 3: |
| 1792 | entry[afirst + (2 ^ bgr)] = (png_byte)blue; |
| 1793 | entry[afirst + 1] = (png_byte)green; |
| 1794 | entry[afirst + bgr] = (png_byte)red; |
| 1795 | break; |
| 1796 | |
| 1797 | case 2: |
| 1798 | entry[1 ^ afirst] = (png_byte)alpha; |
| 1799 | case 1: |
| 1800 | entry[afirst] = (png_byte)green; |
| 1801 | break; |
| 1802 | |
| 1803 | default: |
| 1804 | break; |
| 1805 | } |
| 1806 | } |
| 1807 | |
| 1808 | # ifdef afirst |
| 1809 | # undef afirst |
| 1810 | # endif |
| 1811 | # ifdef bgr |
| 1812 | # undef bgr |
| 1813 | # endif |
| 1814 | } |
| 1815 | } |
| 1816 | |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 1817 | static int |
John Bowler | eda53e5 | 2012-03-18 22:46:28 -0500 | [diff] [blame] | 1818 | make_gray_file_colormap(png_image_read_control *display) |
| 1819 | { |
| 1820 | unsigned int i; |
| 1821 | |
| 1822 | for (i=0; i<256; ++i) |
| 1823 | png_create_colormap_entry(display, i, i, i, i, 255, E_FILE); |
| 1824 | |
| 1825 | return i; |
| 1826 | } |
| 1827 | |
| 1828 | static int |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame] | 1829 | make_gray_colormap(png_image_read_control *display) |
| 1830 | { |
| 1831 | unsigned int i; |
| 1832 | |
| 1833 | for (i=0; i<256; ++i) |
| 1834 | png_create_colormap_entry(display, i, i, i, i, 255, E_sRGB); |
| 1835 | |
| 1836 | return i; |
| 1837 | } |
| 1838 | #define PNG_GRAY_COLORMAP_ENTRIES 256 |
| 1839 | |
| 1840 | static int |
| 1841 | make_ga_colormap(png_image_read_control *display) |
| 1842 | { |
| 1843 | unsigned int i, a; |
| 1844 | |
| 1845 | /* Alpha is retained, the output will be a color-map with entries |
| 1846 | * selected by six levels of alpha. One transparent entry, 6 gray |
| 1847 | * levels for all the intermediate alpha values, leaving 230 entries |
| 1848 | * for the opaque grays. The color-map entries are the six values |
| 1849 | * [0..5]*51, the GA processing uses PNG_DIV51(value) to find the |
| 1850 | * relevant entry. |
| 1851 | * |
| 1852 | * if (alpha > 229) // opaque |
| 1853 | * { |
| 1854 | * // The 231 entries are selected to make the math below work: |
| 1855 | * base = 0; |
| 1856 | * entry = (231 * gray + 128) >> 8; |
| 1857 | * } |
| 1858 | * else if (alpha < 26) // transparent |
| 1859 | * { |
| 1860 | * base = 231; |
| 1861 | * entry = 0; |
| 1862 | * } |
| 1863 | * else // partially opaque |
| 1864 | * { |
| 1865 | * base = 226 + 6 * PNG_DIV51(alpha); |
| 1866 | * entry = PNG_DIV51(gray); |
| 1867 | * } |
| 1868 | */ |
| 1869 | i = 0; |
| 1870 | while (i < 231) |
| 1871 | { |
| 1872 | unsigned int gray = (i * 256 + 115) / 231; |
| 1873 | png_create_colormap_entry(display, i++, gray, gray, gray, 255, E_sRGB); |
| 1874 | } |
| 1875 | |
| 1876 | /* 255 is used here for the component values for consistency with the code |
| 1877 | * that undoes premultiplication in pngwrite.c. |
| 1878 | */ |
| 1879 | png_create_colormap_entry(display, i++, 255, 255, 255, 0, E_sRGB); |
| 1880 | |
| 1881 | for (a=1; a<5; ++a) |
| 1882 | { |
| 1883 | unsigned int g; |
| 1884 | |
| 1885 | for (g=0; g<6; ++g) |
| 1886 | png_create_colormap_entry(display, i++, g*51, g*51, g*51, a*51, |
| 1887 | E_sRGB); |
| 1888 | } |
| 1889 | |
| 1890 | return i; |
| 1891 | } |
| 1892 | |
| 1893 | #define PNG_GA_COLORMAP_ENTRIES 256 |
| 1894 | |
| 1895 | static int |
| 1896 | make_rgb_colormap(png_image_read_control *display) |
| 1897 | { |
| 1898 | unsigned int i, r; |
| 1899 | |
| 1900 | /* Build a 6x6x6 opaque RGB cube */ |
| 1901 | for (i=r=0; r<6; ++r) |
| 1902 | { |
| 1903 | unsigned int g; |
| 1904 | |
| 1905 | for (g=0; g<6; ++g) |
| 1906 | { |
| 1907 | unsigned int b; |
| 1908 | |
| 1909 | for (b=0; b<6; ++b) |
| 1910 | png_create_colormap_entry(display, i++, r*51, g*51, b*51, 255, |
| 1911 | E_sRGB); |
| 1912 | } |
| 1913 | } |
| 1914 | |
| 1915 | return i; |
| 1916 | } |
| 1917 | |
| 1918 | #define PNG_RGB_COLORMAP_ENTRIES 216 |
| 1919 | |
| 1920 | /* Return a palette index to the above palette given three 8-bit sRGB values. */ |
| 1921 | #define PNG_RGB_INDEX(r,g,b) \ |
| 1922 | ((png_byte)(6 * (6 * PNG_DIV51(r) + PNG_DIV51(g)) + PNG_DIV51(b))) |
| 1923 | |
Glenn Randers-Pehrson | 864270e | 2012-02-10 17:13:13 -0600 | [diff] [blame] | 1924 | static int |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame] | 1925 | png_image_read_colormap(png_voidp argument) |
| 1926 | { |
| 1927 | png_image_read_control *display = |
| 1928 | png_voidcast(png_image_read_control*, argument); |
| 1929 | const png_imagep image = display->image; |
| 1930 | |
| 1931 | const png_structrp png_ptr = image->opaque->png_ptr; |
| 1932 | const png_uint_32 output_format = image->format; |
| 1933 | const int output_encoding = (output_format & PNG_FORMAT_FLAG_LINEAR) ? |
| 1934 | E_LINEAR : E_sRGB; |
| 1935 | |
| 1936 | unsigned int cmap_entries; |
| 1937 | unsigned int output_processing; /* Output processing option */ |
| 1938 | unsigned int data_encoding = E_NOTSET; /* Encoding libpng must produce */ |
| 1939 | |
| 1940 | /* Background information; the background color and the index of this color |
| 1941 | * in the color-map if it exists (else 256). |
| 1942 | */ |
| 1943 | unsigned int background_index = 256; |
| 1944 | png_uint_32 back_r, back_g, back_b; |
| 1945 | |
| 1946 | /* Flags to accumulate things that need to be done to the input. */ |
| 1947 | int expand_tRNS = 0; |
| 1948 | |
| 1949 | /* Exclude the NYI feature of compositing onto a color-mapped buffer; it is |
| 1950 | * very difficult to do, the results look awful, and it is difficult to see |
| 1951 | * what possible use it is because the application can't control the |
| 1952 | * color-map. |
| 1953 | */ |
| 1954 | if (((png_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0 || |
| 1955 | png_ptr->num_trans > 0) /* alpha in input */ && |
| 1956 | ((output_format & PNG_FORMAT_FLAG_ALPHA) == 0) /* no alpha in output */) |
| 1957 | { |
| 1958 | if (output_encoding == E_LINEAR) /* compose on black */ |
| 1959 | back_b = back_g = back_r = 0; |
| 1960 | |
| 1961 | else if (display->background == NULL /* no way to remove it */) |
| 1962 | png_error(png_ptr, |
| 1963 | "a background color must be supplied to remove alpha/transparency"); |
| 1964 | |
| 1965 | /* Get a copy of the background color (this avoids repeating the checks |
| 1966 | * below.) The encoding is 8-bit sRGB or 16-bit linear, depending on the |
| 1967 | * output format. |
| 1968 | */ |
| 1969 | else |
| 1970 | { |
| 1971 | back_g = display->background->green; |
| 1972 | if (output_format & PNG_FORMAT_FLAG_COLOR) |
| 1973 | { |
| 1974 | back_r = display->background->red; |
| 1975 | back_b = display->background->blue; |
| 1976 | } |
| 1977 | else |
| 1978 | back_b = back_r = back_g; |
| 1979 | } |
| 1980 | } |
| 1981 | |
| 1982 | else if (output_encoding == E_LINEAR) |
| 1983 | back_b = back_r = back_g = 65535; |
| 1984 | |
| 1985 | else |
| 1986 | back_b = back_r = back_g = 255; |
| 1987 | |
| 1988 | /* Decide what to do based on the PNG color type of the input data. The |
| 1989 | * utility function png_create_colormap_entry deals with most aspects of the |
| 1990 | * output transformations; this code works out how to produce bytes of |
| 1991 | * color-map entries from the original format. |
| 1992 | */ |
| 1993 | switch (png_ptr->color_type) |
| 1994 | { |
| 1995 | case PNG_COLOR_TYPE_GRAY: |
| 1996 | if (png_ptr->bit_depth <= 8) |
| 1997 | { |
| 1998 | /* There at most 256 colors in the output, regardless of |
| 1999 | * transparency. |
| 2000 | */ |
| 2001 | unsigned int step, i, val, trans = 256/*ignore*/, back_alpha = 0; |
| 2002 | |
| 2003 | cmap_entries = 1U << png_ptr->bit_depth; |
| 2004 | if (cmap_entries > image->colormap_entries) |
| 2005 | png_error(png_ptr, "gray[8] color-map: too few entries"); |
| 2006 | |
| 2007 | step = 255 / (cmap_entries - 1); |
| 2008 | output_processing = PNG_CMAP_NONE; |
| 2009 | |
| 2010 | /* If there is a tRNS chunk then this either selects a transparent |
| 2011 | * value or, if the output has no alpha, the background color. |
| 2012 | */ |
| 2013 | if (png_ptr->num_trans > 0) |
| 2014 | { |
| 2015 | trans = png_ptr->trans_color.gray; |
| 2016 | |
| 2017 | if ((output_format & PNG_FORMAT_FLAG_ALPHA) == 0) |
| 2018 | back_alpha = output_encoding == E_LINEAR ? 65535 : 255; |
| 2019 | } |
| 2020 | |
| 2021 | /* png_create_colormap_entry just takes an RGBA and writes the |
| 2022 | * corresponding color-map entry using the format from 'image', |
| 2023 | * including the required convertion to sRGB or linear as |
| 2024 | * appropriate. The input values are always either sRGB (if the |
| 2025 | * gamma correction flag is 0) or 0..255 scaled file encoded values |
| 2026 | * (if the function must gamma correct them). |
| 2027 | */ |
| 2028 | for (i=val=0; i<cmap_entries; ++i, val += step) |
| 2029 | { |
| 2030 | /* 'i' is a file value. While this will result in duplicated |
| 2031 | * entries for 8-bit non-sRGB encoded files it is necessary to |
| 2032 | * have non-gamma corrected values to do tRNS handling. |
| 2033 | */ |
| 2034 | if (i != trans) |
| 2035 | png_create_colormap_entry(display, i, val, val, val, 255, |
| 2036 | E_FILE/*8-bit with file gamma*/); |
| 2037 | |
| 2038 | /* Else this entry is transparent. The colors don't matter if |
| 2039 | * there is an alpha channel (back_alpha == 0), but it does no |
| 2040 | * harm to pass them in; the values are not set above so this |
| 2041 | * passes in white. |
| 2042 | * |
| 2043 | * NOTE: this preserves the full precision of the application |
| 2044 | * supplied background color when it is used. |
| 2045 | */ |
| 2046 | else |
| 2047 | png_create_colormap_entry(display, i, back_r, back_g, back_b, |
| 2048 | back_alpha, output_encoding); |
| 2049 | } |
| 2050 | |
| 2051 | /* We need libpng to preserve the original encoding. */ |
| 2052 | data_encoding = E_FILE; |
| 2053 | |
| 2054 | /* The rows from libpng, while technically gray values, are now also |
| 2055 | * color-map indicies; however, they may need to be expanded to 1 |
| 2056 | * byte per pixel. This is what png_set_packing does (i.e., it |
| 2057 | * unpacks the bit values into bytes.) |
| 2058 | */ |
| 2059 | if (png_ptr->bit_depth < 8) |
| 2060 | png_set_packing(png_ptr); |
| 2061 | } |
| 2062 | |
| 2063 | else /* bit depth is 16 */ |
| 2064 | { |
| 2065 | /* The 16-bit input values can be converted directly to 8-bit gamma |
| 2066 | * encoded values; however, if a tRNS chunk is present 257 color-map |
| 2067 | * entries are required. This means that the extra entry requires |
| 2068 | * special processing; add an alpha channel, sacrifice gray level |
| 2069 | * 254 and convert transparent (alpha==0) entries to that. |
| 2070 | * |
| 2071 | * Use libpng to chop the data to 8 bits. Convert it to sRGB at the |
| 2072 | * same time to minimize quality loss. If a tRNS chunk is present |
| 2073 | * this means libpng must handle it too; otherwise it is impossible |
| 2074 | * to do the exact match on the 16-bit value. |
| 2075 | * |
| 2076 | * If the output has no alpha channel *and* the background color is |
| 2077 | * gray then it is possible to let libpng handle the substitution by |
| 2078 | * ensuring that the corresponding gray level matches the background |
| 2079 | * color exactly. |
| 2080 | */ |
| 2081 | data_encoding = E_sRGB; |
| 2082 | |
| 2083 | if (PNG_GRAY_COLORMAP_ENTRIES > image->colormap_entries) |
| 2084 | png_error(png_ptr, "gray[16] color-map: too few entries"); |
| 2085 | |
| 2086 | cmap_entries = make_gray_colormap(display); |
| 2087 | |
| 2088 | if (png_ptr->num_trans > 0) |
| 2089 | { |
| 2090 | unsigned int back_alpha; |
| 2091 | |
| 2092 | if (output_format & PNG_FORMAT_FLAG_ALPHA) |
| 2093 | back_alpha = 0; |
| 2094 | |
| 2095 | else |
| 2096 | { |
| 2097 | if (back_r == back_g && back_g == back_b) |
| 2098 | { |
| 2099 | /* Background is gray; no special processing will be |
| 2100 | * required. |
| 2101 | */ |
| 2102 | png_color_16 c; |
| 2103 | png_uint_32 gray = back_g; |
| 2104 | |
| 2105 | if (output_encoding == E_LINEAR) |
| 2106 | { |
| 2107 | gray = PNG_sRGB_FROM_LINEAR(gray * 255); |
| 2108 | |
| 2109 | /* And make sure the corresponding palette entry |
| 2110 | * matches. |
| 2111 | */ |
| 2112 | png_create_colormap_entry(display, gray, back_g, back_g, |
| 2113 | back_g, 65535, E_LINEAR); |
| 2114 | } |
| 2115 | |
| 2116 | /* The background passed to libpng, however, must be the |
| 2117 | * sRGB value. |
| 2118 | */ |
| 2119 | c.index = 0; /*unused*/ |
| 2120 | c.gray = c.red = c.green = c.blue = (png_uint_16)gray; |
| 2121 | |
| 2122 | /* NOTE: does this work without expanding tRNS to alpha? |
| 2123 | * It should be the color->gray case below apparently |
| 2124 | * doesn't. |
| 2125 | */ |
| 2126 | png_set_background_fixed(png_ptr, &c, |
| 2127 | PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/, |
| 2128 | 0/*gamma: not used*/); |
| 2129 | |
| 2130 | output_processing = PNG_CMAP_NONE; |
| 2131 | break; |
| 2132 | } |
| 2133 | |
| 2134 | back_alpha = output_encoding == E_LINEAR ? 65535 : 255; |
| 2135 | } |
| 2136 | |
| 2137 | /* output_processing means that the libpng-processed row will be |
| 2138 | * 8-bit GA and it has to be processing to single byte color-map |
| 2139 | * values. Entry 254 is replaced by either a completely |
| 2140 | * transparent entry or by the background color at full |
| 2141 | * precision (and the background color is not a simple gray leve |
| 2142 | * in this case.) |
| 2143 | */ |
| 2144 | expand_tRNS = 1; |
| 2145 | output_processing = PNG_CMAP_TRANS; |
| 2146 | background_index = 254; |
| 2147 | |
| 2148 | /* And set (overwrite) color-map entry 254 to the actual |
| 2149 | * background color at full precision. |
| 2150 | */ |
| 2151 | png_create_colormap_entry(display, 254, back_r, back_g, back_b, |
| 2152 | back_alpha, output_encoding); |
| 2153 | } |
| 2154 | |
| 2155 | else |
| 2156 | output_processing = PNG_CMAP_NONE; |
| 2157 | } |
| 2158 | break; |
| 2159 | |
| 2160 | case PNG_COLOR_TYPE_GRAY_ALPHA: |
| 2161 | /* 8-bit or 16-bit PNG with two channels - gray and alpha. A minimum |
| 2162 | * of 65536 combinations. If, however, the alpha channel is to be |
| 2163 | * removed there are only 256 possibilities if the background is gray. |
| 2164 | * (Otherwise there is a subset of the 65536 possibilities defined by |
| 2165 | * the triangle between black, white and the background color.) |
| 2166 | * |
| 2167 | * Reduce 16-bit files to 8-bit and sRGB encode the result. No need to |
| 2168 | * worry about tRNS matching - tRNS is ignored if there is an alpha |
| 2169 | * channel. |
| 2170 | */ |
| 2171 | data_encoding = E_sRGB; |
| 2172 | |
| 2173 | if (output_format & PNG_FORMAT_FLAG_ALPHA) |
| 2174 | { |
| 2175 | if (PNG_GA_COLORMAP_ENTRIES > image->colormap_entries) |
| 2176 | png_error(png_ptr, "gray+alpha color-map: too few entries"); |
| 2177 | |
| 2178 | cmap_entries = make_ga_colormap(display); |
| 2179 | |
| 2180 | background_index = PNG_CMAP_GA_BACKGROUND; |
| 2181 | output_processing = PNG_CMAP_GA; |
| 2182 | } |
| 2183 | |
| 2184 | else /* alpha is removed */ |
| 2185 | { |
| 2186 | /* Alpha must be removed as the PNG data is processed when the |
| 2187 | * background is a color because the G and A channels are |
| 2188 | * independent and the vector addition (non-parallel vectors) is a |
| 2189 | * 2-D problem. |
| 2190 | * |
| 2191 | * This can be reduced to the same algorithm as above by making a |
| 2192 | * colormap containing gray levels (for the opaque grays), a |
| 2193 | * background entry (for a transparent pixel) and a set of four six |
| 2194 | * level color values, one set for each intermediate alpha value. |
| 2195 | * See the comments in make_ga_colormap for how this works in the |
| 2196 | * per-pixel processing. |
| 2197 | * |
| 2198 | * If the background is gray, however, we only need a 256 entry gray |
| 2199 | * level color map. It is sufficient to make the entry generated |
| 2200 | * for the background color be exactly the color specified. |
| 2201 | */ |
| 2202 | if ((output_format & PNG_FORMAT_FLAG_COLOR) == 0 || |
| 2203 | (back_r == back_g && back_g == back_b)) |
| 2204 | { |
| 2205 | /* Background is gray; no special processing will be required. */ |
| 2206 | png_color_16 c; |
| 2207 | png_uint_32 gray = back_g; |
| 2208 | |
| 2209 | if (PNG_GRAY_COLORMAP_ENTRIES > image->colormap_entries) |
| 2210 | png_error(png_ptr, "gray-alpha color-map: too few entries"); |
| 2211 | |
| 2212 | cmap_entries = make_gray_colormap(display); |
| 2213 | |
| 2214 | if (output_encoding == E_LINEAR) |
| 2215 | { |
| 2216 | gray = PNG_sRGB_FROM_LINEAR(gray * 255); |
| 2217 | |
| 2218 | /* And make sure the corresponding palette entry matches. */ |
| 2219 | png_create_colormap_entry(display, gray, back_g, back_g, |
| 2220 | back_g, 65535, E_LINEAR); |
| 2221 | } |
| 2222 | |
| 2223 | /* The background passed to libpng, however, must be the sRGB |
| 2224 | * value. |
| 2225 | */ |
| 2226 | c.index = 0; /*unused*/ |
| 2227 | c.gray = c.red = c.green = c.blue = (png_uint_16)gray; |
| 2228 | |
| 2229 | png_set_background_fixed(png_ptr, &c, |
| 2230 | PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/, |
| 2231 | 0/*gamma: not used*/); |
| 2232 | |
| 2233 | output_processing = PNG_CMAP_NONE; |
| 2234 | } |
| 2235 | |
| 2236 | else |
| 2237 | { |
| 2238 | png_uint_32 i, a; |
| 2239 | |
| 2240 | /* This is the same as png_make_ga_colormap, above, except that |
| 2241 | * the entries are all opaque. |
| 2242 | */ |
| 2243 | if (PNG_GA_COLORMAP_ENTRIES > image->colormap_entries) |
| 2244 | png_error(png_ptr, "ga-alpha color-map: too few entries"); |
| 2245 | |
| 2246 | i = 0; |
| 2247 | while (i < 231) |
| 2248 | { |
| 2249 | png_uint_32 gray = (i * 256 + 115) / 231; |
| 2250 | png_create_colormap_entry(display, i++, gray, gray, gray, |
| 2251 | 255, E_sRGB); |
| 2252 | } |
| 2253 | |
| 2254 | /* NOTE: this preserves the full precision of the application |
| 2255 | * background color. |
| 2256 | */ |
| 2257 | background_index = i; |
| 2258 | png_create_colormap_entry(display, i++, back_r, back_g, back_b, |
| 2259 | output_encoding == E_LINEAR ? 65535U : 255U, output_encoding); |
| 2260 | |
| 2261 | /* For non-opaque input composite on the sRGB background - this |
| 2262 | * requires inverting the encoding for each component. The input |
| 2263 | * is still converted to the sRGB encoding because this is a |
| 2264 | * reasonable approximate to the logarithmic curve of human |
| 2265 | * visual sensitivity, at least over the narrow range which PNG |
| 2266 | * represents. Consequently 'G' is always sRGB encoded, while |
| 2267 | * 'A' is linear. We need the linear background colors. |
| 2268 | */ |
| 2269 | if (output_encoding == E_sRGB) /* else already linear */ |
| 2270 | { |
| 2271 | /* This may produce a value not exactly matching the |
| 2272 | * background, but that's ok because these numbers are only |
| 2273 | * used when alpha != 0 |
| 2274 | */ |
| 2275 | back_r = png_sRGB_table[back_r]; |
| 2276 | back_g = png_sRGB_table[back_g]; |
| 2277 | back_b = png_sRGB_table[back_b]; |
| 2278 | } |
| 2279 | |
| 2280 | for (a=1; a<5; ++a) |
| 2281 | { |
| 2282 | unsigned int g; |
| 2283 | |
| 2284 | /* PNG_sRGB_FROM_LINEAR expects a 16-bit linear value scaled |
| 2285 | * by an 8-bit alpha value (0..255). |
| 2286 | */ |
| 2287 | png_uint_32 alpha = 51 * a; |
| 2288 | png_uint_32 back_rx = (255-alpha) * back_r; |
| 2289 | png_uint_32 back_gx = (255-alpha) * back_g; |
| 2290 | png_uint_32 back_bx = (255-alpha) * back_b; |
| 2291 | |
| 2292 | for (g=0; g<6; ++g) |
| 2293 | { |
| 2294 | png_uint_32 gray = png_sRGB_table[g*51] * alpha; |
| 2295 | |
| 2296 | png_create_colormap_entry(display, i++, |
| 2297 | PNG_sRGB_FROM_LINEAR(gray + back_rx), |
| 2298 | PNG_sRGB_FROM_LINEAR(gray + back_gx), |
| 2299 | PNG_sRGB_FROM_LINEAR(gray + back_bx), 255, E_sRGB); |
| 2300 | } |
| 2301 | } |
| 2302 | |
| 2303 | cmap_entries = i; |
| 2304 | output_processing = PNG_CMAP_GA; |
| 2305 | } |
| 2306 | } |
| 2307 | break; |
| 2308 | |
| 2309 | case PNG_COLOR_TYPE_RGB: |
| 2310 | case PNG_COLOR_TYPE_RGB_ALPHA: |
| 2311 | /* Exclude the case where the output is gray; we can always handle this |
| 2312 | * with the cases above. |
| 2313 | */ |
| 2314 | if ((output_format & PNG_FORMAT_FLAG_COLOR) == 0) |
| 2315 | { |
| 2316 | /* The color-map will be grayscale, so we may as well convert the |
| 2317 | * input RGB values to a simple grayscale and use the grayscale |
| 2318 | * code above. |
| 2319 | * |
| 2320 | * NOTE: calling this apparently damages the recognition of the |
| 2321 | * transparent color in background color handling; call |
| 2322 | * png_set_tRNS_to_alpha before png_set_background_fixed. |
| 2323 | */ |
| 2324 | png_set_rgb_to_gray_fixed(png_ptr, PNG_ERROR_ACTION_NONE, -1, |
| 2325 | -1); |
| 2326 | data_encoding = E_sRGB; |
| 2327 | |
| 2328 | /* The output will now be one or two 8-bit gray or gray+alpha |
| 2329 | * channels. The more complex case arises when the input has alpha. |
| 2330 | */ |
| 2331 | if ((png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA || |
| 2332 | png_ptr->num_trans > 0) && |
| 2333 | (output_format & PNG_FORMAT_FLAG_ALPHA) != 0) |
| 2334 | { |
| 2335 | /* Both input and output have an alpha channel, so no background |
| 2336 | * processing is required; just map the GA bytes to the right |
| 2337 | * color-map entry. |
| 2338 | */ |
| 2339 | expand_tRNS = 1; |
| 2340 | |
| 2341 | if (PNG_GA_COLORMAP_ENTRIES > image->colormap_entries) |
| 2342 | png_error(png_ptr, "rgb[ga] color-map: too few entries"); |
| 2343 | |
| 2344 | cmap_entries = make_ga_colormap(display); |
| 2345 | background_index = PNG_CMAP_GA_BACKGROUND; |
| 2346 | output_processing = PNG_CMAP_GA; |
| 2347 | } |
| 2348 | |
| 2349 | else |
| 2350 | { |
| 2351 | /* Either the input or the output has no alpha channel, so there |
| 2352 | * will be no non-opaque pixels in the color-map; it will just be |
| 2353 | * grayscale. |
| 2354 | */ |
| 2355 | if (PNG_GRAY_COLORMAP_ENTRIES > image->colormap_entries) |
| 2356 | png_error(png_ptr, "rgb[gray] color-map: too few entries"); |
| 2357 | |
John Bowler | eda53e5 | 2012-03-18 22:46:28 -0500 | [diff] [blame] | 2358 | /* Ideally this code would use libpng to do the gamma correction, |
| 2359 | * but if an input alpha channel is to be removed we will hit the |
| 2360 | * libpng bug in gamma+compose+rgb-to-gray (the double gamma |
| 2361 | * correction bug). Fix this by dropping the gamma correction in |
| 2362 | * this case and doing it in the palette; this will result in |
| 2363 | * duplicate palette entries, but that's better than the |
| 2364 | * alternative of double gamma correction. |
| 2365 | */ |
| 2366 | if ((png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA || |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame] | 2367 | png_ptr->num_trans > 0) && |
| 2368 | png_gamma_not_sRGB(png_ptr->colorspace.gamma)) |
John Bowler | eda53e5 | 2012-03-18 22:46:28 -0500 | [diff] [blame] | 2369 | { |
| 2370 | cmap_entries = make_gray_file_colormap(display); |
| 2371 | data_encoding = E_FILE; |
| 2372 | } |
| 2373 | |
| 2374 | else |
| 2375 | cmap_entries = make_gray_colormap(display); |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame] | 2376 | |
| 2377 | /* But if the input has alpha or transparency it must be removed |
| 2378 | */ |
| 2379 | if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA || |
| 2380 | png_ptr->num_trans > 0) |
| 2381 | { |
| 2382 | png_color_16 c; |
| 2383 | png_uint_32 gray = back_g; |
| 2384 | |
| 2385 | /* We need to ensure that the application background exists in |
| 2386 | * the colormap and that completely transparent pixels map to |
| 2387 | * it. Achieve this simply by ensuring that the entry |
| 2388 | * selected for the background really is the background color. |
| 2389 | */ |
John Bowler | eda53e5 | 2012-03-18 22:46:28 -0500 | [diff] [blame] | 2390 | if (data_encoding == E_FILE) /* from the fixup above */ |
| 2391 | { |
| 2392 | /* The app supplied a gray which is in output_encoding, we |
| 2393 | * need to convert it to a value of the input (E_FILE) |
| 2394 | * encoding then set this palette entry to the required |
| 2395 | * output encoding. |
| 2396 | */ |
| 2397 | if (output_encoding == E_sRGB) |
| 2398 | gray = png_sRGB_table[gray]; /* now E_LINEAR */ |
| 2399 | |
| 2400 | gray = PNG_DIV257(png_gamma_16bit_correct(gray, |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame] | 2401 | png_ptr->colorspace.gamma)); /* now E_FILE */ |
John Bowler | eda53e5 | 2012-03-18 22:46:28 -0500 | [diff] [blame] | 2402 | |
| 2403 | /* And make sure the corresponding palette entry contains |
| 2404 | * exactly the required sRGB value. |
| 2405 | */ |
| 2406 | png_create_colormap_entry(display, gray, back_g, back_g, |
| 2407 | back_g, 0/*unused*/, output_encoding); |
| 2408 | } |
| 2409 | |
| 2410 | else if (output_encoding == E_LINEAR) |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame] | 2411 | { |
| 2412 | gray = PNG_sRGB_FROM_LINEAR(gray * 255); |
| 2413 | |
| 2414 | /* And make sure the corresponding palette entry matches. |
| 2415 | */ |
| 2416 | png_create_colormap_entry(display, gray, back_g, back_g, |
John Bowler | eda53e5 | 2012-03-18 22:46:28 -0500 | [diff] [blame] | 2417 | back_g, 0/*unused*/, E_LINEAR); |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame] | 2418 | } |
| 2419 | |
John Bowler | eda53e5 | 2012-03-18 22:46:28 -0500 | [diff] [blame] | 2420 | /* The background passed to libpng, however, must be the |
| 2421 | * output (normally sRGB) value. |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame] | 2422 | */ |
| 2423 | c.index = 0; /*unused*/ |
| 2424 | c.gray = c.red = c.green = c.blue = (png_uint_16)gray; |
| 2425 | |
| 2426 | /* NOTE: the following is apparently a bug in libpng. Without |
| 2427 | * it the transparent color recognition in |
| 2428 | * png_set_background_fixed seems to go wrong. |
| 2429 | */ |
| 2430 | expand_tRNS = 1; |
| 2431 | png_set_background_fixed(png_ptr, &c, |
| 2432 | PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/, |
| 2433 | 0/*gamma: not used*/); |
| 2434 | } |
| 2435 | |
| 2436 | output_processing = PNG_CMAP_NONE; |
| 2437 | } |
| 2438 | } |
| 2439 | |
| 2440 | else /* output is color */ |
| 2441 | { |
| 2442 | /* We could use png_quantize here so long as there is no transparent |
| 2443 | * color or alpha; png_quantize ignores alpha. Easier overall just |
| 2444 | * to do it once and using PNG_DIV51 on the 6x6x6 reduced RGB cube. |
| 2445 | * Consequently we always want libpng to produce sRGB data. |
| 2446 | */ |
| 2447 | data_encoding = E_sRGB; |
| 2448 | |
| 2449 | /* Is there any transparency or alpha? */ |
| 2450 | if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA || |
| 2451 | png_ptr->num_trans > 0) |
| 2452 | { |
| 2453 | /* Is there alpha in the output too? If so all four channels are |
| 2454 | * processed into a special RGB cube with alpha support. |
| 2455 | */ |
| 2456 | if (output_format & PNG_FORMAT_FLAG_ALPHA) |
| 2457 | { |
| 2458 | png_uint_32 r; |
| 2459 | |
| 2460 | if (PNG_RGB_COLORMAP_ENTRIES+1+27 > image->colormap_entries) |
| 2461 | png_error(png_ptr, "rgb+alpha color-map: too few entries"); |
| 2462 | |
| 2463 | cmap_entries = make_rgb_colormap(display); |
| 2464 | |
| 2465 | /* Add a transparent entry. */ |
| 2466 | png_create_colormap_entry(display, cmap_entries, 255, 255, |
| 2467 | 255, 0, E_sRGB); |
| 2468 | |
| 2469 | /* This is stored as the background index for the processing |
| 2470 | * algorithm. |
| 2471 | */ |
| 2472 | background_index = cmap_entries++; |
| 2473 | |
| 2474 | /* Add 27 r,g,b entries each with alpha 0.5. */ |
| 2475 | for (r=0; r<256; r = (r << 1) | 0x7f) |
| 2476 | { |
| 2477 | png_uint_32 g; |
| 2478 | |
| 2479 | for (g=0; g<256; g = (g << 1) | 0x7f) |
| 2480 | { |
| 2481 | png_uint_32 b; |
| 2482 | |
| 2483 | /* This generates components with the values 0, 127 and |
| 2484 | * 255 |
| 2485 | */ |
| 2486 | for (b=0; b<256; b = (b << 1) | 0x7f) |
| 2487 | png_create_colormap_entry(display, cmap_entries++, |
| 2488 | r, g, b, 128, E_sRGB); |
| 2489 | } |
| 2490 | } |
| 2491 | |
| 2492 | expand_tRNS = 1; |
| 2493 | output_processing = PNG_CMAP_RGB_ALPHA; |
| 2494 | } |
| 2495 | |
| 2496 | else |
| 2497 | { |
| 2498 | /* Alpha/transparency must be removed. The background must |
| 2499 | * exist in the color map (achieved by setting adding it after |
| 2500 | * the 666 color-map). If the standard processing code will |
| 2501 | * pick up this entry automatically that's all that is |
| 2502 | * required; libpng can be called to do the background |
| 2503 | * processing. |
| 2504 | */ |
| 2505 | unsigned int sample_size = |
| 2506 | PNG_IMAGE_SAMPLE_SIZE(output_format); |
| 2507 | png_uint_32 r, g, b; /* sRGB background */ |
| 2508 | |
| 2509 | if (PNG_RGB_COLORMAP_ENTRIES+1+27 > image->colormap_entries) |
| 2510 | png_error(png_ptr, "rgb-alpha color-map: too few entries"); |
| 2511 | |
| 2512 | cmap_entries = make_rgb_colormap(display); |
| 2513 | |
| 2514 | png_create_colormap_entry(display, cmap_entries, back_r, |
| 2515 | back_g, back_b, 0/*unused*/, output_encoding); |
| 2516 | |
| 2517 | if (output_encoding == E_LINEAR) |
| 2518 | { |
| 2519 | r = PNG_sRGB_FROM_LINEAR(back_r * 255); |
| 2520 | g = PNG_sRGB_FROM_LINEAR(back_g * 255); |
| 2521 | b = PNG_sRGB_FROM_LINEAR(back_b * 255); |
| 2522 | } |
| 2523 | |
| 2524 | else |
| 2525 | { |
| 2526 | r = back_r; |
| 2527 | g = back_g; |
| 2528 | b = back_g; |
| 2529 | } |
| 2530 | |
| 2531 | /* Compare the newly-created color-map entry with the one the |
| 2532 | * PNG_CMAP_RGB algorithm will use. If the two entries don't |
| 2533 | * match, add the new one and set this as the background index. |
| 2534 | */ |
| 2535 | if (memcmp((png_const_bytep)display->colormap + |
| 2536 | sample_size * cmap_entries, |
| 2537 | (png_const_bytep)display->colormap + |
| 2538 | sample_size * PNG_RGB_INDEX(r,g,b), |
| 2539 | sample_size) != 0) |
| 2540 | { |
| 2541 | /* The background color must be added. */ |
| 2542 | background_index = cmap_entries++; |
| 2543 | |
| 2544 | /* Add 27 r,g,b entries each with created by composing with |
| 2545 | * the background at alpha 0.5. |
| 2546 | */ |
| 2547 | for (r=0; r<256; r = (r << 1) | 0x7f) |
| 2548 | { |
| 2549 | for (g=0; g<256; g = (g << 1) | 0x7f) |
| 2550 | { |
| 2551 | /* This generates components with the values 0, 127 |
| 2552 | * and 255 |
| 2553 | */ |
| 2554 | for (b=0; b<256; b = (b << 1) | 0x7f) |
| 2555 | png_create_colormap_entry(display, cmap_entries++, |
| 2556 | png_colormap_compose(display, r, E_sRGB, 128, |
| 2557 | back_r, output_encoding), |
| 2558 | png_colormap_compose(display, g, E_sRGB, 128, |
| 2559 | back_g, output_encoding), |
| 2560 | png_colormap_compose(display, b, E_sRGB, 128, |
| 2561 | back_b, output_encoding), |
| 2562 | 0/*unused*/, output_encoding); |
| 2563 | } |
| 2564 | } |
| 2565 | |
| 2566 | expand_tRNS = 1; |
| 2567 | output_processing = PNG_CMAP_RGB_ALPHA; |
| 2568 | } |
| 2569 | |
| 2570 | else /* background color is in the standard color-map */ |
| 2571 | { |
| 2572 | png_color_16 c; |
| 2573 | |
| 2574 | c.index = 0; /*unused*/ |
| 2575 | c.red = (png_uint_16)back_r; |
| 2576 | c.gray = c.green = (png_uint_16)back_g; |
| 2577 | c.blue = (png_uint_16)back_b; |
| 2578 | |
| 2579 | png_set_background_fixed(png_ptr, &c, |
| 2580 | PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/, |
| 2581 | 0/*gamma: not used*/); |
| 2582 | |
| 2583 | output_processing = PNG_CMAP_RGB; |
| 2584 | } |
| 2585 | } |
| 2586 | } |
| 2587 | |
| 2588 | else /* no alpha or transparency in the input */ |
| 2589 | { |
| 2590 | /* Alpha in the output is irrelevant, simply map the opaque input |
| 2591 | * pixels to the 6x6x6 color-map. |
| 2592 | */ |
| 2593 | if (PNG_RGB_COLORMAP_ENTRIES > image->colormap_entries) |
| 2594 | png_error(png_ptr, "rgb color-map: too few entries"); |
| 2595 | |
| 2596 | cmap_entries = make_rgb_colormap(display); |
| 2597 | output_processing = PNG_CMAP_RGB; |
| 2598 | } |
| 2599 | } |
| 2600 | break; |
| 2601 | |
| 2602 | case PNG_COLOR_TYPE_PALETTE: |
| 2603 | /* It's already got a color-map. It may be necessary to eliminate the |
| 2604 | * tRNS entries though. |
| 2605 | */ |
| 2606 | { |
| 2607 | unsigned int num_trans = png_ptr->num_trans; |
| 2608 | png_const_bytep trans = num_trans > 0 ? png_ptr->trans_alpha : NULL; |
| 2609 | png_const_colorp colormap = png_ptr->palette; |
| 2610 | const int do_background = trans != NULL && |
| 2611 | (output_format & PNG_FORMAT_FLAG_ALPHA) == 0; |
| 2612 | unsigned int i; |
| 2613 | |
| 2614 | /* Just in case: */ |
| 2615 | if (trans == NULL) |
| 2616 | num_trans = 0; |
| 2617 | |
| 2618 | output_processing = PNG_CMAP_NONE; |
| 2619 | data_encoding = E_FILE; /* Don't change from color-map indicies */ |
| 2620 | cmap_entries = png_ptr->num_palette; |
| 2621 | if (cmap_entries > 256) |
| 2622 | cmap_entries = 256; |
| 2623 | |
| 2624 | if (cmap_entries > image->colormap_entries) |
| 2625 | png_error(png_ptr, "palette color-map: too few entries"); |
| 2626 | |
| 2627 | for (i=0; i < cmap_entries; ++i) |
| 2628 | { |
| 2629 | if (do_background && i < num_trans && trans[i] < 255) |
| 2630 | { |
| 2631 | if (trans[i] == 0) |
| 2632 | png_create_colormap_entry(display, i, back_r, back_g, |
| 2633 | back_b, 0, output_encoding); |
| 2634 | |
| 2635 | else |
| 2636 | { |
| 2637 | /* Must compose the PNG file color in the color-map entry |
| 2638 | * on the sRGB color in 'back'. |
| 2639 | */ |
| 2640 | png_create_colormap_entry(display, i, |
| 2641 | png_colormap_compose(display, colormap[i].red, E_FILE, |
| 2642 | trans[i], back_r, output_encoding), |
| 2643 | png_colormap_compose(display, colormap[i].green, E_FILE, |
| 2644 | trans[i], back_g, output_encoding), |
| 2645 | png_colormap_compose(display, colormap[i].blue, E_FILE, |
| 2646 | trans[i], back_b, output_encoding), |
| 2647 | output_encoding == E_LINEAR ? trans[i] * 257U : |
| 2648 | trans[i], |
| 2649 | output_encoding); |
| 2650 | } |
| 2651 | } |
Glenn Randers-Pehrson | 864270e | 2012-02-10 17:13:13 -0600 | [diff] [blame] | 2652 | |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame] | 2653 | else |
| 2654 | png_create_colormap_entry(display, i, colormap[i].red, |
| 2655 | colormap[i].green, colormap[i].blue, |
| 2656 | i < num_trans ? trans[i] : 255U, E_FILE/*8-bit*/); |
| 2657 | } |
| 2658 | |
| 2659 | /* The PNG data may have indicies packed in fewer than 8 bits, it |
| 2660 | * must be expanded if so. |
| 2661 | */ |
| 2662 | if (png_ptr->bit_depth < 8) |
| 2663 | png_set_packing(png_ptr); |
| 2664 | } |
| 2665 | break; |
| 2666 | |
| 2667 | default: |
| 2668 | png_error(png_ptr, "invalid PNG color type"); |
| 2669 | /*NOT REACHED*/ |
| 2670 | break; |
| 2671 | } |
| 2672 | |
| 2673 | /* Now deal with the output processing */ |
| 2674 | if (expand_tRNS && png_ptr->num_trans > 0 && |
| 2675 | (png_ptr->color_type & PNG_COLOR_MASK_ALPHA) == 0) |
| 2676 | png_set_tRNS_to_alpha(png_ptr); |
| 2677 | |
| 2678 | switch (data_encoding) |
| 2679 | { |
| 2680 | default: |
| 2681 | png_error(png_ptr, "bad data option (internal error)"); |
| 2682 | break; |
| 2683 | |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame] | 2684 | case E_sRGB: |
| 2685 | /* Change to 8-bit sRGB */ |
| 2686 | png_set_alpha_mode_fixed(png_ptr, PNG_ALPHA_PNG, PNG_GAMMA_sRGB); |
John Bowler | eda53e5 | 2012-03-18 22:46:28 -0500 | [diff] [blame] | 2687 | /* FALL THROUGH */ |
| 2688 | |
| 2689 | case E_FILE: |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame] | 2690 | if (png_ptr->bit_depth > 8) |
| 2691 | png_set_scale_16(png_ptr); |
| 2692 | break; |
| 2693 | } |
| 2694 | |
| 2695 | if (cmap_entries > 256 || cmap_entries > image->colormap_entries) |
| 2696 | png_error(png_ptr, "color map overflow (BAD internal error)"); |
| 2697 | |
| 2698 | image->colormap_entries = cmap_entries; |
| 2699 | |
| 2700 | /* Double check using the recorded background index */ |
| 2701 | switch (output_processing) |
| 2702 | { |
| 2703 | case PNG_CMAP_NONE: |
| 2704 | if (background_index != PNG_CMAP_NONE_BACKGROUND) |
| 2705 | goto bad_background; |
| 2706 | break; |
| 2707 | |
| 2708 | case PNG_CMAP_GA: |
| 2709 | if (background_index != PNG_CMAP_GA_BACKGROUND) |
| 2710 | goto bad_background; |
| 2711 | break; |
| 2712 | |
| 2713 | case PNG_CMAP_TRANS: |
| 2714 | if (background_index >= cmap_entries || |
| 2715 | background_index != PNG_CMAP_TRANS_BACKGROUND) |
| 2716 | goto bad_background; |
| 2717 | break; |
| 2718 | |
| 2719 | case PNG_CMAP_RGB: |
| 2720 | if (background_index != PNG_CMAP_RGB_BACKGROUND) |
| 2721 | goto bad_background; |
| 2722 | break; |
| 2723 | |
| 2724 | case PNG_CMAP_RGB_ALPHA: |
| 2725 | if (background_index != PNG_CMAP_RGB_ALPHA_BACKGROUND) |
| 2726 | goto bad_background; |
| 2727 | break; |
| 2728 | |
| 2729 | default: |
| 2730 | png_error(png_ptr, "bad processing option (internal error)"); |
| 2731 | |
| 2732 | bad_background: |
| 2733 | png_error(png_ptr, "bad background index (internal error)"); |
| 2734 | } |
| 2735 | |
| 2736 | display->colormap_processing = output_processing; |
| 2737 | |
| 2738 | return 1/*ok*/; |
| 2739 | } |
| 2740 | |
| 2741 | /* The final part of the color-map read called from png_image_finish_read. */ |
| 2742 | static int |
| 2743 | png_image_read_and_map(png_voidp argument) |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 2744 | { |
John Bowler | 4fa96a4 | 2011-11-16 16:39:16 -0600 | [diff] [blame] | 2745 | png_image_read_control *display = png_voidcast(png_image_read_control*, |
| 2746 | argument); |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 2747 | png_imagep image = display->image; |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 2748 | png_structrp png_ptr = image->opaque->png_ptr; |
John Bowler | 4f67e40 | 2011-12-28 08:43:37 -0600 | [diff] [blame] | 2749 | int passes; |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 2750 | |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame] | 2751 | /* Called when the libpng data must be transformed into the color-mapped |
| 2752 | * form. There is a local row buffer in display->local and this routine must |
| 2753 | * do the interlace handling. |
| 2754 | */ |
| 2755 | switch (png_ptr->interlaced) |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 2756 | { |
| 2757 | case PNG_INTERLACE_NONE: |
| 2758 | passes = 1; |
| 2759 | break; |
| 2760 | |
| 2761 | case PNG_INTERLACE_ADAM7: |
| 2762 | passes = PNG_INTERLACE_ADAM7_PASSES; |
| 2763 | break; |
| 2764 | |
| 2765 | default: |
John Bowler | 4f67e40 | 2011-12-28 08:43:37 -0600 | [diff] [blame] | 2766 | passes = 0; |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 2767 | png_error(png_ptr, "unknown interlace type"); |
| 2768 | } |
| 2769 | |
| 2770 | { |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame] | 2771 | png_uint_32 height = image->height; |
| 2772 | png_uint_32 width = image->width; |
| 2773 | int proc = display->colormap_processing; |
| 2774 | png_bytep first_row = display->first_row; |
| 2775 | ptrdiff_t step_row = display->row_bytes; |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 2776 | int pass; |
| 2777 | |
| 2778 | for (pass = 0; pass < passes; ++pass) |
| 2779 | { |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 2780 | unsigned int startx, stepx, stepy; |
| 2781 | png_uint_32 y; |
| 2782 | |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame] | 2783 | if (png_ptr->interlaced == PNG_INTERLACE_ADAM7) |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 2784 | { |
| 2785 | /* The row may be empty for a short image: */ |
| 2786 | if (PNG_PASS_COLS(width, pass) == 0) |
| 2787 | continue; |
| 2788 | |
| 2789 | startx = PNG_PASS_START_COL(pass); |
| 2790 | stepx = PNG_PASS_COL_OFFSET(pass); |
| 2791 | y = PNG_PASS_START_ROW(pass); |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 2792 | stepy = PNG_PASS_ROW_OFFSET(pass); |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 2793 | } |
| 2794 | |
| 2795 | else |
| 2796 | { |
| 2797 | y = 0; |
| 2798 | startx = 0; |
| 2799 | stepx = stepy = 1; |
| 2800 | } |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 2801 | |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 2802 | for (; y<height; y += stepy) |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame] | 2803 | { |
| 2804 | png_bytep inrow = display->local_row; |
| 2805 | png_bytep outrow = first_row + y * step_row; |
| 2806 | png_const_bytep end_row = outrow + width; |
| 2807 | |
| 2808 | /* Read read the libpng data into the temporary buffer. */ |
| 2809 | png_read_row(png_ptr, inrow, NULL); |
| 2810 | |
| 2811 | /* Now process the row according to the processing option, note |
| 2812 | * that the caller verifies that the format of the libpng output |
| 2813 | * data is as required. |
| 2814 | */ |
| 2815 | outrow += startx; |
| 2816 | switch (proc) |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 2817 | { |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame] | 2818 | case PNG_CMAP_GA: |
| 2819 | for (; outrow < end_row; outrow += stepx) |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 2820 | { |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame] | 2821 | /* The data is always in the PNG order */ |
| 2822 | unsigned int gray = *inrow++; |
| 2823 | unsigned int alpha = *inrow++; |
| 2824 | unsigned int entry; |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 2825 | |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame] | 2826 | /* NOTE: this code is copied as a comment in |
| 2827 | * make_ga_colormap above. Please update the |
| 2828 | * comment if you change this code! |
| 2829 | */ |
| 2830 | if (alpha > 229) /* opaque */ |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 2831 | { |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame] | 2832 | entry = (231 * gray + 128) >> 8; |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 2833 | } |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame] | 2834 | else if (alpha < 26) /* transparent */ |
| 2835 | { |
| 2836 | entry = 231; |
| 2837 | } |
| 2838 | else /* partially opaque */ |
| 2839 | { |
| 2840 | entry = 226 + 6 * PNG_DIV51(alpha) + PNG_DIV51(gray); |
| 2841 | } |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 2842 | |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame] | 2843 | *outrow = (png_byte)entry; |
| 2844 | } |
| 2845 | break; |
| 2846 | |
| 2847 | case PNG_CMAP_TRANS: |
| 2848 | for (; outrow < end_row; outrow += stepx) |
| 2849 | { |
| 2850 | png_byte gray = *inrow++; |
| 2851 | png_byte alpha = *inrow++; |
| 2852 | |
| 2853 | if (alpha == 0) |
| 2854 | *outrow = PNG_CMAP_TRANS_BACKGROUND; |
| 2855 | |
| 2856 | else if (gray != PNG_CMAP_TRANS_BACKGROUND) |
| 2857 | *outrow = gray; |
| 2858 | |
| 2859 | else |
| 2860 | *outrow = (png_byte)(PNG_CMAP_TRANS_BACKGROUND+1); |
| 2861 | } |
| 2862 | break; |
| 2863 | |
| 2864 | case PNG_CMAP_RGB: |
| 2865 | for (; outrow < end_row; outrow += stepx) |
| 2866 | { |
| 2867 | *outrow = PNG_RGB_INDEX(inrow[0], inrow[1], inrow[2]); |
| 2868 | inrow += 3; |
| 2869 | } |
| 2870 | break; |
| 2871 | |
| 2872 | case PNG_CMAP_RGB_ALPHA: |
| 2873 | for (; outrow < end_row; outrow += stepx) |
| 2874 | { |
| 2875 | unsigned int alpha = inrow[3]; |
| 2876 | |
| 2877 | /* Because the alpha entries only hold alpha==0.5 values |
| 2878 | * split the processing at alpha==0.25 (64) and 0.75 |
| 2879 | * (196). |
| 2880 | */ |
| 2881 | |
| 2882 | if (alpha >= 196) |
| 2883 | *outrow = PNG_RGB_INDEX(inrow[0], inrow[1], |
| 2884 | inrow[2]); |
| 2885 | |
| 2886 | else if (alpha < 64) |
| 2887 | *outrow = PNG_CMAP_RGB_ALPHA_BACKGROUND; |
| 2888 | |
| 2889 | else |
| 2890 | { |
| 2891 | /* Likewise there are three entries for each of r, g |
| 2892 | * and b. We could select the entry by popcount on |
| 2893 | * the top two bits on those architectures that |
| 2894 | * support it, this is what the code below does, |
| 2895 | * crudely. |
| 2896 | */ |
| 2897 | unsigned int back_i = PNG_CMAP_RGB_ALPHA_BACKGROUND+1; |
| 2898 | |
| 2899 | /* Here are how the values map: |
| 2900 | * |
| 2901 | * 0x00 .. 0x3f -> 0 |
| 2902 | * 0x40 .. 0xbf -> 1 |
| 2903 | * 0xc0 .. 0xff -> 2 |
| 2904 | * |
| 2905 | * So, as above with the explicit alpha checks, the |
| 2906 | * breakpoints are at 64 and 196. |
| 2907 | */ |
| 2908 | if (inrow[0] & 0x80) back_i += 9; /* red */ |
| 2909 | if (inrow[0] & 0x40) back_i += 9; |
| 2910 | if (inrow[0] & 0x80) back_i += 3; /* green */ |
| 2911 | if (inrow[0] & 0x40) back_i += 3; |
| 2912 | if (inrow[0] & 0x80) back_i += 1; /* blue */ |
| 2913 | if (inrow[0] & 0x40) back_i += 1; |
| 2914 | |
| 2915 | *outrow = (png_byte)back_i; |
| 2916 | } |
| 2917 | |
| 2918 | inrow += 4; |
| 2919 | } |
| 2920 | break; |
| 2921 | |
| 2922 | default: |
| 2923 | break; |
| 2924 | } |
| 2925 | } |
| 2926 | } |
| 2927 | } |
| 2928 | |
| 2929 | return 1; |
| 2930 | } |
| 2931 | |
| 2932 | static int |
| 2933 | png_image_read_colormapped(png_voidp argument) |
| 2934 | { |
| 2935 | png_image_read_control *display = png_voidcast(png_image_read_control*, |
| 2936 | argument); |
| 2937 | png_imagep image = display->image; |
| 2938 | png_controlp control = image->opaque; |
| 2939 | png_structrp png_ptr = control->png_ptr; |
| 2940 | png_inforp info_ptr = control->info_ptr; |
| 2941 | |
| 2942 | int passes = 0; /* As a flag */ |
| 2943 | |
| 2944 | PNG_SKIP_CHUNKS(png_ptr); |
| 2945 | |
| 2946 | /* Update the 'info' structure and make sure the result is as required; first |
| 2947 | * make sure to turn on the interlace handling if it will be required |
| 2948 | * (because it can't be turned on *after* the call to png_read_update_info!) |
| 2949 | */ |
| 2950 | if (display->colormap_processing == PNG_CMAP_NONE) |
| 2951 | passes = png_set_interlace_handling(png_ptr); |
| 2952 | |
| 2953 | png_read_update_info(png_ptr, info_ptr); |
| 2954 | |
| 2955 | /* The expected output can be deduced from the colormap_processing option. */ |
| 2956 | switch (display->colormap_processing) |
| 2957 | { |
| 2958 | case PNG_CMAP_NONE: |
| 2959 | /* Output must be one channel and one byte per pixel, the output |
| 2960 | * encoding can be anything. |
| 2961 | */ |
| 2962 | if ((info_ptr->color_type == PNG_COLOR_TYPE_PALETTE || |
| 2963 | info_ptr->color_type == PNG_COLOR_TYPE_GRAY) && |
| 2964 | info_ptr->bit_depth == 8) |
| 2965 | break; |
| 2966 | |
| 2967 | goto bad_output; |
| 2968 | |
| 2969 | case PNG_CMAP_TRANS: |
| 2970 | case PNG_CMAP_GA: |
| 2971 | /* Output must be two channels and the 'G' one must be sRGB, the latter |
| 2972 | * can be checked with an exact number because it should have been set |
| 2973 | * to this number above! |
| 2974 | */ |
| 2975 | if (info_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA && |
| 2976 | info_ptr->bit_depth == 8 && |
| 2977 | png_ptr->screen_gamma == PNG_GAMMA_sRGB && |
| 2978 | image->colormap_entries == 256) |
| 2979 | break; |
| 2980 | |
| 2981 | goto bad_output; |
| 2982 | |
| 2983 | case PNG_CMAP_RGB: |
| 2984 | /* Output must be 8-bit sRGB encoded RGB */ |
| 2985 | if (info_ptr->color_type == PNG_COLOR_TYPE_RGB && |
| 2986 | info_ptr->bit_depth == 8 && |
| 2987 | png_ptr->screen_gamma == PNG_GAMMA_sRGB && |
| 2988 | image->colormap_entries == 216) |
| 2989 | break; |
| 2990 | |
| 2991 | goto bad_output; |
| 2992 | |
| 2993 | case PNG_CMAP_RGB_ALPHA: |
| 2994 | /* Output must be 8-bit sRGB encoded RGBA */ |
| 2995 | if (info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA && |
| 2996 | info_ptr->bit_depth == 8 && |
| 2997 | png_ptr->screen_gamma == PNG_GAMMA_sRGB && |
| 2998 | image->colormap_entries == 244 /* 216 + 1 + 27 */) |
| 2999 | break; |
| 3000 | |
| 3001 | /* goto bad_output; */ |
| 3002 | /* FALL THROUGH */ |
| 3003 | |
| 3004 | default: |
| 3005 | bad_output: |
| 3006 | png_error(png_ptr, "bad color-map processing (internal error)"); |
| 3007 | } |
| 3008 | |
| 3009 | /* Now read the rows. Do this here if it is possible to read directly into |
| 3010 | * the output buffer, otherwise allocate a local row buffer of the maximum |
| 3011 | * size libpng requires and call the relevant processing routine safely. |
| 3012 | */ |
| 3013 | { |
| 3014 | png_bytep first_row = png_voidcast(png_bytep, display->buffer); |
| 3015 | ptrdiff_t row_bytes = display->row_stride; |
| 3016 | |
| 3017 | /* The following expression is designed to work correctly whether it gives |
| 3018 | * a signed or an unsigned result. |
| 3019 | */ |
| 3020 | if (row_bytes < 0) |
| 3021 | first_row += (image->height-1) * (-row_bytes); |
| 3022 | |
| 3023 | display->first_row = first_row; |
| 3024 | display->row_bytes = row_bytes; |
| 3025 | } |
| 3026 | |
| 3027 | if (passes == 0) |
| 3028 | { |
| 3029 | int result; |
| 3030 | png_bytep row = png_voidcast(png_bytep, png_malloc(png_ptr, |
| 3031 | png_get_rowbytes(png_ptr, info_ptr))); |
| 3032 | |
| 3033 | display->local_row = row; |
| 3034 | result = png_safe_execute(image, png_image_read_and_map, display); |
| 3035 | display->local_row = NULL; |
| 3036 | png_free(png_ptr, row); |
| 3037 | |
| 3038 | return result; |
| 3039 | } |
| 3040 | |
| 3041 | else |
| 3042 | { |
| 3043 | png_alloc_size_t row_bytes = display->row_bytes; |
| 3044 | |
| 3045 | while (--passes >= 0) |
| 3046 | { |
| 3047 | png_uint_32 y = image->height; |
| 3048 | png_bytep row = display->first_row; |
Glenn Randers-Pehrson | 864270e | 2012-02-10 17:13:13 -0600 | [diff] [blame] | 3049 | |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame] | 3050 | while (y-- > 0) |
| 3051 | { |
| 3052 | png_read_row(png_ptr, row, NULL); |
| 3053 | row += row_bytes; |
| 3054 | } |
| 3055 | } |
| 3056 | |
| 3057 | return 1; |
| 3058 | } |
| 3059 | } |
| 3060 | |
| 3061 | /* Just the row reading part of png_image_read. */ |
| 3062 | static int |
| 3063 | png_image_read_composite(png_voidp argument) |
| 3064 | { |
| 3065 | png_image_read_control *display = png_voidcast(png_image_read_control*, |
| 3066 | argument); |
| 3067 | png_imagep image = display->image; |
| 3068 | png_structrp png_ptr = image->opaque->png_ptr; |
| 3069 | int passes; |
| 3070 | |
| 3071 | switch (png_ptr->interlaced) |
| 3072 | { |
| 3073 | case PNG_INTERLACE_NONE: |
| 3074 | passes = 1; |
| 3075 | break; |
| 3076 | |
| 3077 | case PNG_INTERLACE_ADAM7: |
| 3078 | passes = PNG_INTERLACE_ADAM7_PASSES; |
| 3079 | break; |
| 3080 | |
| 3081 | default: |
| 3082 | passes = 0; |
| 3083 | png_error(png_ptr, "unknown interlace type"); |
| 3084 | } |
| 3085 | |
| 3086 | { |
| 3087 | png_uint_32 height = image->height; |
| 3088 | png_uint_32 width = image->width; |
| 3089 | png_bytep first_row = display->first_row; |
| 3090 | ptrdiff_t step_row = display->row_bytes; |
| 3091 | unsigned int channels = (image->format & PNG_FORMAT_FLAG_COLOR) ? 3 : 1; |
| 3092 | int pass; |
| 3093 | |
| 3094 | for (pass = 0; pass < passes; ++pass) |
| 3095 | { |
| 3096 | unsigned int startx, stepx, stepy; |
| 3097 | png_uint_32 y; |
| 3098 | |
| 3099 | if (png_ptr->interlaced == PNG_INTERLACE_ADAM7) |
| 3100 | { |
| 3101 | /* The row may be empty for a short image: */ |
| 3102 | if (PNG_PASS_COLS(width, pass) == 0) |
| 3103 | continue; |
| 3104 | |
| 3105 | startx = PNG_PASS_START_COL(pass) * channels; |
| 3106 | stepx = PNG_PASS_COL_OFFSET(pass) * channels; |
| 3107 | y = PNG_PASS_START_ROW(pass); |
| 3108 | stepy = PNG_PASS_ROW_OFFSET(pass); |
| 3109 | } |
| 3110 | |
| 3111 | else |
| 3112 | { |
| 3113 | y = 0; |
| 3114 | startx = 0; |
| 3115 | stepx = channels; |
| 3116 | stepy = 1; |
| 3117 | } |
| 3118 | |
| 3119 | for (; y<height; y += stepy) |
| 3120 | { |
| 3121 | png_bytep inrow = display->local_row; |
| 3122 | png_bytep outrow = first_row + y * step_row; |
| 3123 | png_const_bytep end_row = outrow + width * channels; |
| 3124 | |
| 3125 | /* Read the row, which is packed: */ |
| 3126 | png_read_row(png_ptr, inrow, NULL); |
| 3127 | |
| 3128 | /* Now do the composition on each pixel in this row. */ |
| 3129 | outrow += startx; |
| 3130 | for (; outrow < end_row; outrow += stepx) |
| 3131 | { |
| 3132 | png_byte alpha = inrow[channels]; |
| 3133 | |
| 3134 | if (alpha > 0) /* else no change to the output */ |
| 3135 | { |
| 3136 | unsigned int c; |
| 3137 | |
| 3138 | for (c=0; c<channels; ++c) |
| 3139 | { |
| 3140 | png_uint_32 component = inrow[c]; |
| 3141 | |
| 3142 | if (alpha < 255) /* else just use component */ |
| 3143 | { |
| 3144 | /* This is PNG_OPTIMIZED_ALPHA, the component value |
| 3145 | * is a linear 8-bit value. Combine this with the |
| 3146 | * current outrow[c] value which is sRGB encoded. |
| 3147 | * Arithmetic here is 16-bits to preserve the output |
| 3148 | * values correctly. |
| 3149 | */ |
| 3150 | component *= 257*255; /* =65535 */ |
| 3151 | component += (255-alpha)*png_sRGB_table[outrow[c]]; |
| 3152 | |
| 3153 | /* So 'component' is scaled by 255*65535 and is |
| 3154 | * therefore appropriate for the sRGB to linear |
| 3155 | * conversion table. |
| 3156 | */ |
| 3157 | component = PNG_sRGB_FROM_LINEAR(component); |
| 3158 | } |
| 3159 | |
| 3160 | outrow[c] = (png_byte)component; |
| 3161 | } |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3162 | } |
| 3163 | |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame] | 3164 | inrow += channels+1; /* components and alpha channel */ |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3165 | } |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame] | 3166 | } |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3167 | } |
| 3168 | } |
| 3169 | |
| 3170 | return 1; |
| 3171 | } |
| 3172 | |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3173 | /* The do_local_background case; called when all the following transforms are to |
| 3174 | * be done: |
| 3175 | * |
| 3176 | * PNG_RGB_TO_GRAY |
| 3177 | * PNG_COMPOSITE |
| 3178 | * PNG_GAMMA |
| 3179 | * |
| 3180 | * This is a work-round for the fact that both the PNG_RGB_TO_GRAY and |
| 3181 | * PNG_COMPOSITE code performs gamma correction, so we get double gamma |
| 3182 | * correction. The fix-up is to prevent the PNG_COMPOSITE operation happening |
| 3183 | * inside libpng, so this routine sees an 8 or 16-bit gray+alpha row and handles |
| 3184 | * the removal or pre-multiplication of the alpha channel. |
| 3185 | */ |
| 3186 | static int |
| 3187 | png_image_read_background(png_voidp argument) |
| 3188 | { |
John Bowler | 4fa96a4 | 2011-11-16 16:39:16 -0600 | [diff] [blame] | 3189 | png_image_read_control *display = png_voidcast(png_image_read_control*, |
| 3190 | argument); |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3191 | png_imagep image = display->image; |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 3192 | png_structrp png_ptr = image->opaque->png_ptr; |
| 3193 | png_inforp info_ptr = image->opaque->info_ptr; |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3194 | png_uint_32 height = image->height; |
| 3195 | png_uint_32 width = image->width; |
John Bowler | 4f67e40 | 2011-12-28 08:43:37 -0600 | [diff] [blame] | 3196 | int pass, passes; |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3197 | |
| 3198 | /* Double check the convoluted logic below. We expect to get here with |
| 3199 | * libpng doing rgb to gray and gamma correction but background processing |
| 3200 | * left to the png_image_read_background function. The rows libpng produce |
| 3201 | * might be 8 or 16-bit but should always have two channels; gray plus alpha. |
| 3202 | */ |
| 3203 | if ((png_ptr->transformations & PNG_RGB_TO_GRAY) == 0) |
| 3204 | png_error(png_ptr, "lost rgb to gray"); |
| 3205 | |
| 3206 | if ((png_ptr->transformations & PNG_COMPOSE) != 0) |
| 3207 | png_error(png_ptr, "unexpected compose"); |
| 3208 | |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3209 | if (png_get_channels(png_ptr, info_ptr) != 2) |
| 3210 | png_error(png_ptr, "lost/gained channels"); |
| 3211 | |
Glenn Randers-Pehrson | 864270e | 2012-02-10 17:13:13 -0600 | [diff] [blame] | 3212 | /* Expect the 8-bit case to always remove the alpha channel */ |
| 3213 | if ((image->format & PNG_FORMAT_FLAG_LINEAR) == 0 && |
| 3214 | (image->format & PNG_FORMAT_FLAG_ALPHA) != 0) |
| 3215 | png_error(png_ptr, "unexpected 8-bit transformation"); |
| 3216 | |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame] | 3217 | switch (png_ptr->interlaced) |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3218 | { |
| 3219 | case PNG_INTERLACE_NONE: |
| 3220 | passes = 1; |
| 3221 | break; |
| 3222 | |
| 3223 | case PNG_INTERLACE_ADAM7: |
| 3224 | passes = PNG_INTERLACE_ADAM7_PASSES; |
| 3225 | break; |
| 3226 | |
| 3227 | default: |
John Bowler | 4f67e40 | 2011-12-28 08:43:37 -0600 | [diff] [blame] | 3228 | passes = 0; |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3229 | png_error(png_ptr, "unknown interlace type"); |
| 3230 | } |
| 3231 | |
| 3232 | switch (png_get_bit_depth(png_ptr, info_ptr)) |
| 3233 | { |
| 3234 | default: |
| 3235 | png_error(png_ptr, "unexpected bit depth"); |
| 3236 | break; |
| 3237 | |
| 3238 | case 8: |
| 3239 | /* 8-bit sRGB gray values with an alpha channel; the alpha channel is |
| 3240 | * to be removed by composing on a backgroundi: either the row if |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame] | 3241 | * display->background is NULL or display->background->green if not. |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3242 | * Unlike the code above ALPHA_OPTIMIZED has *not* been done. |
| 3243 | */ |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3244 | { |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame] | 3245 | png_bytep first_row = display->first_row; |
| 3246 | ptrdiff_t step_row = display->row_bytes; |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3247 | |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame] | 3248 | for (pass = 0; pass < passes; ++pass) |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3249 | { |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame] | 3250 | png_bytep row = display->first_row; |
| 3251 | unsigned int startx, stepx, stepy; |
| 3252 | png_uint_32 y; |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3253 | |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame] | 3254 | if (png_ptr->interlaced == PNG_INTERLACE_ADAM7) |
| 3255 | { |
| 3256 | /* The row may be empty for a short image: */ |
| 3257 | if (PNG_PASS_COLS(width, pass) == 0) |
| 3258 | continue; |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3259 | |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame] | 3260 | startx = PNG_PASS_START_COL(pass); |
| 3261 | stepx = PNG_PASS_COL_OFFSET(pass); |
| 3262 | y = PNG_PASS_START_ROW(pass); |
| 3263 | stepy = PNG_PASS_ROW_OFFSET(pass); |
| 3264 | } |
| 3265 | |
| 3266 | else |
| 3267 | { |
| 3268 | y = 0; |
| 3269 | startx = 0; |
| 3270 | stepx = stepy = 1; |
| 3271 | } |
Glenn Randers-Pehrson | 864270e | 2012-02-10 17:13:13 -0600 | [diff] [blame] | 3272 | |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame] | 3273 | if (display->background == NULL) |
| 3274 | { |
| 3275 | for (; y<height; y += stepy) |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3276 | { |
| 3277 | png_bytep inrow = display->local_row; |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame] | 3278 | png_bytep outrow = first_row + y * step_row; |
| 3279 | png_const_bytep end_row = outrow + width; |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3280 | |
| 3281 | /* Read the row, which is packed: */ |
| 3282 | png_read_row(png_ptr, inrow, NULL); |
| 3283 | |
| 3284 | /* Now do the composition on each pixel in this row. */ |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame] | 3285 | outrow += startx; |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3286 | for (; outrow < end_row; outrow += stepx) |
| 3287 | { |
| 3288 | png_byte alpha = inrow[1]; |
| 3289 | |
| 3290 | if (alpha > 0) /* else no change to the output */ |
| 3291 | { |
| 3292 | png_uint_32 component = inrow[0]; |
| 3293 | |
| 3294 | if (alpha < 255) /* else just use component */ |
| 3295 | { |
| 3296 | /* Since PNG_OPTIMIZED_ALPHA was not set it is |
| 3297 | * necessary to invert the sRGB transfer |
| 3298 | * function and multiply the alpha out. |
| 3299 | */ |
| 3300 | component = png_sRGB_table[component] * alpha; |
| 3301 | component += png_sRGB_table[outrow[0]] * |
| 3302 | (255-alpha); |
| 3303 | component = PNG_sRGB_FROM_LINEAR(component); |
| 3304 | } |
| 3305 | |
| 3306 | outrow[0] = (png_byte)component; |
| 3307 | } |
| 3308 | |
| 3309 | inrow += 2; /* gray and alpha channel */ |
| 3310 | } |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3311 | } |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame] | 3312 | } |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3313 | |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame] | 3314 | else /* constant background value */ |
| 3315 | { |
| 3316 | png_byte background8 = display->background->green; |
| 3317 | png_uint_16 background = png_sRGB_table[background8]; |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3318 | |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame] | 3319 | for (; y<height; y += stepy) |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3320 | { |
| 3321 | png_bytep inrow = display->local_row; |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame] | 3322 | png_bytep outrow = first_row + y * step_row; |
| 3323 | png_const_bytep end_row = outrow + width; |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3324 | |
| 3325 | /* Read the row, which is packed: */ |
| 3326 | png_read_row(png_ptr, inrow, NULL); |
| 3327 | |
| 3328 | /* Now do the composition on each pixel in this row. */ |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame] | 3329 | outrow += startx; |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3330 | for (; outrow < end_row; outrow += stepx) |
| 3331 | { |
| 3332 | png_byte alpha = inrow[1]; |
| 3333 | |
| 3334 | if (alpha > 0) /* else use background */ |
| 3335 | { |
| 3336 | png_uint_32 component = inrow[0]; |
| 3337 | |
| 3338 | if (alpha < 255) /* else just use component */ |
| 3339 | { |
| 3340 | component = png_sRGB_table[component] * alpha; |
| 3341 | component += background * (255-alpha); |
| 3342 | component = PNG_sRGB_FROM_LINEAR(component); |
| 3343 | } |
| 3344 | |
| 3345 | outrow[0] = (png_byte)component; |
| 3346 | } |
| 3347 | |
| 3348 | else |
| 3349 | outrow[0] = background8; |
| 3350 | |
| 3351 | inrow += 2; /* gray and alpha channel */ |
| 3352 | } |
| 3353 | |
| 3354 | row += display->row_bytes; |
| 3355 | } |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame] | 3356 | } |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3357 | } |
| 3358 | } |
| 3359 | break; |
| 3360 | |
| 3361 | case 16: |
| 3362 | /* 16-bit linear with pre-multiplied alpha; the pre-multiplication must |
| 3363 | * still be done and, maybe, the alpha channel removed. This code also |
| 3364 | * handles the alpha-first option. |
| 3365 | */ |
| 3366 | { |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame] | 3367 | png_bytep first_row = display->first_row; |
| 3368 | ptrdiff_t step_row = display->row_bytes; |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3369 | int preserve_alpha = (image->format & PNG_FORMAT_FLAG_ALPHA) != 0; |
Glenn Randers-Pehrson | 864270e | 2012-02-10 17:13:13 -0600 | [diff] [blame] | 3370 | unsigned int outchannels = 1+preserve_alpha; |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3371 | int swap_alpha = 0; |
| 3372 | |
| 3373 | if (preserve_alpha && (image->format & PNG_FORMAT_FLAG_AFIRST)) |
| 3374 | swap_alpha = 1; |
| 3375 | |
| 3376 | for (pass = 0; pass < passes; ++pass) |
| 3377 | { |
Glenn Randers-Pehrson | 864270e | 2012-02-10 17:13:13 -0600 | [diff] [blame] | 3378 | unsigned int startx, stepx, stepy; |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3379 | png_uint_32 y; |
| 3380 | |
Glenn Randers-Pehrson | 864270e | 2012-02-10 17:13:13 -0600 | [diff] [blame] | 3381 | /* The 'x' start and step are adjusted to output components here. |
| 3382 | */ |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame] | 3383 | if (png_ptr->interlaced == PNG_INTERLACE_ADAM7) |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3384 | { |
| 3385 | /* The row may be empty for a short image: */ |
| 3386 | if (PNG_PASS_COLS(width, pass) == 0) |
| 3387 | continue; |
| 3388 | |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame] | 3389 | startx = PNG_PASS_START_COL(pass) * outchannels; |
| 3390 | stepx = PNG_PASS_COL_OFFSET(pass) * outchannels; |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3391 | y = PNG_PASS_START_ROW(pass); |
| 3392 | stepy = PNG_PASS_ROW_OFFSET(pass); |
| 3393 | } |
| 3394 | |
| 3395 | else |
| 3396 | { |
| 3397 | y = 0; |
| 3398 | startx = 0; |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame] | 3399 | stepx = outchannels; |
| 3400 | stepy = 1; |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3401 | } |
| 3402 | |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3403 | for (; y<height; y += stepy) |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame] | 3404 | { |
| 3405 | png_const_uint_16p inrow; |
| 3406 | png_uint_16p outrow = (png_uint_16p)(first_row + y*step_row); |
| 3407 | png_uint_16p end_row = outrow + width * outchannels; |
| 3408 | |
| 3409 | /* Read the row, which is packed: */ |
| 3410 | png_read_row(png_ptr, display->local_row, NULL); |
| 3411 | inrow = (png_const_uint_16p)display->local_row; |
| 3412 | |
| 3413 | /* Now do the pre-multiplication on each pixel in this row. |
| 3414 | */ |
| 3415 | outrow += startx; |
| 3416 | for (; outrow < end_row; outrow += stepx) |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3417 | { |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame] | 3418 | png_uint_32 component = inrow[0]; |
| 3419 | png_uint_16 alpha = inrow[1]; |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3420 | |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame] | 3421 | if (alpha > 0) /* else 0 */ |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3422 | { |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame] | 3423 | if (alpha < 65535) /* else just use component */ |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3424 | { |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame] | 3425 | component *= alpha; |
| 3426 | component += 32767; |
| 3427 | component /= 65535; |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3428 | } |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3429 | } |
| 3430 | |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame] | 3431 | else |
| 3432 | component = 0; |
| 3433 | |
| 3434 | outrow[swap_alpha] = (png_uint_16)component; |
Glenn Randers-Pehrson | 864270e | 2012-02-10 17:13:13 -0600 | [diff] [blame] | 3435 | if (preserve_alpha) |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame] | 3436 | outrow[1 ^ swap_alpha] = alpha; |
| 3437 | |
| 3438 | inrow += 2; /* components and alpha channel */ |
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 | } |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3441 | } |
| 3442 | } |
| 3443 | break; |
| 3444 | } |
| 3445 | |
| 3446 | return 1; |
| 3447 | } |
| 3448 | |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3449 | /* The guts of png_image_finish_read as a png_safe_execute callback. */ |
| 3450 | static int |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame] | 3451 | png_image_read_direct(png_voidp argument) |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3452 | { |
John Bowler | 4fa96a4 | 2011-11-16 16:39:16 -0600 | [diff] [blame] | 3453 | png_image_read_control *display = png_voidcast(png_image_read_control*, |
| 3454 | argument); |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3455 | png_imagep image = display->image; |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 3456 | png_structrp png_ptr = image->opaque->png_ptr; |
| 3457 | png_inforp info_ptr = image->opaque->info_ptr; |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3458 | |
| 3459 | png_uint_32 format = image->format; |
| 3460 | int linear = (format & PNG_FORMAT_FLAG_LINEAR) != 0; |
| 3461 | int do_local_compose = 0; |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3462 | int do_local_background = 0; /* to avoid double gamma correction bug */ |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3463 | int passes = 0; |
| 3464 | |
| 3465 | /* Add transforms to ensure the correct output format is produced then check |
| 3466 | * that the required implementation support is there. Always expand; always |
| 3467 | * need 8 bits minimum, no palette and expanded tRNS. |
| 3468 | */ |
| 3469 | png_set_expand(png_ptr); |
Glenn Randers-Pehrson | 864270e | 2012-02-10 17:13:13 -0600 | [diff] [blame] | 3470 | |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3471 | /* Now check the format to see if it was modified. */ |
| 3472 | { |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame] | 3473 | png_uint_32 base_format = png_image_format(png_ptr) & |
| 3474 | ~PNG_FORMAT_FLAG_COLORMAP /* removed by png_set_expand */; |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3475 | png_uint_32 change = format ^ base_format; |
John Bowler | 3615d03 | 2011-11-08 10:38:09 -0600 | [diff] [blame] | 3476 | png_fixed_point output_gamma; |
| 3477 | int mode; /* alpha mode */ |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3478 | |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3479 | /* Do this first so that we have a record if rgb to gray is happening. */ |
| 3480 | if (change & PNG_FORMAT_FLAG_COLOR) |
| 3481 | { |
| 3482 | /* gray<->color transformation required. */ |
| 3483 | if (format & PNG_FORMAT_FLAG_COLOR) |
| 3484 | png_set_gray_to_rgb(png_ptr); |
| 3485 | |
| 3486 | else |
| 3487 | { |
| 3488 | /* libpng can't do both rgb to gray and |
| 3489 | * background/pre-multiplication if there is also significant gamma |
| 3490 | * correction, because both operations require linear colors and |
| 3491 | * the code only supports one transform doing the gamma correction. |
| 3492 | * Handle this by doing the pre-multiplication or background |
| 3493 | * operation in this code, if necessary. |
| 3494 | * |
| 3495 | * TODO: fix this by rewriting pngrtran.c (!) |
| 3496 | * |
| 3497 | * For the moment (given that fixing this in pngrtran.c is an |
| 3498 | * enormous change) 'do_local_background' is used to indicate that |
| 3499 | * the problem exists. |
| 3500 | */ |
| 3501 | if (base_format & PNG_FORMAT_FLAG_ALPHA) |
| 3502 | do_local_background = 1/*maybe*/; |
| 3503 | |
| 3504 | png_set_rgb_to_gray_fixed(png_ptr, PNG_ERROR_ACTION_NONE, |
| 3505 | PNG_RGB_TO_GRAY_DEFAULT, PNG_RGB_TO_GRAY_DEFAULT); |
| 3506 | } |
| 3507 | |
| 3508 | change &= ~PNG_FORMAT_FLAG_COLOR; |
| 3509 | } |
| 3510 | |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3511 | /* Set the gamma appropriately, linear for 16-bit input, sRGB otherwise. |
| 3512 | */ |
| 3513 | { |
John Bowler | 3615d03 | 2011-11-08 10:38:09 -0600 | [diff] [blame] | 3514 | png_fixed_point input_gamma_default; |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3515 | |
| 3516 | if (base_format & PNG_FORMAT_FLAG_LINEAR) |
| 3517 | input_gamma_default = PNG_GAMMA_LINEAR; |
| 3518 | else |
| 3519 | input_gamma_default = PNG_DEFAULT_sRGB; |
| 3520 | |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3521 | /* Call png_set_alpha_mode to set the default for the input gamma; the |
| 3522 | * output gamma is set by a second call below. |
| 3523 | */ |
| 3524 | png_set_alpha_mode_fixed(png_ptr, PNG_ALPHA_PNG, input_gamma_default); |
| 3525 | } |
| 3526 | |
| 3527 | if (linear) |
| 3528 | { |
| 3529 | /* If there *is* an alpha channel in the input it must be multiplied |
| 3530 | * out; use PNG_ALPHA_STANDARD, otherwise just use PNG_ALPHA_PNG. |
| 3531 | */ |
| 3532 | if (base_format & PNG_FORMAT_FLAG_ALPHA) |
John Bowler | 3615d03 | 2011-11-08 10:38:09 -0600 | [diff] [blame] | 3533 | mode = PNG_ALPHA_STANDARD; /* associated alpha */ |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3534 | |
| 3535 | else |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3536 | mode = PNG_ALPHA_PNG; |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3537 | |
| 3538 | output_gamma = PNG_GAMMA_LINEAR; |
| 3539 | } |
| 3540 | |
| 3541 | else |
| 3542 | { |
| 3543 | mode = PNG_ALPHA_PNG; |
| 3544 | output_gamma = PNG_DEFAULT_sRGB; |
| 3545 | } |
| 3546 | |
| 3547 | /* If 'do_local_background' is set check for the presence of gamma |
| 3548 | * correction; this is part of the work-round for the libpng bug |
| 3549 | * described above. |
| 3550 | * |
| 3551 | * TODO: fix libpng and remove this. |
| 3552 | */ |
| 3553 | if (do_local_background) |
| 3554 | { |
| 3555 | png_fixed_point gtest; |
| 3556 | |
| 3557 | /* This is 'png_gamma_threshold' from pngrtran.c; the test used for |
| 3558 | * gamma correction, the screen gamma hasn't been set on png_struct |
| 3559 | * yet; it's set below. png_struct::gamma, however, is set to the |
| 3560 | * final value. |
| 3561 | */ |
John Bowler | b11b31a | 2012-03-21 07:55:46 -0500 | [diff] [blame] | 3562 | if (png_muldiv(>est, output_gamma, png_ptr->colorspace.gamma, |
| 3563 | PNG_FP_1) && !png_gamma_significant(gtest)) |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3564 | do_local_background = 0; |
| 3565 | |
| 3566 | else if (mode == PNG_ALPHA_STANDARD) |
| 3567 | { |
| 3568 | do_local_background = 2/*required*/; |
| 3569 | mode = PNG_ALPHA_PNG; /* prevent libpng doing it */ |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3570 | } |
| 3571 | |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3572 | /* else leave as 1 for the checks below */ |
John Bowler | 3615d03 | 2011-11-08 10:38:09 -0600 | [diff] [blame] | 3573 | } |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3574 | |
John Bowler | 3615d03 | 2011-11-08 10:38:09 -0600 | [diff] [blame] | 3575 | /* If the bit-depth changes then handle that here. */ |
| 3576 | if (change & PNG_FORMAT_FLAG_LINEAR) |
| 3577 | { |
| 3578 | if (linear /*16-bit output*/) |
| 3579 | png_set_expand_16(png_ptr); |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3580 | |
John Bowler | 3615d03 | 2011-11-08 10:38:09 -0600 | [diff] [blame] | 3581 | else /* 8-bit output */ |
| 3582 | png_set_scale_16(png_ptr); |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3583 | |
John Bowler | 3615d03 | 2011-11-08 10:38:09 -0600 | [diff] [blame] | 3584 | change &= ~PNG_FORMAT_FLAG_LINEAR; |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3585 | } |
| 3586 | |
| 3587 | /* Now the background/alpha channel changes. */ |
| 3588 | if (change & PNG_FORMAT_FLAG_ALPHA) |
| 3589 | { |
| 3590 | /* Removing an alpha channel requires composition for the 8-bit |
| 3591 | * formats; for the 16-bit it is already done, above, by the |
| 3592 | * pre-multiplication and the channel just needs to be stripped. |
| 3593 | */ |
| 3594 | if (base_format & PNG_FORMAT_FLAG_ALPHA) |
| 3595 | { |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3596 | /* If RGB->gray is happening the alpha channel must be left and the |
| 3597 | * operation completed locally. |
| 3598 | * |
| 3599 | * TODO: fix libpng and remove this. |
| 3600 | */ |
| 3601 | if (do_local_background) |
| 3602 | do_local_background = 2/*required*/; |
| 3603 | |
John Bowler | 3615d03 | 2011-11-08 10:38:09 -0600 | [diff] [blame] | 3604 | /* 16-bit output: just remove the channel */ |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3605 | else if (linear) /* compose on black (well, pre-multiply) */ |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3606 | png_set_strip_alpha(png_ptr); |
| 3607 | |
John Bowler | 3615d03 | 2011-11-08 10:38:09 -0600 | [diff] [blame] | 3608 | /* 8-bit output: do an appropriate compose */ |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3609 | else if (display->background != NULL) |
| 3610 | { |
| 3611 | png_color_16 c; |
| 3612 | |
| 3613 | c.index = 0; /*unused*/ |
| 3614 | c.red = display->background->red; |
| 3615 | c.green = display->background->green; |
| 3616 | c.blue = display->background->blue; |
| 3617 | c.gray = display->background->green; |
| 3618 | |
| 3619 | /* This is always an 8-bit sRGB value, using the 'green' channel |
| 3620 | * for gray is much better than calculating the luminance here; |
| 3621 | * we can get off-by-one errors in that calculation relative to |
| 3622 | * the app expectations and that will show up in transparent |
| 3623 | * pixels. |
| 3624 | */ |
| 3625 | png_set_background_fixed(png_ptr, &c, |
| 3626 | PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/, |
| 3627 | 0/*gamma: not used*/); |
| 3628 | } |
| 3629 | |
| 3630 | else /* compose on row: implemented below. */ |
| 3631 | { |
| 3632 | do_local_compose = 1; |
Glenn Randers-Pehrson | 84b0da9 | 2011-11-10 06:32:19 -0600 | [diff] [blame] | 3633 | /* 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] | 3634 | * removed by the code below. Set the encoding to the 'OPTIMIZE' |
| 3635 | * one so the code only has to hack on the pixels that require |
| 3636 | * composition. |
| 3637 | */ |
John Bowler | 3615d03 | 2011-11-08 10:38:09 -0600 | [diff] [blame] | 3638 | mode = PNG_ALPHA_OPTIMIZED; |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3639 | } |
| 3640 | } |
| 3641 | |
| 3642 | else /* output needs an alpha channel */ |
| 3643 | { |
| 3644 | /* This is tricky because it happens before the swap operation has |
Glenn Randers-Pehrson | 84b0da9 | 2011-11-10 06:32:19 -0600 | [diff] [blame] | 3645 | * been accomplished; however, the swap does *not* swap the added |
John Bowler | e6fb691 | 2011-11-08 21:35:16 -0600 | [diff] [blame] | 3646 | * alpha channel (weird API), so it must be added in the correct |
| 3647 | * place. |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3648 | */ |
John Bowler | 3615d03 | 2011-11-08 10:38:09 -0600 | [diff] [blame] | 3649 | png_uint_32 filler; /* opaque filler */ |
John Bowler | e6fb691 | 2011-11-08 21:35:16 -0600 | [diff] [blame] | 3650 | int where; |
John Bowler | 3615d03 | 2011-11-08 10:38:09 -0600 | [diff] [blame] | 3651 | |
| 3652 | if (linear) |
| 3653 | filler = 65535; |
| 3654 | |
| 3655 | else |
| 3656 | filler = 255; |
| 3657 | |
John Bowler | e6fb691 | 2011-11-08 21:35:16 -0600 | [diff] [blame] | 3658 | # ifdef PNG_FORMAT_AFIRST_SUPPORTED |
| 3659 | if (format & PNG_FORMAT_FLAG_AFIRST) |
| 3660 | { |
| 3661 | where = PNG_FILLER_BEFORE; |
| 3662 | change &= ~PNG_FORMAT_FLAG_AFIRST; |
| 3663 | } |
| 3664 | |
| 3665 | else |
| 3666 | # endif |
| 3667 | where = PNG_FILLER_AFTER; |
| 3668 | |
| 3669 | png_set_add_alpha(png_ptr, filler, where); |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3670 | } |
| 3671 | |
John Bowler | e6fb691 | 2011-11-08 21:35:16 -0600 | [diff] [blame] | 3672 | /* This stops the (irrelevant) call to swap_alpha below. */ |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3673 | change &= ~PNG_FORMAT_FLAG_ALPHA; |
| 3674 | } |
| 3675 | |
John Bowler | 3615d03 | 2011-11-08 10:38:09 -0600 | [diff] [blame] | 3676 | /* Now set the alpha mode correctly; this is always done, even if there is |
| 3677 | * no alpha channel in either the input or the output because it correctly |
| 3678 | * sets the output gamma. |
| 3679 | */ |
| 3680 | png_set_alpha_mode_fixed(png_ptr, mode, output_gamma); |
| 3681 | |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3682 | # ifdef PNG_FORMAT_BGR_SUPPORTED |
John Bowler | e6fb691 | 2011-11-08 21:35:16 -0600 | [diff] [blame] | 3683 | if (change & PNG_FORMAT_FLAG_BGR) |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3684 | { |
Glenn Randers-Pehrson | 84b0da9 | 2011-11-10 06:32:19 -0600 | [diff] [blame] | 3685 | /* 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] | 3686 | * the output is gray, but fix up the 'format' value in that case. |
| 3687 | */ |
| 3688 | if (format & PNG_FORMAT_FLAG_COLOR) |
| 3689 | png_set_bgr(png_ptr); |
| 3690 | |
| 3691 | else |
| 3692 | format &= ~PNG_FORMAT_FLAG_BGR; |
| 3693 | |
| 3694 | change &= ~PNG_FORMAT_FLAG_BGR; |
| 3695 | } |
| 3696 | # endif |
| 3697 | |
| 3698 | # ifdef PNG_FORMAT_AFIRST_SUPPORTED |
John Bowler | e6fb691 | 2011-11-08 21:35:16 -0600 | [diff] [blame] | 3699 | if (change & PNG_FORMAT_FLAG_AFIRST) |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3700 | { |
| 3701 | /* Only relevant if there is an alpha channel - it's particularly |
| 3702 | * important to handle this correctly because do_local_compose may |
| 3703 | * be set above and then libpng will keep the alpha channel for this |
| 3704 | * code to remove. |
| 3705 | */ |
| 3706 | if (format & PNG_FORMAT_FLAG_ALPHA) |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3707 | { |
| 3708 | /* Disable this if doing a local background, |
| 3709 | * TODO: remove this when local background is no longer required. |
| 3710 | */ |
| 3711 | if (do_local_background != 2) |
| 3712 | png_set_swap_alpha(png_ptr); |
| 3713 | } |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3714 | |
| 3715 | else |
| 3716 | format &= ~PNG_FORMAT_FLAG_AFIRST; |
| 3717 | |
| 3718 | change &= ~PNG_FORMAT_FLAG_AFIRST; |
| 3719 | } |
| 3720 | # endif |
| 3721 | |
| 3722 | /* If the *output* is 16-bit then we need to check for a byte-swap on this |
| 3723 | * architecture. |
| 3724 | */ |
| 3725 | if (linear) |
| 3726 | { |
| 3727 | PNG_CONST png_uint_16 le = 0x0001; |
| 3728 | |
| 3729 | if (*(png_const_bytep)&le) |
| 3730 | png_set_swap(png_ptr); |
| 3731 | } |
| 3732 | |
| 3733 | /* If change is not now 0 some transformation is missing - error out. */ |
| 3734 | if (change) |
| 3735 | png_error(png_ptr, "png_read_image: unsupported transformation"); |
| 3736 | } |
| 3737 | |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame] | 3738 | PNG_SKIP_CHUNKS(png_ptr); |
John Bowler | 89c2f84 | 2011-11-16 12:04:39 -0600 | [diff] [blame] | 3739 | |
Glenn Randers-Pehrson | 84b0da9 | 2011-11-10 06:32:19 -0600 | [diff] [blame] | 3740 | /* 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] | 3741 | * make sure to turn on the interlace handling if it will be required |
| 3742 | * (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] | 3743 | * |
| 3744 | * TODO: remove the do_local_background fixup below. |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3745 | */ |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3746 | if (!do_local_compose && do_local_background != 2) |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3747 | passes = png_set_interlace_handling(png_ptr); |
| 3748 | |
| 3749 | png_read_update_info(png_ptr, info_ptr); |
| 3750 | |
| 3751 | { |
| 3752 | png_uint_32 info_format = 0; |
| 3753 | |
| 3754 | if (info_ptr->color_type & PNG_COLOR_MASK_COLOR) |
| 3755 | info_format |= PNG_FORMAT_FLAG_COLOR; |
| 3756 | |
| 3757 | if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA) |
| 3758 | { |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3759 | /* do_local_compose removes this channel below. */ |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3760 | if (!do_local_compose) |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3761 | { |
| 3762 | /* do_local_background does the same if required. */ |
| 3763 | if (do_local_background != 2 || |
| 3764 | (format & PNG_FORMAT_FLAG_ALPHA) != 0) |
| 3765 | info_format |= PNG_FORMAT_FLAG_ALPHA; |
| 3766 | } |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3767 | } |
| 3768 | |
| 3769 | else if (do_local_compose) /* internal error */ |
| 3770 | png_error(png_ptr, "png_image_read: alpha channel lost"); |
| 3771 | |
| 3772 | if (info_ptr->bit_depth == 16) |
| 3773 | info_format |= PNG_FORMAT_FLAG_LINEAR; |
| 3774 | |
| 3775 | # ifdef PNG_FORMAT_BGR_SUPPORTED |
| 3776 | if (png_ptr->transformations & PNG_BGR) |
| 3777 | info_format |= PNG_FORMAT_FLAG_BGR; |
| 3778 | # endif |
| 3779 | |
| 3780 | # ifdef PNG_FORMAT_AFIRST_SUPPORTED |
Glenn Randers-Pehrson | 864270e | 2012-02-10 17:13:13 -0600 | [diff] [blame] | 3781 | if (do_local_background == 2) |
| 3782 | { |
| 3783 | if (format & PNG_FORMAT_FLAG_AFIRST) |
| 3784 | info_format |= PNG_FORMAT_FLAG_AFIRST; |
| 3785 | } |
| 3786 | |
| 3787 | if ((png_ptr->transformations & PNG_SWAP_ALPHA) != 0 || |
John Bowler | e6fb691 | 2011-11-08 21:35:16 -0600 | [diff] [blame] | 3788 | ((png_ptr->transformations & PNG_ADD_ALPHA) != 0 && |
| 3789 | (png_ptr->flags & PNG_FLAG_FILLER_AFTER) == 0)) |
Glenn Randers-Pehrson | 864270e | 2012-02-10 17:13:13 -0600 | [diff] [blame] | 3790 | { |
| 3791 | if (do_local_background == 2) |
| 3792 | png_error(png_ptr, "unexpected alpha swap transformation"); |
| 3793 | |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3794 | info_format |= PNG_FORMAT_FLAG_AFIRST; |
Glenn Randers-Pehrson | 864270e | 2012-02-10 17:13:13 -0600 | [diff] [blame] | 3795 | } |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3796 | # endif |
| 3797 | |
| 3798 | /* This is actually an internal error. */ |
| 3799 | if (info_format != format) |
| 3800 | png_error(png_ptr, "png_read_image: invalid transformations"); |
| 3801 | } |
| 3802 | |
| 3803 | /* Now read the rows. If do_local_compose is set then it is necessary to use |
| 3804 | * a local row buffer. The output will be GA, RGBA or BGRA and must be |
| 3805 | * converted to G, RGB or BGR as appropriate. The 'local_row' member of the |
| 3806 | * display acts as a flag. |
| 3807 | */ |
| 3808 | { |
John Bowler | 4fa96a4 | 2011-11-16 16:39:16 -0600 | [diff] [blame] | 3809 | png_bytep first_row = png_voidcast(png_bytep, display->buffer); |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3810 | ptrdiff_t row_bytes = display->row_stride; |
| 3811 | |
| 3812 | if (linear) |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame] | 3813 | row_bytes *= 2; |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3814 | |
| 3815 | /* The following expression is designed to work correctly whether it gives |
| 3816 | * a signed or an unsigned result. |
| 3817 | */ |
| 3818 | if (row_bytes < 0) |
| 3819 | first_row += (image->height-1) * (-row_bytes); |
| 3820 | |
| 3821 | display->first_row = first_row; |
| 3822 | display->row_bytes = row_bytes; |
| 3823 | } |
| 3824 | |
| 3825 | if (do_local_compose) |
| 3826 | { |
| 3827 | int result; |
John Bowler | 4fa96a4 | 2011-11-16 16:39:16 -0600 | [diff] [blame] | 3828 | png_bytep row = png_voidcast(png_bytep, png_malloc(png_ptr, |
| 3829 | png_get_rowbytes(png_ptr, info_ptr))); |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3830 | |
| 3831 | display->local_row = row; |
| 3832 | result = png_safe_execute(image, png_image_read_composite, display); |
| 3833 | display->local_row = NULL; |
| 3834 | png_free(png_ptr, row); |
| 3835 | |
| 3836 | return result; |
| 3837 | } |
| 3838 | |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3839 | else if (do_local_background == 2) |
| 3840 | { |
| 3841 | int result; |
John Bowler | 4fa96a4 | 2011-11-16 16:39:16 -0600 | [diff] [blame] | 3842 | png_bytep row = png_voidcast(png_bytep, png_malloc(png_ptr, |
| 3843 | png_get_rowbytes(png_ptr, info_ptr))); |
John Bowler | 18c5cfa | 2011-11-16 14:26:34 -0600 | [diff] [blame] | 3844 | |
| 3845 | display->local_row = row; |
| 3846 | result = png_safe_execute(image, png_image_read_background, display); |
| 3847 | display->local_row = NULL; |
| 3848 | png_free(png_ptr, row); |
| 3849 | |
| 3850 | return result; |
| 3851 | } |
| 3852 | |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3853 | else |
| 3854 | { |
| 3855 | png_alloc_size_t row_bytes = display->row_bytes; |
| 3856 | |
| 3857 | while (--passes >= 0) |
| 3858 | { |
| 3859 | png_uint_32 y = image->height; |
| 3860 | png_bytep row = display->first_row; |
Glenn Randers-Pehrson | 864270e | 2012-02-10 17:13:13 -0600 | [diff] [blame] | 3861 | |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3862 | while (y-- > 0) |
| 3863 | { |
| 3864 | png_read_row(png_ptr, row, NULL); |
| 3865 | row += row_bytes; |
| 3866 | } |
| 3867 | } |
| 3868 | |
| 3869 | return 1; |
| 3870 | } |
| 3871 | } |
| 3872 | |
| 3873 | int PNGAPI |
John Bowler | 40ca77a | 2012-01-28 23:19:42 -0600 | [diff] [blame] | 3874 | png_image_finish_read(png_imagep image, png_const_colorp background, |
| 3875 | void *buffer, png_int_32 row_stride, void *colormap) |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3876 | { |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame] | 3877 | if (image != NULL && image->version == PNG_IMAGE_VERSION) |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3878 | { |
| 3879 | png_uint_32 check; |
| 3880 | |
John Bowler | 3706d73 | 2011-11-21 10:28:06 -0600 | [diff] [blame] | 3881 | if (row_stride == 0) |
| 3882 | row_stride = PNG_IMAGE_ROW_STRIDE(*image); |
| 3883 | |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3884 | if (row_stride < 0) |
| 3885 | check = -row_stride; |
| 3886 | |
| 3887 | else |
| 3888 | check = row_stride; |
| 3889 | |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame] | 3890 | if (image->opaque != NULL && buffer != NULL && |
| 3891 | check >= PNG_IMAGE_ROW_STRIDE(*image)) |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3892 | { |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame] | 3893 | if ((image->format & PNG_FORMAT_FLAG_COLORMAP) == 0 || |
| 3894 | (image->colormap_entries > 0 && colormap != NULL)) |
| 3895 | { |
| 3896 | int result; |
| 3897 | png_image_read_control display; |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3898 | |
John Bowler | 5bc9038 | 2012-01-23 22:43:22 -0600 | [diff] [blame] | 3899 | png_memset(&display, 0, sizeof display); |
| 3900 | display.image = image; |
| 3901 | display.buffer = buffer; |
| 3902 | display.row_stride = row_stride; |
| 3903 | display.colormap = colormap; |
| 3904 | display.background = background; |
| 3905 | display.local_row = NULL; |
| 3906 | |
| 3907 | /* Choose the correct 'end' routine; for the color-map case all the |
| 3908 | * setup has already been done. |
| 3909 | */ |
| 3910 | if (image->format & PNG_FORMAT_FLAG_COLORMAP) |
| 3911 | result = |
| 3912 | png_safe_execute(image, png_image_read_colormap, &display) && |
| 3913 | png_safe_execute(image, png_image_read_colormapped, &display); |
| 3914 | |
| 3915 | else |
| 3916 | result = |
| 3917 | png_safe_execute(image, png_image_read_direct, &display); |
| 3918 | |
| 3919 | png_image_free(image); |
| 3920 | return result; |
| 3921 | } |
| 3922 | |
| 3923 | else |
| 3924 | return png_image_error(image, |
| 3925 | "png_image_finish_read[color-map]: no color-map"); |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3926 | } |
| 3927 | |
| 3928 | else |
| 3929 | return png_image_error(image, |
| 3930 | "png_image_finish_read: invalid argument"); |
| 3931 | } |
| 3932 | |
| 3933 | return 0; |
| 3934 | } |
| 3935 | |
| 3936 | #endif /* PNG_SIMPLIFIED_READ_SUPPORTED */ |
Glenn Randers-Pehrson | 9c3ab68 | 2006-02-20 22:09:05 -0600 | [diff] [blame] | 3937 | #endif /* PNG_READ_SUPPORTED */ |