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