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