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