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