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 | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 132 | /* Macros for left-bit-replication to ensure that pixels |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 133 | * and PixelInfos all have the same image->depth, and for use |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 134 | * in PNG8 quantization. |
| 135 | */ |
| 136 | |
| 137 | |
| 138 | /* LBR01: Replicate top bit */ |
| 139 | |
glennrp | 05001c3 | 2011-08-06 13:04:16 +0000 | [diff] [blame] | 140 | #define LBR01PacketRed(pixelpacket) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 141 | (pixelpacket).red=(ScaleQuantumToChar((pixelpacket).red) < 0x10 ? \ |
| 142 | 0 : QuantumRange); |
| 143 | |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 144 | #define LBR01PacketGreen(pixelpacket) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 145 | (pixelpacket).green=(ScaleQuantumToChar((pixelpacket).green) < 0x10 ? \ |
| 146 | 0 : QuantumRange); |
| 147 | |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 148 | #define LBR01PacketBlue(pixelpacket) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 149 | (pixelpacket).blue=(ScaleQuantumToChar((pixelpacket).blue) < 0x10 ? \ |
| 150 | 0 : QuantumRange); |
| 151 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 152 | #define LBR01PacketAlpha(pixelpacket) \ |
| 153 | (pixelpacket).alpha=(ScaleQuantumToChar((pixelpacket).alpha) < 0x10 ? \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 154 | 0 : QuantumRange); |
| 155 | |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 156 | #define LBR01PacketRGB(pixelpacket) \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 157 | { \ |
glennrp | 05001c3 | 2011-08-06 13:04:16 +0000 | [diff] [blame] | 158 | LBR01PacketRed((pixelpacket)); \ |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 159 | LBR01PacketGreen((pixelpacket)); \ |
| 160 | LBR01PacketBlue((pixelpacket)); \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 161 | } |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 162 | |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 163 | #define LBR01PacketRGBO(pixelpacket) \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 164 | { \ |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 165 | LBR01PacketRGB((pixelpacket)); \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 166 | LBR01PacketAlpha((pixelpacket)); \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 167 | } |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 168 | |
cristy | ef61831 | 2011-06-25 12:26:44 +0000 | [diff] [blame] | 169 | #define LBR01PixelRed(pixel) \ |
glennrp | 360c154 | 2013-01-06 01:21:46 +0000 | [diff] [blame] | 170 | (SetPixelRed(image, \ |
| 171 | ScaleQuantumToChar(GetPixelRed(image,(pixel))) < 0x10 ? \ |
glennrp | c7375f9 | 2013-01-06 01:32:16 +0000 | [diff] [blame] | 172 | 0 : QuantumRange,(pixel))); |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 173 | |
glennrp | 54cf797 | 2011-08-06 14:28:09 +0000 | [diff] [blame] | 174 | #define LBR01PixelGreen(pixel) \ |
glennrp | 360c154 | 2013-01-06 01:21:46 +0000 | [diff] [blame] | 175 | (SetPixelGreen(image, \ |
| 176 | ScaleQuantumToChar(GetPixelGreen(image,(pixel))) < 0x10 ? \ |
glennrp | c7375f9 | 2013-01-06 01:32:16 +0000 | [diff] [blame] | 177 | 0 : QuantumRange,(pixel))); |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 178 | |
glennrp | 54cf797 | 2011-08-06 14:28:09 +0000 | [diff] [blame] | 179 | #define LBR01PixelBlue(pixel) \ |
glennrp | 360c154 | 2013-01-06 01:21:46 +0000 | [diff] [blame] | 180 | (SetPixelBlue(image, \ |
| 181 | ScaleQuantumToChar(GetPixelBlue(image,(pixel))) < 0x10 ? \ |
glennrp | c7375f9 | 2013-01-06 01:32:16 +0000 | [diff] [blame] | 182 | 0 : QuantumRange,(pixel))); |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 183 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 184 | #define LBR01PixelAlpha(pixel) \ |
glennrp | 360c154 | 2013-01-06 01:21:46 +0000 | [diff] [blame] | 185 | (SetPixelAlpha(image, \ |
| 186 | ScaleQuantumToChar(GetPixelAlpha(image,(pixel))) < 0x10 ? \ |
glennrp | c7375f9 | 2013-01-06 01:32:16 +0000 | [diff] [blame] | 187 | 0 : QuantumRange,(pixel))); |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 188 | |
glennrp | 54cf797 | 2011-08-06 14:28:09 +0000 | [diff] [blame] | 189 | #define LBR01PixelRGB(pixel) \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 190 | { \ |
cristy | ef61831 | 2011-06-25 12:26:44 +0000 | [diff] [blame] | 191 | LBR01PixelRed((pixel)); \ |
glennrp | 54cf797 | 2011-08-06 14:28:09 +0000 | [diff] [blame] | 192 | LBR01PixelGreen((pixel)); \ |
| 193 | LBR01PixelBlue((pixel)); \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 194 | } |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 195 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 196 | #define LBR01PixelRGBA(pixel) \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 197 | { \ |
glennrp | 54cf797 | 2011-08-06 14:28:09 +0000 | [diff] [blame] | 198 | LBR01PixelRGB((pixel)); \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 199 | LBR01PixelAlpha((pixel)); \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 200 | } |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 201 | |
| 202 | /* LBR02: Replicate top 2 bits */ |
| 203 | |
glennrp | 05001c3 | 2011-08-06 13:04:16 +0000 | [diff] [blame] | 204 | #define LBR02PacketRed(pixelpacket) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 205 | { \ |
| 206 | unsigned char lbr_bits=ScaleQuantumToChar((pixelpacket).red) & 0xc0; \ |
| 207 | (pixelpacket).red=ScaleCharToQuantum( \ |
| 208 | (lbr_bits | (lbr_bits >> 2) | (lbr_bits >> 4) | (lbr_bits >> 6))); \ |
| 209 | } |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 210 | #define LBR02PacketGreen(pixelpacket) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 211 | { \ |
| 212 | unsigned char lbr_bits=ScaleQuantumToChar((pixelpacket).green) & 0xc0; \ |
| 213 | (pixelpacket).green=ScaleCharToQuantum( \ |
| 214 | (lbr_bits | (lbr_bits >> 2) | (lbr_bits >> 4) | (lbr_bits >> 6))); \ |
| 215 | } |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 216 | #define LBR02PacketBlue(pixelpacket) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 217 | { \ |
| 218 | unsigned char lbr_bits=ScaleQuantumToChar((pixelpacket).blue) & 0xc0; \ |
| 219 | (pixelpacket).blue=ScaleCharToQuantum( \ |
| 220 | (lbr_bits | (lbr_bits >> 2) | (lbr_bits >> 4) | (lbr_bits >> 6))); \ |
| 221 | } |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 222 | #define LBR02PacketAlpha(pixelpacket) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 223 | { \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 224 | unsigned char lbr_bits=ScaleQuantumToChar((pixelpacket).alpha) & 0xc0; \ |
| 225 | (pixelpacket).alpha=ScaleCharToQuantum( \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 226 | (lbr_bits | (lbr_bits >> 2) | (lbr_bits >> 4) | (lbr_bits >> 6))); \ |
| 227 | } |
| 228 | |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 229 | #define LBR02PacketRGB(pixelpacket) \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 230 | { \ |
glennrp | 05001c3 | 2011-08-06 13:04:16 +0000 | [diff] [blame] | 231 | LBR02PacketRed((pixelpacket)); \ |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 232 | LBR02PacketGreen((pixelpacket)); \ |
| 233 | LBR02PacketBlue((pixelpacket)); \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 234 | } |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 235 | |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 236 | #define LBR02PacketRGBO(pixelpacket) \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 237 | { \ |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 238 | LBR02PacketRGB((pixelpacket)); \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 239 | LBR02PacketAlpha((pixelpacket)); \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 240 | } |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 241 | |
cristy | ef61831 | 2011-06-25 12:26:44 +0000 | [diff] [blame] | 242 | #define LBR02PixelRed(pixel) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 243 | { \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 244 | unsigned char lbr_bits=ScaleQuantumToChar(GetPixelRed(image,(pixel))) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 245 | & 0xc0; \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 246 | SetPixelRed(image, ScaleCharToQuantum( \ |
| 247 | (lbr_bits | (lbr_bits >> 2) | (lbr_bits >> 4) | (lbr_bits >> 6))), \ |
| 248 | (pixel)); \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 249 | } |
glennrp | 54cf797 | 2011-08-06 14:28:09 +0000 | [diff] [blame] | 250 | #define LBR02PixelGreen(pixel) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 251 | { \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 252 | unsigned char lbr_bits=ScaleQuantumToChar(GetPixelGreen(image,(pixel)))\ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 253 | & 0xc0; \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 254 | SetPixelGreen(image, ScaleCharToQuantum( \ |
| 255 | (lbr_bits | (lbr_bits >> 2) | (lbr_bits >> 4) | (lbr_bits >> 6))), \ |
| 256 | (pixel)); \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 257 | } |
glennrp | 54cf797 | 2011-08-06 14:28:09 +0000 | [diff] [blame] | 258 | #define LBR02PixelBlue(pixel) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 259 | { \ |
| 260 | unsigned char lbr_bits= \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 261 | ScaleQuantumToChar(GetPixelBlue(image,(pixel))) & 0xc0; \ |
| 262 | SetPixelBlue(image, ScaleCharToQuantum( \ |
| 263 | (lbr_bits | (lbr_bits >> 2) | (lbr_bits >> 4) | (lbr_bits >> 6))), \ |
| 264 | (pixel)); \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 265 | } |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 266 | #define LBR02PixelAlpha(pixel) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 267 | { \ |
| 268 | unsigned char lbr_bits= \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 269 | ScaleQuantumToChar(GetPixelAlpha(image,(pixel))) & 0xc0; \ |
| 270 | SetPixelAlpha(image, ScaleCharToQuantum( \ |
| 271 | (lbr_bits | (lbr_bits >> 2) | (lbr_bits >> 4) | (lbr_bits >> 6))), \ |
| 272 | (pixel) ); \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 273 | } |
| 274 | |
glennrp | 54cf797 | 2011-08-06 14:28:09 +0000 | [diff] [blame] | 275 | #define LBR02PixelRGB(pixel) \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 276 | { \ |
cristy | ef61831 | 2011-06-25 12:26:44 +0000 | [diff] [blame] | 277 | LBR02PixelRed((pixel)); \ |
glennrp | 54cf797 | 2011-08-06 14:28:09 +0000 | [diff] [blame] | 278 | LBR02PixelGreen((pixel)); \ |
| 279 | LBR02PixelBlue((pixel)); \ |
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 | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 282 | #define LBR02PixelRGBA(pixel) \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 283 | { \ |
glennrp | 54cf797 | 2011-08-06 14:28:09 +0000 | [diff] [blame] | 284 | LBR02PixelRGB((pixel)); \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 285 | LBR02PixelAlpha((pixel)); \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 286 | } |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 287 | |
| 288 | /* LBR03: Replicate top 3 bits (only used with opaque pixels during |
| 289 | PNG8 quantization) */ |
| 290 | |
glennrp | 05001c3 | 2011-08-06 13:04:16 +0000 | [diff] [blame] | 291 | #define LBR03PacketRed(pixelpacket) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 292 | { \ |
| 293 | unsigned char lbr_bits=ScaleQuantumToChar((pixelpacket).red) & 0xe0; \ |
| 294 | (pixelpacket).red=ScaleCharToQuantum( \ |
| 295 | (lbr_bits | (lbr_bits >> 3) | (lbr_bits >> 6))); \ |
| 296 | } |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 297 | #define LBR03PacketGreen(pixelpacket) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 298 | { \ |
| 299 | unsigned char lbr_bits=ScaleQuantumToChar((pixelpacket).green) & 0xe0; \ |
| 300 | (pixelpacket).green=ScaleCharToQuantum( \ |
| 301 | (lbr_bits | (lbr_bits >> 3) | (lbr_bits >> 6))); \ |
| 302 | } |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 303 | #define LBR03PacketBlue(pixelpacket) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 304 | { \ |
| 305 | unsigned char lbr_bits=ScaleQuantumToChar((pixelpacket).blue) & 0xe0; \ |
| 306 | (pixelpacket).blue=ScaleCharToQuantum( \ |
| 307 | (lbr_bits | (lbr_bits >> 3) | (lbr_bits >> 6))); \ |
| 308 | } |
| 309 | |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 310 | #define LBR03PacketRGB(pixelpacket) \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 311 | { \ |
glennrp | 05001c3 | 2011-08-06 13:04:16 +0000 | [diff] [blame] | 312 | LBR03PacketRed((pixelpacket)); \ |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 313 | LBR03PacketGreen((pixelpacket)); \ |
| 314 | LBR03PacketBlue((pixelpacket)); \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 315 | } |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 316 | |
cristy | ef61831 | 2011-06-25 12:26:44 +0000 | [diff] [blame] | 317 | #define LBR03PixelRed(pixel) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 318 | { \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 319 | unsigned char lbr_bits=ScaleQuantumToChar(GetPixelRed(image,(pixel))) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 320 | & 0xe0; \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 321 | SetPixelRed(image, ScaleCharToQuantum( \ |
| 322 | (lbr_bits | (lbr_bits >> 3) | (lbr_bits >> 6))), (pixel)); \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 323 | } |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 324 | #define LBR03Green(pixel) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 325 | { \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 326 | unsigned char lbr_bits=ScaleQuantumToChar(GetPixelGreen(image,(pixel)))\ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 327 | & 0xe0; \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 328 | SetPixelGreen(image, ScaleCharToQuantum( \ |
| 329 | (lbr_bits | (lbr_bits >> 3) | (lbr_bits >> 6))), (pixel)); \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 330 | } |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 331 | #define LBR03Blue(pixel) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 332 | { \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 333 | unsigned char lbr_bits=ScaleQuantumToChar(GetPixelBlue(image,(pixel))) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 334 | & 0xe0; \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 335 | SetPixelBlue(image, ScaleCharToQuantum( \ |
| 336 | (lbr_bits | (lbr_bits >> 3) | (lbr_bits >> 6))), (pixel)); \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 337 | } |
| 338 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 339 | #define LBR03RGB(pixel) \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 340 | { \ |
cristy | ef61831 | 2011-06-25 12:26:44 +0000 | [diff] [blame] | 341 | LBR03PixelRed((pixel)); \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 342 | LBR03Green((pixel)); \ |
| 343 | LBR03Blue((pixel)); \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 344 | } |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 345 | |
| 346 | /* LBR04: Replicate top 4 bits */ |
| 347 | |
glennrp | 05001c3 | 2011-08-06 13:04:16 +0000 | [diff] [blame] | 348 | #define LBR04PacketRed(pixelpacket) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 349 | { \ |
| 350 | unsigned char lbr_bits=ScaleQuantumToChar((pixelpacket).red) & 0xf0; \ |
| 351 | (pixelpacket).red=ScaleCharToQuantum((lbr_bits | (lbr_bits >> 4))); \ |
| 352 | } |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 353 | #define LBR04PacketGreen(pixelpacket) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 354 | { \ |
| 355 | unsigned char lbr_bits=ScaleQuantumToChar((pixelpacket).green) & 0xf0; \ |
| 356 | (pixelpacket).green=ScaleCharToQuantum((lbr_bits | (lbr_bits >> 4))); \ |
| 357 | } |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 358 | #define LBR04PacketBlue(pixelpacket) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 359 | { \ |
| 360 | unsigned char lbr_bits=ScaleQuantumToChar((pixelpacket).blue) & 0xf0; \ |
| 361 | (pixelpacket).blue=ScaleCharToQuantum((lbr_bits | (lbr_bits >> 4))); \ |
| 362 | } |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 363 | #define LBR04PacketAlpha(pixelpacket) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 364 | { \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 365 | unsigned char lbr_bits=ScaleQuantumToChar((pixelpacket).alpha) & 0xf0; \ |
| 366 | (pixelpacket).alpha=ScaleCharToQuantum((lbr_bits | (lbr_bits >> 4))); \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 367 | } |
| 368 | |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 369 | #define LBR04PacketRGB(pixelpacket) \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 370 | { \ |
glennrp | 05001c3 | 2011-08-06 13:04:16 +0000 | [diff] [blame] | 371 | LBR04PacketRed((pixelpacket)); \ |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 372 | LBR04PacketGreen((pixelpacket)); \ |
| 373 | LBR04PacketBlue((pixelpacket)); \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 374 | } |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 375 | |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 376 | #define LBR04PacketRGBO(pixelpacket) \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 377 | { \ |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 378 | LBR04PacketRGB((pixelpacket)); \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 379 | LBR04PacketAlpha((pixelpacket)); \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 380 | } |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 381 | |
cristy | ef61831 | 2011-06-25 12:26:44 +0000 | [diff] [blame] | 382 | #define LBR04PixelRed(pixel) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 383 | { \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 384 | unsigned char lbr_bits=ScaleQuantumToChar(GetPixelRed(image,(pixel))) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 385 | & 0xf0; \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 386 | SetPixelRed(image,\ |
| 387 | ScaleCharToQuantum((lbr_bits | (lbr_bits >> 4))), (pixel)); \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 388 | } |
glennrp | 54cf797 | 2011-08-06 14:28:09 +0000 | [diff] [blame] | 389 | #define LBR04PixelGreen(pixel) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 390 | { \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 391 | unsigned char lbr_bits=ScaleQuantumToChar(GetPixelGreen(image,(pixel)))\ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 392 | & 0xf0; \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 393 | SetPixelGreen(image,\ |
| 394 | ScaleCharToQuantum((lbr_bits | (lbr_bits >> 4))), (pixel)); \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 395 | } |
glennrp | 54cf797 | 2011-08-06 14:28:09 +0000 | [diff] [blame] | 396 | #define LBR04PixelBlue(pixel) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 397 | { \ |
| 398 | unsigned char lbr_bits= \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 399 | ScaleQuantumToChar(GetPixelBlue(image,(pixel))) & 0xf0; \ |
| 400 | SetPixelBlue(image,\ |
| 401 | ScaleCharToQuantum((lbr_bits | (lbr_bits >> 4))), (pixel)); \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 402 | } |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 403 | #define LBR04PixelAlpha(pixel) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 404 | { \ |
| 405 | unsigned char lbr_bits= \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 406 | ScaleQuantumToChar(GetPixelAlpha(image,(pixel))) & 0xf0; \ |
| 407 | SetPixelAlpha(image,\ |
| 408 | ScaleCharToQuantum((lbr_bits | (lbr_bits >> 4))), (pixel)); \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 409 | } |
| 410 | |
glennrp | 54cf797 | 2011-08-06 14:28:09 +0000 | [diff] [blame] | 411 | #define LBR04PixelRGB(pixel) \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 412 | { \ |
cristy | ef61831 | 2011-06-25 12:26:44 +0000 | [diff] [blame] | 413 | LBR04PixelRed((pixel)); \ |
glennrp | 54cf797 | 2011-08-06 14:28:09 +0000 | [diff] [blame] | 414 | LBR04PixelGreen((pixel)); \ |
| 415 | LBR04PixelBlue((pixel)); \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 416 | } |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 417 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 418 | #define LBR04PixelRGBA(pixel) \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 419 | { \ |
glennrp | 54cf797 | 2011-08-06 14:28:09 +0000 | [diff] [blame] | 420 | LBR04PixelRGB((pixel)); \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 421 | LBR04PixelAlpha((pixel)); \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 422 | } |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 423 | |
| 424 | |
glennrp | 1145899 | 2013-01-08 18:12:52 +0000 | [diff] [blame] | 425 | #if MAGICKCORE_QUANTUM_DEPTH > 8 |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 426 | /* LBR08: Replicate top 8 bits */ |
| 427 | |
glennrp | 05001c3 | 2011-08-06 13:04:16 +0000 | [diff] [blame] | 428 | #define LBR08PacketRed(pixelpacket) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 429 | { \ |
| 430 | unsigned char lbr_bits=ScaleQuantumToChar((pixelpacket).red); \ |
| 431 | (pixelpacket).red=ScaleCharToQuantum((lbr_bits)); \ |
| 432 | } |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 433 | #define LBR08PacketGreen(pixelpacket) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 434 | { \ |
| 435 | unsigned char lbr_bits=ScaleQuantumToChar((pixelpacket).green); \ |
| 436 | (pixelpacket).green=ScaleCharToQuantum((lbr_bits)); \ |
| 437 | } |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 438 | #define LBR08PacketBlue(pixelpacket) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 439 | { \ |
| 440 | unsigned char lbr_bits=ScaleQuantumToChar((pixelpacket).blue); \ |
| 441 | (pixelpacket).blue=ScaleCharToQuantum((lbr_bits)); \ |
| 442 | } |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 443 | #define LBR08PacketAlpha(pixelpacket) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 444 | { \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 445 | unsigned char lbr_bits=ScaleQuantumToChar((pixelpacket).alpha); \ |
| 446 | (pixelpacket).alpha=ScaleCharToQuantum((lbr_bits)); \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 447 | } |
| 448 | |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 449 | #define LBR08PacketRGB(pixelpacket) \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 450 | { \ |
glennrp | 05001c3 | 2011-08-06 13:04:16 +0000 | [diff] [blame] | 451 | LBR08PacketRed((pixelpacket)); \ |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 452 | LBR08PacketGreen((pixelpacket)); \ |
| 453 | LBR08PacketBlue((pixelpacket)); \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 454 | } |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 455 | |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 456 | #define LBR08PacketRGBO(pixelpacket) \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 457 | { \ |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 458 | LBR08PacketRGB((pixelpacket)); \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 459 | LBR08PacketAlpha((pixelpacket)); \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 460 | } |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 461 | |
cristy | ef61831 | 2011-06-25 12:26:44 +0000 | [diff] [blame] | 462 | #define LBR08PixelRed(pixel) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 463 | { \ |
| 464 | unsigned char lbr_bits= \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 465 | ScaleQuantumToChar(GetPixelRed(image,(pixel))); \ |
| 466 | SetPixelRed(image,\ |
| 467 | ScaleCharToQuantum((lbr_bits)), (pixel)); \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 468 | } |
glennrp | 54cf797 | 2011-08-06 14:28:09 +0000 | [diff] [blame] | 469 | #define LBR08PixelGreen(pixel) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 470 | { \ |
| 471 | unsigned char lbr_bits= \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 472 | ScaleQuantumToChar(GetPixelGreen(image,(pixel))); \ |
| 473 | SetPixelGreen(image,\ |
| 474 | ScaleCharToQuantum((lbr_bits)), (pixel)); \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 475 | } |
glennrp | 54cf797 | 2011-08-06 14:28:09 +0000 | [diff] [blame] | 476 | #define LBR08PixelBlue(pixel) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 477 | { \ |
| 478 | unsigned char lbr_bits= \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 479 | ScaleQuantumToChar(GetPixelBlue(image,(pixel))); \ |
| 480 | SetPixelBlue(image,\ |
| 481 | ScaleCharToQuantum((lbr_bits)), (pixel)); \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 482 | } |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 483 | #define LBR08PixelAlpha(pixel) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 484 | { \ |
| 485 | unsigned char lbr_bits= \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 486 | ScaleQuantumToChar(GetPixelAlpha(image,(pixel))); \ |
| 487 | SetPixelAlpha(image,\ |
| 488 | ScaleCharToQuantum((lbr_bits)), (pixel)); \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 489 | } |
| 490 | |
glennrp | 54cf797 | 2011-08-06 14:28:09 +0000 | [diff] [blame] | 491 | #define LBR08PixelRGB(pixel) \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 492 | { \ |
cristy | ef61831 | 2011-06-25 12:26:44 +0000 | [diff] [blame] | 493 | LBR08PixelRed((pixel)); \ |
glennrp | 54cf797 | 2011-08-06 14:28:09 +0000 | [diff] [blame] | 494 | LBR08PixelGreen((pixel)); \ |
| 495 | LBR08PixelBlue((pixel)); \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 496 | } |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 497 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 498 | #define LBR08PixelRGBA(pixel) \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 499 | { \ |
glennrp | 54cf797 | 2011-08-06 14:28:09 +0000 | [diff] [blame] | 500 | LBR08PixelRGB((pixel)); \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 501 | LBR08PixelAlpha((pixel)); \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 502 | } |
glennrp | 1145899 | 2013-01-08 18:12:52 +0000 | [diff] [blame] | 503 | #endif /* MAGICKCORE_QUANTUM_DEPTH > 8 */ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 504 | |
| 505 | |
glennrp | 15f0766 | 2013-01-08 18:08:13 +0000 | [diff] [blame] | 506 | #if MAGICKCORE_QUANTUM_DEPTH > 16 |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 507 | /* LBR16: Replicate top 16 bits */ |
| 508 | |
glennrp | 05001c3 | 2011-08-06 13:04:16 +0000 | [diff] [blame] | 509 | #define LBR16PacketRed(pixelpacket) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 510 | { \ |
| 511 | unsigned short lbr_bits=ScaleQuantumToShort((pixelpacket).red); \ |
| 512 | (pixelpacket).red=ScaleShortToQuantum((lbr_bits)); \ |
| 513 | } |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 514 | #define LBR16PacketGreen(pixelpacket) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 515 | { \ |
| 516 | unsigned short lbr_bits=ScaleQuantumToShort((pixelpacket).green); \ |
| 517 | (pixelpacket).green=ScaleShortToQuantum((lbr_bits)); \ |
| 518 | } |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 519 | #define LBR16PacketBlue(pixelpacket) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 520 | { \ |
| 521 | unsigned short lbr_bits=ScaleQuantumToShort((pixelpacket).blue); \ |
| 522 | (pixelpacket).blue=ScaleShortToQuantum((lbr_bits)); \ |
| 523 | } |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 524 | #define LBR16PacketAlpha(pixelpacket) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 525 | { \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 526 | unsigned short lbr_bits=ScaleQuantumToShort((pixelpacket).alpha); \ |
| 527 | (pixelpacket).alpha=ScaleShortToQuantum((lbr_bits)); \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 528 | } |
| 529 | |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 530 | #define LBR16PacketRGB(pixelpacket) \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 531 | { \ |
glennrp | 05001c3 | 2011-08-06 13:04:16 +0000 | [diff] [blame] | 532 | LBR16PacketRed((pixelpacket)); \ |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 533 | LBR16PacketGreen((pixelpacket)); \ |
| 534 | LBR16PacketBlue((pixelpacket)); \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 535 | } |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 536 | |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 537 | #define LBR16PacketRGBO(pixelpacket) \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 538 | { \ |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 539 | LBR16PacketRGB((pixelpacket)); \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 540 | LBR16PacketAlpha((pixelpacket)); \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 541 | } |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 542 | |
cristy | ef61831 | 2011-06-25 12:26:44 +0000 | [diff] [blame] | 543 | #define LBR16PixelRed(pixel) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 544 | { \ |
| 545 | unsigned short lbr_bits= \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 546 | ScaleQuantumToShort(GetPixelRed(image,(pixel))); \ |
| 547 | SetPixelRed(image,\ |
| 548 | ScaleShortToQuantum((lbr_bits)),(pixel)); \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 549 | } |
glennrp | 54cf797 | 2011-08-06 14:28:09 +0000 | [diff] [blame] | 550 | #define LBR16PixelGreen(pixel) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 551 | { \ |
| 552 | unsigned short lbr_bits= \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 553 | ScaleQuantumToShort(GetPixelGreen(image,(pixel))); \ |
| 554 | SetPixelGreen(image,\ |
| 555 | ScaleShortToQuantum((lbr_bits)),(pixel)); \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 556 | } |
glennrp | 54cf797 | 2011-08-06 14:28:09 +0000 | [diff] [blame] | 557 | #define LBR16PixelBlue(pixel) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 558 | { \ |
| 559 | unsigned short lbr_bits= \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 560 | ScaleQuantumToShort(GetPixelBlue(image,(pixel))); \ |
| 561 | SetPixelBlue(image,\ |
| 562 | ScaleShortToQuantum((lbr_bits)),(pixel)); \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 563 | } |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 564 | #define LBR16PixelAlpha(pixel) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 565 | { \ |
| 566 | unsigned short lbr_bits= \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 567 | ScaleQuantumToShort(GetPixelAlpha(image,(pixel))); \ |
| 568 | SetPixelAlpha(image,\ |
| 569 | ScaleShortToQuantum((lbr_bits)),(pixel)); \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 570 | } |
| 571 | |
glennrp | 54cf797 | 2011-08-06 14:28:09 +0000 | [diff] [blame] | 572 | #define LBR16PixelRGB(pixel) \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 573 | { \ |
cristy | ef61831 | 2011-06-25 12:26:44 +0000 | [diff] [blame] | 574 | LBR16PixelRed((pixel)); \ |
glennrp | 54cf797 | 2011-08-06 14:28:09 +0000 | [diff] [blame] | 575 | LBR16PixelGreen((pixel)); \ |
| 576 | LBR16PixelBlue((pixel)); \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 577 | } |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 578 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 579 | #define LBR16PixelRGBA(pixel) \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 580 | { \ |
glennrp | 54cf797 | 2011-08-06 14:28:09 +0000 | [diff] [blame] | 581 | LBR16PixelRGB((pixel)); \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 582 | LBR16PixelAlpha((pixel)); \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 583 | } |
glennrp | 15f0766 | 2013-01-08 18:08:13 +0000 | [diff] [blame] | 584 | #endif /* MAGICKCORE_QUANTUM_DEPTH > 16 */ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 585 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 586 | /* |
| 587 | Establish thread safety. |
| 588 | setjmp/longjmp is claimed to be safe on these platforms: |
| 589 | setjmp/longjmp is alleged to be unsafe on these platforms: |
| 590 | */ |
| 591 | #ifndef SETJMP_IS_THREAD_SAFE |
| 592 | #define PNG_SETJMP_NOT_THREAD_SAFE |
| 593 | #endif |
| 594 | |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 595 | #ifdef PNG_SETJMP_NOT_THREAD_SAFE |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 596 | static SemaphoreInfo |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 597 | *ping_semaphore = (SemaphoreInfo *) NULL; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 598 | #endif |
| 599 | |
| 600 | /* |
| 601 | This temporary until I set up malloc'ed object attributes array. |
| 602 | Recompile with MNG_MAX_OBJECTS=65536L to avoid this limit but |
| 603 | waste more memory. |
| 604 | */ |
| 605 | #define MNG_MAX_OBJECTS 256 |
| 606 | |
| 607 | /* |
| 608 | If this not defined, spec is interpreted strictly. If it is |
| 609 | defined, an attempt will be made to recover from some errors, |
| 610 | including |
| 611 | o global PLTE too short |
| 612 | */ |
| 613 | #undef MNG_LOOSE |
| 614 | |
| 615 | /* |
| 616 | Don't try to define PNG_MNG_FEATURES_SUPPORTED here. Make sure |
| 617 | it's defined in libpng/pngconf.h, version 1.0.9 or later. It won't work |
| 618 | with earlier versions of libpng. From libpng-1.0.3a to libpng-1.0.8, |
| 619 | PNG_READ|WRITE_EMPTY_PLTE were used but those have been deprecated in |
| 620 | libpng in favor of PNG_MNG_FEATURES_SUPPORTED, so we set them here. |
| 621 | PNG_MNG_FEATURES_SUPPORTED is disabled by default in libpng-1.0.9 and |
| 622 | will be enabled by default in libpng-1.2.0. |
| 623 | */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 624 | #ifdef PNG_MNG_FEATURES_SUPPORTED |
| 625 | # ifndef PNG_READ_EMPTY_PLTE_SUPPORTED |
| 626 | # define PNG_READ_EMPTY_PLTE_SUPPORTED |
| 627 | # endif |
| 628 | # ifndef PNG_WRITE_EMPTY_PLTE_SUPPORTED |
| 629 | # define PNG_WRITE_EMPTY_PLTE_SUPPORTED |
| 630 | # endif |
| 631 | #endif |
| 632 | |
| 633 | /* |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 634 | Maximum valid size_t in PNG/MNG chunks is (2^31)-1 |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 635 | This macro is only defined in libpng-1.0.3 and later. |
| 636 | Previously it was PNG_MAX_UINT but that was deprecated in libpng-1.2.6 |
| 637 | */ |
| 638 | #ifndef PNG_UINT_31_MAX |
| 639 | #define PNG_UINT_31_MAX (png_uint_32) 0x7fffffffL |
| 640 | #endif |
| 641 | |
| 642 | /* |
| 643 | Constant strings for known chunk types. If you need to add a chunk, |
| 644 | add a string holding the name here. To make the code more |
| 645 | portable, we use ASCII numbers like this, not characters. |
| 646 | */ |
| 647 | |
glennrp | 85dcf87 | 2011-12-07 02:51:47 +0000 | [diff] [blame] | 648 | static png_byte mng_MHDR[5]={ 77, 72, 68, 82, (png_byte) '\0'}; |
| 649 | static png_byte mng_BACK[5]={ 66, 65, 67, 75, (png_byte) '\0'}; |
| 650 | static png_byte mng_BASI[5]={ 66, 65, 83, 73, (png_byte) '\0'}; |
| 651 | static png_byte mng_CLIP[5]={ 67, 76, 73, 80, (png_byte) '\0'}; |
| 652 | static png_byte mng_CLON[5]={ 67, 76, 79, 78, (png_byte) '\0'}; |
| 653 | static png_byte mng_DEFI[5]={ 68, 69, 70, 73, (png_byte) '\0'}; |
| 654 | static png_byte mng_DHDR[5]={ 68, 72, 68, 82, (png_byte) '\0'}; |
| 655 | static png_byte mng_DISC[5]={ 68, 73, 83, 67, (png_byte) '\0'}; |
| 656 | static png_byte mng_ENDL[5]={ 69, 78, 68, 76, (png_byte) '\0'}; |
| 657 | static png_byte mng_FRAM[5]={ 70, 82, 65, 77, (png_byte) '\0'}; |
| 658 | static png_byte mng_IEND[5]={ 73, 69, 78, 68, (png_byte) '\0'}; |
| 659 | static png_byte mng_IHDR[5]={ 73, 72, 68, 82, (png_byte) '\0'}; |
| 660 | static png_byte mng_JHDR[5]={ 74, 72, 68, 82, (png_byte) '\0'}; |
| 661 | static png_byte mng_LOOP[5]={ 76, 79, 79, 80, (png_byte) '\0'}; |
| 662 | static png_byte mng_MAGN[5]={ 77, 65, 71, 78, (png_byte) '\0'}; |
| 663 | static png_byte mng_MEND[5]={ 77, 69, 78, 68, (png_byte) '\0'}; |
| 664 | static png_byte mng_MOVE[5]={ 77, 79, 86, 69, (png_byte) '\0'}; |
| 665 | static png_byte mng_PAST[5]={ 80, 65, 83, 84, (png_byte) '\0'}; |
| 666 | static png_byte mng_PLTE[5]={ 80, 76, 84, 69, (png_byte) '\0'}; |
| 667 | static png_byte mng_SAVE[5]={ 83, 65, 86, 69, (png_byte) '\0'}; |
| 668 | static png_byte mng_SEEK[5]={ 83, 69, 69, 75, (png_byte) '\0'}; |
| 669 | static png_byte mng_SHOW[5]={ 83, 72, 79, 87, (png_byte) '\0'}; |
| 670 | static png_byte mng_TERM[5]={ 84, 69, 82, 77, (png_byte) '\0'}; |
| 671 | static png_byte mng_bKGD[5]={ 98, 75, 71, 68, (png_byte) '\0'}; |
| 672 | static png_byte mng_cHRM[5]={ 99, 72, 82, 77, (png_byte) '\0'}; |
| 673 | static png_byte mng_gAMA[5]={103, 65, 77, 65, (png_byte) '\0'}; |
| 674 | static png_byte mng_iCCP[5]={105, 67, 67, 80, (png_byte) '\0'}; |
| 675 | static png_byte mng_nEED[5]={110, 69, 69, 68, (png_byte) '\0'}; |
| 676 | static png_byte mng_pHYg[5]={112, 72, 89, 103, (png_byte) '\0'}; |
| 677 | static png_byte mng_vpAg[5]={118, 112, 65, 103, (png_byte) '\0'}; |
| 678 | static png_byte mng_pHYs[5]={112, 72, 89, 115, (png_byte) '\0'}; |
| 679 | static png_byte mng_sBIT[5]={115, 66, 73, 84, (png_byte) '\0'}; |
| 680 | static png_byte mng_sRGB[5]={115, 82, 71, 66, (png_byte) '\0'}; |
| 681 | static png_byte mng_tRNS[5]={116, 82, 78, 83, (png_byte) '\0'}; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 682 | |
| 683 | #if defined(JNG_SUPPORTED) |
glennrp | 85dcf87 | 2011-12-07 02:51:47 +0000 | [diff] [blame] | 684 | static png_byte mng_IDAT[5]={ 73, 68, 65, 84, (png_byte) '\0'}; |
| 685 | static png_byte mng_JDAT[5]={ 74, 68, 65, 84, (png_byte) '\0'}; |
| 686 | static png_byte mng_JDAA[5]={ 74, 68, 65, 65, (png_byte) '\0'}; |
| 687 | static png_byte mng_JdAA[5]={ 74, 100, 65, 65, (png_byte) '\0'}; |
| 688 | static png_byte mng_JSEP[5]={ 74, 83, 69, 80, (png_byte) '\0'}; |
| 689 | static png_byte mng_oFFs[5]={111, 70, 70, 115, (png_byte) '\0'}; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 690 | #endif |
| 691 | |
| 692 | /* |
| 693 | Other known chunks that are not yet supported by ImageMagick: |
glennrp | 85dcf87 | 2011-12-07 02:51:47 +0000 | [diff] [blame] | 694 | static png_byte mng_hIST[5]={104, 73, 83, 84, (png_byte) '\0'}; |
| 695 | static png_byte mng_iCCP[5]={105, 67, 67, 80, (png_byte) '\0'}; |
| 696 | static png_byte mng_iTXt[5]={105, 84, 88, 116, (png_byte) '\0'}; |
| 697 | static png_byte mng_sPLT[5]={115, 80, 76, 84, (png_byte) '\0'}; |
| 698 | static png_byte mng_sTER[5]={115, 84, 69, 82, (png_byte) '\0'}; |
| 699 | static png_byte mng_tEXt[5]={116, 69, 88, 116, (png_byte) '\0'}; |
| 700 | static png_byte mng_tIME[5]={116, 73, 77, 69, (png_byte) '\0'}; |
| 701 | static png_byte mng_zTXt[5]={122, 84, 88, 116, (png_byte) '\0'}; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 702 | */ |
| 703 | |
| 704 | typedef struct _MngBox |
| 705 | { |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 706 | long |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 707 | left, |
| 708 | right, |
| 709 | top, |
| 710 | bottom; |
| 711 | } MngBox; |
| 712 | |
| 713 | typedef struct _MngPair |
| 714 | { |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 715 | volatile long |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 716 | a, |
| 717 | b; |
| 718 | } MngPair; |
| 719 | |
| 720 | #ifdef MNG_OBJECT_BUFFERS |
| 721 | typedef struct _MngBuffer |
| 722 | { |
| 723 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 724 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 725 | height, |
| 726 | width; |
| 727 | |
| 728 | Image |
| 729 | *image; |
| 730 | |
| 731 | png_color |
| 732 | plte[256]; |
| 733 | |
| 734 | int |
| 735 | reference_count; |
| 736 | |
| 737 | unsigned char |
| 738 | alpha_sample_depth, |
| 739 | compression_method, |
| 740 | color_type, |
| 741 | concrete, |
| 742 | filter_method, |
| 743 | frozen, |
| 744 | image_type, |
| 745 | interlace_method, |
| 746 | pixel_sample_depth, |
| 747 | plte_length, |
| 748 | sample_depth, |
| 749 | viewable; |
| 750 | } MngBuffer; |
| 751 | #endif |
| 752 | |
| 753 | typedef struct _MngInfo |
| 754 | { |
| 755 | |
| 756 | #ifdef MNG_OBJECT_BUFFERS |
| 757 | MngBuffer |
| 758 | *ob[MNG_MAX_OBJECTS]; |
| 759 | #endif |
| 760 | |
| 761 | Image * |
| 762 | image; |
| 763 | |
| 764 | RectangleInfo |
| 765 | page; |
| 766 | |
| 767 | int |
| 768 | adjoin, |
| 769 | #ifndef PNG_READ_EMPTY_PLTE_SUPPORTED |
| 770 | bytes_in_read_buffer, |
| 771 | found_empty_plte, |
| 772 | #endif |
| 773 | equal_backgrounds, |
| 774 | equal_chrms, |
| 775 | equal_gammas, |
| 776 | #if defined(PNG_WRITE_EMPTY_PLTE_SUPPORTED) || \ |
| 777 | defined(PNG_MNG_FEATURES_SUPPORTED) |
| 778 | equal_palettes, |
| 779 | #endif |
| 780 | equal_physs, |
| 781 | equal_srgbs, |
| 782 | framing_mode, |
| 783 | have_global_bkgd, |
| 784 | have_global_chrm, |
| 785 | have_global_gama, |
| 786 | have_global_phys, |
| 787 | have_global_sbit, |
| 788 | have_global_srgb, |
| 789 | have_saved_bkgd_index, |
| 790 | have_write_global_chrm, |
| 791 | have_write_global_gama, |
| 792 | have_write_global_plte, |
| 793 | have_write_global_srgb, |
| 794 | need_fram, |
| 795 | object_id, |
| 796 | old_framing_mode, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 797 | saved_bkgd_index; |
| 798 | |
| 799 | int |
| 800 | new_number_colors; |
| 801 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 802 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 803 | image_found, |
| 804 | loop_count[256], |
| 805 | loop_iteration[256], |
| 806 | scenes_found, |
| 807 | x_off[MNG_MAX_OBJECTS], |
| 808 | y_off[MNG_MAX_OBJECTS]; |
| 809 | |
| 810 | MngBox |
| 811 | clip, |
| 812 | frame, |
| 813 | image_box, |
| 814 | object_clip[MNG_MAX_OBJECTS]; |
| 815 | |
| 816 | unsigned char |
| 817 | /* These flags could be combined into one byte */ |
| 818 | exists[MNG_MAX_OBJECTS], |
| 819 | frozen[MNG_MAX_OBJECTS], |
| 820 | loop_active[256], |
| 821 | invisible[MNG_MAX_OBJECTS], |
| 822 | viewable[MNG_MAX_OBJECTS]; |
| 823 | |
| 824 | MagickOffsetType |
| 825 | loop_jump[256]; |
| 826 | |
| 827 | png_colorp |
| 828 | global_plte; |
| 829 | |
| 830 | png_color_8 |
| 831 | global_sbit; |
| 832 | |
| 833 | png_byte |
| 834 | #ifndef PNG_READ_EMPTY_PLTE_SUPPORTED |
| 835 | read_buffer[8], |
| 836 | #endif |
| 837 | global_trns[256]; |
| 838 | |
| 839 | float |
| 840 | global_gamma; |
| 841 | |
| 842 | ChromaticityInfo |
| 843 | global_chrm; |
| 844 | |
| 845 | RenderingIntent |
| 846 | global_srgb_intent; |
| 847 | |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 848 | unsigned int |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 849 | delay, |
| 850 | global_plte_length, |
| 851 | global_trns_length, |
| 852 | global_x_pixels_per_unit, |
| 853 | global_y_pixels_per_unit, |
| 854 | mng_width, |
| 855 | mng_height, |
| 856 | ticks_per_second; |
| 857 | |
glennrp | b9cfe27 | 2010-12-21 15:08:06 +0000 | [diff] [blame] | 858 | MagickBooleanType |
| 859 | need_blob; |
| 860 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 861 | unsigned int |
| 862 | IsPalette, |
| 863 | global_phys_unit_type, |
| 864 | basi_warning, |
| 865 | clon_warning, |
| 866 | dhdr_warning, |
| 867 | jhdr_warning, |
| 868 | magn_warning, |
| 869 | past_warning, |
| 870 | phyg_warning, |
| 871 | phys_warning, |
| 872 | sbit_warning, |
| 873 | show_warning, |
| 874 | mng_type, |
| 875 | write_mng, |
| 876 | write_png_colortype, |
| 877 | write_png_depth, |
glennrp | 1868258 | 2011-06-30 18:11:47 +0000 | [diff] [blame] | 878 | write_png_compression_level, |
| 879 | write_png_compression_strategy, |
| 880 | write_png_compression_filter, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 881 | write_png8, |
| 882 | write_png24, |
glennrp | fd164d2 | 2013-01-26 21:10:22 +0000 | [diff] [blame] | 883 | write_png32, |
| 884 | write_png48, |
| 885 | write_png64; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 886 | |
| 887 | #ifdef MNG_BASI_SUPPORTED |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 888 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 889 | basi_width, |
| 890 | basi_height; |
| 891 | |
| 892 | unsigned int |
| 893 | basi_depth, |
| 894 | basi_color_type, |
| 895 | basi_compression_method, |
| 896 | basi_filter_type, |
| 897 | basi_interlace_method, |
| 898 | basi_red, |
| 899 | basi_green, |
| 900 | basi_blue, |
| 901 | basi_alpha, |
| 902 | basi_viewable; |
| 903 | #endif |
| 904 | |
| 905 | png_uint_16 |
| 906 | magn_first, |
| 907 | magn_last, |
| 908 | magn_mb, |
| 909 | magn_ml, |
| 910 | magn_mr, |
| 911 | magn_mt, |
| 912 | magn_mx, |
| 913 | magn_my, |
| 914 | magn_methx, |
| 915 | magn_methy; |
| 916 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 917 | PixelInfo |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 918 | mng_global_bkgd; |
| 919 | |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 920 | /* Added at version 6.6.6-7 */ |
| 921 | MagickBooleanType |
| 922 | ping_exclude_bKGD, |
| 923 | ping_exclude_cHRM, |
glennrp | a0ed009 | 2011-04-18 16:36:29 +0000 | [diff] [blame] | 924 | ping_exclude_date, |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 925 | ping_exclude_EXIF, |
| 926 | ping_exclude_gAMA, |
| 927 | ping_exclude_iCCP, |
| 928 | /* ping_exclude_iTXt, */ |
| 929 | ping_exclude_oFFs, |
| 930 | ping_exclude_pHYs, |
| 931 | ping_exclude_sRGB, |
| 932 | ping_exclude_tEXt, |
glennrp | a1e3b7b | 2010-12-24 16:37:33 +0000 | [diff] [blame] | 933 | ping_exclude_tRNS, |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 934 | ping_exclude_vpAg, |
| 935 | ping_exclude_zCCP, /* hex-encoded iCCP */ |
glennrp | 8d3d6e5 | 2011-04-19 04:39:51 +0000 | [diff] [blame] | 936 | ping_exclude_zTXt, |
| 937 | ping_preserve_colormap; |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 938 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 939 | } MngInfo; |
| 940 | #endif /* VER */ |
| 941 | |
| 942 | /* |
| 943 | Forward declarations. |
| 944 | */ |
| 945 | static MagickBooleanType |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 946 | WritePNGImage(const ImageInfo *,Image *,ExceptionInfo *); |
glennrp | 0c3e06b | 2010-11-19 13:45:02 +0000 | [diff] [blame] | 947 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 948 | static MagickBooleanType |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 949 | WriteMNGImage(const ImageInfo *,Image *,ExceptionInfo *); |
glennrp | 0c3e06b | 2010-11-19 13:45:02 +0000 | [diff] [blame] | 950 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 951 | #if defined(JNG_SUPPORTED) |
| 952 | static MagickBooleanType |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 953 | WriteJNGImage(const ImageInfo *,Image *,ExceptionInfo *); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 954 | #endif |
| 955 | |
glennrp | 0c3e06b | 2010-11-19 13:45:02 +0000 | [diff] [blame] | 956 | #if PNG_LIBPNG_VER > 10011 |
| 957 | |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 958 | |
glennrp | 0c3e06b | 2010-11-19 13:45:02 +0000 | [diff] [blame] | 959 | #if (MAGICKCORE_QUANTUM_DEPTH >= 16) |
| 960 | static MagickBooleanType |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 961 | LosslessReduceDepthOK(Image *image,ExceptionInfo *exception) |
glennrp | 0c3e06b | 2010-11-19 13:45:02 +0000 | [diff] [blame] | 962 | { |
glennrp | 67b9c1a | 2011-04-22 18:47:36 +0000 | [diff] [blame] | 963 | /* Reduce bit depth if it can be reduced losslessly from 16+ to 8. |
| 964 | * |
| 965 | * This is true if the high byte and the next highest byte of |
| 966 | * each sample of the image, the colormap, and the background color |
glennrp | 3faa9a3 | 2011-04-23 14:00:25 +0000 | [diff] [blame] | 967 | * are equal to each other. We check this by seeing if the samples |
| 968 | * 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] | 969 | * |
| 970 | * We don't use the method GetImageDepth() because it doesn't check |
glennrp | 3faa9a3 | 2011-04-23 14:00:25 +0000 | [diff] [blame] | 971 | * background and doesn't handle PseudoClass specially. |
glennrp | 67b9c1a | 2011-04-22 18:47:36 +0000 | [diff] [blame] | 972 | */ |
| 973 | |
glennrp | 3faa9a3 | 2011-04-23 14:00:25 +0000 | [diff] [blame] | 974 | #define QuantumToCharToQuantumEqQuantum(quantum) \ |
| 975 | ((ScaleCharToQuantum((unsigned char) ScaleQuantumToChar(quantum))) == quantum) |
| 976 | |
glennrp | 0c3e06b | 2010-11-19 13:45:02 +0000 | [diff] [blame] | 977 | MagickBooleanType |
| 978 | ok_to_reduce=MagickFalse; |
glennrp | 0c3e06b | 2010-11-19 13:45:02 +0000 | [diff] [blame] | 979 | |
glennrp | 03e11f6 | 2011-04-22 13:30:16 +0000 | [diff] [blame] | 980 | if (image->depth >= 16) |
glennrp | 0c3e06b | 2010-11-19 13:45:02 +0000 | [diff] [blame] | 981 | { |
| 982 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 983 | const Quantum |
glennrp | 0c3e06b | 2010-11-19 13:45:02 +0000 | [diff] [blame] | 984 | *p; |
| 985 | |
| 986 | ok_to_reduce= |
glennrp | 3faa9a3 | 2011-04-23 14:00:25 +0000 | [diff] [blame] | 987 | QuantumToCharToQuantumEqQuantum(image->background_color.red) && |
| 988 | QuantumToCharToQuantumEqQuantum(image->background_color.green) && |
| 989 | QuantumToCharToQuantumEqQuantum(image->background_color.blue) ? |
| 990 | MagickTrue : MagickFalse; |
glennrp | 0c3e06b | 2010-11-19 13:45:02 +0000 | [diff] [blame] | 991 | |
| 992 | if (ok_to_reduce != MagickFalse && image->storage_class == PseudoClass) |
| 993 | { |
| 994 | int indx; |
| 995 | |
| 996 | for (indx=0; indx < (ssize_t) image->colors; indx++) |
| 997 | { |
glennrp | 3faa9a3 | 2011-04-23 14:00:25 +0000 | [diff] [blame] | 998 | ok_to_reduce=( |
| 999 | QuantumToCharToQuantumEqQuantum( |
| 1000 | image->colormap[indx].red) && |
| 1001 | QuantumToCharToQuantumEqQuantum( |
| 1002 | image->colormap[indx].green) && |
| 1003 | QuantumToCharToQuantumEqQuantum( |
| 1004 | image->colormap[indx].blue)) ? |
| 1005 | MagickTrue : MagickFalse; |
| 1006 | |
glennrp | 0c3e06b | 2010-11-19 13:45:02 +0000 | [diff] [blame] | 1007 | if (ok_to_reduce == MagickFalse) |
glennrp | 3faa9a3 | 2011-04-23 14:00:25 +0000 | [diff] [blame] | 1008 | break; |
glennrp | 0c3e06b | 2010-11-19 13:45:02 +0000 | [diff] [blame] | 1009 | } |
| 1010 | } |
| 1011 | |
| 1012 | if ((ok_to_reduce != MagickFalse) && |
| 1013 | (image->storage_class != PseudoClass)) |
| 1014 | { |
| 1015 | ssize_t |
| 1016 | y; |
| 1017 | |
| 1018 | register ssize_t |
| 1019 | x; |
| 1020 | |
| 1021 | for (y=0; y < (ssize_t) image->rows; y++) |
| 1022 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 1023 | p=GetVirtualPixels(image,0,y,image->columns,1,exception); |
glennrp | 0c3e06b | 2010-11-19 13:45:02 +0000 | [diff] [blame] | 1024 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 1025 | if (p == (const Quantum *) NULL) |
glennrp | 0c3e06b | 2010-11-19 13:45:02 +0000 | [diff] [blame] | 1026 | { |
| 1027 | ok_to_reduce = MagickFalse; |
| 1028 | break; |
| 1029 | } |
| 1030 | |
| 1031 | for (x=(ssize_t) image->columns-1; x >= 0; x--) |
| 1032 | { |
glennrp | 3faa9a3 | 2011-04-23 14:00:25 +0000 | [diff] [blame] | 1033 | ok_to_reduce= |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 1034 | QuantumToCharToQuantumEqQuantum(GetPixelRed(image,p)) && |
| 1035 | QuantumToCharToQuantumEqQuantum(GetPixelGreen(image,p)) && |
| 1036 | QuantumToCharToQuantumEqQuantum(GetPixelBlue(image,p)) ? |
glennrp | 3faa9a3 | 2011-04-23 14:00:25 +0000 | [diff] [blame] | 1037 | MagickTrue : MagickFalse; |
glennrp | 0c3e06b | 2010-11-19 13:45:02 +0000 | [diff] [blame] | 1038 | |
| 1039 | if (ok_to_reduce == MagickFalse) |
| 1040 | break; |
| 1041 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 1042 | p+=GetPixelChannels(image); |
glennrp | 0c3e06b | 2010-11-19 13:45:02 +0000 | [diff] [blame] | 1043 | } |
glennrp | 8640fb5 | 2010-11-23 15:48:26 +0000 | [diff] [blame] | 1044 | if (x >= 0) |
glennrp | 0c3e06b | 2010-11-19 13:45:02 +0000 | [diff] [blame] | 1045 | break; |
| 1046 | } |
| 1047 | } |
| 1048 | |
| 1049 | if (ok_to_reduce != MagickFalse) |
| 1050 | { |
glennrp | 0c3e06b | 2010-11-19 13:45:02 +0000 | [diff] [blame] | 1051 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 1052 | " OK to reduce PNG bit depth to 8 without loss of info"); |
glennrp | 0c3e06b | 2010-11-19 13:45:02 +0000 | [diff] [blame] | 1053 | } |
glennrp | a6a0663 | 2011-01-19 15:15:34 +0000 | [diff] [blame] | 1054 | else |
| 1055 | { |
| 1056 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 1057 | " Not OK to reduce PNG bit depth to 8 without loss of info"); |
glennrp | a6a0663 | 2011-01-19 15:15:34 +0000 | [diff] [blame] | 1058 | } |
glennrp | 0c3e06b | 2010-11-19 13:45:02 +0000 | [diff] [blame] | 1059 | } |
| 1060 | |
| 1061 | return ok_to_reduce; |
| 1062 | } |
| 1063 | #endif /* MAGICKCORE_QUANTUM_DEPTH >= 16 */ |
| 1064 | |
glennrp | 1a56e9c | 2012-04-25 17:06:57 +0000 | [diff] [blame] | 1065 | static const char* PngColorTypeToString(const unsigned int color_type) |
| 1066 | { |
| 1067 | const char |
| 1068 | *result = "Unknown"; |
| 1069 | |
| 1070 | switch (color_type) |
| 1071 | { |
| 1072 | case PNG_COLOR_TYPE_GRAY: |
| 1073 | result = "Gray"; |
| 1074 | break; |
| 1075 | case PNG_COLOR_TYPE_GRAY_ALPHA: |
| 1076 | result = "Gray+Alpha"; |
| 1077 | break; |
| 1078 | case PNG_COLOR_TYPE_PALETTE: |
| 1079 | result = "Palette"; |
| 1080 | break; |
| 1081 | case PNG_COLOR_TYPE_RGB: |
| 1082 | result = "RGB"; |
| 1083 | break; |
| 1084 | case PNG_COLOR_TYPE_RGB_ALPHA: |
| 1085 | result = "RGB+Alpha"; |
| 1086 | break; |
| 1087 | } |
| 1088 | |
| 1089 | return result; |
| 1090 | } |
| 1091 | |
glennrp | e610a07 | 2010-08-05 17:08:46 +0000 | [diff] [blame] | 1092 | static int |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 1093 | Magick_RenderingIntent_to_PNG_RenderingIntent(const RenderingIntent intent) |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1094 | { |
glennrp | e610a07 | 2010-08-05 17:08:46 +0000 | [diff] [blame] | 1095 | switch (intent) |
| 1096 | { |
| 1097 | case PerceptualIntent: |
| 1098 | return 0; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1099 | |
glennrp | e610a07 | 2010-08-05 17:08:46 +0000 | [diff] [blame] | 1100 | case RelativeIntent: |
| 1101 | return 1; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1102 | |
glennrp | e610a07 | 2010-08-05 17:08:46 +0000 | [diff] [blame] | 1103 | case SaturationIntent: |
| 1104 | return 2; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1105 | |
glennrp | e610a07 | 2010-08-05 17:08:46 +0000 | [diff] [blame] | 1106 | case AbsoluteIntent: |
| 1107 | return 3; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1108 | |
glennrp | e610a07 | 2010-08-05 17:08:46 +0000 | [diff] [blame] | 1109 | default: |
| 1110 | return -1; |
| 1111 | } |
| 1112 | } |
| 1113 | |
| 1114 | static RenderingIntent |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 1115 | Magick_RenderingIntent_from_PNG_RenderingIntent(const int ping_intent) |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1116 | { |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 1117 | switch (ping_intent) |
glennrp | e610a07 | 2010-08-05 17:08:46 +0000 | [diff] [blame] | 1118 | { |
| 1119 | case 0: |
| 1120 | return PerceptualIntent; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1121 | |
glennrp | e610a07 | 2010-08-05 17:08:46 +0000 | [diff] [blame] | 1122 | case 1: |
| 1123 | return RelativeIntent; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1124 | |
glennrp | e610a07 | 2010-08-05 17:08:46 +0000 | [diff] [blame] | 1125 | case 2: |
| 1126 | return SaturationIntent; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1127 | |
glennrp | e610a07 | 2010-08-05 17:08:46 +0000 | [diff] [blame] | 1128 | case 3: |
| 1129 | return AbsoluteIntent; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1130 | |
glennrp | e610a07 | 2010-08-05 17:08:46 +0000 | [diff] [blame] | 1131 | default: |
| 1132 | return UndefinedIntent; |
| 1133 | } |
| 1134 | } |
| 1135 | |
cristy | 9d8c122 | 2012-08-10 12:34:19 +0000 | [diff] [blame] | 1136 | static const char * |
glennrp | 98b83d4 | 2012-07-23 02:50:31 +0000 | [diff] [blame] | 1137 | Magick_RenderingIntentString_from_PNG_RenderingIntent(const int ping_intent) |
| 1138 | { |
| 1139 | switch (ping_intent) |
| 1140 | { |
| 1141 | case 0: |
| 1142 | return "Perceptual Intent"; |
| 1143 | |
| 1144 | case 1: |
| 1145 | return "Relative Intent"; |
| 1146 | |
| 1147 | case 2: |
| 1148 | return "Saturation Intent"; |
| 1149 | |
| 1150 | case 3: |
| 1151 | return "Absolute Intent"; |
| 1152 | |
| 1153 | default: |
| 1154 | return "Undefined Intent"; |
| 1155 | } |
| 1156 | } |
| 1157 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1158 | static inline ssize_t MagickMax(const ssize_t x,const ssize_t y) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1159 | { |
| 1160 | if (x > y) |
| 1161 | return(x); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1162 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1163 | return(y); |
| 1164 | } |
glennrp | 0c3e06b | 2010-11-19 13:45:02 +0000 | [diff] [blame] | 1165 | |
cristy | d9ecd04 | 2012-06-17 18:26:12 +0000 | [diff] [blame] | 1166 | static const char * |
glennrp | 5dff435 | 2012-06-06 22:12:04 +0000 | [diff] [blame] | 1167 | Magick_ColorType_from_PNG_ColorType(const int ping_colortype) |
| 1168 | { |
| 1169 | switch (ping_colortype) |
| 1170 | { |
| 1171 | case 0: |
| 1172 | return "Grayscale"; |
| 1173 | |
| 1174 | case 2: |
| 1175 | return "Truecolor"; |
| 1176 | |
| 1177 | case 3: |
| 1178 | return "Indexed"; |
| 1179 | |
| 1180 | case 4: |
| 1181 | return "GrayAlpha"; |
| 1182 | |
| 1183 | case 6: |
| 1184 | return "RGBA"; |
| 1185 | |
| 1186 | default: |
| 1187 | return "UndefinedColorType"; |
| 1188 | } |
| 1189 | } |
| 1190 | |
| 1191 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1192 | static inline ssize_t MagickMin(const ssize_t x,const ssize_t y) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1193 | { |
| 1194 | if (x < y) |
| 1195 | return(x); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1196 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1197 | return(y); |
| 1198 | } |
glennrp | d5045b4 | 2010-03-24 12:40:35 +0000 | [diff] [blame] | 1199 | #endif /* PNG_LIBPNG_VER > 10011 */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1200 | #endif /* MAGICKCORE_PNG_DELEGATE */ |
| 1201 | |
| 1202 | /* |
| 1203 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1204 | % % |
| 1205 | % % |
| 1206 | % % |
| 1207 | % I s M N G % |
| 1208 | % % |
| 1209 | % % |
| 1210 | % % |
| 1211 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1212 | % |
| 1213 | % IsMNG() returns MagickTrue if the image format type, identified by the |
| 1214 | % magick string, is MNG. |
| 1215 | % |
| 1216 | % The format of the IsMNG method is: |
| 1217 | % |
| 1218 | % MagickBooleanType IsMNG(const unsigned char *magick,const size_t length) |
| 1219 | % |
| 1220 | % A description of each parameter follows: |
| 1221 | % |
| 1222 | % o magick: compare image format pattern against these bytes. |
| 1223 | % |
| 1224 | % o length: Specifies the length of the magick string. |
| 1225 | % |
| 1226 | % |
| 1227 | */ |
| 1228 | static MagickBooleanType IsMNG(const unsigned char *magick,const size_t length) |
| 1229 | { |
| 1230 | if (length < 8) |
| 1231 | return(MagickFalse); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1232 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1233 | if (memcmp(magick,"\212MNG\r\n\032\n",8) == 0) |
| 1234 | return(MagickTrue); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1235 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1236 | return(MagickFalse); |
| 1237 | } |
| 1238 | |
| 1239 | /* |
| 1240 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1241 | % % |
| 1242 | % % |
| 1243 | % % |
| 1244 | % I s J N G % |
| 1245 | % % |
| 1246 | % % |
| 1247 | % % |
| 1248 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1249 | % |
| 1250 | % IsJNG() returns MagickTrue if the image format type, identified by the |
| 1251 | % magick string, is JNG. |
| 1252 | % |
| 1253 | % The format of the IsJNG method is: |
| 1254 | % |
| 1255 | % MagickBooleanType IsJNG(const unsigned char *magick,const size_t length) |
| 1256 | % |
| 1257 | % A description of each parameter follows: |
| 1258 | % |
| 1259 | % o magick: compare image format pattern against these bytes. |
| 1260 | % |
| 1261 | % o length: Specifies the length of the magick string. |
| 1262 | % |
| 1263 | % |
| 1264 | */ |
| 1265 | static MagickBooleanType IsJNG(const unsigned char *magick,const size_t length) |
| 1266 | { |
| 1267 | if (length < 8) |
| 1268 | return(MagickFalse); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1269 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1270 | if (memcmp(magick,"\213JNG\r\n\032\n",8) == 0) |
| 1271 | return(MagickTrue); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1272 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1273 | return(MagickFalse); |
| 1274 | } |
| 1275 | |
| 1276 | /* |
| 1277 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1278 | % % |
| 1279 | % % |
| 1280 | % % |
| 1281 | % I s P N G % |
| 1282 | % % |
| 1283 | % % |
| 1284 | % % |
| 1285 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1286 | % |
| 1287 | % IsPNG() returns MagickTrue if the image format type, identified by the |
| 1288 | % magick string, is PNG. |
| 1289 | % |
| 1290 | % The format of the IsPNG method is: |
| 1291 | % |
| 1292 | % MagickBooleanType IsPNG(const unsigned char *magick,const size_t length) |
| 1293 | % |
| 1294 | % A description of each parameter follows: |
| 1295 | % |
| 1296 | % o magick: compare image format pattern against these bytes. |
| 1297 | % |
| 1298 | % o length: Specifies the length of the magick string. |
| 1299 | % |
| 1300 | */ |
| 1301 | static MagickBooleanType IsPNG(const unsigned char *magick,const size_t length) |
| 1302 | { |
| 1303 | if (length < 8) |
| 1304 | return(MagickFalse); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1305 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1306 | if (memcmp(magick,"\211PNG\r\n\032\n",8) == 0) |
| 1307 | return(MagickTrue); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1308 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1309 | return(MagickFalse); |
| 1310 | } |
| 1311 | |
| 1312 | #if defined(MAGICKCORE_PNG_DELEGATE) |
| 1313 | #if defined(__cplusplus) || defined(c_plusplus) |
| 1314 | extern "C" { |
| 1315 | #endif |
| 1316 | |
glennrp | d5045b4 | 2010-03-24 12:40:35 +0000 | [diff] [blame] | 1317 | #if (PNG_LIBPNG_VER > 10011) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1318 | static size_t WriteBlobMSBULong(Image *image,const size_t value) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1319 | { |
| 1320 | unsigned char |
| 1321 | buffer[4]; |
| 1322 | |
| 1323 | assert(image != (Image *) NULL); |
| 1324 | assert(image->signature == MagickSignature); |
| 1325 | buffer[0]=(unsigned char) (value >> 24); |
| 1326 | buffer[1]=(unsigned char) (value >> 16); |
| 1327 | buffer[2]=(unsigned char) (value >> 8); |
| 1328 | buffer[3]=(unsigned char) value; |
| 1329 | return((size_t) WriteBlob(image,4,buffer)); |
| 1330 | } |
| 1331 | |
| 1332 | static void PNGLong(png_bytep p,png_uint_32 value) |
| 1333 | { |
| 1334 | *p++=(png_byte) ((value >> 24) & 0xff); |
| 1335 | *p++=(png_byte) ((value >> 16) & 0xff); |
| 1336 | *p++=(png_byte) ((value >> 8) & 0xff); |
| 1337 | *p++=(png_byte) (value & 0xff); |
| 1338 | } |
| 1339 | |
glennrp | a521b2f | 2010-10-29 04:11:03 +0000 | [diff] [blame] | 1340 | #if defined(JNG_SUPPORTED) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1341 | static void PNGsLong(png_bytep p,png_int_32 value) |
| 1342 | { |
| 1343 | *p++=(png_byte) ((value >> 24) & 0xff); |
| 1344 | *p++=(png_byte) ((value >> 16) & 0xff); |
| 1345 | *p++=(png_byte) ((value >> 8) & 0xff); |
| 1346 | *p++=(png_byte) (value & 0xff); |
| 1347 | } |
glennrp | a521b2f | 2010-10-29 04:11:03 +0000 | [diff] [blame] | 1348 | #endif |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1349 | |
| 1350 | static void PNGShort(png_bytep p,png_uint_16 value) |
| 1351 | { |
| 1352 | *p++=(png_byte) ((value >> 8) & 0xff); |
| 1353 | *p++=(png_byte) (value & 0xff); |
| 1354 | } |
| 1355 | |
| 1356 | static void PNGType(png_bytep p,png_bytep type) |
| 1357 | { |
| 1358 | (void) CopyMagickMemory(p,type,4*sizeof(png_byte)); |
| 1359 | } |
| 1360 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 1361 | static void LogPNGChunk(MagickBooleanType logging, png_bytep type, |
| 1362 | size_t length) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1363 | { |
| 1364 | if (logging != MagickFalse) |
| 1365 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 1366 | " Writing %c%c%c%c chunk, length: %.20g", |
| 1367 | type[0],type[1],type[2],type[3],(double) length); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1368 | } |
glennrp | d5045b4 | 2010-03-24 12:40:35 +0000 | [diff] [blame] | 1369 | #endif /* PNG_LIBPNG_VER > 10011 */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1370 | |
| 1371 | #if defined(__cplusplus) || defined(c_plusplus) |
| 1372 | } |
| 1373 | #endif |
| 1374 | |
glennrp | d5045b4 | 2010-03-24 12:40:35 +0000 | [diff] [blame] | 1375 | #if PNG_LIBPNG_VER > 10011 |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1376 | /* |
| 1377 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1378 | % % |
| 1379 | % % |
| 1380 | % % |
| 1381 | % R e a d P N G I m a g e % |
| 1382 | % % |
| 1383 | % % |
| 1384 | % % |
| 1385 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1386 | % |
| 1387 | % ReadPNGImage() reads a Portable Network Graphics (PNG) or |
| 1388 | % Multiple-image Network Graphics (MNG) image file and returns it. It |
| 1389 | % allocates the memory necessary for the new Image structure and returns a |
| 1390 | % pointer to the new image or set of images. |
| 1391 | % |
| 1392 | % MNG support written by Glenn Randers-Pehrson, glennrp@image... |
| 1393 | % |
| 1394 | % The format of the ReadPNGImage method is: |
| 1395 | % |
| 1396 | % Image *ReadPNGImage(const ImageInfo *image_info,ExceptionInfo *exception) |
| 1397 | % |
| 1398 | % A description of each parameter follows: |
| 1399 | % |
| 1400 | % o image_info: the image info. |
| 1401 | % |
| 1402 | % o exception: return any errors or warnings in this structure. |
| 1403 | % |
| 1404 | % To do, more or less in chronological order (as of version 5.5.2, |
| 1405 | % November 26, 2002 -- glennrp -- see also "To do" under WriteMNGImage): |
| 1406 | % |
| 1407 | % Get 16-bit cheap transparency working. |
| 1408 | % |
| 1409 | % (At this point, PNG decoding is supposed to be in full MNG-LC compliance) |
| 1410 | % |
| 1411 | % Preserve all unknown and not-yet-handled known chunks found in input |
| 1412 | % PNG file and copy them into output PNG files according to the PNG |
| 1413 | % copying rules. |
| 1414 | % |
| 1415 | % (At this point, PNG encoding should be in full MNG compliance) |
| 1416 | % |
| 1417 | % Provide options for choice of background to use when the MNG BACK |
| 1418 | % chunk is not present or is not mandatory (i.e., leave transparent, |
| 1419 | % user specified, MNG BACK, PNG bKGD) |
| 1420 | % |
| 1421 | % Implement LOOP/ENDL [done, but could do discretionary loops more |
| 1422 | % efficiently by linking in the duplicate frames.]. |
| 1423 | % |
| 1424 | % Decode and act on the MHDR simplicity profile (offer option to reject |
| 1425 | % files or attempt to process them anyway when the profile isn't LC or VLC). |
| 1426 | % |
| 1427 | % Upgrade to full MNG without Delta-PNG. |
| 1428 | % |
| 1429 | % o BACK [done a while ago except for background image ID] |
| 1430 | % o MOVE [done 15 May 1999] |
| 1431 | % o CLIP [done 15 May 1999] |
| 1432 | % o DISC [done 19 May 1999] |
| 1433 | % o SAVE [partially done 19 May 1999 (marks objects frozen)] |
| 1434 | % o SEEK [partially done 19 May 1999 (discard function only)] |
| 1435 | % o SHOW |
| 1436 | % o PAST |
| 1437 | % o BASI |
| 1438 | % o MNG-level tEXt/iTXt/zTXt |
| 1439 | % o pHYg |
| 1440 | % o pHYs |
| 1441 | % o sBIT |
| 1442 | % o bKGD |
| 1443 | % o iTXt (wait for libpng implementation). |
| 1444 | % |
| 1445 | % Use the scene signature to discover when an identical scene is |
| 1446 | % being reused, and just point to the original image->exception instead |
| 1447 | % of storing another set of pixels. This not specific to MNG |
| 1448 | % but could be applied generally. |
| 1449 | % |
| 1450 | % Upgrade to full MNG with Delta-PNG. |
| 1451 | % |
| 1452 | % JNG tEXt/iTXt/zTXt |
| 1453 | % |
| 1454 | % We will not attempt to read files containing the CgBI chunk. |
| 1455 | % They are really Xcode files meant for display on the iPhone. |
| 1456 | % These are not valid PNG files and it is impossible to recover |
glennrp | f54e295 | 2011-06-29 14:20:44 +0000 | [diff] [blame] | 1457 | % the original PNG from files that have been converted to Xcode-PNG, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1458 | % since irretrievable loss of color data has occurred due to the |
| 1459 | % use of premultiplied alpha. |
| 1460 | */ |
| 1461 | |
| 1462 | #if defined(__cplusplus) || defined(c_plusplus) |
| 1463 | extern "C" { |
| 1464 | #endif |
| 1465 | |
| 1466 | /* |
| 1467 | This the function that does the actual reading of data. It is |
| 1468 | the same as the one supplied in libpng, except that it receives the |
| 1469 | datastream from the ReadBlob() function instead of standard input. |
| 1470 | */ |
| 1471 | static void png_get_data(png_structp png_ptr,png_bytep data,png_size_t length) |
| 1472 | { |
| 1473 | Image |
| 1474 | *image; |
| 1475 | |
| 1476 | image=(Image *) png_get_io_ptr(png_ptr); |
| 1477 | if (length) |
| 1478 | { |
| 1479 | png_size_t |
| 1480 | check; |
| 1481 | |
| 1482 | check=(png_size_t) ReadBlob(image,(size_t) length,data); |
| 1483 | if (check != length) |
| 1484 | { |
| 1485 | char |
| 1486 | msg[MaxTextExtent]; |
| 1487 | |
cristy | 3b6fd2e | 2011-05-20 12:53:50 +0000 | [diff] [blame] | 1488 | (void) FormatLocaleString(msg,MaxTextExtent, |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 1489 | "Expected %.20g bytes; found %.20g bytes",(double) length, |
| 1490 | (double) check); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1491 | png_warning(png_ptr,msg); |
| 1492 | png_error(png_ptr,"Read Exception"); |
| 1493 | } |
| 1494 | } |
| 1495 | } |
| 1496 | |
| 1497 | #if !defined(PNG_READ_EMPTY_PLTE_SUPPORTED) && \ |
| 1498 | !defined(PNG_MNG_FEATURES_SUPPORTED) |
| 1499 | /* We use mng_get_data() instead of png_get_data() if we have a libpng |
| 1500 | * older than libpng-1.0.3a, which was the first to allow the empty |
| 1501 | * PLTE, or a newer libpng in which PNG_MNG_FEATURES_SUPPORTED was |
| 1502 | * ifdef'ed out. Earlier versions would crash if the bKGD chunk was |
| 1503 | * encountered after an empty PLTE, so we have to look ahead for bKGD |
| 1504 | * chunks and remove them from the datastream that is passed to libpng, |
| 1505 | * and store their contents for later use. |
| 1506 | */ |
| 1507 | static void mng_get_data(png_structp png_ptr,png_bytep data,png_size_t length) |
| 1508 | { |
| 1509 | MngInfo |
| 1510 | *mng_info; |
| 1511 | |
| 1512 | Image |
| 1513 | *image; |
| 1514 | |
| 1515 | png_size_t |
| 1516 | check; |
| 1517 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1518 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1519 | i; |
| 1520 | |
| 1521 | i=0; |
| 1522 | mng_info=(MngInfo *) png_get_io_ptr(png_ptr); |
| 1523 | image=(Image *) mng_info->image; |
| 1524 | while (mng_info->bytes_in_read_buffer && length) |
| 1525 | { |
| 1526 | data[i]=mng_info->read_buffer[i]; |
| 1527 | mng_info->bytes_in_read_buffer--; |
| 1528 | length--; |
| 1529 | i++; |
| 1530 | } |
| 1531 | if (length) |
| 1532 | { |
| 1533 | check=(png_size_t) ReadBlob(image,(size_t) length,(char *) data); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1534 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1535 | if (check != length) |
| 1536 | png_error(png_ptr,"Read Exception"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1537 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1538 | if (length == 4) |
| 1539 | { |
| 1540 | if ((data[0] == 0) && (data[1] == 0) && (data[2] == 0) && |
| 1541 | (data[3] == 0)) |
| 1542 | { |
| 1543 | check=(png_size_t) ReadBlob(image,(size_t) length, |
| 1544 | (char *) mng_info->read_buffer); |
| 1545 | mng_info->read_buffer[4]=0; |
| 1546 | mng_info->bytes_in_read_buffer=4; |
| 1547 | if (memcmp(mng_info->read_buffer,mng_PLTE,4) == 0) |
| 1548 | mng_info->found_empty_plte=MagickTrue; |
| 1549 | if (memcmp(mng_info->read_buffer,mng_IEND,4) == 0) |
| 1550 | { |
| 1551 | mng_info->found_empty_plte=MagickFalse; |
| 1552 | mng_info->have_saved_bkgd_index=MagickFalse; |
| 1553 | } |
| 1554 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1555 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1556 | if ((data[0] == 0) && (data[1] == 0) && (data[2] == 0) && |
| 1557 | (data[3] == 1)) |
| 1558 | { |
| 1559 | check=(png_size_t) ReadBlob(image,(size_t) length, |
| 1560 | (char *) mng_info->read_buffer); |
| 1561 | mng_info->read_buffer[4]=0; |
| 1562 | mng_info->bytes_in_read_buffer=4; |
| 1563 | if (memcmp(mng_info->read_buffer,mng_bKGD,4) == 0) |
| 1564 | if (mng_info->found_empty_plte) |
| 1565 | { |
| 1566 | /* |
| 1567 | Skip the bKGD data byte and CRC. |
| 1568 | */ |
| 1569 | check=(png_size_t) |
| 1570 | ReadBlob(image,5,(char *) mng_info->read_buffer); |
| 1571 | check=(png_size_t) ReadBlob(image,(size_t) length, |
| 1572 | (char *) mng_info->read_buffer); |
| 1573 | mng_info->saved_bkgd_index=mng_info->read_buffer[0]; |
| 1574 | mng_info->have_saved_bkgd_index=MagickTrue; |
| 1575 | mng_info->bytes_in_read_buffer=0; |
| 1576 | } |
| 1577 | } |
| 1578 | } |
| 1579 | } |
| 1580 | } |
| 1581 | #endif |
| 1582 | |
| 1583 | static void png_put_data(png_structp png_ptr,png_bytep data,png_size_t length) |
| 1584 | { |
| 1585 | Image |
| 1586 | *image; |
| 1587 | |
| 1588 | image=(Image *) png_get_io_ptr(png_ptr); |
| 1589 | if (length) |
| 1590 | { |
| 1591 | png_size_t |
| 1592 | check; |
| 1593 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1594 | check=(png_size_t) WriteBlob(image,(size_t) length,data); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1595 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1596 | if (check != length) |
| 1597 | png_error(png_ptr,"WriteBlob Failed"); |
| 1598 | } |
| 1599 | } |
| 1600 | |
| 1601 | static void png_flush_data(png_structp png_ptr) |
| 1602 | { |
| 1603 | (void) png_ptr; |
| 1604 | } |
| 1605 | |
| 1606 | #ifdef PNG_WRITE_EMPTY_PLTE_SUPPORTED |
| 1607 | static int PalettesAreEqual(Image *a,Image *b) |
| 1608 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1609 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1610 | i; |
| 1611 | |
| 1612 | if ((a == (Image *) NULL) || (b == (Image *) NULL)) |
| 1613 | return((int) MagickFalse); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1614 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1615 | if (a->storage_class != PseudoClass || b->storage_class != PseudoClass) |
| 1616 | return((int) MagickFalse); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1617 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1618 | if (a->colors != b->colors) |
| 1619 | return((int) MagickFalse); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1620 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1621 | for (i=0; i < (ssize_t) a->colors; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1622 | { |
| 1623 | if ((a->colormap[i].red != b->colormap[i].red) || |
| 1624 | (a->colormap[i].green != b->colormap[i].green) || |
| 1625 | (a->colormap[i].blue != b->colormap[i].blue)) |
| 1626 | return((int) MagickFalse); |
| 1627 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1628 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1629 | return((int) MagickTrue); |
| 1630 | } |
| 1631 | #endif |
| 1632 | |
| 1633 | static void MngInfoDiscardObject(MngInfo *mng_info,int i) |
| 1634 | { |
| 1635 | if (i && (i < MNG_MAX_OBJECTS) && (mng_info != (MngInfo *) NULL) && |
| 1636 | mng_info->exists[i] && !mng_info->frozen[i]) |
| 1637 | { |
| 1638 | #ifdef MNG_OBJECT_BUFFERS |
| 1639 | if (mng_info->ob[i] != (MngBuffer *) NULL) |
| 1640 | { |
| 1641 | if (mng_info->ob[i]->reference_count > 0) |
| 1642 | mng_info->ob[i]->reference_count--; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1643 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1644 | if (mng_info->ob[i]->reference_count == 0) |
| 1645 | { |
| 1646 | if (mng_info->ob[i]->image != (Image *) NULL) |
| 1647 | mng_info->ob[i]->image=DestroyImage(mng_info->ob[i]->image); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1648 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1649 | mng_info->ob[i]=DestroyString(mng_info->ob[i]); |
| 1650 | } |
| 1651 | } |
| 1652 | mng_info->ob[i]=(MngBuffer *) NULL; |
| 1653 | #endif |
| 1654 | mng_info->exists[i]=MagickFalse; |
| 1655 | mng_info->invisible[i]=MagickFalse; |
| 1656 | mng_info->viewable[i]=MagickFalse; |
| 1657 | mng_info->frozen[i]=MagickFalse; |
| 1658 | mng_info->x_off[i]=0; |
| 1659 | mng_info->y_off[i]=0; |
| 1660 | mng_info->object_clip[i].left=0; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1661 | mng_info->object_clip[i].right=(ssize_t) PNG_UINT_31_MAX; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1662 | mng_info->object_clip[i].top=0; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1663 | mng_info->object_clip[i].bottom=(ssize_t) PNG_UINT_31_MAX; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1664 | } |
| 1665 | } |
| 1666 | |
glennrp | 21f0e62 | 2011-01-07 16:20:57 +0000 | [diff] [blame] | 1667 | static void MngInfoFreeStruct(MngInfo *mng_info, |
| 1668 | MagickBooleanType *have_mng_structure) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1669 | { |
glennrp | 21f0e62 | 2011-01-07 16:20:57 +0000 | [diff] [blame] | 1670 | if (*have_mng_structure != MagickFalse && (mng_info != (MngInfo *) NULL)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1671 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1672 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1673 | i; |
| 1674 | |
| 1675 | for (i=1; i < MNG_MAX_OBJECTS; i++) |
| 1676 | MngInfoDiscardObject(mng_info,i); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1677 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1678 | if (mng_info->global_plte != (png_colorp) NULL) |
| 1679 | mng_info->global_plte=(png_colorp) |
| 1680 | RelinquishMagickMemory(mng_info->global_plte); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1681 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1682 | mng_info=(MngInfo *) RelinquishMagickMemory(mng_info); |
| 1683 | *have_mng_structure=MagickFalse; |
| 1684 | } |
| 1685 | } |
| 1686 | |
| 1687 | static MngBox mng_minimum_box(MngBox box1,MngBox box2) |
| 1688 | { |
| 1689 | MngBox |
| 1690 | box; |
| 1691 | |
| 1692 | box=box1; |
| 1693 | if (box.left < box2.left) |
| 1694 | box.left=box2.left; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1695 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1696 | if (box.top < box2.top) |
| 1697 | box.top=box2.top; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1698 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1699 | if (box.right > box2.right) |
| 1700 | box.right=box2.right; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1701 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1702 | if (box.bottom > box2.bottom) |
| 1703 | box.bottom=box2.bottom; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1704 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1705 | return box; |
| 1706 | } |
| 1707 | |
| 1708 | static MngBox mng_read_box(MngBox previous_box,char delta_type,unsigned char *p) |
| 1709 | { |
| 1710 | MngBox |
| 1711 | box; |
| 1712 | |
| 1713 | /* |
| 1714 | Read clipping boundaries from DEFI, CLIP, FRAM, or PAST chunk. |
| 1715 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1716 | box.left=(ssize_t) ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]); |
| 1717 | box.right=(ssize_t) ((p[4] << 24) | (p[5] << 16) | (p[6] << 8) | p[7]); |
| 1718 | box.top=(ssize_t) ((p[8] << 24) | (p[9] << 16) | (p[10] << 8) | p[11]); |
| 1719 | 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] | 1720 | if (delta_type != 0) |
| 1721 | { |
| 1722 | box.left+=previous_box.left; |
| 1723 | box.right+=previous_box.right; |
| 1724 | box.top+=previous_box.top; |
| 1725 | box.bottom+=previous_box.bottom; |
| 1726 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1727 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1728 | return(box); |
| 1729 | } |
| 1730 | |
| 1731 | static MngPair mng_read_pair(MngPair previous_pair,int delta_type, |
| 1732 | unsigned char *p) |
| 1733 | { |
| 1734 | MngPair |
| 1735 | pair; |
| 1736 | /* |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1737 | Read two ssize_ts from CLON, MOVE or PAST chunk |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1738 | */ |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 1739 | pair.a=(long) ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]); |
| 1740 | 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] | 1741 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1742 | if (delta_type != 0) |
| 1743 | { |
| 1744 | pair.a+=previous_pair.a; |
| 1745 | pair.b+=previous_pair.b; |
| 1746 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1747 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1748 | return(pair); |
| 1749 | } |
| 1750 | |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 1751 | static long mng_get_long(unsigned char *p) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1752 | { |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 1753 | return((long) ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3])); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1754 | } |
| 1755 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 1756 | typedef struct _PNGErrorInfo |
cristy | c82a27b | 2011-10-21 01:07:16 +0000 | [diff] [blame] | 1757 | { |
cristy | c82a27b | 2011-10-21 01:07:16 +0000 | [diff] [blame] | 1758 | Image |
| 1759 | *image; |
| 1760 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 1761 | ExceptionInfo |
| 1762 | *exception; |
| 1763 | } PNGErrorInfo; |
cristy | c82a27b | 2011-10-21 01:07:16 +0000 | [diff] [blame] | 1764 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 1765 | static void MagickPNGErrorHandler(png_struct *ping,png_const_charp message) |
| 1766 | { |
| 1767 | ExceptionInfo |
| 1768 | *exception; |
cristy | c82a27b | 2011-10-21 01:07:16 +0000 | [diff] [blame] | 1769 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 1770 | Image |
| 1771 | *image; |
| 1772 | |
| 1773 | PNGErrorInfo |
| 1774 | *error_info; |
| 1775 | |
| 1776 | error_info=(PNGErrorInfo *) png_get_error_ptr(ping); |
| 1777 | image=error_info->image; |
| 1778 | exception=error_info->exception; |
| 1779 | |
| 1780 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 1781 | " libpng-%s error: %s", PNG_LIBPNG_VER_STRING,message); |
| 1782 | |
| 1783 | (void) ThrowMagickException(exception,GetMagickModule(),CoderError,message, |
| 1784 | "`%s'",image->filename); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1785 | |
glennrp | e4017e3 | 2011-01-08 17:16:09 +0000 | [diff] [blame] | 1786 | #if (PNG_LIBPNG_VER < 10500) |
glennrp | 8371ecc | 2011-02-19 19:15:38 +0000 | [diff] [blame] | 1787 | /* A warning about deprecated use of jmpbuf here is unavoidable if you |
| 1788 | * are building with libpng-1.4.x and can be ignored. |
| 1789 | */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1790 | longjmp(ping->jmpbuf,1); |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 1791 | #else |
| 1792 | png_longjmp(ping,1); |
| 1793 | #endif |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1794 | } |
| 1795 | |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 1796 | static void MagickPNGWarningHandler(png_struct *ping,png_const_charp message) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1797 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 1798 | ExceptionInfo |
| 1799 | *exception; |
| 1800 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1801 | Image |
| 1802 | *image; |
| 1803 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 1804 | PNGErrorInfo |
| 1805 | *error_info; |
| 1806 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1807 | if (LocaleCompare(message, "Missing PLTE before tRNS") == 0) |
| 1808 | png_error(ping, message); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1809 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 1810 | error_info=(PNGErrorInfo *) png_get_error_ptr(ping); |
| 1811 | image=error_info->image; |
| 1812 | exception=error_info->exception; |
| 1813 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 1814 | " libpng-%s warning: %s", PNG_LIBPNG_VER_STRING,message); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1815 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 1816 | (void) ThrowMagickException(exception,GetMagickModule(),CoderWarning, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1817 | message,"`%s'",image->filename); |
| 1818 | } |
| 1819 | |
| 1820 | #ifdef PNG_USER_MEM_SUPPORTED |
cristy | a865ccd | 2012-07-28 00:33:10 +0000 | [diff] [blame] | 1821 | #if PNG_LIBPNG_VER >= 14000 |
| 1822 | static png_voidp Magick_png_malloc(png_structp png_ptr,png_alloc_size_t size) |
| 1823 | #else |
| 1824 | static png_voidp Magick_png_malloc(png_structp png_ptr,png_size_t size) |
| 1825 | #endif |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1826 | { |
cristy | df0d90e | 2011-12-12 01:03:55 +0000 | [diff] [blame] | 1827 | (void) png_ptr; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1828 | return((png_voidp) AcquireMagickMemory((size_t) size)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1829 | } |
| 1830 | |
| 1831 | /* |
| 1832 | Free a pointer. It is removed from the list at the same time. |
| 1833 | */ |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 1834 | 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] | 1835 | { |
glennrp | 3bd393f | 2011-12-21 18:54:53 +0000 | [diff] [blame] | 1836 | (void) png_ptr; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1837 | ptr=RelinquishMagickMemory(ptr); |
| 1838 | return((png_free_ptr) NULL); |
| 1839 | } |
| 1840 | #endif |
| 1841 | |
| 1842 | #if defined(__cplusplus) || defined(c_plusplus) |
| 1843 | } |
| 1844 | #endif |
| 1845 | |
| 1846 | static int |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 1847 | Magick_png_read_raw_profile(png_struct *ping,Image *image, |
| 1848 | const ImageInfo *image_info, png_textp text,int ii,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1849 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1850 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1851 | i; |
| 1852 | |
| 1853 | register unsigned char |
| 1854 | *dp; |
| 1855 | |
| 1856 | register png_charp |
| 1857 | sp; |
| 1858 | |
| 1859 | png_uint_32 |
| 1860 | length, |
| 1861 | nibbles; |
| 1862 | |
| 1863 | StringInfo |
| 1864 | *profile; |
| 1865 | |
glennrp | 0c3e06b | 2010-11-19 13:45:02 +0000 | [diff] [blame] | 1866 | const unsigned char |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1867 | unhex[103]={0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, |
| 1868 | 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, |
| 1869 | 0,0,0,0,0,0,0,0,0,1, 2,3,4,5,6,7,8,9,0,0, |
| 1870 | 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, |
| 1871 | 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,10,11,12, |
| 1872 | 13,14,15}; |
| 1873 | |
| 1874 | sp=text[ii].text+1; |
| 1875 | /* look for newline */ |
| 1876 | while (*sp != '\n') |
| 1877 | sp++; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1878 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1879 | /* look for length */ |
| 1880 | while (*sp == '\0' || *sp == ' ' || *sp == '\n') |
| 1881 | sp++; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1882 | |
cristy | f2f2727 | 2009-12-17 14:48:46 +0000 | [diff] [blame] | 1883 | length=(png_uint_32) StringToLong(sp); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1884 | |
glennrp | 97f90e2 | 2011-02-22 05:47:58 +0000 | [diff] [blame] | 1885 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 1886 | " length: %lu",(unsigned long) length); |
| 1887 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1888 | while (*sp != ' ' && *sp != '\n') |
| 1889 | sp++; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1890 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1891 | /* allocate space */ |
| 1892 | if (length == 0) |
| 1893 | { |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 1894 | png_warning(ping,"invalid profile length"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1895 | return(MagickFalse); |
| 1896 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1897 | |
cristy | 8723e4b | 2011-09-01 13:11:19 +0000 | [diff] [blame] | 1898 | profile=BlobToStringInfo((const void *) NULL,length); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1899 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1900 | if (profile == (StringInfo *) NULL) |
| 1901 | { |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 1902 | png_warning(ping, "unable to copy profile"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1903 | return(MagickFalse); |
| 1904 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1905 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1906 | /* copy profile, skipping white space and column 1 "=" signs */ |
| 1907 | dp=GetStringInfoDatum(profile); |
| 1908 | nibbles=length*2; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1909 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1910 | for (i=0; i < (ssize_t) nibbles; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1911 | { |
| 1912 | while (*sp < '0' || (*sp > '9' && *sp < 'a') || *sp > 'f') |
| 1913 | { |
| 1914 | if (*sp == '\0') |
| 1915 | { |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 1916 | png_warning(ping, "ran out of profile data"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1917 | profile=DestroyStringInfo(profile); |
| 1918 | return(MagickFalse); |
| 1919 | } |
| 1920 | sp++; |
| 1921 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1922 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1923 | if (i%2 == 0) |
| 1924 | *dp=(unsigned char) (16*unhex[(int) *sp++]); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1925 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1926 | else |
| 1927 | (*dp++)+=unhex[(int) *sp++]; |
| 1928 | } |
| 1929 | /* |
| 1930 | We have already read "Raw profile type. |
| 1931 | */ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 1932 | (void) SetImageProfile(image,&text[ii].key[17],profile,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1933 | profile=DestroyStringInfo(profile); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1934 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1935 | if (image_info->verbose) |
| 1936 | (void) printf(" Found a generic profile, type %s\n",&text[ii].key[17]); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1937 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1938 | return MagickTrue; |
| 1939 | } |
| 1940 | |
| 1941 | #if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED) |
| 1942 | static int read_vpag_chunk_callback(png_struct *ping, png_unknown_chunkp chunk) |
| 1943 | { |
| 1944 | Image |
| 1945 | *image; |
| 1946 | |
| 1947 | |
| 1948 | /* The unknown chunk structure contains the chunk data: |
| 1949 | png_byte name[5]; |
| 1950 | png_byte *data; |
| 1951 | png_size_t size; |
| 1952 | |
| 1953 | Note that libpng has already taken care of the CRC handling. |
| 1954 | */ |
| 1955 | |
| 1956 | |
| 1957 | if (chunk->name[0] != 118 || chunk->name[1] != 112 || |
| 1958 | chunk->name[2] != 65 ||chunk-> name[3] != 103) |
| 1959 | return(0); /* Did not recognize */ |
| 1960 | |
| 1961 | /* recognized vpAg */ |
| 1962 | |
| 1963 | if (chunk->size != 9) |
| 1964 | return(-1); /* Error return */ |
| 1965 | |
| 1966 | if (chunk->data[8] != 0) |
| 1967 | return(0); /* ImageMagick requires pixel units */ |
| 1968 | |
| 1969 | image=(Image *) png_get_user_chunk_ptr(ping); |
| 1970 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1971 | image->page.width=(size_t) ((chunk->data[0] << 24) | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1972 | (chunk->data[1] << 16) | (chunk->data[2] << 8) | chunk->data[3]); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1973 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1974 | image->page.height=(size_t) ((chunk->data[4] << 24) | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1975 | (chunk->data[5] << 16) | (chunk->data[6] << 8) | chunk->data[7]); |
| 1976 | |
| 1977 | /* Return one of the following: */ |
| 1978 | /* return(-n); chunk had an error */ |
| 1979 | /* return(0); did not recognize */ |
| 1980 | /* return(n); success */ |
| 1981 | |
| 1982 | return(1); |
| 1983 | |
| 1984 | } |
| 1985 | #endif |
| 1986 | |
| 1987 | /* |
| 1988 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1989 | % % |
| 1990 | % % |
| 1991 | % % |
| 1992 | % R e a d O n e P N G I m a g e % |
| 1993 | % % |
| 1994 | % % |
| 1995 | % % |
| 1996 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1997 | % |
| 1998 | % ReadOnePNGImage() reads a Portable Network Graphics (PNG) image file |
| 1999 | % (minus the 8-byte signature) and returns it. It allocates the memory |
| 2000 | % necessary for the new Image structure and returns a pointer to the new |
| 2001 | % image. |
| 2002 | % |
| 2003 | % The format of the ReadOnePNGImage method is: |
| 2004 | % |
| 2005 | % Image *ReadOnePNGImage(MngInfo *mng_info, const ImageInfo *image_info, |
| 2006 | % ExceptionInfo *exception) |
| 2007 | % |
| 2008 | % A description of each parameter follows: |
| 2009 | % |
| 2010 | % o mng_info: Specifies a pointer to a MngInfo structure. |
| 2011 | % |
| 2012 | % o image_info: the image info. |
| 2013 | % |
| 2014 | % o exception: return any errors or warnings in this structure. |
| 2015 | % |
| 2016 | */ |
| 2017 | static Image *ReadOnePNGImage(MngInfo *mng_info, |
| 2018 | const ImageInfo *image_info, ExceptionInfo *exception) |
| 2019 | { |
| 2020 | /* Read one PNG image */ |
| 2021 | |
glennrp | cc95c3f | 2011-04-18 16:46:48 +0000 | [diff] [blame] | 2022 | /* To do: Read the tIME chunk into the date:modify property */ |
| 2023 | /* To do: Read the tEXt/Creation Time chunk into the date:create property */ |
| 2024 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2025 | Image |
| 2026 | *image; |
| 2027 | |
| 2028 | int |
glennrp | 98b83d4 | 2012-07-23 02:50:31 +0000 | [diff] [blame] | 2029 | intent, /* "PNG Rendering intent", which is ICC intent + 1 */ |
glennrp | cb395ac | 2011-03-30 19:50:23 +0000 | [diff] [blame] | 2030 | num_raw_profiles, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2031 | num_text, |
glennrp | 4eb3931 | 2011-03-30 21:34:55 +0000 | [diff] [blame] | 2032 | num_text_total, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2033 | num_passes, |
glennrp | 913f961 | 2012-06-27 17:50:00 +0000 | [diff] [blame] | 2034 | number_colors, |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2035 | pass, |
| 2036 | ping_bit_depth, |
| 2037 | ping_color_type, |
glennrp | fcf0616 | 2012-11-05 14:57:08 +0000 | [diff] [blame] | 2038 | ping_file_depth, |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2039 | ping_interlace_method, |
| 2040 | ping_compression_method, |
| 2041 | ping_filter_method, |
glennrp | 4eb3931 | 2011-03-30 21:34:55 +0000 | [diff] [blame] | 2042 | ping_num_trans, |
| 2043 | unit_type; |
| 2044 | |
| 2045 | double |
| 2046 | file_gamma; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2047 | |
| 2048 | MagickBooleanType |
cristy | 4383ec8 | 2011-01-05 15:42:32 +0000 | [diff] [blame] | 2049 | logging, |
glennrp | 98b83d4 | 2012-07-23 02:50:31 +0000 | [diff] [blame] | 2050 | ping_found_cHRM, |
| 2051 | ping_found_gAMA, |
| 2052 | ping_found_iCCP, |
| 2053 | ping_found_sRGB, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2054 | status; |
| 2055 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 2056 | PixelInfo |
| 2057 | transparent_color; |
| 2058 | |
| 2059 | PNGErrorInfo |
| 2060 | error_info; |
| 2061 | |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2062 | png_bytep |
| 2063 | ping_trans_alpha; |
| 2064 | |
| 2065 | png_color_16p |
| 2066 | ping_background, |
| 2067 | ping_trans_color; |
| 2068 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2069 | png_info |
| 2070 | *end_info, |
| 2071 | *ping_info; |
| 2072 | |
| 2073 | png_struct |
| 2074 | *ping; |
| 2075 | |
| 2076 | png_textp |
| 2077 | text; |
| 2078 | |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2079 | png_uint_32 |
| 2080 | ping_height, |
| 2081 | ping_width, |
glennrp | 4eb3931 | 2011-03-30 21:34:55 +0000 | [diff] [blame] | 2082 | x_resolution, |
| 2083 | y_resolution; |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2084 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 2085 | QuantumInfo |
| 2086 | *quantum_info; |
| 2087 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2088 | ssize_t |
cristy | 756ae43 | 2011-11-19 02:18:25 +0000 | [diff] [blame] | 2089 | ping_rowbytes, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2090 | y; |
| 2091 | |
| 2092 | register unsigned char |
| 2093 | *p; |
| 2094 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2095 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2096 | i, |
| 2097 | x; |
| 2098 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 2099 | register Quantum |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2100 | *q; |
| 2101 | |
| 2102 | size_t |
glennrp | 39992b4 | 2010-11-14 00:03:43 +0000 | [diff] [blame] | 2103 | length, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2104 | row_offset; |
| 2105 | |
cristy | eb3b22a | 2011-03-31 20:16:11 +0000 | [diff] [blame] | 2106 | ssize_t |
| 2107 | j; |
| 2108 | |
cristy | 75fc68f | 2012-10-08 16:26:00 +0000 | [diff] [blame] | 2109 | unsigned char |
| 2110 | *volatile ping_pixels; |
cristy | 55b78b5 | 2012-10-08 14:01:27 +0000 | [diff] [blame] | 2111 | |
glennrp | 629960f | 2012-05-29 19:13:52 +0000 | [diff] [blame] | 2112 | #ifdef PNG_UNKNOWN_CHUNKS_SUPPORTED |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2113 | png_byte unused_chunks[]= |
| 2114 | { |
| 2115 | 104, 73, 83, 84, (png_byte) '\0', /* hIST */ |
| 2116 | 105, 84, 88, 116, (png_byte) '\0', /* iTXt */ |
| 2117 | 112, 67, 65, 76, (png_byte) '\0', /* pCAL */ |
| 2118 | 115, 67, 65, 76, (png_byte) '\0', /* sCAL */ |
| 2119 | 115, 80, 76, 84, (png_byte) '\0', /* sPLT */ |
| 2120 | 116, 73, 77, 69, (png_byte) '\0', /* tIME */ |
glennrp | 629960f | 2012-05-29 19:13:52 +0000 | [diff] [blame] | 2121 | #ifdef PNG_APNG_SUPPORTED /* libpng was built with APNG patch; */ |
| 2122 | /* ignore the APNG chunks */ |
| 2123 | 97, 99, 84, 76, (png_byte) '\0', /* acTL */ |
| 2124 | 102, 99, 84, 76, (png_byte) '\0', /* fcTL */ |
| 2125 | 102, 100, 65, 84, (png_byte) '\0', /* fdAT */ |
| 2126 | #endif |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2127 | }; |
| 2128 | #endif |
| 2129 | |
| 2130 | logging=LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 2131 | " Enter ReadOnePNGImage()"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2132 | |
glennrp | 25c1e2b | 2010-03-25 01:39:56 +0000 | [diff] [blame] | 2133 | #if (PNG_LIBPNG_VER < 10200) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2134 | if (image_info->verbose) |
| 2135 | printf("Your PNG library (libpng-%s) is rather old.\n", |
| 2136 | PNG_LIBPNG_VER_STRING); |
| 2137 | #endif |
| 2138 | |
glennrp | 61b4c95 | 2009-11-10 20:40:41 +0000 | [diff] [blame] | 2139 | #if (PNG_LIBPNG_VER >= 10400) |
| 2140 | # ifndef PNG_TRANSFORM_GRAY_TO_RGB /* Added at libpng-1.4.0beta67 */ |
| 2141 | if (image_info->verbose) |
| 2142 | { |
| 2143 | printf("Your PNG library (libpng-%s) is an old beta version.\n", |
| 2144 | PNG_LIBPNG_VER_STRING); |
| 2145 | printf("Please update it.\n"); |
| 2146 | } |
| 2147 | # endif |
| 2148 | #endif |
| 2149 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 2150 | |
| 2151 | quantum_info = (QuantumInfo *) NULL; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2152 | image=mng_info->image; |
| 2153 | |
glennrp | a6a0663 | 2011-01-19 15:15:34 +0000 | [diff] [blame] | 2154 | if (logging != MagickFalse) |
glennrp | 98b83d4 | 2012-07-23 02:50:31 +0000 | [diff] [blame] | 2155 | { |
glennrp | a6a0663 | 2011-01-19 15:15:34 +0000 | [diff] [blame] | 2156 | (void)LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 2157 | " image->alpha_trait=%d",(int) image->alpha_trait); |
glennrp | a6a0663 | 2011-01-19 15:15:34 +0000 | [diff] [blame] | 2158 | |
glennrp | 98b83d4 | 2012-07-23 02:50:31 +0000 | [diff] [blame] | 2159 | (void)LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2160 | " image->rendering_intent=%d",(int) image->rendering_intent); |
glennrp | e88af77 | 2012-08-22 13:59:50 +0000 | [diff] [blame] | 2161 | |
| 2162 | (void)LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2163 | " image->colorspace=%d",(int) image->colorspace); |
glennrp | 98b83d4 | 2012-07-23 02:50:31 +0000 | [diff] [blame] | 2164 | } |
| 2165 | intent=Magick_RenderingIntent_to_PNG_RenderingIntent(image->rendering_intent); |
| 2166 | |
glennrp | 0e31973 | 2011-01-25 21:53:13 +0000 | [diff] [blame] | 2167 | /* Set to an out-of-range color unless tRNS chunk is present */ |
| 2168 | transparent_color.red=65537; |
| 2169 | transparent_color.green=65537; |
| 2170 | transparent_color.blue=65537; |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 2171 | transparent_color.alpha=65537; |
glennrp | 0e31973 | 2011-01-25 21:53:13 +0000 | [diff] [blame] | 2172 | |
glennrp | 913f961 | 2012-06-27 17:50:00 +0000 | [diff] [blame] | 2173 | number_colors=0; |
glennrp | cb395ac | 2011-03-30 19:50:23 +0000 | [diff] [blame] | 2174 | num_text = 0; |
glennrp | 4eb3931 | 2011-03-30 21:34:55 +0000 | [diff] [blame] | 2175 | num_text_total = 0; |
glennrp | cb395ac | 2011-03-30 19:50:23 +0000 | [diff] [blame] | 2176 | num_raw_profiles = 0; |
| 2177 | |
glennrp | 98b83d4 | 2012-07-23 02:50:31 +0000 | [diff] [blame] | 2178 | ping_found_cHRM = MagickFalse; |
| 2179 | ping_found_gAMA = MagickFalse; |
| 2180 | ping_found_iCCP = MagickFalse; |
| 2181 | ping_found_sRGB = MagickFalse; |
| 2182 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2183 | /* |
| 2184 | Allocate the PNG structures |
| 2185 | */ |
| 2186 | #ifdef PNG_USER_MEM_SUPPORTED |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 2187 | error_info.image=image; |
| 2188 | error_info.exception=exception; |
| 2189 | ping=png_create_read_struct_2(PNG_LIBPNG_VER_STRING,&error_info, |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 2190 | MagickPNGErrorHandler,MagickPNGWarningHandler, NULL, |
| 2191 | (png_malloc_ptr) Magick_png_malloc,(png_free_ptr) Magick_png_free); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2192 | #else |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 2193 | ping=png_create_read_struct(PNG_LIBPNG_VER_STRING,&error_info, |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 2194 | MagickPNGErrorHandler,MagickPNGWarningHandler); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2195 | #endif |
| 2196 | if (ping == (png_struct *) NULL) |
| 2197 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2198 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2199 | ping_info=png_create_info_struct(ping); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2200 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2201 | if (ping_info == (png_info *) NULL) |
| 2202 | { |
| 2203 | png_destroy_read_struct(&ping,(png_info **) NULL,(png_info **) NULL); |
| 2204 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
| 2205 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2206 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2207 | end_info=png_create_info_struct(ping); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2208 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2209 | if (end_info == (png_info *) NULL) |
| 2210 | { |
| 2211 | png_destroy_read_struct(&ping,&ping_info,(png_info **) NULL); |
| 2212 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
| 2213 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2214 | |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 2215 | ping_pixels=(unsigned char *) NULL; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2216 | |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2217 | if (setjmp(png_jmpbuf(ping))) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2218 | { |
| 2219 | /* |
| 2220 | PNG image is corrupt. |
| 2221 | */ |
| 2222 | png_destroy_read_struct(&ping,&ping_info,&end_info); |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 2223 | |
| 2224 | #ifdef PNG_SETJMP_NOT_THREAD_SAFE |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 2225 | UnlockSemaphoreInfo(ping_semaphore); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2226 | #endif |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 2227 | |
| 2228 | if (ping_pixels != (unsigned char *) NULL) |
| 2229 | ping_pixels=(unsigned char *) RelinquishMagickMemory(ping_pixels); |
| 2230 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2231 | if (logging != MagickFalse) |
| 2232 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2233 | " exit ReadOnePNGImage() with error."); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2234 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2235 | if (image != (Image *) NULL) |
cristy | 7b755eb | 2009-12-05 14:51:29 +0000 | [diff] [blame] | 2236 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 2237 | InheritException(exception,exception); |
cristy | 7b755eb | 2009-12-05 14:51:29 +0000 | [diff] [blame] | 2238 | image->columns=0; |
| 2239 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2240 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2241 | return(GetFirstImageInList(image)); |
| 2242 | } |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 2243 | |
| 2244 | /* { For navigation to end of SETJMP-protected block. Within this |
| 2245 | * block, use png_error() instead of Throwing an Exception, to ensure |
| 2246 | * that libpng is able to clean up, and that the semaphore is unlocked. |
| 2247 | */ |
| 2248 | |
| 2249 | #ifdef PNG_SETJMP_NOT_THREAD_SAFE |
| 2250 | LockSemaphoreInfo(ping_semaphore); |
| 2251 | #endif |
| 2252 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2253 | /* |
| 2254 | Prepare PNG for reading. |
| 2255 | */ |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2256 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2257 | mng_info->image_found++; |
| 2258 | png_set_sig_bytes(ping,8); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2259 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2260 | if (LocaleCompare(image_info->magick,"MNG") == 0) |
| 2261 | { |
| 2262 | #if defined(PNG_MNG_FEATURES_SUPPORTED) |
| 2263 | (void) png_permit_mng_features(ping,PNG_ALL_MNG_FEATURES); |
| 2264 | png_set_read_fn(ping,image,png_get_data); |
| 2265 | #else |
| 2266 | #if defined(PNG_READ_EMPTY_PLTE_SUPPORTED) |
| 2267 | png_permit_empty_plte(ping,MagickTrue); |
| 2268 | png_set_read_fn(ping,image,png_get_data); |
| 2269 | #else |
| 2270 | mng_info->image=image; |
| 2271 | mng_info->bytes_in_read_buffer=0; |
| 2272 | mng_info->found_empty_plte=MagickFalse; |
| 2273 | mng_info->have_saved_bkgd_index=MagickFalse; |
| 2274 | png_set_read_fn(ping,mng_info,mng_get_data); |
| 2275 | #endif |
| 2276 | #endif |
| 2277 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2278 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2279 | else |
| 2280 | png_set_read_fn(ping,image,png_get_data); |
| 2281 | |
| 2282 | #if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED) |
| 2283 | /* Ignore unused chunks and all unknown chunks except for vpAg */ |
| 2284 | png_set_keep_unknown_chunks(ping, 1, NULL, 0); |
| 2285 | png_set_keep_unknown_chunks(ping, 2, mng_vpAg, 1); |
| 2286 | png_set_keep_unknown_chunks(ping, 1, unused_chunks, |
| 2287 | (int)sizeof(unused_chunks)/5); |
| 2288 | /* Callback for other unknown chunks */ |
| 2289 | png_set_read_user_chunk_fn(ping, image, read_vpag_chunk_callback); |
| 2290 | #endif |
| 2291 | |
glennrp | 2feb141 | 2013-01-22 15:02:16 +0000 | [diff] [blame] | 2292 | #ifdef PNG_SET_USER_LIMITS_SUPPORTED |
glennrp | 09cd962 | 2013-01-22 15:17:38 +0000 | [diff] [blame] | 2293 | # if (PNG_LIBPNG_VER >= 10400) |
glennrp | 2feb141 | 2013-01-22 15:02:16 +0000 | [diff] [blame] | 2294 | /* Limit the size of the chunk storage cache used for sPLT, text, |
glennrp | 6873619 | 2013-01-24 06:32:08 +0000 | [diff] [blame] | 2295 | * and unknown chunks. |
glennrp | 2feb141 | 2013-01-22 15:02:16 +0000 | [diff] [blame] | 2296 | */ |
glennrp | 6873619 | 2013-01-24 06:32:08 +0000 | [diff] [blame] | 2297 | png_set_chunk_cache_max(ping, 32767); |
glennrp | 09cd962 | 2013-01-22 15:17:38 +0000 | [diff] [blame] | 2298 | # endif |
glennrp | 2feb141 | 2013-01-22 15:02:16 +0000 | [diff] [blame] | 2299 | #endif |
| 2300 | |
glennrp | 9bf97b6 | 2012-06-06 21:03:14 +0000 | [diff] [blame] | 2301 | #ifdef PNG_READ_CHECK_FOR_INVALID_INDEX_SUPPORTED |
| 2302 | /* Disable new libpng-1.5.10 feature */ |
| 2303 | png_set_check_for_invalid_index (ping, 0); |
| 2304 | #endif |
| 2305 | |
glennrp | 991e92a | 2010-01-28 03:09:00 +0000 | [diff] [blame] | 2306 | #if (PNG_LIBPNG_VER < 10400) |
| 2307 | # if defined(PNG_USE_PNGGCCRD) && defined(PNG_ASSEMBLER_CODE_SUPPORTED) && \ |
| 2308 | (PNG_LIBPNG_VER >= 10200) && (PNG_LIBPNG_VER < 10220) && defined(__i386__) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2309 | /* Disable thread-unsafe features of pnggccrd */ |
| 2310 | if (png_access_version_number() >= 10200) |
| 2311 | { |
| 2312 | png_uint_32 mmx_disable_mask=0; |
| 2313 | png_uint_32 asm_flags; |
| 2314 | |
| 2315 | mmx_disable_mask |= ( PNG_ASM_FLAG_MMX_READ_COMBINE_ROW \ |
| 2316 | | PNG_ASM_FLAG_MMX_READ_FILTER_SUB \ |
| 2317 | | PNG_ASM_FLAG_MMX_READ_FILTER_AVG \ |
| 2318 | | PNG_ASM_FLAG_MMX_READ_FILTER_PAETH ); |
| 2319 | asm_flags=png_get_asm_flags(ping); |
| 2320 | png_set_asm_flags(ping, asm_flags & ~mmx_disable_mask); |
| 2321 | } |
glennrp | 991e92a | 2010-01-28 03:09:00 +0000 | [diff] [blame] | 2322 | # endif |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2323 | #endif |
| 2324 | |
| 2325 | png_read_info(ping,ping_info); |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2326 | |
| 2327 | png_get_IHDR(ping,ping_info,&ping_width,&ping_height, |
| 2328 | &ping_bit_depth,&ping_color_type, |
| 2329 | &ping_interlace_method,&ping_compression_method, |
| 2330 | &ping_filter_method); |
| 2331 | |
glennrp | fcf0616 | 2012-11-05 14:57:08 +0000 | [diff] [blame] | 2332 | ping_file_depth = ping_bit_depth; |
| 2333 | |
glennrp | 5830fbc | 2013-01-27 06:11:45 +0000 | [diff] [blame] | 2334 | /* Save bit-depth and color-type in case we later want to write a PNG00 */ |
| 2335 | { |
| 2336 | char |
| 2337 | msg[MaxTextExtent]; |
| 2338 | |
| 2339 | (void) FormatLocaleString(msg,MaxTextExtent,"%d",(int) ping_color_type); |
| 2340 | (void) SetImageProperty(image,"png:IHDR.color-type-orig ",msg,exception); |
| 2341 | |
| 2342 | (void) FormatLocaleString(msg,MaxTextExtent,"%d",(int) ping_bit_depth); |
| 2343 | (void) SetImageProperty(image,"png:IHDR.bit-depth-orig ",msg,exception); |
| 2344 | } |
| 2345 | |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2346 | (void) png_get_tRNS(ping, ping_info, &ping_trans_alpha, &ping_num_trans, |
| 2347 | &ping_trans_color); |
| 2348 | |
| 2349 | (void) png_get_bKGD(ping, ping_info, &ping_background); |
| 2350 | |
| 2351 | if (ping_bit_depth < 8) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2352 | { |
glennrp | fcf0616 | 2012-11-05 14:57:08 +0000 | [diff] [blame] | 2353 | png_set_packing(ping); |
| 2354 | ping_bit_depth = 8; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2355 | } |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2356 | |
| 2357 | image->depth=ping_bit_depth; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2358 | image->depth=GetImageQuantumDepth(image,MagickFalse); |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2359 | image->interlace=ping_interlace_method != 0 ? PNGInterlace : NoInterlace; |
glennrp | 98b83d4 | 2012-07-23 02:50:31 +0000 | [diff] [blame] | 2360 | |
cristy | 176b29a | 2012-06-21 13:35:15 +0000 | [diff] [blame] | 2361 | if (((int) ping_color_type == PNG_COLOR_TYPE_GRAY) || |
| 2362 | ((int) ping_color_type == PNG_COLOR_TYPE_GRAY_ALPHA)) |
| 2363 | { |
| 2364 | image->rendering_intent=UndefinedIntent; |
glennrp | 98b83d4 | 2012-07-23 02:50:31 +0000 | [diff] [blame] | 2365 | intent=Magick_RenderingIntent_to_PNG_RenderingIntent(UndefinedIntent); |
cristy | 176b29a | 2012-06-21 13:35:15 +0000 | [diff] [blame] | 2366 | image->gamma=1.000; |
| 2367 | (void) ResetMagickMemory(&image->chromaticity,0, |
| 2368 | sizeof(image->chromaticity)); |
| 2369 | } |
glennrp | 98b83d4 | 2012-07-23 02:50:31 +0000 | [diff] [blame] | 2370 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2371 | if (logging != MagickFalse) |
| 2372 | { |
| 2373 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 2374 | " PNG width: %.20g, height: %.20g", |
| 2375 | (double) ping_width, (double) ping_height); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2376 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2377 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2378 | " PNG color_type: %d, bit_depth: %d", |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2379 | ping_color_type, ping_bit_depth); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2380 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2381 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2382 | " PNG compression_method: %d", |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2383 | ping_compression_method); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2384 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2385 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2386 | " PNG interlace_method: %d, filter_method: %d", |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2387 | ping_interlace_method,ping_filter_method); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2388 | } |
| 2389 | |
glennrp | 98b83d4 | 2012-07-23 02:50:31 +0000 | [diff] [blame] | 2390 | if (png_get_valid(ping,ping_info,PNG_INFO_gAMA)) |
| 2391 | { |
| 2392 | ping_found_gAMA=MagickTrue; |
| 2393 | if (logging != MagickFalse) |
| 2394 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2395 | " Found PNG gAMA chunk."); |
| 2396 | } |
| 2397 | |
| 2398 | if (png_get_valid(ping,ping_info,PNG_INFO_cHRM)) |
| 2399 | { |
| 2400 | ping_found_cHRM=MagickTrue; |
| 2401 | if (logging != MagickFalse) |
| 2402 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2403 | " Found PNG cHRM chunk."); |
| 2404 | } |
| 2405 | |
| 2406 | if (png_get_valid(ping,ping_info,PNG_INFO_iCCP)) |
| 2407 | { |
| 2408 | ping_found_iCCP=MagickTrue; |
| 2409 | if (logging != MagickFalse) |
| 2410 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2411 | " Found PNG iCCP chunk."); |
| 2412 | } |
| 2413 | |
| 2414 | if (png_get_valid(ping,ping_info,PNG_INFO_sRGB)) |
| 2415 | { |
| 2416 | ping_found_sRGB=MagickTrue; |
| 2417 | if (logging != MagickFalse) |
| 2418 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2419 | " Found PNG sRGB chunk."); |
| 2420 | } |
| 2421 | |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2422 | #ifdef PNG_READ_iCCP_SUPPORTED |
| 2423 | if (png_get_valid(ping,ping_info,PNG_INFO_iCCP)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2424 | { |
| 2425 | int |
| 2426 | compression; |
| 2427 | |
glennrp | e4017e3 | 2011-01-08 17:16:09 +0000 | [diff] [blame] | 2428 | #if (PNG_LIBPNG_VER < 10500) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2429 | png_charp |
glennrp | e4017e3 | 2011-01-08 17:16:09 +0000 | [diff] [blame] | 2430 | info; |
| 2431 | #else |
| 2432 | png_bytep |
| 2433 | info; |
| 2434 | #endif |
| 2435 | |
| 2436 | png_charp |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2437 | name; |
| 2438 | |
| 2439 | png_uint_32 |
| 2440 | profile_length; |
| 2441 | |
| 2442 | (void) png_get_iCCP(ping,ping_info,&name,(int *) &compression,&info, |
| 2443 | &profile_length); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2444 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2445 | if (profile_length != 0) |
| 2446 | { |
| 2447 | StringInfo |
| 2448 | *profile; |
| 2449 | |
| 2450 | if (logging != MagickFalse) |
| 2451 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2452 | " Reading PNG iCCP chunk."); |
cristy | e8f8f38 | 2011-09-01 13:32:37 +0000 | [diff] [blame] | 2453 | profile=BlobToStringInfo(info,profile_length); |
| 2454 | if (profile == (StringInfo *) NULL) |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 2455 | { |
| 2456 | png_warning(ping, "ICC profile is NULL"); |
| 2457 | profile=DestroyStringInfo(profile); |
| 2458 | } |
| 2459 | else |
| 2460 | { |
| 2461 | (void) SetImageProfile(image,"icc",profile,exception); |
| 2462 | profile=DestroyStringInfo(profile); |
| 2463 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2464 | } |
| 2465 | } |
| 2466 | #endif |
| 2467 | #if defined(PNG_READ_sRGB_SUPPORTED) |
| 2468 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2469 | if (mng_info->have_global_srgb) |
cristy | 2ea7a8e | 2012-02-08 01:04:50 +0000 | [diff] [blame] | 2470 | { |
cristy | 2ea7a8e | 2012-02-08 01:04:50 +0000 | [diff] [blame] | 2471 | image->rendering_intent=Magick_RenderingIntent_from_PNG_RenderingIntent |
| 2472 | (mng_info->global_srgb_intent); |
| 2473 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2474 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2475 | if (png_get_sRGB(ping,ping_info,&intent)) |
| 2476 | { |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 2477 | image->rendering_intent=Magick_RenderingIntent_from_PNG_RenderingIntent |
| 2478 | (intent); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2479 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2480 | if (logging != MagickFalse) |
| 2481 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | e610a07 | 2010-08-05 17:08:46 +0000 | [diff] [blame] | 2482 | " Reading PNG sRGB chunk: rendering_intent: %d",intent); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2483 | } |
| 2484 | } |
| 2485 | #endif |
| 2486 | { |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2487 | if (!png_get_gAMA(ping,ping_info,&file_gamma)) |
| 2488 | if (mng_info->have_global_gama) |
| 2489 | png_set_gAMA(ping,ping_info,mng_info->global_gamma); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2490 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2491 | if (png_get_gAMA(ping,ping_info,&file_gamma)) |
| 2492 | { |
| 2493 | image->gamma=(float) file_gamma; |
| 2494 | if (logging != MagickFalse) |
| 2495 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2496 | " Reading PNG gAMA chunk: gamma: %f",file_gamma); |
| 2497 | } |
| 2498 | } |
glennrp | 98b83d4 | 2012-07-23 02:50:31 +0000 | [diff] [blame] | 2499 | |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2500 | if (!png_get_valid(ping,ping_info,PNG_INFO_cHRM)) |
| 2501 | { |
| 2502 | if (mng_info->have_global_chrm != MagickFalse) |
| 2503 | { |
| 2504 | (void) png_set_cHRM(ping,ping_info, |
| 2505 | mng_info->global_chrm.white_point.x, |
| 2506 | mng_info->global_chrm.white_point.y, |
| 2507 | mng_info->global_chrm.red_primary.x, |
| 2508 | mng_info->global_chrm.red_primary.y, |
| 2509 | mng_info->global_chrm.green_primary.x, |
| 2510 | mng_info->global_chrm.green_primary.y, |
| 2511 | mng_info->global_chrm.blue_primary.x, |
| 2512 | mng_info->global_chrm.blue_primary.y); |
| 2513 | } |
| 2514 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2515 | |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2516 | if (png_get_valid(ping,ping_info,PNG_INFO_cHRM)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2517 | { |
| 2518 | (void) png_get_cHRM(ping,ping_info, |
| 2519 | &image->chromaticity.white_point.x, |
| 2520 | &image->chromaticity.white_point.y, |
| 2521 | &image->chromaticity.red_primary.x, |
| 2522 | &image->chromaticity.red_primary.y, |
| 2523 | &image->chromaticity.green_primary.x, |
| 2524 | &image->chromaticity.green_primary.y, |
| 2525 | &image->chromaticity.blue_primary.x, |
| 2526 | &image->chromaticity.blue_primary.y); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2527 | |
glennrp | 5cf1bff | 2013-01-16 02:44:03 +0000 | [diff] [blame] | 2528 | ping_found_cHRM=MagickTrue; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2529 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2530 | |
glennrp | e610a07 | 2010-08-05 17:08:46 +0000 | [diff] [blame] | 2531 | if (image->rendering_intent != UndefinedIntent) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2532 | { |
glennrp | 5cf1bff | 2013-01-16 02:44:03 +0000 | [diff] [blame] | 2533 | if (ping_found_sRGB != MagickTrue) |
| 2534 | { |
| 2535 | png_set_sRGB(ping,ping_info, |
| 2536 | Magick_RenderingIntent_to_PNG_RenderingIntent |
| 2537 | (image->rendering_intent)); |
| 2538 | png_set_gAMA(ping,ping_info,1.000f/2.200f); |
| 2539 | file_gamma=1.000f/2.200f; |
| 2540 | ping_found_sRGB=MagickTrue; |
| 2541 | ping_found_cHRM=MagickTrue; |
| 2542 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2543 | } |
glennrp | 5cf1bff | 2013-01-16 02:44:03 +0000 | [diff] [blame] | 2544 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2545 | #if defined(PNG_oFFs_SUPPORTED) |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2546 | if (png_get_valid(ping,ping_info,PNG_INFO_oFFs)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2547 | { |
cristy | 905ef80 | 2011-02-23 00:29:18 +0000 | [diff] [blame] | 2548 | image->page.x=(ssize_t) png_get_x_offset_pixels(ping, ping_info); |
| 2549 | image->page.y=(ssize_t) png_get_y_offset_pixels(ping, ping_info); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2550 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2551 | if (logging != MagickFalse) |
| 2552 | if (image->page.x || image->page.y) |
| 2553 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 2554 | " Reading PNG oFFs chunk: x: %.20g, y: %.20g.",(double) |
| 2555 | image->page.x,(double) image->page.y); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2556 | } |
| 2557 | #endif |
| 2558 | #if defined(PNG_pHYs_SUPPORTED) |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2559 | if (!png_get_valid(ping,ping_info,PNG_INFO_pHYs)) |
| 2560 | { |
| 2561 | if (mng_info->have_global_phys) |
| 2562 | { |
| 2563 | png_set_pHYs(ping,ping_info, |
| 2564 | mng_info->global_x_pixels_per_unit, |
| 2565 | mng_info->global_y_pixels_per_unit, |
| 2566 | mng_info->global_phys_unit_type); |
| 2567 | } |
| 2568 | } |
| 2569 | |
| 2570 | if (png_get_valid(ping,ping_info,PNG_INFO_pHYs)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2571 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2572 | /* |
| 2573 | Set image resolution. |
| 2574 | */ |
| 2575 | (void) png_get_pHYs(ping,ping_info,&x_resolution,&y_resolution, |
cristy | 0881b52 | 2010-04-24 23:45:19 +0000 | [diff] [blame] | 2576 | &unit_type); |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 2577 | image->resolution.x=(double) x_resolution; |
| 2578 | image->resolution.y=(double) y_resolution; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2579 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2580 | if (unit_type == PNG_RESOLUTION_METER) |
| 2581 | { |
| 2582 | image->units=PixelsPerCentimeterResolution; |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 2583 | image->resolution.x=(double) x_resolution/100.0; |
| 2584 | image->resolution.y=(double) y_resolution/100.0; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2585 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2586 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2587 | if (logging != MagickFalse) |
| 2588 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 2589 | " Reading PNG pHYs chunk: xres: %.20g, yres: %.20g, units: %d.", |
| 2590 | (double) x_resolution,(double) y_resolution,unit_type); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2591 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2592 | #endif |
glennrp | 823b55c | 2011-03-14 18:46:46 +0000 | [diff] [blame] | 2593 | |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2594 | if (png_get_valid(ping,ping_info,PNG_INFO_PLTE)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2595 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2596 | png_colorp |
| 2597 | palette; |
| 2598 | |
| 2599 | (void) png_get_PLTE(ping,ping_info,&palette,&number_colors); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2600 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2601 | if ((number_colors == 0) && |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2602 | ((int) ping_color_type == PNG_COLOR_TYPE_PALETTE)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2603 | { |
| 2604 | if (mng_info->global_plte_length) |
| 2605 | { |
| 2606 | png_set_PLTE(ping,ping_info,mng_info->global_plte, |
| 2607 | (int) mng_info->global_plte_length); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2608 | |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2609 | if (!png_get_valid(ping,ping_info,PNG_INFO_tRNS)) |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 2610 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2611 | if (mng_info->global_trns_length) |
| 2612 | { |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 2613 | png_warning(ping, |
| 2614 | "global tRNS has more entries than global PLTE"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2615 | } |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 2616 | else |
| 2617 | { |
| 2618 | png_set_tRNS(ping,ping_info,mng_info->global_trns, |
| 2619 | (int) mng_info->global_trns_length,NULL); |
| 2620 | } |
| 2621 | } |
glennrp | bfd9e61 | 2011-04-22 14:02:20 +0000 | [diff] [blame] | 2622 | #ifdef PNG_READ_bKGD_SUPPORTED |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2623 | if ( |
| 2624 | #ifndef PNG_READ_EMPTY_PLTE_SUPPORTED |
| 2625 | mng_info->have_saved_bkgd_index || |
| 2626 | #endif |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2627 | png_get_valid(ping,ping_info,PNG_INFO_bKGD)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2628 | { |
| 2629 | png_color_16 |
| 2630 | background; |
| 2631 | |
| 2632 | #ifndef PNG_READ_EMPTY_PLTE_SUPPORTED |
| 2633 | if (mng_info->have_saved_bkgd_index) |
| 2634 | background.index=mng_info->saved_bkgd_index; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2635 | #endif |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2636 | if (png_get_valid(ping, ping_info, PNG_INFO_bKGD)) |
| 2637 | background.index=ping_background->index; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2638 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2639 | background.red=(png_uint_16) |
| 2640 | mng_info->global_plte[background.index].red; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2641 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2642 | background.green=(png_uint_16) |
| 2643 | mng_info->global_plte[background.index].green; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2644 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2645 | background.blue=(png_uint_16) |
| 2646 | mng_info->global_plte[background.index].blue; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2647 | |
glennrp | c6c391a | 2011-04-27 02:23:56 +0000 | [diff] [blame] | 2648 | background.gray=(png_uint_16) |
| 2649 | mng_info->global_plte[background.index].green; |
| 2650 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2651 | png_set_bKGD(ping,ping_info,&background); |
| 2652 | } |
| 2653 | #endif |
| 2654 | } |
| 2655 | else |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 2656 | png_error(ping,"No global PLTE in file"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2657 | } |
| 2658 | } |
| 2659 | |
glennrp | bfd9e61 | 2011-04-22 14:02:20 +0000 | [diff] [blame] | 2660 | #ifdef PNG_READ_bKGD_SUPPORTED |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2661 | if (mng_info->have_global_bkgd && |
| 2662 | (!png_get_valid(ping,ping_info,PNG_INFO_bKGD))) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2663 | image->background_color=mng_info->mng_global_bkgd; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2664 | |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2665 | if (png_get_valid(ping,ping_info,PNG_INFO_bKGD)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2666 | { |
glennrp | bfd9e61 | 2011-04-22 14:02:20 +0000 | [diff] [blame] | 2667 | unsigned int |
| 2668 | bkgd_scale; |
| 2669 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2670 | /* |
| 2671 | Set image background color. |
| 2672 | */ |
| 2673 | if (logging != MagickFalse) |
| 2674 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2675 | " Reading PNG bKGD chunk."); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2676 | |
glennrp | bfd9e61 | 2011-04-22 14:02:20 +0000 | [diff] [blame] | 2677 | /* Scale background components to 16-bit, then scale |
| 2678 | * to quantum depth |
| 2679 | */ |
| 2680 | if (logging != MagickFalse) |
| 2681 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2682 | " raw ping_background=(%d,%d,%d).",ping_background->red, |
| 2683 | ping_background->green,ping_background->blue); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2684 | |
glennrp | bfd9e61 | 2011-04-22 14:02:20 +0000 | [diff] [blame] | 2685 | bkgd_scale = 1; |
glennrp | 2cbb448 | 2010-06-02 04:37:24 +0000 | [diff] [blame] | 2686 | |
glennrp | fcf0616 | 2012-11-05 14:57:08 +0000 | [diff] [blame] | 2687 | if (ping_file_depth == 1) |
glennrp | bfd9e61 | 2011-04-22 14:02:20 +0000 | [diff] [blame] | 2688 | bkgd_scale = 255; |
glennrp | 2cbb448 | 2010-06-02 04:37:24 +0000 | [diff] [blame] | 2689 | |
glennrp | fcf0616 | 2012-11-05 14:57:08 +0000 | [diff] [blame] | 2690 | else if (ping_file_depth == 2) |
glennrp | bfd9e61 | 2011-04-22 14:02:20 +0000 | [diff] [blame] | 2691 | bkgd_scale = 85; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2692 | |
glennrp | fcf0616 | 2012-11-05 14:57:08 +0000 | [diff] [blame] | 2693 | else if (ping_file_depth == 4) |
glennrp | bfd9e61 | 2011-04-22 14:02:20 +0000 | [diff] [blame] | 2694 | bkgd_scale = 17; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2695 | |
glennrp | fcf0616 | 2012-11-05 14:57:08 +0000 | [diff] [blame] | 2696 | if (ping_file_depth <= 8) |
glennrp | bfd9e61 | 2011-04-22 14:02:20 +0000 | [diff] [blame] | 2697 | bkgd_scale *= 257; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2698 | |
glennrp | bfd9e61 | 2011-04-22 14:02:20 +0000 | [diff] [blame] | 2699 | ping_background->red *= bkgd_scale; |
| 2700 | ping_background->green *= bkgd_scale; |
| 2701 | ping_background->blue *= bkgd_scale; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2702 | |
glennrp | bfd9e61 | 2011-04-22 14:02:20 +0000 | [diff] [blame] | 2703 | if (logging != MagickFalse) |
| 2704 | { |
glennrp | 2cbb448 | 2010-06-02 04:37:24 +0000 | [diff] [blame] | 2705 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2706 | " bkgd_scale=%d.",bkgd_scale); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2707 | |
glennrp | 2cbb448 | 2010-06-02 04:37:24 +0000 | [diff] [blame] | 2708 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2709 | " ping_background=(%d,%d,%d).",ping_background->red, |
| 2710 | ping_background->green,ping_background->blue); |
glennrp | bfd9e61 | 2011-04-22 14:02:20 +0000 | [diff] [blame] | 2711 | } |
glennrp | 2cbb448 | 2010-06-02 04:37:24 +0000 | [diff] [blame] | 2712 | |
glennrp | bfd9e61 | 2011-04-22 14:02:20 +0000 | [diff] [blame] | 2713 | image->background_color.red= |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2714 | ScaleShortToQuantum(ping_background->red); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2715 | |
glennrp | bfd9e61 | 2011-04-22 14:02:20 +0000 | [diff] [blame] | 2716 | image->background_color.green= |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2717 | ScaleShortToQuantum(ping_background->green); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2718 | |
glennrp | bfd9e61 | 2011-04-22 14:02:20 +0000 | [diff] [blame] | 2719 | image->background_color.blue= |
| 2720 | ScaleShortToQuantum(ping_background->blue); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2721 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 2722 | image->background_color.alpha=OpaqueAlpha; |
glennrp | 2cbb448 | 2010-06-02 04:37:24 +0000 | [diff] [blame] | 2723 | |
glennrp | bfd9e61 | 2011-04-22 14:02:20 +0000 | [diff] [blame] | 2724 | if (logging != MagickFalse) |
| 2725 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2726 | " image->background_color=(%.20g,%.20g,%.20g).", |
| 2727 | (double) image->background_color.red, |
| 2728 | (double) image->background_color.green, |
| 2729 | (double) image->background_color.blue); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2730 | } |
glennrp | bfd9e61 | 2011-04-22 14:02:20 +0000 | [diff] [blame] | 2731 | #endif /* PNG_READ_bKGD_SUPPORTED */ |
glennrp | a6a0663 | 2011-01-19 15:15:34 +0000 | [diff] [blame] | 2732 | |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2733 | if (png_get_valid(ping,ping_info,PNG_INFO_tRNS)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2734 | { |
| 2735 | /* |
glennrp | a6a0663 | 2011-01-19 15:15:34 +0000 | [diff] [blame] | 2736 | Image has a tRNS chunk. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2737 | */ |
| 2738 | int |
| 2739 | max_sample; |
| 2740 | |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 2741 | size_t |
| 2742 | one=1; |
| 2743 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2744 | if (logging != MagickFalse) |
| 2745 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2746 | " Reading PNG tRNS chunk."); |
| 2747 | |
glennrp | fcf0616 | 2012-11-05 14:57:08 +0000 | [diff] [blame] | 2748 | max_sample = (int) ((one << ping_file_depth) - 1); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2749 | |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2750 | if ((ping_color_type == PNG_COLOR_TYPE_GRAY && |
| 2751 | (int)ping_trans_color->gray > max_sample) || |
| 2752 | (ping_color_type == PNG_COLOR_TYPE_RGB && |
| 2753 | ((int)ping_trans_color->red > max_sample || |
| 2754 | (int)ping_trans_color->green > max_sample || |
| 2755 | (int)ping_trans_color->blue > max_sample))) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2756 | { |
| 2757 | if (logging != MagickFalse) |
| 2758 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2759 | " Ignoring PNG tRNS chunk with out-of-range sample."); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2760 | png_free_data(ping, ping_info, PNG_FREE_TRNS, 0); |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2761 | png_set_invalid(ping,ping_info,PNG_INFO_tRNS); |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 2762 | image->alpha_trait=UndefinedPixelTrait; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2763 | } |
| 2764 | else |
| 2765 | { |
glennrp | a6a0663 | 2011-01-19 15:15:34 +0000 | [diff] [blame] | 2766 | int |
| 2767 | scale_to_short; |
| 2768 | |
glennrp | fcf0616 | 2012-11-05 14:57:08 +0000 | [diff] [blame] | 2769 | scale_to_short = 65535L/((1UL << ping_file_depth)-1); |
glennrp | a6a0663 | 2011-01-19 15:15:34 +0000 | [diff] [blame] | 2770 | |
| 2771 | /* Scale transparent_color to short */ |
| 2772 | transparent_color.red= scale_to_short*ping_trans_color->red; |
| 2773 | transparent_color.green= scale_to_short*ping_trans_color->green; |
| 2774 | transparent_color.blue= scale_to_short*ping_trans_color->blue; |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 2775 | transparent_color.alpha= scale_to_short*ping_trans_color->gray; |
glennrp | 05eb4a9 | 2010-07-08 02:21:09 +0000 | [diff] [blame] | 2776 | |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2777 | if (ping_color_type == PNG_COLOR_TYPE_GRAY) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2778 | { |
glennrp | 0f11198 | 2010-07-07 20:18:33 +0000 | [diff] [blame] | 2779 | if (logging != MagickFalse) |
| 2780 | { |
| 2781 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2782 | " Raw tRNS graylevel is %d.",ping_trans_color->gray); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2783 | |
glennrp | 0f11198 | 2010-07-07 20:18:33 +0000 | [diff] [blame] | 2784 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 2785 | " scaled graylevel is %.20g.",transparent_color.alpha); |
glennrp | 0f11198 | 2010-07-07 20:18:33 +0000 | [diff] [blame] | 2786 | } |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 2787 | transparent_color.red=transparent_color.alpha; |
| 2788 | transparent_color.green=transparent_color.alpha; |
| 2789 | transparent_color.blue=transparent_color.alpha; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2790 | } |
| 2791 | } |
| 2792 | } |
| 2793 | #if defined(PNG_READ_sBIT_SUPPORTED) |
| 2794 | if (mng_info->have_global_sbit) |
| 2795 | { |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2796 | if (!png_get_valid(ping,ping_info,PNG_INFO_sBIT)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2797 | png_set_sBIT(ping,ping_info,&mng_info->global_sbit); |
| 2798 | } |
| 2799 | #endif |
| 2800 | num_passes=png_set_interlace_handling(ping); |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2801 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2802 | png_read_update_info(ping,ping_info); |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2803 | |
| 2804 | ping_rowbytes=png_get_rowbytes(ping,ping_info); |
| 2805 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2806 | /* |
| 2807 | Initialize image structure. |
| 2808 | */ |
| 2809 | mng_info->image_box.left=0; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2810 | mng_info->image_box.right=(ssize_t) ping_width; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2811 | mng_info->image_box.top=0; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2812 | mng_info->image_box.bottom=(ssize_t) ping_height; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2813 | if (mng_info->mng_type == 0) |
| 2814 | { |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2815 | mng_info->mng_width=ping_width; |
| 2816 | mng_info->mng_height=ping_height; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2817 | mng_info->frame=mng_info->image_box; |
| 2818 | mng_info->clip=mng_info->image_box; |
| 2819 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2820 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2821 | else |
| 2822 | { |
| 2823 | image->page.y=mng_info->y_off[mng_info->object_id]; |
| 2824 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2825 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2826 | image->compression=ZipCompression; |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2827 | image->columns=ping_width; |
| 2828 | image->rows=ping_height; |
glennrp | a6c5d34 | 2012-05-14 13:21:17 +0000 | [diff] [blame] | 2829 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 2830 | if (((int) ping_color_type == PNG_COLOR_TYPE_GRAY) || |
| 2831 | ((int) ping_color_type == PNG_COLOR_TYPE_GRAY_ALPHA)) |
glennrp | 8d0bca5 | 2012-05-17 02:33:23 +0000 | [diff] [blame] | 2832 | { |
glennrp | e88af77 | 2012-08-22 13:59:50 +0000 | [diff] [blame] | 2833 | if ((!png_get_valid(ping,ping_info,PNG_INFO_gAMA) || |
| 2834 | image->gamma == 1.0) && |
glennrp | 5cf1bff | 2013-01-16 02:44:03 +0000 | [diff] [blame] | 2835 | ping_found_cHRM != MagickTrue && ping_found_sRGB != MagickTrue) |
glennrp | 8d0bca5 | 2012-05-17 02:33:23 +0000 | [diff] [blame] | 2836 | { |
| 2837 | /* Set image->gamma to 1.0, image->rendering_intent to Undefined, |
glennrp | e88af77 | 2012-08-22 13:59:50 +0000 | [diff] [blame] | 2838 | * image->colorspace to GRAY, and reset image->chromaticity. |
glennrp | 8d0bca5 | 2012-05-17 02:33:23 +0000 | [diff] [blame] | 2839 | */ |
| 2840 | SetImageColorspace(image,GRAYColorspace,exception); |
| 2841 | } |
glennrp | 8d0bca5 | 2012-05-17 02:33:23 +0000 | [diff] [blame] | 2842 | } |
glennrp | e88af77 | 2012-08-22 13:59:50 +0000 | [diff] [blame] | 2843 | |
| 2844 | (void)LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2845 | " image->colorspace=%d",(int) image->colorspace); |
glennrp | a6c5d34 | 2012-05-14 13:21:17 +0000 | [diff] [blame] | 2846 | |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2847 | if (((int) ping_color_type == PNG_COLOR_TYPE_PALETTE) || |
glennrp | 32340ff | 2012-08-25 17:36:19 +0000 | [diff] [blame] | 2848 | ((int) ping_bit_depth < 16 && |
| 2849 | (int) ping_color_type == PNG_COLOR_TYPE_GRAY)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2850 | { |
cristy | befe4d2 | 2010-06-07 01:18:58 +0000 | [diff] [blame] | 2851 | size_t |
| 2852 | one; |
| 2853 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2854 | image->storage_class=PseudoClass; |
cristy | befe4d2 | 2010-06-07 01:18:58 +0000 | [diff] [blame] | 2855 | one=1; |
glennrp | fcf0616 | 2012-11-05 14:57:08 +0000 | [diff] [blame] | 2856 | image->colors=one << ping_file_depth; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2857 | #if (MAGICKCORE_QUANTUM_DEPTH == 8) |
| 2858 | if (image->colors > 256) |
glennrp | 67b9c1a | 2011-04-22 18:47:36 +0000 | [diff] [blame] | 2859 | image->colors=256; |
| 2860 | #else |
| 2861 | if (image->colors > 65536L) |
| 2862 | image->colors=65536L; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2863 | #endif |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2864 | if ((int) ping_color_type == PNG_COLOR_TYPE_PALETTE) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2865 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2866 | png_colorp |
| 2867 | palette; |
| 2868 | |
| 2869 | (void) png_get_PLTE(ping,ping_info,&palette,&number_colors); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2870 | image->colors=(size_t) number_colors; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2871 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2872 | if (logging != MagickFalse) |
| 2873 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2874 | " Reading PNG PLTE chunk: number_colors: %d.",number_colors); |
| 2875 | } |
| 2876 | } |
| 2877 | |
| 2878 | if (image->storage_class == PseudoClass) |
| 2879 | { |
| 2880 | /* |
| 2881 | Initialize image colormap. |
| 2882 | */ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 2883 | if (AcquireImageColormap(image,image->colors,exception) == MagickFalse) |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 2884 | png_error(ping,"Memory allocation failed"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2885 | |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2886 | if ((int) ping_color_type == PNG_COLOR_TYPE_PALETTE) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2887 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2888 | png_colorp |
| 2889 | palette; |
| 2890 | |
| 2891 | (void) png_get_PLTE(ping,ping_info,&palette,&number_colors); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2892 | |
glennrp | 6af6cf1 | 2011-04-22 13:05:16 +0000 | [diff] [blame] | 2893 | for (i=0; i < (ssize_t) number_colors; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2894 | { |
| 2895 | image->colormap[i].red=ScaleCharToQuantum(palette[i].red); |
| 2896 | image->colormap[i].green=ScaleCharToQuantum(palette[i].green); |
| 2897 | image->colormap[i].blue=ScaleCharToQuantum(palette[i].blue); |
| 2898 | } |
glennrp | 6af6cf1 | 2011-04-22 13:05:16 +0000 | [diff] [blame] | 2899 | |
glennrp | 67b9c1a | 2011-04-22 18:47:36 +0000 | [diff] [blame] | 2900 | for ( ; i < (ssize_t) image->colors; i++) |
glennrp | 6af6cf1 | 2011-04-22 13:05:16 +0000 | [diff] [blame] | 2901 | { |
| 2902 | image->colormap[i].red=0; |
| 2903 | image->colormap[i].green=0; |
| 2904 | image->colormap[i].blue=0; |
| 2905 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2906 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2907 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2908 | else |
| 2909 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2910 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2911 | scale; |
| 2912 | |
glennrp | fcf0616 | 2012-11-05 14:57:08 +0000 | [diff] [blame] | 2913 | scale=(QuantumRange/((1UL << ping_file_depth)-1)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2914 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2915 | if (scale < 1) |
| 2916 | scale=1; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2917 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2918 | for (i=0; i < (ssize_t) image->colors; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2919 | { |
| 2920 | image->colormap[i].red=(Quantum) (i*scale); |
| 2921 | image->colormap[i].green=(Quantum) (i*scale); |
| 2922 | image->colormap[i].blue=(Quantum) (i*scale); |
| 2923 | } |
| 2924 | } |
| 2925 | } |
glennrp | 147bc91 | 2011-03-30 18:47:21 +0000 | [diff] [blame] | 2926 | |
glennrp | cb395ac | 2011-03-30 19:50:23 +0000 | [diff] [blame] | 2927 | /* Set some properties for reporting by "identify" */ |
| 2928 | { |
glennrp | 147bc91 | 2011-03-30 18:47:21 +0000 | [diff] [blame] | 2929 | char |
| 2930 | msg[MaxTextExtent]; |
| 2931 | |
glennrp | fcf0616 | 2012-11-05 14:57:08 +0000 | [diff] [blame] | 2932 | /* encode ping_width, ping_height, ping_file_depth, ping_color_type, |
glennrp | 147bc91 | 2011-03-30 18:47:21 +0000 | [diff] [blame] | 2933 | ping_interlace_method in value */ |
| 2934 | |
cristy | 3b6fd2e | 2011-05-20 12:53:50 +0000 | [diff] [blame] | 2935 | (void) FormatLocaleString(msg,MaxTextExtent, |
glennrp | 7cdb11c | 2011-03-31 18:17:25 +0000 | [diff] [blame] | 2936 | "%d, %d",(int) ping_width, (int) ping_height); |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 2937 | (void) SetImageProperty(image,"png:IHDR.width,height ",msg,exception); |
glennrp | 147bc91 | 2011-03-30 18:47:21 +0000 | [diff] [blame] | 2938 | |
glennrp | fcf0616 | 2012-11-05 14:57:08 +0000 | [diff] [blame] | 2939 | (void) FormatLocaleString(msg,MaxTextExtent,"%d",(int) ping_file_depth); |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 2940 | (void) SetImageProperty(image,"png:IHDR.bit_depth ",msg,exception); |
glennrp | 147bc91 | 2011-03-30 18:47:21 +0000 | [diff] [blame] | 2941 | |
glennrp | 5dff435 | 2012-06-06 22:12:04 +0000 | [diff] [blame] | 2942 | (void) FormatLocaleString(msg,MaxTextExtent,"%d (%s)", |
| 2943 | (int) ping_color_type, |
| 2944 | Magick_ColorType_from_PNG_ColorType((int)ping_color_type)); |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 2945 | (void) SetImageProperty(image,"png:IHDR.color_type ",msg,exception); |
glennrp | 147bc91 | 2011-03-30 18:47:21 +0000 | [diff] [blame] | 2946 | |
glennrp | 913f961 | 2012-06-27 17:50:00 +0000 | [diff] [blame] | 2947 | if (ping_interlace_method == 0) |
| 2948 | { |
| 2949 | (void) FormatLocaleString(msg,MaxTextExtent,"%d (Not interlaced)", |
| 2950 | (int) ping_interlace_method); |
| 2951 | } |
| 2952 | else if (ping_interlace_method == 1) |
| 2953 | { |
| 2954 | (void) FormatLocaleString(msg,MaxTextExtent,"%d (Adam7 method)", |
| 2955 | (int) ping_interlace_method); |
| 2956 | } |
| 2957 | else |
| 2958 | { |
| 2959 | (void) FormatLocaleString(msg,MaxTextExtent,"%d (Unknown method)", |
| 2960 | (int) ping_interlace_method); |
| 2961 | } |
| 2962 | (void) SetImageProperty(image,"png:IHDR.interlace_method",msg,exception); |
| 2963 | |
| 2964 | if (number_colors != 0) |
| 2965 | { |
| 2966 | (void) FormatLocaleString(msg,MaxTextExtent,"%d", |
| 2967 | (int) number_colors); |
| 2968 | (void) SetImageProperty(image,"png:PLTE.number_colors ",msg, |
| 2969 | exception); |
| 2970 | } |
glennrp | cb395ac | 2011-03-30 19:50:23 +0000 | [diff] [blame] | 2971 | } |
glennrp | 147bc91 | 2011-03-30 18:47:21 +0000 | [diff] [blame] | 2972 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2973 | /* |
| 2974 | Read image scanlines. |
| 2975 | */ |
| 2976 | if (image->delay != 0) |
| 2977 | mng_info->scenes_found++; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2978 | |
glennrp | 0ca69b1 | 2010-07-26 01:57:52 +0000 | [diff] [blame] | 2979 | if ((mng_info->mng_type == 0 && (image->ping != MagickFalse)) || ( |
glennrp | 347e40f | 2010-06-06 11:27:30 +0000 | [diff] [blame] | 2980 | (image_info->number_scenes != 0) && (mng_info->scenes_found > (ssize_t) |
| 2981 | (image_info->first_scene+image_info->number_scenes)))) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2982 | { |
glennrp | 1b888c4 | 2012-03-02 15:18:14 +0000 | [diff] [blame] | 2983 | /* This happens later in non-ping decodes */ |
| 2984 | if (png_get_valid(ping,ping_info,PNG_INFO_tRNS)) |
| 2985 | image->storage_class=DirectClass; |
| 2986 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2987 | if (logging != MagickFalse) |
| 2988 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 2989 | " Skipping PNG image data for scene %.20g",(double) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2990 | mng_info->scenes_found-1); |
| 2991 | png_destroy_read_struct(&ping,&ping_info,&end_info); |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 2992 | |
| 2993 | #ifdef PNG_SETJMP_NOT_THREAD_SAFE |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 2994 | UnlockSemaphoreInfo(ping_semaphore); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2995 | #endif |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 2996 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2997 | if (logging != MagickFalse) |
| 2998 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2999 | " exit ReadOnePNGImage()."); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3000 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3001 | return(image); |
| 3002 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3003 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3004 | if (logging != MagickFalse) |
| 3005 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 3006 | " Reading PNG IDAT chunk(s)"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3007 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3008 | if (num_passes > 1) |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 3009 | ping_pixels=(unsigned char *) AcquireQuantumMemory(image->rows, |
| 3010 | ping_rowbytes*sizeof(*ping_pixels)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3011 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3012 | else |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 3013 | ping_pixels=(unsigned char *) AcquireQuantumMemory(ping_rowbytes, |
| 3014 | sizeof(*ping_pixels)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3015 | |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 3016 | if (ping_pixels == (unsigned char *) NULL) |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 3017 | png_error(ping,"Memory allocation failed"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3018 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3019 | if (logging != MagickFalse) |
| 3020 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 3021 | " Converting PNG pixels to pixel packets"); |
| 3022 | /* |
| 3023 | Convert PNG pixels to pixel packets. |
| 3024 | */ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3025 | quantum_info=AcquireQuantumInfo(image_info,image); |
| 3026 | |
| 3027 | if (quantum_info == (QuantumInfo *) NULL) |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 3028 | png_error(ping,"Failed to allocate quantum_info"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3029 | |
glennrp | 4b840d7 | 2012-11-22 16:01:16 +0000 | [diff] [blame] | 3030 | (void) SetQuantumEndian(image,quantum_info,MSBEndian); |
| 3031 | |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 3032 | { |
| 3033 | |
| 3034 | MagickBooleanType |
| 3035 | found_transparent_pixel; |
| 3036 | |
| 3037 | found_transparent_pixel=MagickFalse; |
| 3038 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3039 | if (image->storage_class == DirectClass) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3040 | { |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 3041 | for (pass=0; pass < num_passes; pass++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3042 | { |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 3043 | /* |
| 3044 | Convert image to DirectClass pixel packets. |
| 3045 | */ |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 3046 | image->alpha_trait=(((int) ping_color_type == PNG_COLOR_TYPE_RGB_ALPHA) || |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 3047 | ((int) ping_color_type == PNG_COLOR_TYPE_GRAY_ALPHA) || |
| 3048 | (png_get_valid(ping,ping_info,PNG_INFO_tRNS))) ? |
cristy | b0a657e | 2012-08-29 00:45:37 +0000 | [diff] [blame] | 3049 | BlendPixelTrait : UndefinedPixelTrait; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3050 | |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 3051 | for (y=0; y < (ssize_t) image->rows; y++) |
| 3052 | { |
| 3053 | if (num_passes > 1) |
| 3054 | row_offset=ping_rowbytes*y; |
| 3055 | |
| 3056 | else |
| 3057 | row_offset=0; |
| 3058 | |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 3059 | png_read_row(ping,ping_pixels+row_offset,NULL); |
glennrp | c5c31be | 2012-11-07 18:42:18 +0000 | [diff] [blame] | 3060 | |
| 3061 | if (pass < num_passes-1) |
| 3062 | continue; |
| 3063 | |
cristy | 862a33c | 2012-05-17 22:49:37 +0000 | [diff] [blame] | 3064 | q=GetAuthenticPixels(image,0,y,image->columns,1,exception); |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 3065 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3066 | if (q == (Quantum *) NULL) |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 3067 | break; |
| 3068 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3069 | if ((int) ping_color_type == PNG_COLOR_TYPE_GRAY) |
| 3070 | (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info, |
| 3071 | GrayQuantum,ping_pixels+row_offset,exception); |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 3072 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3073 | else if ((int) ping_color_type == PNG_COLOR_TYPE_GRAY_ALPHA) |
| 3074 | (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info, |
| 3075 | GrayAlphaQuantum,ping_pixels+row_offset,exception); |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 3076 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3077 | else if ((int) ping_color_type == PNG_COLOR_TYPE_RGB_ALPHA) |
| 3078 | (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info, |
| 3079 | RGBAQuantum,ping_pixels+row_offset,exception); |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 3080 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3081 | else if ((int) ping_color_type == PNG_COLOR_TYPE_PALETTE) |
| 3082 | (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info, |
| 3083 | IndexQuantum,ping_pixels+row_offset,exception); |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 3084 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3085 | else /* ping_color_type == PNG_COLOR_TYPE_RGB */ |
| 3086 | (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info, |
| 3087 | RGBQuantum,ping_pixels+row_offset,exception); |
glennrp | 3faa9a3 | 2011-04-23 14:00:25 +0000 | [diff] [blame] | 3088 | |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 3089 | if (found_transparent_pixel == MagickFalse) |
| 3090 | { |
| 3091 | /* Is there a transparent pixel in the row? */ |
glennrp | a6a0663 | 2011-01-19 15:15:34 +0000 | [diff] [blame] | 3092 | if (y== 0 && logging != MagickFalse) |
| 3093 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 3094 | " Looking for cheap transparent pixel"); |
| 3095 | |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 3096 | for (x=(ssize_t) image->columns-1; x >= 0; x--) |
| 3097 | { |
glennrp | 5aa37f6 | 2011-01-02 03:07:57 +0000 | [diff] [blame] | 3098 | if ((ping_color_type == PNG_COLOR_TYPE_RGBA || |
| 3099 | ping_color_type == PNG_COLOR_TYPE_GRAY_ALPHA) && |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3100 | (GetPixelAlpha(image,q) != OpaqueAlpha)) |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 3101 | { |
glennrp | a6a0663 | 2011-01-19 15:15:34 +0000 | [diff] [blame] | 3102 | if (logging != MagickFalse) |
| 3103 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 3104 | " ...got one."); |
| 3105 | |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 3106 | found_transparent_pixel = MagickTrue; |
| 3107 | break; |
| 3108 | } |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 3109 | if ((ping_color_type == PNG_COLOR_TYPE_RGB || |
| 3110 | ping_color_type == PNG_COLOR_TYPE_GRAY) && |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3111 | (ScaleQuantumToShort(GetPixelRed(image,q)) == |
| 3112 | transparent_color.red && |
| 3113 | ScaleQuantumToShort(GetPixelGreen(image,q)) == |
| 3114 | transparent_color.green && |
| 3115 | ScaleQuantumToShort(GetPixelBlue(image,q)) == |
| 3116 | transparent_color.blue)) |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 3117 | { |
glennrp | a6a0663 | 2011-01-19 15:15:34 +0000 | [diff] [blame] | 3118 | if (logging != MagickFalse) |
| 3119 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 3120 | " ...got one."); |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 3121 | found_transparent_pixel = MagickTrue; |
| 3122 | break; |
| 3123 | } |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3124 | q+=GetPixelChannels(image); |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 3125 | } |
| 3126 | } |
| 3127 | |
| 3128 | if ((image->previous == (Image *) NULL) && (num_passes == 1)) |
| 3129 | { |
| 3130 | status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y, |
| 3131 | image->rows); |
| 3132 | |
| 3133 | if (status == MagickFalse) |
| 3134 | break; |
| 3135 | } |
| 3136 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 3137 | break; |
| 3138 | } |
| 3139 | |
| 3140 | if ((image->previous == (Image *) NULL) && (num_passes != 1)) |
| 3141 | { |
| 3142 | status=SetImageProgress(image,LoadImageTag,pass,num_passes); |
cristy | 7a287bf | 2010-02-14 02:18:09 +0000 | [diff] [blame] | 3143 | if (status == MagickFalse) |
| 3144 | break; |
| 3145 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3146 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3147 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3148 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3149 | else /* image->storage_class != DirectClass */ |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 3150 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3151 | for (pass=0; pass < num_passes; pass++) |
| 3152 | { |
| 3153 | Quantum |
| 3154 | *quantum_scanline; |
| 3155 | |
| 3156 | register Quantum |
| 3157 | *r; |
| 3158 | |
| 3159 | /* |
| 3160 | Convert grayscale image to PseudoClass pixel packets. |
| 3161 | */ |
glennrp | c17d96f | 2011-06-27 01:20:11 +0000 | [diff] [blame] | 3162 | if (logging != MagickFalse) |
| 3163 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 3164 | " Converting grayscale pixels to pixel packets"); |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3165 | |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 3166 | image->alpha_trait=ping_color_type == PNG_COLOR_TYPE_GRAY_ALPHA ? |
cristy | b0a657e | 2012-08-29 00:45:37 +0000 | [diff] [blame] | 3167 | BlendPixelTrait : UndefinedPixelTrait; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3168 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3169 | quantum_scanline=(Quantum *) AcquireQuantumMemory(image->columns, |
cristy | b0a657e | 2012-08-29 00:45:37 +0000 | [diff] [blame] | 3170 | (image->alpha_trait == BlendPixelTrait? 2 : 1)* |
| 3171 | sizeof(*quantum_scanline)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3172 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3173 | if (quantum_scanline == (Quantum *) NULL) |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 3174 | png_error(ping,"Memory allocation failed"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3175 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3176 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3177 | { |
glennrp | 4f99639 | 2013-01-26 22:56:50 +0000 | [diff] [blame] | 3178 | Quantum |
| 3179 | alpha; |
| 3180 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3181 | if (num_passes > 1) |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 3182 | row_offset=ping_rowbytes*y; |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 3183 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3184 | else |
| 3185 | row_offset=0; |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 3186 | |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 3187 | png_read_row(ping,ping_pixels+row_offset,NULL); |
glennrp | c5c31be | 2012-11-07 18:42:18 +0000 | [diff] [blame] | 3188 | |
| 3189 | if (pass < num_passes-1) |
| 3190 | continue; |
| 3191 | |
glennrp | 4f99639 | 2013-01-26 22:56:50 +0000 | [diff] [blame] | 3192 | q=QueueAuthenticPixels(image,0,y,image->columns,1,exception); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3193 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3194 | if (q == (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3195 | break; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3196 | |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 3197 | p=ping_pixels+row_offset; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3198 | r=quantum_scanline; |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 3199 | |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 3200 | switch (ping_bit_depth) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3201 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3202 | case 8: |
| 3203 | { |
glennrp | 4f99639 | 2013-01-26 22:56:50 +0000 | [diff] [blame] | 3204 | |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 3205 | if (ping_color_type == 4) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3206 | for (x=(ssize_t) image->columns-1; x >= 0; x--) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3207 | { |
glennrp | a18d5bc | 2011-04-23 14:51:34 +0000 | [diff] [blame] | 3208 | *r++=*p++; |
glennrp | 4f99639 | 2013-01-26 22:56:50 +0000 | [diff] [blame] | 3209 | |
| 3210 | alpha=ScaleCharToQuantum((unsigned char)*p++); |
| 3211 | |
| 3212 | SetPixelAlpha(image,alpha,q); |
| 3213 | |
| 3214 | if (alpha != OpaqueAlpha) |
glennrp | 0b206f5 | 2011-01-07 04:55:32 +0000 | [diff] [blame] | 3215 | found_transparent_pixel = MagickTrue; |
glennrp | 4f99639 | 2013-01-26 22:56:50 +0000 | [diff] [blame] | 3216 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3217 | q+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3218 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3219 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3220 | else |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3221 | for (x=(ssize_t) image->columns-1; x >= 0; x--) |
glennrp | a18d5bc | 2011-04-23 14:51:34 +0000 | [diff] [blame] | 3222 | *r++=*p++; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3223 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3224 | break; |
| 3225 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3226 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3227 | case 16: |
| 3228 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3229 | for (x=(ssize_t) image->columns-1; x >= 0; x--) |
glennrp | a18d5bc | 2011-04-23 14:51:34 +0000 | [diff] [blame] | 3230 | { |
glennrp | c17d96f | 2011-06-27 01:20:11 +0000 | [diff] [blame] | 3231 | #if (MAGICKCORE_QUANTUM_DEPTH == 16) || (MAGICKCORE_QUANTUM_DEPTH == 32) |
glennrp | 58f77c7 | 2011-04-23 14:09:09 +0000 | [diff] [blame] | 3232 | size_t |
| 3233 | quantum; |
| 3234 | |
| 3235 | if (image->colors > 256) |
glennrp | c17d96f | 2011-06-27 01:20:11 +0000 | [diff] [blame] | 3236 | quantum=((*p++) << 8); |
glennrp | 58f77c7 | 2011-04-23 14:09:09 +0000 | [diff] [blame] | 3237 | |
| 3238 | else |
glennrp | c17d96f | 2011-06-27 01:20:11 +0000 | [diff] [blame] | 3239 | quantum=0; |
glennrp | 58f77c7 | 2011-04-23 14:09:09 +0000 | [diff] [blame] | 3240 | |
glennrp | 58f77c7 | 2011-04-23 14:09:09 +0000 | [diff] [blame] | 3241 | quantum|=(*p++); |
glennrp | c17d96f | 2011-06-27 01:20:11 +0000 | [diff] [blame] | 3242 | *r=ScaleShortToQuantum(quantum); |
glennrp | 58f77c7 | 2011-04-23 14:09:09 +0000 | [diff] [blame] | 3243 | r++; |
glennrp | 9d0ea4d | 2011-04-22 18:35:57 +0000 | [diff] [blame] | 3244 | |
| 3245 | if (ping_color_type == 4) |
| 3246 | { |
glennrp | c17d96f | 2011-06-27 01:20:11 +0000 | [diff] [blame] | 3247 | if (image->colors > 256) |
| 3248 | quantum=((*p++) << 8); |
| 3249 | else |
| 3250 | quantum=0; |
| 3251 | |
glennrp | 58f77c7 | 2011-04-23 14:09:09 +0000 | [diff] [blame] | 3252 | quantum|=(*p++); |
glennrp | 4f99639 | 2013-01-26 22:56:50 +0000 | [diff] [blame] | 3253 | |
| 3254 | alpha=ScaleShortToQuantum(quantum); |
| 3255 | SetPixelAlpha(image,alpha,q); |
| 3256 | |
| 3257 | if (alpha != OpaqueAlpha) |
glennrp | 58f77c7 | 2011-04-23 14:09:09 +0000 | [diff] [blame] | 3258 | found_transparent_pixel = MagickTrue; |
glennrp | 4f99639 | 2013-01-26 22:56:50 +0000 | [diff] [blame] | 3259 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3260 | q+=GetPixelChannels(image); |
glennrp | 58f77c7 | 2011-04-23 14:09:09 +0000 | [diff] [blame] | 3261 | } |
glennrp | 58f77c7 | 2011-04-23 14:09:09 +0000 | [diff] [blame] | 3262 | |
| 3263 | #else /* MAGICKCORE_QUANTUM_DEPTH == 8 */ |
| 3264 | *r++=(*p++); |
| 3265 | p++; /* strip low byte */ |
| 3266 | |
| 3267 | if (ping_color_type == 4) |
| 3268 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3269 | SetPixelAlpha(image,*p++,q); |
glennrp | 4f99639 | 2013-01-26 22:56:50 +0000 | [diff] [blame] | 3270 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3271 | if (GetPixelAlpha(image,q) != OpaqueAlpha) |
glennrp | 9d0ea4d | 2011-04-22 18:35:57 +0000 | [diff] [blame] | 3272 | found_transparent_pixel = MagickTrue; |
glennrp | 4f99639 | 2013-01-26 22:56:50 +0000 | [diff] [blame] | 3273 | |
glennrp | 9d0ea4d | 2011-04-22 18:35:57 +0000 | [diff] [blame] | 3274 | p++; |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3275 | q+=GetPixelChannels(image); |
glennrp | 9d0ea4d | 2011-04-22 18:35:57 +0000 | [diff] [blame] | 3276 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3277 | #endif |
glennrp | a18d5bc | 2011-04-23 14:51:34 +0000 | [diff] [blame] | 3278 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3279 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3280 | break; |
| 3281 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3282 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3283 | default: |
| 3284 | break; |
| 3285 | } |
glennrp | 3faa9a3 | 2011-04-23 14:00:25 +0000 | [diff] [blame] | 3286 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3287 | /* |
| 3288 | Transfer image scanline. |
| 3289 | */ |
| 3290 | r=quantum_scanline; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3291 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3292 | q=GetAuthenticPixels(image,0,y,image->columns,1,exception); |
| 3293 | |
| 3294 | if (q == (Quantum *) NULL) |
| 3295 | break; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3296 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3297 | { |
| 3298 | SetPixelIndex(image,*r++,q); |
| 3299 | q+=GetPixelChannels(image); |
| 3300 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3301 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3302 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 3303 | break; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3304 | |
cristy | 7a287bf | 2010-02-14 02:18:09 +0000 | [diff] [blame] | 3305 | if ((image->previous == (Image *) NULL) && (num_passes == 1)) |
| 3306 | { |
cristy | cee9711 | 2010-05-28 00:44:52 +0000 | [diff] [blame] | 3307 | status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y, |
cristy | 9fff7b4 | 2011-04-29 01:09:31 +0000 | [diff] [blame] | 3308 | image->rows); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3309 | |
cristy | 7a287bf | 2010-02-14 02:18:09 +0000 | [diff] [blame] | 3310 | if (status == MagickFalse) |
| 3311 | break; |
| 3312 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3313 | } |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 3314 | |
cristy | 7a287bf | 2010-02-14 02:18:09 +0000 | [diff] [blame] | 3315 | if ((image->previous == (Image *) NULL) && (num_passes != 1)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3316 | { |
| 3317 | status=SetImageProgress(image,LoadImageTag,pass,num_passes); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3318 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3319 | if (status == MagickFalse) |
| 3320 | break; |
| 3321 | } |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 3322 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3323 | quantum_scanline=(Quantum *) RelinquishMagickMemory(quantum_scanline); |
| 3324 | } |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 3325 | |
cristy | b0a657e | 2012-08-29 00:45:37 +0000 | [diff] [blame] | 3326 | image->alpha_trait=found_transparent_pixel ? BlendPixelTrait : |
| 3327 | UndefinedPixelTrait; |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 3328 | |
| 3329 | if (logging != MagickFalse) |
| 3330 | { |
| 3331 | if (found_transparent_pixel != MagickFalse) |
| 3332 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 3333 | " Found transparent pixel"); |
| 3334 | else |
glennrp | 5aa37f6 | 2011-01-02 03:07:57 +0000 | [diff] [blame] | 3335 | { |
| 3336 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 3337 | " No transparent pixel was found"); |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 3338 | |
glennrp | 5aa37f6 | 2011-01-02 03:07:57 +0000 | [diff] [blame] | 3339 | ping_color_type&=0x03; |
| 3340 | } |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 3341 | } |
| 3342 | } |
| 3343 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3344 | if (quantum_info != (QuantumInfo *) NULL) |
| 3345 | quantum_info=DestroyQuantumInfo(quantum_info); |
| 3346 | |
cristy | 5c6f789 | 2010-05-05 22:53:29 +0000 | [diff] [blame] | 3347 | if (image->storage_class == PseudoClass) |
| 3348 | { |
cristy | b0a657e | 2012-08-29 00:45:37 +0000 | [diff] [blame] | 3349 | PixelTrait |
| 3350 | alpha_trait; |
cristy | 5c6f789 | 2010-05-05 22:53:29 +0000 | [diff] [blame] | 3351 | |
cristy | b0a657e | 2012-08-29 00:45:37 +0000 | [diff] [blame] | 3352 | alpha_trait=image->alpha_trait; |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 3353 | image->alpha_trait=UndefinedPixelTrait; |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3354 | (void) SyncImage(image,exception); |
cristy | b0a657e | 2012-08-29 00:45:37 +0000 | [diff] [blame] | 3355 | image->alpha_trait=alpha_trait; |
cristy | 5c6f789 | 2010-05-05 22:53:29 +0000 | [diff] [blame] | 3356 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3357 | |
glennrp | 4eb3931 | 2011-03-30 21:34:55 +0000 | [diff] [blame] | 3358 | png_read_end(ping,end_info); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3359 | |
| 3360 | if (image_info->number_scenes != 0 && mng_info->scenes_found-1 < |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3361 | (ssize_t) image_info->first_scene && image->delay != 0) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3362 | { |
| 3363 | png_destroy_read_struct(&ping,&ping_info,&end_info); |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 3364 | ping_pixels=(unsigned char *) RelinquishMagickMemory(ping_pixels); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3365 | image->colors=2; |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3366 | (void) SetImageBackgroundColor(image,exception); |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 3367 | #ifdef PNG_SETJMP_NOT_THREAD_SAFE |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 3368 | UnlockSemaphoreInfo(ping_semaphore); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3369 | #endif |
| 3370 | if (logging != MagickFalse) |
| 3371 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 3372 | " exit ReadOnePNGImage() early."); |
| 3373 | return(image); |
| 3374 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3375 | |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 3376 | if (png_get_valid(ping,ping_info,PNG_INFO_tRNS)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3377 | { |
| 3378 | ClassType |
| 3379 | storage_class; |
| 3380 | |
| 3381 | /* |
| 3382 | Image has a transparent background. |
| 3383 | */ |
| 3384 | storage_class=image->storage_class; |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 3385 | image->alpha_trait=BlendPixelTrait; |
glennrp | c11cf6a | 2010-03-20 16:46:19 +0000 | [diff] [blame] | 3386 | |
glennrp | 3c21811 | 2010-11-27 15:31:26 +0000 | [diff] [blame] | 3387 | /* Balfour fix from imagemagick discourse server, 5 Feb 2010 */ |
glennrp | c11cf6a | 2010-03-20 16:46:19 +0000 | [diff] [blame] | 3388 | |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3389 | if (storage_class == PseudoClass) |
| 3390 | { |
| 3391 | if ((int) ping_color_type == PNG_COLOR_TYPE_PALETTE) |
glennrp | c11cf6a | 2010-03-20 16:46:19 +0000 | [diff] [blame] | 3392 | { |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3393 | for (x=0; x < ping_num_trans; x++) |
| 3394 | { |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 3395 | image->colormap[x].alpha_trait=BlendPixelTrait; |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3396 | image->colormap[x].alpha = |
| 3397 | ScaleCharToQuantum((unsigned char)ping_trans_alpha[x]); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3398 | } |
glennrp | c11cf6a | 2010-03-20 16:46:19 +0000 | [diff] [blame] | 3399 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3400 | |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3401 | else if (ping_color_type == PNG_COLOR_TYPE_GRAY) |
| 3402 | { |
| 3403 | for (x=0; x < (int) image->colors; x++) |
| 3404 | { |
| 3405 | if (ScaleQuantumToShort(image->colormap[x].red) == |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3406 | transparent_color.alpha) |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3407 | { |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 3408 | image->colormap[x].alpha_trait=BlendPixelTrait; |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3409 | image->colormap[x].alpha = (Quantum) TransparentAlpha; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3410 | } |
| 3411 | } |
| 3412 | } |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3413 | (void) SyncImage(image,exception); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3414 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3415 | |
glennrp | a6a0663 | 2011-01-19 15:15:34 +0000 | [diff] [blame] | 3416 | #if 1 /* Should have already been done above, but glennrp problem P10 |
| 3417 | * needs this. |
| 3418 | */ |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3419 | else |
| 3420 | { |
| 3421 | for (y=0; y < (ssize_t) image->rows; y++) |
glennrp | c11cf6a | 2010-03-20 16:46:19 +0000 | [diff] [blame] | 3422 | { |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3423 | image->storage_class=storage_class; |
| 3424 | q=GetAuthenticPixels(image,0,y,image->columns,1,exception); |
| 3425 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3426 | if (q == (Quantum *) NULL) |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3427 | break; |
| 3428 | |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3429 | |
glennrp | a6a0663 | 2011-01-19 15:15:34 +0000 | [diff] [blame] | 3430 | /* Caution: on a Q8 build, this does not distinguish between |
| 3431 | * 16-bit colors that differ only in the low byte |
| 3432 | */ |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3433 | for (x=(ssize_t) image->columns-1; x >= 0; x--) |
| 3434 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3435 | if (ScaleQuantumToShort(GetPixelRed(image,q)) == |
| 3436 | transparent_color.red && |
| 3437 | ScaleQuantumToShort(GetPixelGreen(image,q)) == |
| 3438 | transparent_color.green && |
| 3439 | ScaleQuantumToShort(GetPixelBlue(image,q)) == |
| 3440 | transparent_color.blue) |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 3441 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3442 | SetPixelAlpha(image,TransparentAlpha,q); |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 3443 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3444 | |
glennrp | 67b9c1a | 2011-04-22 18:47:36 +0000 | [diff] [blame] | 3445 | #if 0 /* I have not found a case where this is needed. */ |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3446 | else |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 3447 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3448 | SetPixelAlpha(image,q)=(Quantum) OpaqueAlpha; |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 3449 | } |
glennrp | a6a0663 | 2011-01-19 15:15:34 +0000 | [diff] [blame] | 3450 | #endif |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3451 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3452 | q+=GetPixelChannels(image); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3453 | } |
| 3454 | |
| 3455 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 3456 | break; |
glennrp | c11cf6a | 2010-03-20 16:46:19 +0000 | [diff] [blame] | 3457 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3458 | } |
glennrp | a6a0663 | 2011-01-19 15:15:34 +0000 | [diff] [blame] | 3459 | #endif |
glennrp | c11cf6a | 2010-03-20 16:46:19 +0000 | [diff] [blame] | 3460 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3461 | image->storage_class=DirectClass; |
| 3462 | } |
glennrp | 3c21811 | 2010-11-27 15:31:26 +0000 | [diff] [blame] | 3463 | |
cristy | eb3b22a | 2011-03-31 20:16:11 +0000 | [diff] [blame] | 3464 | for (j = 0; j < 2; j++) |
glennrp | 4eb3931 | 2011-03-30 21:34:55 +0000 | [diff] [blame] | 3465 | { |
| 3466 | if (j == 0) |
glennrp | a0ed009 | 2011-04-18 16:36:29 +0000 | [diff] [blame] | 3467 | status = png_get_text(ping,ping_info,&text,&num_text) != 0 ? |
| 3468 | MagickTrue : MagickFalse; |
glennrp | 4eb3931 | 2011-03-30 21:34:55 +0000 | [diff] [blame] | 3469 | else |
glennrp | a0ed009 | 2011-04-18 16:36:29 +0000 | [diff] [blame] | 3470 | status = png_get_text(ping,end_info,&text,&num_text) != 0 ? |
| 3471 | MagickTrue : MagickFalse; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3472 | |
glennrp | 4eb3931 | 2011-03-30 21:34:55 +0000 | [diff] [blame] | 3473 | if (status != MagickFalse) |
| 3474 | for (i=0; i < (ssize_t) num_text; i++) |
| 3475 | { |
| 3476 | /* Check for a profile */ |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3477 | |
glennrp | 4eb3931 | 2011-03-30 21:34:55 +0000 | [diff] [blame] | 3478 | if (logging != MagickFalse) |
| 3479 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 3480 | " Reading PNG text chunk"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3481 | |
glennrp | 4eb3931 | 2011-03-30 21:34:55 +0000 | [diff] [blame] | 3482 | if (memcmp(text[i].key, "Raw profile type ",17) == 0) |
glennrp | 97f90e2 | 2011-02-22 05:47:58 +0000 | [diff] [blame] | 3483 | { |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 3484 | (void) Magick_png_read_raw_profile(ping,image,image_info,text, |
| 3485 | (int) i,exception); |
glennrp | 4eb3931 | 2011-03-30 21:34:55 +0000 | [diff] [blame] | 3486 | num_raw_profiles++; |
glennrp | 97f90e2 | 2011-02-22 05:47:58 +0000 | [diff] [blame] | 3487 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3488 | |
glennrp | 4eb3931 | 2011-03-30 21:34:55 +0000 | [diff] [blame] | 3489 | else |
| 3490 | { |
| 3491 | char |
| 3492 | *value; |
| 3493 | |
| 3494 | length=text[i].text_length; |
| 3495 | value=(char *) AcquireQuantumMemory(length+MaxTextExtent, |
| 3496 | sizeof(*value)); |
| 3497 | if (value == (char *) NULL) |
| 3498 | { |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 3499 | png_error(ping,"Memory allocation failed"); |
glennrp | 4eb3931 | 2011-03-30 21:34:55 +0000 | [diff] [blame] | 3500 | break; |
| 3501 | } |
| 3502 | *value='\0'; |
| 3503 | (void) ConcatenateMagickString(value,text[i].text,length+2); |
| 3504 | |
| 3505 | /* Don't save "density" or "units" property if we have a pHYs |
| 3506 | * chunk |
| 3507 | */ |
| 3508 | if (!png_get_valid(ping,ping_info,PNG_INFO_pHYs) || |
| 3509 | (LocaleCompare(text[i].key,"density") != 0 && |
| 3510 | LocaleCompare(text[i].key,"units") != 0)) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3511 | (void) SetImageProperty(image,text[i].key,value,exception); |
glennrp | 4eb3931 | 2011-03-30 21:34:55 +0000 | [diff] [blame] | 3512 | |
| 3513 | if (logging != MagickFalse) |
| 3514 | { |
| 3515 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 3516 | " length: %lu",(unsigned long) length); |
| 3517 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 3518 | " Keyword: %s",text[i].key); |
| 3519 | } |
| 3520 | |
| 3521 | value=DestroyString(value); |
| 3522 | } |
| 3523 | } |
| 3524 | num_text_total += num_text; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3525 | } |
glennrp | 3c21811 | 2010-11-27 15:31:26 +0000 | [diff] [blame] | 3526 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3527 | #ifdef MNG_OBJECT_BUFFERS |
| 3528 | /* |
| 3529 | Store the object if necessary. |
| 3530 | */ |
| 3531 | if (object_id && !mng_info->frozen[object_id]) |
| 3532 | { |
| 3533 | if (mng_info->ob[object_id] == (MngBuffer *) NULL) |
| 3534 | { |
| 3535 | /* |
| 3536 | create a new object buffer. |
| 3537 | */ |
| 3538 | mng_info->ob[object_id]=(MngBuffer *) |
cristy | 73bd4a5 | 2010-10-05 11:24:23 +0000 | [diff] [blame] | 3539 | AcquireMagickMemory(sizeof(MngBuffer)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3540 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3541 | if (mng_info->ob[object_id] != (MngBuffer *) NULL) |
| 3542 | { |
| 3543 | mng_info->ob[object_id]->image=(Image *) NULL; |
| 3544 | mng_info->ob[object_id]->reference_count=1; |
| 3545 | } |
| 3546 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3547 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3548 | if ((mng_info->ob[object_id] == (MngBuffer *) NULL) || |
| 3549 | mng_info->ob[object_id]->frozen) |
| 3550 | { |
| 3551 | if (mng_info->ob[object_id] == (MngBuffer *) NULL) |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 3552 | png_error(ping,"Memory allocation failed"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3553 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3554 | if (mng_info->ob[object_id]->frozen) |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 3555 | png_error(ping,"Cannot overwrite frozen MNG object buffer"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3556 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3557 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3558 | else |
| 3559 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3560 | |
| 3561 | if (mng_info->ob[object_id]->image != (Image *) NULL) |
| 3562 | mng_info->ob[object_id]->image=DestroyImage |
| 3563 | (mng_info->ob[object_id]->image); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3564 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3565 | mng_info->ob[object_id]->image=CloneImage(image,0,0,MagickTrue, |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3566 | exception); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3567 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3568 | if (mng_info->ob[object_id]->image != (Image *) NULL) |
| 3569 | mng_info->ob[object_id]->image->file=(FILE *) NULL; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3570 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3571 | else |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 3572 | png_error(ping, "Cloning image for object buffer failed"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3573 | |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 3574 | if (ping_width > 250000L || ping_height > 250000L) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3575 | png_error(ping,"PNG Image dimensions are too large."); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3576 | |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 3577 | mng_info->ob[object_id]->width=ping_width; |
| 3578 | mng_info->ob[object_id]->height=ping_height; |
| 3579 | mng_info->ob[object_id]->color_type=ping_color_type; |
| 3580 | mng_info->ob[object_id]->sample_depth=ping_bit_depth; |
| 3581 | mng_info->ob[object_id]->interlace_method=ping_interlace_method; |
| 3582 | mng_info->ob[object_id]->compression_method= |
| 3583 | ping_compression_method; |
| 3584 | mng_info->ob[object_id]->filter_method=ping_filter_method; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3585 | |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 3586 | if (png_get_valid(ping,ping_info,PNG_INFO_PLTE)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3587 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3588 | png_colorp |
| 3589 | plte; |
| 3590 | |
| 3591 | /* |
| 3592 | Copy the PLTE to the object buffer. |
| 3593 | */ |
| 3594 | png_get_PLTE(ping,ping_info,&plte,&number_colors); |
| 3595 | mng_info->ob[object_id]->plte_length=number_colors; |
glennrp | 3c21811 | 2010-11-27 15:31:26 +0000 | [diff] [blame] | 3596 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3597 | for (i=0; i < number_colors; i++) |
| 3598 | { |
| 3599 | mng_info->ob[object_id]->plte[i]=plte[i]; |
| 3600 | } |
| 3601 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3602 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3603 | else |
| 3604 | mng_info->ob[object_id]->plte_length=0; |
| 3605 | } |
| 3606 | } |
| 3607 | #endif |
glennrp | 0a55b4c | 2011-03-23 12:38:38 +0000 | [diff] [blame] | 3608 | |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 3609 | /* Set image->alpha_trait to MagickTrue if the input colortype supports |
glennrp | 0a55b4c | 2011-03-23 12:38:38 +0000 | [diff] [blame] | 3610 | * alpha or if a valid tRNS chunk is present, no matter whether there |
| 3611 | * is actual transparency present. |
| 3612 | */ |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 3613 | image->alpha_trait=(((int) ping_color_type == PNG_COLOR_TYPE_RGB_ALPHA) || |
glennrp | 0a55b4c | 2011-03-23 12:38:38 +0000 | [diff] [blame] | 3614 | ((int) ping_color_type == PNG_COLOR_TYPE_GRAY_ALPHA) || |
| 3615 | (png_get_valid(ping,ping_info,PNG_INFO_tRNS))) ? |
cristy | b0a657e | 2012-08-29 00:45:37 +0000 | [diff] [blame] | 3616 | BlendPixelTrait : UndefinedPixelTrait; |
glennrp | 0a55b4c | 2011-03-23 12:38:38 +0000 | [diff] [blame] | 3617 | |
glennrp | 224ad8d | 2013-02-01 17:04:04 +0000 | [diff] [blame] | 3618 | #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] | 3619 | if (image->alpha_trait == BlendPixelTrait) |
| 3620 | { |
| 3621 | if (ping_color_type == PNG_COLOR_TYPE_GRAY_ALPHA) |
| 3622 | (void) SetImageType(image,GrayscaleMatteType,exception); |
| 3623 | |
| 3624 | else if (ping_color_type == PNG_COLOR_TYPE_PALETTE) |
| 3625 | (void) SetImageType(image,PaletteMatteType,exception); |
| 3626 | |
| 3627 | else |
| 3628 | (void) SetImageType(image,TrueColorMatteType,exception); |
| 3629 | } |
| 3630 | |
| 3631 | else |
| 3632 | { |
| 3633 | if (ping_color_type == PNG_COLOR_TYPE_GRAY) |
| 3634 | (void) SetImageType(image,GrayscaleType,exception); |
| 3635 | |
| 3636 | else if (ping_color_type == PNG_COLOR_TYPE_PALETTE) |
| 3637 | (void) SetImageType(image,PaletteType,exception); |
| 3638 | |
| 3639 | else |
| 3640 | (void) SetImageType(image,TrueColorType,exception); |
| 3641 | } |
glennrp | 224ad8d | 2013-02-01 17:04:04 +0000 | [diff] [blame] | 3642 | #endif |
glennrp | 5830fbc | 2013-01-27 06:11:45 +0000 | [diff] [blame] | 3643 | |
glennrp | cb395ac | 2011-03-30 19:50:23 +0000 | [diff] [blame] | 3644 | /* Set more properties for identify to retrieve */ |
| 3645 | { |
| 3646 | char |
| 3647 | msg[MaxTextExtent]; |
| 3648 | |
glennrp | 4eb3931 | 2011-03-30 21:34:55 +0000 | [diff] [blame] | 3649 | if (num_text_total != 0) |
glennrp | cb395ac | 2011-03-30 19:50:23 +0000 | [diff] [blame] | 3650 | { |
| 3651 | /* libpng doesn't tell us whether they were tEXt, zTXt, or iTXt */ |
cristy | 3b6fd2e | 2011-05-20 12:53:50 +0000 | [diff] [blame] | 3652 | (void) FormatLocaleString(msg,MaxTextExtent, |
glennrp | 613276d | 2011-03-30 21:46:49 +0000 | [diff] [blame] | 3653 | "%d tEXt/zTXt/iTXt chunks were found", num_text_total); |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3654 | (void) SetImageProperty(image,"png:text ",msg, |
| 3655 | exception); |
glennrp | cb395ac | 2011-03-30 19:50:23 +0000 | [diff] [blame] | 3656 | } |
| 3657 | |
| 3658 | if (num_raw_profiles != 0) |
| 3659 | { |
cristy | 3b6fd2e | 2011-05-20 12:53:50 +0000 | [diff] [blame] | 3660 | (void) FormatLocaleString(msg,MaxTextExtent, |
glennrp | cb395ac | 2011-03-30 19:50:23 +0000 | [diff] [blame] | 3661 | "%d were found", num_raw_profiles); |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3662 | (void) SetImageProperty(image,"png:text-encoded profiles",msg, |
| 3663 | exception); |
glennrp | cb395ac | 2011-03-30 19:50:23 +0000 | [diff] [blame] | 3664 | } |
| 3665 | |
glennrp | 98b83d4 | 2012-07-23 02:50:31 +0000 | [diff] [blame] | 3666 | if (ping_found_cHRM != MagickFalse) |
glennrp | 5961225 | 2011-03-30 21:45:21 +0000 | [diff] [blame] | 3667 | { |
cristy | 3b6fd2e | 2011-05-20 12:53:50 +0000 | [diff] [blame] | 3668 | (void) FormatLocaleString(msg,MaxTextExtent,"%s", |
glennrp | 5961225 | 2011-03-30 21:45:21 +0000 | [diff] [blame] | 3669 | "chunk was found (see Chromaticity, above)"); |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3670 | (void) SetImageProperty(image,"png:cHRM ",msg, |
| 3671 | exception); |
glennrp | 5961225 | 2011-03-30 21:45:21 +0000 | [diff] [blame] | 3672 | } |
glennrp | cb395ac | 2011-03-30 19:50:23 +0000 | [diff] [blame] | 3673 | |
| 3674 | if (png_get_valid(ping,ping_info,PNG_INFO_bKGD)) |
glennrp | 5961225 | 2011-03-30 21:45:21 +0000 | [diff] [blame] | 3675 | { |
cristy | 3b6fd2e | 2011-05-20 12:53:50 +0000 | [diff] [blame] | 3676 | (void) FormatLocaleString(msg,MaxTextExtent,"%s", |
glennrp | 5961225 | 2011-03-30 21:45:21 +0000 | [diff] [blame] | 3677 | "chunk was found (see Background color, above)"); |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3678 | (void) SetImageProperty(image,"png:bKGD ",msg, |
| 3679 | exception); |
glennrp | 5961225 | 2011-03-30 21:45:21 +0000 | [diff] [blame] | 3680 | } |
| 3681 | |
cristy | 3b6fd2e | 2011-05-20 12:53:50 +0000 | [diff] [blame] | 3682 | (void) FormatLocaleString(msg,MaxTextExtent,"%s", |
glennrp | 5961225 | 2011-03-30 21:45:21 +0000 | [diff] [blame] | 3683 | "chunk was found"); |
glennrp | cb395ac | 2011-03-30 19:50:23 +0000 | [diff] [blame] | 3684 | |
glennrp | 98b83d4 | 2012-07-23 02:50:31 +0000 | [diff] [blame] | 3685 | #if defined(PNG_iCCP_SUPPORTED) |
| 3686 | if (ping_found_iCCP != MagickFalse) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3687 | (void) SetImageProperty(image,"png:iCCP ",msg, |
| 3688 | exception); |
glennrp | 98b83d4 | 2012-07-23 02:50:31 +0000 | [diff] [blame] | 3689 | #endif |
glennrp | cb395ac | 2011-03-30 19:50:23 +0000 | [diff] [blame] | 3690 | |
glennrp | cb395ac | 2011-03-30 19:50:23 +0000 | [diff] [blame] | 3691 | if (png_get_valid(ping,ping_info,PNG_INFO_tRNS)) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3692 | (void) SetImageProperty(image,"png:tRNS ",msg, |
| 3693 | exception); |
glennrp | 4eb3931 | 2011-03-30 21:34:55 +0000 | [diff] [blame] | 3694 | |
| 3695 | #if defined(PNG_sRGB_SUPPORTED) |
glennrp | 98b83d4 | 2012-07-23 02:50:31 +0000 | [diff] [blame] | 3696 | if (ping_found_sRGB != MagickFalse) |
glennrp | 4eb3931 | 2011-03-30 21:34:55 +0000 | [diff] [blame] | 3697 | { |
cristy | 3b6fd2e | 2011-05-20 12:53:50 +0000 | [diff] [blame] | 3698 | (void) FormatLocaleString(msg,MaxTextExtent, |
glennrp | 98b83d4 | 2012-07-23 02:50:31 +0000 | [diff] [blame] | 3699 | "intent=%d (%s)", |
| 3700 | (int) intent, |
| 3701 | Magick_RenderingIntentString_from_PNG_RenderingIntent(intent)); |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3702 | (void) SetImageProperty(image,"png:sRGB ",msg, |
glennrp | 98b83d4 | 2012-07-23 02:50:31 +0000 | [diff] [blame] | 3703 | exception); |
glennrp | 4eb3931 | 2011-03-30 21:34:55 +0000 | [diff] [blame] | 3704 | } |
| 3705 | #endif |
| 3706 | |
glennrp | 98b83d4 | 2012-07-23 02:50:31 +0000 | [diff] [blame] | 3707 | if (ping_found_gAMA != MagickFalse) |
glennrp | 4eb3931 | 2011-03-30 21:34:55 +0000 | [diff] [blame] | 3708 | { |
cristy | 3b6fd2e | 2011-05-20 12:53:50 +0000 | [diff] [blame] | 3709 | (void) FormatLocaleString(msg,MaxTextExtent, |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3710 | "gamma=%.8g (See Gamma, above)", |
| 3711 | file_gamma); |
| 3712 | (void) SetImageProperty(image,"png:gAMA ",msg, |
| 3713 | exception); |
glennrp | 4eb3931 | 2011-03-30 21:34:55 +0000 | [diff] [blame] | 3714 | } |
| 3715 | |
| 3716 | #if defined(PNG_pHYs_SUPPORTED) |
| 3717 | if (png_get_valid(ping,ping_info,PNG_INFO_pHYs)) |
| 3718 | { |
cristy | 3b6fd2e | 2011-05-20 12:53:50 +0000 | [diff] [blame] | 3719 | (void) FormatLocaleString(msg,MaxTextExtent, |
glennrp | 07523c7 | 2011-03-31 18:12:10 +0000 | [diff] [blame] | 3720 | "x_res=%.10g, y_res=%.10g, units=%d", |
glennrp | 4eb3931 | 2011-03-30 21:34:55 +0000 | [diff] [blame] | 3721 | (double) x_resolution,(double) y_resolution, unit_type); |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3722 | (void) SetImageProperty(image,"png:pHYs ",msg, |
| 3723 | exception); |
glennrp | 4eb3931 | 2011-03-30 21:34:55 +0000 | [diff] [blame] | 3724 | } |
| 3725 | #endif |
| 3726 | |
| 3727 | #if defined(PNG_oFFs_SUPPORTED) |
| 3728 | if (png_get_valid(ping,ping_info,PNG_INFO_oFFs)) |
| 3729 | { |
cristy | 3b6fd2e | 2011-05-20 12:53:50 +0000 | [diff] [blame] | 3730 | (void) FormatLocaleString(msg,MaxTextExtent,"x_off=%.20g, y_off=%.20g", |
glennrp | 4eb3931 | 2011-03-30 21:34:55 +0000 | [diff] [blame] | 3731 | (double) image->page.x,(double) image->page.y); |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3732 | (void) SetImageProperty(image,"png:oFFs ",msg, |
| 3733 | exception); |
glennrp | 4eb3931 | 2011-03-30 21:34:55 +0000 | [diff] [blame] | 3734 | } |
| 3735 | #endif |
| 3736 | |
glennrp | 07523c7 | 2011-03-31 18:12:10 +0000 | [diff] [blame] | 3737 | if ((image->page.width != 0 && image->page.width != image->columns) || |
| 3738 | (image->page.height != 0 && image->page.height != image->rows)) |
| 3739 | { |
cristy | 3b6fd2e | 2011-05-20 12:53:50 +0000 | [diff] [blame] | 3740 | (void) FormatLocaleString(msg,MaxTextExtent, |
glennrp | 07523c7 | 2011-03-31 18:12:10 +0000 | [diff] [blame] | 3741 | "width=%.20g, height=%.20g", |
| 3742 | (double) image->page.width,(double) image->page.height); |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3743 | (void) SetImageProperty(image,"png:vpAg ",msg, |
| 3744 | exception); |
glennrp | 07523c7 | 2011-03-31 18:12:10 +0000 | [diff] [blame] | 3745 | } |
glennrp | cb395ac | 2011-03-30 19:50:23 +0000 | [diff] [blame] | 3746 | } |
| 3747 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3748 | /* |
| 3749 | Relinquish resources. |
| 3750 | */ |
| 3751 | png_destroy_read_struct(&ping,&ping_info,&end_info); |
| 3752 | |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 3753 | ping_pixels=(unsigned char *) RelinquishMagickMemory(ping_pixels); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3754 | |
| 3755 | if (logging != MagickFalse) |
| 3756 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 3757 | " exit ReadOnePNGImage()"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3758 | |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 3759 | #ifdef PNG_SETJMP_NOT_THREAD_SAFE |
| 3760 | UnlockSemaphoreInfo(ping_semaphore); |
| 3761 | #endif |
| 3762 | |
| 3763 | /* } for navigation to beginning of SETJMP-protected block, revert to |
| 3764 | * Throwing an Exception when an error occurs. |
| 3765 | */ |
| 3766 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3767 | return(image); |
| 3768 | |
| 3769 | /* end of reading one PNG image */ |
| 3770 | } |
| 3771 | |
| 3772 | static Image *ReadPNGImage(const ImageInfo *image_info,ExceptionInfo *exception) |
| 3773 | { |
| 3774 | Image |
| 3775 | *image, |
| 3776 | *previous; |
| 3777 | |
| 3778 | MagickBooleanType |
glennrp | 21f0e62 | 2011-01-07 16:20:57 +0000 | [diff] [blame] | 3779 | have_mng_structure, |
| 3780 | logging, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3781 | status; |
| 3782 | |
| 3783 | MngInfo |
| 3784 | *mng_info; |
| 3785 | |
| 3786 | char |
| 3787 | magic_number[MaxTextExtent]; |
| 3788 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3789 | ssize_t |
| 3790 | count; |
| 3791 | |
| 3792 | /* |
| 3793 | Open image file. |
| 3794 | */ |
| 3795 | assert(image_info != (const ImageInfo *) NULL); |
| 3796 | assert(image_info->signature == MagickSignature); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3797 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3798 | if (image_info->debug != MagickFalse) |
| 3799 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s", |
| 3800 | image_info->filename); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3801 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3802 | assert(exception != (ExceptionInfo *) NULL); |
| 3803 | assert(exception->signature == MagickSignature); |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 3804 | logging=LogMagickEvent(CoderEvent,GetMagickModule(),"Enter ReadPNGImage()"); |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3805 | image=AcquireImage(image_info,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3806 | mng_info=(MngInfo *) NULL; |
| 3807 | status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3808 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3809 | if (status == MagickFalse) |
| 3810 | ThrowReaderException(FileOpenError,"UnableToOpenFile"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3811 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3812 | /* |
| 3813 | Verify PNG signature. |
| 3814 | */ |
| 3815 | count=ReadBlob(image,8,(unsigned char *) magic_number); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3816 | |
glennrp | dde35db | 2011-02-21 12:06:32 +0000 | [diff] [blame] | 3817 | if (count < 8 || memcmp(magic_number,"\211PNG\r\n\032\n",8) != 0) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3818 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3819 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3820 | /* |
| 3821 | Allocate a MngInfo structure. |
| 3822 | */ |
| 3823 | have_mng_structure=MagickFalse; |
cristy | 73bd4a5 | 2010-10-05 11:24:23 +0000 | [diff] [blame] | 3824 | mng_info=(MngInfo *) AcquireMagickMemory(sizeof(MngInfo)); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3825 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3826 | if (mng_info == (MngInfo *) NULL) |
| 3827 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3828 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3829 | /* |
| 3830 | Initialize members of the MngInfo structure. |
| 3831 | */ |
| 3832 | (void) ResetMagickMemory(mng_info,0,sizeof(MngInfo)); |
| 3833 | mng_info->image=image; |
| 3834 | have_mng_structure=MagickTrue; |
| 3835 | |
| 3836 | previous=image; |
| 3837 | image=ReadOnePNGImage(mng_info,image_info,exception); |
| 3838 | MngInfoFreeStruct(mng_info,&have_mng_structure); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3839 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3840 | if (image == (Image *) NULL) |
| 3841 | { |
| 3842 | if (previous != (Image *) NULL) |
| 3843 | { |
| 3844 | if (previous->signature != MagickSignature) |
| 3845 | ThrowReaderException(CorruptImageError,"CorruptImage"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3846 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3847 | (void) CloseBlob(previous); |
| 3848 | (void) DestroyImageList(previous); |
| 3849 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3850 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3851 | if (logging != MagickFalse) |
| 3852 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 3853 | "exit ReadPNGImage() with error"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3854 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3855 | return((Image *) NULL); |
| 3856 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3857 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3858 | (void) CloseBlob(image); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3859 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3860 | if ((image->columns == 0) || (image->rows == 0)) |
| 3861 | { |
| 3862 | if (logging != MagickFalse) |
| 3863 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 3864 | "exit ReadPNGImage() with error."); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3865 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3866 | ThrowReaderException(CorruptImageError,"CorruptImage"); |
| 3867 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3868 | |
cristy | 72715f5 | 2012-06-26 17:55:16 +0000 | [diff] [blame] | 3869 | if ((IssRGBColorspace(image->colorspace) != MagickFalse) && |
glennrp | a8036d6 | 2012-11-04 01:46:06 +0000 | [diff] [blame] | 3870 | ((image->gamma < .45) || (image->gamma > .46))) |
cristy | 72715f5 | 2012-06-26 17:55:16 +0000 | [diff] [blame] | 3871 | SetImageColorspace(image,RGBColorspace,exception); |
| 3872 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3873 | if (logging != MagickFalse) |
glennrp | 97f90e2 | 2011-02-22 05:47:58 +0000 | [diff] [blame] | 3874 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 3875 | " page.w: %.20g, page.h: %.20g,page.x: %.20g, page.y: %.20g.", |
| 3876 | (double) image->page.width,(double) image->page.height, |
| 3877 | (double) image->page.x,(double) image->page.y); |
| 3878 | |
| 3879 | if (logging != MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3880 | (void) LogMagickEvent(CoderEvent,GetMagickModule(),"exit ReadPNGImage()"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3881 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3882 | return(image); |
| 3883 | } |
| 3884 | |
| 3885 | |
| 3886 | |
| 3887 | #if defined(JNG_SUPPORTED) |
| 3888 | /* |
| 3889 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3890 | % % |
| 3891 | % % |
| 3892 | % % |
| 3893 | % R e a d O n e J N G I m a g e % |
| 3894 | % % |
| 3895 | % % |
| 3896 | % % |
| 3897 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3898 | % |
| 3899 | % ReadOneJNGImage() reads a JPEG Network Graphics (JNG) image file |
| 3900 | % (minus the 8-byte signature) and returns it. It allocates the memory |
| 3901 | % necessary for the new Image structure and returns a pointer to the new |
| 3902 | % image. |
| 3903 | % |
| 3904 | % JNG support written by Glenn Randers-Pehrson, glennrp@image... |
| 3905 | % |
| 3906 | % The format of the ReadOneJNGImage method is: |
| 3907 | % |
| 3908 | % Image *ReadOneJNGImage(MngInfo *mng_info, const ImageInfo *image_info, |
| 3909 | % ExceptionInfo *exception) |
| 3910 | % |
| 3911 | % A description of each parameter follows: |
| 3912 | % |
| 3913 | % o mng_info: Specifies a pointer to a MngInfo structure. |
| 3914 | % |
| 3915 | % o image_info: the image info. |
| 3916 | % |
| 3917 | % o exception: return any errors or warnings in this structure. |
| 3918 | % |
| 3919 | */ |
| 3920 | static Image *ReadOneJNGImage(MngInfo *mng_info, |
| 3921 | const ImageInfo *image_info, ExceptionInfo *exception) |
| 3922 | { |
| 3923 | Image |
| 3924 | *alpha_image, |
| 3925 | *color_image, |
| 3926 | *image, |
| 3927 | *jng_image; |
| 3928 | |
| 3929 | ImageInfo |
| 3930 | *alpha_image_info, |
| 3931 | *color_image_info; |
| 3932 | |
cristy | 4383ec8 | 2011-01-05 15:42:32 +0000 | [diff] [blame] | 3933 | MagickBooleanType |
| 3934 | logging; |
| 3935 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3936 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3937 | y; |
| 3938 | |
| 3939 | MagickBooleanType |
| 3940 | status; |
| 3941 | |
| 3942 | png_uint_32 |
| 3943 | jng_height, |
| 3944 | jng_width; |
| 3945 | |
| 3946 | png_byte |
| 3947 | jng_color_type, |
| 3948 | jng_image_sample_depth, |
| 3949 | jng_image_compression_method, |
| 3950 | jng_image_interlace_method, |
| 3951 | jng_alpha_sample_depth, |
| 3952 | jng_alpha_compression_method, |
| 3953 | jng_alpha_filter_method, |
| 3954 | jng_alpha_interlace_method; |
| 3955 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3956 | register const Quantum |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3957 | *s; |
| 3958 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3959 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3960 | i, |
| 3961 | x; |
| 3962 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3963 | register Quantum |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3964 | *q; |
| 3965 | |
| 3966 | register unsigned char |
| 3967 | *p; |
| 3968 | |
| 3969 | unsigned int |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3970 | read_JSEP, |
| 3971 | reading_idat, |
| 3972 | skip_to_iend; |
| 3973 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3974 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3975 | length; |
| 3976 | |
| 3977 | jng_alpha_compression_method=0; |
| 3978 | jng_alpha_sample_depth=8; |
| 3979 | jng_color_type=0; |
| 3980 | jng_height=0; |
| 3981 | jng_width=0; |
| 3982 | alpha_image=(Image *) NULL; |
| 3983 | color_image=(Image *) NULL; |
| 3984 | alpha_image_info=(ImageInfo *) NULL; |
| 3985 | color_image_info=(ImageInfo *) NULL; |
| 3986 | |
| 3987 | logging=LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 3988 | " Enter ReadOneJNGImage()"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3989 | |
| 3990 | image=mng_info->image; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3991 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3992 | if (GetAuthenticPixelQueue(image) != (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3993 | { |
| 3994 | /* |
| 3995 | Allocate next image structure. |
| 3996 | */ |
| 3997 | if (logging != MagickFalse) |
| 3998 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 3999 | " AcquireNextImage()"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4000 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 4001 | AcquireNextImage(image_info,image,exception); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4002 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4003 | if (GetNextImageInList(image) == (Image *) NULL) |
| 4004 | return((Image *) NULL); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4005 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4006 | image=SyncNextImageInList(image); |
| 4007 | } |
| 4008 | mng_info->image=image; |
| 4009 | |
| 4010 | /* |
| 4011 | Signature bytes have already been read. |
| 4012 | */ |
| 4013 | |
| 4014 | read_JSEP=MagickFalse; |
| 4015 | reading_idat=MagickFalse; |
| 4016 | skip_to_iend=MagickFalse; |
| 4017 | for (;;) |
| 4018 | { |
| 4019 | char |
| 4020 | type[MaxTextExtent]; |
| 4021 | |
| 4022 | unsigned char |
| 4023 | *chunk; |
| 4024 | |
| 4025 | unsigned int |
| 4026 | count; |
| 4027 | |
| 4028 | /* |
| 4029 | Read a new JNG chunk. |
| 4030 | */ |
| 4031 | status=SetImageProgress(image,LoadImagesTag,TellBlob(image), |
| 4032 | 2*GetBlobSize(image)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4033 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4034 | if (status == MagickFalse) |
| 4035 | break; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4036 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4037 | type[0]='\0'; |
| 4038 | (void) ConcatenateMagickString(type,"errr",MaxTextExtent); |
| 4039 | length=ReadBlobMSBLong(image); |
| 4040 | count=(unsigned int) ReadBlob(image,4,(unsigned char *) type); |
| 4041 | |
| 4042 | if (logging != MagickFalse) |
| 4043 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 4044 | " Reading JNG chunk type %c%c%c%c, length: %.20g", |
| 4045 | type[0],type[1],type[2],type[3],(double) length); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4046 | |
| 4047 | if (length > PNG_UINT_31_MAX || count == 0) |
| 4048 | ThrowReaderException(CorruptImageError,"CorruptImage"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4049 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4050 | p=NULL; |
| 4051 | chunk=(unsigned char *) NULL; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4052 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4053 | if (length) |
| 4054 | { |
| 4055 | chunk=(unsigned char *) AcquireQuantumMemory(length,sizeof(*chunk)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4056 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4057 | if (chunk == (unsigned char *) NULL) |
| 4058 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4059 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 4060 | for (i=0; i < (ssize_t) length; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4061 | chunk[i]=(unsigned char) ReadBlobByte(image); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4062 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4063 | p=chunk; |
| 4064 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4065 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4066 | (void) ReadBlobMSBLong(image); /* read crc word */ |
| 4067 | |
| 4068 | if (skip_to_iend) |
| 4069 | { |
| 4070 | if (length) |
| 4071 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4072 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4073 | continue; |
| 4074 | } |
| 4075 | |
| 4076 | if (memcmp(type,mng_JHDR,4) == 0) |
| 4077 | { |
| 4078 | if (length == 16) |
| 4079 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 4080 | jng_width=(size_t) ((p[0] << 24) | (p[1] << 16) | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4081 | (p[2] << 8) | p[3]); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 4082 | jng_height=(size_t) ((p[4] << 24) | (p[5] << 16) | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4083 | (p[6] << 8) | p[7]); |
| 4084 | jng_color_type=p[8]; |
| 4085 | jng_image_sample_depth=p[9]; |
| 4086 | jng_image_compression_method=p[10]; |
| 4087 | jng_image_interlace_method=p[11]; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4088 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4089 | image->interlace=jng_image_interlace_method != 0 ? PNGInterlace : |
| 4090 | NoInterlace; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4091 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4092 | jng_alpha_sample_depth=p[12]; |
| 4093 | jng_alpha_compression_method=p[13]; |
| 4094 | jng_alpha_filter_method=p[14]; |
| 4095 | jng_alpha_interlace_method=p[15]; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4096 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4097 | if (logging != MagickFalse) |
| 4098 | { |
| 4099 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | f2faecf | 2010-05-28 19:19:36 +0000 | [diff] [blame] | 4100 | " jng_width: %16lu",(unsigned long) jng_width); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4101 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4102 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | f2faecf | 2010-05-28 19:19:36 +0000 | [diff] [blame] | 4103 | " jng_width: %16lu",(unsigned long) jng_height); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4104 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4105 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 4106 | " jng_color_type: %16d",jng_color_type); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4107 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4108 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 4109 | " jng_image_sample_depth: %3d", |
| 4110 | jng_image_sample_depth); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4111 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4112 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 4113 | " jng_image_compression_method:%3d", |
| 4114 | jng_image_compression_method); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4115 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4116 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 4117 | " jng_image_interlace_method: %3d", |
| 4118 | jng_image_interlace_method); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4119 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4120 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 4121 | " jng_alpha_sample_depth: %3d", |
| 4122 | jng_alpha_sample_depth); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4123 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4124 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 4125 | " jng_alpha_compression_method:%3d", |
| 4126 | jng_alpha_compression_method); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4127 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4128 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 4129 | " jng_alpha_filter_method: %3d", |
| 4130 | jng_alpha_filter_method); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4131 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4132 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 4133 | " jng_alpha_interlace_method: %3d", |
| 4134 | jng_alpha_interlace_method); |
| 4135 | } |
| 4136 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4137 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4138 | if (length) |
| 4139 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4140 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4141 | continue; |
| 4142 | } |
| 4143 | |
| 4144 | |
| 4145 | if ((reading_idat == MagickFalse) && (read_JSEP == MagickFalse) && |
| 4146 | ((memcmp(type,mng_JDAT,4) == 0) || (memcmp(type,mng_JdAA,4) == 0) || |
| 4147 | (memcmp(type,mng_IDAT,4) == 0) || (memcmp(type,mng_JDAA,4) == 0))) |
| 4148 | { |
| 4149 | /* |
| 4150 | o create color_image |
| 4151 | o open color_blob, attached to color_image |
| 4152 | o if (color type has alpha) |
| 4153 | open alpha_blob, attached to alpha_image |
| 4154 | */ |
| 4155 | |
cristy | 73bd4a5 | 2010-10-05 11:24:23 +0000 | [diff] [blame] | 4156 | color_image_info=(ImageInfo *)AcquireMagickMemory(sizeof(ImageInfo)); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4157 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4158 | if (color_image_info == (ImageInfo *) NULL) |
| 4159 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4160 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4161 | GetImageInfo(color_image_info); |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 4162 | color_image=AcquireImage(color_image_info,exception); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4163 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4164 | if (color_image == (Image *) NULL) |
| 4165 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
| 4166 | |
| 4167 | if (logging != MagickFalse) |
| 4168 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 4169 | " Creating color_blob."); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4170 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4171 | (void) AcquireUniqueFilename(color_image->filename); |
| 4172 | status=OpenBlob(color_image_info,color_image,WriteBinaryBlobMode, |
| 4173 | exception); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4174 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4175 | if (status == MagickFalse) |
| 4176 | return((Image *) NULL); |
| 4177 | |
| 4178 | if ((image_info->ping == MagickFalse) && (jng_color_type >= 12)) |
| 4179 | { |
| 4180 | alpha_image_info=(ImageInfo *) |
cristy | 73bd4a5 | 2010-10-05 11:24:23 +0000 | [diff] [blame] | 4181 | AcquireMagickMemory(sizeof(ImageInfo)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4182 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4183 | if (alpha_image_info == (ImageInfo *) NULL) |
| 4184 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4185 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4186 | GetImageInfo(alpha_image_info); |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 4187 | alpha_image=AcquireImage(alpha_image_info,exception); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4188 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4189 | if (alpha_image == (Image *) NULL) |
| 4190 | { |
| 4191 | alpha_image=DestroyImage(alpha_image); |
| 4192 | ThrowReaderException(ResourceLimitError, |
| 4193 | "MemoryAllocationFailed"); |
| 4194 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4195 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4196 | if (logging != MagickFalse) |
| 4197 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 4198 | " Creating alpha_blob."); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4199 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4200 | (void) AcquireUniqueFilename(alpha_image->filename); |
| 4201 | status=OpenBlob(alpha_image_info,alpha_image,WriteBinaryBlobMode, |
| 4202 | exception); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4203 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4204 | if (status == MagickFalse) |
| 4205 | return((Image *) NULL); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4206 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4207 | if (jng_alpha_compression_method == 0) |
| 4208 | { |
| 4209 | unsigned char |
| 4210 | data[18]; |
| 4211 | |
| 4212 | if (logging != MagickFalse) |
| 4213 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 4214 | " Writing IHDR chunk to alpha_blob."); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4215 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4216 | (void) WriteBlob(alpha_image,8,(const unsigned char *) |
| 4217 | "\211PNG\r\n\032\n"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4218 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4219 | (void) WriteBlobMSBULong(alpha_image,13L); |
| 4220 | PNGType(data,mng_IHDR); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 4221 | LogPNGChunk(logging,mng_IHDR,13L); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4222 | PNGLong(data+4,jng_width); |
| 4223 | PNGLong(data+8,jng_height); |
| 4224 | data[12]=jng_alpha_sample_depth; |
| 4225 | data[13]=0; /* color_type gray */ |
| 4226 | data[14]=0; /* compression method 0 */ |
| 4227 | data[15]=0; /* filter_method 0 */ |
| 4228 | data[16]=0; /* interlace_method 0 */ |
| 4229 | (void) WriteBlob(alpha_image,17,data); |
| 4230 | (void) WriteBlobMSBULong(alpha_image,crc32(0,data,17)); |
| 4231 | } |
| 4232 | } |
| 4233 | reading_idat=MagickTrue; |
| 4234 | } |
| 4235 | |
| 4236 | if (memcmp(type,mng_JDAT,4) == 0) |
| 4237 | { |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4238 | /* Copy chunk to color_image->blob */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4239 | |
| 4240 | if (logging != MagickFalse) |
| 4241 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 4242 | " Copying JDAT chunk data to color_blob."); |
| 4243 | |
| 4244 | (void) WriteBlob(color_image,length,chunk); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4245 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4246 | if (length) |
| 4247 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4248 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4249 | continue; |
| 4250 | } |
| 4251 | |
| 4252 | if (memcmp(type,mng_IDAT,4) == 0) |
| 4253 | { |
| 4254 | png_byte |
| 4255 | data[5]; |
| 4256 | |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4257 | /* Copy IDAT header and chunk data to alpha_image->blob */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4258 | |
| 4259 | if (image_info->ping == MagickFalse) |
| 4260 | { |
| 4261 | if (logging != MagickFalse) |
| 4262 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 4263 | " Copying IDAT chunk data to alpha_blob."); |
| 4264 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 4265 | (void) WriteBlobMSBULong(alpha_image,(size_t) length); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4266 | PNGType(data,mng_IDAT); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 4267 | LogPNGChunk(logging,mng_IDAT,length); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4268 | (void) WriteBlob(alpha_image,4,data); |
| 4269 | (void) WriteBlob(alpha_image,length,chunk); |
| 4270 | (void) WriteBlobMSBULong(alpha_image, |
| 4271 | crc32(crc32(0,data,4),chunk,(uInt) length)); |
| 4272 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4273 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4274 | if (length) |
| 4275 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4276 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4277 | continue; |
| 4278 | } |
| 4279 | |
| 4280 | if ((memcmp(type,mng_JDAA,4) == 0) || (memcmp(type,mng_JdAA,4) == 0)) |
| 4281 | { |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4282 | /* Copy chunk data to alpha_image->blob */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4283 | |
| 4284 | if (image_info->ping == MagickFalse) |
| 4285 | { |
| 4286 | if (logging != MagickFalse) |
| 4287 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 4288 | " Copying JDAA chunk data to alpha_blob."); |
| 4289 | |
| 4290 | (void) WriteBlob(alpha_image,length,chunk); |
| 4291 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4292 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4293 | if (length) |
| 4294 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4295 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4296 | continue; |
| 4297 | } |
| 4298 | |
| 4299 | if (memcmp(type,mng_JSEP,4) == 0) |
| 4300 | { |
| 4301 | read_JSEP=MagickTrue; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4302 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4303 | if (length) |
| 4304 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4305 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4306 | continue; |
| 4307 | } |
| 4308 | |
| 4309 | if (memcmp(type,mng_bKGD,4) == 0) |
| 4310 | { |
| 4311 | if (length == 2) |
| 4312 | { |
| 4313 | image->background_color.red=ScaleCharToQuantum(p[1]); |
| 4314 | image->background_color.green=image->background_color.red; |
| 4315 | image->background_color.blue=image->background_color.red; |
| 4316 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4317 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4318 | if (length == 6) |
| 4319 | { |
| 4320 | image->background_color.red=ScaleCharToQuantum(p[1]); |
| 4321 | image->background_color.green=ScaleCharToQuantum(p[3]); |
| 4322 | image->background_color.blue=ScaleCharToQuantum(p[5]); |
| 4323 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4324 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4325 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 4326 | continue; |
| 4327 | } |
| 4328 | |
| 4329 | if (memcmp(type,mng_gAMA,4) == 0) |
| 4330 | { |
| 4331 | if (length == 4) |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 4332 | image->gamma=((float) mng_get_long(p))*0.00001; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4333 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4334 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 4335 | continue; |
| 4336 | } |
| 4337 | |
| 4338 | if (memcmp(type,mng_cHRM,4) == 0) |
| 4339 | { |
| 4340 | if (length == 32) |
| 4341 | { |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 4342 | image->chromaticity.white_point.x=0.00001*mng_get_long(p); |
| 4343 | image->chromaticity.white_point.y=0.00001*mng_get_long(&p[4]); |
| 4344 | image->chromaticity.red_primary.x=0.00001*mng_get_long(&p[8]); |
| 4345 | image->chromaticity.red_primary.y=0.00001*mng_get_long(&p[12]); |
| 4346 | image->chromaticity.green_primary.x=0.00001*mng_get_long(&p[16]); |
| 4347 | image->chromaticity.green_primary.y=0.00001*mng_get_long(&p[20]); |
| 4348 | image->chromaticity.blue_primary.x=0.00001*mng_get_long(&p[24]); |
| 4349 | image->chromaticity.blue_primary.y=0.00001*mng_get_long(&p[28]); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4350 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4351 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4352 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 4353 | continue; |
| 4354 | } |
| 4355 | |
| 4356 | if (memcmp(type,mng_sRGB,4) == 0) |
| 4357 | { |
| 4358 | if (length == 1) |
| 4359 | { |
glennrp | e610a07 | 2010-08-05 17:08:46 +0000 | [diff] [blame] | 4360 | image->rendering_intent= |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 4361 | Magick_RenderingIntent_from_PNG_RenderingIntent(p[0]); |
cristy | da7803d | 2012-05-03 01:22:45 +0000 | [diff] [blame] | 4362 | image->gamma=1.000f/2.200f; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4363 | image->chromaticity.red_primary.x=0.6400f; |
| 4364 | image->chromaticity.red_primary.y=0.3300f; |
| 4365 | image->chromaticity.green_primary.x=0.3000f; |
| 4366 | image->chromaticity.green_primary.y=0.6000f; |
| 4367 | image->chromaticity.blue_primary.x=0.1500f; |
| 4368 | image->chromaticity.blue_primary.y=0.0600f; |
| 4369 | image->chromaticity.white_point.x=0.3127f; |
| 4370 | image->chromaticity.white_point.y=0.3290f; |
| 4371 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4372 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4373 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 4374 | continue; |
| 4375 | } |
| 4376 | |
| 4377 | if (memcmp(type,mng_oFFs,4) == 0) |
| 4378 | { |
| 4379 | if (length > 8) |
| 4380 | { |
glennrp | 5eae760 | 2011-02-22 15:21:32 +0000 | [diff] [blame] | 4381 | image->page.x=(ssize_t) mng_get_long(p); |
| 4382 | image->page.y=(ssize_t) mng_get_long(&p[4]); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4383 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4384 | if ((int) p[8] != 0) |
| 4385 | { |
| 4386 | image->page.x/=10000; |
| 4387 | image->page.y/=10000; |
| 4388 | } |
| 4389 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4390 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4391 | if (length) |
| 4392 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4393 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4394 | continue; |
| 4395 | } |
| 4396 | |
| 4397 | if (memcmp(type,mng_pHYs,4) == 0) |
| 4398 | { |
| 4399 | if (length > 8) |
| 4400 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 4401 | image->resolution.x=(double) mng_get_long(p); |
| 4402 | image->resolution.y=(double) mng_get_long(&p[4]); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4403 | if ((int) p[8] == PNG_RESOLUTION_METER) |
| 4404 | { |
| 4405 | image->units=PixelsPerCentimeterResolution; |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 4406 | image->resolution.x=image->resolution.x/100.0f; |
| 4407 | image->resolution.y=image->resolution.y/100.0f; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4408 | } |
| 4409 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4410 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4411 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 4412 | continue; |
| 4413 | } |
| 4414 | |
| 4415 | #if 0 |
| 4416 | if (memcmp(type,mng_iCCP,4) == 0) |
| 4417 | { |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 4418 | /* To do: */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4419 | if (length) |
| 4420 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4421 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4422 | continue; |
| 4423 | } |
| 4424 | #endif |
| 4425 | |
| 4426 | if (length) |
| 4427 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 4428 | |
| 4429 | if (memcmp(type,mng_IEND,4)) |
| 4430 | continue; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4431 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4432 | break; |
| 4433 | } |
| 4434 | |
| 4435 | |
| 4436 | /* IEND found */ |
| 4437 | |
| 4438 | /* |
| 4439 | Finish up reading image data: |
| 4440 | |
| 4441 | o read main image from color_blob. |
| 4442 | |
| 4443 | o close color_blob. |
| 4444 | |
| 4445 | o if (color_type has alpha) |
| 4446 | if alpha_encoding is PNG |
| 4447 | read secondary image from alpha_blob via ReadPNG |
| 4448 | if alpha_encoding is JPEG |
| 4449 | read secondary image from alpha_blob via ReadJPEG |
| 4450 | |
| 4451 | o close alpha_blob. |
| 4452 | |
| 4453 | o copy intensity of secondary image into |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 4454 | alpha samples of main image. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4455 | |
| 4456 | o destroy the secondary image. |
| 4457 | */ |
| 4458 | |
| 4459 | (void) CloseBlob(color_image); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4460 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4461 | if (logging != MagickFalse) |
| 4462 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 4463 | " Reading jng_image from color_blob."); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4464 | |
cristy | 3b6fd2e | 2011-05-20 12:53:50 +0000 | [diff] [blame] | 4465 | (void) FormatLocaleString(color_image_info->filename,MaxTextExtent,"%s", |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4466 | color_image->filename); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4467 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4468 | color_image_info->ping=MagickFalse; /* To do: avoid this */ |
| 4469 | jng_image=ReadImage(color_image_info,exception); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4470 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4471 | if (jng_image == (Image *) NULL) |
| 4472 | return((Image *) NULL); |
| 4473 | |
| 4474 | (void) RelinquishUniqueFileResource(color_image->filename); |
| 4475 | color_image=DestroyImage(color_image); |
| 4476 | color_image_info=DestroyImageInfo(color_image_info); |
| 4477 | |
| 4478 | if (jng_image == (Image *) NULL) |
| 4479 | return((Image *) NULL); |
| 4480 | |
| 4481 | if (logging != MagickFalse) |
| 4482 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 4483 | " Copying jng_image pixels to main image."); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4484 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4485 | image->rows=jng_height; |
| 4486 | image->columns=jng_width; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4487 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 4488 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4489 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 4490 | s=GetVirtualPixels(jng_image,0,y,image->columns,1,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4491 | q=GetAuthenticPixels(image,0,y,image->columns,1,exception); |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 4492 | for (x=(ssize_t) image->columns; x != 0; x--) |
| 4493 | { |
| 4494 | SetPixelRed(image,GetPixelRed(jng_image,s),q); |
| 4495 | SetPixelGreen(image,GetPixelGreen(jng_image,s),q); |
| 4496 | SetPixelBlue(image,GetPixelBlue(jng_image,s),q); |
| 4497 | q+=GetPixelChannels(image); |
| 4498 | s+=GetPixelChannels(jng_image); |
| 4499 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4500 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4501 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 4502 | break; |
| 4503 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4504 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4505 | jng_image=DestroyImage(jng_image); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4506 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4507 | if (image_info->ping == MagickFalse) |
| 4508 | { |
| 4509 | if (jng_color_type >= 12) |
| 4510 | { |
| 4511 | if (jng_alpha_compression_method == 0) |
| 4512 | { |
| 4513 | png_byte |
| 4514 | data[5]; |
| 4515 | (void) WriteBlobMSBULong(alpha_image,0x00000000L); |
| 4516 | PNGType(data,mng_IEND); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 4517 | LogPNGChunk(logging,mng_IEND,0L); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4518 | (void) WriteBlob(alpha_image,4,data); |
| 4519 | (void) WriteBlobMSBULong(alpha_image,crc32(0,data,4)); |
| 4520 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4521 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4522 | (void) CloseBlob(alpha_image); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4523 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4524 | if (logging != MagickFalse) |
| 4525 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 4526 | " Reading alpha from alpha_blob."); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4527 | |
cristy | 3b6fd2e | 2011-05-20 12:53:50 +0000 | [diff] [blame] | 4528 | (void) FormatLocaleString(alpha_image_info->filename,MaxTextExtent, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4529 | "%s",alpha_image->filename); |
| 4530 | |
| 4531 | jng_image=ReadImage(alpha_image_info,exception); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4532 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4533 | if (jng_image != (Image *) NULL) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 4534 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4535 | { |
| 4536 | s=GetVirtualPixels(jng_image,0,y,image->columns,1, |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 4537 | exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4538 | q=GetAuthenticPixels(image,0,y,image->columns,1,exception); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4539 | |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 4540 | if (image->alpha_trait == BlendPixelTrait) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 4541 | for (x=(ssize_t) image->columns; x != 0; x--) |
| 4542 | { |
| 4543 | SetPixelAlpha(image,GetPixelRed(jng_image,s),q); |
| 4544 | q+=GetPixelChannels(image); |
| 4545 | s+=GetPixelChannels(jng_image); |
| 4546 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4547 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4548 | else |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 4549 | for (x=(ssize_t) image->columns; x != 0; x--) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4550 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 4551 | SetPixelAlpha(image,GetPixelRed(jng_image,s),q); |
| 4552 | if (GetPixelAlpha(image,q) != OpaqueAlpha) |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 4553 | image->alpha_trait=BlendPixelTrait; |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 4554 | q+=GetPixelChannels(image); |
| 4555 | s+=GetPixelChannels(jng_image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4556 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4557 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4558 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 4559 | break; |
| 4560 | } |
| 4561 | (void) RelinquishUniqueFileResource(alpha_image->filename); |
| 4562 | alpha_image=DestroyImage(alpha_image); |
| 4563 | alpha_image_info=DestroyImageInfo(alpha_image_info); |
| 4564 | if (jng_image != (Image *) NULL) |
| 4565 | jng_image=DestroyImage(jng_image); |
| 4566 | } |
| 4567 | } |
| 4568 | |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4569 | /* Read the JNG image. */ |
| 4570 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4571 | if (mng_info->mng_type == 0) |
| 4572 | { |
| 4573 | mng_info->mng_width=jng_width; |
| 4574 | mng_info->mng_height=jng_height; |
| 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 | if (image->page.width == 0 && image->page.height == 0) |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4578 | { |
| 4579 | image->page.width=jng_width; |
| 4580 | image->page.height=jng_height; |
| 4581 | } |
| 4582 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4583 | if (image->page.x == 0 && image->page.y == 0) |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4584 | { |
| 4585 | image->page.x=mng_info->x_off[mng_info->object_id]; |
| 4586 | image->page.y=mng_info->y_off[mng_info->object_id]; |
| 4587 | } |
| 4588 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4589 | else |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4590 | { |
| 4591 | image->page.y=mng_info->y_off[mng_info->object_id]; |
| 4592 | } |
| 4593 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4594 | mng_info->image_found++; |
| 4595 | status=SetImageProgress(image,LoadImagesTag,2*TellBlob(image), |
| 4596 | 2*GetBlobSize(image)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4597 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4598 | if (logging != MagickFalse) |
| 4599 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 4600 | " exit ReadOneJNGImage()"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4601 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4602 | return(image); |
| 4603 | } |
| 4604 | |
| 4605 | /* |
| 4606 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 4607 | % % |
| 4608 | % % |
| 4609 | % % |
| 4610 | % R e a d J N G I m a g e % |
| 4611 | % % |
| 4612 | % % |
| 4613 | % % |
| 4614 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 4615 | % |
| 4616 | % ReadJNGImage() reads a JPEG Network Graphics (JNG) image file |
| 4617 | % (including the 8-byte signature) and returns it. It allocates the memory |
| 4618 | % necessary for the new Image structure and returns a pointer to the new |
| 4619 | % image. |
| 4620 | % |
| 4621 | % JNG support written by Glenn Randers-Pehrson, glennrp@image... |
| 4622 | % |
| 4623 | % The format of the ReadJNGImage method is: |
| 4624 | % |
| 4625 | % Image *ReadJNGImage(const ImageInfo *image_info, ExceptionInfo |
| 4626 | % *exception) |
| 4627 | % |
| 4628 | % A description of each parameter follows: |
| 4629 | % |
| 4630 | % o image_info: the image info. |
| 4631 | % |
| 4632 | % o exception: return any errors or warnings in this structure. |
| 4633 | % |
| 4634 | */ |
| 4635 | |
| 4636 | static Image *ReadJNGImage(const ImageInfo *image_info,ExceptionInfo *exception) |
| 4637 | { |
| 4638 | Image |
| 4639 | *image, |
| 4640 | *previous; |
| 4641 | |
| 4642 | MagickBooleanType |
glennrp | 21f0e62 | 2011-01-07 16:20:57 +0000 | [diff] [blame] | 4643 | have_mng_structure, |
| 4644 | logging, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4645 | status; |
| 4646 | |
| 4647 | MngInfo |
| 4648 | *mng_info; |
| 4649 | |
| 4650 | char |
| 4651 | magic_number[MaxTextExtent]; |
| 4652 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4653 | size_t |
| 4654 | count; |
| 4655 | |
| 4656 | /* |
| 4657 | Open image file. |
| 4658 | */ |
| 4659 | assert(image_info != (const ImageInfo *) NULL); |
| 4660 | assert(image_info->signature == MagickSignature); |
| 4661 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image_info->filename); |
| 4662 | assert(exception != (ExceptionInfo *) NULL); |
| 4663 | assert(exception->signature == MagickSignature); |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 4664 | logging=LogMagickEvent(CoderEvent,GetMagickModule(),"Enter ReadJNGImage()"); |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 4665 | image=AcquireImage(image_info,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4666 | mng_info=(MngInfo *) NULL; |
| 4667 | status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4668 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4669 | if (status == MagickFalse) |
| 4670 | return((Image *) NULL); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4671 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4672 | if (LocaleCompare(image_info->magick,"JNG") != 0) |
| 4673 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4674 | |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4675 | /* Verify JNG signature. */ |
| 4676 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4677 | count=(size_t) ReadBlob(image,8,(unsigned char *) magic_number); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4678 | |
glennrp | 3b8763e | 2011-02-21 12:08:18 +0000 | [diff] [blame] | 4679 | if (count < 8 || memcmp(magic_number,"\213JNG\r\n\032\n",8) != 0) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4680 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4681 | |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4682 | /* Allocate a MngInfo structure. */ |
| 4683 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4684 | have_mng_structure=MagickFalse; |
cristy | 73bd4a5 | 2010-10-05 11:24:23 +0000 | [diff] [blame] | 4685 | mng_info=(MngInfo *) AcquireMagickMemory(sizeof(*mng_info)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4686 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4687 | if (mng_info == (MngInfo *) NULL) |
| 4688 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4689 | |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4690 | /* Initialize members of the MngInfo structure. */ |
| 4691 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4692 | (void) ResetMagickMemory(mng_info,0,sizeof(MngInfo)); |
| 4693 | have_mng_structure=MagickTrue; |
| 4694 | |
| 4695 | mng_info->image=image; |
| 4696 | previous=image; |
| 4697 | image=ReadOneJNGImage(mng_info,image_info,exception); |
| 4698 | MngInfoFreeStruct(mng_info,&have_mng_structure); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4699 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4700 | if (image == (Image *) NULL) |
| 4701 | { |
| 4702 | if (IsImageObject(previous) != MagickFalse) |
| 4703 | { |
| 4704 | (void) CloseBlob(previous); |
| 4705 | (void) DestroyImageList(previous); |
| 4706 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4707 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4708 | if (logging != MagickFalse) |
| 4709 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 4710 | "exit ReadJNGImage() with error"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4711 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4712 | return((Image *) NULL); |
| 4713 | } |
| 4714 | (void) CloseBlob(image); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4715 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4716 | if (image->columns == 0 || image->rows == 0) |
| 4717 | { |
| 4718 | if (logging != MagickFalse) |
| 4719 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 4720 | "exit ReadJNGImage() with error"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4721 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4722 | ThrowReaderException(CorruptImageError,"CorruptImage"); |
| 4723 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4724 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4725 | if (logging != MagickFalse) |
| 4726 | (void) LogMagickEvent(CoderEvent,GetMagickModule(),"exit ReadJNGImage()"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4727 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4728 | return(image); |
| 4729 | } |
| 4730 | #endif |
| 4731 | |
| 4732 | static Image *ReadMNGImage(const ImageInfo *image_info,ExceptionInfo *exception) |
| 4733 | { |
| 4734 | char |
| 4735 | page_geometry[MaxTextExtent]; |
| 4736 | |
| 4737 | Image |
| 4738 | *image, |
| 4739 | *previous; |
| 4740 | |
cristy | 4383ec8 | 2011-01-05 15:42:32 +0000 | [diff] [blame] | 4741 | MagickBooleanType |
glennrp | 21f0e62 | 2011-01-07 16:20:57 +0000 | [diff] [blame] | 4742 | logging, |
| 4743 | have_mng_structure; |
cristy | 4383ec8 | 2011-01-05 15:42:32 +0000 | [diff] [blame] | 4744 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4745 | volatile int |
| 4746 | first_mng_object, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4747 | object_id, |
| 4748 | term_chunk_found, |
| 4749 | skip_to_iend; |
| 4750 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 4751 | volatile ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4752 | image_count=0; |
| 4753 | |
| 4754 | MagickBooleanType |
| 4755 | status; |
| 4756 | |
| 4757 | MagickOffsetType |
| 4758 | offset; |
| 4759 | |
| 4760 | MngInfo |
| 4761 | *mng_info; |
| 4762 | |
| 4763 | MngBox |
| 4764 | default_fb, |
| 4765 | fb, |
| 4766 | previous_fb; |
| 4767 | |
| 4768 | #if defined(MNG_INSERT_LAYERS) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 4769 | PixelInfo |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4770 | mng_background_color; |
| 4771 | #endif |
| 4772 | |
| 4773 | register unsigned char |
| 4774 | *p; |
| 4775 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 4776 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4777 | i; |
| 4778 | |
| 4779 | size_t |
| 4780 | count; |
| 4781 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 4782 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4783 | loop_level; |
| 4784 | |
| 4785 | volatile short |
| 4786 | skipping_loop; |
| 4787 | |
| 4788 | #if defined(MNG_INSERT_LAYERS) |
| 4789 | unsigned int |
| 4790 | mandatory_back=0; |
| 4791 | #endif |
| 4792 | |
| 4793 | volatile unsigned int |
| 4794 | #ifdef MNG_OBJECT_BUFFERS |
| 4795 | mng_background_object=0, |
| 4796 | #endif |
| 4797 | mng_type=0; /* 0: PNG or JNG; 1: MNG; 2: MNG-LC; 3: MNG-VLC */ |
| 4798 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 4799 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4800 | default_frame_timeout, |
| 4801 | frame_timeout, |
| 4802 | #if defined(MNG_INSERT_LAYERS) |
| 4803 | image_height, |
| 4804 | image_width, |
| 4805 | #endif |
| 4806 | length; |
| 4807 | |
glennrp | 38ea083 | 2010-06-02 18:50:28 +0000 | [diff] [blame] | 4808 | /* These delays are all measured in image ticks_per_second, |
| 4809 | * not in MNG ticks_per_second |
| 4810 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 4811 | volatile size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4812 | default_frame_delay, |
| 4813 | final_delay, |
| 4814 | final_image_delay, |
| 4815 | frame_delay, |
| 4816 | #if defined(MNG_INSERT_LAYERS) |
| 4817 | insert_layers, |
| 4818 | #endif |
| 4819 | mng_iterations=1, |
| 4820 | simplicity=0, |
| 4821 | subframe_height=0, |
| 4822 | subframe_width=0; |
| 4823 | |
| 4824 | previous_fb.top=0; |
| 4825 | previous_fb.bottom=0; |
| 4826 | previous_fb.left=0; |
| 4827 | previous_fb.right=0; |
| 4828 | default_fb.top=0; |
| 4829 | default_fb.bottom=0; |
| 4830 | default_fb.left=0; |
| 4831 | default_fb.right=0; |
| 4832 | |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4833 | /* Open image file. */ |
| 4834 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4835 | assert(image_info != (const ImageInfo *) NULL); |
| 4836 | assert(image_info->signature == MagickSignature); |
| 4837 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image_info->filename); |
| 4838 | assert(exception != (ExceptionInfo *) NULL); |
| 4839 | assert(exception->signature == MagickSignature); |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 4840 | logging=LogMagickEvent(CoderEvent,GetMagickModule(),"Enter ReadMNGImage()"); |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 4841 | image=AcquireImage(image_info,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4842 | mng_info=(MngInfo *) NULL; |
| 4843 | status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4844 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4845 | if (status == MagickFalse) |
| 4846 | return((Image *) NULL); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4847 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4848 | first_mng_object=MagickFalse; |
| 4849 | skipping_loop=(-1); |
| 4850 | have_mng_structure=MagickFalse; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4851 | |
| 4852 | /* Allocate a MngInfo structure. */ |
| 4853 | |
cristy | 73bd4a5 | 2010-10-05 11:24:23 +0000 | [diff] [blame] | 4854 | mng_info=(MngInfo *) AcquireMagickMemory(sizeof(MngInfo)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4855 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4856 | if (mng_info == (MngInfo *) NULL) |
| 4857 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4858 | |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4859 | /* Initialize members of the MngInfo structure. */ |
| 4860 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4861 | (void) ResetMagickMemory(mng_info,0,sizeof(MngInfo)); |
| 4862 | mng_info->image=image; |
| 4863 | have_mng_structure=MagickTrue; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4864 | |
| 4865 | if (LocaleCompare(image_info->magick,"MNG") == 0) |
| 4866 | { |
| 4867 | char |
| 4868 | magic_number[MaxTextExtent]; |
| 4869 | |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4870 | /* Verify MNG signature. */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4871 | count=(size_t) ReadBlob(image,8,(unsigned char *) magic_number); |
| 4872 | if (memcmp(magic_number,"\212MNG\r\n\032\n",8) != 0) |
| 4873 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4874 | |
| 4875 | /* Initialize some nonzero members of the MngInfo structure. */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4876 | for (i=0; i < MNG_MAX_OBJECTS; i++) |
| 4877 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 4878 | mng_info->object_clip[i].right=(ssize_t) PNG_UINT_31_MAX; |
| 4879 | mng_info->object_clip[i].bottom=(ssize_t) PNG_UINT_31_MAX; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4880 | } |
| 4881 | mng_info->exists[0]=MagickTrue; |
| 4882 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4883 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4884 | first_mng_object=MagickTrue; |
| 4885 | mng_type=0; |
| 4886 | #if defined(MNG_INSERT_LAYERS) |
| 4887 | insert_layers=MagickFalse; /* should be False when converting or mogrifying */ |
| 4888 | #endif |
| 4889 | default_frame_delay=0; |
| 4890 | default_frame_timeout=0; |
| 4891 | frame_delay=0; |
| 4892 | final_delay=1; |
| 4893 | mng_info->ticks_per_second=1UL*image->ticks_per_second; |
| 4894 | object_id=0; |
| 4895 | skip_to_iend=MagickFalse; |
| 4896 | term_chunk_found=MagickFalse; |
| 4897 | mng_info->framing_mode=1; |
| 4898 | #if defined(MNG_INSERT_LAYERS) |
| 4899 | mandatory_back=MagickFalse; |
| 4900 | #endif |
| 4901 | #if defined(MNG_INSERT_LAYERS) |
| 4902 | mng_background_color=image->background_color; |
| 4903 | #endif |
| 4904 | default_fb=mng_info->frame; |
| 4905 | previous_fb=mng_info->frame; |
| 4906 | do |
| 4907 | { |
| 4908 | char |
| 4909 | type[MaxTextExtent]; |
| 4910 | |
| 4911 | if (LocaleCompare(image_info->magick,"MNG") == 0) |
| 4912 | { |
| 4913 | unsigned char |
| 4914 | *chunk; |
| 4915 | |
| 4916 | /* |
| 4917 | Read a new chunk. |
| 4918 | */ |
| 4919 | type[0]='\0'; |
| 4920 | (void) ConcatenateMagickString(type,"errr",MaxTextExtent); |
| 4921 | length=ReadBlobMSBLong(image); |
| 4922 | count=(size_t) ReadBlob(image,4,(unsigned char *) type); |
| 4923 | |
| 4924 | if (logging != MagickFalse) |
| 4925 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 4926 | " Reading MNG chunk type %c%c%c%c, length: %.20g", |
| 4927 | type[0],type[1],type[2],type[3],(double) length); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4928 | |
| 4929 | if (length > PNG_UINT_31_MAX) |
| 4930 | status=MagickFalse; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4931 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4932 | if (count == 0) |
| 4933 | ThrowReaderException(CorruptImageError,"CorruptImage"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4934 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4935 | p=NULL; |
| 4936 | chunk=(unsigned char *) NULL; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4937 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4938 | if (length) |
| 4939 | { |
| 4940 | chunk=(unsigned char *) AcquireQuantumMemory(length,sizeof(*chunk)); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4941 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4942 | if (chunk == (unsigned char *) NULL) |
| 4943 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4944 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 4945 | for (i=0; i < (ssize_t) length; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4946 | chunk[i]=(unsigned char) ReadBlobByte(image); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4947 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4948 | p=chunk; |
| 4949 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4950 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4951 | (void) ReadBlobMSBLong(image); /* read crc word */ |
| 4952 | |
| 4953 | #if !defined(JNG_SUPPORTED) |
| 4954 | if (memcmp(type,mng_JHDR,4) == 0) |
| 4955 | { |
| 4956 | skip_to_iend=MagickTrue; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4957 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4958 | if (mng_info->jhdr_warning == 0) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 4959 | (void) ThrowMagickException(exception,GetMagickModule(), |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4960 | CoderError,"JNGCompressNotSupported","`%s'",image->filename); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4961 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4962 | mng_info->jhdr_warning++; |
| 4963 | } |
| 4964 | #endif |
| 4965 | if (memcmp(type,mng_DHDR,4) == 0) |
| 4966 | { |
| 4967 | skip_to_iend=MagickTrue; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4968 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4969 | if (mng_info->dhdr_warning == 0) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 4970 | (void) ThrowMagickException(exception,GetMagickModule(), |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4971 | CoderError,"DeltaPNGNotSupported","`%s'",image->filename); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4972 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4973 | mng_info->dhdr_warning++; |
| 4974 | } |
| 4975 | if (memcmp(type,mng_MEND,4) == 0) |
| 4976 | break; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4977 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4978 | if (skip_to_iend) |
| 4979 | { |
| 4980 | if (memcmp(type,mng_IEND,4) == 0) |
| 4981 | skip_to_iend=MagickFalse; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4982 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4983 | if (length) |
| 4984 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4985 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4986 | if (logging != MagickFalse) |
| 4987 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 4988 | " Skip to IEND."); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4989 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4990 | continue; |
| 4991 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4992 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4993 | if (memcmp(type,mng_MHDR,4) == 0) |
| 4994 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 4995 | mng_info->mng_width=(size_t) ((p[0] << 24) | (p[1] << 16) | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4996 | (p[2] << 8) | p[3]); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4997 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 4998 | mng_info->mng_height=(size_t) ((p[4] << 24) | (p[5] << 16) | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4999 | (p[6] << 8) | p[7]); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5000 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5001 | if (logging != MagickFalse) |
| 5002 | { |
| 5003 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 5004 | " MNG width: %.20g",(double) mng_info->mng_width); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5005 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 5006 | " MNG height: %.20g",(double) mng_info->mng_height); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5007 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5008 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5009 | p+=8; |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 5010 | mng_info->ticks_per_second=(size_t) mng_get_long(p); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5011 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5012 | if (mng_info->ticks_per_second == 0) |
| 5013 | default_frame_delay=0; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5014 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5015 | else |
| 5016 | default_frame_delay=1UL*image->ticks_per_second/ |
| 5017 | mng_info->ticks_per_second; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5018 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5019 | frame_delay=default_frame_delay; |
| 5020 | simplicity=0; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5021 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5022 | if (length > 16) |
| 5023 | { |
| 5024 | p+=16; |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 5025 | simplicity=(size_t) mng_get_long(p); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5026 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5027 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5028 | mng_type=1; /* Full MNG */ |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5029 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5030 | if ((simplicity != 0) && ((simplicity | 11) == 11)) |
| 5031 | mng_type=2; /* LC */ |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5032 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5033 | if ((simplicity != 0) && ((simplicity | 9) == 9)) |
| 5034 | mng_type=3; /* VLC */ |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5035 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5036 | #if defined(MNG_INSERT_LAYERS) |
| 5037 | if (mng_type != 3) |
| 5038 | insert_layers=MagickTrue; |
| 5039 | #endif |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 5040 | if (GetAuthenticPixelQueue(image) != (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5041 | { |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5042 | /* Allocate next image structure. */ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 5043 | AcquireNextImage(image_info,image,exception); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5044 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5045 | if (GetNextImageInList(image) == (Image *) NULL) |
| 5046 | return((Image *) NULL); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5047 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5048 | image=SyncNextImageInList(image); |
| 5049 | mng_info->image=image; |
| 5050 | } |
| 5051 | |
| 5052 | if ((mng_info->mng_width > 65535L) || |
| 5053 | (mng_info->mng_height > 65535L)) |
| 5054 | ThrowReaderException(ImageError,"WidthOrHeightExceedsLimit"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5055 | |
cristy | 3b6fd2e | 2011-05-20 12:53:50 +0000 | [diff] [blame] | 5056 | (void) FormatLocaleString(page_geometry,MaxTextExtent, |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 5057 | "%.20gx%.20g+0+0",(double) mng_info->mng_width,(double) |
cristy | f2faecf | 2010-05-28 19:19:36 +0000 | [diff] [blame] | 5058 | mng_info->mng_height); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5059 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5060 | mng_info->frame.left=0; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 5061 | mng_info->frame.right=(ssize_t) mng_info->mng_width; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5062 | mng_info->frame.top=0; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 5063 | mng_info->frame.bottom=(ssize_t) mng_info->mng_height; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5064 | mng_info->clip=default_fb=previous_fb=mng_info->frame; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5065 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5066 | for (i=0; i < MNG_MAX_OBJECTS; i++) |
| 5067 | mng_info->object_clip[i]=mng_info->frame; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5068 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5069 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 5070 | continue; |
| 5071 | } |
| 5072 | |
| 5073 | if (memcmp(type,mng_TERM,4) == 0) |
| 5074 | { |
| 5075 | int |
| 5076 | repeat=0; |
| 5077 | |
| 5078 | |
| 5079 | if (length) |
| 5080 | repeat=p[0]; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5081 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5082 | if (repeat == 3) |
| 5083 | { |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 5084 | final_delay=(png_uint_32) mng_get_long(&p[2]); |
| 5085 | mng_iterations=(png_uint_32) mng_get_long(&p[6]); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5086 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5087 | if (mng_iterations == PNG_UINT_31_MAX) |
| 5088 | mng_iterations=0; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5089 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5090 | image->iterations=mng_iterations; |
| 5091 | term_chunk_found=MagickTrue; |
| 5092 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5093 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5094 | if (logging != MagickFalse) |
| 5095 | { |
| 5096 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 5097 | " repeat=%d",repeat); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5098 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5099 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 5100 | " final_delay=%.20g",(double) final_delay); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5101 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5102 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 5103 | " image->iterations=%.20g",(double) image->iterations); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5104 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5105 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5106 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 5107 | continue; |
| 5108 | } |
| 5109 | if (memcmp(type,mng_DEFI,4) == 0) |
| 5110 | { |
| 5111 | if (mng_type == 3) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 5112 | (void) ThrowMagickException(exception,GetMagickModule(), |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5113 | CoderError,"DEFI chunk found in MNG-VLC datastream","`%s'", |
| 5114 | image->filename); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5115 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5116 | object_id=(p[0] << 8) | p[1]; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5117 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5118 | if (mng_type == 2 && object_id != 0) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 5119 | (void) ThrowMagickException(exception,GetMagickModule(), |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5120 | CoderError,"Nonzero object_id in MNG-LC datastream","`%s'", |
| 5121 | image->filename); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5122 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5123 | if (object_id > MNG_MAX_OBJECTS) |
| 5124 | { |
| 5125 | /* |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 5126 | Instead of using a warning we should allocate a larger |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5127 | MngInfo structure and continue. |
| 5128 | */ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 5129 | (void) ThrowMagickException(exception,GetMagickModule(), |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5130 | CoderError,"object id too large","`%s'",image->filename); |
| 5131 | object_id=MNG_MAX_OBJECTS; |
| 5132 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5133 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5134 | if (mng_info->exists[object_id]) |
| 5135 | if (mng_info->frozen[object_id]) |
| 5136 | { |
| 5137 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 5138 | (void) ThrowMagickException(exception, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5139 | GetMagickModule(),CoderError, |
| 5140 | "DEFI cannot redefine a frozen MNG object","`%s'", |
| 5141 | image->filename); |
| 5142 | continue; |
| 5143 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5144 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5145 | mng_info->exists[object_id]=MagickTrue; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5146 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5147 | if (length > 2) |
| 5148 | mng_info->invisible[object_id]=p[2]; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5149 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5150 | /* |
| 5151 | Extract object offset info. |
| 5152 | */ |
| 5153 | if (length > 11) |
| 5154 | { |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5155 | mng_info->x_off[object_id]=(ssize_t) ((p[4] << 24) | |
| 5156 | (p[5] << 16) | (p[6] << 8) | p[7]); |
| 5157 | |
| 5158 | mng_info->y_off[object_id]=(ssize_t) ((p[8] << 24) | |
| 5159 | (p[9] << 16) | (p[10] << 8) | p[11]); |
| 5160 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5161 | if (logging != MagickFalse) |
| 5162 | { |
| 5163 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 5164 | " x_off[%d]: %.20g",object_id,(double) |
cristy | f2faecf | 2010-05-28 19:19:36 +0000 | [diff] [blame] | 5165 | mng_info->x_off[object_id]); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5166 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5167 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 5168 | " y_off[%d]: %.20g",object_id,(double) |
cristy | f2faecf | 2010-05-28 19:19:36 +0000 | [diff] [blame] | 5169 | mng_info->y_off[object_id]); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5170 | } |
| 5171 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5172 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5173 | /* |
| 5174 | Extract object clipping info. |
| 5175 | */ |
| 5176 | if (length > 27) |
| 5177 | mng_info->object_clip[object_id]=mng_read_box(mng_info->frame,0, |
| 5178 | &p[12]); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5179 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5180 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 5181 | continue; |
| 5182 | } |
| 5183 | if (memcmp(type,mng_bKGD,4) == 0) |
| 5184 | { |
| 5185 | mng_info->have_global_bkgd=MagickFalse; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5186 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5187 | if (length > 5) |
| 5188 | { |
| 5189 | mng_info->mng_global_bkgd.red= |
| 5190 | ScaleShortToQuantum((unsigned short) ((p[0] << 8) | p[1])); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5191 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5192 | mng_info->mng_global_bkgd.green= |
| 5193 | ScaleShortToQuantum((unsigned short) ((p[2] << 8) | p[3])); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5194 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5195 | mng_info->mng_global_bkgd.blue= |
| 5196 | ScaleShortToQuantum((unsigned short) ((p[4] << 8) | p[5])); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5197 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5198 | mng_info->have_global_bkgd=MagickTrue; |
| 5199 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5200 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5201 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 5202 | continue; |
| 5203 | } |
| 5204 | if (memcmp(type,mng_BACK,4) == 0) |
| 5205 | { |
| 5206 | #if defined(MNG_INSERT_LAYERS) |
| 5207 | if (length > 6) |
| 5208 | mandatory_back=p[6]; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5209 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5210 | else |
| 5211 | mandatory_back=0; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5212 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5213 | if (mandatory_back && length > 5) |
| 5214 | { |
| 5215 | mng_background_color.red= |
| 5216 | ScaleShortToQuantum((unsigned short) ((p[0] << 8) | p[1])); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5217 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5218 | mng_background_color.green= |
| 5219 | ScaleShortToQuantum((unsigned short) ((p[2] << 8) | p[3])); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5220 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5221 | mng_background_color.blue= |
| 5222 | ScaleShortToQuantum((unsigned short) ((p[4] << 8) | p[5])); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5223 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 5224 | mng_background_color.alpha=OpaqueAlpha; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5225 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5226 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5227 | #ifdef MNG_OBJECT_BUFFERS |
| 5228 | if (length > 8) |
| 5229 | mng_background_object=(p[7] << 8) | p[8]; |
| 5230 | #endif |
| 5231 | #endif |
| 5232 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 5233 | continue; |
| 5234 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5235 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5236 | if (memcmp(type,mng_PLTE,4) == 0) |
| 5237 | { |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5238 | /* Read global PLTE. */ |
| 5239 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5240 | if (length && (length < 769)) |
| 5241 | { |
| 5242 | if (mng_info->global_plte == (png_colorp) NULL) |
| 5243 | mng_info->global_plte=(png_colorp) AcquireQuantumMemory(256, |
| 5244 | sizeof(*mng_info->global_plte)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5245 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 5246 | for (i=0; i < (ssize_t) (length/3); i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5247 | { |
| 5248 | mng_info->global_plte[i].red=p[3*i]; |
| 5249 | mng_info->global_plte[i].green=p[3*i+1]; |
| 5250 | mng_info->global_plte[i].blue=p[3*i+2]; |
| 5251 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5252 | |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 5253 | mng_info->global_plte_length=(unsigned int) (length/3); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5254 | } |
| 5255 | #ifdef MNG_LOOSE |
| 5256 | for ( ; i < 256; i++) |
| 5257 | { |
| 5258 | mng_info->global_plte[i].red=i; |
| 5259 | mng_info->global_plte[i].green=i; |
| 5260 | mng_info->global_plte[i].blue=i; |
| 5261 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5262 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5263 | if (length) |
| 5264 | mng_info->global_plte_length=256; |
| 5265 | #endif |
| 5266 | else |
| 5267 | mng_info->global_plte_length=0; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5268 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5269 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 5270 | continue; |
| 5271 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5272 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5273 | if (memcmp(type,mng_tRNS,4) == 0) |
| 5274 | { |
| 5275 | /* read global tRNS */ |
| 5276 | |
| 5277 | if (length < 257) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 5278 | for (i=0; i < (ssize_t) length; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5279 | mng_info->global_trns[i]=p[i]; |
| 5280 | |
| 5281 | #ifdef MNG_LOOSE |
| 5282 | for ( ; i < 256; i++) |
| 5283 | mng_info->global_trns[i]=255; |
| 5284 | #endif |
cristy | 12560f3 | 2010-06-03 16:51:08 +0000 | [diff] [blame] | 5285 | mng_info->global_trns_length=(unsigned int) length; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5286 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 5287 | continue; |
| 5288 | } |
| 5289 | if (memcmp(type,mng_gAMA,4) == 0) |
| 5290 | { |
| 5291 | if (length == 4) |
| 5292 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 5293 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5294 | igamma; |
| 5295 | |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 5296 | igamma=mng_get_long(p); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5297 | mng_info->global_gamma=((float) igamma)*0.00001; |
| 5298 | mng_info->have_global_gama=MagickTrue; |
| 5299 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5300 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5301 | else |
| 5302 | mng_info->have_global_gama=MagickFalse; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5303 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5304 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 5305 | continue; |
| 5306 | } |
| 5307 | |
| 5308 | if (memcmp(type,mng_cHRM,4) == 0) |
| 5309 | { |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5310 | /* Read global cHRM */ |
| 5311 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5312 | if (length == 32) |
| 5313 | { |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 5314 | mng_info->global_chrm.white_point.x=0.00001*mng_get_long(p); |
| 5315 | mng_info->global_chrm.white_point.y=0.00001*mng_get_long(&p[4]); |
| 5316 | 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] | 5317 | mng_info->global_chrm.red_primary.y=0.00001* |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 5318 | mng_get_long(&p[12]); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5319 | mng_info->global_chrm.green_primary.x=0.00001* |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 5320 | mng_get_long(&p[16]); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5321 | mng_info->global_chrm.green_primary.y=0.00001* |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 5322 | mng_get_long(&p[20]); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5323 | mng_info->global_chrm.blue_primary.x=0.00001* |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 5324 | mng_get_long(&p[24]); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5325 | mng_info->global_chrm.blue_primary.y=0.00001* |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 5326 | mng_get_long(&p[28]); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5327 | mng_info->have_global_chrm=MagickTrue; |
| 5328 | } |
| 5329 | else |
| 5330 | mng_info->have_global_chrm=MagickFalse; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5331 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5332 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 5333 | continue; |
| 5334 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5335 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5336 | if (memcmp(type,mng_sRGB,4) == 0) |
| 5337 | { |
| 5338 | /* |
| 5339 | Read global sRGB. |
| 5340 | */ |
| 5341 | if (length) |
| 5342 | { |
glennrp | e610a07 | 2010-08-05 17:08:46 +0000 | [diff] [blame] | 5343 | mng_info->global_srgb_intent= |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 5344 | Magick_RenderingIntent_from_PNG_RenderingIntent(p[0]); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5345 | mng_info->have_global_srgb=MagickTrue; |
| 5346 | } |
| 5347 | else |
| 5348 | mng_info->have_global_srgb=MagickFalse; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5349 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5350 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 5351 | continue; |
| 5352 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5353 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5354 | if (memcmp(type,mng_iCCP,4) == 0) |
| 5355 | { |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 5356 | /* To do: */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5357 | |
| 5358 | /* |
| 5359 | Read global iCCP. |
| 5360 | */ |
| 5361 | if (length) |
| 5362 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5363 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5364 | continue; |
| 5365 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5366 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5367 | if (memcmp(type,mng_FRAM,4) == 0) |
| 5368 | { |
| 5369 | if (mng_type == 3) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 5370 | (void) ThrowMagickException(exception,GetMagickModule(), |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5371 | CoderError,"FRAM chunk found in MNG-VLC datastream","`%s'", |
| 5372 | image->filename); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5373 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5374 | if ((mng_info->framing_mode == 2) || (mng_info->framing_mode == 4)) |
| 5375 | image->delay=frame_delay; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5376 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5377 | frame_delay=default_frame_delay; |
| 5378 | frame_timeout=default_frame_timeout; |
| 5379 | fb=default_fb; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5380 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5381 | if (length) |
| 5382 | if (p[0]) |
| 5383 | mng_info->framing_mode=p[0]; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5384 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5385 | if (logging != MagickFalse) |
| 5386 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 5387 | " Framing_mode=%d",mng_info->framing_mode); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5388 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5389 | if (length > 6) |
| 5390 | { |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5391 | /* Note the delay and frame clipping boundaries. */ |
| 5392 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5393 | p++; /* framing mode */ |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5394 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 5395 | while (*p && ((p-chunk) < (ssize_t) length)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5396 | p++; /* frame name */ |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5397 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5398 | p++; /* frame name terminator */ |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5399 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 5400 | if ((p-chunk) < (ssize_t) (length-4)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5401 | { |
| 5402 | int |
| 5403 | change_delay, |
| 5404 | change_timeout, |
| 5405 | change_clipping; |
| 5406 | |
| 5407 | change_delay=(*p++); |
| 5408 | change_timeout=(*p++); |
| 5409 | change_clipping=(*p++); |
| 5410 | p++; /* change_sync */ |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5411 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5412 | if (change_delay) |
| 5413 | { |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 5414 | frame_delay=1UL*image->ticks_per_second* |
| 5415 | mng_get_long(p); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5416 | |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 5417 | if (mng_info->ticks_per_second != 0) |
| 5418 | frame_delay/=mng_info->ticks_per_second; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5419 | |
glennrp | bb010dd | 2010-06-01 13:07:15 +0000 | [diff] [blame] | 5420 | else |
| 5421 | frame_delay=PNG_UINT_31_MAX; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5422 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5423 | if (change_delay == 2) |
| 5424 | default_frame_delay=frame_delay; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5425 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5426 | p+=4; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5427 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5428 | if (logging != MagickFalse) |
| 5429 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 5430 | " Framing_delay=%.20g",(double) frame_delay); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5431 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5432 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5433 | if (change_timeout) |
| 5434 | { |
glennrp | bb010dd | 2010-06-01 13:07:15 +0000 | [diff] [blame] | 5435 | frame_timeout=1UL*image->ticks_per_second* |
| 5436 | mng_get_long(p); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5437 | |
glennrp | bb010dd | 2010-06-01 13:07:15 +0000 | [diff] [blame] | 5438 | if (mng_info->ticks_per_second != 0) |
| 5439 | frame_timeout/=mng_info->ticks_per_second; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5440 | |
glennrp | bb010dd | 2010-06-01 13:07:15 +0000 | [diff] [blame] | 5441 | else |
| 5442 | frame_timeout=PNG_UINT_31_MAX; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5443 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5444 | if (change_delay == 2) |
| 5445 | default_frame_timeout=frame_timeout; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5446 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5447 | p+=4; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5448 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5449 | if (logging != MagickFalse) |
| 5450 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 5451 | " Framing_timeout=%.20g",(double) frame_timeout); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5452 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5453 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5454 | if (change_clipping) |
| 5455 | { |
| 5456 | fb=mng_read_box(previous_fb,(char) p[0],&p[1]); |
| 5457 | p+=17; |
| 5458 | previous_fb=fb; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5459 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5460 | if (logging != MagickFalse) |
| 5461 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5462 | " Frame_clip: L=%.20g R=%.20g T=%.20g B=%.20g", |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 5463 | (double) fb.left,(double) fb.right,(double) fb.top, |
| 5464 | (double) fb.bottom); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5465 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5466 | if (change_clipping == 2) |
| 5467 | default_fb=fb; |
| 5468 | } |
| 5469 | } |
| 5470 | } |
| 5471 | mng_info->clip=fb; |
| 5472 | mng_info->clip=mng_minimum_box(fb,mng_info->frame); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5473 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 5474 | subframe_width=(size_t) (mng_info->clip.right |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5475 | -mng_info->clip.left); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5476 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 5477 | subframe_height=(size_t) (mng_info->clip.bottom |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5478 | -mng_info->clip.top); |
| 5479 | /* |
| 5480 | Insert a background layer behind the frame if framing_mode is 4. |
| 5481 | */ |
| 5482 | #if defined(MNG_INSERT_LAYERS) |
| 5483 | if (logging != MagickFalse) |
| 5484 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 5485 | " subframe_width=%.20g, subframe_height=%.20g",(double) |
| 5486 | subframe_width,(double) subframe_height); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5487 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5488 | if (insert_layers && (mng_info->framing_mode == 4) && |
| 5489 | (subframe_width) && (subframe_height)) |
| 5490 | { |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5491 | /* Allocate next image structure. */ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 5492 | if (GetAuthenticPixelQueue(image) != (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5493 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 5494 | AcquireNextImage(image_info,image,exception); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5495 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5496 | if (GetNextImageInList(image) == (Image *) NULL) |
| 5497 | { |
| 5498 | image=DestroyImageList(image); |
| 5499 | MngInfoFreeStruct(mng_info,&have_mng_structure); |
| 5500 | return((Image *) NULL); |
| 5501 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5502 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5503 | image=SyncNextImageInList(image); |
| 5504 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5505 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5506 | mng_info->image=image; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5507 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5508 | if (term_chunk_found) |
| 5509 | { |
| 5510 | image->start_loop=MagickTrue; |
| 5511 | image->iterations=mng_iterations; |
| 5512 | term_chunk_found=MagickFalse; |
| 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 | else |
| 5516 | image->start_loop=MagickFalse; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5517 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5518 | image->columns=subframe_width; |
| 5519 | image->rows=subframe_height; |
| 5520 | image->page.width=subframe_width; |
| 5521 | image->page.height=subframe_height; |
| 5522 | image->page.x=mng_info->clip.left; |
| 5523 | image->page.y=mng_info->clip.top; |
| 5524 | image->background_color=mng_background_color; |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 5525 | image->alpha_trait=UndefinedPixelTrait; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5526 | image->delay=0; |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 5527 | (void) SetImageBackgroundColor(image,exception); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5528 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5529 | if (logging != MagickFalse) |
| 5530 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5531 | " Insert backgd layer, L=%.20g, R=%.20g T=%.20g, B=%.20g", |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 5532 | (double) mng_info->clip.left,(double) mng_info->clip.right, |
| 5533 | (double) mng_info->clip.top,(double) mng_info->clip.bottom); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5534 | } |
| 5535 | #endif |
| 5536 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 5537 | continue; |
| 5538 | } |
| 5539 | if (memcmp(type,mng_CLIP,4) == 0) |
| 5540 | { |
| 5541 | unsigned int |
| 5542 | first_object, |
| 5543 | last_object; |
| 5544 | |
| 5545 | /* |
| 5546 | Read CLIP. |
| 5547 | */ |
| 5548 | first_object=(p[0] << 8) | p[1]; |
| 5549 | last_object=(p[2] << 8) | p[3]; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5550 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5551 | for (i=(int) first_object; i <= (int) last_object; i++) |
| 5552 | { |
| 5553 | if (mng_info->exists[i] && !mng_info->frozen[i]) |
| 5554 | { |
| 5555 | MngBox |
| 5556 | box; |
| 5557 | |
| 5558 | box=mng_info->object_clip[i]; |
| 5559 | mng_info->object_clip[i]=mng_read_box(box,(char) p[4],&p[5]); |
| 5560 | } |
| 5561 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5562 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5563 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 5564 | continue; |
| 5565 | } |
| 5566 | if (memcmp(type,mng_SAVE,4) == 0) |
| 5567 | { |
| 5568 | for (i=1; i < MNG_MAX_OBJECTS; i++) |
| 5569 | if (mng_info->exists[i]) |
| 5570 | { |
| 5571 | mng_info->frozen[i]=MagickTrue; |
| 5572 | #ifdef MNG_OBJECT_BUFFERS |
| 5573 | if (mng_info->ob[i] != (MngBuffer *) NULL) |
| 5574 | mng_info->ob[i]->frozen=MagickTrue; |
| 5575 | #endif |
| 5576 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5577 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5578 | if (length) |
| 5579 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5580 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5581 | continue; |
| 5582 | } |
| 5583 | |
| 5584 | if ((memcmp(type,mng_DISC,4) == 0) || (memcmp(type,mng_SEEK,4) == 0)) |
| 5585 | { |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5586 | /* Read DISC or SEEK. */ |
| 5587 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5588 | if ((length == 0) || !memcmp(type,mng_SEEK,4)) |
| 5589 | { |
| 5590 | for (i=1; i < MNG_MAX_OBJECTS; i++) |
| 5591 | MngInfoDiscardObject(mng_info,i); |
| 5592 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5593 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5594 | else |
| 5595 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 5596 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5597 | j; |
| 5598 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 5599 | for (j=0; j < (ssize_t) length; j+=2) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5600 | { |
| 5601 | i=p[j] << 8 | p[j+1]; |
| 5602 | MngInfoDiscardObject(mng_info,i); |
| 5603 | } |
| 5604 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5605 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5606 | if (length) |
| 5607 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5608 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5609 | continue; |
| 5610 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5611 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5612 | if (memcmp(type,mng_MOVE,4) == 0) |
| 5613 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 5614 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5615 | first_object, |
| 5616 | last_object; |
| 5617 | |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5618 | /* read MOVE */ |
| 5619 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5620 | first_object=(p[0] << 8) | p[1]; |
| 5621 | last_object=(p[2] << 8) | p[3]; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 5622 | for (i=(ssize_t) first_object; i <= (ssize_t) last_object; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5623 | { |
| 5624 | if (mng_info->exists[i] && !mng_info->frozen[i]) |
| 5625 | { |
| 5626 | MngPair |
| 5627 | new_pair; |
| 5628 | |
| 5629 | MngPair |
| 5630 | old_pair; |
| 5631 | |
| 5632 | old_pair.a=mng_info->x_off[i]; |
| 5633 | old_pair.b=mng_info->y_off[i]; |
| 5634 | new_pair=mng_read_pair(old_pair,(int) p[4],&p[5]); |
| 5635 | mng_info->x_off[i]=new_pair.a; |
| 5636 | mng_info->y_off[i]=new_pair.b; |
| 5637 | } |
| 5638 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5639 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5640 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 5641 | continue; |
| 5642 | } |
| 5643 | |
| 5644 | if (memcmp(type,mng_LOOP,4) == 0) |
| 5645 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 5646 | ssize_t loop_iters=1; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5647 | loop_level=chunk[0]; |
| 5648 | mng_info->loop_active[loop_level]=1; /* mark loop active */ |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5649 | |
| 5650 | /* Record starting point. */ |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 5651 | loop_iters=mng_get_long(&chunk[1]); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5652 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5653 | if (logging != MagickFalse) |
| 5654 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 5655 | " LOOP level %.20g has %.20g iterations ",(double) loop_level, |
| 5656 | (double) loop_iters); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5657 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5658 | if (loop_iters == 0) |
| 5659 | skipping_loop=loop_level; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5660 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5661 | else |
| 5662 | { |
| 5663 | mng_info->loop_jump[loop_level]=TellBlob(image); |
| 5664 | mng_info->loop_count[loop_level]=loop_iters; |
| 5665 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5666 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5667 | mng_info->loop_iteration[loop_level]=0; |
| 5668 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 5669 | continue; |
| 5670 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5671 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5672 | if (memcmp(type,mng_ENDL,4) == 0) |
| 5673 | { |
| 5674 | loop_level=chunk[0]; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5675 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5676 | if (skipping_loop > 0) |
| 5677 | { |
| 5678 | if (skipping_loop == loop_level) |
| 5679 | { |
| 5680 | /* |
| 5681 | Found end of zero-iteration loop. |
| 5682 | */ |
| 5683 | skipping_loop=(-1); |
| 5684 | mng_info->loop_active[loop_level]=0; |
| 5685 | } |
| 5686 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5687 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5688 | else |
| 5689 | { |
| 5690 | if (mng_info->loop_active[loop_level] == 1) |
| 5691 | { |
| 5692 | mng_info->loop_count[loop_level]--; |
| 5693 | mng_info->loop_iteration[loop_level]++; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5694 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5695 | if (logging != MagickFalse) |
| 5696 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5697 | " ENDL: LOOP level %.20g has %.20g remaining iters ", |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 5698 | (double) loop_level,(double) |
cristy | f2faecf | 2010-05-28 19:19:36 +0000 | [diff] [blame] | 5699 | mng_info->loop_count[loop_level]); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5700 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5701 | if (mng_info->loop_count[loop_level] != 0) |
| 5702 | { |
| 5703 | offset=SeekBlob(image,mng_info->loop_jump[loop_level], |
| 5704 | SEEK_SET); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5705 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5706 | if (offset < 0) |
| 5707 | ThrowReaderException(CorruptImageError, |
| 5708 | "ImproperImageHeader"); |
| 5709 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5710 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5711 | else |
| 5712 | { |
| 5713 | short |
| 5714 | last_level; |
| 5715 | |
| 5716 | /* |
| 5717 | Finished loop. |
| 5718 | */ |
| 5719 | mng_info->loop_active[loop_level]=0; |
| 5720 | last_level=(-1); |
| 5721 | for (i=0; i < loop_level; i++) |
| 5722 | if (mng_info->loop_active[i] == 1) |
| 5723 | last_level=(short) i; |
| 5724 | loop_level=last_level; |
| 5725 | } |
| 5726 | } |
| 5727 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5728 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5729 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 5730 | continue; |
| 5731 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5732 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5733 | if (memcmp(type,mng_CLON,4) == 0) |
| 5734 | { |
| 5735 | if (mng_info->clon_warning == 0) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 5736 | (void) ThrowMagickException(exception,GetMagickModule(), |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5737 | CoderError,"CLON is not implemented yet","`%s'", |
| 5738 | image->filename); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5739 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5740 | mng_info->clon_warning++; |
| 5741 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5742 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5743 | if (memcmp(type,mng_MAGN,4) == 0) |
| 5744 | { |
| 5745 | png_uint_16 |
| 5746 | magn_first, |
| 5747 | magn_last, |
| 5748 | magn_mb, |
| 5749 | magn_ml, |
| 5750 | magn_mr, |
| 5751 | magn_mt, |
| 5752 | magn_mx, |
| 5753 | magn_my, |
| 5754 | magn_methx, |
| 5755 | magn_methy; |
| 5756 | |
| 5757 | if (length > 1) |
| 5758 | magn_first=(p[0] << 8) | p[1]; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5759 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5760 | else |
| 5761 | magn_first=0; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5762 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5763 | if (length > 3) |
| 5764 | magn_last=(p[2] << 8) | p[3]; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5765 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5766 | else |
| 5767 | magn_last=magn_first; |
| 5768 | #ifndef MNG_OBJECT_BUFFERS |
| 5769 | if (magn_first || magn_last) |
| 5770 | if (mng_info->magn_warning == 0) |
| 5771 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 5772 | (void) ThrowMagickException(exception, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5773 | GetMagickModule(),CoderError, |
| 5774 | "MAGN is not implemented yet for nonzero objects", |
| 5775 | "`%s'",image->filename); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5776 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5777 | mng_info->magn_warning++; |
| 5778 | } |
| 5779 | #endif |
| 5780 | if (length > 4) |
| 5781 | magn_methx=p[4]; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5782 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5783 | else |
| 5784 | magn_methx=0; |
| 5785 | |
| 5786 | if (length > 6) |
| 5787 | magn_mx=(p[5] << 8) | p[6]; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5788 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5789 | else |
| 5790 | magn_mx=1; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5791 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5792 | if (magn_mx == 0) |
| 5793 | magn_mx=1; |
| 5794 | |
| 5795 | if (length > 8) |
| 5796 | magn_my=(p[7] << 8) | p[8]; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5797 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5798 | else |
| 5799 | magn_my=magn_mx; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5800 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5801 | if (magn_my == 0) |
| 5802 | magn_my=1; |
| 5803 | |
| 5804 | if (length > 10) |
| 5805 | magn_ml=(p[9] << 8) | p[10]; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5806 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5807 | else |
| 5808 | magn_ml=magn_mx; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5809 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5810 | if (magn_ml == 0) |
| 5811 | magn_ml=1; |
| 5812 | |
| 5813 | if (length > 12) |
| 5814 | magn_mr=(p[11] << 8) | p[12]; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5815 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5816 | else |
| 5817 | magn_mr=magn_mx; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5818 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5819 | if (magn_mr == 0) |
| 5820 | magn_mr=1; |
| 5821 | |
| 5822 | if (length > 14) |
| 5823 | magn_mt=(p[13] << 8) | p[14]; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5824 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5825 | else |
| 5826 | magn_mt=magn_my; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5827 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5828 | if (magn_mt == 0) |
| 5829 | magn_mt=1; |
| 5830 | |
| 5831 | if (length > 16) |
| 5832 | magn_mb=(p[15] << 8) | p[16]; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5833 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5834 | else |
| 5835 | magn_mb=magn_my; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5836 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5837 | if (magn_mb == 0) |
| 5838 | magn_mb=1; |
| 5839 | |
| 5840 | if (length > 17) |
| 5841 | magn_methy=p[17]; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5842 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5843 | else |
| 5844 | magn_methy=magn_methx; |
| 5845 | |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5846 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5847 | if (magn_methx > 5 || magn_methy > 5) |
| 5848 | if (mng_info->magn_warning == 0) |
| 5849 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 5850 | (void) ThrowMagickException(exception, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5851 | GetMagickModule(),CoderError, |
| 5852 | "Unknown MAGN method in MNG datastream","`%s'", |
| 5853 | image->filename); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5854 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5855 | mng_info->magn_warning++; |
| 5856 | } |
| 5857 | #ifdef MNG_OBJECT_BUFFERS |
| 5858 | /* Magnify existing objects in the range magn_first to magn_last */ |
| 5859 | #endif |
| 5860 | if (magn_first == 0 || magn_last == 0) |
| 5861 | { |
| 5862 | /* Save the magnification factors for object 0 */ |
| 5863 | mng_info->magn_mb=magn_mb; |
| 5864 | mng_info->magn_ml=magn_ml; |
| 5865 | mng_info->magn_mr=magn_mr; |
| 5866 | mng_info->magn_mt=magn_mt; |
| 5867 | mng_info->magn_mx=magn_mx; |
| 5868 | mng_info->magn_my=magn_my; |
| 5869 | mng_info->magn_methx=magn_methx; |
| 5870 | mng_info->magn_methy=magn_methy; |
| 5871 | } |
| 5872 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5873 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5874 | if (memcmp(type,mng_PAST,4) == 0) |
| 5875 | { |
| 5876 | if (mng_info->past_warning == 0) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 5877 | (void) ThrowMagickException(exception,GetMagickModule(), |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5878 | CoderError,"PAST is not implemented yet","`%s'", |
| 5879 | image->filename); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5880 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5881 | mng_info->past_warning++; |
| 5882 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5883 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5884 | if (memcmp(type,mng_SHOW,4) == 0) |
| 5885 | { |
| 5886 | if (mng_info->show_warning == 0) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 5887 | (void) ThrowMagickException(exception,GetMagickModule(), |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5888 | CoderError,"SHOW is not implemented yet","`%s'", |
| 5889 | image->filename); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5890 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5891 | mng_info->show_warning++; |
| 5892 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5893 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5894 | if (memcmp(type,mng_sBIT,4) == 0) |
| 5895 | { |
| 5896 | if (length < 4) |
| 5897 | mng_info->have_global_sbit=MagickFalse; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5898 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5899 | else |
| 5900 | { |
| 5901 | mng_info->global_sbit.gray=p[0]; |
| 5902 | mng_info->global_sbit.red=p[0]; |
| 5903 | mng_info->global_sbit.green=p[1]; |
| 5904 | mng_info->global_sbit.blue=p[2]; |
| 5905 | mng_info->global_sbit.alpha=p[3]; |
| 5906 | mng_info->have_global_sbit=MagickTrue; |
| 5907 | } |
| 5908 | } |
| 5909 | if (memcmp(type,mng_pHYs,4) == 0) |
| 5910 | { |
| 5911 | if (length > 8) |
| 5912 | { |
| 5913 | mng_info->global_x_pixels_per_unit= |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 5914 | (size_t) mng_get_long(p); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5915 | mng_info->global_y_pixels_per_unit= |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 5916 | (size_t) mng_get_long(&p[4]); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5917 | mng_info->global_phys_unit_type=p[8]; |
| 5918 | mng_info->have_global_phys=MagickTrue; |
| 5919 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5920 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5921 | else |
| 5922 | mng_info->have_global_phys=MagickFalse; |
| 5923 | } |
| 5924 | if (memcmp(type,mng_pHYg,4) == 0) |
| 5925 | { |
| 5926 | if (mng_info->phyg_warning == 0) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 5927 | (void) ThrowMagickException(exception,GetMagickModule(), |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5928 | CoderError,"pHYg is not implemented.","`%s'",image->filename); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5929 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5930 | mng_info->phyg_warning++; |
| 5931 | } |
| 5932 | if (memcmp(type,mng_BASI,4) == 0) |
| 5933 | { |
| 5934 | skip_to_iend=MagickTrue; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5935 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5936 | if (mng_info->basi_warning == 0) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 5937 | (void) ThrowMagickException(exception,GetMagickModule(), |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5938 | CoderError,"BASI is not implemented yet","`%s'", |
| 5939 | image->filename); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5940 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5941 | mng_info->basi_warning++; |
| 5942 | #ifdef MNG_BASI_SUPPORTED |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 5943 | basi_width=(size_t) ((p[0] << 24) | (p[1] << 16) | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5944 | (p[2] << 8) | p[3]); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 5945 | basi_height=(size_t) ((p[4] << 24) | (p[5] << 16) | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5946 | (p[6] << 8) | p[7]); |
| 5947 | basi_color_type=p[8]; |
| 5948 | basi_compression_method=p[9]; |
| 5949 | basi_filter_type=p[10]; |
| 5950 | basi_interlace_method=p[11]; |
| 5951 | if (length > 11) |
| 5952 | basi_red=(p[12] << 8) & p[13]; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5953 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5954 | else |
| 5955 | basi_red=0; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5956 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5957 | if (length > 13) |
| 5958 | basi_green=(p[14] << 8) & p[15]; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5959 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5960 | else |
| 5961 | basi_green=0; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5962 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5963 | if (length > 15) |
| 5964 | basi_blue=(p[16] << 8) & p[17]; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5965 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5966 | else |
| 5967 | basi_blue=0; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5968 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5969 | if (length > 17) |
| 5970 | basi_alpha=(p[18] << 8) & p[19]; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5971 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5972 | else |
| 5973 | { |
| 5974 | if (basi_sample_depth == 16) |
| 5975 | basi_alpha=65535L; |
| 5976 | else |
| 5977 | basi_alpha=255; |
| 5978 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5979 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5980 | if (length > 19) |
| 5981 | basi_viewable=p[20]; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5982 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5983 | else |
| 5984 | basi_viewable=0; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5985 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5986 | #endif |
| 5987 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 5988 | continue; |
| 5989 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5990 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5991 | if (memcmp(type,mng_IHDR,4) |
| 5992 | #if defined(JNG_SUPPORTED) |
| 5993 | && memcmp(type,mng_JHDR,4) |
| 5994 | #endif |
| 5995 | ) |
| 5996 | { |
| 5997 | /* Not an IHDR or JHDR chunk */ |
| 5998 | if (length) |
| 5999 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6000 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6001 | continue; |
| 6002 | } |
| 6003 | /* Process IHDR */ |
| 6004 | if (logging != MagickFalse) |
| 6005 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 6006 | " 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] | 6007 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6008 | mng_info->exists[object_id]=MagickTrue; |
| 6009 | mng_info->viewable[object_id]=MagickTrue; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6010 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6011 | if (mng_info->invisible[object_id]) |
| 6012 | { |
| 6013 | if (logging != MagickFalse) |
| 6014 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 6015 | " Skipping invisible object"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6016 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6017 | skip_to_iend=MagickTrue; |
| 6018 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 6019 | continue; |
| 6020 | } |
| 6021 | #if defined(MNG_INSERT_LAYERS) |
| 6022 | if (length < 8) |
| 6023 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6024 | |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 6025 | image_width=(size_t) mng_get_long(p); |
| 6026 | image_height=(size_t) mng_get_long(&p[4]); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6027 | #endif |
| 6028 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 6029 | |
| 6030 | /* |
| 6031 | Insert a transparent background layer behind the entire animation |
| 6032 | if it is not full screen. |
| 6033 | */ |
| 6034 | #if defined(MNG_INSERT_LAYERS) |
| 6035 | if (insert_layers && mng_type && first_mng_object) |
| 6036 | { |
| 6037 | if ((mng_info->clip.left > 0) || (mng_info->clip.top > 0) || |
| 6038 | (image_width < mng_info->mng_width) || |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 6039 | (mng_info->clip.right < (ssize_t) mng_info->mng_width) || |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6040 | (image_height < mng_info->mng_height) || |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 6041 | (mng_info->clip.bottom < (ssize_t) mng_info->mng_height)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6042 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6043 | if (GetAuthenticPixelQueue(image) != (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6044 | { |
| 6045 | /* |
| 6046 | Allocate next image structure. |
| 6047 | */ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6048 | AcquireNextImage(image_info,image,exception); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6049 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6050 | if (GetNextImageInList(image) == (Image *) NULL) |
| 6051 | { |
| 6052 | image=DestroyImageList(image); |
| 6053 | MngInfoFreeStruct(mng_info,&have_mng_structure); |
| 6054 | return((Image *) NULL); |
| 6055 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6056 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6057 | image=SyncNextImageInList(image); |
| 6058 | } |
| 6059 | mng_info->image=image; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6060 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6061 | if (term_chunk_found) |
| 6062 | { |
| 6063 | image->start_loop=MagickTrue; |
| 6064 | image->iterations=mng_iterations; |
| 6065 | term_chunk_found=MagickFalse; |
| 6066 | } |
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 | image->start_loop=MagickFalse; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6070 | |
| 6071 | /* Make a background rectangle. */ |
| 6072 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6073 | image->delay=0; |
| 6074 | image->columns=mng_info->mng_width; |
| 6075 | image->rows=mng_info->mng_height; |
| 6076 | image->page.width=mng_info->mng_width; |
| 6077 | image->page.height=mng_info->mng_height; |
| 6078 | image->page.x=0; |
| 6079 | image->page.y=0; |
| 6080 | image->background_color=mng_background_color; |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6081 | (void) SetImageBackgroundColor(image,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6082 | if (logging != MagickFalse) |
| 6083 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 6084 | " Inserted transparent background layer, W=%.20g, H=%.20g", |
| 6085 | (double) mng_info->mng_width,(double) mng_info->mng_height); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6086 | } |
| 6087 | } |
| 6088 | /* |
| 6089 | Insert a background layer behind the upcoming image if |
| 6090 | framing_mode is 3, and we haven't already inserted one. |
| 6091 | */ |
| 6092 | if (insert_layers && (mng_info->framing_mode == 3) && |
| 6093 | (subframe_width) && (subframe_height) && (simplicity == 0 || |
| 6094 | (simplicity & 0x08))) |
| 6095 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6096 | if (GetAuthenticPixelQueue(image) != (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6097 | { |
| 6098 | /* |
| 6099 | Allocate next image structure. |
| 6100 | */ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6101 | AcquireNextImage(image_info,image,exception); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6102 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6103 | if (GetNextImageInList(image) == (Image *) NULL) |
| 6104 | { |
| 6105 | image=DestroyImageList(image); |
| 6106 | MngInfoFreeStruct(mng_info,&have_mng_structure); |
| 6107 | return((Image *) NULL); |
| 6108 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6109 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6110 | image=SyncNextImageInList(image); |
| 6111 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 6112 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6113 | mng_info->image=image; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 6114 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6115 | if (term_chunk_found) |
| 6116 | { |
| 6117 | image->start_loop=MagickTrue; |
| 6118 | image->iterations=mng_iterations; |
| 6119 | term_chunk_found=MagickFalse; |
| 6120 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 6121 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6122 | else |
| 6123 | image->start_loop=MagickFalse; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6124 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6125 | image->delay=0; |
| 6126 | image->columns=subframe_width; |
| 6127 | image->rows=subframe_height; |
| 6128 | image->page.width=subframe_width; |
| 6129 | image->page.height=subframe_height; |
| 6130 | image->page.x=mng_info->clip.left; |
| 6131 | image->page.y=mng_info->clip.top; |
| 6132 | image->background_color=mng_background_color; |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 6133 | image->alpha_trait=UndefinedPixelTrait; |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6134 | (void) SetImageBackgroundColor(image,exception); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 6135 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6136 | if (logging != MagickFalse) |
| 6137 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 6138 | " Insert background layer, L=%.20g, R=%.20g T=%.20g, B=%.20g", |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 6139 | (double) mng_info->clip.left,(double) mng_info->clip.right, |
| 6140 | (double) mng_info->clip.top,(double) mng_info->clip.bottom); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6141 | } |
| 6142 | #endif /* MNG_INSERT_LAYERS */ |
| 6143 | first_mng_object=MagickFalse; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6144 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6145 | if (GetAuthenticPixelQueue(image) != (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6146 | { |
| 6147 | /* |
| 6148 | Allocate next image structure. |
| 6149 | */ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6150 | AcquireNextImage(image_info,image,exception); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6151 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6152 | if (GetNextImageInList(image) == (Image *) NULL) |
| 6153 | { |
| 6154 | image=DestroyImageList(image); |
| 6155 | MngInfoFreeStruct(mng_info,&have_mng_structure); |
| 6156 | return((Image *) NULL); |
| 6157 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6158 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6159 | image=SyncNextImageInList(image); |
| 6160 | } |
| 6161 | mng_info->image=image; |
| 6162 | status=SetImageProgress(image,LoadImagesTag,TellBlob(image), |
| 6163 | GetBlobSize(image)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 6164 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6165 | if (status == MagickFalse) |
| 6166 | break; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 6167 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6168 | if (term_chunk_found) |
| 6169 | { |
| 6170 | image->start_loop=MagickTrue; |
| 6171 | term_chunk_found=MagickFalse; |
| 6172 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 6173 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6174 | else |
| 6175 | image->start_loop=MagickFalse; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 6176 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6177 | if (mng_info->framing_mode == 1 || mng_info->framing_mode == 3) |
| 6178 | { |
| 6179 | image->delay=frame_delay; |
| 6180 | frame_delay=default_frame_delay; |
| 6181 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 6182 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6183 | else |
| 6184 | image->delay=0; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 6185 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6186 | image->page.width=mng_info->mng_width; |
| 6187 | image->page.height=mng_info->mng_height; |
| 6188 | image->page.x=mng_info->x_off[object_id]; |
| 6189 | image->page.y=mng_info->y_off[object_id]; |
| 6190 | image->iterations=mng_iterations; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6191 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6192 | /* |
| 6193 | Seek back to the beginning of the IHDR or JHDR chunk's length field. |
| 6194 | */ |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6195 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6196 | if (logging != MagickFalse) |
| 6197 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 6198 | " Seeking back to beginning of %c%c%c%c chunk",type[0],type[1], |
| 6199 | type[2],type[3]); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6200 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 6201 | offset=SeekBlob(image,-((ssize_t) length+12),SEEK_CUR); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6202 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6203 | if (offset < 0) |
| 6204 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
| 6205 | } |
| 6206 | |
| 6207 | previous=image; |
| 6208 | mng_info->image=image; |
| 6209 | mng_info->mng_type=mng_type; |
| 6210 | mng_info->object_id=object_id; |
| 6211 | |
| 6212 | if (memcmp(type,mng_IHDR,4) == 0) |
| 6213 | image=ReadOnePNGImage(mng_info,image_info,exception); |
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 defined(JNG_SUPPORTED) |
| 6216 | else |
| 6217 | image=ReadOneJNGImage(mng_info,image_info,exception); |
| 6218 | #endif |
| 6219 | |
| 6220 | if (image == (Image *) NULL) |
| 6221 | { |
| 6222 | if (IsImageObject(previous) != MagickFalse) |
| 6223 | { |
| 6224 | (void) DestroyImageList(previous); |
| 6225 | (void) CloseBlob(previous); |
| 6226 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6227 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6228 | MngInfoFreeStruct(mng_info,&have_mng_structure); |
| 6229 | return((Image *) NULL); |
| 6230 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 6231 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6232 | if (image->columns == 0 || image->rows == 0) |
| 6233 | { |
| 6234 | (void) CloseBlob(image); |
| 6235 | image=DestroyImageList(image); |
| 6236 | MngInfoFreeStruct(mng_info,&have_mng_structure); |
| 6237 | return((Image *) NULL); |
| 6238 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 6239 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6240 | mng_info->image=image; |
| 6241 | |
| 6242 | if (mng_type) |
| 6243 | { |
| 6244 | MngBox |
| 6245 | crop_box; |
| 6246 | |
| 6247 | if (mng_info->magn_methx || mng_info->magn_methy) |
| 6248 | { |
| 6249 | png_uint_32 |
| 6250 | magnified_height, |
| 6251 | magnified_width; |
| 6252 | |
| 6253 | if (logging != MagickFalse) |
| 6254 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 6255 | " Processing MNG MAGN chunk"); |
| 6256 | |
| 6257 | if (mng_info->magn_methx == 1) |
| 6258 | { |
| 6259 | magnified_width=mng_info->magn_ml; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6260 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6261 | if (image->columns > 1) |
| 6262 | magnified_width += mng_info->magn_mr; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6263 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6264 | if (image->columns > 2) |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6265 | magnified_width += (png_uint_32) |
| 6266 | ((image->columns-2)*(mng_info->magn_mx)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6267 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6268 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6269 | else |
| 6270 | { |
cristy | 4e5bc84 | 2010-06-09 13:56:01 +0000 | [diff] [blame] | 6271 | magnified_width=(png_uint_32) image->columns; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6272 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6273 | if (image->columns > 1) |
| 6274 | magnified_width += mng_info->magn_ml-1; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6275 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6276 | if (image->columns > 2) |
| 6277 | magnified_width += mng_info->magn_mr-1; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6278 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6279 | if (image->columns > 3) |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6280 | magnified_width += (png_uint_32) |
| 6281 | ((image->columns-3)*(mng_info->magn_mx-1)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6282 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6283 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6284 | if (mng_info->magn_methy == 1) |
| 6285 | { |
| 6286 | magnified_height=mng_info->magn_mt; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6287 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6288 | if (image->rows > 1) |
| 6289 | magnified_height += mng_info->magn_mb; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6290 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6291 | if (image->rows > 2) |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6292 | magnified_height += (png_uint_32) |
| 6293 | ((image->rows-2)*(mng_info->magn_my)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6294 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6295 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6296 | else |
| 6297 | { |
cristy | 4e5bc84 | 2010-06-09 13:56:01 +0000 | [diff] [blame] | 6298 | magnified_height=(png_uint_32) image->rows; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6299 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6300 | if (image->rows > 1) |
| 6301 | magnified_height += mng_info->magn_mt-1; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6302 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6303 | if (image->rows > 2) |
| 6304 | magnified_height += mng_info->magn_mb-1; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6305 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6306 | if (image->rows > 3) |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6307 | magnified_height += (png_uint_32) |
| 6308 | ((image->rows-3)*(mng_info->magn_my-1)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6309 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6310 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6311 | if (magnified_height > image->rows || |
| 6312 | magnified_width > image->columns) |
| 6313 | { |
| 6314 | Image |
| 6315 | *large_image; |
| 6316 | |
| 6317 | int |
| 6318 | yy; |
| 6319 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6320 | Quantum |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6321 | *next, |
| 6322 | *prev; |
| 6323 | |
| 6324 | png_uint_16 |
| 6325 | magn_methx, |
| 6326 | magn_methy; |
| 6327 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6328 | ssize_t |
| 6329 | m, |
| 6330 | y; |
| 6331 | |
| 6332 | register Quantum |
| 6333 | *n, |
| 6334 | *q; |
| 6335 | |
| 6336 | register ssize_t |
| 6337 | x; |
| 6338 | |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6339 | /* Allocate next image structure. */ |
| 6340 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6341 | if (logging != MagickFalse) |
| 6342 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 6343 | " Allocate magnified image"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6344 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6345 | AcquireNextImage(image_info,image,exception); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6346 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6347 | if (GetNextImageInList(image) == (Image *) NULL) |
| 6348 | { |
| 6349 | image=DestroyImageList(image); |
| 6350 | MngInfoFreeStruct(mng_info,&have_mng_structure); |
| 6351 | return((Image *) NULL); |
| 6352 | } |
| 6353 | |
| 6354 | large_image=SyncNextImageInList(image); |
| 6355 | |
| 6356 | large_image->columns=magnified_width; |
| 6357 | large_image->rows=magnified_height; |
| 6358 | |
| 6359 | magn_methx=mng_info->magn_methx; |
| 6360 | magn_methy=mng_info->magn_methy; |
| 6361 | |
glennrp | 3faa9a3 | 2011-04-23 14:00:25 +0000 | [diff] [blame] | 6362 | #if (MAGICKCORE_QUANTUM_DEPTH > 16) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6363 | #define QM unsigned short |
| 6364 | if (magn_methx != 1 || magn_methy != 1) |
| 6365 | { |
| 6366 | /* |
| 6367 | Scale pixels to unsigned shorts to prevent |
| 6368 | overflow of intermediate values of interpolations |
| 6369 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 6370 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6371 | { |
| 6372 | q=GetAuthenticPixels(image,0,y,image->columns,1, |
| 6373 | exception); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6374 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 6375 | for (x=(ssize_t) image->columns-1; x >= 0; x--) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6376 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6377 | SetPixelRed(image,ScaleQuantumToShort( |
| 6378 | GetPixelRed(image,q)),q); |
| 6379 | SetPixelGreen(image,ScaleQuantumToShort( |
| 6380 | GetPixelGreen(image,q)),q); |
| 6381 | SetPixelBlue(image,ScaleQuantumToShort( |
| 6382 | GetPixelBlue(image,q)),q); |
| 6383 | SetPixelAlpha(image,ScaleQuantumToShort( |
| 6384 | GetPixelAlpha(image,q)),q); |
| 6385 | q+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6386 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6387 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6388 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 6389 | break; |
| 6390 | } |
| 6391 | } |
| 6392 | #else |
| 6393 | #define QM Quantum |
| 6394 | #endif |
| 6395 | |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 6396 | if (image->alpha_trait == BlendPixelTrait) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6397 | (void) SetImageBackgroundColor(large_image,exception); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6398 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6399 | else |
| 6400 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6401 | large_image->background_color.alpha=OpaqueAlpha; |
| 6402 | (void) SetImageBackgroundColor(large_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 (magn_methx == 4) |
| 6405 | magn_methx=2; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6406 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6407 | if (magn_methx == 5) |
| 6408 | magn_methx=3; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6409 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6410 | if (magn_methy == 4) |
| 6411 | magn_methy=2; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6412 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6413 | if (magn_methy == 5) |
| 6414 | magn_methy=3; |
| 6415 | } |
| 6416 | |
| 6417 | /* magnify the rows into the right side of the large image */ |
| 6418 | |
| 6419 | if (logging != MagickFalse) |
| 6420 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 6421 | " Magnify the rows to %.20g",(double) large_image->rows); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 6422 | m=(ssize_t) mng_info->magn_mt; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6423 | yy=0; |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6424 | length=(size_t) image->columns*GetPixelChannels(image); |
| 6425 | next=(Quantum *) AcquireQuantumMemory(length,sizeof(*next)); |
| 6426 | prev=(Quantum *) AcquireQuantumMemory(length,sizeof(*prev)); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6427 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6428 | if ((prev == (Quantum *) NULL) || |
| 6429 | (next == (Quantum *) NULL)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6430 | { |
| 6431 | image=DestroyImageList(image); |
| 6432 | MngInfoFreeStruct(mng_info,&have_mng_structure); |
| 6433 | ThrowReaderException(ResourceLimitError, |
| 6434 | "MemoryAllocationFailed"); |
| 6435 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6436 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6437 | n=GetAuthenticPixels(image,0,0,image->columns,1,exception); |
| 6438 | (void) CopyMagickMemory(next,n,length); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6439 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 6440 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6441 | { |
| 6442 | if (y == 0) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 6443 | m=(ssize_t) mng_info->magn_mt; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6444 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 6445 | else if (magn_methy > 1 && y == (ssize_t) image->rows-2) |
| 6446 | m=(ssize_t) mng_info->magn_mb; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6447 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 6448 | else if (magn_methy <= 1 && y == (ssize_t) image->rows-1) |
| 6449 | m=(ssize_t) mng_info->magn_mb; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6450 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 6451 | else if (magn_methy > 1 && y == (ssize_t) image->rows-1) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6452 | m=1; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6453 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6454 | else |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 6455 | m=(ssize_t) mng_info->magn_my; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6456 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6457 | n=prev; |
| 6458 | prev=next; |
| 6459 | next=n; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6460 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 6461 | if (y < (ssize_t) image->rows-1) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6462 | { |
| 6463 | n=GetAuthenticPixels(image,0,y+1,image->columns,1, |
| 6464 | exception); |
| 6465 | (void) CopyMagickMemory(next,n,length); |
| 6466 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6467 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6468 | for (i=0; i < m; i++, yy++) |
| 6469 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6470 | register Quantum |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6471 | *pixels; |
| 6472 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 6473 | assert(yy < (ssize_t) large_image->rows); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6474 | pixels=prev; |
| 6475 | n=next; |
| 6476 | q=GetAuthenticPixels(large_image,0,yy,large_image->columns, |
cristy | 9fff7b4 | 2011-04-29 01:09:31 +0000 | [diff] [blame] | 6477 | 1,exception); |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6478 | q+=(large_image->columns-image->columns)* |
| 6479 | GetPixelChannels(large_image); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6480 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 6481 | for (x=(ssize_t) image->columns-1; x >= 0; x--) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6482 | { |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 6483 | /* To do: get color as function of indexes[x] */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6484 | /* |
| 6485 | if (image->storage_class == PseudoClass) |
| 6486 | { |
| 6487 | } |
| 6488 | */ |
| 6489 | |
| 6490 | if (magn_methy <= 1) |
| 6491 | { |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 6492 | /* replicate previous */ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6493 | SetPixelRed(large_image,GetPixelRed(image,pixels),q); |
| 6494 | SetPixelGreen(large_image,GetPixelGreen(image, |
| 6495 | pixels),q); |
| 6496 | SetPixelBlue(large_image,GetPixelBlue(image, |
| 6497 | pixels),q); |
| 6498 | SetPixelAlpha(large_image,GetPixelAlpha(image, |
| 6499 | pixels),q); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6500 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6501 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6502 | else if (magn_methy == 2 || magn_methy == 4) |
| 6503 | { |
| 6504 | if (i == 0) |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 6505 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6506 | SetPixelRed(large_image,GetPixelRed(image, |
| 6507 | pixels),q); |
| 6508 | SetPixelGreen(large_image,GetPixelGreen(image, |
| 6509 | pixels),q); |
| 6510 | SetPixelBlue(large_image,GetPixelBlue(image, |
| 6511 | pixels),q); |
| 6512 | SetPixelAlpha(large_image,GetPixelAlpha(image, |
| 6513 | pixels),q); |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 6514 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6515 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6516 | else |
| 6517 | { |
| 6518 | /* Interpolate */ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6519 | SetPixelRed(large_image,((QM) (((ssize_t) |
| 6520 | (2*i*(GetPixelRed(image,n) |
| 6521 | -GetPixelRed(image,pixels)+m))/ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 6522 | ((ssize_t) (m*2)) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6523 | +GetPixelRed(image,pixels)))),q); |
| 6524 | SetPixelGreen(large_image,((QM) (((ssize_t) |
| 6525 | (2*i*(GetPixelGreen(image,n) |
| 6526 | -GetPixelGreen(image,pixels)+m))/ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 6527 | ((ssize_t) (m*2)) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6528 | +GetPixelGreen(image,pixels)))),q); |
| 6529 | SetPixelBlue(large_image,((QM) (((ssize_t) |
| 6530 | (2*i*(GetPixelBlue(image,n) |
| 6531 | -GetPixelBlue(image,pixels)+m))/ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 6532 | ((ssize_t) (m*2)) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6533 | +GetPixelBlue(image,pixels)))),q); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6534 | |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 6535 | if (image->alpha_trait == BlendPixelTrait) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6536 | SetPixelAlpha(large_image, ((QM) (((ssize_t) |
| 6537 | (2*i*(GetPixelAlpha(image,n) |
| 6538 | -GetPixelAlpha(image,pixels)+m)) |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 6539 | /((ssize_t) (m*2))+ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6540 | GetPixelAlpha(image,pixels)))),q); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6541 | } |
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 (magn_methy == 4) |
| 6544 | { |
| 6545 | /* Replicate nearest */ |
| 6546 | if (i <= ((m+1) << 1)) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6547 | SetPixelAlpha(large_image,GetPixelAlpha(image, |
| 6548 | pixels),q); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6549 | else |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6550 | SetPixelAlpha(large_image,GetPixelAlpha(image, |
| 6551 | n),q); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6552 | } |
| 6553 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6554 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6555 | else /* if (magn_methy == 3 || magn_methy == 5) */ |
| 6556 | { |
| 6557 | /* Replicate nearest */ |
| 6558 | if (i <= ((m+1) << 1)) |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 6559 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6560 | SetPixelRed(large_image,GetPixelRed(image, |
| 6561 | pixels),q); |
| 6562 | SetPixelGreen(large_image,GetPixelGreen(image, |
| 6563 | pixels),q); |
| 6564 | SetPixelBlue(large_image,GetPixelBlue(image, |
| 6565 | pixels),q); |
| 6566 | SetPixelAlpha(large_image,GetPixelAlpha(image, |
| 6567 | pixels),q); |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 6568 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6569 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6570 | else |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 6571 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6572 | SetPixelRed(large_image,GetPixelRed(image,n),q); |
| 6573 | SetPixelGreen(large_image,GetPixelGreen(image,n), |
| 6574 | q); |
| 6575 | SetPixelBlue(large_image,GetPixelBlue(image,n), |
| 6576 | q); |
| 6577 | SetPixelAlpha(large_image,GetPixelAlpha(image,n), |
| 6578 | q); |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 6579 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6580 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6581 | if (magn_methy == 5) |
| 6582 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6583 | SetPixelAlpha(large_image,(QM) (((ssize_t) (2*i* |
| 6584 | (GetPixelAlpha(image,n) |
| 6585 | -GetPixelAlpha(image,pixels)) |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 6586 | +m))/((ssize_t) (m*2)) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6587 | +GetPixelAlpha(image,pixels)),q); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6588 | } |
| 6589 | } |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6590 | n+=GetPixelChannels(image); |
| 6591 | q+=GetPixelChannels(large_image); |
| 6592 | pixels+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6593 | } /* x */ |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6594 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6595 | if (SyncAuthenticPixels(large_image,exception) == 0) |
| 6596 | break; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6597 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6598 | } /* i */ |
| 6599 | } /* y */ |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6600 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6601 | prev=(Quantum *) RelinquishMagickMemory(prev); |
| 6602 | next=(Quantum *) RelinquishMagickMemory(next); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6603 | |
| 6604 | length=image->columns; |
| 6605 | |
| 6606 | if (logging != MagickFalse) |
| 6607 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 6608 | " Delete original image"); |
| 6609 | |
| 6610 | DeleteImageFromList(&image); |
| 6611 | |
| 6612 | image=large_image; |
| 6613 | |
| 6614 | mng_info->image=image; |
| 6615 | |
| 6616 | /* magnify the columns */ |
| 6617 | if (logging != MagickFalse) |
| 6618 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 6619 | " Magnify the columns to %.20g",(double) image->columns); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6620 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 6621 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6622 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6623 | register Quantum |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6624 | *pixels; |
| 6625 | |
| 6626 | q=GetAuthenticPixels(image,0,y,image->columns,1,exception); |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6627 | pixels=q+(image->columns-length)*GetPixelChannels(image); |
| 6628 | n=pixels+GetPixelChannels(image); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6629 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 6630 | for (x=(ssize_t) (image->columns-length); |
| 6631 | x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6632 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6633 | /* To do: Rewrite using Get/Set***PixelChannel() */ |
glennrp | 7c7b315 | 2011-04-26 04:01:27 +0000 | [diff] [blame] | 6634 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 6635 | if (x == (ssize_t) (image->columns-length)) |
| 6636 | m=(ssize_t) mng_info->magn_ml; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6637 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 6638 | else if (magn_methx > 1 && x == (ssize_t) image->columns-2) |
| 6639 | m=(ssize_t) mng_info->magn_mr; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6640 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 6641 | else if (magn_methx <= 1 && x == (ssize_t) image->columns-1) |
| 6642 | m=(ssize_t) mng_info->magn_mr; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6643 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 6644 | else if (magn_methx > 1 && x == (ssize_t) image->columns-1) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6645 | m=1; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6646 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6647 | else |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 6648 | m=(ssize_t) mng_info->magn_mx; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6649 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6650 | for (i=0; i < m; i++) |
| 6651 | { |
| 6652 | if (magn_methx <= 1) |
| 6653 | { |
| 6654 | /* replicate previous */ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6655 | SetPixelRed(image,GetPixelRed(image,pixels),q); |
| 6656 | SetPixelGreen(image,GetPixelGreen(image,pixels),q); |
| 6657 | SetPixelBlue(image,GetPixelBlue(image,pixels),q); |
| 6658 | SetPixelAlpha(image,GetPixelAlpha(image,pixels),q); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6659 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6660 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6661 | else if (magn_methx == 2 || magn_methx == 4) |
| 6662 | { |
| 6663 | if (i == 0) |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 6664 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6665 | SetPixelRed(image,GetPixelRed(image,pixels),q); |
| 6666 | SetPixelGreen(image,GetPixelGreen(image,pixels),q); |
| 6667 | SetPixelBlue(image,GetPixelBlue(image,pixels),q); |
| 6668 | SetPixelAlpha(image,GetPixelAlpha(image,pixels),q); |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 6669 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6670 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6671 | /* To do: Rewrite using Get/Set***PixelChannel() */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6672 | else |
| 6673 | { |
| 6674 | /* Interpolate */ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6675 | SetPixelRed(image,(QM) ((2*i*( |
| 6676 | GetPixelRed(image,n) |
| 6677 | -GetPixelRed(image,pixels))+m) |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 6678 | /((ssize_t) (m*2))+ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6679 | GetPixelRed(image,pixels)),q); |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 6680 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6681 | SetPixelGreen(image,(QM) ((2*i*( |
| 6682 | GetPixelGreen(image,n) |
| 6683 | -GetPixelGreen(image,pixels))+m) |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 6684 | /((ssize_t) (m*2))+ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6685 | GetPixelGreen(image,pixels)),q); |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 6686 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6687 | SetPixelBlue(image,(QM) ((2*i*( |
| 6688 | GetPixelBlue(image,n) |
| 6689 | -GetPixelBlue(image,pixels))+m) |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 6690 | /((ssize_t) (m*2))+ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6691 | GetPixelBlue(image,pixels)),q); |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 6692 | if (image->alpha_trait == BlendPixelTrait) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6693 | SetPixelAlpha(image,(QM) ((2*i*( |
| 6694 | GetPixelAlpha(image,n) |
| 6695 | -GetPixelAlpha(image,pixels))+m) |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 6696 | /((ssize_t) (m*2))+ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6697 | GetPixelAlpha(image,pixels)),q); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6698 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6699 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6700 | if (magn_methx == 4) |
| 6701 | { |
| 6702 | /* Replicate nearest */ |
| 6703 | if (i <= ((m+1) << 1)) |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 6704 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6705 | SetPixelAlpha(image, |
| 6706 | GetPixelAlpha(image,pixels)+0,q); |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 6707 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6708 | else |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 6709 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6710 | SetPixelAlpha(image, |
| 6711 | GetPixelAlpha(image,n)+0,q); |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 6712 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6713 | } |
| 6714 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6715 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6716 | else /* if (magn_methx == 3 || magn_methx == 5) */ |
| 6717 | { |
| 6718 | /* Replicate nearest */ |
| 6719 | if (i <= ((m+1) << 1)) |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 6720 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6721 | SetPixelRed(image,GetPixelRed(image,pixels),q); |
| 6722 | SetPixelGreen(image,GetPixelGreen(image,pixels),q); |
| 6723 | SetPixelBlue(image,GetPixelBlue(image,pixels),q); |
| 6724 | SetPixelAlpha(image,GetPixelAlpha(image,pixels),q); |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 6725 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6726 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6727 | else |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 6728 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6729 | SetPixelRed(image,GetPixelRed(image,n),q); |
| 6730 | SetPixelGreen(image,GetPixelGreen(image,n),q); |
| 6731 | SetPixelBlue(image,GetPixelBlue(image,n),q); |
| 6732 | SetPixelAlpha(image,GetPixelAlpha(image,n),q); |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 6733 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6734 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6735 | if (magn_methx == 5) |
| 6736 | { |
| 6737 | /* Interpolate */ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6738 | SetPixelAlpha(image, |
| 6739 | (QM) ((2*i*( GetPixelAlpha(image,n) |
| 6740 | -GetPixelAlpha(image,pixels))+m)/ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 6741 | ((ssize_t) (m*2)) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6742 | +GetPixelAlpha(image,pixels)),q); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6743 | } |
| 6744 | } |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6745 | q+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6746 | } |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6747 | n+=GetPixelChannels(image); |
| 6748 | p+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6749 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6750 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6751 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 6752 | break; |
| 6753 | } |
glennrp | 3faa9a3 | 2011-04-23 14:00:25 +0000 | [diff] [blame] | 6754 | #if (MAGICKCORE_QUANTUM_DEPTH > 16) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6755 | if (magn_methx != 1 || magn_methy != 1) |
| 6756 | { |
| 6757 | /* |
| 6758 | Rescale pixels to Quantum |
| 6759 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 6760 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6761 | { |
| 6762 | q=GetAuthenticPixels(image,0,y,image->columns,1,exception); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6763 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 6764 | for (x=(ssize_t) image->columns-1; x >= 0; x--) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6765 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6766 | SetPixelRed(image,ScaleShortToQuantum( |
| 6767 | GetPixelRed(image,q)),q); |
| 6768 | SetPixelGreen(image,ScaleShortToQuantum( |
| 6769 | GetPixelGreen(image,q)),q); |
| 6770 | SetPixelBlue(image,ScaleShortToQuantum( |
| 6771 | GetPixelBlue(image,q)),q); |
| 6772 | SetPixelAlpha(image,ScaleShortToQuantum( |
| 6773 | GetPixelAlpha(image,q)),q); |
| 6774 | q+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6775 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6776 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6777 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 6778 | break; |
| 6779 | } |
| 6780 | } |
| 6781 | #endif |
| 6782 | if (logging != MagickFalse) |
| 6783 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 6784 | " Finished MAGN processing"); |
| 6785 | } |
| 6786 | } |
| 6787 | |
| 6788 | /* |
| 6789 | Crop_box is with respect to the upper left corner of the MNG. |
| 6790 | */ |
| 6791 | crop_box.left=mng_info->image_box.left+mng_info->x_off[object_id]; |
| 6792 | crop_box.right=mng_info->image_box.right+mng_info->x_off[object_id]; |
| 6793 | crop_box.top=mng_info->image_box.top+mng_info->y_off[object_id]; |
| 6794 | crop_box.bottom=mng_info->image_box.bottom+mng_info->y_off[object_id]; |
| 6795 | crop_box=mng_minimum_box(crop_box,mng_info->clip); |
| 6796 | crop_box=mng_minimum_box(crop_box,mng_info->frame); |
| 6797 | crop_box=mng_minimum_box(crop_box,mng_info->object_clip[object_id]); |
| 6798 | if ((crop_box.left != (mng_info->image_box.left |
| 6799 | +mng_info->x_off[object_id])) || |
| 6800 | (crop_box.right != (mng_info->image_box.right |
| 6801 | +mng_info->x_off[object_id])) || |
| 6802 | (crop_box.top != (mng_info->image_box.top |
| 6803 | +mng_info->y_off[object_id])) || |
| 6804 | (crop_box.bottom != (mng_info->image_box.bottom |
| 6805 | +mng_info->y_off[object_id]))) |
| 6806 | { |
| 6807 | if (logging != MagickFalse) |
| 6808 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 6809 | " Crop the PNG image"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6810 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6811 | if ((crop_box.left < crop_box.right) && |
| 6812 | (crop_box.top < crop_box.bottom)) |
| 6813 | { |
| 6814 | Image |
| 6815 | *im; |
| 6816 | |
| 6817 | RectangleInfo |
| 6818 | crop_info; |
| 6819 | |
| 6820 | /* |
| 6821 | Crop_info is with respect to the upper left corner of |
| 6822 | the image. |
| 6823 | */ |
| 6824 | crop_info.x=(crop_box.left-mng_info->x_off[object_id]); |
| 6825 | crop_info.y=(crop_box.top-mng_info->y_off[object_id]); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 6826 | crop_info.width=(size_t) (crop_box.right-crop_box.left); |
| 6827 | crop_info.height=(size_t) (crop_box.bottom-crop_box.top); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6828 | image->page.width=image->columns; |
| 6829 | image->page.height=image->rows; |
| 6830 | image->page.x=0; |
| 6831 | image->page.y=0; |
| 6832 | im=CropImage(image,&crop_info,exception); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6833 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6834 | if (im != (Image *) NULL) |
| 6835 | { |
| 6836 | image->columns=im->columns; |
| 6837 | image->rows=im->rows; |
| 6838 | im=DestroyImage(im); |
| 6839 | image->page.width=image->columns; |
| 6840 | image->page.height=image->rows; |
| 6841 | image->page.x=crop_box.left; |
| 6842 | image->page.y=crop_box.top; |
| 6843 | } |
| 6844 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6845 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6846 | else |
| 6847 | { |
| 6848 | /* |
| 6849 | No pixels in crop area. The MNG spec still requires |
| 6850 | a layer, though, so make a single transparent pixel in |
| 6851 | the top left corner. |
| 6852 | */ |
| 6853 | image->columns=1; |
| 6854 | image->rows=1; |
| 6855 | image->colors=2; |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6856 | (void) SetImageBackgroundColor(image,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6857 | image->page.width=1; |
| 6858 | image->page.height=1; |
| 6859 | image->page.x=0; |
| 6860 | image->page.y=0; |
| 6861 | } |
| 6862 | } |
| 6863 | #ifndef PNG_READ_EMPTY_PLTE_SUPPORTED |
| 6864 | image=mng_info->image; |
| 6865 | #endif |
| 6866 | } |
| 6867 | |
glennrp | 2b013e4 | 2010-11-24 16:55:50 +0000 | [diff] [blame] | 6868 | #if (MAGICKCORE_QUANTUM_DEPTH > 16) |
| 6869 | /* PNG does not handle depths greater than 16 so reduce it even |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6870 | * if lossy. |
glennrp | 2b013e4 | 2010-11-24 16:55:50 +0000 | [diff] [blame] | 6871 | */ |
| 6872 | if (image->depth > 16) |
| 6873 | image->depth=16; |
| 6874 | #endif |
| 6875 | |
glennrp | 3faa9a3 | 2011-04-23 14:00:25 +0000 | [diff] [blame] | 6876 | #if (MAGICKCORE_QUANTUM_DEPTH > 8) |
glennrp | cc5d45b | 2012-01-06 04:06:10 +0000 | [diff] [blame] | 6877 | if (image->depth > 8) |
| 6878 | { |
| 6879 | /* To do: fill low byte properly */ |
| 6880 | image->depth=16; |
| 6881 | } |
| 6882 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6883 | if (LosslessReduceDepthOK(image,exception) != MagickFalse) |
glennrp | 8640fb5 | 2010-11-23 15:48:26 +0000 | [diff] [blame] | 6884 | image->depth = 8; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6885 | #endif |
glennrp | d6afd54 | 2010-11-19 01:53:05 +0000 | [diff] [blame] | 6886 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6887 | if (image_info->number_scenes != 0) |
| 6888 | { |
| 6889 | if (mng_info->scenes_found > |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 6890 | (ssize_t) (image_info->first_scene+image_info->number_scenes)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6891 | break; |
| 6892 | } |
glennrp | d6afd54 | 2010-11-19 01:53:05 +0000 | [diff] [blame] | 6893 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6894 | if (logging != MagickFalse) |
| 6895 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 6896 | " Finished reading image datastream."); |
glennrp | d6afd54 | 2010-11-19 01:53:05 +0000 | [diff] [blame] | 6897 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6898 | } while (LocaleCompare(image_info->magick,"MNG") == 0); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6899 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6900 | (void) CloseBlob(image); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6901 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6902 | if (logging != MagickFalse) |
| 6903 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 6904 | " Finished reading all image datastreams."); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6905 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6906 | #if defined(MNG_INSERT_LAYERS) |
| 6907 | if (insert_layers && !mng_info->image_found && (mng_info->mng_width) && |
| 6908 | (mng_info->mng_height)) |
| 6909 | { |
| 6910 | /* |
| 6911 | Insert a background layer if nothing else was found. |
| 6912 | */ |
| 6913 | if (logging != MagickFalse) |
| 6914 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 6915 | " No images found. Inserting a background layer."); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 6916 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6917 | if (GetAuthenticPixelQueue(image) != (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6918 | { |
| 6919 | /* |
| 6920 | Allocate next image structure. |
| 6921 | */ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6922 | AcquireNextImage(image_info,image,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6923 | if (GetNextImageInList(image) == (Image *) NULL) |
| 6924 | { |
| 6925 | image=DestroyImageList(image); |
| 6926 | MngInfoFreeStruct(mng_info,&have_mng_structure); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6927 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6928 | if (logging != MagickFalse) |
| 6929 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 6930 | " Allocation failed, returning NULL."); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6931 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6932 | return((Image *) NULL); |
| 6933 | } |
| 6934 | image=SyncNextImageInList(image); |
| 6935 | } |
| 6936 | image->columns=mng_info->mng_width; |
| 6937 | image->rows=mng_info->mng_height; |
| 6938 | image->page.width=mng_info->mng_width; |
| 6939 | image->page.height=mng_info->mng_height; |
| 6940 | image->page.x=0; |
| 6941 | image->page.y=0; |
| 6942 | image->background_color=mng_background_color; |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 6943 | image->alpha_trait=UndefinedPixelTrait; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 6944 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6945 | if (image_info->ping == MagickFalse) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6946 | (void) SetImageBackgroundColor(image,exception); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 6947 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6948 | mng_info->image_found++; |
| 6949 | } |
| 6950 | #endif |
| 6951 | image->iterations=mng_iterations; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 6952 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6953 | if (mng_iterations == 1) |
| 6954 | image->start_loop=MagickTrue; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 6955 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6956 | while (GetPreviousImageInList(image) != (Image *) NULL) |
| 6957 | { |
| 6958 | image_count++; |
| 6959 | if (image_count > 10*mng_info->image_found) |
| 6960 | { |
| 6961 | if (logging != MagickFalse) |
| 6962 | (void) LogMagickEvent(CoderEvent,GetMagickModule()," No beginning"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6963 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6964 | (void) ThrowMagickException(exception,GetMagickModule(), |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6965 | CoderError,"Linked list is corrupted, beginning of list not found", |
| 6966 | "`%s'",image_info->filename); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6967 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6968 | return((Image *) NULL); |
| 6969 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 6970 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6971 | image=GetPreviousImageInList(image); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 6972 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6973 | if (GetNextImageInList(image) == (Image *) NULL) |
| 6974 | { |
| 6975 | if (logging != MagickFalse) |
| 6976 | (void) LogMagickEvent(CoderEvent,GetMagickModule()," Corrupt list"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6977 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6978 | (void) ThrowMagickException(exception,GetMagickModule(), |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6979 | CoderError,"Linked list is corrupted; next_image is NULL","`%s'", |
| 6980 | image_info->filename); |
| 6981 | } |
| 6982 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6983 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6984 | if (mng_info->ticks_per_second && mng_info->image_found > 1 && |
| 6985 | GetNextImageInList(image) == |
| 6986 | (Image *) NULL) |
| 6987 | { |
| 6988 | if (logging != MagickFalse) |
| 6989 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 6990 | " First image null"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6991 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6992 | (void) ThrowMagickException(exception,GetMagickModule(), |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6993 | CoderError,"image->next for first image is NULL but shouldn't be.", |
| 6994 | "`%s'",image_info->filename); |
| 6995 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6996 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6997 | if (mng_info->image_found == 0) |
| 6998 | { |
| 6999 | if (logging != MagickFalse) |
| 7000 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 7001 | " No visible images found."); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7002 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 7003 | (void) ThrowMagickException(exception,GetMagickModule(), |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7004 | CoderError,"No visible images in file","`%s'",image_info->filename); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7005 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7006 | if (image != (Image *) NULL) |
| 7007 | image=DestroyImageList(image); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7008 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7009 | MngInfoFreeStruct(mng_info,&have_mng_structure); |
| 7010 | return((Image *) NULL); |
| 7011 | } |
| 7012 | |
| 7013 | if (mng_info->ticks_per_second) |
| 7014 | final_delay=1UL*MagickMax(image->ticks_per_second,1L)* |
| 7015 | final_delay/mng_info->ticks_per_second; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 7016 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7017 | else |
| 7018 | image->start_loop=MagickTrue; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 7019 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7020 | /* Find final nonzero image delay */ |
| 7021 | final_image_delay=0; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 7022 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7023 | while (GetNextImageInList(image) != (Image *) NULL) |
| 7024 | { |
| 7025 | if (image->delay) |
| 7026 | final_image_delay=image->delay; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7027 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7028 | image=GetNextImageInList(image); |
| 7029 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 7030 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7031 | if (final_delay < final_image_delay) |
| 7032 | final_delay=final_image_delay; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 7033 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7034 | image->delay=final_delay; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 7035 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7036 | if (logging != MagickFalse) |
| 7037 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 7038 | " image->delay=%.20g, final_delay=%.20g",(double) image->delay, |
| 7039 | (double) final_delay); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 7040 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7041 | if (logging != MagickFalse) |
| 7042 | { |
| 7043 | int |
| 7044 | scene; |
| 7045 | |
| 7046 | scene=0; |
| 7047 | image=GetFirstImageInList(image); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7048 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7049 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 7050 | " Before coalesce:"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7051 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7052 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 7053 | " scene 0 delay=%.20g",(double) image->delay); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7054 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7055 | while (GetNextImageInList(image) != (Image *) NULL) |
| 7056 | { |
| 7057 | image=GetNextImageInList(image); |
| 7058 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 7059 | " scene %.20g delay=%.20g",(double) scene++,(double) image->delay); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7060 | } |
| 7061 | } |
| 7062 | |
| 7063 | image=GetFirstImageInList(image); |
| 7064 | #ifdef MNG_COALESCE_LAYERS |
| 7065 | if (insert_layers) |
| 7066 | { |
| 7067 | Image |
| 7068 | *next_image, |
| 7069 | *next; |
| 7070 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 7071 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7072 | scene; |
| 7073 | |
| 7074 | if (logging != MagickFalse) |
| 7075 | (void) LogMagickEvent(CoderEvent,GetMagickModule()," Coalesce Images"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7076 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7077 | scene=image->scene; |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 7078 | next_image=CoalesceImages(image,exception); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7079 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7080 | if (next_image == (Image *) NULL) |
| 7081 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7082 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7083 | image=DestroyImageList(image); |
| 7084 | image=next_image; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7085 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7086 | for (next=image; next != (Image *) NULL; next=next_image) |
| 7087 | { |
| 7088 | next->page.width=mng_info->mng_width; |
| 7089 | next->page.height=mng_info->mng_height; |
| 7090 | next->page.x=0; |
| 7091 | next->page.y=0; |
| 7092 | next->scene=scene++; |
| 7093 | next_image=GetNextImageInList(next); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7094 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7095 | if (next_image == (Image *) NULL) |
| 7096 | break; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7097 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7098 | if (next->delay == 0) |
| 7099 | { |
| 7100 | scene--; |
| 7101 | next_image->previous=GetPreviousImageInList(next); |
| 7102 | if (GetPreviousImageInList(next) == (Image *) NULL) |
| 7103 | image=next_image; |
| 7104 | else |
| 7105 | next->previous->next=next_image; |
| 7106 | next=DestroyImage(next); |
| 7107 | } |
| 7108 | } |
| 7109 | } |
| 7110 | #endif |
| 7111 | |
| 7112 | while (GetNextImageInList(image) != (Image *) NULL) |
| 7113 | image=GetNextImageInList(image); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7114 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7115 | image->dispose=BackgroundDispose; |
| 7116 | |
| 7117 | if (logging != MagickFalse) |
| 7118 | { |
| 7119 | int |
| 7120 | scene; |
| 7121 | |
| 7122 | scene=0; |
| 7123 | image=GetFirstImageInList(image); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7124 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7125 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 7126 | " After coalesce:"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7127 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7128 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 7129 | " scene 0 delay=%.20g dispose=%.20g",(double) image->delay, |
| 7130 | (double) image->dispose); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7131 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7132 | while (GetNextImageInList(image) != (Image *) NULL) |
cristy | f2faecf | 2010-05-28 19:19:36 +0000 | [diff] [blame] | 7133 | { |
| 7134 | image=GetNextImageInList(image); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7135 | |
cristy | f2faecf | 2010-05-28 19:19:36 +0000 | [diff] [blame] | 7136 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 7137 | " scene %.20g delay=%.20g dispose=%.20g",(double) scene++, |
| 7138 | (double) image->delay,(double) image->dispose); |
cristy | f2faecf | 2010-05-28 19:19:36 +0000 | [diff] [blame] | 7139 | } |
| 7140 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7141 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7142 | image=GetFirstImageInList(image); |
| 7143 | MngInfoFreeStruct(mng_info,&have_mng_structure); |
| 7144 | have_mng_structure=MagickFalse; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7145 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7146 | if (logging != MagickFalse) |
| 7147 | (void) LogMagickEvent(CoderEvent,GetMagickModule(),"exit ReadMNGImage()"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7148 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7149 | return(GetFirstImageInList(image)); |
| 7150 | } |
glennrp | 25c1e2b | 2010-03-25 01:39:56 +0000 | [diff] [blame] | 7151 | #else /* PNG_LIBPNG_VER > 10011 */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7152 | static Image *ReadPNGImage(const ImageInfo *image_info,ExceptionInfo *exception) |
| 7153 | { |
| 7154 | printf("Your PNG library is too old: You have libpng-%s\n", |
| 7155 | PNG_LIBPNG_VER_STRING); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7156 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7157 | (void) ThrowMagickException(exception,GetMagickModule(),CoderError, |
| 7158 | "PNG library is too old","`%s'",image_info->filename); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7159 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7160 | return(Image *) NULL; |
| 7161 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7162 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7163 | static Image *ReadMNGImage(const ImageInfo *image_info,ExceptionInfo *exception) |
| 7164 | { |
| 7165 | return(ReadPNGImage(image_info,exception)); |
| 7166 | } |
glennrp | 25c1e2b | 2010-03-25 01:39:56 +0000 | [diff] [blame] | 7167 | #endif /* PNG_LIBPNG_VER > 10011 */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7168 | #endif |
| 7169 | |
| 7170 | /* |
| 7171 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 7172 | % % |
| 7173 | % % |
| 7174 | % % |
| 7175 | % R e g i s t e r P N G I m a g e % |
| 7176 | % % |
| 7177 | % % |
| 7178 | % % |
| 7179 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 7180 | % |
| 7181 | % RegisterPNGImage() adds properties for the PNG image format to |
| 7182 | % the list of supported formats. The properties include the image format |
| 7183 | % tag, a method to read and/or write the format, whether the format |
| 7184 | % supports the saving of more than one frame to the same file or blob, |
| 7185 | % whether the format supports native in-memory I/O, and a brief |
| 7186 | % description of the format. |
| 7187 | % |
| 7188 | % The format of the RegisterPNGImage method is: |
| 7189 | % |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 7190 | % size_t RegisterPNGImage(void) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7191 | % |
| 7192 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 7193 | ModuleExport size_t RegisterPNGImage(void) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7194 | { |
| 7195 | char |
| 7196 | version[MaxTextExtent]; |
| 7197 | |
| 7198 | MagickInfo |
| 7199 | *entry; |
| 7200 | |
| 7201 | static const char |
| 7202 | *PNGNote= |
| 7203 | { |
| 7204 | "See http://www.libpng.org/ for details about the PNG format." |
| 7205 | }, |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7206 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7207 | *JNGNote= |
| 7208 | { |
| 7209 | "See http://www.libpng.org/pub/mng/ for details about the JNG\n" |
| 7210 | "format." |
| 7211 | }, |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7212 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7213 | *MNGNote= |
| 7214 | { |
| 7215 | "See http://www.libpng.org/pub/mng/ for details about the MNG\n" |
| 7216 | "format." |
| 7217 | }; |
| 7218 | |
| 7219 | *version='\0'; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7220 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7221 | #if defined(PNG_LIBPNG_VER_STRING) |
| 7222 | (void) ConcatenateMagickString(version,"libpng ",MaxTextExtent); |
| 7223 | (void) ConcatenateMagickString(version,PNG_LIBPNG_VER_STRING,MaxTextExtent); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7224 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7225 | if (LocaleCompare(PNG_LIBPNG_VER_STRING,png_get_header_ver(NULL)) != 0) |
| 7226 | { |
| 7227 | (void) ConcatenateMagickString(version,",",MaxTextExtent); |
| 7228 | (void) ConcatenateMagickString(version,png_get_libpng_ver(NULL), |
| 7229 | MaxTextExtent); |
| 7230 | } |
| 7231 | #endif |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7232 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7233 | entry=SetMagickInfo("MNG"); |
| 7234 | entry->seekable_stream=MagickTrue; /* To do: eliminate this. */ |
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 defined(MAGICKCORE_PNG_DELEGATE) |
| 7237 | entry->decoder=(DecodeImageHandler *) ReadMNGImage; |
| 7238 | entry->encoder=(EncodeImageHandler *) WriteMNGImage; |
| 7239 | #endif |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7240 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7241 | entry->magick=(IsImageFormatHandler *) IsMNG; |
| 7242 | entry->description=ConstantString("Multiple-image Network Graphics"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7243 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7244 | if (*version != '\0') |
| 7245 | entry->version=ConstantString(version); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7246 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7247 | entry->module=ConstantString("PNG"); |
| 7248 | entry->note=ConstantString(MNGNote); |
| 7249 | (void) RegisterMagickInfo(entry); |
| 7250 | |
| 7251 | entry=SetMagickInfo("PNG"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7252 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7253 | #if defined(MAGICKCORE_PNG_DELEGATE) |
| 7254 | entry->decoder=(DecodeImageHandler *) ReadPNGImage; |
| 7255 | entry->encoder=(EncodeImageHandler *) WritePNGImage; |
| 7256 | #endif |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7257 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7258 | entry->magick=(IsImageFormatHandler *) IsPNG; |
| 7259 | entry->adjoin=MagickFalse; |
| 7260 | entry->description=ConstantString("Portable Network Graphics"); |
| 7261 | entry->module=ConstantString("PNG"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7262 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7263 | if (*version != '\0') |
| 7264 | entry->version=ConstantString(version); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7265 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7266 | entry->note=ConstantString(PNGNote); |
| 7267 | (void) RegisterMagickInfo(entry); |
| 7268 | |
| 7269 | entry=SetMagickInfo("PNG8"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7270 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7271 | #if defined(MAGICKCORE_PNG_DELEGATE) |
| 7272 | entry->decoder=(DecodeImageHandler *) ReadPNGImage; |
| 7273 | entry->encoder=(EncodeImageHandler *) WritePNGImage; |
| 7274 | #endif |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7275 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7276 | entry->magick=(IsImageFormatHandler *) IsPNG; |
| 7277 | entry->adjoin=MagickFalse; |
| 7278 | entry->description=ConstantString( |
| 7279 | "8-bit indexed with optional binary transparency"); |
| 7280 | entry->module=ConstantString("PNG"); |
| 7281 | (void) RegisterMagickInfo(entry); |
| 7282 | |
| 7283 | entry=SetMagickInfo("PNG24"); |
| 7284 | *version='\0'; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7285 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7286 | #if defined(ZLIB_VERSION) |
| 7287 | (void) ConcatenateMagickString(version,"zlib ",MaxTextExtent); |
| 7288 | (void) ConcatenateMagickString(version,ZLIB_VERSION,MaxTextExtent); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7289 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7290 | if (LocaleCompare(ZLIB_VERSION,zlib_version) != 0) |
| 7291 | { |
| 7292 | (void) ConcatenateMagickString(version,",",MaxTextExtent); |
| 7293 | (void) ConcatenateMagickString(version,zlib_version,MaxTextExtent); |
| 7294 | } |
| 7295 | #endif |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7296 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7297 | if (*version != '\0') |
| 7298 | entry->version=ConstantString(version); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7299 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7300 | #if defined(MAGICKCORE_PNG_DELEGATE) |
| 7301 | entry->decoder=(DecodeImageHandler *) ReadPNGImage; |
| 7302 | entry->encoder=(EncodeImageHandler *) WritePNGImage; |
| 7303 | #endif |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7304 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7305 | entry->magick=(IsImageFormatHandler *) IsPNG; |
| 7306 | entry->adjoin=MagickFalse; |
glennrp | fd164d2 | 2013-01-26 21:10:22 +0000 | [diff] [blame] | 7307 | entry->description=ConstantString("opaque or binary transparent 24-bit RGB"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7308 | entry->module=ConstantString("PNG"); |
| 7309 | (void) RegisterMagickInfo(entry); |
| 7310 | |
| 7311 | entry=SetMagickInfo("PNG32"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7312 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7313 | #if defined(MAGICKCORE_PNG_DELEGATE) |
| 7314 | entry->decoder=(DecodeImageHandler *) ReadPNGImage; |
| 7315 | entry->encoder=(EncodeImageHandler *) WritePNGImage; |
| 7316 | #endif |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7317 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7318 | entry->magick=(IsImageFormatHandler *) IsPNG; |
| 7319 | entry->adjoin=MagickFalse; |
| 7320 | entry->description=ConstantString("opaque or transparent 32-bit RGBA"); |
| 7321 | entry->module=ConstantString("PNG"); |
| 7322 | (void) RegisterMagickInfo(entry); |
| 7323 | |
glennrp | fd164d2 | 2013-01-26 21:10:22 +0000 | [diff] [blame] | 7324 | entry=SetMagickInfo("PNG48"); |
| 7325 | |
| 7326 | #if defined(MAGICKCORE_PNG_DELEGATE) |
| 7327 | entry->decoder=(DecodeImageHandler *) ReadPNGImage; |
| 7328 | entry->encoder=(EncodeImageHandler *) WritePNGImage; |
| 7329 | #endif |
| 7330 | |
| 7331 | entry->magick=(IsImageFormatHandler *) IsPNG; |
| 7332 | entry->adjoin=MagickFalse; |
| 7333 | entry->description=ConstantString("opaque or binary transparent 48-bit RGB"); |
| 7334 | entry->module=ConstantString("PNG"); |
| 7335 | (void) RegisterMagickInfo(entry); |
| 7336 | |
| 7337 | entry=SetMagickInfo("PNG64"); |
| 7338 | |
| 7339 | #if defined(MAGICKCORE_PNG_DELEGATE) |
| 7340 | entry->decoder=(DecodeImageHandler *) ReadPNGImage; |
| 7341 | entry->encoder=(EncodeImageHandler *) WritePNGImage; |
| 7342 | #endif |
| 7343 | |
| 7344 | entry->magick=(IsImageFormatHandler *) IsPNG; |
| 7345 | entry->adjoin=MagickFalse; |
| 7346 | entry->description=ConstantString("opaque or transparent 64-bit RGBA"); |
| 7347 | entry->module=ConstantString("PNG"); |
| 7348 | (void) RegisterMagickInfo(entry); |
| 7349 | |
glennrp | 5830fbc | 2013-01-27 06:11:45 +0000 | [diff] [blame] | 7350 | entry=SetMagickInfo("PNG00"); |
| 7351 | |
| 7352 | #if defined(MAGICKCORE_PNG_DELEGATE) |
| 7353 | entry->decoder=(DecodeImageHandler *) ReadPNGImage; |
| 7354 | entry->encoder=(EncodeImageHandler *) WritePNGImage; |
| 7355 | #endif |
| 7356 | |
| 7357 | entry->magick=(IsImageFormatHandler *) IsPNG; |
| 7358 | entry->adjoin=MagickFalse; |
glennrp | 6270857 | 2013-02-15 12:51:48 +0000 | [diff] [blame^] | 7359 | entry->description=ConstantString( |
| 7360 | "PNG inheriting bit-depth and color-type from original"); |
glennrp | 5830fbc | 2013-01-27 06:11:45 +0000 | [diff] [blame] | 7361 | entry->module=ConstantString("PNG"); |
| 7362 | (void) RegisterMagickInfo(entry); |
| 7363 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7364 | entry=SetMagickInfo("JNG"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7365 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7366 | #if defined(JNG_SUPPORTED) |
| 7367 | #if defined(MAGICKCORE_PNG_DELEGATE) |
| 7368 | entry->decoder=(DecodeImageHandler *) ReadJNGImage; |
| 7369 | entry->encoder=(EncodeImageHandler *) WriteJNGImage; |
| 7370 | #endif |
| 7371 | #endif |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7372 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7373 | entry->magick=(IsImageFormatHandler *) IsJNG; |
| 7374 | entry->adjoin=MagickFalse; |
| 7375 | entry->description=ConstantString("JPEG Network Graphics"); |
| 7376 | entry->module=ConstantString("PNG"); |
| 7377 | entry->note=ConstantString(JNGNote); |
| 7378 | (void) RegisterMagickInfo(entry); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7379 | |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 7380 | #ifdef PNG_SETJMP_NOT_THREAD_SAFE |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 7381 | ping_semaphore=AllocateSemaphoreInfo(); |
cristy | 18b1744 | 2009-10-25 18:36:48 +0000 | [diff] [blame] | 7382 | #endif |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7383 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7384 | return(MagickImageCoderSignature); |
| 7385 | } |
| 7386 | |
| 7387 | /* |
| 7388 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 7389 | % % |
| 7390 | % % |
| 7391 | % % |
| 7392 | % U n r e g i s t e r P N G I m a g e % |
| 7393 | % % |
| 7394 | % % |
| 7395 | % % |
| 7396 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 7397 | % |
| 7398 | % UnregisterPNGImage() removes format registrations made by the |
| 7399 | % PNG module from the list of supported formats. |
| 7400 | % |
| 7401 | % The format of the UnregisterPNGImage method is: |
| 7402 | % |
| 7403 | % UnregisterPNGImage(void) |
| 7404 | % |
| 7405 | */ |
| 7406 | ModuleExport void UnregisterPNGImage(void) |
| 7407 | { |
| 7408 | (void) UnregisterMagickInfo("MNG"); |
| 7409 | (void) UnregisterMagickInfo("PNG"); |
| 7410 | (void) UnregisterMagickInfo("PNG8"); |
| 7411 | (void) UnregisterMagickInfo("PNG24"); |
| 7412 | (void) UnregisterMagickInfo("PNG32"); |
glennrp | fd164d2 | 2013-01-26 21:10:22 +0000 | [diff] [blame] | 7413 | (void) UnregisterMagickInfo("PNG48"); |
| 7414 | (void) UnregisterMagickInfo("PNG64"); |
glennrp | 5830fbc | 2013-01-27 06:11:45 +0000 | [diff] [blame] | 7415 | (void) UnregisterMagickInfo("PNG00"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7416 | (void) UnregisterMagickInfo("JNG"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7417 | |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 7418 | #ifdef PNG_SETJMP_NOT_THREAD_SAFE |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 7419 | if (ping_semaphore != (SemaphoreInfo *) NULL) |
| 7420 | DestroySemaphoreInfo(&ping_semaphore); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7421 | #endif |
| 7422 | } |
| 7423 | |
| 7424 | #if defined(MAGICKCORE_PNG_DELEGATE) |
glennrp | 25c1e2b | 2010-03-25 01:39:56 +0000 | [diff] [blame] | 7425 | #if PNG_LIBPNG_VER > 10011 |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7426 | /* |
| 7427 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 7428 | % % |
| 7429 | % % |
| 7430 | % % |
| 7431 | % W r i t e M N G I m a g e % |
| 7432 | % % |
| 7433 | % % |
| 7434 | % % |
| 7435 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 7436 | % |
| 7437 | % WriteMNGImage() writes an image in the Portable Network Graphics |
| 7438 | % Group's "Multiple-image Network Graphics" encoded image format. |
| 7439 | % |
| 7440 | % MNG support written by Glenn Randers-Pehrson, glennrp@image... |
| 7441 | % |
| 7442 | % The format of the WriteMNGImage method is: |
| 7443 | % |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 7444 | % MagickBooleanType WriteMNGImage(const ImageInfo *image_info, |
| 7445 | % Image *image,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7446 | % |
| 7447 | % A description of each parameter follows. |
| 7448 | % |
| 7449 | % o image_info: the image info. |
| 7450 | % |
| 7451 | % o image: The image. |
| 7452 | % |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 7453 | % o exception: return any errors or warnings in this structure. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7454 | % |
| 7455 | % To do (as of version 5.5.2, November 26, 2002 -- glennrp -- see also |
| 7456 | % "To do" under ReadPNGImage): |
| 7457 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7458 | % Preserve all unknown and not-yet-handled known chunks found in input |
| 7459 | % PNG file and copy them into output PNG files according to the PNG |
| 7460 | % copying rules. |
| 7461 | % |
| 7462 | % Write the iCCP chunk at MNG level when (icc profile length > 0) |
| 7463 | % |
| 7464 | % Improve selection of color type (use indexed-colour or indexed-colour |
| 7465 | % with tRNS when 256 or fewer unique RGBA values are present). |
| 7466 | % |
| 7467 | % Figure out what to do with "dispose=<restore-to-previous>" (dispose == 3) |
| 7468 | % This will be complicated if we limit ourselves to generating MNG-LC |
| 7469 | % files. For now we ignore disposal method 3 and simply overlay the next |
| 7470 | % image on it. |
| 7471 | % |
| 7472 | % Check for identical PLTE's or PLTE/tRNS combinations and use a |
| 7473 | % global MNG PLTE or PLTE/tRNS combination when appropriate. |
| 7474 | % [mostly done 15 June 1999 but still need to take care of tRNS] |
| 7475 | % |
| 7476 | % Check for identical sRGB and replace with a global sRGB (and remove |
| 7477 | % gAMA/cHRM if sRGB is found; check for identical gAMA/cHRM and |
| 7478 | % replace with global gAMA/cHRM (or with sRGB if appropriate; replace |
| 7479 | % local gAMA/cHRM with local sRGB if appropriate). |
| 7480 | % |
| 7481 | % Check for identical sBIT chunks and write global ones. |
| 7482 | % |
| 7483 | % Provide option to skip writing the signature tEXt chunks. |
| 7484 | % |
| 7485 | % Use signatures to detect identical objects and reuse the first |
| 7486 | % instance of such objects instead of writing duplicate objects. |
| 7487 | % |
| 7488 | % Use a smaller-than-32k value of compression window size when |
| 7489 | % appropriate. |
| 7490 | % |
| 7491 | % Encode JNG datastreams. Mostly done as of 5.5.2; need to write |
| 7492 | % ancillary text chunks and save profiles. |
| 7493 | % |
| 7494 | % Provide an option to force LC files (to ensure exact framing rate) |
| 7495 | % instead of VLC. |
| 7496 | % |
| 7497 | % Provide an option to force VLC files instead of LC, even when offsets |
| 7498 | % are present. This will involve expanding the embedded images with a |
| 7499 | % transparent region at the top and/or left. |
| 7500 | */ |
| 7501 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7502 | static void |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 7503 | Magick_png_write_raw_profile(const ImageInfo *image_info,png_struct *ping, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7504 | png_info *ping_info, unsigned char *profile_type, unsigned char |
| 7505 | *profile_description, unsigned char *profile_data, png_uint_32 length) |
| 7506 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7507 | png_textp |
| 7508 | text; |
| 7509 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 7510 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7511 | i; |
| 7512 | |
| 7513 | unsigned char |
| 7514 | *sp; |
| 7515 | |
| 7516 | png_charp |
| 7517 | dp; |
| 7518 | |
| 7519 | png_uint_32 |
| 7520 | allocated_length, |
| 7521 | description_length; |
| 7522 | |
| 7523 | unsigned char |
| 7524 | 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] | 7525 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7526 | if (LocaleNCompare((char *) profile_type+1, "ng-chunk-",9) == 0) |
| 7527 | return; |
| 7528 | |
| 7529 | if (image_info->verbose) |
| 7530 | { |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 7531 | (void) printf("writing raw profile: type=%s, length=%.20g\n", |
| 7532 | (char *) profile_type, (double) length); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7533 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 7534 | |
cristy | a865ccd | 2012-07-28 00:33:10 +0000 | [diff] [blame] | 7535 | #if PNG_LIBPNG_VER >= 14000 |
| 7536 | text=(png_textp) png_malloc(ping,(png_alloc_size_t) sizeof(png_text)); |
| 7537 | #else |
| 7538 | text=(png_textp) png_malloc(ping,(png_size_t) sizeof(png_text)); |
| 7539 | #endif |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7540 | description_length=(png_uint_32) strlen((const char *) profile_description); |
| 7541 | allocated_length=(png_uint_32) (length*2 + (length >> 5) + 20 |
| 7542 | + description_length); |
cristy | a865ccd | 2012-07-28 00:33:10 +0000 | [diff] [blame] | 7543 | #if PNG_LIBPNG_VER >= 14000 |
| 7544 | text[0].text=(png_charp) png_malloc(ping, |
| 7545 | (png_alloc_size_t) allocated_length); |
| 7546 | text[0].key=(png_charp) png_malloc(ping, (png_alloc_size_t) 80); |
| 7547 | #else |
| 7548 | text[0].text=(png_charp) png_malloc(ping, (png_size_t) allocated_length); |
| 7549 | text[0].key=(png_charp) png_malloc(ping, (png_size_t) 80); |
| 7550 | #endif |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7551 | text[0].key[0]='\0'; |
| 7552 | (void) ConcatenateMagickString(text[0].key, |
| 7553 | "Raw profile type ",MaxTextExtent); |
| 7554 | (void) ConcatenateMagickString(text[0].key,(const char *) profile_type,62); |
| 7555 | sp=profile_data; |
| 7556 | dp=text[0].text; |
| 7557 | *dp++='\n'; |
| 7558 | (void) CopyMagickString(dp,(const char *) profile_description, |
| 7559 | allocated_length); |
| 7560 | dp+=description_length; |
| 7561 | *dp++='\n'; |
cristy | 3b6fd2e | 2011-05-20 12:53:50 +0000 | [diff] [blame] | 7562 | (void) FormatLocaleString(dp,allocated_length- |
cristy | f2faecf | 2010-05-28 19:19:36 +0000 | [diff] [blame] | 7563 | (png_size_t) (dp-text[0].text),"%8lu ",(unsigned long) length); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7564 | dp+=8; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7565 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 7566 | for (i=0; i < (ssize_t) length; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7567 | { |
| 7568 | if (i%36 == 0) |
| 7569 | *dp++='\n'; |
| 7570 | *(dp++)=(char) hex[((*sp >> 4) & 0x0f)]; |
| 7571 | *(dp++)=(char) hex[((*sp++ ) & 0x0f)]; |
| 7572 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7573 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7574 | *dp++='\n'; |
| 7575 | *dp='\0'; |
| 7576 | text[0].text_length=(png_size_t) (dp-text[0].text); |
| 7577 | text[0].compression=image_info->compression == NoCompression || |
| 7578 | (image_info->compression == UndefinedCompression && |
| 7579 | text[0].text_length < 128) ? -1 : 0; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7580 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7581 | if (text[0].text_length <= allocated_length) |
| 7582 | png_set_text(ping,ping_info,text,1); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7583 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7584 | png_free(ping,text[0].text); |
| 7585 | png_free(ping,text[0].key); |
| 7586 | png_free(ping,text); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7587 | } |
| 7588 | |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 7589 | static MagickBooleanType Magick_png_write_chunk_from_profile(Image *image, |
cristy | 4383ec8 | 2011-01-05 15:42:32 +0000 | [diff] [blame] | 7590 | const char *string, MagickBooleanType logging) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7591 | { |
| 7592 | char |
| 7593 | *name; |
| 7594 | |
| 7595 | const StringInfo |
| 7596 | *profile; |
| 7597 | |
| 7598 | unsigned char |
| 7599 | *data; |
| 7600 | |
| 7601 | png_uint_32 length; |
| 7602 | |
| 7603 | ResetImageProfileIterator(image); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7604 | |
| 7605 | for (name=GetNextImageProfile(image); name != (const char *) NULL; ) |
| 7606 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7607 | profile=GetImageProfile(image,name); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7608 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7609 | if (profile != (const StringInfo *) NULL) |
| 7610 | { |
| 7611 | StringInfo |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 7612 | *ping_profile; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7613 | |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7614 | if (LocaleNCompare(name,string,11) == 0) |
| 7615 | { |
| 7616 | if (logging != MagickFalse) |
| 7617 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 7618 | " Found %s profile",name); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7619 | |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 7620 | ping_profile=CloneStringInfo(profile); |
| 7621 | data=GetStringInfoDatum(ping_profile), |
| 7622 | length=(png_uint_32) GetStringInfoLength(ping_profile); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7623 | data[4]=data[3]; |
| 7624 | data[3]=data[2]; |
| 7625 | data[2]=data[1]; |
| 7626 | data[1]=data[0]; |
| 7627 | (void) WriteBlobMSBULong(image,length-5); /* data length */ |
| 7628 | (void) WriteBlob(image,length-1,data+1); |
| 7629 | (void) WriteBlobMSBULong(image,crc32(0,data+1,(uInt) length-1)); |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 7630 | ping_profile=DestroyStringInfo(ping_profile); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7631 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7632 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7633 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7634 | name=GetNextImageProfile(image); |
| 7635 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7636 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7637 | return(MagickTrue); |
| 7638 | } |
| 7639 | |
glennrp | b9cfe27 | 2010-12-21 15:08:06 +0000 | [diff] [blame] | 7640 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7641 | /* Write one PNG image */ |
glennrp | b9cfe27 | 2010-12-21 15:08:06 +0000 | [diff] [blame] | 7642 | static MagickBooleanType WriteOnePNGImage(MngInfo *mng_info, |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 7643 | const ImageInfo *IMimage_info,Image *IMimage,ExceptionInfo *exception) |
glennrp | b9cfe27 | 2010-12-21 15:08:06 +0000 | [diff] [blame] | 7644 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 7645 | Image |
| 7646 | *image; |
| 7647 | |
| 7648 | ImageInfo |
| 7649 | *image_info; |
| 7650 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7651 | char |
| 7652 | s[2]; |
| 7653 | |
| 7654 | const char |
| 7655 | *name, |
| 7656 | *property, |
| 7657 | *value; |
| 7658 | |
| 7659 | const StringInfo |
| 7660 | *profile; |
| 7661 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7662 | int |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7663 | num_passes, |
glennrp | cecd576 | 2010-03-23 12:07:49 +0000 | [diff] [blame] | 7664 | pass; |
| 7665 | |
glennrp | e9c26dc | 2010-05-30 01:56:35 +0000 | [diff] [blame] | 7666 | png_byte |
| 7667 | ping_trans_alpha[256]; |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 7668 | |
glennrp | 39992b4 | 2010-11-14 00:03:43 +0000 | [diff] [blame] | 7669 | png_color |
| 7670 | palette[257]; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7671 | |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 7672 | png_color_16 |
| 7673 | ping_background, |
| 7674 | ping_trans_color; |
| 7675 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7676 | png_info |
| 7677 | *ping_info; |
| 7678 | |
| 7679 | png_struct |
| 7680 | *ping; |
| 7681 | |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 7682 | png_uint_32 |
| 7683 | ping_height, |
| 7684 | ping_width; |
| 7685 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 7686 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7687 | y; |
| 7688 | |
| 7689 | MagickBooleanType |
glennrp | 58e0176 | 2011-01-07 15:28:54 +0000 | [diff] [blame] | 7690 | image_matte, |
glennrp | 21f0e62 | 2011-01-07 16:20:57 +0000 | [diff] [blame] | 7691 | logging, |
glennrp | 58e0176 | 2011-01-07 15:28:54 +0000 | [diff] [blame] | 7692 | matte, |
| 7693 | |
glennrp | da8f3a7 | 2011-02-27 23:54:12 +0000 | [diff] [blame] | 7694 | ping_have_blob, |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 7695 | ping_have_cheap_transparency, |
glennrp | d6bf161 | 2010-12-17 17:28:54 +0000 | [diff] [blame] | 7696 | ping_have_color, |
glennrp | 8d57966 | 2011-02-23 02:05:02 +0000 | [diff] [blame] | 7697 | ping_have_non_bw, |
glennrp | 39992b4 | 2010-11-14 00:03:43 +0000 | [diff] [blame] | 7698 | ping_have_PLTE, |
glennrp | 991d11d | 2010-11-12 21:55:28 +0000 | [diff] [blame] | 7699 | ping_have_bKGD, |
| 7700 | ping_have_pHYs, |
| 7701 | ping_have_tRNS, |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 7702 | |
| 7703 | ping_exclude_bKGD, |
| 7704 | ping_exclude_cHRM, |
glennrp | a0ed009 | 2011-04-18 16:36:29 +0000 | [diff] [blame] | 7705 | ping_exclude_date, |
glennrp | e4e2d79 | 2011-02-21 12:11:27 +0000 | [diff] [blame] | 7706 | /* ping_exclude_EXIF, */ |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 7707 | ping_exclude_gAMA, |
| 7708 | ping_exclude_iCCP, |
| 7709 | /* ping_exclude_iTXt, */ |
| 7710 | ping_exclude_oFFs, |
| 7711 | ping_exclude_pHYs, |
| 7712 | ping_exclude_sRGB, |
| 7713 | ping_exclude_tEXt, |
glennrp | e4e2d79 | 2011-02-21 12:11:27 +0000 | [diff] [blame] | 7714 | /* ping_exclude_tRNS, */ |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 7715 | ping_exclude_vpAg, |
| 7716 | ping_exclude_zCCP, /* hex-encoded iCCP */ |
| 7717 | ping_exclude_zTXt, |
| 7718 | |
glennrp | 8d3d6e5 | 2011-04-19 04:39:51 +0000 | [diff] [blame] | 7719 | ping_preserve_colormap, |
glennrp | 0e8ea19 | 2010-12-24 18:00:33 +0000 | [diff] [blame] | 7720 | ping_need_colortype_warning, |
| 7721 | |
glennrp | 82b3c53 | 2011-03-22 19:20:54 +0000 | [diff] [blame] | 7722 | status, |
glennrp | 8ca51ad | 2011-05-12 21:22:32 +0000 | [diff] [blame] | 7723 | tried_332, |
glennrp | d337164 | 2011-03-22 19:42:23 +0000 | [diff] [blame] | 7724 | tried_333, |
| 7725 | tried_444; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7726 | |
| 7727 | QuantumInfo |
| 7728 | *quantum_info; |
| 7729 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 7730 | PNGErrorInfo |
| 7731 | error_info; |
| 7732 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 7733 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7734 | i, |
| 7735 | x; |
| 7736 | |
| 7737 | unsigned char |
cristy | 75fc68f | 2012-10-08 16:26:00 +0000 | [diff] [blame] | 7738 | *volatile ping_pixels; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7739 | |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 7740 | volatile int |
glennrp | f09bded | 2011-01-08 01:15:59 +0000 | [diff] [blame] | 7741 | image_colors, |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 7742 | ping_bit_depth, |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 7743 | ping_color_type, |
| 7744 | ping_interlace_method, |
| 7745 | ping_compression_method, |
| 7746 | ping_filter_method, |
| 7747 | ping_num_trans; |
| 7748 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 7749 | volatile size_t |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 7750 | image_depth, |
| 7751 | old_bit_depth; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7752 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 7753 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7754 | quality, |
| 7755 | rowbytes, |
| 7756 | save_image_depth; |
| 7757 | |
glennrp | dfd7080 | 2010-11-14 01:23:35 +0000 | [diff] [blame] | 7758 | int |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 7759 | j, |
glennrp | f09bded | 2011-01-08 01:15:59 +0000 | [diff] [blame] | 7760 | number_colors, |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 7761 | number_opaque, |
| 7762 | number_semitransparent, |
| 7763 | number_transparent, |
glennrp | dfd7080 | 2010-11-14 01:23:35 +0000 | [diff] [blame] | 7764 | ping_pHYs_unit_type; |
| 7765 | |
| 7766 | png_uint_32 |
| 7767 | ping_pHYs_x_resolution, |
| 7768 | ping_pHYs_y_resolution; |
| 7769 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7770 | logging=LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 7771 | " Enter WriteOnePNGImage()"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7772 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 7773 | image = CloneImage(IMimage,0,0,MagickFalse,exception); |
| 7774 | image_info=(ImageInfo *) CloneImageInfo(IMimage_info); |
| 7775 | if (image_info == (ImageInfo *) NULL) |
| 7776 | ThrowWriterException(ResourceLimitError, "MemoryAllocationFailed"); |
glennrp | b9cfe27 | 2010-12-21 15:08:06 +0000 | [diff] [blame] | 7777 | |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 7778 | /* Initialize some stuff */ |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 7779 | ping_bit_depth=0, |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 7780 | ping_color_type=0, |
| 7781 | ping_interlace_method=0, |
| 7782 | ping_compression_method=0, |
| 7783 | ping_filter_method=0, |
| 7784 | ping_num_trans = 0; |
| 7785 | |
| 7786 | ping_background.red = 0; |
| 7787 | ping_background.green = 0; |
| 7788 | ping_background.blue = 0; |
| 7789 | ping_background.gray = 0; |
| 7790 | ping_background.index = 0; |
| 7791 | |
| 7792 | ping_trans_color.red=0; |
| 7793 | ping_trans_color.green=0; |
| 7794 | ping_trans_color.blue=0; |
| 7795 | ping_trans_color.gray=0; |
| 7796 | |
glennrp | dfd7080 | 2010-11-14 01:23:35 +0000 | [diff] [blame] | 7797 | ping_pHYs_unit_type = 0; |
| 7798 | ping_pHYs_x_resolution = 0; |
| 7799 | ping_pHYs_y_resolution = 0; |
| 7800 | |
glennrp | da8f3a7 | 2011-02-27 23:54:12 +0000 | [diff] [blame] | 7801 | ping_have_blob=MagickFalse; |
glennrp | d6bf161 | 2010-12-17 17:28:54 +0000 | [diff] [blame] | 7802 | ping_have_color=MagickTrue; |
glennrp | 8d57966 | 2011-02-23 02:05:02 +0000 | [diff] [blame] | 7803 | ping_have_non_bw=MagickTrue; |
glennrp | 39992b4 | 2010-11-14 00:03:43 +0000 | [diff] [blame] | 7804 | ping_have_PLTE=MagickFalse; |
glennrp | 991d11d | 2010-11-12 21:55:28 +0000 | [diff] [blame] | 7805 | ping_have_bKGD=MagickFalse; |
| 7806 | ping_have_pHYs=MagickFalse; |
| 7807 | ping_have_tRNS=MagickFalse; |
| 7808 | |
glennrp | 0e8ea19 | 2010-12-24 18:00:33 +0000 | [diff] [blame] | 7809 | ping_exclude_bKGD=mng_info->ping_exclude_bKGD; |
| 7810 | ping_exclude_cHRM=mng_info->ping_exclude_cHRM; |
glennrp | a0ed009 | 2011-04-18 16:36:29 +0000 | [diff] [blame] | 7811 | ping_exclude_date=mng_info->ping_exclude_date; |
glennrp | dde35db | 2011-02-21 12:06:32 +0000 | [diff] [blame] | 7812 | /* ping_exclude_EXIF=mng_info->ping_exclude_EXIF; */ |
glennrp | 0e8ea19 | 2010-12-24 18:00:33 +0000 | [diff] [blame] | 7813 | ping_exclude_gAMA=mng_info->ping_exclude_gAMA; |
glennrp | 0e8ea19 | 2010-12-24 18:00:33 +0000 | [diff] [blame] | 7814 | ping_exclude_iCCP=mng_info->ping_exclude_iCCP; |
| 7815 | /* ping_exclude_iTXt=mng_info->ping_exclude_iTXt; */ |
| 7816 | ping_exclude_oFFs=mng_info->ping_exclude_oFFs; |
| 7817 | ping_exclude_pHYs=mng_info->ping_exclude_pHYs; |
| 7818 | ping_exclude_sRGB=mng_info->ping_exclude_sRGB; |
| 7819 | ping_exclude_tEXt=mng_info->ping_exclude_tEXt; |
glennrp | dde35db | 2011-02-21 12:06:32 +0000 | [diff] [blame] | 7820 | /* ping_exclude_tRNS=mng_info->ping_exclude_tRNS; */ |
glennrp | 0e8ea19 | 2010-12-24 18:00:33 +0000 | [diff] [blame] | 7821 | ping_exclude_vpAg=mng_info->ping_exclude_vpAg; |
| 7822 | ping_exclude_zCCP=mng_info->ping_exclude_zCCP; /* hex-encoded iCCP in zTXt */ |
| 7823 | ping_exclude_zTXt=mng_info->ping_exclude_zTXt; |
| 7824 | |
glennrp | 8d3d6e5 | 2011-04-19 04:39:51 +0000 | [diff] [blame] | 7825 | ping_preserve_colormap = mng_info->ping_preserve_colormap; |
glennrp | 0e8ea19 | 2010-12-24 18:00:33 +0000 | [diff] [blame] | 7826 | ping_need_colortype_warning = MagickFalse; |
| 7827 | |
cristy | 0d57eec | 2011-09-04 22:13:56 +0000 | [diff] [blame] | 7828 | /* Recognize the ICC sRGB profile and convert it to the sRGB chunk, |
| 7829 | * i.e., eliminate the ICC profile and set image->rendering_intent. |
| 7830 | * Note that this will not involve any changes to the actual pixels |
| 7831 | * but merely passes information to applications that read the resulting |
| 7832 | * PNG image. |
| 7833 | */ |
| 7834 | if (ping_exclude_sRGB == MagickFalse) |
| 7835 | { |
| 7836 | char |
| 7837 | *name; |
| 7838 | |
| 7839 | const StringInfo |
| 7840 | *profile; |
| 7841 | |
| 7842 | ResetImageProfileIterator(image); |
| 7843 | for (name=GetNextImageProfile(image); name != (const char *) NULL; ) |
| 7844 | { |
| 7845 | profile=GetImageProfile(image,name); |
| 7846 | |
| 7847 | if (profile != (StringInfo *) NULL) |
| 7848 | { |
| 7849 | if ((LocaleCompare(name,"ICC") == 0) || |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 7850 | (LocaleCompare(name,"ICM") == 0)) |
| 7851 | { |
glennrp | ee7b4c0 | 2011-10-04 01:21:09 +0000 | [diff] [blame] | 7852 | int |
| 7853 | icheck; |
| 7854 | |
| 7855 | /* 0: not a known sRGB profile |
| 7856 | * 1: HP-Microsoft sRGB v2 |
| 7857 | * 2: ICC sRGB v4 perceptual |
| 7858 | * 3: ICC sRGB v2 perceptual no black-compensation |
| 7859 | */ |
| 7860 | png_uint_32 |
| 7861 | check_crc[4] = {0, 0xf29e526dUL, 0xbbef7812UL, 0x427ebb21UL}, |
| 7862 | check_len[4] = {0, 3144, 60960, 3052}; |
| 7863 | |
| 7864 | png_uint_32 |
| 7865 | length, |
| 7866 | profile_crc; |
| 7867 | |
cristy | 0d57eec | 2011-09-04 22:13:56 +0000 | [diff] [blame] | 7868 | unsigned char |
| 7869 | *data; |
| 7870 | |
glennrp | 29a106e | 2011-09-06 17:11:42 +0000 | [diff] [blame] | 7871 | length=(png_uint_32) GetStringInfoLength(profile); |
| 7872 | |
glennrp | ee7b4c0 | 2011-10-04 01:21:09 +0000 | [diff] [blame] | 7873 | for (icheck=3; icheck > 0; icheck--) |
cristy | 0d57eec | 2011-09-04 22:13:56 +0000 | [diff] [blame] | 7874 | { |
glennrp | ee7b4c0 | 2011-10-04 01:21:09 +0000 | [diff] [blame] | 7875 | if (length == check_len[icheck]) |
glennrp | 29a106e | 2011-09-06 17:11:42 +0000 | [diff] [blame] | 7876 | { |
glennrp | ee7b4c0 | 2011-10-04 01:21:09 +0000 | [diff] [blame] | 7877 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 7878 | " Got a %lu-byte ICC profile (potentially sRGB)", |
| 7879 | (unsigned long) length); |
glennrp | 29a106e | 2011-09-06 17:11:42 +0000 | [diff] [blame] | 7880 | |
glennrp | ee7b4c0 | 2011-10-04 01:21:09 +0000 | [diff] [blame] | 7881 | data=GetStringInfoDatum(profile); |
| 7882 | profile_crc=crc32(0,data,length); |
glennrp | 29a106e | 2011-09-06 17:11:42 +0000 | [diff] [blame] | 7883 | |
glennrp | ee7b4c0 | 2011-10-04 01:21:09 +0000 | [diff] [blame] | 7884 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | e54cd4b | 2011-10-04 01:31:35 +0000 | [diff] [blame] | 7885 | " with crc=%8x",(unsigned int) profile_crc); |
glennrp | ee7b4c0 | 2011-10-04 01:21:09 +0000 | [diff] [blame] | 7886 | |
| 7887 | if (profile_crc == check_crc[icheck]) |
| 7888 | { |
| 7889 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 7890 | " It is sRGB."); |
| 7891 | if (image->rendering_intent==UndefinedIntent) |
| 7892 | image->rendering_intent=PerceptualIntent; |
| 7893 | break; |
| 7894 | } |
glennrp | 29a106e | 2011-09-06 17:11:42 +0000 | [diff] [blame] | 7895 | } |
glennrp | 29a106e | 2011-09-06 17:11:42 +0000 | [diff] [blame] | 7896 | } |
glennrp | ee7b4c0 | 2011-10-04 01:21:09 +0000 | [diff] [blame] | 7897 | if (icheck == 0) |
glennrp | 29a106e | 2011-09-06 17:11:42 +0000 | [diff] [blame] | 7898 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | ee7b4c0 | 2011-10-04 01:21:09 +0000 | [diff] [blame] | 7899 | " Got a %lu-byte ICC profile", |
glennrp | 29a106e | 2011-09-06 17:11:42 +0000 | [diff] [blame] | 7900 | (unsigned long) length); |
| 7901 | } |
cristy | 0d57eec | 2011-09-04 22:13:56 +0000 | [diff] [blame] | 7902 | } |
| 7903 | name=GetNextImageProfile(image); |
| 7904 | } |
| 7905 | } |
| 7906 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 7907 | number_opaque = 0; |
| 7908 | number_semitransparent = 0; |
| 7909 | number_transparent = 0; |
| 7910 | |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 7911 | if (logging != MagickFalse) |
| 7912 | { |
| 7913 | if (image->storage_class == UndefinedClass) |
| 7914 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 7915 | " storage_class=UndefinedClass"); |
| 7916 | if (image->storage_class == DirectClass) |
| 7917 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 7918 | " storage_class=DirectClass"); |
| 7919 | if (image->storage_class == PseudoClass) |
| 7920 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 7921 | " storage_class=PseudoClass"); |
| 7922 | } |
glennrp | 28af371 | 2011-04-06 18:07:30 +0000 | [diff] [blame] | 7923 | |
glennrp | 750105b | 2012-04-25 16:20:45 +0000 | [diff] [blame] | 7924 | if (image->storage_class == PseudoClass && |
glennrp | 7e65e93 | 2011-08-19 02:31:16 +0000 | [diff] [blame] | 7925 | (mng_info->write_png8 || mng_info->write_png24 || mng_info->write_png32 || |
glennrp | fd164d2 | 2013-01-26 21:10:22 +0000 | [diff] [blame] | 7926 | mng_info->write_png48 || mng_info->write_png64 || |
| 7927 | (mng_info->write_png_colortype != 1 && |
| 7928 | mng_info->write_png_colortype != 5))) |
glennrp | 7e65e93 | 2011-08-19 02:31:16 +0000 | [diff] [blame] | 7929 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 7930 | (void) SyncImage(image,exception); |
glennrp | 7e65e93 | 2011-08-19 02:31:16 +0000 | [diff] [blame] | 7931 | image->storage_class = DirectClass; |
| 7932 | } |
| 7933 | |
glennrp | c6c391a | 2011-04-27 02:23:56 +0000 | [diff] [blame] | 7934 | if (ping_preserve_colormap == MagickFalse) |
glennrp | 28af371 | 2011-04-06 18:07:30 +0000 | [diff] [blame] | 7935 | { |
glennrp | c6c391a | 2011-04-27 02:23:56 +0000 | [diff] [blame] | 7936 | if (image->storage_class != PseudoClass && image->colormap != NULL) |
| 7937 | { |
| 7938 | /* Free the bogus colormap; it can cause trouble later */ |
| 7939 | if (logging != MagickFalse) |
| 7940 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 7941 | " Freeing bogus colormap"); |
cristy | e9ac4c3 | 2011-09-26 18:47:22 +0000 | [diff] [blame] | 7942 | (void) RelinquishMagickMemory(image->colormap); |
glennrp | c6c391a | 2011-04-27 02:23:56 +0000 | [diff] [blame] | 7943 | image->colormap=NULL; |
| 7944 | } |
glennrp | 28af371 | 2011-04-06 18:07:30 +0000 | [diff] [blame] | 7945 | } |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 7946 | |
cristy | 3d9f5ba | 2012-06-26 13:37:31 +0000 | [diff] [blame] | 7947 | if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 7948 | (void) TransformImageColorspace(image,sRGBColorspace,exception); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 7949 | |
glennrp | 3241bd0 | 2010-12-12 04:36:28 +0000 | [diff] [blame] | 7950 | /* |
| 7951 | Sometimes we get PseudoClass images whose RGB values don't match |
| 7952 | the colors in the colormap. This code syncs the RGB values. |
| 7953 | */ |
| 7954 | if (image->depth <= 8 && image->taint && image->storage_class == PseudoClass) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 7955 | (void) SyncImage(image,exception); |
glennrp | 3241bd0 | 2010-12-12 04:36:28 +0000 | [diff] [blame] | 7956 | |
glennrp | a6a0663 | 2011-01-19 15:15:34 +0000 | [diff] [blame] | 7957 | #if (MAGICKCORE_QUANTUM_DEPTH == 8) |
| 7958 | if (image->depth > 8) |
| 7959 | { |
| 7960 | if (logging != MagickFalse) |
| 7961 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 7962 | " Reducing PNG bit depth to 8 since this is a Q8 build."); |
| 7963 | |
| 7964 | image->depth=8; |
| 7965 | } |
| 7966 | #endif |
| 7967 | |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 7968 | /* Respect the -depth option */ |
glennrp | cc95c3f | 2011-04-18 16:46:48 +0000 | [diff] [blame] | 7969 | if (image->depth < MAGICKCORE_QUANTUM_DEPTH) |
| 7970 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 7971 | register Quantum |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 7972 | *r; |
| 7973 | |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 7974 | if (image->depth > 8) |
| 7975 | { |
| 7976 | #if MAGICKCORE_QUANTUM_DEPTH > 16 |
| 7977 | /* Scale to 16-bit */ |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 7978 | LBR16PacketRGBO(image->background_color); |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 7979 | |
| 7980 | for (y=0; y < (ssize_t) image->rows; y++) |
| 7981 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 7982 | r=GetAuthenticPixels(image,0,y,image->columns,1,exception); |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 7983 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 7984 | if (r == (Quantum *) NULL) |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 7985 | break; |
| 7986 | |
| 7987 | for (x=0; x < (ssize_t) image->columns; x++) |
| 7988 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 7989 | LBR16PixelRGBA(r); |
| 7990 | r+=GetPixelChannels(image); |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 7991 | } |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 7992 | |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 7993 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 7994 | break; |
| 7995 | } |
| 7996 | |
| 7997 | if (image->storage_class == PseudoClass && image->colormap != NULL) |
| 7998 | { |
cristy | 3e08f11 | 2011-05-24 13:19:30 +0000 | [diff] [blame] | 7999 | for (i=0; i < (ssize_t) image->colors; i++) |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 8000 | { |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 8001 | LBR16PacketRGBO(image->colormap[i]); |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 8002 | } |
| 8003 | } |
| 8004 | #endif /* MAGICKCORE_QUANTUM_DEPTH > 16 */ |
| 8005 | } |
| 8006 | |
| 8007 | else if (image->depth > 4) |
| 8008 | { |
| 8009 | #if MAGICKCORE_QUANTUM_DEPTH > 8 |
| 8010 | /* Scale to 8-bit */ |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 8011 | LBR08PacketRGBO(image->background_color); |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 8012 | |
| 8013 | for (y=0; y < (ssize_t) image->rows; y++) |
| 8014 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8015 | r=GetAuthenticPixels(image,0,y,image->columns,1,exception); |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 8016 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8017 | if (r == (Quantum *) NULL) |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 8018 | break; |
| 8019 | |
| 8020 | for (x=0; x < (ssize_t) image->columns; x++) |
| 8021 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8022 | LBR08PixelRGBA(r); |
| 8023 | r+=GetPixelChannels(image); |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 8024 | } |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 8025 | |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 8026 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 8027 | break; |
| 8028 | } |
| 8029 | |
| 8030 | if (image->storage_class == PseudoClass && image->colormap != NULL) |
| 8031 | { |
cristy | 3e08f11 | 2011-05-24 13:19:30 +0000 | [diff] [blame] | 8032 | for (i=0; i < (ssize_t) image->colors; i++) |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 8033 | { |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 8034 | LBR08PacketRGBO(image->colormap[i]); |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 8035 | } |
| 8036 | } |
| 8037 | #endif /* MAGICKCORE_QUANTUM_DEPTH > 8 */ |
| 8038 | } |
| 8039 | else |
| 8040 | if (image->depth > 2) |
| 8041 | { |
| 8042 | /* Scale to 4-bit */ |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 8043 | LBR04PacketRGBO(image->background_color); |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 8044 | |
| 8045 | for (y=0; y < (ssize_t) image->rows; y++) |
| 8046 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8047 | r=GetAuthenticPixels(image,0,y,image->columns,1,exception); |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 8048 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8049 | if (r == (Quantum *) NULL) |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 8050 | break; |
| 8051 | |
| 8052 | for (x=0; x < (ssize_t) image->columns; x++) |
| 8053 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8054 | LBR04PixelRGBA(r); |
| 8055 | r+=GetPixelChannels(image); |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 8056 | } |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 8057 | |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 8058 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 8059 | break; |
| 8060 | } |
| 8061 | |
| 8062 | if (image->storage_class == PseudoClass && image->colormap != NULL) |
| 8063 | { |
cristy | 3e08f11 | 2011-05-24 13:19:30 +0000 | [diff] [blame] | 8064 | for (i=0; i < (ssize_t) image->colors; i++) |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 8065 | { |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 8066 | LBR04PacketRGBO(image->colormap[i]); |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 8067 | } |
| 8068 | } |
| 8069 | } |
| 8070 | |
| 8071 | else if (image->depth > 1) |
| 8072 | { |
| 8073 | /* Scale to 2-bit */ |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 8074 | LBR02PacketRGBO(image->background_color); |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 8075 | |
| 8076 | for (y=0; y < (ssize_t) image->rows; y++) |
| 8077 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8078 | r=GetAuthenticPixels(image,0,y,image->columns,1,exception); |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 8079 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8080 | if (r == (Quantum *) NULL) |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 8081 | break; |
| 8082 | |
| 8083 | for (x=0; x < (ssize_t) image->columns; x++) |
| 8084 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8085 | LBR02PixelRGBA(r); |
| 8086 | r+=GetPixelChannels(image); |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 8087 | } |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 8088 | |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 8089 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 8090 | break; |
| 8091 | } |
| 8092 | |
| 8093 | if (image->storage_class == PseudoClass && image->colormap != NULL) |
| 8094 | { |
cristy | 3e08f11 | 2011-05-24 13:19:30 +0000 | [diff] [blame] | 8095 | for (i=0; i < (ssize_t) image->colors; i++) |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 8096 | { |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 8097 | LBR02PacketRGBO(image->colormap[i]); |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 8098 | } |
| 8099 | } |
| 8100 | } |
| 8101 | else |
| 8102 | { |
| 8103 | /* Scale to 1-bit */ |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 8104 | LBR01PacketRGBO(image->background_color); |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 8105 | |
| 8106 | for (y=0; y < (ssize_t) image->rows; y++) |
| 8107 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8108 | r=GetAuthenticPixels(image,0,y,image->columns,1,exception); |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 8109 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8110 | if (r == (Quantum *) NULL) |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 8111 | break; |
| 8112 | |
| 8113 | for (x=0; x < (ssize_t) image->columns; x++) |
| 8114 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8115 | LBR01PixelRGBA(r); |
| 8116 | r+=GetPixelChannels(image); |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 8117 | } |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 8118 | |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 8119 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 8120 | break; |
| 8121 | } |
| 8122 | |
| 8123 | if (image->storage_class == PseudoClass && image->colormap != NULL) |
| 8124 | { |
cristy | 3e08f11 | 2011-05-24 13:19:30 +0000 | [diff] [blame] | 8125 | for (i=0; i < (ssize_t) image->colors; i++) |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 8126 | { |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 8127 | LBR01PacketRGBO(image->colormap[i]); |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 8128 | } |
| 8129 | } |
| 8130 | } |
glennrp | 9d0ea4d | 2011-04-22 18:35:57 +0000 | [diff] [blame] | 8131 | } |
| 8132 | |
glennrp | 67b9c1a | 2011-04-22 18:47:36 +0000 | [diff] [blame] | 8133 | /* To do: set to next higher multiple of 8 */ |
| 8134 | if (image->depth < 8) |
glennrp | 70e68a8 | 2011-04-01 22:51:14 +0000 | [diff] [blame] | 8135 | image->depth=8; |
glennrp | a6a0663 | 2011-01-19 15:15:34 +0000 | [diff] [blame] | 8136 | |
glennrp | 2b013e4 | 2010-11-24 16:55:50 +0000 | [diff] [blame] | 8137 | #if (MAGICKCORE_QUANTUM_DEPTH > 16) |
| 8138 | /* PNG does not handle depths greater than 16 so reduce it even |
| 8139 | * if lossy |
| 8140 | */ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 8141 | if (image->depth > 8) |
glennrp | 2b013e4 | 2010-11-24 16:55:50 +0000 | [diff] [blame] | 8142 | image->depth=16; |
| 8143 | #endif |
| 8144 | |
glennrp | 3faa9a3 | 2011-04-23 14:00:25 +0000 | [diff] [blame] | 8145 | #if (MAGICKCORE_QUANTUM_DEPTH > 8) |
glennrp | cc5d45b | 2012-01-06 04:06:10 +0000 | [diff] [blame] | 8146 | if (image->depth > 8) |
| 8147 | { |
| 8148 | /* To do: fill low byte properly */ |
| 8149 | image->depth=16; |
| 8150 | } |
| 8151 | |
glennrp | c722dd8 | 2011-02-24 05:13:21 +0000 | [diff] [blame] | 8152 | if (image->depth == 16 && mng_info->write_png_depth != 16) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8153 | if (mng_info->write_png8 || LosslessReduceDepthOK(image,exception) != MagickFalse) |
glennrp | 8640fb5 | 2010-11-23 15:48:26 +0000 | [diff] [blame] | 8154 | image->depth = 8; |
| 8155 | #endif |
| 8156 | |
glennrp | a8036d6 | 2012-11-04 01:46:06 +0000 | [diff] [blame] | 8157 | if (image->storage_class != PseudoClass && mng_info->write_png_colortype && |
| 8158 | (mng_info->write_png_colortype > 4 || (mng_info->write_png_depth >= 8 && |
| 8159 | mng_info->write_png_colortype < 4 && |
| 8160 | image->alpha_trait != BlendPixelTrait))) |
| 8161 | { |
| 8162 | /* Avoid the expensive BUILD_PALETTE operation if we're sure that we |
| 8163 | * are not going to need the result. |
| 8164 | */ |
glennrp | 2feb141 | 2013-01-22 15:02:16 +0000 | [diff] [blame] | 8165 | image_colors = (int) image->colors; |
| 8166 | number_opaque = (int) image->colors; |
glennrp | a8036d6 | 2012-11-04 01:46:06 +0000 | [diff] [blame] | 8167 | if (mng_info->write_png_colortype == 1 || |
| 8168 | mng_info->write_png_colortype == 5) |
| 8169 | ping_have_color=MagickFalse; |
| 8170 | else |
| 8171 | ping_have_color=MagickTrue; |
| 8172 | ping_have_non_bw=MagickFalse; |
| 8173 | |
| 8174 | if (image->alpha_trait == BlendPixelTrait) |
| 8175 | { |
| 8176 | number_transparent = 2; |
| 8177 | number_semitransparent = 1; |
| 8178 | } |
| 8179 | |
| 8180 | else |
| 8181 | { |
| 8182 | number_transparent = 0; |
| 8183 | number_semitransparent = 0; |
| 8184 | } |
| 8185 | } |
| 8186 | |
| 8187 | else |
| 8188 | { |
| 8189 | /* BUILD_PALETTE |
| 8190 | * |
| 8191 | * Normally we run this just once, but in the case of writing PNG8 |
glennrp | e9637cb | 2011-03-24 16:34:37 +0000 | [diff] [blame] | 8192 | * we reduce the transparency to binary and run again, then if there |
| 8193 | * 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] | 8194 | * RGBA palette and run again, and then to a simple 3-3-2-1 RGBA |
| 8195 | * palette. Then (To do) we take care of a final reduction that is only |
| 8196 | * needed if there are still 256 colors present and one of them has both |
| 8197 | * transparent and opaque instances. |
glennrp | c8c2f06 | 2011-02-25 19:00:33 +0000 | [diff] [blame] | 8198 | */ |
glennrp | 82b3c53 | 2011-03-22 19:20:54 +0000 | [diff] [blame] | 8199 | |
glennrp | 8ca51ad | 2011-05-12 21:22:32 +0000 | [diff] [blame] | 8200 | tried_332 = MagickFalse; |
glennrp | 82b3c53 | 2011-03-22 19:20:54 +0000 | [diff] [blame] | 8201 | tried_333 = MagickFalse; |
glennrp | d337164 | 2011-03-22 19:42:23 +0000 | [diff] [blame] | 8202 | tried_444 = MagickFalse; |
glennrp | 82b3c53 | 2011-03-22 19:20:54 +0000 | [diff] [blame] | 8203 | |
glennrp | 8ca51ad | 2011-05-12 21:22:32 +0000 | [diff] [blame] | 8204 | for (j=0; j<6; j++) |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8205 | { |
glennrp | a8036d6 | 2012-11-04 01:46:06 +0000 | [diff] [blame] | 8206 | /* |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8207 | * Sometimes we get DirectClass images that have 256 colors or fewer. |
| 8208 | * This code will build a colormap. |
| 8209 | * |
| 8210 | * Also, sometimes we get PseudoClass images with an out-of-date |
| 8211 | * colormap. This code will replace the colormap with a new one. |
| 8212 | * Sometimes we get PseudoClass images that have more than 256 colors. |
| 8213 | * This code will delete the colormap and change the image to |
| 8214 | * DirectClass. |
| 8215 | * |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 8216 | * If image->alpha_trait is MagickFalse, we ignore the alpha channel |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8217 | * even though it sometimes contains left-over non-opaque values. |
| 8218 | * |
| 8219 | * Also we gather some information (number of opaque, transparent, |
| 8220 | * and semitransparent pixels, and whether the image has any non-gray |
| 8221 | * pixels or only black-and-white pixels) that we might need later. |
| 8222 | * |
| 8223 | * Even if the user wants to force GrayAlpha or RGBA (colortype 4 or 6) |
| 8224 | * we need to check for bogus non-opaque values, at least. |
| 8225 | */ |
glennrp | 3c21811 | 2010-11-27 15:31:26 +0000 | [diff] [blame] | 8226 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8227 | int |
| 8228 | n; |
glennrp | 3c21811 | 2010-11-27 15:31:26 +0000 | [diff] [blame] | 8229 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8230 | PixelInfo |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8231 | opaque[260], |
| 8232 | semitransparent[260], |
| 8233 | transparent[260]; |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 8234 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8235 | register const Quantum |
| 8236 | *s; |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 8237 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8238 | register Quantum |
| 8239 | *q, |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 8240 | *r; |
| 8241 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8242 | if (logging != MagickFalse) |
| 8243 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8244 | " Enter BUILD_PALETTE:"); |
| 8245 | |
| 8246 | if (logging != MagickFalse) |
| 8247 | { |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 8248 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8249 | " image->columns=%.20g",(double) image->columns); |
| 8250 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8251 | " image->rows=%.20g",(double) image->rows); |
| 8252 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 8253 | " image->alpha_trait=%.20g",(double) image->alpha_trait); |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8254 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8255 | " image->depth=%.20g",(double) image->depth); |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 8256 | |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 8257 | if (image->storage_class == PseudoClass && image->colormap != NULL) |
glennrp | 7ddcc22 | 2010-12-11 05:01:05 +0000 | [diff] [blame] | 8258 | { |
| 8259 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8260 | " Original colormap:"); |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 8261 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8262 | " i (red,green,blue,alpha)"); |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 8263 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8264 | for (i=0; i < 256; i++) |
glennrp | 7ddcc22 | 2010-12-11 05:01:05 +0000 | [diff] [blame] | 8265 | { |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8266 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8267 | " %d (%d,%d,%d,%d)", |
| 8268 | (int) i, |
| 8269 | (int) image->colormap[i].red, |
| 8270 | (int) image->colormap[i].green, |
| 8271 | (int) image->colormap[i].blue, |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8272 | (int) image->colormap[i].alpha); |
glennrp | 7ddcc22 | 2010-12-11 05:01:05 +0000 | [diff] [blame] | 8273 | } |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 8274 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8275 | for (i=image->colors - 10; i < (ssize_t) image->colors; i++) |
| 8276 | { |
| 8277 | if (i > 255) |
| 8278 | { |
| 8279 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8280 | " %d (%d,%d,%d,%d)", |
| 8281 | (int) i, |
| 8282 | (int) image->colormap[i].red, |
| 8283 | (int) image->colormap[i].green, |
| 8284 | (int) image->colormap[i].blue, |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8285 | (int) image->colormap[i].alpha); |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8286 | } |
| 8287 | } |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 8288 | } |
glennrp | 7ddcc22 | 2010-12-11 05:01:05 +0000 | [diff] [blame] | 8289 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8290 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8291 | " image->colors=%d",(int) image->colors); |
glennrp | 7ddcc22 | 2010-12-11 05:01:05 +0000 | [diff] [blame] | 8292 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8293 | if (image->colors == 0) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8294 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8295 | " (zero means unknown)"); |
glennrp | d6bf161 | 2010-12-17 17:28:54 +0000 | [diff] [blame] | 8296 | |
glennrp | 8d3d6e5 | 2011-04-19 04:39:51 +0000 | [diff] [blame] | 8297 | if (ping_preserve_colormap == MagickFalse) |
| 8298 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8299 | " Regenerate the colormap"); |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8300 | } |
| 8301 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8302 | image_colors=0; |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 8303 | number_opaque = 0; |
| 8304 | number_semitransparent = 0; |
| 8305 | number_transparent = 0; |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8306 | |
| 8307 | for (y=0; y < (ssize_t) image->rows; y++) |
| 8308 | { |
| 8309 | q=GetAuthenticPixels(image,0,y,image->columns,1,exception); |
| 8310 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8311 | if (q == (Quantum *) NULL) |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8312 | break; |
| 8313 | |
| 8314 | for (x=0; x < (ssize_t) image->columns; x++) |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 8315 | { |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 8316 | if (image->alpha_trait != BlendPixelTrait || |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8317 | GetPixelAlpha(image,q) == OpaqueAlpha) |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8318 | { |
| 8319 | if (number_opaque < 259) |
| 8320 | { |
| 8321 | if (number_opaque == 0) |
| 8322 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8323 | GetPixelInfoPixel(image, q, opaque); |
| 8324 | opaque[0].alpha=OpaqueAlpha; |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8325 | number_opaque=1; |
| 8326 | } |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 8327 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8328 | for (i=0; i< (ssize_t) number_opaque; i++) |
| 8329 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8330 | if (IsPixelEquivalent(image,q, opaque+i)) |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8331 | break; |
| 8332 | } |
glennrp | 7ddcc22 | 2010-12-11 05:01:05 +0000 | [diff] [blame] | 8333 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8334 | if (i == (ssize_t) number_opaque && number_opaque < 259) |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8335 | { |
| 8336 | number_opaque++; |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8337 | GetPixelInfoPixel(image, q, opaque+i); |
| 8338 | opaque[i].alpha=OpaqueAlpha; |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8339 | } |
| 8340 | } |
| 8341 | } |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8342 | else if (GetPixelAlpha(image,q) == TransparentAlpha) |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8343 | { |
| 8344 | if (number_transparent < 259) |
| 8345 | { |
| 8346 | if (number_transparent == 0) |
| 8347 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8348 | GetPixelInfoPixel(image, q, transparent); |
| 8349 | ping_trans_color.red=(unsigned short) |
| 8350 | GetPixelRed(image,q); |
| 8351 | ping_trans_color.green=(unsigned short) |
| 8352 | GetPixelGreen(image,q); |
| 8353 | ping_trans_color.blue=(unsigned short) |
| 8354 | GetPixelBlue(image,q); |
| 8355 | ping_trans_color.gray=(unsigned short) |
cristy | 972d1c4 | 2012-07-14 23:29:14 +0000 | [diff] [blame] | 8356 | GetPixelGray(image,q); |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8357 | number_transparent = 1; |
| 8358 | } |
| 8359 | |
| 8360 | for (i=0; i< (ssize_t) number_transparent; i++) |
| 8361 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8362 | if (IsPixelEquivalent(image,q, transparent+i)) |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8363 | break; |
| 8364 | } |
| 8365 | |
| 8366 | if (i == (ssize_t) number_transparent && |
| 8367 | number_transparent < 259) |
| 8368 | { |
| 8369 | number_transparent++; |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8370 | GetPixelInfoPixel(image,q,transparent+i); |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8371 | } |
| 8372 | } |
| 8373 | } |
| 8374 | else |
| 8375 | { |
| 8376 | if (number_semitransparent < 259) |
| 8377 | { |
| 8378 | if (number_semitransparent == 0) |
| 8379 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8380 | GetPixelInfoPixel(image,q,semitransparent); |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8381 | number_semitransparent = 1; |
| 8382 | } |
| 8383 | |
| 8384 | for (i=0; i< (ssize_t) number_semitransparent; i++) |
| 8385 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8386 | if (IsPixelEquivalent(image,q, semitransparent+i) |
| 8387 | && GetPixelAlpha(image,q) == |
| 8388 | semitransparent[i].alpha) |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8389 | break; |
| 8390 | } |
| 8391 | |
| 8392 | if (i == (ssize_t) number_semitransparent && |
| 8393 | number_semitransparent < 259) |
| 8394 | { |
| 8395 | number_semitransparent++; |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8396 | GetPixelInfoPixel(image, q, semitransparent+i); |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8397 | } |
| 8398 | } |
| 8399 | } |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8400 | q+=GetPixelChannels(image); |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8401 | } |
| 8402 | } |
| 8403 | |
cristy | 4054bfb | 2011-08-29 23:41:39 +0000 | [diff] [blame] | 8404 | if (mng_info->write_png8 == MagickFalse && |
| 8405 | ping_exclude_bKGD == MagickFalse) |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8406 | { |
| 8407 | /* Add the background color to the palette, if it |
| 8408 | * isn't already there. |
| 8409 | */ |
glennrp | c6c391a | 2011-04-27 02:23:56 +0000 | [diff] [blame] | 8410 | if (logging != MagickFalse) |
| 8411 | { |
| 8412 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8413 | " Check colormap for background (%d,%d,%d)", |
| 8414 | (int) image->background_color.red, |
| 8415 | (int) image->background_color.green, |
| 8416 | (int) image->background_color.blue); |
| 8417 | } |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8418 | for (i=0; i<number_opaque; i++) |
| 8419 | { |
glennrp | ca7ad3a | 2011-04-26 04:44:54 +0000 | [diff] [blame] | 8420 | if (opaque[i].red == image->background_color.red && |
| 8421 | opaque[i].green == image->background_color.green && |
| 8422 | opaque[i].blue == image->background_color.blue) |
| 8423 | break; |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8424 | } |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8425 | if (number_opaque < 259 && i == number_opaque) |
| 8426 | { |
glennrp | 8e045c8 | 2011-04-27 16:40:27 +0000 | [diff] [blame] | 8427 | opaque[i] = image->background_color; |
glennrp | c6c391a | 2011-04-27 02:23:56 +0000 | [diff] [blame] | 8428 | ping_background.index = i; |
glennrp | 388a8c8 | 2012-06-27 13:41:51 +0000 | [diff] [blame] | 8429 | number_opaque++; |
glennrp | c6c391a | 2011-04-27 02:23:56 +0000 | [diff] [blame] | 8430 | if (logging != MagickFalse) |
| 8431 | { |
| 8432 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8433 | " background_color index is %d",(int) i); |
| 8434 | } |
| 8435 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8436 | } |
glennrp | a080bc3 | 2011-03-11 18:03:44 +0000 | [diff] [blame] | 8437 | else if (logging != MagickFalse) |
| 8438 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8439 | " No room in the colormap to add background color"); |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8440 | } |
| 8441 | |
| 8442 | image_colors=number_opaque+number_transparent+number_semitransparent; |
| 8443 | |
glennrp | a080bc3 | 2011-03-11 18:03:44 +0000 | [diff] [blame] | 8444 | if (mng_info->write_png8 != MagickFalse && image_colors > 256) |
| 8445 | { |
| 8446 | /* No room for the background color; remove it. */ |
| 8447 | number_opaque--; |
| 8448 | image_colors--; |
| 8449 | } |
| 8450 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8451 | if (logging != MagickFalse) |
| 8452 | { |
| 8453 | if (image_colors > 256) |
| 8454 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8455 | " image has more than 256 colors"); |
| 8456 | |
| 8457 | else |
| 8458 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8459 | " image has %d colors",image_colors); |
| 8460 | } |
| 8461 | |
glennrp | 8d3d6e5 | 2011-04-19 04:39:51 +0000 | [diff] [blame] | 8462 | if (ping_preserve_colormap != MagickFalse) |
| 8463 | break; |
glennrp | 8d3d6e5 | 2011-04-19 04:39:51 +0000 | [diff] [blame] | 8464 | |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 8465 | if (mng_info->write_png_colortype != 7) /* We won't need this info */ |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8466 | { |
| 8467 | ping_have_color=MagickFalse; |
| 8468 | ping_have_non_bw=MagickFalse; |
| 8469 | |
glennrp | 98b9577 | 2012-11-29 01:32:00 +0000 | [diff] [blame] | 8470 | if ((IssRGBCompatibleColorspace(image->colorspace) == MagickFalse) || |
| 8471 | (IssRGBColorspace(image->colorspace) != MagickFalse)) |
glennrp | 0fa2580 | 2012-07-20 14:01:06 +0000 | [diff] [blame] | 8472 | { |
| 8473 | ping_have_color=MagickTrue; |
glennrp | 98b9577 | 2012-11-29 01:32:00 +0000 | [diff] [blame] | 8474 | ping_have_non_bw=MagickTrue; |
glennrp | 0fa2580 | 2012-07-20 14:01:06 +0000 | [diff] [blame] | 8475 | } |
| 8476 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8477 | if(image_colors > 256) |
| 8478 | { |
| 8479 | for (y=0; y < (ssize_t) image->rows; y++) |
| 8480 | { |
| 8481 | q=GetAuthenticPixels(image,0,y,image->columns,1,exception); |
| 8482 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8483 | if (q == (Quantum *) NULL) |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8484 | break; |
| 8485 | |
glennrp | e5e6b80 | 2011-07-20 14:44:40 +0000 | [diff] [blame] | 8486 | s=q; |
| 8487 | for (x=0; x < (ssize_t) image->columns; x++) |
| 8488 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8489 | if (GetPixelRed(image,s) != GetPixelGreen(image,s) || |
| 8490 | GetPixelRed(image,s) != GetPixelBlue(image,s)) |
glennrp | e5e6b80 | 2011-07-20 14:44:40 +0000 | [diff] [blame] | 8491 | { |
| 8492 | ping_have_color=MagickTrue; |
| 8493 | ping_have_non_bw=MagickTrue; |
| 8494 | break; |
| 8495 | } |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8496 | s+=GetPixelChannels(image); |
glennrp | e5e6b80 | 2011-07-20 14:44:40 +0000 | [diff] [blame] | 8497 | } |
| 8498 | |
| 8499 | if (ping_have_color != MagickFalse) |
| 8500 | break; |
| 8501 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8502 | /* Worst case is black-and-white; we are looking at every |
| 8503 | * pixel twice. |
| 8504 | */ |
| 8505 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8506 | if (ping_have_non_bw == MagickFalse) |
| 8507 | { |
| 8508 | s=q; |
| 8509 | for (x=0; x < (ssize_t) image->columns; x++) |
| 8510 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8511 | if (GetPixelRed(image,s) != 0 && |
| 8512 | GetPixelRed(image,s) != QuantumRange) |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8513 | { |
| 8514 | ping_have_non_bw=MagickTrue; |
glennrp | e5e6b80 | 2011-07-20 14:44:40 +0000 | [diff] [blame] | 8515 | break; |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8516 | } |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8517 | s+=GetPixelChannels(image); |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8518 | } |
glennrp | e5e6b80 | 2011-07-20 14:44:40 +0000 | [diff] [blame] | 8519 | } |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8520 | } |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 8521 | } |
| 8522 | } |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8523 | |
| 8524 | if (image_colors < 257) |
| 8525 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8526 | PixelInfo |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8527 | colormap[260]; |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 8528 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8529 | /* |
| 8530 | * Initialize image colormap. |
glennrp | 97fd3d0 | 2011-02-23 14:58:06 +0000 | [diff] [blame] | 8531 | */ |
| 8532 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8533 | if (logging != MagickFalse) |
| 8534 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8535 | " Sort the new colormap"); |
glennrp | 8d57966 | 2011-02-23 02:05:02 +0000 | [diff] [blame] | 8536 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8537 | /* Sort palette, transparent first */; |
| 8538 | |
| 8539 | n = 0; |
| 8540 | |
| 8541 | for (i=0; i<number_transparent; i++) |
| 8542 | colormap[n++] = transparent[i]; |
| 8543 | |
| 8544 | for (i=0; i<number_semitransparent; i++) |
| 8545 | colormap[n++] = semitransparent[i]; |
| 8546 | |
| 8547 | for (i=0; i<number_opaque; i++) |
| 8548 | colormap[n++] = opaque[i]; |
| 8549 | |
glennrp | c6c391a | 2011-04-27 02:23:56 +0000 | [diff] [blame] | 8550 | ping_background.index += |
| 8551 | (number_transparent + number_semitransparent); |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 8552 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8553 | /* image_colors < 257; search the colormap instead of the pixels |
| 8554 | * to get ping_have_color and ping_have_non_bw |
| 8555 | */ |
| 8556 | for (i=0; i<n; i++) |
| 8557 | { |
| 8558 | if (ping_have_color == MagickFalse) |
glennrp | 8d57966 | 2011-02-23 02:05:02 +0000 | [diff] [blame] | 8559 | { |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8560 | if (colormap[i].red != colormap[i].green || |
| 8561 | colormap[i].red != colormap[i].blue) |
| 8562 | { |
| 8563 | ping_have_color=MagickTrue; |
| 8564 | ping_have_non_bw=MagickTrue; |
| 8565 | break; |
| 8566 | } |
| 8567 | } |
| 8568 | |
| 8569 | if (ping_have_non_bw == MagickFalse) |
| 8570 | { |
| 8571 | if (colormap[i].red != 0 && colormap[i].red != QuantumRange) |
glennrp | 8d57966 | 2011-02-23 02:05:02 +0000 | [diff] [blame] | 8572 | ping_have_non_bw=MagickTrue; |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 8573 | } |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 8574 | } |
| 8575 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8576 | if ((mng_info->ping_exclude_tRNS == MagickFalse || |
| 8577 | (number_transparent == 0 && number_semitransparent == 0)) && |
| 8578 | (((mng_info->write_png_colortype-1) == |
| 8579 | PNG_COLOR_TYPE_PALETTE) || |
| 8580 | (mng_info->write_png_colortype == 0))) |
| 8581 | { |
glennrp | 6185c53 | 2011-01-14 17:58:40 +0000 | [diff] [blame] | 8582 | if (logging != MagickFalse) |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 8583 | { |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8584 | if (n != (ssize_t) image_colors) |
| 8585 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8586 | " image_colors (%d) and n (%d) don't match", |
| 8587 | image_colors, n); |
glennrp | 6185c53 | 2011-01-14 17:58:40 +0000 | [diff] [blame] | 8588 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8589 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8590 | " AcquireImageColormap"); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 8591 | } |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 8592 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8593 | image->colors = image_colors; |
| 8594 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8595 | if (AcquireImageColormap(image,image_colors,exception) == |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8596 | MagickFalse) |
glennrp | 3faa9a3 | 2011-04-23 14:00:25 +0000 | [diff] [blame] | 8597 | ThrowWriterException(ResourceLimitError, |
| 8598 | "MemoryAllocationFailed"); |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8599 | |
| 8600 | for (i=0; i< (ssize_t) image_colors; i++) |
| 8601 | image->colormap[i] = colormap[i]; |
| 8602 | |
| 8603 | if (logging != MagickFalse) |
glennrp | 6185c53 | 2011-01-14 17:58:40 +0000 | [diff] [blame] | 8604 | { |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8605 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8606 | " image->colors=%d (%d)", |
| 8607 | (int) image->colors, image_colors); |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 8608 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8609 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8610 | " Update the pixel indexes"); |
| 8611 | } |
glennrp | 6185c53 | 2011-01-14 17:58:40 +0000 | [diff] [blame] | 8612 | |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 8613 | /* Sync the pixel indices with the new colormap */ |
| 8614 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8615 | for (y=0; y < (ssize_t) image->rows; y++) |
| 8616 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8617 | q=GetAuthenticPixels(image,0,y,image->columns,1,exception); |
glennrp | 6185c53 | 2011-01-14 17:58:40 +0000 | [diff] [blame] | 8618 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8619 | if (q == (Quantum *) NULL) |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8620 | break; |
glennrp | 6185c53 | 2011-01-14 17:58:40 +0000 | [diff] [blame] | 8621 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8622 | for (x=0; x < (ssize_t) image->columns; x++) |
glennrp | 6185c53 | 2011-01-14 17:58:40 +0000 | [diff] [blame] | 8623 | { |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8624 | for (i=0; i< (ssize_t) image_colors; i++) |
glennrp | 6185c53 | 2011-01-14 17:58:40 +0000 | [diff] [blame] | 8625 | { |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 8626 | if ((image->alpha_trait != BlendPixelTrait || |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8627 | image->colormap[i].alpha == GetPixelAlpha(image,q)) && |
| 8628 | image->colormap[i].red == GetPixelRed(image,q) && |
| 8629 | image->colormap[i].green == GetPixelGreen(image,q) && |
| 8630 | image->colormap[i].blue == GetPixelBlue(image,q)) |
glennrp | 6185c53 | 2011-01-14 17:58:40 +0000 | [diff] [blame] | 8631 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8632 | SetPixelIndex(image,i,q); |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8633 | break; |
glennrp | 6185c53 | 2011-01-14 17:58:40 +0000 | [diff] [blame] | 8634 | } |
glennrp | 6185c53 | 2011-01-14 17:58:40 +0000 | [diff] [blame] | 8635 | } |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8636 | q+=GetPixelChannels(image); |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8637 | } |
glennrp | 6185c53 | 2011-01-14 17:58:40 +0000 | [diff] [blame] | 8638 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8639 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 8640 | break; |
| 8641 | } |
| 8642 | } |
| 8643 | } |
| 8644 | |
| 8645 | if (logging != MagickFalse) |
| 8646 | { |
| 8647 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8648 | " image->colors=%d", (int) image->colors); |
| 8649 | |
| 8650 | if (image->colormap != NULL) |
| 8651 | { |
| 8652 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8653 | " i (red,green,blue,alpha)"); |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8654 | |
| 8655 | for (i=0; i < (ssize_t) image->colors; i++) |
| 8656 | { |
cristy | 7298848 | 2011-03-29 16:34:38 +0000 | [diff] [blame] | 8657 | if (i < 300 || i >= (ssize_t) image->colors - 10) |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8658 | { |
| 8659 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8660 | " %d (%d,%d,%d,%d)", |
| 8661 | (int) i, |
| 8662 | (int) image->colormap[i].red, |
| 8663 | (int) image->colormap[i].green, |
| 8664 | (int) image->colormap[i].blue, |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8665 | (int) image->colormap[i].alpha); |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8666 | } |
glennrp | 6185c53 | 2011-01-14 17:58:40 +0000 | [diff] [blame] | 8667 | } |
| 8668 | } |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 8669 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8670 | if (number_transparent < 257) |
| 8671 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8672 | " number_transparent = %d", |
| 8673 | number_transparent); |
| 8674 | else |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 8675 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8676 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8677 | " number_transparent > 256"); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 8678 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8679 | if (number_opaque < 257) |
| 8680 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8681 | " number_opaque = %d", |
| 8682 | number_opaque); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 8683 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8684 | else |
| 8685 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8686 | " number_opaque > 256"); |
glennrp | 6185c53 | 2011-01-14 17:58:40 +0000 | [diff] [blame] | 8687 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8688 | if (number_semitransparent < 257) |
| 8689 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8690 | " number_semitransparent = %d", |
| 8691 | number_semitransparent); |
glennrp | 6185c53 | 2011-01-14 17:58:40 +0000 | [diff] [blame] | 8692 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8693 | else |
| 8694 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8695 | " number_semitransparent > 256"); |
glennrp | a6a0663 | 2011-01-19 15:15:34 +0000 | [diff] [blame] | 8696 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8697 | if (ping_have_non_bw == MagickFalse) |
| 8698 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8699 | " All pixels and the background are black or white"); |
glennrp | a6a0663 | 2011-01-19 15:15:34 +0000 | [diff] [blame] | 8700 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8701 | else if (ping_have_color == MagickFalse) |
| 8702 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8703 | " All pixels and the background are gray"); |
| 8704 | |
| 8705 | else |
| 8706 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8707 | " At least one pixel or the background is non-gray"); |
glennrp | 6185c53 | 2011-01-14 17:58:40 +0000 | [diff] [blame] | 8708 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 8709 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8710 | " Exit BUILD_PALETTE:"); |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8711 | } |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 8712 | |
glennrp | c8c2f06 | 2011-02-25 19:00:33 +0000 | [diff] [blame] | 8713 | if (mng_info->write_png8 == MagickFalse) |
| 8714 | break; |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 8715 | |
glennrp | c8c2f06 | 2011-02-25 19:00:33 +0000 | [diff] [blame] | 8716 | /* Make any reductions necessary for the PNG8 format */ |
| 8717 | if (image_colors <= 256 && |
| 8718 | image_colors != 0 && image->colormap != NULL && |
| 8719 | number_semitransparent == 0 && |
| 8720 | number_transparent <= 1) |
| 8721 | break; |
| 8722 | |
| 8723 | /* PNG8 can't have semitransparent colors so we threshold the |
glennrp | 130fc45 | 2011-08-20 03:43:18 +0000 | [diff] [blame] | 8724 | * opacity to 0 or OpaqueOpacity, and PNG8 can only have one |
| 8725 | * transparent color so if more than one is transparent we merge |
| 8726 | * them into image->background_color. |
glennrp | c8c2f06 | 2011-02-25 19:00:33 +0000 | [diff] [blame] | 8727 | */ |
glennrp | 130fc45 | 2011-08-20 03:43:18 +0000 | [diff] [blame] | 8728 | if (number_semitransparent != 0 || number_transparent > 1) |
glennrp | c8c2f06 | 2011-02-25 19:00:33 +0000 | [diff] [blame] | 8729 | { |
| 8730 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8731 | " Thresholding the alpha channel to binary"); |
| 8732 | |
| 8733 | for (y=0; y < (ssize_t) image->rows; y++) |
| 8734 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8735 | r=GetAuthenticPixels(image,0,y,image->columns,1,exception); |
glennrp | c8c2f06 | 2011-02-25 19:00:33 +0000 | [diff] [blame] | 8736 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8737 | if (r == (Quantum *) NULL) |
glennrp | c8c2f06 | 2011-02-25 19:00:33 +0000 | [diff] [blame] | 8738 | break; |
| 8739 | |
| 8740 | for (x=0; x < (ssize_t) image->columns; x++) |
| 8741 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8742 | if (GetPixelAlpha(image,r) < OpaqueAlpha/2) |
glennrp | 8ca51ad | 2011-05-12 21:22:32 +0000 | [diff] [blame] | 8743 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8744 | SetPixelInfoPixel(image,&image->background_color,r); |
| 8745 | SetPixelAlpha(image,TransparentAlpha,r); |
glennrp | 8ca51ad | 2011-05-12 21:22:32 +0000 | [diff] [blame] | 8746 | } |
| 8747 | else |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8748 | SetPixelAlpha(image,OpaqueAlpha,r); |
| 8749 | r+=GetPixelChannels(image); |
glennrp | c8c2f06 | 2011-02-25 19:00:33 +0000 | [diff] [blame] | 8750 | } |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 8751 | |
glennrp | c8c2f06 | 2011-02-25 19:00:33 +0000 | [diff] [blame] | 8752 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 8753 | break; |
| 8754 | |
| 8755 | if (image_colors != 0 && image_colors <= 256 && |
| 8756 | image->colormap != NULL) |
| 8757 | for (i=0; i<image_colors; i++) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8758 | image->colormap[i].alpha = |
| 8759 | (image->colormap[i].alpha > TransparentAlpha/2 ? |
| 8760 | TransparentAlpha : OpaqueAlpha); |
glennrp | c8c2f06 | 2011-02-25 19:00:33 +0000 | [diff] [blame] | 8761 | } |
| 8762 | continue; |
| 8763 | } |
| 8764 | |
| 8765 | /* 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] | 8766 | * background color to the 4-4-4-1, 3-3-3-1 or 3-3-2-1 palette. If the |
| 8767 | * image is mostly gray, the 4-4-4-1 palette is likely to end up with 256 |
| 8768 | * colors or less. |
glennrp | c8c2f06 | 2011-02-25 19:00:33 +0000 | [diff] [blame] | 8769 | */ |
glennrp | d337164 | 2011-03-22 19:42:23 +0000 | [diff] [blame] | 8770 | if (tried_444 == MagickFalse && (image_colors == 0 || image_colors > 256)) |
| 8771 | { |
| 8772 | if (logging != MagickFalse) |
| 8773 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8774 | " Quantizing the background color to 4-4-4"); |
| 8775 | |
| 8776 | tried_444 = MagickTrue; |
| 8777 | |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 8778 | LBR04PacketRGB(image->background_color); |
glennrp | d337164 | 2011-03-22 19:42:23 +0000 | [diff] [blame] | 8779 | |
| 8780 | if (logging != MagickFalse) |
| 8781 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8782 | " Quantizing the pixel colors to 4-4-4"); |
| 8783 | |
| 8784 | if (image->colormap == NULL) |
| 8785 | { |
| 8786 | for (y=0; y < (ssize_t) image->rows; y++) |
| 8787 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8788 | r=GetAuthenticPixels(image,0,y,image->columns,1,exception); |
glennrp | d337164 | 2011-03-22 19:42:23 +0000 | [diff] [blame] | 8789 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8790 | if (r == (Quantum *) NULL) |
glennrp | d337164 | 2011-03-22 19:42:23 +0000 | [diff] [blame] | 8791 | break; |
| 8792 | |
| 8793 | for (x=0; x < (ssize_t) image->columns; x++) |
| 8794 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8795 | if (GetPixelAlpha(image,r) == OpaqueAlpha) |
glennrp | 54cf797 | 2011-08-06 14:28:09 +0000 | [diff] [blame] | 8796 | LBR04PixelRGB(r); |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8797 | r+=GetPixelChannels(image); |
glennrp | d337164 | 2011-03-22 19:42:23 +0000 | [diff] [blame] | 8798 | } |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 8799 | |
glennrp | d337164 | 2011-03-22 19:42:23 +0000 | [diff] [blame] | 8800 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 8801 | break; |
| 8802 | } |
| 8803 | } |
| 8804 | |
| 8805 | else /* Should not reach this; colormap already exists and |
| 8806 | must be <= 256 */ |
| 8807 | { |
| 8808 | if (logging != MagickFalse) |
| 8809 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8810 | " Quantizing the colormap to 4-4-4"); |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 8811 | |
glennrp | d337164 | 2011-03-22 19:42:23 +0000 | [diff] [blame] | 8812 | for (i=0; i<image_colors; i++) |
| 8813 | { |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 8814 | LBR04PacketRGB(image->colormap[i]); |
glennrp | d337164 | 2011-03-22 19:42:23 +0000 | [diff] [blame] | 8815 | } |
| 8816 | } |
| 8817 | continue; |
| 8818 | } |
| 8819 | |
glennrp | 82b3c53 | 2011-03-22 19:20:54 +0000 | [diff] [blame] | 8820 | if (tried_333 == MagickFalse && (image_colors == 0 || image_colors > 256)) |
| 8821 | { |
| 8822 | if (logging != MagickFalse) |
| 8823 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8824 | " Quantizing the background color to 3-3-3"); |
| 8825 | |
| 8826 | tried_333 = MagickTrue; |
| 8827 | |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 8828 | LBR03PacketRGB(image->background_color); |
glennrp | 82b3c53 | 2011-03-22 19:20:54 +0000 | [diff] [blame] | 8829 | |
| 8830 | if (logging != MagickFalse) |
| 8831 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | e9637cb | 2011-03-24 16:34:37 +0000 | [diff] [blame] | 8832 | " Quantizing the pixel colors to 3-3-3-1"); |
glennrp | 82b3c53 | 2011-03-22 19:20:54 +0000 | [diff] [blame] | 8833 | |
| 8834 | if (image->colormap == NULL) |
| 8835 | { |
| 8836 | for (y=0; y < (ssize_t) image->rows; y++) |
| 8837 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8838 | r=GetAuthenticPixels(image,0,y,image->columns,1,exception); |
glennrp | 82b3c53 | 2011-03-22 19:20:54 +0000 | [diff] [blame] | 8839 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8840 | if (r == (Quantum *) NULL) |
glennrp | 82b3c53 | 2011-03-22 19:20:54 +0000 | [diff] [blame] | 8841 | break; |
| 8842 | |
| 8843 | for (x=0; x < (ssize_t) image->columns; x++) |
| 8844 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8845 | if (GetPixelAlpha(image,r) == OpaqueAlpha) |
| 8846 | LBR03RGB(r); |
| 8847 | r+=GetPixelChannels(image); |
glennrp | 82b3c53 | 2011-03-22 19:20:54 +0000 | [diff] [blame] | 8848 | } |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 8849 | |
glennrp | 82b3c53 | 2011-03-22 19:20:54 +0000 | [diff] [blame] | 8850 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 8851 | break; |
| 8852 | } |
| 8853 | } |
| 8854 | |
| 8855 | else /* Should not reach this; colormap already exists and |
| 8856 | must be <= 256 */ |
| 8857 | { |
| 8858 | if (logging != MagickFalse) |
| 8859 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | e9637cb | 2011-03-24 16:34:37 +0000 | [diff] [blame] | 8860 | " Quantizing the colormap to 3-3-3-1"); |
glennrp | 82b3c53 | 2011-03-22 19:20:54 +0000 | [diff] [blame] | 8861 | for (i=0; i<image_colors; i++) |
| 8862 | { |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 8863 | LBR03PacketRGB(image->colormap[i]); |
glennrp | 82b3c53 | 2011-03-22 19:20:54 +0000 | [diff] [blame] | 8864 | } |
glennrp | d337164 | 2011-03-22 19:42:23 +0000 | [diff] [blame] | 8865 | } |
| 8866 | continue; |
glennrp | 82b3c53 | 2011-03-22 19:20:54 +0000 | [diff] [blame] | 8867 | } |
glennrp | c8c2f06 | 2011-02-25 19:00:33 +0000 | [diff] [blame] | 8868 | |
glennrp | 8ca51ad | 2011-05-12 21:22:32 +0000 | [diff] [blame] | 8869 | if (tried_332 == MagickFalse && (image_colors == 0 || image_colors > 256)) |
glennrp | c8c2f06 | 2011-02-25 19:00:33 +0000 | [diff] [blame] | 8870 | { |
| 8871 | if (logging != MagickFalse) |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 8872 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | c8c2f06 | 2011-02-25 19:00:33 +0000 | [diff] [blame] | 8873 | " Quantizing the background color to 3-3-2"); |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 8874 | |
glennrp | 8ca51ad | 2011-05-12 21:22:32 +0000 | [diff] [blame] | 8875 | tried_332 = MagickTrue; |
| 8876 | |
glennrp | 3faa9a3 | 2011-04-23 14:00:25 +0000 | [diff] [blame] | 8877 | /* Red and green were already done so we only quantize the blue |
| 8878 | * channel |
| 8879 | */ |
| 8880 | |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 8881 | LBR02PacketBlue(image->background_color); |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 8882 | |
glennrp | c8c2f06 | 2011-02-25 19:00:33 +0000 | [diff] [blame] | 8883 | if (logging != MagickFalse) |
| 8884 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | e9637cb | 2011-03-24 16:34:37 +0000 | [diff] [blame] | 8885 | " Quantizing the pixel colors to 3-3-2-1"); |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 8886 | |
glennrp | c8c2f06 | 2011-02-25 19:00:33 +0000 | [diff] [blame] | 8887 | if (image->colormap == NULL) |
| 8888 | { |
| 8889 | for (y=0; y < (ssize_t) image->rows; y++) |
| 8890 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8891 | r=GetAuthenticPixels(image,0,y,image->columns,1,exception); |
glennrp | c8c2f06 | 2011-02-25 19:00:33 +0000 | [diff] [blame] | 8892 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8893 | if (r == (Quantum *) NULL) |
glennrp | c8c2f06 | 2011-02-25 19:00:33 +0000 | [diff] [blame] | 8894 | break; |
| 8895 | |
| 8896 | for (x=0; x < (ssize_t) image->columns; x++) |
| 8897 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8898 | if (GetPixelAlpha(image,r) == OpaqueAlpha) |
glennrp | 54cf797 | 2011-08-06 14:28:09 +0000 | [diff] [blame] | 8899 | LBR02PixelBlue(r); |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8900 | r+=GetPixelChannels(image); |
glennrp | c8c2f06 | 2011-02-25 19:00:33 +0000 | [diff] [blame] | 8901 | } |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 8902 | |
glennrp | c8c2f06 | 2011-02-25 19:00:33 +0000 | [diff] [blame] | 8903 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 8904 | break; |
| 8905 | } |
| 8906 | } |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 8907 | |
glennrp | c8c2f06 | 2011-02-25 19:00:33 +0000 | [diff] [blame] | 8908 | else /* Should not reach this; colormap already exists and |
| 8909 | must be <= 256 */ |
| 8910 | { |
| 8911 | if (logging != MagickFalse) |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 8912 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | e9637cb | 2011-03-24 16:34:37 +0000 | [diff] [blame] | 8913 | " Quantizing the colormap to 3-3-2-1"); |
glennrp | c8c2f06 | 2011-02-25 19:00:33 +0000 | [diff] [blame] | 8914 | for (i=0; i<image_colors; i++) |
| 8915 | { |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 8916 | LBR02PacketBlue(image->colormap[i]); |
glennrp | c8c2f06 | 2011-02-25 19:00:33 +0000 | [diff] [blame] | 8917 | } |
| 8918 | } |
| 8919 | continue; |
| 8920 | } |
| 8921 | break; |
glennrp | 8ca51ad | 2011-05-12 21:22:32 +0000 | [diff] [blame] | 8922 | |
| 8923 | if (image_colors == 0 || image_colors > 256) |
| 8924 | { |
| 8925 | /* Take care of special case with 256 colors + 1 transparent |
| 8926 | * color. We don't need to quantize to 2-3-2-1; we only need to |
| 8927 | * eliminate one color, so we'll merge the two darkest red |
| 8928 | * colors (0x49, 0, 0) -> (0x24, 0, 0). |
| 8929 | */ |
| 8930 | if (ScaleQuantumToChar(image->background_color.red) == 0x49 && |
| 8931 | ScaleQuantumToChar(image->background_color.green) == 0x00 && |
| 8932 | ScaleQuantumToChar(image->background_color.blue) == 0x00) |
| 8933 | { |
| 8934 | image->background_color.red=ScaleCharToQuantum(0x24); |
| 8935 | } |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 8936 | |
glennrp | 8ca51ad | 2011-05-12 21:22:32 +0000 | [diff] [blame] | 8937 | if (image->colormap == NULL) |
| 8938 | { |
| 8939 | for (y=0; y < (ssize_t) image->rows; y++) |
| 8940 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8941 | r=GetAuthenticPixels(image,0,y,image->columns,1,exception); |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 8942 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8943 | if (r == (Quantum *) NULL) |
glennrp | 8ca51ad | 2011-05-12 21:22:32 +0000 | [diff] [blame] | 8944 | break; |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 8945 | |
glennrp | 8ca51ad | 2011-05-12 21:22:32 +0000 | [diff] [blame] | 8946 | for (x=0; x < (ssize_t) image->columns; x++) |
| 8947 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8948 | if (ScaleQuantumToChar(GetPixelRed(image,r)) == 0x49 && |
| 8949 | ScaleQuantumToChar(GetPixelGreen(image,r)) == 0x00 && |
| 8950 | ScaleQuantumToChar(GetPixelBlue(image,r)) == 0x00 && |
| 8951 | GetPixelAlpha(image,r) == OpaqueAlpha) |
glennrp | 8ca51ad | 2011-05-12 21:22:32 +0000 | [diff] [blame] | 8952 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8953 | SetPixelRed(image,ScaleCharToQuantum(0x24),r); |
glennrp | 8ca51ad | 2011-05-12 21:22:32 +0000 | [diff] [blame] | 8954 | } |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8955 | r+=GetPixelChannels(image); |
glennrp | 8ca51ad | 2011-05-12 21:22:32 +0000 | [diff] [blame] | 8956 | } |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 8957 | |
glennrp | 8ca51ad | 2011-05-12 21:22:32 +0000 | [diff] [blame] | 8958 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 8959 | break; |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 8960 | |
glennrp | 8ca51ad | 2011-05-12 21:22:32 +0000 | [diff] [blame] | 8961 | } |
| 8962 | } |
| 8963 | |
| 8964 | else |
| 8965 | { |
| 8966 | for (i=0; i<image_colors; i++) |
| 8967 | { |
| 8968 | if (ScaleQuantumToChar(image->colormap[i].red) == 0x49 && |
| 8969 | ScaleQuantumToChar(image->colormap[i].green) == 0x00 && |
| 8970 | ScaleQuantumToChar(image->colormap[i].blue) == 0x00) |
| 8971 | { |
| 8972 | image->colormap[i].red=ScaleCharToQuantum(0x24); |
| 8973 | } |
| 8974 | } |
| 8975 | } |
| 8976 | } |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8977 | } |
glennrp | a8036d6 | 2012-11-04 01:46:06 +0000 | [diff] [blame] | 8978 | } |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 8979 | /* END OF BUILD_PALETTE */ |
glennrp | 3c21811 | 2010-11-27 15:31:26 +0000 | [diff] [blame] | 8980 | |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 8981 | /* If we are excluding the tRNS chunk and there is transparency, |
| 8982 | * then we must write a Gray-Alpha (color-type 4) or RGBA (color-type 6) |
| 8983 | * PNG. |
glennrp | 8d57966 | 2011-02-23 02:05:02 +0000 | [diff] [blame] | 8984 | */ |
glennrp | 0e8ea19 | 2010-12-24 18:00:33 +0000 | [diff] [blame] | 8985 | if (mng_info->ping_exclude_tRNS != MagickFalse && |
| 8986 | (number_transparent != 0 || number_semitransparent != 0)) |
| 8987 | { |
glennrp | d17915c | 2011-04-29 14:24:22 +0000 | [diff] [blame] | 8988 | unsigned int colortype=mng_info->write_png_colortype; |
glennrp | 0e8ea19 | 2010-12-24 18:00:33 +0000 | [diff] [blame] | 8989 | |
| 8990 | if (ping_have_color == MagickFalse) |
| 8991 | mng_info->write_png_colortype = 5; |
| 8992 | |
| 8993 | else |
| 8994 | mng_info->write_png_colortype = 7; |
| 8995 | |
glennrp | 8d57966 | 2011-02-23 02:05:02 +0000 | [diff] [blame] | 8996 | if (colortype != 0 && |
glennrp | d17915c | 2011-04-29 14:24:22 +0000 | [diff] [blame] | 8997 | mng_info->write_png_colortype != colortype) |
glennrp | 0e8ea19 | 2010-12-24 18:00:33 +0000 | [diff] [blame] | 8998 | ping_need_colortype_warning=MagickTrue; |
glennrp | 0b206f5 | 2011-01-07 04:55:32 +0000 | [diff] [blame] | 8999 | |
glennrp | 0e8ea19 | 2010-12-24 18:00:33 +0000 | [diff] [blame] | 9000 | } |
| 9001 | |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 9002 | /* See if cheap transparency is possible. It is only possible |
| 9003 | * when there is a single transparent color, no semitransparent |
| 9004 | * color, and no opaque color that has the same RGB components |
glennrp | 5a39f37 | 2011-02-25 04:52:16 +0000 | [diff] [blame] | 9005 | * as the transparent color. We only need this information if |
| 9006 | * we are writing a PNG with colortype 0 or 2, and we have not |
| 9007 | * excluded the tRNS chunk. |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 9008 | */ |
glennrp | 5a39f37 | 2011-02-25 04:52:16 +0000 | [diff] [blame] | 9009 | if (number_transparent == 1 && |
| 9010 | mng_info->write_png_colortype < 4) |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 9011 | { |
| 9012 | ping_have_cheap_transparency = MagickTrue; |
| 9013 | |
| 9014 | if (number_semitransparent != 0) |
| 9015 | ping_have_cheap_transparency = MagickFalse; |
| 9016 | |
| 9017 | else if (image_colors == 0 || image_colors > 256 || |
| 9018 | image->colormap == NULL) |
| 9019 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 9020 | register const Quantum |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 9021 | *q; |
| 9022 | |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 9023 | for (y=0; y < (ssize_t) image->rows; y++) |
| 9024 | { |
| 9025 | q=GetVirtualPixels(image,0,y,image->columns,1, exception); |
| 9026 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 9027 | if (q == (Quantum *) NULL) |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 9028 | break; |
| 9029 | |
| 9030 | for (x=0; x < (ssize_t) image->columns; x++) |
| 9031 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 9032 | if (GetPixelAlpha(image,q) != TransparentAlpha && |
| 9033 | (unsigned short) GetPixelRed(image,q) == |
| 9034 | ping_trans_color.red && |
| 9035 | (unsigned short) GetPixelGreen(image,q) == |
| 9036 | ping_trans_color.green && |
| 9037 | (unsigned short) GetPixelBlue(image,q) == |
| 9038 | ping_trans_color.blue) |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 9039 | { |
| 9040 | ping_have_cheap_transparency = MagickFalse; |
| 9041 | break; |
| 9042 | } |
| 9043 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 9044 | q+=GetPixelChannels(image); |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 9045 | } |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 9046 | |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 9047 | if (ping_have_cheap_transparency == MagickFalse) |
| 9048 | break; |
| 9049 | } |
| 9050 | } |
| 9051 | else |
| 9052 | { |
glennrp | 67b9c1a | 2011-04-22 18:47:36 +0000 | [diff] [blame] | 9053 | /* Assuming that image->colormap[0] is the one transparent color |
| 9054 | * and that all others are opaque. |
| 9055 | */ |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 9056 | if (image_colors > 1) |
glennrp | 67b9c1a | 2011-04-22 18:47:36 +0000 | [diff] [blame] | 9057 | for (i=1; i<image_colors; i++) |
| 9058 | if (image->colormap[i].red == image->colormap[0].red && |
| 9059 | image->colormap[i].green == image->colormap[0].green && |
| 9060 | image->colormap[i].blue == image->colormap[0].blue) |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 9061 | { |
glennrp | 67b9c1a | 2011-04-22 18:47:36 +0000 | [diff] [blame] | 9062 | ping_have_cheap_transparency = MagickFalse; |
| 9063 | break; |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 9064 | } |
| 9065 | } |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 9066 | |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 9067 | if (logging != MagickFalse) |
| 9068 | { |
| 9069 | if (ping_have_cheap_transparency == MagickFalse) |
| 9070 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9071 | " Cheap transparency is not possible."); |
| 9072 | |
| 9073 | else |
| 9074 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9075 | " Cheap transparency is possible."); |
| 9076 | } |
| 9077 | } |
| 9078 | else |
| 9079 | ping_have_cheap_transparency = MagickFalse; |
| 9080 | |
glennrp | 8640fb5 | 2010-11-23 15:48:26 +0000 | [diff] [blame] | 9081 | image_depth=image->depth; |
| 9082 | |
glennrp | 26c990a | 2010-11-23 02:23:20 +0000 | [diff] [blame] | 9083 | quantum_info = (QuantumInfo *) NULL; |
| 9084 | number_colors=0; |
glennrp | f09bded | 2011-01-08 01:15:59 +0000 | [diff] [blame] | 9085 | image_colors=(int) image->colors; |
cristy | b0a657e | 2012-08-29 00:45:37 +0000 | [diff] [blame] | 9086 | image_matte=image->alpha_trait == BlendPixelTrait ? MagickTrue : MagickFalse; |
glennrp | 26c990a | 2010-11-23 02:23:20 +0000 | [diff] [blame] | 9087 | |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9088 | mng_info->IsPalette=image->storage_class == PseudoClass && |
glennrp | 1273f7b | 2011-02-24 03:20:30 +0000 | [diff] [blame] | 9089 | image_colors <= 256 && image->colormap != NULL; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9090 | |
glennrp | 52a479c | 2011-02-26 21:14:38 +0000 | [diff] [blame] | 9091 | if ((mng_info->write_png_colortype == 4 || mng_info->write_png8) && |
| 9092 | (image->colors == 0 || image->colormap == NULL)) |
| 9093 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 9094 | image_info=DestroyImageInfo(image_info); |
| 9095 | image=DestroyImage(image); |
| 9096 | (void) ThrowMagickException(exception,GetMagickModule(),CoderError, |
glennrp | 15e0155 | 2011-03-06 23:29:17 +0000 | [diff] [blame] | 9097 | "Cannot write PNG8 or color-type 3; colormap is NULL", |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 9098 | "`%s'",IMimage->filename); |
glennrp | 52a479c | 2011-02-26 21:14:38 +0000 | [diff] [blame] | 9099 | return(MagickFalse); |
| 9100 | } |
| 9101 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9102 | /* |
| 9103 | Allocate the PNG structures |
| 9104 | */ |
| 9105 | #ifdef PNG_USER_MEM_SUPPORTED |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 9106 | error_info.image=image; |
| 9107 | error_info.exception=exception; |
| 9108 | ping=png_create_write_struct_2(PNG_LIBPNG_VER_STRING,&error_info, |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 9109 | MagickPNGErrorHandler,MagickPNGWarningHandler,(void *) NULL, |
| 9110 | (png_malloc_ptr) Magick_png_malloc,(png_free_ptr) Magick_png_free); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9111 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9112 | #else |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 9113 | ping=png_create_write_struct(PNG_LIBPNG_VER_STRING,&error_info, |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 9114 | MagickPNGErrorHandler,MagickPNGWarningHandler); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9115 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9116 | #endif |
| 9117 | if (ping == (png_struct *) NULL) |
| 9118 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9119 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9120 | ping_info=png_create_info_struct(ping); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9121 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9122 | if (ping_info == (png_info *) NULL) |
| 9123 | { |
| 9124 | png_destroy_write_struct(&ping,(png_info **) NULL); |
| 9125 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
| 9126 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9127 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9128 | png_set_write_fn(ping,image,png_put_data,png_flush_data); |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 9129 | ping_pixels=(unsigned char *) NULL; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9130 | |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 9131 | if (setjmp(png_jmpbuf(ping))) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9132 | { |
| 9133 | /* |
| 9134 | PNG write failed. |
| 9135 | */ |
| 9136 | #ifdef PNG_DEBUG |
| 9137 | if (image_info->verbose) |
| 9138 | (void) printf("PNG write has failed.\n"); |
| 9139 | #endif |
| 9140 | png_destroy_write_struct(&ping,&ping_info); |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 9141 | #ifdef PNG_SETJMP_NOT_THREAD_SAFE |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 9142 | UnlockSemaphoreInfo(ping_semaphore); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9143 | #endif |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 9144 | |
| 9145 | if (ping_pixels != (unsigned char *) NULL) |
| 9146 | ping_pixels=(unsigned char *) RelinquishMagickMemory(ping_pixels); |
| 9147 | |
| 9148 | if (quantum_info != (QuantumInfo *) NULL) |
| 9149 | quantum_info=DestroyQuantumInfo(quantum_info); |
| 9150 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 9151 | if (ping_have_blob != MagickFalse) |
| 9152 | (void) CloseBlob(image); |
| 9153 | image_info=DestroyImageInfo(image_info); |
| 9154 | image=DestroyImage(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9155 | return(MagickFalse); |
| 9156 | } |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 9157 | |
| 9158 | /* { For navigation to end of SETJMP-protected block. Within this |
| 9159 | * block, use png_error() instead of Throwing an Exception, to ensure |
| 9160 | * that libpng is able to clean up, and that the semaphore is unlocked. |
| 9161 | */ |
| 9162 | |
| 9163 | #ifdef PNG_SETJMP_NOT_THREAD_SAFE |
| 9164 | LockSemaphoreInfo(ping_semaphore); |
| 9165 | #endif |
| 9166 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9167 | /* |
| 9168 | Prepare PNG for writing. |
| 9169 | */ |
glennrp | 9bf97b6 | 2012-06-06 21:03:14 +0000 | [diff] [blame] | 9170 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9171 | #if defined(PNG_MNG_FEATURES_SUPPORTED) |
| 9172 | if (mng_info->write_mng) |
glennrp | 25024a6 | 2012-06-07 11:38:34 +0000 | [diff] [blame] | 9173 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9174 | (void) png_permit_mng_features(ping,PNG_ALL_MNG_FEATURES); |
glennrp | 25024a6 | 2012-06-07 11:38:34 +0000 | [diff] [blame] | 9175 | # ifdef PNG_WRITE_CHECK_FOR_INVALID_INDEX_SUPPORTED |
| 9176 | /* Disable new libpng-1.5.10 feature when writing a MNG because |
| 9177 | * zero-length PLTE is OK |
| 9178 | */ |
| 9179 | png_set_check_for_invalid_index (ping, 0); |
| 9180 | # endif |
| 9181 | } |
glennrp | 2b013e4 | 2010-11-24 16:55:50 +0000 | [diff] [blame] | 9182 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9183 | #else |
| 9184 | # ifdef PNG_WRITE_EMPTY_PLTE_SUPPORTED |
| 9185 | if (mng_info->write_mng) |
| 9186 | png_permit_empty_plte(ping,MagickTrue); |
glennrp | 2b013e4 | 2010-11-24 16:55:50 +0000 | [diff] [blame] | 9187 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9188 | # endif |
| 9189 | #endif |
glennrp | 2b013e4 | 2010-11-24 16:55:50 +0000 | [diff] [blame] | 9190 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9191 | x=0; |
glennrp | 2b013e4 | 2010-11-24 16:55:50 +0000 | [diff] [blame] | 9192 | |
cristy | 4e5bc84 | 2010-06-09 13:56:01 +0000 | [diff] [blame] | 9193 | ping_width=(png_uint_32) image->columns; |
| 9194 | ping_height=(png_uint_32) image->rows; |
glennrp | 2b013e4 | 2010-11-24 16:55:50 +0000 | [diff] [blame] | 9195 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9196 | if (mng_info->write_png8 || mng_info->write_png24 || mng_info->write_png32) |
| 9197 | image_depth=8; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9198 | |
glennrp | fd164d2 | 2013-01-26 21:10:22 +0000 | [diff] [blame] | 9199 | if (mng_info->write_png48 || mng_info->write_png64) |
| 9200 | image_depth=16; |
| 9201 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9202 | if (mng_info->write_png_depth != 0) |
| 9203 | image_depth=mng_info->write_png_depth; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9204 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9205 | /* Adjust requested depth to next higher valid depth if necessary */ |
| 9206 | if (image_depth > 8) |
| 9207 | image_depth=16; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9208 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9209 | if ((image_depth > 4) && (image_depth < 8)) |
| 9210 | image_depth=8; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9211 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9212 | if (image_depth == 3) |
| 9213 | image_depth=4; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9214 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9215 | if (logging != MagickFalse) |
| 9216 | { |
| 9217 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 9218 | " width=%.20g",(double) ping_width); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9219 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 9220 | " height=%.20g",(double) ping_height); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9221 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 9222 | " image_matte=%.20g",(double) image->alpha_trait); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9223 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | 8640fb5 | 2010-11-23 15:48:26 +0000 | [diff] [blame] | 9224 | " image->depth=%.20g",(double) image->depth); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9225 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | 8640fb5 | 2010-11-23 15:48:26 +0000 | [diff] [blame] | 9226 | " Tentative ping_bit_depth=%.20g",(double) image_depth); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9227 | } |
glennrp | 8640fb5 | 2010-11-23 15:48:26 +0000 | [diff] [blame] | 9228 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9229 | save_image_depth=image_depth; |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 9230 | ping_bit_depth=(png_byte) save_image_depth; |
glennrp | dfd7080 | 2010-11-14 01:23:35 +0000 | [diff] [blame] | 9231 | |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 9232 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9233 | #if defined(PNG_pHYs_SUPPORTED) |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 9234 | if (ping_exclude_pHYs == MagickFalse) |
| 9235 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 9236 | if ((image->resolution.x != 0) && (image->resolution.y != 0) && |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9237 | (!mng_info->write_mng || !mng_info->equal_physs)) |
| 9238 | { |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9239 | if (logging != MagickFalse) |
glennrp | dfd7080 | 2010-11-14 01:23:35 +0000 | [diff] [blame] | 9240 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9241 | " Setting up pHYs chunk"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9242 | |
| 9243 | if (image->units == PixelsPerInchResolution) |
| 9244 | { |
glennrp | dfd7080 | 2010-11-14 01:23:35 +0000 | [diff] [blame] | 9245 | ping_pHYs_unit_type=PNG_RESOLUTION_METER; |
glennrp | 823b55c | 2011-03-14 18:46:46 +0000 | [diff] [blame] | 9246 | ping_pHYs_x_resolution= |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 9247 | (png_uint_32) ((100.0*image->resolution.x+0.5)/2.54); |
glennrp | 823b55c | 2011-03-14 18:46:46 +0000 | [diff] [blame] | 9248 | ping_pHYs_y_resolution= |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 9249 | (png_uint_32) ((100.0*image->resolution.y+0.5)/2.54); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9250 | } |
glennrp | dfd7080 | 2010-11-14 01:23:35 +0000 | [diff] [blame] | 9251 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9252 | else if (image->units == PixelsPerCentimeterResolution) |
| 9253 | { |
glennrp | dfd7080 | 2010-11-14 01:23:35 +0000 | [diff] [blame] | 9254 | ping_pHYs_unit_type=PNG_RESOLUTION_METER; |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 9255 | ping_pHYs_x_resolution=(png_uint_32) (100.0*image->resolution.x+0.5); |
| 9256 | 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] | 9257 | } |
glennrp | 991d11d | 2010-11-12 21:55:28 +0000 | [diff] [blame] | 9258 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9259 | else |
| 9260 | { |
glennrp | dfd7080 | 2010-11-14 01:23:35 +0000 | [diff] [blame] | 9261 | ping_pHYs_unit_type=PNG_RESOLUTION_UNKNOWN; |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 9262 | ping_pHYs_x_resolution=(png_uint_32) image->resolution.x; |
| 9263 | ping_pHYs_y_resolution=(png_uint_32) image->resolution.y; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9264 | } |
glennrp | 991d11d | 2010-11-12 21:55:28 +0000 | [diff] [blame] | 9265 | |
glennrp | 823b55c | 2011-03-14 18:46:46 +0000 | [diff] [blame] | 9266 | if (logging != MagickFalse) |
| 9267 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9268 | " Set up PNG pHYs chunk: xres: %.20g, yres: %.20g, units: %d.", |
| 9269 | (double) ping_pHYs_x_resolution,(double) ping_pHYs_y_resolution, |
| 9270 | (int) ping_pHYs_unit_type); |
glennrp | 991d11d | 2010-11-12 21:55:28 +0000 | [diff] [blame] | 9271 | ping_have_pHYs = MagickTrue; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9272 | } |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 9273 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9274 | #endif |
glennrp | a521b2f | 2010-10-29 04:11:03 +0000 | [diff] [blame] | 9275 | |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 9276 | if (ping_exclude_bKGD == MagickFalse) |
| 9277 | { |
glennrp | a521b2f | 2010-10-29 04:11:03 +0000 | [diff] [blame] | 9278 | if ((!mng_info->adjoin || !mng_info->equal_backgrounds)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9279 | { |
glennrp | a521b2f | 2010-10-29 04:11:03 +0000 | [diff] [blame] | 9280 | unsigned int |
| 9281 | mask; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9282 | |
glennrp | a521b2f | 2010-10-29 04:11:03 +0000 | [diff] [blame] | 9283 | mask=0xffff; |
| 9284 | if (ping_bit_depth == 8) |
| 9285 | mask=0x00ff; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9286 | |
glennrp | a521b2f | 2010-10-29 04:11:03 +0000 | [diff] [blame] | 9287 | if (ping_bit_depth == 4) |
| 9288 | mask=0x000f; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9289 | |
glennrp | a521b2f | 2010-10-29 04:11:03 +0000 | [diff] [blame] | 9290 | if (ping_bit_depth == 2) |
| 9291 | mask=0x0003; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9292 | |
glennrp | a521b2f | 2010-10-29 04:11:03 +0000 | [diff] [blame] | 9293 | if (ping_bit_depth == 1) |
| 9294 | mask=0x0001; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9295 | |
glennrp | a521b2f | 2010-10-29 04:11:03 +0000 | [diff] [blame] | 9296 | ping_background.red=(png_uint_16) |
| 9297 | (ScaleQuantumToShort(image->background_color.red) & mask); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9298 | |
glennrp | a521b2f | 2010-10-29 04:11:03 +0000 | [diff] [blame] | 9299 | ping_background.green=(png_uint_16) |
| 9300 | (ScaleQuantumToShort(image->background_color.green) & mask); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9301 | |
glennrp | a521b2f | 2010-10-29 04:11:03 +0000 | [diff] [blame] | 9302 | ping_background.blue=(png_uint_16) |
| 9303 | (ScaleQuantumToShort(image->background_color.blue) & mask); |
glennrp | c6c391a | 2011-04-27 02:23:56 +0000 | [diff] [blame] | 9304 | |
| 9305 | ping_background.gray=(png_uint_16) ping_background.green; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9306 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9307 | |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9308 | if (logging != MagickFalse) |
glennrp | 3b51f0e | 2010-11-27 18:14:08 +0000 | [diff] [blame] | 9309 | { |
| 9310 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9311 | " Setting up bKGD chunk (1)"); |
glennrp | c6c391a | 2011-04-27 02:23:56 +0000 | [diff] [blame] | 9312 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9313 | " background_color index is %d", |
| 9314 | (int) ping_background.index); |
glennrp | 3b51f0e | 2010-11-27 18:14:08 +0000 | [diff] [blame] | 9315 | |
| 9316 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9317 | " ping_bit_depth=%d",ping_bit_depth); |
| 9318 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9319 | |
| 9320 | ping_have_bKGD = MagickTrue; |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 9321 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9322 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9323 | /* |
| 9324 | Select the color type. |
| 9325 | */ |
| 9326 | matte=image_matte; |
| 9327 | old_bit_depth=0; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9328 | |
glennrp | 1273f7b | 2011-02-24 03:20:30 +0000 | [diff] [blame] | 9329 | if (mng_info->IsPalette && mng_info->write_png8) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9330 | { |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9331 | |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 9332 | /* To do: make this a function cause it's used twice, except |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9333 | for reducing the sample depth from 8. */ |
| 9334 | |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9335 | number_colors=image_colors; |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 9336 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 9337 | ping_have_tRNS=MagickFalse; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9338 | |
| 9339 | /* |
| 9340 | Set image palette. |
| 9341 | */ |
| 9342 | ping_color_type=(png_byte) PNG_COLOR_TYPE_PALETTE; |
| 9343 | |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9344 | if (logging != MagickFalse) |
| 9345 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9346 | " Setting up PLTE chunk with %d colors (%d)", |
glennrp | f09bded | 2011-01-08 01:15:59 +0000 | [diff] [blame] | 9347 | number_colors, image_colors); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9348 | |
| 9349 | for (i=0; i < (ssize_t) number_colors; i++) |
| 9350 | { |
| 9351 | palette[i].red=ScaleQuantumToChar(image->colormap[i].red); |
| 9352 | palette[i].green=ScaleQuantumToChar(image->colormap[i].green); |
| 9353 | palette[i].blue=ScaleQuantumToChar(image->colormap[i].blue); |
| 9354 | if (logging != MagickFalse) |
| 9355 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | 67b9c1a | 2011-04-22 18:47:36 +0000 | [diff] [blame] | 9356 | #if MAGICKCORE_QUANTUM_DEPTH == 8 |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9357 | " %3ld (%3d,%3d,%3d)", |
glennrp | 67b9c1a | 2011-04-22 18:47:36 +0000 | [diff] [blame] | 9358 | #else |
| 9359 | " %5ld (%5d,%5d,%5d)", |
| 9360 | #endif |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9361 | (long) i,palette[i].red,palette[i].green,palette[i].blue); |
| 9362 | |
| 9363 | } |
glennrp | 2b013e4 | 2010-11-24 16:55:50 +0000 | [diff] [blame] | 9364 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 9365 | ping_have_PLTE=MagickTrue; |
| 9366 | image_depth=ping_bit_depth; |
| 9367 | ping_num_trans=0; |
glennrp | 2b013e4 | 2010-11-24 16:55:50 +0000 | [diff] [blame] | 9368 | |
glennrp | 58e0176 | 2011-01-07 15:28:54 +0000 | [diff] [blame] | 9369 | if (matte != MagickFalse) |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 9370 | { |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9371 | /* |
| 9372 | Identify which colormap entry is transparent. |
| 9373 | */ |
| 9374 | assert(number_colors <= 256); |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 9375 | assert(image->colormap != NULL); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9376 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 9377 | for (i=0; i < (ssize_t) number_transparent; i++) |
| 9378 | ping_trans_alpha[i]=0; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9379 | |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9380 | |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 9381 | ping_num_trans=(unsigned short) (number_transparent + |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 9382 | number_semitransparent); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9383 | |
| 9384 | if (ping_num_trans == 0) |
| 9385 | ping_have_tRNS=MagickFalse; |
| 9386 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 9387 | else |
| 9388 | ping_have_tRNS=MagickTrue; |
| 9389 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9390 | |
glennrp | 1273f7b | 2011-02-24 03:20:30 +0000 | [diff] [blame] | 9391 | if (ping_exclude_bKGD == MagickFalse) |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 9392 | { |
glennrp | 1273f7b | 2011-02-24 03:20:30 +0000 | [diff] [blame] | 9393 | /* |
| 9394 | * Identify which colormap entry is the background color. |
| 9395 | */ |
| 9396 | |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 9397 | for (i=0; i < (ssize_t) MagickMax(1L*number_colors-1L,1L); i++) |
| 9398 | if (IsPNGColorEqual(ping_background,image->colormap[i])) |
| 9399 | break; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9400 | |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 9401 | ping_background.index=(png_byte) i; |
glennrp | c6c391a | 2011-04-27 02:23:56 +0000 | [diff] [blame] | 9402 | |
| 9403 | if (logging != MagickFalse) |
| 9404 | { |
| 9405 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9406 | " background_color index is %d", |
| 9407 | (int) ping_background.index); |
| 9408 | } |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 9409 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9410 | } /* end of write_png8 */ |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9411 | |
glennrp | fd164d2 | 2013-01-26 21:10:22 +0000 | [diff] [blame] | 9412 | else if (mng_info->write_png_colortype == 1) |
| 9413 | { |
| 9414 | image_matte=MagickFalse; |
| 9415 | ping_color_type=(png_byte) PNG_COLOR_TYPE_GRAY; |
| 9416 | } |
| 9417 | |
| 9418 | else if (mng_info->write_png24 || mng_info->write_png48 || |
| 9419 | mng_info->write_png_colortype == 3) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9420 | { |
| 9421 | image_matte=MagickFalse; |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 9422 | ping_color_type=(png_byte) PNG_COLOR_TYPE_RGB; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9423 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9424 | |
glennrp | fd164d2 | 2013-01-26 21:10:22 +0000 | [diff] [blame] | 9425 | else if (mng_info->write_png32 || mng_info->write_png64 || |
| 9426 | mng_info->write_png_colortype == 7) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9427 | { |
| 9428 | image_matte=MagickTrue; |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 9429 | ping_color_type=(png_byte) PNG_COLOR_TYPE_RGB_ALPHA; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9430 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9431 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 9432 | else /* mng_info->write_pngNN not specified */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9433 | { |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 9434 | image_depth=ping_bit_depth; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9435 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 9436 | if (mng_info->write_png_colortype != 0) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9437 | { |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 9438 | ping_color_type=(png_byte) mng_info->write_png_colortype-1; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9439 | |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 9440 | if (ping_color_type == PNG_COLOR_TYPE_GRAY_ALPHA || |
| 9441 | ping_color_type == PNG_COLOR_TYPE_RGB_ALPHA) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9442 | image_matte=MagickTrue; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 9443 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 9444 | else |
| 9445 | image_matte=MagickFalse; |
glennrp | 7c4c9e6 | 2011-03-21 20:23:32 +0000 | [diff] [blame] | 9446 | |
| 9447 | if (logging != MagickFalse) |
| 9448 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9449 | " PNG colortype %d was specified:",(int) ping_color_type); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9450 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9451 | |
glennrp | 7c4c9e6 | 2011-03-21 20:23:32 +0000 | [diff] [blame] | 9452 | else /* write_png_colortype not specified */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9453 | { |
| 9454 | if (logging != MagickFalse) |
| 9455 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | 3c21811 | 2010-11-27 15:31:26 +0000 | [diff] [blame] | 9456 | " Selecting PNG colortype:"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9457 | |
glennrp | d6bf161 | 2010-12-17 17:28:54 +0000 | [diff] [blame] | 9458 | ping_color_type=(png_byte) ((matte != MagickFalse)? |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 9459 | PNG_COLOR_TYPE_RGB_ALPHA:PNG_COLOR_TYPE_RGB); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9460 | |
glennrp | d6bf161 | 2010-12-17 17:28:54 +0000 | [diff] [blame] | 9461 | if (image_info->type == TrueColorType) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9462 | { |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 9463 | ping_color_type=(png_byte) PNG_COLOR_TYPE_RGB; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9464 | image_matte=MagickFalse; |
| 9465 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9466 | |
glennrp | d6bf161 | 2010-12-17 17:28:54 +0000 | [diff] [blame] | 9467 | if (image_info->type == TrueColorMatteType) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9468 | { |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 9469 | ping_color_type=(png_byte) PNG_COLOR_TYPE_RGB_ALPHA; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9470 | image_matte=MagickTrue; |
| 9471 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9472 | |
glennrp | 5aa37f6 | 2011-01-02 03:07:57 +0000 | [diff] [blame] | 9473 | if (image_info->type == PaletteType || |
| 9474 | image_info->type == PaletteMatteType) |
| 9475 | ping_color_type=(png_byte) PNG_COLOR_TYPE_PALETTE; |
| 9476 | |
glennrp | 7c4c9e6 | 2011-03-21 20:23:32 +0000 | [diff] [blame] | 9477 | if (mng_info->write_png_colortype == 0 && |
| 9478 | (image_info->type == UndefinedType || |
| 9479 | image_info->type == OptimizeType)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9480 | { |
glennrp | 5aa37f6 | 2011-01-02 03:07:57 +0000 | [diff] [blame] | 9481 | if (ping_have_color == MagickFalse) |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 9482 | { |
glennrp | 5aa37f6 | 2011-01-02 03:07:57 +0000 | [diff] [blame] | 9483 | if (image_matte == MagickFalse) |
| 9484 | { |
| 9485 | ping_color_type=(png_byte) PNG_COLOR_TYPE_GRAY; |
| 9486 | image_matte=MagickFalse; |
| 9487 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9488 | |
glennrp | 0b206f5 | 2011-01-07 04:55:32 +0000 | [diff] [blame] | 9489 | else |
glennrp | 5aa37f6 | 2011-01-02 03:07:57 +0000 | [diff] [blame] | 9490 | { |
| 9491 | ping_color_type=(png_byte) PNG_COLOR_TYPE_GRAY_ALPHA; |
| 9492 | image_matte=MagickTrue; |
| 9493 | } |
| 9494 | } |
| 9495 | else |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 9496 | { |
glennrp | 5aa37f6 | 2011-01-02 03:07:57 +0000 | [diff] [blame] | 9497 | if (image_matte == MagickFalse) |
| 9498 | { |
| 9499 | ping_color_type=(png_byte) PNG_COLOR_TYPE_RGB; |
| 9500 | image_matte=MagickFalse; |
| 9501 | } |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 9502 | |
glennrp | 0b206f5 | 2011-01-07 04:55:32 +0000 | [diff] [blame] | 9503 | else |
glennrp | 5aa37f6 | 2011-01-02 03:07:57 +0000 | [diff] [blame] | 9504 | { |
| 9505 | ping_color_type=(png_byte) PNG_COLOR_TYPE_RGBA; |
| 9506 | image_matte=MagickTrue; |
| 9507 | } |
| 9508 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9509 | } |
glennrp | 5aa37f6 | 2011-01-02 03:07:57 +0000 | [diff] [blame] | 9510 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9511 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9512 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9513 | if (logging != MagickFalse) |
| 9514 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | 8640fb5 | 2010-11-23 15:48:26 +0000 | [diff] [blame] | 9515 | " Selected PNG colortype=%d",ping_color_type); |
glennrp | 26c990a | 2010-11-23 02:23:20 +0000 | [diff] [blame] | 9516 | |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 9517 | if (ping_bit_depth < 8) |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9518 | { |
| 9519 | if (ping_color_type == PNG_COLOR_TYPE_GRAY_ALPHA || |
| 9520 | ping_color_type == PNG_COLOR_TYPE_RGB || |
| 9521 | ping_color_type == PNG_COLOR_TYPE_RGB_ALPHA) |
| 9522 | ping_bit_depth=8; |
| 9523 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9524 | |
glennrp | d6bf161 | 2010-12-17 17:28:54 +0000 | [diff] [blame] | 9525 | old_bit_depth=ping_bit_depth; |
| 9526 | |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 9527 | if (ping_color_type == PNG_COLOR_TYPE_GRAY) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9528 | { |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 9529 | if (image->alpha_trait != BlendPixelTrait && ping_have_non_bw == MagickFalse) |
glennrp | 8d57966 | 2011-02-23 02:05:02 +0000 | [diff] [blame] | 9530 | ping_bit_depth=1; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9531 | } |
glennrp | 8640fb5 | 2010-11-23 15:48:26 +0000 | [diff] [blame] | 9532 | |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 9533 | if (ping_color_type == PNG_COLOR_TYPE_PALETTE) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9534 | { |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 9535 | size_t one = 1; |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 9536 | ping_bit_depth=1; |
glennrp | 0f11198 | 2010-07-07 20:18:33 +0000 | [diff] [blame] | 9537 | |
| 9538 | if (image->colors == 0) |
| 9539 | { |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9540 | /* DO SOMETHING */ |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 9541 | png_error(ping,"image has 0 colors"); |
glennrp | 0f11198 | 2010-07-07 20:18:33 +0000 | [diff] [blame] | 9542 | } |
| 9543 | |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 9544 | while ((int) (one << ping_bit_depth) < (ssize_t) image_colors) |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 9545 | ping_bit_depth <<= 1; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9546 | } |
glennrp | 2b013e4 | 2010-11-24 16:55:50 +0000 | [diff] [blame] | 9547 | |
glennrp | d6bf161 | 2010-12-17 17:28:54 +0000 | [diff] [blame] | 9548 | if (logging != MagickFalse) |
| 9549 | { |
| 9550 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9551 | " Number of colors: %.20g",(double) image_colors); |
| 9552 | |
| 9553 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9554 | " Tentative PNG bit depth: %d",ping_bit_depth); |
| 9555 | } |
| 9556 | |
| 9557 | if (ping_bit_depth < (int) mng_info->write_png_depth) |
| 9558 | ping_bit_depth = mng_info->write_png_depth; |
| 9559 | } |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 9560 | |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 9561 | image_depth=ping_bit_depth; |
glennrp | 2b013e4 | 2010-11-24 16:55:50 +0000 | [diff] [blame] | 9562 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9563 | if (logging != MagickFalse) |
| 9564 | { |
| 9565 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | 1a56e9c | 2012-04-25 17:06:57 +0000 | [diff] [blame] | 9566 | " Tentative PNG color type: %s (%.20g)", |
| 9567 | PngColorTypeToString(ping_color_type), |
| 9568 | (double) ping_color_type); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9569 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9570 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 9571 | " image_info->type: %.20g",(double) image_info->type); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9572 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9573 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 9574 | " image_depth: %.20g",(double) image_depth); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9575 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9576 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | 3c21811 | 2010-11-27 15:31:26 +0000 | [diff] [blame] | 9577 | |
glennrp | 8640fb5 | 2010-11-23 15:48:26 +0000 | [diff] [blame] | 9578 | " image->depth: %.20g",(double) image->depth); |
| 9579 | |
| 9580 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 9581 | " ping_bit_depth: %.20g",(double) ping_bit_depth); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9582 | } |
| 9583 | |
glennrp | 58e0176 | 2011-01-07 15:28:54 +0000 | [diff] [blame] | 9584 | if (matte != MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9585 | { |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 9586 | if (mng_info->IsPalette) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9587 | { |
glennrp | 7c4c9e6 | 2011-03-21 20:23:32 +0000 | [diff] [blame] | 9588 | if (mng_info->write_png_colortype == 0) |
| 9589 | { |
| 9590 | ping_color_type=PNG_COLOR_TYPE_GRAY_ALPHA; |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 9591 | |
glennrp | 7c4c9e6 | 2011-03-21 20:23:32 +0000 | [diff] [blame] | 9592 | if (ping_have_color != MagickFalse) |
| 9593 | ping_color_type=PNG_COLOR_TYPE_RGBA; |
| 9594 | } |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 9595 | |
| 9596 | /* |
| 9597 | * Determine if there is any transparent color. |
| 9598 | */ |
| 9599 | if (number_transparent + number_semitransparent == 0) |
| 9600 | { |
| 9601 | /* |
| 9602 | No transparent pixels are present. Change 4 or 6 to 0 or 2. |
| 9603 | */ |
glennrp | a6a0663 | 2011-01-19 15:15:34 +0000 | [diff] [blame] | 9604 | |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 9605 | image_matte=MagickFalse; |
glennrp | 7c4c9e6 | 2011-03-21 20:23:32 +0000 | [diff] [blame] | 9606 | |
| 9607 | if (mng_info->write_png_colortype == 0) |
| 9608 | ping_color_type&=0x03; |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 9609 | } |
| 9610 | |
| 9611 | else |
| 9612 | { |
| 9613 | unsigned int |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 9614 | mask; |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 9615 | |
| 9616 | mask=0xffff; |
| 9617 | |
| 9618 | if (ping_bit_depth == 8) |
| 9619 | mask=0x00ff; |
| 9620 | |
| 9621 | if (ping_bit_depth == 4) |
| 9622 | mask=0x000f; |
| 9623 | |
| 9624 | if (ping_bit_depth == 2) |
| 9625 | mask=0x0003; |
| 9626 | |
| 9627 | if (ping_bit_depth == 1) |
| 9628 | mask=0x0001; |
| 9629 | |
| 9630 | ping_trans_color.red=(png_uint_16) |
| 9631 | (ScaleQuantumToShort(image->colormap[0].red) & mask); |
| 9632 | |
| 9633 | ping_trans_color.green=(png_uint_16) |
| 9634 | (ScaleQuantumToShort(image->colormap[0].green) & mask); |
| 9635 | |
| 9636 | ping_trans_color.blue=(png_uint_16) |
| 9637 | (ScaleQuantumToShort(image->colormap[0].blue) & mask); |
| 9638 | |
| 9639 | ping_trans_color.gray=(png_uint_16) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 9640 | (ScaleQuantumToShort(GetPixelInfoIntensity( |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 9641 | image->colormap)) & mask); |
| 9642 | |
| 9643 | ping_trans_color.index=(png_byte) 0; |
| 9644 | |
| 9645 | ping_have_tRNS=MagickTrue; |
| 9646 | } |
| 9647 | |
| 9648 | if (ping_have_tRNS != MagickFalse) |
| 9649 | { |
| 9650 | /* |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 9651 | * Determine if there is one and only one transparent color |
| 9652 | * and if so if it is fully transparent. |
| 9653 | */ |
| 9654 | if (ping_have_cheap_transparency == MagickFalse) |
| 9655 | ping_have_tRNS=MagickFalse; |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 9656 | } |
| 9657 | |
| 9658 | if (ping_have_tRNS != MagickFalse) |
| 9659 | { |
glennrp | 7c4c9e6 | 2011-03-21 20:23:32 +0000 | [diff] [blame] | 9660 | if (mng_info->write_png_colortype == 0) |
| 9661 | ping_color_type &= 0x03; /* changes 4 or 6 to 0 or 2 */ |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 9662 | |
| 9663 | if (image_depth == 8) |
| 9664 | { |
| 9665 | ping_trans_color.red&=0xff; |
| 9666 | ping_trans_color.green&=0xff; |
| 9667 | ping_trans_color.blue&=0xff; |
| 9668 | ping_trans_color.gray&=0xff; |
| 9669 | } |
| 9670 | } |
| 9671 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9672 | else |
| 9673 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9674 | if (image_depth == 8) |
| 9675 | { |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 9676 | ping_trans_color.red&=0xff; |
| 9677 | ping_trans_color.green&=0xff; |
| 9678 | ping_trans_color.blue&=0xff; |
| 9679 | ping_trans_color.gray&=0xff; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9680 | } |
| 9681 | } |
| 9682 | } |
glennrp | 8640fb5 | 2010-11-23 15:48:26 +0000 | [diff] [blame] | 9683 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9684 | matte=image_matte; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9685 | |
glennrp | 2e09f55 | 2010-11-14 00:38:48 +0000 | [diff] [blame] | 9686 | if (ping_have_tRNS != MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9687 | image_matte=MagickFalse; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9688 | |
glennrp | 39992b4 | 2010-11-14 00:03:43 +0000 | [diff] [blame] | 9689 | if ((mng_info->IsPalette) && |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9690 | mng_info->write_png_colortype-1 != PNG_COLOR_TYPE_PALETTE && |
glennrp | 8d57966 | 2011-02-23 02:05:02 +0000 | [diff] [blame] | 9691 | ping_have_color == MagickFalse && |
| 9692 | (image_matte == MagickFalse || image_depth >= 8)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9693 | { |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 9694 | size_t one=1; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9695 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9696 | if (image_matte != MagickFalse) |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 9697 | ping_color_type=PNG_COLOR_TYPE_GRAY_ALPHA; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9698 | |
glennrp | 7c4c9e6 | 2011-03-21 20:23:32 +0000 | [diff] [blame] | 9699 | else if (mng_info->write_png_colortype-1 != PNG_COLOR_TYPE_GRAY_ALPHA) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9700 | { |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 9701 | ping_color_type=PNG_COLOR_TYPE_GRAY; |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 9702 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9703 | if (save_image_depth == 16 && image_depth == 8) |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 9704 | { |
| 9705 | if (logging != MagickFalse) |
| 9706 | { |
| 9707 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9708 | " Scaling ping_trans_color (0)"); |
| 9709 | } |
| 9710 | ping_trans_color.gray*=0x0101; |
| 9711 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9712 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9713 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9714 | if (image_depth > MAGICKCORE_QUANTUM_DEPTH) |
| 9715 | image_depth=MAGICKCORE_QUANTUM_DEPTH; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9716 | |
glennrp | 136ee3a | 2011-04-27 15:47:45 +0000 | [diff] [blame] | 9717 | if ((image_colors == 0) || |
glennrp | d17915c | 2011-04-29 14:24:22 +0000 | [diff] [blame] | 9718 | ((ssize_t) (image_colors-1) > (ssize_t) MaxColormapSize)) |
glennrp | f09bded | 2011-01-08 01:15:59 +0000 | [diff] [blame] | 9719 | image_colors=(int) (one << image_depth); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9720 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9721 | if (image_depth > 8) |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 9722 | ping_bit_depth=16; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9723 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9724 | else |
| 9725 | { |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 9726 | ping_bit_depth=8; |
| 9727 | if ((int) ping_color_type == PNG_COLOR_TYPE_PALETTE) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9728 | { |
| 9729 | if(!mng_info->write_png_depth) |
| 9730 | { |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 9731 | ping_bit_depth=1; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9732 | |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 9733 | while ((int) (one << ping_bit_depth) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 9734 | < (ssize_t) image_colors) |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 9735 | ping_bit_depth <<= 1; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9736 | } |
| 9737 | } |
glennrp | 2b013e4 | 2010-11-24 16:55:50 +0000 | [diff] [blame] | 9738 | |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9739 | else if (ping_color_type == |
| 9740 | PNG_COLOR_TYPE_GRAY && image_colors < 17 && |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9741 | mng_info->IsPalette) |
| 9742 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9743 | /* Check if grayscale is reducible */ |
glennrp | 1a0aaa6 | 2011-03-07 17:40:17 +0000 | [diff] [blame] | 9744 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9745 | int |
| 9746 | depth_4_ok=MagickTrue, |
| 9747 | depth_2_ok=MagickTrue, |
| 9748 | depth_1_ok=MagickTrue; |
| 9749 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 9750 | for (i=0; i < (ssize_t) image_colors; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9751 | { |
| 9752 | unsigned char |
| 9753 | intensity; |
| 9754 | |
| 9755 | intensity=ScaleQuantumToChar(image->colormap[i].red); |
| 9756 | |
| 9757 | if ((intensity & 0x0f) != ((intensity & 0xf0) >> 4)) |
| 9758 | depth_4_ok=depth_2_ok=depth_1_ok=MagickFalse; |
| 9759 | else if ((intensity & 0x03) != ((intensity & 0x0c) >> 2)) |
| 9760 | depth_2_ok=depth_1_ok=MagickFalse; |
glennrp | 4bf8973 | 2011-03-21 13:48:28 +0000 | [diff] [blame] | 9761 | else if ((intensity & 0x01) != ((intensity & 0x02) >> 1)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9762 | depth_1_ok=MagickFalse; |
| 9763 | } |
glennrp | 2b013e4 | 2010-11-24 16:55:50 +0000 | [diff] [blame] | 9764 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9765 | if (depth_1_ok && mng_info->write_png_depth <= 1) |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 9766 | ping_bit_depth=1; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9767 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9768 | else if (depth_2_ok && mng_info->write_png_depth <= 2) |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 9769 | ping_bit_depth=2; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9770 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9771 | else if (depth_4_ok && mng_info->write_png_depth <= 4) |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 9772 | ping_bit_depth=4; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9773 | } |
| 9774 | } |
glennrp | 2b013e4 | 2010-11-24 16:55:50 +0000 | [diff] [blame] | 9775 | |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 9776 | image_depth=ping_bit_depth; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9777 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9778 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9779 | else |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9780 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9781 | if (mng_info->IsPalette) |
| 9782 | { |
glennrp | 17a1485 | 2010-05-10 03:01:59 +0000 | [diff] [blame] | 9783 | number_colors=image_colors; |
| 9784 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9785 | if (image_depth <= 8) |
| 9786 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9787 | /* |
| 9788 | Set image palette. |
| 9789 | */ |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 9790 | ping_color_type=(png_byte) PNG_COLOR_TYPE_PALETTE; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9791 | |
glennrp | 58e0176 | 2011-01-07 15:28:54 +0000 | [diff] [blame] | 9792 | if (mng_info->have_write_global_plte && matte == MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9793 | { |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 9794 | png_set_PLTE(ping,ping_info,NULL,0); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9795 | |
glennrp | 3b51f0e | 2010-11-27 18:14:08 +0000 | [diff] [blame] | 9796 | if (logging != MagickFalse) |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 9797 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9798 | " Setting up empty PLTE chunk"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9799 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9800 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9801 | else |
| 9802 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 9803 | for (i=0; i < (ssize_t) number_colors; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9804 | { |
| 9805 | palette[i].red=ScaleQuantumToChar(image->colormap[i].red); |
| 9806 | palette[i].green=ScaleQuantumToChar(image->colormap[i].green); |
| 9807 | palette[i].blue=ScaleQuantumToChar(image->colormap[i].blue); |
| 9808 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9809 | |
glennrp | 3b51f0e | 2010-11-27 18:14:08 +0000 | [diff] [blame] | 9810 | if (logging != MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9811 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | 98156a3 | 2009-12-09 15:32:44 +0000 | [diff] [blame] | 9812 | " Setting up PLTE chunk with %d colors", |
glennrp | f09bded | 2011-01-08 01:15:59 +0000 | [diff] [blame] | 9813 | number_colors); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9814 | |
glennrp | 39992b4 | 2010-11-14 00:03:43 +0000 | [diff] [blame] | 9815 | ping_have_PLTE=MagickTrue; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9816 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9817 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9818 | /* color_type is PNG_COLOR_TYPE_PALETTE */ |
glennrp | d6bf161 | 2010-12-17 17:28:54 +0000 | [diff] [blame] | 9819 | if (mng_info->write_png_depth == 0) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9820 | { |
cristy | befe4d2 | 2010-06-07 01:18:58 +0000 | [diff] [blame] | 9821 | size_t |
| 9822 | one; |
| 9823 | |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 9824 | ping_bit_depth=1; |
cristy | befe4d2 | 2010-06-07 01:18:58 +0000 | [diff] [blame] | 9825 | one=1; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9826 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 9827 | while ((one << ping_bit_depth) < (size_t) number_colors) |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 9828 | ping_bit_depth <<= 1; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9829 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9830 | |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 9831 | ping_num_trans=0; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9832 | |
glennrp | 58e0176 | 2011-01-07 15:28:54 +0000 | [diff] [blame] | 9833 | if (matte != MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9834 | { |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9835 | /* |
glennrp | d6bf161 | 2010-12-17 17:28:54 +0000 | [diff] [blame] | 9836 | * Set up trans_colors array. |
| 9837 | */ |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9838 | assert(number_colors <= 256); |
| 9839 | |
glennrp | d6bf161 | 2010-12-17 17:28:54 +0000 | [diff] [blame] | 9840 | ping_num_trans=(unsigned short) (number_transparent + |
| 9841 | number_semitransparent); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9842 | |
| 9843 | if (ping_num_trans == 0) |
| 9844 | ping_have_tRNS=MagickFalse; |
| 9845 | |
glennrp | d6bf161 | 2010-12-17 17:28:54 +0000 | [diff] [blame] | 9846 | else |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9847 | { |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 9848 | if (logging != MagickFalse) |
| 9849 | { |
| 9850 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9851 | " Scaling ping_trans_color (1)"); |
| 9852 | } |
glennrp | d6bf161 | 2010-12-17 17:28:54 +0000 | [diff] [blame] | 9853 | ping_have_tRNS=MagickTrue; |
| 9854 | |
| 9855 | for (i=0; i < ping_num_trans; i++) |
| 9856 | { |
glennrp | 750105b | 2012-04-25 16:20:45 +0000 | [diff] [blame] | 9857 | ping_trans_alpha[i]= (png_byte) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 9858 | ScaleQuantumToChar(image->colormap[i].alpha); |
glennrp | d6bf161 | 2010-12-17 17:28:54 +0000 | [diff] [blame] | 9859 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9860 | } |
| 9861 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9862 | } |
| 9863 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9864 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9865 | else |
| 9866 | { |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 9867 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9868 | if (image_depth < 8) |
| 9869 | image_depth=8; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9870 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9871 | if ((save_image_depth == 16) && (image_depth == 8)) |
| 9872 | { |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 9873 | if (logging != MagickFalse) |
| 9874 | { |
| 9875 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9876 | " Scaling ping_trans_color from (%d,%d,%d)", |
| 9877 | (int) ping_trans_color.red, |
| 9878 | (int) ping_trans_color.green, |
| 9879 | (int) ping_trans_color.blue); |
| 9880 | } |
| 9881 | |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 9882 | ping_trans_color.red*=0x0101; |
| 9883 | ping_trans_color.green*=0x0101; |
| 9884 | ping_trans_color.blue*=0x0101; |
| 9885 | ping_trans_color.gray*=0x0101; |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 9886 | |
| 9887 | if (logging != MagickFalse) |
| 9888 | { |
| 9889 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9890 | " to (%d,%d,%d)", |
| 9891 | (int) ping_trans_color.red, |
| 9892 | (int) ping_trans_color.green, |
| 9893 | (int) ping_trans_color.blue); |
| 9894 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9895 | } |
| 9896 | } |
| 9897 | |
cristy | 4383ec8 | 2011-01-05 15:42:32 +0000 | [diff] [blame] | 9898 | if (ping_bit_depth < (ssize_t) mng_info->write_png_depth) |
| 9899 | ping_bit_depth = (ssize_t) mng_info->write_png_depth; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 9900 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9901 | /* |
| 9902 | Adjust background and transparency samples in sub-8-bit grayscale files. |
| 9903 | */ |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 9904 | if (ping_bit_depth < 8 && ping_color_type == |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9905 | PNG_COLOR_TYPE_GRAY) |
| 9906 | { |
| 9907 | png_uint_16 |
| 9908 | maxval; |
| 9909 | |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 9910 | size_t |
| 9911 | one=1; |
| 9912 | |
cristy | 22ffd97 | 2010-06-03 16:51:47 +0000 | [diff] [blame] | 9913 | maxval=(png_uint_16) ((one << ping_bit_depth)-1); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9914 | |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 9915 | if (ping_exclude_bKGD == MagickFalse) |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 9916 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9917 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 9918 | ping_background.gray=(png_uint_16) ((maxval/65535.)* |
| 9919 | (ScaleQuantumToShort(((GetPixelInfoIntensity( |
| 9920 | &image->background_color))) +.5))); |
| 9921 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9922 | if (logging != MagickFalse) |
| 9923 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 9924 | " Setting up bKGD chunk (2)"); |
| 9925 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9926 | " background_color index is %d", |
| 9927 | (int) ping_background.index); |
glennrp | 3b51f0e | 2010-11-27 18:14:08 +0000 | [diff] [blame] | 9928 | |
glennrp | 991d11d | 2010-11-12 21:55:28 +0000 | [diff] [blame] | 9929 | ping_have_bKGD = MagickTrue; |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 9930 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9931 | |
glennrp | 3e3e20f | 2011-06-09 04:21:43 +0000 | [diff] [blame] | 9932 | if (logging != MagickFalse) |
| 9933 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9934 | " Scaling ping_trans_color.gray from %d", |
| 9935 | (int)ping_trans_color.gray); |
| 9936 | |
glennrp | 9be9b1c | 2011-06-09 12:21:45 +0000 | [diff] [blame] | 9937 | ping_trans_color.gray=(png_uint_16) ((maxval/255.)*( |
glennrp | 3e3e20f | 2011-06-09 04:21:43 +0000 | [diff] [blame] | 9938 | ping_trans_color.gray)+.5); |
| 9939 | |
| 9940 | if (logging != MagickFalse) |
| 9941 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9942 | " to %d", (int)ping_trans_color.gray); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9943 | } |
glennrp | 17a1485 | 2010-05-10 03:01:59 +0000 | [diff] [blame] | 9944 | |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 9945 | if (ping_exclude_bKGD == MagickFalse) |
| 9946 | { |
glennrp | 1273f7b | 2011-02-24 03:20:30 +0000 | [diff] [blame] | 9947 | if (mng_info->IsPalette && (int) ping_color_type == PNG_COLOR_TYPE_PALETTE) |
glennrp | 17a1485 | 2010-05-10 03:01:59 +0000 | [diff] [blame] | 9948 | { |
| 9949 | /* |
| 9950 | Identify which colormap entry is the background color. |
| 9951 | */ |
| 9952 | |
glennrp | 17a1485 | 2010-05-10 03:01:59 +0000 | [diff] [blame] | 9953 | number_colors=image_colors; |
| 9954 | |
glennrp | a521b2f | 2010-10-29 04:11:03 +0000 | [diff] [blame] | 9955 | for (i=0; i < (ssize_t) MagickMax(1L*number_colors,1L); i++) |
| 9956 | if (IsPNGColorEqual(image->background_color,image->colormap[i])) |
glennrp | 17a1485 | 2010-05-10 03:01:59 +0000 | [diff] [blame] | 9957 | break; |
| 9958 | |
| 9959 | ping_background.index=(png_byte) i; |
| 9960 | |
glennrp | 3b51f0e | 2010-11-27 18:14:08 +0000 | [diff] [blame] | 9961 | if (logging != MagickFalse) |
glennrp | a521b2f | 2010-10-29 04:11:03 +0000 | [diff] [blame] | 9962 | { |
| 9963 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9964 | " Setting up bKGD chunk with index=%d",(int) i); |
glennrp | a521b2f | 2010-10-29 04:11:03 +0000 | [diff] [blame] | 9965 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9966 | |
cristy | 13d0704 | 2010-11-21 20:56:18 +0000 | [diff] [blame] | 9967 | if (i < (ssize_t) number_colors) |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9968 | { |
| 9969 | ping_have_bKGD = MagickTrue; |
glennrp | 3b51f0e | 2010-11-27 18:14:08 +0000 | [diff] [blame] | 9970 | |
| 9971 | if (logging != MagickFalse) |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9972 | { |
| 9973 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9974 | " background =(%d,%d,%d)", |
| 9975 | (int) ping_background.red, |
| 9976 | (int) ping_background.green, |
| 9977 | (int) ping_background.blue); |
| 9978 | } |
| 9979 | } |
glennrp | a521b2f | 2010-10-29 04:11:03 +0000 | [diff] [blame] | 9980 | |
glennrp | d6bf161 | 2010-12-17 17:28:54 +0000 | [diff] [blame] | 9981 | else /* Can't happen */ |
glennrp | 3c21811 | 2010-11-27 15:31:26 +0000 | [diff] [blame] | 9982 | { |
glennrp | 3b51f0e | 2010-11-27 18:14:08 +0000 | [diff] [blame] | 9983 | if (logging != MagickFalse) |
| 9984 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9985 | " No room in PLTE to add bKGD color"); |
glennrp | 3c21811 | 2010-11-27 15:31:26 +0000 | [diff] [blame] | 9986 | ping_have_bKGD = MagickFalse; |
| 9987 | } |
glennrp | 17a1485 | 2010-05-10 03:01:59 +0000 | [diff] [blame] | 9988 | } |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 9989 | } |
glennrp | 17a1485 | 2010-05-10 03:01:59 +0000 | [diff] [blame] | 9990 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9991 | if (logging != MagickFalse) |
| 9992 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | 1a56e9c | 2012-04-25 17:06:57 +0000 | [diff] [blame] | 9993 | " PNG color type: %s (%d)", PngColorTypeToString(ping_color_type), |
| 9994 | ping_color_type); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9995 | /* |
| 9996 | Initialize compression level and filtering. |
| 9997 | */ |
| 9998 | if (logging != MagickFalse) |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9999 | { |
| 10000 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10001 | " Setting up deflate compression"); |
| 10002 | |
| 10003 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10004 | " Compression buffer size: 32768"); |
| 10005 | } |
| 10006 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10007 | png_set_compression_buffer_size(ping,32768L); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10008 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10009 | if (logging != MagickFalse) |
| 10010 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10011 | " Compression mem level: 9"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10012 | |
cristy | 4054bfb | 2011-08-29 23:41:39 +0000 | [diff] [blame] | 10013 | png_set_compression_mem_level(ping, 9); |
| 10014 | |
glennrp | 10d739e | 2011-06-29 18:00:52 +0000 | [diff] [blame] | 10015 | /* Untangle the "-quality" setting: |
| 10016 | |
| 10017 | Undefined is 0; the default is used. |
| 10018 | Default is 75 |
| 10019 | |
| 10020 | 10's digit: |
| 10021 | |
| 10022 | 0: Use Z_HUFFMAN_ONLY strategy with the |
| 10023 | zlib default compression level |
| 10024 | |
| 10025 | 1-9: the zlib compression level |
| 10026 | |
| 10027 | 1's digit: |
| 10028 | |
| 10029 | 0-4: the PNG filter method |
| 10030 | |
| 10031 | 5: libpng adaptive filtering if compression level > 5 |
| 10032 | libpng filter type "none" if compression level <= 5 |
| 10033 | or if image is grayscale or palette |
glennrp | 750105b | 2012-04-25 16:20:45 +0000 | [diff] [blame] | 10034 | |
glennrp | 10d739e | 2011-06-29 18:00:52 +0000 | [diff] [blame] | 10035 | 6: libpng adaptive filtering |
| 10036 | |
| 10037 | 7: "LOCO" filtering (intrapixel differing) if writing |
| 10038 | a MNG, othewise "none". Did not work in IM-6.7.0-9 |
| 10039 | and earlier because of a missing "else". |
| 10040 | |
| 10041 | 8: Z_RLE strategy, all filters |
glennrp | 1868258 | 2011-06-30 18:11:47 +0000 | [diff] [blame] | 10042 | Unused prior to IM-6.7.0-10, was same as 6 |
glennrp | 10d739e | 2011-06-29 18:00:52 +0000 | [diff] [blame] | 10043 | |
| 10044 | 9: Z_RLE strategy, no PNG filters |
glennrp | 1868258 | 2011-06-30 18:11:47 +0000 | [diff] [blame] | 10045 | Unused prior to IM-6.7.0-10, was same as 6 |
glennrp | 10d739e | 2011-06-29 18:00:52 +0000 | [diff] [blame] | 10046 | |
| 10047 | Note that using the -quality option, not all combinations of |
| 10048 | PNG filter type, zlib compression level, and zlib compression |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 10049 | strategy are possible. This will be addressed soon in a |
| 10050 | release that accomodates "-define png:compression-strategy", etc. |
glennrp | 10d739e | 2011-06-29 18:00:52 +0000 | [diff] [blame] | 10051 | |
| 10052 | */ |
| 10053 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10054 | quality=image->quality == UndefinedCompressionQuality ? 75UL : |
| 10055 | image->quality; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10056 | |
glennrp | 1868258 | 2011-06-30 18:11:47 +0000 | [diff] [blame] | 10057 | if (quality <= 9) |
| 10058 | { |
| 10059 | if (mng_info->write_png_compression_strategy == 0) |
| 10060 | mng_info->write_png_compression_strategy = Z_HUFFMAN_ONLY+1; |
| 10061 | } |
glennrp | 750105b | 2012-04-25 16:20:45 +0000 | [diff] [blame] | 10062 | |
glennrp | 1868258 | 2011-06-30 18:11:47 +0000 | [diff] [blame] | 10063 | else if (mng_info->write_png_compression_level == 0) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10064 | { |
| 10065 | int |
| 10066 | level; |
| 10067 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 10068 | level=(int) MagickMin((ssize_t) quality/10,9); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10069 | |
glennrp | 1868258 | 2011-06-30 18:11:47 +0000 | [diff] [blame] | 10070 | mng_info->write_png_compression_level = level+1; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10071 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10072 | |
glennrp | 1868258 | 2011-06-30 18:11:47 +0000 | [diff] [blame] | 10073 | if (mng_info->write_png_compression_strategy == 0) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10074 | { |
glennrp | 1868258 | 2011-06-30 18:11:47 +0000 | [diff] [blame] | 10075 | if ((quality %10) == 8 || (quality %10) == 9) |
glennrp | a24b245 | 2012-06-27 11:38:38 +0000 | [diff] [blame] | 10076 | #ifdef Z_RLE /* Z_RLE was added to zlib-1.2.0 */ |
| 10077 | mng_info->write_png_compression_strategy=Z_RLE+1; |
| 10078 | #else |
| 10079 | mng_info->write_png_compression_strategy = Z_DEFAULT_STRATEGY+1; |
| 10080 | #endif |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10081 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10082 | |
glennrp | 1868258 | 2011-06-30 18:11:47 +0000 | [diff] [blame] | 10083 | if (mng_info->write_png_compression_filter == 0) |
| 10084 | mng_info->write_png_compression_filter=((int) quality % 10) + 1; |
| 10085 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10086 | if (logging != MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10087 | { |
glennrp | 1868258 | 2011-06-30 18:11:47 +0000 | [diff] [blame] | 10088 | if (mng_info->write_png_compression_level) |
| 10089 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10090 | " Compression level: %d", |
| 10091 | (int) mng_info->write_png_compression_level-1); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10092 | |
glennrp | 1868258 | 2011-06-30 18:11:47 +0000 | [diff] [blame] | 10093 | if (mng_info->write_png_compression_strategy) |
| 10094 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10095 | " Compression strategy: %d", |
| 10096 | (int) mng_info->write_png_compression_strategy-1); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10097 | |
glennrp | 1868258 | 2011-06-30 18:11:47 +0000 | [diff] [blame] | 10098 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10099 | " Setting up filtering"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10100 | |
cristy | 4054bfb | 2011-08-29 23:41:39 +0000 | [diff] [blame] | 10101 | if (mng_info->write_png_compression_filter == 6) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10102 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10103 | " Base filter method: ADAPTIVE"); |
cristy | 4054bfb | 2011-08-29 23:41:39 +0000 | [diff] [blame] | 10104 | else if (mng_info->write_png_compression_filter == 0 || |
| 10105 | mng_info->write_png_compression_filter == 1) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10106 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10107 | " Base filter method: NONE"); |
glennrp | 1868258 | 2011-06-30 18:11:47 +0000 | [diff] [blame] | 10108 | else |
| 10109 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10110 | " Base filter method: %d", |
| 10111 | (int) mng_info->write_png_compression_filter-1); |
| 10112 | } |
glennrp | 2b013e4 | 2010-11-24 16:55:50 +0000 | [diff] [blame] | 10113 | |
glennrp | 1868258 | 2011-06-30 18:11:47 +0000 | [diff] [blame] | 10114 | if (mng_info->write_png_compression_level != 0) |
| 10115 | png_set_compression_level(ping,mng_info->write_png_compression_level-1); |
| 10116 | |
| 10117 | if (mng_info->write_png_compression_filter == 6) |
| 10118 | { |
| 10119 | if (((int) ping_color_type == PNG_COLOR_TYPE_GRAY) || |
| 10120 | ((int) ping_color_type == PNG_COLOR_TYPE_PALETTE) || |
| 10121 | (quality < 50)) |
| 10122 | png_set_filter(ping,PNG_FILTER_TYPE_BASE,PNG_NO_FILTERS); |
| 10123 | else |
| 10124 | png_set_filter(ping,PNG_FILTER_TYPE_BASE,PNG_ALL_FILTERS); |
| 10125 | } |
cristy | 4054bfb | 2011-08-29 23:41:39 +0000 | [diff] [blame] | 10126 | else if (mng_info->write_png_compression_filter == 7 || |
glennrp | 1868258 | 2011-06-30 18:11:47 +0000 | [diff] [blame] | 10127 | mng_info->write_png_compression_filter == 10) |
| 10128 | png_set_filter(ping,PNG_FILTER_TYPE_BASE,PNG_ALL_FILTERS); |
| 10129 | |
| 10130 | else if (mng_info->write_png_compression_filter == 8) |
| 10131 | { |
| 10132 | #if defined(PNG_MNG_FEATURES_SUPPORTED) && defined(PNG_INTRAPIXEL_DIFFERENCING) |
| 10133 | if (mng_info->write_mng) |
| 10134 | { |
| 10135 | if (((int) ping_color_type == PNG_COLOR_TYPE_RGB) || |
| 10136 | ((int) ping_color_type == PNG_COLOR_TYPE_RGBA)) |
| 10137 | ping_filter_method=PNG_INTRAPIXEL_DIFFERENCING; |
| 10138 | } |
| 10139 | #endif |
cristy | 4054bfb | 2011-08-29 23:41:39 +0000 | [diff] [blame] | 10140 | png_set_filter(ping,PNG_FILTER_TYPE_BASE,PNG_NO_FILTERS); |
glennrp | 1868258 | 2011-06-30 18:11:47 +0000 | [diff] [blame] | 10141 | } |
| 10142 | |
| 10143 | else if (mng_info->write_png_compression_filter == 9) |
| 10144 | png_set_filter(ping,PNG_FILTER_TYPE_BASE,PNG_NO_FILTERS); |
| 10145 | |
| 10146 | else if (mng_info->write_png_compression_filter != 0) |
| 10147 | png_set_filter(ping,PNG_FILTER_TYPE_BASE, |
| 10148 | mng_info->write_png_compression_filter-1); |
| 10149 | |
| 10150 | if (mng_info->write_png_compression_strategy != 0) |
| 10151 | png_set_compression_strategy(ping, |
| 10152 | mng_info->write_png_compression_strategy-1); |
| 10153 | |
cristy | 0d57eec | 2011-09-04 22:13:56 +0000 | [diff] [blame] | 10154 | /* Only write the iCCP chunk if we are not writing the sRGB chunk. */ |
| 10155 | if (ping_exclude_sRGB != MagickFalse || |
| 10156 | (image->rendering_intent == UndefinedIntent)) |
| 10157 | { |
| 10158 | if ((ping_exclude_tEXt == MagickFalse || |
| 10159 | ping_exclude_zTXt == MagickFalse) && |
| 10160 | (ping_exclude_iCCP == MagickFalse || ping_exclude_zCCP == MagickFalse)) |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 10161 | { |
| 10162 | ResetImageProfileIterator(image); |
| 10163 | for (name=GetNextImageProfile(image); name != (const char *) NULL; ) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10164 | { |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 10165 | profile=GetImageProfile(image,name); |
| 10166 | |
| 10167 | if (profile != (StringInfo *) NULL) |
| 10168 | { |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 10169 | #ifdef PNG_WRITE_iCCP_SUPPORTED |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 10170 | if ((LocaleCompare(name,"ICC") == 0) || |
| 10171 | (LocaleCompare(name,"ICM") == 0)) |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10172 | { |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 10173 | |
| 10174 | if (ping_exclude_iCCP == MagickFalse) |
| 10175 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 10176 | png_set_iCCP(ping,ping_info,(png_charp) name,0, |
glennrp | e4017e3 | 2011-01-08 17:16:09 +0000 | [diff] [blame] | 10177 | #if (PNG_LIBPNG_VER < 10500) |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 10178 | (png_charp) GetStringInfoDatum(profile), |
glennrp | e4017e3 | 2011-01-08 17:16:09 +0000 | [diff] [blame] | 10179 | #else |
| 10180 | (png_const_bytep) GetStringInfoDatum(profile), |
| 10181 | #endif |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 10182 | (png_uint_32) GetStringInfoLength(profile)); |
| 10183 | } |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10184 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10185 | |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 10186 | else |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10187 | #endif |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 10188 | if (ping_exclude_zCCP == MagickFalse) |
| 10189 | { |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 10190 | Magick_png_write_raw_profile(image_info,ping,ping_info, |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 10191 | (unsigned char *) name,(unsigned char *) name, |
| 10192 | GetStringInfoDatum(profile), |
| 10193 | (png_uint_32) GetStringInfoLength(profile)); |
| 10194 | } |
| 10195 | } |
glennrp | 0b206f5 | 2011-01-07 04:55:32 +0000 | [diff] [blame] | 10196 | |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 10197 | if (logging != MagickFalse) |
| 10198 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10199 | " Setting up text chunk with %s profile",name); |
| 10200 | |
| 10201 | name=GetNextImageProfile(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10202 | } |
cristy | 0d57eec | 2011-09-04 22:13:56 +0000 | [diff] [blame] | 10203 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10204 | } |
| 10205 | |
| 10206 | #if defined(PNG_WRITE_sRGB_SUPPORTED) |
| 10207 | if ((mng_info->have_write_global_srgb == 0) && |
glennrp | 5f11bf9 | 2012-05-05 03:21:03 +0000 | [diff] [blame] | 10208 | (image->rendering_intent != UndefinedIntent)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10209 | { |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10210 | if (ping_exclude_sRGB == MagickFalse) |
| 10211 | { |
| 10212 | /* |
| 10213 | Note image rendering intent. |
| 10214 | */ |
| 10215 | if (logging != MagickFalse) |
| 10216 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10217 | " Setting up sRGB chunk"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10218 | |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10219 | (void) png_set_sRGB(ping,ping_info,( |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 10220 | Magick_RenderingIntent_to_PNG_RenderingIntent( |
| 10221 | image->rendering_intent))); |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10222 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10223 | } |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10224 | |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 10225 | 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] | 10226 | #endif |
| 10227 | { |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10228 | if (ping_exclude_gAMA == MagickFalse && |
| 10229 | (ping_exclude_sRGB == MagickFalse || |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10230 | (image->gamma < .45 || image->gamma > .46))) |
| 10231 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10232 | if ((mng_info->have_write_global_gama == 0) && (image->gamma != 0.0)) |
| 10233 | { |
| 10234 | /* |
| 10235 | Note image gamma. |
| 10236 | To do: check for cHRM+gAMA == sRGB, and write sRGB instead. |
| 10237 | */ |
| 10238 | if (logging != MagickFalse) |
| 10239 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10240 | " Setting up gAMA chunk"); |
glennrp | 3b51f0e | 2010-11-27 18:14:08 +0000 | [diff] [blame] | 10241 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10242 | png_set_gAMA(ping,ping_info,image->gamma); |
| 10243 | } |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10244 | } |
glennrp | 2b013e4 | 2010-11-24 16:55:50 +0000 | [diff] [blame] | 10245 | |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10246 | if (ping_exclude_cHRM == MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10247 | { |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10248 | if ((mng_info->have_write_global_chrm == 0) && |
| 10249 | (image->chromaticity.red_primary.x != 0.0)) |
| 10250 | { |
| 10251 | /* |
| 10252 | Note image chromaticity. |
| 10253 | To do: check for cHRM+gAMA == sRGB, and write sRGB instead. |
| 10254 | */ |
| 10255 | PrimaryInfo |
| 10256 | bp, |
| 10257 | gp, |
| 10258 | rp, |
| 10259 | wp; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10260 | |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10261 | wp=image->chromaticity.white_point; |
| 10262 | rp=image->chromaticity.red_primary; |
| 10263 | gp=image->chromaticity.green_primary; |
| 10264 | bp=image->chromaticity.blue_primary; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10265 | |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10266 | if (logging != MagickFalse) |
| 10267 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10268 | " Setting up cHRM chunk"); |
glennrp | 3b51f0e | 2010-11-27 18:14:08 +0000 | [diff] [blame] | 10269 | |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10270 | png_set_cHRM(ping,ping_info,wp.x,wp.y,rp.x,rp.y,gp.x,gp.y, |
| 10271 | bp.x,bp.y); |
| 10272 | } |
| 10273 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10274 | } |
glennrp | dfd7080 | 2010-11-14 01:23:35 +0000 | [diff] [blame] | 10275 | |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 10276 | ping_interlace_method=image_info->interlace != NoInterlace; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10277 | |
| 10278 | if (mng_info->write_mng) |
| 10279 | png_set_sig_bytes(ping,8); |
| 10280 | |
cristy | 5d6fc9c | 2011-12-27 03:10:42 +0000 | [diff] [blame] | 10281 | /* Bail out if cannot meet defined png:bit-depth or png:color-type */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10282 | |
glennrp | d6bf161 | 2010-12-17 17:28:54 +0000 | [diff] [blame] | 10283 | if (mng_info->write_png_colortype != 0) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10284 | { |
| 10285 | if (mng_info->write_png_colortype-1 == PNG_COLOR_TYPE_GRAY) |
glennrp | 8d57966 | 2011-02-23 02:05:02 +0000 | [diff] [blame] | 10286 | if (ping_have_color != MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10287 | { |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 10288 | ping_color_type = PNG_COLOR_TYPE_RGB; |
glennrp | 2b013e4 | 2010-11-24 16:55:50 +0000 | [diff] [blame] | 10289 | |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 10290 | if (ping_bit_depth < 8) |
| 10291 | ping_bit_depth=8; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10292 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10293 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10294 | if (mng_info->write_png_colortype-1 == PNG_COLOR_TYPE_GRAY_ALPHA) |
glennrp | 8d57966 | 2011-02-23 02:05:02 +0000 | [diff] [blame] | 10295 | if (ping_have_color != MagickFalse) |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 10296 | ping_color_type = PNG_COLOR_TYPE_RGB_ALPHA; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10297 | } |
| 10298 | |
glennrp | 0e8ea19 | 2010-12-24 18:00:33 +0000 | [diff] [blame] | 10299 | if (ping_need_colortype_warning != MagickFalse || |
| 10300 | ((mng_info->write_png_depth && |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 10301 | (int) mng_info->write_png_depth != ping_bit_depth) || |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10302 | (mng_info->write_png_colortype && |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 10303 | ((int) mng_info->write_png_colortype-1 != ping_color_type && |
glennrp | 991e92a | 2010-01-28 03:09:00 +0000 | [diff] [blame] | 10304 | mng_info->write_png_colortype != 7 && |
glennrp | 0e8ea19 | 2010-12-24 18:00:33 +0000 | [diff] [blame] | 10305 | !(mng_info->write_png_colortype == 5 && ping_color_type == 0))))) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10306 | { |
| 10307 | if (logging != MagickFalse) |
| 10308 | { |
glennrp | 0e8ea19 | 2010-12-24 18:00:33 +0000 | [diff] [blame] | 10309 | if (ping_need_colortype_warning != MagickFalse) |
| 10310 | { |
| 10311 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10312 | " Image has transparency but tRNS chunk was excluded"); |
| 10313 | } |
| 10314 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10315 | if (mng_info->write_png_depth) |
| 10316 | { |
| 10317 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | 5d6fc9c | 2011-12-27 03:10:42 +0000 | [diff] [blame] | 10318 | " Defined png:bit-depth=%u, Computed depth=%u", |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10319 | mng_info->write_png_depth, |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 10320 | ping_bit_depth); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10321 | } |
glennrp | 0e8ea19 | 2010-12-24 18:00:33 +0000 | [diff] [blame] | 10322 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10323 | if (mng_info->write_png_colortype) |
| 10324 | { |
| 10325 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | 5d6fc9c | 2011-12-27 03:10:42 +0000 | [diff] [blame] | 10326 | " Defined png:color-type=%u, Computed color type=%u", |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10327 | mng_info->write_png_colortype-1, |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 10328 | ping_color_type); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10329 | } |
| 10330 | } |
glennrp | 0e8ea19 | 2010-12-24 18:00:33 +0000 | [diff] [blame] | 10331 | |
glennrp | 3bd2e41 | 2010-08-10 13:34:52 +0000 | [diff] [blame] | 10332 | png_warning(ping, |
cristy | 5d6fc9c | 2011-12-27 03:10:42 +0000 | [diff] [blame] | 10333 | "Cannot write image with defined png:bit-depth or png:color-type."); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10334 | } |
| 10335 | |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 10336 | if (image_matte != MagickFalse && image->alpha_trait != BlendPixelTrait) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10337 | { |
| 10338 | /* Add an opaque matte channel */ |
cristy | b0a657e | 2012-08-29 00:45:37 +0000 | [diff] [blame] | 10339 | image->alpha_trait = BlendPixelTrait; |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 10340 | (void) SetImageAlpha(image,OpaqueAlpha,exception); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10341 | |
glennrp | b4a1341 | 2010-05-05 12:47:19 +0000 | [diff] [blame] | 10342 | if (logging != MagickFalse) |
| 10343 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10344 | " Added an opaque matte channel"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10345 | } |
| 10346 | |
glennrp | 0e31973 | 2011-01-25 21:53:13 +0000 | [diff] [blame] | 10347 | if (number_transparent != 0 || number_semitransparent != 0) |
glennrp | e9c26dc | 2010-05-30 01:56:35 +0000 | [diff] [blame] | 10348 | { |
glennrp | 991d11d | 2010-11-12 21:55:28 +0000 | [diff] [blame] | 10349 | if (ping_color_type < 4) |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 10350 | { |
glennrp | 991d11d | 2010-11-12 21:55:28 +0000 | [diff] [blame] | 10351 | ping_have_tRNS=MagickTrue; |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 10352 | if (logging != MagickFalse) |
| 10353 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10354 | " Setting ping_have_tRNS=MagickTrue."); |
| 10355 | } |
glennrp | e9c26dc | 2010-05-30 01:56:35 +0000 | [diff] [blame] | 10356 | } |
| 10357 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10358 | if (logging != MagickFalse) |
| 10359 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10360 | " Writing PNG header chunks"); |
| 10361 | |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 10362 | png_set_IHDR(ping,ping_info,ping_width,ping_height, |
| 10363 | ping_bit_depth,ping_color_type, |
| 10364 | ping_interlace_method,ping_compression_method, |
| 10365 | ping_filter_method); |
| 10366 | |
glennrp | 39992b4 | 2010-11-14 00:03:43 +0000 | [diff] [blame] | 10367 | if (ping_color_type == 3 && ping_have_PLTE != MagickFalse) |
| 10368 | { |
glennrp | f09bded | 2011-01-08 01:15:59 +0000 | [diff] [blame] | 10369 | png_set_PLTE(ping,ping_info,palette,number_colors); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10370 | |
glennrp | 3b51f0e | 2010-11-27 18:14:08 +0000 | [diff] [blame] | 10371 | if (logging != MagickFalse) |
glennrp | 39992b4 | 2010-11-14 00:03:43 +0000 | [diff] [blame] | 10372 | { |
glennrp | 8640fb5 | 2010-11-23 15:48:26 +0000 | [diff] [blame] | 10373 | for (i=0; i< (ssize_t) number_colors; i++) |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10374 | { |
glennrp | d6bf161 | 2010-12-17 17:28:54 +0000 | [diff] [blame] | 10375 | if (i < ping_num_trans) |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10376 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | d6bf161 | 2010-12-17 17:28:54 +0000 | [diff] [blame] | 10377 | " PLTE[%d] = (%d,%d,%d), tRNS[%d] = (%d)", |
| 10378 | (int) i, |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10379 | (int) palette[i].red, |
| 10380 | (int) palette[i].green, |
| 10381 | (int) palette[i].blue, |
glennrp | d6bf161 | 2010-12-17 17:28:54 +0000 | [diff] [blame] | 10382 | (int) i, |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10383 | (int) ping_trans_alpha[i]); |
| 10384 | else |
| 10385 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | d6bf161 | 2010-12-17 17:28:54 +0000 | [diff] [blame] | 10386 | " PLTE[%d] = (%d,%d,%d)", |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10387 | (int) i, |
| 10388 | (int) palette[i].red, |
| 10389 | (int) palette[i].green, |
| 10390 | (int) palette[i].blue); |
| 10391 | } |
glennrp | 39992b4 | 2010-11-14 00:03:43 +0000 | [diff] [blame] | 10392 | } |
glennrp | 39992b4 | 2010-11-14 00:03:43 +0000 | [diff] [blame] | 10393 | } |
| 10394 | |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10395 | if (ping_exclude_bKGD == MagickFalse) |
glennrp | dfd7080 | 2010-11-14 01:23:35 +0000 | [diff] [blame] | 10396 | { |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10397 | if (ping_have_bKGD != MagickFalse) |
glennrp | c6c391a | 2011-04-27 02:23:56 +0000 | [diff] [blame] | 10398 | { |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10399 | png_set_bKGD(ping,ping_info,&ping_background); |
glennrp | c6c391a | 2011-04-27 02:23:56 +0000 | [diff] [blame] | 10400 | if (logging) |
| 10401 | { |
| 10402 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10403 | " Setting up bKGD chunk"); |
| 10404 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10405 | " background color = (%d,%d,%d)", |
| 10406 | (int) ping_background.red, |
| 10407 | (int) ping_background.green, |
| 10408 | (int) ping_background.blue); |
| 10409 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10410 | " index = %d, gray=%d", |
| 10411 | (int) ping_background.index, |
| 10412 | (int) ping_background.gray); |
| 10413 | } |
| 10414 | } |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10415 | } |
| 10416 | |
| 10417 | if (ping_exclude_pHYs == MagickFalse) |
| 10418 | { |
| 10419 | if (ping_have_pHYs != MagickFalse) |
| 10420 | { |
| 10421 | png_set_pHYs(ping,ping_info, |
| 10422 | ping_pHYs_x_resolution, |
| 10423 | ping_pHYs_y_resolution, |
| 10424 | ping_pHYs_unit_type); |
glennrp | 823b55c | 2011-03-14 18:46:46 +0000 | [diff] [blame] | 10425 | |
| 10426 | if (logging) |
| 10427 | { |
| 10428 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10429 | " Setting up pHYs chunk"); |
| 10430 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10431 | " x_resolution=%lu", |
| 10432 | (unsigned long) ping_pHYs_x_resolution); |
| 10433 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10434 | " y_resolution=%lu", |
| 10435 | (unsigned long) ping_pHYs_y_resolution); |
| 10436 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10437 | " unit_type=%lu", |
| 10438 | (unsigned long) ping_pHYs_unit_type); |
| 10439 | } |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10440 | } |
glennrp | dfd7080 | 2010-11-14 01:23:35 +0000 | [diff] [blame] | 10441 | } |
| 10442 | |
| 10443 | #if defined(PNG_oFFs_SUPPORTED) |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 10444 | if (ping_exclude_oFFs == MagickFalse) |
glennrp | dfd7080 | 2010-11-14 01:23:35 +0000 | [diff] [blame] | 10445 | { |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10446 | if (image->page.x || image->page.y) |
| 10447 | { |
| 10448 | png_set_oFFs(ping,ping_info,(png_int_32) image->page.x, |
| 10449 | (png_int_32) image->page.y, 0); |
glennrp | dfd7080 | 2010-11-14 01:23:35 +0000 | [diff] [blame] | 10450 | |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10451 | if (logging != MagickFalse) |
| 10452 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10453 | " Setting up oFFs chunk with x=%d, y=%d, units=0", |
| 10454 | (int) image->page.x, (int) image->page.y); |
| 10455 | } |
glennrp | dfd7080 | 2010-11-14 01:23:35 +0000 | [diff] [blame] | 10456 | } |
| 10457 | #endif |
| 10458 | |
glennrp | da8f3a7 | 2011-02-27 23:54:12 +0000 | [diff] [blame] | 10459 | if (mng_info->need_blob != MagickFalse) |
| 10460 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 10461 | if (OpenBlob(image_info,image,WriteBinaryBlobMode,exception) == |
glennrp | da8f3a7 | 2011-02-27 23:54:12 +0000 | [diff] [blame] | 10462 | MagickFalse) |
| 10463 | png_error(ping,"WriteBlob Failed"); |
| 10464 | |
| 10465 | ping_have_blob=MagickTrue; |
| 10466 | } |
| 10467 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10468 | png_write_info_before_PLTE(ping, ping_info); |
glennrp | 991d11d | 2010-11-12 21:55:28 +0000 | [diff] [blame] | 10469 | |
glennrp | 39992b4 | 2010-11-14 00:03:43 +0000 | [diff] [blame] | 10470 | if (ping_have_tRNS != MagickFalse && ping_color_type < 4) |
glennrp | 991d11d | 2010-11-12 21:55:28 +0000 | [diff] [blame] | 10471 | { |
glennrp | 3b51f0e | 2010-11-27 18:14:08 +0000 | [diff] [blame] | 10472 | if (logging != MagickFalse) |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10473 | { |
| 10474 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10475 | " Calling png_set_tRNS with num_trans=%d",ping_num_trans); |
| 10476 | } |
| 10477 | |
| 10478 | if (ping_color_type == 3) |
| 10479 | (void) png_set_tRNS(ping, ping_info, |
| 10480 | ping_trans_alpha, |
| 10481 | ping_num_trans, |
| 10482 | NULL); |
| 10483 | |
| 10484 | else |
| 10485 | { |
| 10486 | (void) png_set_tRNS(ping, ping_info, |
| 10487 | NULL, |
| 10488 | 0, |
| 10489 | &ping_trans_color); |
| 10490 | |
glennrp | 3b51f0e | 2010-11-27 18:14:08 +0000 | [diff] [blame] | 10491 | if (logging != MagickFalse) |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10492 | { |
| 10493 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 10494 | " tRNS color =(%d,%d,%d)", |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10495 | (int) ping_trans_color.red, |
| 10496 | (int) ping_trans_color.green, |
| 10497 | (int) ping_trans_color.blue); |
| 10498 | } |
| 10499 | } |
glennrp | 991d11d | 2010-11-12 21:55:28 +0000 | [diff] [blame] | 10500 | } |
| 10501 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10502 | /* write any png-chunk-b profiles */ |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 10503 | (void) Magick_png_write_chunk_from_profile(image,"PNG-chunk-b",logging); |
glennrp | da8f3a7 | 2011-02-27 23:54:12 +0000 | [diff] [blame] | 10504 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10505 | png_write_info(ping,ping_info); |
glennrp | 991d11d | 2010-11-12 21:55:28 +0000 | [diff] [blame] | 10506 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10507 | /* write any PNG-chunk-m profiles */ |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 10508 | (void) Magick_png_write_chunk_from_profile(image,"PNG-chunk-m",logging); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10509 | |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10510 | if (ping_exclude_vpAg == MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10511 | { |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 10512 | if ((image->page.width != 0 && image->page.width != image->columns) || |
| 10513 | (image->page.height != 0 && image->page.height != image->rows)) |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10514 | { |
| 10515 | unsigned char |
| 10516 | chunk[14]; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10517 | |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10518 | (void) WriteBlobMSBULong(image,9L); /* data length=8 */ |
| 10519 | PNGType(chunk,mng_vpAg); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 10520 | LogPNGChunk(logging,mng_vpAg,9L); |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10521 | PNGLong(chunk+4,(png_uint_32) image->page.width); |
| 10522 | PNGLong(chunk+8,(png_uint_32) image->page.height); |
| 10523 | chunk[12]=0; /* unit = pixels */ |
| 10524 | (void) WriteBlob(image,13,chunk); |
| 10525 | (void) WriteBlobMSBULong(image,crc32(0,chunk,13)); |
| 10526 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10527 | } |
| 10528 | |
| 10529 | #if (PNG_LIBPNG_VER == 10206) |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 10530 | /* avoid libpng-1.2.6 bug by setting PNG_HAVE_IDAT flag */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10531 | #define PNG_HAVE_IDAT 0x04 |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 10532 | ping->mode |= PNG_HAVE_IDAT; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10533 | #undef PNG_HAVE_IDAT |
| 10534 | #endif |
| 10535 | |
| 10536 | png_set_packing(ping); |
| 10537 | /* |
| 10538 | Allocate memory. |
| 10539 | */ |
| 10540 | rowbytes=image->columns; |
glennrp | b4a1341 | 2010-05-05 12:47:19 +0000 | [diff] [blame] | 10541 | if (image_depth > 8) |
| 10542 | rowbytes*=2; |
cristy | 7202c10 | 2010-05-05 19:18:28 +0000 | [diff] [blame] | 10543 | switch (ping_color_type) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10544 | { |
glennrp | b4a1341 | 2010-05-05 12:47:19 +0000 | [diff] [blame] | 10545 | case PNG_COLOR_TYPE_RGB: |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10546 | rowbytes*=3; |
glennrp | b4a1341 | 2010-05-05 12:47:19 +0000 | [diff] [blame] | 10547 | break; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10548 | |
glennrp | b4a1341 | 2010-05-05 12:47:19 +0000 | [diff] [blame] | 10549 | case PNG_COLOR_TYPE_GRAY_ALPHA: |
| 10550 | rowbytes*=2; |
| 10551 | break; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10552 | |
glennrp | b4a1341 | 2010-05-05 12:47:19 +0000 | [diff] [blame] | 10553 | case PNG_COLOR_TYPE_RGBA: |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10554 | rowbytes*=4; |
glennrp | b4a1341 | 2010-05-05 12:47:19 +0000 | [diff] [blame] | 10555 | break; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10556 | |
glennrp | b4a1341 | 2010-05-05 12:47:19 +0000 | [diff] [blame] | 10557 | default: |
| 10558 | break; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10559 | } |
glennrp | 3b51f0e | 2010-11-27 18:14:08 +0000 | [diff] [blame] | 10560 | |
| 10561 | if (logging != MagickFalse) |
glennrp | b4a1341 | 2010-05-05 12:47:19 +0000 | [diff] [blame] | 10562 | { |
| 10563 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10564 | " Writing PNG image data"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10565 | |
glennrp | b4a1341 | 2010-05-05 12:47:19 +0000 | [diff] [blame] | 10566 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 10567 | " Allocating %.20g bytes of memory for pixels",(double) rowbytes); |
glennrp | b4a1341 | 2010-05-05 12:47:19 +0000 | [diff] [blame] | 10568 | } |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 10569 | ping_pixels=(unsigned char *) AcquireQuantumMemory(rowbytes, |
| 10570 | sizeof(*ping_pixels)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10571 | |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 10572 | if (ping_pixels == (unsigned char *) NULL) |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 10573 | png_error(ping,"Allocation of memory for pixels failed"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10574 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10575 | /* |
| 10576 | Initialize image scanlines. |
| 10577 | */ |
cristy | ed55252 | 2009-10-16 14:04:35 +0000 | [diff] [blame] | 10578 | quantum_info=AcquireQuantumInfo(image_info,image); |
| 10579 | if (quantum_info == (QuantumInfo *) NULL) |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 10580 | png_error(ping,"Memory allocation for quantum_info failed"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10581 | quantum_info->format=UndefinedQuantumFormat; |
| 10582 | quantum_info->depth=image_depth; |
glennrp | 4b840d7 | 2012-11-22 16:01:16 +0000 | [diff] [blame] | 10583 | (void) SetQuantumEndian(image,quantum_info,MSBEndian); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10584 | num_passes=png_set_interlace_handling(ping); |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 10585 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10586 | if ((!mng_info->write_png8 && !mng_info->write_png24 && |
glennrp | fd164d2 | 2013-01-26 21:10:22 +0000 | [diff] [blame] | 10587 | !mng_info->write_png48 && !mng_info->write_png64 && |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 10588 | !mng_info->write_png32) && |
| 10589 | (mng_info->IsPalette || |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10590 | (image_info->type == BilevelType)) && |
glennrp | 8d57966 | 2011-02-23 02:05:02 +0000 | [diff] [blame] | 10591 | image_matte == MagickFalse && |
| 10592 | ping_have_non_bw == MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10593 | { |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 10594 | /* Palette, Bilevel, or Opaque Monochrome */ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 10595 | register const Quantum |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10596 | *p; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10597 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10598 | quantum_info->depth=8; |
| 10599 | for (pass=0; pass < num_passes; pass++) |
| 10600 | { |
| 10601 | /* |
| 10602 | Convert PseudoClass image to a PNG monochrome image. |
| 10603 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 10604 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10605 | { |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 10606 | if (logging != MagickFalse && y == 0) |
glennrp | 3b51f0e | 2010-11-27 18:14:08 +0000 | [diff] [blame] | 10607 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10608 | " Writing row of pixels (0)"); |
glennrp | a521b2f | 2010-10-29 04:11:03 +0000 | [diff] [blame] | 10609 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 10610 | p=GetVirtualPixels(image,0,y,image->columns,1,exception); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10611 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 10612 | if (p == (const Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10613 | break; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10614 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10615 | if (mng_info->IsPalette) |
| 10616 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 10617 | (void) ExportQuantumPixels(image,(CacheView *) NULL, |
| 10618 | quantum_info,GrayQuantum,ping_pixels,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10619 | if (mng_info->write_png_colortype-1 == PNG_COLOR_TYPE_PALETTE && |
| 10620 | mng_info->write_png_depth && |
| 10621 | mng_info->write_png_depth != old_bit_depth) |
| 10622 | { |
| 10623 | /* Undo pixel scaling */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 10624 | for (i=0; i < (ssize_t) image->columns; i++) |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 10625 | *(ping_pixels+i)=(unsigned char) (*(ping_pixels+i) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10626 | >> (8-old_bit_depth)); |
| 10627 | } |
| 10628 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10629 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10630 | else |
| 10631 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 10632 | (void) ExportQuantumPixels(image,(CacheView *) NULL, |
| 10633 | quantum_info,RedQuantum,ping_pixels,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10634 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10635 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10636 | if (mng_info->write_png_colortype-1 != PNG_COLOR_TYPE_PALETTE) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 10637 | for (i=0; i < (ssize_t) image->columns; i++) |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 10638 | *(ping_pixels+i)=(unsigned char) ((*(ping_pixels+i) > 127) ? |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10639 | 255 : 0); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10640 | |
glennrp | 3b51f0e | 2010-11-27 18:14:08 +0000 | [diff] [blame] | 10641 | if (logging != MagickFalse && y == 0) |
glennrp | b4a1341 | 2010-05-05 12:47:19 +0000 | [diff] [blame] | 10642 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10643 | " Writing row of pixels (1)"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10644 | |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 10645 | png_write_row(ping,ping_pixels); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10646 | } |
| 10647 | if (image->previous == (Image *) NULL) |
| 10648 | { |
| 10649 | status=SetImageProgress(image,LoadImageTag,pass,num_passes); |
| 10650 | if (status == MagickFalse) |
| 10651 | break; |
| 10652 | } |
| 10653 | } |
| 10654 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10655 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 10656 | else /* Not Palette, Bilevel, or Opaque Monochrome */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10657 | { |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10658 | if ((!mng_info->write_png8 && !mng_info->write_png24 && |
glennrp | fd164d2 | 2013-01-26 21:10:22 +0000 | [diff] [blame] | 10659 | !mng_info->write_png48 && !mng_info->write_png64 && |
| 10660 | !mng_info->write_png32) && (image_matte != MagickFalse || |
| 10661 | (ping_bit_depth >= MAGICKCORE_QUANTUM_DEPTH)) && |
| 10662 | (mng_info->IsPalette) && ping_have_color == MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10663 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 10664 | register const Quantum |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 10665 | *p; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10666 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 10667 | for (pass=0; pass < num_passes; pass++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10668 | { |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 10669 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 10670 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10671 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 10672 | p=GetVirtualPixels(image,0,y,image->columns,1,exception); |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10673 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 10674 | if (p == (const Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10675 | break; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10676 | |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 10677 | if (ping_color_type == PNG_COLOR_TYPE_GRAY) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10678 | { |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 10679 | if (mng_info->IsPalette) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 10680 | (void) ExportQuantumPixels(image,(CacheView *) NULL, |
| 10681 | quantum_info,GrayQuantum,ping_pixels,exception); |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10682 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 10683 | else |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 10684 | (void) ExportQuantumPixels(image,(CacheView *) NULL, |
| 10685 | quantum_info,RedQuantum,ping_pixels,exception); |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10686 | |
glennrp | 3b51f0e | 2010-11-27 18:14:08 +0000 | [diff] [blame] | 10687 | if (logging != MagickFalse && y == 0) |
glennrp | b4a1341 | 2010-05-05 12:47:19 +0000 | [diff] [blame] | 10688 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 10689 | " Writing GRAY PNG pixels (2)"); |
glennrp | b4a1341 | 2010-05-05 12:47:19 +0000 | [diff] [blame] | 10690 | } |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10691 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 10692 | else /* PNG_COLOR_TYPE_GRAY_ALPHA */ |
| 10693 | { |
| 10694 | if (logging != MagickFalse && y == 0) |
| 10695 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10696 | " Writing GRAY_ALPHA PNG pixels (2)"); |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10697 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 10698 | (void) ExportQuantumPixels(image,(CacheView *) NULL, |
| 10699 | quantum_info,GrayAlphaQuantum,ping_pixels,exception); |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 10700 | } |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10701 | |
glennrp | 3b51f0e | 2010-11-27 18:14:08 +0000 | [diff] [blame] | 10702 | if (logging != MagickFalse && y == 0) |
glennrp | b4a1341 | 2010-05-05 12:47:19 +0000 | [diff] [blame] | 10703 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 10704 | " Writing row of pixels (2)"); |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10705 | |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 10706 | png_write_row(ping,ping_pixels); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10707 | } |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10708 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 10709 | if (image->previous == (Image *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10710 | { |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 10711 | status=SetImageProgress(image,LoadImageTag,pass,num_passes); |
| 10712 | if (status == MagickFalse) |
| 10713 | break; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10714 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10715 | } |
| 10716 | } |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 10717 | |
| 10718 | else |
| 10719 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 10720 | register const Quantum |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 10721 | *p; |
| 10722 | |
| 10723 | for (pass=0; pass < num_passes; pass++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10724 | { |
glennrp | fd164d2 | 2013-01-26 21:10:22 +0000 | [diff] [blame] | 10725 | if ((image_depth > 8) || |
| 10726 | mng_info->write_png24 || |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 10727 | mng_info->write_png32 || |
glennrp | fd164d2 | 2013-01-26 21:10:22 +0000 | [diff] [blame] | 10728 | mng_info->write_png48 || |
| 10729 | mng_info->write_png64 || |
| 10730 | (!mng_info->write_png8 && !mng_info->IsPalette)) |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 10731 | { |
| 10732 | for (y=0; y < (ssize_t) image->rows; y++) |
| 10733 | { |
cristy | 862a33c | 2012-05-17 22:49:37 +0000 | [diff] [blame] | 10734 | p=GetVirtualPixels(image,0,y,image->columns,1, exception); |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10735 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 10736 | if (p == (const Quantum *) NULL) |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 10737 | break; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10738 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 10739 | if (ping_color_type == PNG_COLOR_TYPE_GRAY) |
| 10740 | { |
| 10741 | if (image->storage_class == DirectClass) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 10742 | (void) ExportQuantumPixels(image,(CacheView *) NULL, |
| 10743 | quantum_info,RedQuantum,ping_pixels,exception); |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10744 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 10745 | else |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 10746 | (void) ExportQuantumPixels(image,(CacheView *) NULL, |
| 10747 | quantum_info,GrayQuantum,ping_pixels,exception); |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 10748 | } |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10749 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 10750 | else if (ping_color_type == PNG_COLOR_TYPE_GRAY_ALPHA) |
| 10751 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 10752 | (void) ExportQuantumPixels(image,(CacheView *) NULL, |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 10753 | quantum_info,GrayAlphaQuantum,ping_pixels, |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 10754 | exception); |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10755 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 10756 | if (logging != MagickFalse && y == 0) |
| 10757 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10758 | " Writing GRAY_ALPHA PNG pixels (3)"); |
| 10759 | } |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10760 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 10761 | else if (image_matte != MagickFalse) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 10762 | (void) ExportQuantumPixels(image,(CacheView *) NULL, |
| 10763 | quantum_info,RGBAQuantum,ping_pixels,exception); |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10764 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 10765 | else |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 10766 | (void) ExportQuantumPixels(image,(CacheView *) NULL, |
| 10767 | quantum_info,RGBQuantum,ping_pixels,exception); |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10768 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 10769 | if (logging != MagickFalse && y == 0) |
| 10770 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10771 | " Writing row of pixels (3)"); |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10772 | |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 10773 | png_write_row(ping,ping_pixels); |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 10774 | } |
| 10775 | } |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10776 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 10777 | else |
glennrp | fd164d2 | 2013-01-26 21:10:22 +0000 | [diff] [blame] | 10778 | /* not ((image_depth > 8) || |
| 10779 | mng_info->write_png24 || mng_info->write_png32 || |
| 10780 | mng_info->write_png48 || mng_info->write_png64 || |
| 10781 | (!mng_info->write_png8 && !mng_info->IsPalette)) |
| 10782 | */ |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 10783 | { |
| 10784 | if ((ping_color_type != PNG_COLOR_TYPE_GRAY) && |
| 10785 | (ping_color_type != PNG_COLOR_TYPE_GRAY_ALPHA)) |
| 10786 | { |
| 10787 | if (logging != MagickFalse) |
| 10788 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10789 | " pass %d, Image Is not GRAY or GRAY_ALPHA",pass); |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10790 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 10791 | quantum_info->depth=8; |
| 10792 | image_depth=8; |
| 10793 | } |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10794 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 10795 | for (y=0; y < (ssize_t) image->rows; y++) |
| 10796 | { |
| 10797 | if (logging != MagickFalse && y == 0) |
| 10798 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10799 | " pass %d, Image Is RGB, 16-bit GRAY, or GRAY_ALPHA",pass); |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10800 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 10801 | p=GetVirtualPixels(image,0,y,image->columns,1, exception); |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10802 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 10803 | if (p == (const Quantum *) NULL) |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 10804 | break; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10805 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 10806 | if (ping_color_type == PNG_COLOR_TYPE_GRAY) |
glennrp | 44757ab | 2011-03-17 12:57:03 +0000 | [diff] [blame] | 10807 | { |
glennrp | 4bf8973 | 2011-03-21 13:48:28 +0000 | [diff] [blame] | 10808 | quantum_info->depth=image->depth; |
| 10809 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 10810 | (void) ExportQuantumPixels(image,(CacheView *) NULL, |
| 10811 | quantum_info,GrayQuantum,ping_pixels,exception); |
glennrp | 44757ab | 2011-03-17 12:57:03 +0000 | [diff] [blame] | 10812 | } |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10813 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 10814 | else if (ping_color_type == PNG_COLOR_TYPE_GRAY_ALPHA) |
| 10815 | { |
| 10816 | if (logging != MagickFalse && y == 0) |
| 10817 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10818 | " Writing GRAY_ALPHA PNG pixels (4)"); |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10819 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 10820 | (void) ExportQuantumPixels(image,(CacheView *) NULL, |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 10821 | quantum_info,GrayAlphaQuantum,ping_pixels, |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 10822 | exception); |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 10823 | } |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10824 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 10825 | else |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 10826 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 10827 | (void) ExportQuantumPixels(image,(CacheView *) NULL, |
| 10828 | quantum_info,IndexQuantum,ping_pixels,exception); |
glennrp | 5eae760 | 2011-02-22 15:21:32 +0000 | [diff] [blame] | 10829 | |
| 10830 | if (logging != MagickFalse && y <= 2) |
| 10831 | { |
| 10832 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | 1a7d6db | 2011-03-17 13:24:10 +0000 | [diff] [blame] | 10833 | " Writing row of non-gray pixels (4)"); |
glennrp | 5eae760 | 2011-02-22 15:21:32 +0000 | [diff] [blame] | 10834 | |
| 10835 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10836 | " ping_pixels[0]=%d,ping_pixels[1]=%d", |
| 10837 | (int)ping_pixels[0],(int)ping_pixels[1]); |
| 10838 | } |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 10839 | } |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 10840 | png_write_row(ping,ping_pixels); |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 10841 | } |
| 10842 | } |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10843 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 10844 | if (image->previous == (Image *) NULL) |
| 10845 | { |
| 10846 | status=SetImageProgress(image,LoadImageTag,pass,num_passes); |
| 10847 | if (status == MagickFalse) |
| 10848 | break; |
| 10849 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10850 | } |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 10851 | } |
| 10852 | } |
| 10853 | |
cristy | b32b90a | 2009-09-07 21:45:48 +0000 | [diff] [blame] | 10854 | if (quantum_info != (QuantumInfo *) NULL) |
| 10855 | quantum_info=DestroyQuantumInfo(quantum_info); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10856 | |
| 10857 | if (logging != MagickFalse) |
| 10858 | { |
| 10859 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | b4a1341 | 2010-05-05 12:47:19 +0000 | [diff] [blame] | 10860 | " Wrote PNG image data"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10861 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10862 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 10863 | " Width: %.20g",(double) ping_width); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10864 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10865 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 10866 | " Height: %.20g",(double) ping_height); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10867 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10868 | if (mng_info->write_png_depth) |
| 10869 | { |
| 10870 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | 5d6fc9c | 2011-12-27 03:10:42 +0000 | [diff] [blame] | 10871 | " Defined png:bit-depth: %d",mng_info->write_png_depth); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10872 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10873 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10874 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 10875 | " PNG bit-depth written: %d",ping_bit_depth); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10876 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10877 | if (mng_info->write_png_colortype) |
| 10878 | { |
| 10879 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | 5d6fc9c | 2011-12-27 03:10:42 +0000 | [diff] [blame] | 10880 | " Defined png:color-type: %d",mng_info->write_png_colortype-1); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10881 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10882 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10883 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 10884 | " PNG color-type written: %d",ping_color_type); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10885 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10886 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 10887 | " PNG Interlace method: %d",ping_interlace_method); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10888 | } |
| 10889 | /* |
glennrp | a0ed009 | 2011-04-18 16:36:29 +0000 | [diff] [blame] | 10890 | Generate text chunks after IDAT. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10891 | */ |
glennrp | 823b55c | 2011-03-14 18:46:46 +0000 | [diff] [blame] | 10892 | if (ping_exclude_tEXt == MagickFalse || ping_exclude_zTXt == MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10893 | { |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10894 | ResetImagePropertyIterator(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10895 | property=GetNextImageProperty(image); |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10896 | while (property != (const char *) NULL) |
| 10897 | { |
| 10898 | png_textp |
| 10899 | text; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10900 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 10901 | value=GetImageProperty(image,property,exception); |
glennrp | a0ed009 | 2011-04-18 16:36:29 +0000 | [diff] [blame] | 10902 | |
| 10903 | /* Don't write any "png:" properties; those are just for "identify" */ |
| 10904 | if (LocaleNCompare(property,"png:",4) != 0 && |
| 10905 | |
| 10906 | /* Suppress density and units if we wrote a pHYs chunk */ |
| 10907 | (ping_exclude_pHYs != MagickFalse || |
glennrp | 823b55c | 2011-03-14 18:46:46 +0000 | [diff] [blame] | 10908 | LocaleCompare(property,"density") != 0 || |
glennrp | a0ed009 | 2011-04-18 16:36:29 +0000 | [diff] [blame] | 10909 | LocaleCompare(property,"units") != 0) && |
| 10910 | |
| 10911 | /* Suppress the IM-generated Date:create and Date:modify */ |
| 10912 | (ping_exclude_date == MagickFalse || |
| 10913 | LocaleNCompare(property, "Date:",5) != 0)) |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10914 | { |
glennrp | c70af4a | 2011-03-07 00:08:23 +0000 | [diff] [blame] | 10915 | if (value != (const char *) NULL) |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10916 | { |
cristy | a865ccd | 2012-07-28 00:33:10 +0000 | [diff] [blame] | 10917 | |
| 10918 | #if PNG_LIBPNG_VER >= 14000 |
| 10919 | text=(png_textp) png_malloc(ping, |
| 10920 | (png_alloc_size_t) sizeof(png_text)); |
| 10921 | #else |
| 10922 | text=(png_textp) png_malloc(ping,(png_size_t) sizeof(png_text)); |
| 10923 | #endif |
glennrp | c70af4a | 2011-03-07 00:08:23 +0000 | [diff] [blame] | 10924 | text[0].key=(char *) property; |
| 10925 | text[0].text=(char *) value; |
| 10926 | text[0].text_length=strlen(value); |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10927 | |
glennrp | c70af4a | 2011-03-07 00:08:23 +0000 | [diff] [blame] | 10928 | if (ping_exclude_tEXt != MagickFalse) |
| 10929 | text[0].compression=PNG_TEXT_COMPRESSION_zTXt; |
| 10930 | |
| 10931 | else if (ping_exclude_zTXt != MagickFalse) |
| 10932 | text[0].compression=PNG_TEXT_COMPRESSION_NONE; |
| 10933 | |
| 10934 | else |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10935 | { |
glennrp | c70af4a | 2011-03-07 00:08:23 +0000 | [diff] [blame] | 10936 | text[0].compression=image_info->compression == NoCompression || |
| 10937 | (image_info->compression == UndefinedCompression && |
| 10938 | text[0].text_length < 128) ? PNG_TEXT_COMPRESSION_NONE : |
| 10939 | PNG_TEXT_COMPRESSION_zTXt ; |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10940 | } |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10941 | |
glennrp | c70af4a | 2011-03-07 00:08:23 +0000 | [diff] [blame] | 10942 | if (logging != MagickFalse) |
| 10943 | { |
| 10944 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10945 | " Setting up text chunk"); |
| 10946 | |
| 10947 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | cbc9215 | 2013-02-04 15:46:22 +0000 | [diff] [blame] | 10948 | " keyword: '%s'",text[0].key); |
glennrp | c70af4a | 2011-03-07 00:08:23 +0000 | [diff] [blame] | 10949 | } |
| 10950 | |
| 10951 | png_set_text(ping,ping_info,text,1); |
| 10952 | png_free(ping,text); |
| 10953 | } |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10954 | } |
| 10955 | property=GetNextImageProperty(image); |
| 10956 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10957 | } |
| 10958 | |
| 10959 | /* write any PNG-chunk-e profiles */ |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 10960 | (void) Magick_png_write_chunk_from_profile(image,"PNG-chunk-e",logging); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10961 | |
| 10962 | if (logging != MagickFalse) |
| 10963 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10964 | " Writing PNG end info"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10965 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10966 | png_write_end(ping,ping_info); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10967 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10968 | if (mng_info->need_fram && (int) image->dispose == BackgroundDispose) |
| 10969 | { |
| 10970 | if (mng_info->page.x || mng_info->page.y || |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 10971 | (ping_width != mng_info->page.width) || |
| 10972 | (ping_height != mng_info->page.height)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10973 | { |
| 10974 | unsigned char |
| 10975 | chunk[32]; |
| 10976 | |
| 10977 | /* |
| 10978 | Write FRAM 4 with clipping boundaries followed by FRAM 1. |
| 10979 | */ |
| 10980 | (void) WriteBlobMSBULong(image,27L); /* data length=27 */ |
| 10981 | PNGType(chunk,mng_FRAM); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 10982 | LogPNGChunk(logging,mng_FRAM,27L); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10983 | chunk[4]=4; |
| 10984 | chunk[5]=0; /* frame name separator (no name) */ |
| 10985 | chunk[6]=1; /* flag for changing delay, for next frame only */ |
| 10986 | chunk[7]=0; /* flag for changing frame timeout */ |
| 10987 | chunk[8]=1; /* flag for changing frame clipping for next frame */ |
| 10988 | chunk[9]=0; /* flag for changing frame sync_id */ |
| 10989 | PNGLong(chunk+10,(png_uint_32) (0L)); /* temporary 0 delay */ |
| 10990 | chunk[14]=0; /* clipping boundaries delta type */ |
| 10991 | PNGLong(chunk+15,(png_uint_32) (mng_info->page.x)); /* left cb */ |
| 10992 | PNGLong(chunk+19, |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 10993 | (png_uint_32) (mng_info->page.x + ping_width)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10994 | PNGLong(chunk+23,(png_uint_32) (mng_info->page.y)); /* top cb */ |
| 10995 | PNGLong(chunk+27, |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 10996 | (png_uint_32) (mng_info->page.y + ping_height)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10997 | (void) WriteBlob(image,31,chunk); |
| 10998 | (void) WriteBlobMSBULong(image,crc32(0,chunk,31)); |
| 10999 | mng_info->old_framing_mode=4; |
| 11000 | mng_info->framing_mode=1; |
| 11001 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11002 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11003 | else |
| 11004 | mng_info->framing_mode=3; |
| 11005 | } |
| 11006 | if (mng_info->write_mng && !mng_info->need_fram && |
| 11007 | ((int) image->dispose == 3)) |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 11008 | png_error(ping, "Cannot convert GIF with disposal method 3 to MNG-LC"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11009 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11010 | /* |
| 11011 | Free PNG resources. |
| 11012 | */ |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 11013 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11014 | png_destroy_write_struct(&ping,&ping_info); |
| 11015 | |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 11016 | ping_pixels=(unsigned char *) RelinquishMagickMemory(ping_pixels); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11017 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 11018 | if (ping_have_blob != MagickFalse) |
| 11019 | (void) CloseBlob(image); |
| 11020 | |
| 11021 | image_info=DestroyImageInfo(image_info); |
| 11022 | image=DestroyImage(image); |
| 11023 | |
glennrp | b9cfe27 | 2010-12-21 15:08:06 +0000 | [diff] [blame] | 11024 | /* Store bit depth actually written */ |
| 11025 | s[0]=(char) ping_bit_depth; |
| 11026 | s[1]='\0'; |
| 11027 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 11028 | (void) SetImageProperty(IMimage,"png:bit-depth-written",s,exception); |
glennrp | b9cfe27 | 2010-12-21 15:08:06 +0000 | [diff] [blame] | 11029 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11030 | if (logging != MagickFalse) |
| 11031 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 11032 | " exit WriteOnePNGImage()"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11033 | |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 11034 | #ifdef PNG_SETJMP_NOT_THREAD_SAFE |
| 11035 | UnlockSemaphoreInfo(ping_semaphore); |
| 11036 | #endif |
| 11037 | |
| 11038 | /* } for navigation to beginning of SETJMP-protected block. Revert to |
| 11039 | * Throwing an Exception when an error occurs. |
| 11040 | */ |
| 11041 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11042 | return(MagickTrue); |
| 11043 | /* End write one PNG image */ |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 11044 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11045 | } |
| 11046 | |
| 11047 | /* |
| 11048 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 11049 | % % |
| 11050 | % % |
| 11051 | % % |
| 11052 | % W r i t e P N G I m a g e % |
| 11053 | % % |
| 11054 | % % |
| 11055 | % % |
| 11056 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 11057 | % |
| 11058 | % WritePNGImage() writes a Portable Network Graphics (PNG) or |
| 11059 | % Multiple-image Network Graphics (MNG) image file. |
| 11060 | % |
| 11061 | % MNG support written by Glenn Randers-Pehrson, glennrp@image... |
| 11062 | % |
| 11063 | % The format of the WritePNGImage method is: |
| 11064 | % |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 11065 | % MagickBooleanType WritePNGImage(const ImageInfo *image_info, |
| 11066 | % Image *image,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11067 | % |
| 11068 | % A description of each parameter follows: |
| 11069 | % |
| 11070 | % o image_info: the image info. |
| 11071 | % |
| 11072 | % o image: The image. |
| 11073 | % |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 11074 | % o exception: return any errors or warnings in this structure. |
| 11075 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11076 | % Returns MagickTrue on success, MagickFalse on failure. |
| 11077 | % |
| 11078 | % Communicating with the PNG encoder: |
| 11079 | % |
| 11080 | % While the datastream written is always in PNG format and normally would |
| 11081 | % be given the "png" file extension, this method also writes the following |
cristy | 5d6fc9c | 2011-12-27 03:10:42 +0000 | [diff] [blame] | 11082 | % pseudo-formats which are subsets of png: |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11083 | % |
glennrp | 5a39f37 | 2011-02-25 04:52:16 +0000 | [diff] [blame] | 11084 | % o PNG8: An 8-bit indexed PNG datastream is written. If the image has |
| 11085 | % a depth greater than 8, the depth is reduced. If transparency |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11086 | % is present, the tRNS chunk must only have values 0 and 255 |
| 11087 | % (i.e., transparency is binary: fully opaque or fully |
glennrp | 5a39f37 | 2011-02-25 04:52:16 +0000 | [diff] [blame] | 11088 | % transparent). If other values are present they will be |
| 11089 | % 50%-thresholded to binary transparency. If more than 256 |
glennrp | e9637cb | 2011-03-24 16:34:37 +0000 | [diff] [blame] | 11090 | % colors are present, they will be quantized to the 4-4-4-1, |
glennrp | 130fc45 | 2011-08-20 03:43:18 +0000 | [diff] [blame] | 11091 | % 3-3-3-1, or 3-3-2-1 palette. The underlying RGB color |
| 11092 | % of any resulting fully-transparent pixels is changed to |
| 11093 | % the image's background color. |
glennrp | e9637cb | 2011-03-24 16:34:37 +0000 | [diff] [blame] | 11094 | % |
| 11095 | % If you want better quantization or dithering of the colors |
| 11096 | % or alpha than that, you need to do it before calling the |
glennrp | 5a39f37 | 2011-02-25 04:52:16 +0000 | [diff] [blame] | 11097 | % PNG encoder. The pixels contain 8-bit indices even if |
| 11098 | % they could be represented with 1, 2, or 4 bits. Grayscale |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11099 | % images will be written as indexed PNG files even though the |
glennrp | 5a39f37 | 2011-02-25 04:52:16 +0000 | [diff] [blame] | 11100 | % PNG grayscale type might be slightly more efficient. Please |
| 11101 | % note that writing to the PNG8 format may result in loss |
| 11102 | % of color and alpha data. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11103 | % |
| 11104 | % o PNG24: An 8-bit per sample RGB PNG datastream is written. The tRNS |
| 11105 | % chunk can be present to convey binary transparency by naming |
glennrp | 5a39f37 | 2011-02-25 04:52:16 +0000 | [diff] [blame] | 11106 | % one of the colors as transparent. The only loss incurred |
| 11107 | % is reduction of sample depth to 8. If the image has more |
| 11108 | % than one transparent color, has semitransparent pixels, or |
| 11109 | % has an opaque pixel with the same RGB components as the |
| 11110 | % transparent color, an image is not written. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11111 | % |
| 11112 | % o PNG32: An 8-bit per sample RGBA PNG is written. Partial |
| 11113 | % transparency is permitted, i.e., the alpha sample for |
| 11114 | % each pixel can have any value from 0 to 255. The alpha |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11115 | % channel is present even if the image is fully opaque. |
glennrp | 5a39f37 | 2011-02-25 04:52:16 +0000 | [diff] [blame] | 11116 | % The only loss in data is the reduction of the sample depth |
| 11117 | % to 8. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11118 | % |
glennrp | fd164d2 | 2013-01-26 21:10:22 +0000 | [diff] [blame] | 11119 | % o PNG48: A 16-bit per sample RGB PNG datastream is written. The tRNS |
| 11120 | % chunk can be present to convey binary transparency by naming |
| 11121 | % one of the colors as transparent. If the image has more |
| 11122 | % than one transparent color, has semitransparent pixels, or |
| 11123 | % has an opaque pixel with the same RGB components as the |
| 11124 | % transparent color, an image is not written. |
| 11125 | % |
| 11126 | % o PNG64: A 16-bit per sample RGBA PNG is written. Partial |
| 11127 | % transparency is permitted, i.e., the alpha sample for |
| 11128 | % each pixel can have any value from 0 to 65535. The alpha |
| 11129 | % channel is present even if the image is fully opaque. |
| 11130 | % |
glennrp | 5830fbc | 2013-01-27 06:11:45 +0000 | [diff] [blame] | 11131 | % o PNG00: A PNG that inherits its colortype and bit-depth from the input |
| 11132 | % image, if the input was a PNG, is written. If these values |
| 11133 | % cannot be found, then "PNG00" falls back to the regular "PNG" |
| 11134 | % format. |
| 11135 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11136 | % o -define: For more precise control of the PNG output, you can use the |
| 11137 | % Image options "png:bit-depth" and "png:color-type". These |
| 11138 | % can be set from the commandline with "-define" and also |
glennrp | bb8a733 | 2010-11-13 15:17:35 +0000 | [diff] [blame] | 11139 | % from the application programming interfaces. The options |
| 11140 | % are case-independent and are converted to lowercase before |
| 11141 | % being passed to this encoder. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11142 | % |
| 11143 | % png:color-type can be 0, 2, 3, 4, or 6. |
| 11144 | % |
| 11145 | % When png:color-type is 0 (Grayscale), png:bit-depth can |
| 11146 | % be 1, 2, 4, 8, or 16. |
| 11147 | % |
| 11148 | % When png:color-type is 2 (RGB), png:bit-depth can |
| 11149 | % be 8 or 16. |
| 11150 | % |
| 11151 | % When png:color-type is 3 (Indexed), png:bit-depth can |
| 11152 | % be 1, 2, 4, or 8. This refers to the number of bits |
| 11153 | % used to store the index. The color samples always have |
| 11154 | % bit-depth 8 in indexed PNG files. |
| 11155 | % |
| 11156 | % When png:color-type is 4 (Gray-Matte) or 6 (RGB-Matte), |
| 11157 | % png:bit-depth can be 8 or 16. |
| 11158 | % |
glennrp | fd164d2 | 2013-01-26 21:10:22 +0000 | [diff] [blame] | 11159 | % If the image cannot be written without loss with the |
| 11160 | % requested bit-depth and color-type, a PNG file will not |
| 11161 | % be written, a warning will be issued, and the encoder will |
| 11162 | % return MagickFalse. |
glennrp | 5a39f37 | 2011-02-25 04:52:16 +0000 | [diff] [blame] | 11163 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11164 | % Since image encoders should not be responsible for the "heavy lifting", |
| 11165 | % the user should make sure that ImageMagick has already reduced the |
| 11166 | % image depth and number of colors and limit transparency to binary |
glennrp | 5a39f37 | 2011-02-25 04:52:16 +0000 | [diff] [blame] | 11167 | % transparency prior to attempting to write the image with depth, color, |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 11168 | % or transparency limitations. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11169 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11170 | % Note that another definition, "png:bit-depth-written" exists, but it |
| 11171 | % is not intended for external use. It is only used internally by the |
| 11172 | % PNG encoder to inform the JNG encoder of the depth of the alpha channel. |
| 11173 | % |
| 11174 | % It is possible to request that the PNG encoder write previously-formatted |
| 11175 | % ancillary chunks in the output PNG file, using the "-profile" commandline |
| 11176 | % option as shown below or by setting the profile via a programming |
| 11177 | % interface: |
| 11178 | % |
| 11179 | % -profile PNG-chunk-x:<file> |
| 11180 | % |
| 11181 | % where x is a location flag and <file> is a file containing the chunk |
| 11182 | % 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] | 11183 | % This encoder will compute the chunk length and CRC, so those must not |
| 11184 | % be included in the file. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11185 | % |
| 11186 | % "x" can be "b" (before PLTE), "m" (middle, i.e., between PLTE and IDAT), |
| 11187 | % or "e" (end, i.e., after IDAT). If you want to write multiple chunks |
| 11188 | % 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] | 11189 | % subsequent profiles from overwriting the preceding ones, e.g., |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11190 | % |
glennrp | bb8a733 | 2010-11-13 15:17:35 +0000 | [diff] [blame] | 11191 | % -profile PNG-chunk-b01:file01 -profile PNG-chunk-b02:file02 |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11192 | % |
glennrp | 3241bd0 | 2010-12-12 04:36:28 +0000 | [diff] [blame] | 11193 | % As of version 6.6.6 the following optimizations are always done: |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11194 | % |
glennrp | d6afd54 | 2010-11-19 01:53:05 +0000 | [diff] [blame] | 11195 | % o 32-bit depth is reduced to 16. |
| 11196 | % o 16-bit depth is reduced to 8 if all pixels contain samples whose |
| 11197 | % high byte and low byte are identical. |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11198 | % o Palette is sorted to remove unused entries and to put a |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 11199 | % transparent color first, if BUILD_PNG_PALETTE is defined. |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11200 | % o Opaque matte channel is removed when writing an indexed PNG. |
glennrp | d6afd54 | 2010-11-19 01:53:05 +0000 | [diff] [blame] | 11201 | % o Grayscale images are reduced to 1, 2, or 4 bit depth if |
| 11202 | % 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] | 11203 | % requested via the "-define png:bit-depth=N" option. |
glennrp | d6afd54 | 2010-11-19 01:53:05 +0000 | [diff] [blame] | 11204 | % o If matte channel is present but only one transparent color is |
| 11205 | % present, RGB+tRNS is written instead of RGBA |
| 11206 | % o Opaque matte channel is removed (or added, if color-type 4 or 6 |
| 11207 | % was requested when converting an opaque image). |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11208 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11209 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 11210 | */ |
| 11211 | static MagickBooleanType WritePNGImage(const ImageInfo *image_info, |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 11212 | Image *image,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11213 | { |
| 11214 | MagickBooleanType |
glennrp | 21f0e62 | 2011-01-07 16:20:57 +0000 | [diff] [blame] | 11215 | excluding, |
| 11216 | logging, |
| 11217 | have_mng_structure, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11218 | status; |
| 11219 | |
| 11220 | MngInfo |
| 11221 | *mng_info; |
| 11222 | |
| 11223 | const char |
| 11224 | *value; |
| 11225 | |
| 11226 | int |
glennrp | 21f0e62 | 2011-01-07 16:20:57 +0000 | [diff] [blame] | 11227 | i, |
glennrp | 5c7cf4e | 2010-12-24 00:30:00 +0000 | [diff] [blame] | 11228 | source; |
| 11229 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11230 | /* |
| 11231 | Open image file. |
| 11232 | */ |
| 11233 | assert(image_info != (const ImageInfo *) NULL); |
| 11234 | assert(image_info->signature == MagickSignature); |
| 11235 | assert(image != (Image *) NULL); |
| 11236 | assert(image->signature == MagickSignature); |
| 11237 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 11238 | logging=LogMagickEvent(CoderEvent,GetMagickModule(),"Enter WritePNGImage()"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11239 | /* |
| 11240 | Allocate a MngInfo structure. |
| 11241 | */ |
| 11242 | have_mng_structure=MagickFalse; |
cristy | 73bd4a5 | 2010-10-05 11:24:23 +0000 | [diff] [blame] | 11243 | mng_info=(MngInfo *) AcquireMagickMemory(sizeof(MngInfo)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11244 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11245 | if (mng_info == (MngInfo *) NULL) |
| 11246 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11247 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11248 | /* |
| 11249 | Initialize members of the MngInfo structure. |
| 11250 | */ |
| 11251 | (void) ResetMagickMemory(mng_info,0,sizeof(MngInfo)); |
| 11252 | mng_info->image=image; |
glennrp | a521b2f | 2010-10-29 04:11:03 +0000 | [diff] [blame] | 11253 | mng_info->equal_backgrounds=MagickTrue; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11254 | have_mng_structure=MagickTrue; |
| 11255 | |
| 11256 | /* See if user has requested a specific PNG subformat */ |
| 11257 | |
| 11258 | mng_info->write_png8=LocaleCompare(image_info->magick,"PNG8") == 0; |
| 11259 | mng_info->write_png24=LocaleCompare(image_info->magick,"PNG24") == 0; |
| 11260 | mng_info->write_png32=LocaleCompare(image_info->magick,"PNG32") == 0; |
glennrp | fd164d2 | 2013-01-26 21:10:22 +0000 | [diff] [blame] | 11261 | mng_info->write_png48=LocaleCompare(image_info->magick,"PNG48") == 0; |
| 11262 | mng_info->write_png64=LocaleCompare(image_info->magick,"PNG64") == 0; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11263 | |
glennrp | b381a26 | 2012-02-11 17:49:49 +0000 | [diff] [blame] | 11264 | value=GetImageOption(image_info,"png:format"); |
| 11265 | |
| 11266 | if (value != (char *) NULL) |
| 11267 | { |
glennrp | fd164d2 | 2013-01-26 21:10:22 +0000 | [diff] [blame] | 11268 | mng_info->write_png8 = MagickFalse; |
| 11269 | mng_info->write_png24 = MagickFalse; |
| 11270 | mng_info->write_png32 = MagickFalse; |
| 11271 | mng_info->write_png48 = MagickFalse; |
| 11272 | mng_info->write_png64 = MagickFalse; |
| 11273 | |
glennrp | b381a26 | 2012-02-11 17:49:49 +0000 | [diff] [blame] | 11274 | if (LocaleCompare(value,"png8") == 0) |
glennrp | b381a26 | 2012-02-11 17:49:49 +0000 | [diff] [blame] | 11275 | mng_info->write_png8 = MagickTrue; |
glennrp | b381a26 | 2012-02-11 17:49:49 +0000 | [diff] [blame] | 11276 | |
| 11277 | else if (LocaleCompare(value,"png24") == 0) |
glennrp | b381a26 | 2012-02-11 17:49:49 +0000 | [diff] [blame] | 11278 | mng_info->write_png24 = MagickTrue; |
glennrp | b381a26 | 2012-02-11 17:49:49 +0000 | [diff] [blame] | 11279 | |
| 11280 | else if (LocaleCompare(value,"png32") == 0) |
glennrp | b381a26 | 2012-02-11 17:49:49 +0000 | [diff] [blame] | 11281 | mng_info->write_png32 = MagickTrue; |
glennrp | fd164d2 | 2013-01-26 21:10:22 +0000 | [diff] [blame] | 11282 | |
| 11283 | else if (LocaleCompare(value,"png48") == 0) |
| 11284 | mng_info->write_png48 = MagickTrue; |
| 11285 | |
| 11286 | else if (LocaleCompare(value,"png64") == 0) |
| 11287 | mng_info->write_png64 = MagickTrue; |
glennrp | b381a26 | 2012-02-11 17:49:49 +0000 | [diff] [blame] | 11288 | } |
glennrp | 5830fbc | 2013-01-27 06:11:45 +0000 | [diff] [blame] | 11289 | |
| 11290 | if (LocaleCompare(value,"png00") == 0) |
| 11291 | { |
| 11292 | /* Retrieve png:IHDR.bit-depth-orig and png:IHDR.color-type-orig |
| 11293 | Note that whitespace at the end of the property names must match |
| 11294 | that in the corresponding SetImageProperty() calls. |
| 11295 | */ |
| 11296 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 11297 | " Format=%s",value); |
| 11298 | |
| 11299 | value=GetImageProperty(image,"png:IHDR.bit-depth-orig ",exception); |
| 11300 | |
| 11301 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 11302 | " png00 inherited bit depth=%s",value); |
| 11303 | |
| 11304 | if (value != (char *) NULL) |
| 11305 | { |
| 11306 | if (LocaleCompare(value,"1") == 0) |
| 11307 | mng_info->write_png_depth = 1; |
| 11308 | |
| 11309 | else if (LocaleCompare(value,"1") == 0) |
| 11310 | mng_info->write_png_depth = 2; |
| 11311 | |
| 11312 | else if (LocaleCompare(value,"2") == 0) |
| 11313 | mng_info->write_png_depth = 4; |
| 11314 | |
| 11315 | else if (LocaleCompare(value,"8") == 0) |
| 11316 | mng_info->write_png_depth = 8; |
| 11317 | |
| 11318 | else if (LocaleCompare(value,"16") == 0) |
| 11319 | mng_info->write_png_depth = 16; |
| 11320 | } |
| 11321 | |
| 11322 | value=GetImageProperty(image,"png:IHDR.color-type-orig ",exception); |
| 11323 | |
| 11324 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 11325 | " png00 inherited color type=%s",value); |
| 11326 | |
| 11327 | if (value != (char *) NULL) |
| 11328 | { |
| 11329 | if (LocaleCompare(value,"0") == 0) |
| 11330 | mng_info->write_png_colortype = 1; |
| 11331 | |
| 11332 | else if (LocaleCompare(value,"2") == 0) |
| 11333 | mng_info->write_png_colortype = 3; |
| 11334 | |
| 11335 | else if (LocaleCompare(value,"3") == 0) |
| 11336 | mng_info->write_png_colortype = 4; |
| 11337 | |
| 11338 | else if (LocaleCompare(value,"4") == 0) |
| 11339 | mng_info->write_png_colortype = 5; |
| 11340 | |
| 11341 | else if (LocaleCompare(value,"6") == 0) |
| 11342 | mng_info->write_png_colortype = 7; |
| 11343 | } |
| 11344 | } |
| 11345 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11346 | if (mng_info->write_png8) |
| 11347 | { |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 11348 | mng_info->write_png_colortype = /* 3 */ 4; |
| 11349 | mng_info->write_png_depth = 8; |
| 11350 | image->depth = 8; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11351 | } |
| 11352 | |
| 11353 | if (mng_info->write_png24) |
| 11354 | { |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 11355 | mng_info->write_png_colortype = /* 2 */ 3; |
| 11356 | mng_info->write_png_depth = 8; |
| 11357 | image->depth = 8; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11358 | |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 11359 | if (image->alpha_trait == BlendPixelTrait) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 11360 | (void) SetImageType(image,TrueColorMatteType,exception); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11361 | |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 11362 | else |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 11363 | (void) SetImageType(image,TrueColorType,exception); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11364 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 11365 | (void) SyncImage(image,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11366 | } |
| 11367 | |
| 11368 | if (mng_info->write_png32) |
| 11369 | { |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 11370 | mng_info->write_png_colortype = /* 6 */ 7; |
| 11371 | mng_info->write_png_depth = 8; |
| 11372 | image->depth = 8; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11373 | |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 11374 | if (image->alpha_trait == BlendPixelTrait) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 11375 | (void) SetImageType(image,TrueColorMatteType,exception); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11376 | |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 11377 | else |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 11378 | (void) SetImageType(image,TrueColorType,exception); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11379 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 11380 | (void) SyncImage(image,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11381 | } |
| 11382 | |
glennrp | fd164d2 | 2013-01-26 21:10:22 +0000 | [diff] [blame] | 11383 | if (mng_info->write_png48) |
| 11384 | { |
| 11385 | mng_info->write_png_colortype = /* 2 */ 3; |
| 11386 | mng_info->write_png_depth = 16; |
| 11387 | image->depth = 16; |
| 11388 | |
glennrp | 4dda64f | 2013-01-26 21:20:24 +0000 | [diff] [blame] | 11389 | if (image->alpha_trait == BlendPixelTrait) |
| 11390 | (void) SetImageType(image,TrueColorMatteType,exception); |
glennrp | fd164d2 | 2013-01-26 21:10:22 +0000 | [diff] [blame] | 11391 | |
| 11392 | else |
glennrp | 4dda64f | 2013-01-26 21:20:24 +0000 | [diff] [blame] | 11393 | (void) SetImageType(image,TrueColorType,exception); |
glennrp | fd164d2 | 2013-01-26 21:10:22 +0000 | [diff] [blame] | 11394 | |
glennrp | 4dda64f | 2013-01-26 21:20:24 +0000 | [diff] [blame] | 11395 | (void) SyncImage(image,exception); |
glennrp | fd164d2 | 2013-01-26 21:10:22 +0000 | [diff] [blame] | 11396 | } |
| 11397 | |
| 11398 | if (mng_info->write_png64) |
| 11399 | { |
| 11400 | mng_info->write_png_colortype = /* 6 */ 7; |
| 11401 | mng_info->write_png_depth = 16; |
| 11402 | image->depth = 16; |
| 11403 | |
glennrp | 4dda64f | 2013-01-26 21:20:24 +0000 | [diff] [blame] | 11404 | if (image->alpha_trait == BlendPixelTrait) |
| 11405 | (void) SetImageType(image,TrueColorMatteType,exception); |
glennrp | fd164d2 | 2013-01-26 21:10:22 +0000 | [diff] [blame] | 11406 | |
| 11407 | else |
glennrp | 4dda64f | 2013-01-26 21:20:24 +0000 | [diff] [blame] | 11408 | (void) SetImageType(image,TrueColorType,exception); |
glennrp | fd164d2 | 2013-01-26 21:10:22 +0000 | [diff] [blame] | 11409 | |
glennrp | 4dda64f | 2013-01-26 21:20:24 +0000 | [diff] [blame] | 11410 | (void) SyncImage(image,exception); |
glennrp | fd164d2 | 2013-01-26 21:10:22 +0000 | [diff] [blame] | 11411 | } |
| 11412 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11413 | value=GetImageOption(image_info,"png:bit-depth"); |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 11414 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11415 | if (value != (char *) NULL) |
| 11416 | { |
| 11417 | if (LocaleCompare(value,"1") == 0) |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 11418 | mng_info->write_png_depth = 1; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11419 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11420 | else if (LocaleCompare(value,"2") == 0) |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 11421 | mng_info->write_png_depth = 2; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11422 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11423 | else if (LocaleCompare(value,"4") == 0) |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 11424 | mng_info->write_png_depth = 4; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11425 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11426 | else if (LocaleCompare(value,"8") == 0) |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 11427 | mng_info->write_png_depth = 8; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11428 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11429 | else if (LocaleCompare(value,"16") == 0) |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 11430 | mng_info->write_png_depth = 16; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11431 | |
glennrp | bb8a733 | 2010-11-13 15:17:35 +0000 | [diff] [blame] | 11432 | else |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 11433 | (void) ThrowMagickException(exception, |
glennrp | bb8a733 | 2010-11-13 15:17:35 +0000 | [diff] [blame] | 11434 | GetMagickModule(),CoderWarning, |
| 11435 | "ignoring invalid defined png:bit-depth", |
| 11436 | "=%s",value); |
| 11437 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11438 | if (logging != MagickFalse) |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 11439 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | bb8a733 | 2010-11-13 15:17:35 +0000 | [diff] [blame] | 11440 | " png:bit-depth=%d was defined.\n",mng_info->write_png_depth); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11441 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11442 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11443 | value=GetImageOption(image_info,"png:color-type"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11444 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11445 | if (value != (char *) NULL) |
| 11446 | { |
| 11447 | /* We must store colortype+1 because 0 is a valid colortype */ |
| 11448 | if (LocaleCompare(value,"0") == 0) |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 11449 | mng_info->write_png_colortype = 1; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11450 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 11451 | else if (LocaleCompare(value,"1") == 0) |
| 11452 | mng_info->write_png_colortype = 2; |
| 11453 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11454 | else if (LocaleCompare(value,"2") == 0) |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 11455 | mng_info->write_png_colortype = 3; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11456 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11457 | else if (LocaleCompare(value,"3") == 0) |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 11458 | mng_info->write_png_colortype = 4; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11459 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11460 | else if (LocaleCompare(value,"4") == 0) |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 11461 | mng_info->write_png_colortype = 5; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11462 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11463 | else if (LocaleCompare(value,"6") == 0) |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 11464 | mng_info->write_png_colortype = 7; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11465 | |
glennrp | bb8a733 | 2010-11-13 15:17:35 +0000 | [diff] [blame] | 11466 | else |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 11467 | (void) ThrowMagickException(exception, |
glennrp | bb8a733 | 2010-11-13 15:17:35 +0000 | [diff] [blame] | 11468 | GetMagickModule(),CoderWarning, |
| 11469 | "ignoring invalid defined png:color-type", |
| 11470 | "=%s",value); |
| 11471 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11472 | if (logging != MagickFalse) |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 11473 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | d6bf161 | 2010-12-17 17:28:54 +0000 | [diff] [blame] | 11474 | " png:color-type=%d was defined.\n",mng_info->write_png_colortype-1); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11475 | } |
| 11476 | |
glennrp | 0e8ea19 | 2010-12-24 18:00:33 +0000 | [diff] [blame] | 11477 | /* Check for chunks to be excluded: |
| 11478 | * |
glennrp | 0dff56c | 2011-01-29 19:10:02 +0000 | [diff] [blame] | 11479 | * The default is to not exclude any known chunks except for any |
glennrp | 0e8ea19 | 2010-12-24 18:00:33 +0000 | [diff] [blame] | 11480 | * listed in the "unused_chunks" array, above. |
| 11481 | * |
cristy | 5d6fc9c | 2011-12-27 03:10:42 +0000 | [diff] [blame] | 11482 | * Chunks can be listed for exclusion via a "png:exclude-chunk" |
glennrp | 0e8ea19 | 2010-12-24 18:00:33 +0000 | [diff] [blame] | 11483 | * define (in the image properties or in the image artifacts) |
| 11484 | * or via a mng_info member. For convenience, in addition |
| 11485 | * to or instead of a comma-separated list of chunks, the |
| 11486 | * "exclude-chunk" string can be simply "all" or "none". |
| 11487 | * |
| 11488 | * The exclude-chunk define takes priority over the mng_info. |
| 11489 | * |
cristy | 5d6fc9c | 2011-12-27 03:10:42 +0000 | [diff] [blame] | 11490 | * A "png:include-chunk" define takes priority over both the |
| 11491 | * mng_info and the "png:exclude-chunk" define. Like the |
glennrp | 0e8ea19 | 2010-12-24 18:00:33 +0000 | [diff] [blame] | 11492 | * "exclude-chunk" string, it can define "all" or "none" as |
glennrp | 0dff56c | 2011-01-29 19:10:02 +0000 | [diff] [blame] | 11493 | * well as a comma-separated list. Chunks that are unknown to |
| 11494 | * ImageMagick are always excluded, regardless of their "copy-safe" |
| 11495 | * status according to the PNG specification, and even if they |
glennrp | aa192b1 | 2012-01-17 21:35:21 +0000 | [diff] [blame] | 11496 | * appear in the "include-chunk" list. Such defines appearing among |
| 11497 | * the image options take priority over those found among the image |
| 11498 | * artifacts. |
glennrp | 0e8ea19 | 2010-12-24 18:00:33 +0000 | [diff] [blame] | 11499 | * |
| 11500 | * Finally, all chunks listed in the "unused_chunks" array are |
| 11501 | * automatically excluded, regardless of the other instructions |
| 11502 | * or lack thereof. |
| 11503 | * |
| 11504 | * if you exclude sRGB but not gAMA (recommended), then sRGB chunk |
| 11505 | * will not be written and the gAMA chunk will only be written if it |
| 11506 | * is not between .45 and .46, or approximately (1.0/2.2). |
| 11507 | * |
| 11508 | * If you exclude tRNS and the image has transparency, the colortype |
| 11509 | * is forced to be 4 or 6 (GRAY_ALPHA or RGB_ALPHA). |
| 11510 | * |
| 11511 | * The -strip option causes StripImage() to set the png:include-chunk |
glennrp | 104f206 | 2011-08-20 05:08:53 +0000 | [diff] [blame] | 11512 | * artifact to "none,trns,gama". |
glennrp | 0e8ea19 | 2010-12-24 18:00:33 +0000 | [diff] [blame] | 11513 | */ |
| 11514 | |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 11515 | mng_info->ping_exclude_bKGD=MagickFalse; |
| 11516 | mng_info->ping_exclude_cHRM=MagickFalse; |
glennrp | a0ed009 | 2011-04-18 16:36:29 +0000 | [diff] [blame] | 11517 | mng_info->ping_exclude_date=MagickFalse; |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 11518 | mng_info->ping_exclude_EXIF=MagickFalse; /* hex-encoded EXIF in zTXt */ |
| 11519 | mng_info->ping_exclude_gAMA=MagickFalse; |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 11520 | mng_info->ping_exclude_iCCP=MagickFalse; |
| 11521 | /* mng_info->ping_exclude_iTXt=MagickFalse; */ |
| 11522 | mng_info->ping_exclude_oFFs=MagickFalse; |
| 11523 | mng_info->ping_exclude_pHYs=MagickFalse; |
| 11524 | mng_info->ping_exclude_sRGB=MagickFalse; |
| 11525 | mng_info->ping_exclude_tEXt=MagickFalse; |
glennrp | a1e3b7b | 2010-12-24 16:37:33 +0000 | [diff] [blame] | 11526 | mng_info->ping_exclude_tRNS=MagickFalse; |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 11527 | mng_info->ping_exclude_vpAg=MagickFalse; |
| 11528 | mng_info->ping_exclude_zCCP=MagickFalse; /* hex-encoded iCCP in zTXt */ |
| 11529 | mng_info->ping_exclude_zTXt=MagickFalse; |
| 11530 | |
glennrp | 8d3d6e5 | 2011-04-19 04:39:51 +0000 | [diff] [blame] | 11531 | mng_info->ping_preserve_colormap=MagickFalse; |
| 11532 | |
| 11533 | value=GetImageArtifact(image,"png:preserve-colormap"); |
| 11534 | if (value == NULL) |
| 11535 | value=GetImageOption(image_info,"png:preserve-colormap"); |
| 11536 | if (value != NULL) |
| 11537 | mng_info->ping_preserve_colormap=MagickTrue; |
| 11538 | |
glennrp | 1868258 | 2011-06-30 18:11:47 +0000 | [diff] [blame] | 11539 | /* Thes compression-level, compression-strategy, and compression-filter |
| 11540 | * defines take precedence over values from the -quality option. |
| 11541 | */ |
| 11542 | value=GetImageArtifact(image,"png:compression-level"); |
| 11543 | if (value == NULL) |
| 11544 | value=GetImageOption(image_info,"png:compression-level"); |
| 11545 | if (value != NULL) |
| 11546 | { |
glennrp | 1868258 | 2011-06-30 18:11:47 +0000 | [diff] [blame] | 11547 | /* We have to add 1 to everything because 0 is a valid input, |
| 11548 | * and we want to use 0 (the default) to mean undefined. |
| 11549 | */ |
| 11550 | if (LocaleCompare(value,"0") == 0) |
| 11551 | mng_info->write_png_compression_level = 1; |
| 11552 | |
glennrp | 0ffb95c | 2012-01-30 21:16:22 +0000 | [diff] [blame] | 11553 | else if (LocaleCompare(value,"1") == 0) |
glennrp | 1868258 | 2011-06-30 18:11:47 +0000 | [diff] [blame] | 11554 | mng_info->write_png_compression_level = 2; |
| 11555 | |
| 11556 | else if (LocaleCompare(value,"2") == 0) |
| 11557 | mng_info->write_png_compression_level = 3; |
| 11558 | |
| 11559 | else if (LocaleCompare(value,"3") == 0) |
| 11560 | mng_info->write_png_compression_level = 4; |
| 11561 | |
| 11562 | else if (LocaleCompare(value,"4") == 0) |
| 11563 | mng_info->write_png_compression_level = 5; |
| 11564 | |
| 11565 | else if (LocaleCompare(value,"5") == 0) |
| 11566 | mng_info->write_png_compression_level = 6; |
| 11567 | |
| 11568 | else if (LocaleCompare(value,"6") == 0) |
| 11569 | mng_info->write_png_compression_level = 7; |
| 11570 | |
| 11571 | else if (LocaleCompare(value,"7") == 0) |
| 11572 | mng_info->write_png_compression_level = 8; |
| 11573 | |
| 11574 | else if (LocaleCompare(value,"8") == 0) |
| 11575 | mng_info->write_png_compression_level = 9; |
| 11576 | |
| 11577 | else if (LocaleCompare(value,"9") == 0) |
| 11578 | mng_info->write_png_compression_level = 10; |
| 11579 | |
| 11580 | else |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 11581 | (void) ThrowMagickException(exception, |
glennrp | 1868258 | 2011-06-30 18:11:47 +0000 | [diff] [blame] | 11582 | GetMagickModule(),CoderWarning, |
| 11583 | "ignoring invalid defined png:compression-level", |
| 11584 | "=%s",value); |
| 11585 | } |
| 11586 | |
| 11587 | value=GetImageArtifact(image,"png:compression-strategy"); |
| 11588 | if (value == NULL) |
| 11589 | value=GetImageOption(image_info,"png:compression-strategy"); |
| 11590 | if (value != NULL) |
| 11591 | { |
| 11592 | |
| 11593 | if (LocaleCompare(value,"0") == 0) |
| 11594 | mng_info->write_png_compression_strategy = Z_DEFAULT_STRATEGY+1; |
| 11595 | |
| 11596 | else if (LocaleCompare(value,"1") == 0) |
| 11597 | mng_info->write_png_compression_strategy = Z_FILTERED+1; |
| 11598 | |
| 11599 | else if (LocaleCompare(value,"2") == 0) |
| 11600 | mng_info->write_png_compression_strategy = Z_HUFFMAN_ONLY+1; |
| 11601 | |
| 11602 | else if (LocaleCompare(value,"3") == 0) |
glennrp | 98c07ad | 2011-07-01 02:52:59 +0000 | [diff] [blame] | 11603 | #ifdef Z_RLE /* Z_RLE was added to zlib-1.2.0 */ |
glennrp | 1868258 | 2011-06-30 18:11:47 +0000 | [diff] [blame] | 11604 | mng_info->write_png_compression_strategy = Z_RLE+1; |
glennrp | 98c07ad | 2011-07-01 02:52:59 +0000 | [diff] [blame] | 11605 | #else |
| 11606 | mng_info->write_png_compression_strategy = Z_DEFAULT_STRATEGY+1; |
| 11607 | #endif |
glennrp | 1868258 | 2011-06-30 18:11:47 +0000 | [diff] [blame] | 11608 | |
| 11609 | else if (LocaleCompare(value,"4") == 0) |
glennrp | 98c07ad | 2011-07-01 02:52:59 +0000 | [diff] [blame] | 11610 | #ifdef Z_FIXED /* Z_FIXED was added to zlib-1.2.2.2 */ |
glennrp | 1868258 | 2011-06-30 18:11:47 +0000 | [diff] [blame] | 11611 | mng_info->write_png_compression_strategy = Z_FIXED+1; |
glennrp | 98c07ad | 2011-07-01 02:52:59 +0000 | [diff] [blame] | 11612 | #else |
| 11613 | mng_info->write_png_compression_strategy = Z_DEFAULT_STRATEGY+1; |
| 11614 | #endif |
glennrp | 1868258 | 2011-06-30 18:11:47 +0000 | [diff] [blame] | 11615 | |
| 11616 | else |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 11617 | (void) ThrowMagickException(exception, |
glennrp | 1868258 | 2011-06-30 18:11:47 +0000 | [diff] [blame] | 11618 | GetMagickModule(),CoderWarning, |
| 11619 | "ignoring invalid defined png:compression-strategy", |
| 11620 | "=%s",value); |
| 11621 | } |
| 11622 | |
| 11623 | value=GetImageArtifact(image,"png:compression-filter"); |
| 11624 | if (value == NULL) |
| 11625 | value=GetImageOption(image_info,"png:compression-filter"); |
| 11626 | if (value != NULL) |
| 11627 | { |
| 11628 | |
| 11629 | /* To do: combinations of filters allowed by libpng |
| 11630 | * masks 0x08 through 0xf8 |
| 11631 | * |
| 11632 | * Implement this as a comma-separated list of 0,1,2,3,4,5 |
| 11633 | * where 5 is a special case meaning PNG_ALL_FILTERS. |
| 11634 | */ |
| 11635 | |
| 11636 | if (LocaleCompare(value,"0") == 0) |
| 11637 | mng_info->write_png_compression_filter = 1; |
| 11638 | |
cristy | b19b812 | 2012-10-22 11:03:30 +0000 | [diff] [blame] | 11639 | else if (LocaleCompare(value,"1") == 0) |
glennrp | 1868258 | 2011-06-30 18:11:47 +0000 | [diff] [blame] | 11640 | mng_info->write_png_compression_filter = 2; |
| 11641 | |
| 11642 | else if (LocaleCompare(value,"2") == 0) |
| 11643 | mng_info->write_png_compression_filter = 3; |
| 11644 | |
| 11645 | else if (LocaleCompare(value,"3") == 0) |
| 11646 | mng_info->write_png_compression_filter = 4; |
| 11647 | |
| 11648 | else if (LocaleCompare(value,"4") == 0) |
| 11649 | mng_info->write_png_compression_filter = 5; |
| 11650 | |
| 11651 | else if (LocaleCompare(value,"5") == 0) |
| 11652 | mng_info->write_png_compression_filter = 6; |
| 11653 | |
glennrp | 1868258 | 2011-06-30 18:11:47 +0000 | [diff] [blame] | 11654 | else |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 11655 | (void) ThrowMagickException(exception, |
glennrp | 1868258 | 2011-06-30 18:11:47 +0000 | [diff] [blame] | 11656 | GetMagickModule(),CoderWarning, |
| 11657 | "ignoring invalid defined png:compression-filter", |
| 11658 | "=%s",value); |
| 11659 | } |
| 11660 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11661 | excluding=MagickFalse; |
| 11662 | |
glennrp | 5c7cf4e | 2010-12-24 00:30:00 +0000 | [diff] [blame] | 11663 | for (source=0; source<1; source++) |
| 11664 | { |
| 11665 | if (source==0) |
glennrp | acba004 | 2010-12-24 14:27:26 +0000 | [diff] [blame] | 11666 | { |
glennrp | 5c7cf4e | 2010-12-24 00:30:00 +0000 | [diff] [blame] | 11667 | value=GetImageArtifact(image,"png:exclude-chunk"); |
glennrp | acba004 | 2010-12-24 14:27:26 +0000 | [diff] [blame] | 11668 | |
| 11669 | if (value == NULL) |
| 11670 | value=GetImageArtifact(image,"png:exclude-chunks"); |
| 11671 | } |
glennrp | 5c7cf4e | 2010-12-24 00:30:00 +0000 | [diff] [blame] | 11672 | else |
glennrp | acba004 | 2010-12-24 14:27:26 +0000 | [diff] [blame] | 11673 | { |
glennrp | 5c7cf4e | 2010-12-24 00:30:00 +0000 | [diff] [blame] | 11674 | value=GetImageOption(image_info,"png:exclude-chunk"); |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 11675 | |
glennrp | acba004 | 2010-12-24 14:27:26 +0000 | [diff] [blame] | 11676 | if (value == NULL) |
| 11677 | value=GetImageOption(image_info,"png:exclude-chunks"); |
| 11678 | } |
| 11679 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11680 | if (value != NULL) |
glennrp | ce91ed5 | 2010-12-23 22:37:49 +0000 | [diff] [blame] | 11681 | { |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11682 | |
| 11683 | size_t |
| 11684 | last; |
| 11685 | |
| 11686 | excluding=MagickTrue; |
| 11687 | |
| 11688 | if (logging != MagickFalse) |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11689 | { |
| 11690 | if (source == 0) |
| 11691 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 11692 | " png:exclude-chunk=%s found in image artifacts.\n", value); |
| 11693 | else |
| 11694 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 11695 | " png:exclude-chunk=%s found in image properties.\n", value); |
| 11696 | } |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11697 | |
| 11698 | last=strlen(value); |
| 11699 | |
| 11700 | for (i=0; i<(int) last; i+=5) |
glennrp | ce91ed5 | 2010-12-23 22:37:49 +0000 | [diff] [blame] | 11701 | { |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11702 | |
| 11703 | if (LocaleNCompare(value+i,"all",3) == 0) |
| 11704 | { |
| 11705 | mng_info->ping_exclude_bKGD=MagickTrue; |
| 11706 | mng_info->ping_exclude_cHRM=MagickTrue; |
glennrp | a0ed009 | 2011-04-18 16:36:29 +0000 | [diff] [blame] | 11707 | mng_info->ping_exclude_date=MagickTrue; |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11708 | mng_info->ping_exclude_EXIF=MagickTrue; |
| 11709 | mng_info->ping_exclude_gAMA=MagickTrue; |
| 11710 | mng_info->ping_exclude_iCCP=MagickTrue; |
| 11711 | /* mng_info->ping_exclude_iTXt=MagickTrue; */ |
| 11712 | mng_info->ping_exclude_oFFs=MagickTrue; |
| 11713 | mng_info->ping_exclude_pHYs=MagickTrue; |
| 11714 | mng_info->ping_exclude_sRGB=MagickTrue; |
| 11715 | mng_info->ping_exclude_tEXt=MagickTrue; |
glennrp | a1e3b7b | 2010-12-24 16:37:33 +0000 | [diff] [blame] | 11716 | mng_info->ping_exclude_tRNS=MagickTrue; |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11717 | mng_info->ping_exclude_vpAg=MagickTrue; |
| 11718 | mng_info->ping_exclude_zCCP=MagickTrue; |
| 11719 | mng_info->ping_exclude_zTXt=MagickTrue; |
| 11720 | i--; |
| 11721 | } |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11722 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11723 | if (LocaleNCompare(value+i,"none",4) == 0) |
| 11724 | { |
| 11725 | mng_info->ping_exclude_bKGD=MagickFalse; |
| 11726 | mng_info->ping_exclude_cHRM=MagickFalse; |
glennrp | a0ed009 | 2011-04-18 16:36:29 +0000 | [diff] [blame] | 11727 | mng_info->ping_exclude_date=MagickFalse; |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11728 | mng_info->ping_exclude_EXIF=MagickFalse; |
| 11729 | mng_info->ping_exclude_gAMA=MagickFalse; |
| 11730 | mng_info->ping_exclude_iCCP=MagickFalse; |
| 11731 | /* mng_info->ping_exclude_iTXt=MagickFalse; */ |
| 11732 | mng_info->ping_exclude_oFFs=MagickFalse; |
| 11733 | mng_info->ping_exclude_pHYs=MagickFalse; |
| 11734 | mng_info->ping_exclude_sRGB=MagickFalse; |
| 11735 | mng_info->ping_exclude_tEXt=MagickFalse; |
glennrp | a1e3b7b | 2010-12-24 16:37:33 +0000 | [diff] [blame] | 11736 | mng_info->ping_exclude_tRNS=MagickFalse; |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11737 | mng_info->ping_exclude_vpAg=MagickFalse; |
| 11738 | mng_info->ping_exclude_zCCP=MagickFalse; |
| 11739 | mng_info->ping_exclude_zTXt=MagickFalse; |
| 11740 | } |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11741 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11742 | if (LocaleNCompare(value+i,"bkgd",4) == 0) |
| 11743 | mng_info->ping_exclude_bKGD=MagickTrue; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11744 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11745 | if (LocaleNCompare(value+i,"chrm",4) == 0) |
| 11746 | mng_info->ping_exclude_cHRM=MagickTrue; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11747 | |
glennrp | a0ed009 | 2011-04-18 16:36:29 +0000 | [diff] [blame] | 11748 | if (LocaleNCompare(value+i,"date",4) == 0) |
| 11749 | mng_info->ping_exclude_date=MagickTrue; |
| 11750 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11751 | if (LocaleNCompare(value+i,"exif",4) == 0) |
| 11752 | mng_info->ping_exclude_EXIF=MagickTrue; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11753 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11754 | if (LocaleNCompare(value+i,"gama",4) == 0) |
| 11755 | mng_info->ping_exclude_gAMA=MagickTrue; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11756 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11757 | if (LocaleNCompare(value+i,"iccp",4) == 0) |
| 11758 | mng_info->ping_exclude_iCCP=MagickTrue; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11759 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11760 | /* |
| 11761 | if (LocaleNCompare(value+i,"itxt",4) == 0) |
| 11762 | mng_info->ping_exclude_iTXt=MagickTrue; |
| 11763 | */ |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11764 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11765 | if (LocaleNCompare(value+i,"gama",4) == 0) |
| 11766 | mng_info->ping_exclude_gAMA=MagickTrue; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11767 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11768 | if (LocaleNCompare(value+i,"offs",4) == 0) |
| 11769 | mng_info->ping_exclude_oFFs=MagickTrue; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11770 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11771 | if (LocaleNCompare(value+i,"phys",4) == 0) |
| 11772 | mng_info->ping_exclude_pHYs=MagickTrue; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11773 | |
glennrp | a1e3b7b | 2010-12-24 16:37:33 +0000 | [diff] [blame] | 11774 | if (LocaleNCompare(value+i,"srgb",4) == 0) |
| 11775 | mng_info->ping_exclude_sRGB=MagickTrue; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11776 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11777 | if (LocaleNCompare(value+i,"text",4) == 0) |
| 11778 | mng_info->ping_exclude_tEXt=MagickTrue; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11779 | |
glennrp | a1e3b7b | 2010-12-24 16:37:33 +0000 | [diff] [blame] | 11780 | if (LocaleNCompare(value+i,"trns",4) == 0) |
| 11781 | mng_info->ping_exclude_tRNS=MagickTrue; |
| 11782 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11783 | if (LocaleNCompare(value+i,"vpag",4) == 0) |
| 11784 | mng_info->ping_exclude_vpAg=MagickTrue; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11785 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11786 | if (LocaleNCompare(value+i,"zccp",4) == 0) |
| 11787 | mng_info->ping_exclude_zCCP=MagickTrue; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11788 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11789 | if (LocaleNCompare(value+i,"ztxt",4) == 0) |
| 11790 | mng_info->ping_exclude_zTXt=MagickTrue; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11791 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11792 | } |
glennrp | ce91ed5 | 2010-12-23 22:37:49 +0000 | [diff] [blame] | 11793 | } |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 11794 | } |
| 11795 | |
glennrp | 5c7cf4e | 2010-12-24 00:30:00 +0000 | [diff] [blame] | 11796 | for (source=0; source<1; source++) |
| 11797 | { |
| 11798 | if (source==0) |
glennrp | acba004 | 2010-12-24 14:27:26 +0000 | [diff] [blame] | 11799 | { |
glennrp | 5c7cf4e | 2010-12-24 00:30:00 +0000 | [diff] [blame] | 11800 | value=GetImageArtifact(image,"png:include-chunk"); |
glennrp | acba004 | 2010-12-24 14:27:26 +0000 | [diff] [blame] | 11801 | |
| 11802 | if (value == NULL) |
| 11803 | value=GetImageArtifact(image,"png:include-chunks"); |
| 11804 | } |
glennrp | 5c7cf4e | 2010-12-24 00:30:00 +0000 | [diff] [blame] | 11805 | else |
glennrp | acba004 | 2010-12-24 14:27:26 +0000 | [diff] [blame] | 11806 | { |
glennrp | 5c7cf4e | 2010-12-24 00:30:00 +0000 | [diff] [blame] | 11807 | value=GetImageOption(image_info,"png:include-chunk"); |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 11808 | |
glennrp | acba004 | 2010-12-24 14:27:26 +0000 | [diff] [blame] | 11809 | if (value == NULL) |
| 11810 | value=GetImageOption(image_info,"png:include-chunks"); |
| 11811 | } |
| 11812 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11813 | if (value != NULL) |
| 11814 | { |
| 11815 | size_t |
| 11816 | last; |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 11817 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11818 | excluding=MagickTrue; |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 11819 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11820 | if (logging != MagickFalse) |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11821 | { |
| 11822 | if (source == 0) |
| 11823 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 11824 | " png:include-chunk=%s found in image artifacts.\n", value); |
| 11825 | else |
| 11826 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 11827 | " png:include-chunk=%s found in image properties.\n", value); |
| 11828 | } |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11829 | |
| 11830 | last=strlen(value); |
| 11831 | |
| 11832 | for (i=0; i<(int) last; i+=5) |
| 11833 | { |
| 11834 | if (LocaleNCompare(value+i,"all",3) == 0) |
| 11835 | { |
| 11836 | mng_info->ping_exclude_bKGD=MagickFalse; |
| 11837 | mng_info->ping_exclude_cHRM=MagickFalse; |
glennrp | a0ed009 | 2011-04-18 16:36:29 +0000 | [diff] [blame] | 11838 | mng_info->ping_exclude_date=MagickFalse; |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11839 | mng_info->ping_exclude_EXIF=MagickFalse; |
| 11840 | mng_info->ping_exclude_gAMA=MagickFalse; |
| 11841 | mng_info->ping_exclude_iCCP=MagickFalse; |
| 11842 | /* mng_info->ping_exclude_iTXt=MagickFalse; */ |
| 11843 | mng_info->ping_exclude_oFFs=MagickFalse; |
| 11844 | mng_info->ping_exclude_pHYs=MagickFalse; |
| 11845 | mng_info->ping_exclude_sRGB=MagickFalse; |
| 11846 | mng_info->ping_exclude_tEXt=MagickFalse; |
glennrp | a1e3b7b | 2010-12-24 16:37:33 +0000 | [diff] [blame] | 11847 | mng_info->ping_exclude_tRNS=MagickFalse; |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11848 | mng_info->ping_exclude_vpAg=MagickFalse; |
| 11849 | mng_info->ping_exclude_zCCP=MagickFalse; |
| 11850 | mng_info->ping_exclude_zTXt=MagickFalse; |
| 11851 | i--; |
| 11852 | } |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11853 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11854 | if (LocaleNCompare(value+i,"none",4) == 0) |
| 11855 | { |
| 11856 | mng_info->ping_exclude_bKGD=MagickTrue; |
| 11857 | mng_info->ping_exclude_cHRM=MagickTrue; |
glennrp | a0ed009 | 2011-04-18 16:36:29 +0000 | [diff] [blame] | 11858 | mng_info->ping_exclude_date=MagickTrue; |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11859 | mng_info->ping_exclude_EXIF=MagickTrue; |
| 11860 | mng_info->ping_exclude_gAMA=MagickTrue; |
| 11861 | mng_info->ping_exclude_iCCP=MagickTrue; |
| 11862 | /* mng_info->ping_exclude_iTXt=MagickTrue; */ |
| 11863 | mng_info->ping_exclude_oFFs=MagickTrue; |
| 11864 | mng_info->ping_exclude_pHYs=MagickTrue; |
| 11865 | mng_info->ping_exclude_sRGB=MagickTrue; |
| 11866 | mng_info->ping_exclude_tEXt=MagickTrue; |
glennrp | a1e3b7b | 2010-12-24 16:37:33 +0000 | [diff] [blame] | 11867 | mng_info->ping_exclude_tRNS=MagickTrue; |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11868 | mng_info->ping_exclude_vpAg=MagickTrue; |
| 11869 | mng_info->ping_exclude_zCCP=MagickTrue; |
| 11870 | mng_info->ping_exclude_zTXt=MagickTrue; |
| 11871 | } |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11872 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11873 | if (LocaleNCompare(value+i,"bkgd",4) == 0) |
| 11874 | mng_info->ping_exclude_bKGD=MagickFalse; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11875 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11876 | if (LocaleNCompare(value+i,"chrm",4) == 0) |
| 11877 | mng_info->ping_exclude_cHRM=MagickFalse; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11878 | |
glennrp | a0ed009 | 2011-04-18 16:36:29 +0000 | [diff] [blame] | 11879 | if (LocaleNCompare(value+i,"date",4) == 0) |
| 11880 | mng_info->ping_exclude_date=MagickFalse; |
| 11881 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11882 | if (LocaleNCompare(value+i,"exif",4) == 0) |
| 11883 | mng_info->ping_exclude_EXIF=MagickFalse; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11884 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11885 | if (LocaleNCompare(value+i,"gama",4) == 0) |
| 11886 | mng_info->ping_exclude_gAMA=MagickFalse; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11887 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11888 | if (LocaleNCompare(value+i,"iccp",4) == 0) |
| 11889 | mng_info->ping_exclude_iCCP=MagickFalse; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11890 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11891 | /* |
| 11892 | if (LocaleNCompare(value+i,"itxt",4) == 0) |
| 11893 | mng_info->ping_exclude_iTXt=MagickFalse; |
| 11894 | */ |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11895 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11896 | if (LocaleNCompare(value+i,"gama",4) == 0) |
| 11897 | mng_info->ping_exclude_gAMA=MagickFalse; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11898 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11899 | if (LocaleNCompare(value+i,"offs",4) == 0) |
| 11900 | mng_info->ping_exclude_oFFs=MagickFalse; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11901 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11902 | if (LocaleNCompare(value+i,"phys",4) == 0) |
| 11903 | mng_info->ping_exclude_pHYs=MagickFalse; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11904 | |
glennrp | a1e3b7b | 2010-12-24 16:37:33 +0000 | [diff] [blame] | 11905 | if (LocaleNCompare(value+i,"srgb",4) == 0) |
| 11906 | mng_info->ping_exclude_sRGB=MagickFalse; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11907 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11908 | if (LocaleNCompare(value+i,"text",4) == 0) |
| 11909 | mng_info->ping_exclude_tEXt=MagickFalse; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11910 | |
glennrp | a1e3b7b | 2010-12-24 16:37:33 +0000 | [diff] [blame] | 11911 | if (LocaleNCompare(value+i,"trns",4) == 0) |
| 11912 | mng_info->ping_exclude_tRNS=MagickFalse; |
| 11913 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11914 | if (LocaleNCompare(value+i,"vpag",4) == 0) |
| 11915 | mng_info->ping_exclude_vpAg=MagickFalse; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11916 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11917 | if (LocaleNCompare(value+i,"zccp",4) == 0) |
| 11918 | mng_info->ping_exclude_zCCP=MagickFalse; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11919 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11920 | if (LocaleNCompare(value+i,"ztxt",4) == 0) |
| 11921 | mng_info->ping_exclude_zTXt=MagickFalse; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11922 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11923 | } |
glennrp | ce91ed5 | 2010-12-23 22:37:49 +0000 | [diff] [blame] | 11924 | } |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 11925 | } |
| 11926 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11927 | if (excluding != MagickFalse && logging != MagickFalse) |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 11928 | { |
| 11929 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | 5d6fc9c | 2011-12-27 03:10:42 +0000 | [diff] [blame] | 11930 | " Chunks to be excluded from the output png:"); |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 11931 | if (mng_info->ping_exclude_bKGD != MagickFalse) |
| 11932 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 11933 | " bKGD"); |
| 11934 | if (mng_info->ping_exclude_cHRM != MagickFalse) |
| 11935 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 11936 | " cHRM"); |
glennrp | a0ed009 | 2011-04-18 16:36:29 +0000 | [diff] [blame] | 11937 | if (mng_info->ping_exclude_date != MagickFalse) |
| 11938 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 11939 | " date"); |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 11940 | if (mng_info->ping_exclude_EXIF != MagickFalse) |
| 11941 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 11942 | " EXIF"); |
| 11943 | if (mng_info->ping_exclude_gAMA != MagickFalse) |
| 11944 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 11945 | " gAMA"); |
| 11946 | if (mng_info->ping_exclude_iCCP != MagickFalse) |
| 11947 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 11948 | " iCCP"); |
| 11949 | /* |
| 11950 | if (mng_info->ping_exclude_iTXt != MagickFalse) |
| 11951 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 11952 | " iTXt"); |
| 11953 | */ |
| 11954 | if (mng_info->ping_exclude_oFFs != MagickFalse) |
| 11955 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 11956 | " oFFs"); |
| 11957 | if (mng_info->ping_exclude_pHYs != MagickFalse) |
| 11958 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 11959 | " pHYs"); |
| 11960 | if (mng_info->ping_exclude_sRGB != MagickFalse) |
| 11961 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 11962 | " sRGB"); |
| 11963 | if (mng_info->ping_exclude_tEXt != MagickFalse) |
| 11964 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 11965 | " tEXt"); |
glennrp | a1e3b7b | 2010-12-24 16:37:33 +0000 | [diff] [blame] | 11966 | if (mng_info->ping_exclude_tRNS != MagickFalse) |
| 11967 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 11968 | " tRNS"); |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 11969 | if (mng_info->ping_exclude_vpAg != MagickFalse) |
| 11970 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 11971 | " vpAg"); |
| 11972 | if (mng_info->ping_exclude_zCCP != MagickFalse) |
| 11973 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 11974 | " zCCP"); |
| 11975 | if (mng_info->ping_exclude_zTXt != MagickFalse) |
| 11976 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 11977 | " zTXt"); |
| 11978 | } |
| 11979 | |
glennrp | b9cfe27 | 2010-12-21 15:08:06 +0000 | [diff] [blame] | 11980 | mng_info->need_blob = MagickTrue; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11981 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 11982 | status=WriteOnePNGImage(mng_info,image_info,image,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11983 | |
| 11984 | MngInfoFreeStruct(mng_info,&have_mng_structure); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11985 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11986 | if (logging != MagickFalse) |
| 11987 | (void) LogMagickEvent(CoderEvent,GetMagickModule(),"exit WritePNGImage()"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11988 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11989 | return(status); |
| 11990 | } |
| 11991 | |
| 11992 | #if defined(JNG_SUPPORTED) |
| 11993 | |
| 11994 | /* Write one JNG image */ |
| 11995 | static MagickBooleanType WriteOneJNGImage(MngInfo *mng_info, |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 11996 | const ImageInfo *image_info,Image *image,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11997 | { |
| 11998 | Image |
| 11999 | *jpeg_image; |
| 12000 | |
| 12001 | ImageInfo |
| 12002 | *jpeg_image_info; |
| 12003 | |
| 12004 | MagickBooleanType |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12005 | logging, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12006 | status; |
| 12007 | |
| 12008 | size_t |
| 12009 | length; |
| 12010 | |
| 12011 | unsigned char |
| 12012 | *blob, |
| 12013 | chunk[80], |
| 12014 | *p; |
| 12015 | |
| 12016 | unsigned int |
| 12017 | jng_alpha_compression_method, |
| 12018 | jng_alpha_sample_depth, |
| 12019 | jng_color_type, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12020 | transparent; |
| 12021 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 12022 | size_t |
glennrp | 59575fa | 2011-12-31 21:31:39 +0000 | [diff] [blame] | 12023 | jng_alpha_quality, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12024 | jng_quality; |
| 12025 | |
| 12026 | logging=LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 12027 | " Enter WriteOneJNGImage()"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12028 | |
| 12029 | blob=(unsigned char *) NULL; |
| 12030 | jpeg_image=(Image *) NULL; |
| 12031 | jpeg_image_info=(ImageInfo *) NULL; |
| 12032 | |
| 12033 | status=MagickTrue; |
| 12034 | transparent=image_info->type==GrayscaleMatteType || |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 12035 | image_info->type==TrueColorMatteType || image->alpha_trait == BlendPixelTrait; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12036 | |
glennrp | 59575fa | 2011-12-31 21:31:39 +0000 | [diff] [blame] | 12037 | jng_quality=image_info->quality == 0UL ? 75UL : image_info->quality%1000; |
| 12038 | |
| 12039 | jng_alpha_compression_method=image->compression==JPEGCompression? 8 : 0; |
| 12040 | |
glennrp | 750105b | 2012-04-25 16:20:45 +0000 | [diff] [blame] | 12041 | jng_alpha_quality=image_info->quality == 0UL ? 75UL : |
glennrp | 59575fa | 2011-12-31 21:31:39 +0000 | [diff] [blame] | 12042 | image_info->quality; |
| 12043 | |
| 12044 | if (jng_alpha_quality >= 1000) |
| 12045 | jng_alpha_quality /= 1000; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12046 | |
| 12047 | if (transparent) |
| 12048 | { |
| 12049 | jng_color_type=14; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12050 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12051 | /* Create JPEG blob, image, and image_info */ |
| 12052 | if (logging != MagickFalse) |
| 12053 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 12054 | " Creating jpeg_image_info for alpha."); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12055 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12056 | jpeg_image_info=(ImageInfo *) CloneImageInfo(image_info); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12057 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12058 | if (jpeg_image_info == (ImageInfo *) NULL) |
| 12059 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12060 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12061 | if (logging != MagickFalse) |
| 12062 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 12063 | " Creating jpeg_image."); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12064 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 12065 | jpeg_image=SeparateImage(image,AlphaChannel,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12066 | if (jpeg_image == (Image *) NULL) |
| 12067 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
| 12068 | (void) CopyMagickString(jpeg_image->magick,"JPEG",MaxTextExtent); |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 12069 | jpeg_image->alpha_trait=UndefinedPixelTrait; |
glennrp | 8f77fdc | 2012-03-21 15:16:37 +0000 | [diff] [blame] | 12070 | jpeg_image->quality=jng_alpha_quality; |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 12071 | jpeg_image_info->type=GrayscaleType; |
| 12072 | (void) SetImageType(jpeg_image,GrayscaleType,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12073 | (void) AcquireUniqueFilename(jpeg_image->filename); |
cristy | 3b6fd2e | 2011-05-20 12:53:50 +0000 | [diff] [blame] | 12074 | (void) FormatLocaleString(jpeg_image_info->filename,MaxTextExtent, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12075 | "%s",jpeg_image->filename); |
| 12076 | } |
glennrp | 59575fa | 2011-12-31 21:31:39 +0000 | [diff] [blame] | 12077 | else |
| 12078 | { |
| 12079 | jng_alpha_compression_method=0; |
| 12080 | jng_color_type=10; |
| 12081 | jng_alpha_sample_depth=0; |
| 12082 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12083 | |
| 12084 | /* To do: check bit depth of PNG alpha channel */ |
| 12085 | |
| 12086 | /* Check if image is grayscale. */ |
| 12087 | if (image_info->type != TrueColorMatteType && image_info->type != |
cristy | 7fb2652 | 2012-06-21 13:02:48 +0000 | [diff] [blame] | 12088 | TrueColorType && IsImageGray(image,exception)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12089 | jng_color_type-=2; |
| 12090 | |
glennrp | 59575fa | 2011-12-31 21:31:39 +0000 | [diff] [blame] | 12091 | if (logging != MagickFalse) |
| 12092 | { |
| 12093 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 12094 | " JNG Quality = %d",(int) jng_quality); |
| 12095 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 12096 | " JNG Color Type = %d",jng_color_type); |
| 12097 | if (transparent) |
| 12098 | { |
| 12099 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 12100 | " JNG Alpha Compression = %d",jng_alpha_compression_method); |
| 12101 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 12102 | " JNG Alpha Depth = %d",jng_alpha_sample_depth); |
| 12103 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 12104 | " JNG Alpha Quality = %d",(int) jng_alpha_quality); |
| 12105 | } |
| 12106 | } |
| 12107 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12108 | if (transparent) |
| 12109 | { |
| 12110 | if (jng_alpha_compression_method==0) |
| 12111 | { |
| 12112 | const char |
| 12113 | *value; |
| 12114 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 12115 | /* Encode alpha as a grayscale PNG blob */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12116 | status=OpenBlob(jpeg_image_info,jpeg_image,WriteBinaryBlobMode, |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 12117 | exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12118 | if (logging != MagickFalse) |
| 12119 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 12120 | " Creating PNG blob."); |
| 12121 | length=0; |
| 12122 | |
| 12123 | (void) CopyMagickString(jpeg_image_info->magick,"PNG",MaxTextExtent); |
| 12124 | (void) CopyMagickString(jpeg_image->magick,"PNG",MaxTextExtent); |
| 12125 | jpeg_image_info->interlace=NoInterlace; |
| 12126 | |
glennrp | cc5d45b | 2012-01-06 04:06:10 +0000 | [diff] [blame] | 12127 | /* Exclude all ancillary chunks */ |
| 12128 | (void) SetImageArtifact(jpeg_image,"png:exclude-chunks","all"); |
| 12129 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12130 | blob=ImageToBlob(jpeg_image_info,jpeg_image,&length, |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 12131 | exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12132 | |
| 12133 | /* Retrieve sample depth used */ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 12134 | value=GetImageProperty(jpeg_image,"png:bit-depth-written",exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12135 | if (value != (char *) NULL) |
| 12136 | jng_alpha_sample_depth= (unsigned int) value[0]; |
| 12137 | } |
| 12138 | else |
| 12139 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 12140 | /* Encode alpha as a grayscale JPEG blob */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12141 | |
| 12142 | status=OpenBlob(jpeg_image_info,jpeg_image,WriteBinaryBlobMode, |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 12143 | exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12144 | |
| 12145 | (void) CopyMagickString(jpeg_image_info->magick,"JPEG",MaxTextExtent); |
| 12146 | (void) CopyMagickString(jpeg_image->magick,"JPEG",MaxTextExtent); |
| 12147 | jpeg_image_info->interlace=NoInterlace; |
| 12148 | if (logging != MagickFalse) |
| 12149 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 12150 | " Creating blob."); |
| 12151 | blob=ImageToBlob(jpeg_image_info,jpeg_image,&length, |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 12152 | exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12153 | jng_alpha_sample_depth=8; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12154 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12155 | if (logging != MagickFalse) |
| 12156 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 12157 | " Successfully read jpeg_image into a blob, length=%.20g.", |
| 12158 | (double) length); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12159 | |
| 12160 | } |
| 12161 | /* Destroy JPEG image and image_info */ |
| 12162 | jpeg_image=DestroyImage(jpeg_image); |
| 12163 | (void) RelinquishUniqueFileResource(jpeg_image_info->filename); |
| 12164 | jpeg_image_info=DestroyImageInfo(jpeg_image_info); |
| 12165 | } |
| 12166 | |
| 12167 | /* Write JHDR chunk */ |
| 12168 | (void) WriteBlobMSBULong(image,16L); /* chunk data length=16 */ |
| 12169 | PNGType(chunk,mng_JHDR); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12170 | LogPNGChunk(logging,mng_JHDR,16L); |
cristy | 4e5bc84 | 2010-06-09 13:56:01 +0000 | [diff] [blame] | 12171 | PNGLong(chunk+4,(png_uint_32) image->columns); |
| 12172 | PNGLong(chunk+8,(png_uint_32) image->rows); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12173 | chunk[12]=jng_color_type; |
| 12174 | chunk[13]=8; /* sample depth */ |
| 12175 | chunk[14]=8; /*jng_image_compression_method */ |
| 12176 | chunk[15]=(unsigned char) (image_info->interlace == NoInterlace ? 0 : 8); |
| 12177 | chunk[16]=jng_alpha_sample_depth; |
| 12178 | chunk[17]=jng_alpha_compression_method; |
| 12179 | chunk[18]=0; /*jng_alpha_filter_method */ |
| 12180 | chunk[19]=0; /*jng_alpha_interlace_method */ |
| 12181 | (void) WriteBlob(image,20,chunk); |
| 12182 | (void) WriteBlobMSBULong(image,crc32(0,chunk,20)); |
| 12183 | if (logging != MagickFalse) |
| 12184 | { |
| 12185 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | f2faecf | 2010-05-28 19:19:36 +0000 | [diff] [blame] | 12186 | " JNG width:%15lu",(unsigned long) image->columns); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12187 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12188 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | f2faecf | 2010-05-28 19:19:36 +0000 | [diff] [blame] | 12189 | " JNG height:%14lu",(unsigned long) image->rows); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12190 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12191 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 12192 | " JNG color type:%10d",jng_color_type); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12193 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12194 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 12195 | " JNG sample depth:%8d",8); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12196 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12197 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 12198 | " JNG compression:%9d",8); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12199 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12200 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 12201 | " JNG interlace:%11d",0); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12202 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12203 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 12204 | " JNG alpha depth:%9d",jng_alpha_sample_depth); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12205 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12206 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 12207 | " JNG alpha compression:%3d",jng_alpha_compression_method); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12208 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12209 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 12210 | " JNG alpha filter:%8d",0); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12211 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12212 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 12213 | " JNG alpha interlace:%5d",0); |
| 12214 | } |
| 12215 | |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12216 | /* Write any JNG-chunk-b profiles */ |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 12217 | (void) Magick_png_write_chunk_from_profile(image,"JNG-chunk-b",logging); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12218 | |
| 12219 | /* |
| 12220 | Write leading ancillary chunks |
| 12221 | */ |
| 12222 | |
| 12223 | if (transparent) |
| 12224 | { |
| 12225 | /* |
| 12226 | Write JNG bKGD chunk |
| 12227 | */ |
| 12228 | |
| 12229 | unsigned char |
| 12230 | blue, |
| 12231 | green, |
| 12232 | red; |
| 12233 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 12234 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12235 | num_bytes; |
| 12236 | |
| 12237 | if (jng_color_type == 8 || jng_color_type == 12) |
| 12238 | num_bytes=6L; |
| 12239 | else |
| 12240 | num_bytes=10L; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 12241 | (void) WriteBlobMSBULong(image,(size_t) (num_bytes-4L)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12242 | PNGType(chunk,mng_bKGD); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12243 | LogPNGChunk(logging,mng_bKGD,(size_t) (num_bytes-4L)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12244 | red=ScaleQuantumToChar(image->background_color.red); |
| 12245 | green=ScaleQuantumToChar(image->background_color.green); |
| 12246 | blue=ScaleQuantumToChar(image->background_color.blue); |
| 12247 | *(chunk+4)=0; |
| 12248 | *(chunk+5)=red; |
| 12249 | *(chunk+6)=0; |
| 12250 | *(chunk+7)=green; |
| 12251 | *(chunk+8)=0; |
| 12252 | *(chunk+9)=blue; |
| 12253 | (void) WriteBlob(image,(size_t) num_bytes,chunk); |
| 12254 | (void) WriteBlobMSBULong(image,crc32(0,chunk,(uInt) num_bytes)); |
| 12255 | } |
| 12256 | |
| 12257 | if ((image->colorspace == sRGBColorspace || image->rendering_intent)) |
| 12258 | { |
| 12259 | /* |
| 12260 | Write JNG sRGB chunk |
| 12261 | */ |
| 12262 | (void) WriteBlobMSBULong(image,1L); |
| 12263 | PNGType(chunk,mng_sRGB); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12264 | LogPNGChunk(logging,mng_sRGB,1L); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12265 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12266 | if (image->rendering_intent != UndefinedIntent) |
glennrp | e610a07 | 2010-08-05 17:08:46 +0000 | [diff] [blame] | 12267 | chunk[4]=(unsigned char) |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 12268 | Magick_RenderingIntent_to_PNG_RenderingIntent( |
glennrp | e610a07 | 2010-08-05 17:08:46 +0000 | [diff] [blame] | 12269 | (image->rendering_intent)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12270 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12271 | else |
glennrp | e610a07 | 2010-08-05 17:08:46 +0000 | [diff] [blame] | 12272 | chunk[4]=(unsigned char) |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 12273 | Magick_RenderingIntent_to_PNG_RenderingIntent( |
glennrp | e610a07 | 2010-08-05 17:08:46 +0000 | [diff] [blame] | 12274 | (PerceptualIntent)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12275 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12276 | (void) WriteBlob(image,5,chunk); |
| 12277 | (void) WriteBlobMSBULong(image,crc32(0,chunk,5)); |
| 12278 | } |
| 12279 | else |
| 12280 | { |
| 12281 | if (image->gamma != 0.0) |
| 12282 | { |
| 12283 | /* |
| 12284 | Write JNG gAMA chunk |
| 12285 | */ |
| 12286 | (void) WriteBlobMSBULong(image,4L); |
| 12287 | PNGType(chunk,mng_gAMA); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12288 | LogPNGChunk(logging,mng_gAMA,4L); |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 12289 | PNGLong(chunk+4,(png_uint_32) (100000*image->gamma+0.5)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12290 | (void) WriteBlob(image,8,chunk); |
| 12291 | (void) WriteBlobMSBULong(image,crc32(0,chunk,8)); |
| 12292 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12293 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12294 | if ((mng_info->equal_chrms == MagickFalse) && |
| 12295 | (image->chromaticity.red_primary.x != 0.0)) |
| 12296 | { |
| 12297 | PrimaryInfo |
| 12298 | primary; |
| 12299 | |
| 12300 | /* |
| 12301 | Write JNG cHRM chunk |
| 12302 | */ |
| 12303 | (void) WriteBlobMSBULong(image,32L); |
| 12304 | PNGType(chunk,mng_cHRM); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12305 | LogPNGChunk(logging,mng_cHRM,32L); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12306 | primary=image->chromaticity.white_point; |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 12307 | PNGLong(chunk+4,(png_uint_32) (100000*primary.x+0.5)); |
| 12308 | PNGLong(chunk+8,(png_uint_32) (100000*primary.y+0.5)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12309 | primary=image->chromaticity.red_primary; |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 12310 | PNGLong(chunk+12,(png_uint_32) (100000*primary.x+0.5)); |
| 12311 | PNGLong(chunk+16,(png_uint_32) (100000*primary.y+0.5)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12312 | primary=image->chromaticity.green_primary; |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 12313 | PNGLong(chunk+20,(png_uint_32) (100000*primary.x+0.5)); |
| 12314 | PNGLong(chunk+24,(png_uint_32) (100000*primary.y+0.5)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12315 | primary=image->chromaticity.blue_primary; |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 12316 | PNGLong(chunk+28,(png_uint_32) (100000*primary.x+0.5)); |
| 12317 | PNGLong(chunk+32,(png_uint_32) (100000*primary.y+0.5)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12318 | (void) WriteBlob(image,36,chunk); |
| 12319 | (void) WriteBlobMSBULong(image,crc32(0,chunk,36)); |
| 12320 | } |
| 12321 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12322 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 12323 | if (image->resolution.x && image->resolution.y && !mng_info->equal_physs) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12324 | { |
| 12325 | /* |
| 12326 | Write JNG pHYs chunk |
| 12327 | */ |
| 12328 | (void) WriteBlobMSBULong(image,9L); |
| 12329 | PNGType(chunk,mng_pHYs); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12330 | LogPNGChunk(logging,mng_pHYs,9L); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12331 | if (image->units == PixelsPerInchResolution) |
| 12332 | { |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 12333 | PNGLong(chunk+4,(png_uint_32) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 12334 | (image->resolution.x*100.0/2.54+0.5)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12335 | |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 12336 | PNGLong(chunk+8,(png_uint_32) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 12337 | (image->resolution.y*100.0/2.54+0.5)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12338 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12339 | chunk[12]=1; |
| 12340 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12341 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12342 | else |
| 12343 | { |
| 12344 | if (image->units == PixelsPerCentimeterResolution) |
| 12345 | { |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 12346 | PNGLong(chunk+4,(png_uint_32) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 12347 | (image->resolution.x*100.0+0.5)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12348 | |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 12349 | PNGLong(chunk+8,(png_uint_32) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 12350 | (image->resolution.y*100.0+0.5)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12351 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12352 | chunk[12]=1; |
| 12353 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12354 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12355 | else |
| 12356 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 12357 | PNGLong(chunk+4,(png_uint_32) (image->resolution.x+0.5)); |
| 12358 | PNGLong(chunk+8,(png_uint_32) (image->resolution.y+0.5)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12359 | chunk[12]=0; |
| 12360 | } |
| 12361 | } |
| 12362 | (void) WriteBlob(image,13,chunk); |
| 12363 | (void) WriteBlobMSBULong(image,crc32(0,chunk,13)); |
| 12364 | } |
| 12365 | |
| 12366 | if (mng_info->write_mng == 0 && (image->page.x || image->page.y)) |
| 12367 | { |
| 12368 | /* |
| 12369 | Write JNG oFFs chunk |
| 12370 | */ |
| 12371 | (void) WriteBlobMSBULong(image,9L); |
| 12372 | PNGType(chunk,mng_oFFs); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12373 | LogPNGChunk(logging,mng_oFFs,9L); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 12374 | PNGsLong(chunk+4,(ssize_t) (image->page.x)); |
| 12375 | PNGsLong(chunk+8,(ssize_t) (image->page.y)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12376 | chunk[12]=0; |
| 12377 | (void) WriteBlob(image,13,chunk); |
| 12378 | (void) WriteBlobMSBULong(image,crc32(0,chunk,13)); |
| 12379 | } |
| 12380 | if (mng_info->write_mng == 0 && (image->page.width || image->page.height)) |
| 12381 | { |
| 12382 | (void) WriteBlobMSBULong(image,9L); /* data length=8 */ |
| 12383 | PNGType(chunk,mng_vpAg); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12384 | LogPNGChunk(logging,mng_vpAg,9L); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12385 | PNGLong(chunk+4,(png_uint_32) image->page.width); |
| 12386 | PNGLong(chunk+8,(png_uint_32) image->page.height); |
| 12387 | chunk[12]=0; /* unit = pixels */ |
| 12388 | (void) WriteBlob(image,13,chunk); |
| 12389 | (void) WriteBlobMSBULong(image,crc32(0,chunk,13)); |
| 12390 | } |
| 12391 | |
| 12392 | |
| 12393 | if (transparent) |
| 12394 | { |
| 12395 | if (jng_alpha_compression_method==0) |
| 12396 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 12397 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12398 | i; |
| 12399 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 12400 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12401 | len; |
| 12402 | |
| 12403 | /* Write IDAT chunk header */ |
| 12404 | if (logging != MagickFalse) |
| 12405 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 12406 | " Write IDAT chunks from blob, length=%.20g.",(double) |
cristy | f2faecf | 2010-05-28 19:19:36 +0000 | [diff] [blame] | 12407 | length); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12408 | |
| 12409 | /* Copy IDAT chunks */ |
| 12410 | len=0; |
| 12411 | p=blob+8; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 12412 | for (i=8; i<(ssize_t) length; i+=len+12) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12413 | { |
| 12414 | len=(*p<<24)|((*(p+1))<<16)|((*(p+2))<<8)|(*(p+3)); |
| 12415 | p+=4; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12416 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12417 | if (*(p)==73 && *(p+1)==68 && *(p+2)==65 && *(p+3)==84) /* IDAT */ |
| 12418 | { |
| 12419 | /* Found an IDAT chunk. */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 12420 | (void) WriteBlobMSBULong(image,(size_t) len); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12421 | LogPNGChunk(logging,mng_IDAT,(size_t) len); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12422 | (void) WriteBlob(image,(size_t) len+4,p); |
| 12423 | (void) WriteBlobMSBULong(image, |
| 12424 | crc32(0,p,(uInt) len+4)); |
| 12425 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12426 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12427 | else |
| 12428 | { |
| 12429 | if (logging != MagickFalse) |
| 12430 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 12431 | " Skipping %c%c%c%c chunk, length=%.20g.", |
| 12432 | *(p),*(p+1),*(p+2),*(p+3),(double) len); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12433 | } |
| 12434 | p+=(8+len); |
| 12435 | } |
| 12436 | } |
| 12437 | else |
| 12438 | { |
| 12439 | /* Write JDAA chunk header */ |
| 12440 | if (logging != MagickFalse) |
| 12441 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 12442 | " Write JDAA chunk, length=%.20g.",(double) length); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 12443 | (void) WriteBlobMSBULong(image,(size_t) length); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12444 | PNGType(chunk,mng_JDAA); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12445 | LogPNGChunk(logging,mng_JDAA,length); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12446 | /* Write JDAT chunk(s) data */ |
| 12447 | (void) WriteBlob(image,4,chunk); |
| 12448 | (void) WriteBlob(image,length,blob); |
| 12449 | (void) WriteBlobMSBULong(image,crc32(crc32(0,chunk,4),blob, |
| 12450 | (uInt) length)); |
| 12451 | } |
| 12452 | blob=(unsigned char *) RelinquishMagickMemory(blob); |
| 12453 | } |
| 12454 | |
| 12455 | /* Encode image as a JPEG blob */ |
| 12456 | if (logging != MagickFalse) |
| 12457 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 12458 | " Creating jpeg_image_info."); |
| 12459 | jpeg_image_info=(ImageInfo *) CloneImageInfo(image_info); |
| 12460 | if (jpeg_image_info == (ImageInfo *) NULL) |
| 12461 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
| 12462 | |
| 12463 | if (logging != MagickFalse) |
| 12464 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 12465 | " Creating jpeg_image."); |
| 12466 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 12467 | jpeg_image=CloneImage(image,0,0,MagickTrue,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12468 | if (jpeg_image == (Image *) NULL) |
| 12469 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
| 12470 | (void) CopyMagickString(jpeg_image->magick,"JPEG",MaxTextExtent); |
| 12471 | |
| 12472 | (void) AcquireUniqueFilename(jpeg_image->filename); |
cristy | 3b6fd2e | 2011-05-20 12:53:50 +0000 | [diff] [blame] | 12473 | (void) FormatLocaleString(jpeg_image_info->filename,MaxTextExtent,"%s", |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12474 | jpeg_image->filename); |
| 12475 | |
| 12476 | status=OpenBlob(jpeg_image_info,jpeg_image,WriteBinaryBlobMode, |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 12477 | exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12478 | |
| 12479 | if (logging != MagickFalse) |
| 12480 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 12481 | " Created jpeg_image, %.20g x %.20g.",(double) jpeg_image->columns, |
| 12482 | (double) jpeg_image->rows); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12483 | |
| 12484 | if (jng_color_type == 8 || jng_color_type == 12) |
| 12485 | jpeg_image_info->type=GrayscaleType; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12486 | |
glennrp | 59575fa | 2011-12-31 21:31:39 +0000 | [diff] [blame] | 12487 | jpeg_image_info->quality=jng_quality; |
| 12488 | jpeg_image->quality=jng_quality; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12489 | (void) CopyMagickString(jpeg_image_info->magick,"JPEG",MaxTextExtent); |
| 12490 | (void) CopyMagickString(jpeg_image->magick,"JPEG",MaxTextExtent); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12491 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12492 | if (logging != MagickFalse) |
| 12493 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 12494 | " Creating blob."); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12495 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 12496 | blob=ImageToBlob(jpeg_image_info,jpeg_image,&length,exception); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12497 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12498 | if (logging != MagickFalse) |
| 12499 | { |
| 12500 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 12501 | " Successfully read jpeg_image into a blob, length=%.20g.", |
| 12502 | (double) length); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12503 | |
| 12504 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 12505 | " Write JDAT chunk, length=%.20g.",(double) length); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12506 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12507 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12508 | /* Write JDAT chunk(s) */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 12509 | (void) WriteBlobMSBULong(image,(size_t) length); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12510 | PNGType(chunk,mng_JDAT); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12511 | LogPNGChunk(logging,mng_JDAT,length); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12512 | (void) WriteBlob(image,4,chunk); |
| 12513 | (void) WriteBlob(image,length,blob); |
| 12514 | (void) WriteBlobMSBULong(image,crc32(crc32(0,chunk,4),blob,(uInt) length)); |
| 12515 | |
| 12516 | jpeg_image=DestroyImage(jpeg_image); |
| 12517 | (void) RelinquishUniqueFileResource(jpeg_image_info->filename); |
| 12518 | jpeg_image_info=DestroyImageInfo(jpeg_image_info); |
| 12519 | blob=(unsigned char *) RelinquishMagickMemory(blob); |
| 12520 | |
| 12521 | /* Write any JNG-chunk-e profiles */ |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 12522 | (void) Magick_png_write_chunk_from_profile(image,"JNG-chunk-e",logging); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12523 | |
| 12524 | /* Write IEND chunk */ |
| 12525 | (void) WriteBlobMSBULong(image,0L); |
| 12526 | PNGType(chunk,mng_IEND); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12527 | LogPNGChunk(logging,mng_IEND,0); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12528 | (void) WriteBlob(image,4,chunk); |
| 12529 | (void) WriteBlobMSBULong(image,crc32(0,chunk,4)); |
| 12530 | |
| 12531 | if (logging != MagickFalse) |
| 12532 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 12533 | " exit WriteOneJNGImage()"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12534 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12535 | return(status); |
| 12536 | } |
| 12537 | |
| 12538 | |
| 12539 | /* |
| 12540 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 12541 | % % |
| 12542 | % % |
| 12543 | % % |
| 12544 | % W r i t e J N G I m a g e % |
| 12545 | % % |
| 12546 | % % |
| 12547 | % % |
| 12548 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 12549 | % |
| 12550 | % WriteJNGImage() writes a JPEG Network Graphics (JNG) image file. |
| 12551 | % |
| 12552 | % JNG support written by Glenn Randers-Pehrson, glennrp@image... |
| 12553 | % |
| 12554 | % The format of the WriteJNGImage method is: |
| 12555 | % |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 12556 | % MagickBooleanType WriteJNGImage(const ImageInfo *image_info, |
| 12557 | % Image *image,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12558 | % |
| 12559 | % A description of each parameter follows: |
| 12560 | % |
| 12561 | % o image_info: the image info. |
| 12562 | % |
| 12563 | % o image: The image. |
| 12564 | % |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 12565 | % o exception: return any errors or warnings in this structure. |
| 12566 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12567 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 12568 | */ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 12569 | static MagickBooleanType WriteJNGImage(const ImageInfo *image_info,Image *image, |
| 12570 | ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12571 | { |
| 12572 | MagickBooleanType |
glennrp | 21f0e62 | 2011-01-07 16:20:57 +0000 | [diff] [blame] | 12573 | have_mng_structure, |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12574 | logging, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12575 | status; |
| 12576 | |
| 12577 | MngInfo |
| 12578 | *mng_info; |
| 12579 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12580 | /* |
| 12581 | Open image file. |
| 12582 | */ |
| 12583 | assert(image_info != (const ImageInfo *) NULL); |
| 12584 | assert(image_info->signature == MagickSignature); |
| 12585 | assert(image != (Image *) NULL); |
| 12586 | assert(image->signature == MagickSignature); |
| 12587 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 12588 | logging=LogMagickEvent(CoderEvent,GetMagickModule(),"Enter WriteJNGImage()"); |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 12589 | status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12590 | if (status == MagickFalse) |
| 12591 | return(status); |
| 12592 | |
| 12593 | /* |
| 12594 | Allocate a MngInfo structure. |
| 12595 | */ |
| 12596 | have_mng_structure=MagickFalse; |
cristy | 73bd4a5 | 2010-10-05 11:24:23 +0000 | [diff] [blame] | 12597 | mng_info=(MngInfo *) AcquireMagickMemory(sizeof(MngInfo)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12598 | if (mng_info == (MngInfo *) NULL) |
| 12599 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
| 12600 | /* |
| 12601 | Initialize members of the MngInfo structure. |
| 12602 | */ |
| 12603 | (void) ResetMagickMemory(mng_info,0,sizeof(MngInfo)); |
| 12604 | mng_info->image=image; |
| 12605 | have_mng_structure=MagickTrue; |
| 12606 | |
| 12607 | (void) WriteBlob(image,8,(const unsigned char *) "\213JNG\r\n\032\n"); |
| 12608 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 12609 | status=WriteOneJNGImage(mng_info,image_info,image,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12610 | (void) CloseBlob(image); |
| 12611 | |
| 12612 | (void) CatchImageException(image); |
| 12613 | MngInfoFreeStruct(mng_info,&have_mng_structure); |
| 12614 | if (logging != MagickFalse) |
| 12615 | (void) LogMagickEvent(CoderEvent,GetMagickModule(),"exit WriteJNGImage()"); |
| 12616 | return(status); |
| 12617 | } |
| 12618 | #endif |
| 12619 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 12620 | static MagickBooleanType WriteMNGImage(const ImageInfo *image_info,Image *image, |
| 12621 | ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12622 | { |
| 12623 | const char |
| 12624 | *option; |
| 12625 | |
| 12626 | Image |
| 12627 | *next_image; |
| 12628 | |
| 12629 | MagickBooleanType |
glennrp | 21f0e62 | 2011-01-07 16:20:57 +0000 | [diff] [blame] | 12630 | have_mng_structure, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12631 | status; |
| 12632 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12633 | volatile MagickBooleanType |
| 12634 | logging; |
| 12635 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12636 | MngInfo |
| 12637 | *mng_info; |
| 12638 | |
| 12639 | int |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12640 | image_count, |
| 12641 | need_iterations, |
| 12642 | need_matte; |
| 12643 | |
| 12644 | volatile int |
| 12645 | #if defined(PNG_WRITE_EMPTY_PLTE_SUPPORTED) || \ |
| 12646 | defined(PNG_MNG_FEATURES_SUPPORTED) |
| 12647 | need_local_plte, |
| 12648 | #endif |
| 12649 | all_images_are_gray, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12650 | need_defi, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12651 | use_global_plte; |
| 12652 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 12653 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12654 | i; |
| 12655 | |
| 12656 | unsigned char |
| 12657 | chunk[800]; |
| 12658 | |
| 12659 | volatile unsigned int |
| 12660 | write_jng, |
| 12661 | write_mng; |
| 12662 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 12663 | volatile size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12664 | scene; |
| 12665 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 12666 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12667 | final_delay=0, |
| 12668 | initial_delay; |
| 12669 | |
glennrp | d5045b4 | 2010-03-24 12:40:35 +0000 | [diff] [blame] | 12670 | #if (PNG_LIBPNG_VER < 10200) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12671 | if (image_info->verbose) |
| 12672 | printf("Your PNG library (libpng-%s) is rather old.\n", |
| 12673 | PNG_LIBPNG_VER_STRING); |
| 12674 | #endif |
| 12675 | |
| 12676 | /* |
| 12677 | Open image file. |
| 12678 | */ |
| 12679 | assert(image_info != (const ImageInfo *) NULL); |
| 12680 | assert(image_info->signature == MagickSignature); |
| 12681 | assert(image != (Image *) NULL); |
| 12682 | assert(image->signature == MagickSignature); |
| 12683 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 12684 | logging=LogMagickEvent(CoderEvent,GetMagickModule(),"Enter WriteMNGImage()"); |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 12685 | status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12686 | if (status == MagickFalse) |
| 12687 | return(status); |
| 12688 | |
| 12689 | /* |
| 12690 | Allocate a MngInfo structure. |
| 12691 | */ |
| 12692 | have_mng_structure=MagickFalse; |
cristy | 73bd4a5 | 2010-10-05 11:24:23 +0000 | [diff] [blame] | 12693 | mng_info=(MngInfo *) AcquireMagickMemory(sizeof(MngInfo)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12694 | if (mng_info == (MngInfo *) NULL) |
| 12695 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
| 12696 | /* |
| 12697 | Initialize members of the MngInfo structure. |
| 12698 | */ |
| 12699 | (void) ResetMagickMemory(mng_info,0,sizeof(MngInfo)); |
| 12700 | mng_info->image=image; |
| 12701 | have_mng_structure=MagickTrue; |
| 12702 | write_mng=LocaleCompare(image_info->magick,"MNG") == 0; |
| 12703 | |
| 12704 | /* |
| 12705 | * See if user has requested a specific PNG subformat to be used |
| 12706 | * for all of the PNGs in the MNG being written, e.g., |
| 12707 | * |
| 12708 | * convert *.png png8:animation.mng |
| 12709 | * |
| 12710 | * To do: check -define png:bit_depth and png:color_type as well, |
| 12711 | * or perhaps use mng:bit_depth and mng:color_type instead for |
| 12712 | * global settings. |
| 12713 | */ |
| 12714 | |
| 12715 | mng_info->write_png8=LocaleCompare(image_info->magick,"PNG8") == 0; |
| 12716 | mng_info->write_png24=LocaleCompare(image_info->magick,"PNG24") == 0; |
| 12717 | mng_info->write_png32=LocaleCompare(image_info->magick,"PNG32") == 0; |
| 12718 | |
| 12719 | write_jng=MagickFalse; |
| 12720 | if (image_info->compression == JPEGCompression) |
| 12721 | write_jng=MagickTrue; |
| 12722 | |
| 12723 | mng_info->adjoin=image_info->adjoin && |
| 12724 | (GetNextImageInList(image) != (Image *) NULL) && write_mng; |
| 12725 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12726 | if (logging != MagickFalse) |
| 12727 | { |
| 12728 | /* Log some info about the input */ |
| 12729 | Image |
| 12730 | *p; |
| 12731 | |
| 12732 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 12733 | " Checking input image(s)"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12734 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12735 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 12736 | " Image_info depth: %.20g",(double) image_info->depth); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12737 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12738 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 12739 | " Type: %d",image_info->type); |
| 12740 | |
| 12741 | scene=0; |
| 12742 | for (p=image; p != (Image *) NULL; p=GetNextImageInList(p)) |
| 12743 | { |
| 12744 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 12745 | " Scene: %.20g",(double) scene++); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12746 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12747 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 12748 | " Image depth: %.20g",(double) p->depth); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12749 | |
cristy | dc2d327 | 2013-02-12 14:00:44 +0000 | [diff] [blame] | 12750 | if (p->alpha_trait == BlendPixelTrait) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12751 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 12752 | " Matte: True"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12753 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12754 | else |
| 12755 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 12756 | " Matte: False"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12757 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12758 | if (p->storage_class == PseudoClass) |
| 12759 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 12760 | " Storage class: PseudoClass"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12761 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12762 | else |
| 12763 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 12764 | " Storage class: DirectClass"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12765 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12766 | if (p->colors) |
| 12767 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 12768 | " Number of colors: %.20g",(double) p->colors); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12769 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12770 | else |
| 12771 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 12772 | " Number of colors: unspecified"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12773 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12774 | if (mng_info->adjoin == MagickFalse) |
| 12775 | break; |
| 12776 | } |
| 12777 | } |
| 12778 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12779 | use_global_plte=MagickFalse; |
| 12780 | all_images_are_gray=MagickFalse; |
| 12781 | #ifdef PNG_WRITE_EMPTY_PLTE_SUPPORTED |
| 12782 | need_local_plte=MagickTrue; |
| 12783 | #endif |
| 12784 | need_defi=MagickFalse; |
| 12785 | need_matte=MagickFalse; |
| 12786 | mng_info->framing_mode=1; |
| 12787 | mng_info->old_framing_mode=1; |
| 12788 | |
| 12789 | if (write_mng) |
| 12790 | if (image_info->page != (char *) NULL) |
| 12791 | { |
| 12792 | /* |
| 12793 | Determine image bounding box. |
| 12794 | */ |
| 12795 | SetGeometry(image,&mng_info->page); |
| 12796 | (void) ParseMetaGeometry(image_info->page,&mng_info->page.x, |
| 12797 | &mng_info->page.y,&mng_info->page.width,&mng_info->page.height); |
| 12798 | } |
| 12799 | if (write_mng) |
| 12800 | { |
| 12801 | unsigned int |
| 12802 | need_geom; |
| 12803 | |
| 12804 | unsigned short |
| 12805 | red, |
| 12806 | green, |
| 12807 | blue; |
| 12808 | |
| 12809 | mng_info->page=image->page; |
| 12810 | need_geom=MagickTrue; |
| 12811 | if (mng_info->page.width || mng_info->page.height) |
| 12812 | need_geom=MagickFalse; |
| 12813 | /* |
| 12814 | Check all the scenes. |
| 12815 | */ |
| 12816 | initial_delay=image->delay; |
| 12817 | need_iterations=MagickFalse; |
| 12818 | mng_info->equal_chrms=image->chromaticity.red_primary.x != 0.0; |
| 12819 | mng_info->equal_physs=MagickTrue, |
| 12820 | mng_info->equal_gammas=MagickTrue; |
| 12821 | mng_info->equal_srgbs=MagickTrue; |
| 12822 | mng_info->equal_backgrounds=MagickTrue; |
| 12823 | image_count=0; |
| 12824 | #if defined(PNG_WRITE_EMPTY_PLTE_SUPPORTED) || \ |
| 12825 | defined(PNG_MNG_FEATURES_SUPPORTED) |
| 12826 | all_images_are_gray=MagickTrue; |
| 12827 | mng_info->equal_palettes=MagickFalse; |
| 12828 | need_local_plte=MagickFalse; |
| 12829 | #endif |
| 12830 | for (next_image=image; next_image != (Image *) NULL; ) |
| 12831 | { |
| 12832 | if (need_geom) |
| 12833 | { |
| 12834 | if ((next_image->columns+next_image->page.x) > mng_info->page.width) |
| 12835 | mng_info->page.width=next_image->columns+next_image->page.x; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12836 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12837 | if ((next_image->rows+next_image->page.y) > mng_info->page.height) |
| 12838 | mng_info->page.height=next_image->rows+next_image->page.y; |
| 12839 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12840 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12841 | if (next_image->page.x || next_image->page.y) |
| 12842 | need_defi=MagickTrue; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12843 | |
cristy | dc2d327 | 2013-02-12 14:00:44 +0000 | [diff] [blame] | 12844 | if (next_image->alpha_trait == BlendPixelTrait) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12845 | need_matte=MagickTrue; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12846 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12847 | if ((int) next_image->dispose >= BackgroundDispose) |
cristy | dc2d327 | 2013-02-12 14:00:44 +0000 | [diff] [blame] | 12848 | if ((next_image->alpha_trait == BlendPixelTrait) || |
| 12849 | next_image->page.x || next_image->page.y || |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12850 | ((next_image->columns < mng_info->page.width) && |
| 12851 | (next_image->rows < mng_info->page.height))) |
| 12852 | mng_info->need_fram=MagickTrue; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12853 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12854 | if (next_image->iterations) |
| 12855 | need_iterations=MagickTrue; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12856 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12857 | final_delay=next_image->delay; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12858 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12859 | if (final_delay != initial_delay || final_delay > 1UL* |
| 12860 | next_image->ticks_per_second) |
| 12861 | mng_info->need_fram=1; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12862 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12863 | #if defined(PNG_WRITE_EMPTY_PLTE_SUPPORTED) || \ |
| 12864 | defined(PNG_MNG_FEATURES_SUPPORTED) |
| 12865 | /* |
| 12866 | check for global palette possibility. |
| 12867 | */ |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 12868 | if (image->alpha_trait == BlendPixelTrait) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12869 | need_local_plte=MagickTrue; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12870 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12871 | if (need_local_plte == 0) |
| 12872 | { |
cristy | 7fb2652 | 2012-06-21 13:02:48 +0000 | [diff] [blame] | 12873 | if (IsImageGray(image,exception) == MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12874 | all_images_are_gray=MagickFalse; |
| 12875 | mng_info->equal_palettes=PalettesAreEqual(image,next_image); |
| 12876 | if (use_global_plte == 0) |
| 12877 | use_global_plte=mng_info->equal_palettes; |
| 12878 | need_local_plte=!mng_info->equal_palettes; |
| 12879 | } |
| 12880 | #endif |
| 12881 | if (GetNextImageInList(next_image) != (Image *) NULL) |
| 12882 | { |
| 12883 | if (next_image->background_color.red != |
| 12884 | next_image->next->background_color.red || |
| 12885 | next_image->background_color.green != |
| 12886 | next_image->next->background_color.green || |
| 12887 | next_image->background_color.blue != |
| 12888 | next_image->next->background_color.blue) |
| 12889 | mng_info->equal_backgrounds=MagickFalse; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12890 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12891 | if (next_image->gamma != next_image->next->gamma) |
| 12892 | mng_info->equal_gammas=MagickFalse; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12893 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12894 | if (next_image->rendering_intent != |
| 12895 | next_image->next->rendering_intent) |
| 12896 | mng_info->equal_srgbs=MagickFalse; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12897 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12898 | if ((next_image->units != next_image->next->units) || |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 12899 | (next_image->resolution.x != next_image->next->resolution.x) || |
| 12900 | (next_image->resolution.y != next_image->next->resolution.y)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12901 | mng_info->equal_physs=MagickFalse; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12902 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12903 | if (mng_info->equal_chrms) |
| 12904 | { |
| 12905 | if (next_image->chromaticity.red_primary.x != |
| 12906 | next_image->next->chromaticity.red_primary.x || |
| 12907 | next_image->chromaticity.red_primary.y != |
| 12908 | next_image->next->chromaticity.red_primary.y || |
| 12909 | next_image->chromaticity.green_primary.x != |
| 12910 | next_image->next->chromaticity.green_primary.x || |
| 12911 | next_image->chromaticity.green_primary.y != |
| 12912 | next_image->next->chromaticity.green_primary.y || |
| 12913 | next_image->chromaticity.blue_primary.x != |
| 12914 | next_image->next->chromaticity.blue_primary.x || |
| 12915 | next_image->chromaticity.blue_primary.y != |
| 12916 | next_image->next->chromaticity.blue_primary.y || |
| 12917 | next_image->chromaticity.white_point.x != |
| 12918 | next_image->next->chromaticity.white_point.x || |
| 12919 | next_image->chromaticity.white_point.y != |
| 12920 | next_image->next->chromaticity.white_point.y) |
| 12921 | mng_info->equal_chrms=MagickFalse; |
| 12922 | } |
| 12923 | } |
| 12924 | image_count++; |
| 12925 | next_image=GetNextImageInList(next_image); |
| 12926 | } |
| 12927 | if (image_count < 2) |
| 12928 | { |
| 12929 | mng_info->equal_backgrounds=MagickFalse; |
| 12930 | mng_info->equal_chrms=MagickFalse; |
| 12931 | mng_info->equal_gammas=MagickFalse; |
| 12932 | mng_info->equal_srgbs=MagickFalse; |
| 12933 | mng_info->equal_physs=MagickFalse; |
| 12934 | use_global_plte=MagickFalse; |
| 12935 | #ifdef PNG_WRITE_EMPTY_PLTE_SUPPORTED |
| 12936 | need_local_plte=MagickTrue; |
| 12937 | #endif |
| 12938 | need_iterations=MagickFalse; |
| 12939 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12940 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12941 | if (mng_info->need_fram == MagickFalse) |
| 12942 | { |
| 12943 | /* |
| 12944 | Only certain framing rates 100/n are exactly representable without |
| 12945 | the FRAM chunk but we'll allow some slop in VLC files |
| 12946 | */ |
| 12947 | if (final_delay == 0) |
| 12948 | { |
| 12949 | if (need_iterations != MagickFalse) |
| 12950 | { |
| 12951 | /* |
| 12952 | It's probably a GIF with loop; don't run it *too* fast. |
| 12953 | */ |
glennrp | 0261712 | 2010-07-28 13:07:35 +0000 | [diff] [blame] | 12954 | if (mng_info->adjoin) |
glennrp | d908de4 | 2010-07-28 13:28:27 +0000 | [diff] [blame] | 12955 | { |
| 12956 | final_delay=10; |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 12957 | (void) ThrowMagickException(exception,GetMagickModule(), |
| 12958 | CoderWarning, |
glennrp | d908de4 | 2010-07-28 13:28:27 +0000 | [diff] [blame] | 12959 | "input has zero delay between all frames; assuming", |
| 12960 | " 10 cs `%s'",""); |
| 12961 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12962 | } |
| 12963 | else |
| 12964 | mng_info->ticks_per_second=0; |
| 12965 | } |
| 12966 | if (final_delay != 0) |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 12967 | mng_info->ticks_per_second=(png_uint_32) |
| 12968 | (image->ticks_per_second/final_delay); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12969 | if (final_delay > 50) |
| 12970 | mng_info->ticks_per_second=2; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12971 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12972 | if (final_delay > 75) |
| 12973 | mng_info->ticks_per_second=1; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12974 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12975 | if (final_delay > 125) |
| 12976 | mng_info->need_fram=MagickTrue; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12977 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12978 | if (need_defi && final_delay > 2 && (final_delay != 4) && |
| 12979 | (final_delay != 5) && (final_delay != 10) && (final_delay != 20) && |
| 12980 | (final_delay != 25) && (final_delay != 50) && (final_delay != |
| 12981 | 1UL*image->ticks_per_second)) |
| 12982 | mng_info->need_fram=MagickTrue; /* make it exact; cannot be VLC */ |
| 12983 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12984 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12985 | if (mng_info->need_fram != MagickFalse) |
| 12986 | mng_info->ticks_per_second=1UL*image->ticks_per_second; |
| 12987 | /* |
| 12988 | If pseudocolor, we should also check to see if all the |
| 12989 | palettes are identical and write a global PLTE if they are. |
| 12990 | ../glennrp Feb 99. |
| 12991 | */ |
| 12992 | /* |
| 12993 | Write the MNG version 1.0 signature and MHDR chunk. |
| 12994 | */ |
| 12995 | (void) WriteBlob(image,8,(const unsigned char *) "\212MNG\r\n\032\n"); |
| 12996 | (void) WriteBlobMSBULong(image,28L); /* chunk data length=28 */ |
| 12997 | PNGType(chunk,mng_MHDR); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12998 | LogPNGChunk(logging,mng_MHDR,28L); |
cristy | 4e5bc84 | 2010-06-09 13:56:01 +0000 | [diff] [blame] | 12999 | PNGLong(chunk+4,(png_uint_32) mng_info->page.width); |
| 13000 | PNGLong(chunk+8,(png_uint_32) mng_info->page.height); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13001 | PNGLong(chunk+12,mng_info->ticks_per_second); |
| 13002 | PNGLong(chunk+16,0L); /* layer count=unknown */ |
| 13003 | PNGLong(chunk+20,0L); /* frame count=unknown */ |
| 13004 | PNGLong(chunk+24,0L); /* play time=unknown */ |
| 13005 | if (write_jng) |
| 13006 | { |
| 13007 | if (need_matte) |
| 13008 | { |
| 13009 | if (need_defi || mng_info->need_fram || use_global_plte) |
| 13010 | PNGLong(chunk+28,27L); /* simplicity=LC+JNG */ |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13011 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13012 | else |
| 13013 | PNGLong(chunk+28,25L); /* simplicity=VLC+JNG */ |
| 13014 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13015 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13016 | else |
| 13017 | { |
| 13018 | if (need_defi || mng_info->need_fram || use_global_plte) |
| 13019 | PNGLong(chunk+28,19L); /* simplicity=LC+JNG, no transparency */ |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13020 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13021 | else |
| 13022 | PNGLong(chunk+28,17L); /* simplicity=VLC+JNG, no transparency */ |
| 13023 | } |
| 13024 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13025 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13026 | else |
| 13027 | { |
| 13028 | if (need_matte) |
| 13029 | { |
| 13030 | if (need_defi || mng_info->need_fram || use_global_plte) |
| 13031 | PNGLong(chunk+28,11L); /* simplicity=LC */ |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13032 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13033 | else |
| 13034 | PNGLong(chunk+28,9L); /* simplicity=VLC */ |
| 13035 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13036 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13037 | else |
| 13038 | { |
| 13039 | if (need_defi || mng_info->need_fram || use_global_plte) |
| 13040 | PNGLong(chunk+28,3L); /* simplicity=LC, no transparency */ |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13041 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13042 | else |
| 13043 | PNGLong(chunk+28,1L); /* simplicity=VLC, no transparency */ |
| 13044 | } |
| 13045 | } |
| 13046 | (void) WriteBlob(image,32,chunk); |
| 13047 | (void) WriteBlobMSBULong(image,crc32(0,chunk,32)); |
| 13048 | option=GetImageOption(image_info,"mng:need-cacheoff"); |
| 13049 | if (option != (const char *) NULL) |
| 13050 | { |
| 13051 | size_t |
| 13052 | length; |
| 13053 | |
| 13054 | /* |
| 13055 | Write "nEED CACHEOFF" to turn playback caching off for streaming MNG. |
| 13056 | */ |
| 13057 | PNGType(chunk,mng_nEED); |
| 13058 | length=CopyMagickString((char *) chunk+4,"CACHEOFF",20); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 13059 | (void) WriteBlobMSBULong(image,(size_t) length); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 13060 | LogPNGChunk(logging,mng_nEED,(size_t) length); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13061 | length+=4; |
| 13062 | (void) WriteBlob(image,length,chunk); |
| 13063 | (void) WriteBlobMSBULong(image,crc32(0,chunk,(uInt) length)); |
| 13064 | } |
| 13065 | if ((GetPreviousImageInList(image) == (Image *) NULL) && |
| 13066 | (GetNextImageInList(image) != (Image *) NULL) && |
| 13067 | (image->iterations != 1)) |
| 13068 | { |
| 13069 | /* |
| 13070 | Write MNG TERM chunk |
| 13071 | */ |
| 13072 | (void) WriteBlobMSBULong(image,10L); /* data length=10 */ |
| 13073 | PNGType(chunk,mng_TERM); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 13074 | LogPNGChunk(logging,mng_TERM,10L); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13075 | chunk[4]=3; /* repeat animation */ |
| 13076 | chunk[5]=0; /* show last frame when done */ |
| 13077 | PNGLong(chunk+6,(png_uint_32) (mng_info->ticks_per_second* |
| 13078 | final_delay/MagickMax(image->ticks_per_second,1))); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13079 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13080 | if (image->iterations == 0) |
| 13081 | PNGLong(chunk+10,PNG_UINT_31_MAX); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13082 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13083 | else |
| 13084 | PNGLong(chunk+10,(png_uint_32) image->iterations); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13085 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13086 | if (logging != MagickFalse) |
| 13087 | { |
| 13088 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 13089 | " TERM delay: %.20g",(double) (mng_info->ticks_per_second* |
| 13090 | final_delay/MagickMax(image->ticks_per_second,1))); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13091 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13092 | if (image->iterations == 0) |
| 13093 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 13094 | " TERM iterations: %.20g",(double) PNG_UINT_31_MAX); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13095 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13096 | else |
| 13097 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 13098 | " Image iterations: %.20g",(double) image->iterations); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13099 | } |
| 13100 | (void) WriteBlob(image,14,chunk); |
| 13101 | (void) WriteBlobMSBULong(image,crc32(0,chunk,14)); |
| 13102 | } |
| 13103 | /* |
| 13104 | To do: check for cHRM+gAMA == sRGB, and write sRGB instead. |
| 13105 | */ |
| 13106 | if ((image->colorspace == sRGBColorspace || image->rendering_intent) && |
| 13107 | mng_info->equal_srgbs) |
| 13108 | { |
| 13109 | /* |
| 13110 | Write MNG sRGB chunk |
| 13111 | */ |
| 13112 | (void) WriteBlobMSBULong(image,1L); |
| 13113 | PNGType(chunk,mng_sRGB); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 13114 | LogPNGChunk(logging,mng_sRGB,1L); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13115 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13116 | if (image->rendering_intent != UndefinedIntent) |
glennrp | e610a07 | 2010-08-05 17:08:46 +0000 | [diff] [blame] | 13117 | chunk[4]=(unsigned char) |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 13118 | Magick_RenderingIntent_to_PNG_RenderingIntent( |
glennrp | e610a07 | 2010-08-05 17:08:46 +0000 | [diff] [blame] | 13119 | (image->rendering_intent)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13120 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13121 | else |
glennrp | e610a07 | 2010-08-05 17:08:46 +0000 | [diff] [blame] | 13122 | chunk[4]=(unsigned char) |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 13123 | Magick_RenderingIntent_to_PNG_RenderingIntent( |
| 13124 | (PerceptualIntent)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13125 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13126 | (void) WriteBlob(image,5,chunk); |
| 13127 | (void) WriteBlobMSBULong(image,crc32(0,chunk,5)); |
| 13128 | mng_info->have_write_global_srgb=MagickTrue; |
| 13129 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13130 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13131 | else |
| 13132 | { |
| 13133 | if (image->gamma && mng_info->equal_gammas) |
| 13134 | { |
| 13135 | /* |
| 13136 | Write MNG gAMA chunk |
| 13137 | */ |
| 13138 | (void) WriteBlobMSBULong(image,4L); |
| 13139 | PNGType(chunk,mng_gAMA); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 13140 | LogPNGChunk(logging,mng_gAMA,4L); |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 13141 | PNGLong(chunk+4,(png_uint_32) (100000*image->gamma+0.5)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13142 | (void) WriteBlob(image,8,chunk); |
| 13143 | (void) WriteBlobMSBULong(image,crc32(0,chunk,8)); |
| 13144 | mng_info->have_write_global_gama=MagickTrue; |
| 13145 | } |
| 13146 | if (mng_info->equal_chrms) |
| 13147 | { |
| 13148 | PrimaryInfo |
| 13149 | primary; |
| 13150 | |
| 13151 | /* |
| 13152 | Write MNG cHRM chunk |
| 13153 | */ |
| 13154 | (void) WriteBlobMSBULong(image,32L); |
| 13155 | PNGType(chunk,mng_cHRM); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 13156 | LogPNGChunk(logging,mng_cHRM,32L); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13157 | primary=image->chromaticity.white_point; |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 13158 | PNGLong(chunk+4,(png_uint_32) (100000*primary.x+0.5)); |
| 13159 | PNGLong(chunk+8,(png_uint_32) (100000*primary.y+0.5)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13160 | primary=image->chromaticity.red_primary; |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 13161 | PNGLong(chunk+12,(png_uint_32) (100000*primary.x+0.5)); |
| 13162 | PNGLong(chunk+16,(png_uint_32) (100000*primary.y+0.5)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13163 | primary=image->chromaticity.green_primary; |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 13164 | PNGLong(chunk+20,(png_uint_32) (100000*primary.x+0.5)); |
| 13165 | PNGLong(chunk+24,(png_uint_32) (100000*primary.y+0.5)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13166 | primary=image->chromaticity.blue_primary; |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 13167 | PNGLong(chunk+28,(png_uint_32) (100000*primary.x+0.5)); |
| 13168 | PNGLong(chunk+32,(png_uint_32) (100000*primary.y+0.5)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13169 | (void) WriteBlob(image,36,chunk); |
| 13170 | (void) WriteBlobMSBULong(image,crc32(0,chunk,36)); |
| 13171 | mng_info->have_write_global_chrm=MagickTrue; |
| 13172 | } |
| 13173 | } |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 13174 | if (image->resolution.x && image->resolution.y && mng_info->equal_physs) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13175 | { |
| 13176 | /* |
| 13177 | Write MNG pHYs chunk |
| 13178 | */ |
| 13179 | (void) WriteBlobMSBULong(image,9L); |
| 13180 | PNGType(chunk,mng_pHYs); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 13181 | LogPNGChunk(logging,mng_pHYs,9L); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13182 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13183 | if (image->units == PixelsPerInchResolution) |
| 13184 | { |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 13185 | PNGLong(chunk+4,(png_uint_32) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 13186 | (image->resolution.x*100.0/2.54+0.5)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13187 | |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 13188 | PNGLong(chunk+8,(png_uint_32) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 13189 | (image->resolution.y*100.0/2.54+0.5)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13190 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13191 | chunk[12]=1; |
| 13192 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13193 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13194 | else |
| 13195 | { |
| 13196 | if (image->units == PixelsPerCentimeterResolution) |
| 13197 | { |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 13198 | PNGLong(chunk+4,(png_uint_32) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 13199 | (image->resolution.x*100.0+0.5)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13200 | |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 13201 | PNGLong(chunk+8,(png_uint_32) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 13202 | (image->resolution.y*100.0+0.5)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13203 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13204 | chunk[12]=1; |
| 13205 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13206 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13207 | else |
| 13208 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 13209 | PNGLong(chunk+4,(png_uint_32) (image->resolution.x+0.5)); |
| 13210 | PNGLong(chunk+8,(png_uint_32) (image->resolution.y+0.5)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13211 | chunk[12]=0; |
| 13212 | } |
| 13213 | } |
| 13214 | (void) WriteBlob(image,13,chunk); |
| 13215 | (void) WriteBlobMSBULong(image,crc32(0,chunk,13)); |
| 13216 | } |
| 13217 | /* |
| 13218 | Write MNG BACK chunk and global bKGD chunk, if the image is transparent |
| 13219 | or does not cover the entire frame. |
| 13220 | */ |
cristy | dc2d327 | 2013-02-12 14:00:44 +0000 | [diff] [blame] | 13221 | if (write_mng && ((image->alpha_trait == BlendPixelTrait) || |
| 13222 | image->page.x > 0 || image->page.y > 0 || (image->page.width && |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13223 | (image->page.width+image->page.x < mng_info->page.width)) |
| 13224 | || (image->page.height && (image->page.height+image->page.y |
| 13225 | < mng_info->page.height)))) |
| 13226 | { |
| 13227 | (void) WriteBlobMSBULong(image,6L); |
| 13228 | PNGType(chunk,mng_BACK); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 13229 | LogPNGChunk(logging,mng_BACK,6L); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13230 | red=ScaleQuantumToShort(image->background_color.red); |
| 13231 | green=ScaleQuantumToShort(image->background_color.green); |
| 13232 | blue=ScaleQuantumToShort(image->background_color.blue); |
| 13233 | PNGShort(chunk+4,red); |
| 13234 | PNGShort(chunk+6,green); |
| 13235 | PNGShort(chunk+8,blue); |
| 13236 | (void) WriteBlob(image,10,chunk); |
| 13237 | (void) WriteBlobMSBULong(image,crc32(0,chunk,10)); |
| 13238 | if (mng_info->equal_backgrounds) |
| 13239 | { |
| 13240 | (void) WriteBlobMSBULong(image,6L); |
| 13241 | PNGType(chunk,mng_bKGD); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 13242 | LogPNGChunk(logging,mng_bKGD,6L); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13243 | (void) WriteBlob(image,10,chunk); |
| 13244 | (void) WriteBlobMSBULong(image,crc32(0,chunk,10)); |
| 13245 | } |
| 13246 | } |
| 13247 | |
| 13248 | #ifdef PNG_WRITE_EMPTY_PLTE_SUPPORTED |
| 13249 | if ((need_local_plte == MagickFalse) && |
| 13250 | (image->storage_class == PseudoClass) && |
| 13251 | (all_images_are_gray == MagickFalse)) |
| 13252 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 13253 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13254 | data_length; |
| 13255 | |
| 13256 | /* |
| 13257 | Write MNG PLTE chunk |
| 13258 | */ |
| 13259 | data_length=3*image->colors; |
| 13260 | (void) WriteBlobMSBULong(image,data_length); |
| 13261 | PNGType(chunk,mng_PLTE); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 13262 | LogPNGChunk(logging,mng_PLTE,data_length); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13263 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 13264 | for (i=0; i < (ssize_t) image->colors; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13265 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 13266 | chunk[4+i*3]=(unsigned char) (ScaleQuantumToChar( |
| 13267 | image->colormap[i].red) & 0xff); |
| 13268 | chunk[5+i*3]=(unsigned char) (ScaleQuantumToChar( |
| 13269 | image->colormap[i].green) & 0xff); |
| 13270 | chunk[6+i*3]=(unsigned char) (ScaleQuantumToChar( |
| 13271 | image->colormap[i].blue) & 0xff); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13272 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13273 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13274 | (void) WriteBlob(image,data_length+4,chunk); |
| 13275 | (void) WriteBlobMSBULong(image,crc32(0,chunk,(uInt) (data_length+4))); |
| 13276 | mng_info->have_write_global_plte=MagickTrue; |
| 13277 | } |
| 13278 | #endif |
| 13279 | } |
| 13280 | scene=0; |
| 13281 | mng_info->delay=0; |
| 13282 | #if defined(PNG_WRITE_EMPTY_PLTE_SUPPORTED) || \ |
| 13283 | defined(PNG_MNG_FEATURES_SUPPORTED) |
| 13284 | mng_info->equal_palettes=MagickFalse; |
| 13285 | #endif |
| 13286 | do |
| 13287 | { |
| 13288 | if (mng_info->adjoin) |
| 13289 | { |
| 13290 | #if defined(PNG_WRITE_EMPTY_PLTE_SUPPORTED) || \ |
| 13291 | defined(PNG_MNG_FEATURES_SUPPORTED) |
| 13292 | /* |
| 13293 | If we aren't using a global palette for the entire MNG, check to |
| 13294 | see if we can use one for two or more consecutive images. |
| 13295 | */ |
| 13296 | if (need_local_plte && use_global_plte && !all_images_are_gray) |
| 13297 | { |
| 13298 | if (mng_info->IsPalette) |
| 13299 | { |
| 13300 | /* |
| 13301 | When equal_palettes is true, this image has the same palette |
| 13302 | as the previous PseudoClass image |
| 13303 | */ |
| 13304 | mng_info->have_write_global_plte=mng_info->equal_palettes; |
| 13305 | mng_info->equal_palettes=PalettesAreEqual(image,image->next); |
| 13306 | if (mng_info->equal_palettes && !mng_info->have_write_global_plte) |
| 13307 | { |
| 13308 | /* |
| 13309 | Write MNG PLTE chunk |
| 13310 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 13311 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13312 | data_length; |
| 13313 | |
| 13314 | data_length=3*image->colors; |
| 13315 | (void) WriteBlobMSBULong(image,data_length); |
| 13316 | PNGType(chunk,mng_PLTE); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 13317 | LogPNGChunk(logging,mng_PLTE,data_length); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13318 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 13319 | for (i=0; i < (ssize_t) image->colors; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13320 | { |
| 13321 | chunk[4+i*3]=ScaleQuantumToChar(image->colormap[i].red); |
| 13322 | chunk[5+i*3]=ScaleQuantumToChar(image->colormap[i].green); |
| 13323 | chunk[6+i*3]=ScaleQuantumToChar(image->colormap[i].blue); |
| 13324 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13325 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13326 | (void) WriteBlob(image,data_length+4,chunk); |
| 13327 | (void) WriteBlobMSBULong(image,crc32(0,chunk, |
| 13328 | (uInt) (data_length+4))); |
| 13329 | mng_info->have_write_global_plte=MagickTrue; |
| 13330 | } |
| 13331 | } |
| 13332 | else |
| 13333 | mng_info->have_write_global_plte=MagickFalse; |
| 13334 | } |
| 13335 | #endif |
| 13336 | if (need_defi) |
| 13337 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 13338 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13339 | previous_x, |
| 13340 | previous_y; |
| 13341 | |
| 13342 | if (scene) |
| 13343 | { |
| 13344 | previous_x=mng_info->page.x; |
| 13345 | previous_y=mng_info->page.y; |
| 13346 | } |
| 13347 | else |
| 13348 | { |
| 13349 | previous_x=0; |
| 13350 | previous_y=0; |
| 13351 | } |
| 13352 | mng_info->page=image->page; |
| 13353 | if ((mng_info->page.x != previous_x) || |
| 13354 | (mng_info->page.y != previous_y)) |
| 13355 | { |
| 13356 | (void) WriteBlobMSBULong(image,12L); /* data length=12 */ |
| 13357 | PNGType(chunk,mng_DEFI); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 13358 | LogPNGChunk(logging,mng_DEFI,12L); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13359 | chunk[4]=0; /* object 0 MSB */ |
| 13360 | chunk[5]=0; /* object 0 LSB */ |
| 13361 | chunk[6]=0; /* visible */ |
| 13362 | chunk[7]=0; /* abstract */ |
| 13363 | PNGLong(chunk+8,(png_uint_32) mng_info->page.x); |
| 13364 | PNGLong(chunk+12,(png_uint_32) mng_info->page.y); |
| 13365 | (void) WriteBlob(image,16,chunk); |
| 13366 | (void) WriteBlobMSBULong(image,crc32(0,chunk,16)); |
| 13367 | } |
| 13368 | } |
| 13369 | } |
| 13370 | |
| 13371 | mng_info->write_mng=write_mng; |
| 13372 | |
| 13373 | if ((int) image->dispose >= 3) |
| 13374 | mng_info->framing_mode=3; |
| 13375 | |
| 13376 | if (mng_info->need_fram && mng_info->adjoin && |
| 13377 | ((image->delay != mng_info->delay) || |
| 13378 | (mng_info->framing_mode != mng_info->old_framing_mode))) |
| 13379 | { |
| 13380 | if (image->delay == mng_info->delay) |
| 13381 | { |
| 13382 | /* |
| 13383 | Write a MNG FRAM chunk with the new framing mode. |
| 13384 | */ |
| 13385 | (void) WriteBlobMSBULong(image,1L); /* data length=1 */ |
| 13386 | PNGType(chunk,mng_FRAM); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 13387 | LogPNGChunk(logging,mng_FRAM,1L); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13388 | chunk[4]=(unsigned char) mng_info->framing_mode; |
| 13389 | (void) WriteBlob(image,5,chunk); |
| 13390 | (void) WriteBlobMSBULong(image,crc32(0,chunk,5)); |
| 13391 | } |
| 13392 | else |
| 13393 | { |
| 13394 | /* |
| 13395 | Write a MNG FRAM chunk with the delay. |
| 13396 | */ |
| 13397 | (void) WriteBlobMSBULong(image,10L); /* data length=10 */ |
| 13398 | PNGType(chunk,mng_FRAM); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 13399 | LogPNGChunk(logging,mng_FRAM,10L); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13400 | chunk[4]=(unsigned char) mng_info->framing_mode; |
| 13401 | chunk[5]=0; /* frame name separator (no name) */ |
| 13402 | chunk[6]=2; /* flag for changing default delay */ |
| 13403 | chunk[7]=0; /* flag for changing frame timeout */ |
| 13404 | chunk[8]=0; /* flag for changing frame clipping */ |
| 13405 | chunk[9]=0; /* flag for changing frame sync_id */ |
| 13406 | PNGLong(chunk+10,(png_uint_32) |
| 13407 | ((mng_info->ticks_per_second* |
| 13408 | image->delay)/MagickMax(image->ticks_per_second,1))); |
| 13409 | (void) WriteBlob(image,14,chunk); |
| 13410 | (void) WriteBlobMSBULong(image,crc32(0,chunk,14)); |
cristy | 4e5bc84 | 2010-06-09 13:56:01 +0000 | [diff] [blame] | 13411 | mng_info->delay=(png_uint_32) image->delay; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13412 | } |
| 13413 | mng_info->old_framing_mode=mng_info->framing_mode; |
| 13414 | } |
| 13415 | |
| 13416 | #if defined(JNG_SUPPORTED) |
| 13417 | if (image_info->compression == JPEGCompression) |
| 13418 | { |
| 13419 | ImageInfo |
| 13420 | *write_info; |
| 13421 | |
| 13422 | if (logging != MagickFalse) |
| 13423 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 13424 | " Writing JNG object."); |
| 13425 | /* To do: specify the desired alpha compression method. */ |
| 13426 | write_info=CloneImageInfo(image_info); |
| 13427 | write_info->compression=UndefinedCompression; |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 13428 | status=WriteOneJNGImage(mng_info,write_info,image,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13429 | write_info=DestroyImageInfo(write_info); |
| 13430 | } |
| 13431 | else |
| 13432 | #endif |
| 13433 | { |
| 13434 | if (logging != MagickFalse) |
| 13435 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 13436 | " Writing PNG object."); |
glennrp | 2f2e514 | 2010-12-23 19:13:35 +0000 | [diff] [blame] | 13437 | |
glennrp | b9cfe27 | 2010-12-21 15:08:06 +0000 | [diff] [blame] | 13438 | mng_info->need_blob = MagickFalse; |
glennrp | 8d3d6e5 | 2011-04-19 04:39:51 +0000 | [diff] [blame] | 13439 | mng_info->ping_preserve_colormap = MagickFalse; |
glennrp | 2f2e514 | 2010-12-23 19:13:35 +0000 | [diff] [blame] | 13440 | |
| 13441 | /* We don't want any ancillary chunks written */ |
| 13442 | mng_info->ping_exclude_bKGD=MagickTrue; |
| 13443 | mng_info->ping_exclude_cHRM=MagickTrue; |
glennrp | a0ed009 | 2011-04-18 16:36:29 +0000 | [diff] [blame] | 13444 | mng_info->ping_exclude_date=MagickTrue; |
glennrp | 2f2e514 | 2010-12-23 19:13:35 +0000 | [diff] [blame] | 13445 | mng_info->ping_exclude_EXIF=MagickTrue; |
| 13446 | mng_info->ping_exclude_gAMA=MagickTrue; |
glennrp | 2f2e514 | 2010-12-23 19:13:35 +0000 | [diff] [blame] | 13447 | mng_info->ping_exclude_iCCP=MagickTrue; |
| 13448 | /* mng_info->ping_exclude_iTXt=MagickTrue; */ |
| 13449 | mng_info->ping_exclude_oFFs=MagickTrue; |
| 13450 | mng_info->ping_exclude_pHYs=MagickTrue; |
| 13451 | mng_info->ping_exclude_sRGB=MagickTrue; |
| 13452 | mng_info->ping_exclude_tEXt=MagickTrue; |
glennrp | a1e3b7b | 2010-12-24 16:37:33 +0000 | [diff] [blame] | 13453 | mng_info->ping_exclude_tRNS=MagickTrue; |
glennrp | 2f2e514 | 2010-12-23 19:13:35 +0000 | [diff] [blame] | 13454 | mng_info->ping_exclude_vpAg=MagickTrue; |
| 13455 | mng_info->ping_exclude_zCCP=MagickTrue; |
| 13456 | mng_info->ping_exclude_zTXt=MagickTrue; |
| 13457 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 13458 | status=WriteOnePNGImage(mng_info,image_info,image,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13459 | } |
| 13460 | |
| 13461 | if (status == MagickFalse) |
| 13462 | { |
| 13463 | MngInfoFreeStruct(mng_info,&have_mng_structure); |
| 13464 | (void) CloseBlob(image); |
| 13465 | return(MagickFalse); |
| 13466 | } |
| 13467 | (void) CatchImageException(image); |
| 13468 | if (GetNextImageInList(image) == (Image *) NULL) |
| 13469 | break; |
| 13470 | image=SyncNextImageInList(image); |
| 13471 | status=SetImageProgress(image,SaveImagesTag,scene++, |
| 13472 | GetImageListLength(image)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13473 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13474 | if (status == MagickFalse) |
| 13475 | break; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13476 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13477 | } while (mng_info->adjoin); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13478 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13479 | if (write_mng) |
| 13480 | { |
| 13481 | while (GetPreviousImageInList(image) != (Image *) NULL) |
| 13482 | image=GetPreviousImageInList(image); |
| 13483 | /* |
| 13484 | Write the MEND chunk. |
| 13485 | */ |
| 13486 | (void) WriteBlobMSBULong(image,0x00000000L); |
| 13487 | PNGType(chunk,mng_MEND); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 13488 | LogPNGChunk(logging,mng_MEND,0L); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13489 | (void) WriteBlob(image,4,chunk); |
| 13490 | (void) WriteBlobMSBULong(image,crc32(0,chunk,4)); |
| 13491 | } |
| 13492 | /* |
| 13493 | Relinquish resources. |
| 13494 | */ |
| 13495 | (void) CloseBlob(image); |
| 13496 | MngInfoFreeStruct(mng_info,&have_mng_structure); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13497 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13498 | if (logging != MagickFalse) |
| 13499 | (void) LogMagickEvent(CoderEvent,GetMagickModule(),"exit WriteMNGImage()"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13500 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13501 | return(MagickTrue); |
| 13502 | } |
glennrp | d5045b4 | 2010-03-24 12:40:35 +0000 | [diff] [blame] | 13503 | #else /* PNG_LIBPNG_VER > 10011 */ |
glennrp | 39992b4 | 2010-11-14 00:03:43 +0000 | [diff] [blame] | 13504 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13505 | static MagickBooleanType WritePNGImage(const ImageInfo *image_info,Image *image) |
| 13506 | { |
glennrp | 3bd393f | 2011-12-21 18:54:53 +0000 | [diff] [blame] | 13507 | (void) image; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13508 | printf("Your PNG library is too old: You have libpng-%s\n", |
| 13509 | PNG_LIBPNG_VER_STRING); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13510 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13511 | ThrowBinaryException(CoderError,"PNG library is too old", |
| 13512 | image_info->filename); |
| 13513 | } |
glennrp | 39992b4 | 2010-11-14 00:03:43 +0000 | [diff] [blame] | 13514 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13515 | static MagickBooleanType WriteMNGImage(const ImageInfo *image_info,Image *image) |
| 13516 | { |
| 13517 | return(WritePNGImage(image_info,image)); |
| 13518 | } |
glennrp | d5045b4 | 2010-03-24 12:40:35 +0000 | [diff] [blame] | 13519 | #endif /* PNG_LIBPNG_VER > 10011 */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13520 | #endif |