cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1 | /* |
| 2 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3 | % % |
| 4 | % % |
| 5 | % % |
| 6 | % PPPP N N GGGG % |
| 7 | % P P NN N G % |
| 8 | % PPPP N N N G GG % |
| 9 | % P N NN G G % |
| 10 | % P N N GGG % |
| 11 | % % |
| 12 | % % |
| 13 | % Read/Write Portable Network Graphics Image Format % |
| 14 | % % |
| 15 | % Software Design % |
| 16 | % John Cristy % |
| 17 | % Glenn Randers-Pehrson % |
| 18 | % November 1997 % |
| 19 | % % |
| 20 | % % |
cristy | 45ef08f | 2012-12-07 13:13:34 +0000 | [diff] [blame] | 21 | % Copyright 1999-2013 ImageMagick Studio LLC, a non-profit organization % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 22 | % dedicated to making software imaging solutions freely available. % |
| 23 | % % |
| 24 | % You may not use this file except in compliance with the License. You may % |
| 25 | % obtain a copy of the License at % |
| 26 | % % |
| 27 | % http://www.imagemagick.org/script/license.php % |
| 28 | % % |
| 29 | % Unless required by applicable law or agreed to in writing, software % |
| 30 | % distributed under the License is distributed on an "AS IS" BASIS, % |
| 31 | % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. % |
| 32 | % See the License for the specific language governing permissions and % |
| 33 | % limitations under the License. % |
| 34 | % % |
| 35 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 36 | % |
| 37 | % |
| 38 | */ |
| 39 | |
| 40 | |
| 41 | /* |
| 42 | Include declarations. |
| 43 | */ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 44 | #include "MagickCore/studio.h" |
| 45 | #include "MagickCore/artifact.h" |
| 46 | #include "MagickCore/attribute.h" |
| 47 | #include "MagickCore/blob.h" |
| 48 | #include "MagickCore/blob-private.h" |
| 49 | #include "MagickCore/cache.h" |
cristy | aa2c16c | 2012-03-25 22:21:35 +0000 | [diff] [blame] | 50 | #include "MagickCore/channel.h" |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 51 | #include "MagickCore/color.h" |
| 52 | #include "MagickCore/color-private.h" |
| 53 | #include "MagickCore/colormap.h" |
| 54 | #include "MagickCore/colorspace.h" |
| 55 | #include "MagickCore/colorspace-private.h" |
| 56 | #include "MagickCore/constitute.h" |
| 57 | #include "MagickCore/enhance.h" |
| 58 | #include "MagickCore/exception.h" |
| 59 | #include "MagickCore/exception-private.h" |
| 60 | #include "MagickCore/geometry.h" |
| 61 | #include "MagickCore/histogram.h" |
| 62 | #include "MagickCore/image.h" |
| 63 | #include "MagickCore/image-private.h" |
| 64 | #include "MagickCore/layer.h" |
| 65 | #include "MagickCore/list.h" |
| 66 | #include "MagickCore/log.h" |
| 67 | #include "MagickCore/MagickCore.h" |
| 68 | #include "MagickCore/memory_.h" |
| 69 | #include "MagickCore/module.h" |
| 70 | #include "MagickCore/monitor.h" |
| 71 | #include "MagickCore/monitor-private.h" |
| 72 | #include "MagickCore/option.h" |
| 73 | #include "MagickCore/pixel.h" |
| 74 | #include "MagickCore/pixel-accessor.h" |
| 75 | #include "MagickCore/profile.h" |
| 76 | #include "MagickCore/property.h" |
| 77 | #include "MagickCore/quantum-private.h" |
| 78 | #include "MagickCore/resource_.h" |
| 79 | #include "MagickCore/semaphore.h" |
| 80 | #include "MagickCore/quantum-private.h" |
| 81 | #include "MagickCore/static.h" |
| 82 | #include "MagickCore/statistic.h" |
| 83 | #include "MagickCore/string_.h" |
| 84 | #include "MagickCore/string-private.h" |
| 85 | #include "MagickCore/transform.h" |
| 86 | #include "MagickCore/utility.h" |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 87 | #if defined(MAGICKCORE_PNG_DELEGATE) |
glennrp | 286a635 | 2009-11-09 02:58:50 +0000 | [diff] [blame] | 88 | |
glennrp | 7ef138c | 2009-11-10 13:50:20 +0000 | [diff] [blame] | 89 | /* Suppress libpng pedantic warnings that were added in |
| 90 | * libpng-1.2.41 and libpng-1.4.0. If you are working on |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 91 | * migration to libpng-1.5, remove these defines and then |
glennrp | 7ef138c | 2009-11-10 13:50:20 +0000 | [diff] [blame] | 92 | * fix any code that generates warnings. |
| 93 | */ |
glennrp | 991e92a | 2010-01-28 03:09:00 +0000 | [diff] [blame] | 94 | /* #define PNG_DEPRECATED Use of this function is deprecated */ |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 95 | /* #define PNG_USE_RESULT The result of this function must be checked */ |
| 96 | /* #define PNG_NORETURN This function does not return */ |
| 97 | /* #define PNG_ALLOCATED The result of the function is new memory */ |
glennrp | 8371ecc | 2011-02-19 19:15:38 +0000 | [diff] [blame] | 98 | /* #define PNG_DEPSTRUCT Access to this struct member is deprecated */ |
glennrp | 5b92734 | 2011-04-14 14:31:48 +0000 | [diff] [blame] | 99 | |
| 100 | /* PNG_PTR_NORETURN does not work on some platforms, in libpng-1.5.x */ |
cristy | 75cfe70 | 2011-04-14 14:27:17 +0000 | [diff] [blame] | 101 | #define PNG_PTR_NORETURN |
glennrp | 286a635 | 2009-11-09 02:58:50 +0000 | [diff] [blame] | 102 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 103 | #include "png.h" |
| 104 | #include "zlib.h" |
| 105 | |
| 106 | /* ImageMagick differences */ |
| 107 | #define first_scene scene |
| 108 | |
glennrp | d5045b4 | 2010-03-24 12:40:35 +0000 | [diff] [blame] | 109 | #if PNG_LIBPNG_VER > 10011 |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 110 | /* |
| 111 | Optional declarations. Define or undefine them as you like. |
| 112 | */ |
| 113 | /* #define PNG_DEBUG -- turning this on breaks VisualC compiling */ |
| 114 | |
| 115 | /* |
| 116 | Features under construction. Define these to work on them. |
| 117 | */ |
| 118 | #undef MNG_OBJECT_BUFFERS |
| 119 | #undef MNG_BASI_SUPPORTED |
| 120 | #define MNG_COALESCE_LAYERS /* In 5.4.4, this interfered with MMAP'ed files. */ |
| 121 | #define MNG_INSERT_LAYERS /* Troublesome, but seem to work as of 5.4.4 */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 122 | #if defined(MAGICKCORE_JPEG_DELEGATE) |
| 123 | # define JNG_SUPPORTED /* Not finished as of 5.5.2. See "To do" comments. */ |
| 124 | #endif |
| 125 | #if !defined(RGBColorMatchExact) |
| 126 | #define IsPNGColorEqual(color,target) \ |
glennrp | 8e045c8 | 2011-04-27 16:40:27 +0000 | [diff] [blame] | 127 | (((color).red == (target).red) && \ |
| 128 | ((color).green == (target).green) && \ |
| 129 | ((color).blue == (target).blue)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 130 | #endif |
| 131 | |
glennrp | ecab7d7 | 2013-05-14 22:50:32 +0000 | [diff] [blame] | 132 | /* Table of recognized sRGB ICC profiles */ |
| 133 | struct sRGB_info_struct |
| 134 | { |
| 135 | png_uint_32 len; |
| 136 | png_uint_32 crc; |
| 137 | png_byte intent; |
| 138 | }; |
| 139 | |
| 140 | const struct sRGB_info_struct sRGB_info[] = |
| 141 | { |
glennrp | c241d3c | 2013-05-15 04:24:40 +0000 | [diff] [blame] | 142 | /* ICC v2 perceptual sRGB_IEC61966-2-1_black_scaled.icc */ |
glennrp | ecab7d7 | 2013-05-14 22:50:32 +0000 | [diff] [blame] | 143 | { 3048, 0x3b8772b9UL, 0}, |
| 144 | |
glennrp | c241d3c | 2013-05-15 04:24:40 +0000 | [diff] [blame] | 145 | /* ICC v2 relative sRGB_IEC61966-2-1_no_black_scaling.icc */ |
glennrp | ecab7d7 | 2013-05-14 22:50:32 +0000 | [diff] [blame] | 146 | { 3052, 0x427ebb21UL, 1}, |
| 147 | |
glennrp | c241d3c | 2013-05-15 04:24:40 +0000 | [diff] [blame] | 148 | /* ICC v4 perceptual sRGB_v4_ICC_preference_displayclass.icc */ |
glennrp | ecab7d7 | 2013-05-14 22:50:32 +0000 | [diff] [blame] | 149 | {60988, 0x306fd8aeUL, 0}, |
| 150 | |
glennrp | c241d3c | 2013-05-15 04:24:40 +0000 | [diff] [blame] | 151 | /* ICC v4 perceptual sRGB_v4_ICC_preference.icc perceptual */ |
glennrp | ecab7d7 | 2013-05-14 22:50:32 +0000 | [diff] [blame] | 152 | {60960, 0xbbef7812UL, 0}, |
| 153 | |
glennrp | c241d3c | 2013-05-15 04:24:40 +0000 | [diff] [blame] | 154 | /* HP? sRGB v2 media-relative sRGB_IEC61966-2-1_noBPC.icc */ |
glennrp | ecab7d7 | 2013-05-14 22:50:32 +0000 | [diff] [blame] | 155 | { 3024, 0x5d5129ceUL, 1}, |
| 156 | |
| 157 | /* HP-Microsoft sRGB v2 perceptual */ |
| 158 | { 3144, 0x182ea552UL, 0}, |
| 159 | |
| 160 | /* HP-Microsoft sRGB v2 media-relative */ |
| 161 | { 3144, 0xf29e526dUL, 1}, |
| 162 | |
glennrp | c241d3c | 2013-05-15 04:24:40 +0000 | [diff] [blame] | 163 | /* Facebook's "2012/01/25 03:41:57", 524, "TINYsRGB.icc" */ |
| 164 | { 524, 0xd4938c39UL, 0}, |
| 165 | |
| 166 | /* "2012/11/28 22:35:21", 3212, "Argyll_sRGB.icm") */ |
| 167 | { 3212, 0x034af5a1UL, 0}, |
| 168 | |
glennrp | ecab7d7 | 2013-05-14 22:50:32 +0000 | [diff] [blame] | 169 | /* Not recognized */ |
| 170 | { 0, 0x00000000UL, 0}, |
| 171 | }; |
| 172 | |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 173 | /* Macros for left-bit-replication to ensure that pixels |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 174 | * and PixelInfos all have the same image->depth, and for use |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 175 | * in PNG8 quantization. |
| 176 | */ |
| 177 | |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 178 | /* LBR01: Replicate top bit */ |
| 179 | |
glennrp | 05001c3 | 2011-08-06 13:04:16 +0000 | [diff] [blame] | 180 | #define LBR01PacketRed(pixelpacket) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 181 | (pixelpacket).red=(ScaleQuantumToChar((pixelpacket).red) < 0x10 ? \ |
| 182 | 0 : QuantumRange); |
| 183 | |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 184 | #define LBR01PacketGreen(pixelpacket) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 185 | (pixelpacket).green=(ScaleQuantumToChar((pixelpacket).green) < 0x10 ? \ |
| 186 | 0 : QuantumRange); |
| 187 | |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 188 | #define LBR01PacketBlue(pixelpacket) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 189 | (pixelpacket).blue=(ScaleQuantumToChar((pixelpacket).blue) < 0x10 ? \ |
| 190 | 0 : QuantumRange); |
| 191 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 192 | #define LBR01PacketAlpha(pixelpacket) \ |
| 193 | (pixelpacket).alpha=(ScaleQuantumToChar((pixelpacket).alpha) < 0x10 ? \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 194 | 0 : QuantumRange); |
| 195 | |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 196 | #define LBR01PacketRGB(pixelpacket) \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 197 | { \ |
glennrp | 05001c3 | 2011-08-06 13:04:16 +0000 | [diff] [blame] | 198 | LBR01PacketRed((pixelpacket)); \ |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 199 | LBR01PacketGreen((pixelpacket)); \ |
| 200 | LBR01PacketBlue((pixelpacket)); \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 201 | } |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 202 | |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 203 | #define LBR01PacketRGBO(pixelpacket) \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 204 | { \ |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 205 | LBR01PacketRGB((pixelpacket)); \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 206 | LBR01PacketAlpha((pixelpacket)); \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 207 | } |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 208 | |
cristy | ef61831 | 2011-06-25 12:26:44 +0000 | [diff] [blame] | 209 | #define LBR01PixelRed(pixel) \ |
glennrp | 360c154 | 2013-01-06 01:21:46 +0000 | [diff] [blame] | 210 | (SetPixelRed(image, \ |
| 211 | ScaleQuantumToChar(GetPixelRed(image,(pixel))) < 0x10 ? \ |
glennrp | c7375f9 | 2013-01-06 01:32:16 +0000 | [diff] [blame] | 212 | 0 : QuantumRange,(pixel))); |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 213 | |
glennrp | 54cf797 | 2011-08-06 14:28:09 +0000 | [diff] [blame] | 214 | #define LBR01PixelGreen(pixel) \ |
glennrp | 360c154 | 2013-01-06 01:21:46 +0000 | [diff] [blame] | 215 | (SetPixelGreen(image, \ |
| 216 | ScaleQuantumToChar(GetPixelGreen(image,(pixel))) < 0x10 ? \ |
glennrp | c7375f9 | 2013-01-06 01:32:16 +0000 | [diff] [blame] | 217 | 0 : QuantumRange,(pixel))); |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 218 | |
glennrp | 54cf797 | 2011-08-06 14:28:09 +0000 | [diff] [blame] | 219 | #define LBR01PixelBlue(pixel) \ |
glennrp | 360c154 | 2013-01-06 01:21:46 +0000 | [diff] [blame] | 220 | (SetPixelBlue(image, \ |
| 221 | ScaleQuantumToChar(GetPixelBlue(image,(pixel))) < 0x10 ? \ |
glennrp | c7375f9 | 2013-01-06 01:32:16 +0000 | [diff] [blame] | 222 | 0 : QuantumRange,(pixel))); |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 223 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 224 | #define LBR01PixelAlpha(pixel) \ |
glennrp | 360c154 | 2013-01-06 01:21:46 +0000 | [diff] [blame] | 225 | (SetPixelAlpha(image, \ |
| 226 | ScaleQuantumToChar(GetPixelAlpha(image,(pixel))) < 0x10 ? \ |
glennrp | c7375f9 | 2013-01-06 01:32:16 +0000 | [diff] [blame] | 227 | 0 : QuantumRange,(pixel))); |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 228 | |
glennrp | 54cf797 | 2011-08-06 14:28:09 +0000 | [diff] [blame] | 229 | #define LBR01PixelRGB(pixel) \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 230 | { \ |
cristy | ef61831 | 2011-06-25 12:26:44 +0000 | [diff] [blame] | 231 | LBR01PixelRed((pixel)); \ |
glennrp | 54cf797 | 2011-08-06 14:28:09 +0000 | [diff] [blame] | 232 | LBR01PixelGreen((pixel)); \ |
| 233 | LBR01PixelBlue((pixel)); \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 234 | } |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 235 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 236 | #define LBR01PixelRGBA(pixel) \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 237 | { \ |
glennrp | 54cf797 | 2011-08-06 14:28:09 +0000 | [diff] [blame] | 238 | LBR01PixelRGB((pixel)); \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 239 | LBR01PixelAlpha((pixel)); \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 240 | } |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 241 | |
| 242 | /* LBR02: Replicate top 2 bits */ |
| 243 | |
glennrp | 05001c3 | 2011-08-06 13:04:16 +0000 | [diff] [blame] | 244 | #define LBR02PacketRed(pixelpacket) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 245 | { \ |
| 246 | unsigned char lbr_bits=ScaleQuantumToChar((pixelpacket).red) & 0xc0; \ |
| 247 | (pixelpacket).red=ScaleCharToQuantum( \ |
| 248 | (lbr_bits | (lbr_bits >> 2) | (lbr_bits >> 4) | (lbr_bits >> 6))); \ |
| 249 | } |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 250 | #define LBR02PacketGreen(pixelpacket) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 251 | { \ |
| 252 | unsigned char lbr_bits=ScaleQuantumToChar((pixelpacket).green) & 0xc0; \ |
| 253 | (pixelpacket).green=ScaleCharToQuantum( \ |
| 254 | (lbr_bits | (lbr_bits >> 2) | (lbr_bits >> 4) | (lbr_bits >> 6))); \ |
| 255 | } |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 256 | #define LBR02PacketBlue(pixelpacket) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 257 | { \ |
| 258 | unsigned char lbr_bits=ScaleQuantumToChar((pixelpacket).blue) & 0xc0; \ |
| 259 | (pixelpacket).blue=ScaleCharToQuantum( \ |
| 260 | (lbr_bits | (lbr_bits >> 2) | (lbr_bits >> 4) | (lbr_bits >> 6))); \ |
| 261 | } |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 262 | #define LBR02PacketAlpha(pixelpacket) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 263 | { \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 264 | unsigned char lbr_bits=ScaleQuantumToChar((pixelpacket).alpha) & 0xc0; \ |
| 265 | (pixelpacket).alpha=ScaleCharToQuantum( \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 266 | (lbr_bits | (lbr_bits >> 2) | (lbr_bits >> 4) | (lbr_bits >> 6))); \ |
| 267 | } |
| 268 | |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 269 | #define LBR02PacketRGB(pixelpacket) \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 270 | { \ |
glennrp | 05001c3 | 2011-08-06 13:04:16 +0000 | [diff] [blame] | 271 | LBR02PacketRed((pixelpacket)); \ |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 272 | LBR02PacketGreen((pixelpacket)); \ |
| 273 | LBR02PacketBlue((pixelpacket)); \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 274 | } |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 275 | |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 276 | #define LBR02PacketRGBO(pixelpacket) \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 277 | { \ |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 278 | LBR02PacketRGB((pixelpacket)); \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 279 | LBR02PacketAlpha((pixelpacket)); \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 280 | } |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 281 | |
cristy | ef61831 | 2011-06-25 12:26:44 +0000 | [diff] [blame] | 282 | #define LBR02PixelRed(pixel) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 283 | { \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 284 | unsigned char lbr_bits=ScaleQuantumToChar(GetPixelRed(image,(pixel))) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 285 | & 0xc0; \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 286 | SetPixelRed(image, ScaleCharToQuantum( \ |
| 287 | (lbr_bits | (lbr_bits >> 2) | (lbr_bits >> 4) | (lbr_bits >> 6))), \ |
| 288 | (pixel)); \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 289 | } |
glennrp | 54cf797 | 2011-08-06 14:28:09 +0000 | [diff] [blame] | 290 | #define LBR02PixelGreen(pixel) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 291 | { \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 292 | unsigned char lbr_bits=ScaleQuantumToChar(GetPixelGreen(image,(pixel)))\ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 293 | & 0xc0; \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 294 | SetPixelGreen(image, ScaleCharToQuantum( \ |
| 295 | (lbr_bits | (lbr_bits >> 2) | (lbr_bits >> 4) | (lbr_bits >> 6))), \ |
| 296 | (pixel)); \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 297 | } |
glennrp | 54cf797 | 2011-08-06 14:28:09 +0000 | [diff] [blame] | 298 | #define LBR02PixelBlue(pixel) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 299 | { \ |
| 300 | unsigned char lbr_bits= \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 301 | ScaleQuantumToChar(GetPixelBlue(image,(pixel))) & 0xc0; \ |
| 302 | SetPixelBlue(image, ScaleCharToQuantum( \ |
| 303 | (lbr_bits | (lbr_bits >> 2) | (lbr_bits >> 4) | (lbr_bits >> 6))), \ |
| 304 | (pixel)); \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 305 | } |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 306 | #define LBR02PixelAlpha(pixel) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 307 | { \ |
| 308 | unsigned char lbr_bits= \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 309 | ScaleQuantumToChar(GetPixelAlpha(image,(pixel))) & 0xc0; \ |
| 310 | SetPixelAlpha(image, ScaleCharToQuantum( \ |
| 311 | (lbr_bits | (lbr_bits >> 2) | (lbr_bits >> 4) | (lbr_bits >> 6))), \ |
| 312 | (pixel) ); \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 313 | } |
| 314 | |
glennrp | 54cf797 | 2011-08-06 14:28:09 +0000 | [diff] [blame] | 315 | #define LBR02PixelRGB(pixel) \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 316 | { \ |
cristy | ef61831 | 2011-06-25 12:26:44 +0000 | [diff] [blame] | 317 | LBR02PixelRed((pixel)); \ |
glennrp | 54cf797 | 2011-08-06 14:28:09 +0000 | [diff] [blame] | 318 | LBR02PixelGreen((pixel)); \ |
| 319 | LBR02PixelBlue((pixel)); \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 320 | } |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 321 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 322 | #define LBR02PixelRGBA(pixel) \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 323 | { \ |
glennrp | 54cf797 | 2011-08-06 14:28:09 +0000 | [diff] [blame] | 324 | LBR02PixelRGB((pixel)); \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 325 | LBR02PixelAlpha((pixel)); \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 326 | } |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 327 | |
| 328 | /* LBR03: Replicate top 3 bits (only used with opaque pixels during |
| 329 | PNG8 quantization) */ |
| 330 | |
glennrp | 05001c3 | 2011-08-06 13:04:16 +0000 | [diff] [blame] | 331 | #define LBR03PacketRed(pixelpacket) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 332 | { \ |
| 333 | unsigned char lbr_bits=ScaleQuantumToChar((pixelpacket).red) & 0xe0; \ |
| 334 | (pixelpacket).red=ScaleCharToQuantum( \ |
| 335 | (lbr_bits | (lbr_bits >> 3) | (lbr_bits >> 6))); \ |
| 336 | } |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 337 | #define LBR03PacketGreen(pixelpacket) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 338 | { \ |
| 339 | unsigned char lbr_bits=ScaleQuantumToChar((pixelpacket).green) & 0xe0; \ |
| 340 | (pixelpacket).green=ScaleCharToQuantum( \ |
| 341 | (lbr_bits | (lbr_bits >> 3) | (lbr_bits >> 6))); \ |
| 342 | } |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 343 | #define LBR03PacketBlue(pixelpacket) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 344 | { \ |
| 345 | unsigned char lbr_bits=ScaleQuantumToChar((pixelpacket).blue) & 0xe0; \ |
| 346 | (pixelpacket).blue=ScaleCharToQuantum( \ |
| 347 | (lbr_bits | (lbr_bits >> 3) | (lbr_bits >> 6))); \ |
| 348 | } |
| 349 | |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 350 | #define LBR03PacketRGB(pixelpacket) \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 351 | { \ |
glennrp | 05001c3 | 2011-08-06 13:04:16 +0000 | [diff] [blame] | 352 | LBR03PacketRed((pixelpacket)); \ |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 353 | LBR03PacketGreen((pixelpacket)); \ |
| 354 | LBR03PacketBlue((pixelpacket)); \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 355 | } |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 356 | |
cristy | ef61831 | 2011-06-25 12:26:44 +0000 | [diff] [blame] | 357 | #define LBR03PixelRed(pixel) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 358 | { \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 359 | unsigned char lbr_bits=ScaleQuantumToChar(GetPixelRed(image,(pixel))) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 360 | & 0xe0; \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 361 | SetPixelRed(image, ScaleCharToQuantum( \ |
| 362 | (lbr_bits | (lbr_bits >> 3) | (lbr_bits >> 6))), (pixel)); \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 363 | } |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 364 | #define LBR03Green(pixel) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 365 | { \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 366 | unsigned char lbr_bits=ScaleQuantumToChar(GetPixelGreen(image,(pixel)))\ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 367 | & 0xe0; \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 368 | SetPixelGreen(image, ScaleCharToQuantum( \ |
| 369 | (lbr_bits | (lbr_bits >> 3) | (lbr_bits >> 6))), (pixel)); \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 370 | } |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 371 | #define LBR03Blue(pixel) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 372 | { \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 373 | unsigned char lbr_bits=ScaleQuantumToChar(GetPixelBlue(image,(pixel))) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 374 | & 0xe0; \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 375 | SetPixelBlue(image, ScaleCharToQuantum( \ |
| 376 | (lbr_bits | (lbr_bits >> 3) | (lbr_bits >> 6))), (pixel)); \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 377 | } |
| 378 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 379 | #define LBR03RGB(pixel) \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 380 | { \ |
cristy | ef61831 | 2011-06-25 12:26:44 +0000 | [diff] [blame] | 381 | LBR03PixelRed((pixel)); \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 382 | LBR03Green((pixel)); \ |
| 383 | LBR03Blue((pixel)); \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 384 | } |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 385 | |
| 386 | /* LBR04: Replicate top 4 bits */ |
| 387 | |
glennrp | 05001c3 | 2011-08-06 13:04:16 +0000 | [diff] [blame] | 388 | #define LBR04PacketRed(pixelpacket) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 389 | { \ |
| 390 | unsigned char lbr_bits=ScaleQuantumToChar((pixelpacket).red) & 0xf0; \ |
| 391 | (pixelpacket).red=ScaleCharToQuantum((lbr_bits | (lbr_bits >> 4))); \ |
| 392 | } |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 393 | #define LBR04PacketGreen(pixelpacket) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 394 | { \ |
| 395 | unsigned char lbr_bits=ScaleQuantumToChar((pixelpacket).green) & 0xf0; \ |
| 396 | (pixelpacket).green=ScaleCharToQuantum((lbr_bits | (lbr_bits >> 4))); \ |
| 397 | } |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 398 | #define LBR04PacketBlue(pixelpacket) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 399 | { \ |
| 400 | unsigned char lbr_bits=ScaleQuantumToChar((pixelpacket).blue) & 0xf0; \ |
| 401 | (pixelpacket).blue=ScaleCharToQuantum((lbr_bits | (lbr_bits >> 4))); \ |
| 402 | } |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 403 | #define LBR04PacketAlpha(pixelpacket) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 404 | { \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 405 | unsigned char lbr_bits=ScaleQuantumToChar((pixelpacket).alpha) & 0xf0; \ |
| 406 | (pixelpacket).alpha=ScaleCharToQuantum((lbr_bits | (lbr_bits >> 4))); \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 407 | } |
| 408 | |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 409 | #define LBR04PacketRGB(pixelpacket) \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 410 | { \ |
glennrp | 05001c3 | 2011-08-06 13:04:16 +0000 | [diff] [blame] | 411 | LBR04PacketRed((pixelpacket)); \ |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 412 | LBR04PacketGreen((pixelpacket)); \ |
| 413 | LBR04PacketBlue((pixelpacket)); \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 414 | } |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 415 | |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 416 | #define LBR04PacketRGBO(pixelpacket) \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 417 | { \ |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 418 | LBR04PacketRGB((pixelpacket)); \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 419 | LBR04PacketAlpha((pixelpacket)); \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 420 | } |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 421 | |
cristy | ef61831 | 2011-06-25 12:26:44 +0000 | [diff] [blame] | 422 | #define LBR04PixelRed(pixel) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 423 | { \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 424 | unsigned char lbr_bits=ScaleQuantumToChar(GetPixelRed(image,(pixel))) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 425 | & 0xf0; \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 426 | SetPixelRed(image,\ |
| 427 | ScaleCharToQuantum((lbr_bits | (lbr_bits >> 4))), (pixel)); \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 428 | } |
glennrp | 54cf797 | 2011-08-06 14:28:09 +0000 | [diff] [blame] | 429 | #define LBR04PixelGreen(pixel) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 430 | { \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 431 | unsigned char lbr_bits=ScaleQuantumToChar(GetPixelGreen(image,(pixel)))\ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 432 | & 0xf0; \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 433 | SetPixelGreen(image,\ |
| 434 | ScaleCharToQuantum((lbr_bits | (lbr_bits >> 4))), (pixel)); \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 435 | } |
glennrp | 54cf797 | 2011-08-06 14:28:09 +0000 | [diff] [blame] | 436 | #define LBR04PixelBlue(pixel) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 437 | { \ |
| 438 | unsigned char lbr_bits= \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 439 | ScaleQuantumToChar(GetPixelBlue(image,(pixel))) & 0xf0; \ |
| 440 | SetPixelBlue(image,\ |
| 441 | ScaleCharToQuantum((lbr_bits | (lbr_bits >> 4))), (pixel)); \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 442 | } |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 443 | #define LBR04PixelAlpha(pixel) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 444 | { \ |
| 445 | unsigned char lbr_bits= \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 446 | ScaleQuantumToChar(GetPixelAlpha(image,(pixel))) & 0xf0; \ |
| 447 | SetPixelAlpha(image,\ |
| 448 | ScaleCharToQuantum((lbr_bits | (lbr_bits >> 4))), (pixel)); \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 449 | } |
| 450 | |
glennrp | 54cf797 | 2011-08-06 14:28:09 +0000 | [diff] [blame] | 451 | #define LBR04PixelRGB(pixel) \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 452 | { \ |
cristy | ef61831 | 2011-06-25 12:26:44 +0000 | [diff] [blame] | 453 | LBR04PixelRed((pixel)); \ |
glennrp | 54cf797 | 2011-08-06 14:28:09 +0000 | [diff] [blame] | 454 | LBR04PixelGreen((pixel)); \ |
| 455 | LBR04PixelBlue((pixel)); \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 456 | } |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 457 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 458 | #define LBR04PixelRGBA(pixel) \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 459 | { \ |
glennrp | 54cf797 | 2011-08-06 14:28:09 +0000 | [diff] [blame] | 460 | LBR04PixelRGB((pixel)); \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 461 | LBR04PixelAlpha((pixel)); \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 462 | } |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 463 | |
| 464 | |
glennrp | 1145899 | 2013-01-08 18:12:52 +0000 | [diff] [blame] | 465 | #if MAGICKCORE_QUANTUM_DEPTH > 8 |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 466 | /* LBR08: Replicate top 8 bits */ |
| 467 | |
glennrp | 05001c3 | 2011-08-06 13:04:16 +0000 | [diff] [blame] | 468 | #define LBR08PacketRed(pixelpacket) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 469 | { \ |
| 470 | unsigned char lbr_bits=ScaleQuantumToChar((pixelpacket).red); \ |
| 471 | (pixelpacket).red=ScaleCharToQuantum((lbr_bits)); \ |
| 472 | } |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 473 | #define LBR08PacketGreen(pixelpacket) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 474 | { \ |
| 475 | unsigned char lbr_bits=ScaleQuantumToChar((pixelpacket).green); \ |
| 476 | (pixelpacket).green=ScaleCharToQuantum((lbr_bits)); \ |
| 477 | } |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 478 | #define LBR08PacketBlue(pixelpacket) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 479 | { \ |
| 480 | unsigned char lbr_bits=ScaleQuantumToChar((pixelpacket).blue); \ |
| 481 | (pixelpacket).blue=ScaleCharToQuantum((lbr_bits)); \ |
| 482 | } |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 483 | #define LBR08PacketAlpha(pixelpacket) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 484 | { \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 485 | unsigned char lbr_bits=ScaleQuantumToChar((pixelpacket).alpha); \ |
| 486 | (pixelpacket).alpha=ScaleCharToQuantum((lbr_bits)); \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 487 | } |
| 488 | |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 489 | #define LBR08PacketRGB(pixelpacket) \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 490 | { \ |
glennrp | 05001c3 | 2011-08-06 13:04:16 +0000 | [diff] [blame] | 491 | LBR08PacketRed((pixelpacket)); \ |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 492 | LBR08PacketGreen((pixelpacket)); \ |
| 493 | LBR08PacketBlue((pixelpacket)); \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 494 | } |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 495 | |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 496 | #define LBR08PacketRGBO(pixelpacket) \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 497 | { \ |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 498 | LBR08PacketRGB((pixelpacket)); \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 499 | LBR08PacketAlpha((pixelpacket)); \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 500 | } |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 501 | |
cristy | ef61831 | 2011-06-25 12:26:44 +0000 | [diff] [blame] | 502 | #define LBR08PixelRed(pixel) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 503 | { \ |
| 504 | unsigned char lbr_bits= \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 505 | ScaleQuantumToChar(GetPixelRed(image,(pixel))); \ |
| 506 | SetPixelRed(image,\ |
| 507 | ScaleCharToQuantum((lbr_bits)), (pixel)); \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 508 | } |
glennrp | 54cf797 | 2011-08-06 14:28:09 +0000 | [diff] [blame] | 509 | #define LBR08PixelGreen(pixel) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 510 | { \ |
| 511 | unsigned char lbr_bits= \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 512 | ScaleQuantumToChar(GetPixelGreen(image,(pixel))); \ |
| 513 | SetPixelGreen(image,\ |
| 514 | ScaleCharToQuantum((lbr_bits)), (pixel)); \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 515 | } |
glennrp | 54cf797 | 2011-08-06 14:28:09 +0000 | [diff] [blame] | 516 | #define LBR08PixelBlue(pixel) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 517 | { \ |
| 518 | unsigned char lbr_bits= \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 519 | ScaleQuantumToChar(GetPixelBlue(image,(pixel))); \ |
| 520 | SetPixelBlue(image,\ |
| 521 | ScaleCharToQuantum((lbr_bits)), (pixel)); \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 522 | } |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 523 | #define LBR08PixelAlpha(pixel) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 524 | { \ |
| 525 | unsigned char lbr_bits= \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 526 | ScaleQuantumToChar(GetPixelAlpha(image,(pixel))); \ |
| 527 | SetPixelAlpha(image,\ |
| 528 | ScaleCharToQuantum((lbr_bits)), (pixel)); \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 529 | } |
| 530 | |
glennrp | 54cf797 | 2011-08-06 14:28:09 +0000 | [diff] [blame] | 531 | #define LBR08PixelRGB(pixel) \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 532 | { \ |
cristy | ef61831 | 2011-06-25 12:26:44 +0000 | [diff] [blame] | 533 | LBR08PixelRed((pixel)); \ |
glennrp | 54cf797 | 2011-08-06 14:28:09 +0000 | [diff] [blame] | 534 | LBR08PixelGreen((pixel)); \ |
| 535 | LBR08PixelBlue((pixel)); \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 536 | } |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 537 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 538 | #define LBR08PixelRGBA(pixel) \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 539 | { \ |
glennrp | 54cf797 | 2011-08-06 14:28:09 +0000 | [diff] [blame] | 540 | LBR08PixelRGB((pixel)); \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 541 | LBR08PixelAlpha((pixel)); \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 542 | } |
glennrp | 1145899 | 2013-01-08 18:12:52 +0000 | [diff] [blame] | 543 | #endif /* MAGICKCORE_QUANTUM_DEPTH > 8 */ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 544 | |
| 545 | |
glennrp | 15f0766 | 2013-01-08 18:08:13 +0000 | [diff] [blame] | 546 | #if MAGICKCORE_QUANTUM_DEPTH > 16 |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 547 | /* LBR16: Replicate top 16 bits */ |
| 548 | |
glennrp | 05001c3 | 2011-08-06 13:04:16 +0000 | [diff] [blame] | 549 | #define LBR16PacketRed(pixelpacket) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 550 | { \ |
| 551 | unsigned short lbr_bits=ScaleQuantumToShort((pixelpacket).red); \ |
| 552 | (pixelpacket).red=ScaleShortToQuantum((lbr_bits)); \ |
| 553 | } |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 554 | #define LBR16PacketGreen(pixelpacket) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 555 | { \ |
| 556 | unsigned short lbr_bits=ScaleQuantumToShort((pixelpacket).green); \ |
| 557 | (pixelpacket).green=ScaleShortToQuantum((lbr_bits)); \ |
| 558 | } |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 559 | #define LBR16PacketBlue(pixelpacket) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 560 | { \ |
| 561 | unsigned short lbr_bits=ScaleQuantumToShort((pixelpacket).blue); \ |
| 562 | (pixelpacket).blue=ScaleShortToQuantum((lbr_bits)); \ |
| 563 | } |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 564 | #define LBR16PacketAlpha(pixelpacket) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 565 | { \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 566 | unsigned short lbr_bits=ScaleQuantumToShort((pixelpacket).alpha); \ |
| 567 | (pixelpacket).alpha=ScaleShortToQuantum((lbr_bits)); \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 568 | } |
| 569 | |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 570 | #define LBR16PacketRGB(pixelpacket) \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 571 | { \ |
glennrp | 05001c3 | 2011-08-06 13:04:16 +0000 | [diff] [blame] | 572 | LBR16PacketRed((pixelpacket)); \ |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 573 | LBR16PacketGreen((pixelpacket)); \ |
| 574 | LBR16PacketBlue((pixelpacket)); \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 575 | } |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 576 | |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 577 | #define LBR16PacketRGBO(pixelpacket) \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 578 | { \ |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 579 | LBR16PacketRGB((pixelpacket)); \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 580 | LBR16PacketAlpha((pixelpacket)); \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 581 | } |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 582 | |
cristy | ef61831 | 2011-06-25 12:26:44 +0000 | [diff] [blame] | 583 | #define LBR16PixelRed(pixel) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 584 | { \ |
| 585 | unsigned short lbr_bits= \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 586 | ScaleQuantumToShort(GetPixelRed(image,(pixel))); \ |
| 587 | SetPixelRed(image,\ |
| 588 | ScaleShortToQuantum((lbr_bits)),(pixel)); \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 589 | } |
glennrp | 54cf797 | 2011-08-06 14:28:09 +0000 | [diff] [blame] | 590 | #define LBR16PixelGreen(pixel) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 591 | { \ |
| 592 | unsigned short lbr_bits= \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 593 | ScaleQuantumToShort(GetPixelGreen(image,(pixel))); \ |
| 594 | SetPixelGreen(image,\ |
| 595 | ScaleShortToQuantum((lbr_bits)),(pixel)); \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 596 | } |
glennrp | 54cf797 | 2011-08-06 14:28:09 +0000 | [diff] [blame] | 597 | #define LBR16PixelBlue(pixel) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 598 | { \ |
| 599 | unsigned short lbr_bits= \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 600 | ScaleQuantumToShort(GetPixelBlue(image,(pixel))); \ |
| 601 | SetPixelBlue(image,\ |
| 602 | ScaleShortToQuantum((lbr_bits)),(pixel)); \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 603 | } |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 604 | #define LBR16PixelAlpha(pixel) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 605 | { \ |
| 606 | unsigned short lbr_bits= \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 607 | ScaleQuantumToShort(GetPixelAlpha(image,(pixel))); \ |
| 608 | SetPixelAlpha(image,\ |
| 609 | ScaleShortToQuantum((lbr_bits)),(pixel)); \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 610 | } |
| 611 | |
glennrp | 54cf797 | 2011-08-06 14:28:09 +0000 | [diff] [blame] | 612 | #define LBR16PixelRGB(pixel) \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 613 | { \ |
cristy | ef61831 | 2011-06-25 12:26:44 +0000 | [diff] [blame] | 614 | LBR16PixelRed((pixel)); \ |
glennrp | 54cf797 | 2011-08-06 14:28:09 +0000 | [diff] [blame] | 615 | LBR16PixelGreen((pixel)); \ |
| 616 | LBR16PixelBlue((pixel)); \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 617 | } |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 618 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 619 | #define LBR16PixelRGBA(pixel) \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 620 | { \ |
glennrp | 54cf797 | 2011-08-06 14:28:09 +0000 | [diff] [blame] | 621 | LBR16PixelRGB((pixel)); \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 622 | LBR16PixelAlpha((pixel)); \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 623 | } |
glennrp | 15f0766 | 2013-01-08 18:08:13 +0000 | [diff] [blame] | 624 | #endif /* MAGICKCORE_QUANTUM_DEPTH > 16 */ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 625 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 626 | /* |
| 627 | Establish thread safety. |
| 628 | setjmp/longjmp is claimed to be safe on these platforms: |
| 629 | setjmp/longjmp is alleged to be unsafe on these platforms: |
| 630 | */ |
| 631 | #ifndef SETJMP_IS_THREAD_SAFE |
| 632 | #define PNG_SETJMP_NOT_THREAD_SAFE |
| 633 | #endif |
| 634 | |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 635 | #ifdef PNG_SETJMP_NOT_THREAD_SAFE |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 636 | static SemaphoreInfo |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 637 | *ping_semaphore = (SemaphoreInfo *) NULL; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 638 | #endif |
| 639 | |
| 640 | /* |
| 641 | This temporary until I set up malloc'ed object attributes array. |
| 642 | Recompile with MNG_MAX_OBJECTS=65536L to avoid this limit but |
| 643 | waste more memory. |
| 644 | */ |
| 645 | #define MNG_MAX_OBJECTS 256 |
| 646 | |
| 647 | /* |
| 648 | If this not defined, spec is interpreted strictly. If it is |
| 649 | defined, an attempt will be made to recover from some errors, |
| 650 | including |
| 651 | o global PLTE too short |
| 652 | */ |
| 653 | #undef MNG_LOOSE |
| 654 | |
| 655 | /* |
| 656 | Don't try to define PNG_MNG_FEATURES_SUPPORTED here. Make sure |
| 657 | it's defined in libpng/pngconf.h, version 1.0.9 or later. It won't work |
| 658 | with earlier versions of libpng. From libpng-1.0.3a to libpng-1.0.8, |
| 659 | PNG_READ|WRITE_EMPTY_PLTE were used but those have been deprecated in |
| 660 | libpng in favor of PNG_MNG_FEATURES_SUPPORTED, so we set them here. |
| 661 | PNG_MNG_FEATURES_SUPPORTED is disabled by default in libpng-1.0.9 and |
| 662 | will be enabled by default in libpng-1.2.0. |
| 663 | */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 664 | #ifdef PNG_MNG_FEATURES_SUPPORTED |
| 665 | # ifndef PNG_READ_EMPTY_PLTE_SUPPORTED |
| 666 | # define PNG_READ_EMPTY_PLTE_SUPPORTED |
| 667 | # endif |
| 668 | # ifndef PNG_WRITE_EMPTY_PLTE_SUPPORTED |
| 669 | # define PNG_WRITE_EMPTY_PLTE_SUPPORTED |
| 670 | # endif |
| 671 | #endif |
| 672 | |
| 673 | /* |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 674 | Maximum valid size_t in PNG/MNG chunks is (2^31)-1 |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 675 | This macro is only defined in libpng-1.0.3 and later. |
| 676 | Previously it was PNG_MAX_UINT but that was deprecated in libpng-1.2.6 |
| 677 | */ |
| 678 | #ifndef PNG_UINT_31_MAX |
| 679 | #define PNG_UINT_31_MAX (png_uint_32) 0x7fffffffL |
| 680 | #endif |
| 681 | |
| 682 | /* |
| 683 | Constant strings for known chunk types. If you need to add a chunk, |
| 684 | add a string holding the name here. To make the code more |
| 685 | portable, we use ASCII numbers like this, not characters. |
| 686 | */ |
| 687 | |
glennrp | 85dcf87 | 2011-12-07 02:51:47 +0000 | [diff] [blame] | 688 | static png_byte mng_MHDR[5]={ 77, 72, 68, 82, (png_byte) '\0'}; |
| 689 | static png_byte mng_BACK[5]={ 66, 65, 67, 75, (png_byte) '\0'}; |
| 690 | static png_byte mng_BASI[5]={ 66, 65, 83, 73, (png_byte) '\0'}; |
| 691 | static png_byte mng_CLIP[5]={ 67, 76, 73, 80, (png_byte) '\0'}; |
| 692 | static png_byte mng_CLON[5]={ 67, 76, 79, 78, (png_byte) '\0'}; |
| 693 | static png_byte mng_DEFI[5]={ 68, 69, 70, 73, (png_byte) '\0'}; |
| 694 | static png_byte mng_DHDR[5]={ 68, 72, 68, 82, (png_byte) '\0'}; |
| 695 | static png_byte mng_DISC[5]={ 68, 73, 83, 67, (png_byte) '\0'}; |
| 696 | static png_byte mng_ENDL[5]={ 69, 78, 68, 76, (png_byte) '\0'}; |
| 697 | static png_byte mng_FRAM[5]={ 70, 82, 65, 77, (png_byte) '\0'}; |
| 698 | static png_byte mng_IEND[5]={ 73, 69, 78, 68, (png_byte) '\0'}; |
| 699 | static png_byte mng_IHDR[5]={ 73, 72, 68, 82, (png_byte) '\0'}; |
| 700 | static png_byte mng_JHDR[5]={ 74, 72, 68, 82, (png_byte) '\0'}; |
| 701 | static png_byte mng_LOOP[5]={ 76, 79, 79, 80, (png_byte) '\0'}; |
| 702 | static png_byte mng_MAGN[5]={ 77, 65, 71, 78, (png_byte) '\0'}; |
| 703 | static png_byte mng_MEND[5]={ 77, 69, 78, 68, (png_byte) '\0'}; |
| 704 | static png_byte mng_MOVE[5]={ 77, 79, 86, 69, (png_byte) '\0'}; |
| 705 | static png_byte mng_PAST[5]={ 80, 65, 83, 84, (png_byte) '\0'}; |
| 706 | static png_byte mng_PLTE[5]={ 80, 76, 84, 69, (png_byte) '\0'}; |
| 707 | static png_byte mng_SAVE[5]={ 83, 65, 86, 69, (png_byte) '\0'}; |
| 708 | static png_byte mng_SEEK[5]={ 83, 69, 69, 75, (png_byte) '\0'}; |
| 709 | static png_byte mng_SHOW[5]={ 83, 72, 79, 87, (png_byte) '\0'}; |
| 710 | static png_byte mng_TERM[5]={ 84, 69, 82, 77, (png_byte) '\0'}; |
| 711 | static png_byte mng_bKGD[5]={ 98, 75, 71, 68, (png_byte) '\0'}; |
| 712 | static png_byte mng_cHRM[5]={ 99, 72, 82, 77, (png_byte) '\0'}; |
| 713 | static png_byte mng_gAMA[5]={103, 65, 77, 65, (png_byte) '\0'}; |
| 714 | static png_byte mng_iCCP[5]={105, 67, 67, 80, (png_byte) '\0'}; |
| 715 | static png_byte mng_nEED[5]={110, 69, 69, 68, (png_byte) '\0'}; |
| 716 | static png_byte mng_pHYg[5]={112, 72, 89, 103, (png_byte) '\0'}; |
| 717 | static png_byte mng_vpAg[5]={118, 112, 65, 103, (png_byte) '\0'}; |
| 718 | static png_byte mng_pHYs[5]={112, 72, 89, 115, (png_byte) '\0'}; |
| 719 | static png_byte mng_sBIT[5]={115, 66, 73, 84, (png_byte) '\0'}; |
| 720 | static png_byte mng_sRGB[5]={115, 82, 71, 66, (png_byte) '\0'}; |
| 721 | static png_byte mng_tRNS[5]={116, 82, 78, 83, (png_byte) '\0'}; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 722 | |
| 723 | #if defined(JNG_SUPPORTED) |
glennrp | 85dcf87 | 2011-12-07 02:51:47 +0000 | [diff] [blame] | 724 | static png_byte mng_IDAT[5]={ 73, 68, 65, 84, (png_byte) '\0'}; |
| 725 | static png_byte mng_JDAT[5]={ 74, 68, 65, 84, (png_byte) '\0'}; |
| 726 | static png_byte mng_JDAA[5]={ 74, 68, 65, 65, (png_byte) '\0'}; |
| 727 | static png_byte mng_JdAA[5]={ 74, 100, 65, 65, (png_byte) '\0'}; |
| 728 | static png_byte mng_JSEP[5]={ 74, 83, 69, 80, (png_byte) '\0'}; |
| 729 | static png_byte mng_oFFs[5]={111, 70, 70, 115, (png_byte) '\0'}; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 730 | #endif |
| 731 | |
| 732 | /* |
| 733 | Other known chunks that are not yet supported by ImageMagick: |
glennrp | 85dcf87 | 2011-12-07 02:51:47 +0000 | [diff] [blame] | 734 | static png_byte mng_hIST[5]={104, 73, 83, 84, (png_byte) '\0'}; |
| 735 | static png_byte mng_iCCP[5]={105, 67, 67, 80, (png_byte) '\0'}; |
| 736 | static png_byte mng_iTXt[5]={105, 84, 88, 116, (png_byte) '\0'}; |
| 737 | static png_byte mng_sPLT[5]={115, 80, 76, 84, (png_byte) '\0'}; |
| 738 | static png_byte mng_sTER[5]={115, 84, 69, 82, (png_byte) '\0'}; |
| 739 | static png_byte mng_tEXt[5]={116, 69, 88, 116, (png_byte) '\0'}; |
| 740 | static png_byte mng_tIME[5]={116, 73, 77, 69, (png_byte) '\0'}; |
| 741 | static png_byte mng_zTXt[5]={122, 84, 88, 116, (png_byte) '\0'}; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 742 | */ |
| 743 | |
| 744 | typedef struct _MngBox |
| 745 | { |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 746 | long |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 747 | left, |
| 748 | right, |
| 749 | top, |
| 750 | bottom; |
| 751 | } MngBox; |
| 752 | |
| 753 | typedef struct _MngPair |
| 754 | { |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 755 | volatile long |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 756 | a, |
| 757 | b; |
| 758 | } MngPair; |
| 759 | |
| 760 | #ifdef MNG_OBJECT_BUFFERS |
| 761 | typedef struct _MngBuffer |
| 762 | { |
| 763 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 764 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 765 | height, |
| 766 | width; |
| 767 | |
| 768 | Image |
| 769 | *image; |
| 770 | |
| 771 | png_color |
| 772 | plte[256]; |
| 773 | |
| 774 | int |
| 775 | reference_count; |
| 776 | |
| 777 | unsigned char |
| 778 | alpha_sample_depth, |
| 779 | compression_method, |
| 780 | color_type, |
| 781 | concrete, |
| 782 | filter_method, |
| 783 | frozen, |
| 784 | image_type, |
| 785 | interlace_method, |
| 786 | pixel_sample_depth, |
| 787 | plte_length, |
| 788 | sample_depth, |
| 789 | viewable; |
| 790 | } MngBuffer; |
| 791 | #endif |
| 792 | |
| 793 | typedef struct _MngInfo |
| 794 | { |
| 795 | |
| 796 | #ifdef MNG_OBJECT_BUFFERS |
| 797 | MngBuffer |
| 798 | *ob[MNG_MAX_OBJECTS]; |
| 799 | #endif |
| 800 | |
| 801 | Image * |
| 802 | image; |
| 803 | |
| 804 | RectangleInfo |
| 805 | page; |
| 806 | |
| 807 | int |
| 808 | adjoin, |
| 809 | #ifndef PNG_READ_EMPTY_PLTE_SUPPORTED |
| 810 | bytes_in_read_buffer, |
| 811 | found_empty_plte, |
| 812 | #endif |
| 813 | equal_backgrounds, |
| 814 | equal_chrms, |
| 815 | equal_gammas, |
| 816 | #if defined(PNG_WRITE_EMPTY_PLTE_SUPPORTED) || \ |
| 817 | defined(PNG_MNG_FEATURES_SUPPORTED) |
| 818 | equal_palettes, |
| 819 | #endif |
| 820 | equal_physs, |
| 821 | equal_srgbs, |
| 822 | framing_mode, |
| 823 | have_global_bkgd, |
| 824 | have_global_chrm, |
| 825 | have_global_gama, |
| 826 | have_global_phys, |
| 827 | have_global_sbit, |
| 828 | have_global_srgb, |
| 829 | have_saved_bkgd_index, |
| 830 | have_write_global_chrm, |
| 831 | have_write_global_gama, |
| 832 | have_write_global_plte, |
| 833 | have_write_global_srgb, |
| 834 | need_fram, |
| 835 | object_id, |
| 836 | old_framing_mode, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 837 | saved_bkgd_index; |
| 838 | |
| 839 | int |
| 840 | new_number_colors; |
| 841 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 842 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 843 | image_found, |
| 844 | loop_count[256], |
| 845 | loop_iteration[256], |
| 846 | scenes_found, |
| 847 | x_off[MNG_MAX_OBJECTS], |
| 848 | y_off[MNG_MAX_OBJECTS]; |
| 849 | |
| 850 | MngBox |
| 851 | clip, |
| 852 | frame, |
| 853 | image_box, |
| 854 | object_clip[MNG_MAX_OBJECTS]; |
| 855 | |
| 856 | unsigned char |
| 857 | /* These flags could be combined into one byte */ |
| 858 | exists[MNG_MAX_OBJECTS], |
| 859 | frozen[MNG_MAX_OBJECTS], |
| 860 | loop_active[256], |
| 861 | invisible[MNG_MAX_OBJECTS], |
| 862 | viewable[MNG_MAX_OBJECTS]; |
| 863 | |
| 864 | MagickOffsetType |
| 865 | loop_jump[256]; |
| 866 | |
| 867 | png_colorp |
| 868 | global_plte; |
| 869 | |
| 870 | png_color_8 |
| 871 | global_sbit; |
| 872 | |
| 873 | png_byte |
| 874 | #ifndef PNG_READ_EMPTY_PLTE_SUPPORTED |
| 875 | read_buffer[8], |
| 876 | #endif |
| 877 | global_trns[256]; |
| 878 | |
| 879 | float |
| 880 | global_gamma; |
| 881 | |
| 882 | ChromaticityInfo |
| 883 | global_chrm; |
| 884 | |
| 885 | RenderingIntent |
| 886 | global_srgb_intent; |
| 887 | |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 888 | unsigned int |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 889 | delay, |
| 890 | global_plte_length, |
| 891 | global_trns_length, |
| 892 | global_x_pixels_per_unit, |
| 893 | global_y_pixels_per_unit, |
| 894 | mng_width, |
| 895 | mng_height, |
| 896 | ticks_per_second; |
| 897 | |
glennrp | b9cfe27 | 2010-12-21 15:08:06 +0000 | [diff] [blame] | 898 | MagickBooleanType |
| 899 | need_blob; |
| 900 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 901 | unsigned int |
| 902 | IsPalette, |
| 903 | global_phys_unit_type, |
| 904 | basi_warning, |
| 905 | clon_warning, |
| 906 | dhdr_warning, |
| 907 | jhdr_warning, |
| 908 | magn_warning, |
| 909 | past_warning, |
| 910 | phyg_warning, |
| 911 | phys_warning, |
| 912 | sbit_warning, |
| 913 | show_warning, |
| 914 | mng_type, |
| 915 | write_mng, |
| 916 | write_png_colortype, |
| 917 | write_png_depth, |
glennrp | 1868258 | 2011-06-30 18:11:47 +0000 | [diff] [blame] | 918 | write_png_compression_level, |
| 919 | write_png_compression_strategy, |
| 920 | write_png_compression_filter, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 921 | write_png8, |
| 922 | write_png24, |
glennrp | fd164d2 | 2013-01-26 21:10:22 +0000 | [diff] [blame] | 923 | write_png32, |
| 924 | write_png48, |
| 925 | write_png64; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 926 | |
| 927 | #ifdef MNG_BASI_SUPPORTED |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 928 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 929 | basi_width, |
| 930 | basi_height; |
| 931 | |
| 932 | unsigned int |
| 933 | basi_depth, |
| 934 | basi_color_type, |
| 935 | basi_compression_method, |
| 936 | basi_filter_type, |
| 937 | basi_interlace_method, |
| 938 | basi_red, |
| 939 | basi_green, |
| 940 | basi_blue, |
| 941 | basi_alpha, |
| 942 | basi_viewable; |
| 943 | #endif |
| 944 | |
| 945 | png_uint_16 |
| 946 | magn_first, |
| 947 | magn_last, |
| 948 | magn_mb, |
| 949 | magn_ml, |
| 950 | magn_mr, |
| 951 | magn_mt, |
| 952 | magn_mx, |
| 953 | magn_my, |
| 954 | magn_methx, |
| 955 | magn_methy; |
| 956 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 957 | PixelInfo |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 958 | mng_global_bkgd; |
| 959 | |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 960 | /* Added at version 6.6.6-7 */ |
| 961 | MagickBooleanType |
| 962 | ping_exclude_bKGD, |
| 963 | ping_exclude_cHRM, |
glennrp | a0ed009 | 2011-04-18 16:36:29 +0000 | [diff] [blame] | 964 | ping_exclude_date, |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 965 | ping_exclude_EXIF, |
| 966 | ping_exclude_gAMA, |
| 967 | ping_exclude_iCCP, |
| 968 | /* ping_exclude_iTXt, */ |
| 969 | ping_exclude_oFFs, |
| 970 | ping_exclude_pHYs, |
| 971 | ping_exclude_sRGB, |
| 972 | ping_exclude_tEXt, |
glennrp | a1e3b7b | 2010-12-24 16:37:33 +0000 | [diff] [blame] | 973 | ping_exclude_tRNS, |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 974 | ping_exclude_vpAg, |
| 975 | ping_exclude_zCCP, /* hex-encoded iCCP */ |
glennrp | 8d3d6e5 | 2011-04-19 04:39:51 +0000 | [diff] [blame] | 976 | ping_exclude_zTXt, |
glennrp | ecab7d7 | 2013-05-14 22:50:32 +0000 | [diff] [blame] | 977 | ping_preserve_colormap, |
| 978 | /* Added at version 6.8.5-7 */ |
| 979 | ping_preserve_iCCP; |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 980 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 981 | } MngInfo; |
| 982 | #endif /* VER */ |
| 983 | |
| 984 | /* |
| 985 | Forward declarations. |
| 986 | */ |
| 987 | static MagickBooleanType |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 988 | WritePNGImage(const ImageInfo *,Image *,ExceptionInfo *); |
glennrp | 0c3e06b | 2010-11-19 13:45:02 +0000 | [diff] [blame] | 989 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 990 | static MagickBooleanType |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 991 | WriteMNGImage(const ImageInfo *,Image *,ExceptionInfo *); |
glennrp | 0c3e06b | 2010-11-19 13:45:02 +0000 | [diff] [blame] | 992 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 993 | #if defined(JNG_SUPPORTED) |
| 994 | static MagickBooleanType |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 995 | WriteJNGImage(const ImageInfo *,Image *,ExceptionInfo *); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 996 | #endif |
| 997 | |
glennrp | 0c3e06b | 2010-11-19 13:45:02 +0000 | [diff] [blame] | 998 | #if PNG_LIBPNG_VER > 10011 |
| 999 | |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 1000 | |
glennrp | 0c3e06b | 2010-11-19 13:45:02 +0000 | [diff] [blame] | 1001 | #if (MAGICKCORE_QUANTUM_DEPTH >= 16) |
| 1002 | static MagickBooleanType |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 1003 | LosslessReduceDepthOK(Image *image,ExceptionInfo *exception) |
glennrp | 0c3e06b | 2010-11-19 13:45:02 +0000 | [diff] [blame] | 1004 | { |
glennrp | 67b9c1a | 2011-04-22 18:47:36 +0000 | [diff] [blame] | 1005 | /* Reduce bit depth if it can be reduced losslessly from 16+ to 8. |
| 1006 | * |
| 1007 | * This is true if the high byte and the next highest byte of |
| 1008 | * each sample of the image, the colormap, and the background color |
glennrp | 3faa9a3 | 2011-04-23 14:00:25 +0000 | [diff] [blame] | 1009 | * are equal to each other. We check this by seeing if the samples |
| 1010 | * are unchanged when we scale them down to 8 and back up to Quantum. |
glennrp | 67b9c1a | 2011-04-22 18:47:36 +0000 | [diff] [blame] | 1011 | * |
| 1012 | * We don't use the method GetImageDepth() because it doesn't check |
glennrp | 3faa9a3 | 2011-04-23 14:00:25 +0000 | [diff] [blame] | 1013 | * background and doesn't handle PseudoClass specially. |
glennrp | 67b9c1a | 2011-04-22 18:47:36 +0000 | [diff] [blame] | 1014 | */ |
| 1015 | |
glennrp | 3faa9a3 | 2011-04-23 14:00:25 +0000 | [diff] [blame] | 1016 | #define QuantumToCharToQuantumEqQuantum(quantum) \ |
| 1017 | ((ScaleCharToQuantum((unsigned char) ScaleQuantumToChar(quantum))) == quantum) |
| 1018 | |
glennrp | 0c3e06b | 2010-11-19 13:45:02 +0000 | [diff] [blame] | 1019 | MagickBooleanType |
| 1020 | ok_to_reduce=MagickFalse; |
glennrp | 0c3e06b | 2010-11-19 13:45:02 +0000 | [diff] [blame] | 1021 | |
glennrp | 03e11f6 | 2011-04-22 13:30:16 +0000 | [diff] [blame] | 1022 | if (image->depth >= 16) |
glennrp | 0c3e06b | 2010-11-19 13:45:02 +0000 | [diff] [blame] | 1023 | { |
| 1024 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 1025 | const Quantum |
glennrp | 0c3e06b | 2010-11-19 13:45:02 +0000 | [diff] [blame] | 1026 | *p; |
| 1027 | |
| 1028 | ok_to_reduce= |
glennrp | 3faa9a3 | 2011-04-23 14:00:25 +0000 | [diff] [blame] | 1029 | QuantumToCharToQuantumEqQuantum(image->background_color.red) && |
| 1030 | QuantumToCharToQuantumEqQuantum(image->background_color.green) && |
| 1031 | QuantumToCharToQuantumEqQuantum(image->background_color.blue) ? |
| 1032 | MagickTrue : MagickFalse; |
glennrp | 0c3e06b | 2010-11-19 13:45:02 +0000 | [diff] [blame] | 1033 | |
| 1034 | if (ok_to_reduce != MagickFalse && image->storage_class == PseudoClass) |
| 1035 | { |
| 1036 | int indx; |
| 1037 | |
| 1038 | for (indx=0; indx < (ssize_t) image->colors; indx++) |
| 1039 | { |
glennrp | 3faa9a3 | 2011-04-23 14:00:25 +0000 | [diff] [blame] | 1040 | ok_to_reduce=( |
| 1041 | QuantumToCharToQuantumEqQuantum( |
| 1042 | image->colormap[indx].red) && |
| 1043 | QuantumToCharToQuantumEqQuantum( |
| 1044 | image->colormap[indx].green) && |
| 1045 | QuantumToCharToQuantumEqQuantum( |
| 1046 | image->colormap[indx].blue)) ? |
| 1047 | MagickTrue : MagickFalse; |
| 1048 | |
glennrp | 0c3e06b | 2010-11-19 13:45:02 +0000 | [diff] [blame] | 1049 | if (ok_to_reduce == MagickFalse) |
glennrp | 3faa9a3 | 2011-04-23 14:00:25 +0000 | [diff] [blame] | 1050 | break; |
glennrp | 0c3e06b | 2010-11-19 13:45:02 +0000 | [diff] [blame] | 1051 | } |
| 1052 | } |
| 1053 | |
| 1054 | if ((ok_to_reduce != MagickFalse) && |
| 1055 | (image->storage_class != PseudoClass)) |
| 1056 | { |
| 1057 | ssize_t |
| 1058 | y; |
| 1059 | |
| 1060 | register ssize_t |
| 1061 | x; |
| 1062 | |
| 1063 | for (y=0; y < (ssize_t) image->rows; y++) |
| 1064 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 1065 | p=GetVirtualPixels(image,0,y,image->columns,1,exception); |
glennrp | 0c3e06b | 2010-11-19 13:45:02 +0000 | [diff] [blame] | 1066 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 1067 | if (p == (const Quantum *) NULL) |
glennrp | 0c3e06b | 2010-11-19 13:45:02 +0000 | [diff] [blame] | 1068 | { |
| 1069 | ok_to_reduce = MagickFalse; |
| 1070 | break; |
| 1071 | } |
| 1072 | |
| 1073 | for (x=(ssize_t) image->columns-1; x >= 0; x--) |
| 1074 | { |
glennrp | 3faa9a3 | 2011-04-23 14:00:25 +0000 | [diff] [blame] | 1075 | ok_to_reduce= |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 1076 | QuantumToCharToQuantumEqQuantum(GetPixelRed(image,p)) && |
| 1077 | QuantumToCharToQuantumEqQuantum(GetPixelGreen(image,p)) && |
| 1078 | QuantumToCharToQuantumEqQuantum(GetPixelBlue(image,p)) ? |
glennrp | 3faa9a3 | 2011-04-23 14:00:25 +0000 | [diff] [blame] | 1079 | MagickTrue : MagickFalse; |
glennrp | 0c3e06b | 2010-11-19 13:45:02 +0000 | [diff] [blame] | 1080 | |
| 1081 | if (ok_to_reduce == MagickFalse) |
| 1082 | break; |
| 1083 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 1084 | p+=GetPixelChannels(image); |
glennrp | 0c3e06b | 2010-11-19 13:45:02 +0000 | [diff] [blame] | 1085 | } |
glennrp | 8640fb5 | 2010-11-23 15:48:26 +0000 | [diff] [blame] | 1086 | if (x >= 0) |
glennrp | 0c3e06b | 2010-11-19 13:45:02 +0000 | [diff] [blame] | 1087 | break; |
| 1088 | } |
| 1089 | } |
| 1090 | |
| 1091 | if (ok_to_reduce != MagickFalse) |
| 1092 | { |
glennrp | 0c3e06b | 2010-11-19 13:45:02 +0000 | [diff] [blame] | 1093 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 1094 | " OK to reduce PNG bit depth to 8 without loss of info"); |
glennrp | 0c3e06b | 2010-11-19 13:45:02 +0000 | [diff] [blame] | 1095 | } |
glennrp | a6a0663 | 2011-01-19 15:15:34 +0000 | [diff] [blame] | 1096 | else |
| 1097 | { |
| 1098 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 1099 | " Not OK to reduce PNG bit depth to 8 without loss of info"); |
glennrp | a6a0663 | 2011-01-19 15:15:34 +0000 | [diff] [blame] | 1100 | } |
glennrp | 0c3e06b | 2010-11-19 13:45:02 +0000 | [diff] [blame] | 1101 | } |
| 1102 | |
| 1103 | return ok_to_reduce; |
| 1104 | } |
| 1105 | #endif /* MAGICKCORE_QUANTUM_DEPTH >= 16 */ |
| 1106 | |
glennrp | 1a56e9c | 2012-04-25 17:06:57 +0000 | [diff] [blame] | 1107 | static const char* PngColorTypeToString(const unsigned int color_type) |
| 1108 | { |
| 1109 | const char |
| 1110 | *result = "Unknown"; |
| 1111 | |
| 1112 | switch (color_type) |
| 1113 | { |
| 1114 | case PNG_COLOR_TYPE_GRAY: |
| 1115 | result = "Gray"; |
| 1116 | break; |
| 1117 | case PNG_COLOR_TYPE_GRAY_ALPHA: |
| 1118 | result = "Gray+Alpha"; |
| 1119 | break; |
| 1120 | case PNG_COLOR_TYPE_PALETTE: |
| 1121 | result = "Palette"; |
| 1122 | break; |
| 1123 | case PNG_COLOR_TYPE_RGB: |
| 1124 | result = "RGB"; |
| 1125 | break; |
| 1126 | case PNG_COLOR_TYPE_RGB_ALPHA: |
| 1127 | result = "RGB+Alpha"; |
| 1128 | break; |
| 1129 | } |
| 1130 | |
| 1131 | return result; |
| 1132 | } |
| 1133 | |
glennrp | e610a07 | 2010-08-05 17:08:46 +0000 | [diff] [blame] | 1134 | static int |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 1135 | Magick_RenderingIntent_to_PNG_RenderingIntent(const RenderingIntent intent) |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1136 | { |
glennrp | e610a07 | 2010-08-05 17:08:46 +0000 | [diff] [blame] | 1137 | switch (intent) |
| 1138 | { |
| 1139 | case PerceptualIntent: |
| 1140 | return 0; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1141 | |
glennrp | e610a07 | 2010-08-05 17:08:46 +0000 | [diff] [blame] | 1142 | case RelativeIntent: |
| 1143 | return 1; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1144 | |
glennrp | e610a07 | 2010-08-05 17:08:46 +0000 | [diff] [blame] | 1145 | case SaturationIntent: |
| 1146 | return 2; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1147 | |
glennrp | e610a07 | 2010-08-05 17:08:46 +0000 | [diff] [blame] | 1148 | case AbsoluteIntent: |
| 1149 | return 3; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1150 | |
glennrp | e610a07 | 2010-08-05 17:08:46 +0000 | [diff] [blame] | 1151 | default: |
| 1152 | return -1; |
| 1153 | } |
| 1154 | } |
| 1155 | |
| 1156 | static RenderingIntent |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 1157 | Magick_RenderingIntent_from_PNG_RenderingIntent(const int ping_intent) |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1158 | { |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 1159 | switch (ping_intent) |
glennrp | e610a07 | 2010-08-05 17:08:46 +0000 | [diff] [blame] | 1160 | { |
| 1161 | case 0: |
| 1162 | return PerceptualIntent; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1163 | |
glennrp | e610a07 | 2010-08-05 17:08:46 +0000 | [diff] [blame] | 1164 | case 1: |
| 1165 | return RelativeIntent; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1166 | |
glennrp | e610a07 | 2010-08-05 17:08:46 +0000 | [diff] [blame] | 1167 | case 2: |
| 1168 | return SaturationIntent; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1169 | |
glennrp | e610a07 | 2010-08-05 17:08:46 +0000 | [diff] [blame] | 1170 | case 3: |
| 1171 | return AbsoluteIntent; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1172 | |
glennrp | e610a07 | 2010-08-05 17:08:46 +0000 | [diff] [blame] | 1173 | default: |
| 1174 | return UndefinedIntent; |
| 1175 | } |
| 1176 | } |
| 1177 | |
cristy | 9d8c122 | 2012-08-10 12:34:19 +0000 | [diff] [blame] | 1178 | static const char * |
glennrp | 98b83d4 | 2012-07-23 02:50:31 +0000 | [diff] [blame] | 1179 | Magick_RenderingIntentString_from_PNG_RenderingIntent(const int ping_intent) |
| 1180 | { |
| 1181 | switch (ping_intent) |
| 1182 | { |
| 1183 | case 0: |
| 1184 | return "Perceptual Intent"; |
| 1185 | |
| 1186 | case 1: |
| 1187 | return "Relative Intent"; |
| 1188 | |
| 1189 | case 2: |
| 1190 | return "Saturation Intent"; |
| 1191 | |
| 1192 | case 3: |
| 1193 | return "Absolute Intent"; |
| 1194 | |
| 1195 | default: |
| 1196 | return "Undefined Intent"; |
| 1197 | } |
| 1198 | } |
| 1199 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1200 | static inline ssize_t MagickMax(const ssize_t x,const ssize_t y) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1201 | { |
| 1202 | if (x > y) |
| 1203 | return(x); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1204 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1205 | return(y); |
| 1206 | } |
glennrp | 0c3e06b | 2010-11-19 13:45:02 +0000 | [diff] [blame] | 1207 | |
cristy | d9ecd04 | 2012-06-17 18:26:12 +0000 | [diff] [blame] | 1208 | static const char * |
glennrp | 5dff435 | 2012-06-06 22:12:04 +0000 | [diff] [blame] | 1209 | Magick_ColorType_from_PNG_ColorType(const int ping_colortype) |
| 1210 | { |
| 1211 | switch (ping_colortype) |
| 1212 | { |
| 1213 | case 0: |
| 1214 | return "Grayscale"; |
| 1215 | |
| 1216 | case 2: |
| 1217 | return "Truecolor"; |
| 1218 | |
| 1219 | case 3: |
| 1220 | return "Indexed"; |
| 1221 | |
| 1222 | case 4: |
| 1223 | return "GrayAlpha"; |
| 1224 | |
| 1225 | case 6: |
| 1226 | return "RGBA"; |
| 1227 | |
| 1228 | default: |
| 1229 | return "UndefinedColorType"; |
| 1230 | } |
| 1231 | } |
| 1232 | |
| 1233 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1234 | static inline ssize_t MagickMin(const ssize_t x,const ssize_t y) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1235 | { |
| 1236 | if (x < y) |
| 1237 | return(x); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1238 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1239 | return(y); |
| 1240 | } |
glennrp | d5045b4 | 2010-03-24 12:40:35 +0000 | [diff] [blame] | 1241 | #endif /* PNG_LIBPNG_VER > 10011 */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1242 | #endif /* MAGICKCORE_PNG_DELEGATE */ |
| 1243 | |
| 1244 | /* |
| 1245 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1246 | % % |
| 1247 | % % |
| 1248 | % % |
| 1249 | % I s M N G % |
| 1250 | % % |
| 1251 | % % |
| 1252 | % % |
| 1253 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1254 | % |
| 1255 | % IsMNG() returns MagickTrue if the image format type, identified by the |
| 1256 | % magick string, is MNG. |
| 1257 | % |
| 1258 | % The format of the IsMNG method is: |
| 1259 | % |
| 1260 | % MagickBooleanType IsMNG(const unsigned char *magick,const size_t length) |
| 1261 | % |
| 1262 | % A description of each parameter follows: |
| 1263 | % |
| 1264 | % o magick: compare image format pattern against these bytes. |
| 1265 | % |
| 1266 | % o length: Specifies the length of the magick string. |
| 1267 | % |
| 1268 | % |
| 1269 | */ |
| 1270 | static MagickBooleanType IsMNG(const unsigned char *magick,const size_t length) |
| 1271 | { |
| 1272 | if (length < 8) |
| 1273 | return(MagickFalse); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1274 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1275 | if (memcmp(magick,"\212MNG\r\n\032\n",8) == 0) |
| 1276 | return(MagickTrue); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1277 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1278 | return(MagickFalse); |
| 1279 | } |
| 1280 | |
| 1281 | /* |
| 1282 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1283 | % % |
| 1284 | % % |
| 1285 | % % |
| 1286 | % I s J N G % |
| 1287 | % % |
| 1288 | % % |
| 1289 | % % |
| 1290 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1291 | % |
| 1292 | % IsJNG() returns MagickTrue if the image format type, identified by the |
| 1293 | % magick string, is JNG. |
| 1294 | % |
| 1295 | % The format of the IsJNG method is: |
| 1296 | % |
| 1297 | % MagickBooleanType IsJNG(const unsigned char *magick,const size_t length) |
| 1298 | % |
| 1299 | % A description of each parameter follows: |
| 1300 | % |
| 1301 | % o magick: compare image format pattern against these bytes. |
| 1302 | % |
| 1303 | % o length: Specifies the length of the magick string. |
| 1304 | % |
| 1305 | % |
| 1306 | */ |
| 1307 | static MagickBooleanType IsJNG(const unsigned char *magick,const size_t length) |
| 1308 | { |
| 1309 | if (length < 8) |
| 1310 | return(MagickFalse); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1311 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1312 | if (memcmp(magick,"\213JNG\r\n\032\n",8) == 0) |
| 1313 | return(MagickTrue); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1314 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1315 | return(MagickFalse); |
| 1316 | } |
| 1317 | |
| 1318 | /* |
| 1319 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1320 | % % |
| 1321 | % % |
| 1322 | % % |
| 1323 | % I s P N G % |
| 1324 | % % |
| 1325 | % % |
| 1326 | % % |
| 1327 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1328 | % |
| 1329 | % IsPNG() returns MagickTrue if the image format type, identified by the |
| 1330 | % magick string, is PNG. |
| 1331 | % |
| 1332 | % The format of the IsPNG method is: |
| 1333 | % |
| 1334 | % MagickBooleanType IsPNG(const unsigned char *magick,const size_t length) |
| 1335 | % |
| 1336 | % A description of each parameter follows: |
| 1337 | % |
| 1338 | % o magick: compare image format pattern against these bytes. |
| 1339 | % |
| 1340 | % o length: Specifies the length of the magick string. |
| 1341 | % |
| 1342 | */ |
| 1343 | static MagickBooleanType IsPNG(const unsigned char *magick,const size_t length) |
| 1344 | { |
| 1345 | if (length < 8) |
| 1346 | return(MagickFalse); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1347 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1348 | if (memcmp(magick,"\211PNG\r\n\032\n",8) == 0) |
| 1349 | return(MagickTrue); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1350 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1351 | return(MagickFalse); |
| 1352 | } |
| 1353 | |
| 1354 | #if defined(MAGICKCORE_PNG_DELEGATE) |
| 1355 | #if defined(__cplusplus) || defined(c_plusplus) |
| 1356 | extern "C" { |
| 1357 | #endif |
| 1358 | |
glennrp | d5045b4 | 2010-03-24 12:40:35 +0000 | [diff] [blame] | 1359 | #if (PNG_LIBPNG_VER > 10011) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1360 | static size_t WriteBlobMSBULong(Image *image,const size_t value) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1361 | { |
| 1362 | unsigned char |
| 1363 | buffer[4]; |
| 1364 | |
| 1365 | assert(image != (Image *) NULL); |
| 1366 | assert(image->signature == MagickSignature); |
| 1367 | buffer[0]=(unsigned char) (value >> 24); |
| 1368 | buffer[1]=(unsigned char) (value >> 16); |
| 1369 | buffer[2]=(unsigned char) (value >> 8); |
| 1370 | buffer[3]=(unsigned char) value; |
| 1371 | return((size_t) WriteBlob(image,4,buffer)); |
| 1372 | } |
| 1373 | |
| 1374 | static void PNGLong(png_bytep p,png_uint_32 value) |
| 1375 | { |
| 1376 | *p++=(png_byte) ((value >> 24) & 0xff); |
| 1377 | *p++=(png_byte) ((value >> 16) & 0xff); |
| 1378 | *p++=(png_byte) ((value >> 8) & 0xff); |
| 1379 | *p++=(png_byte) (value & 0xff); |
| 1380 | } |
| 1381 | |
glennrp | a521b2f | 2010-10-29 04:11:03 +0000 | [diff] [blame] | 1382 | #if defined(JNG_SUPPORTED) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1383 | static void PNGsLong(png_bytep p,png_int_32 value) |
| 1384 | { |
| 1385 | *p++=(png_byte) ((value >> 24) & 0xff); |
| 1386 | *p++=(png_byte) ((value >> 16) & 0xff); |
| 1387 | *p++=(png_byte) ((value >> 8) & 0xff); |
| 1388 | *p++=(png_byte) (value & 0xff); |
| 1389 | } |
glennrp | a521b2f | 2010-10-29 04:11:03 +0000 | [diff] [blame] | 1390 | #endif |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1391 | |
| 1392 | static void PNGShort(png_bytep p,png_uint_16 value) |
| 1393 | { |
| 1394 | *p++=(png_byte) ((value >> 8) & 0xff); |
| 1395 | *p++=(png_byte) (value & 0xff); |
| 1396 | } |
| 1397 | |
| 1398 | static void PNGType(png_bytep p,png_bytep type) |
| 1399 | { |
| 1400 | (void) CopyMagickMemory(p,type,4*sizeof(png_byte)); |
| 1401 | } |
| 1402 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 1403 | static void LogPNGChunk(MagickBooleanType logging, png_bytep type, |
| 1404 | size_t length) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1405 | { |
| 1406 | if (logging != MagickFalse) |
| 1407 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 1408 | " Writing %c%c%c%c chunk, length: %.20g", |
| 1409 | type[0],type[1],type[2],type[3],(double) length); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1410 | } |
glennrp | d5045b4 | 2010-03-24 12:40:35 +0000 | [diff] [blame] | 1411 | #endif /* PNG_LIBPNG_VER > 10011 */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1412 | |
| 1413 | #if defined(__cplusplus) || defined(c_plusplus) |
| 1414 | } |
| 1415 | #endif |
| 1416 | |
glennrp | d5045b4 | 2010-03-24 12:40:35 +0000 | [diff] [blame] | 1417 | #if PNG_LIBPNG_VER > 10011 |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1418 | /* |
| 1419 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1420 | % % |
| 1421 | % % |
| 1422 | % % |
| 1423 | % R e a d P N G I m a g e % |
| 1424 | % % |
| 1425 | % % |
| 1426 | % % |
| 1427 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1428 | % |
| 1429 | % ReadPNGImage() reads a Portable Network Graphics (PNG) or |
| 1430 | % Multiple-image Network Graphics (MNG) image file and returns it. It |
| 1431 | % allocates the memory necessary for the new Image structure and returns a |
| 1432 | % pointer to the new image or set of images. |
| 1433 | % |
| 1434 | % MNG support written by Glenn Randers-Pehrson, glennrp@image... |
| 1435 | % |
| 1436 | % The format of the ReadPNGImage method is: |
| 1437 | % |
| 1438 | % Image *ReadPNGImage(const ImageInfo *image_info,ExceptionInfo *exception) |
| 1439 | % |
| 1440 | % A description of each parameter follows: |
| 1441 | % |
| 1442 | % o image_info: the image info. |
| 1443 | % |
| 1444 | % o exception: return any errors or warnings in this structure. |
| 1445 | % |
| 1446 | % To do, more or less in chronological order (as of version 5.5.2, |
| 1447 | % November 26, 2002 -- glennrp -- see also "To do" under WriteMNGImage): |
| 1448 | % |
| 1449 | % Get 16-bit cheap transparency working. |
| 1450 | % |
| 1451 | % (At this point, PNG decoding is supposed to be in full MNG-LC compliance) |
| 1452 | % |
| 1453 | % Preserve all unknown and not-yet-handled known chunks found in input |
| 1454 | % PNG file and copy them into output PNG files according to the PNG |
| 1455 | % copying rules. |
| 1456 | % |
| 1457 | % (At this point, PNG encoding should be in full MNG compliance) |
| 1458 | % |
| 1459 | % Provide options for choice of background to use when the MNG BACK |
| 1460 | % chunk is not present or is not mandatory (i.e., leave transparent, |
| 1461 | % user specified, MNG BACK, PNG bKGD) |
| 1462 | % |
| 1463 | % Implement LOOP/ENDL [done, but could do discretionary loops more |
| 1464 | % efficiently by linking in the duplicate frames.]. |
| 1465 | % |
| 1466 | % Decode and act on the MHDR simplicity profile (offer option to reject |
| 1467 | % files or attempt to process them anyway when the profile isn't LC or VLC). |
| 1468 | % |
| 1469 | % Upgrade to full MNG without Delta-PNG. |
| 1470 | % |
| 1471 | % o BACK [done a while ago except for background image ID] |
| 1472 | % o MOVE [done 15 May 1999] |
| 1473 | % o CLIP [done 15 May 1999] |
| 1474 | % o DISC [done 19 May 1999] |
| 1475 | % o SAVE [partially done 19 May 1999 (marks objects frozen)] |
| 1476 | % o SEEK [partially done 19 May 1999 (discard function only)] |
| 1477 | % o SHOW |
| 1478 | % o PAST |
| 1479 | % o BASI |
| 1480 | % o MNG-level tEXt/iTXt/zTXt |
| 1481 | % o pHYg |
| 1482 | % o pHYs |
| 1483 | % o sBIT |
| 1484 | % o bKGD |
| 1485 | % o iTXt (wait for libpng implementation). |
| 1486 | % |
| 1487 | % Use the scene signature to discover when an identical scene is |
| 1488 | % being reused, and just point to the original image->exception instead |
| 1489 | % of storing another set of pixels. This not specific to MNG |
| 1490 | % but could be applied generally. |
| 1491 | % |
| 1492 | % Upgrade to full MNG with Delta-PNG. |
| 1493 | % |
| 1494 | % JNG tEXt/iTXt/zTXt |
| 1495 | % |
| 1496 | % We will not attempt to read files containing the CgBI chunk. |
| 1497 | % They are really Xcode files meant for display on the iPhone. |
| 1498 | % These are not valid PNG files and it is impossible to recover |
glennrp | f54e295 | 2011-06-29 14:20:44 +0000 | [diff] [blame] | 1499 | % the original PNG from files that have been converted to Xcode-PNG, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1500 | % since irretrievable loss of color data has occurred due to the |
| 1501 | % use of premultiplied alpha. |
| 1502 | */ |
| 1503 | |
| 1504 | #if defined(__cplusplus) || defined(c_plusplus) |
| 1505 | extern "C" { |
| 1506 | #endif |
| 1507 | |
| 1508 | /* |
| 1509 | This the function that does the actual reading of data. It is |
| 1510 | the same as the one supplied in libpng, except that it receives the |
| 1511 | datastream from the ReadBlob() function instead of standard input. |
| 1512 | */ |
| 1513 | static void png_get_data(png_structp png_ptr,png_bytep data,png_size_t length) |
| 1514 | { |
| 1515 | Image |
| 1516 | *image; |
| 1517 | |
| 1518 | image=(Image *) png_get_io_ptr(png_ptr); |
| 1519 | if (length) |
| 1520 | { |
| 1521 | png_size_t |
| 1522 | check; |
| 1523 | |
| 1524 | check=(png_size_t) ReadBlob(image,(size_t) length,data); |
| 1525 | if (check != length) |
| 1526 | { |
| 1527 | char |
| 1528 | msg[MaxTextExtent]; |
| 1529 | |
cristy | 3b6fd2e | 2011-05-20 12:53:50 +0000 | [diff] [blame] | 1530 | (void) FormatLocaleString(msg,MaxTextExtent, |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 1531 | "Expected %.20g bytes; found %.20g bytes",(double) length, |
| 1532 | (double) check); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1533 | png_warning(png_ptr,msg); |
| 1534 | png_error(png_ptr,"Read Exception"); |
| 1535 | } |
| 1536 | } |
| 1537 | } |
| 1538 | |
| 1539 | #if !defined(PNG_READ_EMPTY_PLTE_SUPPORTED) && \ |
| 1540 | !defined(PNG_MNG_FEATURES_SUPPORTED) |
| 1541 | /* We use mng_get_data() instead of png_get_data() if we have a libpng |
| 1542 | * older than libpng-1.0.3a, which was the first to allow the empty |
| 1543 | * PLTE, or a newer libpng in which PNG_MNG_FEATURES_SUPPORTED was |
| 1544 | * ifdef'ed out. Earlier versions would crash if the bKGD chunk was |
| 1545 | * encountered after an empty PLTE, so we have to look ahead for bKGD |
| 1546 | * chunks and remove them from the datastream that is passed to libpng, |
| 1547 | * and store their contents for later use. |
| 1548 | */ |
| 1549 | static void mng_get_data(png_structp png_ptr,png_bytep data,png_size_t length) |
| 1550 | { |
| 1551 | MngInfo |
| 1552 | *mng_info; |
| 1553 | |
| 1554 | Image |
| 1555 | *image; |
| 1556 | |
| 1557 | png_size_t |
| 1558 | check; |
| 1559 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1560 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1561 | i; |
| 1562 | |
| 1563 | i=0; |
| 1564 | mng_info=(MngInfo *) png_get_io_ptr(png_ptr); |
| 1565 | image=(Image *) mng_info->image; |
| 1566 | while (mng_info->bytes_in_read_buffer && length) |
| 1567 | { |
| 1568 | data[i]=mng_info->read_buffer[i]; |
| 1569 | mng_info->bytes_in_read_buffer--; |
| 1570 | length--; |
| 1571 | i++; |
| 1572 | } |
| 1573 | if (length) |
| 1574 | { |
| 1575 | check=(png_size_t) ReadBlob(image,(size_t) length,(char *) data); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1576 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1577 | if (check != length) |
| 1578 | png_error(png_ptr,"Read Exception"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1579 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1580 | if (length == 4) |
| 1581 | { |
| 1582 | if ((data[0] == 0) && (data[1] == 0) && (data[2] == 0) && |
| 1583 | (data[3] == 0)) |
| 1584 | { |
| 1585 | check=(png_size_t) ReadBlob(image,(size_t) length, |
| 1586 | (char *) mng_info->read_buffer); |
| 1587 | mng_info->read_buffer[4]=0; |
| 1588 | mng_info->bytes_in_read_buffer=4; |
| 1589 | if (memcmp(mng_info->read_buffer,mng_PLTE,4) == 0) |
| 1590 | mng_info->found_empty_plte=MagickTrue; |
| 1591 | if (memcmp(mng_info->read_buffer,mng_IEND,4) == 0) |
| 1592 | { |
| 1593 | mng_info->found_empty_plte=MagickFalse; |
| 1594 | mng_info->have_saved_bkgd_index=MagickFalse; |
| 1595 | } |
| 1596 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1597 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1598 | if ((data[0] == 0) && (data[1] == 0) && (data[2] == 0) && |
| 1599 | (data[3] == 1)) |
| 1600 | { |
| 1601 | check=(png_size_t) ReadBlob(image,(size_t) length, |
| 1602 | (char *) mng_info->read_buffer); |
| 1603 | mng_info->read_buffer[4]=0; |
| 1604 | mng_info->bytes_in_read_buffer=4; |
| 1605 | if (memcmp(mng_info->read_buffer,mng_bKGD,4) == 0) |
| 1606 | if (mng_info->found_empty_plte) |
| 1607 | { |
| 1608 | /* |
| 1609 | Skip the bKGD data byte and CRC. |
| 1610 | */ |
| 1611 | check=(png_size_t) |
| 1612 | ReadBlob(image,5,(char *) mng_info->read_buffer); |
| 1613 | check=(png_size_t) ReadBlob(image,(size_t) length, |
| 1614 | (char *) mng_info->read_buffer); |
| 1615 | mng_info->saved_bkgd_index=mng_info->read_buffer[0]; |
| 1616 | mng_info->have_saved_bkgd_index=MagickTrue; |
| 1617 | mng_info->bytes_in_read_buffer=0; |
| 1618 | } |
| 1619 | } |
| 1620 | } |
| 1621 | } |
| 1622 | } |
| 1623 | #endif |
| 1624 | |
| 1625 | static void png_put_data(png_structp png_ptr,png_bytep data,png_size_t length) |
| 1626 | { |
| 1627 | Image |
| 1628 | *image; |
| 1629 | |
| 1630 | image=(Image *) png_get_io_ptr(png_ptr); |
| 1631 | if (length) |
| 1632 | { |
| 1633 | png_size_t |
| 1634 | check; |
| 1635 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1636 | check=(png_size_t) WriteBlob(image,(size_t) length,data); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1637 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1638 | if (check != length) |
| 1639 | png_error(png_ptr,"WriteBlob Failed"); |
| 1640 | } |
| 1641 | } |
| 1642 | |
| 1643 | static void png_flush_data(png_structp png_ptr) |
| 1644 | { |
| 1645 | (void) png_ptr; |
| 1646 | } |
| 1647 | |
| 1648 | #ifdef PNG_WRITE_EMPTY_PLTE_SUPPORTED |
| 1649 | static int PalettesAreEqual(Image *a,Image *b) |
| 1650 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1651 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1652 | i; |
| 1653 | |
| 1654 | if ((a == (Image *) NULL) || (b == (Image *) NULL)) |
| 1655 | return((int) MagickFalse); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1656 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1657 | if (a->storage_class != PseudoClass || b->storage_class != PseudoClass) |
| 1658 | return((int) MagickFalse); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1659 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1660 | if (a->colors != b->colors) |
| 1661 | return((int) MagickFalse); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1662 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1663 | for (i=0; i < (ssize_t) a->colors; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1664 | { |
| 1665 | if ((a->colormap[i].red != b->colormap[i].red) || |
| 1666 | (a->colormap[i].green != b->colormap[i].green) || |
| 1667 | (a->colormap[i].blue != b->colormap[i].blue)) |
| 1668 | return((int) MagickFalse); |
| 1669 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1670 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1671 | return((int) MagickTrue); |
| 1672 | } |
| 1673 | #endif |
| 1674 | |
| 1675 | static void MngInfoDiscardObject(MngInfo *mng_info,int i) |
| 1676 | { |
| 1677 | if (i && (i < MNG_MAX_OBJECTS) && (mng_info != (MngInfo *) NULL) && |
| 1678 | mng_info->exists[i] && !mng_info->frozen[i]) |
| 1679 | { |
| 1680 | #ifdef MNG_OBJECT_BUFFERS |
| 1681 | if (mng_info->ob[i] != (MngBuffer *) NULL) |
| 1682 | { |
| 1683 | if (mng_info->ob[i]->reference_count > 0) |
| 1684 | mng_info->ob[i]->reference_count--; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1685 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1686 | if (mng_info->ob[i]->reference_count == 0) |
| 1687 | { |
| 1688 | if (mng_info->ob[i]->image != (Image *) NULL) |
| 1689 | mng_info->ob[i]->image=DestroyImage(mng_info->ob[i]->image); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1690 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1691 | mng_info->ob[i]=DestroyString(mng_info->ob[i]); |
| 1692 | } |
| 1693 | } |
| 1694 | mng_info->ob[i]=(MngBuffer *) NULL; |
| 1695 | #endif |
| 1696 | mng_info->exists[i]=MagickFalse; |
| 1697 | mng_info->invisible[i]=MagickFalse; |
| 1698 | mng_info->viewable[i]=MagickFalse; |
| 1699 | mng_info->frozen[i]=MagickFalse; |
| 1700 | mng_info->x_off[i]=0; |
| 1701 | mng_info->y_off[i]=0; |
| 1702 | mng_info->object_clip[i].left=0; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1703 | mng_info->object_clip[i].right=(ssize_t) PNG_UINT_31_MAX; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1704 | mng_info->object_clip[i].top=0; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1705 | mng_info->object_clip[i].bottom=(ssize_t) PNG_UINT_31_MAX; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1706 | } |
| 1707 | } |
| 1708 | |
glennrp | 21f0e62 | 2011-01-07 16:20:57 +0000 | [diff] [blame] | 1709 | static void MngInfoFreeStruct(MngInfo *mng_info, |
| 1710 | MagickBooleanType *have_mng_structure) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1711 | { |
glennrp | 21f0e62 | 2011-01-07 16:20:57 +0000 | [diff] [blame] | 1712 | if (*have_mng_structure != MagickFalse && (mng_info != (MngInfo *) NULL)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1713 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1714 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1715 | i; |
| 1716 | |
| 1717 | for (i=1; i < MNG_MAX_OBJECTS; i++) |
| 1718 | MngInfoDiscardObject(mng_info,i); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1719 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1720 | if (mng_info->global_plte != (png_colorp) NULL) |
| 1721 | mng_info->global_plte=(png_colorp) |
| 1722 | RelinquishMagickMemory(mng_info->global_plte); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1723 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1724 | mng_info=(MngInfo *) RelinquishMagickMemory(mng_info); |
| 1725 | *have_mng_structure=MagickFalse; |
| 1726 | } |
| 1727 | } |
| 1728 | |
| 1729 | static MngBox mng_minimum_box(MngBox box1,MngBox box2) |
| 1730 | { |
| 1731 | MngBox |
| 1732 | box; |
| 1733 | |
| 1734 | box=box1; |
| 1735 | if (box.left < box2.left) |
| 1736 | box.left=box2.left; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1737 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1738 | if (box.top < box2.top) |
| 1739 | box.top=box2.top; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1740 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1741 | if (box.right > box2.right) |
| 1742 | box.right=box2.right; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1743 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1744 | if (box.bottom > box2.bottom) |
| 1745 | box.bottom=box2.bottom; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1746 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1747 | return box; |
| 1748 | } |
| 1749 | |
| 1750 | static MngBox mng_read_box(MngBox previous_box,char delta_type,unsigned char *p) |
| 1751 | { |
| 1752 | MngBox |
| 1753 | box; |
| 1754 | |
| 1755 | /* |
| 1756 | Read clipping boundaries from DEFI, CLIP, FRAM, or PAST chunk. |
| 1757 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1758 | box.left=(ssize_t) ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]); |
| 1759 | box.right=(ssize_t) ((p[4] << 24) | (p[5] << 16) | (p[6] << 8) | p[7]); |
| 1760 | box.top=(ssize_t) ((p[8] << 24) | (p[9] << 16) | (p[10] << 8) | p[11]); |
| 1761 | box.bottom=(ssize_t) ((p[12] << 24) | (p[13] << 16) | (p[14] << 8) | p[15]); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1762 | if (delta_type != 0) |
| 1763 | { |
| 1764 | box.left+=previous_box.left; |
| 1765 | box.right+=previous_box.right; |
| 1766 | box.top+=previous_box.top; |
| 1767 | box.bottom+=previous_box.bottom; |
| 1768 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1769 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1770 | return(box); |
| 1771 | } |
| 1772 | |
| 1773 | static MngPair mng_read_pair(MngPair previous_pair,int delta_type, |
| 1774 | unsigned char *p) |
| 1775 | { |
| 1776 | MngPair |
| 1777 | pair; |
| 1778 | /* |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1779 | Read two ssize_ts from CLON, MOVE or PAST chunk |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1780 | */ |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 1781 | pair.a=(long) ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]); |
| 1782 | pair.b=(long) ((p[4] << 24) | (p[5] << 16) | (p[6] << 8) | p[7]); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1783 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1784 | if (delta_type != 0) |
| 1785 | { |
| 1786 | pair.a+=previous_pair.a; |
| 1787 | pair.b+=previous_pair.b; |
| 1788 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1789 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1790 | return(pair); |
| 1791 | } |
| 1792 | |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 1793 | static long mng_get_long(unsigned char *p) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1794 | { |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 1795 | return((long) ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3])); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1796 | } |
| 1797 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 1798 | typedef struct _PNGErrorInfo |
cristy | c82a27b | 2011-10-21 01:07:16 +0000 | [diff] [blame] | 1799 | { |
cristy | c82a27b | 2011-10-21 01:07:16 +0000 | [diff] [blame] | 1800 | Image |
| 1801 | *image; |
| 1802 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 1803 | ExceptionInfo |
| 1804 | *exception; |
| 1805 | } PNGErrorInfo; |
cristy | c82a27b | 2011-10-21 01:07:16 +0000 | [diff] [blame] | 1806 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 1807 | static void MagickPNGErrorHandler(png_struct *ping,png_const_charp message) |
| 1808 | { |
| 1809 | ExceptionInfo |
| 1810 | *exception; |
cristy | c82a27b | 2011-10-21 01:07:16 +0000 | [diff] [blame] | 1811 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 1812 | Image |
| 1813 | *image; |
| 1814 | |
| 1815 | PNGErrorInfo |
| 1816 | *error_info; |
| 1817 | |
| 1818 | error_info=(PNGErrorInfo *) png_get_error_ptr(ping); |
| 1819 | image=error_info->image; |
| 1820 | exception=error_info->exception; |
| 1821 | |
| 1822 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 1823 | " libpng-%s error: %s", PNG_LIBPNG_VER_STRING,message); |
| 1824 | |
| 1825 | (void) ThrowMagickException(exception,GetMagickModule(),CoderError,message, |
| 1826 | "`%s'",image->filename); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1827 | |
glennrp | e4017e3 | 2011-01-08 17:16:09 +0000 | [diff] [blame] | 1828 | #if (PNG_LIBPNG_VER < 10500) |
glennrp | 8371ecc | 2011-02-19 19:15:38 +0000 | [diff] [blame] | 1829 | /* A warning about deprecated use of jmpbuf here is unavoidable if you |
| 1830 | * are building with libpng-1.4.x and can be ignored. |
| 1831 | */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1832 | longjmp(ping->jmpbuf,1); |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 1833 | #else |
| 1834 | png_longjmp(ping,1); |
| 1835 | #endif |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1836 | } |
| 1837 | |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 1838 | static void MagickPNGWarningHandler(png_struct *ping,png_const_charp message) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1839 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 1840 | ExceptionInfo |
| 1841 | *exception; |
| 1842 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1843 | Image |
| 1844 | *image; |
| 1845 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 1846 | PNGErrorInfo |
| 1847 | *error_info; |
| 1848 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1849 | if (LocaleCompare(message, "Missing PLTE before tRNS") == 0) |
| 1850 | png_error(ping, message); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1851 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 1852 | error_info=(PNGErrorInfo *) png_get_error_ptr(ping); |
| 1853 | image=error_info->image; |
| 1854 | exception=error_info->exception; |
| 1855 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 1856 | " libpng-%s warning: %s", PNG_LIBPNG_VER_STRING,message); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1857 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 1858 | (void) ThrowMagickException(exception,GetMagickModule(),CoderWarning, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1859 | message,"`%s'",image->filename); |
| 1860 | } |
| 1861 | |
| 1862 | #ifdef PNG_USER_MEM_SUPPORTED |
glennrp | 943b7d3 | 2013-04-21 00:40:38 +0000 | [diff] [blame] | 1863 | #if PNG_LIBPNG_VER >= 10400 |
cristy | a865ccd | 2012-07-28 00:33:10 +0000 | [diff] [blame] | 1864 | static png_voidp Magick_png_malloc(png_structp png_ptr,png_alloc_size_t size) |
| 1865 | #else |
| 1866 | static png_voidp Magick_png_malloc(png_structp png_ptr,png_size_t size) |
| 1867 | #endif |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1868 | { |
cristy | df0d90e | 2011-12-12 01:03:55 +0000 | [diff] [blame] | 1869 | (void) png_ptr; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1870 | return((png_voidp) AcquireMagickMemory((size_t) size)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1871 | } |
| 1872 | |
| 1873 | /* |
| 1874 | Free a pointer. It is removed from the list at the same time. |
| 1875 | */ |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 1876 | static png_free_ptr Magick_png_free(png_structp png_ptr,png_voidp ptr) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1877 | { |
glennrp | 3bd393f | 2011-12-21 18:54:53 +0000 | [diff] [blame] | 1878 | (void) png_ptr; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1879 | ptr=RelinquishMagickMemory(ptr); |
| 1880 | return((png_free_ptr) NULL); |
| 1881 | } |
| 1882 | #endif |
| 1883 | |
| 1884 | #if defined(__cplusplus) || defined(c_plusplus) |
| 1885 | } |
| 1886 | #endif |
| 1887 | |
| 1888 | static int |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 1889 | Magick_png_read_raw_profile(png_struct *ping,Image *image, |
| 1890 | const ImageInfo *image_info, png_textp text,int ii,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1891 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1892 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1893 | i; |
| 1894 | |
| 1895 | register unsigned char |
| 1896 | *dp; |
| 1897 | |
| 1898 | register png_charp |
| 1899 | sp; |
| 1900 | |
| 1901 | png_uint_32 |
| 1902 | length, |
| 1903 | nibbles; |
| 1904 | |
| 1905 | StringInfo |
| 1906 | *profile; |
| 1907 | |
glennrp | 0c3e06b | 2010-11-19 13:45:02 +0000 | [diff] [blame] | 1908 | const unsigned char |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1909 | unhex[103]={0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, |
| 1910 | 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, |
| 1911 | 0,0,0,0,0,0,0,0,0,1, 2,3,4,5,6,7,8,9,0,0, |
| 1912 | 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, |
| 1913 | 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,10,11,12, |
| 1914 | 13,14,15}; |
| 1915 | |
| 1916 | sp=text[ii].text+1; |
| 1917 | /* look for newline */ |
| 1918 | while (*sp != '\n') |
| 1919 | sp++; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1920 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1921 | /* look for length */ |
| 1922 | while (*sp == '\0' || *sp == ' ' || *sp == '\n') |
| 1923 | sp++; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1924 | |
cristy | f2f2727 | 2009-12-17 14:48:46 +0000 | [diff] [blame] | 1925 | length=(png_uint_32) StringToLong(sp); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1926 | |
glennrp | 97f90e2 | 2011-02-22 05:47:58 +0000 | [diff] [blame] | 1927 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 1928 | " length: %lu",(unsigned long) length); |
| 1929 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1930 | while (*sp != ' ' && *sp != '\n') |
| 1931 | sp++; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1932 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1933 | /* allocate space */ |
| 1934 | if (length == 0) |
| 1935 | { |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 1936 | png_warning(ping,"invalid profile length"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1937 | return(MagickFalse); |
| 1938 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1939 | |
cristy | 8723e4b | 2011-09-01 13:11:19 +0000 | [diff] [blame] | 1940 | profile=BlobToStringInfo((const void *) NULL,length); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1941 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1942 | if (profile == (StringInfo *) NULL) |
| 1943 | { |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 1944 | png_warning(ping, "unable to copy profile"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1945 | return(MagickFalse); |
| 1946 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1947 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1948 | /* copy profile, skipping white space and column 1 "=" signs */ |
| 1949 | dp=GetStringInfoDatum(profile); |
| 1950 | nibbles=length*2; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1951 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1952 | for (i=0; i < (ssize_t) nibbles; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1953 | { |
| 1954 | while (*sp < '0' || (*sp > '9' && *sp < 'a') || *sp > 'f') |
| 1955 | { |
| 1956 | if (*sp == '\0') |
| 1957 | { |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 1958 | png_warning(ping, "ran out of profile data"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1959 | profile=DestroyStringInfo(profile); |
| 1960 | return(MagickFalse); |
| 1961 | } |
| 1962 | sp++; |
| 1963 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1964 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1965 | if (i%2 == 0) |
| 1966 | *dp=(unsigned char) (16*unhex[(int) *sp++]); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1967 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1968 | else |
| 1969 | (*dp++)+=unhex[(int) *sp++]; |
| 1970 | } |
| 1971 | /* |
| 1972 | We have already read "Raw profile type. |
| 1973 | */ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 1974 | (void) SetImageProfile(image,&text[ii].key[17],profile,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1975 | profile=DestroyStringInfo(profile); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1976 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1977 | if (image_info->verbose) |
| 1978 | (void) printf(" Found a generic profile, type %s\n",&text[ii].key[17]); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1979 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1980 | return MagickTrue; |
| 1981 | } |
| 1982 | |
| 1983 | #if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED) |
| 1984 | static int read_vpag_chunk_callback(png_struct *ping, png_unknown_chunkp chunk) |
| 1985 | { |
| 1986 | Image |
| 1987 | *image; |
| 1988 | |
| 1989 | |
| 1990 | /* The unknown chunk structure contains the chunk data: |
| 1991 | png_byte name[5]; |
| 1992 | png_byte *data; |
| 1993 | png_size_t size; |
| 1994 | |
| 1995 | Note that libpng has already taken care of the CRC handling. |
| 1996 | */ |
| 1997 | |
glennrp | 2ad7015 | 2013-03-03 00:44:57 +0000 | [diff] [blame] | 1998 | LogMagickEvent(CoderEvent,GetMagickModule(), |
| 1999 | " read_vpag_chunk: found %c%c%c%c chunk", |
| 2000 | chunk->name[0],chunk->name[1],chunk->name[2],chunk->name[3]); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2001 | |
| 2002 | if (chunk->name[0] != 118 || chunk->name[1] != 112 || |
| 2003 | chunk->name[2] != 65 ||chunk-> name[3] != 103) |
| 2004 | return(0); /* Did not recognize */ |
| 2005 | |
| 2006 | /* recognized vpAg */ |
| 2007 | |
| 2008 | if (chunk->size != 9) |
| 2009 | return(-1); /* Error return */ |
| 2010 | |
| 2011 | if (chunk->data[8] != 0) |
| 2012 | return(0); /* ImageMagick requires pixel units */ |
| 2013 | |
| 2014 | image=(Image *) png_get_user_chunk_ptr(ping); |
| 2015 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2016 | image->page.width=(size_t) ((chunk->data[0] << 24) | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2017 | (chunk->data[1] << 16) | (chunk->data[2] << 8) | chunk->data[3]); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2018 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2019 | image->page.height=(size_t) ((chunk->data[4] << 24) | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2020 | (chunk->data[5] << 16) | (chunk->data[6] << 8) | chunk->data[7]); |
| 2021 | |
| 2022 | /* Return one of the following: */ |
| 2023 | /* return(-n); chunk had an error */ |
| 2024 | /* return(0); did not recognize */ |
| 2025 | /* return(n); success */ |
| 2026 | |
| 2027 | return(1); |
| 2028 | |
| 2029 | } |
| 2030 | #endif |
| 2031 | |
| 2032 | /* |
| 2033 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2034 | % % |
| 2035 | % % |
| 2036 | % % |
| 2037 | % R e a d O n e P N G I m a g e % |
| 2038 | % % |
| 2039 | % % |
| 2040 | % % |
| 2041 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2042 | % |
| 2043 | % ReadOnePNGImage() reads a Portable Network Graphics (PNG) image file |
| 2044 | % (minus the 8-byte signature) and returns it. It allocates the memory |
| 2045 | % necessary for the new Image structure and returns a pointer to the new |
| 2046 | % image. |
| 2047 | % |
| 2048 | % The format of the ReadOnePNGImage method is: |
| 2049 | % |
| 2050 | % Image *ReadOnePNGImage(MngInfo *mng_info, const ImageInfo *image_info, |
| 2051 | % ExceptionInfo *exception) |
| 2052 | % |
| 2053 | % A description of each parameter follows: |
| 2054 | % |
| 2055 | % o mng_info: Specifies a pointer to a MngInfo structure. |
| 2056 | % |
| 2057 | % o image_info: the image info. |
| 2058 | % |
| 2059 | % o exception: return any errors or warnings in this structure. |
| 2060 | % |
| 2061 | */ |
| 2062 | static Image *ReadOnePNGImage(MngInfo *mng_info, |
| 2063 | const ImageInfo *image_info, ExceptionInfo *exception) |
| 2064 | { |
| 2065 | /* Read one PNG image */ |
| 2066 | |
glennrp | cc95c3f | 2011-04-18 16:46:48 +0000 | [diff] [blame] | 2067 | /* To do: Read the tIME chunk into the date:modify property */ |
| 2068 | /* To do: Read the tEXt/Creation Time chunk into the date:create property */ |
| 2069 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2070 | Image |
| 2071 | *image; |
| 2072 | |
glennrp | d0cae25 | 2013-03-15 22:30:41 +0000 | [diff] [blame] | 2073 | char |
glennrp | ec0ddbc | 2013-03-16 12:40:12 +0000 | [diff] [blame] | 2074 | im_vers[32], |
| 2075 | libpng_runv[32], |
| 2076 | libpng_vers[32], |
| 2077 | zlib_runv[32], |
| 2078 | zlib_vers[32]; |
glennrp | d0cae25 | 2013-03-15 22:30:41 +0000 | [diff] [blame] | 2079 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2080 | int |
glennrp | 98b83d4 | 2012-07-23 02:50:31 +0000 | [diff] [blame] | 2081 | intent, /* "PNG Rendering intent", which is ICC intent + 1 */ |
glennrp | cb395ac | 2011-03-30 19:50:23 +0000 | [diff] [blame] | 2082 | num_raw_profiles, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2083 | num_text, |
glennrp | 4eb3931 | 2011-03-30 21:34:55 +0000 | [diff] [blame] | 2084 | num_text_total, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2085 | num_passes, |
glennrp | 913f961 | 2012-06-27 17:50:00 +0000 | [diff] [blame] | 2086 | number_colors, |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2087 | pass, |
| 2088 | ping_bit_depth, |
| 2089 | ping_color_type, |
glennrp | fcf0616 | 2012-11-05 14:57:08 +0000 | [diff] [blame] | 2090 | ping_file_depth, |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2091 | ping_interlace_method, |
| 2092 | ping_compression_method, |
| 2093 | ping_filter_method, |
glennrp | 4eb3931 | 2011-03-30 21:34:55 +0000 | [diff] [blame] | 2094 | ping_num_trans, |
| 2095 | unit_type; |
| 2096 | |
| 2097 | double |
| 2098 | file_gamma; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2099 | |
| 2100 | MagickBooleanType |
cristy | 4383ec8 | 2011-01-05 15:42:32 +0000 | [diff] [blame] | 2101 | logging, |
glennrp | 98b83d4 | 2012-07-23 02:50:31 +0000 | [diff] [blame] | 2102 | ping_found_cHRM, |
| 2103 | ping_found_gAMA, |
| 2104 | ping_found_iCCP, |
| 2105 | ping_found_sRGB, |
glennrp | 3d62786 | 2013-02-26 00:19:34 +0000 | [diff] [blame] | 2106 | ping_found_sRGB_cHRM, |
glennrp | ecab7d7 | 2013-05-14 22:50:32 +0000 | [diff] [blame] | 2107 | ping_preserve_iCCP, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2108 | status; |
| 2109 | |
cristy | 0997332 | 2013-06-30 21:06:30 +0000 | [diff] [blame] | 2110 | MemoryInfo |
| 2111 | *volatile pixel_info; |
| 2112 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 2113 | PixelInfo |
| 2114 | transparent_color; |
| 2115 | |
| 2116 | PNGErrorInfo |
| 2117 | error_info; |
| 2118 | |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2119 | png_bytep |
| 2120 | ping_trans_alpha; |
| 2121 | |
| 2122 | png_color_16p |
| 2123 | ping_background, |
| 2124 | ping_trans_color; |
| 2125 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2126 | png_info |
| 2127 | *end_info, |
| 2128 | *ping_info; |
| 2129 | |
| 2130 | png_struct |
| 2131 | *ping; |
| 2132 | |
| 2133 | png_textp |
| 2134 | text; |
| 2135 | |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2136 | png_uint_32 |
| 2137 | ping_height, |
| 2138 | ping_width, |
glennrp | 4eb3931 | 2011-03-30 21:34:55 +0000 | [diff] [blame] | 2139 | x_resolution, |
| 2140 | y_resolution; |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2141 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 2142 | QuantumInfo |
| 2143 | *quantum_info; |
| 2144 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2145 | ssize_t |
cristy | 756ae43 | 2011-11-19 02:18:25 +0000 | [diff] [blame] | 2146 | ping_rowbytes, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2147 | y; |
| 2148 | |
| 2149 | register unsigned char |
| 2150 | *p; |
| 2151 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2152 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2153 | i, |
| 2154 | x; |
| 2155 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 2156 | register Quantum |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2157 | *q; |
| 2158 | |
| 2159 | size_t |
glennrp | 39992b4 | 2010-11-14 00:03:43 +0000 | [diff] [blame] | 2160 | length, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2161 | row_offset; |
| 2162 | |
cristy | eb3b22a | 2011-03-31 20:16:11 +0000 | [diff] [blame] | 2163 | ssize_t |
| 2164 | j; |
| 2165 | |
cristy | 75fc68f | 2012-10-08 16:26:00 +0000 | [diff] [blame] | 2166 | unsigned char |
cristy | 0997332 | 2013-06-30 21:06:30 +0000 | [diff] [blame] | 2167 | *ping_pixels; |
cristy | 55b78b5 | 2012-10-08 14:01:27 +0000 | [diff] [blame] | 2168 | |
glennrp | 629960f | 2012-05-29 19:13:52 +0000 | [diff] [blame] | 2169 | #ifdef PNG_UNKNOWN_CHUNKS_SUPPORTED |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2170 | png_byte unused_chunks[]= |
| 2171 | { |
| 2172 | 104, 73, 83, 84, (png_byte) '\0', /* hIST */ |
| 2173 | 105, 84, 88, 116, (png_byte) '\0', /* iTXt */ |
| 2174 | 112, 67, 65, 76, (png_byte) '\0', /* pCAL */ |
| 2175 | 115, 67, 65, 76, (png_byte) '\0', /* sCAL */ |
| 2176 | 115, 80, 76, 84, (png_byte) '\0', /* sPLT */ |
| 2177 | 116, 73, 77, 69, (png_byte) '\0', /* tIME */ |
glennrp | 629960f | 2012-05-29 19:13:52 +0000 | [diff] [blame] | 2178 | #ifdef PNG_APNG_SUPPORTED /* libpng was built with APNG patch; */ |
| 2179 | /* ignore the APNG chunks */ |
| 2180 | 97, 99, 84, 76, (png_byte) '\0', /* acTL */ |
| 2181 | 102, 99, 84, 76, (png_byte) '\0', /* fcTL */ |
| 2182 | 102, 100, 65, 84, (png_byte) '\0', /* fdAT */ |
| 2183 | #endif |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2184 | }; |
| 2185 | #endif |
| 2186 | |
| 2187 | logging=LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 2188 | " Enter ReadOnePNGImage()"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2189 | |
glennrp | d0cae25 | 2013-03-15 22:30:41 +0000 | [diff] [blame] | 2190 | /* Define these outside of the following "if logging()" block so they will |
| 2191 | * show in debuggers. |
| 2192 | */ |
| 2193 | *im_vers='\0'; |
| 2194 | (void) ConcatenateMagickString(im_vers, |
glennrp | ec0ddbc | 2013-03-16 12:40:12 +0000 | [diff] [blame] | 2195 | MagickLibVersionText,32); |
glennrp | d0cae25 | 2013-03-15 22:30:41 +0000 | [diff] [blame] | 2196 | (void) ConcatenateMagickString(im_vers, |
glennrp | ec0ddbc | 2013-03-16 12:40:12 +0000 | [diff] [blame] | 2197 | MagickLibAddendum,32); |
| 2198 | |
glennrp | d0cae25 | 2013-03-15 22:30:41 +0000 | [diff] [blame] | 2199 | *libpng_vers='\0'; |
| 2200 | (void) ConcatenateMagickString(libpng_vers, |
glennrp | ec0ddbc | 2013-03-16 12:40:12 +0000 | [diff] [blame] | 2201 | PNG_LIBPNG_VER_STRING,32); |
| 2202 | *libpng_runv='\0'; |
| 2203 | (void) ConcatenateMagickString(libpng_runv, |
| 2204 | png_get_libpng_ver(NULL),32); |
| 2205 | |
glennrp | d0cae25 | 2013-03-15 22:30:41 +0000 | [diff] [blame] | 2206 | *zlib_vers='\0'; |
| 2207 | (void) ConcatenateMagickString(zlib_vers, |
glennrp | ec0ddbc | 2013-03-16 12:40:12 +0000 | [diff] [blame] | 2208 | ZLIB_VERSION,32); |
| 2209 | *zlib_runv='\0'; |
| 2210 | (void) ConcatenateMagickString(zlib_runv, |
| 2211 | zlib_version,32); |
| 2212 | |
glennrp | d0cae25 | 2013-03-15 22:30:41 +0000 | [diff] [blame] | 2213 | if (logging) |
| 2214 | { |
| 2215 | LogMagickEvent(CoderEvent,GetMagickModule()," IM version = %s", |
| 2216 | im_vers); |
| 2217 | LogMagickEvent(CoderEvent,GetMagickModule()," Libpng version = %s", |
| 2218 | libpng_vers); |
glennrp | ec0ddbc | 2013-03-16 12:40:12 +0000 | [diff] [blame] | 2219 | if (LocaleCompare(libpng_vers,libpng_runv) != 0) |
| 2220 | { |
| 2221 | LogMagickEvent(CoderEvent,GetMagickModule()," running with %s", |
| 2222 | libpng_runv); |
| 2223 | } |
glennrp | d0cae25 | 2013-03-15 22:30:41 +0000 | [diff] [blame] | 2224 | LogMagickEvent(CoderEvent,GetMagickModule()," Zlib version = %s", |
| 2225 | zlib_vers); |
glennrp | ec0ddbc | 2013-03-16 12:40:12 +0000 | [diff] [blame] | 2226 | if (LocaleCompare(zlib_vers,zlib_runv) != 0) |
| 2227 | { |
| 2228 | LogMagickEvent(CoderEvent,GetMagickModule()," running with %s", |
| 2229 | zlib_runv); |
| 2230 | } |
glennrp | d0cae25 | 2013-03-15 22:30:41 +0000 | [diff] [blame] | 2231 | } |
| 2232 | |
glennrp | 25c1e2b | 2010-03-25 01:39:56 +0000 | [diff] [blame] | 2233 | #if (PNG_LIBPNG_VER < 10200) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2234 | if (image_info->verbose) |
| 2235 | printf("Your PNG library (libpng-%s) is rather old.\n", |
| 2236 | PNG_LIBPNG_VER_STRING); |
| 2237 | #endif |
| 2238 | |
glennrp | 61b4c95 | 2009-11-10 20:40:41 +0000 | [diff] [blame] | 2239 | #if (PNG_LIBPNG_VER >= 10400) |
| 2240 | # ifndef PNG_TRANSFORM_GRAY_TO_RGB /* Added at libpng-1.4.0beta67 */ |
| 2241 | if (image_info->verbose) |
| 2242 | { |
| 2243 | printf("Your PNG library (libpng-%s) is an old beta version.\n", |
| 2244 | PNG_LIBPNG_VER_STRING); |
| 2245 | printf("Please update it.\n"); |
| 2246 | } |
| 2247 | # endif |
| 2248 | #endif |
| 2249 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 2250 | |
| 2251 | quantum_info = (QuantumInfo *) NULL; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2252 | image=mng_info->image; |
| 2253 | |
glennrp | a6a0663 | 2011-01-19 15:15:34 +0000 | [diff] [blame] | 2254 | if (logging != MagickFalse) |
glennrp | 98b83d4 | 2012-07-23 02:50:31 +0000 | [diff] [blame] | 2255 | { |
glennrp | a6a0663 | 2011-01-19 15:15:34 +0000 | [diff] [blame] | 2256 | (void)LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | ec0ddbc | 2013-03-16 12:40:12 +0000 | [diff] [blame] | 2257 | " Before reading:"); |
glennrp | 3d62786 | 2013-02-26 00:19:34 +0000 | [diff] [blame] | 2258 | |
| 2259 | (void)LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | ec0ddbc | 2013-03-16 12:40:12 +0000 | [diff] [blame] | 2260 | " image->alpha_trait=%d",(int) image->alpha_trait); |
glennrp | a6a0663 | 2011-01-19 15:15:34 +0000 | [diff] [blame] | 2261 | |
glennrp | 98b83d4 | 2012-07-23 02:50:31 +0000 | [diff] [blame] | 2262 | (void)LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | ec0ddbc | 2013-03-16 12:40:12 +0000 | [diff] [blame] | 2263 | " image->rendering_intent=%d",(int) image->rendering_intent); |
glennrp | e88af77 | 2012-08-22 13:59:50 +0000 | [diff] [blame] | 2264 | |
| 2265 | (void)LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | ec0ddbc | 2013-03-16 12:40:12 +0000 | [diff] [blame] | 2266 | " image->colorspace=%d",(int) image->colorspace); |
glennrp | 3d62786 | 2013-02-26 00:19:34 +0000 | [diff] [blame] | 2267 | |
| 2268 | (void)LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | ec0ddbc | 2013-03-16 12:40:12 +0000 | [diff] [blame] | 2269 | " image->gamma=%f", image->gamma); |
glennrp | 98b83d4 | 2012-07-23 02:50:31 +0000 | [diff] [blame] | 2270 | } |
| 2271 | intent=Magick_RenderingIntent_to_PNG_RenderingIntent(image->rendering_intent); |
| 2272 | |
glennrp | 0e31973 | 2011-01-25 21:53:13 +0000 | [diff] [blame] | 2273 | /* Set to an out-of-range color unless tRNS chunk is present */ |
| 2274 | transparent_color.red=65537; |
| 2275 | transparent_color.green=65537; |
| 2276 | transparent_color.blue=65537; |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 2277 | transparent_color.alpha=65537; |
glennrp | 0e31973 | 2011-01-25 21:53:13 +0000 | [diff] [blame] | 2278 | |
glennrp | 913f961 | 2012-06-27 17:50:00 +0000 | [diff] [blame] | 2279 | number_colors=0; |
glennrp | cb395ac | 2011-03-30 19:50:23 +0000 | [diff] [blame] | 2280 | num_text = 0; |
glennrp | 4eb3931 | 2011-03-30 21:34:55 +0000 | [diff] [blame] | 2281 | num_text_total = 0; |
glennrp | cb395ac | 2011-03-30 19:50:23 +0000 | [diff] [blame] | 2282 | num_raw_profiles = 0; |
| 2283 | |
glennrp | 98b83d4 | 2012-07-23 02:50:31 +0000 | [diff] [blame] | 2284 | ping_found_cHRM = MagickFalse; |
| 2285 | ping_found_gAMA = MagickFalse; |
| 2286 | ping_found_iCCP = MagickFalse; |
| 2287 | ping_found_sRGB = MagickFalse; |
glennrp | 4b91759 | 2013-06-20 19:53:59 +0000 | [diff] [blame] | 2288 | ping_found_sRGB_cHRM = MagickFalse; |
glennrp | ecab7d7 | 2013-05-14 22:50:32 +0000 | [diff] [blame] | 2289 | ping_preserve_iCCP = MagickFalse; |
| 2290 | |
| 2291 | { |
| 2292 | const char |
| 2293 | *value; |
| 2294 | |
| 2295 | value=GetImageOption(image_info,"png:preserve-iCCP"); |
| 2296 | |
| 2297 | if (value == NULL) |
| 2298 | value=GetImageArtifact(image,"png:preserve-iCCP"); |
| 2299 | |
| 2300 | if (value != NULL) |
| 2301 | ping_preserve_iCCP=MagickTrue; |
| 2302 | } |
glennrp | 98b83d4 | 2012-07-23 02:50:31 +0000 | [diff] [blame] | 2303 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2304 | /* |
| 2305 | Allocate the PNG structures |
| 2306 | */ |
| 2307 | #ifdef PNG_USER_MEM_SUPPORTED |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 2308 | error_info.image=image; |
| 2309 | error_info.exception=exception; |
| 2310 | ping=png_create_read_struct_2(PNG_LIBPNG_VER_STRING,&error_info, |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 2311 | MagickPNGErrorHandler,MagickPNGWarningHandler, NULL, |
| 2312 | (png_malloc_ptr) Magick_png_malloc,(png_free_ptr) Magick_png_free); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2313 | #else |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 2314 | ping=png_create_read_struct(PNG_LIBPNG_VER_STRING,&error_info, |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 2315 | MagickPNGErrorHandler,MagickPNGWarningHandler); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2316 | #endif |
| 2317 | if (ping == (png_struct *) NULL) |
| 2318 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2319 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2320 | ping_info=png_create_info_struct(ping); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2321 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2322 | if (ping_info == (png_info *) NULL) |
| 2323 | { |
| 2324 | png_destroy_read_struct(&ping,(png_info **) NULL,(png_info **) NULL); |
| 2325 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
| 2326 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2327 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2328 | end_info=png_create_info_struct(ping); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2329 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2330 | if (end_info == (png_info *) NULL) |
| 2331 | { |
| 2332 | png_destroy_read_struct(&ping,&ping_info,(png_info **) NULL); |
| 2333 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
| 2334 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2335 | |
cristy | 0997332 | 2013-06-30 21:06:30 +0000 | [diff] [blame] | 2336 | pixel_info=(MemoryInfo *) NULL; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2337 | |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2338 | if (setjmp(png_jmpbuf(ping))) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2339 | { |
| 2340 | /* |
| 2341 | PNG image is corrupt. |
| 2342 | */ |
| 2343 | png_destroy_read_struct(&ping,&ping_info,&end_info); |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 2344 | |
| 2345 | #ifdef PNG_SETJMP_NOT_THREAD_SAFE |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 2346 | UnlockSemaphoreInfo(ping_semaphore); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2347 | #endif |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 2348 | |
cristy | 0997332 | 2013-06-30 21:06:30 +0000 | [diff] [blame] | 2349 | if (pixel_info != (MemoryInfo *) NULL) |
| 2350 | pixel_info=RelinquishVirtualMemory(pixel_info); |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 2351 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2352 | if (logging != MagickFalse) |
| 2353 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2354 | " exit ReadOnePNGImage() with error."); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2355 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2356 | if (image != (Image *) NULL) |
cristy | 7b755eb | 2009-12-05 14:51:29 +0000 | [diff] [blame] | 2357 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 2358 | InheritException(exception,exception); |
cristy | 7b755eb | 2009-12-05 14:51:29 +0000 | [diff] [blame] | 2359 | image->columns=0; |
| 2360 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2361 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2362 | return(GetFirstImageInList(image)); |
| 2363 | } |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 2364 | |
| 2365 | /* { For navigation to end of SETJMP-protected block. Within this |
| 2366 | * block, use png_error() instead of Throwing an Exception, to ensure |
| 2367 | * that libpng is able to clean up, and that the semaphore is unlocked. |
| 2368 | */ |
| 2369 | |
| 2370 | #ifdef PNG_SETJMP_NOT_THREAD_SAFE |
| 2371 | LockSemaphoreInfo(ping_semaphore); |
| 2372 | #endif |
| 2373 | |
glennrp | 943b7d3 | 2013-04-21 00:40:38 +0000 | [diff] [blame] | 2374 | #ifdef PNG_BENIGN_ERRORS_SUPPORTED |
glennrp | a3a5f95 | 2013-04-17 12:58:15 +0000 | [diff] [blame] | 2375 | /* Allow benign errors */ |
| 2376 | png_set_benign_errors(ping, 1); |
| 2377 | #endif |
| 2378 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2379 | /* |
| 2380 | Prepare PNG for reading. |
| 2381 | */ |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2382 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2383 | mng_info->image_found++; |
| 2384 | png_set_sig_bytes(ping,8); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2385 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2386 | if (LocaleCompare(image_info->magick,"MNG") == 0) |
| 2387 | { |
| 2388 | #if defined(PNG_MNG_FEATURES_SUPPORTED) |
| 2389 | (void) png_permit_mng_features(ping,PNG_ALL_MNG_FEATURES); |
| 2390 | png_set_read_fn(ping,image,png_get_data); |
| 2391 | #else |
| 2392 | #if defined(PNG_READ_EMPTY_PLTE_SUPPORTED) |
| 2393 | png_permit_empty_plte(ping,MagickTrue); |
| 2394 | png_set_read_fn(ping,image,png_get_data); |
| 2395 | #else |
| 2396 | mng_info->image=image; |
| 2397 | mng_info->bytes_in_read_buffer=0; |
| 2398 | mng_info->found_empty_plte=MagickFalse; |
| 2399 | mng_info->have_saved_bkgd_index=MagickFalse; |
| 2400 | png_set_read_fn(ping,mng_info,mng_get_data); |
| 2401 | #endif |
| 2402 | #endif |
| 2403 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2404 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2405 | else |
| 2406 | png_set_read_fn(ping,image,png_get_data); |
| 2407 | |
| 2408 | #if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED) |
| 2409 | /* Ignore unused chunks and all unknown chunks except for vpAg */ |
glennrp | 2ad7015 | 2013-03-03 00:44:57 +0000 | [diff] [blame] | 2410 | #if PNG_LIBPNG_VER < 10700 /* Avoid libpng16 warning */ |
| 2411 | png_set_keep_unknown_chunks(ping, 2, NULL, 0); |
| 2412 | #else |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2413 | png_set_keep_unknown_chunks(ping, 1, NULL, 0); |
glennrp | 2ad7015 | 2013-03-03 00:44:57 +0000 | [diff] [blame] | 2414 | #endif |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2415 | png_set_keep_unknown_chunks(ping, 2, mng_vpAg, 1); |
| 2416 | png_set_keep_unknown_chunks(ping, 1, unused_chunks, |
| 2417 | (int)sizeof(unused_chunks)/5); |
| 2418 | /* Callback for other unknown chunks */ |
| 2419 | png_set_read_user_chunk_fn(ping, image, read_vpag_chunk_callback); |
| 2420 | #endif |
| 2421 | |
glennrp | 2feb141 | 2013-01-22 15:02:16 +0000 | [diff] [blame] | 2422 | #ifdef PNG_SET_USER_LIMITS_SUPPORTED |
glennrp | 09cd962 | 2013-01-22 15:17:38 +0000 | [diff] [blame] | 2423 | # if (PNG_LIBPNG_VER >= 10400) |
glennrp | 2feb141 | 2013-01-22 15:02:16 +0000 | [diff] [blame] | 2424 | /* Limit the size of the chunk storage cache used for sPLT, text, |
glennrp | 6873619 | 2013-01-24 06:32:08 +0000 | [diff] [blame] | 2425 | * and unknown chunks. |
glennrp | 2feb141 | 2013-01-22 15:02:16 +0000 | [diff] [blame] | 2426 | */ |
glennrp | 6873619 | 2013-01-24 06:32:08 +0000 | [diff] [blame] | 2427 | png_set_chunk_cache_max(ping, 32767); |
glennrp | 09cd962 | 2013-01-22 15:17:38 +0000 | [diff] [blame] | 2428 | # endif |
glennrp | 2feb141 | 2013-01-22 15:02:16 +0000 | [diff] [blame] | 2429 | #endif |
| 2430 | |
glennrp | 9bf97b6 | 2012-06-06 21:03:14 +0000 | [diff] [blame] | 2431 | #ifdef PNG_READ_CHECK_FOR_INVALID_INDEX_SUPPORTED |
| 2432 | /* Disable new libpng-1.5.10 feature */ |
| 2433 | png_set_check_for_invalid_index (ping, 0); |
| 2434 | #endif |
| 2435 | |
glennrp | 991e92a | 2010-01-28 03:09:00 +0000 | [diff] [blame] | 2436 | #if (PNG_LIBPNG_VER < 10400) |
| 2437 | # if defined(PNG_USE_PNGGCCRD) && defined(PNG_ASSEMBLER_CODE_SUPPORTED) && \ |
| 2438 | (PNG_LIBPNG_VER >= 10200) && (PNG_LIBPNG_VER < 10220) && defined(__i386__) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2439 | /* Disable thread-unsafe features of pnggccrd */ |
| 2440 | if (png_access_version_number() >= 10200) |
| 2441 | { |
| 2442 | png_uint_32 mmx_disable_mask=0; |
| 2443 | png_uint_32 asm_flags; |
| 2444 | |
| 2445 | mmx_disable_mask |= ( PNG_ASM_FLAG_MMX_READ_COMBINE_ROW \ |
| 2446 | | PNG_ASM_FLAG_MMX_READ_FILTER_SUB \ |
| 2447 | | PNG_ASM_FLAG_MMX_READ_FILTER_AVG \ |
| 2448 | | PNG_ASM_FLAG_MMX_READ_FILTER_PAETH ); |
| 2449 | asm_flags=png_get_asm_flags(ping); |
| 2450 | png_set_asm_flags(ping, asm_flags & ~mmx_disable_mask); |
| 2451 | } |
glennrp | 991e92a | 2010-01-28 03:09:00 +0000 | [diff] [blame] | 2452 | # endif |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2453 | #endif |
| 2454 | |
| 2455 | png_read_info(ping,ping_info); |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2456 | |
| 2457 | png_get_IHDR(ping,ping_info,&ping_width,&ping_height, |
| 2458 | &ping_bit_depth,&ping_color_type, |
| 2459 | &ping_interlace_method,&ping_compression_method, |
| 2460 | &ping_filter_method); |
| 2461 | |
glennrp | fcf0616 | 2012-11-05 14:57:08 +0000 | [diff] [blame] | 2462 | ping_file_depth = ping_bit_depth; |
| 2463 | |
glennrp | 5830fbc | 2013-01-27 06:11:45 +0000 | [diff] [blame] | 2464 | /* Save bit-depth and color-type in case we later want to write a PNG00 */ |
| 2465 | { |
| 2466 | char |
| 2467 | msg[MaxTextExtent]; |
| 2468 | |
| 2469 | (void) FormatLocaleString(msg,MaxTextExtent,"%d",(int) ping_color_type); |
glennrp | 3398b5b | 2013-05-03 23:10:31 +0000 | [diff] [blame] | 2470 | (void) SetImageProperty(image,"png:IHDR.color-type-orig",msg,exception); |
glennrp | 5830fbc | 2013-01-27 06:11:45 +0000 | [diff] [blame] | 2471 | |
| 2472 | (void) FormatLocaleString(msg,MaxTextExtent,"%d",(int) ping_bit_depth); |
glennrp | 3398b5b | 2013-05-03 23:10:31 +0000 | [diff] [blame] | 2473 | (void) SetImageProperty(image,"png:IHDR.bit-depth-orig",msg,exception); |
glennrp | 5830fbc | 2013-01-27 06:11:45 +0000 | [diff] [blame] | 2474 | } |
| 2475 | |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2476 | (void) png_get_tRNS(ping, ping_info, &ping_trans_alpha, &ping_num_trans, |
| 2477 | &ping_trans_color); |
| 2478 | |
| 2479 | (void) png_get_bKGD(ping, ping_info, &ping_background); |
| 2480 | |
| 2481 | if (ping_bit_depth < 8) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2482 | { |
glennrp | fcf0616 | 2012-11-05 14:57:08 +0000 | [diff] [blame] | 2483 | png_set_packing(ping); |
| 2484 | ping_bit_depth = 8; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2485 | } |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2486 | |
| 2487 | image->depth=ping_bit_depth; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2488 | image->depth=GetImageQuantumDepth(image,MagickFalse); |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2489 | image->interlace=ping_interlace_method != 0 ? PNGInterlace : NoInterlace; |
glennrp | 98b83d4 | 2012-07-23 02:50:31 +0000 | [diff] [blame] | 2490 | |
cristy | 176b29a | 2012-06-21 13:35:15 +0000 | [diff] [blame] | 2491 | if (((int) ping_color_type == PNG_COLOR_TYPE_GRAY) || |
| 2492 | ((int) ping_color_type == PNG_COLOR_TYPE_GRAY_ALPHA)) |
| 2493 | { |
| 2494 | image->rendering_intent=UndefinedIntent; |
glennrp | 98b83d4 | 2012-07-23 02:50:31 +0000 | [diff] [blame] | 2495 | intent=Magick_RenderingIntent_to_PNG_RenderingIntent(UndefinedIntent); |
cristy | 176b29a | 2012-06-21 13:35:15 +0000 | [diff] [blame] | 2496 | image->gamma=1.000; |
| 2497 | (void) ResetMagickMemory(&image->chromaticity,0, |
| 2498 | sizeof(image->chromaticity)); |
| 2499 | } |
glennrp | 98b83d4 | 2012-07-23 02:50:31 +0000 | [diff] [blame] | 2500 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2501 | if (logging != MagickFalse) |
| 2502 | { |
| 2503 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 2504 | " PNG width: %.20g, height: %.20g", |
| 2505 | (double) ping_width, (double) ping_height); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2506 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2507 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2508 | " PNG color_type: %d, bit_depth: %d", |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2509 | ping_color_type, ping_bit_depth); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2510 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2511 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2512 | " PNG compression_method: %d", |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2513 | ping_compression_method); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2514 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2515 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2516 | " PNG interlace_method: %d, filter_method: %d", |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2517 | ping_interlace_method,ping_filter_method); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2518 | } |
| 2519 | |
glennrp | ecab7d7 | 2013-05-14 22:50:32 +0000 | [diff] [blame] | 2520 | if (png_get_valid(ping,ping_info, PNG_INFO_iCCP)) |
| 2521 | { |
| 2522 | ping_found_iCCP=MagickTrue; |
| 2523 | if (logging != MagickFalse) |
| 2524 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2525 | " Found PNG iCCP chunk."); |
| 2526 | } |
| 2527 | |
glennrp | 98b83d4 | 2012-07-23 02:50:31 +0000 | [diff] [blame] | 2528 | if (png_get_valid(ping,ping_info,PNG_INFO_gAMA)) |
| 2529 | { |
| 2530 | ping_found_gAMA=MagickTrue; |
| 2531 | if (logging != MagickFalse) |
| 2532 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2533 | " Found PNG gAMA chunk."); |
| 2534 | } |
| 2535 | |
| 2536 | if (png_get_valid(ping,ping_info,PNG_INFO_cHRM)) |
| 2537 | { |
| 2538 | ping_found_cHRM=MagickTrue; |
| 2539 | if (logging != MagickFalse) |
| 2540 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2541 | " Found PNG cHRM chunk."); |
| 2542 | } |
| 2543 | |
glennrp | ecab7d7 | 2013-05-14 22:50:32 +0000 | [diff] [blame] | 2544 | if (ping_found_iCCP != MagickTrue && png_get_valid(ping,ping_info, |
| 2545 | PNG_INFO_sRGB)) |
glennrp | 98b83d4 | 2012-07-23 02:50:31 +0000 | [diff] [blame] | 2546 | { |
| 2547 | ping_found_sRGB=MagickTrue; |
| 2548 | if (logging != MagickFalse) |
| 2549 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2550 | " Found PNG sRGB chunk."); |
| 2551 | } |
| 2552 | |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2553 | #ifdef PNG_READ_iCCP_SUPPORTED |
glennrp | ecab7d7 | 2013-05-14 22:50:32 +0000 | [diff] [blame] | 2554 | if (ping_found_iCCP !=MagickTrue && |
| 2555 | ping_found_sRGB != MagickTrue && |
| 2556 | png_get_valid(ping,ping_info, PNG_INFO_iCCP)) |
| 2557 | { |
| 2558 | ping_found_iCCP=MagickTrue; |
| 2559 | if (logging != MagickFalse) |
| 2560 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2561 | " Found PNG iCCP chunk."); |
| 2562 | } |
| 2563 | |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2564 | if (png_get_valid(ping,ping_info,PNG_INFO_iCCP)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2565 | { |
| 2566 | int |
| 2567 | compression; |
| 2568 | |
glennrp | e4017e3 | 2011-01-08 17:16:09 +0000 | [diff] [blame] | 2569 | #if (PNG_LIBPNG_VER < 10500) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2570 | png_charp |
glennrp | e4017e3 | 2011-01-08 17:16:09 +0000 | [diff] [blame] | 2571 | info; |
| 2572 | #else |
| 2573 | png_bytep |
| 2574 | info; |
| 2575 | #endif |
| 2576 | |
| 2577 | png_charp |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2578 | name; |
| 2579 | |
| 2580 | png_uint_32 |
| 2581 | profile_length; |
| 2582 | |
| 2583 | (void) png_get_iCCP(ping,ping_info,&name,(int *) &compression,&info, |
| 2584 | &profile_length); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2585 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2586 | if (profile_length != 0) |
| 2587 | { |
| 2588 | StringInfo |
| 2589 | *profile; |
| 2590 | |
| 2591 | if (logging != MagickFalse) |
| 2592 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2593 | " Reading PNG iCCP chunk."); |
glennrp | ecab7d7 | 2013-05-14 22:50:32 +0000 | [diff] [blame] | 2594 | |
cristy | e8f8f38 | 2011-09-01 13:32:37 +0000 | [diff] [blame] | 2595 | profile=BlobToStringInfo(info,profile_length); |
glennrp | ecab7d7 | 2013-05-14 22:50:32 +0000 | [diff] [blame] | 2596 | |
cristy | e8f8f38 | 2011-09-01 13:32:37 +0000 | [diff] [blame] | 2597 | if (profile == (StringInfo *) NULL) |
glennrp | ecab7d7 | 2013-05-14 22:50:32 +0000 | [diff] [blame] | 2598 | { |
| 2599 | png_warning(ping, "ICC profile is NULL"); |
| 2600 | profile=DestroyStringInfo(profile); |
| 2601 | } |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 2602 | else |
glennrp | ecab7d7 | 2013-05-14 22:50:32 +0000 | [diff] [blame] | 2603 | { |
| 2604 | if (ping_preserve_iCCP == MagickFalse) |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 2605 | { |
glennrp | ecab7d7 | 2013-05-14 22:50:32 +0000 | [diff] [blame] | 2606 | int |
| 2607 | icheck, |
| 2608 | got_crc=0; |
| 2609 | |
| 2610 | |
| 2611 | png_uint_32 |
| 2612 | length, |
| 2613 | profile_crc=0; |
| 2614 | |
| 2615 | unsigned char |
| 2616 | *data; |
| 2617 | |
| 2618 | length=(png_uint_32) GetStringInfoLength(profile); |
| 2619 | |
| 2620 | for (icheck=0; sRGB_info[icheck].len > 0; icheck++) |
| 2621 | { |
| 2622 | if (length == sRGB_info[icheck].len) |
| 2623 | { |
| 2624 | if (got_crc == 0) |
| 2625 | { |
| 2626 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2627 | " Got a %lu-byte ICC profile (potentially sRGB)", |
| 2628 | (unsigned long) length); |
| 2629 | |
| 2630 | data=GetStringInfoDatum(profile); |
| 2631 | profile_crc=crc32(0,data,length); |
| 2632 | |
| 2633 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2634 | " with crc=%8x",(unsigned int) profile_crc); |
| 2635 | got_crc++; |
| 2636 | } |
| 2637 | |
| 2638 | if (profile_crc == sRGB_info[icheck].crc) |
| 2639 | { |
| 2640 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2641 | " It is sRGB with rendering intent = %s", |
| 2642 | Magick_RenderingIntentString_from_PNG_RenderingIntent( |
| 2643 | sRGB_info[icheck].intent)); |
| 2644 | if (image->rendering_intent==UndefinedIntent) |
| 2645 | { |
| 2646 | image->rendering_intent= |
| 2647 | Magick_RenderingIntent_from_PNG_RenderingIntent( |
| 2648 | sRGB_info[icheck].intent); |
| 2649 | } |
| 2650 | break; |
| 2651 | } |
| 2652 | } |
| 2653 | } |
| 2654 | if (sRGB_info[icheck].len == 0) |
| 2655 | { |
| 2656 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2657 | " Got a %lu-byte ICC profile not recognized as sRGB", |
| 2658 | (unsigned long) length); |
| 2659 | (void) SetImageProfile(image,"icc",profile,exception); |
| 2660 | } |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 2661 | } |
glennrp | ecab7d7 | 2013-05-14 22:50:32 +0000 | [diff] [blame] | 2662 | else /* Preserve-iCCP */ |
| 2663 | { |
| 2664 | (void) SetImageProfile(image,"icc",profile,exception); |
| 2665 | } |
| 2666 | |
| 2667 | profile=DestroyStringInfo(profile); |
| 2668 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2669 | } |
| 2670 | } |
| 2671 | #endif |
glennrp | ecab7d7 | 2013-05-14 22:50:32 +0000 | [diff] [blame] | 2672 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2673 | #if defined(PNG_READ_sRGB_SUPPORTED) |
| 2674 | { |
glennrp | ecab7d7 | 2013-05-14 22:50:32 +0000 | [diff] [blame] | 2675 | if (ping_found_iCCP==MagickFalse && png_get_valid(ping,ping_info, |
| 2676 | PNG_INFO_sRGB)) |
| 2677 | { |
| 2678 | if (png_get_sRGB(ping,ping_info,&intent)) |
cristy | 2ea7a8e | 2012-02-08 01:04:50 +0000 | [diff] [blame] | 2679 | { |
glennrp | ecab7d7 | 2013-05-14 22:50:32 +0000 | [diff] [blame] | 2680 | if (image->rendering_intent == UndefinedIntent) |
| 2681 | image->rendering_intent= |
| 2682 | Magick_RenderingIntent_from_PNG_RenderingIntent (intent); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2683 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2684 | if (logging != MagickFalse) |
| 2685 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | e610a07 | 2010-08-05 17:08:46 +0000 | [diff] [blame] | 2686 | " Reading PNG sRGB chunk: rendering_intent: %d",intent); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2687 | } |
glennrp | ecab7d7 | 2013-05-14 22:50:32 +0000 | [diff] [blame] | 2688 | } |
| 2689 | |
| 2690 | else if (mng_info->have_global_srgb) |
| 2691 | { |
| 2692 | if (image->rendering_intent == UndefinedIntent) |
| 2693 | image->rendering_intent= |
| 2694 | Magick_RenderingIntent_from_PNG_RenderingIntent |
| 2695 | (mng_info->global_srgb_intent); |
| 2696 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2697 | } |
| 2698 | #endif |
glennrp | ecab7d7 | 2013-05-14 22:50:32 +0000 | [diff] [blame] | 2699 | |
| 2700 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2701 | { |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2702 | if (!png_get_gAMA(ping,ping_info,&file_gamma)) |
| 2703 | if (mng_info->have_global_gama) |
| 2704 | png_set_gAMA(ping,ping_info,mng_info->global_gamma); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2705 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2706 | if (png_get_gAMA(ping,ping_info,&file_gamma)) |
| 2707 | { |
| 2708 | image->gamma=(float) file_gamma; |
| 2709 | if (logging != MagickFalse) |
| 2710 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2711 | " Reading PNG gAMA chunk: gamma: %f",file_gamma); |
| 2712 | } |
| 2713 | } |
glennrp | 98b83d4 | 2012-07-23 02:50:31 +0000 | [diff] [blame] | 2714 | |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2715 | if (!png_get_valid(ping,ping_info,PNG_INFO_cHRM)) |
| 2716 | { |
| 2717 | if (mng_info->have_global_chrm != MagickFalse) |
| 2718 | { |
| 2719 | (void) png_set_cHRM(ping,ping_info, |
| 2720 | mng_info->global_chrm.white_point.x, |
| 2721 | mng_info->global_chrm.white_point.y, |
| 2722 | mng_info->global_chrm.red_primary.x, |
| 2723 | mng_info->global_chrm.red_primary.y, |
| 2724 | mng_info->global_chrm.green_primary.x, |
| 2725 | mng_info->global_chrm.green_primary.y, |
| 2726 | mng_info->global_chrm.blue_primary.x, |
| 2727 | mng_info->global_chrm.blue_primary.y); |
| 2728 | } |
| 2729 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2730 | |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2731 | if (png_get_valid(ping,ping_info,PNG_INFO_cHRM)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2732 | { |
| 2733 | (void) png_get_cHRM(ping,ping_info, |
| 2734 | &image->chromaticity.white_point.x, |
| 2735 | &image->chromaticity.white_point.y, |
| 2736 | &image->chromaticity.red_primary.x, |
| 2737 | &image->chromaticity.red_primary.y, |
| 2738 | &image->chromaticity.green_primary.x, |
| 2739 | &image->chromaticity.green_primary.y, |
| 2740 | &image->chromaticity.blue_primary.x, |
| 2741 | &image->chromaticity.blue_primary.y); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2742 | |
glennrp | ecab7d7 | 2013-05-14 22:50:32 +0000 | [diff] [blame] | 2743 | ping_found_cHRM=MagickTrue; |
| 2744 | |
glennrp | 3d62786 | 2013-02-26 00:19:34 +0000 | [diff] [blame] | 2745 | if (image->chromaticity.red_primary.x>0.6399f && |
| 2746 | image->chromaticity.red_primary.x<0.6401f && |
| 2747 | image->chromaticity.red_primary.y>0.3299f && |
| 2748 | image->chromaticity.red_primary.y<0.3301f && |
| 2749 | image->chromaticity.green_primary.x>0.2999f && |
| 2750 | image->chromaticity.green_primary.x<0.3001f && |
| 2751 | image->chromaticity.green_primary.y>0.5999f && |
| 2752 | image->chromaticity.green_primary.y<0.6001f && |
| 2753 | image->chromaticity.blue_primary.x>0.1499f && |
| 2754 | image->chromaticity.blue_primary.x<0.1501f && |
| 2755 | image->chromaticity.blue_primary.y>0.0599f && |
| 2756 | image->chromaticity.blue_primary.y<0.0601f && |
| 2757 | image->chromaticity.white_point.x>0.3126f && |
| 2758 | image->chromaticity.white_point.x<0.3128f && |
| 2759 | image->chromaticity.white_point.y>0.3289f && |
| 2760 | image->chromaticity.white_point.y<0.3291f) |
| 2761 | ping_found_sRGB_cHRM=MagickTrue; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2762 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2763 | |
glennrp | e610a07 | 2010-08-05 17:08:46 +0000 | [diff] [blame] | 2764 | if (image->rendering_intent != UndefinedIntent) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2765 | { |
glennrp | 3d62786 | 2013-02-26 00:19:34 +0000 | [diff] [blame] | 2766 | if (ping_found_sRGB != MagickTrue && |
| 2767 | (ping_found_gAMA != MagickTrue || |
glennrp | 8428823 | 2013-03-02 15:52:36 +0000 | [diff] [blame] | 2768 | (image->gamma > .45 && image->gamma < .46)) && |
glennrp | 3d62786 | 2013-02-26 00:19:34 +0000 | [diff] [blame] | 2769 | (ping_found_cHRM != MagickTrue || |
| 2770 | ping_found_sRGB_cHRM == MagickTrue) && |
| 2771 | ping_found_iCCP != MagickTrue) |
glennrp | 5cf1bff | 2013-01-16 02:44:03 +0000 | [diff] [blame] | 2772 | { |
| 2773 | png_set_sRGB(ping,ping_info, |
| 2774 | Magick_RenderingIntent_to_PNG_RenderingIntent |
| 2775 | (image->rendering_intent)); |
glennrp | 5cf1bff | 2013-01-16 02:44:03 +0000 | [diff] [blame] | 2776 | file_gamma=1.000f/2.200f; |
| 2777 | ping_found_sRGB=MagickTrue; |
glennrp | 8428823 | 2013-03-02 15:52:36 +0000 | [diff] [blame] | 2778 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | 918b9dc | 2013-04-03 13:41:41 +0000 | [diff] [blame] | 2779 | " Setting sRGB as if in input"); |
glennrp | 5cf1bff | 2013-01-16 02:44:03 +0000 | [diff] [blame] | 2780 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2781 | } |
glennrp | 5cf1bff | 2013-01-16 02:44:03 +0000 | [diff] [blame] | 2782 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2783 | #if defined(PNG_oFFs_SUPPORTED) |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2784 | if (png_get_valid(ping,ping_info,PNG_INFO_oFFs)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2785 | { |
cristy | 905ef80 | 2011-02-23 00:29:18 +0000 | [diff] [blame] | 2786 | image->page.x=(ssize_t) png_get_x_offset_pixels(ping, ping_info); |
| 2787 | image->page.y=(ssize_t) png_get_y_offset_pixels(ping, ping_info); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2788 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2789 | if (logging != MagickFalse) |
| 2790 | if (image->page.x || image->page.y) |
| 2791 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 2792 | " Reading PNG oFFs chunk: x: %.20g, y: %.20g.",(double) |
| 2793 | image->page.x,(double) image->page.y); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2794 | } |
| 2795 | #endif |
| 2796 | #if defined(PNG_pHYs_SUPPORTED) |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2797 | if (!png_get_valid(ping,ping_info,PNG_INFO_pHYs)) |
| 2798 | { |
| 2799 | if (mng_info->have_global_phys) |
| 2800 | { |
| 2801 | png_set_pHYs(ping,ping_info, |
| 2802 | mng_info->global_x_pixels_per_unit, |
| 2803 | mng_info->global_y_pixels_per_unit, |
| 2804 | mng_info->global_phys_unit_type); |
| 2805 | } |
| 2806 | } |
| 2807 | |
| 2808 | if (png_get_valid(ping,ping_info,PNG_INFO_pHYs)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2809 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2810 | /* |
| 2811 | Set image resolution. |
| 2812 | */ |
| 2813 | (void) png_get_pHYs(ping,ping_info,&x_resolution,&y_resolution, |
cristy | 0881b52 | 2010-04-24 23:45:19 +0000 | [diff] [blame] | 2814 | &unit_type); |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 2815 | image->resolution.x=(double) x_resolution; |
| 2816 | image->resolution.y=(double) y_resolution; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2817 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2818 | if (unit_type == PNG_RESOLUTION_METER) |
| 2819 | { |
| 2820 | image->units=PixelsPerCentimeterResolution; |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 2821 | image->resolution.x=(double) x_resolution/100.0; |
| 2822 | image->resolution.y=(double) y_resolution/100.0; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2823 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2824 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2825 | if (logging != MagickFalse) |
| 2826 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 2827 | " Reading PNG pHYs chunk: xres: %.20g, yres: %.20g, units: %d.", |
| 2828 | (double) x_resolution,(double) y_resolution,unit_type); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2829 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2830 | #endif |
glennrp | 823b55c | 2011-03-14 18:46:46 +0000 | [diff] [blame] | 2831 | |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2832 | if (png_get_valid(ping,ping_info,PNG_INFO_PLTE)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2833 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2834 | png_colorp |
| 2835 | palette; |
| 2836 | |
| 2837 | (void) png_get_PLTE(ping,ping_info,&palette,&number_colors); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2838 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2839 | if ((number_colors == 0) && |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2840 | ((int) ping_color_type == PNG_COLOR_TYPE_PALETTE)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2841 | { |
| 2842 | if (mng_info->global_plte_length) |
| 2843 | { |
| 2844 | png_set_PLTE(ping,ping_info,mng_info->global_plte, |
| 2845 | (int) mng_info->global_plte_length); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2846 | |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2847 | if (!png_get_valid(ping,ping_info,PNG_INFO_tRNS)) |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 2848 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2849 | if (mng_info->global_trns_length) |
| 2850 | { |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 2851 | png_warning(ping, |
| 2852 | "global tRNS has more entries than global PLTE"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2853 | } |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 2854 | else |
| 2855 | { |
| 2856 | png_set_tRNS(ping,ping_info,mng_info->global_trns, |
| 2857 | (int) mng_info->global_trns_length,NULL); |
| 2858 | } |
| 2859 | } |
glennrp | bfd9e61 | 2011-04-22 14:02:20 +0000 | [diff] [blame] | 2860 | #ifdef PNG_READ_bKGD_SUPPORTED |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2861 | if ( |
| 2862 | #ifndef PNG_READ_EMPTY_PLTE_SUPPORTED |
| 2863 | mng_info->have_saved_bkgd_index || |
| 2864 | #endif |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2865 | png_get_valid(ping,ping_info,PNG_INFO_bKGD)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2866 | { |
| 2867 | png_color_16 |
| 2868 | background; |
| 2869 | |
| 2870 | #ifndef PNG_READ_EMPTY_PLTE_SUPPORTED |
| 2871 | if (mng_info->have_saved_bkgd_index) |
| 2872 | background.index=mng_info->saved_bkgd_index; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2873 | #endif |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2874 | if (png_get_valid(ping, ping_info, PNG_INFO_bKGD)) |
| 2875 | background.index=ping_background->index; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2876 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2877 | background.red=(png_uint_16) |
| 2878 | mng_info->global_plte[background.index].red; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2879 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2880 | background.green=(png_uint_16) |
| 2881 | mng_info->global_plte[background.index].green; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2882 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2883 | background.blue=(png_uint_16) |
| 2884 | mng_info->global_plte[background.index].blue; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2885 | |
glennrp | c6c391a | 2011-04-27 02:23:56 +0000 | [diff] [blame] | 2886 | background.gray=(png_uint_16) |
| 2887 | mng_info->global_plte[background.index].green; |
| 2888 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2889 | png_set_bKGD(ping,ping_info,&background); |
| 2890 | } |
| 2891 | #endif |
| 2892 | } |
| 2893 | else |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 2894 | png_error(ping,"No global PLTE in file"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2895 | } |
| 2896 | } |
| 2897 | |
glennrp | bfd9e61 | 2011-04-22 14:02:20 +0000 | [diff] [blame] | 2898 | #ifdef PNG_READ_bKGD_SUPPORTED |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2899 | if (mng_info->have_global_bkgd && |
| 2900 | (!png_get_valid(ping,ping_info,PNG_INFO_bKGD))) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2901 | image->background_color=mng_info->mng_global_bkgd; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2902 | |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2903 | if (png_get_valid(ping,ping_info,PNG_INFO_bKGD)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2904 | { |
glennrp | bfd9e61 | 2011-04-22 14:02:20 +0000 | [diff] [blame] | 2905 | unsigned int |
| 2906 | bkgd_scale; |
| 2907 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2908 | /* |
| 2909 | Set image background color. |
| 2910 | */ |
| 2911 | if (logging != MagickFalse) |
| 2912 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2913 | " Reading PNG bKGD chunk."); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2914 | |
glennrp | bfd9e61 | 2011-04-22 14:02:20 +0000 | [diff] [blame] | 2915 | /* Scale background components to 16-bit, then scale |
| 2916 | * to quantum depth |
| 2917 | */ |
| 2918 | if (logging != MagickFalse) |
| 2919 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2920 | " raw ping_background=(%d,%d,%d).",ping_background->red, |
| 2921 | ping_background->green,ping_background->blue); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2922 | |
glennrp | bfd9e61 | 2011-04-22 14:02:20 +0000 | [diff] [blame] | 2923 | bkgd_scale = 1; |
glennrp | 2cbb448 | 2010-06-02 04:37:24 +0000 | [diff] [blame] | 2924 | |
glennrp | fcf0616 | 2012-11-05 14:57:08 +0000 | [diff] [blame] | 2925 | if (ping_file_depth == 1) |
glennrp | bfd9e61 | 2011-04-22 14:02:20 +0000 | [diff] [blame] | 2926 | bkgd_scale = 255; |
glennrp | 2cbb448 | 2010-06-02 04:37:24 +0000 | [diff] [blame] | 2927 | |
glennrp | fcf0616 | 2012-11-05 14:57:08 +0000 | [diff] [blame] | 2928 | else if (ping_file_depth == 2) |
glennrp | bfd9e61 | 2011-04-22 14:02:20 +0000 | [diff] [blame] | 2929 | bkgd_scale = 85; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2930 | |
glennrp | fcf0616 | 2012-11-05 14:57:08 +0000 | [diff] [blame] | 2931 | else if (ping_file_depth == 4) |
glennrp | bfd9e61 | 2011-04-22 14:02:20 +0000 | [diff] [blame] | 2932 | bkgd_scale = 17; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2933 | |
glennrp | fcf0616 | 2012-11-05 14:57:08 +0000 | [diff] [blame] | 2934 | if (ping_file_depth <= 8) |
glennrp | bfd9e61 | 2011-04-22 14:02:20 +0000 | [diff] [blame] | 2935 | bkgd_scale *= 257; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2936 | |
glennrp | bfd9e61 | 2011-04-22 14:02:20 +0000 | [diff] [blame] | 2937 | ping_background->red *= bkgd_scale; |
| 2938 | ping_background->green *= bkgd_scale; |
| 2939 | ping_background->blue *= bkgd_scale; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2940 | |
glennrp | bfd9e61 | 2011-04-22 14:02:20 +0000 | [diff] [blame] | 2941 | if (logging != MagickFalse) |
| 2942 | { |
glennrp | 2cbb448 | 2010-06-02 04:37:24 +0000 | [diff] [blame] | 2943 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2944 | " bkgd_scale=%d.",bkgd_scale); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2945 | |
glennrp | 2cbb448 | 2010-06-02 04:37:24 +0000 | [diff] [blame] | 2946 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2947 | " ping_background=(%d,%d,%d).",ping_background->red, |
| 2948 | ping_background->green,ping_background->blue); |
glennrp | bfd9e61 | 2011-04-22 14:02:20 +0000 | [diff] [blame] | 2949 | } |
glennrp | 2cbb448 | 2010-06-02 04:37:24 +0000 | [diff] [blame] | 2950 | |
glennrp | bfd9e61 | 2011-04-22 14:02:20 +0000 | [diff] [blame] | 2951 | image->background_color.red= |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2952 | ScaleShortToQuantum(ping_background->red); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2953 | |
glennrp | bfd9e61 | 2011-04-22 14:02:20 +0000 | [diff] [blame] | 2954 | image->background_color.green= |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2955 | ScaleShortToQuantum(ping_background->green); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2956 | |
glennrp | bfd9e61 | 2011-04-22 14:02:20 +0000 | [diff] [blame] | 2957 | image->background_color.blue= |
| 2958 | ScaleShortToQuantum(ping_background->blue); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2959 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 2960 | image->background_color.alpha=OpaqueAlpha; |
glennrp | 2cbb448 | 2010-06-02 04:37:24 +0000 | [diff] [blame] | 2961 | |
glennrp | bfd9e61 | 2011-04-22 14:02:20 +0000 | [diff] [blame] | 2962 | if (logging != MagickFalse) |
| 2963 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2964 | " image->background_color=(%.20g,%.20g,%.20g).", |
| 2965 | (double) image->background_color.red, |
| 2966 | (double) image->background_color.green, |
| 2967 | (double) image->background_color.blue); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2968 | } |
glennrp | bfd9e61 | 2011-04-22 14:02:20 +0000 | [diff] [blame] | 2969 | #endif /* PNG_READ_bKGD_SUPPORTED */ |
glennrp | a6a0663 | 2011-01-19 15:15:34 +0000 | [diff] [blame] | 2970 | |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2971 | if (png_get_valid(ping,ping_info,PNG_INFO_tRNS)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2972 | { |
| 2973 | /* |
glennrp | a6a0663 | 2011-01-19 15:15:34 +0000 | [diff] [blame] | 2974 | Image has a tRNS chunk. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2975 | */ |
| 2976 | int |
| 2977 | max_sample; |
| 2978 | |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 2979 | size_t |
| 2980 | one=1; |
| 2981 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2982 | if (logging != MagickFalse) |
| 2983 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2984 | " Reading PNG tRNS chunk."); |
| 2985 | |
glennrp | fcf0616 | 2012-11-05 14:57:08 +0000 | [diff] [blame] | 2986 | max_sample = (int) ((one << ping_file_depth) - 1); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2987 | |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2988 | if ((ping_color_type == PNG_COLOR_TYPE_GRAY && |
| 2989 | (int)ping_trans_color->gray > max_sample) || |
| 2990 | (ping_color_type == PNG_COLOR_TYPE_RGB && |
| 2991 | ((int)ping_trans_color->red > max_sample || |
| 2992 | (int)ping_trans_color->green > max_sample || |
| 2993 | (int)ping_trans_color->blue > max_sample))) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2994 | { |
| 2995 | if (logging != MagickFalse) |
| 2996 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2997 | " Ignoring PNG tRNS chunk with out-of-range sample."); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2998 | png_free_data(ping, ping_info, PNG_FREE_TRNS, 0); |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2999 | png_set_invalid(ping,ping_info,PNG_INFO_tRNS); |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 3000 | image->alpha_trait=UndefinedPixelTrait; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3001 | } |
| 3002 | else |
| 3003 | { |
glennrp | a6a0663 | 2011-01-19 15:15:34 +0000 | [diff] [blame] | 3004 | int |
| 3005 | scale_to_short; |
| 3006 | |
glennrp | fcf0616 | 2012-11-05 14:57:08 +0000 | [diff] [blame] | 3007 | scale_to_short = 65535L/((1UL << ping_file_depth)-1); |
glennrp | a6a0663 | 2011-01-19 15:15:34 +0000 | [diff] [blame] | 3008 | |
| 3009 | /* Scale transparent_color to short */ |
| 3010 | transparent_color.red= scale_to_short*ping_trans_color->red; |
| 3011 | transparent_color.green= scale_to_short*ping_trans_color->green; |
| 3012 | transparent_color.blue= scale_to_short*ping_trans_color->blue; |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3013 | transparent_color.alpha= scale_to_short*ping_trans_color->gray; |
glennrp | 05eb4a9 | 2010-07-08 02:21:09 +0000 | [diff] [blame] | 3014 | |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 3015 | if (ping_color_type == PNG_COLOR_TYPE_GRAY) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3016 | { |
glennrp | 0f11198 | 2010-07-07 20:18:33 +0000 | [diff] [blame] | 3017 | if (logging != MagickFalse) |
| 3018 | { |
| 3019 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 3020 | " Raw tRNS graylevel is %d.",ping_trans_color->gray); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3021 | |
glennrp | 0f11198 | 2010-07-07 20:18:33 +0000 | [diff] [blame] | 3022 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3023 | " scaled graylevel is %.20g.",transparent_color.alpha); |
glennrp | 0f11198 | 2010-07-07 20:18:33 +0000 | [diff] [blame] | 3024 | } |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3025 | transparent_color.red=transparent_color.alpha; |
| 3026 | transparent_color.green=transparent_color.alpha; |
| 3027 | transparent_color.blue=transparent_color.alpha; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3028 | } |
| 3029 | } |
| 3030 | } |
| 3031 | #if defined(PNG_READ_sBIT_SUPPORTED) |
| 3032 | if (mng_info->have_global_sbit) |
| 3033 | { |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 3034 | if (!png_get_valid(ping,ping_info,PNG_INFO_sBIT)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3035 | png_set_sBIT(ping,ping_info,&mng_info->global_sbit); |
| 3036 | } |
| 3037 | #endif |
| 3038 | num_passes=png_set_interlace_handling(ping); |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 3039 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3040 | png_read_update_info(ping,ping_info); |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 3041 | |
| 3042 | ping_rowbytes=png_get_rowbytes(ping,ping_info); |
| 3043 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3044 | /* |
| 3045 | Initialize image structure. |
| 3046 | */ |
| 3047 | mng_info->image_box.left=0; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3048 | mng_info->image_box.right=(ssize_t) ping_width; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3049 | mng_info->image_box.top=0; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3050 | mng_info->image_box.bottom=(ssize_t) ping_height; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3051 | if (mng_info->mng_type == 0) |
| 3052 | { |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 3053 | mng_info->mng_width=ping_width; |
| 3054 | mng_info->mng_height=ping_height; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3055 | mng_info->frame=mng_info->image_box; |
| 3056 | mng_info->clip=mng_info->image_box; |
| 3057 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3058 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3059 | else |
| 3060 | { |
| 3061 | image->page.y=mng_info->y_off[mng_info->object_id]; |
| 3062 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3063 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3064 | image->compression=ZipCompression; |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 3065 | image->columns=ping_width; |
| 3066 | image->rows=ping_height; |
glennrp | a6c5d34 | 2012-05-14 13:21:17 +0000 | [diff] [blame] | 3067 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3068 | if (((int) ping_color_type == PNG_COLOR_TYPE_GRAY) || |
| 3069 | ((int) ping_color_type == PNG_COLOR_TYPE_GRAY_ALPHA)) |
glennrp | 8d0bca5 | 2012-05-17 02:33:23 +0000 | [diff] [blame] | 3070 | { |
glennrp | e88af77 | 2012-08-22 13:59:50 +0000 | [diff] [blame] | 3071 | if ((!png_get_valid(ping,ping_info,PNG_INFO_gAMA) || |
glennrp | 3d62786 | 2013-02-26 00:19:34 +0000 | [diff] [blame] | 3072 | image->gamma == 1.0) && ping_found_sRGB != MagickTrue) |
glennrp | 8d0bca5 | 2012-05-17 02:33:23 +0000 | [diff] [blame] | 3073 | { |
| 3074 | /* Set image->gamma to 1.0, image->rendering_intent to Undefined, |
glennrp | e88af77 | 2012-08-22 13:59:50 +0000 | [diff] [blame] | 3075 | * image->colorspace to GRAY, and reset image->chromaticity. |
glennrp | 8d0bca5 | 2012-05-17 02:33:23 +0000 | [diff] [blame] | 3076 | */ |
| 3077 | SetImageColorspace(image,GRAYColorspace,exception); |
| 3078 | } |
glennrp | 8d0bca5 | 2012-05-17 02:33:23 +0000 | [diff] [blame] | 3079 | } |
glennrp | e88af77 | 2012-08-22 13:59:50 +0000 | [diff] [blame] | 3080 | |
| 3081 | (void)LogMagickEvent(CoderEvent,GetMagickModule(), |
| 3082 | " image->colorspace=%d",(int) image->colorspace); |
glennrp | a6c5d34 | 2012-05-14 13:21:17 +0000 | [diff] [blame] | 3083 | |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 3084 | if (((int) ping_color_type == PNG_COLOR_TYPE_PALETTE) || |
glennrp | 32340ff | 2012-08-25 17:36:19 +0000 | [diff] [blame] | 3085 | ((int) ping_bit_depth < 16 && |
| 3086 | (int) ping_color_type == PNG_COLOR_TYPE_GRAY)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3087 | { |
cristy | befe4d2 | 2010-06-07 01:18:58 +0000 | [diff] [blame] | 3088 | size_t |
| 3089 | one; |
| 3090 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3091 | image->storage_class=PseudoClass; |
cristy | befe4d2 | 2010-06-07 01:18:58 +0000 | [diff] [blame] | 3092 | one=1; |
glennrp | fcf0616 | 2012-11-05 14:57:08 +0000 | [diff] [blame] | 3093 | image->colors=one << ping_file_depth; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3094 | #if (MAGICKCORE_QUANTUM_DEPTH == 8) |
| 3095 | if (image->colors > 256) |
glennrp | 67b9c1a | 2011-04-22 18:47:36 +0000 | [diff] [blame] | 3096 | image->colors=256; |
| 3097 | #else |
| 3098 | if (image->colors > 65536L) |
| 3099 | image->colors=65536L; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3100 | #endif |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 3101 | if ((int) ping_color_type == PNG_COLOR_TYPE_PALETTE) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3102 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3103 | png_colorp |
| 3104 | palette; |
| 3105 | |
| 3106 | (void) png_get_PLTE(ping,ping_info,&palette,&number_colors); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3107 | image->colors=(size_t) number_colors; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3108 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3109 | if (logging != MagickFalse) |
| 3110 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 3111 | " Reading PNG PLTE chunk: number_colors: %d.",number_colors); |
| 3112 | } |
| 3113 | } |
| 3114 | |
| 3115 | if (image->storage_class == PseudoClass) |
| 3116 | { |
| 3117 | /* |
| 3118 | Initialize image colormap. |
| 3119 | */ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3120 | if (AcquireImageColormap(image,image->colors,exception) == MagickFalse) |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 3121 | png_error(ping,"Memory allocation failed"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3122 | |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 3123 | if ((int) ping_color_type == PNG_COLOR_TYPE_PALETTE) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3124 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3125 | png_colorp |
| 3126 | palette; |
| 3127 | |
| 3128 | (void) png_get_PLTE(ping,ping_info,&palette,&number_colors); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3129 | |
glennrp | 6af6cf1 | 2011-04-22 13:05:16 +0000 | [diff] [blame] | 3130 | for (i=0; i < (ssize_t) number_colors; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3131 | { |
| 3132 | image->colormap[i].red=ScaleCharToQuantum(palette[i].red); |
| 3133 | image->colormap[i].green=ScaleCharToQuantum(palette[i].green); |
| 3134 | image->colormap[i].blue=ScaleCharToQuantum(palette[i].blue); |
| 3135 | } |
glennrp | 6af6cf1 | 2011-04-22 13:05:16 +0000 | [diff] [blame] | 3136 | |
glennrp | 67b9c1a | 2011-04-22 18:47:36 +0000 | [diff] [blame] | 3137 | for ( ; i < (ssize_t) image->colors; i++) |
glennrp | 6af6cf1 | 2011-04-22 13:05:16 +0000 | [diff] [blame] | 3138 | { |
| 3139 | image->colormap[i].red=0; |
| 3140 | image->colormap[i].green=0; |
| 3141 | image->colormap[i].blue=0; |
| 3142 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3143 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3144 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3145 | else |
| 3146 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3147 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3148 | scale; |
| 3149 | |
glennrp | fcf0616 | 2012-11-05 14:57:08 +0000 | [diff] [blame] | 3150 | scale=(QuantumRange/((1UL << ping_file_depth)-1)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3151 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3152 | if (scale < 1) |
| 3153 | scale=1; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3154 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3155 | for (i=0; i < (ssize_t) image->colors; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3156 | { |
| 3157 | image->colormap[i].red=(Quantum) (i*scale); |
| 3158 | image->colormap[i].green=(Quantum) (i*scale); |
| 3159 | image->colormap[i].blue=(Quantum) (i*scale); |
| 3160 | } |
| 3161 | } |
| 3162 | } |
glennrp | 147bc91 | 2011-03-30 18:47:21 +0000 | [diff] [blame] | 3163 | |
glennrp | cb395ac | 2011-03-30 19:50:23 +0000 | [diff] [blame] | 3164 | /* Set some properties for reporting by "identify" */ |
| 3165 | { |
glennrp | 147bc91 | 2011-03-30 18:47:21 +0000 | [diff] [blame] | 3166 | char |
| 3167 | msg[MaxTextExtent]; |
| 3168 | |
glennrp | fcf0616 | 2012-11-05 14:57:08 +0000 | [diff] [blame] | 3169 | /* encode ping_width, ping_height, ping_file_depth, ping_color_type, |
glennrp | 147bc91 | 2011-03-30 18:47:21 +0000 | [diff] [blame] | 3170 | ping_interlace_method in value */ |
| 3171 | |
cristy | 3b6fd2e | 2011-05-20 12:53:50 +0000 | [diff] [blame] | 3172 | (void) FormatLocaleString(msg,MaxTextExtent, |
glennrp | 7cdb11c | 2011-03-31 18:17:25 +0000 | [diff] [blame] | 3173 | "%d, %d",(int) ping_width, (int) ping_height); |
glennrp | 3398b5b | 2013-05-03 23:10:31 +0000 | [diff] [blame] | 3174 | (void) SetImageProperty(image,"png:IHDR.width,height",msg,exception); |
glennrp | 147bc91 | 2011-03-30 18:47:21 +0000 | [diff] [blame] | 3175 | |
glennrp | fcf0616 | 2012-11-05 14:57:08 +0000 | [diff] [blame] | 3176 | (void) FormatLocaleString(msg,MaxTextExtent,"%d",(int) ping_file_depth); |
glennrp | 3398b5b | 2013-05-03 23:10:31 +0000 | [diff] [blame] | 3177 | (void) SetImageProperty(image,"png:IHDR.bit_depth",msg,exception); |
glennrp | 147bc91 | 2011-03-30 18:47:21 +0000 | [diff] [blame] | 3178 | |
glennrp | 5dff435 | 2012-06-06 22:12:04 +0000 | [diff] [blame] | 3179 | (void) FormatLocaleString(msg,MaxTextExtent,"%d (%s)", |
| 3180 | (int) ping_color_type, |
| 3181 | Magick_ColorType_from_PNG_ColorType((int)ping_color_type)); |
glennrp | 3398b5b | 2013-05-03 23:10:31 +0000 | [diff] [blame] | 3182 | (void) SetImageProperty(image,"png:IHDR.color_type",msg,exception); |
glennrp | 147bc91 | 2011-03-30 18:47:21 +0000 | [diff] [blame] | 3183 | |
glennrp | 913f961 | 2012-06-27 17:50:00 +0000 | [diff] [blame] | 3184 | if (ping_interlace_method == 0) |
| 3185 | { |
| 3186 | (void) FormatLocaleString(msg,MaxTextExtent,"%d (Not interlaced)", |
| 3187 | (int) ping_interlace_method); |
| 3188 | } |
| 3189 | else if (ping_interlace_method == 1) |
| 3190 | { |
| 3191 | (void) FormatLocaleString(msg,MaxTextExtent,"%d (Adam7 method)", |
| 3192 | (int) ping_interlace_method); |
| 3193 | } |
| 3194 | else |
| 3195 | { |
| 3196 | (void) FormatLocaleString(msg,MaxTextExtent,"%d (Unknown method)", |
| 3197 | (int) ping_interlace_method); |
| 3198 | } |
| 3199 | (void) SetImageProperty(image,"png:IHDR.interlace_method",msg,exception); |
| 3200 | |
| 3201 | if (number_colors != 0) |
| 3202 | { |
| 3203 | (void) FormatLocaleString(msg,MaxTextExtent,"%d", |
| 3204 | (int) number_colors); |
glennrp | 3398b5b | 2013-05-03 23:10:31 +0000 | [diff] [blame] | 3205 | (void) SetImageProperty(image,"png:PLTE.number_colors",msg, |
glennrp | 913f961 | 2012-06-27 17:50:00 +0000 | [diff] [blame] | 3206 | exception); |
| 3207 | } |
glennrp | cb395ac | 2011-03-30 19:50:23 +0000 | [diff] [blame] | 3208 | } |
glennrp | 147bc91 | 2011-03-30 18:47:21 +0000 | [diff] [blame] | 3209 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3210 | /* |
| 3211 | Read image scanlines. |
| 3212 | */ |
| 3213 | if (image->delay != 0) |
| 3214 | mng_info->scenes_found++; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3215 | |
glennrp | 0ca69b1 | 2010-07-26 01:57:52 +0000 | [diff] [blame] | 3216 | if ((mng_info->mng_type == 0 && (image->ping != MagickFalse)) || ( |
glennrp | 347e40f | 2010-06-06 11:27:30 +0000 | [diff] [blame] | 3217 | (image_info->number_scenes != 0) && (mng_info->scenes_found > (ssize_t) |
| 3218 | (image_info->first_scene+image_info->number_scenes)))) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3219 | { |
glennrp | 1b888c4 | 2012-03-02 15:18:14 +0000 | [diff] [blame] | 3220 | /* This happens later in non-ping decodes */ |
| 3221 | if (png_get_valid(ping,ping_info,PNG_INFO_tRNS)) |
| 3222 | image->storage_class=DirectClass; |
| 3223 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3224 | if (logging != MagickFalse) |
| 3225 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 3226 | " Skipping PNG image data for scene %.20g",(double) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3227 | mng_info->scenes_found-1); |
| 3228 | png_destroy_read_struct(&ping,&ping_info,&end_info); |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 3229 | |
| 3230 | #ifdef PNG_SETJMP_NOT_THREAD_SAFE |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 3231 | UnlockSemaphoreInfo(ping_semaphore); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3232 | #endif |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 3233 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3234 | if (logging != MagickFalse) |
| 3235 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 3236 | " exit ReadOnePNGImage()."); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3237 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3238 | return(image); |
| 3239 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3240 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3241 | if (logging != MagickFalse) |
| 3242 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 3243 | " Reading PNG IDAT chunk(s)"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3244 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3245 | if (num_passes > 1) |
cristy | 0997332 | 2013-06-30 21:06:30 +0000 | [diff] [blame] | 3246 | pixel_info=AcquireVirtualMemory(image->rows,ping_rowbytes* |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 3247 | sizeof(*ping_pixels)); |
cristy | 0997332 | 2013-06-30 21:06:30 +0000 | [diff] [blame] | 3248 | else |
| 3249 | pixel_info=AcquireVirtualMemory(ping_rowbytes,sizeof(*ping_pixels)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3250 | |
cristy | 0997332 | 2013-06-30 21:06:30 +0000 | [diff] [blame] | 3251 | if (pixel_info == (MemoryInfo *) NULL) |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 3252 | png_error(ping,"Memory allocation failed"); |
cristy | 0997332 | 2013-06-30 21:06:30 +0000 | [diff] [blame] | 3253 | ping_pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3254 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3255 | if (logging != MagickFalse) |
| 3256 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 3257 | " Converting PNG pixels to pixel packets"); |
| 3258 | /* |
| 3259 | Convert PNG pixels to pixel packets. |
| 3260 | */ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3261 | quantum_info=AcquireQuantumInfo(image_info,image); |
| 3262 | |
| 3263 | if (quantum_info == (QuantumInfo *) NULL) |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 3264 | png_error(ping,"Failed to allocate quantum_info"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3265 | |
glennrp | 4b840d7 | 2012-11-22 16:01:16 +0000 | [diff] [blame] | 3266 | (void) SetQuantumEndian(image,quantum_info,MSBEndian); |
| 3267 | |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 3268 | { |
| 3269 | |
| 3270 | MagickBooleanType |
| 3271 | found_transparent_pixel; |
| 3272 | |
| 3273 | found_transparent_pixel=MagickFalse; |
| 3274 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3275 | if (image->storage_class == DirectClass) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3276 | { |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 3277 | for (pass=0; pass < num_passes; pass++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3278 | { |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 3279 | /* |
| 3280 | Convert image to DirectClass pixel packets. |
| 3281 | */ |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 3282 | image->alpha_trait=(((int) ping_color_type == PNG_COLOR_TYPE_RGB_ALPHA) || |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 3283 | ((int) ping_color_type == PNG_COLOR_TYPE_GRAY_ALPHA) || |
| 3284 | (png_get_valid(ping,ping_info,PNG_INFO_tRNS))) ? |
cristy | b0a657e | 2012-08-29 00:45:37 +0000 | [diff] [blame] | 3285 | BlendPixelTrait : UndefinedPixelTrait; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3286 | |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 3287 | for (y=0; y < (ssize_t) image->rows; y++) |
| 3288 | { |
| 3289 | if (num_passes > 1) |
| 3290 | row_offset=ping_rowbytes*y; |
| 3291 | |
| 3292 | else |
| 3293 | row_offset=0; |
| 3294 | |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 3295 | png_read_row(ping,ping_pixels+row_offset,NULL); |
glennrp | c5c31be | 2012-11-07 18:42:18 +0000 | [diff] [blame] | 3296 | |
| 3297 | if (pass < num_passes-1) |
| 3298 | continue; |
| 3299 | |
cristy | 862a33c | 2012-05-17 22:49:37 +0000 | [diff] [blame] | 3300 | q=GetAuthenticPixels(image,0,y,image->columns,1,exception); |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 3301 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3302 | if (q == (Quantum *) NULL) |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 3303 | break; |
| 3304 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3305 | if ((int) ping_color_type == PNG_COLOR_TYPE_GRAY) |
| 3306 | (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info, |
| 3307 | GrayQuantum,ping_pixels+row_offset,exception); |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 3308 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3309 | else if ((int) ping_color_type == PNG_COLOR_TYPE_GRAY_ALPHA) |
| 3310 | (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info, |
| 3311 | GrayAlphaQuantum,ping_pixels+row_offset,exception); |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 3312 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3313 | else if ((int) ping_color_type == PNG_COLOR_TYPE_RGB_ALPHA) |
| 3314 | (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info, |
| 3315 | RGBAQuantum,ping_pixels+row_offset,exception); |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 3316 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3317 | else if ((int) ping_color_type == PNG_COLOR_TYPE_PALETTE) |
| 3318 | (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info, |
| 3319 | IndexQuantum,ping_pixels+row_offset,exception); |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 3320 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3321 | else /* ping_color_type == PNG_COLOR_TYPE_RGB */ |
| 3322 | (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info, |
| 3323 | RGBQuantum,ping_pixels+row_offset,exception); |
glennrp | 3faa9a3 | 2011-04-23 14:00:25 +0000 | [diff] [blame] | 3324 | |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 3325 | if (found_transparent_pixel == MagickFalse) |
| 3326 | { |
| 3327 | /* Is there a transparent pixel in the row? */ |
glennrp | a6a0663 | 2011-01-19 15:15:34 +0000 | [diff] [blame] | 3328 | if (y== 0 && logging != MagickFalse) |
| 3329 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 3330 | " Looking for cheap transparent pixel"); |
| 3331 | |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 3332 | for (x=(ssize_t) image->columns-1; x >= 0; x--) |
| 3333 | { |
glennrp | 5aa37f6 | 2011-01-02 03:07:57 +0000 | [diff] [blame] | 3334 | if ((ping_color_type == PNG_COLOR_TYPE_RGBA || |
| 3335 | ping_color_type == PNG_COLOR_TYPE_GRAY_ALPHA) && |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3336 | (GetPixelAlpha(image,q) != OpaqueAlpha)) |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 3337 | { |
glennrp | a6a0663 | 2011-01-19 15:15:34 +0000 | [diff] [blame] | 3338 | if (logging != MagickFalse) |
| 3339 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 3340 | " ...got one."); |
| 3341 | |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 3342 | found_transparent_pixel = MagickTrue; |
| 3343 | break; |
| 3344 | } |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 3345 | if ((ping_color_type == PNG_COLOR_TYPE_RGB || |
| 3346 | ping_color_type == PNG_COLOR_TYPE_GRAY) && |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3347 | (ScaleQuantumToShort(GetPixelRed(image,q)) == |
| 3348 | transparent_color.red && |
| 3349 | ScaleQuantumToShort(GetPixelGreen(image,q)) == |
| 3350 | transparent_color.green && |
| 3351 | ScaleQuantumToShort(GetPixelBlue(image,q)) == |
| 3352 | transparent_color.blue)) |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 3353 | { |
glennrp | a6a0663 | 2011-01-19 15:15:34 +0000 | [diff] [blame] | 3354 | if (logging != MagickFalse) |
| 3355 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 3356 | " ...got one."); |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 3357 | found_transparent_pixel = MagickTrue; |
| 3358 | break; |
| 3359 | } |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3360 | q+=GetPixelChannels(image); |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 3361 | } |
| 3362 | } |
| 3363 | |
| 3364 | if ((image->previous == (Image *) NULL) && (num_passes == 1)) |
| 3365 | { |
| 3366 | status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y, |
| 3367 | image->rows); |
| 3368 | |
| 3369 | if (status == MagickFalse) |
| 3370 | break; |
| 3371 | } |
| 3372 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 3373 | break; |
| 3374 | } |
| 3375 | |
| 3376 | if ((image->previous == (Image *) NULL) && (num_passes != 1)) |
| 3377 | { |
| 3378 | status=SetImageProgress(image,LoadImageTag,pass,num_passes); |
cristy | 7a287bf | 2010-02-14 02:18:09 +0000 | [diff] [blame] | 3379 | if (status == MagickFalse) |
| 3380 | break; |
| 3381 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3382 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3383 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3384 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3385 | else /* image->storage_class != DirectClass */ |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 3386 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3387 | for (pass=0; pass < num_passes; pass++) |
| 3388 | { |
| 3389 | Quantum |
| 3390 | *quantum_scanline; |
| 3391 | |
| 3392 | register Quantum |
| 3393 | *r; |
| 3394 | |
| 3395 | /* |
| 3396 | Convert grayscale image to PseudoClass pixel packets. |
| 3397 | */ |
glennrp | c17d96f | 2011-06-27 01:20:11 +0000 | [diff] [blame] | 3398 | if (logging != MagickFalse) |
| 3399 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 3400 | " Converting grayscale pixels to pixel packets"); |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3401 | |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 3402 | image->alpha_trait=ping_color_type == PNG_COLOR_TYPE_GRAY_ALPHA ? |
cristy | b0a657e | 2012-08-29 00:45:37 +0000 | [diff] [blame] | 3403 | BlendPixelTrait : UndefinedPixelTrait; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3404 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3405 | quantum_scanline=(Quantum *) AcquireQuantumMemory(image->columns, |
cristy | b0a657e | 2012-08-29 00:45:37 +0000 | [diff] [blame] | 3406 | (image->alpha_trait == BlendPixelTrait? 2 : 1)* |
| 3407 | sizeof(*quantum_scanline)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3408 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3409 | if (quantum_scanline == (Quantum *) NULL) |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 3410 | png_error(ping,"Memory allocation failed"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3411 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3412 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3413 | { |
glennrp | 4f99639 | 2013-01-26 22:56:50 +0000 | [diff] [blame] | 3414 | Quantum |
| 3415 | alpha; |
| 3416 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3417 | if (num_passes > 1) |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 3418 | row_offset=ping_rowbytes*y; |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 3419 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3420 | else |
| 3421 | row_offset=0; |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 3422 | |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 3423 | png_read_row(ping,ping_pixels+row_offset,NULL); |
glennrp | c5c31be | 2012-11-07 18:42:18 +0000 | [diff] [blame] | 3424 | |
| 3425 | if (pass < num_passes-1) |
| 3426 | continue; |
| 3427 | |
glennrp | 4f99639 | 2013-01-26 22:56:50 +0000 | [diff] [blame] | 3428 | q=QueueAuthenticPixels(image,0,y,image->columns,1,exception); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3429 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3430 | if (q == (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3431 | break; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3432 | |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 3433 | p=ping_pixels+row_offset; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3434 | r=quantum_scanline; |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 3435 | |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 3436 | switch (ping_bit_depth) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3437 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3438 | case 8: |
| 3439 | { |
glennrp | 4f99639 | 2013-01-26 22:56:50 +0000 | [diff] [blame] | 3440 | |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 3441 | if (ping_color_type == 4) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3442 | for (x=(ssize_t) image->columns-1; x >= 0; x--) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3443 | { |
glennrp | a18d5bc | 2011-04-23 14:51:34 +0000 | [diff] [blame] | 3444 | *r++=*p++; |
glennrp | 4f99639 | 2013-01-26 22:56:50 +0000 | [diff] [blame] | 3445 | |
| 3446 | alpha=ScaleCharToQuantum((unsigned char)*p++); |
| 3447 | |
| 3448 | SetPixelAlpha(image,alpha,q); |
| 3449 | |
| 3450 | if (alpha != OpaqueAlpha) |
glennrp | 0b206f5 | 2011-01-07 04:55:32 +0000 | [diff] [blame] | 3451 | found_transparent_pixel = MagickTrue; |
glennrp | 4f99639 | 2013-01-26 22:56:50 +0000 | [diff] [blame] | 3452 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3453 | q+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3454 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3455 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3456 | else |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3457 | for (x=(ssize_t) image->columns-1; x >= 0; x--) |
glennrp | a18d5bc | 2011-04-23 14:51:34 +0000 | [diff] [blame] | 3458 | *r++=*p++; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3459 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3460 | break; |
| 3461 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3462 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3463 | case 16: |
| 3464 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3465 | for (x=(ssize_t) image->columns-1; x >= 0; x--) |
glennrp | a18d5bc | 2011-04-23 14:51:34 +0000 | [diff] [blame] | 3466 | { |
glennrp | c17d96f | 2011-06-27 01:20:11 +0000 | [diff] [blame] | 3467 | #if (MAGICKCORE_QUANTUM_DEPTH == 16) || (MAGICKCORE_QUANTUM_DEPTH == 32) |
cristy | 87281ec | 2013-05-08 11:50:14 +0000 | [diff] [blame] | 3468 | unsigned short |
glennrp | 58f77c7 | 2011-04-23 14:09:09 +0000 | [diff] [blame] | 3469 | quantum; |
| 3470 | |
| 3471 | if (image->colors > 256) |
glennrp | c17d96f | 2011-06-27 01:20:11 +0000 | [diff] [blame] | 3472 | quantum=((*p++) << 8); |
glennrp | 58f77c7 | 2011-04-23 14:09:09 +0000 | [diff] [blame] | 3473 | |
| 3474 | else |
glennrp | c17d96f | 2011-06-27 01:20:11 +0000 | [diff] [blame] | 3475 | quantum=0; |
glennrp | 58f77c7 | 2011-04-23 14:09:09 +0000 | [diff] [blame] | 3476 | |
glennrp | 58f77c7 | 2011-04-23 14:09:09 +0000 | [diff] [blame] | 3477 | quantum|=(*p++); |
glennrp | c17d96f | 2011-06-27 01:20:11 +0000 | [diff] [blame] | 3478 | *r=ScaleShortToQuantum(quantum); |
glennrp | 58f77c7 | 2011-04-23 14:09:09 +0000 | [diff] [blame] | 3479 | r++; |
glennrp | 9d0ea4d | 2011-04-22 18:35:57 +0000 | [diff] [blame] | 3480 | |
| 3481 | if (ping_color_type == 4) |
| 3482 | { |
glennrp | c17d96f | 2011-06-27 01:20:11 +0000 | [diff] [blame] | 3483 | if (image->colors > 256) |
| 3484 | quantum=((*p++) << 8); |
| 3485 | else |
| 3486 | quantum=0; |
| 3487 | |
glennrp | 58f77c7 | 2011-04-23 14:09:09 +0000 | [diff] [blame] | 3488 | quantum|=(*p++); |
glennrp | 4f99639 | 2013-01-26 22:56:50 +0000 | [diff] [blame] | 3489 | |
| 3490 | alpha=ScaleShortToQuantum(quantum); |
| 3491 | SetPixelAlpha(image,alpha,q); |
| 3492 | |
| 3493 | if (alpha != OpaqueAlpha) |
glennrp | 58f77c7 | 2011-04-23 14:09:09 +0000 | [diff] [blame] | 3494 | found_transparent_pixel = MagickTrue; |
glennrp | 4f99639 | 2013-01-26 22:56:50 +0000 | [diff] [blame] | 3495 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3496 | q+=GetPixelChannels(image); |
glennrp | 58f77c7 | 2011-04-23 14:09:09 +0000 | [diff] [blame] | 3497 | } |
glennrp | 58f77c7 | 2011-04-23 14:09:09 +0000 | [diff] [blame] | 3498 | |
| 3499 | #else /* MAGICKCORE_QUANTUM_DEPTH == 8 */ |
| 3500 | *r++=(*p++); |
| 3501 | p++; /* strip low byte */ |
| 3502 | |
| 3503 | if (ping_color_type == 4) |
| 3504 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3505 | SetPixelAlpha(image,*p++,q); |
glennrp | 4f99639 | 2013-01-26 22:56:50 +0000 | [diff] [blame] | 3506 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3507 | if (GetPixelAlpha(image,q) != OpaqueAlpha) |
glennrp | 9d0ea4d | 2011-04-22 18:35:57 +0000 | [diff] [blame] | 3508 | found_transparent_pixel = MagickTrue; |
glennrp | 4f99639 | 2013-01-26 22:56:50 +0000 | [diff] [blame] | 3509 | |
glennrp | 9d0ea4d | 2011-04-22 18:35:57 +0000 | [diff] [blame] | 3510 | p++; |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3511 | q+=GetPixelChannels(image); |
glennrp | 9d0ea4d | 2011-04-22 18:35:57 +0000 | [diff] [blame] | 3512 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3513 | #endif |
glennrp | a18d5bc | 2011-04-23 14:51:34 +0000 | [diff] [blame] | 3514 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3515 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3516 | break; |
| 3517 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3518 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3519 | default: |
| 3520 | break; |
| 3521 | } |
glennrp | 3faa9a3 | 2011-04-23 14:00:25 +0000 | [diff] [blame] | 3522 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3523 | /* |
| 3524 | Transfer image scanline. |
| 3525 | */ |
| 3526 | r=quantum_scanline; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3527 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3528 | q=GetAuthenticPixels(image,0,y,image->columns,1,exception); |
| 3529 | |
| 3530 | if (q == (Quantum *) NULL) |
| 3531 | break; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3532 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3533 | { |
| 3534 | SetPixelIndex(image,*r++,q); |
| 3535 | q+=GetPixelChannels(image); |
| 3536 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3537 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3538 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 3539 | break; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3540 | |
cristy | 7a287bf | 2010-02-14 02:18:09 +0000 | [diff] [blame] | 3541 | if ((image->previous == (Image *) NULL) && (num_passes == 1)) |
| 3542 | { |
cristy | cee9711 | 2010-05-28 00:44:52 +0000 | [diff] [blame] | 3543 | status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y, |
cristy | 9fff7b4 | 2011-04-29 01:09:31 +0000 | [diff] [blame] | 3544 | image->rows); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3545 | |
cristy | 7a287bf | 2010-02-14 02:18:09 +0000 | [diff] [blame] | 3546 | if (status == MagickFalse) |
| 3547 | break; |
| 3548 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3549 | } |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 3550 | |
cristy | 7a287bf | 2010-02-14 02:18:09 +0000 | [diff] [blame] | 3551 | if ((image->previous == (Image *) NULL) && (num_passes != 1)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3552 | { |
| 3553 | status=SetImageProgress(image,LoadImageTag,pass,num_passes); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3554 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3555 | if (status == MagickFalse) |
| 3556 | break; |
| 3557 | } |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 3558 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3559 | quantum_scanline=(Quantum *) RelinquishMagickMemory(quantum_scanline); |
| 3560 | } |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 3561 | |
cristy | b0a657e | 2012-08-29 00:45:37 +0000 | [diff] [blame] | 3562 | image->alpha_trait=found_transparent_pixel ? BlendPixelTrait : |
| 3563 | UndefinedPixelTrait; |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 3564 | |
| 3565 | if (logging != MagickFalse) |
| 3566 | { |
| 3567 | if (found_transparent_pixel != MagickFalse) |
| 3568 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 3569 | " Found transparent pixel"); |
| 3570 | else |
glennrp | 5aa37f6 | 2011-01-02 03:07:57 +0000 | [diff] [blame] | 3571 | { |
| 3572 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 3573 | " No transparent pixel was found"); |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 3574 | |
glennrp | 5aa37f6 | 2011-01-02 03:07:57 +0000 | [diff] [blame] | 3575 | ping_color_type&=0x03; |
| 3576 | } |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 3577 | } |
| 3578 | } |
| 3579 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3580 | if (quantum_info != (QuantumInfo *) NULL) |
| 3581 | quantum_info=DestroyQuantumInfo(quantum_info); |
| 3582 | |
cristy | 5c6f789 | 2010-05-05 22:53:29 +0000 | [diff] [blame] | 3583 | if (image->storage_class == PseudoClass) |
| 3584 | { |
cristy | b0a657e | 2012-08-29 00:45:37 +0000 | [diff] [blame] | 3585 | PixelTrait |
| 3586 | alpha_trait; |
cristy | 5c6f789 | 2010-05-05 22:53:29 +0000 | [diff] [blame] | 3587 | |
cristy | b0a657e | 2012-08-29 00:45:37 +0000 | [diff] [blame] | 3588 | alpha_trait=image->alpha_trait; |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 3589 | image->alpha_trait=UndefinedPixelTrait; |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3590 | (void) SyncImage(image,exception); |
cristy | b0a657e | 2012-08-29 00:45:37 +0000 | [diff] [blame] | 3591 | image->alpha_trait=alpha_trait; |
cristy | 5c6f789 | 2010-05-05 22:53:29 +0000 | [diff] [blame] | 3592 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3593 | |
glennrp | 4eb3931 | 2011-03-30 21:34:55 +0000 | [diff] [blame] | 3594 | png_read_end(ping,end_info); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3595 | |
| 3596 | if (image_info->number_scenes != 0 && mng_info->scenes_found-1 < |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3597 | (ssize_t) image_info->first_scene && image->delay != 0) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3598 | { |
| 3599 | png_destroy_read_struct(&ping,&ping_info,&end_info); |
cristy | 0997332 | 2013-06-30 21:06:30 +0000 | [diff] [blame] | 3600 | pixel_info=RelinquishVirtualMemory(pixel_info); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3601 | image->colors=2; |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3602 | (void) SetImageBackgroundColor(image,exception); |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 3603 | #ifdef PNG_SETJMP_NOT_THREAD_SAFE |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 3604 | UnlockSemaphoreInfo(ping_semaphore); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3605 | #endif |
| 3606 | if (logging != MagickFalse) |
| 3607 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 3608 | " exit ReadOnePNGImage() early."); |
| 3609 | return(image); |
| 3610 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3611 | |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 3612 | if (png_get_valid(ping,ping_info,PNG_INFO_tRNS)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3613 | { |
| 3614 | ClassType |
| 3615 | storage_class; |
| 3616 | |
| 3617 | /* |
| 3618 | Image has a transparent background. |
| 3619 | */ |
| 3620 | storage_class=image->storage_class; |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 3621 | image->alpha_trait=BlendPixelTrait; |
glennrp | c11cf6a | 2010-03-20 16:46:19 +0000 | [diff] [blame] | 3622 | |
glennrp | 3c21811 | 2010-11-27 15:31:26 +0000 | [diff] [blame] | 3623 | /* Balfour fix from imagemagick discourse server, 5 Feb 2010 */ |
glennrp | c11cf6a | 2010-03-20 16:46:19 +0000 | [diff] [blame] | 3624 | |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3625 | if (storage_class == PseudoClass) |
| 3626 | { |
| 3627 | if ((int) ping_color_type == PNG_COLOR_TYPE_PALETTE) |
glennrp | c11cf6a | 2010-03-20 16:46:19 +0000 | [diff] [blame] | 3628 | { |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3629 | for (x=0; x < ping_num_trans; x++) |
| 3630 | { |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 3631 | image->colormap[x].alpha_trait=BlendPixelTrait; |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3632 | image->colormap[x].alpha = |
| 3633 | ScaleCharToQuantum((unsigned char)ping_trans_alpha[x]); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3634 | } |
glennrp | c11cf6a | 2010-03-20 16:46:19 +0000 | [diff] [blame] | 3635 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3636 | |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3637 | else if (ping_color_type == PNG_COLOR_TYPE_GRAY) |
| 3638 | { |
| 3639 | for (x=0; x < (int) image->colors; x++) |
| 3640 | { |
| 3641 | if (ScaleQuantumToShort(image->colormap[x].red) == |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3642 | transparent_color.alpha) |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3643 | { |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 3644 | image->colormap[x].alpha_trait=BlendPixelTrait; |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3645 | image->colormap[x].alpha = (Quantum) TransparentAlpha; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3646 | } |
| 3647 | } |
| 3648 | } |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3649 | (void) SyncImage(image,exception); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3650 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3651 | |
glennrp | a6a0663 | 2011-01-19 15:15:34 +0000 | [diff] [blame] | 3652 | #if 1 /* Should have already been done above, but glennrp problem P10 |
| 3653 | * needs this. |
| 3654 | */ |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3655 | else |
| 3656 | { |
| 3657 | for (y=0; y < (ssize_t) image->rows; y++) |
glennrp | c11cf6a | 2010-03-20 16:46:19 +0000 | [diff] [blame] | 3658 | { |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3659 | image->storage_class=storage_class; |
| 3660 | q=GetAuthenticPixels(image,0,y,image->columns,1,exception); |
| 3661 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3662 | if (q == (Quantum *) NULL) |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3663 | break; |
| 3664 | |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3665 | |
glennrp | a6a0663 | 2011-01-19 15:15:34 +0000 | [diff] [blame] | 3666 | /* Caution: on a Q8 build, this does not distinguish between |
| 3667 | * 16-bit colors that differ only in the low byte |
| 3668 | */ |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3669 | for (x=(ssize_t) image->columns-1; x >= 0; x--) |
| 3670 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3671 | if (ScaleQuantumToShort(GetPixelRed(image,q)) == |
| 3672 | transparent_color.red && |
| 3673 | ScaleQuantumToShort(GetPixelGreen(image,q)) == |
| 3674 | transparent_color.green && |
| 3675 | ScaleQuantumToShort(GetPixelBlue(image,q)) == |
| 3676 | transparent_color.blue) |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 3677 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3678 | SetPixelAlpha(image,TransparentAlpha,q); |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 3679 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3680 | |
glennrp | 67b9c1a | 2011-04-22 18:47:36 +0000 | [diff] [blame] | 3681 | #if 0 /* I have not found a case where this is needed. */ |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3682 | else |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 3683 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3684 | SetPixelAlpha(image,q)=(Quantum) OpaqueAlpha; |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 3685 | } |
glennrp | a6a0663 | 2011-01-19 15:15:34 +0000 | [diff] [blame] | 3686 | #endif |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3687 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3688 | q+=GetPixelChannels(image); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3689 | } |
| 3690 | |
| 3691 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 3692 | break; |
glennrp | c11cf6a | 2010-03-20 16:46:19 +0000 | [diff] [blame] | 3693 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3694 | } |
glennrp | a6a0663 | 2011-01-19 15:15:34 +0000 | [diff] [blame] | 3695 | #endif |
glennrp | c11cf6a | 2010-03-20 16:46:19 +0000 | [diff] [blame] | 3696 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3697 | image->storage_class=DirectClass; |
| 3698 | } |
glennrp | 3c21811 | 2010-11-27 15:31:26 +0000 | [diff] [blame] | 3699 | |
cristy | eb3b22a | 2011-03-31 20:16:11 +0000 | [diff] [blame] | 3700 | for (j = 0; j < 2; j++) |
glennrp | 4eb3931 | 2011-03-30 21:34:55 +0000 | [diff] [blame] | 3701 | { |
| 3702 | if (j == 0) |
glennrp | a0ed009 | 2011-04-18 16:36:29 +0000 | [diff] [blame] | 3703 | status = png_get_text(ping,ping_info,&text,&num_text) != 0 ? |
| 3704 | MagickTrue : MagickFalse; |
glennrp | 4eb3931 | 2011-03-30 21:34:55 +0000 | [diff] [blame] | 3705 | else |
glennrp | a0ed009 | 2011-04-18 16:36:29 +0000 | [diff] [blame] | 3706 | status = png_get_text(ping,end_info,&text,&num_text) != 0 ? |
| 3707 | MagickTrue : MagickFalse; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3708 | |
glennrp | 4eb3931 | 2011-03-30 21:34:55 +0000 | [diff] [blame] | 3709 | if (status != MagickFalse) |
| 3710 | for (i=0; i < (ssize_t) num_text; i++) |
| 3711 | { |
| 3712 | /* Check for a profile */ |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3713 | |
glennrp | 4eb3931 | 2011-03-30 21:34:55 +0000 | [diff] [blame] | 3714 | if (logging != MagickFalse) |
| 3715 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 3716 | " Reading PNG text chunk"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3717 | |
glennrp | 4eb3931 | 2011-03-30 21:34:55 +0000 | [diff] [blame] | 3718 | if (memcmp(text[i].key, "Raw profile type ",17) == 0) |
glennrp | 97f90e2 | 2011-02-22 05:47:58 +0000 | [diff] [blame] | 3719 | { |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 3720 | (void) Magick_png_read_raw_profile(ping,image,image_info,text, |
| 3721 | (int) i,exception); |
glennrp | 4eb3931 | 2011-03-30 21:34:55 +0000 | [diff] [blame] | 3722 | num_raw_profiles++; |
glennrp | 97f90e2 | 2011-02-22 05:47:58 +0000 | [diff] [blame] | 3723 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3724 | |
glennrp | 4eb3931 | 2011-03-30 21:34:55 +0000 | [diff] [blame] | 3725 | else |
| 3726 | { |
| 3727 | char |
| 3728 | *value; |
| 3729 | |
| 3730 | length=text[i].text_length; |
| 3731 | value=(char *) AcquireQuantumMemory(length+MaxTextExtent, |
| 3732 | sizeof(*value)); |
| 3733 | if (value == (char *) NULL) |
| 3734 | { |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 3735 | png_error(ping,"Memory allocation failed"); |
glennrp | 4eb3931 | 2011-03-30 21:34:55 +0000 | [diff] [blame] | 3736 | break; |
| 3737 | } |
| 3738 | *value='\0'; |
| 3739 | (void) ConcatenateMagickString(value,text[i].text,length+2); |
| 3740 | |
| 3741 | /* Don't save "density" or "units" property if we have a pHYs |
| 3742 | * chunk |
| 3743 | */ |
| 3744 | if (!png_get_valid(ping,ping_info,PNG_INFO_pHYs) || |
| 3745 | (LocaleCompare(text[i].key,"density") != 0 && |
| 3746 | LocaleCompare(text[i].key,"units") != 0)) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3747 | (void) SetImageProperty(image,text[i].key,value,exception); |
glennrp | 4eb3931 | 2011-03-30 21:34:55 +0000 | [diff] [blame] | 3748 | |
| 3749 | if (logging != MagickFalse) |
| 3750 | { |
| 3751 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 3752 | " length: %lu",(unsigned long) length); |
| 3753 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 3754 | " Keyword: %s",text[i].key); |
| 3755 | } |
| 3756 | |
| 3757 | value=DestroyString(value); |
| 3758 | } |
| 3759 | } |
| 3760 | num_text_total += num_text; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3761 | } |
glennrp | 3c21811 | 2010-11-27 15:31:26 +0000 | [diff] [blame] | 3762 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3763 | #ifdef MNG_OBJECT_BUFFERS |
| 3764 | /* |
| 3765 | Store the object if necessary. |
| 3766 | */ |
| 3767 | if (object_id && !mng_info->frozen[object_id]) |
| 3768 | { |
| 3769 | if (mng_info->ob[object_id] == (MngBuffer *) NULL) |
| 3770 | { |
| 3771 | /* |
| 3772 | create a new object buffer. |
| 3773 | */ |
| 3774 | mng_info->ob[object_id]=(MngBuffer *) |
cristy | 73bd4a5 | 2010-10-05 11:24:23 +0000 | [diff] [blame] | 3775 | AcquireMagickMemory(sizeof(MngBuffer)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3776 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3777 | if (mng_info->ob[object_id] != (MngBuffer *) NULL) |
| 3778 | { |
| 3779 | mng_info->ob[object_id]->image=(Image *) NULL; |
| 3780 | mng_info->ob[object_id]->reference_count=1; |
| 3781 | } |
| 3782 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3783 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3784 | if ((mng_info->ob[object_id] == (MngBuffer *) NULL) || |
| 3785 | mng_info->ob[object_id]->frozen) |
| 3786 | { |
| 3787 | if (mng_info->ob[object_id] == (MngBuffer *) NULL) |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 3788 | png_error(ping,"Memory allocation failed"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3789 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3790 | if (mng_info->ob[object_id]->frozen) |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 3791 | png_error(ping,"Cannot overwrite frozen MNG object buffer"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3792 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3793 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3794 | else |
| 3795 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3796 | |
| 3797 | if (mng_info->ob[object_id]->image != (Image *) NULL) |
| 3798 | mng_info->ob[object_id]->image=DestroyImage |
| 3799 | (mng_info->ob[object_id]->image); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3800 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3801 | mng_info->ob[object_id]->image=CloneImage(image,0,0,MagickTrue, |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3802 | exception); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3803 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3804 | if (mng_info->ob[object_id]->image != (Image *) NULL) |
| 3805 | mng_info->ob[object_id]->image->file=(FILE *) NULL; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3806 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3807 | else |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 3808 | png_error(ping, "Cloning image for object buffer failed"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3809 | |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 3810 | if (ping_width > 250000L || ping_height > 250000L) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3811 | png_error(ping,"PNG Image dimensions are too large."); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3812 | |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 3813 | mng_info->ob[object_id]->width=ping_width; |
| 3814 | mng_info->ob[object_id]->height=ping_height; |
| 3815 | mng_info->ob[object_id]->color_type=ping_color_type; |
| 3816 | mng_info->ob[object_id]->sample_depth=ping_bit_depth; |
| 3817 | mng_info->ob[object_id]->interlace_method=ping_interlace_method; |
| 3818 | mng_info->ob[object_id]->compression_method= |
| 3819 | ping_compression_method; |
| 3820 | mng_info->ob[object_id]->filter_method=ping_filter_method; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3821 | |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 3822 | if (png_get_valid(ping,ping_info,PNG_INFO_PLTE)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3823 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3824 | png_colorp |
| 3825 | plte; |
| 3826 | |
| 3827 | /* |
| 3828 | Copy the PLTE to the object buffer. |
| 3829 | */ |
| 3830 | png_get_PLTE(ping,ping_info,&plte,&number_colors); |
| 3831 | mng_info->ob[object_id]->plte_length=number_colors; |
glennrp | 3c21811 | 2010-11-27 15:31:26 +0000 | [diff] [blame] | 3832 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3833 | for (i=0; i < number_colors; i++) |
| 3834 | { |
| 3835 | mng_info->ob[object_id]->plte[i]=plte[i]; |
| 3836 | } |
| 3837 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3838 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3839 | else |
| 3840 | mng_info->ob[object_id]->plte_length=0; |
| 3841 | } |
| 3842 | } |
| 3843 | #endif |
glennrp | 0a55b4c | 2011-03-23 12:38:38 +0000 | [diff] [blame] | 3844 | |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 3845 | /* Set image->alpha_trait to MagickTrue if the input colortype supports |
glennrp | 0a55b4c | 2011-03-23 12:38:38 +0000 | [diff] [blame] | 3846 | * alpha or if a valid tRNS chunk is present, no matter whether there |
| 3847 | * is actual transparency present. |
| 3848 | */ |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 3849 | image->alpha_trait=(((int) ping_color_type == PNG_COLOR_TYPE_RGB_ALPHA) || |
glennrp | 0a55b4c | 2011-03-23 12:38:38 +0000 | [diff] [blame] | 3850 | ((int) ping_color_type == PNG_COLOR_TYPE_GRAY_ALPHA) || |
| 3851 | (png_get_valid(ping,ping_info,PNG_INFO_tRNS))) ? |
cristy | b0a657e | 2012-08-29 00:45:37 +0000 | [diff] [blame] | 3852 | BlendPixelTrait : UndefinedPixelTrait; |
glennrp | 0a55b4c | 2011-03-23 12:38:38 +0000 | [diff] [blame] | 3853 | |
glennrp | 224ad8d | 2013-02-01 17:04:04 +0000 | [diff] [blame] | 3854 | #if 0 /* I'm not sure what's wrong here but it does not work. */ |
glennrp | 5830fbc | 2013-01-27 06:11:45 +0000 | [diff] [blame] | 3855 | if (image->alpha_trait == BlendPixelTrait) |
| 3856 | { |
| 3857 | if (ping_color_type == PNG_COLOR_TYPE_GRAY_ALPHA) |
| 3858 | (void) SetImageType(image,GrayscaleMatteType,exception); |
| 3859 | |
| 3860 | else if (ping_color_type == PNG_COLOR_TYPE_PALETTE) |
| 3861 | (void) SetImageType(image,PaletteMatteType,exception); |
| 3862 | |
| 3863 | else |
| 3864 | (void) SetImageType(image,TrueColorMatteType,exception); |
| 3865 | } |
| 3866 | |
| 3867 | else |
| 3868 | { |
| 3869 | if (ping_color_type == PNG_COLOR_TYPE_GRAY) |
| 3870 | (void) SetImageType(image,GrayscaleType,exception); |
| 3871 | |
| 3872 | else if (ping_color_type == PNG_COLOR_TYPE_PALETTE) |
| 3873 | (void) SetImageType(image,PaletteType,exception); |
| 3874 | |
| 3875 | else |
| 3876 | (void) SetImageType(image,TrueColorType,exception); |
| 3877 | } |
glennrp | 224ad8d | 2013-02-01 17:04:04 +0000 | [diff] [blame] | 3878 | #endif |
glennrp | 5830fbc | 2013-01-27 06:11:45 +0000 | [diff] [blame] | 3879 | |
glennrp | cb395ac | 2011-03-30 19:50:23 +0000 | [diff] [blame] | 3880 | /* Set more properties for identify to retrieve */ |
| 3881 | { |
| 3882 | char |
| 3883 | msg[MaxTextExtent]; |
| 3884 | |
glennrp | 4eb3931 | 2011-03-30 21:34:55 +0000 | [diff] [blame] | 3885 | if (num_text_total != 0) |
glennrp | cb395ac | 2011-03-30 19:50:23 +0000 | [diff] [blame] | 3886 | { |
| 3887 | /* libpng doesn't tell us whether they were tEXt, zTXt, or iTXt */ |
cristy | 3b6fd2e | 2011-05-20 12:53:50 +0000 | [diff] [blame] | 3888 | (void) FormatLocaleString(msg,MaxTextExtent, |
glennrp | 613276d | 2011-03-30 21:46:49 +0000 | [diff] [blame] | 3889 | "%d tEXt/zTXt/iTXt chunks were found", num_text_total); |
glennrp | 3398b5b | 2013-05-03 23:10:31 +0000 | [diff] [blame] | 3890 | (void) SetImageProperty(image,"png:text",msg, |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3891 | exception); |
glennrp | cb395ac | 2011-03-30 19:50:23 +0000 | [diff] [blame] | 3892 | } |
| 3893 | |
| 3894 | if (num_raw_profiles != 0) |
| 3895 | { |
cristy | 3b6fd2e | 2011-05-20 12:53:50 +0000 | [diff] [blame] | 3896 | (void) FormatLocaleString(msg,MaxTextExtent, |
glennrp | cb395ac | 2011-03-30 19:50:23 +0000 | [diff] [blame] | 3897 | "%d were found", num_raw_profiles); |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3898 | (void) SetImageProperty(image,"png:text-encoded profiles",msg, |
| 3899 | exception); |
glennrp | cb395ac | 2011-03-30 19:50:23 +0000 | [diff] [blame] | 3900 | } |
| 3901 | |
glennrp | 98b83d4 | 2012-07-23 02:50:31 +0000 | [diff] [blame] | 3902 | if (ping_found_cHRM != MagickFalse) |
glennrp | 5961225 | 2011-03-30 21:45:21 +0000 | [diff] [blame] | 3903 | { |
cristy | 3b6fd2e | 2011-05-20 12:53:50 +0000 | [diff] [blame] | 3904 | (void) FormatLocaleString(msg,MaxTextExtent,"%s", |
glennrp | 5961225 | 2011-03-30 21:45:21 +0000 | [diff] [blame] | 3905 | "chunk was found (see Chromaticity, above)"); |
glennrp | 3398b5b | 2013-05-03 23:10:31 +0000 | [diff] [blame] | 3906 | (void) SetImageProperty(image,"png:cHRM",msg, |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3907 | exception); |
glennrp | 5961225 | 2011-03-30 21:45:21 +0000 | [diff] [blame] | 3908 | } |
glennrp | cb395ac | 2011-03-30 19:50:23 +0000 | [diff] [blame] | 3909 | |
| 3910 | if (png_get_valid(ping,ping_info,PNG_INFO_bKGD)) |
glennrp | 5961225 | 2011-03-30 21:45:21 +0000 | [diff] [blame] | 3911 | { |
cristy | 3b6fd2e | 2011-05-20 12:53:50 +0000 | [diff] [blame] | 3912 | (void) FormatLocaleString(msg,MaxTextExtent,"%s", |
glennrp | 5961225 | 2011-03-30 21:45:21 +0000 | [diff] [blame] | 3913 | "chunk was found (see Background color, above)"); |
glennrp | 3398b5b | 2013-05-03 23:10:31 +0000 | [diff] [blame] | 3914 | (void) SetImageProperty(image,"png:bKGD",msg, |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3915 | exception); |
glennrp | 5961225 | 2011-03-30 21:45:21 +0000 | [diff] [blame] | 3916 | } |
| 3917 | |
cristy | 3b6fd2e | 2011-05-20 12:53:50 +0000 | [diff] [blame] | 3918 | (void) FormatLocaleString(msg,MaxTextExtent,"%s", |
glennrp | 5961225 | 2011-03-30 21:45:21 +0000 | [diff] [blame] | 3919 | "chunk was found"); |
glennrp | cb395ac | 2011-03-30 19:50:23 +0000 | [diff] [blame] | 3920 | |
glennrp | 98b83d4 | 2012-07-23 02:50:31 +0000 | [diff] [blame] | 3921 | #if defined(PNG_iCCP_SUPPORTED) |
| 3922 | if (ping_found_iCCP != MagickFalse) |
glennrp | 3398b5b | 2013-05-03 23:10:31 +0000 | [diff] [blame] | 3923 | (void) SetImageProperty(image,"png:iCCP",msg, |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3924 | exception); |
glennrp | 98b83d4 | 2012-07-23 02:50:31 +0000 | [diff] [blame] | 3925 | #endif |
glennrp | cb395ac | 2011-03-30 19:50:23 +0000 | [diff] [blame] | 3926 | |
glennrp | cb395ac | 2011-03-30 19:50:23 +0000 | [diff] [blame] | 3927 | if (png_get_valid(ping,ping_info,PNG_INFO_tRNS)) |
glennrp | 3398b5b | 2013-05-03 23:10:31 +0000 | [diff] [blame] | 3928 | (void) SetImageProperty(image,"png:tRNS",msg, |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3929 | exception); |
glennrp | 4eb3931 | 2011-03-30 21:34:55 +0000 | [diff] [blame] | 3930 | |
| 3931 | #if defined(PNG_sRGB_SUPPORTED) |
glennrp | 98b83d4 | 2012-07-23 02:50:31 +0000 | [diff] [blame] | 3932 | if (ping_found_sRGB != MagickFalse) |
glennrp | 4eb3931 | 2011-03-30 21:34:55 +0000 | [diff] [blame] | 3933 | { |
cristy | 3b6fd2e | 2011-05-20 12:53:50 +0000 | [diff] [blame] | 3934 | (void) FormatLocaleString(msg,MaxTextExtent, |
glennrp | 98b83d4 | 2012-07-23 02:50:31 +0000 | [diff] [blame] | 3935 | "intent=%d (%s)", |
| 3936 | (int) intent, |
| 3937 | Magick_RenderingIntentString_from_PNG_RenderingIntent(intent)); |
glennrp | 3398b5b | 2013-05-03 23:10:31 +0000 | [diff] [blame] | 3938 | (void) SetImageProperty(image,"png:sRGB",msg, |
glennrp | 98b83d4 | 2012-07-23 02:50:31 +0000 | [diff] [blame] | 3939 | exception); |
glennrp | 4eb3931 | 2011-03-30 21:34:55 +0000 | [diff] [blame] | 3940 | } |
| 3941 | #endif |
| 3942 | |
glennrp | 98b83d4 | 2012-07-23 02:50:31 +0000 | [diff] [blame] | 3943 | if (ping_found_gAMA != MagickFalse) |
glennrp | 4eb3931 | 2011-03-30 21:34:55 +0000 | [diff] [blame] | 3944 | { |
cristy | 3b6fd2e | 2011-05-20 12:53:50 +0000 | [diff] [blame] | 3945 | (void) FormatLocaleString(msg,MaxTextExtent, |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3946 | "gamma=%.8g (See Gamma, above)", |
| 3947 | file_gamma); |
glennrp | 3398b5b | 2013-05-03 23:10:31 +0000 | [diff] [blame] | 3948 | (void) SetImageProperty(image,"png:gAMA",msg, |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3949 | exception); |
glennrp | 4eb3931 | 2011-03-30 21:34:55 +0000 | [diff] [blame] | 3950 | } |
| 3951 | |
| 3952 | #if defined(PNG_pHYs_SUPPORTED) |
| 3953 | if (png_get_valid(ping,ping_info,PNG_INFO_pHYs)) |
| 3954 | { |
cristy | 3b6fd2e | 2011-05-20 12:53:50 +0000 | [diff] [blame] | 3955 | (void) FormatLocaleString(msg,MaxTextExtent, |
glennrp | 07523c7 | 2011-03-31 18:12:10 +0000 | [diff] [blame] | 3956 | "x_res=%.10g, y_res=%.10g, units=%d", |
glennrp | 4eb3931 | 2011-03-30 21:34:55 +0000 | [diff] [blame] | 3957 | (double) x_resolution,(double) y_resolution, unit_type); |
glennrp | 3398b5b | 2013-05-03 23:10:31 +0000 | [diff] [blame] | 3958 | (void) SetImageProperty(image,"png:pHYs",msg, |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3959 | exception); |
glennrp | 4eb3931 | 2011-03-30 21:34:55 +0000 | [diff] [blame] | 3960 | } |
| 3961 | #endif |
| 3962 | |
| 3963 | #if defined(PNG_oFFs_SUPPORTED) |
| 3964 | if (png_get_valid(ping,ping_info,PNG_INFO_oFFs)) |
| 3965 | { |
cristy | 3b6fd2e | 2011-05-20 12:53:50 +0000 | [diff] [blame] | 3966 | (void) FormatLocaleString(msg,MaxTextExtent,"x_off=%.20g, y_off=%.20g", |
glennrp | 4eb3931 | 2011-03-30 21:34:55 +0000 | [diff] [blame] | 3967 | (double) image->page.x,(double) image->page.y); |
glennrp | 3398b5b | 2013-05-03 23:10:31 +0000 | [diff] [blame] | 3968 | (void) SetImageProperty(image,"png:oFFs",msg, |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3969 | exception); |
glennrp | 4eb3931 | 2011-03-30 21:34:55 +0000 | [diff] [blame] | 3970 | } |
| 3971 | #endif |
| 3972 | |
glennrp | 07523c7 | 2011-03-31 18:12:10 +0000 | [diff] [blame] | 3973 | if ((image->page.width != 0 && image->page.width != image->columns) || |
| 3974 | (image->page.height != 0 && image->page.height != image->rows)) |
| 3975 | { |
cristy | 3b6fd2e | 2011-05-20 12:53:50 +0000 | [diff] [blame] | 3976 | (void) FormatLocaleString(msg,MaxTextExtent, |
glennrp | 07523c7 | 2011-03-31 18:12:10 +0000 | [diff] [blame] | 3977 | "width=%.20g, height=%.20g", |
| 3978 | (double) image->page.width,(double) image->page.height); |
glennrp | 3398b5b | 2013-05-03 23:10:31 +0000 | [diff] [blame] | 3979 | (void) SetImageProperty(image,"png:vpAg",msg, |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3980 | exception); |
glennrp | 07523c7 | 2011-03-31 18:12:10 +0000 | [diff] [blame] | 3981 | } |
glennrp | cb395ac | 2011-03-30 19:50:23 +0000 | [diff] [blame] | 3982 | } |
| 3983 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3984 | /* |
| 3985 | Relinquish resources. |
| 3986 | */ |
| 3987 | png_destroy_read_struct(&ping,&ping_info,&end_info); |
| 3988 | |
cristy | 0997332 | 2013-06-30 21:06:30 +0000 | [diff] [blame] | 3989 | pixel_info=RelinquishVirtualMemory(pixel_info); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3990 | |
| 3991 | if (logging != MagickFalse) |
| 3992 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 3993 | " exit ReadOnePNGImage()"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3994 | |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 3995 | #ifdef PNG_SETJMP_NOT_THREAD_SAFE |
| 3996 | UnlockSemaphoreInfo(ping_semaphore); |
| 3997 | #endif |
| 3998 | |
| 3999 | /* } for navigation to beginning of SETJMP-protected block, revert to |
| 4000 | * Throwing an Exception when an error occurs. |
| 4001 | */ |
| 4002 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4003 | return(image); |
| 4004 | |
| 4005 | /* end of reading one PNG image */ |
| 4006 | } |
| 4007 | |
| 4008 | static Image *ReadPNGImage(const ImageInfo *image_info,ExceptionInfo *exception) |
| 4009 | { |
| 4010 | Image |
| 4011 | *image, |
| 4012 | *previous; |
| 4013 | |
| 4014 | MagickBooleanType |
glennrp | 21f0e62 | 2011-01-07 16:20:57 +0000 | [diff] [blame] | 4015 | have_mng_structure, |
| 4016 | logging, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4017 | status; |
| 4018 | |
| 4019 | MngInfo |
| 4020 | *mng_info; |
| 4021 | |
| 4022 | char |
| 4023 | magic_number[MaxTextExtent]; |
| 4024 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4025 | ssize_t |
| 4026 | count; |
| 4027 | |
| 4028 | /* |
| 4029 | Open image file. |
| 4030 | */ |
| 4031 | assert(image_info != (const ImageInfo *) NULL); |
| 4032 | assert(image_info->signature == MagickSignature); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4033 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4034 | if (image_info->debug != MagickFalse) |
| 4035 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s", |
| 4036 | image_info->filename); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4037 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4038 | assert(exception != (ExceptionInfo *) NULL); |
| 4039 | assert(exception->signature == MagickSignature); |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 4040 | logging=LogMagickEvent(CoderEvent,GetMagickModule(),"Enter ReadPNGImage()"); |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 4041 | image=AcquireImage(image_info,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4042 | mng_info=(MngInfo *) NULL; |
| 4043 | status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4044 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4045 | if (status == MagickFalse) |
| 4046 | ThrowReaderException(FileOpenError,"UnableToOpenFile"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4047 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4048 | /* |
| 4049 | Verify PNG signature. |
| 4050 | */ |
| 4051 | count=ReadBlob(image,8,(unsigned char *) magic_number); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4052 | |
glennrp | dde35db | 2011-02-21 12:06:32 +0000 | [diff] [blame] | 4053 | if (count < 8 || memcmp(magic_number,"\211PNG\r\n\032\n",8) != 0) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4054 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4055 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4056 | /* |
| 4057 | Allocate a MngInfo structure. |
| 4058 | */ |
| 4059 | have_mng_structure=MagickFalse; |
cristy | 73bd4a5 | 2010-10-05 11:24:23 +0000 | [diff] [blame] | 4060 | mng_info=(MngInfo *) AcquireMagickMemory(sizeof(MngInfo)); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4061 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4062 | if (mng_info == (MngInfo *) NULL) |
| 4063 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4064 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4065 | /* |
| 4066 | Initialize members of the MngInfo structure. |
| 4067 | */ |
| 4068 | (void) ResetMagickMemory(mng_info,0,sizeof(MngInfo)); |
| 4069 | mng_info->image=image; |
| 4070 | have_mng_structure=MagickTrue; |
| 4071 | |
| 4072 | previous=image; |
| 4073 | image=ReadOnePNGImage(mng_info,image_info,exception); |
| 4074 | MngInfoFreeStruct(mng_info,&have_mng_structure); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4075 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4076 | if (image == (Image *) NULL) |
| 4077 | { |
| 4078 | if (previous != (Image *) NULL) |
| 4079 | { |
| 4080 | if (previous->signature != MagickSignature) |
| 4081 | ThrowReaderException(CorruptImageError,"CorruptImage"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4082 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4083 | (void) CloseBlob(previous); |
| 4084 | (void) DestroyImageList(previous); |
| 4085 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4086 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4087 | if (logging != MagickFalse) |
| 4088 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 4089 | "exit ReadPNGImage() with error"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4090 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4091 | return((Image *) NULL); |
| 4092 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4093 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4094 | (void) CloseBlob(image); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4095 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4096 | if ((image->columns == 0) || (image->rows == 0)) |
| 4097 | { |
| 4098 | if (logging != MagickFalse) |
| 4099 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 4100 | "exit ReadPNGImage() with error."); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4101 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4102 | ThrowReaderException(CorruptImageError,"CorruptImage"); |
| 4103 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4104 | |
cristy | 72715f5 | 2012-06-26 17:55:16 +0000 | [diff] [blame] | 4105 | if ((IssRGBColorspace(image->colorspace) != MagickFalse) && |
glennrp | 3d62786 | 2013-02-26 00:19:34 +0000 | [diff] [blame] | 4106 | ((image->gamma < .45) || (image->gamma > .46)) && |
| 4107 | !(image->chromaticity.red_primary.x>0.6399f && |
| 4108 | image->chromaticity.red_primary.x<0.6401f && |
| 4109 | image->chromaticity.red_primary.y>0.3299f && |
| 4110 | image->chromaticity.red_primary.y<0.3301f && |
| 4111 | image->chromaticity.green_primary.x>0.2999f && |
| 4112 | image->chromaticity.green_primary.x<0.3001f && |
| 4113 | image->chromaticity.green_primary.y>0.5999f && |
| 4114 | image->chromaticity.green_primary.y<0.6001f && |
| 4115 | image->chromaticity.blue_primary.x>0.1499f && |
| 4116 | image->chromaticity.blue_primary.x<0.1501f && |
| 4117 | image->chromaticity.blue_primary.y>0.0599f && |
| 4118 | image->chromaticity.blue_primary.y<0.0601f && |
| 4119 | image->chromaticity.white_point.x>0.3126f && |
| 4120 | image->chromaticity.white_point.x<0.3128f && |
| 4121 | image->chromaticity.white_point.y>0.3289f && |
| 4122 | image->chromaticity.white_point.y<0.3291f)) |
cristy | 72715f5 | 2012-06-26 17:55:16 +0000 | [diff] [blame] | 4123 | SetImageColorspace(image,RGBColorspace,exception); |
| 4124 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4125 | if (logging != MagickFalse) |
glennrp | 97f90e2 | 2011-02-22 05:47:58 +0000 | [diff] [blame] | 4126 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 4127 | " page.w: %.20g, page.h: %.20g,page.x: %.20g, page.y: %.20g.", |
| 4128 | (double) image->page.width,(double) image->page.height, |
| 4129 | (double) image->page.x,(double) image->page.y); |
| 4130 | |
| 4131 | if (logging != MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4132 | (void) LogMagickEvent(CoderEvent,GetMagickModule(),"exit ReadPNGImage()"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4133 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4134 | return(image); |
| 4135 | } |
| 4136 | |
| 4137 | |
| 4138 | |
| 4139 | #if defined(JNG_SUPPORTED) |
| 4140 | /* |
| 4141 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 4142 | % % |
| 4143 | % % |
| 4144 | % % |
| 4145 | % R e a d O n e J N G I m a g e % |
| 4146 | % % |
| 4147 | % % |
| 4148 | % % |
| 4149 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 4150 | % |
| 4151 | % ReadOneJNGImage() reads a JPEG Network Graphics (JNG) image file |
| 4152 | % (minus the 8-byte signature) and returns it. It allocates the memory |
| 4153 | % necessary for the new Image structure and returns a pointer to the new |
| 4154 | % image. |
| 4155 | % |
| 4156 | % JNG support written by Glenn Randers-Pehrson, glennrp@image... |
| 4157 | % |
| 4158 | % The format of the ReadOneJNGImage method is: |
| 4159 | % |
| 4160 | % Image *ReadOneJNGImage(MngInfo *mng_info, const ImageInfo *image_info, |
| 4161 | % ExceptionInfo *exception) |
| 4162 | % |
| 4163 | % A description of each parameter follows: |
| 4164 | % |
| 4165 | % o mng_info: Specifies a pointer to a MngInfo structure. |
| 4166 | % |
| 4167 | % o image_info: the image info. |
| 4168 | % |
| 4169 | % o exception: return any errors or warnings in this structure. |
| 4170 | % |
| 4171 | */ |
| 4172 | static Image *ReadOneJNGImage(MngInfo *mng_info, |
| 4173 | const ImageInfo *image_info, ExceptionInfo *exception) |
| 4174 | { |
| 4175 | Image |
| 4176 | *alpha_image, |
| 4177 | *color_image, |
| 4178 | *image, |
| 4179 | *jng_image; |
| 4180 | |
| 4181 | ImageInfo |
| 4182 | *alpha_image_info, |
| 4183 | *color_image_info; |
| 4184 | |
cristy | 4383ec8 | 2011-01-05 15:42:32 +0000 | [diff] [blame] | 4185 | MagickBooleanType |
| 4186 | logging; |
| 4187 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 4188 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4189 | y; |
| 4190 | |
| 4191 | MagickBooleanType |
| 4192 | status; |
| 4193 | |
| 4194 | png_uint_32 |
| 4195 | jng_height, |
| 4196 | jng_width; |
| 4197 | |
| 4198 | png_byte |
| 4199 | jng_color_type, |
| 4200 | jng_image_sample_depth, |
| 4201 | jng_image_compression_method, |
| 4202 | jng_image_interlace_method, |
| 4203 | jng_alpha_sample_depth, |
| 4204 | jng_alpha_compression_method, |
| 4205 | jng_alpha_filter_method, |
| 4206 | jng_alpha_interlace_method; |
| 4207 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 4208 | register const Quantum |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4209 | *s; |
| 4210 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 4211 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4212 | i, |
| 4213 | x; |
| 4214 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 4215 | register Quantum |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4216 | *q; |
| 4217 | |
| 4218 | register unsigned char |
| 4219 | *p; |
| 4220 | |
| 4221 | unsigned int |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4222 | read_JSEP, |
| 4223 | reading_idat, |
| 4224 | skip_to_iend; |
| 4225 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 4226 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4227 | length; |
| 4228 | |
| 4229 | jng_alpha_compression_method=0; |
| 4230 | jng_alpha_sample_depth=8; |
| 4231 | jng_color_type=0; |
| 4232 | jng_height=0; |
| 4233 | jng_width=0; |
| 4234 | alpha_image=(Image *) NULL; |
| 4235 | color_image=(Image *) NULL; |
| 4236 | alpha_image_info=(ImageInfo *) NULL; |
| 4237 | color_image_info=(ImageInfo *) NULL; |
| 4238 | |
| 4239 | logging=LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 4240 | " Enter ReadOneJNGImage()"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4241 | |
| 4242 | image=mng_info->image; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4243 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 4244 | if (GetAuthenticPixelQueue(image) != (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4245 | { |
| 4246 | /* |
| 4247 | Allocate next image structure. |
| 4248 | */ |
| 4249 | if (logging != MagickFalse) |
| 4250 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 4251 | " AcquireNextImage()"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4252 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 4253 | AcquireNextImage(image_info,image,exception); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4254 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4255 | if (GetNextImageInList(image) == (Image *) NULL) |
| 4256 | return((Image *) NULL); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4257 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4258 | image=SyncNextImageInList(image); |
| 4259 | } |
| 4260 | mng_info->image=image; |
| 4261 | |
| 4262 | /* |
| 4263 | Signature bytes have already been read. |
| 4264 | */ |
| 4265 | |
| 4266 | read_JSEP=MagickFalse; |
| 4267 | reading_idat=MagickFalse; |
| 4268 | skip_to_iend=MagickFalse; |
| 4269 | for (;;) |
| 4270 | { |
| 4271 | char |
| 4272 | type[MaxTextExtent]; |
| 4273 | |
| 4274 | unsigned char |
| 4275 | *chunk; |
| 4276 | |
| 4277 | unsigned int |
| 4278 | count; |
| 4279 | |
| 4280 | /* |
| 4281 | Read a new JNG chunk. |
| 4282 | */ |
| 4283 | status=SetImageProgress(image,LoadImagesTag,TellBlob(image), |
| 4284 | 2*GetBlobSize(image)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4285 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4286 | if (status == MagickFalse) |
| 4287 | break; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4288 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4289 | type[0]='\0'; |
| 4290 | (void) ConcatenateMagickString(type,"errr",MaxTextExtent); |
| 4291 | length=ReadBlobMSBLong(image); |
| 4292 | count=(unsigned int) ReadBlob(image,4,(unsigned char *) type); |
| 4293 | |
| 4294 | if (logging != MagickFalse) |
| 4295 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 4296 | " Reading JNG chunk type %c%c%c%c, length: %.20g", |
| 4297 | type[0],type[1],type[2],type[3],(double) length); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4298 | |
| 4299 | if (length > PNG_UINT_31_MAX || count == 0) |
| 4300 | ThrowReaderException(CorruptImageError,"CorruptImage"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4301 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4302 | p=NULL; |
| 4303 | chunk=(unsigned char *) NULL; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4304 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4305 | if (length) |
| 4306 | { |
| 4307 | chunk=(unsigned char *) AcquireQuantumMemory(length,sizeof(*chunk)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4308 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4309 | if (chunk == (unsigned char *) NULL) |
| 4310 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4311 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 4312 | for (i=0; i < (ssize_t) length; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4313 | chunk[i]=(unsigned char) ReadBlobByte(image); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4314 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4315 | p=chunk; |
| 4316 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4317 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4318 | (void) ReadBlobMSBLong(image); /* read crc word */ |
| 4319 | |
| 4320 | if (skip_to_iend) |
| 4321 | { |
| 4322 | if (length) |
| 4323 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4324 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4325 | continue; |
| 4326 | } |
| 4327 | |
| 4328 | if (memcmp(type,mng_JHDR,4) == 0) |
| 4329 | { |
| 4330 | if (length == 16) |
| 4331 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 4332 | jng_width=(size_t) ((p[0] << 24) | (p[1] << 16) | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4333 | (p[2] << 8) | p[3]); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 4334 | jng_height=(size_t) ((p[4] << 24) | (p[5] << 16) | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4335 | (p[6] << 8) | p[7]); |
| 4336 | jng_color_type=p[8]; |
| 4337 | jng_image_sample_depth=p[9]; |
| 4338 | jng_image_compression_method=p[10]; |
| 4339 | jng_image_interlace_method=p[11]; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4340 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4341 | image->interlace=jng_image_interlace_method != 0 ? PNGInterlace : |
| 4342 | NoInterlace; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4343 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4344 | jng_alpha_sample_depth=p[12]; |
| 4345 | jng_alpha_compression_method=p[13]; |
| 4346 | jng_alpha_filter_method=p[14]; |
| 4347 | jng_alpha_interlace_method=p[15]; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4348 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4349 | if (logging != MagickFalse) |
| 4350 | { |
| 4351 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | f2faecf | 2010-05-28 19:19:36 +0000 | [diff] [blame] | 4352 | " jng_width: %16lu",(unsigned long) jng_width); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4353 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4354 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | f2faecf | 2010-05-28 19:19:36 +0000 | [diff] [blame] | 4355 | " jng_width: %16lu",(unsigned long) jng_height); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4356 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4357 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 4358 | " jng_color_type: %16d",jng_color_type); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4359 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4360 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 4361 | " jng_image_sample_depth: %3d", |
| 4362 | jng_image_sample_depth); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4363 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4364 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 4365 | " jng_image_compression_method:%3d", |
| 4366 | jng_image_compression_method); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4367 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4368 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 4369 | " jng_image_interlace_method: %3d", |
| 4370 | jng_image_interlace_method); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4371 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4372 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 4373 | " jng_alpha_sample_depth: %3d", |
| 4374 | jng_alpha_sample_depth); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4375 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4376 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 4377 | " jng_alpha_compression_method:%3d", |
| 4378 | jng_alpha_compression_method); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4379 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4380 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 4381 | " jng_alpha_filter_method: %3d", |
| 4382 | jng_alpha_filter_method); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4383 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4384 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 4385 | " jng_alpha_interlace_method: %3d", |
| 4386 | jng_alpha_interlace_method); |
| 4387 | } |
| 4388 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4389 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4390 | if (length) |
| 4391 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4392 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4393 | continue; |
| 4394 | } |
| 4395 | |
| 4396 | |
| 4397 | if ((reading_idat == MagickFalse) && (read_JSEP == MagickFalse) && |
| 4398 | ((memcmp(type,mng_JDAT,4) == 0) || (memcmp(type,mng_JdAA,4) == 0) || |
| 4399 | (memcmp(type,mng_IDAT,4) == 0) || (memcmp(type,mng_JDAA,4) == 0))) |
| 4400 | { |
| 4401 | /* |
| 4402 | o create color_image |
| 4403 | o open color_blob, attached to color_image |
| 4404 | o if (color type has alpha) |
| 4405 | open alpha_blob, attached to alpha_image |
| 4406 | */ |
| 4407 | |
cristy | 73bd4a5 | 2010-10-05 11:24:23 +0000 | [diff] [blame] | 4408 | color_image_info=(ImageInfo *)AcquireMagickMemory(sizeof(ImageInfo)); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4409 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4410 | if (color_image_info == (ImageInfo *) NULL) |
| 4411 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4412 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4413 | GetImageInfo(color_image_info); |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 4414 | color_image=AcquireImage(color_image_info,exception); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4415 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4416 | if (color_image == (Image *) NULL) |
| 4417 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
| 4418 | |
| 4419 | if (logging != MagickFalse) |
| 4420 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 4421 | " Creating color_blob."); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4422 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4423 | (void) AcquireUniqueFilename(color_image->filename); |
| 4424 | status=OpenBlob(color_image_info,color_image,WriteBinaryBlobMode, |
| 4425 | exception); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4426 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4427 | if (status == MagickFalse) |
| 4428 | return((Image *) NULL); |
| 4429 | |
| 4430 | if ((image_info->ping == MagickFalse) && (jng_color_type >= 12)) |
| 4431 | { |
| 4432 | alpha_image_info=(ImageInfo *) |
cristy | 73bd4a5 | 2010-10-05 11:24:23 +0000 | [diff] [blame] | 4433 | AcquireMagickMemory(sizeof(ImageInfo)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4434 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4435 | if (alpha_image_info == (ImageInfo *) NULL) |
| 4436 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4437 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4438 | GetImageInfo(alpha_image_info); |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 4439 | alpha_image=AcquireImage(alpha_image_info,exception); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4440 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4441 | if (alpha_image == (Image *) NULL) |
| 4442 | { |
| 4443 | alpha_image=DestroyImage(alpha_image); |
| 4444 | ThrowReaderException(ResourceLimitError, |
| 4445 | "MemoryAllocationFailed"); |
| 4446 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4447 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4448 | if (logging != MagickFalse) |
| 4449 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 4450 | " Creating alpha_blob."); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4451 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4452 | (void) AcquireUniqueFilename(alpha_image->filename); |
| 4453 | status=OpenBlob(alpha_image_info,alpha_image,WriteBinaryBlobMode, |
| 4454 | exception); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4455 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4456 | if (status == MagickFalse) |
| 4457 | return((Image *) NULL); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4458 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4459 | if (jng_alpha_compression_method == 0) |
| 4460 | { |
| 4461 | unsigned char |
| 4462 | data[18]; |
| 4463 | |
| 4464 | if (logging != MagickFalse) |
| 4465 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 4466 | " Writing IHDR chunk to alpha_blob."); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4467 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4468 | (void) WriteBlob(alpha_image,8,(const unsigned char *) |
| 4469 | "\211PNG\r\n\032\n"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4470 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4471 | (void) WriteBlobMSBULong(alpha_image,13L); |
| 4472 | PNGType(data,mng_IHDR); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 4473 | LogPNGChunk(logging,mng_IHDR,13L); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4474 | PNGLong(data+4,jng_width); |
| 4475 | PNGLong(data+8,jng_height); |
| 4476 | data[12]=jng_alpha_sample_depth; |
| 4477 | data[13]=0; /* color_type gray */ |
| 4478 | data[14]=0; /* compression method 0 */ |
| 4479 | data[15]=0; /* filter_method 0 */ |
| 4480 | data[16]=0; /* interlace_method 0 */ |
| 4481 | (void) WriteBlob(alpha_image,17,data); |
| 4482 | (void) WriteBlobMSBULong(alpha_image,crc32(0,data,17)); |
| 4483 | } |
| 4484 | } |
| 4485 | reading_idat=MagickTrue; |
| 4486 | } |
| 4487 | |
| 4488 | if (memcmp(type,mng_JDAT,4) == 0) |
| 4489 | { |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4490 | /* Copy chunk to color_image->blob */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4491 | |
| 4492 | if (logging != MagickFalse) |
| 4493 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 4494 | " Copying JDAT chunk data to color_blob."); |
| 4495 | |
| 4496 | (void) WriteBlob(color_image,length,chunk); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4497 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4498 | if (length) |
| 4499 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4500 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4501 | continue; |
| 4502 | } |
| 4503 | |
| 4504 | if (memcmp(type,mng_IDAT,4) == 0) |
| 4505 | { |
| 4506 | png_byte |
| 4507 | data[5]; |
| 4508 | |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4509 | /* Copy IDAT header and chunk data to alpha_image->blob */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4510 | |
| 4511 | if (image_info->ping == MagickFalse) |
| 4512 | { |
| 4513 | if (logging != MagickFalse) |
| 4514 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 4515 | " Copying IDAT chunk data to alpha_blob."); |
| 4516 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 4517 | (void) WriteBlobMSBULong(alpha_image,(size_t) length); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4518 | PNGType(data,mng_IDAT); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 4519 | LogPNGChunk(logging,mng_IDAT,length); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4520 | (void) WriteBlob(alpha_image,4,data); |
| 4521 | (void) WriteBlob(alpha_image,length,chunk); |
| 4522 | (void) WriteBlobMSBULong(alpha_image, |
| 4523 | crc32(crc32(0,data,4),chunk,(uInt) length)); |
| 4524 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4525 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4526 | if (length) |
| 4527 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4528 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4529 | continue; |
| 4530 | } |
| 4531 | |
| 4532 | if ((memcmp(type,mng_JDAA,4) == 0) || (memcmp(type,mng_JdAA,4) == 0)) |
| 4533 | { |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4534 | /* Copy chunk data to alpha_image->blob */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4535 | |
| 4536 | if (image_info->ping == MagickFalse) |
| 4537 | { |
| 4538 | if (logging != MagickFalse) |
| 4539 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 4540 | " Copying JDAA chunk data to alpha_blob."); |
| 4541 | |
| 4542 | (void) WriteBlob(alpha_image,length,chunk); |
| 4543 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4544 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4545 | if (length) |
| 4546 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4547 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4548 | continue; |
| 4549 | } |
| 4550 | |
| 4551 | if (memcmp(type,mng_JSEP,4) == 0) |
| 4552 | { |
| 4553 | read_JSEP=MagickTrue; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4554 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4555 | if (length) |
| 4556 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4557 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4558 | continue; |
| 4559 | } |
| 4560 | |
| 4561 | if (memcmp(type,mng_bKGD,4) == 0) |
| 4562 | { |
| 4563 | if (length == 2) |
| 4564 | { |
| 4565 | image->background_color.red=ScaleCharToQuantum(p[1]); |
| 4566 | image->background_color.green=image->background_color.red; |
| 4567 | image->background_color.blue=image->background_color.red; |
| 4568 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4569 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4570 | if (length == 6) |
| 4571 | { |
| 4572 | image->background_color.red=ScaleCharToQuantum(p[1]); |
| 4573 | image->background_color.green=ScaleCharToQuantum(p[3]); |
| 4574 | image->background_color.blue=ScaleCharToQuantum(p[5]); |
| 4575 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4576 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4577 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 4578 | continue; |
| 4579 | } |
| 4580 | |
| 4581 | if (memcmp(type,mng_gAMA,4) == 0) |
| 4582 | { |
| 4583 | if (length == 4) |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 4584 | image->gamma=((float) mng_get_long(p))*0.00001; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4585 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4586 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 4587 | continue; |
| 4588 | } |
| 4589 | |
| 4590 | if (memcmp(type,mng_cHRM,4) == 0) |
| 4591 | { |
| 4592 | if (length == 32) |
| 4593 | { |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 4594 | image->chromaticity.white_point.x=0.00001*mng_get_long(p); |
| 4595 | image->chromaticity.white_point.y=0.00001*mng_get_long(&p[4]); |
| 4596 | image->chromaticity.red_primary.x=0.00001*mng_get_long(&p[8]); |
| 4597 | image->chromaticity.red_primary.y=0.00001*mng_get_long(&p[12]); |
| 4598 | image->chromaticity.green_primary.x=0.00001*mng_get_long(&p[16]); |
| 4599 | image->chromaticity.green_primary.y=0.00001*mng_get_long(&p[20]); |
| 4600 | image->chromaticity.blue_primary.x=0.00001*mng_get_long(&p[24]); |
| 4601 | image->chromaticity.blue_primary.y=0.00001*mng_get_long(&p[28]); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4602 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4603 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4604 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 4605 | continue; |
| 4606 | } |
| 4607 | |
| 4608 | if (memcmp(type,mng_sRGB,4) == 0) |
| 4609 | { |
| 4610 | if (length == 1) |
| 4611 | { |
glennrp | e610a07 | 2010-08-05 17:08:46 +0000 | [diff] [blame] | 4612 | image->rendering_intent= |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 4613 | Magick_RenderingIntent_from_PNG_RenderingIntent(p[0]); |
cristy | da7803d | 2012-05-03 01:22:45 +0000 | [diff] [blame] | 4614 | image->gamma=1.000f/2.200f; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4615 | image->chromaticity.red_primary.x=0.6400f; |
| 4616 | image->chromaticity.red_primary.y=0.3300f; |
| 4617 | image->chromaticity.green_primary.x=0.3000f; |
| 4618 | image->chromaticity.green_primary.y=0.6000f; |
| 4619 | image->chromaticity.blue_primary.x=0.1500f; |
| 4620 | image->chromaticity.blue_primary.y=0.0600f; |
| 4621 | image->chromaticity.white_point.x=0.3127f; |
| 4622 | image->chromaticity.white_point.y=0.3290f; |
| 4623 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4624 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4625 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 4626 | continue; |
| 4627 | } |
| 4628 | |
| 4629 | if (memcmp(type,mng_oFFs,4) == 0) |
| 4630 | { |
| 4631 | if (length > 8) |
| 4632 | { |
glennrp | 5eae760 | 2011-02-22 15:21:32 +0000 | [diff] [blame] | 4633 | image->page.x=(ssize_t) mng_get_long(p); |
| 4634 | image->page.y=(ssize_t) mng_get_long(&p[4]); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4635 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4636 | if ((int) p[8] != 0) |
| 4637 | { |
| 4638 | image->page.x/=10000; |
| 4639 | image->page.y/=10000; |
| 4640 | } |
| 4641 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4642 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4643 | if (length) |
| 4644 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4645 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4646 | continue; |
| 4647 | } |
| 4648 | |
| 4649 | if (memcmp(type,mng_pHYs,4) == 0) |
| 4650 | { |
| 4651 | if (length > 8) |
| 4652 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 4653 | image->resolution.x=(double) mng_get_long(p); |
| 4654 | image->resolution.y=(double) mng_get_long(&p[4]); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4655 | if ((int) p[8] == PNG_RESOLUTION_METER) |
| 4656 | { |
| 4657 | image->units=PixelsPerCentimeterResolution; |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 4658 | image->resolution.x=image->resolution.x/100.0f; |
| 4659 | image->resolution.y=image->resolution.y/100.0f; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4660 | } |
| 4661 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4662 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4663 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 4664 | continue; |
| 4665 | } |
| 4666 | |
| 4667 | #if 0 |
| 4668 | if (memcmp(type,mng_iCCP,4) == 0) |
| 4669 | { |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 4670 | /* To do: */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4671 | if (length) |
| 4672 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4673 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4674 | continue; |
| 4675 | } |
| 4676 | #endif |
| 4677 | |
| 4678 | if (length) |
| 4679 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 4680 | |
| 4681 | if (memcmp(type,mng_IEND,4)) |
| 4682 | continue; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4683 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4684 | break; |
| 4685 | } |
| 4686 | |
| 4687 | |
| 4688 | /* IEND found */ |
| 4689 | |
| 4690 | /* |
| 4691 | Finish up reading image data: |
| 4692 | |
| 4693 | o read main image from color_blob. |
| 4694 | |
| 4695 | o close color_blob. |
| 4696 | |
| 4697 | o if (color_type has alpha) |
| 4698 | if alpha_encoding is PNG |
| 4699 | read secondary image from alpha_blob via ReadPNG |
| 4700 | if alpha_encoding is JPEG |
| 4701 | read secondary image from alpha_blob via ReadJPEG |
| 4702 | |
| 4703 | o close alpha_blob. |
| 4704 | |
| 4705 | o copy intensity of secondary image into |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 4706 | alpha samples of main image. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4707 | |
| 4708 | o destroy the secondary image. |
| 4709 | */ |
| 4710 | |
| 4711 | (void) CloseBlob(color_image); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4712 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4713 | if (logging != MagickFalse) |
| 4714 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 4715 | " Reading jng_image from color_blob."); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4716 | |
cristy | 3b6fd2e | 2011-05-20 12:53:50 +0000 | [diff] [blame] | 4717 | (void) FormatLocaleString(color_image_info->filename,MaxTextExtent,"%s", |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4718 | color_image->filename); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4719 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4720 | color_image_info->ping=MagickFalse; /* To do: avoid this */ |
| 4721 | jng_image=ReadImage(color_image_info,exception); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4722 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4723 | if (jng_image == (Image *) NULL) |
| 4724 | return((Image *) NULL); |
| 4725 | |
| 4726 | (void) RelinquishUniqueFileResource(color_image->filename); |
| 4727 | color_image=DestroyImage(color_image); |
| 4728 | color_image_info=DestroyImageInfo(color_image_info); |
| 4729 | |
| 4730 | if (jng_image == (Image *) NULL) |
| 4731 | return((Image *) NULL); |
| 4732 | |
| 4733 | if (logging != MagickFalse) |
| 4734 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 4735 | " Copying jng_image pixels to main image."); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4736 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4737 | image->rows=jng_height; |
| 4738 | image->columns=jng_width; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4739 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 4740 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4741 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 4742 | s=GetVirtualPixels(jng_image,0,y,image->columns,1,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4743 | q=GetAuthenticPixels(image,0,y,image->columns,1,exception); |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 4744 | for (x=(ssize_t) image->columns; x != 0; x--) |
| 4745 | { |
| 4746 | SetPixelRed(image,GetPixelRed(jng_image,s),q); |
| 4747 | SetPixelGreen(image,GetPixelGreen(jng_image,s),q); |
| 4748 | SetPixelBlue(image,GetPixelBlue(jng_image,s),q); |
| 4749 | q+=GetPixelChannels(image); |
| 4750 | s+=GetPixelChannels(jng_image); |
| 4751 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4752 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4753 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 4754 | break; |
| 4755 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4756 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4757 | jng_image=DestroyImage(jng_image); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4758 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4759 | if (image_info->ping == MagickFalse) |
| 4760 | { |
| 4761 | if (jng_color_type >= 12) |
| 4762 | { |
| 4763 | if (jng_alpha_compression_method == 0) |
| 4764 | { |
| 4765 | png_byte |
| 4766 | data[5]; |
| 4767 | (void) WriteBlobMSBULong(alpha_image,0x00000000L); |
| 4768 | PNGType(data,mng_IEND); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 4769 | LogPNGChunk(logging,mng_IEND,0L); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4770 | (void) WriteBlob(alpha_image,4,data); |
| 4771 | (void) WriteBlobMSBULong(alpha_image,crc32(0,data,4)); |
| 4772 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4773 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4774 | (void) CloseBlob(alpha_image); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4775 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4776 | if (logging != MagickFalse) |
| 4777 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 4778 | " Reading alpha from alpha_blob."); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4779 | |
cristy | 3b6fd2e | 2011-05-20 12:53:50 +0000 | [diff] [blame] | 4780 | (void) FormatLocaleString(alpha_image_info->filename,MaxTextExtent, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4781 | "%s",alpha_image->filename); |
| 4782 | |
| 4783 | jng_image=ReadImage(alpha_image_info,exception); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4784 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4785 | if (jng_image != (Image *) NULL) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 4786 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4787 | { |
| 4788 | s=GetVirtualPixels(jng_image,0,y,image->columns,1, |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 4789 | exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4790 | q=GetAuthenticPixels(image,0,y,image->columns,1,exception); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4791 | |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 4792 | if (image->alpha_trait == BlendPixelTrait) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 4793 | for (x=(ssize_t) image->columns; x != 0; x--) |
| 4794 | { |
| 4795 | SetPixelAlpha(image,GetPixelRed(jng_image,s),q); |
| 4796 | q+=GetPixelChannels(image); |
| 4797 | s+=GetPixelChannels(jng_image); |
| 4798 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4799 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4800 | else |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 4801 | for (x=(ssize_t) image->columns; x != 0; x--) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4802 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 4803 | SetPixelAlpha(image,GetPixelRed(jng_image,s),q); |
| 4804 | if (GetPixelAlpha(image,q) != OpaqueAlpha) |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 4805 | image->alpha_trait=BlendPixelTrait; |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 4806 | q+=GetPixelChannels(image); |
| 4807 | s+=GetPixelChannels(jng_image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4808 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4809 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4810 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 4811 | break; |
| 4812 | } |
| 4813 | (void) RelinquishUniqueFileResource(alpha_image->filename); |
| 4814 | alpha_image=DestroyImage(alpha_image); |
| 4815 | alpha_image_info=DestroyImageInfo(alpha_image_info); |
| 4816 | if (jng_image != (Image *) NULL) |
| 4817 | jng_image=DestroyImage(jng_image); |
| 4818 | } |
| 4819 | } |
| 4820 | |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4821 | /* Read the JNG image. */ |
| 4822 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4823 | if (mng_info->mng_type == 0) |
| 4824 | { |
| 4825 | mng_info->mng_width=jng_width; |
| 4826 | mng_info->mng_height=jng_height; |
| 4827 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4828 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4829 | if (image->page.width == 0 && image->page.height == 0) |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4830 | { |
| 4831 | image->page.width=jng_width; |
| 4832 | image->page.height=jng_height; |
| 4833 | } |
| 4834 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4835 | if (image->page.x == 0 && image->page.y == 0) |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4836 | { |
| 4837 | image->page.x=mng_info->x_off[mng_info->object_id]; |
| 4838 | image->page.y=mng_info->y_off[mng_info->object_id]; |
| 4839 | } |
| 4840 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4841 | else |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4842 | { |
| 4843 | image->page.y=mng_info->y_off[mng_info->object_id]; |
| 4844 | } |
| 4845 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4846 | mng_info->image_found++; |
| 4847 | status=SetImageProgress(image,LoadImagesTag,2*TellBlob(image), |
| 4848 | 2*GetBlobSize(image)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4849 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4850 | if (logging != MagickFalse) |
| 4851 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 4852 | " exit ReadOneJNGImage()"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4853 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4854 | return(image); |
| 4855 | } |
| 4856 | |
| 4857 | /* |
| 4858 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 4859 | % % |
| 4860 | % % |
| 4861 | % % |
| 4862 | % R e a d J N G I m a g e % |
| 4863 | % % |
| 4864 | % % |
| 4865 | % % |
| 4866 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 4867 | % |
| 4868 | % ReadJNGImage() reads a JPEG Network Graphics (JNG) image file |
| 4869 | % (including the 8-byte signature) and returns it. It allocates the memory |
| 4870 | % necessary for the new Image structure and returns a pointer to the new |
| 4871 | % image. |
| 4872 | % |
| 4873 | % JNG support written by Glenn Randers-Pehrson, glennrp@image... |
| 4874 | % |
| 4875 | % The format of the ReadJNGImage method is: |
| 4876 | % |
| 4877 | % Image *ReadJNGImage(const ImageInfo *image_info, ExceptionInfo |
| 4878 | % *exception) |
| 4879 | % |
| 4880 | % A description of each parameter follows: |
| 4881 | % |
| 4882 | % o image_info: the image info. |
| 4883 | % |
| 4884 | % o exception: return any errors or warnings in this structure. |
| 4885 | % |
| 4886 | */ |
| 4887 | |
| 4888 | static Image *ReadJNGImage(const ImageInfo *image_info,ExceptionInfo *exception) |
| 4889 | { |
| 4890 | Image |
| 4891 | *image, |
| 4892 | *previous; |
| 4893 | |
| 4894 | MagickBooleanType |
glennrp | 21f0e62 | 2011-01-07 16:20:57 +0000 | [diff] [blame] | 4895 | have_mng_structure, |
| 4896 | logging, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4897 | status; |
| 4898 | |
| 4899 | MngInfo |
| 4900 | *mng_info; |
| 4901 | |
| 4902 | char |
| 4903 | magic_number[MaxTextExtent]; |
| 4904 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4905 | size_t |
| 4906 | count; |
| 4907 | |
| 4908 | /* |
| 4909 | Open image file. |
| 4910 | */ |
| 4911 | assert(image_info != (const ImageInfo *) NULL); |
| 4912 | assert(image_info->signature == MagickSignature); |
| 4913 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image_info->filename); |
| 4914 | assert(exception != (ExceptionInfo *) NULL); |
| 4915 | assert(exception->signature == MagickSignature); |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 4916 | logging=LogMagickEvent(CoderEvent,GetMagickModule(),"Enter ReadJNGImage()"); |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 4917 | image=AcquireImage(image_info,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4918 | mng_info=(MngInfo *) NULL; |
| 4919 | status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4920 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4921 | if (status == MagickFalse) |
| 4922 | return((Image *) NULL); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4923 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4924 | if (LocaleCompare(image_info->magick,"JNG") != 0) |
| 4925 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4926 | |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4927 | /* Verify JNG signature. */ |
| 4928 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4929 | count=(size_t) ReadBlob(image,8,(unsigned char *) magic_number); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4930 | |
glennrp | 3b8763e | 2011-02-21 12:08:18 +0000 | [diff] [blame] | 4931 | if (count < 8 || memcmp(magic_number,"\213JNG\r\n\032\n",8) != 0) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4932 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4933 | |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4934 | /* Allocate a MngInfo structure. */ |
| 4935 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4936 | have_mng_structure=MagickFalse; |
cristy | 73bd4a5 | 2010-10-05 11:24:23 +0000 | [diff] [blame] | 4937 | mng_info=(MngInfo *) AcquireMagickMemory(sizeof(*mng_info)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4938 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4939 | if (mng_info == (MngInfo *) NULL) |
| 4940 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4941 | |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4942 | /* Initialize members of the MngInfo structure. */ |
| 4943 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4944 | (void) ResetMagickMemory(mng_info,0,sizeof(MngInfo)); |
| 4945 | have_mng_structure=MagickTrue; |
| 4946 | |
| 4947 | mng_info->image=image; |
| 4948 | previous=image; |
| 4949 | image=ReadOneJNGImage(mng_info,image_info,exception); |
| 4950 | MngInfoFreeStruct(mng_info,&have_mng_structure); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4951 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4952 | if (image == (Image *) NULL) |
| 4953 | { |
| 4954 | if (IsImageObject(previous) != MagickFalse) |
| 4955 | { |
| 4956 | (void) CloseBlob(previous); |
| 4957 | (void) DestroyImageList(previous); |
| 4958 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4959 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4960 | if (logging != MagickFalse) |
| 4961 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 4962 | "exit ReadJNGImage() with error"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4963 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4964 | return((Image *) NULL); |
| 4965 | } |
| 4966 | (void) CloseBlob(image); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4967 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4968 | if (image->columns == 0 || image->rows == 0) |
| 4969 | { |
| 4970 | if (logging != MagickFalse) |
| 4971 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 4972 | "exit ReadJNGImage() with error"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4973 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4974 | ThrowReaderException(CorruptImageError,"CorruptImage"); |
| 4975 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4976 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4977 | if (logging != MagickFalse) |
| 4978 | (void) LogMagickEvent(CoderEvent,GetMagickModule(),"exit ReadJNGImage()"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4979 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4980 | return(image); |
| 4981 | } |
| 4982 | #endif |
| 4983 | |
| 4984 | static Image *ReadMNGImage(const ImageInfo *image_info,ExceptionInfo *exception) |
| 4985 | { |
| 4986 | char |
| 4987 | page_geometry[MaxTextExtent]; |
| 4988 | |
| 4989 | Image |
| 4990 | *image, |
| 4991 | *previous; |
| 4992 | |
cristy | 4383ec8 | 2011-01-05 15:42:32 +0000 | [diff] [blame] | 4993 | MagickBooleanType |
glennrp | 21f0e62 | 2011-01-07 16:20:57 +0000 | [diff] [blame] | 4994 | logging, |
| 4995 | have_mng_structure; |
cristy | 4383ec8 | 2011-01-05 15:42:32 +0000 | [diff] [blame] | 4996 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4997 | volatile int |
| 4998 | first_mng_object, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4999 | object_id, |
| 5000 | term_chunk_found, |
| 5001 | skip_to_iend; |
| 5002 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 5003 | volatile ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5004 | image_count=0; |
| 5005 | |
| 5006 | MagickBooleanType |
| 5007 | status; |
| 5008 | |
| 5009 | MagickOffsetType |
| 5010 | offset; |
| 5011 | |
| 5012 | MngInfo |
| 5013 | *mng_info; |
| 5014 | |
| 5015 | MngBox |
| 5016 | default_fb, |
| 5017 | fb, |
| 5018 | previous_fb; |
| 5019 | |
| 5020 | #if defined(MNG_INSERT_LAYERS) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 5021 | PixelInfo |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5022 | mng_background_color; |
| 5023 | #endif |
| 5024 | |
| 5025 | register unsigned char |
| 5026 | *p; |
| 5027 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 5028 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5029 | i; |
| 5030 | |
| 5031 | size_t |
| 5032 | count; |
| 5033 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 5034 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5035 | loop_level; |
| 5036 | |
| 5037 | volatile short |
| 5038 | skipping_loop; |
| 5039 | |
| 5040 | #if defined(MNG_INSERT_LAYERS) |
| 5041 | unsigned int |
| 5042 | mandatory_back=0; |
| 5043 | #endif |
| 5044 | |
| 5045 | volatile unsigned int |
| 5046 | #ifdef MNG_OBJECT_BUFFERS |
| 5047 | mng_background_object=0, |
| 5048 | #endif |
| 5049 | mng_type=0; /* 0: PNG or JNG; 1: MNG; 2: MNG-LC; 3: MNG-VLC */ |
| 5050 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 5051 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5052 | default_frame_timeout, |
| 5053 | frame_timeout, |
| 5054 | #if defined(MNG_INSERT_LAYERS) |
| 5055 | image_height, |
| 5056 | image_width, |
| 5057 | #endif |
| 5058 | length; |
| 5059 | |
glennrp | 38ea083 | 2010-06-02 18:50:28 +0000 | [diff] [blame] | 5060 | /* These delays are all measured in image ticks_per_second, |
| 5061 | * not in MNG ticks_per_second |
| 5062 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 5063 | volatile size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5064 | default_frame_delay, |
| 5065 | final_delay, |
| 5066 | final_image_delay, |
| 5067 | frame_delay, |
| 5068 | #if defined(MNG_INSERT_LAYERS) |
| 5069 | insert_layers, |
| 5070 | #endif |
| 5071 | mng_iterations=1, |
| 5072 | simplicity=0, |
| 5073 | subframe_height=0, |
| 5074 | subframe_width=0; |
| 5075 | |
| 5076 | previous_fb.top=0; |
| 5077 | previous_fb.bottom=0; |
| 5078 | previous_fb.left=0; |
| 5079 | previous_fb.right=0; |
| 5080 | default_fb.top=0; |
| 5081 | default_fb.bottom=0; |
| 5082 | default_fb.left=0; |
| 5083 | default_fb.right=0; |
| 5084 | |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5085 | /* Open image file. */ |
| 5086 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5087 | assert(image_info != (const ImageInfo *) NULL); |
| 5088 | assert(image_info->signature == MagickSignature); |
| 5089 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image_info->filename); |
| 5090 | assert(exception != (ExceptionInfo *) NULL); |
| 5091 | assert(exception->signature == MagickSignature); |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 5092 | logging=LogMagickEvent(CoderEvent,GetMagickModule(),"Enter ReadMNGImage()"); |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 5093 | image=AcquireImage(image_info,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5094 | mng_info=(MngInfo *) NULL; |
| 5095 | status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5096 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5097 | if (status == MagickFalse) |
| 5098 | return((Image *) NULL); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5099 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5100 | first_mng_object=MagickFalse; |
| 5101 | skipping_loop=(-1); |
| 5102 | have_mng_structure=MagickFalse; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5103 | |
| 5104 | /* Allocate a MngInfo structure. */ |
| 5105 | |
cristy | 73bd4a5 | 2010-10-05 11:24:23 +0000 | [diff] [blame] | 5106 | mng_info=(MngInfo *) AcquireMagickMemory(sizeof(MngInfo)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5107 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5108 | if (mng_info == (MngInfo *) NULL) |
| 5109 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5110 | |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5111 | /* Initialize members of the MngInfo structure. */ |
| 5112 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5113 | (void) ResetMagickMemory(mng_info,0,sizeof(MngInfo)); |
| 5114 | mng_info->image=image; |
| 5115 | have_mng_structure=MagickTrue; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5116 | |
| 5117 | if (LocaleCompare(image_info->magick,"MNG") == 0) |
| 5118 | { |
| 5119 | char |
| 5120 | magic_number[MaxTextExtent]; |
| 5121 | |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5122 | /* Verify MNG signature. */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5123 | count=(size_t) ReadBlob(image,8,(unsigned char *) magic_number); |
| 5124 | if (memcmp(magic_number,"\212MNG\r\n\032\n",8) != 0) |
| 5125 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5126 | |
| 5127 | /* Initialize some nonzero members of the MngInfo structure. */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5128 | for (i=0; i < MNG_MAX_OBJECTS; i++) |
| 5129 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 5130 | mng_info->object_clip[i].right=(ssize_t) PNG_UINT_31_MAX; |
| 5131 | mng_info->object_clip[i].bottom=(ssize_t) PNG_UINT_31_MAX; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5132 | } |
| 5133 | mng_info->exists[0]=MagickTrue; |
| 5134 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5135 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5136 | first_mng_object=MagickTrue; |
| 5137 | mng_type=0; |
| 5138 | #if defined(MNG_INSERT_LAYERS) |
| 5139 | insert_layers=MagickFalse; /* should be False when converting or mogrifying */ |
| 5140 | #endif |
| 5141 | default_frame_delay=0; |
| 5142 | default_frame_timeout=0; |
| 5143 | frame_delay=0; |
| 5144 | final_delay=1; |
| 5145 | mng_info->ticks_per_second=1UL*image->ticks_per_second; |
| 5146 | object_id=0; |
| 5147 | skip_to_iend=MagickFalse; |
| 5148 | term_chunk_found=MagickFalse; |
| 5149 | mng_info->framing_mode=1; |
| 5150 | #if defined(MNG_INSERT_LAYERS) |
| 5151 | mandatory_back=MagickFalse; |
| 5152 | #endif |
| 5153 | #if defined(MNG_INSERT_LAYERS) |
| 5154 | mng_background_color=image->background_color; |
| 5155 | #endif |
| 5156 | default_fb=mng_info->frame; |
| 5157 | previous_fb=mng_info->frame; |
| 5158 | do |
| 5159 | { |
| 5160 | char |
| 5161 | type[MaxTextExtent]; |
| 5162 | |
| 5163 | if (LocaleCompare(image_info->magick,"MNG") == 0) |
| 5164 | { |
| 5165 | unsigned char |
| 5166 | *chunk; |
| 5167 | |
| 5168 | /* |
| 5169 | Read a new chunk. |
| 5170 | */ |
| 5171 | type[0]='\0'; |
| 5172 | (void) ConcatenateMagickString(type,"errr",MaxTextExtent); |
| 5173 | length=ReadBlobMSBLong(image); |
| 5174 | count=(size_t) ReadBlob(image,4,(unsigned char *) type); |
| 5175 | |
| 5176 | if (logging != MagickFalse) |
| 5177 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 5178 | " Reading MNG chunk type %c%c%c%c, length: %.20g", |
| 5179 | type[0],type[1],type[2],type[3],(double) length); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5180 | |
| 5181 | if (length > PNG_UINT_31_MAX) |
| 5182 | status=MagickFalse; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5183 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5184 | if (count == 0) |
| 5185 | ThrowReaderException(CorruptImageError,"CorruptImage"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5186 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5187 | p=NULL; |
| 5188 | chunk=(unsigned char *) NULL; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5189 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5190 | if (length) |
| 5191 | { |
| 5192 | chunk=(unsigned char *) AcquireQuantumMemory(length,sizeof(*chunk)); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5193 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5194 | if (chunk == (unsigned char *) NULL) |
| 5195 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5196 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 5197 | for (i=0; i < (ssize_t) length; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5198 | chunk[i]=(unsigned char) ReadBlobByte(image); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5199 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5200 | p=chunk; |
| 5201 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5202 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5203 | (void) ReadBlobMSBLong(image); /* read crc word */ |
| 5204 | |
| 5205 | #if !defined(JNG_SUPPORTED) |
| 5206 | if (memcmp(type,mng_JHDR,4) == 0) |
| 5207 | { |
| 5208 | skip_to_iend=MagickTrue; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5209 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5210 | if (mng_info->jhdr_warning == 0) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 5211 | (void) ThrowMagickException(exception,GetMagickModule(), |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5212 | CoderError,"JNGCompressNotSupported","`%s'",image->filename); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5213 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5214 | mng_info->jhdr_warning++; |
| 5215 | } |
| 5216 | #endif |
| 5217 | if (memcmp(type,mng_DHDR,4) == 0) |
| 5218 | { |
| 5219 | skip_to_iend=MagickTrue; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5220 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5221 | if (mng_info->dhdr_warning == 0) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 5222 | (void) ThrowMagickException(exception,GetMagickModule(), |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5223 | CoderError,"DeltaPNGNotSupported","`%s'",image->filename); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5224 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5225 | mng_info->dhdr_warning++; |
| 5226 | } |
| 5227 | if (memcmp(type,mng_MEND,4) == 0) |
| 5228 | break; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5229 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5230 | if (skip_to_iend) |
| 5231 | { |
| 5232 | if (memcmp(type,mng_IEND,4) == 0) |
| 5233 | skip_to_iend=MagickFalse; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5234 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5235 | if (length) |
| 5236 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5237 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5238 | if (logging != MagickFalse) |
| 5239 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 5240 | " Skip to IEND."); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5241 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5242 | continue; |
| 5243 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5244 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5245 | if (memcmp(type,mng_MHDR,4) == 0) |
| 5246 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 5247 | mng_info->mng_width=(size_t) ((p[0] << 24) | (p[1] << 16) | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5248 | (p[2] << 8) | p[3]); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5249 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 5250 | mng_info->mng_height=(size_t) ((p[4] << 24) | (p[5] << 16) | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5251 | (p[6] << 8) | p[7]); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5252 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5253 | if (logging != MagickFalse) |
| 5254 | { |
| 5255 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 5256 | " MNG width: %.20g",(double) mng_info->mng_width); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5257 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 5258 | " MNG height: %.20g",(double) mng_info->mng_height); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5259 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5260 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5261 | p+=8; |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 5262 | mng_info->ticks_per_second=(size_t) mng_get_long(p); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5263 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5264 | if (mng_info->ticks_per_second == 0) |
| 5265 | default_frame_delay=0; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5266 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5267 | else |
| 5268 | default_frame_delay=1UL*image->ticks_per_second/ |
| 5269 | mng_info->ticks_per_second; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5270 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5271 | frame_delay=default_frame_delay; |
| 5272 | simplicity=0; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5273 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5274 | if (length > 16) |
| 5275 | { |
| 5276 | p+=16; |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 5277 | simplicity=(size_t) mng_get_long(p); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5278 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5279 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5280 | mng_type=1; /* Full MNG */ |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5281 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5282 | if ((simplicity != 0) && ((simplicity | 11) == 11)) |
| 5283 | mng_type=2; /* LC */ |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5284 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5285 | if ((simplicity != 0) && ((simplicity | 9) == 9)) |
| 5286 | mng_type=3; /* VLC */ |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5287 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5288 | #if defined(MNG_INSERT_LAYERS) |
| 5289 | if (mng_type != 3) |
| 5290 | insert_layers=MagickTrue; |
| 5291 | #endif |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 5292 | if (GetAuthenticPixelQueue(image) != (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5293 | { |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5294 | /* Allocate next image structure. */ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 5295 | AcquireNextImage(image_info,image,exception); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5296 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5297 | if (GetNextImageInList(image) == (Image *) NULL) |
| 5298 | return((Image *) NULL); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5299 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5300 | image=SyncNextImageInList(image); |
| 5301 | mng_info->image=image; |
| 5302 | } |
| 5303 | |
| 5304 | if ((mng_info->mng_width > 65535L) || |
| 5305 | (mng_info->mng_height > 65535L)) |
| 5306 | ThrowReaderException(ImageError,"WidthOrHeightExceedsLimit"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5307 | |
cristy | 3b6fd2e | 2011-05-20 12:53:50 +0000 | [diff] [blame] | 5308 | (void) FormatLocaleString(page_geometry,MaxTextExtent, |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 5309 | "%.20gx%.20g+0+0",(double) mng_info->mng_width,(double) |
cristy | f2faecf | 2010-05-28 19:19:36 +0000 | [diff] [blame] | 5310 | mng_info->mng_height); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5311 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5312 | mng_info->frame.left=0; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 5313 | mng_info->frame.right=(ssize_t) mng_info->mng_width; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5314 | mng_info->frame.top=0; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 5315 | mng_info->frame.bottom=(ssize_t) mng_info->mng_height; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5316 | mng_info->clip=default_fb=previous_fb=mng_info->frame; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5317 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5318 | for (i=0; i < MNG_MAX_OBJECTS; i++) |
| 5319 | mng_info->object_clip[i]=mng_info->frame; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5320 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5321 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 5322 | continue; |
| 5323 | } |
| 5324 | |
| 5325 | if (memcmp(type,mng_TERM,4) == 0) |
| 5326 | { |
| 5327 | int |
| 5328 | repeat=0; |
| 5329 | |
| 5330 | |
| 5331 | if (length) |
| 5332 | repeat=p[0]; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5333 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5334 | if (repeat == 3) |
| 5335 | { |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 5336 | final_delay=(png_uint_32) mng_get_long(&p[2]); |
| 5337 | mng_iterations=(png_uint_32) mng_get_long(&p[6]); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5338 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5339 | if (mng_iterations == PNG_UINT_31_MAX) |
| 5340 | mng_iterations=0; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5341 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5342 | image->iterations=mng_iterations; |
| 5343 | term_chunk_found=MagickTrue; |
| 5344 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5345 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5346 | if (logging != MagickFalse) |
| 5347 | { |
| 5348 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 5349 | " repeat=%d",repeat); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5350 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5351 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 5352 | " final_delay=%.20g",(double) final_delay); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5353 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5354 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 5355 | " image->iterations=%.20g",(double) image->iterations); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5356 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5357 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5358 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 5359 | continue; |
| 5360 | } |
| 5361 | if (memcmp(type,mng_DEFI,4) == 0) |
| 5362 | { |
| 5363 | if (mng_type == 3) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 5364 | (void) ThrowMagickException(exception,GetMagickModule(), |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5365 | CoderError,"DEFI chunk found in MNG-VLC datastream","`%s'", |
| 5366 | image->filename); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5367 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5368 | object_id=(p[0] << 8) | p[1]; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5369 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5370 | if (mng_type == 2 && object_id != 0) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 5371 | (void) ThrowMagickException(exception,GetMagickModule(), |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5372 | CoderError,"Nonzero object_id in MNG-LC datastream","`%s'", |
| 5373 | image->filename); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5374 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5375 | if (object_id > MNG_MAX_OBJECTS) |
| 5376 | { |
| 5377 | /* |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 5378 | Instead of using a warning we should allocate a larger |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5379 | MngInfo structure and continue. |
| 5380 | */ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 5381 | (void) ThrowMagickException(exception,GetMagickModule(), |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5382 | CoderError,"object id too large","`%s'",image->filename); |
| 5383 | object_id=MNG_MAX_OBJECTS; |
| 5384 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5385 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5386 | if (mng_info->exists[object_id]) |
| 5387 | if (mng_info->frozen[object_id]) |
| 5388 | { |
| 5389 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 5390 | (void) ThrowMagickException(exception, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5391 | GetMagickModule(),CoderError, |
| 5392 | "DEFI cannot redefine a frozen MNG object","`%s'", |
| 5393 | image->filename); |
| 5394 | continue; |
| 5395 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5396 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5397 | mng_info->exists[object_id]=MagickTrue; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5398 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5399 | if (length > 2) |
| 5400 | mng_info->invisible[object_id]=p[2]; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5401 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5402 | /* |
| 5403 | Extract object offset info. |
| 5404 | */ |
| 5405 | if (length > 11) |
| 5406 | { |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5407 | mng_info->x_off[object_id]=(ssize_t) ((p[4] << 24) | |
| 5408 | (p[5] << 16) | (p[6] << 8) | p[7]); |
| 5409 | |
| 5410 | mng_info->y_off[object_id]=(ssize_t) ((p[8] << 24) | |
| 5411 | (p[9] << 16) | (p[10] << 8) | p[11]); |
| 5412 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5413 | if (logging != MagickFalse) |
| 5414 | { |
| 5415 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 5416 | " x_off[%d]: %.20g",object_id,(double) |
cristy | f2faecf | 2010-05-28 19:19:36 +0000 | [diff] [blame] | 5417 | mng_info->x_off[object_id]); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5418 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5419 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 5420 | " y_off[%d]: %.20g",object_id,(double) |
cristy | f2faecf | 2010-05-28 19:19:36 +0000 | [diff] [blame] | 5421 | mng_info->y_off[object_id]); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5422 | } |
| 5423 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5424 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5425 | /* |
| 5426 | Extract object clipping info. |
| 5427 | */ |
| 5428 | if (length > 27) |
| 5429 | mng_info->object_clip[object_id]=mng_read_box(mng_info->frame,0, |
| 5430 | &p[12]); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5431 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5432 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 5433 | continue; |
| 5434 | } |
| 5435 | if (memcmp(type,mng_bKGD,4) == 0) |
| 5436 | { |
| 5437 | mng_info->have_global_bkgd=MagickFalse; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5438 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5439 | if (length > 5) |
| 5440 | { |
| 5441 | mng_info->mng_global_bkgd.red= |
| 5442 | ScaleShortToQuantum((unsigned short) ((p[0] << 8) | p[1])); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5443 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5444 | mng_info->mng_global_bkgd.green= |
| 5445 | ScaleShortToQuantum((unsigned short) ((p[2] << 8) | p[3])); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5446 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5447 | mng_info->mng_global_bkgd.blue= |
| 5448 | ScaleShortToQuantum((unsigned short) ((p[4] << 8) | p[5])); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5449 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5450 | mng_info->have_global_bkgd=MagickTrue; |
| 5451 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5452 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5453 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 5454 | continue; |
| 5455 | } |
| 5456 | if (memcmp(type,mng_BACK,4) == 0) |
| 5457 | { |
| 5458 | #if defined(MNG_INSERT_LAYERS) |
| 5459 | if (length > 6) |
| 5460 | mandatory_back=p[6]; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5461 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5462 | else |
| 5463 | mandatory_back=0; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5464 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5465 | if (mandatory_back && length > 5) |
| 5466 | { |
| 5467 | mng_background_color.red= |
| 5468 | ScaleShortToQuantum((unsigned short) ((p[0] << 8) | p[1])); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5469 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5470 | mng_background_color.green= |
| 5471 | ScaleShortToQuantum((unsigned short) ((p[2] << 8) | p[3])); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5472 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5473 | mng_background_color.blue= |
| 5474 | ScaleShortToQuantum((unsigned short) ((p[4] << 8) | p[5])); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5475 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 5476 | mng_background_color.alpha=OpaqueAlpha; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5477 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5478 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5479 | #ifdef MNG_OBJECT_BUFFERS |
| 5480 | if (length > 8) |
| 5481 | mng_background_object=(p[7] << 8) | p[8]; |
| 5482 | #endif |
| 5483 | #endif |
| 5484 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 5485 | continue; |
| 5486 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5487 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5488 | if (memcmp(type,mng_PLTE,4) == 0) |
| 5489 | { |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5490 | /* Read global PLTE. */ |
| 5491 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5492 | if (length && (length < 769)) |
| 5493 | { |
| 5494 | if (mng_info->global_plte == (png_colorp) NULL) |
| 5495 | mng_info->global_plte=(png_colorp) AcquireQuantumMemory(256, |
| 5496 | sizeof(*mng_info->global_plte)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5497 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 5498 | for (i=0; i < (ssize_t) (length/3); i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5499 | { |
| 5500 | mng_info->global_plte[i].red=p[3*i]; |
| 5501 | mng_info->global_plte[i].green=p[3*i+1]; |
| 5502 | mng_info->global_plte[i].blue=p[3*i+2]; |
| 5503 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5504 | |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 5505 | mng_info->global_plte_length=(unsigned int) (length/3); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5506 | } |
| 5507 | #ifdef MNG_LOOSE |
| 5508 | for ( ; i < 256; i++) |
| 5509 | { |
| 5510 | mng_info->global_plte[i].red=i; |
| 5511 | mng_info->global_plte[i].green=i; |
| 5512 | mng_info->global_plte[i].blue=i; |
| 5513 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5514 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5515 | if (length) |
| 5516 | mng_info->global_plte_length=256; |
| 5517 | #endif |
| 5518 | else |
| 5519 | mng_info->global_plte_length=0; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5520 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5521 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 5522 | continue; |
| 5523 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5524 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5525 | if (memcmp(type,mng_tRNS,4) == 0) |
| 5526 | { |
| 5527 | /* read global tRNS */ |
| 5528 | |
| 5529 | if (length < 257) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 5530 | for (i=0; i < (ssize_t) length; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5531 | mng_info->global_trns[i]=p[i]; |
| 5532 | |
| 5533 | #ifdef MNG_LOOSE |
| 5534 | for ( ; i < 256; i++) |
| 5535 | mng_info->global_trns[i]=255; |
| 5536 | #endif |
cristy | 12560f3 | 2010-06-03 16:51:08 +0000 | [diff] [blame] | 5537 | mng_info->global_trns_length=(unsigned int) length; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5538 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 5539 | continue; |
| 5540 | } |
| 5541 | if (memcmp(type,mng_gAMA,4) == 0) |
| 5542 | { |
| 5543 | if (length == 4) |
| 5544 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 5545 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5546 | igamma; |
| 5547 | |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 5548 | igamma=mng_get_long(p); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5549 | mng_info->global_gamma=((float) igamma)*0.00001; |
| 5550 | mng_info->have_global_gama=MagickTrue; |
| 5551 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5552 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5553 | else |
| 5554 | mng_info->have_global_gama=MagickFalse; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5555 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5556 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 5557 | continue; |
| 5558 | } |
| 5559 | |
| 5560 | if (memcmp(type,mng_cHRM,4) == 0) |
| 5561 | { |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5562 | /* Read global cHRM */ |
| 5563 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5564 | if (length == 32) |
| 5565 | { |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 5566 | mng_info->global_chrm.white_point.x=0.00001*mng_get_long(p); |
| 5567 | mng_info->global_chrm.white_point.y=0.00001*mng_get_long(&p[4]); |
| 5568 | mng_info->global_chrm.red_primary.x=0.00001*mng_get_long(&p[8]); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5569 | mng_info->global_chrm.red_primary.y=0.00001* |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 5570 | mng_get_long(&p[12]); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5571 | mng_info->global_chrm.green_primary.x=0.00001* |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 5572 | mng_get_long(&p[16]); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5573 | mng_info->global_chrm.green_primary.y=0.00001* |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 5574 | mng_get_long(&p[20]); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5575 | mng_info->global_chrm.blue_primary.x=0.00001* |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 5576 | mng_get_long(&p[24]); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5577 | mng_info->global_chrm.blue_primary.y=0.00001* |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 5578 | mng_get_long(&p[28]); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5579 | mng_info->have_global_chrm=MagickTrue; |
| 5580 | } |
| 5581 | else |
| 5582 | mng_info->have_global_chrm=MagickFalse; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5583 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5584 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 5585 | continue; |
| 5586 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5587 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5588 | if (memcmp(type,mng_sRGB,4) == 0) |
| 5589 | { |
| 5590 | /* |
| 5591 | Read global sRGB. |
| 5592 | */ |
| 5593 | if (length) |
| 5594 | { |
glennrp | e610a07 | 2010-08-05 17:08:46 +0000 | [diff] [blame] | 5595 | mng_info->global_srgb_intent= |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 5596 | Magick_RenderingIntent_from_PNG_RenderingIntent(p[0]); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5597 | mng_info->have_global_srgb=MagickTrue; |
| 5598 | } |
| 5599 | else |
| 5600 | mng_info->have_global_srgb=MagickFalse; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5601 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5602 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 5603 | continue; |
| 5604 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5605 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5606 | if (memcmp(type,mng_iCCP,4) == 0) |
| 5607 | { |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 5608 | /* To do: */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5609 | |
| 5610 | /* |
| 5611 | Read global iCCP. |
| 5612 | */ |
| 5613 | if (length) |
| 5614 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5615 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5616 | continue; |
| 5617 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5618 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5619 | if (memcmp(type,mng_FRAM,4) == 0) |
| 5620 | { |
| 5621 | if (mng_type == 3) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 5622 | (void) ThrowMagickException(exception,GetMagickModule(), |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5623 | CoderError,"FRAM chunk found in MNG-VLC datastream","`%s'", |
| 5624 | image->filename); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5625 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5626 | if ((mng_info->framing_mode == 2) || (mng_info->framing_mode == 4)) |
| 5627 | image->delay=frame_delay; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5628 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5629 | frame_delay=default_frame_delay; |
| 5630 | frame_timeout=default_frame_timeout; |
| 5631 | fb=default_fb; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5632 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5633 | if (length) |
| 5634 | if (p[0]) |
| 5635 | mng_info->framing_mode=p[0]; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5636 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5637 | if (logging != MagickFalse) |
| 5638 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 5639 | " Framing_mode=%d",mng_info->framing_mode); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5640 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5641 | if (length > 6) |
| 5642 | { |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5643 | /* Note the delay and frame clipping boundaries. */ |
| 5644 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5645 | p++; /* framing mode */ |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5646 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 5647 | while (*p && ((p-chunk) < (ssize_t) length)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5648 | p++; /* frame name */ |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5649 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5650 | p++; /* frame name terminator */ |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5651 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 5652 | if ((p-chunk) < (ssize_t) (length-4)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5653 | { |
| 5654 | int |
| 5655 | change_delay, |
| 5656 | change_timeout, |
| 5657 | change_clipping; |
| 5658 | |
| 5659 | change_delay=(*p++); |
| 5660 | change_timeout=(*p++); |
| 5661 | change_clipping=(*p++); |
| 5662 | p++; /* change_sync */ |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5663 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5664 | if (change_delay) |
| 5665 | { |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 5666 | frame_delay=1UL*image->ticks_per_second* |
| 5667 | mng_get_long(p); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5668 | |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 5669 | if (mng_info->ticks_per_second != 0) |
| 5670 | frame_delay/=mng_info->ticks_per_second; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5671 | |
glennrp | bb010dd | 2010-06-01 13:07:15 +0000 | [diff] [blame] | 5672 | else |
| 5673 | frame_delay=PNG_UINT_31_MAX; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5674 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5675 | if (change_delay == 2) |
| 5676 | default_frame_delay=frame_delay; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5677 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5678 | p+=4; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5679 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5680 | if (logging != MagickFalse) |
| 5681 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 5682 | " Framing_delay=%.20g",(double) frame_delay); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5683 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5684 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5685 | if (change_timeout) |
| 5686 | { |
glennrp | bb010dd | 2010-06-01 13:07:15 +0000 | [diff] [blame] | 5687 | frame_timeout=1UL*image->ticks_per_second* |
| 5688 | mng_get_long(p); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5689 | |
glennrp | bb010dd | 2010-06-01 13:07:15 +0000 | [diff] [blame] | 5690 | if (mng_info->ticks_per_second != 0) |
| 5691 | frame_timeout/=mng_info->ticks_per_second; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5692 | |
glennrp | bb010dd | 2010-06-01 13:07:15 +0000 | [diff] [blame] | 5693 | else |
| 5694 | frame_timeout=PNG_UINT_31_MAX; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5695 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5696 | if (change_delay == 2) |
| 5697 | default_frame_timeout=frame_timeout; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5698 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5699 | p+=4; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5700 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5701 | if (logging != MagickFalse) |
| 5702 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 5703 | " Framing_timeout=%.20g",(double) frame_timeout); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5704 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5705 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5706 | if (change_clipping) |
| 5707 | { |
| 5708 | fb=mng_read_box(previous_fb,(char) p[0],&p[1]); |
| 5709 | p+=17; |
| 5710 | previous_fb=fb; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5711 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5712 | if (logging != MagickFalse) |
| 5713 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5714 | " Frame_clip: L=%.20g R=%.20g T=%.20g B=%.20g", |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 5715 | (double) fb.left,(double) fb.right,(double) fb.top, |
| 5716 | (double) fb.bottom); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5717 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5718 | if (change_clipping == 2) |
| 5719 | default_fb=fb; |
| 5720 | } |
| 5721 | } |
| 5722 | } |
| 5723 | mng_info->clip=fb; |
| 5724 | mng_info->clip=mng_minimum_box(fb,mng_info->frame); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5725 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 5726 | subframe_width=(size_t) (mng_info->clip.right |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5727 | -mng_info->clip.left); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5728 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 5729 | subframe_height=(size_t) (mng_info->clip.bottom |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5730 | -mng_info->clip.top); |
| 5731 | /* |
| 5732 | Insert a background layer behind the frame if framing_mode is 4. |
| 5733 | */ |
| 5734 | #if defined(MNG_INSERT_LAYERS) |
| 5735 | if (logging != MagickFalse) |
| 5736 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 5737 | " subframe_width=%.20g, subframe_height=%.20g",(double) |
| 5738 | subframe_width,(double) subframe_height); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5739 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5740 | if (insert_layers && (mng_info->framing_mode == 4) && |
| 5741 | (subframe_width) && (subframe_height)) |
| 5742 | { |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5743 | /* Allocate next image structure. */ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 5744 | if (GetAuthenticPixelQueue(image) != (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5745 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 5746 | AcquireNextImage(image_info,image,exception); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5747 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5748 | if (GetNextImageInList(image) == (Image *) NULL) |
| 5749 | { |
| 5750 | image=DestroyImageList(image); |
| 5751 | MngInfoFreeStruct(mng_info,&have_mng_structure); |
| 5752 | return((Image *) NULL); |
| 5753 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5754 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5755 | image=SyncNextImageInList(image); |
| 5756 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5757 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5758 | mng_info->image=image; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5759 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5760 | if (term_chunk_found) |
| 5761 | { |
| 5762 | image->start_loop=MagickTrue; |
| 5763 | image->iterations=mng_iterations; |
| 5764 | term_chunk_found=MagickFalse; |
| 5765 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5766 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5767 | else |
| 5768 | image->start_loop=MagickFalse; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5769 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5770 | image->columns=subframe_width; |
| 5771 | image->rows=subframe_height; |
| 5772 | image->page.width=subframe_width; |
| 5773 | image->page.height=subframe_height; |
| 5774 | image->page.x=mng_info->clip.left; |
| 5775 | image->page.y=mng_info->clip.top; |
| 5776 | image->background_color=mng_background_color; |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 5777 | image->alpha_trait=UndefinedPixelTrait; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5778 | image->delay=0; |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 5779 | (void) SetImageBackgroundColor(image,exception); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5780 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5781 | if (logging != MagickFalse) |
| 5782 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5783 | " Insert backgd layer, L=%.20g, R=%.20g T=%.20g, B=%.20g", |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 5784 | (double) mng_info->clip.left,(double) mng_info->clip.right, |
| 5785 | (double) mng_info->clip.top,(double) mng_info->clip.bottom); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5786 | } |
| 5787 | #endif |
| 5788 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 5789 | continue; |
| 5790 | } |
| 5791 | if (memcmp(type,mng_CLIP,4) == 0) |
| 5792 | { |
| 5793 | unsigned int |
| 5794 | first_object, |
| 5795 | last_object; |
| 5796 | |
| 5797 | /* |
| 5798 | Read CLIP. |
| 5799 | */ |
| 5800 | first_object=(p[0] << 8) | p[1]; |
| 5801 | last_object=(p[2] << 8) | p[3]; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5802 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5803 | for (i=(int) first_object; i <= (int) last_object; i++) |
| 5804 | { |
| 5805 | if (mng_info->exists[i] && !mng_info->frozen[i]) |
| 5806 | { |
| 5807 | MngBox |
| 5808 | box; |
| 5809 | |
| 5810 | box=mng_info->object_clip[i]; |
| 5811 | mng_info->object_clip[i]=mng_read_box(box,(char) p[4],&p[5]); |
| 5812 | } |
| 5813 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5814 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5815 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 5816 | continue; |
| 5817 | } |
| 5818 | if (memcmp(type,mng_SAVE,4) == 0) |
| 5819 | { |
| 5820 | for (i=1; i < MNG_MAX_OBJECTS; i++) |
| 5821 | if (mng_info->exists[i]) |
| 5822 | { |
| 5823 | mng_info->frozen[i]=MagickTrue; |
| 5824 | #ifdef MNG_OBJECT_BUFFERS |
| 5825 | if (mng_info->ob[i] != (MngBuffer *) NULL) |
| 5826 | mng_info->ob[i]->frozen=MagickTrue; |
| 5827 | #endif |
| 5828 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5829 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5830 | if (length) |
| 5831 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5832 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5833 | continue; |
| 5834 | } |
| 5835 | |
| 5836 | if ((memcmp(type,mng_DISC,4) == 0) || (memcmp(type,mng_SEEK,4) == 0)) |
| 5837 | { |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5838 | /* Read DISC or SEEK. */ |
| 5839 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5840 | if ((length == 0) || !memcmp(type,mng_SEEK,4)) |
| 5841 | { |
| 5842 | for (i=1; i < MNG_MAX_OBJECTS; i++) |
| 5843 | MngInfoDiscardObject(mng_info,i); |
| 5844 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5845 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5846 | else |
| 5847 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 5848 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5849 | j; |
| 5850 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 5851 | for (j=0; j < (ssize_t) length; j+=2) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5852 | { |
| 5853 | i=p[j] << 8 | p[j+1]; |
| 5854 | MngInfoDiscardObject(mng_info,i); |
| 5855 | } |
| 5856 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5857 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5858 | if (length) |
| 5859 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5860 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5861 | continue; |
| 5862 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5863 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5864 | if (memcmp(type,mng_MOVE,4) == 0) |
| 5865 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 5866 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5867 | first_object, |
| 5868 | last_object; |
| 5869 | |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5870 | /* read MOVE */ |
| 5871 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5872 | first_object=(p[0] << 8) | p[1]; |
| 5873 | last_object=(p[2] << 8) | p[3]; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 5874 | for (i=(ssize_t) first_object; i <= (ssize_t) last_object; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5875 | { |
| 5876 | if (mng_info->exists[i] && !mng_info->frozen[i]) |
| 5877 | { |
| 5878 | MngPair |
| 5879 | new_pair; |
| 5880 | |
| 5881 | MngPair |
| 5882 | old_pair; |
| 5883 | |
| 5884 | old_pair.a=mng_info->x_off[i]; |
| 5885 | old_pair.b=mng_info->y_off[i]; |
| 5886 | new_pair=mng_read_pair(old_pair,(int) p[4],&p[5]); |
| 5887 | mng_info->x_off[i]=new_pair.a; |
| 5888 | mng_info->y_off[i]=new_pair.b; |
| 5889 | } |
| 5890 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5891 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5892 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 5893 | continue; |
| 5894 | } |
| 5895 | |
| 5896 | if (memcmp(type,mng_LOOP,4) == 0) |
| 5897 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 5898 | ssize_t loop_iters=1; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5899 | loop_level=chunk[0]; |
| 5900 | mng_info->loop_active[loop_level]=1; /* mark loop active */ |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5901 | |
| 5902 | /* Record starting point. */ |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 5903 | loop_iters=mng_get_long(&chunk[1]); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5904 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5905 | if (logging != MagickFalse) |
| 5906 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 5907 | " LOOP level %.20g has %.20g iterations ",(double) loop_level, |
| 5908 | (double) loop_iters); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5909 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5910 | if (loop_iters == 0) |
| 5911 | skipping_loop=loop_level; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5912 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5913 | else |
| 5914 | { |
| 5915 | mng_info->loop_jump[loop_level]=TellBlob(image); |
| 5916 | mng_info->loop_count[loop_level]=loop_iters; |
| 5917 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5918 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5919 | mng_info->loop_iteration[loop_level]=0; |
| 5920 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 5921 | continue; |
| 5922 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5923 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5924 | if (memcmp(type,mng_ENDL,4) == 0) |
| 5925 | { |
| 5926 | loop_level=chunk[0]; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5927 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5928 | if (skipping_loop > 0) |
| 5929 | { |
| 5930 | if (skipping_loop == loop_level) |
| 5931 | { |
| 5932 | /* |
| 5933 | Found end of zero-iteration loop. |
| 5934 | */ |
| 5935 | skipping_loop=(-1); |
| 5936 | mng_info->loop_active[loop_level]=0; |
| 5937 | } |
| 5938 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5939 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5940 | else |
| 5941 | { |
| 5942 | if (mng_info->loop_active[loop_level] == 1) |
| 5943 | { |
| 5944 | mng_info->loop_count[loop_level]--; |
| 5945 | mng_info->loop_iteration[loop_level]++; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5946 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5947 | if (logging != MagickFalse) |
| 5948 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5949 | " ENDL: LOOP level %.20g has %.20g remaining iters ", |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 5950 | (double) loop_level,(double) |
cristy | f2faecf | 2010-05-28 19:19:36 +0000 | [diff] [blame] | 5951 | mng_info->loop_count[loop_level]); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5952 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5953 | if (mng_info->loop_count[loop_level] != 0) |
| 5954 | { |
| 5955 | offset=SeekBlob(image,mng_info->loop_jump[loop_level], |
| 5956 | SEEK_SET); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5957 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5958 | if (offset < 0) |
| 5959 | ThrowReaderException(CorruptImageError, |
| 5960 | "ImproperImageHeader"); |
| 5961 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5962 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5963 | else |
| 5964 | { |
| 5965 | short |
| 5966 | last_level; |
| 5967 | |
| 5968 | /* |
| 5969 | Finished loop. |
| 5970 | */ |
| 5971 | mng_info->loop_active[loop_level]=0; |
| 5972 | last_level=(-1); |
| 5973 | for (i=0; i < loop_level; i++) |
| 5974 | if (mng_info->loop_active[i] == 1) |
| 5975 | last_level=(short) i; |
| 5976 | loop_level=last_level; |
| 5977 | } |
| 5978 | } |
| 5979 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5980 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5981 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 5982 | continue; |
| 5983 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5984 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5985 | if (memcmp(type,mng_CLON,4) == 0) |
| 5986 | { |
| 5987 | if (mng_info->clon_warning == 0) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 5988 | (void) ThrowMagickException(exception,GetMagickModule(), |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5989 | CoderError,"CLON is not implemented yet","`%s'", |
| 5990 | image->filename); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5991 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5992 | mng_info->clon_warning++; |
| 5993 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5994 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5995 | if (memcmp(type,mng_MAGN,4) == 0) |
| 5996 | { |
| 5997 | png_uint_16 |
| 5998 | magn_first, |
| 5999 | magn_last, |
| 6000 | magn_mb, |
| 6001 | magn_ml, |
| 6002 | magn_mr, |
| 6003 | magn_mt, |
| 6004 | magn_mx, |
| 6005 | magn_my, |
| 6006 | magn_methx, |
| 6007 | magn_methy; |
| 6008 | |
| 6009 | if (length > 1) |
| 6010 | magn_first=(p[0] << 8) | p[1]; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 6011 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6012 | else |
| 6013 | magn_first=0; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 6014 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6015 | if (length > 3) |
| 6016 | magn_last=(p[2] << 8) | p[3]; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 6017 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6018 | else |
| 6019 | magn_last=magn_first; |
| 6020 | #ifndef MNG_OBJECT_BUFFERS |
| 6021 | if (magn_first || magn_last) |
| 6022 | if (mng_info->magn_warning == 0) |
| 6023 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6024 | (void) ThrowMagickException(exception, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6025 | GetMagickModule(),CoderError, |
| 6026 | "MAGN is not implemented yet for nonzero objects", |
| 6027 | "`%s'",image->filename); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6028 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6029 | mng_info->magn_warning++; |
| 6030 | } |
| 6031 | #endif |
| 6032 | if (length > 4) |
| 6033 | magn_methx=p[4]; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6034 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6035 | else |
| 6036 | magn_methx=0; |
| 6037 | |
| 6038 | if (length > 6) |
| 6039 | magn_mx=(p[5] << 8) | p[6]; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6040 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6041 | else |
| 6042 | magn_mx=1; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6043 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6044 | if (magn_mx == 0) |
| 6045 | magn_mx=1; |
| 6046 | |
| 6047 | if (length > 8) |
| 6048 | magn_my=(p[7] << 8) | p[8]; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6049 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6050 | else |
| 6051 | magn_my=magn_mx; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6052 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6053 | if (magn_my == 0) |
| 6054 | magn_my=1; |
| 6055 | |
| 6056 | if (length > 10) |
| 6057 | magn_ml=(p[9] << 8) | p[10]; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6058 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6059 | else |
| 6060 | magn_ml=magn_mx; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6061 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6062 | if (magn_ml == 0) |
| 6063 | magn_ml=1; |
| 6064 | |
| 6065 | if (length > 12) |
| 6066 | magn_mr=(p[11] << 8) | p[12]; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6067 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6068 | else |
| 6069 | magn_mr=magn_mx; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6070 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6071 | if (magn_mr == 0) |
| 6072 | magn_mr=1; |
| 6073 | |
| 6074 | if (length > 14) |
| 6075 | magn_mt=(p[13] << 8) | p[14]; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6076 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6077 | else |
| 6078 | magn_mt=magn_my; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6079 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6080 | if (magn_mt == 0) |
| 6081 | magn_mt=1; |
| 6082 | |
| 6083 | if (length > 16) |
| 6084 | magn_mb=(p[15] << 8) | p[16]; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6085 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6086 | else |
| 6087 | magn_mb=magn_my; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6088 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6089 | if (magn_mb == 0) |
| 6090 | magn_mb=1; |
| 6091 | |
| 6092 | if (length > 17) |
| 6093 | magn_methy=p[17]; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6094 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6095 | else |
| 6096 | magn_methy=magn_methx; |
| 6097 | |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6098 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6099 | if (magn_methx > 5 || magn_methy > 5) |
| 6100 | if (mng_info->magn_warning == 0) |
| 6101 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6102 | (void) ThrowMagickException(exception, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6103 | GetMagickModule(),CoderError, |
| 6104 | "Unknown MAGN method in MNG datastream","`%s'", |
| 6105 | image->filename); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6106 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6107 | mng_info->magn_warning++; |
| 6108 | } |
| 6109 | #ifdef MNG_OBJECT_BUFFERS |
| 6110 | /* Magnify existing objects in the range magn_first to magn_last */ |
| 6111 | #endif |
| 6112 | if (magn_first == 0 || magn_last == 0) |
| 6113 | { |
| 6114 | /* Save the magnification factors for object 0 */ |
| 6115 | mng_info->magn_mb=magn_mb; |
| 6116 | mng_info->magn_ml=magn_ml; |
| 6117 | mng_info->magn_mr=magn_mr; |
| 6118 | mng_info->magn_mt=magn_mt; |
| 6119 | mng_info->magn_mx=magn_mx; |
| 6120 | mng_info->magn_my=magn_my; |
| 6121 | mng_info->magn_methx=magn_methx; |
| 6122 | mng_info->magn_methy=magn_methy; |
| 6123 | } |
| 6124 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6125 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6126 | if (memcmp(type,mng_PAST,4) == 0) |
| 6127 | { |
| 6128 | if (mng_info->past_warning == 0) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6129 | (void) ThrowMagickException(exception,GetMagickModule(), |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6130 | CoderError,"PAST is not implemented yet","`%s'", |
| 6131 | image->filename); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6132 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6133 | mng_info->past_warning++; |
| 6134 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6135 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6136 | if (memcmp(type,mng_SHOW,4) == 0) |
| 6137 | { |
| 6138 | if (mng_info->show_warning == 0) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6139 | (void) ThrowMagickException(exception,GetMagickModule(), |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6140 | CoderError,"SHOW is not implemented yet","`%s'", |
| 6141 | image->filename); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6142 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6143 | mng_info->show_warning++; |
| 6144 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6145 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6146 | if (memcmp(type,mng_sBIT,4) == 0) |
| 6147 | { |
| 6148 | if (length < 4) |
| 6149 | mng_info->have_global_sbit=MagickFalse; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6150 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6151 | else |
| 6152 | { |
| 6153 | mng_info->global_sbit.gray=p[0]; |
| 6154 | mng_info->global_sbit.red=p[0]; |
| 6155 | mng_info->global_sbit.green=p[1]; |
| 6156 | mng_info->global_sbit.blue=p[2]; |
| 6157 | mng_info->global_sbit.alpha=p[3]; |
| 6158 | mng_info->have_global_sbit=MagickTrue; |
| 6159 | } |
| 6160 | } |
| 6161 | if (memcmp(type,mng_pHYs,4) == 0) |
| 6162 | { |
| 6163 | if (length > 8) |
| 6164 | { |
| 6165 | mng_info->global_x_pixels_per_unit= |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 6166 | (size_t) mng_get_long(p); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6167 | mng_info->global_y_pixels_per_unit= |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 6168 | (size_t) mng_get_long(&p[4]); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6169 | mng_info->global_phys_unit_type=p[8]; |
| 6170 | mng_info->have_global_phys=MagickTrue; |
| 6171 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6172 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6173 | else |
| 6174 | mng_info->have_global_phys=MagickFalse; |
| 6175 | } |
| 6176 | if (memcmp(type,mng_pHYg,4) == 0) |
| 6177 | { |
| 6178 | if (mng_info->phyg_warning == 0) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6179 | (void) ThrowMagickException(exception,GetMagickModule(), |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6180 | CoderError,"pHYg is not implemented.","`%s'",image->filename); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6181 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6182 | mng_info->phyg_warning++; |
| 6183 | } |
| 6184 | if (memcmp(type,mng_BASI,4) == 0) |
| 6185 | { |
| 6186 | skip_to_iend=MagickTrue; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6187 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6188 | if (mng_info->basi_warning == 0) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6189 | (void) ThrowMagickException(exception,GetMagickModule(), |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6190 | CoderError,"BASI is not implemented yet","`%s'", |
| 6191 | image->filename); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6192 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6193 | mng_info->basi_warning++; |
| 6194 | #ifdef MNG_BASI_SUPPORTED |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 6195 | basi_width=(size_t) ((p[0] << 24) | (p[1] << 16) | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6196 | (p[2] << 8) | p[3]); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 6197 | basi_height=(size_t) ((p[4] << 24) | (p[5] << 16) | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6198 | (p[6] << 8) | p[7]); |
| 6199 | basi_color_type=p[8]; |
| 6200 | basi_compression_method=p[9]; |
| 6201 | basi_filter_type=p[10]; |
| 6202 | basi_interlace_method=p[11]; |
| 6203 | if (length > 11) |
| 6204 | basi_red=(p[12] << 8) & p[13]; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6205 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6206 | else |
| 6207 | basi_red=0; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6208 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6209 | if (length > 13) |
| 6210 | basi_green=(p[14] << 8) & p[15]; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6211 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6212 | else |
| 6213 | basi_green=0; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6214 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6215 | if (length > 15) |
| 6216 | basi_blue=(p[16] << 8) & p[17]; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6217 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6218 | else |
| 6219 | basi_blue=0; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6220 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6221 | if (length > 17) |
| 6222 | basi_alpha=(p[18] << 8) & p[19]; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6223 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6224 | else |
| 6225 | { |
| 6226 | if (basi_sample_depth == 16) |
| 6227 | basi_alpha=65535L; |
| 6228 | else |
| 6229 | basi_alpha=255; |
| 6230 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6231 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6232 | if (length > 19) |
| 6233 | basi_viewable=p[20]; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6234 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6235 | else |
| 6236 | basi_viewable=0; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6237 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6238 | #endif |
| 6239 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 6240 | continue; |
| 6241 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6242 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6243 | if (memcmp(type,mng_IHDR,4) |
| 6244 | #if defined(JNG_SUPPORTED) |
| 6245 | && memcmp(type,mng_JHDR,4) |
| 6246 | #endif |
| 6247 | ) |
| 6248 | { |
| 6249 | /* Not an IHDR or JHDR chunk */ |
| 6250 | if (length) |
| 6251 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6252 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6253 | continue; |
| 6254 | } |
| 6255 | /* Process IHDR */ |
| 6256 | if (logging != MagickFalse) |
| 6257 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 6258 | " Processing %c%c%c%c chunk",type[0],type[1],type[2],type[3]); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6259 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6260 | mng_info->exists[object_id]=MagickTrue; |
| 6261 | mng_info->viewable[object_id]=MagickTrue; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6262 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6263 | if (mng_info->invisible[object_id]) |
| 6264 | { |
| 6265 | if (logging != MagickFalse) |
| 6266 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 6267 | " Skipping invisible object"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6268 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6269 | skip_to_iend=MagickTrue; |
| 6270 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 6271 | continue; |
| 6272 | } |
| 6273 | #if defined(MNG_INSERT_LAYERS) |
| 6274 | if (length < 8) |
| 6275 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6276 | |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 6277 | image_width=(size_t) mng_get_long(p); |
| 6278 | image_height=(size_t) mng_get_long(&p[4]); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6279 | #endif |
| 6280 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 6281 | |
| 6282 | /* |
| 6283 | Insert a transparent background layer behind the entire animation |
| 6284 | if it is not full screen. |
| 6285 | */ |
| 6286 | #if defined(MNG_INSERT_LAYERS) |
| 6287 | if (insert_layers && mng_type && first_mng_object) |
| 6288 | { |
| 6289 | if ((mng_info->clip.left > 0) || (mng_info->clip.top > 0) || |
| 6290 | (image_width < mng_info->mng_width) || |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 6291 | (mng_info->clip.right < (ssize_t) mng_info->mng_width) || |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6292 | (image_height < mng_info->mng_height) || |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 6293 | (mng_info->clip.bottom < (ssize_t) mng_info->mng_height)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6294 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6295 | if (GetAuthenticPixelQueue(image) != (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6296 | { |
| 6297 | /* |
| 6298 | Allocate next image structure. |
| 6299 | */ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6300 | AcquireNextImage(image_info,image,exception); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6301 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6302 | if (GetNextImageInList(image) == (Image *) NULL) |
| 6303 | { |
| 6304 | image=DestroyImageList(image); |
| 6305 | MngInfoFreeStruct(mng_info,&have_mng_structure); |
| 6306 | return((Image *) NULL); |
| 6307 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6308 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6309 | image=SyncNextImageInList(image); |
| 6310 | } |
| 6311 | mng_info->image=image; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6312 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6313 | if (term_chunk_found) |
| 6314 | { |
| 6315 | image->start_loop=MagickTrue; |
| 6316 | image->iterations=mng_iterations; |
| 6317 | term_chunk_found=MagickFalse; |
| 6318 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6319 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6320 | else |
| 6321 | image->start_loop=MagickFalse; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6322 | |
| 6323 | /* Make a background rectangle. */ |
| 6324 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6325 | image->delay=0; |
| 6326 | image->columns=mng_info->mng_width; |
| 6327 | image->rows=mng_info->mng_height; |
| 6328 | image->page.width=mng_info->mng_width; |
| 6329 | image->page.height=mng_info->mng_height; |
| 6330 | image->page.x=0; |
| 6331 | image->page.y=0; |
| 6332 | image->background_color=mng_background_color; |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6333 | (void) SetImageBackgroundColor(image,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6334 | if (logging != MagickFalse) |
| 6335 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 6336 | " Inserted transparent background layer, W=%.20g, H=%.20g", |
| 6337 | (double) mng_info->mng_width,(double) mng_info->mng_height); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6338 | } |
| 6339 | } |
| 6340 | /* |
| 6341 | Insert a background layer behind the upcoming image if |
| 6342 | framing_mode is 3, and we haven't already inserted one. |
| 6343 | */ |
| 6344 | if (insert_layers && (mng_info->framing_mode == 3) && |
| 6345 | (subframe_width) && (subframe_height) && (simplicity == 0 || |
| 6346 | (simplicity & 0x08))) |
| 6347 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6348 | if (GetAuthenticPixelQueue(image) != (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6349 | { |
| 6350 | /* |
| 6351 | Allocate next image structure. |
| 6352 | */ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6353 | AcquireNextImage(image_info,image,exception); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6354 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6355 | if (GetNextImageInList(image) == (Image *) NULL) |
| 6356 | { |
| 6357 | image=DestroyImageList(image); |
| 6358 | MngInfoFreeStruct(mng_info,&have_mng_structure); |
| 6359 | return((Image *) NULL); |
| 6360 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6361 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6362 | image=SyncNextImageInList(image); |
| 6363 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 6364 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6365 | mng_info->image=image; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 6366 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6367 | if (term_chunk_found) |
| 6368 | { |
| 6369 | image->start_loop=MagickTrue; |
| 6370 | image->iterations=mng_iterations; |
| 6371 | term_chunk_found=MagickFalse; |
| 6372 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 6373 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6374 | else |
| 6375 | image->start_loop=MagickFalse; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6376 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6377 | image->delay=0; |
| 6378 | image->columns=subframe_width; |
| 6379 | image->rows=subframe_height; |
| 6380 | image->page.width=subframe_width; |
| 6381 | image->page.height=subframe_height; |
| 6382 | image->page.x=mng_info->clip.left; |
| 6383 | image->page.y=mng_info->clip.top; |
| 6384 | image->background_color=mng_background_color; |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 6385 | image->alpha_trait=UndefinedPixelTrait; |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6386 | (void) SetImageBackgroundColor(image,exception); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 6387 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6388 | if (logging != MagickFalse) |
| 6389 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 6390 | " Insert background layer, L=%.20g, R=%.20g T=%.20g, B=%.20g", |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 6391 | (double) mng_info->clip.left,(double) mng_info->clip.right, |
| 6392 | (double) mng_info->clip.top,(double) mng_info->clip.bottom); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6393 | } |
| 6394 | #endif /* MNG_INSERT_LAYERS */ |
| 6395 | first_mng_object=MagickFalse; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6396 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6397 | if (GetAuthenticPixelQueue(image) != (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6398 | { |
| 6399 | /* |
| 6400 | Allocate next image structure. |
| 6401 | */ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6402 | AcquireNextImage(image_info,image,exception); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6403 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6404 | if (GetNextImageInList(image) == (Image *) NULL) |
| 6405 | { |
| 6406 | image=DestroyImageList(image); |
| 6407 | MngInfoFreeStruct(mng_info,&have_mng_structure); |
| 6408 | return((Image *) NULL); |
| 6409 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6410 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6411 | image=SyncNextImageInList(image); |
| 6412 | } |
| 6413 | mng_info->image=image; |
| 6414 | status=SetImageProgress(image,LoadImagesTag,TellBlob(image), |
| 6415 | GetBlobSize(image)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 6416 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6417 | if (status == MagickFalse) |
| 6418 | break; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 6419 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6420 | if (term_chunk_found) |
| 6421 | { |
| 6422 | image->start_loop=MagickTrue; |
| 6423 | term_chunk_found=MagickFalse; |
| 6424 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 6425 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6426 | else |
| 6427 | image->start_loop=MagickFalse; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 6428 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6429 | if (mng_info->framing_mode == 1 || mng_info->framing_mode == 3) |
| 6430 | { |
| 6431 | image->delay=frame_delay; |
| 6432 | frame_delay=default_frame_delay; |
| 6433 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 6434 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6435 | else |
| 6436 | image->delay=0; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 6437 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6438 | image->page.width=mng_info->mng_width; |
| 6439 | image->page.height=mng_info->mng_height; |
| 6440 | image->page.x=mng_info->x_off[object_id]; |
| 6441 | image->page.y=mng_info->y_off[object_id]; |
| 6442 | image->iterations=mng_iterations; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6443 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6444 | /* |
| 6445 | Seek back to the beginning of the IHDR or JHDR chunk's length field. |
| 6446 | */ |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6447 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6448 | if (logging != MagickFalse) |
| 6449 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 6450 | " Seeking back to beginning of %c%c%c%c chunk",type[0],type[1], |
| 6451 | type[2],type[3]); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6452 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 6453 | offset=SeekBlob(image,-((ssize_t) length+12),SEEK_CUR); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6454 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6455 | if (offset < 0) |
| 6456 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
| 6457 | } |
| 6458 | |
| 6459 | previous=image; |
| 6460 | mng_info->image=image; |
| 6461 | mng_info->mng_type=mng_type; |
| 6462 | mng_info->object_id=object_id; |
| 6463 | |
| 6464 | if (memcmp(type,mng_IHDR,4) == 0) |
| 6465 | image=ReadOnePNGImage(mng_info,image_info,exception); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6466 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6467 | #if defined(JNG_SUPPORTED) |
| 6468 | else |
| 6469 | image=ReadOneJNGImage(mng_info,image_info,exception); |
| 6470 | #endif |
| 6471 | |
| 6472 | if (image == (Image *) NULL) |
| 6473 | { |
| 6474 | if (IsImageObject(previous) != MagickFalse) |
| 6475 | { |
| 6476 | (void) DestroyImageList(previous); |
| 6477 | (void) CloseBlob(previous); |
| 6478 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6479 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6480 | MngInfoFreeStruct(mng_info,&have_mng_structure); |
| 6481 | return((Image *) NULL); |
| 6482 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 6483 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6484 | if (image->columns == 0 || image->rows == 0) |
| 6485 | { |
| 6486 | (void) CloseBlob(image); |
| 6487 | image=DestroyImageList(image); |
| 6488 | MngInfoFreeStruct(mng_info,&have_mng_structure); |
| 6489 | return((Image *) NULL); |
| 6490 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 6491 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6492 | mng_info->image=image; |
| 6493 | |
| 6494 | if (mng_type) |
| 6495 | { |
| 6496 | MngBox |
| 6497 | crop_box; |
| 6498 | |
| 6499 | if (mng_info->magn_methx || mng_info->magn_methy) |
| 6500 | { |
| 6501 | png_uint_32 |
| 6502 | magnified_height, |
| 6503 | magnified_width; |
| 6504 | |
| 6505 | if (logging != MagickFalse) |
| 6506 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 6507 | " Processing MNG MAGN chunk"); |
| 6508 | |
| 6509 | if (mng_info->magn_methx == 1) |
| 6510 | { |
| 6511 | magnified_width=mng_info->magn_ml; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6512 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6513 | if (image->columns > 1) |
| 6514 | magnified_width += mng_info->magn_mr; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6515 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6516 | if (image->columns > 2) |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6517 | magnified_width += (png_uint_32) |
| 6518 | ((image->columns-2)*(mng_info->magn_mx)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6519 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6520 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6521 | else |
| 6522 | { |
cristy | 4e5bc84 | 2010-06-09 13:56:01 +0000 | [diff] [blame] | 6523 | magnified_width=(png_uint_32) image->columns; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6524 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6525 | if (image->columns > 1) |
| 6526 | magnified_width += mng_info->magn_ml-1; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6527 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6528 | if (image->columns > 2) |
| 6529 | magnified_width += mng_info->magn_mr-1; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6530 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6531 | if (image->columns > 3) |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6532 | magnified_width += (png_uint_32) |
| 6533 | ((image->columns-3)*(mng_info->magn_mx-1)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6534 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6535 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6536 | if (mng_info->magn_methy == 1) |
| 6537 | { |
| 6538 | magnified_height=mng_info->magn_mt; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6539 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6540 | if (image->rows > 1) |
| 6541 | magnified_height += mng_info->magn_mb; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6542 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6543 | if (image->rows > 2) |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6544 | magnified_height += (png_uint_32) |
| 6545 | ((image->rows-2)*(mng_info->magn_my)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6546 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6547 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6548 | else |
| 6549 | { |
cristy | 4e5bc84 | 2010-06-09 13:56:01 +0000 | [diff] [blame] | 6550 | magnified_height=(png_uint_32) image->rows; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6551 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6552 | if (image->rows > 1) |
| 6553 | magnified_height += mng_info->magn_mt-1; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6554 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6555 | if (image->rows > 2) |
| 6556 | magnified_height += mng_info->magn_mb-1; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6557 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6558 | if (image->rows > 3) |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6559 | magnified_height += (png_uint_32) |
| 6560 | ((image->rows-3)*(mng_info->magn_my-1)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6561 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6562 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6563 | if (magnified_height > image->rows || |
| 6564 | magnified_width > image->columns) |
| 6565 | { |
| 6566 | Image |
| 6567 | *large_image; |
| 6568 | |
| 6569 | int |
| 6570 | yy; |
| 6571 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6572 | Quantum |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6573 | *next, |
| 6574 | *prev; |
| 6575 | |
| 6576 | png_uint_16 |
| 6577 | magn_methx, |
| 6578 | magn_methy; |
| 6579 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6580 | ssize_t |
| 6581 | m, |
| 6582 | y; |
| 6583 | |
| 6584 | register Quantum |
| 6585 | *n, |
| 6586 | *q; |
| 6587 | |
| 6588 | register ssize_t |
| 6589 | x; |
| 6590 | |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6591 | /* Allocate next image structure. */ |
| 6592 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6593 | if (logging != MagickFalse) |
| 6594 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 6595 | " Allocate magnified image"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6596 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6597 | AcquireNextImage(image_info,image,exception); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6598 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6599 | if (GetNextImageInList(image) == (Image *) NULL) |
| 6600 | { |
| 6601 | image=DestroyImageList(image); |
| 6602 | MngInfoFreeStruct(mng_info,&have_mng_structure); |
| 6603 | return((Image *) NULL); |
| 6604 | } |
| 6605 | |
| 6606 | large_image=SyncNextImageInList(image); |
| 6607 | |
| 6608 | large_image->columns=magnified_width; |
| 6609 | large_image->rows=magnified_height; |
| 6610 | |
| 6611 | magn_methx=mng_info->magn_methx; |
| 6612 | magn_methy=mng_info->magn_methy; |
| 6613 | |
glennrp | 3faa9a3 | 2011-04-23 14:00:25 +0000 | [diff] [blame] | 6614 | #if (MAGICKCORE_QUANTUM_DEPTH > 16) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6615 | #define QM unsigned short |
| 6616 | if (magn_methx != 1 || magn_methy != 1) |
| 6617 | { |
| 6618 | /* |
| 6619 | Scale pixels to unsigned shorts to prevent |
| 6620 | overflow of intermediate values of interpolations |
| 6621 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 6622 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6623 | { |
| 6624 | q=GetAuthenticPixels(image,0,y,image->columns,1, |
| 6625 | exception); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6626 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 6627 | for (x=(ssize_t) image->columns-1; x >= 0; x--) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6628 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6629 | SetPixelRed(image,ScaleQuantumToShort( |
| 6630 | GetPixelRed(image,q)),q); |
| 6631 | SetPixelGreen(image,ScaleQuantumToShort( |
| 6632 | GetPixelGreen(image,q)),q); |
| 6633 | SetPixelBlue(image,ScaleQuantumToShort( |
| 6634 | GetPixelBlue(image,q)),q); |
| 6635 | SetPixelAlpha(image,ScaleQuantumToShort( |
| 6636 | GetPixelAlpha(image,q)),q); |
| 6637 | q+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6638 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6639 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6640 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 6641 | break; |
| 6642 | } |
| 6643 | } |
| 6644 | #else |
| 6645 | #define QM Quantum |
| 6646 | #endif |
| 6647 | |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 6648 | if (image->alpha_trait == BlendPixelTrait) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6649 | (void) SetImageBackgroundColor(large_image,exception); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6650 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6651 | else |
| 6652 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6653 | large_image->background_color.alpha=OpaqueAlpha; |
| 6654 | (void) SetImageBackgroundColor(large_image,exception); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6655 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6656 | if (magn_methx == 4) |
| 6657 | magn_methx=2; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6658 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6659 | if (magn_methx == 5) |
| 6660 | magn_methx=3; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6661 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6662 | if (magn_methy == 4) |
| 6663 | magn_methy=2; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6664 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6665 | if (magn_methy == 5) |
| 6666 | magn_methy=3; |
| 6667 | } |
| 6668 | |
| 6669 | /* magnify the rows into the right side of the large image */ |
| 6670 | |
| 6671 | if (logging != MagickFalse) |
| 6672 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 6673 | " Magnify the rows to %.20g",(double) large_image->rows); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 6674 | m=(ssize_t) mng_info->magn_mt; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6675 | yy=0; |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6676 | length=(size_t) image->columns*GetPixelChannels(image); |
| 6677 | next=(Quantum *) AcquireQuantumMemory(length,sizeof(*next)); |
| 6678 | prev=(Quantum *) AcquireQuantumMemory(length,sizeof(*prev)); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6679 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6680 | if ((prev == (Quantum *) NULL) || |
| 6681 | (next == (Quantum *) NULL)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6682 | { |
| 6683 | image=DestroyImageList(image); |
| 6684 | MngInfoFreeStruct(mng_info,&have_mng_structure); |
| 6685 | ThrowReaderException(ResourceLimitError, |
| 6686 | "MemoryAllocationFailed"); |
| 6687 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6688 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6689 | n=GetAuthenticPixels(image,0,0,image->columns,1,exception); |
| 6690 | (void) CopyMagickMemory(next,n,length); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6691 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 6692 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6693 | { |
| 6694 | if (y == 0) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 6695 | m=(ssize_t) mng_info->magn_mt; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6696 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 6697 | else if (magn_methy > 1 && y == (ssize_t) image->rows-2) |
| 6698 | m=(ssize_t) mng_info->magn_mb; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6699 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 6700 | else if (magn_methy <= 1 && y == (ssize_t) image->rows-1) |
| 6701 | m=(ssize_t) mng_info->magn_mb; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6702 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 6703 | else if (magn_methy > 1 && y == (ssize_t) image->rows-1) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6704 | m=1; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6705 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6706 | else |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 6707 | m=(ssize_t) mng_info->magn_my; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6708 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6709 | n=prev; |
| 6710 | prev=next; |
| 6711 | next=n; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6712 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 6713 | if (y < (ssize_t) image->rows-1) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6714 | { |
| 6715 | n=GetAuthenticPixels(image,0,y+1,image->columns,1, |
| 6716 | exception); |
| 6717 | (void) CopyMagickMemory(next,n,length); |
| 6718 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6719 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6720 | for (i=0; i < m; i++, yy++) |
| 6721 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6722 | register Quantum |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6723 | *pixels; |
| 6724 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 6725 | assert(yy < (ssize_t) large_image->rows); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6726 | pixels=prev; |
| 6727 | n=next; |
| 6728 | q=GetAuthenticPixels(large_image,0,yy,large_image->columns, |
cristy | 9fff7b4 | 2011-04-29 01:09:31 +0000 | [diff] [blame] | 6729 | 1,exception); |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6730 | q+=(large_image->columns-image->columns)* |
| 6731 | GetPixelChannels(large_image); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6732 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 6733 | for (x=(ssize_t) image->columns-1; x >= 0; x--) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6734 | { |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 6735 | /* To do: get color as function of indexes[x] */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6736 | /* |
| 6737 | if (image->storage_class == PseudoClass) |
| 6738 | { |
| 6739 | } |
| 6740 | */ |
| 6741 | |
| 6742 | if (magn_methy <= 1) |
| 6743 | { |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 6744 | /* replicate previous */ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6745 | SetPixelRed(large_image,GetPixelRed(image,pixels),q); |
| 6746 | SetPixelGreen(large_image,GetPixelGreen(image, |
| 6747 | pixels),q); |
| 6748 | SetPixelBlue(large_image,GetPixelBlue(image, |
| 6749 | pixels),q); |
| 6750 | SetPixelAlpha(large_image,GetPixelAlpha(image, |
| 6751 | pixels),q); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6752 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6753 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6754 | else if (magn_methy == 2 || magn_methy == 4) |
| 6755 | { |
| 6756 | if (i == 0) |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 6757 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6758 | SetPixelRed(large_image,GetPixelRed(image, |
| 6759 | pixels),q); |
| 6760 | SetPixelGreen(large_image,GetPixelGreen(image, |
| 6761 | pixels),q); |
| 6762 | SetPixelBlue(large_image,GetPixelBlue(image, |
| 6763 | pixels),q); |
| 6764 | SetPixelAlpha(large_image,GetPixelAlpha(image, |
| 6765 | pixels),q); |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 6766 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6767 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6768 | else |
| 6769 | { |
| 6770 | /* Interpolate */ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6771 | SetPixelRed(large_image,((QM) (((ssize_t) |
| 6772 | (2*i*(GetPixelRed(image,n) |
| 6773 | -GetPixelRed(image,pixels)+m))/ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 6774 | ((ssize_t) (m*2)) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6775 | +GetPixelRed(image,pixels)))),q); |
| 6776 | SetPixelGreen(large_image,((QM) (((ssize_t) |
| 6777 | (2*i*(GetPixelGreen(image,n) |
| 6778 | -GetPixelGreen(image,pixels)+m))/ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 6779 | ((ssize_t) (m*2)) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6780 | +GetPixelGreen(image,pixels)))),q); |
| 6781 | SetPixelBlue(large_image,((QM) (((ssize_t) |
| 6782 | (2*i*(GetPixelBlue(image,n) |
| 6783 | -GetPixelBlue(image,pixels)+m))/ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 6784 | ((ssize_t) (m*2)) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6785 | +GetPixelBlue(image,pixels)))),q); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6786 | |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 6787 | if (image->alpha_trait == BlendPixelTrait) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6788 | SetPixelAlpha(large_image, ((QM) (((ssize_t) |
| 6789 | (2*i*(GetPixelAlpha(image,n) |
| 6790 | -GetPixelAlpha(image,pixels)+m)) |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 6791 | /((ssize_t) (m*2))+ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6792 | GetPixelAlpha(image,pixels)))),q); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6793 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6794 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6795 | if (magn_methy == 4) |
| 6796 | { |
| 6797 | /* Replicate nearest */ |
| 6798 | if (i <= ((m+1) << 1)) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6799 | SetPixelAlpha(large_image,GetPixelAlpha(image, |
| 6800 | pixels),q); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6801 | else |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6802 | SetPixelAlpha(large_image,GetPixelAlpha(image, |
| 6803 | n),q); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6804 | } |
| 6805 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6806 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6807 | else /* if (magn_methy == 3 || magn_methy == 5) */ |
| 6808 | { |
| 6809 | /* Replicate nearest */ |
| 6810 | if (i <= ((m+1) << 1)) |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 6811 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6812 | SetPixelRed(large_image,GetPixelRed(image, |
| 6813 | pixels),q); |
| 6814 | SetPixelGreen(large_image,GetPixelGreen(image, |
| 6815 | pixels),q); |
| 6816 | SetPixelBlue(large_image,GetPixelBlue(image, |
| 6817 | pixels),q); |
| 6818 | SetPixelAlpha(large_image,GetPixelAlpha(image, |
| 6819 | pixels),q); |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 6820 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6821 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6822 | else |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 6823 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6824 | SetPixelRed(large_image,GetPixelRed(image,n),q); |
| 6825 | SetPixelGreen(large_image,GetPixelGreen(image,n), |
| 6826 | q); |
| 6827 | SetPixelBlue(large_image,GetPixelBlue(image,n), |
| 6828 | q); |
| 6829 | SetPixelAlpha(large_image,GetPixelAlpha(image,n), |
| 6830 | q); |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 6831 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6832 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6833 | if (magn_methy == 5) |
| 6834 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6835 | SetPixelAlpha(large_image,(QM) (((ssize_t) (2*i* |
| 6836 | (GetPixelAlpha(image,n) |
| 6837 | -GetPixelAlpha(image,pixels)) |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 6838 | +m))/((ssize_t) (m*2)) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6839 | +GetPixelAlpha(image,pixels)),q); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6840 | } |
| 6841 | } |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6842 | n+=GetPixelChannels(image); |
| 6843 | q+=GetPixelChannels(large_image); |
| 6844 | pixels+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6845 | } /* x */ |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6846 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6847 | if (SyncAuthenticPixels(large_image,exception) == 0) |
| 6848 | break; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6849 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6850 | } /* i */ |
| 6851 | } /* y */ |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6852 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6853 | prev=(Quantum *) RelinquishMagickMemory(prev); |
| 6854 | next=(Quantum *) RelinquishMagickMemory(next); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6855 | |
| 6856 | length=image->columns; |
| 6857 | |
| 6858 | if (logging != MagickFalse) |
| 6859 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 6860 | " Delete original image"); |
| 6861 | |
| 6862 | DeleteImageFromList(&image); |
| 6863 | |
| 6864 | image=large_image; |
| 6865 | |
| 6866 | mng_info->image=image; |
| 6867 | |
| 6868 | /* magnify the columns */ |
| 6869 | if (logging != MagickFalse) |
| 6870 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 6871 | " Magnify the columns to %.20g",(double) image->columns); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6872 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 6873 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6874 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6875 | register Quantum |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6876 | *pixels; |
| 6877 | |
| 6878 | q=GetAuthenticPixels(image,0,y,image->columns,1,exception); |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6879 | pixels=q+(image->columns-length)*GetPixelChannels(image); |
| 6880 | n=pixels+GetPixelChannels(image); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6881 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 6882 | for (x=(ssize_t) (image->columns-length); |
| 6883 | x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6884 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6885 | /* To do: Rewrite using Get/Set***PixelChannel() */ |
glennrp | 7c7b315 | 2011-04-26 04:01:27 +0000 | [diff] [blame] | 6886 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 6887 | if (x == (ssize_t) (image->columns-length)) |
| 6888 | m=(ssize_t) mng_info->magn_ml; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6889 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 6890 | else if (magn_methx > 1 && x == (ssize_t) image->columns-2) |
| 6891 | m=(ssize_t) mng_info->magn_mr; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6892 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 6893 | else if (magn_methx <= 1 && x == (ssize_t) image->columns-1) |
| 6894 | m=(ssize_t) mng_info->magn_mr; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6895 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 6896 | else if (magn_methx > 1 && x == (ssize_t) image->columns-1) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6897 | m=1; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6898 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6899 | else |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 6900 | m=(ssize_t) mng_info->magn_mx; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6901 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6902 | for (i=0; i < m; i++) |
| 6903 | { |
| 6904 | if (magn_methx <= 1) |
| 6905 | { |
| 6906 | /* replicate previous */ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6907 | SetPixelRed(image,GetPixelRed(image,pixels),q); |
| 6908 | SetPixelGreen(image,GetPixelGreen(image,pixels),q); |
| 6909 | SetPixelBlue(image,GetPixelBlue(image,pixels),q); |
| 6910 | SetPixelAlpha(image,GetPixelAlpha(image,pixels),q); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6911 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6912 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6913 | else if (magn_methx == 2 || magn_methx == 4) |
| 6914 | { |
| 6915 | if (i == 0) |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 6916 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6917 | SetPixelRed(image,GetPixelRed(image,pixels),q); |
| 6918 | SetPixelGreen(image,GetPixelGreen(image,pixels),q); |
| 6919 | SetPixelBlue(image,GetPixelBlue(image,pixels),q); |
| 6920 | SetPixelAlpha(image,GetPixelAlpha(image,pixels),q); |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 6921 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6922 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6923 | /* To do: Rewrite using Get/Set***PixelChannel() */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6924 | else |
| 6925 | { |
| 6926 | /* Interpolate */ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6927 | SetPixelRed(image,(QM) ((2*i*( |
| 6928 | GetPixelRed(image,n) |
| 6929 | -GetPixelRed(image,pixels))+m) |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 6930 | /((ssize_t) (m*2))+ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6931 | GetPixelRed(image,pixels)),q); |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 6932 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6933 | SetPixelGreen(image,(QM) ((2*i*( |
| 6934 | GetPixelGreen(image,n) |
| 6935 | -GetPixelGreen(image,pixels))+m) |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 6936 | /((ssize_t) (m*2))+ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6937 | GetPixelGreen(image,pixels)),q); |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 6938 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6939 | SetPixelBlue(image,(QM) ((2*i*( |
| 6940 | GetPixelBlue(image,n) |
| 6941 | -GetPixelBlue(image,pixels))+m) |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 6942 | /((ssize_t) (m*2))+ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6943 | GetPixelBlue(image,pixels)),q); |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 6944 | if (image->alpha_trait == BlendPixelTrait) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6945 | SetPixelAlpha(image,(QM) ((2*i*( |
| 6946 | GetPixelAlpha(image,n) |
| 6947 | -GetPixelAlpha(image,pixels))+m) |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 6948 | /((ssize_t) (m*2))+ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6949 | GetPixelAlpha(image,pixels)),q); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6950 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6951 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6952 | if (magn_methx == 4) |
| 6953 | { |
| 6954 | /* Replicate nearest */ |
| 6955 | if (i <= ((m+1) << 1)) |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 6956 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6957 | SetPixelAlpha(image, |
| 6958 | GetPixelAlpha(image,pixels)+0,q); |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 6959 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6960 | else |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 6961 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6962 | SetPixelAlpha(image, |
| 6963 | GetPixelAlpha(image,n)+0,q); |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 6964 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6965 | } |
| 6966 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6967 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6968 | else /* if (magn_methx == 3 || magn_methx == 5) */ |
| 6969 | { |
| 6970 | /* Replicate nearest */ |
| 6971 | if (i <= ((m+1) << 1)) |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 6972 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6973 | SetPixelRed(image,GetPixelRed(image,pixels),q); |
| 6974 | SetPixelGreen(image,GetPixelGreen(image,pixels),q); |
| 6975 | SetPixelBlue(image,GetPixelBlue(image,pixels),q); |
| 6976 | SetPixelAlpha(image,GetPixelAlpha(image,pixels),q); |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 6977 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6978 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6979 | else |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 6980 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6981 | SetPixelRed(image,GetPixelRed(image,n),q); |
| 6982 | SetPixelGreen(image,GetPixelGreen(image,n),q); |
| 6983 | SetPixelBlue(image,GetPixelBlue(image,n),q); |
| 6984 | SetPixelAlpha(image,GetPixelAlpha(image,n),q); |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 6985 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6986 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6987 | if (magn_methx == 5) |
| 6988 | { |
| 6989 | /* Interpolate */ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6990 | SetPixelAlpha(image, |
| 6991 | (QM) ((2*i*( GetPixelAlpha(image,n) |
| 6992 | -GetPixelAlpha(image,pixels))+m)/ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 6993 | ((ssize_t) (m*2)) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6994 | +GetPixelAlpha(image,pixels)),q); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6995 | } |
| 6996 | } |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6997 | q+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6998 | } |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6999 | n+=GetPixelChannels(image); |
| 7000 | p+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7001 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7002 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7003 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 7004 | break; |
| 7005 | } |
glennrp | 3faa9a3 | 2011-04-23 14:00:25 +0000 | [diff] [blame] | 7006 | #if (MAGICKCORE_QUANTUM_DEPTH > 16) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7007 | if (magn_methx != 1 || magn_methy != 1) |
| 7008 | { |
| 7009 | /* |
| 7010 | Rescale pixels to Quantum |
| 7011 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 7012 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7013 | { |
| 7014 | q=GetAuthenticPixels(image,0,y,image->columns,1,exception); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7015 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 7016 | for (x=(ssize_t) image->columns-1; x >= 0; x--) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7017 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 7018 | SetPixelRed(image,ScaleShortToQuantum( |
| 7019 | GetPixelRed(image,q)),q); |
| 7020 | SetPixelGreen(image,ScaleShortToQuantum( |
| 7021 | GetPixelGreen(image,q)),q); |
| 7022 | SetPixelBlue(image,ScaleShortToQuantum( |
| 7023 | GetPixelBlue(image,q)),q); |
| 7024 | SetPixelAlpha(image,ScaleShortToQuantum( |
| 7025 | GetPixelAlpha(image,q)),q); |
| 7026 | q+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7027 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7028 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7029 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 7030 | break; |
| 7031 | } |
| 7032 | } |
| 7033 | #endif |
| 7034 | if (logging != MagickFalse) |
| 7035 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 7036 | " Finished MAGN processing"); |
| 7037 | } |
| 7038 | } |
| 7039 | |
| 7040 | /* |
| 7041 | Crop_box is with respect to the upper left corner of the MNG. |
| 7042 | */ |
| 7043 | crop_box.left=mng_info->image_box.left+mng_info->x_off[object_id]; |
| 7044 | crop_box.right=mng_info->image_box.right+mng_info->x_off[object_id]; |
| 7045 | crop_box.top=mng_info->image_box.top+mng_info->y_off[object_id]; |
| 7046 | crop_box.bottom=mng_info->image_box.bottom+mng_info->y_off[object_id]; |
| 7047 | crop_box=mng_minimum_box(crop_box,mng_info->clip); |
| 7048 | crop_box=mng_minimum_box(crop_box,mng_info->frame); |
| 7049 | crop_box=mng_minimum_box(crop_box,mng_info->object_clip[object_id]); |
| 7050 | if ((crop_box.left != (mng_info->image_box.left |
| 7051 | +mng_info->x_off[object_id])) || |
| 7052 | (crop_box.right != (mng_info->image_box.right |
| 7053 | +mng_info->x_off[object_id])) || |
| 7054 | (crop_box.top != (mng_info->image_box.top |
| 7055 | +mng_info->y_off[object_id])) || |
| 7056 | (crop_box.bottom != (mng_info->image_box.bottom |
| 7057 | +mng_info->y_off[object_id]))) |
| 7058 | { |
| 7059 | if (logging != MagickFalse) |
| 7060 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 7061 | " Crop the PNG image"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7062 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7063 | if ((crop_box.left < crop_box.right) && |
| 7064 | (crop_box.top < crop_box.bottom)) |
| 7065 | { |
| 7066 | Image |
| 7067 | *im; |
| 7068 | |
| 7069 | RectangleInfo |
| 7070 | crop_info; |
| 7071 | |
| 7072 | /* |
| 7073 | Crop_info is with respect to the upper left corner of |
| 7074 | the image. |
| 7075 | */ |
| 7076 | crop_info.x=(crop_box.left-mng_info->x_off[object_id]); |
| 7077 | crop_info.y=(crop_box.top-mng_info->y_off[object_id]); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 7078 | crop_info.width=(size_t) (crop_box.right-crop_box.left); |
| 7079 | crop_info.height=(size_t) (crop_box.bottom-crop_box.top); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7080 | image->page.width=image->columns; |
| 7081 | image->page.height=image->rows; |
| 7082 | image->page.x=0; |
| 7083 | image->page.y=0; |
| 7084 | im=CropImage(image,&crop_info,exception); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7085 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7086 | if (im != (Image *) NULL) |
| 7087 | { |
| 7088 | image->columns=im->columns; |
| 7089 | image->rows=im->rows; |
| 7090 | im=DestroyImage(im); |
| 7091 | image->page.width=image->columns; |
| 7092 | image->page.height=image->rows; |
| 7093 | image->page.x=crop_box.left; |
| 7094 | image->page.y=crop_box.top; |
| 7095 | } |
| 7096 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7097 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7098 | else |
| 7099 | { |
| 7100 | /* |
| 7101 | No pixels in crop area. The MNG spec still requires |
| 7102 | a layer, though, so make a single transparent pixel in |
| 7103 | the top left corner. |
| 7104 | */ |
| 7105 | image->columns=1; |
| 7106 | image->rows=1; |
| 7107 | image->colors=2; |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 7108 | (void) SetImageBackgroundColor(image,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7109 | image->page.width=1; |
| 7110 | image->page.height=1; |
| 7111 | image->page.x=0; |
| 7112 | image->page.y=0; |
| 7113 | } |
| 7114 | } |
| 7115 | #ifndef PNG_READ_EMPTY_PLTE_SUPPORTED |
| 7116 | image=mng_info->image; |
| 7117 | #endif |
| 7118 | } |
| 7119 | |
glennrp | 2b013e4 | 2010-11-24 16:55:50 +0000 | [diff] [blame] | 7120 | #if (MAGICKCORE_QUANTUM_DEPTH > 16) |
| 7121 | /* PNG does not handle depths greater than 16 so reduce it even |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 7122 | * if lossy. |
glennrp | 2b013e4 | 2010-11-24 16:55:50 +0000 | [diff] [blame] | 7123 | */ |
| 7124 | if (image->depth > 16) |
| 7125 | image->depth=16; |
| 7126 | #endif |
| 7127 | |
glennrp | 3faa9a3 | 2011-04-23 14:00:25 +0000 | [diff] [blame] | 7128 | #if (MAGICKCORE_QUANTUM_DEPTH > 8) |
glennrp | cc5d45b | 2012-01-06 04:06:10 +0000 | [diff] [blame] | 7129 | if (image->depth > 8) |
| 7130 | { |
| 7131 | /* To do: fill low byte properly */ |
| 7132 | image->depth=16; |
| 7133 | } |
| 7134 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 7135 | if (LosslessReduceDepthOK(image,exception) != MagickFalse) |
glennrp | 8640fb5 | 2010-11-23 15:48:26 +0000 | [diff] [blame] | 7136 | image->depth = 8; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7137 | #endif |
glennrp | d6afd54 | 2010-11-19 01:53:05 +0000 | [diff] [blame] | 7138 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7139 | if (image_info->number_scenes != 0) |
| 7140 | { |
| 7141 | if (mng_info->scenes_found > |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 7142 | (ssize_t) (image_info->first_scene+image_info->number_scenes)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7143 | break; |
| 7144 | } |
glennrp | d6afd54 | 2010-11-19 01:53:05 +0000 | [diff] [blame] | 7145 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7146 | if (logging != MagickFalse) |
| 7147 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 7148 | " Finished reading image datastream."); |
glennrp | d6afd54 | 2010-11-19 01:53:05 +0000 | [diff] [blame] | 7149 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7150 | } while (LocaleCompare(image_info->magick,"MNG") == 0); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7151 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7152 | (void) CloseBlob(image); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7153 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7154 | if (logging != MagickFalse) |
| 7155 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 7156 | " Finished reading all image datastreams."); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7157 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7158 | #if defined(MNG_INSERT_LAYERS) |
| 7159 | if (insert_layers && !mng_info->image_found && (mng_info->mng_width) && |
| 7160 | (mng_info->mng_height)) |
| 7161 | { |
| 7162 | /* |
| 7163 | Insert a background layer if nothing else was found. |
| 7164 | */ |
| 7165 | if (logging != MagickFalse) |
| 7166 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 7167 | " No images found. Inserting a background layer."); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 7168 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 7169 | if (GetAuthenticPixelQueue(image) != (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7170 | { |
| 7171 | /* |
| 7172 | Allocate next image structure. |
| 7173 | */ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 7174 | AcquireNextImage(image_info,image,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7175 | if (GetNextImageInList(image) == (Image *) NULL) |
| 7176 | { |
| 7177 | image=DestroyImageList(image); |
| 7178 | MngInfoFreeStruct(mng_info,&have_mng_structure); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7179 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7180 | if (logging != MagickFalse) |
| 7181 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 7182 | " Allocation failed, returning NULL."); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7183 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7184 | return((Image *) NULL); |
| 7185 | } |
| 7186 | image=SyncNextImageInList(image); |
| 7187 | } |
| 7188 | image->columns=mng_info->mng_width; |
| 7189 | image->rows=mng_info->mng_height; |
| 7190 | image->page.width=mng_info->mng_width; |
| 7191 | image->page.height=mng_info->mng_height; |
| 7192 | image->page.x=0; |
| 7193 | image->page.y=0; |
| 7194 | image->background_color=mng_background_color; |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 7195 | image->alpha_trait=UndefinedPixelTrait; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 7196 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7197 | if (image_info->ping == MagickFalse) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 7198 | (void) SetImageBackgroundColor(image,exception); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 7199 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7200 | mng_info->image_found++; |
| 7201 | } |
| 7202 | #endif |
| 7203 | image->iterations=mng_iterations; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 7204 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7205 | if (mng_iterations == 1) |
| 7206 | image->start_loop=MagickTrue; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 7207 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7208 | while (GetPreviousImageInList(image) != (Image *) NULL) |
| 7209 | { |
| 7210 | image_count++; |
| 7211 | if (image_count > 10*mng_info->image_found) |
| 7212 | { |
| 7213 | if (logging != MagickFalse) |
| 7214 | (void) LogMagickEvent(CoderEvent,GetMagickModule()," No beginning"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7215 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 7216 | (void) ThrowMagickException(exception,GetMagickModule(), |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7217 | CoderError,"Linked list is corrupted, beginning of list not found", |
| 7218 | "`%s'",image_info->filename); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7219 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7220 | return((Image *) NULL); |
| 7221 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 7222 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7223 | image=GetPreviousImageInList(image); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 7224 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7225 | if (GetNextImageInList(image) == (Image *) NULL) |
| 7226 | { |
| 7227 | if (logging != MagickFalse) |
| 7228 | (void) LogMagickEvent(CoderEvent,GetMagickModule()," Corrupt list"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7229 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 7230 | (void) ThrowMagickException(exception,GetMagickModule(), |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7231 | CoderError,"Linked list is corrupted; next_image is NULL","`%s'", |
| 7232 | image_info->filename); |
| 7233 | } |
| 7234 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7235 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7236 | if (mng_info->ticks_per_second && mng_info->image_found > 1 && |
| 7237 | GetNextImageInList(image) == |
| 7238 | (Image *) NULL) |
| 7239 | { |
| 7240 | if (logging != MagickFalse) |
| 7241 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 7242 | " First image null"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7243 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 7244 | (void) ThrowMagickException(exception,GetMagickModule(), |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7245 | CoderError,"image->next for first image is NULL but shouldn't be.", |
| 7246 | "`%s'",image_info->filename); |
| 7247 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7248 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7249 | if (mng_info->image_found == 0) |
| 7250 | { |
| 7251 | if (logging != MagickFalse) |
| 7252 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 7253 | " No visible images found."); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7254 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 7255 | (void) ThrowMagickException(exception,GetMagickModule(), |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7256 | CoderError,"No visible images in file","`%s'",image_info->filename); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7257 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7258 | if (image != (Image *) NULL) |
| 7259 | image=DestroyImageList(image); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7260 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7261 | MngInfoFreeStruct(mng_info,&have_mng_structure); |
| 7262 | return((Image *) NULL); |
| 7263 | } |
| 7264 | |
| 7265 | if (mng_info->ticks_per_second) |
| 7266 | final_delay=1UL*MagickMax(image->ticks_per_second,1L)* |
| 7267 | final_delay/mng_info->ticks_per_second; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 7268 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7269 | else |
| 7270 | image->start_loop=MagickTrue; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 7271 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7272 | /* Find final nonzero image delay */ |
| 7273 | final_image_delay=0; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 7274 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7275 | while (GetNextImageInList(image) != (Image *) NULL) |
| 7276 | { |
| 7277 | if (image->delay) |
| 7278 | final_image_delay=image->delay; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7279 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7280 | image=GetNextImageInList(image); |
| 7281 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 7282 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7283 | if (final_delay < final_image_delay) |
| 7284 | final_delay=final_image_delay; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 7285 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7286 | image->delay=final_delay; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 7287 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7288 | if (logging != MagickFalse) |
| 7289 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 7290 | " image->delay=%.20g, final_delay=%.20g",(double) image->delay, |
| 7291 | (double) final_delay); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 7292 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7293 | if (logging != MagickFalse) |
| 7294 | { |
| 7295 | int |
| 7296 | scene; |
| 7297 | |
| 7298 | scene=0; |
| 7299 | image=GetFirstImageInList(image); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7300 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7301 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 7302 | " Before coalesce:"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7303 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7304 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 7305 | " scene 0 delay=%.20g",(double) image->delay); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7306 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7307 | while (GetNextImageInList(image) != (Image *) NULL) |
| 7308 | { |
| 7309 | image=GetNextImageInList(image); |
| 7310 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 7311 | " scene %.20g delay=%.20g",(double) scene++,(double) image->delay); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7312 | } |
| 7313 | } |
| 7314 | |
| 7315 | image=GetFirstImageInList(image); |
| 7316 | #ifdef MNG_COALESCE_LAYERS |
| 7317 | if (insert_layers) |
| 7318 | { |
| 7319 | Image |
| 7320 | *next_image, |
| 7321 | *next; |
| 7322 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 7323 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7324 | scene; |
| 7325 | |
| 7326 | if (logging != MagickFalse) |
| 7327 | (void) LogMagickEvent(CoderEvent,GetMagickModule()," Coalesce Images"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7328 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7329 | scene=image->scene; |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 7330 | next_image=CoalesceImages(image,exception); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7331 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7332 | if (next_image == (Image *) NULL) |
| 7333 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7334 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7335 | image=DestroyImageList(image); |
| 7336 | image=next_image; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7337 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7338 | for (next=image; next != (Image *) NULL; next=next_image) |
| 7339 | { |
| 7340 | next->page.width=mng_info->mng_width; |
| 7341 | next->page.height=mng_info->mng_height; |
| 7342 | next->page.x=0; |
| 7343 | next->page.y=0; |
| 7344 | next->scene=scene++; |
| 7345 | next_image=GetNextImageInList(next); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7346 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7347 | if (next_image == (Image *) NULL) |
| 7348 | break; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7349 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7350 | if (next->delay == 0) |
| 7351 | { |
| 7352 | scene--; |
| 7353 | next_image->previous=GetPreviousImageInList(next); |
| 7354 | if (GetPreviousImageInList(next) == (Image *) NULL) |
| 7355 | image=next_image; |
| 7356 | else |
| 7357 | next->previous->next=next_image; |
| 7358 | next=DestroyImage(next); |
| 7359 | } |
| 7360 | } |
| 7361 | } |
| 7362 | #endif |
| 7363 | |
| 7364 | while (GetNextImageInList(image) != (Image *) NULL) |
| 7365 | image=GetNextImageInList(image); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7366 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7367 | image->dispose=BackgroundDispose; |
| 7368 | |
| 7369 | if (logging != MagickFalse) |
| 7370 | { |
| 7371 | int |
| 7372 | scene; |
| 7373 | |
| 7374 | scene=0; |
| 7375 | image=GetFirstImageInList(image); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7376 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7377 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 7378 | " After coalesce:"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7379 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7380 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 7381 | " scene 0 delay=%.20g dispose=%.20g",(double) image->delay, |
| 7382 | (double) image->dispose); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7383 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7384 | while (GetNextImageInList(image) != (Image *) NULL) |
cristy | f2faecf | 2010-05-28 19:19:36 +0000 | [diff] [blame] | 7385 | { |
| 7386 | image=GetNextImageInList(image); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7387 | |
cristy | f2faecf | 2010-05-28 19:19:36 +0000 | [diff] [blame] | 7388 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 7389 | " scene %.20g delay=%.20g dispose=%.20g",(double) scene++, |
| 7390 | (double) image->delay,(double) image->dispose); |
cristy | f2faecf | 2010-05-28 19:19:36 +0000 | [diff] [blame] | 7391 | } |
| 7392 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7393 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7394 | image=GetFirstImageInList(image); |
| 7395 | MngInfoFreeStruct(mng_info,&have_mng_structure); |
| 7396 | have_mng_structure=MagickFalse; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7397 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7398 | if (logging != MagickFalse) |
| 7399 | (void) LogMagickEvent(CoderEvent,GetMagickModule(),"exit ReadMNGImage()"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7400 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7401 | return(GetFirstImageInList(image)); |
| 7402 | } |
glennrp | 25c1e2b | 2010-03-25 01:39:56 +0000 | [diff] [blame] | 7403 | #else /* PNG_LIBPNG_VER > 10011 */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7404 | static Image *ReadPNGImage(const ImageInfo *image_info,ExceptionInfo *exception) |
| 7405 | { |
| 7406 | printf("Your PNG library is too old: You have libpng-%s\n", |
| 7407 | PNG_LIBPNG_VER_STRING); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7408 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7409 | (void) ThrowMagickException(exception,GetMagickModule(),CoderError, |
| 7410 | "PNG library is too old","`%s'",image_info->filename); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7411 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7412 | return(Image *) NULL; |
| 7413 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7414 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7415 | static Image *ReadMNGImage(const ImageInfo *image_info,ExceptionInfo *exception) |
| 7416 | { |
| 7417 | return(ReadPNGImage(image_info,exception)); |
| 7418 | } |
glennrp | 25c1e2b | 2010-03-25 01:39:56 +0000 | [diff] [blame] | 7419 | #endif /* PNG_LIBPNG_VER > 10011 */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7420 | #endif |
| 7421 | |
| 7422 | /* |
| 7423 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 7424 | % % |
| 7425 | % % |
| 7426 | % % |
| 7427 | % R e g i s t e r P N G I m a g e % |
| 7428 | % % |
| 7429 | % % |
| 7430 | % % |
| 7431 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 7432 | % |
| 7433 | % RegisterPNGImage() adds properties for the PNG image format to |
| 7434 | % the list of supported formats. The properties include the image format |
| 7435 | % tag, a method to read and/or write the format, whether the format |
| 7436 | % supports the saving of more than one frame to the same file or blob, |
| 7437 | % whether the format supports native in-memory I/O, and a brief |
| 7438 | % description of the format. |
| 7439 | % |
| 7440 | % The format of the RegisterPNGImage method is: |
| 7441 | % |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 7442 | % size_t RegisterPNGImage(void) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7443 | % |
| 7444 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 7445 | ModuleExport size_t RegisterPNGImage(void) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7446 | { |
| 7447 | char |
| 7448 | version[MaxTextExtent]; |
| 7449 | |
| 7450 | MagickInfo |
| 7451 | *entry; |
| 7452 | |
| 7453 | static const char |
| 7454 | *PNGNote= |
| 7455 | { |
| 7456 | "See http://www.libpng.org/ for details about the PNG format." |
| 7457 | }, |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7458 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7459 | *JNGNote= |
| 7460 | { |
| 7461 | "See http://www.libpng.org/pub/mng/ for details about the JNG\n" |
| 7462 | "format." |
| 7463 | }, |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7464 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7465 | *MNGNote= |
| 7466 | { |
| 7467 | "See http://www.libpng.org/pub/mng/ for details about the MNG\n" |
| 7468 | "format." |
| 7469 | }; |
| 7470 | |
| 7471 | *version='\0'; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7472 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7473 | #if defined(PNG_LIBPNG_VER_STRING) |
| 7474 | (void) ConcatenateMagickString(version,"libpng ",MaxTextExtent); |
| 7475 | (void) ConcatenateMagickString(version,PNG_LIBPNG_VER_STRING,MaxTextExtent); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7476 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7477 | if (LocaleCompare(PNG_LIBPNG_VER_STRING,png_get_header_ver(NULL)) != 0) |
| 7478 | { |
| 7479 | (void) ConcatenateMagickString(version,",",MaxTextExtent); |
| 7480 | (void) ConcatenateMagickString(version,png_get_libpng_ver(NULL), |
| 7481 | MaxTextExtent); |
| 7482 | } |
| 7483 | #endif |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7484 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7485 | entry=SetMagickInfo("MNG"); |
| 7486 | entry->seekable_stream=MagickTrue; /* To do: eliminate this. */ |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7487 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7488 | #if defined(MAGICKCORE_PNG_DELEGATE) |
| 7489 | entry->decoder=(DecodeImageHandler *) ReadMNGImage; |
| 7490 | entry->encoder=(EncodeImageHandler *) WriteMNGImage; |
| 7491 | #endif |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7492 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7493 | entry->magick=(IsImageFormatHandler *) IsMNG; |
| 7494 | entry->description=ConstantString("Multiple-image Network Graphics"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7495 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7496 | if (*version != '\0') |
| 7497 | entry->version=ConstantString(version); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7498 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7499 | entry->module=ConstantString("PNG"); |
| 7500 | entry->note=ConstantString(MNGNote); |
| 7501 | (void) RegisterMagickInfo(entry); |
| 7502 | |
| 7503 | entry=SetMagickInfo("PNG"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7504 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7505 | #if defined(MAGICKCORE_PNG_DELEGATE) |
| 7506 | entry->decoder=(DecodeImageHandler *) ReadPNGImage; |
| 7507 | entry->encoder=(EncodeImageHandler *) WritePNGImage; |
| 7508 | #endif |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7509 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7510 | entry->magick=(IsImageFormatHandler *) IsPNG; |
| 7511 | entry->adjoin=MagickFalse; |
| 7512 | entry->description=ConstantString("Portable Network Graphics"); |
| 7513 | entry->module=ConstantString("PNG"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7514 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7515 | if (*version != '\0') |
| 7516 | entry->version=ConstantString(version); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7517 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7518 | entry->note=ConstantString(PNGNote); |
| 7519 | (void) RegisterMagickInfo(entry); |
| 7520 | |
| 7521 | entry=SetMagickInfo("PNG8"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7522 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7523 | #if defined(MAGICKCORE_PNG_DELEGATE) |
| 7524 | entry->decoder=(DecodeImageHandler *) ReadPNGImage; |
| 7525 | entry->encoder=(EncodeImageHandler *) WritePNGImage; |
| 7526 | #endif |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7527 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7528 | entry->magick=(IsImageFormatHandler *) IsPNG; |
| 7529 | entry->adjoin=MagickFalse; |
| 7530 | entry->description=ConstantString( |
| 7531 | "8-bit indexed with optional binary transparency"); |
| 7532 | entry->module=ConstantString("PNG"); |
| 7533 | (void) RegisterMagickInfo(entry); |
| 7534 | |
| 7535 | entry=SetMagickInfo("PNG24"); |
| 7536 | *version='\0'; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7537 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7538 | #if defined(ZLIB_VERSION) |
| 7539 | (void) ConcatenateMagickString(version,"zlib ",MaxTextExtent); |
| 7540 | (void) ConcatenateMagickString(version,ZLIB_VERSION,MaxTextExtent); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7541 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7542 | if (LocaleCompare(ZLIB_VERSION,zlib_version) != 0) |
| 7543 | { |
| 7544 | (void) ConcatenateMagickString(version,",",MaxTextExtent); |
| 7545 | (void) ConcatenateMagickString(version,zlib_version,MaxTextExtent); |
| 7546 | } |
| 7547 | #endif |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7548 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7549 | if (*version != '\0') |
| 7550 | entry->version=ConstantString(version); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7551 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7552 | #if defined(MAGICKCORE_PNG_DELEGATE) |
| 7553 | entry->decoder=(DecodeImageHandler *) ReadPNGImage; |
| 7554 | entry->encoder=(EncodeImageHandler *) WritePNGImage; |
| 7555 | #endif |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7556 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7557 | entry->magick=(IsImageFormatHandler *) IsPNG; |
| 7558 | entry->adjoin=MagickFalse; |
glennrp | fd164d2 | 2013-01-26 21:10:22 +0000 | [diff] [blame] | 7559 | entry->description=ConstantString("opaque or binary transparent 24-bit RGB"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7560 | entry->module=ConstantString("PNG"); |
| 7561 | (void) RegisterMagickInfo(entry); |
| 7562 | |
| 7563 | entry=SetMagickInfo("PNG32"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7564 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7565 | #if defined(MAGICKCORE_PNG_DELEGATE) |
| 7566 | entry->decoder=(DecodeImageHandler *) ReadPNGImage; |
| 7567 | entry->encoder=(EncodeImageHandler *) WritePNGImage; |
| 7568 | #endif |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7569 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7570 | entry->magick=(IsImageFormatHandler *) IsPNG; |
| 7571 | entry->adjoin=MagickFalse; |
| 7572 | entry->description=ConstantString("opaque or transparent 32-bit RGBA"); |
| 7573 | entry->module=ConstantString("PNG"); |
| 7574 | (void) RegisterMagickInfo(entry); |
| 7575 | |
glennrp | fd164d2 | 2013-01-26 21:10:22 +0000 | [diff] [blame] | 7576 | entry=SetMagickInfo("PNG48"); |
| 7577 | |
| 7578 | #if defined(MAGICKCORE_PNG_DELEGATE) |
| 7579 | entry->decoder=(DecodeImageHandler *) ReadPNGImage; |
| 7580 | entry->encoder=(EncodeImageHandler *) WritePNGImage; |
| 7581 | #endif |
| 7582 | |
| 7583 | entry->magick=(IsImageFormatHandler *) IsPNG; |
| 7584 | entry->adjoin=MagickFalse; |
| 7585 | entry->description=ConstantString("opaque or binary transparent 48-bit RGB"); |
| 7586 | entry->module=ConstantString("PNG"); |
| 7587 | (void) RegisterMagickInfo(entry); |
| 7588 | |
| 7589 | entry=SetMagickInfo("PNG64"); |
| 7590 | |
| 7591 | #if defined(MAGICKCORE_PNG_DELEGATE) |
| 7592 | entry->decoder=(DecodeImageHandler *) ReadPNGImage; |
| 7593 | entry->encoder=(EncodeImageHandler *) WritePNGImage; |
| 7594 | #endif |
| 7595 | |
| 7596 | entry->magick=(IsImageFormatHandler *) IsPNG; |
| 7597 | entry->adjoin=MagickFalse; |
| 7598 | entry->description=ConstantString("opaque or transparent 64-bit RGBA"); |
| 7599 | entry->module=ConstantString("PNG"); |
| 7600 | (void) RegisterMagickInfo(entry); |
| 7601 | |
glennrp | 5830fbc | 2013-01-27 06:11:45 +0000 | [diff] [blame] | 7602 | entry=SetMagickInfo("PNG00"); |
| 7603 | |
| 7604 | #if defined(MAGICKCORE_PNG_DELEGATE) |
| 7605 | entry->decoder=(DecodeImageHandler *) ReadPNGImage; |
| 7606 | entry->encoder=(EncodeImageHandler *) WritePNGImage; |
| 7607 | #endif |
| 7608 | |
| 7609 | entry->magick=(IsImageFormatHandler *) IsPNG; |
| 7610 | entry->adjoin=MagickFalse; |
glennrp | 6270857 | 2013-02-15 12:51:48 +0000 | [diff] [blame] | 7611 | entry->description=ConstantString( |
| 7612 | "PNG inheriting bit-depth and color-type from original"); |
glennrp | 5830fbc | 2013-01-27 06:11:45 +0000 | [diff] [blame] | 7613 | entry->module=ConstantString("PNG"); |
| 7614 | (void) RegisterMagickInfo(entry); |
| 7615 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7616 | entry=SetMagickInfo("JNG"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7617 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7618 | #if defined(JNG_SUPPORTED) |
| 7619 | #if defined(MAGICKCORE_PNG_DELEGATE) |
| 7620 | entry->decoder=(DecodeImageHandler *) ReadJNGImage; |
| 7621 | entry->encoder=(EncodeImageHandler *) WriteJNGImage; |
| 7622 | #endif |
| 7623 | #endif |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7624 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7625 | entry->magick=(IsImageFormatHandler *) IsJNG; |
| 7626 | entry->adjoin=MagickFalse; |
| 7627 | entry->description=ConstantString("JPEG Network Graphics"); |
| 7628 | entry->module=ConstantString("PNG"); |
| 7629 | entry->note=ConstantString(JNGNote); |
| 7630 | (void) RegisterMagickInfo(entry); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7631 | |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 7632 | #ifdef PNG_SETJMP_NOT_THREAD_SAFE |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 7633 | ping_semaphore=AllocateSemaphoreInfo(); |
cristy | 18b1744 | 2009-10-25 18:36:48 +0000 | [diff] [blame] | 7634 | #endif |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7635 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7636 | return(MagickImageCoderSignature); |
| 7637 | } |
| 7638 | |
| 7639 | /* |
| 7640 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 7641 | % % |
| 7642 | % % |
| 7643 | % % |
| 7644 | % U n r e g i s t e r P N G I m a g e % |
| 7645 | % % |
| 7646 | % % |
| 7647 | % % |
| 7648 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 7649 | % |
| 7650 | % UnregisterPNGImage() removes format registrations made by the |
| 7651 | % PNG module from the list of supported formats. |
| 7652 | % |
| 7653 | % The format of the UnregisterPNGImage method is: |
| 7654 | % |
| 7655 | % UnregisterPNGImage(void) |
| 7656 | % |
| 7657 | */ |
| 7658 | ModuleExport void UnregisterPNGImage(void) |
| 7659 | { |
| 7660 | (void) UnregisterMagickInfo("MNG"); |
| 7661 | (void) UnregisterMagickInfo("PNG"); |
| 7662 | (void) UnregisterMagickInfo("PNG8"); |
| 7663 | (void) UnregisterMagickInfo("PNG24"); |
| 7664 | (void) UnregisterMagickInfo("PNG32"); |
glennrp | fd164d2 | 2013-01-26 21:10:22 +0000 | [diff] [blame] | 7665 | (void) UnregisterMagickInfo("PNG48"); |
| 7666 | (void) UnregisterMagickInfo("PNG64"); |
glennrp | 5830fbc | 2013-01-27 06:11:45 +0000 | [diff] [blame] | 7667 | (void) UnregisterMagickInfo("PNG00"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7668 | (void) UnregisterMagickInfo("JNG"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7669 | |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 7670 | #ifdef PNG_SETJMP_NOT_THREAD_SAFE |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 7671 | if (ping_semaphore != (SemaphoreInfo *) NULL) |
| 7672 | DestroySemaphoreInfo(&ping_semaphore); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7673 | #endif |
| 7674 | } |
| 7675 | |
| 7676 | #if defined(MAGICKCORE_PNG_DELEGATE) |
glennrp | 25c1e2b | 2010-03-25 01:39:56 +0000 | [diff] [blame] | 7677 | #if PNG_LIBPNG_VER > 10011 |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7678 | /* |
| 7679 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 7680 | % % |
| 7681 | % % |
| 7682 | % % |
| 7683 | % W r i t e M N G I m a g e % |
| 7684 | % % |
| 7685 | % % |
| 7686 | % % |
| 7687 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 7688 | % |
| 7689 | % WriteMNGImage() writes an image in the Portable Network Graphics |
| 7690 | % Group's "Multiple-image Network Graphics" encoded image format. |
| 7691 | % |
| 7692 | % MNG support written by Glenn Randers-Pehrson, glennrp@image... |
| 7693 | % |
| 7694 | % The format of the WriteMNGImage method is: |
| 7695 | % |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 7696 | % MagickBooleanType WriteMNGImage(const ImageInfo *image_info, |
| 7697 | % Image *image,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7698 | % |
| 7699 | % A description of each parameter follows. |
| 7700 | % |
| 7701 | % o image_info: the image info. |
| 7702 | % |
| 7703 | % o image: The image. |
| 7704 | % |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 7705 | % o exception: return any errors or warnings in this structure. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7706 | % |
| 7707 | % To do (as of version 5.5.2, November 26, 2002 -- glennrp -- see also |
| 7708 | % "To do" under ReadPNGImage): |
| 7709 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7710 | % Preserve all unknown and not-yet-handled known chunks found in input |
| 7711 | % PNG file and copy them into output PNG files according to the PNG |
| 7712 | % copying rules. |
| 7713 | % |
| 7714 | % Write the iCCP chunk at MNG level when (icc profile length > 0) |
| 7715 | % |
| 7716 | % Improve selection of color type (use indexed-colour or indexed-colour |
| 7717 | % with tRNS when 256 or fewer unique RGBA values are present). |
| 7718 | % |
| 7719 | % Figure out what to do with "dispose=<restore-to-previous>" (dispose == 3) |
| 7720 | % This will be complicated if we limit ourselves to generating MNG-LC |
| 7721 | % files. For now we ignore disposal method 3 and simply overlay the next |
| 7722 | % image on it. |
| 7723 | % |
| 7724 | % Check for identical PLTE's or PLTE/tRNS combinations and use a |
| 7725 | % global MNG PLTE or PLTE/tRNS combination when appropriate. |
| 7726 | % [mostly done 15 June 1999 but still need to take care of tRNS] |
| 7727 | % |
| 7728 | % Check for identical sRGB and replace with a global sRGB (and remove |
| 7729 | % gAMA/cHRM if sRGB is found; check for identical gAMA/cHRM and |
| 7730 | % replace with global gAMA/cHRM (or with sRGB if appropriate; replace |
| 7731 | % local gAMA/cHRM with local sRGB if appropriate). |
| 7732 | % |
| 7733 | % Check for identical sBIT chunks and write global ones. |
| 7734 | % |
| 7735 | % Provide option to skip writing the signature tEXt chunks. |
| 7736 | % |
| 7737 | % Use signatures to detect identical objects and reuse the first |
| 7738 | % instance of such objects instead of writing duplicate objects. |
| 7739 | % |
| 7740 | % Use a smaller-than-32k value of compression window size when |
| 7741 | % appropriate. |
| 7742 | % |
| 7743 | % Encode JNG datastreams. Mostly done as of 5.5.2; need to write |
| 7744 | % ancillary text chunks and save profiles. |
| 7745 | % |
| 7746 | % Provide an option to force LC files (to ensure exact framing rate) |
| 7747 | % instead of VLC. |
| 7748 | % |
| 7749 | % Provide an option to force VLC files instead of LC, even when offsets |
| 7750 | % are present. This will involve expanding the embedded images with a |
| 7751 | % transparent region at the top and/or left. |
| 7752 | */ |
| 7753 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7754 | static void |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 7755 | Magick_png_write_raw_profile(const ImageInfo *image_info,png_struct *ping, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7756 | png_info *ping_info, unsigned char *profile_type, unsigned char |
| 7757 | *profile_description, unsigned char *profile_data, png_uint_32 length) |
| 7758 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7759 | png_textp |
| 7760 | text; |
| 7761 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 7762 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7763 | i; |
| 7764 | |
| 7765 | unsigned char |
| 7766 | *sp; |
| 7767 | |
| 7768 | png_charp |
| 7769 | dp; |
| 7770 | |
| 7771 | png_uint_32 |
| 7772 | allocated_length, |
| 7773 | description_length; |
| 7774 | |
| 7775 | unsigned char |
| 7776 | hex[16]={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'}; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7777 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7778 | if (LocaleNCompare((char *) profile_type+1, "ng-chunk-",9) == 0) |
| 7779 | return; |
| 7780 | |
| 7781 | if (image_info->verbose) |
| 7782 | { |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 7783 | (void) printf("writing raw profile: type=%s, length=%.20g\n", |
| 7784 | (char *) profile_type, (double) length); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7785 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 7786 | |
glennrp | ecab7d7 | 2013-05-14 22:50:32 +0000 | [diff] [blame] | 7787 | #if PNG_LIBPNG_VER >= 10400 |
cristy | a865ccd | 2012-07-28 00:33:10 +0000 | [diff] [blame] | 7788 | text=(png_textp) png_malloc(ping,(png_alloc_size_t) sizeof(png_text)); |
| 7789 | #else |
| 7790 | text=(png_textp) png_malloc(ping,(png_size_t) sizeof(png_text)); |
| 7791 | #endif |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7792 | description_length=(png_uint_32) strlen((const char *) profile_description); |
| 7793 | allocated_length=(png_uint_32) (length*2 + (length >> 5) + 20 |
| 7794 | + description_length); |
glennrp | ecab7d7 | 2013-05-14 22:50:32 +0000 | [diff] [blame] | 7795 | #if PNG_LIBPNG_VER >= 10400 |
cristy | a865ccd | 2012-07-28 00:33:10 +0000 | [diff] [blame] | 7796 | text[0].text=(png_charp) png_malloc(ping, |
| 7797 | (png_alloc_size_t) allocated_length); |
| 7798 | text[0].key=(png_charp) png_malloc(ping, (png_alloc_size_t) 80); |
| 7799 | #else |
| 7800 | text[0].text=(png_charp) png_malloc(ping, (png_size_t) allocated_length); |
| 7801 | text[0].key=(png_charp) png_malloc(ping, (png_size_t) 80); |
| 7802 | #endif |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7803 | text[0].key[0]='\0'; |
| 7804 | (void) ConcatenateMagickString(text[0].key, |
| 7805 | "Raw profile type ",MaxTextExtent); |
| 7806 | (void) ConcatenateMagickString(text[0].key,(const char *) profile_type,62); |
| 7807 | sp=profile_data; |
| 7808 | dp=text[0].text; |
| 7809 | *dp++='\n'; |
| 7810 | (void) CopyMagickString(dp,(const char *) profile_description, |
| 7811 | allocated_length); |
| 7812 | dp+=description_length; |
| 7813 | *dp++='\n'; |
cristy | 3b6fd2e | 2011-05-20 12:53:50 +0000 | [diff] [blame] | 7814 | (void) FormatLocaleString(dp,allocated_length- |
cristy | f2faecf | 2010-05-28 19:19:36 +0000 | [diff] [blame] | 7815 | (png_size_t) (dp-text[0].text),"%8lu ",(unsigned long) length); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7816 | dp+=8; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7817 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 7818 | for (i=0; i < (ssize_t) length; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7819 | { |
| 7820 | if (i%36 == 0) |
| 7821 | *dp++='\n'; |
| 7822 | *(dp++)=(char) hex[((*sp >> 4) & 0x0f)]; |
| 7823 | *(dp++)=(char) hex[((*sp++ ) & 0x0f)]; |
| 7824 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7825 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7826 | *dp++='\n'; |
| 7827 | *dp='\0'; |
| 7828 | text[0].text_length=(png_size_t) (dp-text[0].text); |
| 7829 | text[0].compression=image_info->compression == NoCompression || |
| 7830 | (image_info->compression == UndefinedCompression && |
| 7831 | text[0].text_length < 128) ? -1 : 0; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7832 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7833 | if (text[0].text_length <= allocated_length) |
| 7834 | png_set_text(ping,ping_info,text,1); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7835 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7836 | png_free(ping,text[0].text); |
| 7837 | png_free(ping,text[0].key); |
| 7838 | png_free(ping,text); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7839 | } |
| 7840 | |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 7841 | static MagickBooleanType Magick_png_write_chunk_from_profile(Image *image, |
cristy | 4383ec8 | 2011-01-05 15:42:32 +0000 | [diff] [blame] | 7842 | const char *string, MagickBooleanType logging) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7843 | { |
| 7844 | char |
| 7845 | *name; |
| 7846 | |
| 7847 | const StringInfo |
| 7848 | *profile; |
| 7849 | |
| 7850 | unsigned char |
| 7851 | *data; |
| 7852 | |
| 7853 | png_uint_32 length; |
| 7854 | |
| 7855 | ResetImageProfileIterator(image); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7856 | |
| 7857 | for (name=GetNextImageProfile(image); name != (const char *) NULL; ) |
| 7858 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7859 | profile=GetImageProfile(image,name); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7860 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7861 | if (profile != (const StringInfo *) NULL) |
| 7862 | { |
| 7863 | StringInfo |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 7864 | *ping_profile; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7865 | |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7866 | if (LocaleNCompare(name,string,11) == 0) |
| 7867 | { |
| 7868 | if (logging != MagickFalse) |
| 7869 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 7870 | " Found %s profile",name); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7871 | |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 7872 | ping_profile=CloneStringInfo(profile); |
| 7873 | data=GetStringInfoDatum(ping_profile), |
| 7874 | length=(png_uint_32) GetStringInfoLength(ping_profile); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7875 | data[4]=data[3]; |
| 7876 | data[3]=data[2]; |
| 7877 | data[2]=data[1]; |
| 7878 | data[1]=data[0]; |
| 7879 | (void) WriteBlobMSBULong(image,length-5); /* data length */ |
| 7880 | (void) WriteBlob(image,length-1,data+1); |
| 7881 | (void) WriteBlobMSBULong(image,crc32(0,data+1,(uInt) length-1)); |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 7882 | ping_profile=DestroyStringInfo(ping_profile); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7883 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7884 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7885 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7886 | name=GetNextImageProfile(image); |
| 7887 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7888 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7889 | return(MagickTrue); |
| 7890 | } |
| 7891 | |
glennrp | b9cfe27 | 2010-12-21 15:08:06 +0000 | [diff] [blame] | 7892 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7893 | /* Write one PNG image */ |
glennrp | b9cfe27 | 2010-12-21 15:08:06 +0000 | [diff] [blame] | 7894 | static MagickBooleanType WriteOnePNGImage(MngInfo *mng_info, |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 7895 | const ImageInfo *IMimage_info,Image *IMimage,ExceptionInfo *exception) |
glennrp | b9cfe27 | 2010-12-21 15:08:06 +0000 | [diff] [blame] | 7896 | { |
cristy | 0997332 | 2013-06-30 21:06:30 +0000 | [diff] [blame] | 7897 | char |
| 7898 | im_vers[32], |
| 7899 | libpng_runv[32], |
| 7900 | libpng_vers[32], |
| 7901 | zlib_runv[32], |
| 7902 | zlib_vers[32]; |
| 7903 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 7904 | Image |
| 7905 | *image; |
| 7906 | |
| 7907 | ImageInfo |
| 7908 | *image_info; |
| 7909 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7910 | char |
| 7911 | s[2]; |
| 7912 | |
| 7913 | const char |
| 7914 | *name, |
| 7915 | *property, |
| 7916 | *value; |
| 7917 | |
| 7918 | const StringInfo |
| 7919 | *profile; |
| 7920 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7921 | int |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7922 | num_passes, |
glennrp | cecd576 | 2010-03-23 12:07:49 +0000 | [diff] [blame] | 7923 | pass; |
| 7924 | |
glennrp | e9c26dc | 2010-05-30 01:56:35 +0000 | [diff] [blame] | 7925 | png_byte |
| 7926 | ping_trans_alpha[256]; |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 7927 | |
glennrp | 39992b4 | 2010-11-14 00:03:43 +0000 | [diff] [blame] | 7928 | png_color |
| 7929 | palette[257]; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7930 | |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 7931 | png_color_16 |
| 7932 | ping_background, |
| 7933 | ping_trans_color; |
| 7934 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7935 | png_info |
| 7936 | *ping_info; |
| 7937 | |
| 7938 | png_struct |
| 7939 | *ping; |
| 7940 | |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 7941 | png_uint_32 |
| 7942 | ping_height, |
| 7943 | ping_width; |
| 7944 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 7945 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7946 | y; |
| 7947 | |
| 7948 | MagickBooleanType |
glennrp | 58e0176 | 2011-01-07 15:28:54 +0000 | [diff] [blame] | 7949 | image_matte, |
glennrp | 21f0e62 | 2011-01-07 16:20:57 +0000 | [diff] [blame] | 7950 | logging, |
glennrp | 58e0176 | 2011-01-07 15:28:54 +0000 | [diff] [blame] | 7951 | matte, |
| 7952 | |
glennrp | da8f3a7 | 2011-02-27 23:54:12 +0000 | [diff] [blame] | 7953 | ping_have_blob, |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 7954 | ping_have_cheap_transparency, |
glennrp | d6bf161 | 2010-12-17 17:28:54 +0000 | [diff] [blame] | 7955 | ping_have_color, |
glennrp | 8d57966 | 2011-02-23 02:05:02 +0000 | [diff] [blame] | 7956 | ping_have_non_bw, |
glennrp | 39992b4 | 2010-11-14 00:03:43 +0000 | [diff] [blame] | 7957 | ping_have_PLTE, |
glennrp | 991d11d | 2010-11-12 21:55:28 +0000 | [diff] [blame] | 7958 | ping_have_bKGD, |
glennrp | 918b9dc | 2013-04-03 13:41:41 +0000 | [diff] [blame] | 7959 | ping_have_iCCP, |
glennrp | 991d11d | 2010-11-12 21:55:28 +0000 | [diff] [blame] | 7960 | ping_have_pHYs, |
glennrp | 918b9dc | 2013-04-03 13:41:41 +0000 | [diff] [blame] | 7961 | ping_have_sRGB, |
glennrp | 991d11d | 2010-11-12 21:55:28 +0000 | [diff] [blame] | 7962 | ping_have_tRNS, |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 7963 | |
| 7964 | ping_exclude_bKGD, |
| 7965 | ping_exclude_cHRM, |
glennrp | a0ed009 | 2011-04-18 16:36:29 +0000 | [diff] [blame] | 7966 | ping_exclude_date, |
glennrp | e4e2d79 | 2011-02-21 12:11:27 +0000 | [diff] [blame] | 7967 | /* ping_exclude_EXIF, */ |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 7968 | ping_exclude_gAMA, |
| 7969 | ping_exclude_iCCP, |
| 7970 | /* ping_exclude_iTXt, */ |
| 7971 | ping_exclude_oFFs, |
| 7972 | ping_exclude_pHYs, |
| 7973 | ping_exclude_sRGB, |
| 7974 | ping_exclude_tEXt, |
glennrp | e4e2d79 | 2011-02-21 12:11:27 +0000 | [diff] [blame] | 7975 | /* ping_exclude_tRNS, */ |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 7976 | ping_exclude_vpAg, |
| 7977 | ping_exclude_zCCP, /* hex-encoded iCCP */ |
| 7978 | ping_exclude_zTXt, |
| 7979 | |
glennrp | 8d3d6e5 | 2011-04-19 04:39:51 +0000 | [diff] [blame] | 7980 | ping_preserve_colormap, |
glennrp | ecab7d7 | 2013-05-14 22:50:32 +0000 | [diff] [blame] | 7981 | ping_preserve_iCCP, |
glennrp | 0e8ea19 | 2010-12-24 18:00:33 +0000 | [diff] [blame] | 7982 | ping_need_colortype_warning, |
| 7983 | |
glennrp | 82b3c53 | 2011-03-22 19:20:54 +0000 | [diff] [blame] | 7984 | status, |
glennrp | 8ca51ad | 2011-05-12 21:22:32 +0000 | [diff] [blame] | 7985 | tried_332, |
glennrp | d337164 | 2011-03-22 19:42:23 +0000 | [diff] [blame] | 7986 | tried_333, |
| 7987 | tried_444; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7988 | |
cristy | 0997332 | 2013-06-30 21:06:30 +0000 | [diff] [blame] | 7989 | MemoryInfo |
cristy | af1534a | 2013-06-30 21:12:25 +0000 | [diff] [blame] | 7990 | *volatile pixel_info; |
cristy | 0997332 | 2013-06-30 21:06:30 +0000 | [diff] [blame] | 7991 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7992 | QuantumInfo |
| 7993 | *quantum_info; |
| 7994 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 7995 | PNGErrorInfo |
| 7996 | error_info; |
| 7997 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 7998 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7999 | i, |
| 8000 | x; |
| 8001 | |
| 8002 | unsigned char |
cristy | 0997332 | 2013-06-30 21:06:30 +0000 | [diff] [blame] | 8003 | *ping_pixels; |
glennrp | d0cae25 | 2013-03-15 22:30:41 +0000 | [diff] [blame] | 8004 | |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 8005 | volatile int |
glennrp | f09bded | 2011-01-08 01:15:59 +0000 | [diff] [blame] | 8006 | image_colors, |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8007 | ping_bit_depth, |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 8008 | ping_color_type, |
| 8009 | ping_interlace_method, |
| 8010 | ping_compression_method, |
| 8011 | ping_filter_method, |
| 8012 | ping_num_trans; |
| 8013 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 8014 | volatile size_t |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 8015 | image_depth, |
| 8016 | old_bit_depth; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8017 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 8018 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8019 | quality, |
| 8020 | rowbytes, |
| 8021 | save_image_depth; |
| 8022 | |
glennrp | dfd7080 | 2010-11-14 01:23:35 +0000 | [diff] [blame] | 8023 | int |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 8024 | j, |
glennrp | f09bded | 2011-01-08 01:15:59 +0000 | [diff] [blame] | 8025 | number_colors, |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 8026 | number_opaque, |
| 8027 | number_semitransparent, |
| 8028 | number_transparent, |
glennrp | dfd7080 | 2010-11-14 01:23:35 +0000 | [diff] [blame] | 8029 | ping_pHYs_unit_type; |
| 8030 | |
| 8031 | png_uint_32 |
| 8032 | ping_pHYs_x_resolution, |
| 8033 | ping_pHYs_y_resolution; |
| 8034 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8035 | logging=LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 8036 | " Enter WriteOnePNGImage()"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8037 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8038 | image = CloneImage(IMimage,0,0,MagickFalse,exception); |
| 8039 | image_info=(ImageInfo *) CloneImageInfo(IMimage_info); |
| 8040 | if (image_info == (ImageInfo *) NULL) |
| 8041 | ThrowWriterException(ResourceLimitError, "MemoryAllocationFailed"); |
glennrp | b9cfe27 | 2010-12-21 15:08:06 +0000 | [diff] [blame] | 8042 | |
glennrp | d0cae25 | 2013-03-15 22:30:41 +0000 | [diff] [blame] | 8043 | /* Define these outside of the following "if logging()" block so they will |
| 8044 | * show in debuggers. |
| 8045 | */ |
| 8046 | *im_vers='\0'; |
| 8047 | (void) ConcatenateMagickString(im_vers, |
| 8048 | MagickLibVersionText,MaxTextExtent); |
| 8049 | (void) ConcatenateMagickString(im_vers, |
| 8050 | MagickLibAddendum,MaxTextExtent); |
glennrp | ec0ddbc | 2013-03-16 12:40:12 +0000 | [diff] [blame] | 8051 | |
glennrp | d0cae25 | 2013-03-15 22:30:41 +0000 | [diff] [blame] | 8052 | *libpng_vers='\0'; |
| 8053 | (void) ConcatenateMagickString(libpng_vers, |
glennrp | ec0ddbc | 2013-03-16 12:40:12 +0000 | [diff] [blame] | 8054 | PNG_LIBPNG_VER_STRING,32); |
| 8055 | *libpng_runv='\0'; |
| 8056 | (void) ConcatenateMagickString(libpng_runv, |
| 8057 | png_get_libpng_ver(NULL),32); |
| 8058 | |
glennrp | d0cae25 | 2013-03-15 22:30:41 +0000 | [diff] [blame] | 8059 | *zlib_vers='\0'; |
| 8060 | (void) ConcatenateMagickString(zlib_vers, |
glennrp | ec0ddbc | 2013-03-16 12:40:12 +0000 | [diff] [blame] | 8061 | ZLIB_VERSION,32); |
| 8062 | *zlib_runv='\0'; |
| 8063 | (void) ConcatenateMagickString(zlib_runv, |
| 8064 | zlib_version,32); |
| 8065 | |
glennrp | d0cae25 | 2013-03-15 22:30:41 +0000 | [diff] [blame] | 8066 | if (logging) |
| 8067 | { |
| 8068 | LogMagickEvent(CoderEvent,GetMagickModule()," IM version = %s", |
| 8069 | im_vers); |
| 8070 | LogMagickEvent(CoderEvent,GetMagickModule()," Libpng version = %s", |
| 8071 | libpng_vers); |
glennrp | ec0ddbc | 2013-03-16 12:40:12 +0000 | [diff] [blame] | 8072 | if (LocaleCompare(libpng_vers,libpng_runv) != 0) |
| 8073 | { |
| 8074 | LogMagickEvent(CoderEvent,GetMagickModule()," running with %s", |
| 8075 | libpng_runv); |
| 8076 | } |
glennrp | d0cae25 | 2013-03-15 22:30:41 +0000 | [diff] [blame] | 8077 | LogMagickEvent(CoderEvent,GetMagickModule()," Zlib version = %s", |
| 8078 | zlib_vers); |
glennrp | ec0ddbc | 2013-03-16 12:40:12 +0000 | [diff] [blame] | 8079 | if (LocaleCompare(zlib_vers,zlib_runv) != 0) |
| 8080 | { |
| 8081 | LogMagickEvent(CoderEvent,GetMagickModule()," running with %s", |
| 8082 | zlib_runv); |
| 8083 | } |
glennrp | d0cae25 | 2013-03-15 22:30:41 +0000 | [diff] [blame] | 8084 | } |
| 8085 | |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 8086 | /* Initialize some stuff */ |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8087 | ping_bit_depth=0, |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 8088 | ping_color_type=0, |
| 8089 | ping_interlace_method=0, |
| 8090 | ping_compression_method=0, |
| 8091 | ping_filter_method=0, |
| 8092 | ping_num_trans = 0; |
| 8093 | |
| 8094 | ping_background.red = 0; |
| 8095 | ping_background.green = 0; |
| 8096 | ping_background.blue = 0; |
| 8097 | ping_background.gray = 0; |
| 8098 | ping_background.index = 0; |
| 8099 | |
| 8100 | ping_trans_color.red=0; |
| 8101 | ping_trans_color.green=0; |
| 8102 | ping_trans_color.blue=0; |
| 8103 | ping_trans_color.gray=0; |
| 8104 | |
glennrp | dfd7080 | 2010-11-14 01:23:35 +0000 | [diff] [blame] | 8105 | ping_pHYs_unit_type = 0; |
| 8106 | ping_pHYs_x_resolution = 0; |
| 8107 | ping_pHYs_y_resolution = 0; |
| 8108 | |
glennrp | da8f3a7 | 2011-02-27 23:54:12 +0000 | [diff] [blame] | 8109 | ping_have_blob=MagickFalse; |
glennrp | f70c4d2 | 2013-03-19 15:26:48 +0000 | [diff] [blame] | 8110 | ping_have_cheap_transparency=MagickFalse; |
glennrp | d6bf161 | 2010-12-17 17:28:54 +0000 | [diff] [blame] | 8111 | ping_have_color=MagickTrue; |
glennrp | 8d57966 | 2011-02-23 02:05:02 +0000 | [diff] [blame] | 8112 | ping_have_non_bw=MagickTrue; |
glennrp | 39992b4 | 2010-11-14 00:03:43 +0000 | [diff] [blame] | 8113 | ping_have_PLTE=MagickFalse; |
glennrp | 991d11d | 2010-11-12 21:55:28 +0000 | [diff] [blame] | 8114 | ping_have_bKGD=MagickFalse; |
glennrp | 918b9dc | 2013-04-03 13:41:41 +0000 | [diff] [blame] | 8115 | ping_have_iCCP=MagickFalse; |
glennrp | 991d11d | 2010-11-12 21:55:28 +0000 | [diff] [blame] | 8116 | ping_have_pHYs=MagickFalse; |
glennrp | 918b9dc | 2013-04-03 13:41:41 +0000 | [diff] [blame] | 8117 | ping_have_sRGB=MagickFalse; |
glennrp | 991d11d | 2010-11-12 21:55:28 +0000 | [diff] [blame] | 8118 | ping_have_tRNS=MagickFalse; |
| 8119 | |
glennrp | 0e8ea19 | 2010-12-24 18:00:33 +0000 | [diff] [blame] | 8120 | ping_exclude_bKGD=mng_info->ping_exclude_bKGD; |
| 8121 | ping_exclude_cHRM=mng_info->ping_exclude_cHRM; |
glennrp | a0ed009 | 2011-04-18 16:36:29 +0000 | [diff] [blame] | 8122 | ping_exclude_date=mng_info->ping_exclude_date; |
glennrp | dde35db | 2011-02-21 12:06:32 +0000 | [diff] [blame] | 8123 | /* ping_exclude_EXIF=mng_info->ping_exclude_EXIF; */ |
glennrp | 0e8ea19 | 2010-12-24 18:00:33 +0000 | [diff] [blame] | 8124 | ping_exclude_gAMA=mng_info->ping_exclude_gAMA; |
glennrp | 0e8ea19 | 2010-12-24 18:00:33 +0000 | [diff] [blame] | 8125 | ping_exclude_iCCP=mng_info->ping_exclude_iCCP; |
| 8126 | /* ping_exclude_iTXt=mng_info->ping_exclude_iTXt; */ |
| 8127 | ping_exclude_oFFs=mng_info->ping_exclude_oFFs; |
| 8128 | ping_exclude_pHYs=mng_info->ping_exclude_pHYs; |
| 8129 | ping_exclude_sRGB=mng_info->ping_exclude_sRGB; |
| 8130 | ping_exclude_tEXt=mng_info->ping_exclude_tEXt; |
glennrp | dde35db | 2011-02-21 12:06:32 +0000 | [diff] [blame] | 8131 | /* ping_exclude_tRNS=mng_info->ping_exclude_tRNS; */ |
glennrp | 0e8ea19 | 2010-12-24 18:00:33 +0000 | [diff] [blame] | 8132 | ping_exclude_vpAg=mng_info->ping_exclude_vpAg; |
| 8133 | ping_exclude_zCCP=mng_info->ping_exclude_zCCP; /* hex-encoded iCCP in zTXt */ |
| 8134 | ping_exclude_zTXt=mng_info->ping_exclude_zTXt; |
| 8135 | |
glennrp | 8d3d6e5 | 2011-04-19 04:39:51 +0000 | [diff] [blame] | 8136 | ping_preserve_colormap = mng_info->ping_preserve_colormap; |
glennrp | ecab7d7 | 2013-05-14 22:50:32 +0000 | [diff] [blame] | 8137 | ping_preserve_iCCP = mng_info->ping_preserve_iCCP; |
glennrp | 0e8ea19 | 2010-12-24 18:00:33 +0000 | [diff] [blame] | 8138 | ping_need_colortype_warning = MagickFalse; |
| 8139 | |
cristy | 0d57eec | 2011-09-04 22:13:56 +0000 | [diff] [blame] | 8140 | /* Recognize the ICC sRGB profile and convert it to the sRGB chunk, |
| 8141 | * i.e., eliminate the ICC profile and set image->rendering_intent. |
| 8142 | * Note that this will not involve any changes to the actual pixels |
| 8143 | * but merely passes information to applications that read the resulting |
| 8144 | * PNG image. |
glennrp | ecab7d7 | 2013-05-14 22:50:32 +0000 | [diff] [blame] | 8145 | * |
| 8146 | * To do: recognize other variants of the sRGB profile, using the CRC to |
| 8147 | * verify all recognized variants including the 7 already known. |
| 8148 | * |
| 8149 | * Work around libpng16+ rejecting some "known invalid sRGB profiles". |
| 8150 | * |
| 8151 | * Use something other than image->rendering_intent to record the fact |
| 8152 | * that the sRGB profile was found. |
| 8153 | * |
| 8154 | * Record the ICC version (currently v2 or v4) of the incoming sRGB ICC |
| 8155 | * profile. Record the Blackpoint Compensation, if any. |
cristy | 0d57eec | 2011-09-04 22:13:56 +0000 | [diff] [blame] | 8156 | */ |
glennrp | ecab7d7 | 2013-05-14 22:50:32 +0000 | [diff] [blame] | 8157 | if (ping_exclude_sRGB == MagickFalse && ping_preserve_iCCP == MagickFalse) |
cristy | 0d57eec | 2011-09-04 22:13:56 +0000 | [diff] [blame] | 8158 | { |
| 8159 | char |
| 8160 | *name; |
| 8161 | |
| 8162 | const StringInfo |
| 8163 | *profile; |
| 8164 | |
| 8165 | ResetImageProfileIterator(image); |
| 8166 | for (name=GetNextImageProfile(image); name != (const char *) NULL; ) |
| 8167 | { |
| 8168 | profile=GetImageProfile(image,name); |
| 8169 | |
| 8170 | if (profile != (StringInfo *) NULL) |
| 8171 | { |
| 8172 | if ((LocaleCompare(name,"ICC") == 0) || |
glennrp | ecab7d7 | 2013-05-14 22:50:32 +0000 | [diff] [blame] | 8173 | (LocaleCompare(name,"ICM") == 0)) |
glennrp | ee7b4c0 | 2011-10-04 01:21:09 +0000 | [diff] [blame] | 8174 | |
glennrp | ecab7d7 | 2013-05-14 22:50:32 +0000 | [diff] [blame] | 8175 | { |
| 8176 | int |
| 8177 | icheck, |
| 8178 | got_crc=0; |
| 8179 | |
glennrp | ee7b4c0 | 2011-10-04 01:21:09 +0000 | [diff] [blame] | 8180 | |
| 8181 | png_uint_32 |
| 8182 | length, |
glennrp | ecab7d7 | 2013-05-14 22:50:32 +0000 | [diff] [blame] | 8183 | profile_crc=0; |
glennrp | ee7b4c0 | 2011-10-04 01:21:09 +0000 | [diff] [blame] | 8184 | |
cristy | 0d57eec | 2011-09-04 22:13:56 +0000 | [diff] [blame] | 8185 | unsigned char |
| 8186 | *data; |
| 8187 | |
glennrp | 29a106e | 2011-09-06 17:11:42 +0000 | [diff] [blame] | 8188 | length=(png_uint_32) GetStringInfoLength(profile); |
| 8189 | |
glennrp | ecab7d7 | 2013-05-14 22:50:32 +0000 | [diff] [blame] | 8190 | for (icheck=0; sRGB_info[icheck].len > 0; icheck++) |
cristy | 0d57eec | 2011-09-04 22:13:56 +0000 | [diff] [blame] | 8191 | { |
glennrp | ecab7d7 | 2013-05-14 22:50:32 +0000 | [diff] [blame] | 8192 | if (length == sRGB_info[icheck].len) |
glennrp | 29a106e | 2011-09-06 17:11:42 +0000 | [diff] [blame] | 8193 | { |
glennrp | ecab7d7 | 2013-05-14 22:50:32 +0000 | [diff] [blame] | 8194 | if (got_crc == 0) |
| 8195 | { |
| 8196 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | ee7b4c0 | 2011-10-04 01:21:09 +0000 | [diff] [blame] | 8197 | " Got a %lu-byte ICC profile (potentially sRGB)", |
| 8198 | (unsigned long) length); |
glennrp | 29a106e | 2011-09-06 17:11:42 +0000 | [diff] [blame] | 8199 | |
glennrp | ecab7d7 | 2013-05-14 22:50:32 +0000 | [diff] [blame] | 8200 | data=GetStringInfoDatum(profile); |
| 8201 | profile_crc=crc32(0,data,length); |
glennrp | 29a106e | 2011-09-06 17:11:42 +0000 | [diff] [blame] | 8202 | |
glennrp | ecab7d7 | 2013-05-14 22:50:32 +0000 | [diff] [blame] | 8203 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8204 | " with crc=%8x",(unsigned int) profile_crc); |
| 8205 | got_crc++; |
| 8206 | } |
glennrp | ee7b4c0 | 2011-10-04 01:21:09 +0000 | [diff] [blame] | 8207 | |
glennrp | ecab7d7 | 2013-05-14 22:50:32 +0000 | [diff] [blame] | 8208 | if (profile_crc == sRGB_info[icheck].crc) |
glennrp | ee7b4c0 | 2011-10-04 01:21:09 +0000 | [diff] [blame] | 8209 | { |
| 8210 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | ecab7d7 | 2013-05-14 22:50:32 +0000 | [diff] [blame] | 8211 | " It is sRGB with rendering intent = %s", |
| 8212 | Magick_RenderingIntentString_from_PNG_RenderingIntent( |
| 8213 | sRGB_info[icheck].intent)); |
glennrp | ee7b4c0 | 2011-10-04 01:21:09 +0000 | [diff] [blame] | 8214 | if (image->rendering_intent==UndefinedIntent) |
glennrp | ecab7d7 | 2013-05-14 22:50:32 +0000 | [diff] [blame] | 8215 | { |
| 8216 | image->rendering_intent= |
| 8217 | Magick_RenderingIntent_from_PNG_RenderingIntent( |
| 8218 | sRGB_info[icheck].intent); |
| 8219 | } |
| 8220 | ping_exclude_iCCP = MagickTrue; |
| 8221 | ping_exclude_zCCP = MagickTrue; |
| 8222 | ping_have_sRGB = MagickTrue; |
glennrp | ee7b4c0 | 2011-10-04 01:21:09 +0000 | [diff] [blame] | 8223 | break; |
| 8224 | } |
glennrp | 29a106e | 2011-09-06 17:11:42 +0000 | [diff] [blame] | 8225 | } |
glennrp | 29a106e | 2011-09-06 17:11:42 +0000 | [diff] [blame] | 8226 | } |
glennrp | ecab7d7 | 2013-05-14 22:50:32 +0000 | [diff] [blame] | 8227 | if (sRGB_info[icheck].len == 0) |
glennrp | 29a106e | 2011-09-06 17:11:42 +0000 | [diff] [blame] | 8228 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | ecab7d7 | 2013-05-14 22:50:32 +0000 | [diff] [blame] | 8229 | " Got a %lu-byte ICC profile not recognized as sRGB", |
glennrp | 29a106e | 2011-09-06 17:11:42 +0000 | [diff] [blame] | 8230 | (unsigned long) length); |
| 8231 | } |
cristy | 0d57eec | 2011-09-04 22:13:56 +0000 | [diff] [blame] | 8232 | } |
| 8233 | name=GetNextImageProfile(image); |
| 8234 | } |
| 8235 | } |
| 8236 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 8237 | number_opaque = 0; |
| 8238 | number_semitransparent = 0; |
| 8239 | number_transparent = 0; |
| 8240 | |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 8241 | if (logging != MagickFalse) |
| 8242 | { |
| 8243 | if (image->storage_class == UndefinedClass) |
| 8244 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8245 | " storage_class=UndefinedClass"); |
| 8246 | if (image->storage_class == DirectClass) |
| 8247 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8248 | " storage_class=DirectClass"); |
| 8249 | if (image->storage_class == PseudoClass) |
| 8250 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8251 | " storage_class=PseudoClass"); |
| 8252 | } |
glennrp | 28af371 | 2011-04-06 18:07:30 +0000 | [diff] [blame] | 8253 | |
glennrp | 750105b | 2012-04-25 16:20:45 +0000 | [diff] [blame] | 8254 | if (image->storage_class == PseudoClass && |
glennrp | 7e65e93 | 2011-08-19 02:31:16 +0000 | [diff] [blame] | 8255 | (mng_info->write_png8 || mng_info->write_png24 || mng_info->write_png32 || |
glennrp | fd164d2 | 2013-01-26 21:10:22 +0000 | [diff] [blame] | 8256 | mng_info->write_png48 || mng_info->write_png64 || |
| 8257 | (mng_info->write_png_colortype != 1 && |
| 8258 | mng_info->write_png_colortype != 5))) |
glennrp | 7e65e93 | 2011-08-19 02:31:16 +0000 | [diff] [blame] | 8259 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8260 | (void) SyncImage(image,exception); |
glennrp | 7e65e93 | 2011-08-19 02:31:16 +0000 | [diff] [blame] | 8261 | image->storage_class = DirectClass; |
| 8262 | } |
| 8263 | |
glennrp | c6c391a | 2011-04-27 02:23:56 +0000 | [diff] [blame] | 8264 | if (ping_preserve_colormap == MagickFalse) |
glennrp | 28af371 | 2011-04-06 18:07:30 +0000 | [diff] [blame] | 8265 | { |
glennrp | c6c391a | 2011-04-27 02:23:56 +0000 | [diff] [blame] | 8266 | if (image->storage_class != PseudoClass && image->colormap != NULL) |
| 8267 | { |
| 8268 | /* Free the bogus colormap; it can cause trouble later */ |
| 8269 | if (logging != MagickFalse) |
| 8270 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8271 | " Freeing bogus colormap"); |
cristy | e9ac4c3 | 2011-09-26 18:47:22 +0000 | [diff] [blame] | 8272 | (void) RelinquishMagickMemory(image->colormap); |
glennrp | c6c391a | 2011-04-27 02:23:56 +0000 | [diff] [blame] | 8273 | image->colormap=NULL; |
| 8274 | } |
glennrp | 28af371 | 2011-04-06 18:07:30 +0000 | [diff] [blame] | 8275 | } |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 8276 | |
cristy | 3d9f5ba | 2012-06-26 13:37:31 +0000 | [diff] [blame] | 8277 | if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8278 | (void) TransformImageColorspace(image,sRGBColorspace,exception); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8279 | |
glennrp | 3241bd0 | 2010-12-12 04:36:28 +0000 | [diff] [blame] | 8280 | /* |
| 8281 | Sometimes we get PseudoClass images whose RGB values don't match |
| 8282 | the colors in the colormap. This code syncs the RGB values. |
| 8283 | */ |
| 8284 | if (image->depth <= 8 && image->taint && image->storage_class == PseudoClass) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8285 | (void) SyncImage(image,exception); |
glennrp | 3241bd0 | 2010-12-12 04:36:28 +0000 | [diff] [blame] | 8286 | |
glennrp | a6a0663 | 2011-01-19 15:15:34 +0000 | [diff] [blame] | 8287 | #if (MAGICKCORE_QUANTUM_DEPTH == 8) |
| 8288 | if (image->depth > 8) |
| 8289 | { |
| 8290 | if (logging != MagickFalse) |
| 8291 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8292 | " Reducing PNG bit depth to 8 since this is a Q8 build."); |
| 8293 | |
| 8294 | image->depth=8; |
| 8295 | } |
| 8296 | #endif |
| 8297 | |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 8298 | /* Respect the -depth option */ |
glennrp | cc95c3f | 2011-04-18 16:46:48 +0000 | [diff] [blame] | 8299 | if (image->depth < MAGICKCORE_QUANTUM_DEPTH) |
| 8300 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8301 | register Quantum |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 8302 | *r; |
| 8303 | |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 8304 | if (image->depth > 8) |
| 8305 | { |
| 8306 | #if MAGICKCORE_QUANTUM_DEPTH > 16 |
| 8307 | /* Scale to 16-bit */ |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 8308 | LBR16PacketRGBO(image->background_color); |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 8309 | |
| 8310 | for (y=0; y < (ssize_t) image->rows; y++) |
| 8311 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8312 | r=GetAuthenticPixels(image,0,y,image->columns,1,exception); |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 8313 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8314 | if (r == (Quantum *) NULL) |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 8315 | break; |
| 8316 | |
| 8317 | for (x=0; x < (ssize_t) image->columns; x++) |
| 8318 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8319 | LBR16PixelRGBA(r); |
| 8320 | r+=GetPixelChannels(image); |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 8321 | } |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 8322 | |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 8323 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 8324 | break; |
| 8325 | } |
| 8326 | |
| 8327 | if (image->storage_class == PseudoClass && image->colormap != NULL) |
| 8328 | { |
cristy | 3e08f11 | 2011-05-24 13:19:30 +0000 | [diff] [blame] | 8329 | for (i=0; i < (ssize_t) image->colors; i++) |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 8330 | { |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 8331 | LBR16PacketRGBO(image->colormap[i]); |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 8332 | } |
| 8333 | } |
| 8334 | #endif /* MAGICKCORE_QUANTUM_DEPTH > 16 */ |
| 8335 | } |
| 8336 | |
| 8337 | else if (image->depth > 4) |
| 8338 | { |
| 8339 | #if MAGICKCORE_QUANTUM_DEPTH > 8 |
| 8340 | /* Scale to 8-bit */ |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 8341 | LBR08PacketRGBO(image->background_color); |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 8342 | |
| 8343 | for (y=0; y < (ssize_t) image->rows; y++) |
| 8344 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8345 | r=GetAuthenticPixels(image,0,y,image->columns,1,exception); |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 8346 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8347 | if (r == (Quantum *) NULL) |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 8348 | break; |
| 8349 | |
| 8350 | for (x=0; x < (ssize_t) image->columns; x++) |
| 8351 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8352 | LBR08PixelRGBA(r); |
| 8353 | r+=GetPixelChannels(image); |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 8354 | } |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 8355 | |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 8356 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 8357 | break; |
| 8358 | } |
| 8359 | |
| 8360 | if (image->storage_class == PseudoClass && image->colormap != NULL) |
| 8361 | { |
cristy | 3e08f11 | 2011-05-24 13:19:30 +0000 | [diff] [blame] | 8362 | for (i=0; i < (ssize_t) image->colors; i++) |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 8363 | { |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 8364 | LBR08PacketRGBO(image->colormap[i]); |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 8365 | } |
| 8366 | } |
| 8367 | #endif /* MAGICKCORE_QUANTUM_DEPTH > 8 */ |
| 8368 | } |
| 8369 | else |
| 8370 | if (image->depth > 2) |
| 8371 | { |
| 8372 | /* Scale to 4-bit */ |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 8373 | LBR04PacketRGBO(image->background_color); |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 8374 | |
| 8375 | for (y=0; y < (ssize_t) image->rows; y++) |
| 8376 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8377 | r=GetAuthenticPixels(image,0,y,image->columns,1,exception); |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 8378 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8379 | if (r == (Quantum *) NULL) |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 8380 | break; |
| 8381 | |
| 8382 | for (x=0; x < (ssize_t) image->columns; x++) |
| 8383 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8384 | LBR04PixelRGBA(r); |
| 8385 | r+=GetPixelChannels(image); |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 8386 | } |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 8387 | |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 8388 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 8389 | break; |
| 8390 | } |
| 8391 | |
| 8392 | if (image->storage_class == PseudoClass && image->colormap != NULL) |
| 8393 | { |
cristy | 3e08f11 | 2011-05-24 13:19:30 +0000 | [diff] [blame] | 8394 | for (i=0; i < (ssize_t) image->colors; i++) |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 8395 | { |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 8396 | LBR04PacketRGBO(image->colormap[i]); |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 8397 | } |
| 8398 | } |
| 8399 | } |
| 8400 | |
| 8401 | else if (image->depth > 1) |
| 8402 | { |
| 8403 | /* Scale to 2-bit */ |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 8404 | LBR02PacketRGBO(image->background_color); |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 8405 | |
| 8406 | for (y=0; y < (ssize_t) image->rows; y++) |
| 8407 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8408 | r=GetAuthenticPixels(image,0,y,image->columns,1,exception); |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 8409 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8410 | if (r == (Quantum *) NULL) |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 8411 | break; |
| 8412 | |
| 8413 | for (x=0; x < (ssize_t) image->columns; x++) |
| 8414 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8415 | LBR02PixelRGBA(r); |
| 8416 | r+=GetPixelChannels(image); |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 8417 | } |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 8418 | |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 8419 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 8420 | break; |
| 8421 | } |
| 8422 | |
| 8423 | if (image->storage_class == PseudoClass && image->colormap != NULL) |
| 8424 | { |
cristy | 3e08f11 | 2011-05-24 13:19:30 +0000 | [diff] [blame] | 8425 | for (i=0; i < (ssize_t) image->colors; i++) |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 8426 | { |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 8427 | LBR02PacketRGBO(image->colormap[i]); |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 8428 | } |
| 8429 | } |
| 8430 | } |
| 8431 | else |
| 8432 | { |
| 8433 | /* Scale to 1-bit */ |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 8434 | LBR01PacketRGBO(image->background_color); |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 8435 | |
| 8436 | for (y=0; y < (ssize_t) image->rows; y++) |
| 8437 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8438 | r=GetAuthenticPixels(image,0,y,image->columns,1,exception); |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 8439 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8440 | if (r == (Quantum *) NULL) |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 8441 | break; |
| 8442 | |
| 8443 | for (x=0; x < (ssize_t) image->columns; x++) |
| 8444 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8445 | LBR01PixelRGBA(r); |
| 8446 | r+=GetPixelChannels(image); |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 8447 | } |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 8448 | |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 8449 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 8450 | break; |
| 8451 | } |
| 8452 | |
| 8453 | if (image->storage_class == PseudoClass && image->colormap != NULL) |
| 8454 | { |
cristy | 3e08f11 | 2011-05-24 13:19:30 +0000 | [diff] [blame] | 8455 | for (i=0; i < (ssize_t) image->colors; i++) |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 8456 | { |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 8457 | LBR01PacketRGBO(image->colormap[i]); |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 8458 | } |
| 8459 | } |
| 8460 | } |
glennrp | 9d0ea4d | 2011-04-22 18:35:57 +0000 | [diff] [blame] | 8461 | } |
| 8462 | |
glennrp | 67b9c1a | 2011-04-22 18:47:36 +0000 | [diff] [blame] | 8463 | /* To do: set to next higher multiple of 8 */ |
| 8464 | if (image->depth < 8) |
glennrp | 70e68a8 | 2011-04-01 22:51:14 +0000 | [diff] [blame] | 8465 | image->depth=8; |
glennrp | a6a0663 | 2011-01-19 15:15:34 +0000 | [diff] [blame] | 8466 | |
glennrp | 2b013e4 | 2010-11-24 16:55:50 +0000 | [diff] [blame] | 8467 | #if (MAGICKCORE_QUANTUM_DEPTH > 16) |
| 8468 | /* PNG does not handle depths greater than 16 so reduce it even |
| 8469 | * if lossy |
| 8470 | */ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 8471 | if (image->depth > 8) |
glennrp | 2b013e4 | 2010-11-24 16:55:50 +0000 | [diff] [blame] | 8472 | image->depth=16; |
| 8473 | #endif |
| 8474 | |
glennrp | 3faa9a3 | 2011-04-23 14:00:25 +0000 | [diff] [blame] | 8475 | #if (MAGICKCORE_QUANTUM_DEPTH > 8) |
glennrp | cc5d45b | 2012-01-06 04:06:10 +0000 | [diff] [blame] | 8476 | if (image->depth > 8) |
| 8477 | { |
| 8478 | /* To do: fill low byte properly */ |
| 8479 | image->depth=16; |
| 8480 | } |
| 8481 | |
glennrp | c722dd8 | 2011-02-24 05:13:21 +0000 | [diff] [blame] | 8482 | if (image->depth == 16 && mng_info->write_png_depth != 16) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8483 | if (mng_info->write_png8 || LosslessReduceDepthOK(image,exception) != MagickFalse) |
glennrp | 8640fb5 | 2010-11-23 15:48:26 +0000 | [diff] [blame] | 8484 | image->depth = 8; |
| 8485 | #endif |
| 8486 | |
glennrp | a8036d6 | 2012-11-04 01:46:06 +0000 | [diff] [blame] | 8487 | if (image->storage_class != PseudoClass && mng_info->write_png_colortype && |
| 8488 | (mng_info->write_png_colortype > 4 || (mng_info->write_png_depth >= 8 && |
| 8489 | mng_info->write_png_colortype < 4 && |
| 8490 | image->alpha_trait != BlendPixelTrait))) |
| 8491 | { |
| 8492 | /* Avoid the expensive BUILD_PALETTE operation if we're sure that we |
| 8493 | * are not going to need the result. |
| 8494 | */ |
glennrp | 2feb141 | 2013-01-22 15:02:16 +0000 | [diff] [blame] | 8495 | image_colors = (int) image->colors; |
| 8496 | number_opaque = (int) image->colors; |
glennrp | a8036d6 | 2012-11-04 01:46:06 +0000 | [diff] [blame] | 8497 | if (mng_info->write_png_colortype == 1 || |
| 8498 | mng_info->write_png_colortype == 5) |
| 8499 | ping_have_color=MagickFalse; |
| 8500 | else |
| 8501 | ping_have_color=MagickTrue; |
| 8502 | ping_have_non_bw=MagickFalse; |
| 8503 | |
| 8504 | if (image->alpha_trait == BlendPixelTrait) |
| 8505 | { |
| 8506 | number_transparent = 2; |
| 8507 | number_semitransparent = 1; |
| 8508 | } |
| 8509 | |
| 8510 | else |
| 8511 | { |
| 8512 | number_transparent = 0; |
| 8513 | number_semitransparent = 0; |
| 8514 | } |
| 8515 | } |
| 8516 | |
| 8517 | else |
| 8518 | { |
| 8519 | /* BUILD_PALETTE |
| 8520 | * |
| 8521 | * Normally we run this just once, but in the case of writing PNG8 |
glennrp | e9637cb | 2011-03-24 16:34:37 +0000 | [diff] [blame] | 8522 | * we reduce the transparency to binary and run again, then if there |
| 8523 | * are still too many colors we reduce to a simple 4-4-4-1, then 3-3-3-1 |
glennrp | 8ca51ad | 2011-05-12 21:22:32 +0000 | [diff] [blame] | 8524 | * RGBA palette and run again, and then to a simple 3-3-2-1 RGBA |
| 8525 | * palette. Then (To do) we take care of a final reduction that is only |
| 8526 | * needed if there are still 256 colors present and one of them has both |
| 8527 | * transparent and opaque instances. |
glennrp | c8c2f06 | 2011-02-25 19:00:33 +0000 | [diff] [blame] | 8528 | */ |
glennrp | 82b3c53 | 2011-03-22 19:20:54 +0000 | [diff] [blame] | 8529 | |
glennrp | 8ca51ad | 2011-05-12 21:22:32 +0000 | [diff] [blame] | 8530 | tried_332 = MagickFalse; |
glennrp | 82b3c53 | 2011-03-22 19:20:54 +0000 | [diff] [blame] | 8531 | tried_333 = MagickFalse; |
glennrp | d337164 | 2011-03-22 19:42:23 +0000 | [diff] [blame] | 8532 | tried_444 = MagickFalse; |
glennrp | 82b3c53 | 2011-03-22 19:20:54 +0000 | [diff] [blame] | 8533 | |
glennrp | 8ca51ad | 2011-05-12 21:22:32 +0000 | [diff] [blame] | 8534 | for (j=0; j<6; j++) |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8535 | { |
glennrp | a8036d6 | 2012-11-04 01:46:06 +0000 | [diff] [blame] | 8536 | /* |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8537 | * Sometimes we get DirectClass images that have 256 colors or fewer. |
| 8538 | * This code will build a colormap. |
| 8539 | * |
| 8540 | * Also, sometimes we get PseudoClass images with an out-of-date |
| 8541 | * colormap. This code will replace the colormap with a new one. |
| 8542 | * Sometimes we get PseudoClass images that have more than 256 colors. |
| 8543 | * This code will delete the colormap and change the image to |
| 8544 | * DirectClass. |
| 8545 | * |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 8546 | * If image->alpha_trait is MagickFalse, we ignore the alpha channel |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8547 | * even though it sometimes contains left-over non-opaque values. |
| 8548 | * |
| 8549 | * Also we gather some information (number of opaque, transparent, |
| 8550 | * and semitransparent pixels, and whether the image has any non-gray |
| 8551 | * pixels or only black-and-white pixels) that we might need later. |
| 8552 | * |
| 8553 | * Even if the user wants to force GrayAlpha or RGBA (colortype 4 or 6) |
| 8554 | * we need to check for bogus non-opaque values, at least. |
| 8555 | */ |
glennrp | 3c21811 | 2010-11-27 15:31:26 +0000 | [diff] [blame] | 8556 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8557 | int |
| 8558 | n; |
glennrp | 3c21811 | 2010-11-27 15:31:26 +0000 | [diff] [blame] | 8559 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8560 | PixelInfo |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8561 | opaque[260], |
| 8562 | semitransparent[260], |
| 8563 | transparent[260]; |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 8564 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8565 | register const Quantum |
| 8566 | *s; |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 8567 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8568 | register Quantum |
| 8569 | *q, |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 8570 | *r; |
| 8571 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8572 | if (logging != MagickFalse) |
| 8573 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8574 | " Enter BUILD_PALETTE:"); |
| 8575 | |
| 8576 | if (logging != MagickFalse) |
| 8577 | { |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 8578 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8579 | " image->columns=%.20g",(double) image->columns); |
| 8580 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8581 | " image->rows=%.20g",(double) image->rows); |
| 8582 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 8583 | " image->alpha_trait=%.20g",(double) image->alpha_trait); |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8584 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8585 | " image->depth=%.20g",(double) image->depth); |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 8586 | |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 8587 | if (image->storage_class == PseudoClass && image->colormap != NULL) |
glennrp | 7ddcc22 | 2010-12-11 05:01:05 +0000 | [diff] [blame] | 8588 | { |
| 8589 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8590 | " Original colormap:"); |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 8591 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8592 | " i (red,green,blue,alpha)"); |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 8593 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8594 | for (i=0; i < 256; i++) |
glennrp | 7ddcc22 | 2010-12-11 05:01:05 +0000 | [diff] [blame] | 8595 | { |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8596 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8597 | " %d (%d,%d,%d,%d)", |
| 8598 | (int) i, |
| 8599 | (int) image->colormap[i].red, |
| 8600 | (int) image->colormap[i].green, |
| 8601 | (int) image->colormap[i].blue, |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8602 | (int) image->colormap[i].alpha); |
glennrp | 7ddcc22 | 2010-12-11 05:01:05 +0000 | [diff] [blame] | 8603 | } |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 8604 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8605 | for (i=image->colors - 10; i < (ssize_t) image->colors; i++) |
| 8606 | { |
| 8607 | if (i > 255) |
| 8608 | { |
| 8609 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8610 | " %d (%d,%d,%d,%d)", |
| 8611 | (int) i, |
| 8612 | (int) image->colormap[i].red, |
| 8613 | (int) image->colormap[i].green, |
| 8614 | (int) image->colormap[i].blue, |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8615 | (int) image->colormap[i].alpha); |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8616 | } |
| 8617 | } |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 8618 | } |
glennrp | 7ddcc22 | 2010-12-11 05:01:05 +0000 | [diff] [blame] | 8619 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8620 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8621 | " image->colors=%d",(int) image->colors); |
glennrp | 7ddcc22 | 2010-12-11 05:01:05 +0000 | [diff] [blame] | 8622 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8623 | if (image->colors == 0) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8624 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8625 | " (zero means unknown)"); |
glennrp | d6bf161 | 2010-12-17 17:28:54 +0000 | [diff] [blame] | 8626 | |
glennrp | 8d3d6e5 | 2011-04-19 04:39:51 +0000 | [diff] [blame] | 8627 | if (ping_preserve_colormap == MagickFalse) |
| 8628 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8629 | " Regenerate the colormap"); |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8630 | } |
| 8631 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8632 | image_colors=0; |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 8633 | number_opaque = 0; |
| 8634 | number_semitransparent = 0; |
| 8635 | number_transparent = 0; |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8636 | |
| 8637 | for (y=0; y < (ssize_t) image->rows; y++) |
| 8638 | { |
| 8639 | q=GetAuthenticPixels(image,0,y,image->columns,1,exception); |
| 8640 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8641 | if (q == (Quantum *) NULL) |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8642 | break; |
| 8643 | |
| 8644 | for (x=0; x < (ssize_t) image->columns; x++) |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 8645 | { |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 8646 | if (image->alpha_trait != BlendPixelTrait || |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8647 | GetPixelAlpha(image,q) == OpaqueAlpha) |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8648 | { |
| 8649 | if (number_opaque < 259) |
| 8650 | { |
| 8651 | if (number_opaque == 0) |
| 8652 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8653 | GetPixelInfoPixel(image, q, opaque); |
| 8654 | opaque[0].alpha=OpaqueAlpha; |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8655 | number_opaque=1; |
| 8656 | } |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 8657 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8658 | for (i=0; i< (ssize_t) number_opaque; i++) |
| 8659 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8660 | if (IsPixelEquivalent(image,q, opaque+i)) |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8661 | break; |
| 8662 | } |
glennrp | 7ddcc22 | 2010-12-11 05:01:05 +0000 | [diff] [blame] | 8663 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8664 | if (i == (ssize_t) number_opaque && number_opaque < 259) |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8665 | { |
| 8666 | number_opaque++; |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8667 | GetPixelInfoPixel(image, q, opaque+i); |
| 8668 | opaque[i].alpha=OpaqueAlpha; |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8669 | } |
| 8670 | } |
| 8671 | } |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8672 | else if (GetPixelAlpha(image,q) == TransparentAlpha) |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8673 | { |
| 8674 | if (number_transparent < 259) |
| 8675 | { |
| 8676 | if (number_transparent == 0) |
| 8677 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8678 | GetPixelInfoPixel(image, q, transparent); |
| 8679 | ping_trans_color.red=(unsigned short) |
| 8680 | GetPixelRed(image,q); |
| 8681 | ping_trans_color.green=(unsigned short) |
| 8682 | GetPixelGreen(image,q); |
| 8683 | ping_trans_color.blue=(unsigned short) |
| 8684 | GetPixelBlue(image,q); |
| 8685 | ping_trans_color.gray=(unsigned short) |
cristy | 972d1c4 | 2012-07-14 23:29:14 +0000 | [diff] [blame] | 8686 | GetPixelGray(image,q); |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8687 | number_transparent = 1; |
| 8688 | } |
| 8689 | |
| 8690 | for (i=0; i< (ssize_t) number_transparent; i++) |
| 8691 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8692 | if (IsPixelEquivalent(image,q, transparent+i)) |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8693 | break; |
| 8694 | } |
| 8695 | |
| 8696 | if (i == (ssize_t) number_transparent && |
| 8697 | number_transparent < 259) |
| 8698 | { |
| 8699 | number_transparent++; |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8700 | GetPixelInfoPixel(image,q,transparent+i); |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8701 | } |
| 8702 | } |
| 8703 | } |
| 8704 | else |
| 8705 | { |
| 8706 | if (number_semitransparent < 259) |
| 8707 | { |
| 8708 | if (number_semitransparent == 0) |
| 8709 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8710 | GetPixelInfoPixel(image,q,semitransparent); |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8711 | number_semitransparent = 1; |
| 8712 | } |
| 8713 | |
| 8714 | for (i=0; i< (ssize_t) number_semitransparent; i++) |
| 8715 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8716 | if (IsPixelEquivalent(image,q, semitransparent+i) |
| 8717 | && GetPixelAlpha(image,q) == |
| 8718 | semitransparent[i].alpha) |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8719 | break; |
| 8720 | } |
| 8721 | |
| 8722 | if (i == (ssize_t) number_semitransparent && |
| 8723 | number_semitransparent < 259) |
| 8724 | { |
| 8725 | number_semitransparent++; |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8726 | GetPixelInfoPixel(image, q, semitransparent+i); |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8727 | } |
| 8728 | } |
| 8729 | } |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8730 | q+=GetPixelChannels(image); |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8731 | } |
| 8732 | } |
| 8733 | |
cristy | 4054bfb | 2011-08-29 23:41:39 +0000 | [diff] [blame] | 8734 | if (mng_info->write_png8 == MagickFalse && |
| 8735 | ping_exclude_bKGD == MagickFalse) |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8736 | { |
| 8737 | /* Add the background color to the palette, if it |
| 8738 | * isn't already there. |
| 8739 | */ |
glennrp | c6c391a | 2011-04-27 02:23:56 +0000 | [diff] [blame] | 8740 | if (logging != MagickFalse) |
| 8741 | { |
| 8742 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8743 | " Check colormap for background (%d,%d,%d)", |
| 8744 | (int) image->background_color.red, |
| 8745 | (int) image->background_color.green, |
| 8746 | (int) image->background_color.blue); |
| 8747 | } |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8748 | for (i=0; i<number_opaque; i++) |
| 8749 | { |
glennrp | ca7ad3a | 2011-04-26 04:44:54 +0000 | [diff] [blame] | 8750 | if (opaque[i].red == image->background_color.red && |
| 8751 | opaque[i].green == image->background_color.green && |
| 8752 | opaque[i].blue == image->background_color.blue) |
| 8753 | break; |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8754 | } |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8755 | if (number_opaque < 259 && i == number_opaque) |
| 8756 | { |
glennrp | 8e045c8 | 2011-04-27 16:40:27 +0000 | [diff] [blame] | 8757 | opaque[i] = image->background_color; |
glennrp | c6c391a | 2011-04-27 02:23:56 +0000 | [diff] [blame] | 8758 | ping_background.index = i; |
glennrp | 388a8c8 | 2012-06-27 13:41:51 +0000 | [diff] [blame] | 8759 | number_opaque++; |
glennrp | c6c391a | 2011-04-27 02:23:56 +0000 | [diff] [blame] | 8760 | if (logging != MagickFalse) |
| 8761 | { |
| 8762 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8763 | " background_color index is %d",(int) i); |
| 8764 | } |
| 8765 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8766 | } |
glennrp | a080bc3 | 2011-03-11 18:03:44 +0000 | [diff] [blame] | 8767 | else if (logging != MagickFalse) |
| 8768 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8769 | " No room in the colormap to add background color"); |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8770 | } |
| 8771 | |
| 8772 | image_colors=number_opaque+number_transparent+number_semitransparent; |
| 8773 | |
glennrp | a080bc3 | 2011-03-11 18:03:44 +0000 | [diff] [blame] | 8774 | if (mng_info->write_png8 != MagickFalse && image_colors > 256) |
| 8775 | { |
| 8776 | /* No room for the background color; remove it. */ |
| 8777 | number_opaque--; |
| 8778 | image_colors--; |
| 8779 | } |
| 8780 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8781 | if (logging != MagickFalse) |
| 8782 | { |
| 8783 | if (image_colors > 256) |
| 8784 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8785 | " image has more than 256 colors"); |
| 8786 | |
| 8787 | else |
| 8788 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8789 | " image has %d colors",image_colors); |
| 8790 | } |
| 8791 | |
glennrp | 8d3d6e5 | 2011-04-19 04:39:51 +0000 | [diff] [blame] | 8792 | if (ping_preserve_colormap != MagickFalse) |
| 8793 | break; |
glennrp | 8d3d6e5 | 2011-04-19 04:39:51 +0000 | [diff] [blame] | 8794 | |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 8795 | if (mng_info->write_png_colortype != 7) /* We won't need this info */ |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8796 | { |
| 8797 | ping_have_color=MagickFalse; |
| 8798 | ping_have_non_bw=MagickFalse; |
| 8799 | |
glennrp | 98b9577 | 2012-11-29 01:32:00 +0000 | [diff] [blame] | 8800 | if ((IssRGBCompatibleColorspace(image->colorspace) == MagickFalse) || |
| 8801 | (IssRGBColorspace(image->colorspace) != MagickFalse)) |
glennrp | 0fa2580 | 2012-07-20 14:01:06 +0000 | [diff] [blame] | 8802 | { |
| 8803 | ping_have_color=MagickTrue; |
glennrp | 98b9577 | 2012-11-29 01:32:00 +0000 | [diff] [blame] | 8804 | ping_have_non_bw=MagickTrue; |
glennrp | 0fa2580 | 2012-07-20 14:01:06 +0000 | [diff] [blame] | 8805 | } |
| 8806 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8807 | if(image_colors > 256) |
| 8808 | { |
| 8809 | for (y=0; y < (ssize_t) image->rows; y++) |
| 8810 | { |
| 8811 | q=GetAuthenticPixels(image,0,y,image->columns,1,exception); |
| 8812 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8813 | if (q == (Quantum *) NULL) |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8814 | break; |
| 8815 | |
glennrp | e5e6b80 | 2011-07-20 14:44:40 +0000 | [diff] [blame] | 8816 | s=q; |
| 8817 | for (x=0; x < (ssize_t) image->columns; x++) |
| 8818 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8819 | if (GetPixelRed(image,s) != GetPixelGreen(image,s) || |
| 8820 | GetPixelRed(image,s) != GetPixelBlue(image,s)) |
glennrp | e5e6b80 | 2011-07-20 14:44:40 +0000 | [diff] [blame] | 8821 | { |
| 8822 | ping_have_color=MagickTrue; |
| 8823 | ping_have_non_bw=MagickTrue; |
| 8824 | break; |
| 8825 | } |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8826 | s+=GetPixelChannels(image); |
glennrp | e5e6b80 | 2011-07-20 14:44:40 +0000 | [diff] [blame] | 8827 | } |
| 8828 | |
| 8829 | if (ping_have_color != MagickFalse) |
| 8830 | break; |
| 8831 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8832 | /* Worst case is black-and-white; we are looking at every |
| 8833 | * pixel twice. |
| 8834 | */ |
| 8835 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8836 | if (ping_have_non_bw == MagickFalse) |
| 8837 | { |
| 8838 | s=q; |
| 8839 | for (x=0; x < (ssize_t) image->columns; x++) |
| 8840 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8841 | if (GetPixelRed(image,s) != 0 && |
| 8842 | GetPixelRed(image,s) != QuantumRange) |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8843 | { |
| 8844 | ping_have_non_bw=MagickTrue; |
glennrp | e5e6b80 | 2011-07-20 14:44:40 +0000 | [diff] [blame] | 8845 | break; |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8846 | } |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8847 | s+=GetPixelChannels(image); |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8848 | } |
glennrp | e5e6b80 | 2011-07-20 14:44:40 +0000 | [diff] [blame] | 8849 | } |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8850 | } |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 8851 | } |
| 8852 | } |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8853 | |
| 8854 | if (image_colors < 257) |
| 8855 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8856 | PixelInfo |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8857 | colormap[260]; |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 8858 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8859 | /* |
| 8860 | * Initialize image colormap. |
glennrp | 97fd3d0 | 2011-02-23 14:58:06 +0000 | [diff] [blame] | 8861 | */ |
| 8862 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8863 | if (logging != MagickFalse) |
| 8864 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8865 | " Sort the new colormap"); |
glennrp | 8d57966 | 2011-02-23 02:05:02 +0000 | [diff] [blame] | 8866 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8867 | /* Sort palette, transparent first */; |
| 8868 | |
| 8869 | n = 0; |
| 8870 | |
| 8871 | for (i=0; i<number_transparent; i++) |
| 8872 | colormap[n++] = transparent[i]; |
| 8873 | |
| 8874 | for (i=0; i<number_semitransparent; i++) |
| 8875 | colormap[n++] = semitransparent[i]; |
| 8876 | |
| 8877 | for (i=0; i<number_opaque; i++) |
| 8878 | colormap[n++] = opaque[i]; |
| 8879 | |
glennrp | c6c391a | 2011-04-27 02:23:56 +0000 | [diff] [blame] | 8880 | ping_background.index += |
| 8881 | (number_transparent + number_semitransparent); |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 8882 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8883 | /* image_colors < 257; search the colormap instead of the pixels |
| 8884 | * to get ping_have_color and ping_have_non_bw |
| 8885 | */ |
| 8886 | for (i=0; i<n; i++) |
| 8887 | { |
| 8888 | if (ping_have_color == MagickFalse) |
glennrp | 8d57966 | 2011-02-23 02:05:02 +0000 | [diff] [blame] | 8889 | { |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8890 | if (colormap[i].red != colormap[i].green || |
| 8891 | colormap[i].red != colormap[i].blue) |
| 8892 | { |
| 8893 | ping_have_color=MagickTrue; |
| 8894 | ping_have_non_bw=MagickTrue; |
| 8895 | break; |
| 8896 | } |
| 8897 | } |
| 8898 | |
| 8899 | if (ping_have_non_bw == MagickFalse) |
| 8900 | { |
| 8901 | if (colormap[i].red != 0 && colormap[i].red != QuantumRange) |
glennrp | 8d57966 | 2011-02-23 02:05:02 +0000 | [diff] [blame] | 8902 | ping_have_non_bw=MagickTrue; |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 8903 | } |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 8904 | } |
| 8905 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8906 | if ((mng_info->ping_exclude_tRNS == MagickFalse || |
| 8907 | (number_transparent == 0 && number_semitransparent == 0)) && |
| 8908 | (((mng_info->write_png_colortype-1) == |
| 8909 | PNG_COLOR_TYPE_PALETTE) || |
| 8910 | (mng_info->write_png_colortype == 0))) |
| 8911 | { |
glennrp | 6185c53 | 2011-01-14 17:58:40 +0000 | [diff] [blame] | 8912 | if (logging != MagickFalse) |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 8913 | { |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8914 | if (n != (ssize_t) image_colors) |
| 8915 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8916 | " image_colors (%d) and n (%d) don't match", |
| 8917 | image_colors, n); |
glennrp | 6185c53 | 2011-01-14 17:58:40 +0000 | [diff] [blame] | 8918 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8919 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8920 | " AcquireImageColormap"); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 8921 | } |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 8922 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8923 | image->colors = image_colors; |
| 8924 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8925 | if (AcquireImageColormap(image,image_colors,exception) == |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8926 | MagickFalse) |
glennrp | 3faa9a3 | 2011-04-23 14:00:25 +0000 | [diff] [blame] | 8927 | ThrowWriterException(ResourceLimitError, |
| 8928 | "MemoryAllocationFailed"); |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8929 | |
| 8930 | for (i=0; i< (ssize_t) image_colors; i++) |
| 8931 | image->colormap[i] = colormap[i]; |
| 8932 | |
| 8933 | if (logging != MagickFalse) |
glennrp | 6185c53 | 2011-01-14 17:58:40 +0000 | [diff] [blame] | 8934 | { |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8935 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8936 | " image->colors=%d (%d)", |
| 8937 | (int) image->colors, image_colors); |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 8938 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8939 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8940 | " Update the pixel indexes"); |
| 8941 | } |
glennrp | 6185c53 | 2011-01-14 17:58:40 +0000 | [diff] [blame] | 8942 | |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 8943 | /* Sync the pixel indices with the new colormap */ |
| 8944 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8945 | for (y=0; y < (ssize_t) image->rows; y++) |
| 8946 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8947 | q=GetAuthenticPixels(image,0,y,image->columns,1,exception); |
glennrp | 6185c53 | 2011-01-14 17:58:40 +0000 | [diff] [blame] | 8948 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8949 | if (q == (Quantum *) NULL) |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8950 | break; |
glennrp | 6185c53 | 2011-01-14 17:58:40 +0000 | [diff] [blame] | 8951 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8952 | for (x=0; x < (ssize_t) image->columns; x++) |
glennrp | 6185c53 | 2011-01-14 17:58:40 +0000 | [diff] [blame] | 8953 | { |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8954 | for (i=0; i< (ssize_t) image_colors; i++) |
glennrp | 6185c53 | 2011-01-14 17:58:40 +0000 | [diff] [blame] | 8955 | { |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 8956 | if ((image->alpha_trait != BlendPixelTrait || |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8957 | image->colormap[i].alpha == GetPixelAlpha(image,q)) && |
| 8958 | image->colormap[i].red == GetPixelRed(image,q) && |
| 8959 | image->colormap[i].green == GetPixelGreen(image,q) && |
| 8960 | image->colormap[i].blue == GetPixelBlue(image,q)) |
glennrp | 6185c53 | 2011-01-14 17:58:40 +0000 | [diff] [blame] | 8961 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8962 | SetPixelIndex(image,i,q); |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8963 | break; |
glennrp | 6185c53 | 2011-01-14 17:58:40 +0000 | [diff] [blame] | 8964 | } |
glennrp | 6185c53 | 2011-01-14 17:58:40 +0000 | [diff] [blame] | 8965 | } |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8966 | q+=GetPixelChannels(image); |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8967 | } |
glennrp | 6185c53 | 2011-01-14 17:58:40 +0000 | [diff] [blame] | 8968 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8969 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 8970 | break; |
| 8971 | } |
| 8972 | } |
| 8973 | } |
| 8974 | |
| 8975 | if (logging != MagickFalse) |
| 8976 | { |
| 8977 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8978 | " image->colors=%d", (int) image->colors); |
| 8979 | |
| 8980 | if (image->colormap != NULL) |
| 8981 | { |
| 8982 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8983 | " i (red,green,blue,alpha)"); |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8984 | |
| 8985 | for (i=0; i < (ssize_t) image->colors; i++) |
| 8986 | { |
cristy | 7298848 | 2011-03-29 16:34:38 +0000 | [diff] [blame] | 8987 | if (i < 300 || i >= (ssize_t) image->colors - 10) |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8988 | { |
| 8989 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8990 | " %d (%d,%d,%d,%d)", |
| 8991 | (int) i, |
| 8992 | (int) image->colormap[i].red, |
| 8993 | (int) image->colormap[i].green, |
| 8994 | (int) image->colormap[i].blue, |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8995 | (int) image->colormap[i].alpha); |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8996 | } |
glennrp | 6185c53 | 2011-01-14 17:58:40 +0000 | [diff] [blame] | 8997 | } |
| 8998 | } |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 8999 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 9000 | if (number_transparent < 257) |
| 9001 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9002 | " number_transparent = %d", |
| 9003 | number_transparent); |
| 9004 | else |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 9005 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 9006 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9007 | " number_transparent > 256"); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 9008 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 9009 | if (number_opaque < 257) |
| 9010 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9011 | " number_opaque = %d", |
| 9012 | number_opaque); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 9013 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 9014 | else |
| 9015 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9016 | " number_opaque > 256"); |
glennrp | 6185c53 | 2011-01-14 17:58:40 +0000 | [diff] [blame] | 9017 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 9018 | if (number_semitransparent < 257) |
| 9019 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9020 | " number_semitransparent = %d", |
| 9021 | number_semitransparent); |
glennrp | 6185c53 | 2011-01-14 17:58:40 +0000 | [diff] [blame] | 9022 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 9023 | else |
| 9024 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9025 | " number_semitransparent > 256"); |
glennrp | a6a0663 | 2011-01-19 15:15:34 +0000 | [diff] [blame] | 9026 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 9027 | if (ping_have_non_bw == MagickFalse) |
| 9028 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9029 | " All pixels and the background are black or white"); |
glennrp | a6a0663 | 2011-01-19 15:15:34 +0000 | [diff] [blame] | 9030 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 9031 | else if (ping_have_color == MagickFalse) |
| 9032 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9033 | " All pixels and the background are gray"); |
| 9034 | |
| 9035 | else |
| 9036 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9037 | " At least one pixel or the background is non-gray"); |
glennrp | 6185c53 | 2011-01-14 17:58:40 +0000 | [diff] [blame] | 9038 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 9039 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9040 | " Exit BUILD_PALETTE:"); |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 9041 | } |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 9042 | |
glennrp | c8c2f06 | 2011-02-25 19:00:33 +0000 | [diff] [blame] | 9043 | if (mng_info->write_png8 == MagickFalse) |
| 9044 | break; |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 9045 | |
glennrp | c8c2f06 | 2011-02-25 19:00:33 +0000 | [diff] [blame] | 9046 | /* Make any reductions necessary for the PNG8 format */ |
| 9047 | if (image_colors <= 256 && |
| 9048 | image_colors != 0 && image->colormap != NULL && |
| 9049 | number_semitransparent == 0 && |
| 9050 | number_transparent <= 1) |
| 9051 | break; |
| 9052 | |
| 9053 | /* PNG8 can't have semitransparent colors so we threshold the |
glennrp | 130fc45 | 2011-08-20 03:43:18 +0000 | [diff] [blame] | 9054 | * opacity to 0 or OpaqueOpacity, and PNG8 can only have one |
| 9055 | * transparent color so if more than one is transparent we merge |
| 9056 | * them into image->background_color. |
glennrp | c8c2f06 | 2011-02-25 19:00:33 +0000 | [diff] [blame] | 9057 | */ |
glennrp | 130fc45 | 2011-08-20 03:43:18 +0000 | [diff] [blame] | 9058 | if (number_semitransparent != 0 || number_transparent > 1) |
glennrp | c8c2f06 | 2011-02-25 19:00:33 +0000 | [diff] [blame] | 9059 | { |
| 9060 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9061 | " Thresholding the alpha channel to binary"); |
| 9062 | |
| 9063 | for (y=0; y < (ssize_t) image->rows; y++) |
| 9064 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 9065 | r=GetAuthenticPixels(image,0,y,image->columns,1,exception); |
glennrp | c8c2f06 | 2011-02-25 19:00:33 +0000 | [diff] [blame] | 9066 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 9067 | if (r == (Quantum *) NULL) |
glennrp | c8c2f06 | 2011-02-25 19:00:33 +0000 | [diff] [blame] | 9068 | break; |
| 9069 | |
| 9070 | for (x=0; x < (ssize_t) image->columns; x++) |
| 9071 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 9072 | if (GetPixelAlpha(image,r) < OpaqueAlpha/2) |
glennrp | 8ca51ad | 2011-05-12 21:22:32 +0000 | [diff] [blame] | 9073 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 9074 | SetPixelInfoPixel(image,&image->background_color,r); |
| 9075 | SetPixelAlpha(image,TransparentAlpha,r); |
glennrp | 8ca51ad | 2011-05-12 21:22:32 +0000 | [diff] [blame] | 9076 | } |
| 9077 | else |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 9078 | SetPixelAlpha(image,OpaqueAlpha,r); |
| 9079 | r+=GetPixelChannels(image); |
glennrp | c8c2f06 | 2011-02-25 19:00:33 +0000 | [diff] [blame] | 9080 | } |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 9081 | |
glennrp | c8c2f06 | 2011-02-25 19:00:33 +0000 | [diff] [blame] | 9082 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 9083 | break; |
| 9084 | |
| 9085 | if (image_colors != 0 && image_colors <= 256 && |
| 9086 | image->colormap != NULL) |
| 9087 | for (i=0; i<image_colors; i++) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 9088 | image->colormap[i].alpha = |
| 9089 | (image->colormap[i].alpha > TransparentAlpha/2 ? |
| 9090 | TransparentAlpha : OpaqueAlpha); |
glennrp | c8c2f06 | 2011-02-25 19:00:33 +0000 | [diff] [blame] | 9091 | } |
| 9092 | continue; |
| 9093 | } |
| 9094 | |
| 9095 | /* PNG8 can't have more than 256 colors so we quantize the pixels and |
glennrp | e9637cb | 2011-03-24 16:34:37 +0000 | [diff] [blame] | 9096 | * background color to the 4-4-4-1, 3-3-3-1 or 3-3-2-1 palette. If the |
| 9097 | * image is mostly gray, the 4-4-4-1 palette is likely to end up with 256 |
| 9098 | * colors or less. |
glennrp | c8c2f06 | 2011-02-25 19:00:33 +0000 | [diff] [blame] | 9099 | */ |
glennrp | d337164 | 2011-03-22 19:42:23 +0000 | [diff] [blame] | 9100 | if (tried_444 == MagickFalse && (image_colors == 0 || image_colors > 256)) |
| 9101 | { |
| 9102 | if (logging != MagickFalse) |
| 9103 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9104 | " Quantizing the background color to 4-4-4"); |
| 9105 | |
| 9106 | tried_444 = MagickTrue; |
| 9107 | |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 9108 | LBR04PacketRGB(image->background_color); |
glennrp | d337164 | 2011-03-22 19:42:23 +0000 | [diff] [blame] | 9109 | |
| 9110 | if (logging != MagickFalse) |
| 9111 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9112 | " Quantizing the pixel colors to 4-4-4"); |
| 9113 | |
| 9114 | if (image->colormap == NULL) |
| 9115 | { |
| 9116 | for (y=0; y < (ssize_t) image->rows; y++) |
| 9117 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 9118 | r=GetAuthenticPixels(image,0,y,image->columns,1,exception); |
glennrp | d337164 | 2011-03-22 19:42:23 +0000 | [diff] [blame] | 9119 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 9120 | if (r == (Quantum *) NULL) |
glennrp | d337164 | 2011-03-22 19:42:23 +0000 | [diff] [blame] | 9121 | break; |
| 9122 | |
| 9123 | for (x=0; x < (ssize_t) image->columns; x++) |
| 9124 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 9125 | if (GetPixelAlpha(image,r) == OpaqueAlpha) |
glennrp | 54cf797 | 2011-08-06 14:28:09 +0000 | [diff] [blame] | 9126 | LBR04PixelRGB(r); |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 9127 | r+=GetPixelChannels(image); |
glennrp | d337164 | 2011-03-22 19:42:23 +0000 | [diff] [blame] | 9128 | } |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 9129 | |
glennrp | d337164 | 2011-03-22 19:42:23 +0000 | [diff] [blame] | 9130 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 9131 | break; |
| 9132 | } |
| 9133 | } |
| 9134 | |
| 9135 | else /* Should not reach this; colormap already exists and |
| 9136 | must be <= 256 */ |
| 9137 | { |
| 9138 | if (logging != MagickFalse) |
| 9139 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9140 | " Quantizing the colormap to 4-4-4"); |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 9141 | |
glennrp | d337164 | 2011-03-22 19:42:23 +0000 | [diff] [blame] | 9142 | for (i=0; i<image_colors; i++) |
| 9143 | { |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 9144 | LBR04PacketRGB(image->colormap[i]); |
glennrp | d337164 | 2011-03-22 19:42:23 +0000 | [diff] [blame] | 9145 | } |
| 9146 | } |
| 9147 | continue; |
| 9148 | } |
| 9149 | |
glennrp | 82b3c53 | 2011-03-22 19:20:54 +0000 | [diff] [blame] | 9150 | if (tried_333 == MagickFalse && (image_colors == 0 || image_colors > 256)) |
| 9151 | { |
| 9152 | if (logging != MagickFalse) |
| 9153 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9154 | " Quantizing the background color to 3-3-3"); |
| 9155 | |
| 9156 | tried_333 = MagickTrue; |
| 9157 | |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 9158 | LBR03PacketRGB(image->background_color); |
glennrp | 82b3c53 | 2011-03-22 19:20:54 +0000 | [diff] [blame] | 9159 | |
| 9160 | if (logging != MagickFalse) |
| 9161 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | e9637cb | 2011-03-24 16:34:37 +0000 | [diff] [blame] | 9162 | " Quantizing the pixel colors to 3-3-3-1"); |
glennrp | 82b3c53 | 2011-03-22 19:20:54 +0000 | [diff] [blame] | 9163 | |
| 9164 | if (image->colormap == NULL) |
| 9165 | { |
| 9166 | for (y=0; y < (ssize_t) image->rows; y++) |
| 9167 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 9168 | r=GetAuthenticPixels(image,0,y,image->columns,1,exception); |
glennrp | 82b3c53 | 2011-03-22 19:20:54 +0000 | [diff] [blame] | 9169 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 9170 | if (r == (Quantum *) NULL) |
glennrp | 82b3c53 | 2011-03-22 19:20:54 +0000 | [diff] [blame] | 9171 | break; |
| 9172 | |
| 9173 | for (x=0; x < (ssize_t) image->columns; x++) |
| 9174 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 9175 | if (GetPixelAlpha(image,r) == OpaqueAlpha) |
| 9176 | LBR03RGB(r); |
| 9177 | r+=GetPixelChannels(image); |
glennrp | 82b3c53 | 2011-03-22 19:20:54 +0000 | [diff] [blame] | 9178 | } |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 9179 | |
glennrp | 82b3c53 | 2011-03-22 19:20:54 +0000 | [diff] [blame] | 9180 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 9181 | break; |
| 9182 | } |
| 9183 | } |
| 9184 | |
| 9185 | else /* Should not reach this; colormap already exists and |
| 9186 | must be <= 256 */ |
| 9187 | { |
| 9188 | if (logging != MagickFalse) |
| 9189 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | e9637cb | 2011-03-24 16:34:37 +0000 | [diff] [blame] | 9190 | " Quantizing the colormap to 3-3-3-1"); |
glennrp | 82b3c53 | 2011-03-22 19:20:54 +0000 | [diff] [blame] | 9191 | for (i=0; i<image_colors; i++) |
| 9192 | { |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 9193 | LBR03PacketRGB(image->colormap[i]); |
glennrp | 82b3c53 | 2011-03-22 19:20:54 +0000 | [diff] [blame] | 9194 | } |
glennrp | d337164 | 2011-03-22 19:42:23 +0000 | [diff] [blame] | 9195 | } |
| 9196 | continue; |
glennrp | 82b3c53 | 2011-03-22 19:20:54 +0000 | [diff] [blame] | 9197 | } |
glennrp | c8c2f06 | 2011-02-25 19:00:33 +0000 | [diff] [blame] | 9198 | |
glennrp | 8ca51ad | 2011-05-12 21:22:32 +0000 | [diff] [blame] | 9199 | if (tried_332 == MagickFalse && (image_colors == 0 || image_colors > 256)) |
glennrp | c8c2f06 | 2011-02-25 19:00:33 +0000 | [diff] [blame] | 9200 | { |
| 9201 | if (logging != MagickFalse) |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 9202 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | c8c2f06 | 2011-02-25 19:00:33 +0000 | [diff] [blame] | 9203 | " Quantizing the background color to 3-3-2"); |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 9204 | |
glennrp | 8ca51ad | 2011-05-12 21:22:32 +0000 | [diff] [blame] | 9205 | tried_332 = MagickTrue; |
| 9206 | |
glennrp | 3faa9a3 | 2011-04-23 14:00:25 +0000 | [diff] [blame] | 9207 | /* Red and green were already done so we only quantize the blue |
| 9208 | * channel |
| 9209 | */ |
| 9210 | |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 9211 | LBR02PacketBlue(image->background_color); |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 9212 | |
glennrp | c8c2f06 | 2011-02-25 19:00:33 +0000 | [diff] [blame] | 9213 | if (logging != MagickFalse) |
| 9214 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | e9637cb | 2011-03-24 16:34:37 +0000 | [diff] [blame] | 9215 | " Quantizing the pixel colors to 3-3-2-1"); |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 9216 | |
glennrp | c8c2f06 | 2011-02-25 19:00:33 +0000 | [diff] [blame] | 9217 | if (image->colormap == NULL) |
| 9218 | { |
| 9219 | for (y=0; y < (ssize_t) image->rows; y++) |
| 9220 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 9221 | r=GetAuthenticPixels(image,0,y,image->columns,1,exception); |
glennrp | c8c2f06 | 2011-02-25 19:00:33 +0000 | [diff] [blame] | 9222 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 9223 | if (r == (Quantum *) NULL) |
glennrp | c8c2f06 | 2011-02-25 19:00:33 +0000 | [diff] [blame] | 9224 | break; |
| 9225 | |
| 9226 | for (x=0; x < (ssize_t) image->columns; x++) |
| 9227 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 9228 | if (GetPixelAlpha(image,r) == OpaqueAlpha) |
glennrp | 54cf797 | 2011-08-06 14:28:09 +0000 | [diff] [blame] | 9229 | LBR02PixelBlue(r); |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 9230 | r+=GetPixelChannels(image); |
glennrp | c8c2f06 | 2011-02-25 19:00:33 +0000 | [diff] [blame] | 9231 | } |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 9232 | |
glennrp | c8c2f06 | 2011-02-25 19:00:33 +0000 | [diff] [blame] | 9233 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 9234 | break; |
| 9235 | } |
| 9236 | } |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 9237 | |
glennrp | c8c2f06 | 2011-02-25 19:00:33 +0000 | [diff] [blame] | 9238 | else /* Should not reach this; colormap already exists and |
| 9239 | must be <= 256 */ |
| 9240 | { |
| 9241 | if (logging != MagickFalse) |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 9242 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | e9637cb | 2011-03-24 16:34:37 +0000 | [diff] [blame] | 9243 | " Quantizing the colormap to 3-3-2-1"); |
glennrp | c8c2f06 | 2011-02-25 19:00:33 +0000 | [diff] [blame] | 9244 | for (i=0; i<image_colors; i++) |
| 9245 | { |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 9246 | LBR02PacketBlue(image->colormap[i]); |
glennrp | c8c2f06 | 2011-02-25 19:00:33 +0000 | [diff] [blame] | 9247 | } |
| 9248 | } |
| 9249 | continue; |
| 9250 | } |
| 9251 | break; |
glennrp | 8ca51ad | 2011-05-12 21:22:32 +0000 | [diff] [blame] | 9252 | |
| 9253 | if (image_colors == 0 || image_colors > 256) |
| 9254 | { |
| 9255 | /* Take care of special case with 256 colors + 1 transparent |
| 9256 | * color. We don't need to quantize to 2-3-2-1; we only need to |
| 9257 | * eliminate one color, so we'll merge the two darkest red |
| 9258 | * colors (0x49, 0, 0) -> (0x24, 0, 0). |
| 9259 | */ |
| 9260 | if (ScaleQuantumToChar(image->background_color.red) == 0x49 && |
| 9261 | ScaleQuantumToChar(image->background_color.green) == 0x00 && |
| 9262 | ScaleQuantumToChar(image->background_color.blue) == 0x00) |
| 9263 | { |
| 9264 | image->background_color.red=ScaleCharToQuantum(0x24); |
| 9265 | } |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 9266 | |
glennrp | 8ca51ad | 2011-05-12 21:22:32 +0000 | [diff] [blame] | 9267 | if (image->colormap == NULL) |
| 9268 | { |
| 9269 | for (y=0; y < (ssize_t) image->rows; y++) |
| 9270 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 9271 | r=GetAuthenticPixels(image,0,y,image->columns,1,exception); |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 9272 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 9273 | if (r == (Quantum *) NULL) |
glennrp | 8ca51ad | 2011-05-12 21:22:32 +0000 | [diff] [blame] | 9274 | break; |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 9275 | |
glennrp | 8ca51ad | 2011-05-12 21:22:32 +0000 | [diff] [blame] | 9276 | for (x=0; x < (ssize_t) image->columns; x++) |
| 9277 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 9278 | if (ScaleQuantumToChar(GetPixelRed(image,r)) == 0x49 && |
| 9279 | ScaleQuantumToChar(GetPixelGreen(image,r)) == 0x00 && |
| 9280 | ScaleQuantumToChar(GetPixelBlue(image,r)) == 0x00 && |
| 9281 | GetPixelAlpha(image,r) == OpaqueAlpha) |
glennrp | 8ca51ad | 2011-05-12 21:22:32 +0000 | [diff] [blame] | 9282 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 9283 | SetPixelRed(image,ScaleCharToQuantum(0x24),r); |
glennrp | 8ca51ad | 2011-05-12 21:22:32 +0000 | [diff] [blame] | 9284 | } |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 9285 | r+=GetPixelChannels(image); |
glennrp | 8ca51ad | 2011-05-12 21:22:32 +0000 | [diff] [blame] | 9286 | } |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 9287 | |
glennrp | 8ca51ad | 2011-05-12 21:22:32 +0000 | [diff] [blame] | 9288 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 9289 | break; |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 9290 | |
glennrp | 8ca51ad | 2011-05-12 21:22:32 +0000 | [diff] [blame] | 9291 | } |
| 9292 | } |
| 9293 | |
| 9294 | else |
| 9295 | { |
| 9296 | for (i=0; i<image_colors; i++) |
| 9297 | { |
| 9298 | if (ScaleQuantumToChar(image->colormap[i].red) == 0x49 && |
| 9299 | ScaleQuantumToChar(image->colormap[i].green) == 0x00 && |
| 9300 | ScaleQuantumToChar(image->colormap[i].blue) == 0x00) |
| 9301 | { |
| 9302 | image->colormap[i].red=ScaleCharToQuantum(0x24); |
| 9303 | } |
| 9304 | } |
| 9305 | } |
| 9306 | } |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 9307 | } |
glennrp | a8036d6 | 2012-11-04 01:46:06 +0000 | [diff] [blame] | 9308 | } |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 9309 | /* END OF BUILD_PALETTE */ |
glennrp | 3c21811 | 2010-11-27 15:31:26 +0000 | [diff] [blame] | 9310 | |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 9311 | /* If we are excluding the tRNS chunk and there is transparency, |
| 9312 | * then we must write a Gray-Alpha (color-type 4) or RGBA (color-type 6) |
| 9313 | * PNG. |
glennrp | 8d57966 | 2011-02-23 02:05:02 +0000 | [diff] [blame] | 9314 | */ |
glennrp | 0e8ea19 | 2010-12-24 18:00:33 +0000 | [diff] [blame] | 9315 | if (mng_info->ping_exclude_tRNS != MagickFalse && |
| 9316 | (number_transparent != 0 || number_semitransparent != 0)) |
| 9317 | { |
glennrp | d17915c | 2011-04-29 14:24:22 +0000 | [diff] [blame] | 9318 | unsigned int colortype=mng_info->write_png_colortype; |
glennrp | 0e8ea19 | 2010-12-24 18:00:33 +0000 | [diff] [blame] | 9319 | |
| 9320 | if (ping_have_color == MagickFalse) |
| 9321 | mng_info->write_png_colortype = 5; |
| 9322 | |
| 9323 | else |
| 9324 | mng_info->write_png_colortype = 7; |
| 9325 | |
glennrp | 8d57966 | 2011-02-23 02:05:02 +0000 | [diff] [blame] | 9326 | if (colortype != 0 && |
glennrp | d17915c | 2011-04-29 14:24:22 +0000 | [diff] [blame] | 9327 | mng_info->write_png_colortype != colortype) |
glennrp | 0e8ea19 | 2010-12-24 18:00:33 +0000 | [diff] [blame] | 9328 | ping_need_colortype_warning=MagickTrue; |
glennrp | 0b206f5 | 2011-01-07 04:55:32 +0000 | [diff] [blame] | 9329 | |
glennrp | 0e8ea19 | 2010-12-24 18:00:33 +0000 | [diff] [blame] | 9330 | } |
| 9331 | |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 9332 | /* See if cheap transparency is possible. It is only possible |
| 9333 | * when there is a single transparent color, no semitransparent |
| 9334 | * color, and no opaque color that has the same RGB components |
glennrp | 5a39f37 | 2011-02-25 04:52:16 +0000 | [diff] [blame] | 9335 | * as the transparent color. We only need this information if |
| 9336 | * we are writing a PNG with colortype 0 or 2, and we have not |
| 9337 | * excluded the tRNS chunk. |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 9338 | */ |
glennrp | 5a39f37 | 2011-02-25 04:52:16 +0000 | [diff] [blame] | 9339 | if (number_transparent == 1 && |
| 9340 | mng_info->write_png_colortype < 4) |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 9341 | { |
| 9342 | ping_have_cheap_transparency = MagickTrue; |
| 9343 | |
| 9344 | if (number_semitransparent != 0) |
| 9345 | ping_have_cheap_transparency = MagickFalse; |
| 9346 | |
| 9347 | else if (image_colors == 0 || image_colors > 256 || |
| 9348 | image->colormap == NULL) |
| 9349 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 9350 | register const Quantum |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 9351 | *q; |
| 9352 | |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 9353 | for (y=0; y < (ssize_t) image->rows; y++) |
| 9354 | { |
| 9355 | q=GetVirtualPixels(image,0,y,image->columns,1, exception); |
| 9356 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 9357 | if (q == (Quantum *) NULL) |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 9358 | break; |
| 9359 | |
| 9360 | for (x=0; x < (ssize_t) image->columns; x++) |
| 9361 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 9362 | if (GetPixelAlpha(image,q) != TransparentAlpha && |
| 9363 | (unsigned short) GetPixelRed(image,q) == |
| 9364 | ping_trans_color.red && |
| 9365 | (unsigned short) GetPixelGreen(image,q) == |
| 9366 | ping_trans_color.green && |
| 9367 | (unsigned short) GetPixelBlue(image,q) == |
| 9368 | ping_trans_color.blue) |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 9369 | { |
| 9370 | ping_have_cheap_transparency = MagickFalse; |
| 9371 | break; |
| 9372 | } |
| 9373 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 9374 | q+=GetPixelChannels(image); |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 9375 | } |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 9376 | |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 9377 | if (ping_have_cheap_transparency == MagickFalse) |
| 9378 | break; |
| 9379 | } |
| 9380 | } |
| 9381 | else |
| 9382 | { |
glennrp | 67b9c1a | 2011-04-22 18:47:36 +0000 | [diff] [blame] | 9383 | /* Assuming that image->colormap[0] is the one transparent color |
| 9384 | * and that all others are opaque. |
| 9385 | */ |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 9386 | if (image_colors > 1) |
glennrp | 67b9c1a | 2011-04-22 18:47:36 +0000 | [diff] [blame] | 9387 | for (i=1; i<image_colors; i++) |
| 9388 | if (image->colormap[i].red == image->colormap[0].red && |
| 9389 | image->colormap[i].green == image->colormap[0].green && |
| 9390 | image->colormap[i].blue == image->colormap[0].blue) |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 9391 | { |
glennrp | 67b9c1a | 2011-04-22 18:47:36 +0000 | [diff] [blame] | 9392 | ping_have_cheap_transparency = MagickFalse; |
| 9393 | break; |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 9394 | } |
| 9395 | } |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 9396 | |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 9397 | if (logging != MagickFalse) |
| 9398 | { |
| 9399 | if (ping_have_cheap_transparency == MagickFalse) |
| 9400 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9401 | " Cheap transparency is not possible."); |
| 9402 | |
| 9403 | else |
| 9404 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9405 | " Cheap transparency is possible."); |
| 9406 | } |
| 9407 | } |
| 9408 | else |
| 9409 | ping_have_cheap_transparency = MagickFalse; |
| 9410 | |
glennrp | 8640fb5 | 2010-11-23 15:48:26 +0000 | [diff] [blame] | 9411 | image_depth=image->depth; |
| 9412 | |
glennrp | 26c990a | 2010-11-23 02:23:20 +0000 | [diff] [blame] | 9413 | quantum_info = (QuantumInfo *) NULL; |
| 9414 | number_colors=0; |
glennrp | f09bded | 2011-01-08 01:15:59 +0000 | [diff] [blame] | 9415 | image_colors=(int) image->colors; |
cristy | b0a657e | 2012-08-29 00:45:37 +0000 | [diff] [blame] | 9416 | image_matte=image->alpha_trait == BlendPixelTrait ? MagickTrue : MagickFalse; |
glennrp | 26c990a | 2010-11-23 02:23:20 +0000 | [diff] [blame] | 9417 | |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9418 | mng_info->IsPalette=image->storage_class == PseudoClass && |
glennrp | 1273f7b | 2011-02-24 03:20:30 +0000 | [diff] [blame] | 9419 | image_colors <= 256 && image->colormap != NULL; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9420 | |
glennrp | 52a479c | 2011-02-26 21:14:38 +0000 | [diff] [blame] | 9421 | if ((mng_info->write_png_colortype == 4 || mng_info->write_png8) && |
| 9422 | (image->colors == 0 || image->colormap == NULL)) |
| 9423 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 9424 | image_info=DestroyImageInfo(image_info); |
| 9425 | image=DestroyImage(image); |
| 9426 | (void) ThrowMagickException(exception,GetMagickModule(),CoderError, |
glennrp | 15e0155 | 2011-03-06 23:29:17 +0000 | [diff] [blame] | 9427 | "Cannot write PNG8 or color-type 3; colormap is NULL", |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 9428 | "`%s'",IMimage->filename); |
glennrp | 52a479c | 2011-02-26 21:14:38 +0000 | [diff] [blame] | 9429 | return(MagickFalse); |
| 9430 | } |
| 9431 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9432 | /* |
| 9433 | Allocate the PNG structures |
| 9434 | */ |
| 9435 | #ifdef PNG_USER_MEM_SUPPORTED |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 9436 | error_info.image=image; |
| 9437 | error_info.exception=exception; |
| 9438 | ping=png_create_write_struct_2(PNG_LIBPNG_VER_STRING,&error_info, |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 9439 | MagickPNGErrorHandler,MagickPNGWarningHandler,(void *) NULL, |
| 9440 | (png_malloc_ptr) Magick_png_malloc,(png_free_ptr) Magick_png_free); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9441 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9442 | #else |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 9443 | ping=png_create_write_struct(PNG_LIBPNG_VER_STRING,&error_info, |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 9444 | MagickPNGErrorHandler,MagickPNGWarningHandler); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9445 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9446 | #endif |
| 9447 | if (ping == (png_struct *) NULL) |
| 9448 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9449 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9450 | ping_info=png_create_info_struct(ping); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9451 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9452 | if (ping_info == (png_info *) NULL) |
| 9453 | { |
| 9454 | png_destroy_write_struct(&ping,(png_info **) NULL); |
| 9455 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
| 9456 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9457 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9458 | png_set_write_fn(ping,image,png_put_data,png_flush_data); |
cristy | 0997332 | 2013-06-30 21:06:30 +0000 | [diff] [blame] | 9459 | pixel_info=(MemoryInfo *) NULL; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9460 | |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 9461 | if (setjmp(png_jmpbuf(ping))) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9462 | { |
| 9463 | /* |
| 9464 | PNG write failed. |
| 9465 | */ |
| 9466 | #ifdef PNG_DEBUG |
| 9467 | if (image_info->verbose) |
| 9468 | (void) printf("PNG write has failed.\n"); |
| 9469 | #endif |
| 9470 | png_destroy_write_struct(&ping,&ping_info); |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 9471 | #ifdef PNG_SETJMP_NOT_THREAD_SAFE |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 9472 | UnlockSemaphoreInfo(ping_semaphore); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9473 | #endif |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 9474 | |
cristy | 0997332 | 2013-06-30 21:06:30 +0000 | [diff] [blame] | 9475 | if (pixel_info != (MemoryInfo *) NULL) |
| 9476 | pixel_info=RelinquishVirtualMemory(pixel_info); |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 9477 | |
| 9478 | if (quantum_info != (QuantumInfo *) NULL) |
| 9479 | quantum_info=DestroyQuantumInfo(quantum_info); |
| 9480 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 9481 | if (ping_have_blob != MagickFalse) |
| 9482 | (void) CloseBlob(image); |
| 9483 | image_info=DestroyImageInfo(image_info); |
| 9484 | image=DestroyImage(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9485 | return(MagickFalse); |
| 9486 | } |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 9487 | |
| 9488 | /* { For navigation to end of SETJMP-protected block. Within this |
| 9489 | * block, use png_error() instead of Throwing an Exception, to ensure |
| 9490 | * that libpng is able to clean up, and that the semaphore is unlocked. |
| 9491 | */ |
| 9492 | |
| 9493 | #ifdef PNG_SETJMP_NOT_THREAD_SAFE |
| 9494 | LockSemaphoreInfo(ping_semaphore); |
| 9495 | #endif |
| 9496 | |
glennrp | 943b7d3 | 2013-04-21 00:40:38 +0000 | [diff] [blame] | 9497 | #ifdef PNG_BENIGN_ERRORS_SUPPORTED |
glennrp | a3a5f95 | 2013-04-17 12:58:15 +0000 | [diff] [blame] | 9498 | /* Allow benign errors */ |
| 9499 | png_set_benign_errors(ping, 1); |
| 9500 | #endif |
| 9501 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9502 | /* |
| 9503 | Prepare PNG for writing. |
| 9504 | */ |
glennrp | 9bf97b6 | 2012-06-06 21:03:14 +0000 | [diff] [blame] | 9505 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9506 | #if defined(PNG_MNG_FEATURES_SUPPORTED) |
| 9507 | if (mng_info->write_mng) |
glennrp | 25024a6 | 2012-06-07 11:38:34 +0000 | [diff] [blame] | 9508 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9509 | (void) png_permit_mng_features(ping,PNG_ALL_MNG_FEATURES); |
glennrp | 25024a6 | 2012-06-07 11:38:34 +0000 | [diff] [blame] | 9510 | # ifdef PNG_WRITE_CHECK_FOR_INVALID_INDEX_SUPPORTED |
| 9511 | /* Disable new libpng-1.5.10 feature when writing a MNG because |
| 9512 | * zero-length PLTE is OK |
| 9513 | */ |
| 9514 | png_set_check_for_invalid_index (ping, 0); |
| 9515 | # endif |
| 9516 | } |
glennrp | 2b013e4 | 2010-11-24 16:55:50 +0000 | [diff] [blame] | 9517 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9518 | #else |
| 9519 | # ifdef PNG_WRITE_EMPTY_PLTE_SUPPORTED |
| 9520 | if (mng_info->write_mng) |
| 9521 | png_permit_empty_plte(ping,MagickTrue); |
glennrp | 2b013e4 | 2010-11-24 16:55:50 +0000 | [diff] [blame] | 9522 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9523 | # endif |
| 9524 | #endif |
glennrp | 2b013e4 | 2010-11-24 16:55:50 +0000 | [diff] [blame] | 9525 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9526 | x=0; |
glennrp | 2b013e4 | 2010-11-24 16:55:50 +0000 | [diff] [blame] | 9527 | |
cristy | 4e5bc84 | 2010-06-09 13:56:01 +0000 | [diff] [blame] | 9528 | ping_width=(png_uint_32) image->columns; |
| 9529 | ping_height=(png_uint_32) image->rows; |
glennrp | 2b013e4 | 2010-11-24 16:55:50 +0000 | [diff] [blame] | 9530 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9531 | if (mng_info->write_png8 || mng_info->write_png24 || mng_info->write_png32) |
| 9532 | image_depth=8; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9533 | |
glennrp | fd164d2 | 2013-01-26 21:10:22 +0000 | [diff] [blame] | 9534 | if (mng_info->write_png48 || mng_info->write_png64) |
| 9535 | image_depth=16; |
| 9536 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9537 | if (mng_info->write_png_depth != 0) |
| 9538 | image_depth=mng_info->write_png_depth; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9539 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9540 | /* Adjust requested depth to next higher valid depth if necessary */ |
| 9541 | if (image_depth > 8) |
| 9542 | image_depth=16; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9543 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9544 | if ((image_depth > 4) && (image_depth < 8)) |
| 9545 | image_depth=8; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9546 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9547 | if (image_depth == 3) |
| 9548 | image_depth=4; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9549 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9550 | if (logging != MagickFalse) |
| 9551 | { |
| 9552 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 9553 | " width=%.20g",(double) ping_width); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9554 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 9555 | " height=%.20g",(double) ping_height); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9556 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 9557 | " image_matte=%.20g",(double) image->alpha_trait); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9558 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | 8640fb5 | 2010-11-23 15:48:26 +0000 | [diff] [blame] | 9559 | " image->depth=%.20g",(double) image->depth); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9560 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | 8640fb5 | 2010-11-23 15:48:26 +0000 | [diff] [blame] | 9561 | " Tentative ping_bit_depth=%.20g",(double) image_depth); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9562 | } |
glennrp | 8640fb5 | 2010-11-23 15:48:26 +0000 | [diff] [blame] | 9563 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9564 | save_image_depth=image_depth; |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 9565 | ping_bit_depth=(png_byte) save_image_depth; |
glennrp | dfd7080 | 2010-11-14 01:23:35 +0000 | [diff] [blame] | 9566 | |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 9567 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9568 | #if defined(PNG_pHYs_SUPPORTED) |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 9569 | if (ping_exclude_pHYs == MagickFalse) |
| 9570 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 9571 | if ((image->resolution.x != 0) && (image->resolution.y != 0) && |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9572 | (!mng_info->write_mng || !mng_info->equal_physs)) |
| 9573 | { |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9574 | if (logging != MagickFalse) |
glennrp | dfd7080 | 2010-11-14 01:23:35 +0000 | [diff] [blame] | 9575 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9576 | " Setting up pHYs chunk"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9577 | |
| 9578 | if (image->units == PixelsPerInchResolution) |
| 9579 | { |
glennrp | dfd7080 | 2010-11-14 01:23:35 +0000 | [diff] [blame] | 9580 | ping_pHYs_unit_type=PNG_RESOLUTION_METER; |
glennrp | 823b55c | 2011-03-14 18:46:46 +0000 | [diff] [blame] | 9581 | ping_pHYs_x_resolution= |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 9582 | (png_uint_32) ((100.0*image->resolution.x+0.5)/2.54); |
glennrp | 823b55c | 2011-03-14 18:46:46 +0000 | [diff] [blame] | 9583 | ping_pHYs_y_resolution= |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 9584 | (png_uint_32) ((100.0*image->resolution.y+0.5)/2.54); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9585 | } |
glennrp | dfd7080 | 2010-11-14 01:23:35 +0000 | [diff] [blame] | 9586 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9587 | else if (image->units == PixelsPerCentimeterResolution) |
| 9588 | { |
glennrp | dfd7080 | 2010-11-14 01:23:35 +0000 | [diff] [blame] | 9589 | ping_pHYs_unit_type=PNG_RESOLUTION_METER; |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 9590 | ping_pHYs_x_resolution=(png_uint_32) (100.0*image->resolution.x+0.5); |
| 9591 | ping_pHYs_y_resolution=(png_uint_32) (100.0*image->resolution.y+0.5); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9592 | } |
glennrp | 991d11d | 2010-11-12 21:55:28 +0000 | [diff] [blame] | 9593 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9594 | else |
| 9595 | { |
glennrp | dfd7080 | 2010-11-14 01:23:35 +0000 | [diff] [blame] | 9596 | ping_pHYs_unit_type=PNG_RESOLUTION_UNKNOWN; |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 9597 | ping_pHYs_x_resolution=(png_uint_32) image->resolution.x; |
| 9598 | ping_pHYs_y_resolution=(png_uint_32) image->resolution.y; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9599 | } |
glennrp | 991d11d | 2010-11-12 21:55:28 +0000 | [diff] [blame] | 9600 | |
glennrp | 823b55c | 2011-03-14 18:46:46 +0000 | [diff] [blame] | 9601 | if (logging != MagickFalse) |
| 9602 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9603 | " Set up PNG pHYs chunk: xres: %.20g, yres: %.20g, units: %d.", |
| 9604 | (double) ping_pHYs_x_resolution,(double) ping_pHYs_y_resolution, |
| 9605 | (int) ping_pHYs_unit_type); |
glennrp | 991d11d | 2010-11-12 21:55:28 +0000 | [diff] [blame] | 9606 | ping_have_pHYs = MagickTrue; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9607 | } |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 9608 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9609 | #endif |
glennrp | a521b2f | 2010-10-29 04:11:03 +0000 | [diff] [blame] | 9610 | |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 9611 | if (ping_exclude_bKGD == MagickFalse) |
| 9612 | { |
glennrp | a521b2f | 2010-10-29 04:11:03 +0000 | [diff] [blame] | 9613 | if ((!mng_info->adjoin || !mng_info->equal_backgrounds)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9614 | { |
glennrp | a521b2f | 2010-10-29 04:11:03 +0000 | [diff] [blame] | 9615 | unsigned int |
| 9616 | mask; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9617 | |
glennrp | a521b2f | 2010-10-29 04:11:03 +0000 | [diff] [blame] | 9618 | mask=0xffff; |
| 9619 | if (ping_bit_depth == 8) |
| 9620 | mask=0x00ff; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9621 | |
glennrp | a521b2f | 2010-10-29 04:11:03 +0000 | [diff] [blame] | 9622 | if (ping_bit_depth == 4) |
| 9623 | mask=0x000f; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9624 | |
glennrp | a521b2f | 2010-10-29 04:11:03 +0000 | [diff] [blame] | 9625 | if (ping_bit_depth == 2) |
| 9626 | mask=0x0003; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9627 | |
glennrp | a521b2f | 2010-10-29 04:11:03 +0000 | [diff] [blame] | 9628 | if (ping_bit_depth == 1) |
| 9629 | mask=0x0001; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9630 | |
glennrp | a521b2f | 2010-10-29 04:11:03 +0000 | [diff] [blame] | 9631 | ping_background.red=(png_uint_16) |
| 9632 | (ScaleQuantumToShort(image->background_color.red) & mask); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9633 | |
glennrp | a521b2f | 2010-10-29 04:11:03 +0000 | [diff] [blame] | 9634 | ping_background.green=(png_uint_16) |
| 9635 | (ScaleQuantumToShort(image->background_color.green) & mask); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9636 | |
glennrp | a521b2f | 2010-10-29 04:11:03 +0000 | [diff] [blame] | 9637 | ping_background.blue=(png_uint_16) |
| 9638 | (ScaleQuantumToShort(image->background_color.blue) & mask); |
glennrp | c6c391a | 2011-04-27 02:23:56 +0000 | [diff] [blame] | 9639 | |
| 9640 | ping_background.gray=(png_uint_16) ping_background.green; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9641 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9642 | |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9643 | if (logging != MagickFalse) |
glennrp | 3b51f0e | 2010-11-27 18:14:08 +0000 | [diff] [blame] | 9644 | { |
| 9645 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9646 | " Setting up bKGD chunk (1)"); |
glennrp | c6c391a | 2011-04-27 02:23:56 +0000 | [diff] [blame] | 9647 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9648 | " background_color index is %d", |
| 9649 | (int) ping_background.index); |
glennrp | 3b51f0e | 2010-11-27 18:14:08 +0000 | [diff] [blame] | 9650 | |
| 9651 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9652 | " ping_bit_depth=%d",ping_bit_depth); |
| 9653 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9654 | |
| 9655 | ping_have_bKGD = MagickTrue; |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 9656 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9657 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9658 | /* |
| 9659 | Select the color type. |
| 9660 | */ |
| 9661 | matte=image_matte; |
| 9662 | old_bit_depth=0; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9663 | |
glennrp | 1273f7b | 2011-02-24 03:20:30 +0000 | [diff] [blame] | 9664 | if (mng_info->IsPalette && mng_info->write_png8) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9665 | { |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9666 | |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 9667 | /* To do: make this a function cause it's used twice, except |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9668 | for reducing the sample depth from 8. */ |
| 9669 | |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9670 | number_colors=image_colors; |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 9671 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 9672 | ping_have_tRNS=MagickFalse; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9673 | |
| 9674 | /* |
| 9675 | Set image palette. |
| 9676 | */ |
| 9677 | ping_color_type=(png_byte) PNG_COLOR_TYPE_PALETTE; |
| 9678 | |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9679 | if (logging != MagickFalse) |
| 9680 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9681 | " Setting up PLTE chunk with %d colors (%d)", |
glennrp | f09bded | 2011-01-08 01:15:59 +0000 | [diff] [blame] | 9682 | number_colors, image_colors); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9683 | |
| 9684 | for (i=0; i < (ssize_t) number_colors; i++) |
| 9685 | { |
| 9686 | palette[i].red=ScaleQuantumToChar(image->colormap[i].red); |
| 9687 | palette[i].green=ScaleQuantumToChar(image->colormap[i].green); |
| 9688 | palette[i].blue=ScaleQuantumToChar(image->colormap[i].blue); |
| 9689 | if (logging != MagickFalse) |
| 9690 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | 67b9c1a | 2011-04-22 18:47:36 +0000 | [diff] [blame] | 9691 | #if MAGICKCORE_QUANTUM_DEPTH == 8 |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9692 | " %3ld (%3d,%3d,%3d)", |
glennrp | 67b9c1a | 2011-04-22 18:47:36 +0000 | [diff] [blame] | 9693 | #else |
| 9694 | " %5ld (%5d,%5d,%5d)", |
| 9695 | #endif |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9696 | (long) i,palette[i].red,palette[i].green,palette[i].blue); |
| 9697 | |
| 9698 | } |
glennrp | 2b013e4 | 2010-11-24 16:55:50 +0000 | [diff] [blame] | 9699 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 9700 | ping_have_PLTE=MagickTrue; |
| 9701 | image_depth=ping_bit_depth; |
| 9702 | ping_num_trans=0; |
glennrp | 2b013e4 | 2010-11-24 16:55:50 +0000 | [diff] [blame] | 9703 | |
glennrp | 58e0176 | 2011-01-07 15:28:54 +0000 | [diff] [blame] | 9704 | if (matte != MagickFalse) |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 9705 | { |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9706 | /* |
| 9707 | Identify which colormap entry is transparent. |
| 9708 | */ |
| 9709 | assert(number_colors <= 256); |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 9710 | assert(image->colormap != NULL); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9711 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 9712 | for (i=0; i < (ssize_t) number_transparent; i++) |
| 9713 | ping_trans_alpha[i]=0; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9714 | |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9715 | |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 9716 | ping_num_trans=(unsigned short) (number_transparent + |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 9717 | number_semitransparent); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9718 | |
| 9719 | if (ping_num_trans == 0) |
| 9720 | ping_have_tRNS=MagickFalse; |
| 9721 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 9722 | else |
| 9723 | ping_have_tRNS=MagickTrue; |
| 9724 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9725 | |
glennrp | 1273f7b | 2011-02-24 03:20:30 +0000 | [diff] [blame] | 9726 | if (ping_exclude_bKGD == MagickFalse) |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 9727 | { |
glennrp | 1273f7b | 2011-02-24 03:20:30 +0000 | [diff] [blame] | 9728 | /* |
| 9729 | * Identify which colormap entry is the background color. |
| 9730 | */ |
| 9731 | |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 9732 | for (i=0; i < (ssize_t) MagickMax(1L*number_colors-1L,1L); i++) |
| 9733 | if (IsPNGColorEqual(ping_background,image->colormap[i])) |
| 9734 | break; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9735 | |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 9736 | ping_background.index=(png_byte) i; |
glennrp | c6c391a | 2011-04-27 02:23:56 +0000 | [diff] [blame] | 9737 | |
| 9738 | if (logging != MagickFalse) |
| 9739 | { |
| 9740 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9741 | " background_color index is %d", |
| 9742 | (int) ping_background.index); |
| 9743 | } |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 9744 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9745 | } /* end of write_png8 */ |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9746 | |
glennrp | fd164d2 | 2013-01-26 21:10:22 +0000 | [diff] [blame] | 9747 | else if (mng_info->write_png_colortype == 1) |
| 9748 | { |
| 9749 | image_matte=MagickFalse; |
| 9750 | ping_color_type=(png_byte) PNG_COLOR_TYPE_GRAY; |
| 9751 | } |
| 9752 | |
| 9753 | else if (mng_info->write_png24 || mng_info->write_png48 || |
| 9754 | mng_info->write_png_colortype == 3) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9755 | { |
| 9756 | image_matte=MagickFalse; |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 9757 | ping_color_type=(png_byte) PNG_COLOR_TYPE_RGB; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9758 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9759 | |
glennrp | fd164d2 | 2013-01-26 21:10:22 +0000 | [diff] [blame] | 9760 | else if (mng_info->write_png32 || mng_info->write_png64 || |
| 9761 | mng_info->write_png_colortype == 7) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9762 | { |
| 9763 | image_matte=MagickTrue; |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 9764 | ping_color_type=(png_byte) PNG_COLOR_TYPE_RGB_ALPHA; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9765 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9766 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 9767 | else /* mng_info->write_pngNN not specified */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9768 | { |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 9769 | image_depth=ping_bit_depth; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9770 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 9771 | if (mng_info->write_png_colortype != 0) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9772 | { |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 9773 | ping_color_type=(png_byte) mng_info->write_png_colortype-1; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9774 | |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 9775 | if (ping_color_type == PNG_COLOR_TYPE_GRAY_ALPHA || |
| 9776 | ping_color_type == PNG_COLOR_TYPE_RGB_ALPHA) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9777 | image_matte=MagickTrue; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 9778 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 9779 | else |
| 9780 | image_matte=MagickFalse; |
glennrp | 7c4c9e6 | 2011-03-21 20:23:32 +0000 | [diff] [blame] | 9781 | |
| 9782 | if (logging != MagickFalse) |
| 9783 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9784 | " PNG colortype %d was specified:",(int) ping_color_type); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9785 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9786 | |
glennrp | 7c4c9e6 | 2011-03-21 20:23:32 +0000 | [diff] [blame] | 9787 | else /* write_png_colortype not specified */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9788 | { |
| 9789 | if (logging != MagickFalse) |
| 9790 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | 3c21811 | 2010-11-27 15:31:26 +0000 | [diff] [blame] | 9791 | " Selecting PNG colortype:"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9792 | |
glennrp | d6bf161 | 2010-12-17 17:28:54 +0000 | [diff] [blame] | 9793 | ping_color_type=(png_byte) ((matte != MagickFalse)? |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 9794 | PNG_COLOR_TYPE_RGB_ALPHA:PNG_COLOR_TYPE_RGB); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9795 | |
glennrp | d6bf161 | 2010-12-17 17:28:54 +0000 | [diff] [blame] | 9796 | if (image_info->type == TrueColorType) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9797 | { |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 9798 | ping_color_type=(png_byte) PNG_COLOR_TYPE_RGB; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9799 | image_matte=MagickFalse; |
| 9800 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9801 | |
glennrp | d6bf161 | 2010-12-17 17:28:54 +0000 | [diff] [blame] | 9802 | if (image_info->type == TrueColorMatteType) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9803 | { |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 9804 | ping_color_type=(png_byte) PNG_COLOR_TYPE_RGB_ALPHA; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9805 | image_matte=MagickTrue; |
| 9806 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9807 | |
glennrp | 5aa37f6 | 2011-01-02 03:07:57 +0000 | [diff] [blame] | 9808 | if (image_info->type == PaletteType || |
| 9809 | image_info->type == PaletteMatteType) |
| 9810 | ping_color_type=(png_byte) PNG_COLOR_TYPE_PALETTE; |
| 9811 | |
glennrp | 7c4c9e6 | 2011-03-21 20:23:32 +0000 | [diff] [blame] | 9812 | if (mng_info->write_png_colortype == 0 && |
| 9813 | (image_info->type == UndefinedType || |
| 9814 | image_info->type == OptimizeType)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9815 | { |
glennrp | 5aa37f6 | 2011-01-02 03:07:57 +0000 | [diff] [blame] | 9816 | if (ping_have_color == MagickFalse) |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 9817 | { |
glennrp | 5aa37f6 | 2011-01-02 03:07:57 +0000 | [diff] [blame] | 9818 | if (image_matte == MagickFalse) |
| 9819 | { |
| 9820 | ping_color_type=(png_byte) PNG_COLOR_TYPE_GRAY; |
| 9821 | image_matte=MagickFalse; |
| 9822 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9823 | |
glennrp | 0b206f5 | 2011-01-07 04:55:32 +0000 | [diff] [blame] | 9824 | else |
glennrp | 5aa37f6 | 2011-01-02 03:07:57 +0000 | [diff] [blame] | 9825 | { |
| 9826 | ping_color_type=(png_byte) PNG_COLOR_TYPE_GRAY_ALPHA; |
| 9827 | image_matte=MagickTrue; |
| 9828 | } |
| 9829 | } |
| 9830 | else |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 9831 | { |
glennrp | 5aa37f6 | 2011-01-02 03:07:57 +0000 | [diff] [blame] | 9832 | if (image_matte == MagickFalse) |
| 9833 | { |
| 9834 | ping_color_type=(png_byte) PNG_COLOR_TYPE_RGB; |
| 9835 | image_matte=MagickFalse; |
| 9836 | } |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 9837 | |
glennrp | 0b206f5 | 2011-01-07 04:55:32 +0000 | [diff] [blame] | 9838 | else |
glennrp | 5aa37f6 | 2011-01-02 03:07:57 +0000 | [diff] [blame] | 9839 | { |
| 9840 | ping_color_type=(png_byte) PNG_COLOR_TYPE_RGBA; |
| 9841 | image_matte=MagickTrue; |
| 9842 | } |
| 9843 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9844 | } |
glennrp | 5aa37f6 | 2011-01-02 03:07:57 +0000 | [diff] [blame] | 9845 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9846 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9847 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9848 | if (logging != MagickFalse) |
| 9849 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | 8640fb5 | 2010-11-23 15:48:26 +0000 | [diff] [blame] | 9850 | " Selected PNG colortype=%d",ping_color_type); |
glennrp | 26c990a | 2010-11-23 02:23:20 +0000 | [diff] [blame] | 9851 | |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 9852 | if (ping_bit_depth < 8) |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9853 | { |
| 9854 | if (ping_color_type == PNG_COLOR_TYPE_GRAY_ALPHA || |
| 9855 | ping_color_type == PNG_COLOR_TYPE_RGB || |
| 9856 | ping_color_type == PNG_COLOR_TYPE_RGB_ALPHA) |
| 9857 | ping_bit_depth=8; |
| 9858 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9859 | |
glennrp | d6bf161 | 2010-12-17 17:28:54 +0000 | [diff] [blame] | 9860 | old_bit_depth=ping_bit_depth; |
| 9861 | |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 9862 | if (ping_color_type == PNG_COLOR_TYPE_GRAY) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9863 | { |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 9864 | if (image->alpha_trait != BlendPixelTrait && ping_have_non_bw == MagickFalse) |
glennrp | 8d57966 | 2011-02-23 02:05:02 +0000 | [diff] [blame] | 9865 | ping_bit_depth=1; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9866 | } |
glennrp | 8640fb5 | 2010-11-23 15:48:26 +0000 | [diff] [blame] | 9867 | |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 9868 | if (ping_color_type == PNG_COLOR_TYPE_PALETTE) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9869 | { |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 9870 | size_t one = 1; |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 9871 | ping_bit_depth=1; |
glennrp | 0f11198 | 2010-07-07 20:18:33 +0000 | [diff] [blame] | 9872 | |
| 9873 | if (image->colors == 0) |
| 9874 | { |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9875 | /* DO SOMETHING */ |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 9876 | png_error(ping,"image has 0 colors"); |
glennrp | 0f11198 | 2010-07-07 20:18:33 +0000 | [diff] [blame] | 9877 | } |
| 9878 | |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 9879 | while ((int) (one << ping_bit_depth) < (ssize_t) image_colors) |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 9880 | ping_bit_depth <<= 1; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9881 | } |
glennrp | 2b013e4 | 2010-11-24 16:55:50 +0000 | [diff] [blame] | 9882 | |
glennrp | d6bf161 | 2010-12-17 17:28:54 +0000 | [diff] [blame] | 9883 | if (logging != MagickFalse) |
| 9884 | { |
| 9885 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9886 | " Number of colors: %.20g",(double) image_colors); |
| 9887 | |
| 9888 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9889 | " Tentative PNG bit depth: %d",ping_bit_depth); |
| 9890 | } |
| 9891 | |
| 9892 | if (ping_bit_depth < (int) mng_info->write_png_depth) |
| 9893 | ping_bit_depth = mng_info->write_png_depth; |
| 9894 | } |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 9895 | |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 9896 | image_depth=ping_bit_depth; |
glennrp | 2b013e4 | 2010-11-24 16:55:50 +0000 | [diff] [blame] | 9897 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9898 | if (logging != MagickFalse) |
| 9899 | { |
| 9900 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | 1a56e9c | 2012-04-25 17:06:57 +0000 | [diff] [blame] | 9901 | " Tentative PNG color type: %s (%.20g)", |
| 9902 | PngColorTypeToString(ping_color_type), |
| 9903 | (double) ping_color_type); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9904 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9905 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 9906 | " image_info->type: %.20g",(double) image_info->type); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9907 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9908 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 9909 | " image_depth: %.20g",(double) image_depth); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9910 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9911 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | 3c21811 | 2010-11-27 15:31:26 +0000 | [diff] [blame] | 9912 | |
glennrp | 8640fb5 | 2010-11-23 15:48:26 +0000 | [diff] [blame] | 9913 | " image->depth: %.20g",(double) image->depth); |
| 9914 | |
| 9915 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 9916 | " ping_bit_depth: %.20g",(double) ping_bit_depth); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9917 | } |
| 9918 | |
glennrp | 58e0176 | 2011-01-07 15:28:54 +0000 | [diff] [blame] | 9919 | if (matte != MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9920 | { |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 9921 | if (mng_info->IsPalette) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9922 | { |
glennrp | 7c4c9e6 | 2011-03-21 20:23:32 +0000 | [diff] [blame] | 9923 | if (mng_info->write_png_colortype == 0) |
| 9924 | { |
| 9925 | ping_color_type=PNG_COLOR_TYPE_GRAY_ALPHA; |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 9926 | |
glennrp | 7c4c9e6 | 2011-03-21 20:23:32 +0000 | [diff] [blame] | 9927 | if (ping_have_color != MagickFalse) |
| 9928 | ping_color_type=PNG_COLOR_TYPE_RGBA; |
| 9929 | } |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 9930 | |
| 9931 | /* |
| 9932 | * Determine if there is any transparent color. |
| 9933 | */ |
| 9934 | if (number_transparent + number_semitransparent == 0) |
| 9935 | { |
| 9936 | /* |
| 9937 | No transparent pixels are present. Change 4 or 6 to 0 or 2. |
| 9938 | */ |
glennrp | a6a0663 | 2011-01-19 15:15:34 +0000 | [diff] [blame] | 9939 | |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 9940 | image_matte=MagickFalse; |
glennrp | 7c4c9e6 | 2011-03-21 20:23:32 +0000 | [diff] [blame] | 9941 | |
| 9942 | if (mng_info->write_png_colortype == 0) |
| 9943 | ping_color_type&=0x03; |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 9944 | } |
| 9945 | |
| 9946 | else |
| 9947 | { |
| 9948 | unsigned int |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 9949 | mask; |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 9950 | |
| 9951 | mask=0xffff; |
| 9952 | |
| 9953 | if (ping_bit_depth == 8) |
| 9954 | mask=0x00ff; |
| 9955 | |
| 9956 | if (ping_bit_depth == 4) |
| 9957 | mask=0x000f; |
| 9958 | |
| 9959 | if (ping_bit_depth == 2) |
| 9960 | mask=0x0003; |
| 9961 | |
| 9962 | if (ping_bit_depth == 1) |
| 9963 | mask=0x0001; |
| 9964 | |
| 9965 | ping_trans_color.red=(png_uint_16) |
| 9966 | (ScaleQuantumToShort(image->colormap[0].red) & mask); |
| 9967 | |
| 9968 | ping_trans_color.green=(png_uint_16) |
| 9969 | (ScaleQuantumToShort(image->colormap[0].green) & mask); |
| 9970 | |
| 9971 | ping_trans_color.blue=(png_uint_16) |
| 9972 | (ScaleQuantumToShort(image->colormap[0].blue) & mask); |
| 9973 | |
| 9974 | ping_trans_color.gray=(png_uint_16) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 9975 | (ScaleQuantumToShort(GetPixelInfoIntensity( |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 9976 | image->colormap)) & mask); |
| 9977 | |
| 9978 | ping_trans_color.index=(png_byte) 0; |
| 9979 | |
| 9980 | ping_have_tRNS=MagickTrue; |
| 9981 | } |
| 9982 | |
| 9983 | if (ping_have_tRNS != MagickFalse) |
| 9984 | { |
| 9985 | /* |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 9986 | * Determine if there is one and only one transparent color |
| 9987 | * and if so if it is fully transparent. |
| 9988 | */ |
| 9989 | if (ping_have_cheap_transparency == MagickFalse) |
| 9990 | ping_have_tRNS=MagickFalse; |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 9991 | } |
| 9992 | |
| 9993 | if (ping_have_tRNS != MagickFalse) |
| 9994 | { |
glennrp | 7c4c9e6 | 2011-03-21 20:23:32 +0000 | [diff] [blame] | 9995 | if (mng_info->write_png_colortype == 0) |
| 9996 | ping_color_type &= 0x03; /* changes 4 or 6 to 0 or 2 */ |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 9997 | |
| 9998 | if (image_depth == 8) |
| 9999 | { |
| 10000 | ping_trans_color.red&=0xff; |
| 10001 | ping_trans_color.green&=0xff; |
| 10002 | ping_trans_color.blue&=0xff; |
| 10003 | ping_trans_color.gray&=0xff; |
| 10004 | } |
| 10005 | } |
| 10006 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10007 | else |
| 10008 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10009 | if (image_depth == 8) |
| 10010 | { |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 10011 | ping_trans_color.red&=0xff; |
| 10012 | ping_trans_color.green&=0xff; |
| 10013 | ping_trans_color.blue&=0xff; |
| 10014 | ping_trans_color.gray&=0xff; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10015 | } |
| 10016 | } |
| 10017 | } |
glennrp | 8640fb5 | 2010-11-23 15:48:26 +0000 | [diff] [blame] | 10018 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10019 | matte=image_matte; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10020 | |
glennrp | 2e09f55 | 2010-11-14 00:38:48 +0000 | [diff] [blame] | 10021 | if (ping_have_tRNS != MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10022 | image_matte=MagickFalse; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10023 | |
glennrp | 39992b4 | 2010-11-14 00:03:43 +0000 | [diff] [blame] | 10024 | if ((mng_info->IsPalette) && |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10025 | mng_info->write_png_colortype-1 != PNG_COLOR_TYPE_PALETTE && |
glennrp | 8d57966 | 2011-02-23 02:05:02 +0000 | [diff] [blame] | 10026 | ping_have_color == MagickFalse && |
| 10027 | (image_matte == MagickFalse || image_depth >= 8)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10028 | { |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 10029 | size_t one=1; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10030 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10031 | if (image_matte != MagickFalse) |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 10032 | ping_color_type=PNG_COLOR_TYPE_GRAY_ALPHA; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10033 | |
glennrp | 7c4c9e6 | 2011-03-21 20:23:32 +0000 | [diff] [blame] | 10034 | else if (mng_info->write_png_colortype-1 != PNG_COLOR_TYPE_GRAY_ALPHA) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10035 | { |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 10036 | ping_color_type=PNG_COLOR_TYPE_GRAY; |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 10037 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10038 | if (save_image_depth == 16 && image_depth == 8) |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 10039 | { |
| 10040 | if (logging != MagickFalse) |
| 10041 | { |
| 10042 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10043 | " Scaling ping_trans_color (0)"); |
| 10044 | } |
| 10045 | ping_trans_color.gray*=0x0101; |
| 10046 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10047 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10048 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10049 | if (image_depth > MAGICKCORE_QUANTUM_DEPTH) |
| 10050 | image_depth=MAGICKCORE_QUANTUM_DEPTH; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10051 | |
glennrp | 136ee3a | 2011-04-27 15:47:45 +0000 | [diff] [blame] | 10052 | if ((image_colors == 0) || |
glennrp | d17915c | 2011-04-29 14:24:22 +0000 | [diff] [blame] | 10053 | ((ssize_t) (image_colors-1) > (ssize_t) MaxColormapSize)) |
glennrp | f09bded | 2011-01-08 01:15:59 +0000 | [diff] [blame] | 10054 | image_colors=(int) (one << image_depth); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10055 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10056 | if (image_depth > 8) |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 10057 | ping_bit_depth=16; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10058 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10059 | else |
| 10060 | { |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 10061 | ping_bit_depth=8; |
| 10062 | if ((int) ping_color_type == PNG_COLOR_TYPE_PALETTE) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10063 | { |
| 10064 | if(!mng_info->write_png_depth) |
| 10065 | { |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 10066 | ping_bit_depth=1; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10067 | |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 10068 | while ((int) (one << ping_bit_depth) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 10069 | < (ssize_t) image_colors) |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 10070 | ping_bit_depth <<= 1; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10071 | } |
| 10072 | } |
glennrp | 2b013e4 | 2010-11-24 16:55:50 +0000 | [diff] [blame] | 10073 | |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10074 | else if (ping_color_type == |
| 10075 | PNG_COLOR_TYPE_GRAY && image_colors < 17 && |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10076 | mng_info->IsPalette) |
| 10077 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10078 | /* Check if grayscale is reducible */ |
glennrp | 1a0aaa6 | 2011-03-07 17:40:17 +0000 | [diff] [blame] | 10079 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10080 | int |
| 10081 | depth_4_ok=MagickTrue, |
| 10082 | depth_2_ok=MagickTrue, |
| 10083 | depth_1_ok=MagickTrue; |
| 10084 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 10085 | for (i=0; i < (ssize_t) image_colors; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10086 | { |
| 10087 | unsigned char |
| 10088 | intensity; |
| 10089 | |
| 10090 | intensity=ScaleQuantumToChar(image->colormap[i].red); |
| 10091 | |
| 10092 | if ((intensity & 0x0f) != ((intensity & 0xf0) >> 4)) |
| 10093 | depth_4_ok=depth_2_ok=depth_1_ok=MagickFalse; |
| 10094 | else if ((intensity & 0x03) != ((intensity & 0x0c) >> 2)) |
| 10095 | depth_2_ok=depth_1_ok=MagickFalse; |
glennrp | 4bf8973 | 2011-03-21 13:48:28 +0000 | [diff] [blame] | 10096 | else if ((intensity & 0x01) != ((intensity & 0x02) >> 1)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10097 | depth_1_ok=MagickFalse; |
| 10098 | } |
glennrp | 2b013e4 | 2010-11-24 16:55:50 +0000 | [diff] [blame] | 10099 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10100 | if (depth_1_ok && mng_info->write_png_depth <= 1) |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 10101 | ping_bit_depth=1; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10102 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10103 | else if (depth_2_ok && mng_info->write_png_depth <= 2) |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 10104 | ping_bit_depth=2; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10105 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10106 | else if (depth_4_ok && mng_info->write_png_depth <= 4) |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 10107 | ping_bit_depth=4; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10108 | } |
| 10109 | } |
glennrp | 2b013e4 | 2010-11-24 16:55:50 +0000 | [diff] [blame] | 10110 | |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 10111 | image_depth=ping_bit_depth; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10112 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10113 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10114 | else |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10115 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10116 | if (mng_info->IsPalette) |
| 10117 | { |
glennrp | 17a1485 | 2010-05-10 03:01:59 +0000 | [diff] [blame] | 10118 | number_colors=image_colors; |
| 10119 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10120 | if (image_depth <= 8) |
| 10121 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10122 | /* |
| 10123 | Set image palette. |
| 10124 | */ |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 10125 | ping_color_type=(png_byte) PNG_COLOR_TYPE_PALETTE; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10126 | |
glennrp | 3d62786 | 2013-02-26 00:19:34 +0000 | [diff] [blame] | 10127 | if (!(mng_info->have_write_global_plte && matte == MagickFalse)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10128 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 10129 | for (i=0; i < (ssize_t) number_colors; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10130 | { |
| 10131 | palette[i].red=ScaleQuantumToChar(image->colormap[i].red); |
| 10132 | palette[i].green=ScaleQuantumToChar(image->colormap[i].green); |
| 10133 | palette[i].blue=ScaleQuantumToChar(image->colormap[i].blue); |
| 10134 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10135 | |
glennrp | 3b51f0e | 2010-11-27 18:14:08 +0000 | [diff] [blame] | 10136 | if (logging != MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10137 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | 98156a3 | 2009-12-09 15:32:44 +0000 | [diff] [blame] | 10138 | " Setting up PLTE chunk with %d colors", |
glennrp | f09bded | 2011-01-08 01:15:59 +0000 | [diff] [blame] | 10139 | number_colors); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10140 | |
glennrp | 39992b4 | 2010-11-14 00:03:43 +0000 | [diff] [blame] | 10141 | ping_have_PLTE=MagickTrue; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10142 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10143 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10144 | /* color_type is PNG_COLOR_TYPE_PALETTE */ |
glennrp | d6bf161 | 2010-12-17 17:28:54 +0000 | [diff] [blame] | 10145 | if (mng_info->write_png_depth == 0) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10146 | { |
cristy | befe4d2 | 2010-06-07 01:18:58 +0000 | [diff] [blame] | 10147 | size_t |
| 10148 | one; |
| 10149 | |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 10150 | ping_bit_depth=1; |
cristy | befe4d2 | 2010-06-07 01:18:58 +0000 | [diff] [blame] | 10151 | one=1; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10152 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 10153 | while ((one << ping_bit_depth) < (size_t) number_colors) |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 10154 | ping_bit_depth <<= 1; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10155 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10156 | |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 10157 | ping_num_trans=0; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10158 | |
glennrp | 58e0176 | 2011-01-07 15:28:54 +0000 | [diff] [blame] | 10159 | if (matte != MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10160 | { |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10161 | /* |
glennrp | d6bf161 | 2010-12-17 17:28:54 +0000 | [diff] [blame] | 10162 | * Set up trans_colors array. |
| 10163 | */ |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10164 | assert(number_colors <= 256); |
| 10165 | |
glennrp | d6bf161 | 2010-12-17 17:28:54 +0000 | [diff] [blame] | 10166 | ping_num_trans=(unsigned short) (number_transparent + |
| 10167 | number_semitransparent); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10168 | |
| 10169 | if (ping_num_trans == 0) |
| 10170 | ping_have_tRNS=MagickFalse; |
| 10171 | |
glennrp | d6bf161 | 2010-12-17 17:28:54 +0000 | [diff] [blame] | 10172 | else |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10173 | { |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 10174 | if (logging != MagickFalse) |
| 10175 | { |
| 10176 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10177 | " Scaling ping_trans_color (1)"); |
| 10178 | } |
glennrp | d6bf161 | 2010-12-17 17:28:54 +0000 | [diff] [blame] | 10179 | ping_have_tRNS=MagickTrue; |
| 10180 | |
| 10181 | for (i=0; i < ping_num_trans; i++) |
| 10182 | { |
glennrp | 750105b | 2012-04-25 16:20:45 +0000 | [diff] [blame] | 10183 | ping_trans_alpha[i]= (png_byte) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 10184 | ScaleQuantumToChar(image->colormap[i].alpha); |
glennrp | d6bf161 | 2010-12-17 17:28:54 +0000 | [diff] [blame] | 10185 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10186 | } |
| 10187 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10188 | } |
| 10189 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10190 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10191 | else |
| 10192 | { |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 10193 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10194 | if (image_depth < 8) |
| 10195 | image_depth=8; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10196 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10197 | if ((save_image_depth == 16) && (image_depth == 8)) |
| 10198 | { |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 10199 | if (logging != MagickFalse) |
| 10200 | { |
| 10201 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10202 | " Scaling ping_trans_color from (%d,%d,%d)", |
| 10203 | (int) ping_trans_color.red, |
| 10204 | (int) ping_trans_color.green, |
| 10205 | (int) ping_trans_color.blue); |
| 10206 | } |
| 10207 | |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 10208 | ping_trans_color.red*=0x0101; |
| 10209 | ping_trans_color.green*=0x0101; |
| 10210 | ping_trans_color.blue*=0x0101; |
| 10211 | ping_trans_color.gray*=0x0101; |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 10212 | |
| 10213 | if (logging != MagickFalse) |
| 10214 | { |
| 10215 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10216 | " to (%d,%d,%d)", |
| 10217 | (int) ping_trans_color.red, |
| 10218 | (int) ping_trans_color.green, |
| 10219 | (int) ping_trans_color.blue); |
| 10220 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10221 | } |
| 10222 | } |
| 10223 | |
cristy | 4383ec8 | 2011-01-05 15:42:32 +0000 | [diff] [blame] | 10224 | if (ping_bit_depth < (ssize_t) mng_info->write_png_depth) |
| 10225 | ping_bit_depth = (ssize_t) mng_info->write_png_depth; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10226 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10227 | /* |
| 10228 | Adjust background and transparency samples in sub-8-bit grayscale files. |
| 10229 | */ |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 10230 | if (ping_bit_depth < 8 && ping_color_type == |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10231 | PNG_COLOR_TYPE_GRAY) |
| 10232 | { |
| 10233 | png_uint_16 |
| 10234 | maxval; |
| 10235 | |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 10236 | size_t |
| 10237 | one=1; |
| 10238 | |
cristy | 22ffd97 | 2010-06-03 16:51:47 +0000 | [diff] [blame] | 10239 | maxval=(png_uint_16) ((one << ping_bit_depth)-1); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10240 | |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 10241 | if (ping_exclude_bKGD == MagickFalse) |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10242 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10243 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 10244 | ping_background.gray=(png_uint_16) ((maxval/65535.)* |
| 10245 | (ScaleQuantumToShort(((GetPixelInfoIntensity( |
| 10246 | &image->background_color))) +.5))); |
| 10247 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10248 | if (logging != MagickFalse) |
| 10249 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 10250 | " Setting up bKGD chunk (2)"); |
| 10251 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10252 | " background_color index is %d", |
| 10253 | (int) ping_background.index); |
glennrp | 3b51f0e | 2010-11-27 18:14:08 +0000 | [diff] [blame] | 10254 | |
glennrp | 991d11d | 2010-11-12 21:55:28 +0000 | [diff] [blame] | 10255 | ping_have_bKGD = MagickTrue; |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10256 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10257 | |
glennrp | 3e3e20f | 2011-06-09 04:21:43 +0000 | [diff] [blame] | 10258 | if (logging != MagickFalse) |
| 10259 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10260 | " Scaling ping_trans_color.gray from %d", |
| 10261 | (int)ping_trans_color.gray); |
| 10262 | |
glennrp | 9be9b1c | 2011-06-09 12:21:45 +0000 | [diff] [blame] | 10263 | ping_trans_color.gray=(png_uint_16) ((maxval/255.)*( |
glennrp | 3e3e20f | 2011-06-09 04:21:43 +0000 | [diff] [blame] | 10264 | ping_trans_color.gray)+.5); |
| 10265 | |
| 10266 | if (logging != MagickFalse) |
| 10267 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10268 | " to %d", (int)ping_trans_color.gray); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10269 | } |
glennrp | 17a1485 | 2010-05-10 03:01:59 +0000 | [diff] [blame] | 10270 | |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10271 | if (ping_exclude_bKGD == MagickFalse) |
| 10272 | { |
glennrp | 1273f7b | 2011-02-24 03:20:30 +0000 | [diff] [blame] | 10273 | if (mng_info->IsPalette && (int) ping_color_type == PNG_COLOR_TYPE_PALETTE) |
glennrp | 17a1485 | 2010-05-10 03:01:59 +0000 | [diff] [blame] | 10274 | { |
| 10275 | /* |
| 10276 | Identify which colormap entry is the background color. |
| 10277 | */ |
| 10278 | |
glennrp | 17a1485 | 2010-05-10 03:01:59 +0000 | [diff] [blame] | 10279 | number_colors=image_colors; |
| 10280 | |
glennrp | a521b2f | 2010-10-29 04:11:03 +0000 | [diff] [blame] | 10281 | for (i=0; i < (ssize_t) MagickMax(1L*number_colors,1L); i++) |
| 10282 | if (IsPNGColorEqual(image->background_color,image->colormap[i])) |
glennrp | 17a1485 | 2010-05-10 03:01:59 +0000 | [diff] [blame] | 10283 | break; |
| 10284 | |
| 10285 | ping_background.index=(png_byte) i; |
| 10286 | |
glennrp | 3b51f0e | 2010-11-27 18:14:08 +0000 | [diff] [blame] | 10287 | if (logging != MagickFalse) |
glennrp | a521b2f | 2010-10-29 04:11:03 +0000 | [diff] [blame] | 10288 | { |
| 10289 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10290 | " Setting up bKGD chunk with index=%d",(int) i); |
glennrp | a521b2f | 2010-10-29 04:11:03 +0000 | [diff] [blame] | 10291 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10292 | |
cristy | 13d0704 | 2010-11-21 20:56:18 +0000 | [diff] [blame] | 10293 | if (i < (ssize_t) number_colors) |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10294 | { |
| 10295 | ping_have_bKGD = MagickTrue; |
glennrp | 3b51f0e | 2010-11-27 18:14:08 +0000 | [diff] [blame] | 10296 | |
| 10297 | if (logging != MagickFalse) |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10298 | { |
| 10299 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10300 | " background =(%d,%d,%d)", |
| 10301 | (int) ping_background.red, |
| 10302 | (int) ping_background.green, |
| 10303 | (int) ping_background.blue); |
| 10304 | } |
| 10305 | } |
glennrp | a521b2f | 2010-10-29 04:11:03 +0000 | [diff] [blame] | 10306 | |
glennrp | d6bf161 | 2010-12-17 17:28:54 +0000 | [diff] [blame] | 10307 | else /* Can't happen */ |
glennrp | 3c21811 | 2010-11-27 15:31:26 +0000 | [diff] [blame] | 10308 | { |
glennrp | 3b51f0e | 2010-11-27 18:14:08 +0000 | [diff] [blame] | 10309 | if (logging != MagickFalse) |
| 10310 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10311 | " No room in PLTE to add bKGD color"); |
glennrp | 3c21811 | 2010-11-27 15:31:26 +0000 | [diff] [blame] | 10312 | ping_have_bKGD = MagickFalse; |
| 10313 | } |
glennrp | 17a1485 | 2010-05-10 03:01:59 +0000 | [diff] [blame] | 10314 | } |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10315 | } |
glennrp | 17a1485 | 2010-05-10 03:01:59 +0000 | [diff] [blame] | 10316 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10317 | if (logging != MagickFalse) |
| 10318 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | 1a56e9c | 2012-04-25 17:06:57 +0000 | [diff] [blame] | 10319 | " PNG color type: %s (%d)", PngColorTypeToString(ping_color_type), |
| 10320 | ping_color_type); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10321 | /* |
| 10322 | Initialize compression level and filtering. |
| 10323 | */ |
| 10324 | if (logging != MagickFalse) |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10325 | { |
| 10326 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10327 | " Setting up deflate compression"); |
| 10328 | |
| 10329 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10330 | " Compression buffer size: 32768"); |
| 10331 | } |
| 10332 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10333 | png_set_compression_buffer_size(ping,32768L); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10334 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10335 | if (logging != MagickFalse) |
| 10336 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10337 | " Compression mem level: 9"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10338 | |
cristy | 4054bfb | 2011-08-29 23:41:39 +0000 | [diff] [blame] | 10339 | png_set_compression_mem_level(ping, 9); |
| 10340 | |
glennrp | 10d739e | 2011-06-29 18:00:52 +0000 | [diff] [blame] | 10341 | /* Untangle the "-quality" setting: |
| 10342 | |
| 10343 | Undefined is 0; the default is used. |
| 10344 | Default is 75 |
| 10345 | |
| 10346 | 10's digit: |
| 10347 | |
| 10348 | 0: Use Z_HUFFMAN_ONLY strategy with the |
| 10349 | zlib default compression level |
| 10350 | |
| 10351 | 1-9: the zlib compression level |
| 10352 | |
| 10353 | 1's digit: |
| 10354 | |
| 10355 | 0-4: the PNG filter method |
| 10356 | |
| 10357 | 5: libpng adaptive filtering if compression level > 5 |
| 10358 | libpng filter type "none" if compression level <= 5 |
| 10359 | or if image is grayscale or palette |
glennrp | 750105b | 2012-04-25 16:20:45 +0000 | [diff] [blame] | 10360 | |
glennrp | 10d739e | 2011-06-29 18:00:52 +0000 | [diff] [blame] | 10361 | 6: libpng adaptive filtering |
| 10362 | |
| 10363 | 7: "LOCO" filtering (intrapixel differing) if writing |
| 10364 | a MNG, othewise "none". Did not work in IM-6.7.0-9 |
| 10365 | and earlier because of a missing "else". |
| 10366 | |
| 10367 | 8: Z_RLE strategy, all filters |
glennrp | 1868258 | 2011-06-30 18:11:47 +0000 | [diff] [blame] | 10368 | Unused prior to IM-6.7.0-10, was same as 6 |
glennrp | 10d739e | 2011-06-29 18:00:52 +0000 | [diff] [blame] | 10369 | |
| 10370 | 9: Z_RLE strategy, no PNG filters |
glennrp | 1868258 | 2011-06-30 18:11:47 +0000 | [diff] [blame] | 10371 | Unused prior to IM-6.7.0-10, was same as 6 |
glennrp | 10d739e | 2011-06-29 18:00:52 +0000 | [diff] [blame] | 10372 | |
| 10373 | Note that using the -quality option, not all combinations of |
| 10374 | PNG filter type, zlib compression level, and zlib compression |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 10375 | strategy are possible. This will be addressed soon in a |
| 10376 | release that accomodates "-define png:compression-strategy", etc. |
glennrp | 10d739e | 2011-06-29 18:00:52 +0000 | [diff] [blame] | 10377 | |
| 10378 | */ |
| 10379 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10380 | quality=image->quality == UndefinedCompressionQuality ? 75UL : |
| 10381 | image->quality; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10382 | |
glennrp | 1868258 | 2011-06-30 18:11:47 +0000 | [diff] [blame] | 10383 | if (quality <= 9) |
| 10384 | { |
| 10385 | if (mng_info->write_png_compression_strategy == 0) |
| 10386 | mng_info->write_png_compression_strategy = Z_HUFFMAN_ONLY+1; |
| 10387 | } |
glennrp | 750105b | 2012-04-25 16:20:45 +0000 | [diff] [blame] | 10388 | |
glennrp | 1868258 | 2011-06-30 18:11:47 +0000 | [diff] [blame] | 10389 | else if (mng_info->write_png_compression_level == 0) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10390 | { |
| 10391 | int |
| 10392 | level; |
| 10393 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 10394 | level=(int) MagickMin((ssize_t) quality/10,9); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10395 | |
glennrp | 1868258 | 2011-06-30 18:11:47 +0000 | [diff] [blame] | 10396 | mng_info->write_png_compression_level = level+1; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10397 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10398 | |
glennrp | 1868258 | 2011-06-30 18:11:47 +0000 | [diff] [blame] | 10399 | if (mng_info->write_png_compression_strategy == 0) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10400 | { |
glennrp | 1868258 | 2011-06-30 18:11:47 +0000 | [diff] [blame] | 10401 | if ((quality %10) == 8 || (quality %10) == 9) |
glennrp | a24b245 | 2012-06-27 11:38:38 +0000 | [diff] [blame] | 10402 | #ifdef Z_RLE /* Z_RLE was added to zlib-1.2.0 */ |
| 10403 | mng_info->write_png_compression_strategy=Z_RLE+1; |
| 10404 | #else |
| 10405 | mng_info->write_png_compression_strategy = Z_DEFAULT_STRATEGY+1; |
| 10406 | #endif |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10407 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10408 | |
glennrp | 1868258 | 2011-06-30 18:11:47 +0000 | [diff] [blame] | 10409 | if (mng_info->write_png_compression_filter == 0) |
| 10410 | mng_info->write_png_compression_filter=((int) quality % 10) + 1; |
| 10411 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10412 | if (logging != MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10413 | { |
glennrp | 1868258 | 2011-06-30 18:11:47 +0000 | [diff] [blame] | 10414 | if (mng_info->write_png_compression_level) |
| 10415 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10416 | " Compression level: %d", |
| 10417 | (int) mng_info->write_png_compression_level-1); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10418 | |
glennrp | 1868258 | 2011-06-30 18:11:47 +0000 | [diff] [blame] | 10419 | if (mng_info->write_png_compression_strategy) |
| 10420 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10421 | " Compression strategy: %d", |
| 10422 | (int) mng_info->write_png_compression_strategy-1); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10423 | |
glennrp | 1868258 | 2011-06-30 18:11:47 +0000 | [diff] [blame] | 10424 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10425 | " Setting up filtering"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10426 | |
cristy | 4054bfb | 2011-08-29 23:41:39 +0000 | [diff] [blame] | 10427 | if (mng_info->write_png_compression_filter == 6) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10428 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10429 | " Base filter method: ADAPTIVE"); |
cristy | 4054bfb | 2011-08-29 23:41:39 +0000 | [diff] [blame] | 10430 | else if (mng_info->write_png_compression_filter == 0 || |
| 10431 | mng_info->write_png_compression_filter == 1) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10432 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10433 | " Base filter method: NONE"); |
glennrp | 1868258 | 2011-06-30 18:11:47 +0000 | [diff] [blame] | 10434 | else |
| 10435 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10436 | " Base filter method: %d", |
| 10437 | (int) mng_info->write_png_compression_filter-1); |
| 10438 | } |
glennrp | 2b013e4 | 2010-11-24 16:55:50 +0000 | [diff] [blame] | 10439 | |
glennrp | 1868258 | 2011-06-30 18:11:47 +0000 | [diff] [blame] | 10440 | if (mng_info->write_png_compression_level != 0) |
| 10441 | png_set_compression_level(ping,mng_info->write_png_compression_level-1); |
| 10442 | |
| 10443 | if (mng_info->write_png_compression_filter == 6) |
| 10444 | { |
| 10445 | if (((int) ping_color_type == PNG_COLOR_TYPE_GRAY) || |
| 10446 | ((int) ping_color_type == PNG_COLOR_TYPE_PALETTE) || |
| 10447 | (quality < 50)) |
| 10448 | png_set_filter(ping,PNG_FILTER_TYPE_BASE,PNG_NO_FILTERS); |
| 10449 | else |
| 10450 | png_set_filter(ping,PNG_FILTER_TYPE_BASE,PNG_ALL_FILTERS); |
| 10451 | } |
cristy | 4054bfb | 2011-08-29 23:41:39 +0000 | [diff] [blame] | 10452 | else if (mng_info->write_png_compression_filter == 7 || |
glennrp | 1868258 | 2011-06-30 18:11:47 +0000 | [diff] [blame] | 10453 | mng_info->write_png_compression_filter == 10) |
| 10454 | png_set_filter(ping,PNG_FILTER_TYPE_BASE,PNG_ALL_FILTERS); |
| 10455 | |
| 10456 | else if (mng_info->write_png_compression_filter == 8) |
| 10457 | { |
| 10458 | #if defined(PNG_MNG_FEATURES_SUPPORTED) && defined(PNG_INTRAPIXEL_DIFFERENCING) |
| 10459 | if (mng_info->write_mng) |
| 10460 | { |
| 10461 | if (((int) ping_color_type == PNG_COLOR_TYPE_RGB) || |
| 10462 | ((int) ping_color_type == PNG_COLOR_TYPE_RGBA)) |
| 10463 | ping_filter_method=PNG_INTRAPIXEL_DIFFERENCING; |
| 10464 | } |
| 10465 | #endif |
cristy | 4054bfb | 2011-08-29 23:41:39 +0000 | [diff] [blame] | 10466 | png_set_filter(ping,PNG_FILTER_TYPE_BASE,PNG_NO_FILTERS); |
glennrp | 1868258 | 2011-06-30 18:11:47 +0000 | [diff] [blame] | 10467 | } |
| 10468 | |
| 10469 | else if (mng_info->write_png_compression_filter == 9) |
| 10470 | png_set_filter(ping,PNG_FILTER_TYPE_BASE,PNG_NO_FILTERS); |
| 10471 | |
| 10472 | else if (mng_info->write_png_compression_filter != 0) |
| 10473 | png_set_filter(ping,PNG_FILTER_TYPE_BASE, |
| 10474 | mng_info->write_png_compression_filter-1); |
| 10475 | |
| 10476 | if (mng_info->write_png_compression_strategy != 0) |
| 10477 | png_set_compression_strategy(ping, |
| 10478 | mng_info->write_png_compression_strategy-1); |
| 10479 | |
glennrp | dec72c9 | 2013-02-26 17:42:47 +0000 | [diff] [blame] | 10480 | ping_interlace_method=image_info->interlace != NoInterlace; |
| 10481 | |
| 10482 | if (mng_info->write_mng) |
| 10483 | png_set_sig_bytes(ping,8); |
| 10484 | |
| 10485 | /* Bail out if cannot meet defined png:bit-depth or png:color-type */ |
| 10486 | |
| 10487 | if (mng_info->write_png_colortype != 0) |
| 10488 | { |
| 10489 | if (mng_info->write_png_colortype-1 == PNG_COLOR_TYPE_GRAY) |
| 10490 | if (ping_have_color != MagickFalse) |
| 10491 | { |
| 10492 | ping_color_type = PNG_COLOR_TYPE_RGB; |
| 10493 | |
| 10494 | if (ping_bit_depth < 8) |
| 10495 | ping_bit_depth=8; |
| 10496 | } |
| 10497 | |
| 10498 | if (mng_info->write_png_colortype-1 == PNG_COLOR_TYPE_GRAY_ALPHA) |
| 10499 | if (ping_have_color != MagickFalse) |
| 10500 | ping_color_type = PNG_COLOR_TYPE_RGB_ALPHA; |
| 10501 | } |
| 10502 | |
| 10503 | if (ping_need_colortype_warning != MagickFalse || |
| 10504 | ((mng_info->write_png_depth && |
| 10505 | (int) mng_info->write_png_depth != ping_bit_depth) || |
| 10506 | (mng_info->write_png_colortype && |
| 10507 | ((int) mng_info->write_png_colortype-1 != ping_color_type && |
| 10508 | mng_info->write_png_colortype != 7 && |
| 10509 | !(mng_info->write_png_colortype == 5 && ping_color_type == 0))))) |
| 10510 | { |
| 10511 | if (logging != MagickFalse) |
| 10512 | { |
| 10513 | if (ping_need_colortype_warning != MagickFalse) |
| 10514 | { |
| 10515 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10516 | " Image has transparency but tRNS chunk was excluded"); |
| 10517 | } |
| 10518 | |
| 10519 | if (mng_info->write_png_depth) |
| 10520 | { |
| 10521 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10522 | " Defined png:bit-depth=%u, Computed depth=%u", |
| 10523 | mng_info->write_png_depth, |
| 10524 | ping_bit_depth); |
| 10525 | } |
| 10526 | |
| 10527 | if (mng_info->write_png_colortype) |
| 10528 | { |
| 10529 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10530 | " Defined png:color-type=%u, Computed color type=%u", |
| 10531 | mng_info->write_png_colortype-1, |
| 10532 | ping_color_type); |
| 10533 | } |
| 10534 | } |
| 10535 | |
| 10536 | png_warning(ping, |
| 10537 | "Cannot write image with defined png:bit-depth or png:color-type."); |
| 10538 | } |
| 10539 | |
| 10540 | if (image_matte != MagickFalse && image->alpha_trait != BlendPixelTrait) |
| 10541 | { |
| 10542 | /* Add an opaque matte channel */ |
| 10543 | image->alpha_trait = BlendPixelTrait; |
| 10544 | (void) SetImageAlpha(image,OpaqueAlpha,exception); |
| 10545 | |
| 10546 | if (logging != MagickFalse) |
| 10547 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10548 | " Added an opaque matte channel"); |
| 10549 | } |
| 10550 | |
| 10551 | if (number_transparent != 0 || number_semitransparent != 0) |
| 10552 | { |
| 10553 | if (ping_color_type < 4) |
| 10554 | { |
| 10555 | ping_have_tRNS=MagickTrue; |
| 10556 | if (logging != MagickFalse) |
| 10557 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10558 | " Setting ping_have_tRNS=MagickTrue."); |
| 10559 | } |
| 10560 | } |
| 10561 | |
| 10562 | if (logging != MagickFalse) |
| 10563 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10564 | " Writing PNG header chunks"); |
| 10565 | |
| 10566 | png_set_IHDR(ping,ping_info,ping_width,ping_height, |
| 10567 | ping_bit_depth,ping_color_type, |
| 10568 | ping_interlace_method,ping_compression_method, |
| 10569 | ping_filter_method); |
| 10570 | |
| 10571 | if (ping_color_type == 3 && ping_have_PLTE != MagickFalse) |
| 10572 | { |
| 10573 | png_set_PLTE(ping,ping_info,palette,number_colors); |
| 10574 | |
| 10575 | if (logging != MagickFalse) |
| 10576 | { |
| 10577 | for (i=0; i< (ssize_t) number_colors; i++) |
| 10578 | { |
| 10579 | if (i < ping_num_trans) |
| 10580 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10581 | " PLTE[%d] = (%d,%d,%d), tRNS[%d] = (%d)", |
| 10582 | (int) i, |
| 10583 | (int) palette[i].red, |
| 10584 | (int) palette[i].green, |
| 10585 | (int) palette[i].blue, |
| 10586 | (int) i, |
| 10587 | (int) ping_trans_alpha[i]); |
| 10588 | else |
| 10589 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10590 | " PLTE[%d] = (%d,%d,%d)", |
| 10591 | (int) i, |
| 10592 | (int) palette[i].red, |
| 10593 | (int) palette[i].green, |
| 10594 | (int) palette[i].blue); |
| 10595 | } |
| 10596 | } |
| 10597 | } |
| 10598 | |
cristy | 0d57eec | 2011-09-04 22:13:56 +0000 | [diff] [blame] | 10599 | /* Only write the iCCP chunk if we are not writing the sRGB chunk. */ |
| 10600 | if (ping_exclude_sRGB != MagickFalse || |
glennrp | 3d62786 | 2013-02-26 00:19:34 +0000 | [diff] [blame] | 10601 | (!png_get_valid(ping,ping_info,PNG_INFO_sRGB))) |
cristy | 0d57eec | 2011-09-04 22:13:56 +0000 | [diff] [blame] | 10602 | { |
| 10603 | if ((ping_exclude_tEXt == MagickFalse || |
| 10604 | ping_exclude_zTXt == MagickFalse) && |
| 10605 | (ping_exclude_iCCP == MagickFalse || ping_exclude_zCCP == MagickFalse)) |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 10606 | { |
| 10607 | ResetImageProfileIterator(image); |
| 10608 | for (name=GetNextImageProfile(image); name != (const char *) NULL; ) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10609 | { |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 10610 | profile=GetImageProfile(image,name); |
| 10611 | |
| 10612 | if (profile != (StringInfo *) NULL) |
| 10613 | { |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 10614 | #ifdef PNG_WRITE_iCCP_SUPPORTED |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 10615 | if ((LocaleCompare(name,"ICC") == 0) || |
| 10616 | (LocaleCompare(name,"ICM") == 0)) |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10617 | { |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 10618 | |
| 10619 | if (ping_exclude_iCCP == MagickFalse) |
| 10620 | { |
glennrp | ecab7d7 | 2013-05-14 22:50:32 +0000 | [diff] [blame] | 10621 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10622 | " Setting up iCCP chunk"); |
| 10623 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 10624 | png_set_iCCP(ping,ping_info,(png_charp) name,0, |
glennrp | e4017e3 | 2011-01-08 17:16:09 +0000 | [diff] [blame] | 10625 | #if (PNG_LIBPNG_VER < 10500) |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 10626 | (png_charp) GetStringInfoDatum(profile), |
glennrp | e4017e3 | 2011-01-08 17:16:09 +0000 | [diff] [blame] | 10627 | #else |
| 10628 | (png_const_bytep) GetStringInfoDatum(profile), |
| 10629 | #endif |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 10630 | (png_uint_32) GetStringInfoLength(profile)); |
glennrp | 918b9dc | 2013-04-03 13:41:41 +0000 | [diff] [blame] | 10631 | ping_have_iCCP = MagickTrue; |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 10632 | } |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10633 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10634 | |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 10635 | else |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10636 | #endif |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 10637 | if (ping_exclude_zCCP == MagickFalse) |
| 10638 | { |
glennrp | ecab7d7 | 2013-05-14 22:50:32 +0000 | [diff] [blame] | 10639 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10640 | " Setting up zTXT chunk with uuencoded ICC"); |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 10641 | Magick_png_write_raw_profile(image_info,ping,ping_info, |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 10642 | (unsigned char *) name,(unsigned char *) name, |
| 10643 | GetStringInfoDatum(profile), |
| 10644 | (png_uint_32) GetStringInfoLength(profile)); |
glennrp | ecab7d7 | 2013-05-14 22:50:32 +0000 | [diff] [blame] | 10645 | ping_have_iCCP = MagickTrue; |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 10646 | } |
| 10647 | } |
glennrp | 0b206f5 | 2011-01-07 04:55:32 +0000 | [diff] [blame] | 10648 | |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 10649 | if (logging != MagickFalse) |
| 10650 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10651 | " Setting up text chunk with %s profile",name); |
| 10652 | |
| 10653 | name=GetNextImageProfile(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10654 | } |
cristy | 0d57eec | 2011-09-04 22:13:56 +0000 | [diff] [blame] | 10655 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10656 | } |
| 10657 | |
| 10658 | #if defined(PNG_WRITE_sRGB_SUPPORTED) |
| 10659 | if ((mng_info->have_write_global_srgb == 0) && |
glennrp | ecab7d7 | 2013-05-14 22:50:32 +0000 | [diff] [blame] | 10660 | ping_have_iCCP != MagickTrue && |
| 10661 | (ping_have_sRGB != MagickFalse || |
| 10662 | png_get_valid(ping,ping_info,PNG_INFO_sRGB))) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10663 | { |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10664 | if (ping_exclude_sRGB == MagickFalse) |
| 10665 | { |
| 10666 | /* |
| 10667 | Note image rendering intent. |
| 10668 | */ |
| 10669 | if (logging != MagickFalse) |
| 10670 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10671 | " Setting up sRGB chunk"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10672 | |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10673 | (void) png_set_sRGB(ping,ping_info,( |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 10674 | Magick_RenderingIntent_to_PNG_RenderingIntent( |
| 10675 | image->rendering_intent))); |
glennrp | 918b9dc | 2013-04-03 13:41:41 +0000 | [diff] [blame] | 10676 | |
| 10677 | ping_have_sRGB = MagickTrue; |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10678 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10679 | } |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10680 | |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 10681 | if ((!mng_info->write_mng) || (!png_get_valid(ping,ping_info,PNG_INFO_sRGB))) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10682 | #endif |
| 10683 | { |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10684 | if (ping_exclude_gAMA == MagickFalse && |
glennrp | 918b9dc | 2013-04-03 13:41:41 +0000 | [diff] [blame] | 10685 | ping_have_iCCP == MagickFalse && |
| 10686 | ping_have_sRGB == MagickFalse && |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10687 | (ping_exclude_sRGB == MagickFalse || |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10688 | (image->gamma < .45 || image->gamma > .46))) |
| 10689 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10690 | if ((mng_info->have_write_global_gama == 0) && (image->gamma != 0.0)) |
| 10691 | { |
| 10692 | /* |
| 10693 | Note image gamma. |
| 10694 | To do: check for cHRM+gAMA == sRGB, and write sRGB instead. |
| 10695 | */ |
| 10696 | if (logging != MagickFalse) |
| 10697 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10698 | " Setting up gAMA chunk"); |
glennrp | 3b51f0e | 2010-11-27 18:14:08 +0000 | [diff] [blame] | 10699 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10700 | png_set_gAMA(ping,ping_info,image->gamma); |
| 10701 | } |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10702 | } |
glennrp | 2b013e4 | 2010-11-24 16:55:50 +0000 | [diff] [blame] | 10703 | |
glennrp | 918b9dc | 2013-04-03 13:41:41 +0000 | [diff] [blame] | 10704 | if (ping_exclude_cHRM == MagickFalse && ping_have_sRGB == MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10705 | { |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10706 | if ((mng_info->have_write_global_chrm == 0) && |
| 10707 | (image->chromaticity.red_primary.x != 0.0)) |
| 10708 | { |
| 10709 | /* |
| 10710 | Note image chromaticity. |
glennrp | 918b9dc | 2013-04-03 13:41:41 +0000 | [diff] [blame] | 10711 | Note: if cHRM+gAMA == sRGB write sRGB instead. |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10712 | */ |
| 10713 | PrimaryInfo |
| 10714 | bp, |
| 10715 | gp, |
| 10716 | rp, |
| 10717 | wp; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10718 | |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10719 | wp=image->chromaticity.white_point; |
| 10720 | rp=image->chromaticity.red_primary; |
| 10721 | gp=image->chromaticity.green_primary; |
| 10722 | bp=image->chromaticity.blue_primary; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10723 | |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10724 | if (logging != MagickFalse) |
| 10725 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10726 | " Setting up cHRM chunk"); |
glennrp | 3b51f0e | 2010-11-27 18:14:08 +0000 | [diff] [blame] | 10727 | |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10728 | png_set_cHRM(ping,ping_info,wp.x,wp.y,rp.x,rp.y,gp.x,gp.y, |
| 10729 | bp.x,bp.y); |
| 10730 | } |
| 10731 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10732 | } |
glennrp | dfd7080 | 2010-11-14 01:23:35 +0000 | [diff] [blame] | 10733 | |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10734 | if (ping_exclude_bKGD == MagickFalse) |
glennrp | dfd7080 | 2010-11-14 01:23:35 +0000 | [diff] [blame] | 10735 | { |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10736 | if (ping_have_bKGD != MagickFalse) |
glennrp | c6c391a | 2011-04-27 02:23:56 +0000 | [diff] [blame] | 10737 | { |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10738 | png_set_bKGD(ping,ping_info,&ping_background); |
glennrp | c6c391a | 2011-04-27 02:23:56 +0000 | [diff] [blame] | 10739 | if (logging) |
| 10740 | { |
| 10741 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10742 | " Setting up bKGD chunk"); |
| 10743 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10744 | " background color = (%d,%d,%d)", |
| 10745 | (int) ping_background.red, |
| 10746 | (int) ping_background.green, |
| 10747 | (int) ping_background.blue); |
| 10748 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10749 | " index = %d, gray=%d", |
| 10750 | (int) ping_background.index, |
| 10751 | (int) ping_background.gray); |
| 10752 | } |
| 10753 | } |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10754 | } |
| 10755 | |
| 10756 | if (ping_exclude_pHYs == MagickFalse) |
| 10757 | { |
| 10758 | if (ping_have_pHYs != MagickFalse) |
| 10759 | { |
| 10760 | png_set_pHYs(ping,ping_info, |
| 10761 | ping_pHYs_x_resolution, |
| 10762 | ping_pHYs_y_resolution, |
| 10763 | ping_pHYs_unit_type); |
glennrp | 823b55c | 2011-03-14 18:46:46 +0000 | [diff] [blame] | 10764 | |
| 10765 | if (logging) |
| 10766 | { |
| 10767 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10768 | " Setting up pHYs chunk"); |
| 10769 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10770 | " x_resolution=%lu", |
| 10771 | (unsigned long) ping_pHYs_x_resolution); |
| 10772 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10773 | " y_resolution=%lu", |
| 10774 | (unsigned long) ping_pHYs_y_resolution); |
| 10775 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10776 | " unit_type=%lu", |
| 10777 | (unsigned long) ping_pHYs_unit_type); |
| 10778 | } |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10779 | } |
glennrp | dfd7080 | 2010-11-14 01:23:35 +0000 | [diff] [blame] | 10780 | } |
| 10781 | |
| 10782 | #if defined(PNG_oFFs_SUPPORTED) |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 10783 | if (ping_exclude_oFFs == MagickFalse) |
glennrp | dfd7080 | 2010-11-14 01:23:35 +0000 | [diff] [blame] | 10784 | { |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10785 | if (image->page.x || image->page.y) |
| 10786 | { |
| 10787 | png_set_oFFs(ping,ping_info,(png_int_32) image->page.x, |
| 10788 | (png_int_32) image->page.y, 0); |
glennrp | dfd7080 | 2010-11-14 01:23:35 +0000 | [diff] [blame] | 10789 | |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10790 | if (logging != MagickFalse) |
| 10791 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10792 | " Setting up oFFs chunk with x=%d, y=%d, units=0", |
| 10793 | (int) image->page.x, (int) image->page.y); |
| 10794 | } |
glennrp | dfd7080 | 2010-11-14 01:23:35 +0000 | [diff] [blame] | 10795 | } |
| 10796 | #endif |
| 10797 | |
glennrp | da8f3a7 | 2011-02-27 23:54:12 +0000 | [diff] [blame] | 10798 | if (mng_info->need_blob != MagickFalse) |
| 10799 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 10800 | if (OpenBlob(image_info,image,WriteBinaryBlobMode,exception) == |
glennrp | da8f3a7 | 2011-02-27 23:54:12 +0000 | [diff] [blame] | 10801 | MagickFalse) |
| 10802 | png_error(ping,"WriteBlob Failed"); |
| 10803 | |
| 10804 | ping_have_blob=MagickTrue; |
| 10805 | } |
| 10806 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10807 | png_write_info_before_PLTE(ping, ping_info); |
glennrp | 991d11d | 2010-11-12 21:55:28 +0000 | [diff] [blame] | 10808 | |
glennrp | 39992b4 | 2010-11-14 00:03:43 +0000 | [diff] [blame] | 10809 | if (ping_have_tRNS != MagickFalse && ping_color_type < 4) |
glennrp | 991d11d | 2010-11-12 21:55:28 +0000 | [diff] [blame] | 10810 | { |
glennrp | 3b51f0e | 2010-11-27 18:14:08 +0000 | [diff] [blame] | 10811 | if (logging != MagickFalse) |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10812 | { |
| 10813 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10814 | " Calling png_set_tRNS with num_trans=%d",ping_num_trans); |
| 10815 | } |
| 10816 | |
| 10817 | if (ping_color_type == 3) |
| 10818 | (void) png_set_tRNS(ping, ping_info, |
| 10819 | ping_trans_alpha, |
| 10820 | ping_num_trans, |
| 10821 | NULL); |
| 10822 | |
| 10823 | else |
| 10824 | { |
| 10825 | (void) png_set_tRNS(ping, ping_info, |
| 10826 | NULL, |
| 10827 | 0, |
| 10828 | &ping_trans_color); |
| 10829 | |
glennrp | 3b51f0e | 2010-11-27 18:14:08 +0000 | [diff] [blame] | 10830 | if (logging != MagickFalse) |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10831 | { |
| 10832 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 10833 | " tRNS color =(%d,%d,%d)", |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10834 | (int) ping_trans_color.red, |
| 10835 | (int) ping_trans_color.green, |
| 10836 | (int) ping_trans_color.blue); |
| 10837 | } |
| 10838 | } |
glennrp | 991d11d | 2010-11-12 21:55:28 +0000 | [diff] [blame] | 10839 | } |
| 10840 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10841 | /* write any png-chunk-b profiles */ |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 10842 | (void) Magick_png_write_chunk_from_profile(image,"PNG-chunk-b",logging); |
glennrp | da8f3a7 | 2011-02-27 23:54:12 +0000 | [diff] [blame] | 10843 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10844 | png_write_info(ping,ping_info); |
glennrp | 991d11d | 2010-11-12 21:55:28 +0000 | [diff] [blame] | 10845 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10846 | /* write any PNG-chunk-m profiles */ |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 10847 | (void) Magick_png_write_chunk_from_profile(image,"PNG-chunk-m",logging); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10848 | |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10849 | if (ping_exclude_vpAg == MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10850 | { |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 10851 | if ((image->page.width != 0 && image->page.width != image->columns) || |
| 10852 | (image->page.height != 0 && image->page.height != image->rows)) |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10853 | { |
| 10854 | unsigned char |
| 10855 | chunk[14]; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10856 | |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10857 | (void) WriteBlobMSBULong(image,9L); /* data length=8 */ |
| 10858 | PNGType(chunk,mng_vpAg); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 10859 | LogPNGChunk(logging,mng_vpAg,9L); |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10860 | PNGLong(chunk+4,(png_uint_32) image->page.width); |
| 10861 | PNGLong(chunk+8,(png_uint_32) image->page.height); |
| 10862 | chunk[12]=0; /* unit = pixels */ |
| 10863 | (void) WriteBlob(image,13,chunk); |
| 10864 | (void) WriteBlobMSBULong(image,crc32(0,chunk,13)); |
| 10865 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10866 | } |
| 10867 | |
| 10868 | #if (PNG_LIBPNG_VER == 10206) |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 10869 | /* avoid libpng-1.2.6 bug by setting PNG_HAVE_IDAT flag */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10870 | #define PNG_HAVE_IDAT 0x04 |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 10871 | ping->mode |= PNG_HAVE_IDAT; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10872 | #undef PNG_HAVE_IDAT |
| 10873 | #endif |
| 10874 | |
| 10875 | png_set_packing(ping); |
| 10876 | /* |
| 10877 | Allocate memory. |
| 10878 | */ |
| 10879 | rowbytes=image->columns; |
glennrp | b4a1341 | 2010-05-05 12:47:19 +0000 | [diff] [blame] | 10880 | if (image_depth > 8) |
| 10881 | rowbytes*=2; |
cristy | 7202c10 | 2010-05-05 19:18:28 +0000 | [diff] [blame] | 10882 | switch (ping_color_type) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10883 | { |
glennrp | b4a1341 | 2010-05-05 12:47:19 +0000 | [diff] [blame] | 10884 | case PNG_COLOR_TYPE_RGB: |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10885 | rowbytes*=3; |
glennrp | b4a1341 | 2010-05-05 12:47:19 +0000 | [diff] [blame] | 10886 | break; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10887 | |
glennrp | b4a1341 | 2010-05-05 12:47:19 +0000 | [diff] [blame] | 10888 | case PNG_COLOR_TYPE_GRAY_ALPHA: |
| 10889 | rowbytes*=2; |
| 10890 | break; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10891 | |
glennrp | b4a1341 | 2010-05-05 12:47:19 +0000 | [diff] [blame] | 10892 | case PNG_COLOR_TYPE_RGBA: |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10893 | rowbytes*=4; |
glennrp | b4a1341 | 2010-05-05 12:47:19 +0000 | [diff] [blame] | 10894 | break; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10895 | |
glennrp | b4a1341 | 2010-05-05 12:47:19 +0000 | [diff] [blame] | 10896 | default: |
| 10897 | break; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10898 | } |
glennrp | 3b51f0e | 2010-11-27 18:14:08 +0000 | [diff] [blame] | 10899 | |
| 10900 | if (logging != MagickFalse) |
glennrp | b4a1341 | 2010-05-05 12:47:19 +0000 | [diff] [blame] | 10901 | { |
| 10902 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10903 | " Writing PNG image data"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10904 | |
glennrp | b4a1341 | 2010-05-05 12:47:19 +0000 | [diff] [blame] | 10905 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 10906 | " Allocating %.20g bytes of memory for pixels",(double) rowbytes); |
glennrp | b4a1341 | 2010-05-05 12:47:19 +0000 | [diff] [blame] | 10907 | } |
cristy | 0997332 | 2013-06-30 21:06:30 +0000 | [diff] [blame] | 10908 | pixel_info=AcquireVirtualMemory(rowbytes,sizeof(*ping_pixels)); |
| 10909 | if (pixel_info == (MemoryInfo *) NULL) |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 10910 | png_error(ping,"Allocation of memory for pixels failed"); |
cristy | 0997332 | 2013-06-30 21:06:30 +0000 | [diff] [blame] | 10911 | ping_pixels=(unsigned char *) GetVirtualMemoryBlob(pixel_info); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10912 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10913 | /* |
| 10914 | Initialize image scanlines. |
| 10915 | */ |
cristy | ed55252 | 2009-10-16 14:04:35 +0000 | [diff] [blame] | 10916 | quantum_info=AcquireQuantumInfo(image_info,image); |
| 10917 | if (quantum_info == (QuantumInfo *) NULL) |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 10918 | png_error(ping,"Memory allocation for quantum_info failed"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10919 | quantum_info->format=UndefinedQuantumFormat; |
| 10920 | quantum_info->depth=image_depth; |
glennrp | 4b840d7 | 2012-11-22 16:01:16 +0000 | [diff] [blame] | 10921 | (void) SetQuantumEndian(image,quantum_info,MSBEndian); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10922 | num_passes=png_set_interlace_handling(ping); |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 10923 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10924 | if ((!mng_info->write_png8 && !mng_info->write_png24 && |
glennrp | fd164d2 | 2013-01-26 21:10:22 +0000 | [diff] [blame] | 10925 | !mng_info->write_png48 && !mng_info->write_png64 && |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 10926 | !mng_info->write_png32) && |
| 10927 | (mng_info->IsPalette || |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10928 | (image_info->type == BilevelType)) && |
glennrp | 8d57966 | 2011-02-23 02:05:02 +0000 | [diff] [blame] | 10929 | image_matte == MagickFalse && |
| 10930 | ping_have_non_bw == MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10931 | { |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 10932 | /* Palette, Bilevel, or Opaque Monochrome */ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 10933 | register const Quantum |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10934 | *p; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10935 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10936 | quantum_info->depth=8; |
| 10937 | for (pass=0; pass < num_passes; pass++) |
| 10938 | { |
| 10939 | /* |
| 10940 | Convert PseudoClass image to a PNG monochrome image. |
| 10941 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 10942 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10943 | { |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 10944 | if (logging != MagickFalse && y == 0) |
glennrp | 3b51f0e | 2010-11-27 18:14:08 +0000 | [diff] [blame] | 10945 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10946 | " Writing row of pixels (0)"); |
glennrp | a521b2f | 2010-10-29 04:11:03 +0000 | [diff] [blame] | 10947 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 10948 | p=GetVirtualPixels(image,0,y,image->columns,1,exception); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10949 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 10950 | if (p == (const Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10951 | break; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10952 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10953 | if (mng_info->IsPalette) |
| 10954 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 10955 | (void) ExportQuantumPixels(image,(CacheView *) NULL, |
| 10956 | quantum_info,GrayQuantum,ping_pixels,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10957 | if (mng_info->write_png_colortype-1 == PNG_COLOR_TYPE_PALETTE && |
| 10958 | mng_info->write_png_depth && |
| 10959 | mng_info->write_png_depth != old_bit_depth) |
| 10960 | { |
| 10961 | /* Undo pixel scaling */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 10962 | for (i=0; i < (ssize_t) image->columns; i++) |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 10963 | *(ping_pixels+i)=(unsigned char) (*(ping_pixels+i) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10964 | >> (8-old_bit_depth)); |
| 10965 | } |
| 10966 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10967 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10968 | else |
| 10969 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 10970 | (void) ExportQuantumPixels(image,(CacheView *) NULL, |
| 10971 | quantum_info,RedQuantum,ping_pixels,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10972 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10973 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10974 | if (mng_info->write_png_colortype-1 != PNG_COLOR_TYPE_PALETTE) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 10975 | for (i=0; i < (ssize_t) image->columns; i++) |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 10976 | *(ping_pixels+i)=(unsigned char) ((*(ping_pixels+i) > 127) ? |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10977 | 255 : 0); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10978 | |
glennrp | 3b51f0e | 2010-11-27 18:14:08 +0000 | [diff] [blame] | 10979 | if (logging != MagickFalse && y == 0) |
glennrp | b4a1341 | 2010-05-05 12:47:19 +0000 | [diff] [blame] | 10980 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10981 | " Writing row of pixels (1)"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10982 | |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 10983 | png_write_row(ping,ping_pixels); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10984 | } |
| 10985 | if (image->previous == (Image *) NULL) |
| 10986 | { |
| 10987 | status=SetImageProgress(image,LoadImageTag,pass,num_passes); |
| 10988 | if (status == MagickFalse) |
| 10989 | break; |
| 10990 | } |
| 10991 | } |
| 10992 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10993 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 10994 | else /* Not Palette, Bilevel, or Opaque Monochrome */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10995 | { |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10996 | if ((!mng_info->write_png8 && !mng_info->write_png24 && |
glennrp | fd164d2 | 2013-01-26 21:10:22 +0000 | [diff] [blame] | 10997 | !mng_info->write_png48 && !mng_info->write_png64 && |
| 10998 | !mng_info->write_png32) && (image_matte != MagickFalse || |
| 10999 | (ping_bit_depth >= MAGICKCORE_QUANTUM_DEPTH)) && |
| 11000 | (mng_info->IsPalette) && ping_have_color == MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11001 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 11002 | register const Quantum |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 11003 | *p; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11004 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 11005 | for (pass=0; pass < num_passes; pass++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11006 | { |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 11007 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 11008 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11009 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 11010 | p=GetVirtualPixels(image,0,y,image->columns,1,exception); |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11011 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 11012 | if (p == (const Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11013 | break; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11014 | |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 11015 | if (ping_color_type == PNG_COLOR_TYPE_GRAY) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11016 | { |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 11017 | if (mng_info->IsPalette) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 11018 | (void) ExportQuantumPixels(image,(CacheView *) NULL, |
| 11019 | quantum_info,GrayQuantum,ping_pixels,exception); |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11020 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 11021 | else |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 11022 | (void) ExportQuantumPixels(image,(CacheView *) NULL, |
| 11023 | quantum_info,RedQuantum,ping_pixels,exception); |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11024 | |
glennrp | 3b51f0e | 2010-11-27 18:14:08 +0000 | [diff] [blame] | 11025 | if (logging != MagickFalse && y == 0) |
glennrp | b4a1341 | 2010-05-05 12:47:19 +0000 | [diff] [blame] | 11026 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 11027 | " Writing GRAY PNG pixels (2)"); |
glennrp | b4a1341 | 2010-05-05 12:47:19 +0000 | [diff] [blame] | 11028 | } |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11029 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 11030 | else /* PNG_COLOR_TYPE_GRAY_ALPHA */ |
| 11031 | { |
| 11032 | if (logging != MagickFalse && y == 0) |
| 11033 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 11034 | " Writing GRAY_ALPHA PNG pixels (2)"); |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11035 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 11036 | (void) ExportQuantumPixels(image,(CacheView *) NULL, |
| 11037 | quantum_info,GrayAlphaQuantum,ping_pixels,exception); |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 11038 | } |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11039 | |
glennrp | 3b51f0e | 2010-11-27 18:14:08 +0000 | [diff] [blame] | 11040 | if (logging != MagickFalse && y == 0) |
glennrp | b4a1341 | 2010-05-05 12:47:19 +0000 | [diff] [blame] | 11041 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 11042 | " Writing row of pixels (2)"); |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11043 | |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 11044 | png_write_row(ping,ping_pixels); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11045 | } |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11046 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 11047 | if (image->previous == (Image *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11048 | { |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 11049 | status=SetImageProgress(image,LoadImageTag,pass,num_passes); |
| 11050 | if (status == MagickFalse) |
| 11051 | break; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11052 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11053 | } |
| 11054 | } |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 11055 | |
| 11056 | else |
| 11057 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 11058 | register const Quantum |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 11059 | *p; |
| 11060 | |
| 11061 | for (pass=0; pass < num_passes; pass++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11062 | { |
glennrp | fd164d2 | 2013-01-26 21:10:22 +0000 | [diff] [blame] | 11063 | if ((image_depth > 8) || |
| 11064 | mng_info->write_png24 || |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 11065 | mng_info->write_png32 || |
glennrp | fd164d2 | 2013-01-26 21:10:22 +0000 | [diff] [blame] | 11066 | mng_info->write_png48 || |
| 11067 | mng_info->write_png64 || |
| 11068 | (!mng_info->write_png8 && !mng_info->IsPalette)) |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 11069 | { |
| 11070 | for (y=0; y < (ssize_t) image->rows; y++) |
| 11071 | { |
cristy | 862a33c | 2012-05-17 22:49:37 +0000 | [diff] [blame] | 11072 | p=GetVirtualPixels(image,0,y,image->columns,1, exception); |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11073 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 11074 | if (p == (const Quantum *) NULL) |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 11075 | break; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11076 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 11077 | if (ping_color_type == PNG_COLOR_TYPE_GRAY) |
| 11078 | { |
| 11079 | if (image->storage_class == DirectClass) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 11080 | (void) ExportQuantumPixels(image,(CacheView *) NULL, |
| 11081 | quantum_info,RedQuantum,ping_pixels,exception); |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11082 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 11083 | else |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 11084 | (void) ExportQuantumPixels(image,(CacheView *) NULL, |
| 11085 | quantum_info,GrayQuantum,ping_pixels,exception); |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 11086 | } |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11087 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 11088 | else if (ping_color_type == PNG_COLOR_TYPE_GRAY_ALPHA) |
| 11089 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 11090 | (void) ExportQuantumPixels(image,(CacheView *) NULL, |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 11091 | quantum_info,GrayAlphaQuantum,ping_pixels, |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 11092 | exception); |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11093 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 11094 | if (logging != MagickFalse && y == 0) |
| 11095 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 11096 | " Writing GRAY_ALPHA PNG pixels (3)"); |
| 11097 | } |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11098 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 11099 | else if (image_matte != MagickFalse) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 11100 | (void) ExportQuantumPixels(image,(CacheView *) NULL, |
| 11101 | quantum_info,RGBAQuantum,ping_pixels,exception); |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11102 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 11103 | else |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 11104 | (void) ExportQuantumPixels(image,(CacheView *) NULL, |
| 11105 | quantum_info,RGBQuantum,ping_pixels,exception); |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11106 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 11107 | if (logging != MagickFalse && y == 0) |
| 11108 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 11109 | " Writing row of pixels (3)"); |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11110 | |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 11111 | png_write_row(ping,ping_pixels); |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 11112 | } |
| 11113 | } |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11114 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 11115 | else |
glennrp | fd164d2 | 2013-01-26 21:10:22 +0000 | [diff] [blame] | 11116 | /* not ((image_depth > 8) || |
| 11117 | mng_info->write_png24 || mng_info->write_png32 || |
| 11118 | mng_info->write_png48 || mng_info->write_png64 || |
| 11119 | (!mng_info->write_png8 && !mng_info->IsPalette)) |
| 11120 | */ |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 11121 | { |
| 11122 | if ((ping_color_type != PNG_COLOR_TYPE_GRAY) && |
| 11123 | (ping_color_type != PNG_COLOR_TYPE_GRAY_ALPHA)) |
| 11124 | { |
| 11125 | if (logging != MagickFalse) |
| 11126 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 11127 | " pass %d, Image Is not GRAY or GRAY_ALPHA",pass); |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11128 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 11129 | quantum_info->depth=8; |
| 11130 | image_depth=8; |
| 11131 | } |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11132 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 11133 | for (y=0; y < (ssize_t) image->rows; y++) |
| 11134 | { |
| 11135 | if (logging != MagickFalse && y == 0) |
| 11136 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 11137 | " pass %d, Image Is RGB, 16-bit GRAY, or GRAY_ALPHA",pass); |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11138 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 11139 | p=GetVirtualPixels(image,0,y,image->columns,1, exception); |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11140 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 11141 | if (p == (const Quantum *) NULL) |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 11142 | break; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11143 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 11144 | if (ping_color_type == PNG_COLOR_TYPE_GRAY) |
glennrp | 44757ab | 2011-03-17 12:57:03 +0000 | [diff] [blame] | 11145 | { |
glennrp | 4bf8973 | 2011-03-21 13:48:28 +0000 | [diff] [blame] | 11146 | quantum_info->depth=image->depth; |
| 11147 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 11148 | (void) ExportQuantumPixels(image,(CacheView *) NULL, |
| 11149 | quantum_info,GrayQuantum,ping_pixels,exception); |
glennrp | 44757ab | 2011-03-17 12:57:03 +0000 | [diff] [blame] | 11150 | } |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11151 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 11152 | else if (ping_color_type == PNG_COLOR_TYPE_GRAY_ALPHA) |
| 11153 | { |
| 11154 | if (logging != MagickFalse && y == 0) |
| 11155 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 11156 | " Writing GRAY_ALPHA PNG pixels (4)"); |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11157 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 11158 | (void) ExportQuantumPixels(image,(CacheView *) NULL, |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 11159 | quantum_info,GrayAlphaQuantum,ping_pixels, |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 11160 | exception); |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 11161 | } |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11162 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 11163 | else |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 11164 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 11165 | (void) ExportQuantumPixels(image,(CacheView *) NULL, |
| 11166 | quantum_info,IndexQuantum,ping_pixels,exception); |
glennrp | 5eae760 | 2011-02-22 15:21:32 +0000 | [diff] [blame] | 11167 | |
| 11168 | if (logging != MagickFalse && y <= 2) |
| 11169 | { |
| 11170 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | 1a7d6db | 2011-03-17 13:24:10 +0000 | [diff] [blame] | 11171 | " Writing row of non-gray pixels (4)"); |
glennrp | 5eae760 | 2011-02-22 15:21:32 +0000 | [diff] [blame] | 11172 | |
| 11173 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 11174 | " ping_pixels[0]=%d,ping_pixels[1]=%d", |
| 11175 | (int)ping_pixels[0],(int)ping_pixels[1]); |
| 11176 | } |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 11177 | } |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 11178 | png_write_row(ping,ping_pixels); |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 11179 | } |
| 11180 | } |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11181 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 11182 | if (image->previous == (Image *) NULL) |
| 11183 | { |
| 11184 | status=SetImageProgress(image,LoadImageTag,pass,num_passes); |
| 11185 | if (status == MagickFalse) |
| 11186 | break; |
| 11187 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11188 | } |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 11189 | } |
| 11190 | } |
| 11191 | |
cristy | b32b90a | 2009-09-07 21:45:48 +0000 | [diff] [blame] | 11192 | if (quantum_info != (QuantumInfo *) NULL) |
| 11193 | quantum_info=DestroyQuantumInfo(quantum_info); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11194 | |
| 11195 | if (logging != MagickFalse) |
| 11196 | { |
| 11197 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | b4a1341 | 2010-05-05 12:47:19 +0000 | [diff] [blame] | 11198 | " Wrote PNG image data"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11199 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11200 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 11201 | " Width: %.20g",(double) ping_width); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11202 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11203 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 11204 | " Height: %.20g",(double) ping_height); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11205 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11206 | if (mng_info->write_png_depth) |
| 11207 | { |
| 11208 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | 5d6fc9c | 2011-12-27 03:10:42 +0000 | [diff] [blame] | 11209 | " Defined png:bit-depth: %d",mng_info->write_png_depth); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11210 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11211 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11212 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 11213 | " PNG bit-depth written: %d",ping_bit_depth); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11214 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11215 | if (mng_info->write_png_colortype) |
| 11216 | { |
| 11217 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | 5d6fc9c | 2011-12-27 03:10:42 +0000 | [diff] [blame] | 11218 | " Defined png:color-type: %d",mng_info->write_png_colortype-1); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11219 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11220 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11221 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 11222 | " PNG color-type written: %d",ping_color_type); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11223 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11224 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 11225 | " PNG Interlace method: %d",ping_interlace_method); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11226 | } |
| 11227 | /* |
glennrp | a0ed009 | 2011-04-18 16:36:29 +0000 | [diff] [blame] | 11228 | Generate text chunks after IDAT. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11229 | */ |
glennrp | 823b55c | 2011-03-14 18:46:46 +0000 | [diff] [blame] | 11230 | if (ping_exclude_tEXt == MagickFalse || ping_exclude_zTXt == MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11231 | { |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 11232 | ResetImagePropertyIterator(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11233 | property=GetNextImageProperty(image); |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 11234 | while (property != (const char *) NULL) |
| 11235 | { |
| 11236 | png_textp |
| 11237 | text; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11238 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 11239 | value=GetImageProperty(image,property,exception); |
glennrp | a0ed009 | 2011-04-18 16:36:29 +0000 | [diff] [blame] | 11240 | |
glennrp | e4d5faf | 2013-07-22 00:11:04 +0000 | [diff] [blame] | 11241 | /* Don't write any "png:" or "jpeg:" properties; those are just for |
| 11242 | * "identify" or for passing through to another JPEG |
| 11243 | */ |
| 11244 | if ((LocaleNCompare(property,"png:",4) != 0 && |
| 11245 | LocaleNCompare(property,"jpeg:",5)) && |
| 11246 | |
glennrp | a0ed009 | 2011-04-18 16:36:29 +0000 | [diff] [blame] | 11247 | |
| 11248 | /* Suppress density and units if we wrote a pHYs chunk */ |
| 11249 | (ping_exclude_pHYs != MagickFalse || |
glennrp | 823b55c | 2011-03-14 18:46:46 +0000 | [diff] [blame] | 11250 | LocaleCompare(property,"density") != 0 || |
glennrp | a0ed009 | 2011-04-18 16:36:29 +0000 | [diff] [blame] | 11251 | LocaleCompare(property,"units") != 0) && |
| 11252 | |
| 11253 | /* Suppress the IM-generated Date:create and Date:modify */ |
| 11254 | (ping_exclude_date == MagickFalse || |
| 11255 | LocaleNCompare(property, "Date:",5) != 0)) |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 11256 | { |
glennrp | c70af4a | 2011-03-07 00:08:23 +0000 | [diff] [blame] | 11257 | if (value != (const char *) NULL) |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 11258 | { |
cristy | a865ccd | 2012-07-28 00:33:10 +0000 | [diff] [blame] | 11259 | |
glennrp | ecab7d7 | 2013-05-14 22:50:32 +0000 | [diff] [blame] | 11260 | #if PNG_LIBPNG_VER >= 10400 |
cristy | a865ccd | 2012-07-28 00:33:10 +0000 | [diff] [blame] | 11261 | text=(png_textp) png_malloc(ping, |
| 11262 | (png_alloc_size_t) sizeof(png_text)); |
| 11263 | #else |
| 11264 | text=(png_textp) png_malloc(ping,(png_size_t) sizeof(png_text)); |
| 11265 | #endif |
glennrp | c70af4a | 2011-03-07 00:08:23 +0000 | [diff] [blame] | 11266 | text[0].key=(char *) property; |
| 11267 | text[0].text=(char *) value; |
| 11268 | text[0].text_length=strlen(value); |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11269 | |
glennrp | c70af4a | 2011-03-07 00:08:23 +0000 | [diff] [blame] | 11270 | if (ping_exclude_tEXt != MagickFalse) |
| 11271 | text[0].compression=PNG_TEXT_COMPRESSION_zTXt; |
| 11272 | |
| 11273 | else if (ping_exclude_zTXt != MagickFalse) |
| 11274 | text[0].compression=PNG_TEXT_COMPRESSION_NONE; |
| 11275 | |
| 11276 | else |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 11277 | { |
glennrp | c70af4a | 2011-03-07 00:08:23 +0000 | [diff] [blame] | 11278 | text[0].compression=image_info->compression == NoCompression || |
| 11279 | (image_info->compression == UndefinedCompression && |
| 11280 | text[0].text_length < 128) ? PNG_TEXT_COMPRESSION_NONE : |
| 11281 | PNG_TEXT_COMPRESSION_zTXt ; |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 11282 | } |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11283 | |
glennrp | c70af4a | 2011-03-07 00:08:23 +0000 | [diff] [blame] | 11284 | if (logging != MagickFalse) |
| 11285 | { |
| 11286 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 11287 | " Setting up text chunk"); |
| 11288 | |
| 11289 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | cbc9215 | 2013-02-04 15:46:22 +0000 | [diff] [blame] | 11290 | " keyword: '%s'",text[0].key); |
glennrp | c70af4a | 2011-03-07 00:08:23 +0000 | [diff] [blame] | 11291 | } |
| 11292 | |
| 11293 | png_set_text(ping,ping_info,text,1); |
| 11294 | png_free(ping,text); |
| 11295 | } |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 11296 | } |
| 11297 | property=GetNextImageProperty(image); |
| 11298 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11299 | } |
| 11300 | |
| 11301 | /* write any PNG-chunk-e profiles */ |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 11302 | (void) Magick_png_write_chunk_from_profile(image,"PNG-chunk-e",logging); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11303 | |
| 11304 | if (logging != MagickFalse) |
| 11305 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 11306 | " Writing PNG end info"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11307 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11308 | png_write_end(ping,ping_info); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11309 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11310 | if (mng_info->need_fram && (int) image->dispose == BackgroundDispose) |
| 11311 | { |
| 11312 | if (mng_info->page.x || mng_info->page.y || |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 11313 | (ping_width != mng_info->page.width) || |
| 11314 | (ping_height != mng_info->page.height)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11315 | { |
| 11316 | unsigned char |
| 11317 | chunk[32]; |
| 11318 | |
| 11319 | /* |
| 11320 | Write FRAM 4 with clipping boundaries followed by FRAM 1. |
| 11321 | */ |
| 11322 | (void) WriteBlobMSBULong(image,27L); /* data length=27 */ |
| 11323 | PNGType(chunk,mng_FRAM); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11324 | LogPNGChunk(logging,mng_FRAM,27L); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11325 | chunk[4]=4; |
| 11326 | chunk[5]=0; /* frame name separator (no name) */ |
| 11327 | chunk[6]=1; /* flag for changing delay, for next frame only */ |
| 11328 | chunk[7]=0; /* flag for changing frame timeout */ |
| 11329 | chunk[8]=1; /* flag for changing frame clipping for next frame */ |
| 11330 | chunk[9]=0; /* flag for changing frame sync_id */ |
| 11331 | PNGLong(chunk+10,(png_uint_32) (0L)); /* temporary 0 delay */ |
| 11332 | chunk[14]=0; /* clipping boundaries delta type */ |
| 11333 | PNGLong(chunk+15,(png_uint_32) (mng_info->page.x)); /* left cb */ |
| 11334 | PNGLong(chunk+19, |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 11335 | (png_uint_32) (mng_info->page.x + ping_width)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11336 | PNGLong(chunk+23,(png_uint_32) (mng_info->page.y)); /* top cb */ |
| 11337 | PNGLong(chunk+27, |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 11338 | (png_uint_32) (mng_info->page.y + ping_height)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11339 | (void) WriteBlob(image,31,chunk); |
| 11340 | (void) WriteBlobMSBULong(image,crc32(0,chunk,31)); |
| 11341 | mng_info->old_framing_mode=4; |
| 11342 | mng_info->framing_mode=1; |
| 11343 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11344 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11345 | else |
| 11346 | mng_info->framing_mode=3; |
| 11347 | } |
| 11348 | if (mng_info->write_mng && !mng_info->need_fram && |
| 11349 | ((int) image->dispose == 3)) |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 11350 | png_error(ping, "Cannot convert GIF with disposal method 3 to MNG-LC"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11351 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11352 | /* |
| 11353 | Free PNG resources. |
| 11354 | */ |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 11355 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11356 | png_destroy_write_struct(&ping,&ping_info); |
| 11357 | |
cristy | 0997332 | 2013-06-30 21:06:30 +0000 | [diff] [blame] | 11358 | pixel_info=RelinquishVirtualMemory(pixel_info); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11359 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 11360 | if (ping_have_blob != MagickFalse) |
| 11361 | (void) CloseBlob(image); |
| 11362 | |
| 11363 | image_info=DestroyImageInfo(image_info); |
| 11364 | image=DestroyImage(image); |
| 11365 | |
glennrp | b9cfe27 | 2010-12-21 15:08:06 +0000 | [diff] [blame] | 11366 | /* Store bit depth actually written */ |
| 11367 | s[0]=(char) ping_bit_depth; |
| 11368 | s[1]='\0'; |
| 11369 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 11370 | (void) SetImageProperty(IMimage,"png:bit-depth-written",s,exception); |
glennrp | b9cfe27 | 2010-12-21 15:08:06 +0000 | [diff] [blame] | 11371 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11372 | if (logging != MagickFalse) |
| 11373 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 11374 | " exit WriteOnePNGImage()"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11375 | |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 11376 | #ifdef PNG_SETJMP_NOT_THREAD_SAFE |
| 11377 | UnlockSemaphoreInfo(ping_semaphore); |
| 11378 | #endif |
| 11379 | |
| 11380 | /* } for navigation to beginning of SETJMP-protected block. Revert to |
| 11381 | * Throwing an Exception when an error occurs. |
| 11382 | */ |
| 11383 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11384 | return(MagickTrue); |
| 11385 | /* End write one PNG image */ |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 11386 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11387 | } |
| 11388 | |
| 11389 | /* |
| 11390 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 11391 | % % |
| 11392 | % % |
| 11393 | % % |
| 11394 | % W r i t e P N G I m a g e % |
| 11395 | % % |
| 11396 | % % |
| 11397 | % % |
| 11398 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 11399 | % |
| 11400 | % WritePNGImage() writes a Portable Network Graphics (PNG) or |
| 11401 | % Multiple-image Network Graphics (MNG) image file. |
| 11402 | % |
| 11403 | % MNG support written by Glenn Randers-Pehrson, glennrp@image... |
| 11404 | % |
| 11405 | % The format of the WritePNGImage method is: |
| 11406 | % |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 11407 | % MagickBooleanType WritePNGImage(const ImageInfo *image_info, |
| 11408 | % Image *image,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11409 | % |
| 11410 | % A description of each parameter follows: |
| 11411 | % |
| 11412 | % o image_info: the image info. |
| 11413 | % |
| 11414 | % o image: The image. |
| 11415 | % |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 11416 | % o exception: return any errors or warnings in this structure. |
| 11417 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11418 | % Returns MagickTrue on success, MagickFalse on failure. |
| 11419 | % |
| 11420 | % Communicating with the PNG encoder: |
| 11421 | % |
| 11422 | % While the datastream written is always in PNG format and normally would |
| 11423 | % be given the "png" file extension, this method also writes the following |
cristy | 5d6fc9c | 2011-12-27 03:10:42 +0000 | [diff] [blame] | 11424 | % pseudo-formats which are subsets of png: |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11425 | % |
glennrp | 5a39f37 | 2011-02-25 04:52:16 +0000 | [diff] [blame] | 11426 | % o PNG8: An 8-bit indexed PNG datastream is written. If the image has |
| 11427 | % a depth greater than 8, the depth is reduced. If transparency |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11428 | % is present, the tRNS chunk must only have values 0 and 255 |
| 11429 | % (i.e., transparency is binary: fully opaque or fully |
glennrp | 5a39f37 | 2011-02-25 04:52:16 +0000 | [diff] [blame] | 11430 | % transparent). If other values are present they will be |
| 11431 | % 50%-thresholded to binary transparency. If more than 256 |
glennrp | e9637cb | 2011-03-24 16:34:37 +0000 | [diff] [blame] | 11432 | % colors are present, they will be quantized to the 4-4-4-1, |
glennrp | 130fc45 | 2011-08-20 03:43:18 +0000 | [diff] [blame] | 11433 | % 3-3-3-1, or 3-3-2-1 palette. The underlying RGB color |
| 11434 | % of any resulting fully-transparent pixels is changed to |
| 11435 | % the image's background color. |
glennrp | e9637cb | 2011-03-24 16:34:37 +0000 | [diff] [blame] | 11436 | % |
| 11437 | % If you want better quantization or dithering of the colors |
| 11438 | % or alpha than that, you need to do it before calling the |
glennrp | 5a39f37 | 2011-02-25 04:52:16 +0000 | [diff] [blame] | 11439 | % PNG encoder. The pixels contain 8-bit indices even if |
| 11440 | % they could be represented with 1, 2, or 4 bits. Grayscale |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11441 | % images will be written as indexed PNG files even though the |
glennrp | 5a39f37 | 2011-02-25 04:52:16 +0000 | [diff] [blame] | 11442 | % PNG grayscale type might be slightly more efficient. Please |
| 11443 | % note that writing to the PNG8 format may result in loss |
| 11444 | % of color and alpha data. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11445 | % |
| 11446 | % o PNG24: An 8-bit per sample RGB PNG datastream is written. The tRNS |
| 11447 | % chunk can be present to convey binary transparency by naming |
glennrp | 5a39f37 | 2011-02-25 04:52:16 +0000 | [diff] [blame] | 11448 | % one of the colors as transparent. The only loss incurred |
| 11449 | % is reduction of sample depth to 8. If the image has more |
| 11450 | % than one transparent color, has semitransparent pixels, or |
| 11451 | % has an opaque pixel with the same RGB components as the |
| 11452 | % transparent color, an image is not written. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11453 | % |
| 11454 | % o PNG32: An 8-bit per sample RGBA PNG is written. Partial |
| 11455 | % transparency is permitted, i.e., the alpha sample for |
| 11456 | % each pixel can have any value from 0 to 255. The alpha |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11457 | % channel is present even if the image is fully opaque. |
glennrp | 5a39f37 | 2011-02-25 04:52:16 +0000 | [diff] [blame] | 11458 | % The only loss in data is the reduction of the sample depth |
| 11459 | % to 8. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11460 | % |
glennrp | fd164d2 | 2013-01-26 21:10:22 +0000 | [diff] [blame] | 11461 | % o PNG48: A 16-bit per sample RGB PNG datastream is written. The tRNS |
| 11462 | % chunk can be present to convey binary transparency by naming |
| 11463 | % one of the colors as transparent. If the image has more |
| 11464 | % than one transparent color, has semitransparent pixels, or |
| 11465 | % has an opaque pixel with the same RGB components as the |
| 11466 | % transparent color, an image is not written. |
| 11467 | % |
| 11468 | % o PNG64: A 16-bit per sample RGBA PNG is written. Partial |
| 11469 | % transparency is permitted, i.e., the alpha sample for |
| 11470 | % each pixel can have any value from 0 to 65535. The alpha |
| 11471 | % channel is present even if the image is fully opaque. |
| 11472 | % |
glennrp | 5830fbc | 2013-01-27 06:11:45 +0000 | [diff] [blame] | 11473 | % o PNG00: A PNG that inherits its colortype and bit-depth from the input |
| 11474 | % image, if the input was a PNG, is written. If these values |
| 11475 | % cannot be found, then "PNG00" falls back to the regular "PNG" |
| 11476 | % format. |
| 11477 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11478 | % o -define: For more precise control of the PNG output, you can use the |
| 11479 | % Image options "png:bit-depth" and "png:color-type". These |
| 11480 | % can be set from the commandline with "-define" and also |
glennrp | bb8a733 | 2010-11-13 15:17:35 +0000 | [diff] [blame] | 11481 | % from the application programming interfaces. The options |
| 11482 | % are case-independent and are converted to lowercase before |
| 11483 | % being passed to this encoder. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11484 | % |
| 11485 | % png:color-type can be 0, 2, 3, 4, or 6. |
| 11486 | % |
| 11487 | % When png:color-type is 0 (Grayscale), png:bit-depth can |
| 11488 | % be 1, 2, 4, 8, or 16. |
| 11489 | % |
| 11490 | % When png:color-type is 2 (RGB), png:bit-depth can |
| 11491 | % be 8 or 16. |
| 11492 | % |
| 11493 | % When png:color-type is 3 (Indexed), png:bit-depth can |
| 11494 | % be 1, 2, 4, or 8. This refers to the number of bits |
| 11495 | % used to store the index. The color samples always have |
| 11496 | % bit-depth 8 in indexed PNG files. |
| 11497 | % |
| 11498 | % When png:color-type is 4 (Gray-Matte) or 6 (RGB-Matte), |
| 11499 | % png:bit-depth can be 8 or 16. |
| 11500 | % |
glennrp | fd164d2 | 2013-01-26 21:10:22 +0000 | [diff] [blame] | 11501 | % If the image cannot be written without loss with the |
| 11502 | % requested bit-depth and color-type, a PNG file will not |
| 11503 | % be written, a warning will be issued, and the encoder will |
| 11504 | % return MagickFalse. |
glennrp | 5a39f37 | 2011-02-25 04:52:16 +0000 | [diff] [blame] | 11505 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11506 | % Since image encoders should not be responsible for the "heavy lifting", |
| 11507 | % the user should make sure that ImageMagick has already reduced the |
| 11508 | % image depth and number of colors and limit transparency to binary |
glennrp | 5a39f37 | 2011-02-25 04:52:16 +0000 | [diff] [blame] | 11509 | % transparency prior to attempting to write the image with depth, color, |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 11510 | % or transparency limitations. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11511 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11512 | % Note that another definition, "png:bit-depth-written" exists, but it |
| 11513 | % is not intended for external use. It is only used internally by the |
| 11514 | % PNG encoder to inform the JNG encoder of the depth of the alpha channel. |
| 11515 | % |
| 11516 | % It is possible to request that the PNG encoder write previously-formatted |
| 11517 | % ancillary chunks in the output PNG file, using the "-profile" commandline |
| 11518 | % option as shown below or by setting the profile via a programming |
| 11519 | % interface: |
| 11520 | % |
| 11521 | % -profile PNG-chunk-x:<file> |
| 11522 | % |
| 11523 | % where x is a location flag and <file> is a file containing the chunk |
| 11524 | % name in the first 4 bytes, then a colon (":"), followed by the chunk data. |
glennrp | bb8a733 | 2010-11-13 15:17:35 +0000 | [diff] [blame] | 11525 | % This encoder will compute the chunk length and CRC, so those must not |
| 11526 | % be included in the file. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11527 | % |
| 11528 | % "x" can be "b" (before PLTE), "m" (middle, i.e., between PLTE and IDAT), |
| 11529 | % or "e" (end, i.e., after IDAT). If you want to write multiple chunks |
| 11530 | % of the same type, then add a short unique string after the "x" to prevent |
glennrp | bb8a733 | 2010-11-13 15:17:35 +0000 | [diff] [blame] | 11531 | % subsequent profiles from overwriting the preceding ones, e.g., |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11532 | % |
glennrp | bb8a733 | 2010-11-13 15:17:35 +0000 | [diff] [blame] | 11533 | % -profile PNG-chunk-b01:file01 -profile PNG-chunk-b02:file02 |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11534 | % |
glennrp | 3241bd0 | 2010-12-12 04:36:28 +0000 | [diff] [blame] | 11535 | % As of version 6.6.6 the following optimizations are always done: |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11536 | % |
glennrp | d6afd54 | 2010-11-19 01:53:05 +0000 | [diff] [blame] | 11537 | % o 32-bit depth is reduced to 16. |
| 11538 | % o 16-bit depth is reduced to 8 if all pixels contain samples whose |
| 11539 | % high byte and low byte are identical. |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11540 | % o Palette is sorted to remove unused entries and to put a |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 11541 | % transparent color first, if BUILD_PNG_PALETTE is defined. |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11542 | % o Opaque matte channel is removed when writing an indexed PNG. |
glennrp | d6afd54 | 2010-11-19 01:53:05 +0000 | [diff] [blame] | 11543 | % o Grayscale images are reduced to 1, 2, or 4 bit depth if |
| 11544 | % this can be done without loss and a larger bit depth N was not |
cristy | 5d6fc9c | 2011-12-27 03:10:42 +0000 | [diff] [blame] | 11545 | % requested via the "-define png:bit-depth=N" option. |
glennrp | d6afd54 | 2010-11-19 01:53:05 +0000 | [diff] [blame] | 11546 | % o If matte channel is present but only one transparent color is |
| 11547 | % present, RGB+tRNS is written instead of RGBA |
| 11548 | % o Opaque matte channel is removed (or added, if color-type 4 or 6 |
| 11549 | % was requested when converting an opaque image). |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11550 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11551 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 11552 | */ |
| 11553 | static MagickBooleanType WritePNGImage(const ImageInfo *image_info, |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 11554 | Image *image,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11555 | { |
| 11556 | MagickBooleanType |
glennrp | 21f0e62 | 2011-01-07 16:20:57 +0000 | [diff] [blame] | 11557 | excluding, |
| 11558 | logging, |
| 11559 | have_mng_structure, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11560 | status; |
| 11561 | |
| 11562 | MngInfo |
| 11563 | *mng_info; |
| 11564 | |
| 11565 | const char |
| 11566 | *value; |
| 11567 | |
| 11568 | int |
glennrp | 21f0e62 | 2011-01-07 16:20:57 +0000 | [diff] [blame] | 11569 | i, |
glennrp | 5c7cf4e | 2010-12-24 00:30:00 +0000 | [diff] [blame] | 11570 | source; |
| 11571 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11572 | /* |
| 11573 | Open image file. |
| 11574 | */ |
| 11575 | assert(image_info != (const ImageInfo *) NULL); |
| 11576 | assert(image_info->signature == MagickSignature); |
| 11577 | assert(image != (Image *) NULL); |
| 11578 | assert(image->signature == MagickSignature); |
| 11579 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 11580 | logging=LogMagickEvent(CoderEvent,GetMagickModule(),"Enter WritePNGImage()"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11581 | /* |
| 11582 | Allocate a MngInfo structure. |
| 11583 | */ |
| 11584 | have_mng_structure=MagickFalse; |
cristy | 73bd4a5 | 2010-10-05 11:24:23 +0000 | [diff] [blame] | 11585 | mng_info=(MngInfo *) AcquireMagickMemory(sizeof(MngInfo)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11586 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11587 | if (mng_info == (MngInfo *) NULL) |
| 11588 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11589 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11590 | /* |
| 11591 | Initialize members of the MngInfo structure. |
| 11592 | */ |
| 11593 | (void) ResetMagickMemory(mng_info,0,sizeof(MngInfo)); |
| 11594 | mng_info->image=image; |
glennrp | a521b2f | 2010-10-29 04:11:03 +0000 | [diff] [blame] | 11595 | mng_info->equal_backgrounds=MagickTrue; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11596 | have_mng_structure=MagickTrue; |
| 11597 | |
| 11598 | /* See if user has requested a specific PNG subformat */ |
| 11599 | |
| 11600 | mng_info->write_png8=LocaleCompare(image_info->magick,"PNG8") == 0; |
| 11601 | mng_info->write_png24=LocaleCompare(image_info->magick,"PNG24") == 0; |
| 11602 | mng_info->write_png32=LocaleCompare(image_info->magick,"PNG32") == 0; |
glennrp | fd164d2 | 2013-01-26 21:10:22 +0000 | [diff] [blame] | 11603 | mng_info->write_png48=LocaleCompare(image_info->magick,"PNG48") == 0; |
| 11604 | mng_info->write_png64=LocaleCompare(image_info->magick,"PNG64") == 0; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11605 | |
cristy | 092ec8d | 2013-04-26 13:46:22 +0000 | [diff] [blame] | 11606 | value=GetImageOption(image_info,"png:format"); |
glennrp | b381a26 | 2012-02-11 17:49:49 +0000 | [diff] [blame] | 11607 | |
| 11608 | if (value != (char *) NULL) |
| 11609 | { |
glennrp | f70c4d2 | 2013-03-19 15:26:48 +0000 | [diff] [blame] | 11610 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 11611 | " Format=%s",value); |
| 11612 | |
glennrp | fd164d2 | 2013-01-26 21:10:22 +0000 | [diff] [blame] | 11613 | mng_info->write_png8 = MagickFalse; |
| 11614 | mng_info->write_png24 = MagickFalse; |
| 11615 | mng_info->write_png32 = MagickFalse; |
| 11616 | mng_info->write_png48 = MagickFalse; |
| 11617 | mng_info->write_png64 = MagickFalse; |
| 11618 | |
glennrp | b381a26 | 2012-02-11 17:49:49 +0000 | [diff] [blame] | 11619 | if (LocaleCompare(value,"png8") == 0) |
glennrp | b381a26 | 2012-02-11 17:49:49 +0000 | [diff] [blame] | 11620 | mng_info->write_png8 = MagickTrue; |
glennrp | b381a26 | 2012-02-11 17:49:49 +0000 | [diff] [blame] | 11621 | |
| 11622 | else if (LocaleCompare(value,"png24") == 0) |
glennrp | b381a26 | 2012-02-11 17:49:49 +0000 | [diff] [blame] | 11623 | mng_info->write_png24 = MagickTrue; |
glennrp | b381a26 | 2012-02-11 17:49:49 +0000 | [diff] [blame] | 11624 | |
| 11625 | else if (LocaleCompare(value,"png32") == 0) |
glennrp | b381a26 | 2012-02-11 17:49:49 +0000 | [diff] [blame] | 11626 | mng_info->write_png32 = MagickTrue; |
glennrp | fd164d2 | 2013-01-26 21:10:22 +0000 | [diff] [blame] | 11627 | |
| 11628 | else if (LocaleCompare(value,"png48") == 0) |
| 11629 | mng_info->write_png48 = MagickTrue; |
| 11630 | |
| 11631 | else if (LocaleCompare(value,"png64") == 0) |
| 11632 | mng_info->write_png64 = MagickTrue; |
glennrp | 5830fbc | 2013-01-27 06:11:45 +0000 | [diff] [blame] | 11633 | |
glennrp | f70c4d2 | 2013-03-19 15:26:48 +0000 | [diff] [blame] | 11634 | else if (LocaleCompare(value,"png00") == 0) |
glennrp | 5830fbc | 2013-01-27 06:11:45 +0000 | [diff] [blame] | 11635 | { |
glennrp | 3398b5b | 2013-05-03 23:10:31 +0000 | [diff] [blame] | 11636 | /* Retrieve png:IHDR.bit-depth-orig and png:IHDR.color-type-orig. */ |
| 11637 | value=GetImageProperty(image,"png:IHDR.bit-depth-orig",exception); |
glennrp | f70c4d2 | 2013-03-19 15:26:48 +0000 | [diff] [blame] | 11638 | |
| 11639 | if (value != (char *) NULL) |
| 11640 | { |
| 11641 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 11642 | " png00 inherited bit depth=%s",value); |
| 11643 | |
| 11644 | if (LocaleCompare(value,"1") == 0) |
| 11645 | mng_info->write_png_depth = 1; |
| 11646 | |
| 11647 | else if (LocaleCompare(value,"1") == 0) |
| 11648 | mng_info->write_png_depth = 2; |
| 11649 | |
| 11650 | else if (LocaleCompare(value,"2") == 0) |
| 11651 | mng_info->write_png_depth = 4; |
| 11652 | |
| 11653 | else if (LocaleCompare(value,"8") == 0) |
| 11654 | mng_info->write_png_depth = 8; |
| 11655 | |
| 11656 | else if (LocaleCompare(value,"16") == 0) |
| 11657 | mng_info->write_png_depth = 16; |
| 11658 | } |
| 11659 | |
glennrp | 3398b5b | 2013-05-03 23:10:31 +0000 | [diff] [blame] | 11660 | value=GetImageProperty(image,"png:IHDR.color-type-orig",exception); |
glennrp | f70c4d2 | 2013-03-19 15:26:48 +0000 | [diff] [blame] | 11661 | |
| 11662 | if (value != (char *) NULL) |
| 11663 | { |
| 11664 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 11665 | " png00 inherited color type=%s",value); |
| 11666 | |
| 11667 | if (LocaleCompare(value,"0") == 0) |
| 11668 | mng_info->write_png_colortype = 1; |
| 11669 | |
| 11670 | else if (LocaleCompare(value,"2") == 0) |
| 11671 | mng_info->write_png_colortype = 3; |
| 11672 | |
| 11673 | else if (LocaleCompare(value,"3") == 0) |
| 11674 | mng_info->write_png_colortype = 4; |
| 11675 | |
| 11676 | else if (LocaleCompare(value,"4") == 0) |
| 11677 | mng_info->write_png_colortype = 5; |
| 11678 | |
| 11679 | else if (LocaleCompare(value,"6") == 0) |
| 11680 | mng_info->write_png_colortype = 7; |
| 11681 | } |
glennrp | 5830fbc | 2013-01-27 06:11:45 +0000 | [diff] [blame] | 11682 | } |
| 11683 | } |
| 11684 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11685 | if (mng_info->write_png8) |
| 11686 | { |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 11687 | mng_info->write_png_colortype = /* 3 */ 4; |
| 11688 | mng_info->write_png_depth = 8; |
| 11689 | image->depth = 8; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11690 | } |
| 11691 | |
| 11692 | if (mng_info->write_png24) |
| 11693 | { |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 11694 | mng_info->write_png_colortype = /* 2 */ 3; |
| 11695 | mng_info->write_png_depth = 8; |
| 11696 | image->depth = 8; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11697 | |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 11698 | if (image->alpha_trait == BlendPixelTrait) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 11699 | (void) SetImageType(image,TrueColorMatteType,exception); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11700 | |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 11701 | else |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 11702 | (void) SetImageType(image,TrueColorType,exception); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11703 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 11704 | (void) SyncImage(image,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11705 | } |
| 11706 | |
| 11707 | if (mng_info->write_png32) |
| 11708 | { |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 11709 | mng_info->write_png_colortype = /* 6 */ 7; |
| 11710 | mng_info->write_png_depth = 8; |
| 11711 | image->depth = 8; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11712 | |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 11713 | if (image->alpha_trait == BlendPixelTrait) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 11714 | (void) SetImageType(image,TrueColorMatteType,exception); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11715 | |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 11716 | else |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 11717 | (void) SetImageType(image,TrueColorType,exception); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11718 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 11719 | (void) SyncImage(image,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11720 | } |
| 11721 | |
glennrp | fd164d2 | 2013-01-26 21:10:22 +0000 | [diff] [blame] | 11722 | if (mng_info->write_png48) |
| 11723 | { |
| 11724 | mng_info->write_png_colortype = /* 2 */ 3; |
| 11725 | mng_info->write_png_depth = 16; |
| 11726 | image->depth = 16; |
| 11727 | |
glennrp | 4dda64f | 2013-01-26 21:20:24 +0000 | [diff] [blame] | 11728 | if (image->alpha_trait == BlendPixelTrait) |
| 11729 | (void) SetImageType(image,TrueColorMatteType,exception); |
glennrp | fd164d2 | 2013-01-26 21:10:22 +0000 | [diff] [blame] | 11730 | |
| 11731 | else |
glennrp | 4dda64f | 2013-01-26 21:20:24 +0000 | [diff] [blame] | 11732 | (void) SetImageType(image,TrueColorType,exception); |
glennrp | fd164d2 | 2013-01-26 21:10:22 +0000 | [diff] [blame] | 11733 | |
glennrp | 4dda64f | 2013-01-26 21:20:24 +0000 | [diff] [blame] | 11734 | (void) SyncImage(image,exception); |
glennrp | fd164d2 | 2013-01-26 21:10:22 +0000 | [diff] [blame] | 11735 | } |
| 11736 | |
| 11737 | if (mng_info->write_png64) |
| 11738 | { |
| 11739 | mng_info->write_png_colortype = /* 6 */ 7; |
| 11740 | mng_info->write_png_depth = 16; |
| 11741 | image->depth = 16; |
| 11742 | |
glennrp | 4dda64f | 2013-01-26 21:20:24 +0000 | [diff] [blame] | 11743 | if (image->alpha_trait == BlendPixelTrait) |
| 11744 | (void) SetImageType(image,TrueColorMatteType,exception); |
glennrp | fd164d2 | 2013-01-26 21:10:22 +0000 | [diff] [blame] | 11745 | |
| 11746 | else |
glennrp | 4dda64f | 2013-01-26 21:20:24 +0000 | [diff] [blame] | 11747 | (void) SetImageType(image,TrueColorType,exception); |
glennrp | fd164d2 | 2013-01-26 21:10:22 +0000 | [diff] [blame] | 11748 | |
glennrp | 4dda64f | 2013-01-26 21:20:24 +0000 | [diff] [blame] | 11749 | (void) SyncImage(image,exception); |
glennrp | fd164d2 | 2013-01-26 21:10:22 +0000 | [diff] [blame] | 11750 | } |
| 11751 | |
cristy | 092ec8d | 2013-04-26 13:46:22 +0000 | [diff] [blame] | 11752 | value=GetImageOption(image_info,"png:bit-depth"); |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 11753 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11754 | if (value != (char *) NULL) |
| 11755 | { |
| 11756 | if (LocaleCompare(value,"1") == 0) |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 11757 | mng_info->write_png_depth = 1; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11758 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11759 | else if (LocaleCompare(value,"2") == 0) |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 11760 | mng_info->write_png_depth = 2; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11761 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11762 | else if (LocaleCompare(value,"4") == 0) |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 11763 | mng_info->write_png_depth = 4; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11764 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11765 | else if (LocaleCompare(value,"8") == 0) |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 11766 | mng_info->write_png_depth = 8; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11767 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11768 | else if (LocaleCompare(value,"16") == 0) |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 11769 | mng_info->write_png_depth = 16; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11770 | |
glennrp | bb8a733 | 2010-11-13 15:17:35 +0000 | [diff] [blame] | 11771 | else |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 11772 | (void) ThrowMagickException(exception, |
glennrp | bb8a733 | 2010-11-13 15:17:35 +0000 | [diff] [blame] | 11773 | GetMagickModule(),CoderWarning, |
| 11774 | "ignoring invalid defined png:bit-depth", |
| 11775 | "=%s",value); |
| 11776 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11777 | if (logging != MagickFalse) |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 11778 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | bb8a733 | 2010-11-13 15:17:35 +0000 | [diff] [blame] | 11779 | " png:bit-depth=%d was defined.\n",mng_info->write_png_depth); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11780 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11781 | |
cristy | 092ec8d | 2013-04-26 13:46:22 +0000 | [diff] [blame] | 11782 | value=GetImageOption(image_info,"png:color-type"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11783 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11784 | if (value != (char *) NULL) |
| 11785 | { |
| 11786 | /* We must store colortype+1 because 0 is a valid colortype */ |
| 11787 | if (LocaleCompare(value,"0") == 0) |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 11788 | mng_info->write_png_colortype = 1; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11789 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 11790 | else if (LocaleCompare(value,"1") == 0) |
| 11791 | mng_info->write_png_colortype = 2; |
| 11792 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11793 | else if (LocaleCompare(value,"2") == 0) |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 11794 | mng_info->write_png_colortype = 3; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11795 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11796 | else if (LocaleCompare(value,"3") == 0) |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 11797 | mng_info->write_png_colortype = 4; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11798 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11799 | else if (LocaleCompare(value,"4") == 0) |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 11800 | mng_info->write_png_colortype = 5; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11801 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11802 | else if (LocaleCompare(value,"6") == 0) |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 11803 | mng_info->write_png_colortype = 7; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11804 | |
glennrp | bb8a733 | 2010-11-13 15:17:35 +0000 | [diff] [blame] | 11805 | else |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 11806 | (void) ThrowMagickException(exception, |
glennrp | bb8a733 | 2010-11-13 15:17:35 +0000 | [diff] [blame] | 11807 | GetMagickModule(),CoderWarning, |
| 11808 | "ignoring invalid defined png:color-type", |
| 11809 | "=%s",value); |
| 11810 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11811 | if (logging != MagickFalse) |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 11812 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | d6bf161 | 2010-12-17 17:28:54 +0000 | [diff] [blame] | 11813 | " png:color-type=%d was defined.\n",mng_info->write_png_colortype-1); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11814 | } |
| 11815 | |
glennrp | 0e8ea19 | 2010-12-24 18:00:33 +0000 | [diff] [blame] | 11816 | /* Check for chunks to be excluded: |
| 11817 | * |
glennrp | 0dff56c | 2011-01-29 19:10:02 +0000 | [diff] [blame] | 11818 | * The default is to not exclude any known chunks except for any |
glennrp | 0e8ea19 | 2010-12-24 18:00:33 +0000 | [diff] [blame] | 11819 | * listed in the "unused_chunks" array, above. |
| 11820 | * |
cristy | 5d6fc9c | 2011-12-27 03:10:42 +0000 | [diff] [blame] | 11821 | * Chunks can be listed for exclusion via a "png:exclude-chunk" |
glennrp | 0e8ea19 | 2010-12-24 18:00:33 +0000 | [diff] [blame] | 11822 | * define (in the image properties or in the image artifacts) |
| 11823 | * or via a mng_info member. For convenience, in addition |
| 11824 | * to or instead of a comma-separated list of chunks, the |
| 11825 | * "exclude-chunk" string can be simply "all" or "none". |
| 11826 | * |
| 11827 | * The exclude-chunk define takes priority over the mng_info. |
| 11828 | * |
cristy | 5d6fc9c | 2011-12-27 03:10:42 +0000 | [diff] [blame] | 11829 | * A "png:include-chunk" define takes priority over both the |
| 11830 | * mng_info and the "png:exclude-chunk" define. Like the |
glennrp | 0e8ea19 | 2010-12-24 18:00:33 +0000 | [diff] [blame] | 11831 | * "exclude-chunk" string, it can define "all" or "none" as |
glennrp | 0dff56c | 2011-01-29 19:10:02 +0000 | [diff] [blame] | 11832 | * well as a comma-separated list. Chunks that are unknown to |
| 11833 | * ImageMagick are always excluded, regardless of their "copy-safe" |
| 11834 | * status according to the PNG specification, and even if they |
glennrp | aa192b1 | 2012-01-17 21:35:21 +0000 | [diff] [blame] | 11835 | * appear in the "include-chunk" list. Such defines appearing among |
| 11836 | * the image options take priority over those found among the image |
| 11837 | * artifacts. |
glennrp | 0e8ea19 | 2010-12-24 18:00:33 +0000 | [diff] [blame] | 11838 | * |
| 11839 | * Finally, all chunks listed in the "unused_chunks" array are |
| 11840 | * automatically excluded, regardless of the other instructions |
| 11841 | * or lack thereof. |
| 11842 | * |
| 11843 | * if you exclude sRGB but not gAMA (recommended), then sRGB chunk |
| 11844 | * will not be written and the gAMA chunk will only be written if it |
| 11845 | * is not between .45 and .46, or approximately (1.0/2.2). |
| 11846 | * |
| 11847 | * If you exclude tRNS and the image has transparency, the colortype |
| 11848 | * is forced to be 4 or 6 (GRAY_ALPHA or RGB_ALPHA). |
| 11849 | * |
| 11850 | * The -strip option causes StripImage() to set the png:include-chunk |
glennrp | 104f206 | 2011-08-20 05:08:53 +0000 | [diff] [blame] | 11851 | * artifact to "none,trns,gama". |
glennrp | 0e8ea19 | 2010-12-24 18:00:33 +0000 | [diff] [blame] | 11852 | */ |
| 11853 | |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 11854 | mng_info->ping_exclude_bKGD=MagickFalse; |
| 11855 | mng_info->ping_exclude_cHRM=MagickFalse; |
glennrp | a0ed009 | 2011-04-18 16:36:29 +0000 | [diff] [blame] | 11856 | mng_info->ping_exclude_date=MagickFalse; |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 11857 | mng_info->ping_exclude_EXIF=MagickFalse; /* hex-encoded EXIF in zTXt */ |
| 11858 | mng_info->ping_exclude_gAMA=MagickFalse; |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 11859 | mng_info->ping_exclude_iCCP=MagickFalse; |
| 11860 | /* mng_info->ping_exclude_iTXt=MagickFalse; */ |
| 11861 | mng_info->ping_exclude_oFFs=MagickFalse; |
| 11862 | mng_info->ping_exclude_pHYs=MagickFalse; |
| 11863 | mng_info->ping_exclude_sRGB=MagickFalse; |
| 11864 | mng_info->ping_exclude_tEXt=MagickFalse; |
glennrp | a1e3b7b | 2010-12-24 16:37:33 +0000 | [diff] [blame] | 11865 | mng_info->ping_exclude_tRNS=MagickFalse; |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 11866 | mng_info->ping_exclude_vpAg=MagickFalse; |
| 11867 | mng_info->ping_exclude_zCCP=MagickFalse; /* hex-encoded iCCP in zTXt */ |
| 11868 | mng_info->ping_exclude_zTXt=MagickFalse; |
| 11869 | |
glennrp | 8d3d6e5 | 2011-04-19 04:39:51 +0000 | [diff] [blame] | 11870 | mng_info->ping_preserve_colormap=MagickFalse; |
| 11871 | |
cristy | 092ec8d | 2013-04-26 13:46:22 +0000 | [diff] [blame] | 11872 | value=GetImageOption(image_info,"png:preserve-colormap"); |
glennrp | 8d3d6e5 | 2011-04-19 04:39:51 +0000 | [diff] [blame] | 11873 | if (value == NULL) |
cristy | 8b206ba | 2013-04-05 01:11:15 +0000 | [diff] [blame] | 11874 | value=GetImageArtifact(image,"png:preserve-colormap"); |
glennrp | 8d3d6e5 | 2011-04-19 04:39:51 +0000 | [diff] [blame] | 11875 | if (value != NULL) |
| 11876 | mng_info->ping_preserve_colormap=MagickTrue; |
| 11877 | |
glennrp | ecab7d7 | 2013-05-14 22:50:32 +0000 | [diff] [blame] | 11878 | mng_info->ping_preserve_iCCP=MagickFalse; |
| 11879 | |
| 11880 | value=GetImageOption(image_info,"png:preserve-iCCP"); |
| 11881 | if (value == NULL) |
| 11882 | value=GetImageArtifact(image,"png:preserve-iCCP"); |
| 11883 | if (value != NULL) |
| 11884 | mng_info->ping_preserve_iCCP=MagickTrue; |
| 11885 | |
| 11886 | /* These compression-level, compression-strategy, and compression-filter |
glennrp | 1868258 | 2011-06-30 18:11:47 +0000 | [diff] [blame] | 11887 | * defines take precedence over values from the -quality option. |
| 11888 | */ |
cristy | 092ec8d | 2013-04-26 13:46:22 +0000 | [diff] [blame] | 11889 | value=GetImageOption(image_info,"png:compression-level"); |
glennrp | 1868258 | 2011-06-30 18:11:47 +0000 | [diff] [blame] | 11890 | if (value == NULL) |
cristy | 8b206ba | 2013-04-05 01:11:15 +0000 | [diff] [blame] | 11891 | value=GetImageArtifact(image,"png:compression-level"); |
glennrp | 1868258 | 2011-06-30 18:11:47 +0000 | [diff] [blame] | 11892 | if (value != NULL) |
| 11893 | { |
glennrp | 1868258 | 2011-06-30 18:11:47 +0000 | [diff] [blame] | 11894 | /* We have to add 1 to everything because 0 is a valid input, |
| 11895 | * and we want to use 0 (the default) to mean undefined. |
| 11896 | */ |
| 11897 | if (LocaleCompare(value,"0") == 0) |
| 11898 | mng_info->write_png_compression_level = 1; |
| 11899 | |
glennrp | 0ffb95c | 2012-01-30 21:16:22 +0000 | [diff] [blame] | 11900 | else if (LocaleCompare(value,"1") == 0) |
glennrp | 1868258 | 2011-06-30 18:11:47 +0000 | [diff] [blame] | 11901 | mng_info->write_png_compression_level = 2; |
| 11902 | |
| 11903 | else if (LocaleCompare(value,"2") == 0) |
| 11904 | mng_info->write_png_compression_level = 3; |
| 11905 | |
| 11906 | else if (LocaleCompare(value,"3") == 0) |
| 11907 | mng_info->write_png_compression_level = 4; |
| 11908 | |
| 11909 | else if (LocaleCompare(value,"4") == 0) |
| 11910 | mng_info->write_png_compression_level = 5; |
| 11911 | |
| 11912 | else if (LocaleCompare(value,"5") == 0) |
| 11913 | mng_info->write_png_compression_level = 6; |
| 11914 | |
| 11915 | else if (LocaleCompare(value,"6") == 0) |
| 11916 | mng_info->write_png_compression_level = 7; |
| 11917 | |
| 11918 | else if (LocaleCompare(value,"7") == 0) |
| 11919 | mng_info->write_png_compression_level = 8; |
| 11920 | |
| 11921 | else if (LocaleCompare(value,"8") == 0) |
| 11922 | mng_info->write_png_compression_level = 9; |
| 11923 | |
| 11924 | else if (LocaleCompare(value,"9") == 0) |
| 11925 | mng_info->write_png_compression_level = 10; |
| 11926 | |
| 11927 | else |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 11928 | (void) ThrowMagickException(exception, |
glennrp | 1868258 | 2011-06-30 18:11:47 +0000 | [diff] [blame] | 11929 | GetMagickModule(),CoderWarning, |
| 11930 | "ignoring invalid defined png:compression-level", |
| 11931 | "=%s",value); |
| 11932 | } |
| 11933 | |
cristy | 092ec8d | 2013-04-26 13:46:22 +0000 | [diff] [blame] | 11934 | value=GetImageOption(image_info,"png:compression-strategy"); |
glennrp | 1868258 | 2011-06-30 18:11:47 +0000 | [diff] [blame] | 11935 | if (value == NULL) |
cristy | 8b206ba | 2013-04-05 01:11:15 +0000 | [diff] [blame] | 11936 | value=GetImageArtifact(image,"png:compression-strategy"); |
glennrp | 1868258 | 2011-06-30 18:11:47 +0000 | [diff] [blame] | 11937 | if (value != NULL) |
| 11938 | { |
| 11939 | |
| 11940 | if (LocaleCompare(value,"0") == 0) |
| 11941 | mng_info->write_png_compression_strategy = Z_DEFAULT_STRATEGY+1; |
| 11942 | |
| 11943 | else if (LocaleCompare(value,"1") == 0) |
| 11944 | mng_info->write_png_compression_strategy = Z_FILTERED+1; |
| 11945 | |
| 11946 | else if (LocaleCompare(value,"2") == 0) |
| 11947 | mng_info->write_png_compression_strategy = Z_HUFFMAN_ONLY+1; |
| 11948 | |
| 11949 | else if (LocaleCompare(value,"3") == 0) |
glennrp | 98c07ad | 2011-07-01 02:52:59 +0000 | [diff] [blame] | 11950 | #ifdef Z_RLE /* Z_RLE was added to zlib-1.2.0 */ |
glennrp | 1868258 | 2011-06-30 18:11:47 +0000 | [diff] [blame] | 11951 | mng_info->write_png_compression_strategy = Z_RLE+1; |
glennrp | 98c07ad | 2011-07-01 02:52:59 +0000 | [diff] [blame] | 11952 | #else |
| 11953 | mng_info->write_png_compression_strategy = Z_DEFAULT_STRATEGY+1; |
| 11954 | #endif |
glennrp | 1868258 | 2011-06-30 18:11:47 +0000 | [diff] [blame] | 11955 | |
| 11956 | else if (LocaleCompare(value,"4") == 0) |
glennrp | 98c07ad | 2011-07-01 02:52:59 +0000 | [diff] [blame] | 11957 | #ifdef Z_FIXED /* Z_FIXED was added to zlib-1.2.2.2 */ |
glennrp | 1868258 | 2011-06-30 18:11:47 +0000 | [diff] [blame] | 11958 | mng_info->write_png_compression_strategy = Z_FIXED+1; |
glennrp | 98c07ad | 2011-07-01 02:52:59 +0000 | [diff] [blame] | 11959 | #else |
| 11960 | mng_info->write_png_compression_strategy = Z_DEFAULT_STRATEGY+1; |
| 11961 | #endif |
glennrp | 1868258 | 2011-06-30 18:11:47 +0000 | [diff] [blame] | 11962 | |
| 11963 | else |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 11964 | (void) ThrowMagickException(exception, |
glennrp | 1868258 | 2011-06-30 18:11:47 +0000 | [diff] [blame] | 11965 | GetMagickModule(),CoderWarning, |
| 11966 | "ignoring invalid defined png:compression-strategy", |
| 11967 | "=%s",value); |
| 11968 | } |
| 11969 | |
cristy | 092ec8d | 2013-04-26 13:46:22 +0000 | [diff] [blame] | 11970 | value=GetImageOption(image_info,"png:compression-filter"); |
glennrp | 1868258 | 2011-06-30 18:11:47 +0000 | [diff] [blame] | 11971 | if (value == NULL) |
cristy | 8b206ba | 2013-04-05 01:11:15 +0000 | [diff] [blame] | 11972 | value=GetImageArtifact(image,"png:compression-filter"); |
glennrp | 1868258 | 2011-06-30 18:11:47 +0000 | [diff] [blame] | 11973 | if (value != NULL) |
| 11974 | { |
| 11975 | |
| 11976 | /* To do: combinations of filters allowed by libpng |
| 11977 | * masks 0x08 through 0xf8 |
| 11978 | * |
| 11979 | * Implement this as a comma-separated list of 0,1,2,3,4,5 |
| 11980 | * where 5 is a special case meaning PNG_ALL_FILTERS. |
| 11981 | */ |
| 11982 | |
| 11983 | if (LocaleCompare(value,"0") == 0) |
| 11984 | mng_info->write_png_compression_filter = 1; |
| 11985 | |
cristy | b19b812 | 2012-10-22 11:03:30 +0000 | [diff] [blame] | 11986 | else if (LocaleCompare(value,"1") == 0) |
glennrp | 1868258 | 2011-06-30 18:11:47 +0000 | [diff] [blame] | 11987 | mng_info->write_png_compression_filter = 2; |
| 11988 | |
| 11989 | else if (LocaleCompare(value,"2") == 0) |
| 11990 | mng_info->write_png_compression_filter = 3; |
| 11991 | |
| 11992 | else if (LocaleCompare(value,"3") == 0) |
| 11993 | mng_info->write_png_compression_filter = 4; |
| 11994 | |
| 11995 | else if (LocaleCompare(value,"4") == 0) |
| 11996 | mng_info->write_png_compression_filter = 5; |
| 11997 | |
| 11998 | else if (LocaleCompare(value,"5") == 0) |
| 11999 | mng_info->write_png_compression_filter = 6; |
| 12000 | |
glennrp | 1868258 | 2011-06-30 18:11:47 +0000 | [diff] [blame] | 12001 | else |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 12002 | (void) ThrowMagickException(exception, |
glennrp | 1868258 | 2011-06-30 18:11:47 +0000 | [diff] [blame] | 12003 | GetMagickModule(),CoderWarning, |
| 12004 | "ignoring invalid defined png:compression-filter", |
| 12005 | "=%s",value); |
| 12006 | } |
| 12007 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12008 | excluding=MagickFalse; |
| 12009 | |
glennrp | 5c7cf4e | 2010-12-24 00:30:00 +0000 | [diff] [blame] | 12010 | for (source=0; source<1; source++) |
| 12011 | { |
| 12012 | if (source==0) |
glennrp | acba004 | 2010-12-24 14:27:26 +0000 | [diff] [blame] | 12013 | { |
cristy | 092ec8d | 2013-04-26 13:46:22 +0000 | [diff] [blame] | 12014 | value=GetImageOption(image_info,"png:exclude-chunk"); |
glennrp | acba004 | 2010-12-24 14:27:26 +0000 | [diff] [blame] | 12015 | |
| 12016 | if (value == NULL) |
| 12017 | value=GetImageArtifact(image,"png:exclude-chunks"); |
| 12018 | } |
glennrp | 5c7cf4e | 2010-12-24 00:30:00 +0000 | [diff] [blame] | 12019 | else |
glennrp | acba004 | 2010-12-24 14:27:26 +0000 | [diff] [blame] | 12020 | { |
cristy | 092ec8d | 2013-04-26 13:46:22 +0000 | [diff] [blame] | 12021 | value=GetImageOption(image_info,"png:exclude-chunk"); |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 12022 | |
glennrp | acba004 | 2010-12-24 14:27:26 +0000 | [diff] [blame] | 12023 | if (value == NULL) |
cristy | 8b206ba | 2013-04-05 01:11:15 +0000 | [diff] [blame] | 12024 | value=GetImageArtifact(image,"png:exclude-chunks"); |
glennrp | acba004 | 2010-12-24 14:27:26 +0000 | [diff] [blame] | 12025 | } |
| 12026 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12027 | if (value != NULL) |
glennrp | ce91ed5 | 2010-12-23 22:37:49 +0000 | [diff] [blame] | 12028 | { |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12029 | |
| 12030 | size_t |
| 12031 | last; |
| 12032 | |
| 12033 | excluding=MagickTrue; |
| 12034 | |
| 12035 | if (logging != MagickFalse) |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 12036 | { |
| 12037 | if (source == 0) |
| 12038 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 12039 | " png:exclude-chunk=%s found in image artifacts.\n", value); |
| 12040 | else |
| 12041 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 12042 | " png:exclude-chunk=%s found in image properties.\n", value); |
| 12043 | } |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12044 | |
| 12045 | last=strlen(value); |
| 12046 | |
| 12047 | for (i=0; i<(int) last; i+=5) |
glennrp | ce91ed5 | 2010-12-23 22:37:49 +0000 | [diff] [blame] | 12048 | { |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12049 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12050 | if (LocaleNCompare(value+i,"none",4) == 0) |
| 12051 | { |
| 12052 | mng_info->ping_exclude_bKGD=MagickFalse; |
| 12053 | mng_info->ping_exclude_cHRM=MagickFalse; |
glennrp | a0ed009 | 2011-04-18 16:36:29 +0000 | [diff] [blame] | 12054 | mng_info->ping_exclude_date=MagickFalse; |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12055 | mng_info->ping_exclude_EXIF=MagickFalse; |
| 12056 | mng_info->ping_exclude_gAMA=MagickFalse; |
| 12057 | mng_info->ping_exclude_iCCP=MagickFalse; |
| 12058 | /* mng_info->ping_exclude_iTXt=MagickFalse; */ |
| 12059 | mng_info->ping_exclude_oFFs=MagickFalse; |
| 12060 | mng_info->ping_exclude_pHYs=MagickFalse; |
| 12061 | mng_info->ping_exclude_sRGB=MagickFalse; |
| 12062 | mng_info->ping_exclude_tEXt=MagickFalse; |
glennrp | a1e3b7b | 2010-12-24 16:37:33 +0000 | [diff] [blame] | 12063 | mng_info->ping_exclude_tRNS=MagickFalse; |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12064 | mng_info->ping_exclude_vpAg=MagickFalse; |
| 12065 | mng_info->ping_exclude_zCCP=MagickFalse; |
| 12066 | mng_info->ping_exclude_zTXt=MagickFalse; |
| 12067 | } |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 12068 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12069 | if (LocaleNCompare(value+i,"bkgd",4) == 0) |
| 12070 | mng_info->ping_exclude_bKGD=MagickTrue; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 12071 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12072 | if (LocaleNCompare(value+i,"chrm",4) == 0) |
| 12073 | mng_info->ping_exclude_cHRM=MagickTrue; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 12074 | |
glennrp | a0ed009 | 2011-04-18 16:36:29 +0000 | [diff] [blame] | 12075 | if (LocaleNCompare(value+i,"date",4) == 0) |
| 12076 | mng_info->ping_exclude_date=MagickTrue; |
| 12077 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12078 | if (LocaleNCompare(value+i,"exif",4) == 0) |
| 12079 | mng_info->ping_exclude_EXIF=MagickTrue; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 12080 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12081 | if (LocaleNCompare(value+i,"gama",4) == 0) |
| 12082 | mng_info->ping_exclude_gAMA=MagickTrue; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 12083 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12084 | if (LocaleNCompare(value+i,"iccp",4) == 0) |
| 12085 | mng_info->ping_exclude_iCCP=MagickTrue; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 12086 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12087 | /* |
| 12088 | if (LocaleNCompare(value+i,"itxt",4) == 0) |
| 12089 | mng_info->ping_exclude_iTXt=MagickTrue; |
| 12090 | */ |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 12091 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12092 | if (LocaleNCompare(value+i,"gama",4) == 0) |
| 12093 | mng_info->ping_exclude_gAMA=MagickTrue; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 12094 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12095 | if (LocaleNCompare(value+i,"offs",4) == 0) |
| 12096 | mng_info->ping_exclude_oFFs=MagickTrue; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 12097 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12098 | if (LocaleNCompare(value+i,"phys",4) == 0) |
| 12099 | mng_info->ping_exclude_pHYs=MagickTrue; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 12100 | |
glennrp | a1e3b7b | 2010-12-24 16:37:33 +0000 | [diff] [blame] | 12101 | if (LocaleNCompare(value+i,"srgb",4) == 0) |
| 12102 | mng_info->ping_exclude_sRGB=MagickTrue; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 12103 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12104 | if (LocaleNCompare(value+i,"text",4) == 0) |
| 12105 | mng_info->ping_exclude_tEXt=MagickTrue; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 12106 | |
glennrp | a1e3b7b | 2010-12-24 16:37:33 +0000 | [diff] [blame] | 12107 | if (LocaleNCompare(value+i,"trns",4) == 0) |
| 12108 | mng_info->ping_exclude_tRNS=MagickTrue; |
| 12109 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12110 | if (LocaleNCompare(value+i,"vpag",4) == 0) |
| 12111 | mng_info->ping_exclude_vpAg=MagickTrue; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 12112 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12113 | if (LocaleNCompare(value+i,"zccp",4) == 0) |
| 12114 | mng_info->ping_exclude_zCCP=MagickTrue; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 12115 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12116 | if (LocaleNCompare(value+i,"ztxt",4) == 0) |
| 12117 | mng_info->ping_exclude_zTXt=MagickTrue; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 12118 | |
glennrp | 97d1b57 | 2013-05-24 01:41:32 +0000 | [diff] [blame] | 12119 | if (LocaleNCompare(value+i,"all",3) == 0) |
| 12120 | { |
| 12121 | mng_info->ping_exclude_bKGD=MagickTrue; |
| 12122 | mng_info->ping_exclude_cHRM=MagickTrue; |
| 12123 | mng_info->ping_exclude_date=MagickTrue; |
| 12124 | mng_info->ping_exclude_EXIF=MagickTrue; |
| 12125 | mng_info->ping_exclude_gAMA=MagickTrue; |
| 12126 | mng_info->ping_exclude_iCCP=MagickTrue; |
| 12127 | /* mng_info->ping_exclude_iTXt=MagickTrue; */ |
| 12128 | mng_info->ping_exclude_oFFs=MagickTrue; |
| 12129 | mng_info->ping_exclude_pHYs=MagickTrue; |
| 12130 | mng_info->ping_exclude_sRGB=MagickTrue; |
| 12131 | mng_info->ping_exclude_tEXt=MagickTrue; |
| 12132 | mng_info->ping_exclude_tRNS=MagickTrue; |
| 12133 | mng_info->ping_exclude_vpAg=MagickTrue; |
| 12134 | mng_info->ping_exclude_zCCP=MagickTrue; |
| 12135 | mng_info->ping_exclude_zTXt=MagickTrue; |
| 12136 | i--; |
| 12137 | } |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12138 | } |
glennrp | ce91ed5 | 2010-12-23 22:37:49 +0000 | [diff] [blame] | 12139 | } |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 12140 | } |
| 12141 | |
glennrp | 5c7cf4e | 2010-12-24 00:30:00 +0000 | [diff] [blame] | 12142 | for (source=0; source<1; source++) |
| 12143 | { |
| 12144 | if (source==0) |
glennrp | acba004 | 2010-12-24 14:27:26 +0000 | [diff] [blame] | 12145 | { |
cristy | 092ec8d | 2013-04-26 13:46:22 +0000 | [diff] [blame] | 12146 | value=GetImageOption(image_info,"png:include-chunk"); |
glennrp | acba004 | 2010-12-24 14:27:26 +0000 | [diff] [blame] | 12147 | |
| 12148 | if (value == NULL) |
| 12149 | value=GetImageArtifact(image,"png:include-chunks"); |
| 12150 | } |
glennrp | 5c7cf4e | 2010-12-24 00:30:00 +0000 | [diff] [blame] | 12151 | else |
glennrp | acba004 | 2010-12-24 14:27:26 +0000 | [diff] [blame] | 12152 | { |
cristy | 092ec8d | 2013-04-26 13:46:22 +0000 | [diff] [blame] | 12153 | value=GetImageOption(image_info,"png:include-chunk"); |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 12154 | |
glennrp | acba004 | 2010-12-24 14:27:26 +0000 | [diff] [blame] | 12155 | if (value == NULL) |
cristy | 8b206ba | 2013-04-05 01:11:15 +0000 | [diff] [blame] | 12156 | value=GetImageArtifact(image,"png:include-chunks"); |
glennrp | acba004 | 2010-12-24 14:27:26 +0000 | [diff] [blame] | 12157 | } |
| 12158 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12159 | if (value != NULL) |
| 12160 | { |
| 12161 | size_t |
| 12162 | last; |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 12163 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12164 | excluding=MagickTrue; |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 12165 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12166 | if (logging != MagickFalse) |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 12167 | { |
| 12168 | if (source == 0) |
| 12169 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 12170 | " png:include-chunk=%s found in image artifacts.\n", value); |
| 12171 | else |
| 12172 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 12173 | " png:include-chunk=%s found in image properties.\n", value); |
| 12174 | } |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12175 | |
| 12176 | last=strlen(value); |
| 12177 | |
| 12178 | for (i=0; i<(int) last; i+=5) |
| 12179 | { |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12180 | if (LocaleNCompare(value+i,"none",4) == 0) |
| 12181 | { |
| 12182 | mng_info->ping_exclude_bKGD=MagickTrue; |
| 12183 | mng_info->ping_exclude_cHRM=MagickTrue; |
glennrp | a0ed009 | 2011-04-18 16:36:29 +0000 | [diff] [blame] | 12184 | mng_info->ping_exclude_date=MagickTrue; |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12185 | mng_info->ping_exclude_EXIF=MagickTrue; |
| 12186 | mng_info->ping_exclude_gAMA=MagickTrue; |
| 12187 | mng_info->ping_exclude_iCCP=MagickTrue; |
| 12188 | /* mng_info->ping_exclude_iTXt=MagickTrue; */ |
| 12189 | mng_info->ping_exclude_oFFs=MagickTrue; |
| 12190 | mng_info->ping_exclude_pHYs=MagickTrue; |
| 12191 | mng_info->ping_exclude_sRGB=MagickTrue; |
| 12192 | mng_info->ping_exclude_tEXt=MagickTrue; |
glennrp | a1e3b7b | 2010-12-24 16:37:33 +0000 | [diff] [blame] | 12193 | mng_info->ping_exclude_tRNS=MagickTrue; |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12194 | mng_info->ping_exclude_vpAg=MagickTrue; |
| 12195 | mng_info->ping_exclude_zCCP=MagickTrue; |
| 12196 | mng_info->ping_exclude_zTXt=MagickTrue; |
| 12197 | } |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 12198 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12199 | if (LocaleNCompare(value+i,"bkgd",4) == 0) |
| 12200 | mng_info->ping_exclude_bKGD=MagickFalse; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 12201 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12202 | if (LocaleNCompare(value+i,"chrm",4) == 0) |
| 12203 | mng_info->ping_exclude_cHRM=MagickFalse; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 12204 | |
glennrp | a0ed009 | 2011-04-18 16:36:29 +0000 | [diff] [blame] | 12205 | if (LocaleNCompare(value+i,"date",4) == 0) |
| 12206 | mng_info->ping_exclude_date=MagickFalse; |
| 12207 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12208 | if (LocaleNCompare(value+i,"exif",4) == 0) |
| 12209 | mng_info->ping_exclude_EXIF=MagickFalse; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 12210 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12211 | if (LocaleNCompare(value+i,"gama",4) == 0) |
| 12212 | mng_info->ping_exclude_gAMA=MagickFalse; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 12213 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12214 | if (LocaleNCompare(value+i,"iccp",4) == 0) |
| 12215 | mng_info->ping_exclude_iCCP=MagickFalse; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 12216 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12217 | /* |
| 12218 | if (LocaleNCompare(value+i,"itxt",4) == 0) |
| 12219 | mng_info->ping_exclude_iTXt=MagickFalse; |
| 12220 | */ |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 12221 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12222 | if (LocaleNCompare(value+i,"gama",4) == 0) |
| 12223 | mng_info->ping_exclude_gAMA=MagickFalse; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 12224 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12225 | if (LocaleNCompare(value+i,"offs",4) == 0) |
| 12226 | mng_info->ping_exclude_oFFs=MagickFalse; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 12227 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12228 | if (LocaleNCompare(value+i,"phys",4) == 0) |
| 12229 | mng_info->ping_exclude_pHYs=MagickFalse; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 12230 | |
glennrp | a1e3b7b | 2010-12-24 16:37:33 +0000 | [diff] [blame] | 12231 | if (LocaleNCompare(value+i,"srgb",4) == 0) |
| 12232 | mng_info->ping_exclude_sRGB=MagickFalse; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 12233 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12234 | if (LocaleNCompare(value+i,"text",4) == 0) |
| 12235 | mng_info->ping_exclude_tEXt=MagickFalse; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 12236 | |
glennrp | a1e3b7b | 2010-12-24 16:37:33 +0000 | [diff] [blame] | 12237 | if (LocaleNCompare(value+i,"trns",4) == 0) |
| 12238 | mng_info->ping_exclude_tRNS=MagickFalse; |
| 12239 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12240 | if (LocaleNCompare(value+i,"vpag",4) == 0) |
| 12241 | mng_info->ping_exclude_vpAg=MagickFalse; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 12242 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12243 | if (LocaleNCompare(value+i,"zccp",4) == 0) |
| 12244 | mng_info->ping_exclude_zCCP=MagickFalse; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 12245 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12246 | if (LocaleNCompare(value+i,"ztxt",4) == 0) |
| 12247 | mng_info->ping_exclude_zTXt=MagickFalse; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 12248 | |
glennrp | 97d1b57 | 2013-05-24 01:41:32 +0000 | [diff] [blame] | 12249 | if (LocaleNCompare(value+i,"all",3) == 0) |
| 12250 | { |
| 12251 | mng_info->ping_exclude_bKGD=MagickFalse; |
| 12252 | mng_info->ping_exclude_cHRM=MagickFalse; |
| 12253 | mng_info->ping_exclude_date=MagickFalse; |
| 12254 | mng_info->ping_exclude_EXIF=MagickFalse; |
| 12255 | mng_info->ping_exclude_gAMA=MagickFalse; |
| 12256 | mng_info->ping_exclude_iCCP=MagickFalse; |
| 12257 | /* mng_info->ping_exclude_iTXt=MagickFalse; */ |
| 12258 | mng_info->ping_exclude_oFFs=MagickFalse; |
| 12259 | mng_info->ping_exclude_pHYs=MagickFalse; |
| 12260 | mng_info->ping_exclude_sRGB=MagickFalse; |
| 12261 | mng_info->ping_exclude_tEXt=MagickFalse; |
| 12262 | mng_info->ping_exclude_tRNS=MagickFalse; |
| 12263 | mng_info->ping_exclude_vpAg=MagickFalse; |
| 12264 | mng_info->ping_exclude_zCCP=MagickFalse; |
| 12265 | mng_info->ping_exclude_zTXt=MagickFalse; |
| 12266 | i--; |
| 12267 | } |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12268 | } |
glennrp | ce91ed5 | 2010-12-23 22:37:49 +0000 | [diff] [blame] | 12269 | } |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 12270 | } |
| 12271 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12272 | if (excluding != MagickFalse && logging != MagickFalse) |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 12273 | { |
| 12274 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | 5d6fc9c | 2011-12-27 03:10:42 +0000 | [diff] [blame] | 12275 | " Chunks to be excluded from the output png:"); |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 12276 | if (mng_info->ping_exclude_bKGD != MagickFalse) |
| 12277 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 12278 | " bKGD"); |
| 12279 | if (mng_info->ping_exclude_cHRM != MagickFalse) |
| 12280 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 12281 | " cHRM"); |
glennrp | a0ed009 | 2011-04-18 16:36:29 +0000 | [diff] [blame] | 12282 | if (mng_info->ping_exclude_date != MagickFalse) |
| 12283 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 12284 | " date"); |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 12285 | if (mng_info->ping_exclude_EXIF != MagickFalse) |
| 12286 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 12287 | " EXIF"); |
| 12288 | if (mng_info->ping_exclude_gAMA != MagickFalse) |
| 12289 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 12290 | " gAMA"); |
| 12291 | if (mng_info->ping_exclude_iCCP != MagickFalse) |
| 12292 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 12293 | " iCCP"); |
| 12294 | /* |
| 12295 | if (mng_info->ping_exclude_iTXt != MagickFalse) |
| 12296 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 12297 | " iTXt"); |
| 12298 | */ |
| 12299 | if (mng_info->ping_exclude_oFFs != MagickFalse) |
| 12300 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 12301 | " oFFs"); |
| 12302 | if (mng_info->ping_exclude_pHYs != MagickFalse) |
| 12303 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 12304 | " pHYs"); |
| 12305 | if (mng_info->ping_exclude_sRGB != MagickFalse) |
| 12306 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 12307 | " sRGB"); |
| 12308 | if (mng_info->ping_exclude_tEXt != MagickFalse) |
| 12309 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 12310 | " tEXt"); |
glennrp | a1e3b7b | 2010-12-24 16:37:33 +0000 | [diff] [blame] | 12311 | if (mng_info->ping_exclude_tRNS != MagickFalse) |
| 12312 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 12313 | " tRNS"); |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 12314 | if (mng_info->ping_exclude_vpAg != MagickFalse) |
| 12315 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 12316 | " vpAg"); |
| 12317 | if (mng_info->ping_exclude_zCCP != MagickFalse) |
| 12318 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 12319 | " zCCP"); |
| 12320 | if (mng_info->ping_exclude_zTXt != MagickFalse) |
| 12321 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 12322 | " zTXt"); |
| 12323 | } |
| 12324 | |
glennrp | b9cfe27 | 2010-12-21 15:08:06 +0000 | [diff] [blame] | 12325 | mng_info->need_blob = MagickTrue; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12326 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 12327 | status=WriteOnePNGImage(mng_info,image_info,image,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12328 | |
| 12329 | MngInfoFreeStruct(mng_info,&have_mng_structure); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12330 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12331 | if (logging != MagickFalse) |
| 12332 | (void) LogMagickEvent(CoderEvent,GetMagickModule(),"exit WritePNGImage()"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12333 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12334 | return(status); |
| 12335 | } |
| 12336 | |
| 12337 | #if defined(JNG_SUPPORTED) |
| 12338 | |
| 12339 | /* Write one JNG image */ |
| 12340 | static MagickBooleanType WriteOneJNGImage(MngInfo *mng_info, |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 12341 | const ImageInfo *image_info,Image *image,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12342 | { |
| 12343 | Image |
| 12344 | *jpeg_image; |
| 12345 | |
| 12346 | ImageInfo |
| 12347 | *jpeg_image_info; |
| 12348 | |
| 12349 | MagickBooleanType |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12350 | logging, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12351 | status; |
| 12352 | |
| 12353 | size_t |
| 12354 | length; |
| 12355 | |
| 12356 | unsigned char |
| 12357 | *blob, |
| 12358 | chunk[80], |
| 12359 | *p; |
| 12360 | |
| 12361 | unsigned int |
| 12362 | jng_alpha_compression_method, |
| 12363 | jng_alpha_sample_depth, |
| 12364 | jng_color_type, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12365 | transparent; |
| 12366 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 12367 | size_t |
glennrp | 59575fa | 2011-12-31 21:31:39 +0000 | [diff] [blame] | 12368 | jng_alpha_quality, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12369 | jng_quality; |
| 12370 | |
| 12371 | logging=LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 12372 | " Enter WriteOneJNGImage()"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12373 | |
| 12374 | blob=(unsigned char *) NULL; |
| 12375 | jpeg_image=(Image *) NULL; |
| 12376 | jpeg_image_info=(ImageInfo *) NULL; |
| 12377 | |
| 12378 | status=MagickTrue; |
| 12379 | transparent=image_info->type==GrayscaleMatteType || |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 12380 | image_info->type==TrueColorMatteType || image->alpha_trait == BlendPixelTrait; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12381 | |
glennrp | 4b91759 | 2013-06-20 19:53:59 +0000 | [diff] [blame] | 12382 | jng_alpha_sample_depth = 0; |
| 12383 | |
glennrp | 59575fa | 2011-12-31 21:31:39 +0000 | [diff] [blame] | 12384 | jng_quality=image_info->quality == 0UL ? 75UL : image_info->quality%1000; |
| 12385 | |
| 12386 | jng_alpha_compression_method=image->compression==JPEGCompression? 8 : 0; |
| 12387 | |
glennrp | 750105b | 2012-04-25 16:20:45 +0000 | [diff] [blame] | 12388 | jng_alpha_quality=image_info->quality == 0UL ? 75UL : |
glennrp | 59575fa | 2011-12-31 21:31:39 +0000 | [diff] [blame] | 12389 | image_info->quality; |
| 12390 | |
| 12391 | if (jng_alpha_quality >= 1000) |
| 12392 | jng_alpha_quality /= 1000; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12393 | |
| 12394 | if (transparent) |
| 12395 | { |
| 12396 | jng_color_type=14; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12397 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12398 | /* Create JPEG blob, image, and image_info */ |
| 12399 | if (logging != MagickFalse) |
| 12400 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 12401 | " Creating jpeg_image_info for alpha."); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12402 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12403 | jpeg_image_info=(ImageInfo *) CloneImageInfo(image_info); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12404 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12405 | if (jpeg_image_info == (ImageInfo *) NULL) |
| 12406 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12407 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12408 | if (logging != MagickFalse) |
| 12409 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 12410 | " Creating jpeg_image."); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12411 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 12412 | jpeg_image=SeparateImage(image,AlphaChannel,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12413 | if (jpeg_image == (Image *) NULL) |
| 12414 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
| 12415 | (void) CopyMagickString(jpeg_image->magick,"JPEG",MaxTextExtent); |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 12416 | jpeg_image->alpha_trait=UndefinedPixelTrait; |
glennrp | 8f77fdc | 2012-03-21 15:16:37 +0000 | [diff] [blame] | 12417 | jpeg_image->quality=jng_alpha_quality; |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 12418 | jpeg_image_info->type=GrayscaleType; |
| 12419 | (void) SetImageType(jpeg_image,GrayscaleType,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12420 | (void) AcquireUniqueFilename(jpeg_image->filename); |
cristy | 3b6fd2e | 2011-05-20 12:53:50 +0000 | [diff] [blame] | 12421 | (void) FormatLocaleString(jpeg_image_info->filename,MaxTextExtent, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12422 | "%s",jpeg_image->filename); |
| 12423 | } |
glennrp | 59575fa | 2011-12-31 21:31:39 +0000 | [diff] [blame] | 12424 | else |
| 12425 | { |
| 12426 | jng_alpha_compression_method=0; |
| 12427 | jng_color_type=10; |
| 12428 | jng_alpha_sample_depth=0; |
| 12429 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12430 | |
| 12431 | /* To do: check bit depth of PNG alpha channel */ |
| 12432 | |
| 12433 | /* Check if image is grayscale. */ |
| 12434 | if (image_info->type != TrueColorMatteType && image_info->type != |
cristy | 7fb2652 | 2012-06-21 13:02:48 +0000 | [diff] [blame] | 12435 | TrueColorType && IsImageGray(image,exception)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12436 | jng_color_type-=2; |
| 12437 | |
glennrp | 59575fa | 2011-12-31 21:31:39 +0000 | [diff] [blame] | 12438 | if (logging != MagickFalse) |
| 12439 | { |
| 12440 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 12441 | " JNG Quality = %d",(int) jng_quality); |
| 12442 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 12443 | " JNG Color Type = %d",jng_color_type); |
| 12444 | if (transparent) |
| 12445 | { |
| 12446 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 12447 | " JNG Alpha Compression = %d",jng_alpha_compression_method); |
| 12448 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 12449 | " JNG Alpha Depth = %d",jng_alpha_sample_depth); |
| 12450 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 12451 | " JNG Alpha Quality = %d",(int) jng_alpha_quality); |
| 12452 | } |
| 12453 | } |
| 12454 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12455 | if (transparent) |
| 12456 | { |
| 12457 | if (jng_alpha_compression_method==0) |
| 12458 | { |
| 12459 | const char |
| 12460 | *value; |
| 12461 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 12462 | /* Encode alpha as a grayscale PNG blob */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12463 | status=OpenBlob(jpeg_image_info,jpeg_image,WriteBinaryBlobMode, |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 12464 | exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12465 | if (logging != MagickFalse) |
| 12466 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 12467 | " Creating PNG blob."); |
| 12468 | length=0; |
| 12469 | |
| 12470 | (void) CopyMagickString(jpeg_image_info->magick,"PNG",MaxTextExtent); |
| 12471 | (void) CopyMagickString(jpeg_image->magick,"PNG",MaxTextExtent); |
| 12472 | jpeg_image_info->interlace=NoInterlace; |
| 12473 | |
glennrp | cc5d45b | 2012-01-06 04:06:10 +0000 | [diff] [blame] | 12474 | /* Exclude all ancillary chunks */ |
| 12475 | (void) SetImageArtifact(jpeg_image,"png:exclude-chunks","all"); |
| 12476 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12477 | blob=ImageToBlob(jpeg_image_info,jpeg_image,&length, |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 12478 | exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12479 | |
| 12480 | /* Retrieve sample depth used */ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 12481 | value=GetImageProperty(jpeg_image,"png:bit-depth-written",exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12482 | if (value != (char *) NULL) |
| 12483 | jng_alpha_sample_depth= (unsigned int) value[0]; |
| 12484 | } |
| 12485 | else |
| 12486 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 12487 | /* Encode alpha as a grayscale JPEG blob */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12488 | |
| 12489 | status=OpenBlob(jpeg_image_info,jpeg_image,WriteBinaryBlobMode, |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 12490 | exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12491 | |
| 12492 | (void) CopyMagickString(jpeg_image_info->magick,"JPEG",MaxTextExtent); |
| 12493 | (void) CopyMagickString(jpeg_image->magick,"JPEG",MaxTextExtent); |
| 12494 | jpeg_image_info->interlace=NoInterlace; |
| 12495 | if (logging != MagickFalse) |
| 12496 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 12497 | " Creating blob."); |
| 12498 | blob=ImageToBlob(jpeg_image_info,jpeg_image,&length, |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 12499 | exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12500 | jng_alpha_sample_depth=8; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12501 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12502 | if (logging != MagickFalse) |
| 12503 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 12504 | " Successfully read jpeg_image into a blob, length=%.20g.", |
| 12505 | (double) length); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12506 | |
| 12507 | } |
| 12508 | /* Destroy JPEG image and image_info */ |
| 12509 | jpeg_image=DestroyImage(jpeg_image); |
| 12510 | (void) RelinquishUniqueFileResource(jpeg_image_info->filename); |
| 12511 | jpeg_image_info=DestroyImageInfo(jpeg_image_info); |
| 12512 | } |
| 12513 | |
| 12514 | /* Write JHDR chunk */ |
| 12515 | (void) WriteBlobMSBULong(image,16L); /* chunk data length=16 */ |
| 12516 | PNGType(chunk,mng_JHDR); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12517 | LogPNGChunk(logging,mng_JHDR,16L); |
cristy | 4e5bc84 | 2010-06-09 13:56:01 +0000 | [diff] [blame] | 12518 | PNGLong(chunk+4,(png_uint_32) image->columns); |
| 12519 | PNGLong(chunk+8,(png_uint_32) image->rows); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12520 | chunk[12]=jng_color_type; |
| 12521 | chunk[13]=8; /* sample depth */ |
| 12522 | chunk[14]=8; /*jng_image_compression_method */ |
| 12523 | chunk[15]=(unsigned char) (image_info->interlace == NoInterlace ? 0 : 8); |
| 12524 | chunk[16]=jng_alpha_sample_depth; |
| 12525 | chunk[17]=jng_alpha_compression_method; |
| 12526 | chunk[18]=0; /*jng_alpha_filter_method */ |
| 12527 | chunk[19]=0; /*jng_alpha_interlace_method */ |
| 12528 | (void) WriteBlob(image,20,chunk); |
| 12529 | (void) WriteBlobMSBULong(image,crc32(0,chunk,20)); |
| 12530 | if (logging != MagickFalse) |
| 12531 | { |
| 12532 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | f2faecf | 2010-05-28 19:19:36 +0000 | [diff] [blame] | 12533 | " JNG width:%15lu",(unsigned long) image->columns); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12534 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12535 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | f2faecf | 2010-05-28 19:19:36 +0000 | [diff] [blame] | 12536 | " JNG height:%14lu",(unsigned long) image->rows); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12537 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12538 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 12539 | " JNG color type:%10d",jng_color_type); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12540 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12541 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 12542 | " JNG sample depth:%8d",8); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12543 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12544 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 12545 | " JNG compression:%9d",8); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12546 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12547 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 12548 | " JNG interlace:%11d",0); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12549 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12550 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 12551 | " JNG alpha depth:%9d",jng_alpha_sample_depth); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12552 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12553 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 12554 | " JNG alpha compression:%3d",jng_alpha_compression_method); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12555 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12556 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 12557 | " JNG alpha filter:%8d",0); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12558 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12559 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 12560 | " JNG alpha interlace:%5d",0); |
| 12561 | } |
| 12562 | |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12563 | /* Write any JNG-chunk-b profiles */ |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 12564 | (void) Magick_png_write_chunk_from_profile(image,"JNG-chunk-b",logging); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12565 | |
| 12566 | /* |
| 12567 | Write leading ancillary chunks |
| 12568 | */ |
| 12569 | |
| 12570 | if (transparent) |
| 12571 | { |
| 12572 | /* |
| 12573 | Write JNG bKGD chunk |
| 12574 | */ |
| 12575 | |
| 12576 | unsigned char |
| 12577 | blue, |
| 12578 | green, |
| 12579 | red; |
| 12580 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 12581 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12582 | num_bytes; |
| 12583 | |
| 12584 | if (jng_color_type == 8 || jng_color_type == 12) |
| 12585 | num_bytes=6L; |
| 12586 | else |
| 12587 | num_bytes=10L; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 12588 | (void) WriteBlobMSBULong(image,(size_t) (num_bytes-4L)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12589 | PNGType(chunk,mng_bKGD); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12590 | LogPNGChunk(logging,mng_bKGD,(size_t) (num_bytes-4L)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12591 | red=ScaleQuantumToChar(image->background_color.red); |
| 12592 | green=ScaleQuantumToChar(image->background_color.green); |
| 12593 | blue=ScaleQuantumToChar(image->background_color.blue); |
| 12594 | *(chunk+4)=0; |
| 12595 | *(chunk+5)=red; |
| 12596 | *(chunk+6)=0; |
| 12597 | *(chunk+7)=green; |
| 12598 | *(chunk+8)=0; |
| 12599 | *(chunk+9)=blue; |
| 12600 | (void) WriteBlob(image,(size_t) num_bytes,chunk); |
| 12601 | (void) WriteBlobMSBULong(image,crc32(0,chunk,(uInt) num_bytes)); |
| 12602 | } |
| 12603 | |
| 12604 | if ((image->colorspace == sRGBColorspace || image->rendering_intent)) |
| 12605 | { |
| 12606 | /* |
| 12607 | Write JNG sRGB chunk |
| 12608 | */ |
| 12609 | (void) WriteBlobMSBULong(image,1L); |
| 12610 | PNGType(chunk,mng_sRGB); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12611 | LogPNGChunk(logging,mng_sRGB,1L); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12612 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12613 | if (image->rendering_intent != UndefinedIntent) |
glennrp | e610a07 | 2010-08-05 17:08:46 +0000 | [diff] [blame] | 12614 | chunk[4]=(unsigned char) |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 12615 | Magick_RenderingIntent_to_PNG_RenderingIntent( |
glennrp | e610a07 | 2010-08-05 17:08:46 +0000 | [diff] [blame] | 12616 | (image->rendering_intent)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12617 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12618 | else |
glennrp | e610a07 | 2010-08-05 17:08:46 +0000 | [diff] [blame] | 12619 | chunk[4]=(unsigned char) |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 12620 | Magick_RenderingIntent_to_PNG_RenderingIntent( |
glennrp | e610a07 | 2010-08-05 17:08:46 +0000 | [diff] [blame] | 12621 | (PerceptualIntent)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12622 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12623 | (void) WriteBlob(image,5,chunk); |
| 12624 | (void) WriteBlobMSBULong(image,crc32(0,chunk,5)); |
| 12625 | } |
| 12626 | else |
| 12627 | { |
| 12628 | if (image->gamma != 0.0) |
| 12629 | { |
| 12630 | /* |
| 12631 | Write JNG gAMA chunk |
| 12632 | */ |
| 12633 | (void) WriteBlobMSBULong(image,4L); |
| 12634 | PNGType(chunk,mng_gAMA); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12635 | LogPNGChunk(logging,mng_gAMA,4L); |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 12636 | PNGLong(chunk+4,(png_uint_32) (100000*image->gamma+0.5)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12637 | (void) WriteBlob(image,8,chunk); |
| 12638 | (void) WriteBlobMSBULong(image,crc32(0,chunk,8)); |
| 12639 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12640 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12641 | if ((mng_info->equal_chrms == MagickFalse) && |
| 12642 | (image->chromaticity.red_primary.x != 0.0)) |
| 12643 | { |
| 12644 | PrimaryInfo |
| 12645 | primary; |
| 12646 | |
| 12647 | /* |
| 12648 | Write JNG cHRM chunk |
| 12649 | */ |
| 12650 | (void) WriteBlobMSBULong(image,32L); |
| 12651 | PNGType(chunk,mng_cHRM); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12652 | LogPNGChunk(logging,mng_cHRM,32L); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12653 | primary=image->chromaticity.white_point; |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 12654 | PNGLong(chunk+4,(png_uint_32) (100000*primary.x+0.5)); |
| 12655 | PNGLong(chunk+8,(png_uint_32) (100000*primary.y+0.5)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12656 | primary=image->chromaticity.red_primary; |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 12657 | PNGLong(chunk+12,(png_uint_32) (100000*primary.x+0.5)); |
| 12658 | PNGLong(chunk+16,(png_uint_32) (100000*primary.y+0.5)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12659 | primary=image->chromaticity.green_primary; |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 12660 | PNGLong(chunk+20,(png_uint_32) (100000*primary.x+0.5)); |
| 12661 | PNGLong(chunk+24,(png_uint_32) (100000*primary.y+0.5)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12662 | primary=image->chromaticity.blue_primary; |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 12663 | PNGLong(chunk+28,(png_uint_32) (100000*primary.x+0.5)); |
| 12664 | PNGLong(chunk+32,(png_uint_32) (100000*primary.y+0.5)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12665 | (void) WriteBlob(image,36,chunk); |
| 12666 | (void) WriteBlobMSBULong(image,crc32(0,chunk,36)); |
| 12667 | } |
| 12668 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12669 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 12670 | if (image->resolution.x && image->resolution.y && !mng_info->equal_physs) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12671 | { |
| 12672 | /* |
| 12673 | Write JNG pHYs chunk |
| 12674 | */ |
| 12675 | (void) WriteBlobMSBULong(image,9L); |
| 12676 | PNGType(chunk,mng_pHYs); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12677 | LogPNGChunk(logging,mng_pHYs,9L); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12678 | if (image->units == PixelsPerInchResolution) |
| 12679 | { |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 12680 | PNGLong(chunk+4,(png_uint_32) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 12681 | (image->resolution.x*100.0/2.54+0.5)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12682 | |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 12683 | PNGLong(chunk+8,(png_uint_32) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 12684 | (image->resolution.y*100.0/2.54+0.5)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12685 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12686 | chunk[12]=1; |
| 12687 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12688 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12689 | else |
| 12690 | { |
| 12691 | if (image->units == PixelsPerCentimeterResolution) |
| 12692 | { |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 12693 | PNGLong(chunk+4,(png_uint_32) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 12694 | (image->resolution.x*100.0+0.5)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12695 | |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 12696 | PNGLong(chunk+8,(png_uint_32) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 12697 | (image->resolution.y*100.0+0.5)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12698 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12699 | chunk[12]=1; |
| 12700 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12701 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12702 | else |
| 12703 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 12704 | PNGLong(chunk+4,(png_uint_32) (image->resolution.x+0.5)); |
| 12705 | PNGLong(chunk+8,(png_uint_32) (image->resolution.y+0.5)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12706 | chunk[12]=0; |
| 12707 | } |
| 12708 | } |
| 12709 | (void) WriteBlob(image,13,chunk); |
| 12710 | (void) WriteBlobMSBULong(image,crc32(0,chunk,13)); |
| 12711 | } |
| 12712 | |
| 12713 | if (mng_info->write_mng == 0 && (image->page.x || image->page.y)) |
| 12714 | { |
| 12715 | /* |
| 12716 | Write JNG oFFs chunk |
| 12717 | */ |
| 12718 | (void) WriteBlobMSBULong(image,9L); |
| 12719 | PNGType(chunk,mng_oFFs); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12720 | LogPNGChunk(logging,mng_oFFs,9L); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 12721 | PNGsLong(chunk+4,(ssize_t) (image->page.x)); |
| 12722 | PNGsLong(chunk+8,(ssize_t) (image->page.y)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12723 | chunk[12]=0; |
| 12724 | (void) WriteBlob(image,13,chunk); |
| 12725 | (void) WriteBlobMSBULong(image,crc32(0,chunk,13)); |
| 12726 | } |
| 12727 | if (mng_info->write_mng == 0 && (image->page.width || image->page.height)) |
| 12728 | { |
| 12729 | (void) WriteBlobMSBULong(image,9L); /* data length=8 */ |
| 12730 | PNGType(chunk,mng_vpAg); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12731 | LogPNGChunk(logging,mng_vpAg,9L); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12732 | PNGLong(chunk+4,(png_uint_32) image->page.width); |
| 12733 | PNGLong(chunk+8,(png_uint_32) image->page.height); |
| 12734 | chunk[12]=0; /* unit = pixels */ |
| 12735 | (void) WriteBlob(image,13,chunk); |
| 12736 | (void) WriteBlobMSBULong(image,crc32(0,chunk,13)); |
| 12737 | } |
| 12738 | |
| 12739 | |
| 12740 | if (transparent) |
| 12741 | { |
| 12742 | if (jng_alpha_compression_method==0) |
| 12743 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 12744 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12745 | i; |
| 12746 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 12747 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12748 | len; |
| 12749 | |
| 12750 | /* Write IDAT chunk header */ |
| 12751 | if (logging != MagickFalse) |
| 12752 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 12753 | " Write IDAT chunks from blob, length=%.20g.",(double) |
cristy | f2faecf | 2010-05-28 19:19:36 +0000 | [diff] [blame] | 12754 | length); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12755 | |
| 12756 | /* Copy IDAT chunks */ |
| 12757 | len=0; |
| 12758 | p=blob+8; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 12759 | for (i=8; i<(ssize_t) length; i+=len+12) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12760 | { |
| 12761 | len=(*p<<24)|((*(p+1))<<16)|((*(p+2))<<8)|(*(p+3)); |
| 12762 | p+=4; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12763 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12764 | if (*(p)==73 && *(p+1)==68 && *(p+2)==65 && *(p+3)==84) /* IDAT */ |
| 12765 | { |
| 12766 | /* Found an IDAT chunk. */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 12767 | (void) WriteBlobMSBULong(image,(size_t) len); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12768 | LogPNGChunk(logging,mng_IDAT,(size_t) len); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12769 | (void) WriteBlob(image,(size_t) len+4,p); |
| 12770 | (void) WriteBlobMSBULong(image, |
| 12771 | crc32(0,p,(uInt) len+4)); |
| 12772 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12773 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12774 | else |
| 12775 | { |
| 12776 | if (logging != MagickFalse) |
| 12777 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 12778 | " Skipping %c%c%c%c chunk, length=%.20g.", |
| 12779 | *(p),*(p+1),*(p+2),*(p+3),(double) len); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12780 | } |
| 12781 | p+=(8+len); |
| 12782 | } |
| 12783 | } |
| 12784 | else |
| 12785 | { |
| 12786 | /* Write JDAA chunk header */ |
| 12787 | if (logging != MagickFalse) |
| 12788 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 12789 | " Write JDAA chunk, length=%.20g.",(double) length); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 12790 | (void) WriteBlobMSBULong(image,(size_t) length); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12791 | PNGType(chunk,mng_JDAA); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12792 | LogPNGChunk(logging,mng_JDAA,length); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12793 | /* Write JDAT chunk(s) data */ |
| 12794 | (void) WriteBlob(image,4,chunk); |
| 12795 | (void) WriteBlob(image,length,blob); |
| 12796 | (void) WriteBlobMSBULong(image,crc32(crc32(0,chunk,4),blob, |
| 12797 | (uInt) length)); |
| 12798 | } |
| 12799 | blob=(unsigned char *) RelinquishMagickMemory(blob); |
| 12800 | } |
| 12801 | |
| 12802 | /* Encode image as a JPEG blob */ |
| 12803 | if (logging != MagickFalse) |
| 12804 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 12805 | " Creating jpeg_image_info."); |
| 12806 | jpeg_image_info=(ImageInfo *) CloneImageInfo(image_info); |
| 12807 | if (jpeg_image_info == (ImageInfo *) NULL) |
| 12808 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
| 12809 | |
| 12810 | if (logging != MagickFalse) |
| 12811 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 12812 | " Creating jpeg_image."); |
| 12813 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 12814 | jpeg_image=CloneImage(image,0,0,MagickTrue,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12815 | if (jpeg_image == (Image *) NULL) |
| 12816 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
| 12817 | (void) CopyMagickString(jpeg_image->magick,"JPEG",MaxTextExtent); |
| 12818 | |
| 12819 | (void) AcquireUniqueFilename(jpeg_image->filename); |
cristy | 3b6fd2e | 2011-05-20 12:53:50 +0000 | [diff] [blame] | 12820 | (void) FormatLocaleString(jpeg_image_info->filename,MaxTextExtent,"%s", |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12821 | jpeg_image->filename); |
| 12822 | |
| 12823 | status=OpenBlob(jpeg_image_info,jpeg_image,WriteBinaryBlobMode, |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 12824 | exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12825 | |
| 12826 | if (logging != MagickFalse) |
| 12827 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 12828 | " Created jpeg_image, %.20g x %.20g.",(double) jpeg_image->columns, |
| 12829 | (double) jpeg_image->rows); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12830 | |
| 12831 | if (jng_color_type == 8 || jng_color_type == 12) |
| 12832 | jpeg_image_info->type=GrayscaleType; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12833 | |
glennrp | 59575fa | 2011-12-31 21:31:39 +0000 | [diff] [blame] | 12834 | jpeg_image_info->quality=jng_quality; |
| 12835 | jpeg_image->quality=jng_quality; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12836 | (void) CopyMagickString(jpeg_image_info->magick,"JPEG",MaxTextExtent); |
| 12837 | (void) CopyMagickString(jpeg_image->magick,"JPEG",MaxTextExtent); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12838 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12839 | if (logging != MagickFalse) |
| 12840 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 12841 | " Creating blob."); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12842 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 12843 | blob=ImageToBlob(jpeg_image_info,jpeg_image,&length,exception); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12844 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12845 | if (logging != MagickFalse) |
| 12846 | { |
| 12847 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 12848 | " Successfully read jpeg_image into a blob, length=%.20g.", |
| 12849 | (double) length); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12850 | |
| 12851 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 12852 | " Write JDAT chunk, length=%.20g.",(double) length); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12853 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12854 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12855 | /* Write JDAT chunk(s) */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 12856 | (void) WriteBlobMSBULong(image,(size_t) length); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12857 | PNGType(chunk,mng_JDAT); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12858 | LogPNGChunk(logging,mng_JDAT,length); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12859 | (void) WriteBlob(image,4,chunk); |
| 12860 | (void) WriteBlob(image,length,blob); |
| 12861 | (void) WriteBlobMSBULong(image,crc32(crc32(0,chunk,4),blob,(uInt) length)); |
| 12862 | |
| 12863 | jpeg_image=DestroyImage(jpeg_image); |
| 12864 | (void) RelinquishUniqueFileResource(jpeg_image_info->filename); |
| 12865 | jpeg_image_info=DestroyImageInfo(jpeg_image_info); |
| 12866 | blob=(unsigned char *) RelinquishMagickMemory(blob); |
| 12867 | |
| 12868 | /* Write any JNG-chunk-e profiles */ |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 12869 | (void) Magick_png_write_chunk_from_profile(image,"JNG-chunk-e",logging); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12870 | |
| 12871 | /* Write IEND chunk */ |
| 12872 | (void) WriteBlobMSBULong(image,0L); |
| 12873 | PNGType(chunk,mng_IEND); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12874 | LogPNGChunk(logging,mng_IEND,0); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12875 | (void) WriteBlob(image,4,chunk); |
| 12876 | (void) WriteBlobMSBULong(image,crc32(0,chunk,4)); |
| 12877 | |
| 12878 | if (logging != MagickFalse) |
| 12879 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 12880 | " exit WriteOneJNGImage()"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12881 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12882 | return(status); |
| 12883 | } |
| 12884 | |
| 12885 | |
| 12886 | /* |
| 12887 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 12888 | % % |
| 12889 | % % |
| 12890 | % % |
| 12891 | % W r i t e J N G I m a g e % |
| 12892 | % % |
| 12893 | % % |
| 12894 | % % |
| 12895 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 12896 | % |
| 12897 | % WriteJNGImage() writes a JPEG Network Graphics (JNG) image file. |
| 12898 | % |
| 12899 | % JNG support written by Glenn Randers-Pehrson, glennrp@image... |
| 12900 | % |
| 12901 | % The format of the WriteJNGImage method is: |
| 12902 | % |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 12903 | % MagickBooleanType WriteJNGImage(const ImageInfo *image_info, |
| 12904 | % Image *image,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12905 | % |
| 12906 | % A description of each parameter follows: |
| 12907 | % |
| 12908 | % o image_info: the image info. |
| 12909 | % |
| 12910 | % o image: The image. |
| 12911 | % |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 12912 | % o exception: return any errors or warnings in this structure. |
| 12913 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12914 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 12915 | */ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 12916 | static MagickBooleanType WriteJNGImage(const ImageInfo *image_info,Image *image, |
| 12917 | ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12918 | { |
| 12919 | MagickBooleanType |
glennrp | 21f0e62 | 2011-01-07 16:20:57 +0000 | [diff] [blame] | 12920 | have_mng_structure, |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12921 | logging, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12922 | status; |
| 12923 | |
| 12924 | MngInfo |
| 12925 | *mng_info; |
| 12926 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12927 | /* |
| 12928 | Open image file. |
| 12929 | */ |
| 12930 | assert(image_info != (const ImageInfo *) NULL); |
| 12931 | assert(image_info->signature == MagickSignature); |
| 12932 | assert(image != (Image *) NULL); |
| 12933 | assert(image->signature == MagickSignature); |
| 12934 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 12935 | logging=LogMagickEvent(CoderEvent,GetMagickModule(),"Enter WriteJNGImage()"); |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 12936 | status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12937 | if (status == MagickFalse) |
| 12938 | return(status); |
| 12939 | |
| 12940 | /* |
| 12941 | Allocate a MngInfo structure. |
| 12942 | */ |
| 12943 | have_mng_structure=MagickFalse; |
cristy | 73bd4a5 | 2010-10-05 11:24:23 +0000 | [diff] [blame] | 12944 | mng_info=(MngInfo *) AcquireMagickMemory(sizeof(MngInfo)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12945 | if (mng_info == (MngInfo *) NULL) |
| 12946 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
| 12947 | /* |
| 12948 | Initialize members of the MngInfo structure. |
| 12949 | */ |
| 12950 | (void) ResetMagickMemory(mng_info,0,sizeof(MngInfo)); |
| 12951 | mng_info->image=image; |
| 12952 | have_mng_structure=MagickTrue; |
| 12953 | |
| 12954 | (void) WriteBlob(image,8,(const unsigned char *) "\213JNG\r\n\032\n"); |
| 12955 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 12956 | status=WriteOneJNGImage(mng_info,image_info,image,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12957 | (void) CloseBlob(image); |
| 12958 | |
| 12959 | (void) CatchImageException(image); |
| 12960 | MngInfoFreeStruct(mng_info,&have_mng_structure); |
| 12961 | if (logging != MagickFalse) |
| 12962 | (void) LogMagickEvent(CoderEvent,GetMagickModule(),"exit WriteJNGImage()"); |
| 12963 | return(status); |
| 12964 | } |
| 12965 | #endif |
| 12966 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 12967 | static MagickBooleanType WriteMNGImage(const ImageInfo *image_info,Image *image, |
| 12968 | ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12969 | { |
| 12970 | const char |
| 12971 | *option; |
| 12972 | |
| 12973 | Image |
| 12974 | *next_image; |
| 12975 | |
| 12976 | MagickBooleanType |
glennrp | 21f0e62 | 2011-01-07 16:20:57 +0000 | [diff] [blame] | 12977 | have_mng_structure, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12978 | status; |
| 12979 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12980 | volatile MagickBooleanType |
| 12981 | logging; |
| 12982 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12983 | MngInfo |
| 12984 | *mng_info; |
| 12985 | |
| 12986 | int |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12987 | image_count, |
| 12988 | need_iterations, |
| 12989 | need_matte; |
| 12990 | |
| 12991 | volatile int |
| 12992 | #if defined(PNG_WRITE_EMPTY_PLTE_SUPPORTED) || \ |
| 12993 | defined(PNG_MNG_FEATURES_SUPPORTED) |
| 12994 | need_local_plte, |
| 12995 | #endif |
| 12996 | all_images_are_gray, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12997 | need_defi, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12998 | use_global_plte; |
| 12999 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 13000 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13001 | i; |
| 13002 | |
| 13003 | unsigned char |
| 13004 | chunk[800]; |
| 13005 | |
| 13006 | volatile unsigned int |
| 13007 | write_jng, |
| 13008 | write_mng; |
| 13009 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 13010 | volatile size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13011 | scene; |
| 13012 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 13013 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13014 | final_delay=0, |
| 13015 | initial_delay; |
| 13016 | |
glennrp | d5045b4 | 2010-03-24 12:40:35 +0000 | [diff] [blame] | 13017 | #if (PNG_LIBPNG_VER < 10200) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13018 | if (image_info->verbose) |
| 13019 | printf("Your PNG library (libpng-%s) is rather old.\n", |
| 13020 | PNG_LIBPNG_VER_STRING); |
| 13021 | #endif |
| 13022 | |
| 13023 | /* |
| 13024 | Open image file. |
| 13025 | */ |
| 13026 | assert(image_info != (const ImageInfo *) NULL); |
| 13027 | assert(image_info->signature == MagickSignature); |
| 13028 | assert(image != (Image *) NULL); |
| 13029 | assert(image->signature == MagickSignature); |
| 13030 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 13031 | logging=LogMagickEvent(CoderEvent,GetMagickModule(),"Enter WriteMNGImage()"); |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 13032 | status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13033 | if (status == MagickFalse) |
| 13034 | return(status); |
| 13035 | |
| 13036 | /* |
| 13037 | Allocate a MngInfo structure. |
| 13038 | */ |
| 13039 | have_mng_structure=MagickFalse; |
cristy | 73bd4a5 | 2010-10-05 11:24:23 +0000 | [diff] [blame] | 13040 | mng_info=(MngInfo *) AcquireMagickMemory(sizeof(MngInfo)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13041 | if (mng_info == (MngInfo *) NULL) |
| 13042 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
| 13043 | /* |
| 13044 | Initialize members of the MngInfo structure. |
| 13045 | */ |
| 13046 | (void) ResetMagickMemory(mng_info,0,sizeof(MngInfo)); |
| 13047 | mng_info->image=image; |
| 13048 | have_mng_structure=MagickTrue; |
| 13049 | write_mng=LocaleCompare(image_info->magick,"MNG") == 0; |
| 13050 | |
| 13051 | /* |
| 13052 | * See if user has requested a specific PNG subformat to be used |
| 13053 | * for all of the PNGs in the MNG being written, e.g., |
| 13054 | * |
| 13055 | * convert *.png png8:animation.mng |
| 13056 | * |
| 13057 | * To do: check -define png:bit_depth and png:color_type as well, |
| 13058 | * or perhaps use mng:bit_depth and mng:color_type instead for |
| 13059 | * global settings. |
| 13060 | */ |
| 13061 | |
| 13062 | mng_info->write_png8=LocaleCompare(image_info->magick,"PNG8") == 0; |
| 13063 | mng_info->write_png24=LocaleCompare(image_info->magick,"PNG24") == 0; |
| 13064 | mng_info->write_png32=LocaleCompare(image_info->magick,"PNG32") == 0; |
| 13065 | |
| 13066 | write_jng=MagickFalse; |
| 13067 | if (image_info->compression == JPEGCompression) |
| 13068 | write_jng=MagickTrue; |
| 13069 | |
| 13070 | mng_info->adjoin=image_info->adjoin && |
| 13071 | (GetNextImageInList(image) != (Image *) NULL) && write_mng; |
| 13072 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13073 | if (logging != MagickFalse) |
| 13074 | { |
| 13075 | /* Log some info about the input */ |
| 13076 | Image |
| 13077 | *p; |
| 13078 | |
| 13079 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 13080 | " Checking input image(s)"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13081 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13082 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 13083 | " Image_info depth: %.20g",(double) image_info->depth); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13084 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13085 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 13086 | " Type: %d",image_info->type); |
| 13087 | |
| 13088 | scene=0; |
| 13089 | for (p=image; p != (Image *) NULL; p=GetNextImageInList(p)) |
| 13090 | { |
| 13091 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 13092 | " Scene: %.20g",(double) scene++); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13093 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13094 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 13095 | " Image depth: %.20g",(double) p->depth); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13096 | |
cristy | dc2d327 | 2013-02-12 14:00:44 +0000 | [diff] [blame] | 13097 | if (p->alpha_trait == BlendPixelTrait) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13098 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 13099 | " Matte: True"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13100 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13101 | else |
| 13102 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 13103 | " Matte: False"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13104 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13105 | if (p->storage_class == PseudoClass) |
| 13106 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 13107 | " Storage class: PseudoClass"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13108 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13109 | else |
| 13110 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 13111 | " Storage class: DirectClass"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13112 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13113 | if (p->colors) |
| 13114 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 13115 | " Number of colors: %.20g",(double) p->colors); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13116 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13117 | else |
| 13118 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 13119 | " Number of colors: unspecified"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13120 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13121 | if (mng_info->adjoin == MagickFalse) |
| 13122 | break; |
| 13123 | } |
| 13124 | } |
| 13125 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13126 | use_global_plte=MagickFalse; |
| 13127 | all_images_are_gray=MagickFalse; |
| 13128 | #ifdef PNG_WRITE_EMPTY_PLTE_SUPPORTED |
| 13129 | need_local_plte=MagickTrue; |
| 13130 | #endif |
| 13131 | need_defi=MagickFalse; |
| 13132 | need_matte=MagickFalse; |
| 13133 | mng_info->framing_mode=1; |
| 13134 | mng_info->old_framing_mode=1; |
| 13135 | |
| 13136 | if (write_mng) |
| 13137 | if (image_info->page != (char *) NULL) |
| 13138 | { |
| 13139 | /* |
| 13140 | Determine image bounding box. |
| 13141 | */ |
| 13142 | SetGeometry(image,&mng_info->page); |
| 13143 | (void) ParseMetaGeometry(image_info->page,&mng_info->page.x, |
| 13144 | &mng_info->page.y,&mng_info->page.width,&mng_info->page.height); |
| 13145 | } |
| 13146 | if (write_mng) |
| 13147 | { |
| 13148 | unsigned int |
| 13149 | need_geom; |
| 13150 | |
| 13151 | unsigned short |
| 13152 | red, |
| 13153 | green, |
| 13154 | blue; |
| 13155 | |
| 13156 | mng_info->page=image->page; |
| 13157 | need_geom=MagickTrue; |
| 13158 | if (mng_info->page.width || mng_info->page.height) |
| 13159 | need_geom=MagickFalse; |
| 13160 | /* |
| 13161 | Check all the scenes. |
| 13162 | */ |
| 13163 | initial_delay=image->delay; |
| 13164 | need_iterations=MagickFalse; |
| 13165 | mng_info->equal_chrms=image->chromaticity.red_primary.x != 0.0; |
| 13166 | mng_info->equal_physs=MagickTrue, |
| 13167 | mng_info->equal_gammas=MagickTrue; |
| 13168 | mng_info->equal_srgbs=MagickTrue; |
| 13169 | mng_info->equal_backgrounds=MagickTrue; |
| 13170 | image_count=0; |
| 13171 | #if defined(PNG_WRITE_EMPTY_PLTE_SUPPORTED) || \ |
| 13172 | defined(PNG_MNG_FEATURES_SUPPORTED) |
| 13173 | all_images_are_gray=MagickTrue; |
| 13174 | mng_info->equal_palettes=MagickFalse; |
| 13175 | need_local_plte=MagickFalse; |
| 13176 | #endif |
| 13177 | for (next_image=image; next_image != (Image *) NULL; ) |
| 13178 | { |
| 13179 | if (need_geom) |
| 13180 | { |
| 13181 | if ((next_image->columns+next_image->page.x) > mng_info->page.width) |
| 13182 | mng_info->page.width=next_image->columns+next_image->page.x; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13183 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13184 | if ((next_image->rows+next_image->page.y) > mng_info->page.height) |
| 13185 | mng_info->page.height=next_image->rows+next_image->page.y; |
| 13186 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13187 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13188 | if (next_image->page.x || next_image->page.y) |
| 13189 | need_defi=MagickTrue; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13190 | |
cristy | dc2d327 | 2013-02-12 14:00:44 +0000 | [diff] [blame] | 13191 | if (next_image->alpha_trait == BlendPixelTrait) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13192 | need_matte=MagickTrue; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13193 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13194 | if ((int) next_image->dispose >= BackgroundDispose) |
cristy | dc2d327 | 2013-02-12 14:00:44 +0000 | [diff] [blame] | 13195 | if ((next_image->alpha_trait == BlendPixelTrait) || |
| 13196 | next_image->page.x || next_image->page.y || |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13197 | ((next_image->columns < mng_info->page.width) && |
| 13198 | (next_image->rows < mng_info->page.height))) |
| 13199 | mng_info->need_fram=MagickTrue; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13200 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13201 | if (next_image->iterations) |
| 13202 | need_iterations=MagickTrue; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13203 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13204 | final_delay=next_image->delay; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13205 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13206 | if (final_delay != initial_delay || final_delay > 1UL* |
| 13207 | next_image->ticks_per_second) |
| 13208 | mng_info->need_fram=1; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13209 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13210 | #if defined(PNG_WRITE_EMPTY_PLTE_SUPPORTED) || \ |
| 13211 | defined(PNG_MNG_FEATURES_SUPPORTED) |
| 13212 | /* |
| 13213 | check for global palette possibility. |
| 13214 | */ |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 13215 | if (image->alpha_trait == BlendPixelTrait) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13216 | need_local_plte=MagickTrue; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13217 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13218 | if (need_local_plte == 0) |
| 13219 | { |
cristy | 7fb2652 | 2012-06-21 13:02:48 +0000 | [diff] [blame] | 13220 | if (IsImageGray(image,exception) == MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13221 | all_images_are_gray=MagickFalse; |
| 13222 | mng_info->equal_palettes=PalettesAreEqual(image,next_image); |
| 13223 | if (use_global_plte == 0) |
| 13224 | use_global_plte=mng_info->equal_palettes; |
| 13225 | need_local_plte=!mng_info->equal_palettes; |
| 13226 | } |
| 13227 | #endif |
| 13228 | if (GetNextImageInList(next_image) != (Image *) NULL) |
| 13229 | { |
| 13230 | if (next_image->background_color.red != |
| 13231 | next_image->next->background_color.red || |
| 13232 | next_image->background_color.green != |
| 13233 | next_image->next->background_color.green || |
| 13234 | next_image->background_color.blue != |
| 13235 | next_image->next->background_color.blue) |
| 13236 | mng_info->equal_backgrounds=MagickFalse; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13237 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13238 | if (next_image->gamma != next_image->next->gamma) |
| 13239 | mng_info->equal_gammas=MagickFalse; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13240 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13241 | if (next_image->rendering_intent != |
| 13242 | next_image->next->rendering_intent) |
| 13243 | mng_info->equal_srgbs=MagickFalse; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13244 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13245 | if ((next_image->units != next_image->next->units) || |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 13246 | (next_image->resolution.x != next_image->next->resolution.x) || |
| 13247 | (next_image->resolution.y != next_image->next->resolution.y)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13248 | mng_info->equal_physs=MagickFalse; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13249 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13250 | if (mng_info->equal_chrms) |
| 13251 | { |
| 13252 | if (next_image->chromaticity.red_primary.x != |
| 13253 | next_image->next->chromaticity.red_primary.x || |
| 13254 | next_image->chromaticity.red_primary.y != |
| 13255 | next_image->next->chromaticity.red_primary.y || |
| 13256 | next_image->chromaticity.green_primary.x != |
| 13257 | next_image->next->chromaticity.green_primary.x || |
| 13258 | next_image->chromaticity.green_primary.y != |
| 13259 | next_image->next->chromaticity.green_primary.y || |
| 13260 | next_image->chromaticity.blue_primary.x != |
| 13261 | next_image->next->chromaticity.blue_primary.x || |
| 13262 | next_image->chromaticity.blue_primary.y != |
| 13263 | next_image->next->chromaticity.blue_primary.y || |
| 13264 | next_image->chromaticity.white_point.x != |
| 13265 | next_image->next->chromaticity.white_point.x || |
| 13266 | next_image->chromaticity.white_point.y != |
| 13267 | next_image->next->chromaticity.white_point.y) |
| 13268 | mng_info->equal_chrms=MagickFalse; |
| 13269 | } |
| 13270 | } |
| 13271 | image_count++; |
| 13272 | next_image=GetNextImageInList(next_image); |
| 13273 | } |
| 13274 | if (image_count < 2) |
| 13275 | { |
| 13276 | mng_info->equal_backgrounds=MagickFalse; |
| 13277 | mng_info->equal_chrms=MagickFalse; |
| 13278 | mng_info->equal_gammas=MagickFalse; |
| 13279 | mng_info->equal_srgbs=MagickFalse; |
| 13280 | mng_info->equal_physs=MagickFalse; |
| 13281 | use_global_plte=MagickFalse; |
| 13282 | #ifdef PNG_WRITE_EMPTY_PLTE_SUPPORTED |
| 13283 | need_local_plte=MagickTrue; |
| 13284 | #endif |
| 13285 | need_iterations=MagickFalse; |
| 13286 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13287 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13288 | if (mng_info->need_fram == MagickFalse) |
| 13289 | { |
| 13290 | /* |
| 13291 | Only certain framing rates 100/n are exactly representable without |
| 13292 | the FRAM chunk but we'll allow some slop in VLC files |
| 13293 | */ |
| 13294 | if (final_delay == 0) |
| 13295 | { |
| 13296 | if (need_iterations != MagickFalse) |
| 13297 | { |
| 13298 | /* |
| 13299 | It's probably a GIF with loop; don't run it *too* fast. |
| 13300 | */ |
glennrp | 0261712 | 2010-07-28 13:07:35 +0000 | [diff] [blame] | 13301 | if (mng_info->adjoin) |
glennrp | d908de4 | 2010-07-28 13:28:27 +0000 | [diff] [blame] | 13302 | { |
| 13303 | final_delay=10; |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 13304 | (void) ThrowMagickException(exception,GetMagickModule(), |
| 13305 | CoderWarning, |
glennrp | d908de4 | 2010-07-28 13:28:27 +0000 | [diff] [blame] | 13306 | "input has zero delay between all frames; assuming", |
| 13307 | " 10 cs `%s'",""); |
| 13308 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13309 | } |
| 13310 | else |
| 13311 | mng_info->ticks_per_second=0; |
| 13312 | } |
| 13313 | if (final_delay != 0) |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 13314 | mng_info->ticks_per_second=(png_uint_32) |
| 13315 | (image->ticks_per_second/final_delay); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13316 | if (final_delay > 50) |
| 13317 | mng_info->ticks_per_second=2; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13318 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13319 | if (final_delay > 75) |
| 13320 | mng_info->ticks_per_second=1; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13321 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13322 | if (final_delay > 125) |
| 13323 | mng_info->need_fram=MagickTrue; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13324 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13325 | if (need_defi && final_delay > 2 && (final_delay != 4) && |
| 13326 | (final_delay != 5) && (final_delay != 10) && (final_delay != 20) && |
| 13327 | (final_delay != 25) && (final_delay != 50) && (final_delay != |
| 13328 | 1UL*image->ticks_per_second)) |
| 13329 | mng_info->need_fram=MagickTrue; /* make it exact; cannot be VLC */ |
| 13330 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13331 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13332 | if (mng_info->need_fram != MagickFalse) |
| 13333 | mng_info->ticks_per_second=1UL*image->ticks_per_second; |
| 13334 | /* |
| 13335 | If pseudocolor, we should also check to see if all the |
| 13336 | palettes are identical and write a global PLTE if they are. |
| 13337 | ../glennrp Feb 99. |
| 13338 | */ |
| 13339 | /* |
| 13340 | Write the MNG version 1.0 signature and MHDR chunk. |
| 13341 | */ |
| 13342 | (void) WriteBlob(image,8,(const unsigned char *) "\212MNG\r\n\032\n"); |
| 13343 | (void) WriteBlobMSBULong(image,28L); /* chunk data length=28 */ |
| 13344 | PNGType(chunk,mng_MHDR); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 13345 | LogPNGChunk(logging,mng_MHDR,28L); |
cristy | 4e5bc84 | 2010-06-09 13:56:01 +0000 | [diff] [blame] | 13346 | PNGLong(chunk+4,(png_uint_32) mng_info->page.width); |
| 13347 | PNGLong(chunk+8,(png_uint_32) mng_info->page.height); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13348 | PNGLong(chunk+12,mng_info->ticks_per_second); |
| 13349 | PNGLong(chunk+16,0L); /* layer count=unknown */ |
| 13350 | PNGLong(chunk+20,0L); /* frame count=unknown */ |
| 13351 | PNGLong(chunk+24,0L); /* play time=unknown */ |
| 13352 | if (write_jng) |
| 13353 | { |
| 13354 | if (need_matte) |
| 13355 | { |
| 13356 | if (need_defi || mng_info->need_fram || use_global_plte) |
| 13357 | PNGLong(chunk+28,27L); /* simplicity=LC+JNG */ |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13358 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13359 | else |
| 13360 | PNGLong(chunk+28,25L); /* simplicity=VLC+JNG */ |
| 13361 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13362 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13363 | else |
| 13364 | { |
| 13365 | if (need_defi || mng_info->need_fram || use_global_plte) |
| 13366 | PNGLong(chunk+28,19L); /* simplicity=LC+JNG, no transparency */ |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13367 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13368 | else |
| 13369 | PNGLong(chunk+28,17L); /* simplicity=VLC+JNG, no transparency */ |
| 13370 | } |
| 13371 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13372 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13373 | else |
| 13374 | { |
| 13375 | if (need_matte) |
| 13376 | { |
| 13377 | if (need_defi || mng_info->need_fram || use_global_plte) |
| 13378 | PNGLong(chunk+28,11L); /* simplicity=LC */ |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13379 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13380 | else |
| 13381 | PNGLong(chunk+28,9L); /* simplicity=VLC */ |
| 13382 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13383 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13384 | else |
| 13385 | { |
| 13386 | if (need_defi || mng_info->need_fram || use_global_plte) |
| 13387 | PNGLong(chunk+28,3L); /* simplicity=LC, no transparency */ |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13388 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13389 | else |
| 13390 | PNGLong(chunk+28,1L); /* simplicity=VLC, no transparency */ |
| 13391 | } |
| 13392 | } |
| 13393 | (void) WriteBlob(image,32,chunk); |
| 13394 | (void) WriteBlobMSBULong(image,crc32(0,chunk,32)); |
cristy | 092ec8d | 2013-04-26 13:46:22 +0000 | [diff] [blame] | 13395 | option=GetImageOption(image_info,"mng:need-cacheoff"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13396 | if (option != (const char *) NULL) |
| 13397 | { |
| 13398 | size_t |
| 13399 | length; |
| 13400 | |
| 13401 | /* |
| 13402 | Write "nEED CACHEOFF" to turn playback caching off for streaming MNG. |
| 13403 | */ |
| 13404 | PNGType(chunk,mng_nEED); |
| 13405 | length=CopyMagickString((char *) chunk+4,"CACHEOFF",20); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 13406 | (void) WriteBlobMSBULong(image,(size_t) length); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 13407 | LogPNGChunk(logging,mng_nEED,(size_t) length); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13408 | length+=4; |
| 13409 | (void) WriteBlob(image,length,chunk); |
| 13410 | (void) WriteBlobMSBULong(image,crc32(0,chunk,(uInt) length)); |
| 13411 | } |
| 13412 | if ((GetPreviousImageInList(image) == (Image *) NULL) && |
| 13413 | (GetNextImageInList(image) != (Image *) NULL) && |
| 13414 | (image->iterations != 1)) |
| 13415 | { |
| 13416 | /* |
| 13417 | Write MNG TERM chunk |
| 13418 | */ |
| 13419 | (void) WriteBlobMSBULong(image,10L); /* data length=10 */ |
| 13420 | PNGType(chunk,mng_TERM); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 13421 | LogPNGChunk(logging,mng_TERM,10L); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13422 | chunk[4]=3; /* repeat animation */ |
| 13423 | chunk[5]=0; /* show last frame when done */ |
| 13424 | PNGLong(chunk+6,(png_uint_32) (mng_info->ticks_per_second* |
| 13425 | final_delay/MagickMax(image->ticks_per_second,1))); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13426 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13427 | if (image->iterations == 0) |
| 13428 | PNGLong(chunk+10,PNG_UINT_31_MAX); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13429 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13430 | else |
| 13431 | PNGLong(chunk+10,(png_uint_32) image->iterations); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13432 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13433 | if (logging != MagickFalse) |
| 13434 | { |
| 13435 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 13436 | " TERM delay: %.20g",(double) (mng_info->ticks_per_second* |
| 13437 | final_delay/MagickMax(image->ticks_per_second,1))); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13438 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13439 | if (image->iterations == 0) |
| 13440 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 13441 | " TERM iterations: %.20g",(double) PNG_UINT_31_MAX); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13442 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13443 | else |
| 13444 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 13445 | " Image iterations: %.20g",(double) image->iterations); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13446 | } |
| 13447 | (void) WriteBlob(image,14,chunk); |
| 13448 | (void) WriteBlobMSBULong(image,crc32(0,chunk,14)); |
| 13449 | } |
| 13450 | /* |
| 13451 | To do: check for cHRM+gAMA == sRGB, and write sRGB instead. |
| 13452 | */ |
| 13453 | if ((image->colorspace == sRGBColorspace || image->rendering_intent) && |
| 13454 | mng_info->equal_srgbs) |
| 13455 | { |
| 13456 | /* |
| 13457 | Write MNG sRGB chunk |
| 13458 | */ |
| 13459 | (void) WriteBlobMSBULong(image,1L); |
| 13460 | PNGType(chunk,mng_sRGB); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 13461 | LogPNGChunk(logging,mng_sRGB,1L); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13462 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13463 | if (image->rendering_intent != UndefinedIntent) |
glennrp | e610a07 | 2010-08-05 17:08:46 +0000 | [diff] [blame] | 13464 | chunk[4]=(unsigned char) |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 13465 | Magick_RenderingIntent_to_PNG_RenderingIntent( |
glennrp | e610a07 | 2010-08-05 17:08:46 +0000 | [diff] [blame] | 13466 | (image->rendering_intent)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13467 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13468 | else |
glennrp | e610a07 | 2010-08-05 17:08:46 +0000 | [diff] [blame] | 13469 | chunk[4]=(unsigned char) |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 13470 | Magick_RenderingIntent_to_PNG_RenderingIntent( |
| 13471 | (PerceptualIntent)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13472 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13473 | (void) WriteBlob(image,5,chunk); |
| 13474 | (void) WriteBlobMSBULong(image,crc32(0,chunk,5)); |
| 13475 | mng_info->have_write_global_srgb=MagickTrue; |
| 13476 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13477 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13478 | else |
| 13479 | { |
| 13480 | if (image->gamma && mng_info->equal_gammas) |
| 13481 | { |
| 13482 | /* |
| 13483 | Write MNG gAMA chunk |
| 13484 | */ |
| 13485 | (void) WriteBlobMSBULong(image,4L); |
| 13486 | PNGType(chunk,mng_gAMA); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 13487 | LogPNGChunk(logging,mng_gAMA,4L); |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 13488 | PNGLong(chunk+4,(png_uint_32) (100000*image->gamma+0.5)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13489 | (void) WriteBlob(image,8,chunk); |
| 13490 | (void) WriteBlobMSBULong(image,crc32(0,chunk,8)); |
| 13491 | mng_info->have_write_global_gama=MagickTrue; |
| 13492 | } |
| 13493 | if (mng_info->equal_chrms) |
| 13494 | { |
| 13495 | PrimaryInfo |
| 13496 | primary; |
| 13497 | |
| 13498 | /* |
| 13499 | Write MNG cHRM chunk |
| 13500 | */ |
| 13501 | (void) WriteBlobMSBULong(image,32L); |
| 13502 | PNGType(chunk,mng_cHRM); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 13503 | LogPNGChunk(logging,mng_cHRM,32L); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13504 | primary=image->chromaticity.white_point; |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 13505 | PNGLong(chunk+4,(png_uint_32) (100000*primary.x+0.5)); |
| 13506 | PNGLong(chunk+8,(png_uint_32) (100000*primary.y+0.5)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13507 | primary=image->chromaticity.red_primary; |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 13508 | PNGLong(chunk+12,(png_uint_32) (100000*primary.x+0.5)); |
| 13509 | PNGLong(chunk+16,(png_uint_32) (100000*primary.y+0.5)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13510 | primary=image->chromaticity.green_primary; |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 13511 | PNGLong(chunk+20,(png_uint_32) (100000*primary.x+0.5)); |
| 13512 | PNGLong(chunk+24,(png_uint_32) (100000*primary.y+0.5)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13513 | primary=image->chromaticity.blue_primary; |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 13514 | PNGLong(chunk+28,(png_uint_32) (100000*primary.x+0.5)); |
| 13515 | PNGLong(chunk+32,(png_uint_32) (100000*primary.y+0.5)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13516 | (void) WriteBlob(image,36,chunk); |
| 13517 | (void) WriteBlobMSBULong(image,crc32(0,chunk,36)); |
| 13518 | mng_info->have_write_global_chrm=MagickTrue; |
| 13519 | } |
| 13520 | } |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 13521 | if (image->resolution.x && image->resolution.y && mng_info->equal_physs) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13522 | { |
| 13523 | /* |
| 13524 | Write MNG pHYs chunk |
| 13525 | */ |
| 13526 | (void) WriteBlobMSBULong(image,9L); |
| 13527 | PNGType(chunk,mng_pHYs); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 13528 | LogPNGChunk(logging,mng_pHYs,9L); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13529 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13530 | if (image->units == PixelsPerInchResolution) |
| 13531 | { |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 13532 | PNGLong(chunk+4,(png_uint_32) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 13533 | (image->resolution.x*100.0/2.54+0.5)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13534 | |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 13535 | PNGLong(chunk+8,(png_uint_32) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 13536 | (image->resolution.y*100.0/2.54+0.5)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13537 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13538 | chunk[12]=1; |
| 13539 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13540 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13541 | else |
| 13542 | { |
| 13543 | if (image->units == PixelsPerCentimeterResolution) |
| 13544 | { |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 13545 | PNGLong(chunk+4,(png_uint_32) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 13546 | (image->resolution.x*100.0+0.5)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13547 | |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 13548 | PNGLong(chunk+8,(png_uint_32) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 13549 | (image->resolution.y*100.0+0.5)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13550 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13551 | chunk[12]=1; |
| 13552 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13553 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13554 | else |
| 13555 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 13556 | PNGLong(chunk+4,(png_uint_32) (image->resolution.x+0.5)); |
| 13557 | PNGLong(chunk+8,(png_uint_32) (image->resolution.y+0.5)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13558 | chunk[12]=0; |
| 13559 | } |
| 13560 | } |
| 13561 | (void) WriteBlob(image,13,chunk); |
| 13562 | (void) WriteBlobMSBULong(image,crc32(0,chunk,13)); |
| 13563 | } |
| 13564 | /* |
| 13565 | Write MNG BACK chunk and global bKGD chunk, if the image is transparent |
| 13566 | or does not cover the entire frame. |
| 13567 | */ |
cristy | dc2d327 | 2013-02-12 14:00:44 +0000 | [diff] [blame] | 13568 | if (write_mng && ((image->alpha_trait == BlendPixelTrait) || |
| 13569 | image->page.x > 0 || image->page.y > 0 || (image->page.width && |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13570 | (image->page.width+image->page.x < mng_info->page.width)) |
| 13571 | || (image->page.height && (image->page.height+image->page.y |
| 13572 | < mng_info->page.height)))) |
| 13573 | { |
| 13574 | (void) WriteBlobMSBULong(image,6L); |
| 13575 | PNGType(chunk,mng_BACK); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 13576 | LogPNGChunk(logging,mng_BACK,6L); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13577 | red=ScaleQuantumToShort(image->background_color.red); |
| 13578 | green=ScaleQuantumToShort(image->background_color.green); |
| 13579 | blue=ScaleQuantumToShort(image->background_color.blue); |
| 13580 | PNGShort(chunk+4,red); |
| 13581 | PNGShort(chunk+6,green); |
| 13582 | PNGShort(chunk+8,blue); |
| 13583 | (void) WriteBlob(image,10,chunk); |
| 13584 | (void) WriteBlobMSBULong(image,crc32(0,chunk,10)); |
| 13585 | if (mng_info->equal_backgrounds) |
| 13586 | { |
| 13587 | (void) WriteBlobMSBULong(image,6L); |
| 13588 | PNGType(chunk,mng_bKGD); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 13589 | LogPNGChunk(logging,mng_bKGD,6L); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13590 | (void) WriteBlob(image,10,chunk); |
| 13591 | (void) WriteBlobMSBULong(image,crc32(0,chunk,10)); |
| 13592 | } |
| 13593 | } |
| 13594 | |
| 13595 | #ifdef PNG_WRITE_EMPTY_PLTE_SUPPORTED |
| 13596 | if ((need_local_plte == MagickFalse) && |
| 13597 | (image->storage_class == PseudoClass) && |
| 13598 | (all_images_are_gray == MagickFalse)) |
| 13599 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 13600 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13601 | data_length; |
| 13602 | |
| 13603 | /* |
| 13604 | Write MNG PLTE chunk |
| 13605 | */ |
| 13606 | data_length=3*image->colors; |
| 13607 | (void) WriteBlobMSBULong(image,data_length); |
| 13608 | PNGType(chunk,mng_PLTE); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 13609 | LogPNGChunk(logging,mng_PLTE,data_length); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13610 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 13611 | for (i=0; i < (ssize_t) image->colors; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13612 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 13613 | chunk[4+i*3]=(unsigned char) (ScaleQuantumToChar( |
| 13614 | image->colormap[i].red) & 0xff); |
| 13615 | chunk[5+i*3]=(unsigned char) (ScaleQuantumToChar( |
| 13616 | image->colormap[i].green) & 0xff); |
| 13617 | chunk[6+i*3]=(unsigned char) (ScaleQuantumToChar( |
| 13618 | image->colormap[i].blue) & 0xff); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13619 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13620 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13621 | (void) WriteBlob(image,data_length+4,chunk); |
| 13622 | (void) WriteBlobMSBULong(image,crc32(0,chunk,(uInt) (data_length+4))); |
| 13623 | mng_info->have_write_global_plte=MagickTrue; |
| 13624 | } |
| 13625 | #endif |
| 13626 | } |
| 13627 | scene=0; |
| 13628 | mng_info->delay=0; |
| 13629 | #if defined(PNG_WRITE_EMPTY_PLTE_SUPPORTED) || \ |
| 13630 | defined(PNG_MNG_FEATURES_SUPPORTED) |
| 13631 | mng_info->equal_palettes=MagickFalse; |
| 13632 | #endif |
| 13633 | do |
| 13634 | { |
| 13635 | if (mng_info->adjoin) |
| 13636 | { |
| 13637 | #if defined(PNG_WRITE_EMPTY_PLTE_SUPPORTED) || \ |
| 13638 | defined(PNG_MNG_FEATURES_SUPPORTED) |
| 13639 | /* |
| 13640 | If we aren't using a global palette for the entire MNG, check to |
| 13641 | see if we can use one for two or more consecutive images. |
| 13642 | */ |
| 13643 | if (need_local_plte && use_global_plte && !all_images_are_gray) |
| 13644 | { |
| 13645 | if (mng_info->IsPalette) |
| 13646 | { |
| 13647 | /* |
| 13648 | When equal_palettes is true, this image has the same palette |
| 13649 | as the previous PseudoClass image |
| 13650 | */ |
| 13651 | mng_info->have_write_global_plte=mng_info->equal_palettes; |
| 13652 | mng_info->equal_palettes=PalettesAreEqual(image,image->next); |
| 13653 | if (mng_info->equal_palettes && !mng_info->have_write_global_plte) |
| 13654 | { |
| 13655 | /* |
| 13656 | Write MNG PLTE chunk |
| 13657 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 13658 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13659 | data_length; |
| 13660 | |
| 13661 | data_length=3*image->colors; |
| 13662 | (void) WriteBlobMSBULong(image,data_length); |
| 13663 | PNGType(chunk,mng_PLTE); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 13664 | LogPNGChunk(logging,mng_PLTE,data_length); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13665 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 13666 | for (i=0; i < (ssize_t) image->colors; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13667 | { |
| 13668 | chunk[4+i*3]=ScaleQuantumToChar(image->colormap[i].red); |
| 13669 | chunk[5+i*3]=ScaleQuantumToChar(image->colormap[i].green); |
| 13670 | chunk[6+i*3]=ScaleQuantumToChar(image->colormap[i].blue); |
| 13671 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13672 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13673 | (void) WriteBlob(image,data_length+4,chunk); |
| 13674 | (void) WriteBlobMSBULong(image,crc32(0,chunk, |
| 13675 | (uInt) (data_length+4))); |
| 13676 | mng_info->have_write_global_plte=MagickTrue; |
| 13677 | } |
| 13678 | } |
| 13679 | else |
| 13680 | mng_info->have_write_global_plte=MagickFalse; |
| 13681 | } |
| 13682 | #endif |
| 13683 | if (need_defi) |
| 13684 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 13685 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13686 | previous_x, |
| 13687 | previous_y; |
| 13688 | |
| 13689 | if (scene) |
| 13690 | { |
| 13691 | previous_x=mng_info->page.x; |
| 13692 | previous_y=mng_info->page.y; |
| 13693 | } |
| 13694 | else |
| 13695 | { |
| 13696 | previous_x=0; |
| 13697 | previous_y=0; |
| 13698 | } |
| 13699 | mng_info->page=image->page; |
| 13700 | if ((mng_info->page.x != previous_x) || |
| 13701 | (mng_info->page.y != previous_y)) |
| 13702 | { |
| 13703 | (void) WriteBlobMSBULong(image,12L); /* data length=12 */ |
| 13704 | PNGType(chunk,mng_DEFI); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 13705 | LogPNGChunk(logging,mng_DEFI,12L); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13706 | chunk[4]=0; /* object 0 MSB */ |
| 13707 | chunk[5]=0; /* object 0 LSB */ |
| 13708 | chunk[6]=0; /* visible */ |
| 13709 | chunk[7]=0; /* abstract */ |
| 13710 | PNGLong(chunk+8,(png_uint_32) mng_info->page.x); |
| 13711 | PNGLong(chunk+12,(png_uint_32) mng_info->page.y); |
| 13712 | (void) WriteBlob(image,16,chunk); |
| 13713 | (void) WriteBlobMSBULong(image,crc32(0,chunk,16)); |
| 13714 | } |
| 13715 | } |
| 13716 | } |
| 13717 | |
| 13718 | mng_info->write_mng=write_mng; |
| 13719 | |
| 13720 | if ((int) image->dispose >= 3) |
| 13721 | mng_info->framing_mode=3; |
| 13722 | |
| 13723 | if (mng_info->need_fram && mng_info->adjoin && |
| 13724 | ((image->delay != mng_info->delay) || |
| 13725 | (mng_info->framing_mode != mng_info->old_framing_mode))) |
| 13726 | { |
| 13727 | if (image->delay == mng_info->delay) |
| 13728 | { |
| 13729 | /* |
| 13730 | Write a MNG FRAM chunk with the new framing mode. |
| 13731 | */ |
| 13732 | (void) WriteBlobMSBULong(image,1L); /* data length=1 */ |
| 13733 | PNGType(chunk,mng_FRAM); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 13734 | LogPNGChunk(logging,mng_FRAM,1L); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13735 | chunk[4]=(unsigned char) mng_info->framing_mode; |
| 13736 | (void) WriteBlob(image,5,chunk); |
| 13737 | (void) WriteBlobMSBULong(image,crc32(0,chunk,5)); |
| 13738 | } |
| 13739 | else |
| 13740 | { |
| 13741 | /* |
| 13742 | Write a MNG FRAM chunk with the delay. |
| 13743 | */ |
| 13744 | (void) WriteBlobMSBULong(image,10L); /* data length=10 */ |
| 13745 | PNGType(chunk,mng_FRAM); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 13746 | LogPNGChunk(logging,mng_FRAM,10L); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13747 | chunk[4]=(unsigned char) mng_info->framing_mode; |
| 13748 | chunk[5]=0; /* frame name separator (no name) */ |
| 13749 | chunk[6]=2; /* flag for changing default delay */ |
| 13750 | chunk[7]=0; /* flag for changing frame timeout */ |
| 13751 | chunk[8]=0; /* flag for changing frame clipping */ |
| 13752 | chunk[9]=0; /* flag for changing frame sync_id */ |
| 13753 | PNGLong(chunk+10,(png_uint_32) |
| 13754 | ((mng_info->ticks_per_second* |
| 13755 | image->delay)/MagickMax(image->ticks_per_second,1))); |
| 13756 | (void) WriteBlob(image,14,chunk); |
| 13757 | (void) WriteBlobMSBULong(image,crc32(0,chunk,14)); |
cristy | 4e5bc84 | 2010-06-09 13:56:01 +0000 | [diff] [blame] | 13758 | mng_info->delay=(png_uint_32) image->delay; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13759 | } |
| 13760 | mng_info->old_framing_mode=mng_info->framing_mode; |
| 13761 | } |
| 13762 | |
| 13763 | #if defined(JNG_SUPPORTED) |
| 13764 | if (image_info->compression == JPEGCompression) |
| 13765 | { |
| 13766 | ImageInfo |
| 13767 | *write_info; |
| 13768 | |
| 13769 | if (logging != MagickFalse) |
| 13770 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 13771 | " Writing JNG object."); |
| 13772 | /* To do: specify the desired alpha compression method. */ |
| 13773 | write_info=CloneImageInfo(image_info); |
| 13774 | write_info->compression=UndefinedCompression; |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 13775 | status=WriteOneJNGImage(mng_info,write_info,image,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13776 | write_info=DestroyImageInfo(write_info); |
| 13777 | } |
| 13778 | else |
| 13779 | #endif |
| 13780 | { |
| 13781 | if (logging != MagickFalse) |
| 13782 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 13783 | " Writing PNG object."); |
glennrp | 2f2e514 | 2010-12-23 19:13:35 +0000 | [diff] [blame] | 13784 | |
glennrp | b9cfe27 | 2010-12-21 15:08:06 +0000 | [diff] [blame] | 13785 | mng_info->need_blob = MagickFalse; |
glennrp | 8d3d6e5 | 2011-04-19 04:39:51 +0000 | [diff] [blame] | 13786 | mng_info->ping_preserve_colormap = MagickFalse; |
glennrp | 2f2e514 | 2010-12-23 19:13:35 +0000 | [diff] [blame] | 13787 | |
| 13788 | /* We don't want any ancillary chunks written */ |
| 13789 | mng_info->ping_exclude_bKGD=MagickTrue; |
| 13790 | mng_info->ping_exclude_cHRM=MagickTrue; |
glennrp | a0ed009 | 2011-04-18 16:36:29 +0000 | [diff] [blame] | 13791 | mng_info->ping_exclude_date=MagickTrue; |
glennrp | 2f2e514 | 2010-12-23 19:13:35 +0000 | [diff] [blame] | 13792 | mng_info->ping_exclude_EXIF=MagickTrue; |
| 13793 | mng_info->ping_exclude_gAMA=MagickTrue; |
glennrp | 2f2e514 | 2010-12-23 19:13:35 +0000 | [diff] [blame] | 13794 | mng_info->ping_exclude_iCCP=MagickTrue; |
| 13795 | /* mng_info->ping_exclude_iTXt=MagickTrue; */ |
| 13796 | mng_info->ping_exclude_oFFs=MagickTrue; |
| 13797 | mng_info->ping_exclude_pHYs=MagickTrue; |
| 13798 | mng_info->ping_exclude_sRGB=MagickTrue; |
| 13799 | mng_info->ping_exclude_tEXt=MagickTrue; |
glennrp | a1e3b7b | 2010-12-24 16:37:33 +0000 | [diff] [blame] | 13800 | mng_info->ping_exclude_tRNS=MagickTrue; |
glennrp | 2f2e514 | 2010-12-23 19:13:35 +0000 | [diff] [blame] | 13801 | mng_info->ping_exclude_vpAg=MagickTrue; |
| 13802 | mng_info->ping_exclude_zCCP=MagickTrue; |
| 13803 | mng_info->ping_exclude_zTXt=MagickTrue; |
| 13804 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 13805 | status=WriteOnePNGImage(mng_info,image_info,image,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13806 | } |
| 13807 | |
| 13808 | if (status == MagickFalse) |
| 13809 | { |
| 13810 | MngInfoFreeStruct(mng_info,&have_mng_structure); |
| 13811 | (void) CloseBlob(image); |
| 13812 | return(MagickFalse); |
| 13813 | } |
| 13814 | (void) CatchImageException(image); |
| 13815 | if (GetNextImageInList(image) == (Image *) NULL) |
| 13816 | break; |
| 13817 | image=SyncNextImageInList(image); |
| 13818 | status=SetImageProgress(image,SaveImagesTag,scene++, |
| 13819 | GetImageListLength(image)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13820 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13821 | if (status == MagickFalse) |
| 13822 | break; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13823 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13824 | } while (mng_info->adjoin); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13825 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13826 | if (write_mng) |
| 13827 | { |
| 13828 | while (GetPreviousImageInList(image) != (Image *) NULL) |
| 13829 | image=GetPreviousImageInList(image); |
| 13830 | /* |
| 13831 | Write the MEND chunk. |
| 13832 | */ |
| 13833 | (void) WriteBlobMSBULong(image,0x00000000L); |
| 13834 | PNGType(chunk,mng_MEND); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 13835 | LogPNGChunk(logging,mng_MEND,0L); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13836 | (void) WriteBlob(image,4,chunk); |
| 13837 | (void) WriteBlobMSBULong(image,crc32(0,chunk,4)); |
| 13838 | } |
| 13839 | /* |
| 13840 | Relinquish resources. |
| 13841 | */ |
| 13842 | (void) CloseBlob(image); |
| 13843 | MngInfoFreeStruct(mng_info,&have_mng_structure); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13844 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13845 | if (logging != MagickFalse) |
| 13846 | (void) LogMagickEvent(CoderEvent,GetMagickModule(),"exit WriteMNGImage()"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13847 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13848 | return(MagickTrue); |
| 13849 | } |
glennrp | d5045b4 | 2010-03-24 12:40:35 +0000 | [diff] [blame] | 13850 | #else /* PNG_LIBPNG_VER > 10011 */ |
glennrp | 39992b4 | 2010-11-14 00:03:43 +0000 | [diff] [blame] | 13851 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13852 | static MagickBooleanType WritePNGImage(const ImageInfo *image_info,Image *image) |
| 13853 | { |
glennrp | 3bd393f | 2011-12-21 18:54:53 +0000 | [diff] [blame] | 13854 | (void) image; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13855 | printf("Your PNG library is too old: You have libpng-%s\n", |
| 13856 | PNG_LIBPNG_VER_STRING); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13857 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13858 | ThrowBinaryException(CoderError,"PNG library is too old", |
| 13859 | image_info->filename); |
| 13860 | } |
glennrp | 39992b4 | 2010-11-14 00:03:43 +0000 | [diff] [blame] | 13861 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13862 | static MagickBooleanType WriteMNGImage(const ImageInfo *image_info,Image *image) |
| 13863 | { |
| 13864 | return(WritePNGImage(image_info,image)); |
| 13865 | } |
glennrp | d5045b4 | 2010-03-24 12:40:35 +0000 | [diff] [blame] | 13866 | #endif /* PNG_LIBPNG_VER > 10011 */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13867 | #endif |