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