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 | 1454be7 | 2011-12-19 01:52:48 +0000 | [diff] [blame] | 21 | % Copyright 1999-2012 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) \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 170 | (ScaleQuantumToChar(GetPixelRed(image,(pixel))) < 0x10 ? \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 171 | 0 : QuantumRange); |
| 172 | |
glennrp | 54cf797 | 2011-08-06 14:28:09 +0000 | [diff] [blame] | 173 | #define LBR01PixelGreen(pixel) \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 174 | (ScaleQuantumToChar(GetPixelGreen(image,(pixel))) < 0x10 ? \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 175 | 0 : QuantumRange); |
| 176 | |
glennrp | 54cf797 | 2011-08-06 14:28:09 +0000 | [diff] [blame] | 177 | #define LBR01PixelBlue(pixel) \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 178 | (ScaleQuantumToChar(GetPixelBlue(image,(pixel))) < 0x10 ? \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 179 | 0 : QuantumRange); |
| 180 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 181 | #define LBR01PixelAlpha(pixel) \ |
| 182 | (ScaleQuantumToChar(GetPixelAlpha(image,(pixel))) < 0x10 ? \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 183 | 0 : QuantumRange); |
| 184 | |
glennrp | 54cf797 | 2011-08-06 14:28:09 +0000 | [diff] [blame] | 185 | #define LBR01PixelRGB(pixel) \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 186 | { \ |
cristy | ef61831 | 2011-06-25 12:26:44 +0000 | [diff] [blame] | 187 | LBR01PixelRed((pixel)); \ |
glennrp | 54cf797 | 2011-08-06 14:28:09 +0000 | [diff] [blame] | 188 | LBR01PixelGreen((pixel)); \ |
| 189 | LBR01PixelBlue((pixel)); \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 190 | } |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 191 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 192 | #define LBR01PixelRGBA(pixel) \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 193 | { \ |
glennrp | 54cf797 | 2011-08-06 14:28:09 +0000 | [diff] [blame] | 194 | LBR01PixelRGB((pixel)); \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 195 | LBR01PixelAlpha((pixel)); \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 196 | } |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 197 | |
| 198 | /* LBR02: Replicate top 2 bits */ |
| 199 | |
glennrp | 05001c3 | 2011-08-06 13:04:16 +0000 | [diff] [blame] | 200 | #define LBR02PacketRed(pixelpacket) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 201 | { \ |
| 202 | unsigned char lbr_bits=ScaleQuantumToChar((pixelpacket).red) & 0xc0; \ |
| 203 | (pixelpacket).red=ScaleCharToQuantum( \ |
| 204 | (lbr_bits | (lbr_bits >> 2) | (lbr_bits >> 4) | (lbr_bits >> 6))); \ |
| 205 | } |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 206 | #define LBR02PacketGreen(pixelpacket) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 207 | { \ |
| 208 | unsigned char lbr_bits=ScaleQuantumToChar((pixelpacket).green) & 0xc0; \ |
| 209 | (pixelpacket).green=ScaleCharToQuantum( \ |
| 210 | (lbr_bits | (lbr_bits >> 2) | (lbr_bits >> 4) | (lbr_bits >> 6))); \ |
| 211 | } |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 212 | #define LBR02PacketBlue(pixelpacket) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 213 | { \ |
| 214 | unsigned char lbr_bits=ScaleQuantumToChar((pixelpacket).blue) & 0xc0; \ |
| 215 | (pixelpacket).blue=ScaleCharToQuantum( \ |
| 216 | (lbr_bits | (lbr_bits >> 2) | (lbr_bits >> 4) | (lbr_bits >> 6))); \ |
| 217 | } |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 218 | #define LBR02PacketAlpha(pixelpacket) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 219 | { \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 220 | unsigned char lbr_bits=ScaleQuantumToChar((pixelpacket).alpha) & 0xc0; \ |
| 221 | (pixelpacket).alpha=ScaleCharToQuantum( \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 222 | (lbr_bits | (lbr_bits >> 2) | (lbr_bits >> 4) | (lbr_bits >> 6))); \ |
| 223 | } |
| 224 | |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 225 | #define LBR02PacketRGB(pixelpacket) \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 226 | { \ |
glennrp | 05001c3 | 2011-08-06 13:04:16 +0000 | [diff] [blame] | 227 | LBR02PacketRed((pixelpacket)); \ |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 228 | LBR02PacketGreen((pixelpacket)); \ |
| 229 | LBR02PacketBlue((pixelpacket)); \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 230 | } |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 231 | |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 232 | #define LBR02PacketRGBO(pixelpacket) \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 233 | { \ |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 234 | LBR02PacketRGB((pixelpacket)); \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 235 | LBR02PacketAlpha((pixelpacket)); \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 236 | } |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 237 | |
cristy | ef61831 | 2011-06-25 12:26:44 +0000 | [diff] [blame] | 238 | #define LBR02PixelRed(pixel) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 239 | { \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 240 | unsigned char lbr_bits=ScaleQuantumToChar(GetPixelRed(image,(pixel))) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 241 | & 0xc0; \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 242 | SetPixelRed(image, ScaleCharToQuantum( \ |
| 243 | (lbr_bits | (lbr_bits >> 2) | (lbr_bits >> 4) | (lbr_bits >> 6))), \ |
| 244 | (pixel)); \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 245 | } |
glennrp | 54cf797 | 2011-08-06 14:28:09 +0000 | [diff] [blame] | 246 | #define LBR02PixelGreen(pixel) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 247 | { \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 248 | unsigned char lbr_bits=ScaleQuantumToChar(GetPixelGreen(image,(pixel)))\ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 249 | & 0xc0; \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 250 | SetPixelGreen(image, ScaleCharToQuantum( \ |
| 251 | (lbr_bits | (lbr_bits >> 2) | (lbr_bits >> 4) | (lbr_bits >> 6))), \ |
| 252 | (pixel)); \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 253 | } |
glennrp | 54cf797 | 2011-08-06 14:28:09 +0000 | [diff] [blame] | 254 | #define LBR02PixelBlue(pixel) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 255 | { \ |
| 256 | unsigned char lbr_bits= \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 257 | ScaleQuantumToChar(GetPixelBlue(image,(pixel))) & 0xc0; \ |
| 258 | SetPixelBlue(image, ScaleCharToQuantum( \ |
| 259 | (lbr_bits | (lbr_bits >> 2) | (lbr_bits >> 4) | (lbr_bits >> 6))), \ |
| 260 | (pixel)); \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 261 | } |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 262 | #define LBR02PixelAlpha(pixel) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 263 | { \ |
| 264 | unsigned char lbr_bits= \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 265 | ScaleQuantumToChar(GetPixelAlpha(image,(pixel))) & 0xc0; \ |
| 266 | SetPixelAlpha(image, ScaleCharToQuantum( \ |
| 267 | (lbr_bits | (lbr_bits >> 2) | (lbr_bits >> 4) | (lbr_bits >> 6))), \ |
| 268 | (pixel) ); \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 269 | } |
| 270 | |
glennrp | 54cf797 | 2011-08-06 14:28:09 +0000 | [diff] [blame] | 271 | #define LBR02PixelRGB(pixel) \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 272 | { \ |
cristy | ef61831 | 2011-06-25 12:26:44 +0000 | [diff] [blame] | 273 | LBR02PixelRed((pixel)); \ |
glennrp | 54cf797 | 2011-08-06 14:28:09 +0000 | [diff] [blame] | 274 | LBR02PixelGreen((pixel)); \ |
| 275 | LBR02PixelBlue((pixel)); \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 276 | } |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 277 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 278 | #define LBR02PixelRGBA(pixel) \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 279 | { \ |
glennrp | 54cf797 | 2011-08-06 14:28:09 +0000 | [diff] [blame] | 280 | LBR02PixelRGB((pixel)); \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 281 | LBR02PixelAlpha((pixel)); \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 282 | } |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 283 | |
| 284 | /* LBR03: Replicate top 3 bits (only used with opaque pixels during |
| 285 | PNG8 quantization) */ |
| 286 | |
glennrp | 05001c3 | 2011-08-06 13:04:16 +0000 | [diff] [blame] | 287 | #define LBR03PacketRed(pixelpacket) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 288 | { \ |
| 289 | unsigned char lbr_bits=ScaleQuantumToChar((pixelpacket).red) & 0xe0; \ |
| 290 | (pixelpacket).red=ScaleCharToQuantum( \ |
| 291 | (lbr_bits | (lbr_bits >> 3) | (lbr_bits >> 6))); \ |
| 292 | } |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 293 | #define LBR03PacketGreen(pixelpacket) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 294 | { \ |
| 295 | unsigned char lbr_bits=ScaleQuantumToChar((pixelpacket).green) & 0xe0; \ |
| 296 | (pixelpacket).green=ScaleCharToQuantum( \ |
| 297 | (lbr_bits | (lbr_bits >> 3) | (lbr_bits >> 6))); \ |
| 298 | } |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 299 | #define LBR03PacketBlue(pixelpacket) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 300 | { \ |
| 301 | unsigned char lbr_bits=ScaleQuantumToChar((pixelpacket).blue) & 0xe0; \ |
| 302 | (pixelpacket).blue=ScaleCharToQuantum( \ |
| 303 | (lbr_bits | (lbr_bits >> 3) | (lbr_bits >> 6))); \ |
| 304 | } |
| 305 | |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 306 | #define LBR03PacketRGB(pixelpacket) \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 307 | { \ |
glennrp | 05001c3 | 2011-08-06 13:04:16 +0000 | [diff] [blame] | 308 | LBR03PacketRed((pixelpacket)); \ |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 309 | LBR03PacketGreen((pixelpacket)); \ |
| 310 | LBR03PacketBlue((pixelpacket)); \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 311 | } |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 312 | |
cristy | ef61831 | 2011-06-25 12:26:44 +0000 | [diff] [blame] | 313 | #define LBR03PixelRed(pixel) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 314 | { \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 315 | unsigned char lbr_bits=ScaleQuantumToChar(GetPixelRed(image,(pixel))) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 316 | & 0xe0; \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 317 | SetPixelRed(image, ScaleCharToQuantum( \ |
| 318 | (lbr_bits | (lbr_bits >> 3) | (lbr_bits >> 6))), (pixel)); \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 319 | } |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 320 | #define LBR03Green(pixel) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 321 | { \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 322 | unsigned char lbr_bits=ScaleQuantumToChar(GetPixelGreen(image,(pixel)))\ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 323 | & 0xe0; \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 324 | SetPixelGreen(image, ScaleCharToQuantum( \ |
| 325 | (lbr_bits | (lbr_bits >> 3) | (lbr_bits >> 6))), (pixel)); \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 326 | } |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 327 | #define LBR03Blue(pixel) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 328 | { \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 329 | unsigned char lbr_bits=ScaleQuantumToChar(GetPixelBlue(image,(pixel))) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 330 | & 0xe0; \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 331 | SetPixelBlue(image, ScaleCharToQuantum( \ |
| 332 | (lbr_bits | (lbr_bits >> 3) | (lbr_bits >> 6))), (pixel)); \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 333 | } |
| 334 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 335 | #define LBR03RGB(pixel) \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 336 | { \ |
cristy | ef61831 | 2011-06-25 12:26:44 +0000 | [diff] [blame] | 337 | LBR03PixelRed((pixel)); \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 338 | LBR03Green((pixel)); \ |
| 339 | LBR03Blue((pixel)); \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 340 | } |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 341 | |
| 342 | /* LBR04: Replicate top 4 bits */ |
| 343 | |
glennrp | 05001c3 | 2011-08-06 13:04:16 +0000 | [diff] [blame] | 344 | #define LBR04PacketRed(pixelpacket) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 345 | { \ |
| 346 | unsigned char lbr_bits=ScaleQuantumToChar((pixelpacket).red) & 0xf0; \ |
| 347 | (pixelpacket).red=ScaleCharToQuantum((lbr_bits | (lbr_bits >> 4))); \ |
| 348 | } |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 349 | #define LBR04PacketGreen(pixelpacket) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 350 | { \ |
| 351 | unsigned char lbr_bits=ScaleQuantumToChar((pixelpacket).green) & 0xf0; \ |
| 352 | (pixelpacket).green=ScaleCharToQuantum((lbr_bits | (lbr_bits >> 4))); \ |
| 353 | } |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 354 | #define LBR04PacketBlue(pixelpacket) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 355 | { \ |
| 356 | unsigned char lbr_bits=ScaleQuantumToChar((pixelpacket).blue) & 0xf0; \ |
| 357 | (pixelpacket).blue=ScaleCharToQuantum((lbr_bits | (lbr_bits >> 4))); \ |
| 358 | } |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 359 | #define LBR04PacketAlpha(pixelpacket) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 360 | { \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 361 | unsigned char lbr_bits=ScaleQuantumToChar((pixelpacket).alpha) & 0xf0; \ |
| 362 | (pixelpacket).alpha=ScaleCharToQuantum((lbr_bits | (lbr_bits >> 4))); \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 363 | } |
| 364 | |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 365 | #define LBR04PacketRGB(pixelpacket) \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 366 | { \ |
glennrp | 05001c3 | 2011-08-06 13:04:16 +0000 | [diff] [blame] | 367 | LBR04PacketRed((pixelpacket)); \ |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 368 | LBR04PacketGreen((pixelpacket)); \ |
| 369 | LBR04PacketBlue((pixelpacket)); \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 370 | } |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 371 | |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 372 | #define LBR04PacketRGBO(pixelpacket) \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 373 | { \ |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 374 | LBR04PacketRGB((pixelpacket)); \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 375 | LBR04PacketAlpha((pixelpacket)); \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 376 | } |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 377 | |
cristy | ef61831 | 2011-06-25 12:26:44 +0000 | [diff] [blame] | 378 | #define LBR04PixelRed(pixel) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 379 | { \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 380 | unsigned char lbr_bits=ScaleQuantumToChar(GetPixelRed(image,(pixel))) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 381 | & 0xf0; \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 382 | SetPixelRed(image,\ |
| 383 | ScaleCharToQuantum((lbr_bits | (lbr_bits >> 4))), (pixel)); \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 384 | } |
glennrp | 54cf797 | 2011-08-06 14:28:09 +0000 | [diff] [blame] | 385 | #define LBR04PixelGreen(pixel) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 386 | { \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 387 | unsigned char lbr_bits=ScaleQuantumToChar(GetPixelGreen(image,(pixel)))\ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 388 | & 0xf0; \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 389 | SetPixelGreen(image,\ |
| 390 | ScaleCharToQuantum((lbr_bits | (lbr_bits >> 4))), (pixel)); \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 391 | } |
glennrp | 54cf797 | 2011-08-06 14:28:09 +0000 | [diff] [blame] | 392 | #define LBR04PixelBlue(pixel) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 393 | { \ |
| 394 | unsigned char lbr_bits= \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 395 | ScaleQuantumToChar(GetPixelBlue(image,(pixel))) & 0xf0; \ |
| 396 | SetPixelBlue(image,\ |
| 397 | ScaleCharToQuantum((lbr_bits | (lbr_bits >> 4))), (pixel)); \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 398 | } |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 399 | #define LBR04PixelAlpha(pixel) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 400 | { \ |
| 401 | unsigned char lbr_bits= \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 402 | ScaleQuantumToChar(GetPixelAlpha(image,(pixel))) & 0xf0; \ |
| 403 | SetPixelAlpha(image,\ |
| 404 | ScaleCharToQuantum((lbr_bits | (lbr_bits >> 4))), (pixel)); \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 405 | } |
| 406 | |
glennrp | 54cf797 | 2011-08-06 14:28:09 +0000 | [diff] [blame] | 407 | #define LBR04PixelRGB(pixel) \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 408 | { \ |
cristy | ef61831 | 2011-06-25 12:26:44 +0000 | [diff] [blame] | 409 | LBR04PixelRed((pixel)); \ |
glennrp | 54cf797 | 2011-08-06 14:28:09 +0000 | [diff] [blame] | 410 | LBR04PixelGreen((pixel)); \ |
| 411 | LBR04PixelBlue((pixel)); \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 412 | } |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 413 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 414 | #define LBR04PixelRGBA(pixel) \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 415 | { \ |
glennrp | 54cf797 | 2011-08-06 14:28:09 +0000 | [diff] [blame] | 416 | LBR04PixelRGB((pixel)); \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 417 | LBR04PixelAlpha((pixel)); \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 418 | } |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 419 | |
| 420 | |
| 421 | /* LBR08: Replicate top 8 bits */ |
| 422 | |
glennrp | 05001c3 | 2011-08-06 13:04:16 +0000 | [diff] [blame] | 423 | #define LBR08PacketRed(pixelpacket) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 424 | { \ |
| 425 | unsigned char lbr_bits=ScaleQuantumToChar((pixelpacket).red); \ |
| 426 | (pixelpacket).red=ScaleCharToQuantum((lbr_bits)); \ |
| 427 | } |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 428 | #define LBR08PacketGreen(pixelpacket) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 429 | { \ |
| 430 | unsigned char lbr_bits=ScaleQuantumToChar((pixelpacket).green); \ |
| 431 | (pixelpacket).green=ScaleCharToQuantum((lbr_bits)); \ |
| 432 | } |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 433 | #define LBR08PacketBlue(pixelpacket) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 434 | { \ |
| 435 | unsigned char lbr_bits=ScaleQuantumToChar((pixelpacket).blue); \ |
| 436 | (pixelpacket).blue=ScaleCharToQuantum((lbr_bits)); \ |
| 437 | } |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 438 | #define LBR08PacketAlpha(pixelpacket) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 439 | { \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 440 | unsigned char lbr_bits=ScaleQuantumToChar((pixelpacket).alpha); \ |
| 441 | (pixelpacket).alpha=ScaleCharToQuantum((lbr_bits)); \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 442 | } |
| 443 | |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 444 | #define LBR08PacketRGB(pixelpacket) \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 445 | { \ |
glennrp | 05001c3 | 2011-08-06 13:04:16 +0000 | [diff] [blame] | 446 | LBR08PacketRed((pixelpacket)); \ |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 447 | LBR08PacketGreen((pixelpacket)); \ |
| 448 | LBR08PacketBlue((pixelpacket)); \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 449 | } |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 450 | |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 451 | #define LBR08PacketRGBO(pixelpacket) \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 452 | { \ |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 453 | LBR08PacketRGB((pixelpacket)); \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 454 | LBR08PacketAlpha((pixelpacket)); \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 455 | } |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 456 | |
cristy | ef61831 | 2011-06-25 12:26:44 +0000 | [diff] [blame] | 457 | #define LBR08PixelRed(pixel) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 458 | { \ |
| 459 | unsigned char lbr_bits= \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 460 | ScaleQuantumToChar(GetPixelRed(image,(pixel))); \ |
| 461 | SetPixelRed(image,\ |
| 462 | ScaleCharToQuantum((lbr_bits)), (pixel)); \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 463 | } |
glennrp | 54cf797 | 2011-08-06 14:28:09 +0000 | [diff] [blame] | 464 | #define LBR08PixelGreen(pixel) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 465 | { \ |
| 466 | unsigned char lbr_bits= \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 467 | ScaleQuantumToChar(GetPixelGreen(image,(pixel))); \ |
| 468 | SetPixelGreen(image,\ |
| 469 | ScaleCharToQuantum((lbr_bits)), (pixel)); \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 470 | } |
glennrp | 54cf797 | 2011-08-06 14:28:09 +0000 | [diff] [blame] | 471 | #define LBR08PixelBlue(pixel) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 472 | { \ |
| 473 | unsigned char lbr_bits= \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 474 | ScaleQuantumToChar(GetPixelBlue(image,(pixel))); \ |
| 475 | SetPixelBlue(image,\ |
| 476 | ScaleCharToQuantum((lbr_bits)), (pixel)); \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 477 | } |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 478 | #define LBR08PixelAlpha(pixel) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 479 | { \ |
| 480 | unsigned char lbr_bits= \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 481 | ScaleQuantumToChar(GetPixelAlpha(image,(pixel))); \ |
| 482 | SetPixelAlpha(image,\ |
| 483 | ScaleCharToQuantum((lbr_bits)), (pixel)); \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 484 | } |
| 485 | |
glennrp | 54cf797 | 2011-08-06 14:28:09 +0000 | [diff] [blame] | 486 | #define LBR08PixelRGB(pixel) \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 487 | { \ |
cristy | ef61831 | 2011-06-25 12:26:44 +0000 | [diff] [blame] | 488 | LBR08PixelRed((pixel)); \ |
glennrp | 54cf797 | 2011-08-06 14:28:09 +0000 | [diff] [blame] | 489 | LBR08PixelGreen((pixel)); \ |
| 490 | LBR08PixelBlue((pixel)); \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 491 | } |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 492 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 493 | #define LBR08PixelRGBA(pixel) \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 494 | { \ |
glennrp | 54cf797 | 2011-08-06 14:28:09 +0000 | [diff] [blame] | 495 | LBR08PixelRGB((pixel)); \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 496 | LBR08PixelAlpha((pixel)); \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 497 | } |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 498 | |
| 499 | |
| 500 | /* LBR16: Replicate top 16 bits */ |
| 501 | |
glennrp | 05001c3 | 2011-08-06 13:04:16 +0000 | [diff] [blame] | 502 | #define LBR16PacketRed(pixelpacket) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 503 | { \ |
| 504 | unsigned short lbr_bits=ScaleQuantumToShort((pixelpacket).red); \ |
| 505 | (pixelpacket).red=ScaleShortToQuantum((lbr_bits)); \ |
| 506 | } |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 507 | #define LBR16PacketGreen(pixelpacket) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 508 | { \ |
| 509 | unsigned short lbr_bits=ScaleQuantumToShort((pixelpacket).green); \ |
| 510 | (pixelpacket).green=ScaleShortToQuantum((lbr_bits)); \ |
| 511 | } |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 512 | #define LBR16PacketBlue(pixelpacket) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 513 | { \ |
| 514 | unsigned short lbr_bits=ScaleQuantumToShort((pixelpacket).blue); \ |
| 515 | (pixelpacket).blue=ScaleShortToQuantum((lbr_bits)); \ |
| 516 | } |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 517 | #define LBR16PacketAlpha(pixelpacket) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 518 | { \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 519 | unsigned short lbr_bits=ScaleQuantumToShort((pixelpacket).alpha); \ |
| 520 | (pixelpacket).alpha=ScaleShortToQuantum((lbr_bits)); \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 521 | } |
| 522 | |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 523 | #define LBR16PacketRGB(pixelpacket) \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 524 | { \ |
glennrp | 05001c3 | 2011-08-06 13:04:16 +0000 | [diff] [blame] | 525 | LBR16PacketRed((pixelpacket)); \ |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 526 | LBR16PacketGreen((pixelpacket)); \ |
| 527 | LBR16PacketBlue((pixelpacket)); \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 528 | } |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 529 | |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 530 | #define LBR16PacketRGBO(pixelpacket) \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 531 | { \ |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 532 | LBR16PacketRGB((pixelpacket)); \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 533 | LBR16PacketAlpha((pixelpacket)); \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 534 | } |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 535 | |
cristy | ef61831 | 2011-06-25 12:26:44 +0000 | [diff] [blame] | 536 | #define LBR16PixelRed(pixel) \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 537 | { \ |
| 538 | unsigned short lbr_bits= \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 539 | ScaleQuantumToShort(GetPixelRed(image,(pixel))); \ |
| 540 | SetPixelRed(image,\ |
| 541 | ScaleShortToQuantum((lbr_bits)),(pixel)); \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 542 | } |
glennrp | 54cf797 | 2011-08-06 14:28:09 +0000 | [diff] [blame] | 543 | #define LBR16PixelGreen(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(GetPixelGreen(image,(pixel))); \ |
| 547 | SetPixelGreen(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 LBR16PixelBlue(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(GetPixelBlue(image,(pixel))); \ |
| 554 | SetPixelBlue(image,\ |
| 555 | ScaleShortToQuantum((lbr_bits)),(pixel)); \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 556 | } |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 557 | #define LBR16PixelAlpha(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(GetPixelAlpha(image,(pixel))); \ |
| 561 | SetPixelAlpha(image,\ |
| 562 | ScaleShortToQuantum((lbr_bits)),(pixel)); \ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 563 | } |
| 564 | |
glennrp | 54cf797 | 2011-08-06 14:28:09 +0000 | [diff] [blame] | 565 | #define LBR16PixelRGB(pixel) \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 566 | { \ |
cristy | ef61831 | 2011-06-25 12:26:44 +0000 | [diff] [blame] | 567 | LBR16PixelRed((pixel)); \ |
glennrp | 54cf797 | 2011-08-06 14:28:09 +0000 | [diff] [blame] | 568 | LBR16PixelGreen((pixel)); \ |
| 569 | LBR16PixelBlue((pixel)); \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 570 | } |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 571 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 572 | #define LBR16PixelRGBA(pixel) \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 573 | { \ |
glennrp | 54cf797 | 2011-08-06 14:28:09 +0000 | [diff] [blame] | 574 | LBR16PixelRGB((pixel)); \ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 575 | LBR16PixelAlpha((pixel)); \ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 576 | } |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 577 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 578 | /* |
| 579 | Establish thread safety. |
| 580 | setjmp/longjmp is claimed to be safe on these platforms: |
| 581 | setjmp/longjmp is alleged to be unsafe on these platforms: |
| 582 | */ |
| 583 | #ifndef SETJMP_IS_THREAD_SAFE |
| 584 | #define PNG_SETJMP_NOT_THREAD_SAFE |
| 585 | #endif |
| 586 | |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 587 | #ifdef PNG_SETJMP_NOT_THREAD_SAFE |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 588 | static SemaphoreInfo |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 589 | *ping_semaphore = (SemaphoreInfo *) NULL; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 590 | #endif |
| 591 | |
| 592 | /* |
| 593 | This temporary until I set up malloc'ed object attributes array. |
| 594 | Recompile with MNG_MAX_OBJECTS=65536L to avoid this limit but |
| 595 | waste more memory. |
| 596 | */ |
| 597 | #define MNG_MAX_OBJECTS 256 |
| 598 | |
| 599 | /* |
| 600 | If this not defined, spec is interpreted strictly. If it is |
| 601 | defined, an attempt will be made to recover from some errors, |
| 602 | including |
| 603 | o global PLTE too short |
| 604 | */ |
| 605 | #undef MNG_LOOSE |
| 606 | |
| 607 | /* |
| 608 | Don't try to define PNG_MNG_FEATURES_SUPPORTED here. Make sure |
| 609 | it's defined in libpng/pngconf.h, version 1.0.9 or later. It won't work |
| 610 | with earlier versions of libpng. From libpng-1.0.3a to libpng-1.0.8, |
| 611 | PNG_READ|WRITE_EMPTY_PLTE were used but those have been deprecated in |
| 612 | libpng in favor of PNG_MNG_FEATURES_SUPPORTED, so we set them here. |
| 613 | PNG_MNG_FEATURES_SUPPORTED is disabled by default in libpng-1.0.9 and |
| 614 | will be enabled by default in libpng-1.2.0. |
| 615 | */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 616 | #ifdef PNG_MNG_FEATURES_SUPPORTED |
| 617 | # ifndef PNG_READ_EMPTY_PLTE_SUPPORTED |
| 618 | # define PNG_READ_EMPTY_PLTE_SUPPORTED |
| 619 | # endif |
| 620 | # ifndef PNG_WRITE_EMPTY_PLTE_SUPPORTED |
| 621 | # define PNG_WRITE_EMPTY_PLTE_SUPPORTED |
| 622 | # endif |
| 623 | #endif |
| 624 | |
| 625 | /* |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 626 | Maximum valid size_t in PNG/MNG chunks is (2^31)-1 |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 627 | This macro is only defined in libpng-1.0.3 and later. |
| 628 | Previously it was PNG_MAX_UINT but that was deprecated in libpng-1.2.6 |
| 629 | */ |
| 630 | #ifndef PNG_UINT_31_MAX |
| 631 | #define PNG_UINT_31_MAX (png_uint_32) 0x7fffffffL |
| 632 | #endif |
| 633 | |
| 634 | /* |
| 635 | Constant strings for known chunk types. If you need to add a chunk, |
| 636 | add a string holding the name here. To make the code more |
| 637 | portable, we use ASCII numbers like this, not characters. |
| 638 | */ |
| 639 | |
glennrp | 85dcf87 | 2011-12-07 02:51:47 +0000 | [diff] [blame] | 640 | static png_byte mng_MHDR[5]={ 77, 72, 68, 82, (png_byte) '\0'}; |
| 641 | static png_byte mng_BACK[5]={ 66, 65, 67, 75, (png_byte) '\0'}; |
| 642 | static png_byte mng_BASI[5]={ 66, 65, 83, 73, (png_byte) '\0'}; |
| 643 | static png_byte mng_CLIP[5]={ 67, 76, 73, 80, (png_byte) '\0'}; |
| 644 | static png_byte mng_CLON[5]={ 67, 76, 79, 78, (png_byte) '\0'}; |
| 645 | static png_byte mng_DEFI[5]={ 68, 69, 70, 73, (png_byte) '\0'}; |
| 646 | static png_byte mng_DHDR[5]={ 68, 72, 68, 82, (png_byte) '\0'}; |
| 647 | static png_byte mng_DISC[5]={ 68, 73, 83, 67, (png_byte) '\0'}; |
| 648 | static png_byte mng_ENDL[5]={ 69, 78, 68, 76, (png_byte) '\0'}; |
| 649 | static png_byte mng_FRAM[5]={ 70, 82, 65, 77, (png_byte) '\0'}; |
| 650 | static png_byte mng_IEND[5]={ 73, 69, 78, 68, (png_byte) '\0'}; |
| 651 | static png_byte mng_IHDR[5]={ 73, 72, 68, 82, (png_byte) '\0'}; |
| 652 | static png_byte mng_JHDR[5]={ 74, 72, 68, 82, (png_byte) '\0'}; |
| 653 | static png_byte mng_LOOP[5]={ 76, 79, 79, 80, (png_byte) '\0'}; |
| 654 | static png_byte mng_MAGN[5]={ 77, 65, 71, 78, (png_byte) '\0'}; |
| 655 | static png_byte mng_MEND[5]={ 77, 69, 78, 68, (png_byte) '\0'}; |
| 656 | static png_byte mng_MOVE[5]={ 77, 79, 86, 69, (png_byte) '\0'}; |
| 657 | static png_byte mng_PAST[5]={ 80, 65, 83, 84, (png_byte) '\0'}; |
| 658 | static png_byte mng_PLTE[5]={ 80, 76, 84, 69, (png_byte) '\0'}; |
| 659 | static png_byte mng_SAVE[5]={ 83, 65, 86, 69, (png_byte) '\0'}; |
| 660 | static png_byte mng_SEEK[5]={ 83, 69, 69, 75, (png_byte) '\0'}; |
| 661 | static png_byte mng_SHOW[5]={ 83, 72, 79, 87, (png_byte) '\0'}; |
| 662 | static png_byte mng_TERM[5]={ 84, 69, 82, 77, (png_byte) '\0'}; |
| 663 | static png_byte mng_bKGD[5]={ 98, 75, 71, 68, (png_byte) '\0'}; |
| 664 | static png_byte mng_cHRM[5]={ 99, 72, 82, 77, (png_byte) '\0'}; |
| 665 | static png_byte mng_gAMA[5]={103, 65, 77, 65, (png_byte) '\0'}; |
| 666 | static png_byte mng_iCCP[5]={105, 67, 67, 80, (png_byte) '\0'}; |
| 667 | static png_byte mng_nEED[5]={110, 69, 69, 68, (png_byte) '\0'}; |
| 668 | static png_byte mng_pHYg[5]={112, 72, 89, 103, (png_byte) '\0'}; |
| 669 | static png_byte mng_vpAg[5]={118, 112, 65, 103, (png_byte) '\0'}; |
| 670 | static png_byte mng_pHYs[5]={112, 72, 89, 115, (png_byte) '\0'}; |
| 671 | static png_byte mng_sBIT[5]={115, 66, 73, 84, (png_byte) '\0'}; |
| 672 | static png_byte mng_sRGB[5]={115, 82, 71, 66, (png_byte) '\0'}; |
| 673 | static png_byte mng_tRNS[5]={116, 82, 78, 83, (png_byte) '\0'}; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 674 | |
| 675 | #if defined(JNG_SUPPORTED) |
glennrp | 85dcf87 | 2011-12-07 02:51:47 +0000 | [diff] [blame] | 676 | static png_byte mng_IDAT[5]={ 73, 68, 65, 84, (png_byte) '\0'}; |
| 677 | static png_byte mng_JDAT[5]={ 74, 68, 65, 84, (png_byte) '\0'}; |
| 678 | static png_byte mng_JDAA[5]={ 74, 68, 65, 65, (png_byte) '\0'}; |
| 679 | static png_byte mng_JdAA[5]={ 74, 100, 65, 65, (png_byte) '\0'}; |
| 680 | static png_byte mng_JSEP[5]={ 74, 83, 69, 80, (png_byte) '\0'}; |
| 681 | static png_byte mng_oFFs[5]={111, 70, 70, 115, (png_byte) '\0'}; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 682 | #endif |
| 683 | |
| 684 | /* |
| 685 | Other known chunks that are not yet supported by ImageMagick: |
glennrp | 85dcf87 | 2011-12-07 02:51:47 +0000 | [diff] [blame] | 686 | static png_byte mng_hIST[5]={104, 73, 83, 84, (png_byte) '\0'}; |
| 687 | static png_byte mng_iCCP[5]={105, 67, 67, 80, (png_byte) '\0'}; |
| 688 | static png_byte mng_iTXt[5]={105, 84, 88, 116, (png_byte) '\0'}; |
| 689 | static png_byte mng_sPLT[5]={115, 80, 76, 84, (png_byte) '\0'}; |
| 690 | static png_byte mng_sTER[5]={115, 84, 69, 82, (png_byte) '\0'}; |
| 691 | static png_byte mng_tEXt[5]={116, 69, 88, 116, (png_byte) '\0'}; |
| 692 | static png_byte mng_tIME[5]={116, 73, 77, 69, (png_byte) '\0'}; |
| 693 | static png_byte mng_zTXt[5]={122, 84, 88, 116, (png_byte) '\0'}; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 694 | */ |
| 695 | |
| 696 | typedef struct _MngBox |
| 697 | { |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 698 | long |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 699 | left, |
| 700 | right, |
| 701 | top, |
| 702 | bottom; |
| 703 | } MngBox; |
| 704 | |
| 705 | typedef struct _MngPair |
| 706 | { |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 707 | volatile long |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 708 | a, |
| 709 | b; |
| 710 | } MngPair; |
| 711 | |
| 712 | #ifdef MNG_OBJECT_BUFFERS |
| 713 | typedef struct _MngBuffer |
| 714 | { |
| 715 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 716 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 717 | height, |
| 718 | width; |
| 719 | |
| 720 | Image |
| 721 | *image; |
| 722 | |
| 723 | png_color |
| 724 | plte[256]; |
| 725 | |
| 726 | int |
| 727 | reference_count; |
| 728 | |
| 729 | unsigned char |
| 730 | alpha_sample_depth, |
| 731 | compression_method, |
| 732 | color_type, |
| 733 | concrete, |
| 734 | filter_method, |
| 735 | frozen, |
| 736 | image_type, |
| 737 | interlace_method, |
| 738 | pixel_sample_depth, |
| 739 | plte_length, |
| 740 | sample_depth, |
| 741 | viewable; |
| 742 | } MngBuffer; |
| 743 | #endif |
| 744 | |
| 745 | typedef struct _MngInfo |
| 746 | { |
| 747 | |
| 748 | #ifdef MNG_OBJECT_BUFFERS |
| 749 | MngBuffer |
| 750 | *ob[MNG_MAX_OBJECTS]; |
| 751 | #endif |
| 752 | |
| 753 | Image * |
| 754 | image; |
| 755 | |
| 756 | RectangleInfo |
| 757 | page; |
| 758 | |
| 759 | int |
| 760 | adjoin, |
| 761 | #ifndef PNG_READ_EMPTY_PLTE_SUPPORTED |
| 762 | bytes_in_read_buffer, |
| 763 | found_empty_plte, |
| 764 | #endif |
| 765 | equal_backgrounds, |
| 766 | equal_chrms, |
| 767 | equal_gammas, |
| 768 | #if defined(PNG_WRITE_EMPTY_PLTE_SUPPORTED) || \ |
| 769 | defined(PNG_MNG_FEATURES_SUPPORTED) |
| 770 | equal_palettes, |
| 771 | #endif |
| 772 | equal_physs, |
| 773 | equal_srgbs, |
| 774 | framing_mode, |
| 775 | have_global_bkgd, |
| 776 | have_global_chrm, |
| 777 | have_global_gama, |
| 778 | have_global_phys, |
| 779 | have_global_sbit, |
| 780 | have_global_srgb, |
| 781 | have_saved_bkgd_index, |
| 782 | have_write_global_chrm, |
| 783 | have_write_global_gama, |
| 784 | have_write_global_plte, |
| 785 | have_write_global_srgb, |
| 786 | need_fram, |
| 787 | object_id, |
| 788 | old_framing_mode, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 789 | saved_bkgd_index; |
| 790 | |
| 791 | int |
| 792 | new_number_colors; |
| 793 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 794 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 795 | image_found, |
| 796 | loop_count[256], |
| 797 | loop_iteration[256], |
| 798 | scenes_found, |
| 799 | x_off[MNG_MAX_OBJECTS], |
| 800 | y_off[MNG_MAX_OBJECTS]; |
| 801 | |
| 802 | MngBox |
| 803 | clip, |
| 804 | frame, |
| 805 | image_box, |
| 806 | object_clip[MNG_MAX_OBJECTS]; |
| 807 | |
| 808 | unsigned char |
| 809 | /* These flags could be combined into one byte */ |
| 810 | exists[MNG_MAX_OBJECTS], |
| 811 | frozen[MNG_MAX_OBJECTS], |
| 812 | loop_active[256], |
| 813 | invisible[MNG_MAX_OBJECTS], |
| 814 | viewable[MNG_MAX_OBJECTS]; |
| 815 | |
| 816 | MagickOffsetType |
| 817 | loop_jump[256]; |
| 818 | |
| 819 | png_colorp |
| 820 | global_plte; |
| 821 | |
| 822 | png_color_8 |
| 823 | global_sbit; |
| 824 | |
| 825 | png_byte |
| 826 | #ifndef PNG_READ_EMPTY_PLTE_SUPPORTED |
| 827 | read_buffer[8], |
| 828 | #endif |
| 829 | global_trns[256]; |
| 830 | |
| 831 | float |
| 832 | global_gamma; |
| 833 | |
| 834 | ChromaticityInfo |
| 835 | global_chrm; |
| 836 | |
| 837 | RenderingIntent |
| 838 | global_srgb_intent; |
| 839 | |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 840 | unsigned int |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 841 | delay, |
| 842 | global_plte_length, |
| 843 | global_trns_length, |
| 844 | global_x_pixels_per_unit, |
| 845 | global_y_pixels_per_unit, |
| 846 | mng_width, |
| 847 | mng_height, |
| 848 | ticks_per_second; |
| 849 | |
glennrp | b9cfe27 | 2010-12-21 15:08:06 +0000 | [diff] [blame] | 850 | MagickBooleanType |
| 851 | need_blob; |
| 852 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 853 | unsigned int |
| 854 | IsPalette, |
| 855 | global_phys_unit_type, |
| 856 | basi_warning, |
| 857 | clon_warning, |
| 858 | dhdr_warning, |
| 859 | jhdr_warning, |
| 860 | magn_warning, |
| 861 | past_warning, |
| 862 | phyg_warning, |
| 863 | phys_warning, |
| 864 | sbit_warning, |
| 865 | show_warning, |
| 866 | mng_type, |
| 867 | write_mng, |
| 868 | write_png_colortype, |
| 869 | write_png_depth, |
glennrp | 1868258 | 2011-06-30 18:11:47 +0000 | [diff] [blame] | 870 | write_png_compression_level, |
| 871 | write_png_compression_strategy, |
| 872 | write_png_compression_filter, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 873 | write_png8, |
| 874 | write_png24, |
| 875 | write_png32; |
| 876 | |
| 877 | #ifdef MNG_BASI_SUPPORTED |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 878 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 879 | basi_width, |
| 880 | basi_height; |
| 881 | |
| 882 | unsigned int |
| 883 | basi_depth, |
| 884 | basi_color_type, |
| 885 | basi_compression_method, |
| 886 | basi_filter_type, |
| 887 | basi_interlace_method, |
| 888 | basi_red, |
| 889 | basi_green, |
| 890 | basi_blue, |
| 891 | basi_alpha, |
| 892 | basi_viewable; |
| 893 | #endif |
| 894 | |
| 895 | png_uint_16 |
| 896 | magn_first, |
| 897 | magn_last, |
| 898 | magn_mb, |
| 899 | magn_ml, |
| 900 | magn_mr, |
| 901 | magn_mt, |
| 902 | magn_mx, |
| 903 | magn_my, |
| 904 | magn_methx, |
| 905 | magn_methy; |
| 906 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 907 | PixelInfo |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 908 | mng_global_bkgd; |
| 909 | |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 910 | /* Added at version 6.6.6-7 */ |
| 911 | MagickBooleanType |
| 912 | ping_exclude_bKGD, |
| 913 | ping_exclude_cHRM, |
glennrp | a0ed009 | 2011-04-18 16:36:29 +0000 | [diff] [blame] | 914 | ping_exclude_date, |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 915 | ping_exclude_EXIF, |
| 916 | ping_exclude_gAMA, |
| 917 | ping_exclude_iCCP, |
| 918 | /* ping_exclude_iTXt, */ |
| 919 | ping_exclude_oFFs, |
| 920 | ping_exclude_pHYs, |
| 921 | ping_exclude_sRGB, |
| 922 | ping_exclude_tEXt, |
glennrp | a1e3b7b | 2010-12-24 16:37:33 +0000 | [diff] [blame] | 923 | ping_exclude_tRNS, |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 924 | ping_exclude_vpAg, |
| 925 | ping_exclude_zCCP, /* hex-encoded iCCP */ |
glennrp | 8d3d6e5 | 2011-04-19 04:39:51 +0000 | [diff] [blame] | 926 | ping_exclude_zTXt, |
| 927 | ping_preserve_colormap; |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 928 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 929 | } MngInfo; |
| 930 | #endif /* VER */ |
| 931 | |
| 932 | /* |
| 933 | Forward declarations. |
| 934 | */ |
| 935 | static MagickBooleanType |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 936 | WritePNGImage(const ImageInfo *,Image *,ExceptionInfo *); |
glennrp | 0c3e06b | 2010-11-19 13:45:02 +0000 | [diff] [blame] | 937 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 938 | static MagickBooleanType |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 939 | WriteMNGImage(const ImageInfo *,Image *,ExceptionInfo *); |
glennrp | 0c3e06b | 2010-11-19 13:45:02 +0000 | [diff] [blame] | 940 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 941 | #if defined(JNG_SUPPORTED) |
| 942 | static MagickBooleanType |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 943 | WriteJNGImage(const ImageInfo *,Image *,ExceptionInfo *); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 944 | #endif |
| 945 | |
glennrp | 0c3e06b | 2010-11-19 13:45:02 +0000 | [diff] [blame] | 946 | #if PNG_LIBPNG_VER > 10011 |
| 947 | |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 948 | |
glennrp | 0c3e06b | 2010-11-19 13:45:02 +0000 | [diff] [blame] | 949 | #if (MAGICKCORE_QUANTUM_DEPTH >= 16) |
| 950 | static MagickBooleanType |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 951 | LosslessReduceDepthOK(Image *image,ExceptionInfo *exception) |
glennrp | 0c3e06b | 2010-11-19 13:45:02 +0000 | [diff] [blame] | 952 | { |
glennrp | 67b9c1a | 2011-04-22 18:47:36 +0000 | [diff] [blame] | 953 | /* Reduce bit depth if it can be reduced losslessly from 16+ to 8. |
| 954 | * |
| 955 | * This is true if the high byte and the next highest byte of |
| 956 | * each sample of the image, the colormap, and the background color |
glennrp | 3faa9a3 | 2011-04-23 14:00:25 +0000 | [diff] [blame] | 957 | * are equal to each other. We check this by seeing if the samples |
| 958 | * 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] | 959 | * |
| 960 | * We don't use the method GetImageDepth() because it doesn't check |
glennrp | 3faa9a3 | 2011-04-23 14:00:25 +0000 | [diff] [blame] | 961 | * background and doesn't handle PseudoClass specially. |
glennrp | 67b9c1a | 2011-04-22 18:47:36 +0000 | [diff] [blame] | 962 | */ |
| 963 | |
glennrp | 3faa9a3 | 2011-04-23 14:00:25 +0000 | [diff] [blame] | 964 | #define QuantumToCharToQuantumEqQuantum(quantum) \ |
| 965 | ((ScaleCharToQuantum((unsigned char) ScaleQuantumToChar(quantum))) == quantum) |
| 966 | |
glennrp | 0c3e06b | 2010-11-19 13:45:02 +0000 | [diff] [blame] | 967 | MagickBooleanType |
| 968 | ok_to_reduce=MagickFalse; |
glennrp | 0c3e06b | 2010-11-19 13:45:02 +0000 | [diff] [blame] | 969 | |
glennrp | 03e11f6 | 2011-04-22 13:30:16 +0000 | [diff] [blame] | 970 | if (image->depth >= 16) |
glennrp | 0c3e06b | 2010-11-19 13:45:02 +0000 | [diff] [blame] | 971 | { |
| 972 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 973 | const Quantum |
glennrp | 0c3e06b | 2010-11-19 13:45:02 +0000 | [diff] [blame] | 974 | *p; |
| 975 | |
| 976 | ok_to_reduce= |
glennrp | 3faa9a3 | 2011-04-23 14:00:25 +0000 | [diff] [blame] | 977 | QuantumToCharToQuantumEqQuantum(image->background_color.red) && |
| 978 | QuantumToCharToQuantumEqQuantum(image->background_color.green) && |
| 979 | QuantumToCharToQuantumEqQuantum(image->background_color.blue) ? |
| 980 | MagickTrue : MagickFalse; |
glennrp | 0c3e06b | 2010-11-19 13:45:02 +0000 | [diff] [blame] | 981 | |
| 982 | if (ok_to_reduce != MagickFalse && image->storage_class == PseudoClass) |
| 983 | { |
| 984 | int indx; |
| 985 | |
| 986 | for (indx=0; indx < (ssize_t) image->colors; indx++) |
| 987 | { |
glennrp | 3faa9a3 | 2011-04-23 14:00:25 +0000 | [diff] [blame] | 988 | ok_to_reduce=( |
| 989 | QuantumToCharToQuantumEqQuantum( |
| 990 | image->colormap[indx].red) && |
| 991 | QuantumToCharToQuantumEqQuantum( |
| 992 | image->colormap[indx].green) && |
| 993 | QuantumToCharToQuantumEqQuantum( |
| 994 | image->colormap[indx].blue)) ? |
| 995 | MagickTrue : MagickFalse; |
| 996 | |
glennrp | 0c3e06b | 2010-11-19 13:45:02 +0000 | [diff] [blame] | 997 | if (ok_to_reduce == MagickFalse) |
glennrp | 3faa9a3 | 2011-04-23 14:00:25 +0000 | [diff] [blame] | 998 | break; |
glennrp | 0c3e06b | 2010-11-19 13:45:02 +0000 | [diff] [blame] | 999 | } |
| 1000 | } |
| 1001 | |
| 1002 | if ((ok_to_reduce != MagickFalse) && |
| 1003 | (image->storage_class != PseudoClass)) |
| 1004 | { |
| 1005 | ssize_t |
| 1006 | y; |
| 1007 | |
| 1008 | register ssize_t |
| 1009 | x; |
| 1010 | |
| 1011 | for (y=0; y < (ssize_t) image->rows; y++) |
| 1012 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 1013 | p=GetVirtualPixels(image,0,y,image->columns,1,exception); |
glennrp | 0c3e06b | 2010-11-19 13:45:02 +0000 | [diff] [blame] | 1014 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 1015 | if (p == (const Quantum *) NULL) |
glennrp | 0c3e06b | 2010-11-19 13:45:02 +0000 | [diff] [blame] | 1016 | { |
| 1017 | ok_to_reduce = MagickFalse; |
| 1018 | break; |
| 1019 | } |
| 1020 | |
| 1021 | for (x=(ssize_t) image->columns-1; x >= 0; x--) |
| 1022 | { |
glennrp | 3faa9a3 | 2011-04-23 14:00:25 +0000 | [diff] [blame] | 1023 | ok_to_reduce= |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 1024 | QuantumToCharToQuantumEqQuantum(GetPixelRed(image,p)) && |
| 1025 | QuantumToCharToQuantumEqQuantum(GetPixelGreen(image,p)) && |
| 1026 | QuantumToCharToQuantumEqQuantum(GetPixelBlue(image,p)) ? |
glennrp | 3faa9a3 | 2011-04-23 14:00:25 +0000 | [diff] [blame] | 1027 | MagickTrue : MagickFalse; |
glennrp | 0c3e06b | 2010-11-19 13:45:02 +0000 | [diff] [blame] | 1028 | |
| 1029 | if (ok_to_reduce == MagickFalse) |
| 1030 | break; |
| 1031 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 1032 | p+=GetPixelChannels(image); |
glennrp | 0c3e06b | 2010-11-19 13:45:02 +0000 | [diff] [blame] | 1033 | } |
glennrp | 8640fb5 | 2010-11-23 15:48:26 +0000 | [diff] [blame] | 1034 | if (x >= 0) |
glennrp | 0c3e06b | 2010-11-19 13:45:02 +0000 | [diff] [blame] | 1035 | break; |
| 1036 | } |
| 1037 | } |
| 1038 | |
| 1039 | if (ok_to_reduce != MagickFalse) |
| 1040 | { |
glennrp | 0c3e06b | 2010-11-19 13:45:02 +0000 | [diff] [blame] | 1041 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 1042 | " OK to reduce PNG bit depth to 8 without loss of info"); |
glennrp | 0c3e06b | 2010-11-19 13:45:02 +0000 | [diff] [blame] | 1043 | } |
glennrp | a6a0663 | 2011-01-19 15:15:34 +0000 | [diff] [blame] | 1044 | else |
| 1045 | { |
| 1046 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 1047 | " Not OK to reduce PNG bit depth to 8 without loss of info"); |
glennrp | a6a0663 | 2011-01-19 15:15:34 +0000 | [diff] [blame] | 1048 | } |
glennrp | 0c3e06b | 2010-11-19 13:45:02 +0000 | [diff] [blame] | 1049 | } |
| 1050 | |
| 1051 | return ok_to_reduce; |
| 1052 | } |
| 1053 | #endif /* MAGICKCORE_QUANTUM_DEPTH >= 16 */ |
| 1054 | |
glennrp | 1a56e9c | 2012-04-25 17:06:57 +0000 | [diff] [blame] | 1055 | static const char* PngColorTypeToString(const unsigned int color_type) |
| 1056 | { |
| 1057 | const char |
| 1058 | *result = "Unknown"; |
| 1059 | |
| 1060 | switch (color_type) |
| 1061 | { |
| 1062 | case PNG_COLOR_TYPE_GRAY: |
| 1063 | result = "Gray"; |
| 1064 | break; |
| 1065 | case PNG_COLOR_TYPE_GRAY_ALPHA: |
| 1066 | result = "Gray+Alpha"; |
| 1067 | break; |
| 1068 | case PNG_COLOR_TYPE_PALETTE: |
| 1069 | result = "Palette"; |
| 1070 | break; |
| 1071 | case PNG_COLOR_TYPE_RGB: |
| 1072 | result = "RGB"; |
| 1073 | break; |
| 1074 | case PNG_COLOR_TYPE_RGB_ALPHA: |
| 1075 | result = "RGB+Alpha"; |
| 1076 | break; |
| 1077 | } |
| 1078 | |
| 1079 | return result; |
| 1080 | } |
| 1081 | |
glennrp | e610a07 | 2010-08-05 17:08:46 +0000 | [diff] [blame] | 1082 | static int |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 1083 | Magick_RenderingIntent_to_PNG_RenderingIntent(const RenderingIntent intent) |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1084 | { |
glennrp | e610a07 | 2010-08-05 17:08:46 +0000 | [diff] [blame] | 1085 | switch (intent) |
| 1086 | { |
| 1087 | case PerceptualIntent: |
| 1088 | return 0; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1089 | |
glennrp | e610a07 | 2010-08-05 17:08:46 +0000 | [diff] [blame] | 1090 | case RelativeIntent: |
| 1091 | return 1; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1092 | |
glennrp | e610a07 | 2010-08-05 17:08:46 +0000 | [diff] [blame] | 1093 | case SaturationIntent: |
| 1094 | return 2; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1095 | |
glennrp | e610a07 | 2010-08-05 17:08:46 +0000 | [diff] [blame] | 1096 | case AbsoluteIntent: |
| 1097 | return 3; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1098 | |
glennrp | e610a07 | 2010-08-05 17:08:46 +0000 | [diff] [blame] | 1099 | default: |
| 1100 | return -1; |
| 1101 | } |
| 1102 | } |
| 1103 | |
| 1104 | static RenderingIntent |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 1105 | Magick_RenderingIntent_from_PNG_RenderingIntent(const int ping_intent) |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1106 | { |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 1107 | switch (ping_intent) |
glennrp | e610a07 | 2010-08-05 17:08:46 +0000 | [diff] [blame] | 1108 | { |
| 1109 | case 0: |
| 1110 | return PerceptualIntent; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1111 | |
glennrp | e610a07 | 2010-08-05 17:08:46 +0000 | [diff] [blame] | 1112 | case 1: |
| 1113 | return RelativeIntent; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1114 | |
glennrp | e610a07 | 2010-08-05 17:08:46 +0000 | [diff] [blame] | 1115 | case 2: |
| 1116 | return SaturationIntent; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1117 | |
glennrp | e610a07 | 2010-08-05 17:08:46 +0000 | [diff] [blame] | 1118 | case 3: |
| 1119 | return AbsoluteIntent; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1120 | |
glennrp | e610a07 | 2010-08-05 17:08:46 +0000 | [diff] [blame] | 1121 | default: |
| 1122 | return UndefinedIntent; |
| 1123 | } |
| 1124 | } |
| 1125 | |
cristy | 9d8c122 | 2012-08-10 12:34:19 +0000 | [diff] [blame] | 1126 | static const char * |
glennrp | 98b83d4 | 2012-07-23 02:50:31 +0000 | [diff] [blame] | 1127 | Magick_RenderingIntentString_from_PNG_RenderingIntent(const int ping_intent) |
| 1128 | { |
| 1129 | switch (ping_intent) |
| 1130 | { |
| 1131 | case 0: |
| 1132 | return "Perceptual Intent"; |
| 1133 | |
| 1134 | case 1: |
| 1135 | return "Relative Intent"; |
| 1136 | |
| 1137 | case 2: |
| 1138 | return "Saturation Intent"; |
| 1139 | |
| 1140 | case 3: |
| 1141 | return "Absolute Intent"; |
| 1142 | |
| 1143 | default: |
| 1144 | return "Undefined Intent"; |
| 1145 | } |
| 1146 | } |
| 1147 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1148 | static inline ssize_t MagickMax(const ssize_t x,const ssize_t y) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1149 | { |
| 1150 | if (x > y) |
| 1151 | return(x); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1152 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1153 | return(y); |
| 1154 | } |
glennrp | 0c3e06b | 2010-11-19 13:45:02 +0000 | [diff] [blame] | 1155 | |
cristy | d9ecd04 | 2012-06-17 18:26:12 +0000 | [diff] [blame] | 1156 | static const char * |
glennrp | 5dff435 | 2012-06-06 22:12:04 +0000 | [diff] [blame] | 1157 | Magick_ColorType_from_PNG_ColorType(const int ping_colortype) |
| 1158 | { |
| 1159 | switch (ping_colortype) |
| 1160 | { |
| 1161 | case 0: |
| 1162 | return "Grayscale"; |
| 1163 | |
| 1164 | case 2: |
| 1165 | return "Truecolor"; |
| 1166 | |
| 1167 | case 3: |
| 1168 | return "Indexed"; |
| 1169 | |
| 1170 | case 4: |
| 1171 | return "GrayAlpha"; |
| 1172 | |
| 1173 | case 6: |
| 1174 | return "RGBA"; |
| 1175 | |
| 1176 | default: |
| 1177 | return "UndefinedColorType"; |
| 1178 | } |
| 1179 | } |
| 1180 | |
| 1181 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1182 | static inline ssize_t MagickMin(const ssize_t x,const ssize_t y) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1183 | { |
| 1184 | if (x < y) |
| 1185 | return(x); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1186 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1187 | return(y); |
| 1188 | } |
glennrp | d5045b4 | 2010-03-24 12:40:35 +0000 | [diff] [blame] | 1189 | #endif /* PNG_LIBPNG_VER > 10011 */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1190 | #endif /* MAGICKCORE_PNG_DELEGATE */ |
| 1191 | |
| 1192 | /* |
| 1193 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1194 | % % |
| 1195 | % % |
| 1196 | % % |
| 1197 | % I s M N G % |
| 1198 | % % |
| 1199 | % % |
| 1200 | % % |
| 1201 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1202 | % |
| 1203 | % IsMNG() returns MagickTrue if the image format type, identified by the |
| 1204 | % magick string, is MNG. |
| 1205 | % |
| 1206 | % The format of the IsMNG method is: |
| 1207 | % |
| 1208 | % MagickBooleanType IsMNG(const unsigned char *magick,const size_t length) |
| 1209 | % |
| 1210 | % A description of each parameter follows: |
| 1211 | % |
| 1212 | % o magick: compare image format pattern against these bytes. |
| 1213 | % |
| 1214 | % o length: Specifies the length of the magick string. |
| 1215 | % |
| 1216 | % |
| 1217 | */ |
| 1218 | static MagickBooleanType IsMNG(const unsigned char *magick,const size_t length) |
| 1219 | { |
| 1220 | if (length < 8) |
| 1221 | return(MagickFalse); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1222 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1223 | if (memcmp(magick,"\212MNG\r\n\032\n",8) == 0) |
| 1224 | return(MagickTrue); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1225 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1226 | return(MagickFalse); |
| 1227 | } |
| 1228 | |
| 1229 | /* |
| 1230 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1231 | % % |
| 1232 | % % |
| 1233 | % % |
| 1234 | % I s J N G % |
| 1235 | % % |
| 1236 | % % |
| 1237 | % % |
| 1238 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1239 | % |
| 1240 | % IsJNG() returns MagickTrue if the image format type, identified by the |
| 1241 | % magick string, is JNG. |
| 1242 | % |
| 1243 | % The format of the IsJNG method is: |
| 1244 | % |
| 1245 | % MagickBooleanType IsJNG(const unsigned char *magick,const size_t length) |
| 1246 | % |
| 1247 | % A description of each parameter follows: |
| 1248 | % |
| 1249 | % o magick: compare image format pattern against these bytes. |
| 1250 | % |
| 1251 | % o length: Specifies the length of the magick string. |
| 1252 | % |
| 1253 | % |
| 1254 | */ |
| 1255 | static MagickBooleanType IsJNG(const unsigned char *magick,const size_t length) |
| 1256 | { |
| 1257 | if (length < 8) |
| 1258 | return(MagickFalse); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1259 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1260 | if (memcmp(magick,"\213JNG\r\n\032\n",8) == 0) |
| 1261 | return(MagickTrue); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1262 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1263 | return(MagickFalse); |
| 1264 | } |
| 1265 | |
| 1266 | /* |
| 1267 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1268 | % % |
| 1269 | % % |
| 1270 | % % |
| 1271 | % I s P N G % |
| 1272 | % % |
| 1273 | % % |
| 1274 | % % |
| 1275 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1276 | % |
| 1277 | % IsPNG() returns MagickTrue if the image format type, identified by the |
| 1278 | % magick string, is PNG. |
| 1279 | % |
| 1280 | % The format of the IsPNG method is: |
| 1281 | % |
| 1282 | % MagickBooleanType IsPNG(const unsigned char *magick,const size_t length) |
| 1283 | % |
| 1284 | % A description of each parameter follows: |
| 1285 | % |
| 1286 | % o magick: compare image format pattern against these bytes. |
| 1287 | % |
| 1288 | % o length: Specifies the length of the magick string. |
| 1289 | % |
| 1290 | */ |
| 1291 | static MagickBooleanType IsPNG(const unsigned char *magick,const size_t length) |
| 1292 | { |
| 1293 | if (length < 8) |
| 1294 | return(MagickFalse); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1295 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1296 | if (memcmp(magick,"\211PNG\r\n\032\n",8) == 0) |
| 1297 | return(MagickTrue); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1298 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1299 | return(MagickFalse); |
| 1300 | } |
| 1301 | |
| 1302 | #if defined(MAGICKCORE_PNG_DELEGATE) |
| 1303 | #if defined(__cplusplus) || defined(c_plusplus) |
| 1304 | extern "C" { |
| 1305 | #endif |
| 1306 | |
glennrp | d5045b4 | 2010-03-24 12:40:35 +0000 | [diff] [blame] | 1307 | #if (PNG_LIBPNG_VER > 10011) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1308 | static size_t WriteBlobMSBULong(Image *image,const size_t value) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1309 | { |
| 1310 | unsigned char |
| 1311 | buffer[4]; |
| 1312 | |
| 1313 | assert(image != (Image *) NULL); |
| 1314 | assert(image->signature == MagickSignature); |
| 1315 | buffer[0]=(unsigned char) (value >> 24); |
| 1316 | buffer[1]=(unsigned char) (value >> 16); |
| 1317 | buffer[2]=(unsigned char) (value >> 8); |
| 1318 | buffer[3]=(unsigned char) value; |
| 1319 | return((size_t) WriteBlob(image,4,buffer)); |
| 1320 | } |
| 1321 | |
| 1322 | static void PNGLong(png_bytep p,png_uint_32 value) |
| 1323 | { |
| 1324 | *p++=(png_byte) ((value >> 24) & 0xff); |
| 1325 | *p++=(png_byte) ((value >> 16) & 0xff); |
| 1326 | *p++=(png_byte) ((value >> 8) & 0xff); |
| 1327 | *p++=(png_byte) (value & 0xff); |
| 1328 | } |
| 1329 | |
glennrp | a521b2f | 2010-10-29 04:11:03 +0000 | [diff] [blame] | 1330 | #if defined(JNG_SUPPORTED) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1331 | static void PNGsLong(png_bytep p,png_int_32 value) |
| 1332 | { |
| 1333 | *p++=(png_byte) ((value >> 24) & 0xff); |
| 1334 | *p++=(png_byte) ((value >> 16) & 0xff); |
| 1335 | *p++=(png_byte) ((value >> 8) & 0xff); |
| 1336 | *p++=(png_byte) (value & 0xff); |
| 1337 | } |
glennrp | a521b2f | 2010-10-29 04:11:03 +0000 | [diff] [blame] | 1338 | #endif |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1339 | |
| 1340 | static void PNGShort(png_bytep p,png_uint_16 value) |
| 1341 | { |
| 1342 | *p++=(png_byte) ((value >> 8) & 0xff); |
| 1343 | *p++=(png_byte) (value & 0xff); |
| 1344 | } |
| 1345 | |
| 1346 | static void PNGType(png_bytep p,png_bytep type) |
| 1347 | { |
| 1348 | (void) CopyMagickMemory(p,type,4*sizeof(png_byte)); |
| 1349 | } |
| 1350 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 1351 | static void LogPNGChunk(MagickBooleanType logging, png_bytep type, |
| 1352 | size_t length) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1353 | { |
| 1354 | if (logging != MagickFalse) |
| 1355 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 1356 | " Writing %c%c%c%c chunk, length: %.20g", |
| 1357 | type[0],type[1],type[2],type[3],(double) length); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1358 | } |
glennrp | d5045b4 | 2010-03-24 12:40:35 +0000 | [diff] [blame] | 1359 | #endif /* PNG_LIBPNG_VER > 10011 */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1360 | |
| 1361 | #if defined(__cplusplus) || defined(c_plusplus) |
| 1362 | } |
| 1363 | #endif |
| 1364 | |
glennrp | d5045b4 | 2010-03-24 12:40:35 +0000 | [diff] [blame] | 1365 | #if PNG_LIBPNG_VER > 10011 |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1366 | /* |
| 1367 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1368 | % % |
| 1369 | % % |
| 1370 | % % |
| 1371 | % R e a d P N G I m a g e % |
| 1372 | % % |
| 1373 | % % |
| 1374 | % % |
| 1375 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1376 | % |
| 1377 | % ReadPNGImage() reads a Portable Network Graphics (PNG) or |
| 1378 | % Multiple-image Network Graphics (MNG) image file and returns it. It |
| 1379 | % allocates the memory necessary for the new Image structure and returns a |
| 1380 | % pointer to the new image or set of images. |
| 1381 | % |
| 1382 | % MNG support written by Glenn Randers-Pehrson, glennrp@image... |
| 1383 | % |
| 1384 | % The format of the ReadPNGImage method is: |
| 1385 | % |
| 1386 | % Image *ReadPNGImage(const ImageInfo *image_info,ExceptionInfo *exception) |
| 1387 | % |
| 1388 | % A description of each parameter follows: |
| 1389 | % |
| 1390 | % o image_info: the image info. |
| 1391 | % |
| 1392 | % o exception: return any errors or warnings in this structure. |
| 1393 | % |
| 1394 | % To do, more or less in chronological order (as of version 5.5.2, |
| 1395 | % November 26, 2002 -- glennrp -- see also "To do" under WriteMNGImage): |
| 1396 | % |
| 1397 | % Get 16-bit cheap transparency working. |
| 1398 | % |
| 1399 | % (At this point, PNG decoding is supposed to be in full MNG-LC compliance) |
| 1400 | % |
| 1401 | % Preserve all unknown and not-yet-handled known chunks found in input |
| 1402 | % PNG file and copy them into output PNG files according to the PNG |
| 1403 | % copying rules. |
| 1404 | % |
| 1405 | % (At this point, PNG encoding should be in full MNG compliance) |
| 1406 | % |
| 1407 | % Provide options for choice of background to use when the MNG BACK |
| 1408 | % chunk is not present or is not mandatory (i.e., leave transparent, |
| 1409 | % user specified, MNG BACK, PNG bKGD) |
| 1410 | % |
| 1411 | % Implement LOOP/ENDL [done, but could do discretionary loops more |
| 1412 | % efficiently by linking in the duplicate frames.]. |
| 1413 | % |
| 1414 | % Decode and act on the MHDR simplicity profile (offer option to reject |
| 1415 | % files or attempt to process them anyway when the profile isn't LC or VLC). |
| 1416 | % |
| 1417 | % Upgrade to full MNG without Delta-PNG. |
| 1418 | % |
| 1419 | % o BACK [done a while ago except for background image ID] |
| 1420 | % o MOVE [done 15 May 1999] |
| 1421 | % o CLIP [done 15 May 1999] |
| 1422 | % o DISC [done 19 May 1999] |
| 1423 | % o SAVE [partially done 19 May 1999 (marks objects frozen)] |
| 1424 | % o SEEK [partially done 19 May 1999 (discard function only)] |
| 1425 | % o SHOW |
| 1426 | % o PAST |
| 1427 | % o BASI |
| 1428 | % o MNG-level tEXt/iTXt/zTXt |
| 1429 | % o pHYg |
| 1430 | % o pHYs |
| 1431 | % o sBIT |
| 1432 | % o bKGD |
| 1433 | % o iTXt (wait for libpng implementation). |
| 1434 | % |
| 1435 | % Use the scene signature to discover when an identical scene is |
| 1436 | % being reused, and just point to the original image->exception instead |
| 1437 | % of storing another set of pixels. This not specific to MNG |
| 1438 | % but could be applied generally. |
| 1439 | % |
| 1440 | % Upgrade to full MNG with Delta-PNG. |
| 1441 | % |
| 1442 | % JNG tEXt/iTXt/zTXt |
| 1443 | % |
| 1444 | % We will not attempt to read files containing the CgBI chunk. |
| 1445 | % They are really Xcode files meant for display on the iPhone. |
| 1446 | % These are not valid PNG files and it is impossible to recover |
glennrp | f54e295 | 2011-06-29 14:20:44 +0000 | [diff] [blame] | 1447 | % the original PNG from files that have been converted to Xcode-PNG, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1448 | % since irretrievable loss of color data has occurred due to the |
| 1449 | % use of premultiplied alpha. |
| 1450 | */ |
| 1451 | |
| 1452 | #if defined(__cplusplus) || defined(c_plusplus) |
| 1453 | extern "C" { |
| 1454 | #endif |
| 1455 | |
| 1456 | /* |
| 1457 | This the function that does the actual reading of data. It is |
| 1458 | the same as the one supplied in libpng, except that it receives the |
| 1459 | datastream from the ReadBlob() function instead of standard input. |
| 1460 | */ |
| 1461 | static void png_get_data(png_structp png_ptr,png_bytep data,png_size_t length) |
| 1462 | { |
| 1463 | Image |
| 1464 | *image; |
| 1465 | |
| 1466 | image=(Image *) png_get_io_ptr(png_ptr); |
| 1467 | if (length) |
| 1468 | { |
| 1469 | png_size_t |
| 1470 | check; |
| 1471 | |
| 1472 | check=(png_size_t) ReadBlob(image,(size_t) length,data); |
| 1473 | if (check != length) |
| 1474 | { |
| 1475 | char |
| 1476 | msg[MaxTextExtent]; |
| 1477 | |
cristy | 3b6fd2e | 2011-05-20 12:53:50 +0000 | [diff] [blame] | 1478 | (void) FormatLocaleString(msg,MaxTextExtent, |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 1479 | "Expected %.20g bytes; found %.20g bytes",(double) length, |
| 1480 | (double) check); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1481 | png_warning(png_ptr,msg); |
| 1482 | png_error(png_ptr,"Read Exception"); |
| 1483 | } |
| 1484 | } |
| 1485 | } |
| 1486 | |
| 1487 | #if !defined(PNG_READ_EMPTY_PLTE_SUPPORTED) && \ |
| 1488 | !defined(PNG_MNG_FEATURES_SUPPORTED) |
| 1489 | /* We use mng_get_data() instead of png_get_data() if we have a libpng |
| 1490 | * older than libpng-1.0.3a, which was the first to allow the empty |
| 1491 | * PLTE, or a newer libpng in which PNG_MNG_FEATURES_SUPPORTED was |
| 1492 | * ifdef'ed out. Earlier versions would crash if the bKGD chunk was |
| 1493 | * encountered after an empty PLTE, so we have to look ahead for bKGD |
| 1494 | * chunks and remove them from the datastream that is passed to libpng, |
| 1495 | * and store their contents for later use. |
| 1496 | */ |
| 1497 | static void mng_get_data(png_structp png_ptr,png_bytep data,png_size_t length) |
| 1498 | { |
| 1499 | MngInfo |
| 1500 | *mng_info; |
| 1501 | |
| 1502 | Image |
| 1503 | *image; |
| 1504 | |
| 1505 | png_size_t |
| 1506 | check; |
| 1507 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1508 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1509 | i; |
| 1510 | |
| 1511 | i=0; |
| 1512 | mng_info=(MngInfo *) png_get_io_ptr(png_ptr); |
| 1513 | image=(Image *) mng_info->image; |
| 1514 | while (mng_info->bytes_in_read_buffer && length) |
| 1515 | { |
| 1516 | data[i]=mng_info->read_buffer[i]; |
| 1517 | mng_info->bytes_in_read_buffer--; |
| 1518 | length--; |
| 1519 | i++; |
| 1520 | } |
| 1521 | if (length) |
| 1522 | { |
| 1523 | check=(png_size_t) ReadBlob(image,(size_t) length,(char *) data); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1524 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1525 | if (check != length) |
| 1526 | png_error(png_ptr,"Read Exception"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1527 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1528 | if (length == 4) |
| 1529 | { |
| 1530 | if ((data[0] == 0) && (data[1] == 0) && (data[2] == 0) && |
| 1531 | (data[3] == 0)) |
| 1532 | { |
| 1533 | check=(png_size_t) ReadBlob(image,(size_t) length, |
| 1534 | (char *) mng_info->read_buffer); |
| 1535 | mng_info->read_buffer[4]=0; |
| 1536 | mng_info->bytes_in_read_buffer=4; |
| 1537 | if (memcmp(mng_info->read_buffer,mng_PLTE,4) == 0) |
| 1538 | mng_info->found_empty_plte=MagickTrue; |
| 1539 | if (memcmp(mng_info->read_buffer,mng_IEND,4) == 0) |
| 1540 | { |
| 1541 | mng_info->found_empty_plte=MagickFalse; |
| 1542 | mng_info->have_saved_bkgd_index=MagickFalse; |
| 1543 | } |
| 1544 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1545 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1546 | if ((data[0] == 0) && (data[1] == 0) && (data[2] == 0) && |
| 1547 | (data[3] == 1)) |
| 1548 | { |
| 1549 | check=(png_size_t) ReadBlob(image,(size_t) length, |
| 1550 | (char *) mng_info->read_buffer); |
| 1551 | mng_info->read_buffer[4]=0; |
| 1552 | mng_info->bytes_in_read_buffer=4; |
| 1553 | if (memcmp(mng_info->read_buffer,mng_bKGD,4) == 0) |
| 1554 | if (mng_info->found_empty_plte) |
| 1555 | { |
| 1556 | /* |
| 1557 | Skip the bKGD data byte and CRC. |
| 1558 | */ |
| 1559 | check=(png_size_t) |
| 1560 | ReadBlob(image,5,(char *) mng_info->read_buffer); |
| 1561 | check=(png_size_t) ReadBlob(image,(size_t) length, |
| 1562 | (char *) mng_info->read_buffer); |
| 1563 | mng_info->saved_bkgd_index=mng_info->read_buffer[0]; |
| 1564 | mng_info->have_saved_bkgd_index=MagickTrue; |
| 1565 | mng_info->bytes_in_read_buffer=0; |
| 1566 | } |
| 1567 | } |
| 1568 | } |
| 1569 | } |
| 1570 | } |
| 1571 | #endif |
| 1572 | |
| 1573 | static void png_put_data(png_structp png_ptr,png_bytep data,png_size_t length) |
| 1574 | { |
| 1575 | Image |
| 1576 | *image; |
| 1577 | |
| 1578 | image=(Image *) png_get_io_ptr(png_ptr); |
| 1579 | if (length) |
| 1580 | { |
| 1581 | png_size_t |
| 1582 | check; |
| 1583 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1584 | check=(png_size_t) WriteBlob(image,(size_t) length,data); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1585 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1586 | if (check != length) |
| 1587 | png_error(png_ptr,"WriteBlob Failed"); |
| 1588 | } |
| 1589 | } |
| 1590 | |
| 1591 | static void png_flush_data(png_structp png_ptr) |
| 1592 | { |
| 1593 | (void) png_ptr; |
| 1594 | } |
| 1595 | |
| 1596 | #ifdef PNG_WRITE_EMPTY_PLTE_SUPPORTED |
| 1597 | static int PalettesAreEqual(Image *a,Image *b) |
| 1598 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1599 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1600 | i; |
| 1601 | |
| 1602 | if ((a == (Image *) NULL) || (b == (Image *) NULL)) |
| 1603 | return((int) MagickFalse); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1604 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1605 | if (a->storage_class != PseudoClass || b->storage_class != PseudoClass) |
| 1606 | return((int) MagickFalse); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1607 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1608 | if (a->colors != b->colors) |
| 1609 | return((int) MagickFalse); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1610 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1611 | for (i=0; i < (ssize_t) a->colors; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1612 | { |
| 1613 | if ((a->colormap[i].red != b->colormap[i].red) || |
| 1614 | (a->colormap[i].green != b->colormap[i].green) || |
| 1615 | (a->colormap[i].blue != b->colormap[i].blue)) |
| 1616 | return((int) MagickFalse); |
| 1617 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1618 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1619 | return((int) MagickTrue); |
| 1620 | } |
| 1621 | #endif |
| 1622 | |
| 1623 | static void MngInfoDiscardObject(MngInfo *mng_info,int i) |
| 1624 | { |
| 1625 | if (i && (i < MNG_MAX_OBJECTS) && (mng_info != (MngInfo *) NULL) && |
| 1626 | mng_info->exists[i] && !mng_info->frozen[i]) |
| 1627 | { |
| 1628 | #ifdef MNG_OBJECT_BUFFERS |
| 1629 | if (mng_info->ob[i] != (MngBuffer *) NULL) |
| 1630 | { |
| 1631 | if (mng_info->ob[i]->reference_count > 0) |
| 1632 | mng_info->ob[i]->reference_count--; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1633 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1634 | if (mng_info->ob[i]->reference_count == 0) |
| 1635 | { |
| 1636 | if (mng_info->ob[i]->image != (Image *) NULL) |
| 1637 | mng_info->ob[i]->image=DestroyImage(mng_info->ob[i]->image); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1638 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1639 | mng_info->ob[i]=DestroyString(mng_info->ob[i]); |
| 1640 | } |
| 1641 | } |
| 1642 | mng_info->ob[i]=(MngBuffer *) NULL; |
| 1643 | #endif |
| 1644 | mng_info->exists[i]=MagickFalse; |
| 1645 | mng_info->invisible[i]=MagickFalse; |
| 1646 | mng_info->viewable[i]=MagickFalse; |
| 1647 | mng_info->frozen[i]=MagickFalse; |
| 1648 | mng_info->x_off[i]=0; |
| 1649 | mng_info->y_off[i]=0; |
| 1650 | mng_info->object_clip[i].left=0; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1651 | mng_info->object_clip[i].right=(ssize_t) PNG_UINT_31_MAX; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1652 | mng_info->object_clip[i].top=0; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1653 | mng_info->object_clip[i].bottom=(ssize_t) PNG_UINT_31_MAX; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1654 | } |
| 1655 | } |
| 1656 | |
glennrp | 21f0e62 | 2011-01-07 16:20:57 +0000 | [diff] [blame] | 1657 | static void MngInfoFreeStruct(MngInfo *mng_info, |
| 1658 | MagickBooleanType *have_mng_structure) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1659 | { |
glennrp | 21f0e62 | 2011-01-07 16:20:57 +0000 | [diff] [blame] | 1660 | if (*have_mng_structure != MagickFalse && (mng_info != (MngInfo *) NULL)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1661 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1662 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1663 | i; |
| 1664 | |
| 1665 | for (i=1; i < MNG_MAX_OBJECTS; i++) |
| 1666 | MngInfoDiscardObject(mng_info,i); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1667 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1668 | if (mng_info->global_plte != (png_colorp) NULL) |
| 1669 | mng_info->global_plte=(png_colorp) |
| 1670 | RelinquishMagickMemory(mng_info->global_plte); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1671 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1672 | mng_info=(MngInfo *) RelinquishMagickMemory(mng_info); |
| 1673 | *have_mng_structure=MagickFalse; |
| 1674 | } |
| 1675 | } |
| 1676 | |
| 1677 | static MngBox mng_minimum_box(MngBox box1,MngBox box2) |
| 1678 | { |
| 1679 | MngBox |
| 1680 | box; |
| 1681 | |
| 1682 | box=box1; |
| 1683 | if (box.left < box2.left) |
| 1684 | box.left=box2.left; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1685 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1686 | if (box.top < box2.top) |
| 1687 | box.top=box2.top; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1688 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1689 | if (box.right > box2.right) |
| 1690 | box.right=box2.right; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1691 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1692 | if (box.bottom > box2.bottom) |
| 1693 | box.bottom=box2.bottom; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1694 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1695 | return box; |
| 1696 | } |
| 1697 | |
| 1698 | static MngBox mng_read_box(MngBox previous_box,char delta_type,unsigned char *p) |
| 1699 | { |
| 1700 | MngBox |
| 1701 | box; |
| 1702 | |
| 1703 | /* |
| 1704 | Read clipping boundaries from DEFI, CLIP, FRAM, or PAST chunk. |
| 1705 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1706 | box.left=(ssize_t) ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]); |
| 1707 | box.right=(ssize_t) ((p[4] << 24) | (p[5] << 16) | (p[6] << 8) | p[7]); |
| 1708 | box.top=(ssize_t) ((p[8] << 24) | (p[9] << 16) | (p[10] << 8) | p[11]); |
| 1709 | 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] | 1710 | if (delta_type != 0) |
| 1711 | { |
| 1712 | box.left+=previous_box.left; |
| 1713 | box.right+=previous_box.right; |
| 1714 | box.top+=previous_box.top; |
| 1715 | box.bottom+=previous_box.bottom; |
| 1716 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1717 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1718 | return(box); |
| 1719 | } |
| 1720 | |
| 1721 | static MngPair mng_read_pair(MngPair previous_pair,int delta_type, |
| 1722 | unsigned char *p) |
| 1723 | { |
| 1724 | MngPair |
| 1725 | pair; |
| 1726 | /* |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1727 | Read two ssize_ts from CLON, MOVE or PAST chunk |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1728 | */ |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 1729 | pair.a=(long) ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]); |
| 1730 | 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] | 1731 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1732 | if (delta_type != 0) |
| 1733 | { |
| 1734 | pair.a+=previous_pair.a; |
| 1735 | pair.b+=previous_pair.b; |
| 1736 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1737 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1738 | return(pair); |
| 1739 | } |
| 1740 | |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 1741 | static long mng_get_long(unsigned char *p) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1742 | { |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 1743 | return((long) ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3])); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1744 | } |
| 1745 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 1746 | typedef struct _PNGErrorInfo |
cristy | c82a27b | 2011-10-21 01:07:16 +0000 | [diff] [blame] | 1747 | { |
cristy | c82a27b | 2011-10-21 01:07:16 +0000 | [diff] [blame] | 1748 | Image |
| 1749 | *image; |
| 1750 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 1751 | ExceptionInfo |
| 1752 | *exception; |
| 1753 | } PNGErrorInfo; |
cristy | c82a27b | 2011-10-21 01:07:16 +0000 | [diff] [blame] | 1754 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 1755 | static void MagickPNGErrorHandler(png_struct *ping,png_const_charp message) |
| 1756 | { |
| 1757 | ExceptionInfo |
| 1758 | *exception; |
cristy | c82a27b | 2011-10-21 01:07:16 +0000 | [diff] [blame] | 1759 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 1760 | Image |
| 1761 | *image; |
| 1762 | |
| 1763 | PNGErrorInfo |
| 1764 | *error_info; |
| 1765 | |
| 1766 | error_info=(PNGErrorInfo *) png_get_error_ptr(ping); |
| 1767 | image=error_info->image; |
| 1768 | exception=error_info->exception; |
| 1769 | |
| 1770 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 1771 | " libpng-%s error: %s", PNG_LIBPNG_VER_STRING,message); |
| 1772 | |
| 1773 | (void) ThrowMagickException(exception,GetMagickModule(),CoderError,message, |
| 1774 | "`%s'",image->filename); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1775 | |
glennrp | e4017e3 | 2011-01-08 17:16:09 +0000 | [diff] [blame] | 1776 | #if (PNG_LIBPNG_VER < 10500) |
glennrp | 8371ecc | 2011-02-19 19:15:38 +0000 | [diff] [blame] | 1777 | /* A warning about deprecated use of jmpbuf here is unavoidable if you |
| 1778 | * are building with libpng-1.4.x and can be ignored. |
| 1779 | */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1780 | longjmp(ping->jmpbuf,1); |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 1781 | #else |
| 1782 | png_longjmp(ping,1); |
| 1783 | #endif |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1784 | } |
| 1785 | |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 1786 | static void MagickPNGWarningHandler(png_struct *ping,png_const_charp message) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1787 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 1788 | ExceptionInfo |
| 1789 | *exception; |
| 1790 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1791 | Image |
| 1792 | *image; |
| 1793 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 1794 | PNGErrorInfo |
| 1795 | *error_info; |
| 1796 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1797 | if (LocaleCompare(message, "Missing PLTE before tRNS") == 0) |
| 1798 | png_error(ping, message); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1799 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 1800 | error_info=(PNGErrorInfo *) png_get_error_ptr(ping); |
| 1801 | image=error_info->image; |
| 1802 | exception=error_info->exception; |
| 1803 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 1804 | " libpng-%s warning: %s", PNG_LIBPNG_VER_STRING,message); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1805 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 1806 | (void) ThrowMagickException(exception,GetMagickModule(),CoderWarning, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1807 | message,"`%s'",image->filename); |
| 1808 | } |
| 1809 | |
| 1810 | #ifdef PNG_USER_MEM_SUPPORTED |
cristy | a865ccd | 2012-07-28 00:33:10 +0000 | [diff] [blame] | 1811 | #if PNG_LIBPNG_VER >= 14000 |
| 1812 | static png_voidp Magick_png_malloc(png_structp png_ptr,png_alloc_size_t size) |
| 1813 | #else |
| 1814 | static png_voidp Magick_png_malloc(png_structp png_ptr,png_size_t size) |
| 1815 | #endif |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1816 | { |
cristy | df0d90e | 2011-12-12 01:03:55 +0000 | [diff] [blame] | 1817 | (void) png_ptr; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1818 | return((png_voidp) AcquireMagickMemory((size_t) size)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1819 | } |
| 1820 | |
| 1821 | /* |
| 1822 | Free a pointer. It is removed from the list at the same time. |
| 1823 | */ |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 1824 | 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] | 1825 | { |
glennrp | 3bd393f | 2011-12-21 18:54:53 +0000 | [diff] [blame] | 1826 | (void) png_ptr; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1827 | ptr=RelinquishMagickMemory(ptr); |
| 1828 | return((png_free_ptr) NULL); |
| 1829 | } |
| 1830 | #endif |
| 1831 | |
| 1832 | #if defined(__cplusplus) || defined(c_plusplus) |
| 1833 | } |
| 1834 | #endif |
| 1835 | |
| 1836 | static int |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 1837 | Magick_png_read_raw_profile(png_struct *ping,Image *image, |
| 1838 | const ImageInfo *image_info, png_textp text,int ii,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1839 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1840 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1841 | i; |
| 1842 | |
| 1843 | register unsigned char |
| 1844 | *dp; |
| 1845 | |
| 1846 | register png_charp |
| 1847 | sp; |
| 1848 | |
| 1849 | png_uint_32 |
| 1850 | length, |
| 1851 | nibbles; |
| 1852 | |
| 1853 | StringInfo |
| 1854 | *profile; |
| 1855 | |
glennrp | 0c3e06b | 2010-11-19 13:45:02 +0000 | [diff] [blame] | 1856 | const unsigned char |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1857 | unhex[103]={0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, |
| 1858 | 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, |
| 1859 | 0,0,0,0,0,0,0,0,0,1, 2,3,4,5,6,7,8,9,0,0, |
| 1860 | 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, |
| 1861 | 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,10,11,12, |
| 1862 | 13,14,15}; |
| 1863 | |
| 1864 | sp=text[ii].text+1; |
| 1865 | /* look for newline */ |
| 1866 | while (*sp != '\n') |
| 1867 | sp++; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1868 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1869 | /* look for length */ |
| 1870 | while (*sp == '\0' || *sp == ' ' || *sp == '\n') |
| 1871 | sp++; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1872 | |
cristy | f2f2727 | 2009-12-17 14:48:46 +0000 | [diff] [blame] | 1873 | length=(png_uint_32) StringToLong(sp); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1874 | |
glennrp | 97f90e2 | 2011-02-22 05:47:58 +0000 | [diff] [blame] | 1875 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 1876 | " length: %lu",(unsigned long) length); |
| 1877 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1878 | while (*sp != ' ' && *sp != '\n') |
| 1879 | sp++; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1880 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1881 | /* allocate space */ |
| 1882 | if (length == 0) |
| 1883 | { |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 1884 | png_warning(ping,"invalid profile length"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1885 | return(MagickFalse); |
| 1886 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1887 | |
cristy | 8723e4b | 2011-09-01 13:11:19 +0000 | [diff] [blame] | 1888 | profile=BlobToStringInfo((const void *) NULL,length); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1889 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1890 | if (profile == (StringInfo *) NULL) |
| 1891 | { |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 1892 | png_warning(ping, "unable to copy profile"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1893 | return(MagickFalse); |
| 1894 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1895 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1896 | /* copy profile, skipping white space and column 1 "=" signs */ |
| 1897 | dp=GetStringInfoDatum(profile); |
| 1898 | nibbles=length*2; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1899 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1900 | for (i=0; i < (ssize_t) nibbles; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1901 | { |
| 1902 | while (*sp < '0' || (*sp > '9' && *sp < 'a') || *sp > 'f') |
| 1903 | { |
| 1904 | if (*sp == '\0') |
| 1905 | { |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 1906 | png_warning(ping, "ran out of profile data"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1907 | profile=DestroyStringInfo(profile); |
| 1908 | return(MagickFalse); |
| 1909 | } |
| 1910 | sp++; |
| 1911 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1912 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1913 | if (i%2 == 0) |
| 1914 | *dp=(unsigned char) (16*unhex[(int) *sp++]); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1915 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1916 | else |
| 1917 | (*dp++)+=unhex[(int) *sp++]; |
| 1918 | } |
| 1919 | /* |
| 1920 | We have already read "Raw profile type. |
| 1921 | */ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 1922 | (void) SetImageProfile(image,&text[ii].key[17],profile,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1923 | profile=DestroyStringInfo(profile); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1924 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1925 | if (image_info->verbose) |
| 1926 | (void) printf(" Found a generic profile, type %s\n",&text[ii].key[17]); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1927 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1928 | return MagickTrue; |
| 1929 | } |
| 1930 | |
| 1931 | #if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED) |
| 1932 | static int read_vpag_chunk_callback(png_struct *ping, png_unknown_chunkp chunk) |
| 1933 | { |
| 1934 | Image |
| 1935 | *image; |
| 1936 | |
| 1937 | |
| 1938 | /* The unknown chunk structure contains the chunk data: |
| 1939 | png_byte name[5]; |
| 1940 | png_byte *data; |
| 1941 | png_size_t size; |
| 1942 | |
| 1943 | Note that libpng has already taken care of the CRC handling. |
| 1944 | */ |
| 1945 | |
| 1946 | |
| 1947 | if (chunk->name[0] != 118 || chunk->name[1] != 112 || |
| 1948 | chunk->name[2] != 65 ||chunk-> name[3] != 103) |
| 1949 | return(0); /* Did not recognize */ |
| 1950 | |
| 1951 | /* recognized vpAg */ |
| 1952 | |
| 1953 | if (chunk->size != 9) |
| 1954 | return(-1); /* Error return */ |
| 1955 | |
| 1956 | if (chunk->data[8] != 0) |
| 1957 | return(0); /* ImageMagick requires pixel units */ |
| 1958 | |
| 1959 | image=(Image *) png_get_user_chunk_ptr(ping); |
| 1960 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1961 | image->page.width=(size_t) ((chunk->data[0] << 24) | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1962 | (chunk->data[1] << 16) | (chunk->data[2] << 8) | chunk->data[3]); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 1963 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1964 | image->page.height=(size_t) ((chunk->data[4] << 24) | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1965 | (chunk->data[5] << 16) | (chunk->data[6] << 8) | chunk->data[7]); |
| 1966 | |
| 1967 | /* Return one of the following: */ |
| 1968 | /* return(-n); chunk had an error */ |
| 1969 | /* return(0); did not recognize */ |
| 1970 | /* return(n); success */ |
| 1971 | |
| 1972 | return(1); |
| 1973 | |
| 1974 | } |
| 1975 | #endif |
| 1976 | |
| 1977 | /* |
| 1978 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1979 | % % |
| 1980 | % % |
| 1981 | % % |
| 1982 | % R e a d O n e P N G I m a g e % |
| 1983 | % % |
| 1984 | % % |
| 1985 | % % |
| 1986 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1987 | % |
| 1988 | % ReadOnePNGImage() reads a Portable Network Graphics (PNG) image file |
| 1989 | % (minus the 8-byte signature) and returns it. It allocates the memory |
| 1990 | % necessary for the new Image structure and returns a pointer to the new |
| 1991 | % image. |
| 1992 | % |
| 1993 | % The format of the ReadOnePNGImage method is: |
| 1994 | % |
| 1995 | % Image *ReadOnePNGImage(MngInfo *mng_info, const ImageInfo *image_info, |
| 1996 | % ExceptionInfo *exception) |
| 1997 | % |
| 1998 | % A description of each parameter follows: |
| 1999 | % |
| 2000 | % o mng_info: Specifies a pointer to a MngInfo structure. |
| 2001 | % |
| 2002 | % o image_info: the image info. |
| 2003 | % |
| 2004 | % o exception: return any errors or warnings in this structure. |
| 2005 | % |
| 2006 | */ |
| 2007 | static Image *ReadOnePNGImage(MngInfo *mng_info, |
| 2008 | const ImageInfo *image_info, ExceptionInfo *exception) |
| 2009 | { |
| 2010 | /* Read one PNG image */ |
| 2011 | |
glennrp | cc95c3f | 2011-04-18 16:46:48 +0000 | [diff] [blame] | 2012 | /* To do: Read the tIME chunk into the date:modify property */ |
| 2013 | /* To do: Read the tEXt/Creation Time chunk into the date:create property */ |
| 2014 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2015 | Image |
| 2016 | *image; |
| 2017 | |
| 2018 | int |
glennrp | 98b83d4 | 2012-07-23 02:50:31 +0000 | [diff] [blame] | 2019 | intent, /* "PNG Rendering intent", which is ICC intent + 1 */ |
glennrp | cb395ac | 2011-03-30 19:50:23 +0000 | [diff] [blame] | 2020 | num_raw_profiles, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2021 | num_text, |
glennrp | 4eb3931 | 2011-03-30 21:34:55 +0000 | [diff] [blame] | 2022 | num_text_total, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2023 | num_passes, |
glennrp | 913f961 | 2012-06-27 17:50:00 +0000 | [diff] [blame] | 2024 | number_colors, |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2025 | pass, |
| 2026 | ping_bit_depth, |
| 2027 | ping_color_type, |
| 2028 | ping_interlace_method, |
| 2029 | ping_compression_method, |
| 2030 | ping_filter_method, |
glennrp | 4eb3931 | 2011-03-30 21:34:55 +0000 | [diff] [blame] | 2031 | ping_num_trans, |
| 2032 | unit_type; |
| 2033 | |
| 2034 | double |
| 2035 | file_gamma; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2036 | |
| 2037 | MagickBooleanType |
cristy | 4383ec8 | 2011-01-05 15:42:32 +0000 | [diff] [blame] | 2038 | logging, |
glennrp | 98b83d4 | 2012-07-23 02:50:31 +0000 | [diff] [blame] | 2039 | ping_found_cHRM, |
| 2040 | ping_found_gAMA, |
| 2041 | ping_found_iCCP, |
| 2042 | ping_found_sRGB, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2043 | status; |
| 2044 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 2045 | PixelInfo |
| 2046 | transparent_color; |
| 2047 | |
| 2048 | PNGErrorInfo |
| 2049 | error_info; |
| 2050 | |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2051 | png_bytep |
| 2052 | ping_trans_alpha; |
| 2053 | |
| 2054 | png_color_16p |
| 2055 | ping_background, |
| 2056 | ping_trans_color; |
| 2057 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2058 | png_info |
| 2059 | *end_info, |
| 2060 | *ping_info; |
| 2061 | |
| 2062 | png_struct |
| 2063 | *ping; |
| 2064 | |
| 2065 | png_textp |
| 2066 | text; |
| 2067 | |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2068 | png_uint_32 |
| 2069 | ping_height, |
| 2070 | ping_width, |
glennrp | 4eb3931 | 2011-03-30 21:34:55 +0000 | [diff] [blame] | 2071 | x_resolution, |
| 2072 | y_resolution; |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2073 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 2074 | QuantumInfo |
| 2075 | *quantum_info; |
| 2076 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2077 | unsigned char |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 2078 | *ping_pixels; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2079 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2080 | ssize_t |
cristy | 756ae43 | 2011-11-19 02:18:25 +0000 | [diff] [blame] | 2081 | ping_rowbytes, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2082 | y; |
| 2083 | |
| 2084 | register unsigned char |
| 2085 | *p; |
| 2086 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2087 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2088 | i, |
| 2089 | x; |
| 2090 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 2091 | register Quantum |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2092 | *q; |
| 2093 | |
| 2094 | size_t |
glennrp | 39992b4 | 2010-11-14 00:03:43 +0000 | [diff] [blame] | 2095 | length, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2096 | row_offset; |
| 2097 | |
cristy | eb3b22a | 2011-03-31 20:16:11 +0000 | [diff] [blame] | 2098 | ssize_t |
| 2099 | j; |
| 2100 | |
glennrp | 629960f | 2012-05-29 19:13:52 +0000 | [diff] [blame] | 2101 | #ifdef PNG_UNKNOWN_CHUNKS_SUPPORTED |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2102 | png_byte unused_chunks[]= |
| 2103 | { |
| 2104 | 104, 73, 83, 84, (png_byte) '\0', /* hIST */ |
| 2105 | 105, 84, 88, 116, (png_byte) '\0', /* iTXt */ |
| 2106 | 112, 67, 65, 76, (png_byte) '\0', /* pCAL */ |
| 2107 | 115, 67, 65, 76, (png_byte) '\0', /* sCAL */ |
| 2108 | 115, 80, 76, 84, (png_byte) '\0', /* sPLT */ |
| 2109 | 116, 73, 77, 69, (png_byte) '\0', /* tIME */ |
glennrp | 629960f | 2012-05-29 19:13:52 +0000 | [diff] [blame] | 2110 | #ifdef PNG_APNG_SUPPORTED /* libpng was built with APNG patch; */ |
| 2111 | /* ignore the APNG chunks */ |
| 2112 | 97, 99, 84, 76, (png_byte) '\0', /* acTL */ |
| 2113 | 102, 99, 84, 76, (png_byte) '\0', /* fcTL */ |
| 2114 | 102, 100, 65, 84, (png_byte) '\0', /* fdAT */ |
| 2115 | #endif |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2116 | }; |
| 2117 | #endif |
| 2118 | |
| 2119 | logging=LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 2120 | " Enter ReadOnePNGImage()"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2121 | |
glennrp | 25c1e2b | 2010-03-25 01:39:56 +0000 | [diff] [blame] | 2122 | #if (PNG_LIBPNG_VER < 10200) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2123 | if (image_info->verbose) |
| 2124 | printf("Your PNG library (libpng-%s) is rather old.\n", |
| 2125 | PNG_LIBPNG_VER_STRING); |
| 2126 | #endif |
| 2127 | |
glennrp | 61b4c95 | 2009-11-10 20:40:41 +0000 | [diff] [blame] | 2128 | #if (PNG_LIBPNG_VER >= 10400) |
| 2129 | # ifndef PNG_TRANSFORM_GRAY_TO_RGB /* Added at libpng-1.4.0beta67 */ |
| 2130 | if (image_info->verbose) |
| 2131 | { |
| 2132 | printf("Your PNG library (libpng-%s) is an old beta version.\n", |
| 2133 | PNG_LIBPNG_VER_STRING); |
| 2134 | printf("Please update it.\n"); |
| 2135 | } |
| 2136 | # endif |
| 2137 | #endif |
| 2138 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 2139 | |
| 2140 | quantum_info = (QuantumInfo *) NULL; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2141 | image=mng_info->image; |
| 2142 | |
glennrp | a6a0663 | 2011-01-19 15:15:34 +0000 | [diff] [blame] | 2143 | if (logging != MagickFalse) |
glennrp | 98b83d4 | 2012-07-23 02:50:31 +0000 | [diff] [blame] | 2144 | { |
glennrp | a6a0663 | 2011-01-19 15:15:34 +0000 | [diff] [blame] | 2145 | (void)LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2146 | " image->matte=%d",(int) image->matte); |
| 2147 | |
glennrp | 98b83d4 | 2012-07-23 02:50:31 +0000 | [diff] [blame] | 2148 | (void)LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2149 | " image->rendering_intent=%d",(int) image->rendering_intent); |
glennrp | e88af77 | 2012-08-22 13:59:50 +0000 | [diff] [blame] | 2150 | |
| 2151 | (void)LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2152 | " image->colorspace=%d",(int) image->colorspace); |
glennrp | 98b83d4 | 2012-07-23 02:50:31 +0000 | [diff] [blame] | 2153 | } |
| 2154 | intent=Magick_RenderingIntent_to_PNG_RenderingIntent(image->rendering_intent); |
| 2155 | |
glennrp | 0e31973 | 2011-01-25 21:53:13 +0000 | [diff] [blame] | 2156 | /* Set to an out-of-range color unless tRNS chunk is present */ |
| 2157 | transparent_color.red=65537; |
| 2158 | transparent_color.green=65537; |
| 2159 | transparent_color.blue=65537; |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 2160 | transparent_color.alpha=65537; |
glennrp | 0e31973 | 2011-01-25 21:53:13 +0000 | [diff] [blame] | 2161 | |
glennrp | 913f961 | 2012-06-27 17:50:00 +0000 | [diff] [blame] | 2162 | number_colors=0; |
glennrp | cb395ac | 2011-03-30 19:50:23 +0000 | [diff] [blame] | 2163 | num_text = 0; |
glennrp | 4eb3931 | 2011-03-30 21:34:55 +0000 | [diff] [blame] | 2164 | num_text_total = 0; |
glennrp | cb395ac | 2011-03-30 19:50:23 +0000 | [diff] [blame] | 2165 | num_raw_profiles = 0; |
| 2166 | |
glennrp | 98b83d4 | 2012-07-23 02:50:31 +0000 | [diff] [blame] | 2167 | ping_found_cHRM = MagickFalse; |
| 2168 | ping_found_gAMA = MagickFalse; |
| 2169 | ping_found_iCCP = MagickFalse; |
| 2170 | ping_found_sRGB = MagickFalse; |
| 2171 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2172 | /* |
| 2173 | Allocate the PNG structures |
| 2174 | */ |
| 2175 | #ifdef PNG_USER_MEM_SUPPORTED |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 2176 | error_info.image=image; |
| 2177 | error_info.exception=exception; |
| 2178 | ping=png_create_read_struct_2(PNG_LIBPNG_VER_STRING,&error_info, |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 2179 | MagickPNGErrorHandler,MagickPNGWarningHandler, NULL, |
| 2180 | (png_malloc_ptr) Magick_png_malloc,(png_free_ptr) Magick_png_free); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2181 | #else |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 2182 | ping=png_create_read_struct(PNG_LIBPNG_VER_STRING,&error_info, |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 2183 | MagickPNGErrorHandler,MagickPNGWarningHandler); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2184 | #endif |
| 2185 | if (ping == (png_struct *) NULL) |
| 2186 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2187 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2188 | ping_info=png_create_info_struct(ping); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2189 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2190 | if (ping_info == (png_info *) NULL) |
| 2191 | { |
| 2192 | png_destroy_read_struct(&ping,(png_info **) NULL,(png_info **) NULL); |
| 2193 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
| 2194 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2195 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2196 | end_info=png_create_info_struct(ping); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2197 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2198 | if (end_info == (png_info *) NULL) |
| 2199 | { |
| 2200 | png_destroy_read_struct(&ping,&ping_info,(png_info **) NULL); |
| 2201 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
| 2202 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2203 | |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 2204 | ping_pixels=(unsigned char *) NULL; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2205 | |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2206 | if (setjmp(png_jmpbuf(ping))) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2207 | { |
| 2208 | /* |
| 2209 | PNG image is corrupt. |
| 2210 | */ |
| 2211 | png_destroy_read_struct(&ping,&ping_info,&end_info); |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 2212 | |
| 2213 | #ifdef PNG_SETJMP_NOT_THREAD_SAFE |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 2214 | UnlockSemaphoreInfo(ping_semaphore); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2215 | #endif |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 2216 | |
| 2217 | if (ping_pixels != (unsigned char *) NULL) |
| 2218 | ping_pixels=(unsigned char *) RelinquishMagickMemory(ping_pixels); |
| 2219 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2220 | if (logging != MagickFalse) |
| 2221 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2222 | " exit ReadOnePNGImage() with error."); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2223 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2224 | if (image != (Image *) NULL) |
cristy | 7b755eb | 2009-12-05 14:51:29 +0000 | [diff] [blame] | 2225 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 2226 | InheritException(exception,exception); |
cristy | 7b755eb | 2009-12-05 14:51:29 +0000 | [diff] [blame] | 2227 | image->columns=0; |
| 2228 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2229 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2230 | return(GetFirstImageInList(image)); |
| 2231 | } |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 2232 | |
| 2233 | /* { For navigation to end of SETJMP-protected block. Within this |
| 2234 | * block, use png_error() instead of Throwing an Exception, to ensure |
| 2235 | * that libpng is able to clean up, and that the semaphore is unlocked. |
| 2236 | */ |
| 2237 | |
| 2238 | #ifdef PNG_SETJMP_NOT_THREAD_SAFE |
| 2239 | LockSemaphoreInfo(ping_semaphore); |
| 2240 | #endif |
| 2241 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2242 | /* |
| 2243 | Prepare PNG for reading. |
| 2244 | */ |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2245 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2246 | mng_info->image_found++; |
| 2247 | png_set_sig_bytes(ping,8); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2248 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2249 | if (LocaleCompare(image_info->magick,"MNG") == 0) |
| 2250 | { |
| 2251 | #if defined(PNG_MNG_FEATURES_SUPPORTED) |
| 2252 | (void) png_permit_mng_features(ping,PNG_ALL_MNG_FEATURES); |
| 2253 | png_set_read_fn(ping,image,png_get_data); |
| 2254 | #else |
| 2255 | #if defined(PNG_READ_EMPTY_PLTE_SUPPORTED) |
| 2256 | png_permit_empty_plte(ping,MagickTrue); |
| 2257 | png_set_read_fn(ping,image,png_get_data); |
| 2258 | #else |
| 2259 | mng_info->image=image; |
| 2260 | mng_info->bytes_in_read_buffer=0; |
| 2261 | mng_info->found_empty_plte=MagickFalse; |
| 2262 | mng_info->have_saved_bkgd_index=MagickFalse; |
| 2263 | png_set_read_fn(ping,mng_info,mng_get_data); |
| 2264 | #endif |
| 2265 | #endif |
| 2266 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2267 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2268 | else |
| 2269 | png_set_read_fn(ping,image,png_get_data); |
| 2270 | |
| 2271 | #if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED) |
| 2272 | /* Ignore unused chunks and all unknown chunks except for vpAg */ |
| 2273 | png_set_keep_unknown_chunks(ping, 1, NULL, 0); |
| 2274 | png_set_keep_unknown_chunks(ping, 2, mng_vpAg, 1); |
| 2275 | png_set_keep_unknown_chunks(ping, 1, unused_chunks, |
| 2276 | (int)sizeof(unused_chunks)/5); |
| 2277 | /* Callback for other unknown chunks */ |
| 2278 | png_set_read_user_chunk_fn(ping, image, read_vpag_chunk_callback); |
| 2279 | #endif |
| 2280 | |
glennrp | 9bf97b6 | 2012-06-06 21:03:14 +0000 | [diff] [blame] | 2281 | #ifdef PNG_READ_CHECK_FOR_INVALID_INDEX_SUPPORTED |
| 2282 | /* Disable new libpng-1.5.10 feature */ |
| 2283 | png_set_check_for_invalid_index (ping, 0); |
| 2284 | #endif |
| 2285 | |
glennrp | 991e92a | 2010-01-28 03:09:00 +0000 | [diff] [blame] | 2286 | #if (PNG_LIBPNG_VER < 10400) |
| 2287 | # if defined(PNG_USE_PNGGCCRD) && defined(PNG_ASSEMBLER_CODE_SUPPORTED) && \ |
| 2288 | (PNG_LIBPNG_VER >= 10200) && (PNG_LIBPNG_VER < 10220) && defined(__i386__) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2289 | /* Disable thread-unsafe features of pnggccrd */ |
| 2290 | if (png_access_version_number() >= 10200) |
| 2291 | { |
| 2292 | png_uint_32 mmx_disable_mask=0; |
| 2293 | png_uint_32 asm_flags; |
| 2294 | |
| 2295 | mmx_disable_mask |= ( PNG_ASM_FLAG_MMX_READ_COMBINE_ROW \ |
| 2296 | | PNG_ASM_FLAG_MMX_READ_FILTER_SUB \ |
| 2297 | | PNG_ASM_FLAG_MMX_READ_FILTER_AVG \ |
| 2298 | | PNG_ASM_FLAG_MMX_READ_FILTER_PAETH ); |
| 2299 | asm_flags=png_get_asm_flags(ping); |
| 2300 | png_set_asm_flags(ping, asm_flags & ~mmx_disable_mask); |
| 2301 | } |
glennrp | 991e92a | 2010-01-28 03:09:00 +0000 | [diff] [blame] | 2302 | # endif |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2303 | #endif |
| 2304 | |
| 2305 | png_read_info(ping,ping_info); |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2306 | |
| 2307 | png_get_IHDR(ping,ping_info,&ping_width,&ping_height, |
| 2308 | &ping_bit_depth,&ping_color_type, |
| 2309 | &ping_interlace_method,&ping_compression_method, |
| 2310 | &ping_filter_method); |
| 2311 | |
| 2312 | (void) png_get_tRNS(ping, ping_info, &ping_trans_alpha, &ping_num_trans, |
| 2313 | &ping_trans_color); |
| 2314 | |
| 2315 | (void) png_get_bKGD(ping, ping_info, &ping_background); |
| 2316 | |
| 2317 | if (ping_bit_depth < 8) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2318 | { |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2319 | if (((int) ping_color_type == PNG_COLOR_TYPE_PALETTE)) |
| 2320 | { |
| 2321 | png_set_packing(ping); |
| 2322 | ping_bit_depth = 8; |
| 2323 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2324 | } |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2325 | |
| 2326 | image->depth=ping_bit_depth; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2327 | image->depth=GetImageQuantumDepth(image,MagickFalse); |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2328 | image->interlace=ping_interlace_method != 0 ? PNGInterlace : NoInterlace; |
glennrp | 98b83d4 | 2012-07-23 02:50:31 +0000 | [diff] [blame] | 2329 | |
cristy | 176b29a | 2012-06-21 13:35:15 +0000 | [diff] [blame] | 2330 | if (((int) ping_color_type == PNG_COLOR_TYPE_GRAY) || |
| 2331 | ((int) ping_color_type == PNG_COLOR_TYPE_GRAY_ALPHA)) |
| 2332 | { |
| 2333 | image->rendering_intent=UndefinedIntent; |
glennrp | 98b83d4 | 2012-07-23 02:50:31 +0000 | [diff] [blame] | 2334 | intent=Magick_RenderingIntent_to_PNG_RenderingIntent(UndefinedIntent); |
cristy | 176b29a | 2012-06-21 13:35:15 +0000 | [diff] [blame] | 2335 | image->gamma=1.000; |
| 2336 | (void) ResetMagickMemory(&image->chromaticity,0, |
| 2337 | sizeof(image->chromaticity)); |
| 2338 | } |
glennrp | 98b83d4 | 2012-07-23 02:50:31 +0000 | [diff] [blame] | 2339 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2340 | if (logging != MagickFalse) |
| 2341 | { |
| 2342 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 2343 | " PNG width: %.20g, height: %.20g", |
| 2344 | (double) ping_width, (double) ping_height); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2345 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2346 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2347 | " PNG color_type: %d, bit_depth: %d", |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2348 | ping_color_type, ping_bit_depth); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2349 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2350 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2351 | " PNG compression_method: %d", |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2352 | ping_compression_method); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2353 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2354 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2355 | " PNG interlace_method: %d, filter_method: %d", |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2356 | ping_interlace_method,ping_filter_method); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2357 | } |
| 2358 | |
glennrp | 98b83d4 | 2012-07-23 02:50:31 +0000 | [diff] [blame] | 2359 | if (png_get_valid(ping,ping_info,PNG_INFO_gAMA)) |
| 2360 | { |
| 2361 | ping_found_gAMA=MagickTrue; |
| 2362 | if (logging != MagickFalse) |
| 2363 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2364 | " Found PNG gAMA chunk."); |
| 2365 | } |
| 2366 | |
| 2367 | if (png_get_valid(ping,ping_info,PNG_INFO_cHRM)) |
| 2368 | { |
| 2369 | ping_found_cHRM=MagickTrue; |
| 2370 | if (logging != MagickFalse) |
| 2371 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2372 | " Found PNG cHRM chunk."); |
| 2373 | } |
| 2374 | |
| 2375 | if (png_get_valid(ping,ping_info,PNG_INFO_iCCP)) |
| 2376 | { |
| 2377 | ping_found_iCCP=MagickTrue; |
| 2378 | if (logging != MagickFalse) |
| 2379 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2380 | " Found PNG iCCP chunk."); |
| 2381 | } |
| 2382 | |
| 2383 | if (png_get_valid(ping,ping_info,PNG_INFO_sRGB)) |
| 2384 | { |
| 2385 | ping_found_sRGB=MagickTrue; |
| 2386 | if (logging != MagickFalse) |
| 2387 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2388 | " Found PNG sRGB chunk."); |
| 2389 | } |
| 2390 | |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2391 | #ifdef PNG_READ_iCCP_SUPPORTED |
| 2392 | if (png_get_valid(ping,ping_info,PNG_INFO_iCCP)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2393 | { |
| 2394 | int |
| 2395 | compression; |
| 2396 | |
glennrp | e4017e3 | 2011-01-08 17:16:09 +0000 | [diff] [blame] | 2397 | #if (PNG_LIBPNG_VER < 10500) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2398 | png_charp |
glennrp | e4017e3 | 2011-01-08 17:16:09 +0000 | [diff] [blame] | 2399 | info; |
| 2400 | #else |
| 2401 | png_bytep |
| 2402 | info; |
| 2403 | #endif |
| 2404 | |
| 2405 | png_charp |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2406 | name; |
| 2407 | |
| 2408 | png_uint_32 |
| 2409 | profile_length; |
| 2410 | |
| 2411 | (void) png_get_iCCP(ping,ping_info,&name,(int *) &compression,&info, |
| 2412 | &profile_length); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2413 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2414 | if (profile_length != 0) |
| 2415 | { |
| 2416 | StringInfo |
| 2417 | *profile; |
| 2418 | |
| 2419 | if (logging != MagickFalse) |
| 2420 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2421 | " Reading PNG iCCP chunk."); |
cristy | e8f8f38 | 2011-09-01 13:32:37 +0000 | [diff] [blame] | 2422 | profile=BlobToStringInfo(info,profile_length); |
| 2423 | if (profile == (StringInfo *) NULL) |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 2424 | { |
| 2425 | png_warning(ping, "ICC profile is NULL"); |
| 2426 | profile=DestroyStringInfo(profile); |
| 2427 | } |
| 2428 | else |
| 2429 | { |
| 2430 | (void) SetImageProfile(image,"icc",profile,exception); |
| 2431 | profile=DestroyStringInfo(profile); |
| 2432 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2433 | } |
| 2434 | } |
| 2435 | #endif |
| 2436 | #if defined(PNG_READ_sRGB_SUPPORTED) |
| 2437 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2438 | if (mng_info->have_global_srgb) |
cristy | 2ea7a8e | 2012-02-08 01:04:50 +0000 | [diff] [blame] | 2439 | { |
cristy | 2ea7a8e | 2012-02-08 01:04:50 +0000 | [diff] [blame] | 2440 | image->rendering_intent=Magick_RenderingIntent_from_PNG_RenderingIntent |
| 2441 | (mng_info->global_srgb_intent); |
| 2442 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2443 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2444 | if (png_get_sRGB(ping,ping_info,&intent)) |
| 2445 | { |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 2446 | image->rendering_intent=Magick_RenderingIntent_from_PNG_RenderingIntent |
| 2447 | (intent); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2448 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2449 | if (logging != MagickFalse) |
| 2450 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | e610a07 | 2010-08-05 17:08:46 +0000 | [diff] [blame] | 2451 | " Reading PNG sRGB chunk: rendering_intent: %d",intent); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2452 | } |
| 2453 | } |
| 2454 | #endif |
| 2455 | { |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2456 | if (!png_get_gAMA(ping,ping_info,&file_gamma)) |
| 2457 | if (mng_info->have_global_gama) |
| 2458 | png_set_gAMA(ping,ping_info,mng_info->global_gamma); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2459 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2460 | if (png_get_gAMA(ping,ping_info,&file_gamma)) |
| 2461 | { |
| 2462 | image->gamma=(float) file_gamma; |
| 2463 | if (logging != MagickFalse) |
| 2464 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2465 | " Reading PNG gAMA chunk: gamma: %f",file_gamma); |
| 2466 | } |
| 2467 | } |
glennrp | 98b83d4 | 2012-07-23 02:50:31 +0000 | [diff] [blame] | 2468 | |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2469 | if (!png_get_valid(ping,ping_info,PNG_INFO_cHRM)) |
| 2470 | { |
| 2471 | if (mng_info->have_global_chrm != MagickFalse) |
| 2472 | { |
| 2473 | (void) png_set_cHRM(ping,ping_info, |
| 2474 | mng_info->global_chrm.white_point.x, |
| 2475 | mng_info->global_chrm.white_point.y, |
| 2476 | mng_info->global_chrm.red_primary.x, |
| 2477 | mng_info->global_chrm.red_primary.y, |
| 2478 | mng_info->global_chrm.green_primary.x, |
| 2479 | mng_info->global_chrm.green_primary.y, |
| 2480 | mng_info->global_chrm.blue_primary.x, |
| 2481 | mng_info->global_chrm.blue_primary.y); |
| 2482 | } |
| 2483 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2484 | |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2485 | if (png_get_valid(ping,ping_info,PNG_INFO_cHRM)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2486 | { |
| 2487 | (void) png_get_cHRM(ping,ping_info, |
| 2488 | &image->chromaticity.white_point.x, |
| 2489 | &image->chromaticity.white_point.y, |
| 2490 | &image->chromaticity.red_primary.x, |
| 2491 | &image->chromaticity.red_primary.y, |
| 2492 | &image->chromaticity.green_primary.x, |
| 2493 | &image->chromaticity.green_primary.y, |
| 2494 | &image->chromaticity.blue_primary.x, |
| 2495 | &image->chromaticity.blue_primary.y); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2496 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2497 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2498 | |
glennrp | e610a07 | 2010-08-05 17:08:46 +0000 | [diff] [blame] | 2499 | if (image->rendering_intent != UndefinedIntent) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2500 | { |
glennrp | e610a07 | 2010-08-05 17:08:46 +0000 | [diff] [blame] | 2501 | png_set_sRGB(ping,ping_info, |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 2502 | Magick_RenderingIntent_to_PNG_RenderingIntent |
| 2503 | (image->rendering_intent)); |
cristy | da7803d | 2012-05-03 01:22:45 +0000 | [diff] [blame] | 2504 | png_set_gAMA(ping,ping_info,1.000f/2.200f); |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2505 | png_set_cHRM(ping,ping_info, |
| 2506 | 0.6400f, 0.3300f, 0.3000f, 0.6000f, |
| 2507 | 0.1500f, 0.0600f, 0.3127f, 0.3290f); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2508 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2509 | #if defined(PNG_oFFs_SUPPORTED) |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2510 | if (png_get_valid(ping,ping_info,PNG_INFO_oFFs)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2511 | { |
cristy | 905ef80 | 2011-02-23 00:29:18 +0000 | [diff] [blame] | 2512 | image->page.x=(ssize_t) png_get_x_offset_pixels(ping, ping_info); |
| 2513 | image->page.y=(ssize_t) png_get_y_offset_pixels(ping, ping_info); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2514 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2515 | if (logging != MagickFalse) |
| 2516 | if (image->page.x || image->page.y) |
| 2517 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 2518 | " Reading PNG oFFs chunk: x: %.20g, y: %.20g.",(double) |
| 2519 | image->page.x,(double) image->page.y); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2520 | } |
| 2521 | #endif |
| 2522 | #if defined(PNG_pHYs_SUPPORTED) |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2523 | if (!png_get_valid(ping,ping_info,PNG_INFO_pHYs)) |
| 2524 | { |
| 2525 | if (mng_info->have_global_phys) |
| 2526 | { |
| 2527 | png_set_pHYs(ping,ping_info, |
| 2528 | mng_info->global_x_pixels_per_unit, |
| 2529 | mng_info->global_y_pixels_per_unit, |
| 2530 | mng_info->global_phys_unit_type); |
| 2531 | } |
| 2532 | } |
| 2533 | |
| 2534 | if (png_get_valid(ping,ping_info,PNG_INFO_pHYs)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2535 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2536 | /* |
| 2537 | Set image resolution. |
| 2538 | */ |
| 2539 | (void) png_get_pHYs(ping,ping_info,&x_resolution,&y_resolution, |
cristy | 0881b52 | 2010-04-24 23:45:19 +0000 | [diff] [blame] | 2540 | &unit_type); |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 2541 | image->resolution.x=(double) x_resolution; |
| 2542 | image->resolution.y=(double) y_resolution; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2543 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2544 | if (unit_type == PNG_RESOLUTION_METER) |
| 2545 | { |
| 2546 | image->units=PixelsPerCentimeterResolution; |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 2547 | image->resolution.x=(double) x_resolution/100.0; |
| 2548 | image->resolution.y=(double) y_resolution/100.0; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2549 | } |
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 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 2553 | " Reading PNG pHYs chunk: xres: %.20g, yres: %.20g, units: %d.", |
| 2554 | (double) x_resolution,(double) y_resolution,unit_type); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2555 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2556 | #endif |
glennrp | 823b55c | 2011-03-14 18:46:46 +0000 | [diff] [blame] | 2557 | |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2558 | if (png_get_valid(ping,ping_info,PNG_INFO_PLTE)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2559 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2560 | png_colorp |
| 2561 | palette; |
| 2562 | |
| 2563 | (void) png_get_PLTE(ping,ping_info,&palette,&number_colors); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2564 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2565 | if ((number_colors == 0) && |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2566 | ((int) ping_color_type == PNG_COLOR_TYPE_PALETTE)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2567 | { |
| 2568 | if (mng_info->global_plte_length) |
| 2569 | { |
| 2570 | png_set_PLTE(ping,ping_info,mng_info->global_plte, |
| 2571 | (int) mng_info->global_plte_length); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2572 | |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2573 | if (!png_get_valid(ping,ping_info,PNG_INFO_tRNS)) |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 2574 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2575 | if (mng_info->global_trns_length) |
| 2576 | { |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 2577 | png_warning(ping, |
| 2578 | "global tRNS has more entries than global PLTE"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2579 | } |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 2580 | else |
| 2581 | { |
| 2582 | png_set_tRNS(ping,ping_info,mng_info->global_trns, |
| 2583 | (int) mng_info->global_trns_length,NULL); |
| 2584 | } |
| 2585 | } |
glennrp | bfd9e61 | 2011-04-22 14:02:20 +0000 | [diff] [blame] | 2586 | #ifdef PNG_READ_bKGD_SUPPORTED |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2587 | if ( |
| 2588 | #ifndef PNG_READ_EMPTY_PLTE_SUPPORTED |
| 2589 | mng_info->have_saved_bkgd_index || |
| 2590 | #endif |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2591 | png_get_valid(ping,ping_info,PNG_INFO_bKGD)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2592 | { |
| 2593 | png_color_16 |
| 2594 | background; |
| 2595 | |
| 2596 | #ifndef PNG_READ_EMPTY_PLTE_SUPPORTED |
| 2597 | if (mng_info->have_saved_bkgd_index) |
| 2598 | background.index=mng_info->saved_bkgd_index; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2599 | #endif |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2600 | if (png_get_valid(ping, ping_info, PNG_INFO_bKGD)) |
| 2601 | background.index=ping_background->index; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2602 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2603 | background.red=(png_uint_16) |
| 2604 | mng_info->global_plte[background.index].red; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2605 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2606 | background.green=(png_uint_16) |
| 2607 | mng_info->global_plte[background.index].green; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2608 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2609 | background.blue=(png_uint_16) |
| 2610 | mng_info->global_plte[background.index].blue; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2611 | |
glennrp | c6c391a | 2011-04-27 02:23:56 +0000 | [diff] [blame] | 2612 | background.gray=(png_uint_16) |
| 2613 | mng_info->global_plte[background.index].green; |
| 2614 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2615 | png_set_bKGD(ping,ping_info,&background); |
| 2616 | } |
| 2617 | #endif |
| 2618 | } |
| 2619 | else |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 2620 | png_error(ping,"No global PLTE in file"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2621 | } |
| 2622 | } |
| 2623 | |
glennrp | bfd9e61 | 2011-04-22 14:02:20 +0000 | [diff] [blame] | 2624 | #ifdef PNG_READ_bKGD_SUPPORTED |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2625 | if (mng_info->have_global_bkgd && |
| 2626 | (!png_get_valid(ping,ping_info,PNG_INFO_bKGD))) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2627 | image->background_color=mng_info->mng_global_bkgd; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2628 | |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2629 | if (png_get_valid(ping,ping_info,PNG_INFO_bKGD)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2630 | { |
glennrp | bfd9e61 | 2011-04-22 14:02:20 +0000 | [diff] [blame] | 2631 | unsigned int |
| 2632 | bkgd_scale; |
| 2633 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2634 | /* |
| 2635 | Set image background color. |
| 2636 | */ |
| 2637 | if (logging != MagickFalse) |
| 2638 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2639 | " Reading PNG bKGD chunk."); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2640 | |
glennrp | bfd9e61 | 2011-04-22 14:02:20 +0000 | [diff] [blame] | 2641 | /* Scale background components to 16-bit, then scale |
| 2642 | * to quantum depth |
| 2643 | */ |
| 2644 | if (logging != MagickFalse) |
| 2645 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2646 | " raw ping_background=(%d,%d,%d).",ping_background->red, |
| 2647 | ping_background->green,ping_background->blue); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2648 | |
glennrp | bfd9e61 | 2011-04-22 14:02:20 +0000 | [diff] [blame] | 2649 | bkgd_scale = 1; |
glennrp | 2cbb448 | 2010-06-02 04:37:24 +0000 | [diff] [blame] | 2650 | |
glennrp | bfd9e61 | 2011-04-22 14:02:20 +0000 | [diff] [blame] | 2651 | if (ping_bit_depth == 1) |
| 2652 | bkgd_scale = 255; |
glennrp | 2cbb448 | 2010-06-02 04:37:24 +0000 | [diff] [blame] | 2653 | |
glennrp | bfd9e61 | 2011-04-22 14:02:20 +0000 | [diff] [blame] | 2654 | else if (ping_bit_depth == 2) |
| 2655 | bkgd_scale = 85; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2656 | |
glennrp | bfd9e61 | 2011-04-22 14:02:20 +0000 | [diff] [blame] | 2657 | else if (ping_bit_depth == 4) |
| 2658 | bkgd_scale = 17; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2659 | |
glennrp | bfd9e61 | 2011-04-22 14:02:20 +0000 | [diff] [blame] | 2660 | if (ping_bit_depth <= 8) |
| 2661 | bkgd_scale *= 257; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2662 | |
glennrp | bfd9e61 | 2011-04-22 14:02:20 +0000 | [diff] [blame] | 2663 | ping_background->red *= bkgd_scale; |
| 2664 | ping_background->green *= bkgd_scale; |
| 2665 | ping_background->blue *= bkgd_scale; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2666 | |
glennrp | bfd9e61 | 2011-04-22 14:02:20 +0000 | [diff] [blame] | 2667 | if (logging != MagickFalse) |
| 2668 | { |
glennrp | 2cbb448 | 2010-06-02 04:37:24 +0000 | [diff] [blame] | 2669 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2670 | " bkgd_scale=%d.",bkgd_scale); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2671 | |
glennrp | 2cbb448 | 2010-06-02 04:37:24 +0000 | [diff] [blame] | 2672 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2673 | " ping_background=(%d,%d,%d).",ping_background->red, |
| 2674 | ping_background->green,ping_background->blue); |
glennrp | bfd9e61 | 2011-04-22 14:02:20 +0000 | [diff] [blame] | 2675 | } |
glennrp | 2cbb448 | 2010-06-02 04:37:24 +0000 | [diff] [blame] | 2676 | |
glennrp | bfd9e61 | 2011-04-22 14:02:20 +0000 | [diff] [blame] | 2677 | image->background_color.red= |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2678 | ScaleShortToQuantum(ping_background->red); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2679 | |
glennrp | bfd9e61 | 2011-04-22 14:02:20 +0000 | [diff] [blame] | 2680 | image->background_color.green= |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2681 | ScaleShortToQuantum(ping_background->green); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2682 | |
glennrp | bfd9e61 | 2011-04-22 14:02:20 +0000 | [diff] [blame] | 2683 | image->background_color.blue= |
| 2684 | ScaleShortToQuantum(ping_background->blue); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2685 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 2686 | image->background_color.alpha=OpaqueAlpha; |
glennrp | 2cbb448 | 2010-06-02 04:37:24 +0000 | [diff] [blame] | 2687 | |
glennrp | bfd9e61 | 2011-04-22 14:02:20 +0000 | [diff] [blame] | 2688 | if (logging != MagickFalse) |
| 2689 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2690 | " image->background_color=(%.20g,%.20g,%.20g).", |
| 2691 | (double) image->background_color.red, |
| 2692 | (double) image->background_color.green, |
| 2693 | (double) image->background_color.blue); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2694 | } |
glennrp | bfd9e61 | 2011-04-22 14:02:20 +0000 | [diff] [blame] | 2695 | #endif /* PNG_READ_bKGD_SUPPORTED */ |
glennrp | a6a0663 | 2011-01-19 15:15:34 +0000 | [diff] [blame] | 2696 | |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2697 | if (png_get_valid(ping,ping_info,PNG_INFO_tRNS)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2698 | { |
| 2699 | /* |
glennrp | a6a0663 | 2011-01-19 15:15:34 +0000 | [diff] [blame] | 2700 | Image has a tRNS chunk. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2701 | */ |
| 2702 | int |
| 2703 | max_sample; |
| 2704 | |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 2705 | size_t |
| 2706 | one=1; |
| 2707 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2708 | if (logging != MagickFalse) |
| 2709 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2710 | " Reading PNG tRNS chunk."); |
| 2711 | |
cristy | f9cca6a | 2010-06-04 23:49:28 +0000 | [diff] [blame] | 2712 | max_sample = (int) ((one << ping_bit_depth) - 1); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2713 | |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2714 | if ((ping_color_type == PNG_COLOR_TYPE_GRAY && |
| 2715 | (int)ping_trans_color->gray > max_sample) || |
| 2716 | (ping_color_type == PNG_COLOR_TYPE_RGB && |
| 2717 | ((int)ping_trans_color->red > max_sample || |
| 2718 | (int)ping_trans_color->green > max_sample || |
| 2719 | (int)ping_trans_color->blue > max_sample))) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2720 | { |
| 2721 | if (logging != MagickFalse) |
| 2722 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2723 | " Ignoring PNG tRNS chunk with out-of-range sample."); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2724 | png_free_data(ping, ping_info, PNG_FREE_TRNS, 0); |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2725 | png_set_invalid(ping,ping_info,PNG_INFO_tRNS); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2726 | image->matte=MagickFalse; |
| 2727 | } |
| 2728 | else |
| 2729 | { |
glennrp | a6a0663 | 2011-01-19 15:15:34 +0000 | [diff] [blame] | 2730 | int |
| 2731 | scale_to_short; |
| 2732 | |
| 2733 | scale_to_short = 65535L/((1UL << ping_bit_depth)-1); |
| 2734 | |
| 2735 | /* Scale transparent_color to short */ |
| 2736 | transparent_color.red= scale_to_short*ping_trans_color->red; |
| 2737 | transparent_color.green= scale_to_short*ping_trans_color->green; |
| 2738 | transparent_color.blue= scale_to_short*ping_trans_color->blue; |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 2739 | transparent_color.alpha= scale_to_short*ping_trans_color->gray; |
glennrp | 05eb4a9 | 2010-07-08 02:21:09 +0000 | [diff] [blame] | 2740 | |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2741 | if (ping_color_type == PNG_COLOR_TYPE_GRAY) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2742 | { |
glennrp | 0f11198 | 2010-07-07 20:18:33 +0000 | [diff] [blame] | 2743 | if (logging != MagickFalse) |
| 2744 | { |
| 2745 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2746 | " Raw tRNS graylevel is %d.",ping_trans_color->gray); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2747 | |
glennrp | 0f11198 | 2010-07-07 20:18:33 +0000 | [diff] [blame] | 2748 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 2749 | " scaled graylevel is %.20g.",transparent_color.alpha); |
glennrp | 0f11198 | 2010-07-07 20:18:33 +0000 | [diff] [blame] | 2750 | } |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 2751 | transparent_color.red=transparent_color.alpha; |
| 2752 | transparent_color.green=transparent_color.alpha; |
| 2753 | transparent_color.blue=transparent_color.alpha; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2754 | } |
| 2755 | } |
| 2756 | } |
| 2757 | #if defined(PNG_READ_sBIT_SUPPORTED) |
| 2758 | if (mng_info->have_global_sbit) |
| 2759 | { |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2760 | if (!png_get_valid(ping,ping_info,PNG_INFO_sBIT)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2761 | png_set_sBIT(ping,ping_info,&mng_info->global_sbit); |
| 2762 | } |
| 2763 | #endif |
| 2764 | num_passes=png_set_interlace_handling(ping); |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2765 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2766 | png_read_update_info(ping,ping_info); |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2767 | |
| 2768 | ping_rowbytes=png_get_rowbytes(ping,ping_info); |
| 2769 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2770 | /* |
| 2771 | Initialize image structure. |
| 2772 | */ |
| 2773 | mng_info->image_box.left=0; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2774 | mng_info->image_box.right=(ssize_t) ping_width; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2775 | mng_info->image_box.top=0; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2776 | mng_info->image_box.bottom=(ssize_t) ping_height; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2777 | if (mng_info->mng_type == 0) |
| 2778 | { |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2779 | mng_info->mng_width=ping_width; |
| 2780 | mng_info->mng_height=ping_height; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2781 | mng_info->frame=mng_info->image_box; |
| 2782 | mng_info->clip=mng_info->image_box; |
| 2783 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2784 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2785 | else |
| 2786 | { |
| 2787 | image->page.y=mng_info->y_off[mng_info->object_id]; |
| 2788 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2789 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2790 | image->compression=ZipCompression; |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2791 | image->columns=ping_width; |
| 2792 | image->rows=ping_height; |
glennrp | a6c5d34 | 2012-05-14 13:21:17 +0000 | [diff] [blame] | 2793 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 2794 | if (((int) ping_color_type == PNG_COLOR_TYPE_GRAY) || |
| 2795 | ((int) ping_color_type == PNG_COLOR_TYPE_GRAY_ALPHA)) |
glennrp | 8d0bca5 | 2012-05-17 02:33:23 +0000 | [diff] [blame] | 2796 | { |
glennrp | e88af77 | 2012-08-22 13:59:50 +0000 | [diff] [blame] | 2797 | if ((!png_get_valid(ping,ping_info,PNG_INFO_gAMA) || |
| 2798 | image->gamma == 1.0) && |
glennrp | 8d0bca5 | 2012-05-17 02:33:23 +0000 | [diff] [blame] | 2799 | !png_get_valid(ping,ping_info,PNG_INFO_cHRM) && |
| 2800 | !png_get_valid(ping,ping_info,PNG_INFO_sRGB)) |
| 2801 | { |
| 2802 | /* Set image->gamma to 1.0, image->rendering_intent to Undefined, |
glennrp | e88af77 | 2012-08-22 13:59:50 +0000 | [diff] [blame] | 2803 | * image->colorspace to GRAY, and reset image->chromaticity. |
glennrp | 8d0bca5 | 2012-05-17 02:33:23 +0000 | [diff] [blame] | 2804 | */ |
| 2805 | SetImageColorspace(image,GRAYColorspace,exception); |
| 2806 | } |
glennrp | 8d0bca5 | 2012-05-17 02:33:23 +0000 | [diff] [blame] | 2807 | } |
glennrp | e88af77 | 2012-08-22 13:59:50 +0000 | [diff] [blame] | 2808 | |
| 2809 | (void)LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2810 | " image->colorspace=%d",(int) image->colorspace); |
glennrp | a6c5d34 | 2012-05-14 13:21:17 +0000 | [diff] [blame] | 2811 | |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2812 | if (((int) ping_color_type == PNG_COLOR_TYPE_PALETTE) || |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2813 | ((int) ping_color_type == PNG_COLOR_TYPE_GRAY)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2814 | { |
cristy | befe4d2 | 2010-06-07 01:18:58 +0000 | [diff] [blame] | 2815 | size_t |
| 2816 | one; |
| 2817 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2818 | image->storage_class=PseudoClass; |
cristy | befe4d2 | 2010-06-07 01:18:58 +0000 | [diff] [blame] | 2819 | one=1; |
| 2820 | image->colors=one << ping_bit_depth; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2821 | #if (MAGICKCORE_QUANTUM_DEPTH == 8) |
| 2822 | if (image->colors > 256) |
glennrp | 67b9c1a | 2011-04-22 18:47:36 +0000 | [diff] [blame] | 2823 | image->colors=256; |
| 2824 | #else |
| 2825 | if (image->colors > 65536L) |
| 2826 | image->colors=65536L; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2827 | #endif |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2828 | if ((int) ping_color_type == PNG_COLOR_TYPE_PALETTE) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2829 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2830 | png_colorp |
| 2831 | palette; |
| 2832 | |
| 2833 | (void) png_get_PLTE(ping,ping_info,&palette,&number_colors); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2834 | image->colors=(size_t) number_colors; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2835 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2836 | if (logging != MagickFalse) |
| 2837 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2838 | " Reading PNG PLTE chunk: number_colors: %d.",number_colors); |
| 2839 | } |
| 2840 | } |
| 2841 | |
| 2842 | if (image->storage_class == PseudoClass) |
| 2843 | { |
| 2844 | /* |
| 2845 | Initialize image colormap. |
| 2846 | */ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 2847 | if (AcquireImageColormap(image,image->colors,exception) == MagickFalse) |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 2848 | png_error(ping,"Memory allocation failed"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2849 | |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2850 | if ((int) ping_color_type == PNG_COLOR_TYPE_PALETTE) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2851 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2852 | png_colorp |
| 2853 | palette; |
| 2854 | |
| 2855 | (void) png_get_PLTE(ping,ping_info,&palette,&number_colors); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2856 | |
glennrp | 6af6cf1 | 2011-04-22 13:05:16 +0000 | [diff] [blame] | 2857 | for (i=0; i < (ssize_t) number_colors; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2858 | { |
| 2859 | image->colormap[i].red=ScaleCharToQuantum(palette[i].red); |
| 2860 | image->colormap[i].green=ScaleCharToQuantum(palette[i].green); |
| 2861 | image->colormap[i].blue=ScaleCharToQuantum(palette[i].blue); |
| 2862 | } |
glennrp | 6af6cf1 | 2011-04-22 13:05:16 +0000 | [diff] [blame] | 2863 | |
glennrp | 67b9c1a | 2011-04-22 18:47:36 +0000 | [diff] [blame] | 2864 | for ( ; i < (ssize_t) image->colors; i++) |
glennrp | 6af6cf1 | 2011-04-22 13:05:16 +0000 | [diff] [blame] | 2865 | { |
| 2866 | image->colormap[i].red=0; |
| 2867 | image->colormap[i].green=0; |
| 2868 | image->colormap[i].blue=0; |
| 2869 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2870 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2871 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2872 | else |
| 2873 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2874 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2875 | scale; |
| 2876 | |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 2877 | scale=(QuantumRange/((1UL << ping_bit_depth)-1)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2878 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2879 | if (scale < 1) |
| 2880 | scale=1; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2881 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2882 | for (i=0; i < (ssize_t) image->colors; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2883 | { |
| 2884 | image->colormap[i].red=(Quantum) (i*scale); |
| 2885 | image->colormap[i].green=(Quantum) (i*scale); |
| 2886 | image->colormap[i].blue=(Quantum) (i*scale); |
| 2887 | } |
| 2888 | } |
| 2889 | } |
glennrp | 147bc91 | 2011-03-30 18:47:21 +0000 | [diff] [blame] | 2890 | |
glennrp | cb395ac | 2011-03-30 19:50:23 +0000 | [diff] [blame] | 2891 | /* Set some properties for reporting by "identify" */ |
| 2892 | { |
glennrp | 147bc91 | 2011-03-30 18:47:21 +0000 | [diff] [blame] | 2893 | char |
| 2894 | msg[MaxTextExtent]; |
| 2895 | |
| 2896 | /* encode ping_width, ping_height, ping_bit_depth, ping_color_type, |
| 2897 | ping_interlace_method in value */ |
| 2898 | |
cristy | 3b6fd2e | 2011-05-20 12:53:50 +0000 | [diff] [blame] | 2899 | (void) FormatLocaleString(msg,MaxTextExtent, |
glennrp | 7cdb11c | 2011-03-31 18:17:25 +0000 | [diff] [blame] | 2900 | "%d, %d",(int) ping_width, (int) ping_height); |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 2901 | (void) SetImageProperty(image,"png:IHDR.width,height ",msg,exception); |
glennrp | 147bc91 | 2011-03-30 18:47:21 +0000 | [diff] [blame] | 2902 | |
cristy | 3b6fd2e | 2011-05-20 12:53:50 +0000 | [diff] [blame] | 2903 | (void) FormatLocaleString(msg,MaxTextExtent,"%d",(int) ping_bit_depth); |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 2904 | (void) SetImageProperty(image,"png:IHDR.bit_depth ",msg,exception); |
glennrp | 147bc91 | 2011-03-30 18:47:21 +0000 | [diff] [blame] | 2905 | |
glennrp | 5dff435 | 2012-06-06 22:12:04 +0000 | [diff] [blame] | 2906 | (void) FormatLocaleString(msg,MaxTextExtent,"%d (%s)", |
| 2907 | (int) ping_color_type, |
| 2908 | Magick_ColorType_from_PNG_ColorType((int)ping_color_type)); |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 2909 | (void) SetImageProperty(image,"png:IHDR.color_type ",msg,exception); |
glennrp | 147bc91 | 2011-03-30 18:47:21 +0000 | [diff] [blame] | 2910 | |
glennrp | 913f961 | 2012-06-27 17:50:00 +0000 | [diff] [blame] | 2911 | if (ping_interlace_method == 0) |
| 2912 | { |
| 2913 | (void) FormatLocaleString(msg,MaxTextExtent,"%d (Not interlaced)", |
| 2914 | (int) ping_interlace_method); |
| 2915 | } |
| 2916 | else if (ping_interlace_method == 1) |
| 2917 | { |
| 2918 | (void) FormatLocaleString(msg,MaxTextExtent,"%d (Adam7 method)", |
| 2919 | (int) ping_interlace_method); |
| 2920 | } |
| 2921 | else |
| 2922 | { |
| 2923 | (void) FormatLocaleString(msg,MaxTextExtent,"%d (Unknown method)", |
| 2924 | (int) ping_interlace_method); |
| 2925 | } |
| 2926 | (void) SetImageProperty(image,"png:IHDR.interlace_method",msg,exception); |
| 2927 | |
| 2928 | if (number_colors != 0) |
| 2929 | { |
| 2930 | (void) FormatLocaleString(msg,MaxTextExtent,"%d", |
| 2931 | (int) number_colors); |
| 2932 | (void) SetImageProperty(image,"png:PLTE.number_colors ",msg, |
| 2933 | exception); |
| 2934 | } |
glennrp | cb395ac | 2011-03-30 19:50:23 +0000 | [diff] [blame] | 2935 | } |
glennrp | 147bc91 | 2011-03-30 18:47:21 +0000 | [diff] [blame] | 2936 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2937 | /* |
| 2938 | Read image scanlines. |
| 2939 | */ |
| 2940 | if (image->delay != 0) |
| 2941 | mng_info->scenes_found++; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2942 | |
glennrp | 0ca69b1 | 2010-07-26 01:57:52 +0000 | [diff] [blame] | 2943 | if ((mng_info->mng_type == 0 && (image->ping != MagickFalse)) || ( |
glennrp | 347e40f | 2010-06-06 11:27:30 +0000 | [diff] [blame] | 2944 | (image_info->number_scenes != 0) && (mng_info->scenes_found > (ssize_t) |
| 2945 | (image_info->first_scene+image_info->number_scenes)))) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2946 | { |
glennrp | 1b888c4 | 2012-03-02 15:18:14 +0000 | [diff] [blame] | 2947 | /* This happens later in non-ping decodes */ |
| 2948 | if (png_get_valid(ping,ping_info,PNG_INFO_tRNS)) |
| 2949 | image->storage_class=DirectClass; |
| 2950 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2951 | if (logging != MagickFalse) |
| 2952 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 2953 | " Skipping PNG image data for scene %.20g",(double) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2954 | mng_info->scenes_found-1); |
| 2955 | png_destroy_read_struct(&ping,&ping_info,&end_info); |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 2956 | |
| 2957 | #ifdef PNG_SETJMP_NOT_THREAD_SAFE |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 2958 | UnlockSemaphoreInfo(ping_semaphore); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2959 | #endif |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 2960 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2961 | if (logging != MagickFalse) |
| 2962 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2963 | " exit ReadOnePNGImage()."); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2964 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2965 | return(image); |
| 2966 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2967 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2968 | if (logging != MagickFalse) |
| 2969 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2970 | " Reading PNG IDAT chunk(s)"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2971 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2972 | if (num_passes > 1) |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 2973 | ping_pixels=(unsigned char *) AcquireQuantumMemory(image->rows, |
| 2974 | ping_rowbytes*sizeof(*ping_pixels)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2975 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2976 | else |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 2977 | ping_pixels=(unsigned char *) AcquireQuantumMemory(ping_rowbytes, |
| 2978 | sizeof(*ping_pixels)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2979 | |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 2980 | if (ping_pixels == (unsigned char *) NULL) |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 2981 | png_error(ping,"Memory allocation failed"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2982 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2983 | if (logging != MagickFalse) |
| 2984 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 2985 | " Converting PNG pixels to pixel packets"); |
| 2986 | /* |
| 2987 | Convert PNG pixels to pixel packets. |
| 2988 | */ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 2989 | quantum_info=AcquireQuantumInfo(image_info,image); |
| 2990 | |
| 2991 | if (quantum_info == (QuantumInfo *) NULL) |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 2992 | png_error(ping,"Failed to allocate quantum_info"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 2993 | |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 2994 | { |
| 2995 | |
| 2996 | MagickBooleanType |
| 2997 | found_transparent_pixel; |
| 2998 | |
| 2999 | found_transparent_pixel=MagickFalse; |
| 3000 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3001 | if (image->storage_class == DirectClass) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3002 | { |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 3003 | for (pass=0; pass < num_passes; pass++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3004 | { |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 3005 | /* |
| 3006 | Convert image to DirectClass pixel packets. |
| 3007 | */ |
glennrp | 67b9c1a | 2011-04-22 18:47:36 +0000 | [diff] [blame] | 3008 | #if (MAGICKCORE_QUANTUM_DEPTH == 8) |
| 3009 | int |
| 3010 | depth; |
| 3011 | |
| 3012 | depth=(ssize_t) ping_bit_depth; |
| 3013 | #endif |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 3014 | image->matte=(((int) ping_color_type == PNG_COLOR_TYPE_RGB_ALPHA) || |
| 3015 | ((int) ping_color_type == PNG_COLOR_TYPE_GRAY_ALPHA) || |
| 3016 | (png_get_valid(ping,ping_info,PNG_INFO_tRNS))) ? |
| 3017 | MagickTrue : MagickFalse; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3018 | |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 3019 | for (y=0; y < (ssize_t) image->rows; y++) |
| 3020 | { |
| 3021 | if (num_passes > 1) |
| 3022 | row_offset=ping_rowbytes*y; |
| 3023 | |
| 3024 | else |
| 3025 | row_offset=0; |
| 3026 | |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 3027 | png_read_row(ping,ping_pixels+row_offset,NULL); |
cristy | 862a33c | 2012-05-17 22:49:37 +0000 | [diff] [blame] | 3028 | q=GetAuthenticPixels(image,0,y,image->columns,1,exception); |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 3029 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3030 | if (q == (Quantum *) NULL) |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 3031 | break; |
| 3032 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3033 | if ((int) ping_color_type == PNG_COLOR_TYPE_GRAY) |
| 3034 | (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info, |
| 3035 | GrayQuantum,ping_pixels+row_offset,exception); |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 3036 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3037 | else if ((int) ping_color_type == PNG_COLOR_TYPE_GRAY_ALPHA) |
| 3038 | (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info, |
| 3039 | GrayAlphaQuantum,ping_pixels+row_offset,exception); |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 3040 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3041 | else if ((int) ping_color_type == PNG_COLOR_TYPE_RGB_ALPHA) |
| 3042 | (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info, |
| 3043 | RGBAQuantum,ping_pixels+row_offset,exception); |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 3044 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3045 | else if ((int) ping_color_type == PNG_COLOR_TYPE_PALETTE) |
| 3046 | (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info, |
| 3047 | IndexQuantum,ping_pixels+row_offset,exception); |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 3048 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3049 | else /* ping_color_type == PNG_COLOR_TYPE_RGB */ |
| 3050 | (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info, |
| 3051 | RGBQuantum,ping_pixels+row_offset,exception); |
glennrp | 3faa9a3 | 2011-04-23 14:00:25 +0000 | [diff] [blame] | 3052 | |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 3053 | if (found_transparent_pixel == MagickFalse) |
| 3054 | { |
| 3055 | /* Is there a transparent pixel in the row? */ |
glennrp | a6a0663 | 2011-01-19 15:15:34 +0000 | [diff] [blame] | 3056 | if (y== 0 && logging != MagickFalse) |
| 3057 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 3058 | " Looking for cheap transparent pixel"); |
| 3059 | |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 3060 | for (x=(ssize_t) image->columns-1; x >= 0; x--) |
| 3061 | { |
glennrp | 5aa37f6 | 2011-01-02 03:07:57 +0000 | [diff] [blame] | 3062 | if ((ping_color_type == PNG_COLOR_TYPE_RGBA || |
| 3063 | ping_color_type == PNG_COLOR_TYPE_GRAY_ALPHA) && |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3064 | (GetPixelAlpha(image,q) != OpaqueAlpha)) |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 3065 | { |
glennrp | a6a0663 | 2011-01-19 15:15:34 +0000 | [diff] [blame] | 3066 | if (logging != MagickFalse) |
| 3067 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 3068 | " ...got one."); |
| 3069 | |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 3070 | found_transparent_pixel = MagickTrue; |
| 3071 | break; |
| 3072 | } |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 3073 | if ((ping_color_type == PNG_COLOR_TYPE_RGB || |
| 3074 | ping_color_type == PNG_COLOR_TYPE_GRAY) && |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3075 | (ScaleQuantumToShort(GetPixelRed(image,q)) == |
| 3076 | transparent_color.red && |
| 3077 | ScaleQuantumToShort(GetPixelGreen(image,q)) == |
| 3078 | transparent_color.green && |
| 3079 | ScaleQuantumToShort(GetPixelBlue(image,q)) == |
| 3080 | transparent_color.blue)) |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 3081 | { |
glennrp | a6a0663 | 2011-01-19 15:15:34 +0000 | [diff] [blame] | 3082 | if (logging != MagickFalse) |
| 3083 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 3084 | " ...got one."); |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 3085 | found_transparent_pixel = MagickTrue; |
| 3086 | break; |
| 3087 | } |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3088 | q+=GetPixelChannels(image); |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 3089 | } |
| 3090 | } |
| 3091 | |
| 3092 | if ((image->previous == (Image *) NULL) && (num_passes == 1)) |
| 3093 | { |
| 3094 | status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y, |
| 3095 | image->rows); |
| 3096 | |
| 3097 | if (status == MagickFalse) |
| 3098 | break; |
| 3099 | } |
| 3100 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 3101 | break; |
| 3102 | } |
| 3103 | |
| 3104 | if ((image->previous == (Image *) NULL) && (num_passes != 1)) |
| 3105 | { |
| 3106 | status=SetImageProgress(image,LoadImageTag,pass,num_passes); |
cristy | 7a287bf | 2010-02-14 02:18:09 +0000 | [diff] [blame] | 3107 | if (status == MagickFalse) |
| 3108 | break; |
| 3109 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3110 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3111 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3112 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3113 | else /* image->storage_class != DirectClass */ |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 3114 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3115 | for (pass=0; pass < num_passes; pass++) |
| 3116 | { |
| 3117 | Quantum |
| 3118 | *quantum_scanline; |
| 3119 | |
| 3120 | register Quantum |
| 3121 | *r; |
| 3122 | |
| 3123 | /* |
| 3124 | Convert grayscale image to PseudoClass pixel packets. |
| 3125 | */ |
glennrp | c17d96f | 2011-06-27 01:20:11 +0000 | [diff] [blame] | 3126 | if (logging != MagickFalse) |
| 3127 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 3128 | " Converting grayscale pixels to pixel packets"); |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3129 | |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 3130 | image->matte=ping_color_type == PNG_COLOR_TYPE_GRAY_ALPHA ? |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3131 | MagickTrue : MagickFalse; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3132 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3133 | quantum_scanline=(Quantum *) AcquireQuantumMemory(image->columns, |
| 3134 | (image->matte ? 2 : 1)*sizeof(*quantum_scanline)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3135 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3136 | if (quantum_scanline == (Quantum *) NULL) |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 3137 | png_error(ping,"Memory allocation failed"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3138 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3139 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3140 | { |
| 3141 | if (num_passes > 1) |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 3142 | row_offset=ping_rowbytes*y; |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 3143 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3144 | else |
| 3145 | row_offset=0; |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 3146 | |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 3147 | png_read_row(ping,ping_pixels+row_offset,NULL); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3148 | q=GetAuthenticPixels(image,0,y,image->columns,1,exception); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3149 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3150 | if (q == (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3151 | break; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3152 | |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 3153 | p=ping_pixels+row_offset; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3154 | r=quantum_scanline; |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 3155 | |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 3156 | switch (ping_bit_depth) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3157 | { |
| 3158 | case 1: |
| 3159 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3160 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3161 | bit; |
| 3162 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3163 | for (x=(ssize_t) image->columns-7; x > 0; x-=8) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3164 | { |
| 3165 | for (bit=7; bit >= 0; bit--) |
glennrp | a18d5bc | 2011-04-23 14:51:34 +0000 | [diff] [blame] | 3166 | *r++=(Quantum) ((*p) & (0x01 << bit) ? 0x01 : 0x00); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3167 | p++; |
| 3168 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3169 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3170 | if ((image->columns % 8) != 0) |
| 3171 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3172 | for (bit=7; bit >= (ssize_t) (8-(image->columns % 8)); bit--) |
glennrp | a18d5bc | 2011-04-23 14:51:34 +0000 | [diff] [blame] | 3173 | *r++=(Quantum) ((*p) & (0x01 << bit) ? 0x01 : 0x00); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3174 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3175 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3176 | break; |
| 3177 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3178 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3179 | case 2: |
| 3180 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3181 | for (x=(ssize_t) image->columns-3; x > 0; x-=4) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3182 | { |
glennrp | a18d5bc | 2011-04-23 14:51:34 +0000 | [diff] [blame] | 3183 | *r++=(*p >> 6) & 0x03; |
| 3184 | *r++=(*p >> 4) & 0x03; |
| 3185 | *r++=(*p >> 2) & 0x03; |
| 3186 | *r++=(*p++) & 0x03; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3187 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3188 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3189 | if ((image->columns % 4) != 0) |
| 3190 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3191 | for (i=3; i >= (ssize_t) (4-(image->columns % 4)); i--) |
glennrp | a18d5bc | 2011-04-23 14:51:34 +0000 | [diff] [blame] | 3192 | *r++=(Quantum) ((*p >> (i*2)) & 0x03); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3193 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3194 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3195 | break; |
| 3196 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3197 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3198 | case 4: |
| 3199 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3200 | for (x=(ssize_t) image->columns-1; x > 0; x-=2) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3201 | { |
glennrp | a18d5bc | 2011-04-23 14:51:34 +0000 | [diff] [blame] | 3202 | *r++=(*p >> 4) & 0x0f; |
| 3203 | *r++=(*p++) & 0x0f; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3204 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3205 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3206 | if ((image->columns % 2) != 0) |
glennrp | a18d5bc | 2011-04-23 14:51:34 +0000 | [diff] [blame] | 3207 | *r++=(*p++ >> 4) & 0x0f; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3208 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3209 | break; |
| 3210 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3211 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3212 | case 8: |
| 3213 | { |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 3214 | if (ping_color_type == 4) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3215 | for (x=(ssize_t) image->columns-1; x >= 0; x--) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3216 | { |
glennrp | a18d5bc | 2011-04-23 14:51:34 +0000 | [diff] [blame] | 3217 | *r++=*p++; |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3218 | SetPixelAlpha(image,ScaleCharToQuantum((unsigned char) *p++),q); |
| 3219 | if (GetPixelAlpha(image,q) != OpaqueAlpha) |
glennrp | 0b206f5 | 2011-01-07 04:55:32 +0000 | [diff] [blame] | 3220 | found_transparent_pixel = MagickTrue; |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3221 | q+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3222 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3223 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3224 | else |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3225 | for (x=(ssize_t) image->columns-1; x >= 0; x--) |
glennrp | a18d5bc | 2011-04-23 14:51:34 +0000 | [diff] [blame] | 3226 | *r++=*p++; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3227 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3228 | break; |
| 3229 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3230 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3231 | case 16: |
| 3232 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3233 | for (x=(ssize_t) image->columns-1; x >= 0; x--) |
glennrp | a18d5bc | 2011-04-23 14:51:34 +0000 | [diff] [blame] | 3234 | { |
glennrp | c17d96f | 2011-06-27 01:20:11 +0000 | [diff] [blame] | 3235 | #if (MAGICKCORE_QUANTUM_DEPTH == 16) || (MAGICKCORE_QUANTUM_DEPTH == 32) |
glennrp | 58f77c7 | 2011-04-23 14:09:09 +0000 | [diff] [blame] | 3236 | size_t |
| 3237 | quantum; |
| 3238 | |
| 3239 | if (image->colors > 256) |
glennrp | c17d96f | 2011-06-27 01:20:11 +0000 | [diff] [blame] | 3240 | quantum=((*p++) << 8); |
glennrp | 58f77c7 | 2011-04-23 14:09:09 +0000 | [diff] [blame] | 3241 | |
| 3242 | else |
glennrp | c17d96f | 2011-06-27 01:20:11 +0000 | [diff] [blame] | 3243 | quantum=0; |
glennrp | 58f77c7 | 2011-04-23 14:09:09 +0000 | [diff] [blame] | 3244 | |
glennrp | 58f77c7 | 2011-04-23 14:09:09 +0000 | [diff] [blame] | 3245 | quantum|=(*p++); |
glennrp | c17d96f | 2011-06-27 01:20:11 +0000 | [diff] [blame] | 3246 | *r=ScaleShortToQuantum(quantum); |
glennrp | 58f77c7 | 2011-04-23 14:09:09 +0000 | [diff] [blame] | 3247 | r++; |
glennrp | 9d0ea4d | 2011-04-22 18:35:57 +0000 | [diff] [blame] | 3248 | |
| 3249 | if (ping_color_type == 4) |
| 3250 | { |
glennrp | c17d96f | 2011-06-27 01:20:11 +0000 | [diff] [blame] | 3251 | if (image->colors > 256) |
| 3252 | quantum=((*p++) << 8); |
| 3253 | else |
| 3254 | quantum=0; |
| 3255 | |
glennrp | 58f77c7 | 2011-04-23 14:09:09 +0000 | [diff] [blame] | 3256 | quantum|=(*p++); |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3257 | SetPixelAlpha(image,ScaleShortToQuantum(quantum),q); |
| 3258 | if (GetPixelAlpha(image,q) != OpaqueAlpha) |
glennrp | 58f77c7 | 2011-04-23 14:09:09 +0000 | [diff] [blame] | 3259 | found_transparent_pixel = MagickTrue; |
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); |
| 3270 | if (GetPixelAlpha(image,q) != OpaqueAlpha) |
glennrp | 9d0ea4d | 2011-04-22 18:35:57 +0000 | [diff] [blame] | 3271 | found_transparent_pixel = MagickTrue; |
| 3272 | p++; |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3273 | q+=GetPixelChannels(image); |
glennrp | 9d0ea4d | 2011-04-22 18:35:57 +0000 | [diff] [blame] | 3274 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3275 | #endif |
glennrp | a18d5bc | 2011-04-23 14:51:34 +0000 | [diff] [blame] | 3276 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3277 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3278 | break; |
| 3279 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3280 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3281 | default: |
| 3282 | break; |
| 3283 | } |
glennrp | 3faa9a3 | 2011-04-23 14:00:25 +0000 | [diff] [blame] | 3284 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3285 | /* |
| 3286 | Transfer image scanline. |
| 3287 | */ |
| 3288 | r=quantum_scanline; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3289 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3290 | q=GetAuthenticPixels(image,0,y,image->columns,1,exception); |
| 3291 | |
| 3292 | if (q == (Quantum *) NULL) |
| 3293 | break; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3294 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3295 | { |
| 3296 | SetPixelIndex(image,*r++,q); |
| 3297 | q+=GetPixelChannels(image); |
| 3298 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3299 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3300 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 3301 | break; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3302 | |
cristy | 7a287bf | 2010-02-14 02:18:09 +0000 | [diff] [blame] | 3303 | if ((image->previous == (Image *) NULL) && (num_passes == 1)) |
| 3304 | { |
cristy | cee9711 | 2010-05-28 00:44:52 +0000 | [diff] [blame] | 3305 | status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y, |
cristy | 9fff7b4 | 2011-04-29 01:09:31 +0000 | [diff] [blame] | 3306 | image->rows); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3307 | |
cristy | 7a287bf | 2010-02-14 02:18:09 +0000 | [diff] [blame] | 3308 | if (status == MagickFalse) |
| 3309 | break; |
| 3310 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3311 | } |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 3312 | |
cristy | 7a287bf | 2010-02-14 02:18:09 +0000 | [diff] [blame] | 3313 | if ((image->previous == (Image *) NULL) && (num_passes != 1)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3314 | { |
| 3315 | status=SetImageProgress(image,LoadImageTag,pass,num_passes); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3316 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3317 | if (status == MagickFalse) |
| 3318 | break; |
| 3319 | } |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 3320 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3321 | quantum_scanline=(Quantum *) RelinquishMagickMemory(quantum_scanline); |
| 3322 | } |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 3323 | |
| 3324 | image->matte=found_transparent_pixel; |
| 3325 | |
| 3326 | if (logging != MagickFalse) |
| 3327 | { |
| 3328 | if (found_transparent_pixel != MagickFalse) |
| 3329 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 3330 | " Found transparent pixel"); |
| 3331 | else |
glennrp | 5aa37f6 | 2011-01-02 03:07:57 +0000 | [diff] [blame] | 3332 | { |
| 3333 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 3334 | " No transparent pixel was found"); |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 3335 | |
glennrp | 5aa37f6 | 2011-01-02 03:07:57 +0000 | [diff] [blame] | 3336 | ping_color_type&=0x03; |
| 3337 | } |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 3338 | } |
| 3339 | } |
| 3340 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3341 | if (quantum_info != (QuantumInfo *) NULL) |
| 3342 | quantum_info=DestroyQuantumInfo(quantum_info); |
| 3343 | |
cristy | 5c6f789 | 2010-05-05 22:53:29 +0000 | [diff] [blame] | 3344 | if (image->storage_class == PseudoClass) |
| 3345 | { |
cristy | aeb2cbc | 2010-05-07 13:28:58 +0000 | [diff] [blame] | 3346 | MagickBooleanType |
cristy | 5c6f789 | 2010-05-05 22:53:29 +0000 | [diff] [blame] | 3347 | matte; |
| 3348 | |
| 3349 | matte=image->matte; |
| 3350 | image->matte=MagickFalse; |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3351 | (void) SyncImage(image,exception); |
cristy | aeb2cbc | 2010-05-07 13:28:58 +0000 | [diff] [blame] | 3352 | image->matte=matte; |
cristy | 5c6f789 | 2010-05-05 22:53:29 +0000 | [diff] [blame] | 3353 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3354 | |
glennrp | 4eb3931 | 2011-03-30 21:34:55 +0000 | [diff] [blame] | 3355 | png_read_end(ping,end_info); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3356 | |
| 3357 | if (image_info->number_scenes != 0 && mng_info->scenes_found-1 < |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3358 | (ssize_t) image_info->first_scene && image->delay != 0) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3359 | { |
| 3360 | png_destroy_read_struct(&ping,&ping_info,&end_info); |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 3361 | ping_pixels=(unsigned char *) RelinquishMagickMemory(ping_pixels); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3362 | image->colors=2; |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3363 | (void) SetImageBackgroundColor(image,exception); |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 3364 | #ifdef PNG_SETJMP_NOT_THREAD_SAFE |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 3365 | UnlockSemaphoreInfo(ping_semaphore); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3366 | #endif |
| 3367 | if (logging != MagickFalse) |
| 3368 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 3369 | " exit ReadOnePNGImage() early."); |
| 3370 | return(image); |
| 3371 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3372 | |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 3373 | if (png_get_valid(ping,ping_info,PNG_INFO_tRNS)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3374 | { |
| 3375 | ClassType |
| 3376 | storage_class; |
| 3377 | |
| 3378 | /* |
| 3379 | Image has a transparent background. |
| 3380 | */ |
| 3381 | storage_class=image->storage_class; |
| 3382 | image->matte=MagickTrue; |
glennrp | c11cf6a | 2010-03-20 16:46:19 +0000 | [diff] [blame] | 3383 | |
glennrp | 3c21811 | 2010-11-27 15:31:26 +0000 | [diff] [blame] | 3384 | /* Balfour fix from imagemagick discourse server, 5 Feb 2010 */ |
glennrp | c11cf6a | 2010-03-20 16:46:19 +0000 | [diff] [blame] | 3385 | |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3386 | if (storage_class == PseudoClass) |
| 3387 | { |
| 3388 | if ((int) ping_color_type == PNG_COLOR_TYPE_PALETTE) |
glennrp | c11cf6a | 2010-03-20 16:46:19 +0000 | [diff] [blame] | 3389 | { |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3390 | for (x=0; x < ping_num_trans; x++) |
| 3391 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3392 | image->colormap[x].matte=MagickTrue; |
| 3393 | image->colormap[x].alpha = |
| 3394 | ScaleCharToQuantum((unsigned char)ping_trans_alpha[x]); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3395 | } |
glennrp | c11cf6a | 2010-03-20 16:46:19 +0000 | [diff] [blame] | 3396 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3397 | |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3398 | else if (ping_color_type == PNG_COLOR_TYPE_GRAY) |
| 3399 | { |
| 3400 | for (x=0; x < (int) image->colors; x++) |
| 3401 | { |
| 3402 | if (ScaleQuantumToShort(image->colormap[x].red) == |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3403 | transparent_color.alpha) |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3404 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3405 | image->colormap[x].matte=MagickTrue; |
| 3406 | image->colormap[x].alpha = (Quantum) TransparentAlpha; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3407 | } |
| 3408 | } |
| 3409 | } |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3410 | (void) SyncImage(image,exception); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3411 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3412 | |
glennrp | a6a0663 | 2011-01-19 15:15:34 +0000 | [diff] [blame] | 3413 | #if 1 /* Should have already been done above, but glennrp problem P10 |
| 3414 | * needs this. |
| 3415 | */ |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3416 | else |
| 3417 | { |
| 3418 | for (y=0; y < (ssize_t) image->rows; y++) |
glennrp | c11cf6a | 2010-03-20 16:46:19 +0000 | [diff] [blame] | 3419 | { |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3420 | image->storage_class=storage_class; |
| 3421 | q=GetAuthenticPixels(image,0,y,image->columns,1,exception); |
| 3422 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3423 | if (q == (Quantum *) NULL) |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3424 | break; |
| 3425 | |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3426 | |
glennrp | a6a0663 | 2011-01-19 15:15:34 +0000 | [diff] [blame] | 3427 | /* Caution: on a Q8 build, this does not distinguish between |
| 3428 | * 16-bit colors that differ only in the low byte |
| 3429 | */ |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3430 | for (x=(ssize_t) image->columns-1; x >= 0; x--) |
| 3431 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3432 | if (ScaleQuantumToShort(GetPixelRed(image,q)) == |
| 3433 | transparent_color.red && |
| 3434 | ScaleQuantumToShort(GetPixelGreen(image,q)) == |
| 3435 | transparent_color.green && |
| 3436 | ScaleQuantumToShort(GetPixelBlue(image,q)) == |
| 3437 | transparent_color.blue) |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 3438 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3439 | SetPixelAlpha(image,TransparentAlpha,q); |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 3440 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3441 | |
glennrp | 67b9c1a | 2011-04-22 18:47:36 +0000 | [diff] [blame] | 3442 | #if 0 /* I have not found a case where this is needed. */ |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3443 | else |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 3444 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3445 | SetPixelAlpha(image,q)=(Quantum) OpaqueAlpha; |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 3446 | } |
glennrp | a6a0663 | 2011-01-19 15:15:34 +0000 | [diff] [blame] | 3447 | #endif |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3448 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3449 | q+=GetPixelChannels(image); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3450 | } |
| 3451 | |
| 3452 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 3453 | break; |
glennrp | c11cf6a | 2010-03-20 16:46:19 +0000 | [diff] [blame] | 3454 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3455 | } |
glennrp | a6a0663 | 2011-01-19 15:15:34 +0000 | [diff] [blame] | 3456 | #endif |
glennrp | c11cf6a | 2010-03-20 16:46:19 +0000 | [diff] [blame] | 3457 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3458 | image->storage_class=DirectClass; |
| 3459 | } |
glennrp | 3c21811 | 2010-11-27 15:31:26 +0000 | [diff] [blame] | 3460 | |
cristy | eb3b22a | 2011-03-31 20:16:11 +0000 | [diff] [blame] | 3461 | for (j = 0; j < 2; j++) |
glennrp | 4eb3931 | 2011-03-30 21:34:55 +0000 | [diff] [blame] | 3462 | { |
| 3463 | if (j == 0) |
glennrp | a0ed009 | 2011-04-18 16:36:29 +0000 | [diff] [blame] | 3464 | status = png_get_text(ping,ping_info,&text,&num_text) != 0 ? |
| 3465 | MagickTrue : MagickFalse; |
glennrp | 4eb3931 | 2011-03-30 21:34:55 +0000 | [diff] [blame] | 3466 | else |
glennrp | a0ed009 | 2011-04-18 16:36:29 +0000 | [diff] [blame] | 3467 | status = png_get_text(ping,end_info,&text,&num_text) != 0 ? |
| 3468 | MagickTrue : MagickFalse; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3469 | |
glennrp | 4eb3931 | 2011-03-30 21:34:55 +0000 | [diff] [blame] | 3470 | if (status != MagickFalse) |
| 3471 | for (i=0; i < (ssize_t) num_text; i++) |
| 3472 | { |
| 3473 | /* Check for a profile */ |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3474 | |
glennrp | 4eb3931 | 2011-03-30 21:34:55 +0000 | [diff] [blame] | 3475 | if (logging != MagickFalse) |
| 3476 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 3477 | " Reading PNG text chunk"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3478 | |
glennrp | 4eb3931 | 2011-03-30 21:34:55 +0000 | [diff] [blame] | 3479 | if (memcmp(text[i].key, "Raw profile type ",17) == 0) |
glennrp | 97f90e2 | 2011-02-22 05:47:58 +0000 | [diff] [blame] | 3480 | { |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 3481 | (void) Magick_png_read_raw_profile(ping,image,image_info,text, |
| 3482 | (int) i,exception); |
glennrp | 4eb3931 | 2011-03-30 21:34:55 +0000 | [diff] [blame] | 3483 | num_raw_profiles++; |
glennrp | 97f90e2 | 2011-02-22 05:47:58 +0000 | [diff] [blame] | 3484 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3485 | |
glennrp | 4eb3931 | 2011-03-30 21:34:55 +0000 | [diff] [blame] | 3486 | else |
| 3487 | { |
| 3488 | char |
| 3489 | *value; |
| 3490 | |
| 3491 | length=text[i].text_length; |
| 3492 | value=(char *) AcquireQuantumMemory(length+MaxTextExtent, |
| 3493 | sizeof(*value)); |
| 3494 | if (value == (char *) NULL) |
| 3495 | { |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 3496 | png_error(ping,"Memory allocation failed"); |
glennrp | 4eb3931 | 2011-03-30 21:34:55 +0000 | [diff] [blame] | 3497 | break; |
| 3498 | } |
| 3499 | *value='\0'; |
| 3500 | (void) ConcatenateMagickString(value,text[i].text,length+2); |
| 3501 | |
| 3502 | /* Don't save "density" or "units" property if we have a pHYs |
| 3503 | * chunk |
| 3504 | */ |
| 3505 | if (!png_get_valid(ping,ping_info,PNG_INFO_pHYs) || |
| 3506 | (LocaleCompare(text[i].key,"density") != 0 && |
| 3507 | LocaleCompare(text[i].key,"units") != 0)) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3508 | (void) SetImageProperty(image,text[i].key,value,exception); |
glennrp | 4eb3931 | 2011-03-30 21:34:55 +0000 | [diff] [blame] | 3509 | |
| 3510 | if (logging != MagickFalse) |
| 3511 | { |
| 3512 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 3513 | " length: %lu",(unsigned long) length); |
| 3514 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 3515 | " Keyword: %s",text[i].key); |
| 3516 | } |
| 3517 | |
| 3518 | value=DestroyString(value); |
| 3519 | } |
| 3520 | } |
| 3521 | num_text_total += num_text; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3522 | } |
glennrp | 3c21811 | 2010-11-27 15:31:26 +0000 | [diff] [blame] | 3523 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3524 | #ifdef MNG_OBJECT_BUFFERS |
| 3525 | /* |
| 3526 | Store the object if necessary. |
| 3527 | */ |
| 3528 | if (object_id && !mng_info->frozen[object_id]) |
| 3529 | { |
| 3530 | if (mng_info->ob[object_id] == (MngBuffer *) NULL) |
| 3531 | { |
| 3532 | /* |
| 3533 | create a new object buffer. |
| 3534 | */ |
| 3535 | mng_info->ob[object_id]=(MngBuffer *) |
cristy | 73bd4a5 | 2010-10-05 11:24:23 +0000 | [diff] [blame] | 3536 | AcquireMagickMemory(sizeof(MngBuffer)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3537 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3538 | if (mng_info->ob[object_id] != (MngBuffer *) NULL) |
| 3539 | { |
| 3540 | mng_info->ob[object_id]->image=(Image *) NULL; |
| 3541 | mng_info->ob[object_id]->reference_count=1; |
| 3542 | } |
| 3543 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3544 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3545 | if ((mng_info->ob[object_id] == (MngBuffer *) NULL) || |
| 3546 | mng_info->ob[object_id]->frozen) |
| 3547 | { |
| 3548 | if (mng_info->ob[object_id] == (MngBuffer *) NULL) |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 3549 | png_error(ping,"Memory allocation failed"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3550 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3551 | if (mng_info->ob[object_id]->frozen) |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 3552 | png_error(ping,"Cannot overwrite frozen MNG object buffer"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3553 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3554 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3555 | else |
| 3556 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3557 | |
| 3558 | if (mng_info->ob[object_id]->image != (Image *) NULL) |
| 3559 | mng_info->ob[object_id]->image=DestroyImage |
| 3560 | (mng_info->ob[object_id]->image); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3561 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3562 | mng_info->ob[object_id]->image=CloneImage(image,0,0,MagickTrue, |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3563 | exception); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3564 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3565 | if (mng_info->ob[object_id]->image != (Image *) NULL) |
| 3566 | mng_info->ob[object_id]->image->file=(FILE *) NULL; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3567 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3568 | else |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 3569 | png_error(ping, "Cloning image for object buffer failed"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3570 | |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 3571 | if (ping_width > 250000L || ping_height > 250000L) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3572 | png_error(ping,"PNG Image dimensions are too large."); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3573 | |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 3574 | mng_info->ob[object_id]->width=ping_width; |
| 3575 | mng_info->ob[object_id]->height=ping_height; |
| 3576 | mng_info->ob[object_id]->color_type=ping_color_type; |
| 3577 | mng_info->ob[object_id]->sample_depth=ping_bit_depth; |
| 3578 | mng_info->ob[object_id]->interlace_method=ping_interlace_method; |
| 3579 | mng_info->ob[object_id]->compression_method= |
| 3580 | ping_compression_method; |
| 3581 | mng_info->ob[object_id]->filter_method=ping_filter_method; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3582 | |
glennrp | faa852b | 2010-03-30 12:17:00 +0000 | [diff] [blame] | 3583 | if (png_get_valid(ping,ping_info,PNG_INFO_PLTE)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3584 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3585 | png_colorp |
| 3586 | plte; |
| 3587 | |
| 3588 | /* |
| 3589 | Copy the PLTE to the object buffer. |
| 3590 | */ |
| 3591 | png_get_PLTE(ping,ping_info,&plte,&number_colors); |
| 3592 | mng_info->ob[object_id]->plte_length=number_colors; |
glennrp | 3c21811 | 2010-11-27 15:31:26 +0000 | [diff] [blame] | 3593 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3594 | for (i=0; i < number_colors; i++) |
| 3595 | { |
| 3596 | mng_info->ob[object_id]->plte[i]=plte[i]; |
| 3597 | } |
| 3598 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3599 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3600 | else |
| 3601 | mng_info->ob[object_id]->plte_length=0; |
| 3602 | } |
| 3603 | } |
| 3604 | #endif |
glennrp | 0a55b4c | 2011-03-23 12:38:38 +0000 | [diff] [blame] | 3605 | |
| 3606 | /* Set image->matte to MagickTrue if the input colortype supports |
| 3607 | * alpha or if a valid tRNS chunk is present, no matter whether there |
| 3608 | * is actual transparency present. |
| 3609 | */ |
| 3610 | image->matte=(((int) ping_color_type == PNG_COLOR_TYPE_RGB_ALPHA) || |
| 3611 | ((int) ping_color_type == PNG_COLOR_TYPE_GRAY_ALPHA) || |
| 3612 | (png_get_valid(ping,ping_info,PNG_INFO_tRNS))) ? |
| 3613 | MagickTrue : MagickFalse; |
| 3614 | |
glennrp | cb395ac | 2011-03-30 19:50:23 +0000 | [diff] [blame] | 3615 | /* Set more properties for identify to retrieve */ |
| 3616 | { |
| 3617 | char |
| 3618 | msg[MaxTextExtent]; |
| 3619 | |
glennrp | 4eb3931 | 2011-03-30 21:34:55 +0000 | [diff] [blame] | 3620 | if (num_text_total != 0) |
glennrp | cb395ac | 2011-03-30 19:50:23 +0000 | [diff] [blame] | 3621 | { |
| 3622 | /* libpng doesn't tell us whether they were tEXt, zTXt, or iTXt */ |
cristy | 3b6fd2e | 2011-05-20 12:53:50 +0000 | [diff] [blame] | 3623 | (void) FormatLocaleString(msg,MaxTextExtent, |
glennrp | 613276d | 2011-03-30 21:46:49 +0000 | [diff] [blame] | 3624 | "%d tEXt/zTXt/iTXt chunks were found", num_text_total); |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3625 | (void) SetImageProperty(image,"png:text ",msg, |
| 3626 | exception); |
glennrp | cb395ac | 2011-03-30 19:50:23 +0000 | [diff] [blame] | 3627 | } |
| 3628 | |
| 3629 | if (num_raw_profiles != 0) |
| 3630 | { |
cristy | 3b6fd2e | 2011-05-20 12:53:50 +0000 | [diff] [blame] | 3631 | (void) FormatLocaleString(msg,MaxTextExtent, |
glennrp | cb395ac | 2011-03-30 19:50:23 +0000 | [diff] [blame] | 3632 | "%d were found", num_raw_profiles); |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3633 | (void) SetImageProperty(image,"png:text-encoded profiles",msg, |
| 3634 | exception); |
glennrp | cb395ac | 2011-03-30 19:50:23 +0000 | [diff] [blame] | 3635 | } |
| 3636 | |
glennrp | 98b83d4 | 2012-07-23 02:50:31 +0000 | [diff] [blame] | 3637 | if (ping_found_cHRM != MagickFalse) |
glennrp | 5961225 | 2011-03-30 21:45:21 +0000 | [diff] [blame] | 3638 | { |
cristy | 3b6fd2e | 2011-05-20 12:53:50 +0000 | [diff] [blame] | 3639 | (void) FormatLocaleString(msg,MaxTextExtent,"%s", |
glennrp | 5961225 | 2011-03-30 21:45:21 +0000 | [diff] [blame] | 3640 | "chunk was found (see Chromaticity, above)"); |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3641 | (void) SetImageProperty(image,"png:cHRM ",msg, |
| 3642 | exception); |
glennrp | 5961225 | 2011-03-30 21:45:21 +0000 | [diff] [blame] | 3643 | } |
glennrp | cb395ac | 2011-03-30 19:50:23 +0000 | [diff] [blame] | 3644 | |
| 3645 | if (png_get_valid(ping,ping_info,PNG_INFO_bKGD)) |
glennrp | 5961225 | 2011-03-30 21:45:21 +0000 | [diff] [blame] | 3646 | { |
cristy | 3b6fd2e | 2011-05-20 12:53:50 +0000 | [diff] [blame] | 3647 | (void) FormatLocaleString(msg,MaxTextExtent,"%s", |
glennrp | 5961225 | 2011-03-30 21:45:21 +0000 | [diff] [blame] | 3648 | "chunk was found (see Background color, above)"); |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3649 | (void) SetImageProperty(image,"png:bKGD ",msg, |
| 3650 | exception); |
glennrp | 5961225 | 2011-03-30 21:45:21 +0000 | [diff] [blame] | 3651 | } |
| 3652 | |
cristy | 3b6fd2e | 2011-05-20 12:53:50 +0000 | [diff] [blame] | 3653 | (void) FormatLocaleString(msg,MaxTextExtent,"%s", |
glennrp | 5961225 | 2011-03-30 21:45:21 +0000 | [diff] [blame] | 3654 | "chunk was found"); |
glennrp | cb395ac | 2011-03-30 19:50:23 +0000 | [diff] [blame] | 3655 | |
glennrp | 98b83d4 | 2012-07-23 02:50:31 +0000 | [diff] [blame] | 3656 | #if defined(PNG_iCCP_SUPPORTED) |
| 3657 | if (ping_found_iCCP != MagickFalse) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3658 | (void) SetImageProperty(image,"png:iCCP ",msg, |
| 3659 | exception); |
glennrp | 98b83d4 | 2012-07-23 02:50:31 +0000 | [diff] [blame] | 3660 | #endif |
glennrp | cb395ac | 2011-03-30 19:50:23 +0000 | [diff] [blame] | 3661 | |
glennrp | cb395ac | 2011-03-30 19:50:23 +0000 | [diff] [blame] | 3662 | if (png_get_valid(ping,ping_info,PNG_INFO_tRNS)) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3663 | (void) SetImageProperty(image,"png:tRNS ",msg, |
| 3664 | exception); |
glennrp | 4eb3931 | 2011-03-30 21:34:55 +0000 | [diff] [blame] | 3665 | |
| 3666 | #if defined(PNG_sRGB_SUPPORTED) |
glennrp | 98b83d4 | 2012-07-23 02:50:31 +0000 | [diff] [blame] | 3667 | if (ping_found_sRGB != MagickFalse) |
glennrp | 4eb3931 | 2011-03-30 21:34:55 +0000 | [diff] [blame] | 3668 | { |
cristy | 3b6fd2e | 2011-05-20 12:53:50 +0000 | [diff] [blame] | 3669 | (void) FormatLocaleString(msg,MaxTextExtent, |
glennrp | 98b83d4 | 2012-07-23 02:50:31 +0000 | [diff] [blame] | 3670 | "intent=%d (%s)", |
| 3671 | (int) intent, |
| 3672 | Magick_RenderingIntentString_from_PNG_RenderingIntent(intent)); |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3673 | (void) SetImageProperty(image,"png:sRGB ",msg, |
glennrp | 98b83d4 | 2012-07-23 02:50:31 +0000 | [diff] [blame] | 3674 | exception); |
glennrp | 4eb3931 | 2011-03-30 21:34:55 +0000 | [diff] [blame] | 3675 | } |
| 3676 | #endif |
| 3677 | |
glennrp | 98b83d4 | 2012-07-23 02:50:31 +0000 | [diff] [blame] | 3678 | if (ping_found_gAMA != MagickFalse) |
glennrp | 4eb3931 | 2011-03-30 21:34:55 +0000 | [diff] [blame] | 3679 | { |
cristy | 3b6fd2e | 2011-05-20 12:53:50 +0000 | [diff] [blame] | 3680 | (void) FormatLocaleString(msg,MaxTextExtent, |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3681 | "gamma=%.8g (See Gamma, above)", |
| 3682 | file_gamma); |
| 3683 | (void) SetImageProperty(image,"png:gAMA ",msg, |
| 3684 | exception); |
glennrp | 4eb3931 | 2011-03-30 21:34:55 +0000 | [diff] [blame] | 3685 | } |
| 3686 | |
| 3687 | #if defined(PNG_pHYs_SUPPORTED) |
| 3688 | if (png_get_valid(ping,ping_info,PNG_INFO_pHYs)) |
| 3689 | { |
cristy | 3b6fd2e | 2011-05-20 12:53:50 +0000 | [diff] [blame] | 3690 | (void) FormatLocaleString(msg,MaxTextExtent, |
glennrp | 07523c7 | 2011-03-31 18:12:10 +0000 | [diff] [blame] | 3691 | "x_res=%.10g, y_res=%.10g, units=%d", |
glennrp | 4eb3931 | 2011-03-30 21:34:55 +0000 | [diff] [blame] | 3692 | (double) x_resolution,(double) y_resolution, unit_type); |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3693 | (void) SetImageProperty(image,"png:pHYs ",msg, |
| 3694 | exception); |
glennrp | 4eb3931 | 2011-03-30 21:34:55 +0000 | [diff] [blame] | 3695 | } |
| 3696 | #endif |
| 3697 | |
| 3698 | #if defined(PNG_oFFs_SUPPORTED) |
| 3699 | if (png_get_valid(ping,ping_info,PNG_INFO_oFFs)) |
| 3700 | { |
cristy | 3b6fd2e | 2011-05-20 12:53:50 +0000 | [diff] [blame] | 3701 | (void) FormatLocaleString(msg,MaxTextExtent,"x_off=%.20g, y_off=%.20g", |
glennrp | 4eb3931 | 2011-03-30 21:34:55 +0000 | [diff] [blame] | 3702 | (double) image->page.x,(double) image->page.y); |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3703 | (void) SetImageProperty(image,"png:oFFs ",msg, |
| 3704 | exception); |
glennrp | 4eb3931 | 2011-03-30 21:34:55 +0000 | [diff] [blame] | 3705 | } |
| 3706 | #endif |
| 3707 | |
glennrp | 07523c7 | 2011-03-31 18:12:10 +0000 | [diff] [blame] | 3708 | if ((image->page.width != 0 && image->page.width != image->columns) || |
| 3709 | (image->page.height != 0 && image->page.height != image->rows)) |
| 3710 | { |
cristy | 3b6fd2e | 2011-05-20 12:53:50 +0000 | [diff] [blame] | 3711 | (void) FormatLocaleString(msg,MaxTextExtent, |
glennrp | 07523c7 | 2011-03-31 18:12:10 +0000 | [diff] [blame] | 3712 | "width=%.20g, height=%.20g", |
| 3713 | (double) image->page.width,(double) image->page.height); |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3714 | (void) SetImageProperty(image,"png:vpAg ",msg, |
| 3715 | exception); |
glennrp | 07523c7 | 2011-03-31 18:12:10 +0000 | [diff] [blame] | 3716 | } |
glennrp | cb395ac | 2011-03-30 19:50:23 +0000 | [diff] [blame] | 3717 | } |
| 3718 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3719 | /* |
| 3720 | Relinquish resources. |
| 3721 | */ |
| 3722 | png_destroy_read_struct(&ping,&ping_info,&end_info); |
| 3723 | |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 3724 | ping_pixels=(unsigned char *) RelinquishMagickMemory(ping_pixels); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3725 | |
| 3726 | if (logging != MagickFalse) |
| 3727 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 3728 | " exit ReadOnePNGImage()"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3729 | |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 3730 | #ifdef PNG_SETJMP_NOT_THREAD_SAFE |
| 3731 | UnlockSemaphoreInfo(ping_semaphore); |
| 3732 | #endif |
| 3733 | |
| 3734 | /* } for navigation to beginning of SETJMP-protected block, revert to |
| 3735 | * Throwing an Exception when an error occurs. |
| 3736 | */ |
| 3737 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3738 | return(image); |
| 3739 | |
| 3740 | /* end of reading one PNG image */ |
| 3741 | } |
| 3742 | |
| 3743 | static Image *ReadPNGImage(const ImageInfo *image_info,ExceptionInfo *exception) |
| 3744 | { |
| 3745 | Image |
| 3746 | *image, |
| 3747 | *previous; |
| 3748 | |
| 3749 | MagickBooleanType |
glennrp | 21f0e62 | 2011-01-07 16:20:57 +0000 | [diff] [blame] | 3750 | have_mng_structure, |
| 3751 | logging, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3752 | status; |
| 3753 | |
| 3754 | MngInfo |
| 3755 | *mng_info; |
| 3756 | |
| 3757 | char |
| 3758 | magic_number[MaxTextExtent]; |
| 3759 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3760 | ssize_t |
| 3761 | count; |
| 3762 | |
| 3763 | /* |
| 3764 | Open image file. |
| 3765 | */ |
| 3766 | assert(image_info != (const ImageInfo *) NULL); |
| 3767 | assert(image_info->signature == MagickSignature); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3768 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3769 | if (image_info->debug != MagickFalse) |
| 3770 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s", |
| 3771 | image_info->filename); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3772 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3773 | assert(exception != (ExceptionInfo *) NULL); |
| 3774 | assert(exception->signature == MagickSignature); |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 3775 | logging=LogMagickEvent(CoderEvent,GetMagickModule(),"Enter ReadPNGImage()"); |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3776 | image=AcquireImage(image_info,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3777 | mng_info=(MngInfo *) NULL; |
| 3778 | status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3779 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3780 | if (status == MagickFalse) |
| 3781 | ThrowReaderException(FileOpenError,"UnableToOpenFile"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3782 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3783 | /* |
| 3784 | Verify PNG signature. |
| 3785 | */ |
| 3786 | count=ReadBlob(image,8,(unsigned char *) magic_number); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3787 | |
glennrp | dde35db | 2011-02-21 12:06:32 +0000 | [diff] [blame] | 3788 | if (count < 8 || memcmp(magic_number,"\211PNG\r\n\032\n",8) != 0) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3789 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3790 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3791 | /* |
| 3792 | Allocate a MngInfo structure. |
| 3793 | */ |
| 3794 | have_mng_structure=MagickFalse; |
cristy | 73bd4a5 | 2010-10-05 11:24:23 +0000 | [diff] [blame] | 3795 | mng_info=(MngInfo *) AcquireMagickMemory(sizeof(MngInfo)); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3796 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3797 | if (mng_info == (MngInfo *) NULL) |
| 3798 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3799 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3800 | /* |
| 3801 | Initialize members of the MngInfo structure. |
| 3802 | */ |
| 3803 | (void) ResetMagickMemory(mng_info,0,sizeof(MngInfo)); |
| 3804 | mng_info->image=image; |
| 3805 | have_mng_structure=MagickTrue; |
| 3806 | |
| 3807 | previous=image; |
| 3808 | image=ReadOnePNGImage(mng_info,image_info,exception); |
| 3809 | MngInfoFreeStruct(mng_info,&have_mng_structure); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3810 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3811 | if (image == (Image *) NULL) |
| 3812 | { |
| 3813 | if (previous != (Image *) NULL) |
| 3814 | { |
| 3815 | if (previous->signature != MagickSignature) |
| 3816 | ThrowReaderException(CorruptImageError,"CorruptImage"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3817 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3818 | (void) CloseBlob(previous); |
| 3819 | (void) DestroyImageList(previous); |
| 3820 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3821 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3822 | if (logging != MagickFalse) |
| 3823 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 3824 | "exit ReadPNGImage() with error"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3825 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3826 | return((Image *) NULL); |
| 3827 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3828 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3829 | (void) CloseBlob(image); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3830 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3831 | if ((image->columns == 0) || (image->rows == 0)) |
| 3832 | { |
| 3833 | if (logging != MagickFalse) |
| 3834 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 3835 | "exit ReadPNGImage() with error."); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3836 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3837 | ThrowReaderException(CorruptImageError,"CorruptImage"); |
| 3838 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 3839 | |
cristy | 72715f5 | 2012-06-26 17:55:16 +0000 | [diff] [blame] | 3840 | if ((IssRGBColorspace(image->colorspace) != MagickFalse) && |
| 3841 | (image->gamma == 1.0)) |
| 3842 | SetImageColorspace(image,RGBColorspace,exception); |
| 3843 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3844 | if (LocaleCompare(image_info->magick,"PNG24") == 0) |
| 3845 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3846 | (void) SetImageType(image,TrueColorType,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3847 | image->matte=MagickFalse; |
| 3848 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3849 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3850 | if (LocaleCompare(image_info->magick,"PNG32") == 0) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3851 | (void) SetImageType(image,TrueColorMatteType,exception); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3852 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3853 | if (logging != MagickFalse) |
glennrp | 97f90e2 | 2011-02-22 05:47:58 +0000 | [diff] [blame] | 3854 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 3855 | " page.w: %.20g, page.h: %.20g,page.x: %.20g, page.y: %.20g.", |
| 3856 | (double) image->page.width,(double) image->page.height, |
| 3857 | (double) image->page.x,(double) image->page.y); |
| 3858 | |
| 3859 | if (logging != MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3860 | (void) LogMagickEvent(CoderEvent,GetMagickModule(),"exit ReadPNGImage()"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3861 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3862 | return(image); |
| 3863 | } |
| 3864 | |
| 3865 | |
| 3866 | |
| 3867 | #if defined(JNG_SUPPORTED) |
| 3868 | /* |
| 3869 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3870 | % % |
| 3871 | % % |
| 3872 | % % |
| 3873 | % R e a d O n e J N G I m a g e % |
| 3874 | % % |
| 3875 | % % |
| 3876 | % % |
| 3877 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3878 | % |
| 3879 | % ReadOneJNGImage() reads a JPEG Network Graphics (JNG) image file |
| 3880 | % (minus the 8-byte signature) and returns it. It allocates the memory |
| 3881 | % necessary for the new Image structure and returns a pointer to the new |
| 3882 | % image. |
| 3883 | % |
| 3884 | % JNG support written by Glenn Randers-Pehrson, glennrp@image... |
| 3885 | % |
| 3886 | % The format of the ReadOneJNGImage method is: |
| 3887 | % |
| 3888 | % Image *ReadOneJNGImage(MngInfo *mng_info, const ImageInfo *image_info, |
| 3889 | % ExceptionInfo *exception) |
| 3890 | % |
| 3891 | % A description of each parameter follows: |
| 3892 | % |
| 3893 | % o mng_info: Specifies a pointer to a MngInfo structure. |
| 3894 | % |
| 3895 | % o image_info: the image info. |
| 3896 | % |
| 3897 | % o exception: return any errors or warnings in this structure. |
| 3898 | % |
| 3899 | */ |
| 3900 | static Image *ReadOneJNGImage(MngInfo *mng_info, |
| 3901 | const ImageInfo *image_info, ExceptionInfo *exception) |
| 3902 | { |
| 3903 | Image |
| 3904 | *alpha_image, |
| 3905 | *color_image, |
| 3906 | *image, |
| 3907 | *jng_image; |
| 3908 | |
| 3909 | ImageInfo |
| 3910 | *alpha_image_info, |
| 3911 | *color_image_info; |
| 3912 | |
cristy | 4383ec8 | 2011-01-05 15:42:32 +0000 | [diff] [blame] | 3913 | MagickBooleanType |
| 3914 | logging; |
| 3915 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3916 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3917 | y; |
| 3918 | |
| 3919 | MagickBooleanType |
| 3920 | status; |
| 3921 | |
| 3922 | png_uint_32 |
| 3923 | jng_height, |
| 3924 | jng_width; |
| 3925 | |
| 3926 | png_byte |
| 3927 | jng_color_type, |
| 3928 | jng_image_sample_depth, |
| 3929 | jng_image_compression_method, |
| 3930 | jng_image_interlace_method, |
| 3931 | jng_alpha_sample_depth, |
| 3932 | jng_alpha_compression_method, |
| 3933 | jng_alpha_filter_method, |
| 3934 | jng_alpha_interlace_method; |
| 3935 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3936 | register const Quantum |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3937 | *s; |
| 3938 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3939 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3940 | i, |
| 3941 | x; |
| 3942 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3943 | register Quantum |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3944 | *q; |
| 3945 | |
| 3946 | register unsigned char |
| 3947 | *p; |
| 3948 | |
| 3949 | unsigned int |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3950 | read_JSEP, |
| 3951 | reading_idat, |
| 3952 | skip_to_iend; |
| 3953 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3954 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3955 | length; |
| 3956 | |
| 3957 | jng_alpha_compression_method=0; |
| 3958 | jng_alpha_sample_depth=8; |
| 3959 | jng_color_type=0; |
| 3960 | jng_height=0; |
| 3961 | jng_width=0; |
| 3962 | alpha_image=(Image *) NULL; |
| 3963 | color_image=(Image *) NULL; |
| 3964 | alpha_image_info=(ImageInfo *) NULL; |
| 3965 | color_image_info=(ImageInfo *) NULL; |
| 3966 | |
| 3967 | logging=LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 3968 | " Enter ReadOneJNGImage()"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3969 | |
| 3970 | image=mng_info->image; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3971 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3972 | if (GetAuthenticPixelQueue(image) != (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3973 | { |
| 3974 | /* |
| 3975 | Allocate next image structure. |
| 3976 | */ |
| 3977 | if (logging != MagickFalse) |
| 3978 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 3979 | " AcquireNextImage()"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3980 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 3981 | AcquireNextImage(image_info,image,exception); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3982 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3983 | if (GetNextImageInList(image) == (Image *) NULL) |
| 3984 | return((Image *) NULL); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 3985 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3986 | image=SyncNextImageInList(image); |
| 3987 | } |
| 3988 | mng_info->image=image; |
| 3989 | |
| 3990 | /* |
| 3991 | Signature bytes have already been read. |
| 3992 | */ |
| 3993 | |
| 3994 | read_JSEP=MagickFalse; |
| 3995 | reading_idat=MagickFalse; |
| 3996 | skip_to_iend=MagickFalse; |
| 3997 | for (;;) |
| 3998 | { |
| 3999 | char |
| 4000 | type[MaxTextExtent]; |
| 4001 | |
| 4002 | unsigned char |
| 4003 | *chunk; |
| 4004 | |
| 4005 | unsigned int |
| 4006 | count; |
| 4007 | |
| 4008 | /* |
| 4009 | Read a new JNG chunk. |
| 4010 | */ |
| 4011 | status=SetImageProgress(image,LoadImagesTag,TellBlob(image), |
| 4012 | 2*GetBlobSize(image)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4013 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4014 | if (status == MagickFalse) |
| 4015 | break; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4016 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4017 | type[0]='\0'; |
| 4018 | (void) ConcatenateMagickString(type,"errr",MaxTextExtent); |
| 4019 | length=ReadBlobMSBLong(image); |
| 4020 | count=(unsigned int) ReadBlob(image,4,(unsigned char *) type); |
| 4021 | |
| 4022 | if (logging != MagickFalse) |
| 4023 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 4024 | " Reading JNG chunk type %c%c%c%c, length: %.20g", |
| 4025 | type[0],type[1],type[2],type[3],(double) length); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4026 | |
| 4027 | if (length > PNG_UINT_31_MAX || count == 0) |
| 4028 | ThrowReaderException(CorruptImageError,"CorruptImage"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4029 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4030 | p=NULL; |
| 4031 | chunk=(unsigned char *) NULL; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4032 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4033 | if (length) |
| 4034 | { |
| 4035 | chunk=(unsigned char *) AcquireQuantumMemory(length,sizeof(*chunk)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4036 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4037 | if (chunk == (unsigned char *) NULL) |
| 4038 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4039 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 4040 | for (i=0; i < (ssize_t) length; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4041 | chunk[i]=(unsigned char) ReadBlobByte(image); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4042 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4043 | p=chunk; |
| 4044 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4045 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4046 | (void) ReadBlobMSBLong(image); /* read crc word */ |
| 4047 | |
| 4048 | if (skip_to_iend) |
| 4049 | { |
| 4050 | if (length) |
| 4051 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4052 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4053 | continue; |
| 4054 | } |
| 4055 | |
| 4056 | if (memcmp(type,mng_JHDR,4) == 0) |
| 4057 | { |
| 4058 | if (length == 16) |
| 4059 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 4060 | jng_width=(size_t) ((p[0] << 24) | (p[1] << 16) | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4061 | (p[2] << 8) | p[3]); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 4062 | jng_height=(size_t) ((p[4] << 24) | (p[5] << 16) | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4063 | (p[6] << 8) | p[7]); |
| 4064 | jng_color_type=p[8]; |
| 4065 | jng_image_sample_depth=p[9]; |
| 4066 | jng_image_compression_method=p[10]; |
| 4067 | jng_image_interlace_method=p[11]; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4068 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4069 | image->interlace=jng_image_interlace_method != 0 ? PNGInterlace : |
| 4070 | NoInterlace; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4071 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4072 | jng_alpha_sample_depth=p[12]; |
| 4073 | jng_alpha_compression_method=p[13]; |
| 4074 | jng_alpha_filter_method=p[14]; |
| 4075 | jng_alpha_interlace_method=p[15]; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4076 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4077 | if (logging != MagickFalse) |
| 4078 | { |
| 4079 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | f2faecf | 2010-05-28 19:19:36 +0000 | [diff] [blame] | 4080 | " jng_width: %16lu",(unsigned long) jng_width); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4081 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4082 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | f2faecf | 2010-05-28 19:19:36 +0000 | [diff] [blame] | 4083 | " jng_width: %16lu",(unsigned long) jng_height); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4084 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4085 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 4086 | " jng_color_type: %16d",jng_color_type); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4087 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4088 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 4089 | " jng_image_sample_depth: %3d", |
| 4090 | jng_image_sample_depth); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4091 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4092 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 4093 | " jng_image_compression_method:%3d", |
| 4094 | jng_image_compression_method); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4095 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4096 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 4097 | " jng_image_interlace_method: %3d", |
| 4098 | jng_image_interlace_method); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4099 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4100 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 4101 | " jng_alpha_sample_depth: %3d", |
| 4102 | jng_alpha_sample_depth); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4103 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4104 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 4105 | " jng_alpha_compression_method:%3d", |
| 4106 | jng_alpha_compression_method); |
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_alpha_filter_method: %3d", |
| 4110 | jng_alpha_filter_method); |
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_alpha_interlace_method: %3d", |
| 4114 | jng_alpha_interlace_method); |
| 4115 | } |
| 4116 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4117 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4118 | if (length) |
| 4119 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4120 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4121 | continue; |
| 4122 | } |
| 4123 | |
| 4124 | |
| 4125 | if ((reading_idat == MagickFalse) && (read_JSEP == MagickFalse) && |
| 4126 | ((memcmp(type,mng_JDAT,4) == 0) || (memcmp(type,mng_JdAA,4) == 0) || |
| 4127 | (memcmp(type,mng_IDAT,4) == 0) || (memcmp(type,mng_JDAA,4) == 0))) |
| 4128 | { |
| 4129 | /* |
| 4130 | o create color_image |
| 4131 | o open color_blob, attached to color_image |
| 4132 | o if (color type has alpha) |
| 4133 | open alpha_blob, attached to alpha_image |
| 4134 | */ |
| 4135 | |
cristy | 73bd4a5 | 2010-10-05 11:24:23 +0000 | [diff] [blame] | 4136 | color_image_info=(ImageInfo *)AcquireMagickMemory(sizeof(ImageInfo)); |
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 (color_image_info == (ImageInfo *) NULL) |
| 4139 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4140 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4141 | GetImageInfo(color_image_info); |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 4142 | color_image=AcquireImage(color_image_info,exception); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4143 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4144 | if (color_image == (Image *) NULL) |
| 4145 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
| 4146 | |
| 4147 | if (logging != MagickFalse) |
| 4148 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 4149 | " Creating color_blob."); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4150 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4151 | (void) AcquireUniqueFilename(color_image->filename); |
| 4152 | status=OpenBlob(color_image_info,color_image,WriteBinaryBlobMode, |
| 4153 | exception); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4154 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4155 | if (status == MagickFalse) |
| 4156 | return((Image *) NULL); |
| 4157 | |
| 4158 | if ((image_info->ping == MagickFalse) && (jng_color_type >= 12)) |
| 4159 | { |
| 4160 | alpha_image_info=(ImageInfo *) |
cristy | 73bd4a5 | 2010-10-05 11:24:23 +0000 | [diff] [blame] | 4161 | AcquireMagickMemory(sizeof(ImageInfo)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4162 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4163 | if (alpha_image_info == (ImageInfo *) NULL) |
| 4164 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4165 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4166 | GetImageInfo(alpha_image_info); |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 4167 | alpha_image=AcquireImage(alpha_image_info,exception); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4168 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4169 | if (alpha_image == (Image *) NULL) |
| 4170 | { |
| 4171 | alpha_image=DestroyImage(alpha_image); |
| 4172 | ThrowReaderException(ResourceLimitError, |
| 4173 | "MemoryAllocationFailed"); |
| 4174 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4175 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4176 | if (logging != MagickFalse) |
| 4177 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 4178 | " Creating alpha_blob."); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4179 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4180 | (void) AcquireUniqueFilename(alpha_image->filename); |
| 4181 | status=OpenBlob(alpha_image_info,alpha_image,WriteBinaryBlobMode, |
| 4182 | exception); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4183 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4184 | if (status == MagickFalse) |
| 4185 | return((Image *) NULL); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4186 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4187 | if (jng_alpha_compression_method == 0) |
| 4188 | { |
| 4189 | unsigned char |
| 4190 | data[18]; |
| 4191 | |
| 4192 | if (logging != MagickFalse) |
| 4193 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 4194 | " Writing IHDR chunk to alpha_blob."); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4195 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4196 | (void) WriteBlob(alpha_image,8,(const unsigned char *) |
| 4197 | "\211PNG\r\n\032\n"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4198 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4199 | (void) WriteBlobMSBULong(alpha_image,13L); |
| 4200 | PNGType(data,mng_IHDR); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 4201 | LogPNGChunk(logging,mng_IHDR,13L); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4202 | PNGLong(data+4,jng_width); |
| 4203 | PNGLong(data+8,jng_height); |
| 4204 | data[12]=jng_alpha_sample_depth; |
| 4205 | data[13]=0; /* color_type gray */ |
| 4206 | data[14]=0; /* compression method 0 */ |
| 4207 | data[15]=0; /* filter_method 0 */ |
| 4208 | data[16]=0; /* interlace_method 0 */ |
| 4209 | (void) WriteBlob(alpha_image,17,data); |
| 4210 | (void) WriteBlobMSBULong(alpha_image,crc32(0,data,17)); |
| 4211 | } |
| 4212 | } |
| 4213 | reading_idat=MagickTrue; |
| 4214 | } |
| 4215 | |
| 4216 | if (memcmp(type,mng_JDAT,4) == 0) |
| 4217 | { |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4218 | /* Copy chunk to color_image->blob */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4219 | |
| 4220 | if (logging != MagickFalse) |
| 4221 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 4222 | " Copying JDAT chunk data to color_blob."); |
| 4223 | |
| 4224 | (void) WriteBlob(color_image,length,chunk); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4225 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4226 | if (length) |
| 4227 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4228 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4229 | continue; |
| 4230 | } |
| 4231 | |
| 4232 | if (memcmp(type,mng_IDAT,4) == 0) |
| 4233 | { |
| 4234 | png_byte |
| 4235 | data[5]; |
| 4236 | |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4237 | /* Copy IDAT header and chunk data to alpha_image->blob */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4238 | |
| 4239 | if (image_info->ping == MagickFalse) |
| 4240 | { |
| 4241 | if (logging != MagickFalse) |
| 4242 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 4243 | " Copying IDAT chunk data to alpha_blob."); |
| 4244 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 4245 | (void) WriteBlobMSBULong(alpha_image,(size_t) length); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4246 | PNGType(data,mng_IDAT); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 4247 | LogPNGChunk(logging,mng_IDAT,length); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4248 | (void) WriteBlob(alpha_image,4,data); |
| 4249 | (void) WriteBlob(alpha_image,length,chunk); |
| 4250 | (void) WriteBlobMSBULong(alpha_image, |
| 4251 | crc32(crc32(0,data,4),chunk,(uInt) length)); |
| 4252 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4253 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4254 | if (length) |
| 4255 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4256 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4257 | continue; |
| 4258 | } |
| 4259 | |
| 4260 | if ((memcmp(type,mng_JDAA,4) == 0) || (memcmp(type,mng_JdAA,4) == 0)) |
| 4261 | { |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4262 | /* Copy chunk data to alpha_image->blob */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4263 | |
| 4264 | if (image_info->ping == MagickFalse) |
| 4265 | { |
| 4266 | if (logging != MagickFalse) |
| 4267 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 4268 | " Copying JDAA chunk data to alpha_blob."); |
| 4269 | |
| 4270 | (void) WriteBlob(alpha_image,length,chunk); |
| 4271 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4272 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4273 | if (length) |
| 4274 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4275 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4276 | continue; |
| 4277 | } |
| 4278 | |
| 4279 | if (memcmp(type,mng_JSEP,4) == 0) |
| 4280 | { |
| 4281 | read_JSEP=MagickTrue; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4282 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4283 | if (length) |
| 4284 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4285 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4286 | continue; |
| 4287 | } |
| 4288 | |
| 4289 | if (memcmp(type,mng_bKGD,4) == 0) |
| 4290 | { |
| 4291 | if (length == 2) |
| 4292 | { |
| 4293 | image->background_color.red=ScaleCharToQuantum(p[1]); |
| 4294 | image->background_color.green=image->background_color.red; |
| 4295 | image->background_color.blue=image->background_color.red; |
| 4296 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4297 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4298 | if (length == 6) |
| 4299 | { |
| 4300 | image->background_color.red=ScaleCharToQuantum(p[1]); |
| 4301 | image->background_color.green=ScaleCharToQuantum(p[3]); |
| 4302 | image->background_color.blue=ScaleCharToQuantum(p[5]); |
| 4303 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4304 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4305 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 4306 | continue; |
| 4307 | } |
| 4308 | |
| 4309 | if (memcmp(type,mng_gAMA,4) == 0) |
| 4310 | { |
| 4311 | if (length == 4) |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 4312 | image->gamma=((float) mng_get_long(p))*0.00001; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4313 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4314 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 4315 | continue; |
| 4316 | } |
| 4317 | |
| 4318 | if (memcmp(type,mng_cHRM,4) == 0) |
| 4319 | { |
| 4320 | if (length == 32) |
| 4321 | { |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 4322 | image->chromaticity.white_point.x=0.00001*mng_get_long(p); |
| 4323 | image->chromaticity.white_point.y=0.00001*mng_get_long(&p[4]); |
| 4324 | image->chromaticity.red_primary.x=0.00001*mng_get_long(&p[8]); |
| 4325 | image->chromaticity.red_primary.y=0.00001*mng_get_long(&p[12]); |
| 4326 | image->chromaticity.green_primary.x=0.00001*mng_get_long(&p[16]); |
| 4327 | image->chromaticity.green_primary.y=0.00001*mng_get_long(&p[20]); |
| 4328 | image->chromaticity.blue_primary.x=0.00001*mng_get_long(&p[24]); |
| 4329 | image->chromaticity.blue_primary.y=0.00001*mng_get_long(&p[28]); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4330 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4331 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4332 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 4333 | continue; |
| 4334 | } |
| 4335 | |
| 4336 | if (memcmp(type,mng_sRGB,4) == 0) |
| 4337 | { |
| 4338 | if (length == 1) |
| 4339 | { |
glennrp | e610a07 | 2010-08-05 17:08:46 +0000 | [diff] [blame] | 4340 | image->rendering_intent= |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 4341 | Magick_RenderingIntent_from_PNG_RenderingIntent(p[0]); |
cristy | da7803d | 2012-05-03 01:22:45 +0000 | [diff] [blame] | 4342 | image->gamma=1.000f/2.200f; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4343 | image->chromaticity.red_primary.x=0.6400f; |
| 4344 | image->chromaticity.red_primary.y=0.3300f; |
| 4345 | image->chromaticity.green_primary.x=0.3000f; |
| 4346 | image->chromaticity.green_primary.y=0.6000f; |
| 4347 | image->chromaticity.blue_primary.x=0.1500f; |
| 4348 | image->chromaticity.blue_primary.y=0.0600f; |
| 4349 | image->chromaticity.white_point.x=0.3127f; |
| 4350 | image->chromaticity.white_point.y=0.3290f; |
| 4351 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4352 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4353 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 4354 | continue; |
| 4355 | } |
| 4356 | |
| 4357 | if (memcmp(type,mng_oFFs,4) == 0) |
| 4358 | { |
| 4359 | if (length > 8) |
| 4360 | { |
glennrp | 5eae760 | 2011-02-22 15:21:32 +0000 | [diff] [blame] | 4361 | image->page.x=(ssize_t) mng_get_long(p); |
| 4362 | image->page.y=(ssize_t) mng_get_long(&p[4]); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4363 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4364 | if ((int) p[8] != 0) |
| 4365 | { |
| 4366 | image->page.x/=10000; |
| 4367 | image->page.y/=10000; |
| 4368 | } |
| 4369 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4370 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4371 | if (length) |
| 4372 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4373 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4374 | continue; |
| 4375 | } |
| 4376 | |
| 4377 | if (memcmp(type,mng_pHYs,4) == 0) |
| 4378 | { |
| 4379 | if (length > 8) |
| 4380 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 4381 | image->resolution.x=(double) mng_get_long(p); |
| 4382 | image->resolution.y=(double) mng_get_long(&p[4]); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4383 | if ((int) p[8] == PNG_RESOLUTION_METER) |
| 4384 | { |
| 4385 | image->units=PixelsPerCentimeterResolution; |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 4386 | image->resolution.x=image->resolution.x/100.0f; |
| 4387 | image->resolution.y=image->resolution.y/100.0f; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4388 | } |
| 4389 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4390 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4391 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 4392 | continue; |
| 4393 | } |
| 4394 | |
| 4395 | #if 0 |
| 4396 | if (memcmp(type,mng_iCCP,4) == 0) |
| 4397 | { |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 4398 | /* To do: */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4399 | if (length) |
| 4400 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4401 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4402 | continue; |
| 4403 | } |
| 4404 | #endif |
| 4405 | |
| 4406 | if (length) |
| 4407 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 4408 | |
| 4409 | if (memcmp(type,mng_IEND,4)) |
| 4410 | continue; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4411 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4412 | break; |
| 4413 | } |
| 4414 | |
| 4415 | |
| 4416 | /* IEND found */ |
| 4417 | |
| 4418 | /* |
| 4419 | Finish up reading image data: |
| 4420 | |
| 4421 | o read main image from color_blob. |
| 4422 | |
| 4423 | o close color_blob. |
| 4424 | |
| 4425 | o if (color_type has alpha) |
| 4426 | if alpha_encoding is PNG |
| 4427 | read secondary image from alpha_blob via ReadPNG |
| 4428 | if alpha_encoding is JPEG |
| 4429 | read secondary image from alpha_blob via ReadJPEG |
| 4430 | |
| 4431 | o close alpha_blob. |
| 4432 | |
| 4433 | o copy intensity of secondary image into |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 4434 | alpha samples of main image. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4435 | |
| 4436 | o destroy the secondary image. |
| 4437 | */ |
| 4438 | |
| 4439 | (void) CloseBlob(color_image); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4440 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4441 | if (logging != MagickFalse) |
| 4442 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 4443 | " Reading jng_image from color_blob."); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4444 | |
cristy | 3b6fd2e | 2011-05-20 12:53:50 +0000 | [diff] [blame] | 4445 | (void) FormatLocaleString(color_image_info->filename,MaxTextExtent,"%s", |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4446 | color_image->filename); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4447 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4448 | color_image_info->ping=MagickFalse; /* To do: avoid this */ |
| 4449 | jng_image=ReadImage(color_image_info,exception); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4450 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4451 | if (jng_image == (Image *) NULL) |
| 4452 | return((Image *) NULL); |
| 4453 | |
| 4454 | (void) RelinquishUniqueFileResource(color_image->filename); |
| 4455 | color_image=DestroyImage(color_image); |
| 4456 | color_image_info=DestroyImageInfo(color_image_info); |
| 4457 | |
| 4458 | if (jng_image == (Image *) NULL) |
| 4459 | return((Image *) NULL); |
| 4460 | |
| 4461 | if (logging != MagickFalse) |
| 4462 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 4463 | " Copying jng_image pixels to main image."); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4464 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4465 | image->rows=jng_height; |
| 4466 | image->columns=jng_width; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4467 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 4468 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4469 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 4470 | s=GetVirtualPixels(jng_image,0,y,image->columns,1,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4471 | q=GetAuthenticPixels(image,0,y,image->columns,1,exception); |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 4472 | for (x=(ssize_t) image->columns; x != 0; x--) |
| 4473 | { |
| 4474 | SetPixelRed(image,GetPixelRed(jng_image,s),q); |
| 4475 | SetPixelGreen(image,GetPixelGreen(jng_image,s),q); |
| 4476 | SetPixelBlue(image,GetPixelBlue(jng_image,s),q); |
| 4477 | q+=GetPixelChannels(image); |
| 4478 | s+=GetPixelChannels(jng_image); |
| 4479 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4480 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4481 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 4482 | break; |
| 4483 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4484 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4485 | jng_image=DestroyImage(jng_image); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4486 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4487 | if (image_info->ping == MagickFalse) |
| 4488 | { |
| 4489 | if (jng_color_type >= 12) |
| 4490 | { |
| 4491 | if (jng_alpha_compression_method == 0) |
| 4492 | { |
| 4493 | png_byte |
| 4494 | data[5]; |
| 4495 | (void) WriteBlobMSBULong(alpha_image,0x00000000L); |
| 4496 | PNGType(data,mng_IEND); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 4497 | LogPNGChunk(logging,mng_IEND,0L); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4498 | (void) WriteBlob(alpha_image,4,data); |
| 4499 | (void) WriteBlobMSBULong(alpha_image,crc32(0,data,4)); |
| 4500 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4501 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4502 | (void) CloseBlob(alpha_image); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4503 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4504 | if (logging != MagickFalse) |
| 4505 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 4506 | " Reading alpha from alpha_blob."); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4507 | |
cristy | 3b6fd2e | 2011-05-20 12:53:50 +0000 | [diff] [blame] | 4508 | (void) FormatLocaleString(alpha_image_info->filename,MaxTextExtent, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4509 | "%s",alpha_image->filename); |
| 4510 | |
| 4511 | jng_image=ReadImage(alpha_image_info,exception); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4512 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4513 | if (jng_image != (Image *) NULL) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 4514 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4515 | { |
| 4516 | s=GetVirtualPixels(jng_image,0,y,image->columns,1, |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 4517 | exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4518 | q=GetAuthenticPixels(image,0,y,image->columns,1,exception); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4519 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4520 | if (image->matte != MagickFalse) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 4521 | for (x=(ssize_t) image->columns; x != 0; x--) |
| 4522 | { |
| 4523 | SetPixelAlpha(image,GetPixelRed(jng_image,s),q); |
| 4524 | q+=GetPixelChannels(image); |
| 4525 | s+=GetPixelChannels(jng_image); |
| 4526 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4527 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4528 | else |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 4529 | for (x=(ssize_t) image->columns; x != 0; x--) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4530 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 4531 | SetPixelAlpha(image,GetPixelRed(jng_image,s),q); |
| 4532 | if (GetPixelAlpha(image,q) != OpaqueAlpha) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4533 | image->matte=MagickTrue; |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 4534 | q+=GetPixelChannels(image); |
| 4535 | s+=GetPixelChannels(jng_image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4536 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4537 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4538 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 4539 | break; |
| 4540 | } |
| 4541 | (void) RelinquishUniqueFileResource(alpha_image->filename); |
| 4542 | alpha_image=DestroyImage(alpha_image); |
| 4543 | alpha_image_info=DestroyImageInfo(alpha_image_info); |
| 4544 | if (jng_image != (Image *) NULL) |
| 4545 | jng_image=DestroyImage(jng_image); |
| 4546 | } |
| 4547 | } |
| 4548 | |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4549 | /* Read the JNG image. */ |
| 4550 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4551 | if (mng_info->mng_type == 0) |
| 4552 | { |
| 4553 | mng_info->mng_width=jng_width; |
| 4554 | mng_info->mng_height=jng_height; |
| 4555 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4556 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4557 | if (image->page.width == 0 && image->page.height == 0) |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4558 | { |
| 4559 | image->page.width=jng_width; |
| 4560 | image->page.height=jng_height; |
| 4561 | } |
| 4562 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4563 | if (image->page.x == 0 && image->page.y == 0) |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4564 | { |
| 4565 | image->page.x=mng_info->x_off[mng_info->object_id]; |
| 4566 | image->page.y=mng_info->y_off[mng_info->object_id]; |
| 4567 | } |
| 4568 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4569 | else |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4570 | { |
| 4571 | image->page.y=mng_info->y_off[mng_info->object_id]; |
| 4572 | } |
| 4573 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4574 | mng_info->image_found++; |
| 4575 | status=SetImageProgress(image,LoadImagesTag,2*TellBlob(image), |
| 4576 | 2*GetBlobSize(image)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4577 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4578 | if (logging != MagickFalse) |
| 4579 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 4580 | " exit ReadOneJNGImage()"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4581 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4582 | return(image); |
| 4583 | } |
| 4584 | |
| 4585 | /* |
| 4586 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 4587 | % % |
| 4588 | % % |
| 4589 | % % |
| 4590 | % R e a d J N G I m a g e % |
| 4591 | % % |
| 4592 | % % |
| 4593 | % % |
| 4594 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 4595 | % |
| 4596 | % ReadJNGImage() reads a JPEG Network Graphics (JNG) image file |
| 4597 | % (including the 8-byte signature) and returns it. It allocates the memory |
| 4598 | % necessary for the new Image structure and returns a pointer to the new |
| 4599 | % image. |
| 4600 | % |
| 4601 | % JNG support written by Glenn Randers-Pehrson, glennrp@image... |
| 4602 | % |
| 4603 | % The format of the ReadJNGImage method is: |
| 4604 | % |
| 4605 | % Image *ReadJNGImage(const ImageInfo *image_info, ExceptionInfo |
| 4606 | % *exception) |
| 4607 | % |
| 4608 | % A description of each parameter follows: |
| 4609 | % |
| 4610 | % o image_info: the image info. |
| 4611 | % |
| 4612 | % o exception: return any errors or warnings in this structure. |
| 4613 | % |
| 4614 | */ |
| 4615 | |
| 4616 | static Image *ReadJNGImage(const ImageInfo *image_info,ExceptionInfo *exception) |
| 4617 | { |
| 4618 | Image |
| 4619 | *image, |
| 4620 | *previous; |
| 4621 | |
| 4622 | MagickBooleanType |
glennrp | 21f0e62 | 2011-01-07 16:20:57 +0000 | [diff] [blame] | 4623 | have_mng_structure, |
| 4624 | logging, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4625 | status; |
| 4626 | |
| 4627 | MngInfo |
| 4628 | *mng_info; |
| 4629 | |
| 4630 | char |
| 4631 | magic_number[MaxTextExtent]; |
| 4632 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4633 | size_t |
| 4634 | count; |
| 4635 | |
| 4636 | /* |
| 4637 | Open image file. |
| 4638 | */ |
| 4639 | assert(image_info != (const ImageInfo *) NULL); |
| 4640 | assert(image_info->signature == MagickSignature); |
| 4641 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image_info->filename); |
| 4642 | assert(exception != (ExceptionInfo *) NULL); |
| 4643 | assert(exception->signature == MagickSignature); |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 4644 | logging=LogMagickEvent(CoderEvent,GetMagickModule(),"Enter ReadJNGImage()"); |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 4645 | image=AcquireImage(image_info,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4646 | mng_info=(MngInfo *) NULL; |
| 4647 | status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4648 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4649 | if (status == MagickFalse) |
| 4650 | return((Image *) NULL); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4651 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4652 | if (LocaleCompare(image_info->magick,"JNG") != 0) |
| 4653 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4654 | |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4655 | /* Verify JNG signature. */ |
| 4656 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4657 | count=(size_t) ReadBlob(image,8,(unsigned char *) magic_number); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4658 | |
glennrp | 3b8763e | 2011-02-21 12:08:18 +0000 | [diff] [blame] | 4659 | if (count < 8 || memcmp(magic_number,"\213JNG\r\n\032\n",8) != 0) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4660 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4661 | |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4662 | /* Allocate a MngInfo structure. */ |
| 4663 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4664 | have_mng_structure=MagickFalse; |
cristy | 73bd4a5 | 2010-10-05 11:24:23 +0000 | [diff] [blame] | 4665 | mng_info=(MngInfo *) AcquireMagickMemory(sizeof(*mng_info)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4666 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4667 | if (mng_info == (MngInfo *) NULL) |
| 4668 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4669 | |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4670 | /* Initialize members of the MngInfo structure. */ |
| 4671 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4672 | (void) ResetMagickMemory(mng_info,0,sizeof(MngInfo)); |
| 4673 | have_mng_structure=MagickTrue; |
| 4674 | |
| 4675 | mng_info->image=image; |
| 4676 | previous=image; |
| 4677 | image=ReadOneJNGImage(mng_info,image_info,exception); |
| 4678 | MngInfoFreeStruct(mng_info,&have_mng_structure); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4679 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4680 | if (image == (Image *) NULL) |
| 4681 | { |
| 4682 | if (IsImageObject(previous) != MagickFalse) |
| 4683 | { |
| 4684 | (void) CloseBlob(previous); |
| 4685 | (void) DestroyImageList(previous); |
| 4686 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4687 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4688 | if (logging != MagickFalse) |
| 4689 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 4690 | "exit ReadJNGImage() with error"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4691 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4692 | return((Image *) NULL); |
| 4693 | } |
| 4694 | (void) CloseBlob(image); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4695 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4696 | if (image->columns == 0 || image->rows == 0) |
| 4697 | { |
| 4698 | if (logging != MagickFalse) |
| 4699 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 4700 | "exit ReadJNGImage() with error"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4701 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4702 | ThrowReaderException(CorruptImageError,"CorruptImage"); |
| 4703 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4704 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4705 | if (logging != MagickFalse) |
| 4706 | (void) LogMagickEvent(CoderEvent,GetMagickModule(),"exit ReadJNGImage()"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4707 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4708 | return(image); |
| 4709 | } |
| 4710 | #endif |
| 4711 | |
| 4712 | static Image *ReadMNGImage(const ImageInfo *image_info,ExceptionInfo *exception) |
| 4713 | { |
| 4714 | char |
| 4715 | page_geometry[MaxTextExtent]; |
| 4716 | |
| 4717 | Image |
| 4718 | *image, |
| 4719 | *previous; |
| 4720 | |
cristy | 4383ec8 | 2011-01-05 15:42:32 +0000 | [diff] [blame] | 4721 | MagickBooleanType |
glennrp | 21f0e62 | 2011-01-07 16:20:57 +0000 | [diff] [blame] | 4722 | logging, |
| 4723 | have_mng_structure; |
cristy | 4383ec8 | 2011-01-05 15:42:32 +0000 | [diff] [blame] | 4724 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4725 | volatile int |
| 4726 | first_mng_object, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4727 | object_id, |
| 4728 | term_chunk_found, |
| 4729 | skip_to_iend; |
| 4730 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 4731 | volatile ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4732 | image_count=0; |
| 4733 | |
| 4734 | MagickBooleanType |
| 4735 | status; |
| 4736 | |
| 4737 | MagickOffsetType |
| 4738 | offset; |
| 4739 | |
| 4740 | MngInfo |
| 4741 | *mng_info; |
| 4742 | |
| 4743 | MngBox |
| 4744 | default_fb, |
| 4745 | fb, |
| 4746 | previous_fb; |
| 4747 | |
| 4748 | #if defined(MNG_INSERT_LAYERS) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 4749 | PixelInfo |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4750 | mng_background_color; |
| 4751 | #endif |
| 4752 | |
| 4753 | register unsigned char |
| 4754 | *p; |
| 4755 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 4756 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4757 | i; |
| 4758 | |
| 4759 | size_t |
| 4760 | count; |
| 4761 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 4762 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4763 | loop_level; |
| 4764 | |
| 4765 | volatile short |
| 4766 | skipping_loop; |
| 4767 | |
| 4768 | #if defined(MNG_INSERT_LAYERS) |
| 4769 | unsigned int |
| 4770 | mandatory_back=0; |
| 4771 | #endif |
| 4772 | |
| 4773 | volatile unsigned int |
| 4774 | #ifdef MNG_OBJECT_BUFFERS |
| 4775 | mng_background_object=0, |
| 4776 | #endif |
| 4777 | mng_type=0; /* 0: PNG or JNG; 1: MNG; 2: MNG-LC; 3: MNG-VLC */ |
| 4778 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 4779 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4780 | default_frame_timeout, |
| 4781 | frame_timeout, |
| 4782 | #if defined(MNG_INSERT_LAYERS) |
| 4783 | image_height, |
| 4784 | image_width, |
| 4785 | #endif |
| 4786 | length; |
| 4787 | |
glennrp | 38ea083 | 2010-06-02 18:50:28 +0000 | [diff] [blame] | 4788 | /* These delays are all measured in image ticks_per_second, |
| 4789 | * not in MNG ticks_per_second |
| 4790 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 4791 | volatile size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4792 | default_frame_delay, |
| 4793 | final_delay, |
| 4794 | final_image_delay, |
| 4795 | frame_delay, |
| 4796 | #if defined(MNG_INSERT_LAYERS) |
| 4797 | insert_layers, |
| 4798 | #endif |
| 4799 | mng_iterations=1, |
| 4800 | simplicity=0, |
| 4801 | subframe_height=0, |
| 4802 | subframe_width=0; |
| 4803 | |
| 4804 | previous_fb.top=0; |
| 4805 | previous_fb.bottom=0; |
| 4806 | previous_fb.left=0; |
| 4807 | previous_fb.right=0; |
| 4808 | default_fb.top=0; |
| 4809 | default_fb.bottom=0; |
| 4810 | default_fb.left=0; |
| 4811 | default_fb.right=0; |
| 4812 | |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4813 | /* Open image file. */ |
| 4814 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4815 | assert(image_info != (const ImageInfo *) NULL); |
| 4816 | assert(image_info->signature == MagickSignature); |
| 4817 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image_info->filename); |
| 4818 | assert(exception != (ExceptionInfo *) NULL); |
| 4819 | assert(exception->signature == MagickSignature); |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 4820 | logging=LogMagickEvent(CoderEvent,GetMagickModule(),"Enter ReadMNGImage()"); |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 4821 | image=AcquireImage(image_info,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4822 | mng_info=(MngInfo *) NULL; |
| 4823 | status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4824 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4825 | if (status == MagickFalse) |
| 4826 | return((Image *) NULL); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4827 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4828 | first_mng_object=MagickFalse; |
| 4829 | skipping_loop=(-1); |
| 4830 | have_mng_structure=MagickFalse; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4831 | |
| 4832 | /* Allocate a MngInfo structure. */ |
| 4833 | |
cristy | 73bd4a5 | 2010-10-05 11:24:23 +0000 | [diff] [blame] | 4834 | mng_info=(MngInfo *) AcquireMagickMemory(sizeof(MngInfo)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4835 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4836 | if (mng_info == (MngInfo *) NULL) |
| 4837 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4838 | |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4839 | /* Initialize members of the MngInfo structure. */ |
| 4840 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4841 | (void) ResetMagickMemory(mng_info,0,sizeof(MngInfo)); |
| 4842 | mng_info->image=image; |
| 4843 | have_mng_structure=MagickTrue; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4844 | |
| 4845 | if (LocaleCompare(image_info->magick,"MNG") == 0) |
| 4846 | { |
| 4847 | char |
| 4848 | magic_number[MaxTextExtent]; |
| 4849 | |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4850 | /* Verify MNG signature. */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4851 | count=(size_t) ReadBlob(image,8,(unsigned char *) magic_number); |
| 4852 | if (memcmp(magic_number,"\212MNG\r\n\032\n",8) != 0) |
| 4853 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4854 | |
| 4855 | /* Initialize some nonzero members of the MngInfo structure. */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4856 | for (i=0; i < MNG_MAX_OBJECTS; i++) |
| 4857 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 4858 | mng_info->object_clip[i].right=(ssize_t) PNG_UINT_31_MAX; |
| 4859 | mng_info->object_clip[i].bottom=(ssize_t) PNG_UINT_31_MAX; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4860 | } |
| 4861 | mng_info->exists[0]=MagickTrue; |
| 4862 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4863 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4864 | first_mng_object=MagickTrue; |
| 4865 | mng_type=0; |
| 4866 | #if defined(MNG_INSERT_LAYERS) |
| 4867 | insert_layers=MagickFalse; /* should be False when converting or mogrifying */ |
| 4868 | #endif |
| 4869 | default_frame_delay=0; |
| 4870 | default_frame_timeout=0; |
| 4871 | frame_delay=0; |
| 4872 | final_delay=1; |
| 4873 | mng_info->ticks_per_second=1UL*image->ticks_per_second; |
| 4874 | object_id=0; |
| 4875 | skip_to_iend=MagickFalse; |
| 4876 | term_chunk_found=MagickFalse; |
| 4877 | mng_info->framing_mode=1; |
| 4878 | #if defined(MNG_INSERT_LAYERS) |
| 4879 | mandatory_back=MagickFalse; |
| 4880 | #endif |
| 4881 | #if defined(MNG_INSERT_LAYERS) |
| 4882 | mng_background_color=image->background_color; |
| 4883 | #endif |
| 4884 | default_fb=mng_info->frame; |
| 4885 | previous_fb=mng_info->frame; |
| 4886 | do |
| 4887 | { |
| 4888 | char |
| 4889 | type[MaxTextExtent]; |
| 4890 | |
| 4891 | if (LocaleCompare(image_info->magick,"MNG") == 0) |
| 4892 | { |
| 4893 | unsigned char |
| 4894 | *chunk; |
| 4895 | |
| 4896 | /* |
| 4897 | Read a new chunk. |
| 4898 | */ |
| 4899 | type[0]='\0'; |
| 4900 | (void) ConcatenateMagickString(type,"errr",MaxTextExtent); |
| 4901 | length=ReadBlobMSBLong(image); |
| 4902 | count=(size_t) ReadBlob(image,4,(unsigned char *) type); |
| 4903 | |
| 4904 | if (logging != MagickFalse) |
| 4905 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 4906 | " Reading MNG chunk type %c%c%c%c, length: %.20g", |
| 4907 | type[0],type[1],type[2],type[3],(double) length); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4908 | |
| 4909 | if (length > PNG_UINT_31_MAX) |
| 4910 | status=MagickFalse; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4911 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4912 | if (count == 0) |
| 4913 | ThrowReaderException(CorruptImageError,"CorruptImage"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4914 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4915 | p=NULL; |
| 4916 | chunk=(unsigned char *) NULL; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4917 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4918 | if (length) |
| 4919 | { |
| 4920 | chunk=(unsigned char *) AcquireQuantumMemory(length,sizeof(*chunk)); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4921 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4922 | if (chunk == (unsigned char *) NULL) |
| 4923 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4924 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 4925 | for (i=0; i < (ssize_t) length; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4926 | chunk[i]=(unsigned char) ReadBlobByte(image); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4927 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4928 | p=chunk; |
| 4929 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4930 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4931 | (void) ReadBlobMSBLong(image); /* read crc word */ |
| 4932 | |
| 4933 | #if !defined(JNG_SUPPORTED) |
| 4934 | if (memcmp(type,mng_JHDR,4) == 0) |
| 4935 | { |
| 4936 | skip_to_iend=MagickTrue; |
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 (mng_info->jhdr_warning == 0) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 4939 | (void) ThrowMagickException(exception,GetMagickModule(), |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4940 | CoderError,"JNGCompressNotSupported","`%s'",image->filename); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4941 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4942 | mng_info->jhdr_warning++; |
| 4943 | } |
| 4944 | #endif |
| 4945 | if (memcmp(type,mng_DHDR,4) == 0) |
| 4946 | { |
| 4947 | skip_to_iend=MagickTrue; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4948 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4949 | if (mng_info->dhdr_warning == 0) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 4950 | (void) ThrowMagickException(exception,GetMagickModule(), |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4951 | CoderError,"DeltaPNGNotSupported","`%s'",image->filename); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4952 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4953 | mng_info->dhdr_warning++; |
| 4954 | } |
| 4955 | if (memcmp(type,mng_MEND,4) == 0) |
| 4956 | break; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 4957 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4958 | if (skip_to_iend) |
| 4959 | { |
| 4960 | if (memcmp(type,mng_IEND,4) == 0) |
| 4961 | skip_to_iend=MagickFalse; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4962 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4963 | if (length) |
| 4964 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4965 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4966 | if (logging != MagickFalse) |
| 4967 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 4968 | " Skip to IEND."); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4969 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4970 | continue; |
| 4971 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4972 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4973 | if (memcmp(type,mng_MHDR,4) == 0) |
| 4974 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 4975 | mng_info->mng_width=(size_t) ((p[0] << 24) | (p[1] << 16) | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4976 | (p[2] << 8) | p[3]); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4977 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 4978 | mng_info->mng_height=(size_t) ((p[4] << 24) | (p[5] << 16) | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4979 | (p[6] << 8) | p[7]); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4980 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4981 | if (logging != MagickFalse) |
| 4982 | { |
| 4983 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 4984 | " MNG width: %.20g",(double) mng_info->mng_width); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4985 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 4986 | " MNG height: %.20g",(double) mng_info->mng_height); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4987 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4988 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4989 | p+=8; |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 4990 | mng_info->ticks_per_second=(size_t) mng_get_long(p); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4991 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4992 | if (mng_info->ticks_per_second == 0) |
| 4993 | default_frame_delay=0; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4994 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4995 | else |
| 4996 | default_frame_delay=1UL*image->ticks_per_second/ |
| 4997 | mng_info->ticks_per_second; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 4998 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 4999 | frame_delay=default_frame_delay; |
| 5000 | simplicity=0; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5001 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5002 | if (length > 16) |
| 5003 | { |
| 5004 | p+=16; |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 5005 | simplicity=(size_t) mng_get_long(p); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5006 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5007 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5008 | mng_type=1; /* Full MNG */ |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5009 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5010 | if ((simplicity != 0) && ((simplicity | 11) == 11)) |
| 5011 | mng_type=2; /* LC */ |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5012 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5013 | if ((simplicity != 0) && ((simplicity | 9) == 9)) |
| 5014 | mng_type=3; /* VLC */ |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5015 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5016 | #if defined(MNG_INSERT_LAYERS) |
| 5017 | if (mng_type != 3) |
| 5018 | insert_layers=MagickTrue; |
| 5019 | #endif |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 5020 | if (GetAuthenticPixelQueue(image) != (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5021 | { |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5022 | /* Allocate next image structure. */ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 5023 | AcquireNextImage(image_info,image,exception); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5024 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5025 | if (GetNextImageInList(image) == (Image *) NULL) |
| 5026 | return((Image *) NULL); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5027 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5028 | image=SyncNextImageInList(image); |
| 5029 | mng_info->image=image; |
| 5030 | } |
| 5031 | |
| 5032 | if ((mng_info->mng_width > 65535L) || |
| 5033 | (mng_info->mng_height > 65535L)) |
| 5034 | ThrowReaderException(ImageError,"WidthOrHeightExceedsLimit"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5035 | |
cristy | 3b6fd2e | 2011-05-20 12:53:50 +0000 | [diff] [blame] | 5036 | (void) FormatLocaleString(page_geometry,MaxTextExtent, |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 5037 | "%.20gx%.20g+0+0",(double) mng_info->mng_width,(double) |
cristy | f2faecf | 2010-05-28 19:19:36 +0000 | [diff] [blame] | 5038 | mng_info->mng_height); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5039 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5040 | mng_info->frame.left=0; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 5041 | mng_info->frame.right=(ssize_t) mng_info->mng_width; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5042 | mng_info->frame.top=0; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 5043 | mng_info->frame.bottom=(ssize_t) mng_info->mng_height; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5044 | mng_info->clip=default_fb=previous_fb=mng_info->frame; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5045 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5046 | for (i=0; i < MNG_MAX_OBJECTS; i++) |
| 5047 | mng_info->object_clip[i]=mng_info->frame; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5048 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5049 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 5050 | continue; |
| 5051 | } |
| 5052 | |
| 5053 | if (memcmp(type,mng_TERM,4) == 0) |
| 5054 | { |
| 5055 | int |
| 5056 | repeat=0; |
| 5057 | |
| 5058 | |
| 5059 | if (length) |
| 5060 | repeat=p[0]; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5061 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5062 | if (repeat == 3) |
| 5063 | { |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 5064 | final_delay=(png_uint_32) mng_get_long(&p[2]); |
| 5065 | mng_iterations=(png_uint_32) mng_get_long(&p[6]); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5066 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5067 | if (mng_iterations == PNG_UINT_31_MAX) |
| 5068 | mng_iterations=0; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5069 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5070 | image->iterations=mng_iterations; |
| 5071 | term_chunk_found=MagickTrue; |
| 5072 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5073 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5074 | if (logging != MagickFalse) |
| 5075 | { |
| 5076 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 5077 | " repeat=%d",repeat); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5078 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5079 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 5080 | " final_delay=%.20g",(double) final_delay); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5081 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5082 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 5083 | " image->iterations=%.20g",(double) image->iterations); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5084 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5085 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5086 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 5087 | continue; |
| 5088 | } |
| 5089 | if (memcmp(type,mng_DEFI,4) == 0) |
| 5090 | { |
| 5091 | if (mng_type == 3) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 5092 | (void) ThrowMagickException(exception,GetMagickModule(), |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5093 | CoderError,"DEFI chunk found in MNG-VLC datastream","`%s'", |
| 5094 | image->filename); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5095 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5096 | object_id=(p[0] << 8) | p[1]; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5097 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5098 | if (mng_type == 2 && object_id != 0) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 5099 | (void) ThrowMagickException(exception,GetMagickModule(), |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5100 | CoderError,"Nonzero object_id in MNG-LC datastream","`%s'", |
| 5101 | image->filename); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5102 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5103 | if (object_id > MNG_MAX_OBJECTS) |
| 5104 | { |
| 5105 | /* |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 5106 | Instead of using a warning we should allocate a larger |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5107 | MngInfo structure and continue. |
| 5108 | */ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 5109 | (void) ThrowMagickException(exception,GetMagickModule(), |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5110 | CoderError,"object id too large","`%s'",image->filename); |
| 5111 | object_id=MNG_MAX_OBJECTS; |
| 5112 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5113 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5114 | if (mng_info->exists[object_id]) |
| 5115 | if (mng_info->frozen[object_id]) |
| 5116 | { |
| 5117 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 5118 | (void) ThrowMagickException(exception, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5119 | GetMagickModule(),CoderError, |
| 5120 | "DEFI cannot redefine a frozen MNG object","`%s'", |
| 5121 | image->filename); |
| 5122 | continue; |
| 5123 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5124 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5125 | mng_info->exists[object_id]=MagickTrue; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5126 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5127 | if (length > 2) |
| 5128 | mng_info->invisible[object_id]=p[2]; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5129 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5130 | /* |
| 5131 | Extract object offset info. |
| 5132 | */ |
| 5133 | if (length > 11) |
| 5134 | { |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5135 | mng_info->x_off[object_id]=(ssize_t) ((p[4] << 24) | |
| 5136 | (p[5] << 16) | (p[6] << 8) | p[7]); |
| 5137 | |
| 5138 | mng_info->y_off[object_id]=(ssize_t) ((p[8] << 24) | |
| 5139 | (p[9] << 16) | (p[10] << 8) | p[11]); |
| 5140 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5141 | if (logging != MagickFalse) |
| 5142 | { |
| 5143 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 5144 | " x_off[%d]: %.20g",object_id,(double) |
cristy | f2faecf | 2010-05-28 19:19:36 +0000 | [diff] [blame] | 5145 | mng_info->x_off[object_id]); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5146 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5147 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 5148 | " y_off[%d]: %.20g",object_id,(double) |
cristy | f2faecf | 2010-05-28 19:19:36 +0000 | [diff] [blame] | 5149 | mng_info->y_off[object_id]); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5150 | } |
| 5151 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5152 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5153 | /* |
| 5154 | Extract object clipping info. |
| 5155 | */ |
| 5156 | if (length > 27) |
| 5157 | mng_info->object_clip[object_id]=mng_read_box(mng_info->frame,0, |
| 5158 | &p[12]); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5159 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5160 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 5161 | continue; |
| 5162 | } |
| 5163 | if (memcmp(type,mng_bKGD,4) == 0) |
| 5164 | { |
| 5165 | mng_info->have_global_bkgd=MagickFalse; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5166 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5167 | if (length > 5) |
| 5168 | { |
| 5169 | mng_info->mng_global_bkgd.red= |
| 5170 | ScaleShortToQuantum((unsigned short) ((p[0] << 8) | p[1])); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5171 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5172 | mng_info->mng_global_bkgd.green= |
| 5173 | ScaleShortToQuantum((unsigned short) ((p[2] << 8) | p[3])); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5174 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5175 | mng_info->mng_global_bkgd.blue= |
| 5176 | ScaleShortToQuantum((unsigned short) ((p[4] << 8) | p[5])); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5177 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5178 | mng_info->have_global_bkgd=MagickTrue; |
| 5179 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5180 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5181 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 5182 | continue; |
| 5183 | } |
| 5184 | if (memcmp(type,mng_BACK,4) == 0) |
| 5185 | { |
| 5186 | #if defined(MNG_INSERT_LAYERS) |
| 5187 | if (length > 6) |
| 5188 | mandatory_back=p[6]; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5189 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5190 | else |
| 5191 | mandatory_back=0; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5192 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5193 | if (mandatory_back && length > 5) |
| 5194 | { |
| 5195 | mng_background_color.red= |
| 5196 | ScaleShortToQuantum((unsigned short) ((p[0] << 8) | p[1])); |
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_background_color.green= |
| 5199 | ScaleShortToQuantum((unsigned short) ((p[2] << 8) | p[3])); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5200 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5201 | mng_background_color.blue= |
| 5202 | ScaleShortToQuantum((unsigned short) ((p[4] << 8) | p[5])); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5203 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 5204 | mng_background_color.alpha=OpaqueAlpha; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5205 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5206 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5207 | #ifdef MNG_OBJECT_BUFFERS |
| 5208 | if (length > 8) |
| 5209 | mng_background_object=(p[7] << 8) | p[8]; |
| 5210 | #endif |
| 5211 | #endif |
| 5212 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 5213 | continue; |
| 5214 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5215 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5216 | if (memcmp(type,mng_PLTE,4) == 0) |
| 5217 | { |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5218 | /* Read global PLTE. */ |
| 5219 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5220 | if (length && (length < 769)) |
| 5221 | { |
| 5222 | if (mng_info->global_plte == (png_colorp) NULL) |
| 5223 | mng_info->global_plte=(png_colorp) AcquireQuantumMemory(256, |
| 5224 | sizeof(*mng_info->global_plte)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5225 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 5226 | for (i=0; i < (ssize_t) (length/3); i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5227 | { |
| 5228 | mng_info->global_plte[i].red=p[3*i]; |
| 5229 | mng_info->global_plte[i].green=p[3*i+1]; |
| 5230 | mng_info->global_plte[i].blue=p[3*i+2]; |
| 5231 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5232 | |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 5233 | mng_info->global_plte_length=(unsigned int) (length/3); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5234 | } |
| 5235 | #ifdef MNG_LOOSE |
| 5236 | for ( ; i < 256; i++) |
| 5237 | { |
| 5238 | mng_info->global_plte[i].red=i; |
| 5239 | mng_info->global_plte[i].green=i; |
| 5240 | mng_info->global_plte[i].blue=i; |
| 5241 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5242 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5243 | if (length) |
| 5244 | mng_info->global_plte_length=256; |
| 5245 | #endif |
| 5246 | else |
| 5247 | mng_info->global_plte_length=0; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5248 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5249 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 5250 | continue; |
| 5251 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5252 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5253 | if (memcmp(type,mng_tRNS,4) == 0) |
| 5254 | { |
| 5255 | /* read global tRNS */ |
| 5256 | |
| 5257 | if (length < 257) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 5258 | for (i=0; i < (ssize_t) length; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5259 | mng_info->global_trns[i]=p[i]; |
| 5260 | |
| 5261 | #ifdef MNG_LOOSE |
| 5262 | for ( ; i < 256; i++) |
| 5263 | mng_info->global_trns[i]=255; |
| 5264 | #endif |
cristy | 12560f3 | 2010-06-03 16:51:08 +0000 | [diff] [blame] | 5265 | mng_info->global_trns_length=(unsigned int) length; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5266 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 5267 | continue; |
| 5268 | } |
| 5269 | if (memcmp(type,mng_gAMA,4) == 0) |
| 5270 | { |
| 5271 | if (length == 4) |
| 5272 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 5273 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5274 | igamma; |
| 5275 | |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 5276 | igamma=mng_get_long(p); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5277 | mng_info->global_gamma=((float) igamma)*0.00001; |
| 5278 | mng_info->have_global_gama=MagickTrue; |
| 5279 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5280 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5281 | else |
| 5282 | mng_info->have_global_gama=MagickFalse; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5283 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5284 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 5285 | continue; |
| 5286 | } |
| 5287 | |
| 5288 | if (memcmp(type,mng_cHRM,4) == 0) |
| 5289 | { |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5290 | /* Read global cHRM */ |
| 5291 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5292 | if (length == 32) |
| 5293 | { |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 5294 | mng_info->global_chrm.white_point.x=0.00001*mng_get_long(p); |
| 5295 | mng_info->global_chrm.white_point.y=0.00001*mng_get_long(&p[4]); |
| 5296 | 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] | 5297 | mng_info->global_chrm.red_primary.y=0.00001* |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 5298 | mng_get_long(&p[12]); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5299 | mng_info->global_chrm.green_primary.x=0.00001* |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 5300 | mng_get_long(&p[16]); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5301 | mng_info->global_chrm.green_primary.y=0.00001* |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 5302 | mng_get_long(&p[20]); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5303 | mng_info->global_chrm.blue_primary.x=0.00001* |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 5304 | mng_get_long(&p[24]); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5305 | mng_info->global_chrm.blue_primary.y=0.00001* |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 5306 | mng_get_long(&p[28]); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5307 | mng_info->have_global_chrm=MagickTrue; |
| 5308 | } |
| 5309 | else |
| 5310 | mng_info->have_global_chrm=MagickFalse; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5311 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5312 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 5313 | continue; |
| 5314 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5315 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5316 | if (memcmp(type,mng_sRGB,4) == 0) |
| 5317 | { |
| 5318 | /* |
| 5319 | Read global sRGB. |
| 5320 | */ |
| 5321 | if (length) |
| 5322 | { |
glennrp | e610a07 | 2010-08-05 17:08:46 +0000 | [diff] [blame] | 5323 | mng_info->global_srgb_intent= |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 5324 | Magick_RenderingIntent_from_PNG_RenderingIntent(p[0]); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5325 | mng_info->have_global_srgb=MagickTrue; |
| 5326 | } |
| 5327 | else |
| 5328 | mng_info->have_global_srgb=MagickFalse; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5329 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5330 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 5331 | continue; |
| 5332 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5333 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5334 | if (memcmp(type,mng_iCCP,4) == 0) |
| 5335 | { |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 5336 | /* To do: */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5337 | |
| 5338 | /* |
| 5339 | Read global iCCP. |
| 5340 | */ |
| 5341 | if (length) |
| 5342 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5343 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5344 | continue; |
| 5345 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5346 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5347 | if (memcmp(type,mng_FRAM,4) == 0) |
| 5348 | { |
| 5349 | if (mng_type == 3) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 5350 | (void) ThrowMagickException(exception,GetMagickModule(), |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5351 | CoderError,"FRAM chunk found in MNG-VLC datastream","`%s'", |
| 5352 | image->filename); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5353 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5354 | if ((mng_info->framing_mode == 2) || (mng_info->framing_mode == 4)) |
| 5355 | image->delay=frame_delay; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5356 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5357 | frame_delay=default_frame_delay; |
| 5358 | frame_timeout=default_frame_timeout; |
| 5359 | fb=default_fb; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5360 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5361 | if (length) |
| 5362 | if (p[0]) |
| 5363 | mng_info->framing_mode=p[0]; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5364 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5365 | if (logging != MagickFalse) |
| 5366 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 5367 | " Framing_mode=%d",mng_info->framing_mode); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5368 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5369 | if (length > 6) |
| 5370 | { |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5371 | /* Note the delay and frame clipping boundaries. */ |
| 5372 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5373 | p++; /* framing mode */ |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5374 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 5375 | while (*p && ((p-chunk) < (ssize_t) length)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5376 | p++; /* frame name */ |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5377 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5378 | p++; /* frame name terminator */ |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5379 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 5380 | if ((p-chunk) < (ssize_t) (length-4)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5381 | { |
| 5382 | int |
| 5383 | change_delay, |
| 5384 | change_timeout, |
| 5385 | change_clipping; |
| 5386 | |
| 5387 | change_delay=(*p++); |
| 5388 | change_timeout=(*p++); |
| 5389 | change_clipping=(*p++); |
| 5390 | p++; /* change_sync */ |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5391 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5392 | if (change_delay) |
| 5393 | { |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 5394 | frame_delay=1UL*image->ticks_per_second* |
| 5395 | mng_get_long(p); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5396 | |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 5397 | if (mng_info->ticks_per_second != 0) |
| 5398 | frame_delay/=mng_info->ticks_per_second; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5399 | |
glennrp | bb010dd | 2010-06-01 13:07:15 +0000 | [diff] [blame] | 5400 | else |
| 5401 | frame_delay=PNG_UINT_31_MAX; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5402 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5403 | if (change_delay == 2) |
| 5404 | default_frame_delay=frame_delay; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5405 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5406 | p+=4; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5407 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5408 | if (logging != MagickFalse) |
| 5409 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 5410 | " Framing_delay=%.20g",(double) frame_delay); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5411 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5412 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5413 | if (change_timeout) |
| 5414 | { |
glennrp | bb010dd | 2010-06-01 13:07:15 +0000 | [diff] [blame] | 5415 | frame_timeout=1UL*image->ticks_per_second* |
| 5416 | mng_get_long(p); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5417 | |
glennrp | bb010dd | 2010-06-01 13:07:15 +0000 | [diff] [blame] | 5418 | if (mng_info->ticks_per_second != 0) |
| 5419 | frame_timeout/=mng_info->ticks_per_second; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5420 | |
glennrp | bb010dd | 2010-06-01 13:07:15 +0000 | [diff] [blame] | 5421 | else |
| 5422 | frame_timeout=PNG_UINT_31_MAX; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5423 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5424 | if (change_delay == 2) |
| 5425 | default_frame_timeout=frame_timeout; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5426 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5427 | p+=4; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5428 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5429 | if (logging != MagickFalse) |
| 5430 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 5431 | " Framing_timeout=%.20g",(double) frame_timeout); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5432 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5433 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5434 | if (change_clipping) |
| 5435 | { |
| 5436 | fb=mng_read_box(previous_fb,(char) p[0],&p[1]); |
| 5437 | p+=17; |
| 5438 | previous_fb=fb; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5439 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5440 | if (logging != MagickFalse) |
| 5441 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5442 | " Frame_clip: L=%.20g R=%.20g T=%.20g B=%.20g", |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 5443 | (double) fb.left,(double) fb.right,(double) fb.top, |
| 5444 | (double) fb.bottom); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5445 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5446 | if (change_clipping == 2) |
| 5447 | default_fb=fb; |
| 5448 | } |
| 5449 | } |
| 5450 | } |
| 5451 | mng_info->clip=fb; |
| 5452 | mng_info->clip=mng_minimum_box(fb,mng_info->frame); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5453 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 5454 | subframe_width=(size_t) (mng_info->clip.right |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5455 | -mng_info->clip.left); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5456 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 5457 | subframe_height=(size_t) (mng_info->clip.bottom |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5458 | -mng_info->clip.top); |
| 5459 | /* |
| 5460 | Insert a background layer behind the frame if framing_mode is 4. |
| 5461 | */ |
| 5462 | #if defined(MNG_INSERT_LAYERS) |
| 5463 | if (logging != MagickFalse) |
| 5464 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 5465 | " subframe_width=%.20g, subframe_height=%.20g",(double) |
| 5466 | subframe_width,(double) subframe_height); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5467 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5468 | if (insert_layers && (mng_info->framing_mode == 4) && |
| 5469 | (subframe_width) && (subframe_height)) |
| 5470 | { |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5471 | /* Allocate next image structure. */ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 5472 | if (GetAuthenticPixelQueue(image) != (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5473 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 5474 | AcquireNextImage(image_info,image,exception); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5475 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5476 | if (GetNextImageInList(image) == (Image *) NULL) |
| 5477 | { |
| 5478 | image=DestroyImageList(image); |
| 5479 | MngInfoFreeStruct(mng_info,&have_mng_structure); |
| 5480 | return((Image *) NULL); |
| 5481 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5482 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5483 | image=SyncNextImageInList(image); |
| 5484 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5485 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5486 | mng_info->image=image; |
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 (term_chunk_found) |
| 5489 | { |
| 5490 | image->start_loop=MagickTrue; |
| 5491 | image->iterations=mng_iterations; |
| 5492 | term_chunk_found=MagickFalse; |
| 5493 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5494 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5495 | else |
| 5496 | image->start_loop=MagickFalse; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5497 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5498 | image->columns=subframe_width; |
| 5499 | image->rows=subframe_height; |
| 5500 | image->page.width=subframe_width; |
| 5501 | image->page.height=subframe_height; |
| 5502 | image->page.x=mng_info->clip.left; |
| 5503 | image->page.y=mng_info->clip.top; |
| 5504 | image->background_color=mng_background_color; |
| 5505 | image->matte=MagickFalse; |
| 5506 | image->delay=0; |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 5507 | (void) SetImageBackgroundColor(image,exception); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5508 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5509 | if (logging != MagickFalse) |
| 5510 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5511 | " Insert backgd layer, L=%.20g, R=%.20g T=%.20g, B=%.20g", |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 5512 | (double) mng_info->clip.left,(double) mng_info->clip.right, |
| 5513 | (double) mng_info->clip.top,(double) mng_info->clip.bottom); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5514 | } |
| 5515 | #endif |
| 5516 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 5517 | continue; |
| 5518 | } |
| 5519 | if (memcmp(type,mng_CLIP,4) == 0) |
| 5520 | { |
| 5521 | unsigned int |
| 5522 | first_object, |
| 5523 | last_object; |
| 5524 | |
| 5525 | /* |
| 5526 | Read CLIP. |
| 5527 | */ |
| 5528 | first_object=(p[0] << 8) | p[1]; |
| 5529 | last_object=(p[2] << 8) | p[3]; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5530 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5531 | for (i=(int) first_object; i <= (int) last_object; i++) |
| 5532 | { |
| 5533 | if (mng_info->exists[i] && !mng_info->frozen[i]) |
| 5534 | { |
| 5535 | MngBox |
| 5536 | box; |
| 5537 | |
| 5538 | box=mng_info->object_clip[i]; |
| 5539 | mng_info->object_clip[i]=mng_read_box(box,(char) p[4],&p[5]); |
| 5540 | } |
| 5541 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5542 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5543 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 5544 | continue; |
| 5545 | } |
| 5546 | if (memcmp(type,mng_SAVE,4) == 0) |
| 5547 | { |
| 5548 | for (i=1; i < MNG_MAX_OBJECTS; i++) |
| 5549 | if (mng_info->exists[i]) |
| 5550 | { |
| 5551 | mng_info->frozen[i]=MagickTrue; |
| 5552 | #ifdef MNG_OBJECT_BUFFERS |
| 5553 | if (mng_info->ob[i] != (MngBuffer *) NULL) |
| 5554 | mng_info->ob[i]->frozen=MagickTrue; |
| 5555 | #endif |
| 5556 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5557 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5558 | if (length) |
| 5559 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5560 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5561 | continue; |
| 5562 | } |
| 5563 | |
| 5564 | if ((memcmp(type,mng_DISC,4) == 0) || (memcmp(type,mng_SEEK,4) == 0)) |
| 5565 | { |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5566 | /* Read DISC or SEEK. */ |
| 5567 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5568 | if ((length == 0) || !memcmp(type,mng_SEEK,4)) |
| 5569 | { |
| 5570 | for (i=1; i < MNG_MAX_OBJECTS; i++) |
| 5571 | MngInfoDiscardObject(mng_info,i); |
| 5572 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5573 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5574 | else |
| 5575 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 5576 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5577 | j; |
| 5578 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 5579 | for (j=0; j < (ssize_t) length; j+=2) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5580 | { |
| 5581 | i=p[j] << 8 | p[j+1]; |
| 5582 | MngInfoDiscardObject(mng_info,i); |
| 5583 | } |
| 5584 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5585 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5586 | if (length) |
| 5587 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5588 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5589 | continue; |
| 5590 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5591 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5592 | if (memcmp(type,mng_MOVE,4) == 0) |
| 5593 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 5594 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5595 | first_object, |
| 5596 | last_object; |
| 5597 | |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5598 | /* read MOVE */ |
| 5599 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5600 | first_object=(p[0] << 8) | p[1]; |
| 5601 | last_object=(p[2] << 8) | p[3]; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 5602 | for (i=(ssize_t) first_object; i <= (ssize_t) last_object; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5603 | { |
| 5604 | if (mng_info->exists[i] && !mng_info->frozen[i]) |
| 5605 | { |
| 5606 | MngPair |
| 5607 | new_pair; |
| 5608 | |
| 5609 | MngPair |
| 5610 | old_pair; |
| 5611 | |
| 5612 | old_pair.a=mng_info->x_off[i]; |
| 5613 | old_pair.b=mng_info->y_off[i]; |
| 5614 | new_pair=mng_read_pair(old_pair,(int) p[4],&p[5]); |
| 5615 | mng_info->x_off[i]=new_pair.a; |
| 5616 | mng_info->y_off[i]=new_pair.b; |
| 5617 | } |
| 5618 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5619 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5620 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 5621 | continue; |
| 5622 | } |
| 5623 | |
| 5624 | if (memcmp(type,mng_LOOP,4) == 0) |
| 5625 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 5626 | ssize_t loop_iters=1; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5627 | loop_level=chunk[0]; |
| 5628 | mng_info->loop_active[loop_level]=1; /* mark loop active */ |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5629 | |
| 5630 | /* Record starting point. */ |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 5631 | loop_iters=mng_get_long(&chunk[1]); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5632 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5633 | if (logging != MagickFalse) |
| 5634 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 5635 | " LOOP level %.20g has %.20g iterations ",(double) loop_level, |
| 5636 | (double) loop_iters); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5637 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5638 | if (loop_iters == 0) |
| 5639 | skipping_loop=loop_level; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5640 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5641 | else |
| 5642 | { |
| 5643 | mng_info->loop_jump[loop_level]=TellBlob(image); |
| 5644 | mng_info->loop_count[loop_level]=loop_iters; |
| 5645 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5646 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5647 | mng_info->loop_iteration[loop_level]=0; |
| 5648 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 5649 | continue; |
| 5650 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5651 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5652 | if (memcmp(type,mng_ENDL,4) == 0) |
| 5653 | { |
| 5654 | loop_level=chunk[0]; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5655 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5656 | if (skipping_loop > 0) |
| 5657 | { |
| 5658 | if (skipping_loop == loop_level) |
| 5659 | { |
| 5660 | /* |
| 5661 | Found end of zero-iteration loop. |
| 5662 | */ |
| 5663 | skipping_loop=(-1); |
| 5664 | mng_info->loop_active[loop_level]=0; |
| 5665 | } |
| 5666 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5667 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5668 | else |
| 5669 | { |
| 5670 | if (mng_info->loop_active[loop_level] == 1) |
| 5671 | { |
| 5672 | mng_info->loop_count[loop_level]--; |
| 5673 | mng_info->loop_iteration[loop_level]++; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5674 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5675 | if (logging != MagickFalse) |
| 5676 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5677 | " ENDL: LOOP level %.20g has %.20g remaining iters ", |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 5678 | (double) loop_level,(double) |
cristy | f2faecf | 2010-05-28 19:19:36 +0000 | [diff] [blame] | 5679 | mng_info->loop_count[loop_level]); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5680 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5681 | if (mng_info->loop_count[loop_level] != 0) |
| 5682 | { |
| 5683 | offset=SeekBlob(image,mng_info->loop_jump[loop_level], |
| 5684 | SEEK_SET); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5685 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5686 | if (offset < 0) |
| 5687 | ThrowReaderException(CorruptImageError, |
| 5688 | "ImproperImageHeader"); |
| 5689 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5690 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5691 | else |
| 5692 | { |
| 5693 | short |
| 5694 | last_level; |
| 5695 | |
| 5696 | /* |
| 5697 | Finished loop. |
| 5698 | */ |
| 5699 | mng_info->loop_active[loop_level]=0; |
| 5700 | last_level=(-1); |
| 5701 | for (i=0; i < loop_level; i++) |
| 5702 | if (mng_info->loop_active[i] == 1) |
| 5703 | last_level=(short) i; |
| 5704 | loop_level=last_level; |
| 5705 | } |
| 5706 | } |
| 5707 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5708 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5709 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 5710 | continue; |
| 5711 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5712 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5713 | if (memcmp(type,mng_CLON,4) == 0) |
| 5714 | { |
| 5715 | if (mng_info->clon_warning == 0) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 5716 | (void) ThrowMagickException(exception,GetMagickModule(), |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5717 | CoderError,"CLON is not implemented yet","`%s'", |
| 5718 | image->filename); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5719 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5720 | mng_info->clon_warning++; |
| 5721 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5722 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5723 | if (memcmp(type,mng_MAGN,4) == 0) |
| 5724 | { |
| 5725 | png_uint_16 |
| 5726 | magn_first, |
| 5727 | magn_last, |
| 5728 | magn_mb, |
| 5729 | magn_ml, |
| 5730 | magn_mr, |
| 5731 | magn_mt, |
| 5732 | magn_mx, |
| 5733 | magn_my, |
| 5734 | magn_methx, |
| 5735 | magn_methy; |
| 5736 | |
| 5737 | if (length > 1) |
| 5738 | magn_first=(p[0] << 8) | p[1]; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5739 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5740 | else |
| 5741 | magn_first=0; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5742 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5743 | if (length > 3) |
| 5744 | magn_last=(p[2] << 8) | p[3]; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 5745 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5746 | else |
| 5747 | magn_last=magn_first; |
| 5748 | #ifndef MNG_OBJECT_BUFFERS |
| 5749 | if (magn_first || magn_last) |
| 5750 | if (mng_info->magn_warning == 0) |
| 5751 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 5752 | (void) ThrowMagickException(exception, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5753 | GetMagickModule(),CoderError, |
| 5754 | "MAGN is not implemented yet for nonzero objects", |
| 5755 | "`%s'",image->filename); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5756 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5757 | mng_info->magn_warning++; |
| 5758 | } |
| 5759 | #endif |
| 5760 | if (length > 4) |
| 5761 | magn_methx=p[4]; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5762 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5763 | else |
| 5764 | magn_methx=0; |
| 5765 | |
| 5766 | if (length > 6) |
| 5767 | magn_mx=(p[5] << 8) | p[6]; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5768 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5769 | else |
| 5770 | magn_mx=1; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5771 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5772 | if (magn_mx == 0) |
| 5773 | magn_mx=1; |
| 5774 | |
| 5775 | if (length > 8) |
| 5776 | magn_my=(p[7] << 8) | p[8]; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5777 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5778 | else |
| 5779 | magn_my=magn_mx; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5780 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5781 | if (magn_my == 0) |
| 5782 | magn_my=1; |
| 5783 | |
| 5784 | if (length > 10) |
| 5785 | magn_ml=(p[9] << 8) | p[10]; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5786 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5787 | else |
| 5788 | magn_ml=magn_mx; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5789 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5790 | if (magn_ml == 0) |
| 5791 | magn_ml=1; |
| 5792 | |
| 5793 | if (length > 12) |
| 5794 | magn_mr=(p[11] << 8) | p[12]; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5795 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5796 | else |
| 5797 | magn_mr=magn_mx; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5798 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5799 | if (magn_mr == 0) |
| 5800 | magn_mr=1; |
| 5801 | |
| 5802 | if (length > 14) |
| 5803 | magn_mt=(p[13] << 8) | p[14]; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5804 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5805 | else |
| 5806 | magn_mt=magn_my; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5807 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5808 | if (magn_mt == 0) |
| 5809 | magn_mt=1; |
| 5810 | |
| 5811 | if (length > 16) |
| 5812 | magn_mb=(p[15] << 8) | p[16]; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5813 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5814 | else |
| 5815 | magn_mb=magn_my; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5816 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5817 | if (magn_mb == 0) |
| 5818 | magn_mb=1; |
| 5819 | |
| 5820 | if (length > 17) |
| 5821 | magn_methy=p[17]; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5822 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5823 | else |
| 5824 | magn_methy=magn_methx; |
| 5825 | |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5826 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5827 | if (magn_methx > 5 || magn_methy > 5) |
| 5828 | if (mng_info->magn_warning == 0) |
| 5829 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 5830 | (void) ThrowMagickException(exception, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5831 | GetMagickModule(),CoderError, |
| 5832 | "Unknown MAGN method in MNG datastream","`%s'", |
| 5833 | image->filename); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5834 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5835 | mng_info->magn_warning++; |
| 5836 | } |
| 5837 | #ifdef MNG_OBJECT_BUFFERS |
| 5838 | /* Magnify existing objects in the range magn_first to magn_last */ |
| 5839 | #endif |
| 5840 | if (magn_first == 0 || magn_last == 0) |
| 5841 | { |
| 5842 | /* Save the magnification factors for object 0 */ |
| 5843 | mng_info->magn_mb=magn_mb; |
| 5844 | mng_info->magn_ml=magn_ml; |
| 5845 | mng_info->magn_mr=magn_mr; |
| 5846 | mng_info->magn_mt=magn_mt; |
| 5847 | mng_info->magn_mx=magn_mx; |
| 5848 | mng_info->magn_my=magn_my; |
| 5849 | mng_info->magn_methx=magn_methx; |
| 5850 | mng_info->magn_methy=magn_methy; |
| 5851 | } |
| 5852 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5853 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5854 | if (memcmp(type,mng_PAST,4) == 0) |
| 5855 | { |
| 5856 | if (mng_info->past_warning == 0) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 5857 | (void) ThrowMagickException(exception,GetMagickModule(), |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5858 | CoderError,"PAST is not implemented yet","`%s'", |
| 5859 | image->filename); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5860 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5861 | mng_info->past_warning++; |
| 5862 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5863 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5864 | if (memcmp(type,mng_SHOW,4) == 0) |
| 5865 | { |
| 5866 | if (mng_info->show_warning == 0) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 5867 | (void) ThrowMagickException(exception,GetMagickModule(), |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5868 | CoderError,"SHOW is not implemented yet","`%s'", |
| 5869 | image->filename); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5870 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5871 | mng_info->show_warning++; |
| 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_sBIT,4) == 0) |
| 5875 | { |
| 5876 | if (length < 4) |
| 5877 | mng_info->have_global_sbit=MagickFalse; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5878 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5879 | else |
| 5880 | { |
| 5881 | mng_info->global_sbit.gray=p[0]; |
| 5882 | mng_info->global_sbit.red=p[0]; |
| 5883 | mng_info->global_sbit.green=p[1]; |
| 5884 | mng_info->global_sbit.blue=p[2]; |
| 5885 | mng_info->global_sbit.alpha=p[3]; |
| 5886 | mng_info->have_global_sbit=MagickTrue; |
| 5887 | } |
| 5888 | } |
| 5889 | if (memcmp(type,mng_pHYs,4) == 0) |
| 5890 | { |
| 5891 | if (length > 8) |
| 5892 | { |
| 5893 | mng_info->global_x_pixels_per_unit= |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 5894 | (size_t) mng_get_long(p); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5895 | mng_info->global_y_pixels_per_unit= |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 5896 | (size_t) mng_get_long(&p[4]); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5897 | mng_info->global_phys_unit_type=p[8]; |
| 5898 | mng_info->have_global_phys=MagickTrue; |
| 5899 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5900 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5901 | else |
| 5902 | mng_info->have_global_phys=MagickFalse; |
| 5903 | } |
| 5904 | if (memcmp(type,mng_pHYg,4) == 0) |
| 5905 | { |
| 5906 | if (mng_info->phyg_warning == 0) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 5907 | (void) ThrowMagickException(exception,GetMagickModule(), |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5908 | CoderError,"pHYg is not implemented.","`%s'",image->filename); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5909 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5910 | mng_info->phyg_warning++; |
| 5911 | } |
| 5912 | if (memcmp(type,mng_BASI,4) == 0) |
| 5913 | { |
| 5914 | skip_to_iend=MagickTrue; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5915 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5916 | if (mng_info->basi_warning == 0) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 5917 | (void) ThrowMagickException(exception,GetMagickModule(), |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5918 | CoderError,"BASI is not implemented yet","`%s'", |
| 5919 | image->filename); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5920 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5921 | mng_info->basi_warning++; |
| 5922 | #ifdef MNG_BASI_SUPPORTED |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 5923 | basi_width=(size_t) ((p[0] << 24) | (p[1] << 16) | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5924 | (p[2] << 8) | p[3]); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 5925 | basi_height=(size_t) ((p[4] << 24) | (p[5] << 16) | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5926 | (p[6] << 8) | p[7]); |
| 5927 | basi_color_type=p[8]; |
| 5928 | basi_compression_method=p[9]; |
| 5929 | basi_filter_type=p[10]; |
| 5930 | basi_interlace_method=p[11]; |
| 5931 | if (length > 11) |
| 5932 | basi_red=(p[12] << 8) & p[13]; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5933 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5934 | else |
| 5935 | basi_red=0; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5936 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5937 | if (length > 13) |
| 5938 | basi_green=(p[14] << 8) & p[15]; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5939 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5940 | else |
| 5941 | basi_green=0; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5942 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5943 | if (length > 15) |
| 5944 | basi_blue=(p[16] << 8) & p[17]; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5945 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5946 | else |
| 5947 | basi_blue=0; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5948 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5949 | if (length > 17) |
| 5950 | basi_alpha=(p[18] << 8) & p[19]; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5951 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5952 | else |
| 5953 | { |
| 5954 | if (basi_sample_depth == 16) |
| 5955 | basi_alpha=65535L; |
| 5956 | else |
| 5957 | basi_alpha=255; |
| 5958 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5959 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5960 | if (length > 19) |
| 5961 | basi_viewable=p[20]; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5962 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5963 | else |
| 5964 | basi_viewable=0; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5965 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5966 | #endif |
| 5967 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 5968 | continue; |
| 5969 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5970 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5971 | if (memcmp(type,mng_IHDR,4) |
| 5972 | #if defined(JNG_SUPPORTED) |
| 5973 | && memcmp(type,mng_JHDR,4) |
| 5974 | #endif |
| 5975 | ) |
| 5976 | { |
| 5977 | /* Not an IHDR or JHDR chunk */ |
| 5978 | if (length) |
| 5979 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5980 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5981 | continue; |
| 5982 | } |
| 5983 | /* Process IHDR */ |
| 5984 | if (logging != MagickFalse) |
| 5985 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 5986 | " 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] | 5987 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5988 | mng_info->exists[object_id]=MagickTrue; |
| 5989 | mng_info->viewable[object_id]=MagickTrue; |
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 (mng_info->invisible[object_id]) |
| 5992 | { |
| 5993 | if (logging != MagickFalse) |
| 5994 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 5995 | " Skipping invisible object"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 5996 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 5997 | skip_to_iend=MagickTrue; |
| 5998 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 5999 | continue; |
| 6000 | } |
| 6001 | #if defined(MNG_INSERT_LAYERS) |
| 6002 | if (length < 8) |
| 6003 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6004 | |
cristy | 8182b07 | 2010-05-30 20:10:53 +0000 | [diff] [blame] | 6005 | image_width=(size_t) mng_get_long(p); |
| 6006 | image_height=(size_t) mng_get_long(&p[4]); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6007 | #endif |
| 6008 | chunk=(unsigned char *) RelinquishMagickMemory(chunk); |
| 6009 | |
| 6010 | /* |
| 6011 | Insert a transparent background layer behind the entire animation |
| 6012 | if it is not full screen. |
| 6013 | */ |
| 6014 | #if defined(MNG_INSERT_LAYERS) |
| 6015 | if (insert_layers && mng_type && first_mng_object) |
| 6016 | { |
| 6017 | if ((mng_info->clip.left > 0) || (mng_info->clip.top > 0) || |
| 6018 | (image_width < mng_info->mng_width) || |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 6019 | (mng_info->clip.right < (ssize_t) mng_info->mng_width) || |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6020 | (image_height < mng_info->mng_height) || |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 6021 | (mng_info->clip.bottom < (ssize_t) mng_info->mng_height)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6022 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6023 | if (GetAuthenticPixelQueue(image) != (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6024 | { |
| 6025 | /* |
| 6026 | Allocate next image structure. |
| 6027 | */ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6028 | AcquireNextImage(image_info,image,exception); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6029 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6030 | if (GetNextImageInList(image) == (Image *) NULL) |
| 6031 | { |
| 6032 | image=DestroyImageList(image); |
| 6033 | MngInfoFreeStruct(mng_info,&have_mng_structure); |
| 6034 | return((Image *) NULL); |
| 6035 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6036 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6037 | image=SyncNextImageInList(image); |
| 6038 | } |
| 6039 | mng_info->image=image; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6040 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6041 | if (term_chunk_found) |
| 6042 | { |
| 6043 | image->start_loop=MagickTrue; |
| 6044 | image->iterations=mng_iterations; |
| 6045 | term_chunk_found=MagickFalse; |
| 6046 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6047 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6048 | else |
| 6049 | image->start_loop=MagickFalse; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6050 | |
| 6051 | /* Make a background rectangle. */ |
| 6052 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6053 | image->delay=0; |
| 6054 | image->columns=mng_info->mng_width; |
| 6055 | image->rows=mng_info->mng_height; |
| 6056 | image->page.width=mng_info->mng_width; |
| 6057 | image->page.height=mng_info->mng_height; |
| 6058 | image->page.x=0; |
| 6059 | image->page.y=0; |
| 6060 | image->background_color=mng_background_color; |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6061 | (void) SetImageBackgroundColor(image,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6062 | if (logging != MagickFalse) |
| 6063 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 6064 | " Inserted transparent background layer, W=%.20g, H=%.20g", |
| 6065 | (double) mng_info->mng_width,(double) mng_info->mng_height); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6066 | } |
| 6067 | } |
| 6068 | /* |
| 6069 | Insert a background layer behind the upcoming image if |
| 6070 | framing_mode is 3, and we haven't already inserted one. |
| 6071 | */ |
| 6072 | if (insert_layers && (mng_info->framing_mode == 3) && |
| 6073 | (subframe_width) && (subframe_height) && (simplicity == 0 || |
| 6074 | (simplicity & 0x08))) |
| 6075 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6076 | if (GetAuthenticPixelQueue(image) != (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6077 | { |
| 6078 | /* |
| 6079 | Allocate next image structure. |
| 6080 | */ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6081 | AcquireNextImage(image_info,image,exception); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6082 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6083 | if (GetNextImageInList(image) == (Image *) NULL) |
| 6084 | { |
| 6085 | image=DestroyImageList(image); |
| 6086 | MngInfoFreeStruct(mng_info,&have_mng_structure); |
| 6087 | return((Image *) NULL); |
| 6088 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6089 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6090 | image=SyncNextImageInList(image); |
| 6091 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 6092 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6093 | mng_info->image=image; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 6094 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6095 | if (term_chunk_found) |
| 6096 | { |
| 6097 | image->start_loop=MagickTrue; |
| 6098 | image->iterations=mng_iterations; |
| 6099 | term_chunk_found=MagickFalse; |
| 6100 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 6101 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6102 | else |
| 6103 | image->start_loop=MagickFalse; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6104 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6105 | image->delay=0; |
| 6106 | image->columns=subframe_width; |
| 6107 | image->rows=subframe_height; |
| 6108 | image->page.width=subframe_width; |
| 6109 | image->page.height=subframe_height; |
| 6110 | image->page.x=mng_info->clip.left; |
| 6111 | image->page.y=mng_info->clip.top; |
| 6112 | image->background_color=mng_background_color; |
| 6113 | image->matte=MagickFalse; |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6114 | (void) SetImageBackgroundColor(image,exception); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 6115 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6116 | if (logging != MagickFalse) |
| 6117 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 6118 | " Insert background layer, L=%.20g, R=%.20g T=%.20g, B=%.20g", |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 6119 | (double) mng_info->clip.left,(double) mng_info->clip.right, |
| 6120 | (double) mng_info->clip.top,(double) mng_info->clip.bottom); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6121 | } |
| 6122 | #endif /* MNG_INSERT_LAYERS */ |
| 6123 | first_mng_object=MagickFalse; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6124 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6125 | if (GetAuthenticPixelQueue(image) != (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6126 | { |
| 6127 | /* |
| 6128 | Allocate next image structure. |
| 6129 | */ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6130 | AcquireNextImage(image_info,image,exception); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6131 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6132 | if (GetNextImageInList(image) == (Image *) NULL) |
| 6133 | { |
| 6134 | image=DestroyImageList(image); |
| 6135 | MngInfoFreeStruct(mng_info,&have_mng_structure); |
| 6136 | return((Image *) NULL); |
| 6137 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6138 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6139 | image=SyncNextImageInList(image); |
| 6140 | } |
| 6141 | mng_info->image=image; |
| 6142 | status=SetImageProgress(image,LoadImagesTag,TellBlob(image), |
| 6143 | GetBlobSize(image)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 6144 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6145 | if (status == MagickFalse) |
| 6146 | break; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 6147 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6148 | if (term_chunk_found) |
| 6149 | { |
| 6150 | image->start_loop=MagickTrue; |
| 6151 | term_chunk_found=MagickFalse; |
| 6152 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 6153 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6154 | else |
| 6155 | image->start_loop=MagickFalse; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 6156 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6157 | if (mng_info->framing_mode == 1 || mng_info->framing_mode == 3) |
| 6158 | { |
| 6159 | image->delay=frame_delay; |
| 6160 | frame_delay=default_frame_delay; |
| 6161 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 6162 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6163 | else |
| 6164 | image->delay=0; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 6165 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6166 | image->page.width=mng_info->mng_width; |
| 6167 | image->page.height=mng_info->mng_height; |
| 6168 | image->page.x=mng_info->x_off[object_id]; |
| 6169 | image->page.y=mng_info->y_off[object_id]; |
| 6170 | image->iterations=mng_iterations; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6171 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6172 | /* |
| 6173 | Seek back to the beginning of the IHDR or JHDR chunk's length field. |
| 6174 | */ |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6175 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6176 | if (logging != MagickFalse) |
| 6177 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 6178 | " Seeking back to beginning of %c%c%c%c chunk",type[0],type[1], |
| 6179 | type[2],type[3]); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6180 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 6181 | offset=SeekBlob(image,-((ssize_t) length+12),SEEK_CUR); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6182 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6183 | if (offset < 0) |
| 6184 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
| 6185 | } |
| 6186 | |
| 6187 | previous=image; |
| 6188 | mng_info->image=image; |
| 6189 | mng_info->mng_type=mng_type; |
| 6190 | mng_info->object_id=object_id; |
| 6191 | |
| 6192 | if (memcmp(type,mng_IHDR,4) == 0) |
| 6193 | image=ReadOnePNGImage(mng_info,image_info,exception); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6194 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6195 | #if defined(JNG_SUPPORTED) |
| 6196 | else |
| 6197 | image=ReadOneJNGImage(mng_info,image_info,exception); |
| 6198 | #endif |
| 6199 | |
| 6200 | if (image == (Image *) NULL) |
| 6201 | { |
| 6202 | if (IsImageObject(previous) != MagickFalse) |
| 6203 | { |
| 6204 | (void) DestroyImageList(previous); |
| 6205 | (void) CloseBlob(previous); |
| 6206 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6207 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6208 | MngInfoFreeStruct(mng_info,&have_mng_structure); |
| 6209 | return((Image *) NULL); |
| 6210 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 6211 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6212 | if (image->columns == 0 || image->rows == 0) |
| 6213 | { |
| 6214 | (void) CloseBlob(image); |
| 6215 | image=DestroyImageList(image); |
| 6216 | MngInfoFreeStruct(mng_info,&have_mng_structure); |
| 6217 | return((Image *) NULL); |
| 6218 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 6219 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6220 | mng_info->image=image; |
| 6221 | |
| 6222 | if (mng_type) |
| 6223 | { |
| 6224 | MngBox |
| 6225 | crop_box; |
| 6226 | |
| 6227 | if (mng_info->magn_methx || mng_info->magn_methy) |
| 6228 | { |
| 6229 | png_uint_32 |
| 6230 | magnified_height, |
| 6231 | magnified_width; |
| 6232 | |
| 6233 | if (logging != MagickFalse) |
| 6234 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 6235 | " Processing MNG MAGN chunk"); |
| 6236 | |
| 6237 | if (mng_info->magn_methx == 1) |
| 6238 | { |
| 6239 | magnified_width=mng_info->magn_ml; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6240 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6241 | if (image->columns > 1) |
| 6242 | magnified_width += mng_info->magn_mr; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6243 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6244 | if (image->columns > 2) |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6245 | magnified_width += (png_uint_32) |
| 6246 | ((image->columns-2)*(mng_info->magn_mx)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6247 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6248 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6249 | else |
| 6250 | { |
cristy | 4e5bc84 | 2010-06-09 13:56:01 +0000 | [diff] [blame] | 6251 | magnified_width=(png_uint_32) image->columns; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6252 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6253 | if (image->columns > 1) |
| 6254 | magnified_width += mng_info->magn_ml-1; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6255 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6256 | if (image->columns > 2) |
| 6257 | magnified_width += mng_info->magn_mr-1; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6258 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6259 | if (image->columns > 3) |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6260 | magnified_width += (png_uint_32) |
| 6261 | ((image->columns-3)*(mng_info->magn_mx-1)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6262 | } |
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 (mng_info->magn_methy == 1) |
| 6265 | { |
| 6266 | magnified_height=mng_info->magn_mt; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6267 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6268 | if (image->rows > 1) |
| 6269 | magnified_height += mng_info->magn_mb; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6270 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6271 | if (image->rows > 2) |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6272 | magnified_height += (png_uint_32) |
| 6273 | ((image->rows-2)*(mng_info->magn_my)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6274 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6275 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6276 | else |
| 6277 | { |
cristy | 4e5bc84 | 2010-06-09 13:56:01 +0000 | [diff] [blame] | 6278 | magnified_height=(png_uint_32) image->rows; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6279 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6280 | if (image->rows > 1) |
| 6281 | magnified_height += mng_info->magn_mt-1; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6282 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6283 | if (image->rows > 2) |
| 6284 | magnified_height += mng_info->magn_mb-1; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6285 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6286 | if (image->rows > 3) |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6287 | magnified_height += (png_uint_32) |
| 6288 | ((image->rows-3)*(mng_info->magn_my-1)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6289 | } |
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 (magnified_height > image->rows || |
| 6292 | magnified_width > image->columns) |
| 6293 | { |
| 6294 | Image |
| 6295 | *large_image; |
| 6296 | |
| 6297 | int |
| 6298 | yy; |
| 6299 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6300 | Quantum |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6301 | *next, |
| 6302 | *prev; |
| 6303 | |
| 6304 | png_uint_16 |
| 6305 | magn_methx, |
| 6306 | magn_methy; |
| 6307 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6308 | ssize_t |
| 6309 | m, |
| 6310 | y; |
| 6311 | |
| 6312 | register Quantum |
| 6313 | *n, |
| 6314 | *q; |
| 6315 | |
| 6316 | register ssize_t |
| 6317 | x; |
| 6318 | |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6319 | /* Allocate next image structure. */ |
| 6320 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6321 | if (logging != MagickFalse) |
| 6322 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 6323 | " Allocate magnified image"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6324 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6325 | AcquireNextImage(image_info,image,exception); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6326 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6327 | if (GetNextImageInList(image) == (Image *) NULL) |
| 6328 | { |
| 6329 | image=DestroyImageList(image); |
| 6330 | MngInfoFreeStruct(mng_info,&have_mng_structure); |
| 6331 | return((Image *) NULL); |
| 6332 | } |
| 6333 | |
| 6334 | large_image=SyncNextImageInList(image); |
| 6335 | |
| 6336 | large_image->columns=magnified_width; |
| 6337 | large_image->rows=magnified_height; |
| 6338 | |
| 6339 | magn_methx=mng_info->magn_methx; |
| 6340 | magn_methy=mng_info->magn_methy; |
| 6341 | |
glennrp | 3faa9a3 | 2011-04-23 14:00:25 +0000 | [diff] [blame] | 6342 | #if (MAGICKCORE_QUANTUM_DEPTH > 16) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6343 | #define QM unsigned short |
| 6344 | if (magn_methx != 1 || magn_methy != 1) |
| 6345 | { |
| 6346 | /* |
| 6347 | Scale pixels to unsigned shorts to prevent |
| 6348 | overflow of intermediate values of interpolations |
| 6349 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 6350 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6351 | { |
| 6352 | q=GetAuthenticPixels(image,0,y,image->columns,1, |
| 6353 | exception); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6354 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 6355 | for (x=(ssize_t) image->columns-1; x >= 0; x--) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6356 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6357 | SetPixelRed(image,ScaleQuantumToShort( |
| 6358 | GetPixelRed(image,q)),q); |
| 6359 | SetPixelGreen(image,ScaleQuantumToShort( |
| 6360 | GetPixelGreen(image,q)),q); |
| 6361 | SetPixelBlue(image,ScaleQuantumToShort( |
| 6362 | GetPixelBlue(image,q)),q); |
| 6363 | SetPixelAlpha(image,ScaleQuantumToShort( |
| 6364 | GetPixelAlpha(image,q)),q); |
| 6365 | q+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6366 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6367 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6368 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 6369 | break; |
| 6370 | } |
| 6371 | } |
| 6372 | #else |
| 6373 | #define QM Quantum |
| 6374 | #endif |
| 6375 | |
| 6376 | if (image->matte != MagickFalse) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6377 | (void) SetImageBackgroundColor(large_image,exception); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6378 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6379 | else |
| 6380 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6381 | large_image->background_color.alpha=OpaqueAlpha; |
| 6382 | (void) SetImageBackgroundColor(large_image,exception); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6383 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6384 | if (magn_methx == 4) |
| 6385 | magn_methx=2; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6386 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6387 | if (magn_methx == 5) |
| 6388 | magn_methx=3; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6389 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6390 | if (magn_methy == 4) |
| 6391 | magn_methy=2; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6392 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6393 | if (magn_methy == 5) |
| 6394 | magn_methy=3; |
| 6395 | } |
| 6396 | |
| 6397 | /* magnify the rows into the right side of the large image */ |
| 6398 | |
| 6399 | if (logging != MagickFalse) |
| 6400 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 6401 | " Magnify the rows to %.20g",(double) large_image->rows); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 6402 | m=(ssize_t) mng_info->magn_mt; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6403 | yy=0; |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6404 | length=(size_t) image->columns*GetPixelChannels(image); |
| 6405 | next=(Quantum *) AcquireQuantumMemory(length,sizeof(*next)); |
| 6406 | prev=(Quantum *) AcquireQuantumMemory(length,sizeof(*prev)); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6407 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6408 | if ((prev == (Quantum *) NULL) || |
| 6409 | (next == (Quantum *) NULL)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6410 | { |
| 6411 | image=DestroyImageList(image); |
| 6412 | MngInfoFreeStruct(mng_info,&have_mng_structure); |
| 6413 | ThrowReaderException(ResourceLimitError, |
| 6414 | "MemoryAllocationFailed"); |
| 6415 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6416 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6417 | n=GetAuthenticPixels(image,0,0,image->columns,1,exception); |
| 6418 | (void) CopyMagickMemory(next,n,length); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6419 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 6420 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6421 | { |
| 6422 | if (y == 0) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 6423 | m=(ssize_t) mng_info->magn_mt; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6424 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 6425 | else if (magn_methy > 1 && y == (ssize_t) image->rows-2) |
| 6426 | m=(ssize_t) mng_info->magn_mb; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6427 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 6428 | else if (magn_methy <= 1 && y == (ssize_t) image->rows-1) |
| 6429 | m=(ssize_t) mng_info->magn_mb; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6430 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 6431 | else if (magn_methy > 1 && y == (ssize_t) image->rows-1) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6432 | m=1; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6433 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6434 | else |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 6435 | m=(ssize_t) mng_info->magn_my; |
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=prev; |
| 6438 | prev=next; |
| 6439 | next=n; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6440 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 6441 | if (y < (ssize_t) image->rows-1) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6442 | { |
| 6443 | n=GetAuthenticPixels(image,0,y+1,image->columns,1, |
| 6444 | exception); |
| 6445 | (void) CopyMagickMemory(next,n,length); |
| 6446 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6447 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6448 | for (i=0; i < m; i++, yy++) |
| 6449 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6450 | register Quantum |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6451 | *pixels; |
| 6452 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 6453 | assert(yy < (ssize_t) large_image->rows); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6454 | pixels=prev; |
| 6455 | n=next; |
| 6456 | q=GetAuthenticPixels(large_image,0,yy,large_image->columns, |
cristy | 9fff7b4 | 2011-04-29 01:09:31 +0000 | [diff] [blame] | 6457 | 1,exception); |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6458 | q+=(large_image->columns-image->columns)* |
| 6459 | GetPixelChannels(large_image); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6460 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 6461 | for (x=(ssize_t) image->columns-1; x >= 0; x--) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6462 | { |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 6463 | /* To do: get color as function of indexes[x] */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6464 | /* |
| 6465 | if (image->storage_class == PseudoClass) |
| 6466 | { |
| 6467 | } |
| 6468 | */ |
| 6469 | |
| 6470 | if (magn_methy <= 1) |
| 6471 | { |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 6472 | /* replicate previous */ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6473 | SetPixelRed(large_image,GetPixelRed(image,pixels),q); |
| 6474 | SetPixelGreen(large_image,GetPixelGreen(image, |
| 6475 | pixels),q); |
| 6476 | SetPixelBlue(large_image,GetPixelBlue(image, |
| 6477 | pixels),q); |
| 6478 | SetPixelAlpha(large_image,GetPixelAlpha(image, |
| 6479 | pixels),q); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6480 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6481 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6482 | else if (magn_methy == 2 || magn_methy == 4) |
| 6483 | { |
| 6484 | if (i == 0) |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 6485 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6486 | SetPixelRed(large_image,GetPixelRed(image, |
| 6487 | pixels),q); |
| 6488 | SetPixelGreen(large_image,GetPixelGreen(image, |
| 6489 | pixels),q); |
| 6490 | SetPixelBlue(large_image,GetPixelBlue(image, |
| 6491 | pixels),q); |
| 6492 | SetPixelAlpha(large_image,GetPixelAlpha(image, |
| 6493 | pixels),q); |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 6494 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6495 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6496 | else |
| 6497 | { |
| 6498 | /* Interpolate */ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6499 | SetPixelRed(large_image,((QM) (((ssize_t) |
| 6500 | (2*i*(GetPixelRed(image,n) |
| 6501 | -GetPixelRed(image,pixels)+m))/ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 6502 | ((ssize_t) (m*2)) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6503 | +GetPixelRed(image,pixels)))),q); |
| 6504 | SetPixelGreen(large_image,((QM) (((ssize_t) |
| 6505 | (2*i*(GetPixelGreen(image,n) |
| 6506 | -GetPixelGreen(image,pixels)+m))/ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 6507 | ((ssize_t) (m*2)) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6508 | +GetPixelGreen(image,pixels)))),q); |
| 6509 | SetPixelBlue(large_image,((QM) (((ssize_t) |
| 6510 | (2*i*(GetPixelBlue(image,n) |
| 6511 | -GetPixelBlue(image,pixels)+m))/ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 6512 | ((ssize_t) (m*2)) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6513 | +GetPixelBlue(image,pixels)))),q); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6514 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6515 | if (image->matte != MagickFalse) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6516 | SetPixelAlpha(large_image, ((QM) (((ssize_t) |
| 6517 | (2*i*(GetPixelAlpha(image,n) |
| 6518 | -GetPixelAlpha(image,pixels)+m)) |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 6519 | /((ssize_t) (m*2))+ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6520 | GetPixelAlpha(image,pixels)))),q); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6521 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6522 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6523 | if (magn_methy == 4) |
| 6524 | { |
| 6525 | /* Replicate nearest */ |
| 6526 | if (i <= ((m+1) << 1)) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6527 | SetPixelAlpha(large_image,GetPixelAlpha(image, |
| 6528 | pixels),q); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6529 | else |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6530 | SetPixelAlpha(large_image,GetPixelAlpha(image, |
| 6531 | n),q); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6532 | } |
| 6533 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6534 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6535 | else /* if (magn_methy == 3 || magn_methy == 5) */ |
| 6536 | { |
| 6537 | /* Replicate nearest */ |
| 6538 | if (i <= ((m+1) << 1)) |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 6539 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6540 | SetPixelRed(large_image,GetPixelRed(image, |
| 6541 | pixels),q); |
| 6542 | SetPixelGreen(large_image,GetPixelGreen(image, |
| 6543 | pixels),q); |
| 6544 | SetPixelBlue(large_image,GetPixelBlue(image, |
| 6545 | pixels),q); |
| 6546 | SetPixelAlpha(large_image,GetPixelAlpha(image, |
| 6547 | pixels),q); |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 6548 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6549 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6550 | else |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 6551 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6552 | SetPixelRed(large_image,GetPixelRed(image,n),q); |
| 6553 | SetPixelGreen(large_image,GetPixelGreen(image,n), |
| 6554 | q); |
| 6555 | SetPixelBlue(large_image,GetPixelBlue(image,n), |
| 6556 | q); |
| 6557 | SetPixelAlpha(large_image,GetPixelAlpha(image,n), |
| 6558 | q); |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 6559 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6560 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6561 | if (magn_methy == 5) |
| 6562 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6563 | SetPixelAlpha(large_image,(QM) (((ssize_t) (2*i* |
| 6564 | (GetPixelAlpha(image,n) |
| 6565 | -GetPixelAlpha(image,pixels)) |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 6566 | +m))/((ssize_t) (m*2)) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6567 | +GetPixelAlpha(image,pixels)),q); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6568 | } |
| 6569 | } |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6570 | n+=GetPixelChannels(image); |
| 6571 | q+=GetPixelChannels(large_image); |
| 6572 | pixels+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6573 | } /* x */ |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6574 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6575 | if (SyncAuthenticPixels(large_image,exception) == 0) |
| 6576 | break; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6577 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6578 | } /* i */ |
| 6579 | } /* y */ |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6580 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6581 | prev=(Quantum *) RelinquishMagickMemory(prev); |
| 6582 | next=(Quantum *) RelinquishMagickMemory(next); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6583 | |
| 6584 | length=image->columns; |
| 6585 | |
| 6586 | if (logging != MagickFalse) |
| 6587 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 6588 | " Delete original image"); |
| 6589 | |
| 6590 | DeleteImageFromList(&image); |
| 6591 | |
| 6592 | image=large_image; |
| 6593 | |
| 6594 | mng_info->image=image; |
| 6595 | |
| 6596 | /* magnify the columns */ |
| 6597 | if (logging != MagickFalse) |
| 6598 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 6599 | " Magnify the columns to %.20g",(double) image->columns); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6600 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 6601 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6602 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6603 | register Quantum |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6604 | *pixels; |
| 6605 | |
| 6606 | q=GetAuthenticPixels(image,0,y,image->columns,1,exception); |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6607 | pixels=q+(image->columns-length)*GetPixelChannels(image); |
| 6608 | n=pixels+GetPixelChannels(image); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6609 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 6610 | for (x=(ssize_t) (image->columns-length); |
| 6611 | x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6612 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6613 | /* To do: Rewrite using Get/Set***PixelChannel() */ |
glennrp | 7c7b315 | 2011-04-26 04:01:27 +0000 | [diff] [blame] | 6614 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 6615 | if (x == (ssize_t) (image->columns-length)) |
| 6616 | m=(ssize_t) mng_info->magn_ml; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6617 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 6618 | else if (magn_methx > 1 && x == (ssize_t) image->columns-2) |
| 6619 | m=(ssize_t) mng_info->magn_mr; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6620 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 6621 | else if (magn_methx <= 1 && x == (ssize_t) image->columns-1) |
| 6622 | m=(ssize_t) mng_info->magn_mr; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6623 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 6624 | else if (magn_methx > 1 && x == (ssize_t) image->columns-1) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6625 | m=1; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6626 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6627 | else |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 6628 | m=(ssize_t) mng_info->magn_mx; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6629 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6630 | for (i=0; i < m; i++) |
| 6631 | { |
| 6632 | if (magn_methx <= 1) |
| 6633 | { |
| 6634 | /* replicate previous */ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6635 | SetPixelRed(image,GetPixelRed(image,pixels),q); |
| 6636 | SetPixelGreen(image,GetPixelGreen(image,pixels),q); |
| 6637 | SetPixelBlue(image,GetPixelBlue(image,pixels),q); |
| 6638 | SetPixelAlpha(image,GetPixelAlpha(image,pixels),q); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6639 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6640 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6641 | else if (magn_methx == 2 || magn_methx == 4) |
| 6642 | { |
| 6643 | if (i == 0) |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 6644 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6645 | SetPixelRed(image,GetPixelRed(image,pixels),q); |
| 6646 | SetPixelGreen(image,GetPixelGreen(image,pixels),q); |
| 6647 | SetPixelBlue(image,GetPixelBlue(image,pixels),q); |
| 6648 | SetPixelAlpha(image,GetPixelAlpha(image,pixels),q); |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 6649 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6650 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6651 | /* To do: Rewrite using Get/Set***PixelChannel() */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6652 | else |
| 6653 | { |
| 6654 | /* Interpolate */ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6655 | SetPixelRed(image,(QM) ((2*i*( |
| 6656 | GetPixelRed(image,n) |
| 6657 | -GetPixelRed(image,pixels))+m) |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 6658 | /((ssize_t) (m*2))+ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6659 | GetPixelRed(image,pixels)),q); |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 6660 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6661 | SetPixelGreen(image,(QM) ((2*i*( |
| 6662 | GetPixelGreen(image,n) |
| 6663 | -GetPixelGreen(image,pixels))+m) |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 6664 | /((ssize_t) (m*2))+ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6665 | GetPixelGreen(image,pixels)),q); |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 6666 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6667 | SetPixelBlue(image,(QM) ((2*i*( |
| 6668 | GetPixelBlue(image,n) |
| 6669 | -GetPixelBlue(image,pixels))+m) |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 6670 | /((ssize_t) (m*2))+ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6671 | GetPixelBlue(image,pixels)),q); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6672 | if (image->matte != MagickFalse) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6673 | SetPixelAlpha(image,(QM) ((2*i*( |
| 6674 | GetPixelAlpha(image,n) |
| 6675 | -GetPixelAlpha(image,pixels))+m) |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 6676 | /((ssize_t) (m*2))+ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6677 | GetPixelAlpha(image,pixels)),q); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6678 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6679 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6680 | if (magn_methx == 4) |
| 6681 | { |
| 6682 | /* Replicate nearest */ |
| 6683 | if (i <= ((m+1) << 1)) |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 6684 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6685 | SetPixelAlpha(image, |
| 6686 | GetPixelAlpha(image,pixels)+0,q); |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 6687 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6688 | else |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 6689 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6690 | SetPixelAlpha(image, |
| 6691 | GetPixelAlpha(image,n)+0,q); |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 6692 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6693 | } |
| 6694 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6695 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6696 | else /* if (magn_methx == 3 || magn_methx == 5) */ |
| 6697 | { |
| 6698 | /* Replicate nearest */ |
| 6699 | if (i <= ((m+1) << 1)) |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 6700 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6701 | SetPixelRed(image,GetPixelRed(image,pixels),q); |
| 6702 | SetPixelGreen(image,GetPixelGreen(image,pixels),q); |
| 6703 | SetPixelBlue(image,GetPixelBlue(image,pixels),q); |
| 6704 | SetPixelAlpha(image,GetPixelAlpha(image,pixels),q); |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 6705 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6706 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6707 | else |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 6708 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6709 | SetPixelRed(image,GetPixelRed(image,n),q); |
| 6710 | SetPixelGreen(image,GetPixelGreen(image,n),q); |
| 6711 | SetPixelBlue(image,GetPixelBlue(image,n),q); |
| 6712 | SetPixelAlpha(image,GetPixelAlpha(image,n),q); |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 6713 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6714 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6715 | if (magn_methx == 5) |
| 6716 | { |
| 6717 | /* Interpolate */ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6718 | SetPixelAlpha(image, |
| 6719 | (QM) ((2*i*( GetPixelAlpha(image,n) |
| 6720 | -GetPixelAlpha(image,pixels))+m)/ |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 6721 | ((ssize_t) (m*2)) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6722 | +GetPixelAlpha(image,pixels)),q); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6723 | } |
| 6724 | } |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6725 | q+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6726 | } |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6727 | n+=GetPixelChannels(image); |
| 6728 | p+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6729 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6730 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6731 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 6732 | break; |
| 6733 | } |
glennrp | 3faa9a3 | 2011-04-23 14:00:25 +0000 | [diff] [blame] | 6734 | #if (MAGICKCORE_QUANTUM_DEPTH > 16) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6735 | if (magn_methx != 1 || magn_methy != 1) |
| 6736 | { |
| 6737 | /* |
| 6738 | Rescale pixels to Quantum |
| 6739 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 6740 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6741 | { |
| 6742 | q=GetAuthenticPixels(image,0,y,image->columns,1,exception); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6743 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 6744 | for (x=(ssize_t) image->columns-1; x >= 0; x--) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6745 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6746 | SetPixelRed(image,ScaleShortToQuantum( |
| 6747 | GetPixelRed(image,q)),q); |
| 6748 | SetPixelGreen(image,ScaleShortToQuantum( |
| 6749 | GetPixelGreen(image,q)),q); |
| 6750 | SetPixelBlue(image,ScaleShortToQuantum( |
| 6751 | GetPixelBlue(image,q)),q); |
| 6752 | SetPixelAlpha(image,ScaleShortToQuantum( |
| 6753 | GetPixelAlpha(image,q)),q); |
| 6754 | q+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6755 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6756 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6757 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 6758 | break; |
| 6759 | } |
| 6760 | } |
| 6761 | #endif |
| 6762 | if (logging != MagickFalse) |
| 6763 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 6764 | " Finished MAGN processing"); |
| 6765 | } |
| 6766 | } |
| 6767 | |
| 6768 | /* |
| 6769 | Crop_box is with respect to the upper left corner of the MNG. |
| 6770 | */ |
| 6771 | crop_box.left=mng_info->image_box.left+mng_info->x_off[object_id]; |
| 6772 | crop_box.right=mng_info->image_box.right+mng_info->x_off[object_id]; |
| 6773 | crop_box.top=mng_info->image_box.top+mng_info->y_off[object_id]; |
| 6774 | crop_box.bottom=mng_info->image_box.bottom+mng_info->y_off[object_id]; |
| 6775 | crop_box=mng_minimum_box(crop_box,mng_info->clip); |
| 6776 | crop_box=mng_minimum_box(crop_box,mng_info->frame); |
| 6777 | crop_box=mng_minimum_box(crop_box,mng_info->object_clip[object_id]); |
| 6778 | if ((crop_box.left != (mng_info->image_box.left |
| 6779 | +mng_info->x_off[object_id])) || |
| 6780 | (crop_box.right != (mng_info->image_box.right |
| 6781 | +mng_info->x_off[object_id])) || |
| 6782 | (crop_box.top != (mng_info->image_box.top |
| 6783 | +mng_info->y_off[object_id])) || |
| 6784 | (crop_box.bottom != (mng_info->image_box.bottom |
| 6785 | +mng_info->y_off[object_id]))) |
| 6786 | { |
| 6787 | if (logging != MagickFalse) |
| 6788 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 6789 | " Crop the PNG image"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6790 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6791 | if ((crop_box.left < crop_box.right) && |
| 6792 | (crop_box.top < crop_box.bottom)) |
| 6793 | { |
| 6794 | Image |
| 6795 | *im; |
| 6796 | |
| 6797 | RectangleInfo |
| 6798 | crop_info; |
| 6799 | |
| 6800 | /* |
| 6801 | Crop_info is with respect to the upper left corner of |
| 6802 | the image. |
| 6803 | */ |
| 6804 | crop_info.x=(crop_box.left-mng_info->x_off[object_id]); |
| 6805 | crop_info.y=(crop_box.top-mng_info->y_off[object_id]); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 6806 | crop_info.width=(size_t) (crop_box.right-crop_box.left); |
| 6807 | crop_info.height=(size_t) (crop_box.bottom-crop_box.top); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6808 | image->page.width=image->columns; |
| 6809 | image->page.height=image->rows; |
| 6810 | image->page.x=0; |
| 6811 | image->page.y=0; |
| 6812 | im=CropImage(image,&crop_info,exception); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6813 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6814 | if (im != (Image *) NULL) |
| 6815 | { |
| 6816 | image->columns=im->columns; |
| 6817 | image->rows=im->rows; |
| 6818 | im=DestroyImage(im); |
| 6819 | image->page.width=image->columns; |
| 6820 | image->page.height=image->rows; |
| 6821 | image->page.x=crop_box.left; |
| 6822 | image->page.y=crop_box.top; |
| 6823 | } |
| 6824 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6825 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6826 | else |
| 6827 | { |
| 6828 | /* |
| 6829 | No pixels in crop area. The MNG spec still requires |
| 6830 | a layer, though, so make a single transparent pixel in |
| 6831 | the top left corner. |
| 6832 | */ |
| 6833 | image->columns=1; |
| 6834 | image->rows=1; |
| 6835 | image->colors=2; |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6836 | (void) SetImageBackgroundColor(image,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6837 | image->page.width=1; |
| 6838 | image->page.height=1; |
| 6839 | image->page.x=0; |
| 6840 | image->page.y=0; |
| 6841 | } |
| 6842 | } |
| 6843 | #ifndef PNG_READ_EMPTY_PLTE_SUPPORTED |
| 6844 | image=mng_info->image; |
| 6845 | #endif |
| 6846 | } |
| 6847 | |
glennrp | 2b013e4 | 2010-11-24 16:55:50 +0000 | [diff] [blame] | 6848 | #if (MAGICKCORE_QUANTUM_DEPTH > 16) |
| 6849 | /* PNG does not handle depths greater than 16 so reduce it even |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6850 | * if lossy. |
glennrp | 2b013e4 | 2010-11-24 16:55:50 +0000 | [diff] [blame] | 6851 | */ |
| 6852 | if (image->depth > 16) |
| 6853 | image->depth=16; |
| 6854 | #endif |
| 6855 | |
glennrp | 3faa9a3 | 2011-04-23 14:00:25 +0000 | [diff] [blame] | 6856 | #if (MAGICKCORE_QUANTUM_DEPTH > 8) |
glennrp | cc5d45b | 2012-01-06 04:06:10 +0000 | [diff] [blame] | 6857 | if (image->depth > 8) |
| 6858 | { |
| 6859 | /* To do: fill low byte properly */ |
| 6860 | image->depth=16; |
| 6861 | } |
| 6862 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6863 | if (LosslessReduceDepthOK(image,exception) != MagickFalse) |
glennrp | 8640fb5 | 2010-11-23 15:48:26 +0000 | [diff] [blame] | 6864 | image->depth = 8; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6865 | #endif |
glennrp | d6afd54 | 2010-11-19 01:53:05 +0000 | [diff] [blame] | 6866 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6867 | if (image_info->number_scenes != 0) |
| 6868 | { |
| 6869 | if (mng_info->scenes_found > |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 6870 | (ssize_t) (image_info->first_scene+image_info->number_scenes)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6871 | break; |
| 6872 | } |
glennrp | d6afd54 | 2010-11-19 01:53:05 +0000 | [diff] [blame] | 6873 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6874 | if (logging != MagickFalse) |
| 6875 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 6876 | " Finished reading image datastream."); |
glennrp | d6afd54 | 2010-11-19 01:53:05 +0000 | [diff] [blame] | 6877 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6878 | } while (LocaleCompare(image_info->magick,"MNG") == 0); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6879 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6880 | (void) CloseBlob(image); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6881 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6882 | if (logging != MagickFalse) |
| 6883 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 6884 | " Finished reading all image datastreams."); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6885 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6886 | #if defined(MNG_INSERT_LAYERS) |
| 6887 | if (insert_layers && !mng_info->image_found && (mng_info->mng_width) && |
| 6888 | (mng_info->mng_height)) |
| 6889 | { |
| 6890 | /* |
| 6891 | Insert a background layer if nothing else was found. |
| 6892 | */ |
| 6893 | if (logging != MagickFalse) |
| 6894 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 6895 | " No images found. Inserting a background layer."); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 6896 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6897 | if (GetAuthenticPixelQueue(image) != (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6898 | { |
| 6899 | /* |
| 6900 | Allocate next image structure. |
| 6901 | */ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6902 | AcquireNextImage(image_info,image,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6903 | if (GetNextImageInList(image) == (Image *) NULL) |
| 6904 | { |
| 6905 | image=DestroyImageList(image); |
| 6906 | MngInfoFreeStruct(mng_info,&have_mng_structure); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6907 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6908 | if (logging != MagickFalse) |
| 6909 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 6910 | " Allocation failed, returning NULL."); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6911 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6912 | return((Image *) NULL); |
| 6913 | } |
| 6914 | image=SyncNextImageInList(image); |
| 6915 | } |
| 6916 | image->columns=mng_info->mng_width; |
| 6917 | image->rows=mng_info->mng_height; |
| 6918 | image->page.width=mng_info->mng_width; |
| 6919 | image->page.height=mng_info->mng_height; |
| 6920 | image->page.x=0; |
| 6921 | image->page.y=0; |
| 6922 | image->background_color=mng_background_color; |
| 6923 | image->matte=MagickFalse; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 6924 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6925 | if (image_info->ping == MagickFalse) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6926 | (void) SetImageBackgroundColor(image,exception); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 6927 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6928 | mng_info->image_found++; |
| 6929 | } |
| 6930 | #endif |
| 6931 | image->iterations=mng_iterations; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 6932 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6933 | if (mng_iterations == 1) |
| 6934 | image->start_loop=MagickTrue; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 6935 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6936 | while (GetPreviousImageInList(image) != (Image *) NULL) |
| 6937 | { |
| 6938 | image_count++; |
| 6939 | if (image_count > 10*mng_info->image_found) |
| 6940 | { |
| 6941 | if (logging != MagickFalse) |
| 6942 | (void) LogMagickEvent(CoderEvent,GetMagickModule()," No beginning"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6943 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6944 | (void) ThrowMagickException(exception,GetMagickModule(), |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6945 | CoderError,"Linked list is corrupted, beginning of list not found", |
| 6946 | "`%s'",image_info->filename); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6947 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6948 | return((Image *) NULL); |
| 6949 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 6950 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6951 | image=GetPreviousImageInList(image); |
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 (GetNextImageInList(image) == (Image *) NULL) |
| 6954 | { |
| 6955 | if (logging != MagickFalse) |
| 6956 | (void) LogMagickEvent(CoderEvent,GetMagickModule()," Corrupt list"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6957 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6958 | (void) ThrowMagickException(exception,GetMagickModule(), |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6959 | CoderError,"Linked list is corrupted; next_image is NULL","`%s'", |
| 6960 | image_info->filename); |
| 6961 | } |
| 6962 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6963 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6964 | if (mng_info->ticks_per_second && mng_info->image_found > 1 && |
| 6965 | GetNextImageInList(image) == |
| 6966 | (Image *) NULL) |
| 6967 | { |
| 6968 | if (logging != MagickFalse) |
| 6969 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 6970 | " First image null"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6971 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6972 | (void) ThrowMagickException(exception,GetMagickModule(), |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6973 | CoderError,"image->next for first image is NULL but shouldn't be.", |
| 6974 | "`%s'",image_info->filename); |
| 6975 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6976 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6977 | if (mng_info->image_found == 0) |
| 6978 | { |
| 6979 | if (logging != MagickFalse) |
| 6980 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 6981 | " No visible images found."); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6982 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 6983 | (void) ThrowMagickException(exception,GetMagickModule(), |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6984 | CoderError,"No visible images in file","`%s'",image_info->filename); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6985 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6986 | if (image != (Image *) NULL) |
| 6987 | image=DestroyImageList(image); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 6988 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6989 | MngInfoFreeStruct(mng_info,&have_mng_structure); |
| 6990 | return((Image *) NULL); |
| 6991 | } |
| 6992 | |
| 6993 | if (mng_info->ticks_per_second) |
| 6994 | final_delay=1UL*MagickMax(image->ticks_per_second,1L)* |
| 6995 | final_delay/mng_info->ticks_per_second; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 6996 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 6997 | else |
| 6998 | image->start_loop=MagickTrue; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 6999 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7000 | /* Find final nonzero image delay */ |
| 7001 | final_image_delay=0; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 7002 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7003 | while (GetNextImageInList(image) != (Image *) NULL) |
| 7004 | { |
| 7005 | if (image->delay) |
| 7006 | final_image_delay=image->delay; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7007 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7008 | image=GetNextImageInList(image); |
| 7009 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 7010 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7011 | if (final_delay < final_image_delay) |
| 7012 | final_delay=final_image_delay; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 7013 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7014 | image->delay=final_delay; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 7015 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7016 | if (logging != MagickFalse) |
| 7017 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 7018 | " image->delay=%.20g, final_delay=%.20g",(double) image->delay, |
| 7019 | (double) final_delay); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 7020 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7021 | if (logging != MagickFalse) |
| 7022 | { |
| 7023 | int |
| 7024 | scene; |
| 7025 | |
| 7026 | scene=0; |
| 7027 | image=GetFirstImageInList(image); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7028 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7029 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 7030 | " Before coalesce:"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7031 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7032 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 7033 | " scene 0 delay=%.20g",(double) image->delay); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7034 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7035 | while (GetNextImageInList(image) != (Image *) NULL) |
| 7036 | { |
| 7037 | image=GetNextImageInList(image); |
| 7038 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 7039 | " scene %.20g delay=%.20g",(double) scene++,(double) image->delay); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7040 | } |
| 7041 | } |
| 7042 | |
| 7043 | image=GetFirstImageInList(image); |
| 7044 | #ifdef MNG_COALESCE_LAYERS |
| 7045 | if (insert_layers) |
| 7046 | { |
| 7047 | Image |
| 7048 | *next_image, |
| 7049 | *next; |
| 7050 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 7051 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7052 | scene; |
| 7053 | |
| 7054 | if (logging != MagickFalse) |
| 7055 | (void) LogMagickEvent(CoderEvent,GetMagickModule()," Coalesce Images"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7056 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7057 | scene=image->scene; |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 7058 | next_image=CoalesceImages(image,exception); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7059 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7060 | if (next_image == (Image *) NULL) |
| 7061 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7062 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7063 | image=DestroyImageList(image); |
| 7064 | image=next_image; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7065 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7066 | for (next=image; next != (Image *) NULL; next=next_image) |
| 7067 | { |
| 7068 | next->page.width=mng_info->mng_width; |
| 7069 | next->page.height=mng_info->mng_height; |
| 7070 | next->page.x=0; |
| 7071 | next->page.y=0; |
| 7072 | next->scene=scene++; |
| 7073 | next_image=GetNextImageInList(next); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7074 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7075 | if (next_image == (Image *) NULL) |
| 7076 | break; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7077 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7078 | if (next->delay == 0) |
| 7079 | { |
| 7080 | scene--; |
| 7081 | next_image->previous=GetPreviousImageInList(next); |
| 7082 | if (GetPreviousImageInList(next) == (Image *) NULL) |
| 7083 | image=next_image; |
| 7084 | else |
| 7085 | next->previous->next=next_image; |
| 7086 | next=DestroyImage(next); |
| 7087 | } |
| 7088 | } |
| 7089 | } |
| 7090 | #endif |
| 7091 | |
| 7092 | while (GetNextImageInList(image) != (Image *) NULL) |
| 7093 | image=GetNextImageInList(image); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7094 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7095 | image->dispose=BackgroundDispose; |
| 7096 | |
| 7097 | if (logging != MagickFalse) |
| 7098 | { |
| 7099 | int |
| 7100 | scene; |
| 7101 | |
| 7102 | scene=0; |
| 7103 | image=GetFirstImageInList(image); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7104 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7105 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 7106 | " After coalesce:"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7107 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7108 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 7109 | " scene 0 delay=%.20g dispose=%.20g",(double) image->delay, |
| 7110 | (double) image->dispose); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7111 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7112 | while (GetNextImageInList(image) != (Image *) NULL) |
cristy | f2faecf | 2010-05-28 19:19:36 +0000 | [diff] [blame] | 7113 | { |
| 7114 | image=GetNextImageInList(image); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7115 | |
cristy | f2faecf | 2010-05-28 19:19:36 +0000 | [diff] [blame] | 7116 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 7117 | " scene %.20g delay=%.20g dispose=%.20g",(double) scene++, |
| 7118 | (double) image->delay,(double) image->dispose); |
cristy | f2faecf | 2010-05-28 19:19:36 +0000 | [diff] [blame] | 7119 | } |
| 7120 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7121 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7122 | image=GetFirstImageInList(image); |
| 7123 | MngInfoFreeStruct(mng_info,&have_mng_structure); |
| 7124 | have_mng_structure=MagickFalse; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7125 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7126 | if (logging != MagickFalse) |
| 7127 | (void) LogMagickEvent(CoderEvent,GetMagickModule(),"exit ReadMNGImage()"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7128 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7129 | return(GetFirstImageInList(image)); |
| 7130 | } |
glennrp | 25c1e2b | 2010-03-25 01:39:56 +0000 | [diff] [blame] | 7131 | #else /* PNG_LIBPNG_VER > 10011 */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7132 | static Image *ReadPNGImage(const ImageInfo *image_info,ExceptionInfo *exception) |
| 7133 | { |
| 7134 | printf("Your PNG library is too old: You have libpng-%s\n", |
| 7135 | PNG_LIBPNG_VER_STRING); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7136 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7137 | (void) ThrowMagickException(exception,GetMagickModule(),CoderError, |
| 7138 | "PNG library is too old","`%s'",image_info->filename); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7139 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7140 | return(Image *) NULL; |
| 7141 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7142 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7143 | static Image *ReadMNGImage(const ImageInfo *image_info,ExceptionInfo *exception) |
| 7144 | { |
| 7145 | return(ReadPNGImage(image_info,exception)); |
| 7146 | } |
glennrp | 25c1e2b | 2010-03-25 01:39:56 +0000 | [diff] [blame] | 7147 | #endif /* PNG_LIBPNG_VER > 10011 */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7148 | #endif |
| 7149 | |
| 7150 | /* |
| 7151 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 7152 | % % |
| 7153 | % % |
| 7154 | % % |
| 7155 | % R e g i s t e r P N G I m a g e % |
| 7156 | % % |
| 7157 | % % |
| 7158 | % % |
| 7159 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 7160 | % |
| 7161 | % RegisterPNGImage() adds properties for the PNG image format to |
| 7162 | % the list of supported formats. The properties include the image format |
| 7163 | % tag, a method to read and/or write the format, whether the format |
| 7164 | % supports the saving of more than one frame to the same file or blob, |
| 7165 | % whether the format supports native in-memory I/O, and a brief |
| 7166 | % description of the format. |
| 7167 | % |
| 7168 | % The format of the RegisterPNGImage method is: |
| 7169 | % |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 7170 | % size_t RegisterPNGImage(void) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7171 | % |
| 7172 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 7173 | ModuleExport size_t RegisterPNGImage(void) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7174 | { |
| 7175 | char |
| 7176 | version[MaxTextExtent]; |
| 7177 | |
| 7178 | MagickInfo |
| 7179 | *entry; |
| 7180 | |
| 7181 | static const char |
| 7182 | *PNGNote= |
| 7183 | { |
| 7184 | "See http://www.libpng.org/ for details about the PNG format." |
| 7185 | }, |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7186 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7187 | *JNGNote= |
| 7188 | { |
| 7189 | "See http://www.libpng.org/pub/mng/ for details about the JNG\n" |
| 7190 | "format." |
| 7191 | }, |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7192 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7193 | *MNGNote= |
| 7194 | { |
| 7195 | "See http://www.libpng.org/pub/mng/ for details about the MNG\n" |
| 7196 | "format." |
| 7197 | }; |
| 7198 | |
| 7199 | *version='\0'; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7200 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7201 | #if defined(PNG_LIBPNG_VER_STRING) |
| 7202 | (void) ConcatenateMagickString(version,"libpng ",MaxTextExtent); |
| 7203 | (void) ConcatenateMagickString(version,PNG_LIBPNG_VER_STRING,MaxTextExtent); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7204 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7205 | if (LocaleCompare(PNG_LIBPNG_VER_STRING,png_get_header_ver(NULL)) != 0) |
| 7206 | { |
| 7207 | (void) ConcatenateMagickString(version,",",MaxTextExtent); |
| 7208 | (void) ConcatenateMagickString(version,png_get_libpng_ver(NULL), |
| 7209 | MaxTextExtent); |
| 7210 | } |
| 7211 | #endif |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7212 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7213 | entry=SetMagickInfo("MNG"); |
| 7214 | entry->seekable_stream=MagickTrue; /* To do: eliminate this. */ |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7215 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7216 | #if defined(MAGICKCORE_PNG_DELEGATE) |
| 7217 | entry->decoder=(DecodeImageHandler *) ReadMNGImage; |
| 7218 | entry->encoder=(EncodeImageHandler *) WriteMNGImage; |
| 7219 | #endif |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7220 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7221 | entry->magick=(IsImageFormatHandler *) IsMNG; |
| 7222 | entry->description=ConstantString("Multiple-image Network Graphics"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7223 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7224 | if (*version != '\0') |
| 7225 | entry->version=ConstantString(version); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7226 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7227 | entry->module=ConstantString("PNG"); |
| 7228 | entry->note=ConstantString(MNGNote); |
| 7229 | (void) RegisterMagickInfo(entry); |
| 7230 | |
| 7231 | entry=SetMagickInfo("PNG"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7232 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7233 | #if defined(MAGICKCORE_PNG_DELEGATE) |
| 7234 | entry->decoder=(DecodeImageHandler *) ReadPNGImage; |
| 7235 | entry->encoder=(EncodeImageHandler *) WritePNGImage; |
| 7236 | #endif |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7237 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7238 | entry->magick=(IsImageFormatHandler *) IsPNG; |
| 7239 | entry->adjoin=MagickFalse; |
| 7240 | entry->description=ConstantString("Portable Network Graphics"); |
| 7241 | entry->module=ConstantString("PNG"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7242 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7243 | if (*version != '\0') |
| 7244 | entry->version=ConstantString(version); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7245 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7246 | entry->note=ConstantString(PNGNote); |
| 7247 | (void) RegisterMagickInfo(entry); |
| 7248 | |
| 7249 | entry=SetMagickInfo("PNG8"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7250 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7251 | #if defined(MAGICKCORE_PNG_DELEGATE) |
| 7252 | entry->decoder=(DecodeImageHandler *) ReadPNGImage; |
| 7253 | entry->encoder=(EncodeImageHandler *) WritePNGImage; |
| 7254 | #endif |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7255 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7256 | entry->magick=(IsImageFormatHandler *) IsPNG; |
| 7257 | entry->adjoin=MagickFalse; |
| 7258 | entry->description=ConstantString( |
| 7259 | "8-bit indexed with optional binary transparency"); |
| 7260 | entry->module=ConstantString("PNG"); |
| 7261 | (void) RegisterMagickInfo(entry); |
| 7262 | |
| 7263 | entry=SetMagickInfo("PNG24"); |
| 7264 | *version='\0'; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7265 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7266 | #if defined(ZLIB_VERSION) |
| 7267 | (void) ConcatenateMagickString(version,"zlib ",MaxTextExtent); |
| 7268 | (void) ConcatenateMagickString(version,ZLIB_VERSION,MaxTextExtent); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7269 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7270 | if (LocaleCompare(ZLIB_VERSION,zlib_version) != 0) |
| 7271 | { |
| 7272 | (void) ConcatenateMagickString(version,",",MaxTextExtent); |
| 7273 | (void) ConcatenateMagickString(version,zlib_version,MaxTextExtent); |
| 7274 | } |
| 7275 | #endif |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7276 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7277 | if (*version != '\0') |
| 7278 | entry->version=ConstantString(version); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7279 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7280 | #if defined(MAGICKCORE_PNG_DELEGATE) |
| 7281 | entry->decoder=(DecodeImageHandler *) ReadPNGImage; |
| 7282 | entry->encoder=(EncodeImageHandler *) WritePNGImage; |
| 7283 | #endif |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7284 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7285 | entry->magick=(IsImageFormatHandler *) IsPNG; |
| 7286 | entry->adjoin=MagickFalse; |
| 7287 | entry->description=ConstantString("opaque 24-bit RGB"); |
| 7288 | entry->module=ConstantString("PNG"); |
| 7289 | (void) RegisterMagickInfo(entry); |
| 7290 | |
| 7291 | entry=SetMagickInfo("PNG32"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7292 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7293 | #if defined(MAGICKCORE_PNG_DELEGATE) |
| 7294 | entry->decoder=(DecodeImageHandler *) ReadPNGImage; |
| 7295 | entry->encoder=(EncodeImageHandler *) WritePNGImage; |
| 7296 | #endif |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7297 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7298 | entry->magick=(IsImageFormatHandler *) IsPNG; |
| 7299 | entry->adjoin=MagickFalse; |
| 7300 | entry->description=ConstantString("opaque or transparent 32-bit RGBA"); |
| 7301 | entry->module=ConstantString("PNG"); |
| 7302 | (void) RegisterMagickInfo(entry); |
| 7303 | |
| 7304 | entry=SetMagickInfo("JNG"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7305 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7306 | #if defined(JNG_SUPPORTED) |
| 7307 | #if defined(MAGICKCORE_PNG_DELEGATE) |
| 7308 | entry->decoder=(DecodeImageHandler *) ReadJNGImage; |
| 7309 | entry->encoder=(EncodeImageHandler *) WriteJNGImage; |
| 7310 | #endif |
| 7311 | #endif |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7312 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7313 | entry->magick=(IsImageFormatHandler *) IsJNG; |
| 7314 | entry->adjoin=MagickFalse; |
| 7315 | entry->description=ConstantString("JPEG Network Graphics"); |
| 7316 | entry->module=ConstantString("PNG"); |
| 7317 | entry->note=ConstantString(JNGNote); |
| 7318 | (void) RegisterMagickInfo(entry); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7319 | |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 7320 | #ifdef PNG_SETJMP_NOT_THREAD_SAFE |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 7321 | ping_semaphore=AllocateSemaphoreInfo(); |
cristy | 18b1744 | 2009-10-25 18:36:48 +0000 | [diff] [blame] | 7322 | #endif |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7323 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7324 | return(MagickImageCoderSignature); |
| 7325 | } |
| 7326 | |
| 7327 | /* |
| 7328 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 7329 | % % |
| 7330 | % % |
| 7331 | % % |
| 7332 | % U n r e g i s t e r P N G I m a g e % |
| 7333 | % % |
| 7334 | % % |
| 7335 | % % |
| 7336 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 7337 | % |
| 7338 | % UnregisterPNGImage() removes format registrations made by the |
| 7339 | % PNG module from the list of supported formats. |
| 7340 | % |
| 7341 | % The format of the UnregisterPNGImage method is: |
| 7342 | % |
| 7343 | % UnregisterPNGImage(void) |
| 7344 | % |
| 7345 | */ |
| 7346 | ModuleExport void UnregisterPNGImage(void) |
| 7347 | { |
| 7348 | (void) UnregisterMagickInfo("MNG"); |
| 7349 | (void) UnregisterMagickInfo("PNG"); |
| 7350 | (void) UnregisterMagickInfo("PNG8"); |
| 7351 | (void) UnregisterMagickInfo("PNG24"); |
| 7352 | (void) UnregisterMagickInfo("PNG32"); |
| 7353 | (void) UnregisterMagickInfo("JNG"); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7354 | |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 7355 | #ifdef PNG_SETJMP_NOT_THREAD_SAFE |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 7356 | if (ping_semaphore != (SemaphoreInfo *) NULL) |
| 7357 | DestroySemaphoreInfo(&ping_semaphore); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7358 | #endif |
| 7359 | } |
| 7360 | |
| 7361 | #if defined(MAGICKCORE_PNG_DELEGATE) |
glennrp | 25c1e2b | 2010-03-25 01:39:56 +0000 | [diff] [blame] | 7362 | #if PNG_LIBPNG_VER > 10011 |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7363 | /* |
| 7364 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 7365 | % % |
| 7366 | % % |
| 7367 | % % |
| 7368 | % W r i t e M N G I m a g e % |
| 7369 | % % |
| 7370 | % % |
| 7371 | % % |
| 7372 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 7373 | % |
| 7374 | % WriteMNGImage() writes an image in the Portable Network Graphics |
| 7375 | % Group's "Multiple-image Network Graphics" encoded image format. |
| 7376 | % |
| 7377 | % MNG support written by Glenn Randers-Pehrson, glennrp@image... |
| 7378 | % |
| 7379 | % The format of the WriteMNGImage method is: |
| 7380 | % |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 7381 | % MagickBooleanType WriteMNGImage(const ImageInfo *image_info, |
| 7382 | % Image *image,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7383 | % |
| 7384 | % A description of each parameter follows. |
| 7385 | % |
| 7386 | % o image_info: the image info. |
| 7387 | % |
| 7388 | % o image: The image. |
| 7389 | % |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 7390 | % o exception: return any errors or warnings in this structure. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7391 | % |
| 7392 | % To do (as of version 5.5.2, November 26, 2002 -- glennrp -- see also |
| 7393 | % "To do" under ReadPNGImage): |
| 7394 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7395 | % Preserve all unknown and not-yet-handled known chunks found in input |
| 7396 | % PNG file and copy them into output PNG files according to the PNG |
| 7397 | % copying rules. |
| 7398 | % |
| 7399 | % Write the iCCP chunk at MNG level when (icc profile length > 0) |
| 7400 | % |
| 7401 | % Improve selection of color type (use indexed-colour or indexed-colour |
| 7402 | % with tRNS when 256 or fewer unique RGBA values are present). |
| 7403 | % |
| 7404 | % Figure out what to do with "dispose=<restore-to-previous>" (dispose == 3) |
| 7405 | % This will be complicated if we limit ourselves to generating MNG-LC |
| 7406 | % files. For now we ignore disposal method 3 and simply overlay the next |
| 7407 | % image on it. |
| 7408 | % |
| 7409 | % Check for identical PLTE's or PLTE/tRNS combinations and use a |
| 7410 | % global MNG PLTE or PLTE/tRNS combination when appropriate. |
| 7411 | % [mostly done 15 June 1999 but still need to take care of tRNS] |
| 7412 | % |
| 7413 | % Check for identical sRGB and replace with a global sRGB (and remove |
| 7414 | % gAMA/cHRM if sRGB is found; check for identical gAMA/cHRM and |
| 7415 | % replace with global gAMA/cHRM (or with sRGB if appropriate; replace |
| 7416 | % local gAMA/cHRM with local sRGB if appropriate). |
| 7417 | % |
| 7418 | % Check for identical sBIT chunks and write global ones. |
| 7419 | % |
| 7420 | % Provide option to skip writing the signature tEXt chunks. |
| 7421 | % |
| 7422 | % Use signatures to detect identical objects and reuse the first |
| 7423 | % instance of such objects instead of writing duplicate objects. |
| 7424 | % |
| 7425 | % Use a smaller-than-32k value of compression window size when |
| 7426 | % appropriate. |
| 7427 | % |
| 7428 | % Encode JNG datastreams. Mostly done as of 5.5.2; need to write |
| 7429 | % ancillary text chunks and save profiles. |
| 7430 | % |
| 7431 | % Provide an option to force LC files (to ensure exact framing rate) |
| 7432 | % instead of VLC. |
| 7433 | % |
| 7434 | % Provide an option to force VLC files instead of LC, even when offsets |
| 7435 | % are present. This will involve expanding the embedded images with a |
| 7436 | % transparent region at the top and/or left. |
| 7437 | */ |
| 7438 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7439 | static void |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 7440 | Magick_png_write_raw_profile(const ImageInfo *image_info,png_struct *ping, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7441 | png_info *ping_info, unsigned char *profile_type, unsigned char |
| 7442 | *profile_description, unsigned char *profile_data, png_uint_32 length) |
| 7443 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7444 | png_textp |
| 7445 | text; |
| 7446 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 7447 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7448 | i; |
| 7449 | |
| 7450 | unsigned char |
| 7451 | *sp; |
| 7452 | |
| 7453 | png_charp |
| 7454 | dp; |
| 7455 | |
| 7456 | png_uint_32 |
| 7457 | allocated_length, |
| 7458 | description_length; |
| 7459 | |
| 7460 | unsigned char |
| 7461 | 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] | 7462 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7463 | if (LocaleNCompare((char *) profile_type+1, "ng-chunk-",9) == 0) |
| 7464 | return; |
| 7465 | |
| 7466 | if (image_info->verbose) |
| 7467 | { |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 7468 | (void) printf("writing raw profile: type=%s, length=%.20g\n", |
| 7469 | (char *) profile_type, (double) length); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7470 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 7471 | |
cristy | a865ccd | 2012-07-28 00:33:10 +0000 | [diff] [blame] | 7472 | #if PNG_LIBPNG_VER >= 14000 |
| 7473 | text=(png_textp) png_malloc(ping,(png_alloc_size_t) sizeof(png_text)); |
| 7474 | #else |
| 7475 | text=(png_textp) png_malloc(ping,(png_size_t) sizeof(png_text)); |
| 7476 | #endif |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7477 | description_length=(png_uint_32) strlen((const char *) profile_description); |
| 7478 | allocated_length=(png_uint_32) (length*2 + (length >> 5) + 20 |
| 7479 | + description_length); |
cristy | a865ccd | 2012-07-28 00:33:10 +0000 | [diff] [blame] | 7480 | #if PNG_LIBPNG_VER >= 14000 |
| 7481 | text[0].text=(png_charp) png_malloc(ping, |
| 7482 | (png_alloc_size_t) allocated_length); |
| 7483 | text[0].key=(png_charp) png_malloc(ping, (png_alloc_size_t) 80); |
| 7484 | #else |
| 7485 | text[0].text=(png_charp) png_malloc(ping, (png_size_t) allocated_length); |
| 7486 | text[0].key=(png_charp) png_malloc(ping, (png_size_t) 80); |
| 7487 | #endif |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7488 | text[0].key[0]='\0'; |
| 7489 | (void) ConcatenateMagickString(text[0].key, |
| 7490 | "Raw profile type ",MaxTextExtent); |
| 7491 | (void) ConcatenateMagickString(text[0].key,(const char *) profile_type,62); |
| 7492 | sp=profile_data; |
| 7493 | dp=text[0].text; |
| 7494 | *dp++='\n'; |
| 7495 | (void) CopyMagickString(dp,(const char *) profile_description, |
| 7496 | allocated_length); |
| 7497 | dp+=description_length; |
| 7498 | *dp++='\n'; |
cristy | 3b6fd2e | 2011-05-20 12:53:50 +0000 | [diff] [blame] | 7499 | (void) FormatLocaleString(dp,allocated_length- |
cristy | f2faecf | 2010-05-28 19:19:36 +0000 | [diff] [blame] | 7500 | (png_size_t) (dp-text[0].text),"%8lu ",(unsigned long) length); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7501 | dp+=8; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7502 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 7503 | for (i=0; i < (ssize_t) length; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7504 | { |
| 7505 | if (i%36 == 0) |
| 7506 | *dp++='\n'; |
| 7507 | *(dp++)=(char) hex[((*sp >> 4) & 0x0f)]; |
| 7508 | *(dp++)=(char) hex[((*sp++ ) & 0x0f)]; |
| 7509 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7510 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7511 | *dp++='\n'; |
| 7512 | *dp='\0'; |
| 7513 | text[0].text_length=(png_size_t) (dp-text[0].text); |
| 7514 | text[0].compression=image_info->compression == NoCompression || |
| 7515 | (image_info->compression == UndefinedCompression && |
| 7516 | text[0].text_length < 128) ? -1 : 0; |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7517 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7518 | if (text[0].text_length <= allocated_length) |
| 7519 | png_set_text(ping,ping_info,text,1); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7520 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7521 | png_free(ping,text[0].text); |
| 7522 | png_free(ping,text[0].key); |
| 7523 | png_free(ping,text); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7524 | } |
| 7525 | |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 7526 | static MagickBooleanType Magick_png_write_chunk_from_profile(Image *image, |
cristy | 4383ec8 | 2011-01-05 15:42:32 +0000 | [diff] [blame] | 7527 | const char *string, MagickBooleanType logging) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7528 | { |
| 7529 | char |
| 7530 | *name; |
| 7531 | |
| 7532 | const StringInfo |
| 7533 | *profile; |
| 7534 | |
| 7535 | unsigned char |
| 7536 | *data; |
| 7537 | |
| 7538 | png_uint_32 length; |
| 7539 | |
| 7540 | ResetImageProfileIterator(image); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7541 | |
| 7542 | for (name=GetNextImageProfile(image); name != (const char *) NULL; ) |
| 7543 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7544 | profile=GetImageProfile(image,name); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7545 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7546 | if (profile != (const StringInfo *) NULL) |
| 7547 | { |
| 7548 | StringInfo |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 7549 | *ping_profile; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7550 | |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7551 | if (LocaleNCompare(name,string,11) == 0) |
| 7552 | { |
| 7553 | if (logging != MagickFalse) |
| 7554 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 7555 | " Found %s profile",name); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7556 | |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 7557 | ping_profile=CloneStringInfo(profile); |
| 7558 | data=GetStringInfoDatum(ping_profile), |
| 7559 | length=(png_uint_32) GetStringInfoLength(ping_profile); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7560 | data[4]=data[3]; |
| 7561 | data[3]=data[2]; |
| 7562 | data[2]=data[1]; |
| 7563 | data[1]=data[0]; |
| 7564 | (void) WriteBlobMSBULong(image,length-5); /* data length */ |
| 7565 | (void) WriteBlob(image,length-1,data+1); |
| 7566 | (void) WriteBlobMSBULong(image,crc32(0,data+1,(uInt) length-1)); |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 7567 | ping_profile=DestroyStringInfo(ping_profile); |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7568 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7569 | } |
glennrp | 47b9dd5 | 2010-11-24 18:12:06 +0000 | [diff] [blame] | 7570 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7571 | name=GetNextImageProfile(image); |
| 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 | return(MagickTrue); |
| 7575 | } |
| 7576 | |
glennrp | b9cfe27 | 2010-12-21 15:08:06 +0000 | [diff] [blame] | 7577 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7578 | /* Write one PNG image */ |
glennrp | b9cfe27 | 2010-12-21 15:08:06 +0000 | [diff] [blame] | 7579 | static MagickBooleanType WriteOnePNGImage(MngInfo *mng_info, |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 7580 | const ImageInfo *IMimage_info,Image *IMimage,ExceptionInfo *exception) |
glennrp | b9cfe27 | 2010-12-21 15:08:06 +0000 | [diff] [blame] | 7581 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 7582 | Image |
| 7583 | *image; |
| 7584 | |
| 7585 | ImageInfo |
| 7586 | *image_info; |
| 7587 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7588 | char |
| 7589 | s[2]; |
| 7590 | |
| 7591 | const char |
| 7592 | *name, |
| 7593 | *property, |
| 7594 | *value; |
| 7595 | |
| 7596 | const StringInfo |
| 7597 | *profile; |
| 7598 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7599 | int |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7600 | num_passes, |
glennrp | cecd576 | 2010-03-23 12:07:49 +0000 | [diff] [blame] | 7601 | pass; |
| 7602 | |
glennrp | e9c26dc | 2010-05-30 01:56:35 +0000 | [diff] [blame] | 7603 | png_byte |
| 7604 | ping_trans_alpha[256]; |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 7605 | |
glennrp | 39992b4 | 2010-11-14 00:03:43 +0000 | [diff] [blame] | 7606 | png_color |
| 7607 | palette[257]; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7608 | |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 7609 | png_color_16 |
| 7610 | ping_background, |
| 7611 | ping_trans_color; |
| 7612 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7613 | png_info |
| 7614 | *ping_info; |
| 7615 | |
| 7616 | png_struct |
| 7617 | *ping; |
| 7618 | |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 7619 | png_uint_32 |
| 7620 | ping_height, |
| 7621 | ping_width; |
| 7622 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 7623 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7624 | y; |
| 7625 | |
| 7626 | MagickBooleanType |
glennrp | 58e0176 | 2011-01-07 15:28:54 +0000 | [diff] [blame] | 7627 | image_matte, |
glennrp | 21f0e62 | 2011-01-07 16:20:57 +0000 | [diff] [blame] | 7628 | logging, |
glennrp | 58e0176 | 2011-01-07 15:28:54 +0000 | [diff] [blame] | 7629 | matte, |
| 7630 | |
glennrp | da8f3a7 | 2011-02-27 23:54:12 +0000 | [diff] [blame] | 7631 | ping_have_blob, |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 7632 | ping_have_cheap_transparency, |
glennrp | d6bf161 | 2010-12-17 17:28:54 +0000 | [diff] [blame] | 7633 | ping_have_color, |
glennrp | 8d57966 | 2011-02-23 02:05:02 +0000 | [diff] [blame] | 7634 | ping_have_non_bw, |
glennrp | 39992b4 | 2010-11-14 00:03:43 +0000 | [diff] [blame] | 7635 | ping_have_PLTE, |
glennrp | 991d11d | 2010-11-12 21:55:28 +0000 | [diff] [blame] | 7636 | ping_have_bKGD, |
| 7637 | ping_have_pHYs, |
| 7638 | ping_have_tRNS, |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 7639 | |
| 7640 | ping_exclude_bKGD, |
| 7641 | ping_exclude_cHRM, |
glennrp | a0ed009 | 2011-04-18 16:36:29 +0000 | [diff] [blame] | 7642 | ping_exclude_date, |
glennrp | e4e2d79 | 2011-02-21 12:11:27 +0000 | [diff] [blame] | 7643 | /* ping_exclude_EXIF, */ |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 7644 | ping_exclude_gAMA, |
| 7645 | ping_exclude_iCCP, |
| 7646 | /* ping_exclude_iTXt, */ |
| 7647 | ping_exclude_oFFs, |
| 7648 | ping_exclude_pHYs, |
| 7649 | ping_exclude_sRGB, |
| 7650 | ping_exclude_tEXt, |
glennrp | e4e2d79 | 2011-02-21 12:11:27 +0000 | [diff] [blame] | 7651 | /* ping_exclude_tRNS, */ |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 7652 | ping_exclude_vpAg, |
| 7653 | ping_exclude_zCCP, /* hex-encoded iCCP */ |
| 7654 | ping_exclude_zTXt, |
| 7655 | |
glennrp | 8d3d6e5 | 2011-04-19 04:39:51 +0000 | [diff] [blame] | 7656 | ping_preserve_colormap, |
glennrp | 0e8ea19 | 2010-12-24 18:00:33 +0000 | [diff] [blame] | 7657 | ping_need_colortype_warning, |
| 7658 | |
glennrp | 82b3c53 | 2011-03-22 19:20:54 +0000 | [diff] [blame] | 7659 | status, |
glennrp | 8ca51ad | 2011-05-12 21:22:32 +0000 | [diff] [blame] | 7660 | tried_332, |
glennrp | d337164 | 2011-03-22 19:42:23 +0000 | [diff] [blame] | 7661 | tried_333, |
| 7662 | tried_444; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7663 | |
| 7664 | QuantumInfo |
| 7665 | *quantum_info; |
| 7666 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 7667 | PNGErrorInfo |
| 7668 | error_info; |
| 7669 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 7670 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7671 | i, |
| 7672 | x; |
| 7673 | |
| 7674 | unsigned char |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 7675 | *ping_pixels; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7676 | |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 7677 | volatile int |
glennrp | f09bded | 2011-01-08 01:15:59 +0000 | [diff] [blame] | 7678 | image_colors, |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 7679 | ping_bit_depth, |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 7680 | ping_color_type, |
| 7681 | ping_interlace_method, |
| 7682 | ping_compression_method, |
| 7683 | ping_filter_method, |
| 7684 | ping_num_trans; |
| 7685 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 7686 | volatile size_t |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 7687 | image_depth, |
| 7688 | old_bit_depth; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7689 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 7690 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7691 | quality, |
| 7692 | rowbytes, |
| 7693 | save_image_depth; |
| 7694 | |
glennrp | dfd7080 | 2010-11-14 01:23:35 +0000 | [diff] [blame] | 7695 | int |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 7696 | j, |
glennrp | f09bded | 2011-01-08 01:15:59 +0000 | [diff] [blame] | 7697 | number_colors, |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 7698 | number_opaque, |
| 7699 | number_semitransparent, |
| 7700 | number_transparent, |
glennrp | dfd7080 | 2010-11-14 01:23:35 +0000 | [diff] [blame] | 7701 | ping_pHYs_unit_type; |
| 7702 | |
| 7703 | png_uint_32 |
| 7704 | ping_pHYs_x_resolution, |
| 7705 | ping_pHYs_y_resolution; |
| 7706 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7707 | logging=LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 7708 | " Enter WriteOnePNGImage()"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 7709 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 7710 | image = CloneImage(IMimage,0,0,MagickFalse,exception); |
| 7711 | image_info=(ImageInfo *) CloneImageInfo(IMimage_info); |
| 7712 | if (image_info == (ImageInfo *) NULL) |
| 7713 | ThrowWriterException(ResourceLimitError, "MemoryAllocationFailed"); |
glennrp | b9cfe27 | 2010-12-21 15:08:06 +0000 | [diff] [blame] | 7714 | |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 7715 | /* Initialize some stuff */ |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 7716 | ping_bit_depth=0, |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 7717 | ping_color_type=0, |
| 7718 | ping_interlace_method=0, |
| 7719 | ping_compression_method=0, |
| 7720 | ping_filter_method=0, |
| 7721 | ping_num_trans = 0; |
| 7722 | |
| 7723 | ping_background.red = 0; |
| 7724 | ping_background.green = 0; |
| 7725 | ping_background.blue = 0; |
| 7726 | ping_background.gray = 0; |
| 7727 | ping_background.index = 0; |
| 7728 | |
| 7729 | ping_trans_color.red=0; |
| 7730 | ping_trans_color.green=0; |
| 7731 | ping_trans_color.blue=0; |
| 7732 | ping_trans_color.gray=0; |
| 7733 | |
glennrp | dfd7080 | 2010-11-14 01:23:35 +0000 | [diff] [blame] | 7734 | ping_pHYs_unit_type = 0; |
| 7735 | ping_pHYs_x_resolution = 0; |
| 7736 | ping_pHYs_y_resolution = 0; |
| 7737 | |
glennrp | da8f3a7 | 2011-02-27 23:54:12 +0000 | [diff] [blame] | 7738 | ping_have_blob=MagickFalse; |
glennrp | d6bf161 | 2010-12-17 17:28:54 +0000 | [diff] [blame] | 7739 | ping_have_color=MagickTrue; |
glennrp | 8d57966 | 2011-02-23 02:05:02 +0000 | [diff] [blame] | 7740 | ping_have_non_bw=MagickTrue; |
glennrp | 39992b4 | 2010-11-14 00:03:43 +0000 | [diff] [blame] | 7741 | ping_have_PLTE=MagickFalse; |
glennrp | 991d11d | 2010-11-12 21:55:28 +0000 | [diff] [blame] | 7742 | ping_have_bKGD=MagickFalse; |
| 7743 | ping_have_pHYs=MagickFalse; |
| 7744 | ping_have_tRNS=MagickFalse; |
| 7745 | |
glennrp | 0e8ea19 | 2010-12-24 18:00:33 +0000 | [diff] [blame] | 7746 | ping_exclude_bKGD=mng_info->ping_exclude_bKGD; |
| 7747 | ping_exclude_cHRM=mng_info->ping_exclude_cHRM; |
glennrp | a0ed009 | 2011-04-18 16:36:29 +0000 | [diff] [blame] | 7748 | ping_exclude_date=mng_info->ping_exclude_date; |
glennrp | dde35db | 2011-02-21 12:06:32 +0000 | [diff] [blame] | 7749 | /* ping_exclude_EXIF=mng_info->ping_exclude_EXIF; */ |
glennrp | 0e8ea19 | 2010-12-24 18:00:33 +0000 | [diff] [blame] | 7750 | ping_exclude_gAMA=mng_info->ping_exclude_gAMA; |
glennrp | 0e8ea19 | 2010-12-24 18:00:33 +0000 | [diff] [blame] | 7751 | ping_exclude_iCCP=mng_info->ping_exclude_iCCP; |
| 7752 | /* ping_exclude_iTXt=mng_info->ping_exclude_iTXt; */ |
| 7753 | ping_exclude_oFFs=mng_info->ping_exclude_oFFs; |
| 7754 | ping_exclude_pHYs=mng_info->ping_exclude_pHYs; |
| 7755 | ping_exclude_sRGB=mng_info->ping_exclude_sRGB; |
| 7756 | ping_exclude_tEXt=mng_info->ping_exclude_tEXt; |
glennrp | dde35db | 2011-02-21 12:06:32 +0000 | [diff] [blame] | 7757 | /* ping_exclude_tRNS=mng_info->ping_exclude_tRNS; */ |
glennrp | 0e8ea19 | 2010-12-24 18:00:33 +0000 | [diff] [blame] | 7758 | ping_exclude_vpAg=mng_info->ping_exclude_vpAg; |
| 7759 | ping_exclude_zCCP=mng_info->ping_exclude_zCCP; /* hex-encoded iCCP in zTXt */ |
| 7760 | ping_exclude_zTXt=mng_info->ping_exclude_zTXt; |
| 7761 | |
glennrp | 8d3d6e5 | 2011-04-19 04:39:51 +0000 | [diff] [blame] | 7762 | ping_preserve_colormap = mng_info->ping_preserve_colormap; |
glennrp | 0e8ea19 | 2010-12-24 18:00:33 +0000 | [diff] [blame] | 7763 | ping_need_colortype_warning = MagickFalse; |
| 7764 | |
cristy | 0d57eec | 2011-09-04 22:13:56 +0000 | [diff] [blame] | 7765 | /* Recognize the ICC sRGB profile and convert it to the sRGB chunk, |
| 7766 | * i.e., eliminate the ICC profile and set image->rendering_intent. |
| 7767 | * Note that this will not involve any changes to the actual pixels |
| 7768 | * but merely passes information to applications that read the resulting |
| 7769 | * PNG image. |
| 7770 | */ |
| 7771 | if (ping_exclude_sRGB == MagickFalse) |
| 7772 | { |
| 7773 | char |
| 7774 | *name; |
| 7775 | |
| 7776 | const StringInfo |
| 7777 | *profile; |
| 7778 | |
| 7779 | ResetImageProfileIterator(image); |
| 7780 | for (name=GetNextImageProfile(image); name != (const char *) NULL; ) |
| 7781 | { |
| 7782 | profile=GetImageProfile(image,name); |
| 7783 | |
| 7784 | if (profile != (StringInfo *) NULL) |
| 7785 | { |
| 7786 | if ((LocaleCompare(name,"ICC") == 0) || |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 7787 | (LocaleCompare(name,"ICM") == 0)) |
| 7788 | { |
glennrp | ee7b4c0 | 2011-10-04 01:21:09 +0000 | [diff] [blame] | 7789 | int |
| 7790 | icheck; |
| 7791 | |
| 7792 | /* 0: not a known sRGB profile |
| 7793 | * 1: HP-Microsoft sRGB v2 |
| 7794 | * 2: ICC sRGB v4 perceptual |
| 7795 | * 3: ICC sRGB v2 perceptual no black-compensation |
| 7796 | */ |
| 7797 | png_uint_32 |
| 7798 | check_crc[4] = {0, 0xf29e526dUL, 0xbbef7812UL, 0x427ebb21UL}, |
| 7799 | check_len[4] = {0, 3144, 60960, 3052}; |
| 7800 | |
| 7801 | png_uint_32 |
| 7802 | length, |
| 7803 | profile_crc; |
| 7804 | |
cristy | 0d57eec | 2011-09-04 22:13:56 +0000 | [diff] [blame] | 7805 | unsigned char |
| 7806 | *data; |
| 7807 | |
glennrp | 29a106e | 2011-09-06 17:11:42 +0000 | [diff] [blame] | 7808 | length=(png_uint_32) GetStringInfoLength(profile); |
| 7809 | |
glennrp | ee7b4c0 | 2011-10-04 01:21:09 +0000 | [diff] [blame] | 7810 | for (icheck=3; icheck > 0; icheck--) |
cristy | 0d57eec | 2011-09-04 22:13:56 +0000 | [diff] [blame] | 7811 | { |
glennrp | ee7b4c0 | 2011-10-04 01:21:09 +0000 | [diff] [blame] | 7812 | if (length == check_len[icheck]) |
glennrp | 29a106e | 2011-09-06 17:11:42 +0000 | [diff] [blame] | 7813 | { |
glennrp | ee7b4c0 | 2011-10-04 01:21:09 +0000 | [diff] [blame] | 7814 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 7815 | " Got a %lu-byte ICC profile (potentially sRGB)", |
| 7816 | (unsigned long) length); |
glennrp | 29a106e | 2011-09-06 17:11:42 +0000 | [diff] [blame] | 7817 | |
glennrp | ee7b4c0 | 2011-10-04 01:21:09 +0000 | [diff] [blame] | 7818 | data=GetStringInfoDatum(profile); |
| 7819 | profile_crc=crc32(0,data,length); |
glennrp | 29a106e | 2011-09-06 17:11:42 +0000 | [diff] [blame] | 7820 | |
glennrp | ee7b4c0 | 2011-10-04 01:21:09 +0000 | [diff] [blame] | 7821 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | e54cd4b | 2011-10-04 01:31:35 +0000 | [diff] [blame] | 7822 | " with crc=%8x",(unsigned int) profile_crc); |
glennrp | ee7b4c0 | 2011-10-04 01:21:09 +0000 | [diff] [blame] | 7823 | |
| 7824 | if (profile_crc == check_crc[icheck]) |
| 7825 | { |
| 7826 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 7827 | " It is sRGB."); |
| 7828 | if (image->rendering_intent==UndefinedIntent) |
| 7829 | image->rendering_intent=PerceptualIntent; |
| 7830 | break; |
| 7831 | } |
glennrp | 29a106e | 2011-09-06 17:11:42 +0000 | [diff] [blame] | 7832 | } |
glennrp | 29a106e | 2011-09-06 17:11:42 +0000 | [diff] [blame] | 7833 | } |
glennrp | ee7b4c0 | 2011-10-04 01:21:09 +0000 | [diff] [blame] | 7834 | if (icheck == 0) |
glennrp | 29a106e | 2011-09-06 17:11:42 +0000 | [diff] [blame] | 7835 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | ee7b4c0 | 2011-10-04 01:21:09 +0000 | [diff] [blame] | 7836 | " Got a %lu-byte ICC profile", |
glennrp | 29a106e | 2011-09-06 17:11:42 +0000 | [diff] [blame] | 7837 | (unsigned long) length); |
| 7838 | } |
cristy | 0d57eec | 2011-09-04 22:13:56 +0000 | [diff] [blame] | 7839 | } |
| 7840 | name=GetNextImageProfile(image); |
| 7841 | } |
| 7842 | } |
| 7843 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 7844 | number_opaque = 0; |
| 7845 | number_semitransparent = 0; |
| 7846 | number_transparent = 0; |
| 7847 | |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 7848 | if (logging != MagickFalse) |
| 7849 | { |
| 7850 | if (image->storage_class == UndefinedClass) |
| 7851 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 7852 | " storage_class=UndefinedClass"); |
| 7853 | if (image->storage_class == DirectClass) |
| 7854 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 7855 | " storage_class=DirectClass"); |
| 7856 | if (image->storage_class == PseudoClass) |
| 7857 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 7858 | " storage_class=PseudoClass"); |
| 7859 | } |
glennrp | 28af371 | 2011-04-06 18:07:30 +0000 | [diff] [blame] | 7860 | |
glennrp | 750105b | 2012-04-25 16:20:45 +0000 | [diff] [blame] | 7861 | if (image->storage_class == PseudoClass && |
glennrp | 7e65e93 | 2011-08-19 02:31:16 +0000 | [diff] [blame] | 7862 | (mng_info->write_png8 || mng_info->write_png24 || mng_info->write_png32 || |
| 7863 | (mng_info->write_png_colortype != 0 && |
| 7864 | mng_info->write_png_colortype != 4))) |
| 7865 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 7866 | (void) SyncImage(image,exception); |
glennrp | 7e65e93 | 2011-08-19 02:31:16 +0000 | [diff] [blame] | 7867 | image->storage_class = DirectClass; |
| 7868 | } |
| 7869 | |
glennrp | c6c391a | 2011-04-27 02:23:56 +0000 | [diff] [blame] | 7870 | if (ping_preserve_colormap == MagickFalse) |
glennrp | 28af371 | 2011-04-06 18:07:30 +0000 | [diff] [blame] | 7871 | { |
glennrp | c6c391a | 2011-04-27 02:23:56 +0000 | [diff] [blame] | 7872 | if (image->storage_class != PseudoClass && image->colormap != NULL) |
| 7873 | { |
| 7874 | /* Free the bogus colormap; it can cause trouble later */ |
| 7875 | if (logging != MagickFalse) |
| 7876 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 7877 | " Freeing bogus colormap"); |
cristy | e9ac4c3 | 2011-09-26 18:47:22 +0000 | [diff] [blame] | 7878 | (void) RelinquishMagickMemory(image->colormap); |
glennrp | c6c391a | 2011-04-27 02:23:56 +0000 | [diff] [blame] | 7879 | image->colormap=NULL; |
| 7880 | } |
glennrp | 28af371 | 2011-04-06 18:07:30 +0000 | [diff] [blame] | 7881 | } |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 7882 | |
cristy | 3d9f5ba | 2012-06-26 13:37:31 +0000 | [diff] [blame] | 7883 | if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 7884 | (void) TransformImageColorspace(image,sRGBColorspace,exception); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 7885 | |
glennrp | 3241bd0 | 2010-12-12 04:36:28 +0000 | [diff] [blame] | 7886 | /* |
| 7887 | Sometimes we get PseudoClass images whose RGB values don't match |
| 7888 | the colors in the colormap. This code syncs the RGB values. |
| 7889 | */ |
| 7890 | if (image->depth <= 8 && image->taint && image->storage_class == PseudoClass) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 7891 | (void) SyncImage(image,exception); |
glennrp | 3241bd0 | 2010-12-12 04:36:28 +0000 | [diff] [blame] | 7892 | |
glennrp | a6a0663 | 2011-01-19 15:15:34 +0000 | [diff] [blame] | 7893 | #if (MAGICKCORE_QUANTUM_DEPTH == 8) |
| 7894 | if (image->depth > 8) |
| 7895 | { |
| 7896 | if (logging != MagickFalse) |
| 7897 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 7898 | " Reducing PNG bit depth to 8 since this is a Q8 build."); |
| 7899 | |
| 7900 | image->depth=8; |
| 7901 | } |
| 7902 | #endif |
| 7903 | |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 7904 | /* Respect the -depth option */ |
glennrp | cc95c3f | 2011-04-18 16:46:48 +0000 | [diff] [blame] | 7905 | if (image->depth < MAGICKCORE_QUANTUM_DEPTH) |
| 7906 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 7907 | register Quantum |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 7908 | *r; |
| 7909 | |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 7910 | if (image->depth > 8) |
| 7911 | { |
| 7912 | #if MAGICKCORE_QUANTUM_DEPTH > 16 |
| 7913 | /* Scale to 16-bit */ |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 7914 | LBR16PacketRGBO(image->background_color); |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 7915 | |
| 7916 | for (y=0; y < (ssize_t) image->rows; y++) |
| 7917 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 7918 | r=GetAuthenticPixels(image,0,y,image->columns,1,exception); |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 7919 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 7920 | if (r == (Quantum *) NULL) |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 7921 | break; |
| 7922 | |
| 7923 | for (x=0; x < (ssize_t) image->columns; x++) |
| 7924 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 7925 | LBR16PixelRGBA(r); |
| 7926 | r+=GetPixelChannels(image); |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 7927 | } |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 7928 | |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 7929 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 7930 | break; |
| 7931 | } |
| 7932 | |
| 7933 | if (image->storage_class == PseudoClass && image->colormap != NULL) |
| 7934 | { |
cristy | 3e08f11 | 2011-05-24 13:19:30 +0000 | [diff] [blame] | 7935 | for (i=0; i < (ssize_t) image->colors; i++) |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 7936 | { |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 7937 | LBR16PacketRGBO(image->colormap[i]); |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 7938 | } |
| 7939 | } |
| 7940 | #endif /* MAGICKCORE_QUANTUM_DEPTH > 16 */ |
| 7941 | } |
| 7942 | |
| 7943 | else if (image->depth > 4) |
| 7944 | { |
| 7945 | #if MAGICKCORE_QUANTUM_DEPTH > 8 |
| 7946 | /* Scale to 8-bit */ |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 7947 | LBR08PacketRGBO(image->background_color); |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 7948 | |
| 7949 | for (y=0; y < (ssize_t) image->rows; y++) |
| 7950 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 7951 | r=GetAuthenticPixels(image,0,y,image->columns,1,exception); |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 7952 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 7953 | if (r == (Quantum *) NULL) |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 7954 | break; |
| 7955 | |
| 7956 | for (x=0; x < (ssize_t) image->columns; x++) |
| 7957 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 7958 | LBR08PixelRGBA(r); |
| 7959 | r+=GetPixelChannels(image); |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 7960 | } |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 7961 | |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 7962 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 7963 | break; |
| 7964 | } |
| 7965 | |
| 7966 | if (image->storage_class == PseudoClass && image->colormap != NULL) |
| 7967 | { |
cristy | 3e08f11 | 2011-05-24 13:19:30 +0000 | [diff] [blame] | 7968 | for (i=0; i < (ssize_t) image->colors; i++) |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 7969 | { |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 7970 | LBR08PacketRGBO(image->colormap[i]); |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 7971 | } |
| 7972 | } |
| 7973 | #endif /* MAGICKCORE_QUANTUM_DEPTH > 8 */ |
| 7974 | } |
| 7975 | else |
| 7976 | if (image->depth > 2) |
| 7977 | { |
| 7978 | /* Scale to 4-bit */ |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 7979 | LBR04PacketRGBO(image->background_color); |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 7980 | |
| 7981 | for (y=0; y < (ssize_t) image->rows; y++) |
| 7982 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 7983 | r=GetAuthenticPixels(image,0,y,image->columns,1,exception); |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 7984 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 7985 | if (r == (Quantum *) NULL) |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 7986 | break; |
| 7987 | |
| 7988 | for (x=0; x < (ssize_t) image->columns; x++) |
| 7989 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 7990 | LBR04PixelRGBA(r); |
| 7991 | r+=GetPixelChannels(image); |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 7992 | } |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 7993 | |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 7994 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 7995 | break; |
| 7996 | } |
| 7997 | |
| 7998 | if (image->storage_class == PseudoClass && image->colormap != NULL) |
| 7999 | { |
cristy | 3e08f11 | 2011-05-24 13:19:30 +0000 | [diff] [blame] | 8000 | for (i=0; i < (ssize_t) image->colors; i++) |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 8001 | { |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 8002 | LBR04PacketRGBO(image->colormap[i]); |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 8003 | } |
| 8004 | } |
| 8005 | } |
| 8006 | |
| 8007 | else if (image->depth > 1) |
| 8008 | { |
| 8009 | /* Scale to 2-bit */ |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 8010 | LBR02PacketRGBO(image->background_color); |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 8011 | |
| 8012 | for (y=0; y < (ssize_t) image->rows; y++) |
| 8013 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8014 | r=GetAuthenticPixels(image,0,y,image->columns,1,exception); |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 8015 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8016 | if (r == (Quantum *) NULL) |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 8017 | break; |
| 8018 | |
| 8019 | for (x=0; x < (ssize_t) image->columns; x++) |
| 8020 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8021 | LBR02PixelRGBA(r); |
| 8022 | r+=GetPixelChannels(image); |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 8023 | } |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 8024 | |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 8025 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 8026 | break; |
| 8027 | } |
| 8028 | |
| 8029 | if (image->storage_class == PseudoClass && image->colormap != NULL) |
| 8030 | { |
cristy | 3e08f11 | 2011-05-24 13:19:30 +0000 | [diff] [blame] | 8031 | for (i=0; i < (ssize_t) image->colors; i++) |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 8032 | { |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 8033 | LBR02PacketRGBO(image->colormap[i]); |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 8034 | } |
| 8035 | } |
| 8036 | } |
| 8037 | else |
| 8038 | { |
| 8039 | /* Scale to 1-bit */ |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 8040 | LBR01PacketRGBO(image->background_color); |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 8041 | |
| 8042 | for (y=0; y < (ssize_t) image->rows; y++) |
| 8043 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8044 | r=GetAuthenticPixels(image,0,y,image->columns,1,exception); |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 8045 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8046 | if (r == (Quantum *) NULL) |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 8047 | break; |
| 8048 | |
| 8049 | for (x=0; x < (ssize_t) image->columns; x++) |
| 8050 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8051 | LBR01PixelRGBA(r); |
| 8052 | r+=GetPixelChannels(image); |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 8053 | } |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 8054 | |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 8055 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 8056 | break; |
| 8057 | } |
| 8058 | |
| 8059 | if (image->storage_class == PseudoClass && image->colormap != NULL) |
| 8060 | { |
cristy | 3e08f11 | 2011-05-24 13:19:30 +0000 | [diff] [blame] | 8061 | for (i=0; i < (ssize_t) image->colors; i++) |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 8062 | { |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 8063 | LBR01PacketRGBO(image->colormap[i]); |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 8064 | } |
| 8065 | } |
| 8066 | } |
glennrp | 9d0ea4d | 2011-04-22 18:35:57 +0000 | [diff] [blame] | 8067 | } |
| 8068 | |
glennrp | 67b9c1a | 2011-04-22 18:47:36 +0000 | [diff] [blame] | 8069 | /* To do: set to next higher multiple of 8 */ |
| 8070 | if (image->depth < 8) |
glennrp | 70e68a8 | 2011-04-01 22:51:14 +0000 | [diff] [blame] | 8071 | image->depth=8; |
glennrp | a6a0663 | 2011-01-19 15:15:34 +0000 | [diff] [blame] | 8072 | |
glennrp | 2b013e4 | 2010-11-24 16:55:50 +0000 | [diff] [blame] | 8073 | #if (MAGICKCORE_QUANTUM_DEPTH > 16) |
| 8074 | /* PNG does not handle depths greater than 16 so reduce it even |
| 8075 | * if lossy |
| 8076 | */ |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 8077 | if (image->depth > 8) |
glennrp | 2b013e4 | 2010-11-24 16:55:50 +0000 | [diff] [blame] | 8078 | image->depth=16; |
| 8079 | #endif |
| 8080 | |
glennrp | 3faa9a3 | 2011-04-23 14:00:25 +0000 | [diff] [blame] | 8081 | #if (MAGICKCORE_QUANTUM_DEPTH > 8) |
glennrp | cc5d45b | 2012-01-06 04:06:10 +0000 | [diff] [blame] | 8082 | if (image->depth > 8) |
| 8083 | { |
| 8084 | /* To do: fill low byte properly */ |
| 8085 | image->depth=16; |
| 8086 | } |
| 8087 | |
glennrp | c722dd8 | 2011-02-24 05:13:21 +0000 | [diff] [blame] | 8088 | if (image->depth == 16 && mng_info->write_png_depth != 16) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8089 | if (mng_info->write_png8 || LosslessReduceDepthOK(image,exception) != MagickFalse) |
glennrp | 8640fb5 | 2010-11-23 15:48:26 +0000 | [diff] [blame] | 8090 | image->depth = 8; |
| 8091 | #endif |
| 8092 | |
glennrp | c8c2f06 | 2011-02-25 19:00:33 +0000 | [diff] [blame] | 8093 | /* Normally we run this just once, but in the case of writing PNG8 |
glennrp | e9637cb | 2011-03-24 16:34:37 +0000 | [diff] [blame] | 8094 | * we reduce the transparency to binary and run again, then if there |
| 8095 | * 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] | 8096 | * RGBA palette and run again, and then to a simple 3-3-2-1 RGBA |
| 8097 | * palette. Then (To do) we take care of a final reduction that is only |
| 8098 | * needed if there are still 256 colors present and one of them has both |
| 8099 | * transparent and opaque instances. |
glennrp | c8c2f06 | 2011-02-25 19:00:33 +0000 | [diff] [blame] | 8100 | */ |
glennrp | 82b3c53 | 2011-03-22 19:20:54 +0000 | [diff] [blame] | 8101 | |
glennrp | 8ca51ad | 2011-05-12 21:22:32 +0000 | [diff] [blame] | 8102 | tried_332 = MagickFalse; |
glennrp | 82b3c53 | 2011-03-22 19:20:54 +0000 | [diff] [blame] | 8103 | tried_333 = MagickFalse; |
glennrp | d337164 | 2011-03-22 19:42:23 +0000 | [diff] [blame] | 8104 | tried_444 = MagickFalse; |
glennrp | 82b3c53 | 2011-03-22 19:20:54 +0000 | [diff] [blame] | 8105 | |
glennrp | 8ca51ad | 2011-05-12 21:22:32 +0000 | [diff] [blame] | 8106 | for (j=0; j<6; j++) |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8107 | { |
| 8108 | /* BUILD_PALETTE |
| 8109 | * |
| 8110 | * Sometimes we get DirectClass images that have 256 colors or fewer. |
| 8111 | * This code will build a colormap. |
| 8112 | * |
| 8113 | * Also, sometimes we get PseudoClass images with an out-of-date |
| 8114 | * colormap. This code will replace the colormap with a new one. |
| 8115 | * Sometimes we get PseudoClass images that have more than 256 colors. |
| 8116 | * This code will delete the colormap and change the image to |
| 8117 | * DirectClass. |
| 8118 | * |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8119 | * If image->matte is MagickFalse, we ignore the alpha channel |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8120 | * even though it sometimes contains left-over non-opaque values. |
| 8121 | * |
| 8122 | * Also we gather some information (number of opaque, transparent, |
| 8123 | * and semitransparent pixels, and whether the image has any non-gray |
| 8124 | * pixels or only black-and-white pixels) that we might need later. |
| 8125 | * |
| 8126 | * Even if the user wants to force GrayAlpha or RGBA (colortype 4 or 6) |
| 8127 | * we need to check for bogus non-opaque values, at least. |
| 8128 | */ |
glennrp | 3c21811 | 2010-11-27 15:31:26 +0000 | [diff] [blame] | 8129 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8130 | int |
| 8131 | n; |
glennrp | 3c21811 | 2010-11-27 15:31:26 +0000 | [diff] [blame] | 8132 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8133 | PixelInfo |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8134 | opaque[260], |
| 8135 | semitransparent[260], |
| 8136 | transparent[260]; |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 8137 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8138 | register const Quantum |
| 8139 | *s; |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 8140 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8141 | register Quantum |
| 8142 | *q, |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 8143 | *r; |
| 8144 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8145 | if (logging != MagickFalse) |
| 8146 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8147 | " Enter BUILD_PALETTE:"); |
| 8148 | |
| 8149 | if (logging != MagickFalse) |
| 8150 | { |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 8151 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8152 | " image->columns=%.20g",(double) image->columns); |
| 8153 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8154 | " image->rows=%.20g",(double) image->rows); |
| 8155 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8156 | " image->matte=%.20g",(double) image->matte); |
| 8157 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8158 | " image->depth=%.20g",(double) image->depth); |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 8159 | |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 8160 | if (image->storage_class == PseudoClass && image->colormap != NULL) |
glennrp | 7ddcc22 | 2010-12-11 05:01:05 +0000 | [diff] [blame] | 8161 | { |
| 8162 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8163 | " Original colormap:"); |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 8164 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8165 | " i (red,green,blue,alpha)"); |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 8166 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8167 | for (i=0; i < 256; i++) |
glennrp | 7ddcc22 | 2010-12-11 05:01:05 +0000 | [diff] [blame] | 8168 | { |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8169 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8170 | " %d (%d,%d,%d,%d)", |
| 8171 | (int) i, |
| 8172 | (int) image->colormap[i].red, |
| 8173 | (int) image->colormap[i].green, |
| 8174 | (int) image->colormap[i].blue, |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8175 | (int) image->colormap[i].alpha); |
glennrp | 7ddcc22 | 2010-12-11 05:01:05 +0000 | [diff] [blame] | 8176 | } |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 8177 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8178 | for (i=image->colors - 10; i < (ssize_t) image->colors; i++) |
| 8179 | { |
| 8180 | if (i > 255) |
| 8181 | { |
| 8182 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8183 | " %d (%d,%d,%d,%d)", |
| 8184 | (int) i, |
| 8185 | (int) image->colormap[i].red, |
| 8186 | (int) image->colormap[i].green, |
| 8187 | (int) image->colormap[i].blue, |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8188 | (int) image->colormap[i].alpha); |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8189 | } |
| 8190 | } |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 8191 | } |
glennrp | 7ddcc22 | 2010-12-11 05:01:05 +0000 | [diff] [blame] | 8192 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8193 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8194 | " image->colors=%d",(int) image->colors); |
glennrp | 7ddcc22 | 2010-12-11 05:01:05 +0000 | [diff] [blame] | 8195 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8196 | if (image->colors == 0) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8197 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8198 | " (zero means unknown)"); |
glennrp | d6bf161 | 2010-12-17 17:28:54 +0000 | [diff] [blame] | 8199 | |
glennrp | 8d3d6e5 | 2011-04-19 04:39:51 +0000 | [diff] [blame] | 8200 | if (ping_preserve_colormap == MagickFalse) |
| 8201 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8202 | " Regenerate the colormap"); |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8203 | } |
| 8204 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8205 | image_colors=0; |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 8206 | number_opaque = 0; |
| 8207 | number_semitransparent = 0; |
| 8208 | number_transparent = 0; |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8209 | |
| 8210 | for (y=0; y < (ssize_t) image->rows; y++) |
| 8211 | { |
| 8212 | q=GetAuthenticPixels(image,0,y,image->columns,1,exception); |
| 8213 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8214 | if (q == (Quantum *) NULL) |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8215 | break; |
| 8216 | |
| 8217 | for (x=0; x < (ssize_t) image->columns; x++) |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 8218 | { |
glennrp | 4737d52 | 2011-04-29 03:33:42 +0000 | [diff] [blame] | 8219 | if (image->matte == MagickFalse || |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8220 | GetPixelAlpha(image,q) == OpaqueAlpha) |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8221 | { |
| 8222 | if (number_opaque < 259) |
| 8223 | { |
| 8224 | if (number_opaque == 0) |
| 8225 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8226 | GetPixelInfoPixel(image, q, opaque); |
| 8227 | opaque[0].alpha=OpaqueAlpha; |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8228 | number_opaque=1; |
| 8229 | } |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 8230 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8231 | for (i=0; i< (ssize_t) number_opaque; i++) |
| 8232 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8233 | if (IsPixelEquivalent(image,q, opaque+i)) |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8234 | break; |
| 8235 | } |
glennrp | 7ddcc22 | 2010-12-11 05:01:05 +0000 | [diff] [blame] | 8236 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8237 | if (i == (ssize_t) number_opaque && number_opaque < 259) |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8238 | { |
| 8239 | number_opaque++; |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8240 | GetPixelInfoPixel(image, q, opaque+i); |
| 8241 | opaque[i].alpha=OpaqueAlpha; |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8242 | } |
| 8243 | } |
| 8244 | } |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8245 | else if (GetPixelAlpha(image,q) == TransparentAlpha) |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8246 | { |
| 8247 | if (number_transparent < 259) |
| 8248 | { |
| 8249 | if (number_transparent == 0) |
| 8250 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8251 | GetPixelInfoPixel(image, q, transparent); |
| 8252 | ping_trans_color.red=(unsigned short) |
| 8253 | GetPixelRed(image,q); |
| 8254 | ping_trans_color.green=(unsigned short) |
| 8255 | GetPixelGreen(image,q); |
| 8256 | ping_trans_color.blue=(unsigned short) |
| 8257 | GetPixelBlue(image,q); |
| 8258 | ping_trans_color.gray=(unsigned short) |
cristy | 972d1c4 | 2012-07-14 23:29:14 +0000 | [diff] [blame] | 8259 | GetPixelGray(image,q); |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8260 | number_transparent = 1; |
| 8261 | } |
| 8262 | |
| 8263 | for (i=0; i< (ssize_t) number_transparent; i++) |
| 8264 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8265 | if (IsPixelEquivalent(image,q, transparent+i)) |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8266 | break; |
| 8267 | } |
| 8268 | |
| 8269 | if (i == (ssize_t) number_transparent && |
| 8270 | number_transparent < 259) |
| 8271 | { |
| 8272 | number_transparent++; |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8273 | GetPixelInfoPixel(image,q,transparent+i); |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8274 | } |
| 8275 | } |
| 8276 | } |
| 8277 | else |
| 8278 | { |
| 8279 | if (number_semitransparent < 259) |
| 8280 | { |
| 8281 | if (number_semitransparent == 0) |
| 8282 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8283 | GetPixelInfoPixel(image,q,semitransparent); |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8284 | number_semitransparent = 1; |
| 8285 | } |
| 8286 | |
| 8287 | for (i=0; i< (ssize_t) number_semitransparent; i++) |
| 8288 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8289 | if (IsPixelEquivalent(image,q, semitransparent+i) |
| 8290 | && GetPixelAlpha(image,q) == |
| 8291 | semitransparent[i].alpha) |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8292 | break; |
| 8293 | } |
| 8294 | |
| 8295 | if (i == (ssize_t) number_semitransparent && |
| 8296 | number_semitransparent < 259) |
| 8297 | { |
| 8298 | number_semitransparent++; |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8299 | GetPixelInfoPixel(image, q, semitransparent+i); |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8300 | } |
| 8301 | } |
| 8302 | } |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8303 | q+=GetPixelChannels(image); |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8304 | } |
| 8305 | } |
| 8306 | |
cristy | 4054bfb | 2011-08-29 23:41:39 +0000 | [diff] [blame] | 8307 | if (mng_info->write_png8 == MagickFalse && |
| 8308 | ping_exclude_bKGD == MagickFalse) |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8309 | { |
| 8310 | /* Add the background color to the palette, if it |
| 8311 | * isn't already there. |
| 8312 | */ |
glennrp | c6c391a | 2011-04-27 02:23:56 +0000 | [diff] [blame] | 8313 | if (logging != MagickFalse) |
| 8314 | { |
| 8315 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8316 | " Check colormap for background (%d,%d,%d)", |
| 8317 | (int) image->background_color.red, |
| 8318 | (int) image->background_color.green, |
| 8319 | (int) image->background_color.blue); |
| 8320 | } |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8321 | for (i=0; i<number_opaque; i++) |
| 8322 | { |
glennrp | ca7ad3a | 2011-04-26 04:44:54 +0000 | [diff] [blame] | 8323 | if (opaque[i].red == image->background_color.red && |
| 8324 | opaque[i].green == image->background_color.green && |
| 8325 | opaque[i].blue == image->background_color.blue) |
| 8326 | break; |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8327 | } |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8328 | if (number_opaque < 259 && i == number_opaque) |
| 8329 | { |
glennrp | 8e045c8 | 2011-04-27 16:40:27 +0000 | [diff] [blame] | 8330 | opaque[i] = image->background_color; |
glennrp | c6c391a | 2011-04-27 02:23:56 +0000 | [diff] [blame] | 8331 | ping_background.index = i; |
glennrp | 388a8c8 | 2012-06-27 13:41:51 +0000 | [diff] [blame] | 8332 | number_opaque++; |
glennrp | c6c391a | 2011-04-27 02:23:56 +0000 | [diff] [blame] | 8333 | if (logging != MagickFalse) |
| 8334 | { |
| 8335 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8336 | " background_color index is %d",(int) i); |
| 8337 | } |
| 8338 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8339 | } |
glennrp | a080bc3 | 2011-03-11 18:03:44 +0000 | [diff] [blame] | 8340 | else if (logging != MagickFalse) |
| 8341 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8342 | " No room in the colormap to add background color"); |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8343 | } |
| 8344 | |
| 8345 | image_colors=number_opaque+number_transparent+number_semitransparent; |
| 8346 | |
glennrp | a080bc3 | 2011-03-11 18:03:44 +0000 | [diff] [blame] | 8347 | if (mng_info->write_png8 != MagickFalse && image_colors > 256) |
| 8348 | { |
| 8349 | /* No room for the background color; remove it. */ |
| 8350 | number_opaque--; |
| 8351 | image_colors--; |
| 8352 | } |
| 8353 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8354 | if (logging != MagickFalse) |
| 8355 | { |
| 8356 | if (image_colors > 256) |
| 8357 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8358 | " image has more than 256 colors"); |
| 8359 | |
| 8360 | else |
| 8361 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8362 | " image has %d colors",image_colors); |
| 8363 | } |
| 8364 | |
glennrp | 8d3d6e5 | 2011-04-19 04:39:51 +0000 | [diff] [blame] | 8365 | if (ping_preserve_colormap != MagickFalse) |
| 8366 | break; |
glennrp | 8d3d6e5 | 2011-04-19 04:39:51 +0000 | [diff] [blame] | 8367 | |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 8368 | if (mng_info->write_png_colortype != 7) /* We won't need this info */ |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8369 | { |
| 8370 | ping_have_color=MagickFalse; |
| 8371 | ping_have_non_bw=MagickFalse; |
| 8372 | |
glennrp | 0fa2580 | 2012-07-20 14:01:06 +0000 | [diff] [blame] | 8373 | if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse) |
| 8374 | { |
| 8375 | ping_have_color=MagickTrue; |
| 8376 | ping_have_non_bw=MagickFalse; |
| 8377 | } |
| 8378 | |
| 8379 | if (IssRGBColorspace(image->colorspace) != MagickFalse) |
| 8380 | { |
| 8381 | ping_have_color=MagickTrue; |
| 8382 | ping_have_non_bw=MagickTrue; |
| 8383 | } |
| 8384 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8385 | if(image_colors > 256) |
| 8386 | { |
| 8387 | for (y=0; y < (ssize_t) image->rows; y++) |
| 8388 | { |
| 8389 | q=GetAuthenticPixels(image,0,y,image->columns,1,exception); |
| 8390 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8391 | if (q == (Quantum *) NULL) |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8392 | break; |
| 8393 | |
glennrp | e5e6b80 | 2011-07-20 14:44:40 +0000 | [diff] [blame] | 8394 | s=q; |
| 8395 | for (x=0; x < (ssize_t) image->columns; x++) |
| 8396 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8397 | if (GetPixelRed(image,s) != GetPixelGreen(image,s) || |
| 8398 | GetPixelRed(image,s) != GetPixelBlue(image,s)) |
glennrp | e5e6b80 | 2011-07-20 14:44:40 +0000 | [diff] [blame] | 8399 | { |
| 8400 | ping_have_color=MagickTrue; |
| 8401 | ping_have_non_bw=MagickTrue; |
| 8402 | break; |
| 8403 | } |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8404 | s+=GetPixelChannels(image); |
glennrp | e5e6b80 | 2011-07-20 14:44:40 +0000 | [diff] [blame] | 8405 | } |
| 8406 | |
| 8407 | if (ping_have_color != MagickFalse) |
| 8408 | break; |
| 8409 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8410 | /* Worst case is black-and-white; we are looking at every |
| 8411 | * pixel twice. |
| 8412 | */ |
| 8413 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8414 | if (ping_have_non_bw == MagickFalse) |
| 8415 | { |
| 8416 | s=q; |
| 8417 | for (x=0; x < (ssize_t) image->columns; x++) |
| 8418 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8419 | if (GetPixelRed(image,s) != 0 && |
| 8420 | GetPixelRed(image,s) != QuantumRange) |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8421 | { |
| 8422 | ping_have_non_bw=MagickTrue; |
glennrp | e5e6b80 | 2011-07-20 14:44:40 +0000 | [diff] [blame] | 8423 | break; |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8424 | } |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8425 | s+=GetPixelChannels(image); |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8426 | } |
glennrp | e5e6b80 | 2011-07-20 14:44:40 +0000 | [diff] [blame] | 8427 | } |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8428 | } |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 8429 | } |
| 8430 | } |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8431 | |
| 8432 | if (image_colors < 257) |
| 8433 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8434 | PixelInfo |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8435 | colormap[260]; |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 8436 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8437 | /* |
| 8438 | * Initialize image colormap. |
glennrp | 97fd3d0 | 2011-02-23 14:58:06 +0000 | [diff] [blame] | 8439 | */ |
| 8440 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8441 | if (logging != MagickFalse) |
| 8442 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8443 | " Sort the new colormap"); |
glennrp | 8d57966 | 2011-02-23 02:05:02 +0000 | [diff] [blame] | 8444 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8445 | /* Sort palette, transparent first */; |
| 8446 | |
| 8447 | n = 0; |
| 8448 | |
| 8449 | for (i=0; i<number_transparent; i++) |
| 8450 | colormap[n++] = transparent[i]; |
| 8451 | |
| 8452 | for (i=0; i<number_semitransparent; i++) |
| 8453 | colormap[n++] = semitransparent[i]; |
| 8454 | |
| 8455 | for (i=0; i<number_opaque; i++) |
| 8456 | colormap[n++] = opaque[i]; |
| 8457 | |
glennrp | c6c391a | 2011-04-27 02:23:56 +0000 | [diff] [blame] | 8458 | ping_background.index += |
| 8459 | (number_transparent + number_semitransparent); |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 8460 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8461 | /* image_colors < 257; search the colormap instead of the pixels |
| 8462 | * to get ping_have_color and ping_have_non_bw |
| 8463 | */ |
| 8464 | for (i=0; i<n; i++) |
| 8465 | { |
| 8466 | if (ping_have_color == MagickFalse) |
glennrp | 8d57966 | 2011-02-23 02:05:02 +0000 | [diff] [blame] | 8467 | { |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8468 | if (colormap[i].red != colormap[i].green || |
| 8469 | colormap[i].red != colormap[i].blue) |
| 8470 | { |
| 8471 | ping_have_color=MagickTrue; |
| 8472 | ping_have_non_bw=MagickTrue; |
| 8473 | break; |
| 8474 | } |
| 8475 | } |
| 8476 | |
| 8477 | if (ping_have_non_bw == MagickFalse) |
| 8478 | { |
| 8479 | if (colormap[i].red != 0 && colormap[i].red != QuantumRange) |
glennrp | 8d57966 | 2011-02-23 02:05:02 +0000 | [diff] [blame] | 8480 | ping_have_non_bw=MagickTrue; |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 8481 | } |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 8482 | } |
| 8483 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8484 | if ((mng_info->ping_exclude_tRNS == MagickFalse || |
| 8485 | (number_transparent == 0 && number_semitransparent == 0)) && |
| 8486 | (((mng_info->write_png_colortype-1) == |
| 8487 | PNG_COLOR_TYPE_PALETTE) || |
| 8488 | (mng_info->write_png_colortype == 0))) |
| 8489 | { |
glennrp | 6185c53 | 2011-01-14 17:58:40 +0000 | [diff] [blame] | 8490 | if (logging != MagickFalse) |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 8491 | { |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8492 | if (n != (ssize_t) image_colors) |
| 8493 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8494 | " image_colors (%d) and n (%d) don't match", |
| 8495 | image_colors, n); |
glennrp | 6185c53 | 2011-01-14 17:58:40 +0000 | [diff] [blame] | 8496 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8497 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8498 | " AcquireImageColormap"); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 8499 | } |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 8500 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8501 | image->colors = image_colors; |
| 8502 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8503 | if (AcquireImageColormap(image,image_colors,exception) == |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8504 | MagickFalse) |
glennrp | 3faa9a3 | 2011-04-23 14:00:25 +0000 | [diff] [blame] | 8505 | ThrowWriterException(ResourceLimitError, |
| 8506 | "MemoryAllocationFailed"); |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8507 | |
| 8508 | for (i=0; i< (ssize_t) image_colors; i++) |
| 8509 | image->colormap[i] = colormap[i]; |
| 8510 | |
| 8511 | if (logging != MagickFalse) |
glennrp | 6185c53 | 2011-01-14 17:58:40 +0000 | [diff] [blame] | 8512 | { |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8513 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8514 | " image->colors=%d (%d)", |
| 8515 | (int) image->colors, image_colors); |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 8516 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8517 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8518 | " Update the pixel indexes"); |
| 8519 | } |
glennrp | 6185c53 | 2011-01-14 17:58:40 +0000 | [diff] [blame] | 8520 | |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 8521 | /* Sync the pixel indices with the new colormap */ |
| 8522 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8523 | for (y=0; y < (ssize_t) image->rows; y++) |
| 8524 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8525 | q=GetAuthenticPixels(image,0,y,image->columns,1,exception); |
glennrp | 6185c53 | 2011-01-14 17:58:40 +0000 | [diff] [blame] | 8526 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8527 | if (q == (Quantum *) NULL) |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8528 | break; |
glennrp | 6185c53 | 2011-01-14 17:58:40 +0000 | [diff] [blame] | 8529 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8530 | for (x=0; x < (ssize_t) image->columns; x++) |
glennrp | 6185c53 | 2011-01-14 17:58:40 +0000 | [diff] [blame] | 8531 | { |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8532 | for (i=0; i< (ssize_t) image_colors; i++) |
glennrp | 6185c53 | 2011-01-14 17:58:40 +0000 | [diff] [blame] | 8533 | { |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8534 | if ((image->matte == MagickFalse || |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8535 | image->colormap[i].alpha == GetPixelAlpha(image,q)) && |
| 8536 | image->colormap[i].red == GetPixelRed(image,q) && |
| 8537 | image->colormap[i].green == GetPixelGreen(image,q) && |
| 8538 | image->colormap[i].blue == GetPixelBlue(image,q)) |
glennrp | 6185c53 | 2011-01-14 17:58:40 +0000 | [diff] [blame] | 8539 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8540 | SetPixelIndex(image,i,q); |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8541 | break; |
glennrp | 6185c53 | 2011-01-14 17:58:40 +0000 | [diff] [blame] | 8542 | } |
glennrp | 6185c53 | 2011-01-14 17:58:40 +0000 | [diff] [blame] | 8543 | } |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8544 | q+=GetPixelChannels(image); |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8545 | } |
glennrp | 6185c53 | 2011-01-14 17:58:40 +0000 | [diff] [blame] | 8546 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8547 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 8548 | break; |
| 8549 | } |
| 8550 | } |
| 8551 | } |
| 8552 | |
| 8553 | if (logging != MagickFalse) |
| 8554 | { |
| 8555 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8556 | " image->colors=%d", (int) image->colors); |
| 8557 | |
| 8558 | if (image->colormap != NULL) |
| 8559 | { |
| 8560 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8561 | " i (red,green,blue,alpha)"); |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8562 | |
| 8563 | for (i=0; i < (ssize_t) image->colors; i++) |
| 8564 | { |
cristy | 7298848 | 2011-03-29 16:34:38 +0000 | [diff] [blame] | 8565 | if (i < 300 || i >= (ssize_t) image->colors - 10) |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8566 | { |
| 8567 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8568 | " %d (%d,%d,%d,%d)", |
| 8569 | (int) i, |
| 8570 | (int) image->colormap[i].red, |
| 8571 | (int) image->colormap[i].green, |
| 8572 | (int) image->colormap[i].blue, |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8573 | (int) image->colormap[i].alpha); |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8574 | } |
glennrp | 6185c53 | 2011-01-14 17:58:40 +0000 | [diff] [blame] | 8575 | } |
| 8576 | } |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 8577 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8578 | if (number_transparent < 257) |
| 8579 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8580 | " number_transparent = %d", |
| 8581 | number_transparent); |
| 8582 | else |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 8583 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8584 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8585 | " number_transparent > 256"); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 8586 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8587 | if (number_opaque < 257) |
| 8588 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8589 | " number_opaque = %d", |
| 8590 | number_opaque); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 8591 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8592 | else |
| 8593 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8594 | " number_opaque > 256"); |
glennrp | 6185c53 | 2011-01-14 17:58:40 +0000 | [diff] [blame] | 8595 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8596 | if (number_semitransparent < 257) |
| 8597 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8598 | " number_semitransparent = %d", |
| 8599 | number_semitransparent); |
glennrp | 6185c53 | 2011-01-14 17:58:40 +0000 | [diff] [blame] | 8600 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8601 | else |
| 8602 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8603 | " number_semitransparent > 256"); |
glennrp | a6a0663 | 2011-01-19 15:15:34 +0000 | [diff] [blame] | 8604 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8605 | if (ping_have_non_bw == MagickFalse) |
| 8606 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8607 | " All pixels and the background are black or white"); |
glennrp | a6a0663 | 2011-01-19 15:15:34 +0000 | [diff] [blame] | 8608 | |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8609 | else if (ping_have_color == MagickFalse) |
| 8610 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8611 | " All pixels and the background are gray"); |
| 8612 | |
| 8613 | else |
| 8614 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8615 | " At least one pixel or the background is non-gray"); |
glennrp | 6185c53 | 2011-01-14 17:58:40 +0000 | [diff] [blame] | 8616 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 8617 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8618 | " Exit BUILD_PALETTE:"); |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8619 | } |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 8620 | |
glennrp | c8c2f06 | 2011-02-25 19:00:33 +0000 | [diff] [blame] | 8621 | if (mng_info->write_png8 == MagickFalse) |
| 8622 | break; |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 8623 | |
glennrp | c8c2f06 | 2011-02-25 19:00:33 +0000 | [diff] [blame] | 8624 | /* Make any reductions necessary for the PNG8 format */ |
| 8625 | if (image_colors <= 256 && |
| 8626 | image_colors != 0 && image->colormap != NULL && |
| 8627 | number_semitransparent == 0 && |
| 8628 | number_transparent <= 1) |
| 8629 | break; |
| 8630 | |
| 8631 | /* PNG8 can't have semitransparent colors so we threshold the |
glennrp | 130fc45 | 2011-08-20 03:43:18 +0000 | [diff] [blame] | 8632 | * opacity to 0 or OpaqueOpacity, and PNG8 can only have one |
| 8633 | * transparent color so if more than one is transparent we merge |
| 8634 | * them into image->background_color. |
glennrp | c8c2f06 | 2011-02-25 19:00:33 +0000 | [diff] [blame] | 8635 | */ |
glennrp | 130fc45 | 2011-08-20 03:43:18 +0000 | [diff] [blame] | 8636 | if (number_semitransparent != 0 || number_transparent > 1) |
glennrp | c8c2f06 | 2011-02-25 19:00:33 +0000 | [diff] [blame] | 8637 | { |
| 8638 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8639 | " Thresholding the alpha channel to binary"); |
| 8640 | |
| 8641 | for (y=0; y < (ssize_t) image->rows; y++) |
| 8642 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8643 | r=GetAuthenticPixels(image,0,y,image->columns,1,exception); |
glennrp | c8c2f06 | 2011-02-25 19:00:33 +0000 | [diff] [blame] | 8644 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8645 | if (r == (Quantum *) NULL) |
glennrp | c8c2f06 | 2011-02-25 19:00:33 +0000 | [diff] [blame] | 8646 | break; |
| 8647 | |
| 8648 | for (x=0; x < (ssize_t) image->columns; x++) |
| 8649 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8650 | if (GetPixelAlpha(image,r) < OpaqueAlpha/2) |
glennrp | 8ca51ad | 2011-05-12 21:22:32 +0000 | [diff] [blame] | 8651 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8652 | SetPixelInfoPixel(image,&image->background_color,r); |
| 8653 | SetPixelAlpha(image,TransparentAlpha,r); |
glennrp | 8ca51ad | 2011-05-12 21:22:32 +0000 | [diff] [blame] | 8654 | } |
| 8655 | else |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8656 | SetPixelAlpha(image,OpaqueAlpha,r); |
| 8657 | r+=GetPixelChannels(image); |
glennrp | c8c2f06 | 2011-02-25 19:00:33 +0000 | [diff] [blame] | 8658 | } |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 8659 | |
glennrp | c8c2f06 | 2011-02-25 19:00:33 +0000 | [diff] [blame] | 8660 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 8661 | break; |
| 8662 | |
| 8663 | if (image_colors != 0 && image_colors <= 256 && |
| 8664 | image->colormap != NULL) |
| 8665 | for (i=0; i<image_colors; i++) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8666 | image->colormap[i].alpha = |
| 8667 | (image->colormap[i].alpha > TransparentAlpha/2 ? |
| 8668 | TransparentAlpha : OpaqueAlpha); |
glennrp | c8c2f06 | 2011-02-25 19:00:33 +0000 | [diff] [blame] | 8669 | } |
| 8670 | continue; |
| 8671 | } |
| 8672 | |
| 8673 | /* 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] | 8674 | * background color to the 4-4-4-1, 3-3-3-1 or 3-3-2-1 palette. If the |
| 8675 | * image is mostly gray, the 4-4-4-1 palette is likely to end up with 256 |
| 8676 | * colors or less. |
glennrp | c8c2f06 | 2011-02-25 19:00:33 +0000 | [diff] [blame] | 8677 | */ |
glennrp | d337164 | 2011-03-22 19:42:23 +0000 | [diff] [blame] | 8678 | if (tried_444 == MagickFalse && (image_colors == 0 || image_colors > 256)) |
| 8679 | { |
| 8680 | if (logging != MagickFalse) |
| 8681 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8682 | " Quantizing the background color to 4-4-4"); |
| 8683 | |
| 8684 | tried_444 = MagickTrue; |
| 8685 | |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 8686 | LBR04PacketRGB(image->background_color); |
glennrp | d337164 | 2011-03-22 19:42:23 +0000 | [diff] [blame] | 8687 | |
| 8688 | if (logging != MagickFalse) |
| 8689 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8690 | " Quantizing the pixel colors to 4-4-4"); |
| 8691 | |
| 8692 | if (image->colormap == NULL) |
| 8693 | { |
| 8694 | for (y=0; y < (ssize_t) image->rows; y++) |
| 8695 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8696 | r=GetAuthenticPixels(image,0,y,image->columns,1,exception); |
glennrp | d337164 | 2011-03-22 19:42:23 +0000 | [diff] [blame] | 8697 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8698 | if (r == (Quantum *) NULL) |
glennrp | d337164 | 2011-03-22 19:42:23 +0000 | [diff] [blame] | 8699 | break; |
| 8700 | |
| 8701 | for (x=0; x < (ssize_t) image->columns; x++) |
| 8702 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8703 | if (GetPixelAlpha(image,r) == OpaqueAlpha) |
glennrp | 54cf797 | 2011-08-06 14:28:09 +0000 | [diff] [blame] | 8704 | LBR04PixelRGB(r); |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8705 | r+=GetPixelChannels(image); |
glennrp | d337164 | 2011-03-22 19:42:23 +0000 | [diff] [blame] | 8706 | } |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 8707 | |
glennrp | d337164 | 2011-03-22 19:42:23 +0000 | [diff] [blame] | 8708 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 8709 | break; |
| 8710 | } |
| 8711 | } |
| 8712 | |
| 8713 | else /* Should not reach this; colormap already exists and |
| 8714 | must be <= 256 */ |
| 8715 | { |
| 8716 | if (logging != MagickFalse) |
| 8717 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8718 | " Quantizing the colormap to 4-4-4"); |
glennrp | 8e58efd | 2011-05-20 12:16:29 +0000 | [diff] [blame] | 8719 | |
glennrp | d337164 | 2011-03-22 19:42:23 +0000 | [diff] [blame] | 8720 | for (i=0; i<image_colors; i++) |
| 8721 | { |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 8722 | LBR04PacketRGB(image->colormap[i]); |
glennrp | d337164 | 2011-03-22 19:42:23 +0000 | [diff] [blame] | 8723 | } |
| 8724 | } |
| 8725 | continue; |
| 8726 | } |
| 8727 | |
glennrp | 82b3c53 | 2011-03-22 19:20:54 +0000 | [diff] [blame] | 8728 | if (tried_333 == MagickFalse && (image_colors == 0 || image_colors > 256)) |
| 8729 | { |
| 8730 | if (logging != MagickFalse) |
| 8731 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8732 | " Quantizing the background color to 3-3-3"); |
| 8733 | |
| 8734 | tried_333 = MagickTrue; |
| 8735 | |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 8736 | LBR03PacketRGB(image->background_color); |
glennrp | 82b3c53 | 2011-03-22 19:20:54 +0000 | [diff] [blame] | 8737 | |
| 8738 | if (logging != MagickFalse) |
| 8739 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | e9637cb | 2011-03-24 16:34:37 +0000 | [diff] [blame] | 8740 | " Quantizing the pixel colors to 3-3-3-1"); |
glennrp | 82b3c53 | 2011-03-22 19:20:54 +0000 | [diff] [blame] | 8741 | |
| 8742 | if (image->colormap == NULL) |
| 8743 | { |
| 8744 | for (y=0; y < (ssize_t) image->rows; y++) |
| 8745 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8746 | r=GetAuthenticPixels(image,0,y,image->columns,1,exception); |
glennrp | 82b3c53 | 2011-03-22 19:20:54 +0000 | [diff] [blame] | 8747 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8748 | if (r == (Quantum *) NULL) |
glennrp | 82b3c53 | 2011-03-22 19:20:54 +0000 | [diff] [blame] | 8749 | break; |
| 8750 | |
| 8751 | for (x=0; x < (ssize_t) image->columns; x++) |
| 8752 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8753 | if (GetPixelAlpha(image,r) == OpaqueAlpha) |
| 8754 | LBR03RGB(r); |
| 8755 | r+=GetPixelChannels(image); |
glennrp | 82b3c53 | 2011-03-22 19:20:54 +0000 | [diff] [blame] | 8756 | } |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 8757 | |
glennrp | 82b3c53 | 2011-03-22 19:20:54 +0000 | [diff] [blame] | 8758 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 8759 | break; |
| 8760 | } |
| 8761 | } |
| 8762 | |
| 8763 | else /* Should not reach this; colormap already exists and |
| 8764 | must be <= 256 */ |
| 8765 | { |
| 8766 | if (logging != MagickFalse) |
| 8767 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | e9637cb | 2011-03-24 16:34:37 +0000 | [diff] [blame] | 8768 | " Quantizing the colormap to 3-3-3-1"); |
glennrp | 82b3c53 | 2011-03-22 19:20:54 +0000 | [diff] [blame] | 8769 | for (i=0; i<image_colors; i++) |
| 8770 | { |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 8771 | LBR03PacketRGB(image->colormap[i]); |
glennrp | 82b3c53 | 2011-03-22 19:20:54 +0000 | [diff] [blame] | 8772 | } |
glennrp | d337164 | 2011-03-22 19:42:23 +0000 | [diff] [blame] | 8773 | } |
| 8774 | continue; |
glennrp | 82b3c53 | 2011-03-22 19:20:54 +0000 | [diff] [blame] | 8775 | } |
glennrp | c8c2f06 | 2011-02-25 19:00:33 +0000 | [diff] [blame] | 8776 | |
glennrp | 8ca51ad | 2011-05-12 21:22:32 +0000 | [diff] [blame] | 8777 | if (tried_332 == MagickFalse && (image_colors == 0 || image_colors > 256)) |
glennrp | c8c2f06 | 2011-02-25 19:00:33 +0000 | [diff] [blame] | 8778 | { |
| 8779 | if (logging != MagickFalse) |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 8780 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | c8c2f06 | 2011-02-25 19:00:33 +0000 | [diff] [blame] | 8781 | " Quantizing the background color to 3-3-2"); |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 8782 | |
glennrp | 8ca51ad | 2011-05-12 21:22:32 +0000 | [diff] [blame] | 8783 | tried_332 = MagickTrue; |
| 8784 | |
glennrp | 3faa9a3 | 2011-04-23 14:00:25 +0000 | [diff] [blame] | 8785 | /* Red and green were already done so we only quantize the blue |
| 8786 | * channel |
| 8787 | */ |
| 8788 | |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 8789 | LBR02PacketBlue(image->background_color); |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 8790 | |
glennrp | c8c2f06 | 2011-02-25 19:00:33 +0000 | [diff] [blame] | 8791 | if (logging != MagickFalse) |
| 8792 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | e9637cb | 2011-03-24 16:34:37 +0000 | [diff] [blame] | 8793 | " Quantizing the pixel colors to 3-3-2-1"); |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 8794 | |
glennrp | c8c2f06 | 2011-02-25 19:00:33 +0000 | [diff] [blame] | 8795 | if (image->colormap == NULL) |
| 8796 | { |
| 8797 | for (y=0; y < (ssize_t) image->rows; y++) |
| 8798 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8799 | r=GetAuthenticPixels(image,0,y,image->columns,1,exception); |
glennrp | c8c2f06 | 2011-02-25 19:00:33 +0000 | [diff] [blame] | 8800 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8801 | if (r == (Quantum *) NULL) |
glennrp | c8c2f06 | 2011-02-25 19:00:33 +0000 | [diff] [blame] | 8802 | break; |
| 8803 | |
| 8804 | for (x=0; x < (ssize_t) image->columns; x++) |
| 8805 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8806 | if (GetPixelAlpha(image,r) == OpaqueAlpha) |
glennrp | 54cf797 | 2011-08-06 14:28:09 +0000 | [diff] [blame] | 8807 | LBR02PixelBlue(r); |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8808 | r+=GetPixelChannels(image); |
glennrp | c8c2f06 | 2011-02-25 19:00:33 +0000 | [diff] [blame] | 8809 | } |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 8810 | |
glennrp | c8c2f06 | 2011-02-25 19:00:33 +0000 | [diff] [blame] | 8811 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 8812 | break; |
| 8813 | } |
| 8814 | } |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 8815 | |
glennrp | c8c2f06 | 2011-02-25 19:00:33 +0000 | [diff] [blame] | 8816 | else /* Should not reach this; colormap already exists and |
| 8817 | must be <= 256 */ |
| 8818 | { |
| 8819 | if (logging != MagickFalse) |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 8820 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | e9637cb | 2011-03-24 16:34:37 +0000 | [diff] [blame] | 8821 | " Quantizing the colormap to 3-3-2-1"); |
glennrp | c8c2f06 | 2011-02-25 19:00:33 +0000 | [diff] [blame] | 8822 | for (i=0; i<image_colors; i++) |
| 8823 | { |
glennrp | 91d9925 | 2011-06-25 14:30:13 +0000 | [diff] [blame] | 8824 | LBR02PacketBlue(image->colormap[i]); |
glennrp | c8c2f06 | 2011-02-25 19:00:33 +0000 | [diff] [blame] | 8825 | } |
| 8826 | } |
| 8827 | continue; |
| 8828 | } |
| 8829 | break; |
glennrp | 8ca51ad | 2011-05-12 21:22:32 +0000 | [diff] [blame] | 8830 | |
| 8831 | if (image_colors == 0 || image_colors > 256) |
| 8832 | { |
| 8833 | /* Take care of special case with 256 colors + 1 transparent |
| 8834 | * color. We don't need to quantize to 2-3-2-1; we only need to |
| 8835 | * eliminate one color, so we'll merge the two darkest red |
| 8836 | * colors (0x49, 0, 0) -> (0x24, 0, 0). |
| 8837 | */ |
| 8838 | if (ScaleQuantumToChar(image->background_color.red) == 0x49 && |
| 8839 | ScaleQuantumToChar(image->background_color.green) == 0x00 && |
| 8840 | ScaleQuantumToChar(image->background_color.blue) == 0x00) |
| 8841 | { |
| 8842 | image->background_color.red=ScaleCharToQuantum(0x24); |
| 8843 | } |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 8844 | |
glennrp | 8ca51ad | 2011-05-12 21:22:32 +0000 | [diff] [blame] | 8845 | if (image->colormap == NULL) |
| 8846 | { |
| 8847 | for (y=0; y < (ssize_t) image->rows; y++) |
| 8848 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8849 | r=GetAuthenticPixels(image,0,y,image->columns,1,exception); |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 8850 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8851 | if (r == (Quantum *) NULL) |
glennrp | 8ca51ad | 2011-05-12 21:22:32 +0000 | [diff] [blame] | 8852 | break; |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 8853 | |
glennrp | 8ca51ad | 2011-05-12 21:22:32 +0000 | [diff] [blame] | 8854 | for (x=0; x < (ssize_t) image->columns; x++) |
| 8855 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8856 | if (ScaleQuantumToChar(GetPixelRed(image,r)) == 0x49 && |
| 8857 | ScaleQuantumToChar(GetPixelGreen(image,r)) == 0x00 && |
| 8858 | ScaleQuantumToChar(GetPixelBlue(image,r)) == 0x00 && |
| 8859 | GetPixelAlpha(image,r) == OpaqueAlpha) |
glennrp | 8ca51ad | 2011-05-12 21:22:32 +0000 | [diff] [blame] | 8860 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8861 | SetPixelRed(image,ScaleCharToQuantum(0x24),r); |
glennrp | 8ca51ad | 2011-05-12 21:22:32 +0000 | [diff] [blame] | 8862 | } |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8863 | r+=GetPixelChannels(image); |
glennrp | 8ca51ad | 2011-05-12 21:22:32 +0000 | [diff] [blame] | 8864 | } |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 8865 | |
glennrp | 8ca51ad | 2011-05-12 21:22:32 +0000 | [diff] [blame] | 8866 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 8867 | break; |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 8868 | |
glennrp | 8ca51ad | 2011-05-12 21:22:32 +0000 | [diff] [blame] | 8869 | } |
| 8870 | } |
| 8871 | |
| 8872 | else |
| 8873 | { |
| 8874 | for (i=0; i<image_colors; i++) |
| 8875 | { |
| 8876 | if (ScaleQuantumToChar(image->colormap[i].red) == 0x49 && |
| 8877 | ScaleQuantumToChar(image->colormap[i].green) == 0x00 && |
| 8878 | ScaleQuantumToChar(image->colormap[i].blue) == 0x00) |
| 8879 | { |
| 8880 | image->colormap[i].red=ScaleCharToQuantum(0x24); |
| 8881 | } |
| 8882 | } |
| 8883 | } |
| 8884 | } |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 8885 | } |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 8886 | /* END OF BUILD_PALETTE */ |
glennrp | 3c21811 | 2010-11-27 15:31:26 +0000 | [diff] [blame] | 8887 | |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 8888 | /* If we are excluding the tRNS chunk and there is transparency, |
| 8889 | * then we must write a Gray-Alpha (color-type 4) or RGBA (color-type 6) |
| 8890 | * PNG. |
glennrp | 8d57966 | 2011-02-23 02:05:02 +0000 | [diff] [blame] | 8891 | */ |
glennrp | 0e8ea19 | 2010-12-24 18:00:33 +0000 | [diff] [blame] | 8892 | if (mng_info->ping_exclude_tRNS != MagickFalse && |
| 8893 | (number_transparent != 0 || number_semitransparent != 0)) |
| 8894 | { |
glennrp | d17915c | 2011-04-29 14:24:22 +0000 | [diff] [blame] | 8895 | unsigned int colortype=mng_info->write_png_colortype; |
glennrp | 0e8ea19 | 2010-12-24 18:00:33 +0000 | [diff] [blame] | 8896 | |
| 8897 | if (ping_have_color == MagickFalse) |
| 8898 | mng_info->write_png_colortype = 5; |
| 8899 | |
| 8900 | else |
| 8901 | mng_info->write_png_colortype = 7; |
| 8902 | |
glennrp | 8d57966 | 2011-02-23 02:05:02 +0000 | [diff] [blame] | 8903 | if (colortype != 0 && |
glennrp | d17915c | 2011-04-29 14:24:22 +0000 | [diff] [blame] | 8904 | mng_info->write_png_colortype != colortype) |
glennrp | 0e8ea19 | 2010-12-24 18:00:33 +0000 | [diff] [blame] | 8905 | ping_need_colortype_warning=MagickTrue; |
glennrp | 0b206f5 | 2011-01-07 04:55:32 +0000 | [diff] [blame] | 8906 | |
glennrp | 0e8ea19 | 2010-12-24 18:00:33 +0000 | [diff] [blame] | 8907 | } |
| 8908 | |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 8909 | /* See if cheap transparency is possible. It is only possible |
| 8910 | * when there is a single transparent color, no semitransparent |
| 8911 | * color, and no opaque color that has the same RGB components |
glennrp | 5a39f37 | 2011-02-25 04:52:16 +0000 | [diff] [blame] | 8912 | * as the transparent color. We only need this information if |
| 8913 | * we are writing a PNG with colortype 0 or 2, and we have not |
| 8914 | * excluded the tRNS chunk. |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 8915 | */ |
glennrp | 5a39f37 | 2011-02-25 04:52:16 +0000 | [diff] [blame] | 8916 | if (number_transparent == 1 && |
| 8917 | mng_info->write_png_colortype < 4) |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 8918 | { |
| 8919 | ping_have_cheap_transparency = MagickTrue; |
| 8920 | |
| 8921 | if (number_semitransparent != 0) |
| 8922 | ping_have_cheap_transparency = MagickFalse; |
| 8923 | |
| 8924 | else if (image_colors == 0 || image_colors > 256 || |
| 8925 | image->colormap == NULL) |
| 8926 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8927 | register const Quantum |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 8928 | *q; |
| 8929 | |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 8930 | for (y=0; y < (ssize_t) image->rows; y++) |
| 8931 | { |
| 8932 | q=GetVirtualPixels(image,0,y,image->columns,1, exception); |
| 8933 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8934 | if (q == (Quantum *) NULL) |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 8935 | break; |
| 8936 | |
| 8937 | for (x=0; x < (ssize_t) image->columns; x++) |
| 8938 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8939 | if (GetPixelAlpha(image,q) != TransparentAlpha && |
| 8940 | (unsigned short) GetPixelRed(image,q) == |
| 8941 | ping_trans_color.red && |
| 8942 | (unsigned short) GetPixelGreen(image,q) == |
| 8943 | ping_trans_color.green && |
| 8944 | (unsigned short) GetPixelBlue(image,q) == |
| 8945 | ping_trans_color.blue) |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 8946 | { |
| 8947 | ping_have_cheap_transparency = MagickFalse; |
| 8948 | break; |
| 8949 | } |
| 8950 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 8951 | q+=GetPixelChannels(image); |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 8952 | } |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 8953 | |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 8954 | if (ping_have_cheap_transparency == MagickFalse) |
| 8955 | break; |
| 8956 | } |
| 8957 | } |
| 8958 | else |
| 8959 | { |
glennrp | 67b9c1a | 2011-04-22 18:47:36 +0000 | [diff] [blame] | 8960 | /* Assuming that image->colormap[0] is the one transparent color |
| 8961 | * and that all others are opaque. |
| 8962 | */ |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 8963 | if (image_colors > 1) |
glennrp | 67b9c1a | 2011-04-22 18:47:36 +0000 | [diff] [blame] | 8964 | for (i=1; i<image_colors; i++) |
| 8965 | if (image->colormap[i].red == image->colormap[0].red && |
| 8966 | image->colormap[i].green == image->colormap[0].green && |
| 8967 | image->colormap[i].blue == image->colormap[0].blue) |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 8968 | { |
glennrp | 67b9c1a | 2011-04-22 18:47:36 +0000 | [diff] [blame] | 8969 | ping_have_cheap_transparency = MagickFalse; |
| 8970 | break; |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 8971 | } |
| 8972 | } |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 8973 | |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 8974 | if (logging != MagickFalse) |
| 8975 | { |
| 8976 | if (ping_have_cheap_transparency == MagickFalse) |
| 8977 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8978 | " Cheap transparency is not possible."); |
| 8979 | |
| 8980 | else |
| 8981 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 8982 | " Cheap transparency is possible."); |
| 8983 | } |
| 8984 | } |
| 8985 | else |
| 8986 | ping_have_cheap_transparency = MagickFalse; |
| 8987 | |
glennrp | 8640fb5 | 2010-11-23 15:48:26 +0000 | [diff] [blame] | 8988 | image_depth=image->depth; |
| 8989 | |
glennrp | 26c990a | 2010-11-23 02:23:20 +0000 | [diff] [blame] | 8990 | quantum_info = (QuantumInfo *) NULL; |
| 8991 | number_colors=0; |
glennrp | f09bded | 2011-01-08 01:15:59 +0000 | [diff] [blame] | 8992 | image_colors=(int) image->colors; |
glennrp | 26c990a | 2010-11-23 02:23:20 +0000 | [diff] [blame] | 8993 | image_matte=image->matte; |
| 8994 | |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 8995 | mng_info->IsPalette=image->storage_class == PseudoClass && |
glennrp | 1273f7b | 2011-02-24 03:20:30 +0000 | [diff] [blame] | 8996 | image_colors <= 256 && image->colormap != NULL; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 8997 | |
glennrp | 52a479c | 2011-02-26 21:14:38 +0000 | [diff] [blame] | 8998 | if ((mng_info->write_png_colortype == 4 || mng_info->write_png8) && |
| 8999 | (image->colors == 0 || image->colormap == NULL)) |
| 9000 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 9001 | image_info=DestroyImageInfo(image_info); |
| 9002 | image=DestroyImage(image); |
| 9003 | (void) ThrowMagickException(exception,GetMagickModule(),CoderError, |
glennrp | 15e0155 | 2011-03-06 23:29:17 +0000 | [diff] [blame] | 9004 | "Cannot write PNG8 or color-type 3; colormap is NULL", |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 9005 | "`%s'",IMimage->filename); |
glennrp | 52a479c | 2011-02-26 21:14:38 +0000 | [diff] [blame] | 9006 | return(MagickFalse); |
| 9007 | } |
| 9008 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9009 | /* |
| 9010 | Allocate the PNG structures |
| 9011 | */ |
| 9012 | #ifdef PNG_USER_MEM_SUPPORTED |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 9013 | error_info.image=image; |
| 9014 | error_info.exception=exception; |
| 9015 | ping=png_create_write_struct_2(PNG_LIBPNG_VER_STRING,&error_info, |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 9016 | MagickPNGErrorHandler,MagickPNGWarningHandler,(void *) NULL, |
| 9017 | (png_malloc_ptr) Magick_png_malloc,(png_free_ptr) Magick_png_free); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9018 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9019 | #else |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 9020 | ping=png_create_write_struct(PNG_LIBPNG_VER_STRING,&error_info, |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 9021 | MagickPNGErrorHandler,MagickPNGWarningHandler); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9022 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9023 | #endif |
| 9024 | if (ping == (png_struct *) NULL) |
| 9025 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9026 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9027 | ping_info=png_create_info_struct(ping); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9028 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9029 | if (ping_info == (png_info *) NULL) |
| 9030 | { |
| 9031 | png_destroy_write_struct(&ping,(png_info **) NULL); |
| 9032 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
| 9033 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9034 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9035 | png_set_write_fn(ping,image,png_put_data,png_flush_data); |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 9036 | ping_pixels=(unsigned char *) NULL; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9037 | |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 9038 | if (setjmp(png_jmpbuf(ping))) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9039 | { |
| 9040 | /* |
| 9041 | PNG write failed. |
| 9042 | */ |
| 9043 | #ifdef PNG_DEBUG |
| 9044 | if (image_info->verbose) |
| 9045 | (void) printf("PNG write has failed.\n"); |
| 9046 | #endif |
| 9047 | png_destroy_write_struct(&ping,&ping_info); |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 9048 | #ifdef PNG_SETJMP_NOT_THREAD_SAFE |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 9049 | UnlockSemaphoreInfo(ping_semaphore); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9050 | #endif |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 9051 | |
| 9052 | if (ping_pixels != (unsigned char *) NULL) |
| 9053 | ping_pixels=(unsigned char *) RelinquishMagickMemory(ping_pixels); |
| 9054 | |
| 9055 | if (quantum_info != (QuantumInfo *) NULL) |
| 9056 | quantum_info=DestroyQuantumInfo(quantum_info); |
| 9057 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 9058 | if (ping_have_blob != MagickFalse) |
| 9059 | (void) CloseBlob(image); |
| 9060 | image_info=DestroyImageInfo(image_info); |
| 9061 | image=DestroyImage(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9062 | return(MagickFalse); |
| 9063 | } |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 9064 | |
| 9065 | /* { For navigation to end of SETJMP-protected block. Within this |
| 9066 | * block, use png_error() instead of Throwing an Exception, to ensure |
| 9067 | * that libpng is able to clean up, and that the semaphore is unlocked. |
| 9068 | */ |
| 9069 | |
| 9070 | #ifdef PNG_SETJMP_NOT_THREAD_SAFE |
| 9071 | LockSemaphoreInfo(ping_semaphore); |
| 9072 | #endif |
| 9073 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9074 | /* |
| 9075 | Prepare PNG for writing. |
| 9076 | */ |
glennrp | 9bf97b6 | 2012-06-06 21:03:14 +0000 | [diff] [blame] | 9077 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9078 | #if defined(PNG_MNG_FEATURES_SUPPORTED) |
| 9079 | if (mng_info->write_mng) |
glennrp | 25024a6 | 2012-06-07 11:38:34 +0000 | [diff] [blame] | 9080 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9081 | (void) png_permit_mng_features(ping,PNG_ALL_MNG_FEATURES); |
glennrp | 25024a6 | 2012-06-07 11:38:34 +0000 | [diff] [blame] | 9082 | # ifdef PNG_WRITE_CHECK_FOR_INVALID_INDEX_SUPPORTED |
| 9083 | /* Disable new libpng-1.5.10 feature when writing a MNG because |
| 9084 | * zero-length PLTE is OK |
| 9085 | */ |
| 9086 | png_set_check_for_invalid_index (ping, 0); |
| 9087 | # endif |
| 9088 | } |
glennrp | 2b013e4 | 2010-11-24 16:55:50 +0000 | [diff] [blame] | 9089 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9090 | #else |
| 9091 | # ifdef PNG_WRITE_EMPTY_PLTE_SUPPORTED |
| 9092 | if (mng_info->write_mng) |
| 9093 | png_permit_empty_plte(ping,MagickTrue); |
glennrp | 2b013e4 | 2010-11-24 16:55:50 +0000 | [diff] [blame] | 9094 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9095 | # endif |
| 9096 | #endif |
glennrp | 2b013e4 | 2010-11-24 16:55:50 +0000 | [diff] [blame] | 9097 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9098 | x=0; |
glennrp | 2b013e4 | 2010-11-24 16:55:50 +0000 | [diff] [blame] | 9099 | |
cristy | 4e5bc84 | 2010-06-09 13:56:01 +0000 | [diff] [blame] | 9100 | ping_width=(png_uint_32) image->columns; |
| 9101 | ping_height=(png_uint_32) image->rows; |
glennrp | 2b013e4 | 2010-11-24 16:55:50 +0000 | [diff] [blame] | 9102 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9103 | if (mng_info->write_png8 || mng_info->write_png24 || mng_info->write_png32) |
| 9104 | image_depth=8; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9105 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9106 | if (mng_info->write_png_depth != 0) |
| 9107 | image_depth=mng_info->write_png_depth; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9108 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9109 | /* Adjust requested depth to next higher valid depth if necessary */ |
| 9110 | if (image_depth > 8) |
| 9111 | image_depth=16; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9112 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9113 | if ((image_depth > 4) && (image_depth < 8)) |
| 9114 | image_depth=8; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9115 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9116 | if (image_depth == 3) |
| 9117 | image_depth=4; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9118 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9119 | if (logging != MagickFalse) |
| 9120 | { |
| 9121 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 9122 | " width=%.20g",(double) ping_width); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9123 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 9124 | " height=%.20g",(double) ping_height); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9125 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 9126 | " image_matte=%.20g",(double) image->matte); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9127 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | 8640fb5 | 2010-11-23 15:48:26 +0000 | [diff] [blame] | 9128 | " image->depth=%.20g",(double) image->depth); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9129 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | 8640fb5 | 2010-11-23 15:48:26 +0000 | [diff] [blame] | 9130 | " Tentative ping_bit_depth=%.20g",(double) image_depth); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9131 | } |
glennrp | 8640fb5 | 2010-11-23 15:48:26 +0000 | [diff] [blame] | 9132 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9133 | save_image_depth=image_depth; |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 9134 | ping_bit_depth=(png_byte) save_image_depth; |
glennrp | dfd7080 | 2010-11-14 01:23:35 +0000 | [diff] [blame] | 9135 | |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 9136 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9137 | #if defined(PNG_pHYs_SUPPORTED) |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 9138 | if (ping_exclude_pHYs == MagickFalse) |
| 9139 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 9140 | if ((image->resolution.x != 0) && (image->resolution.y != 0) && |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9141 | (!mng_info->write_mng || !mng_info->equal_physs)) |
| 9142 | { |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9143 | if (logging != MagickFalse) |
glennrp | dfd7080 | 2010-11-14 01:23:35 +0000 | [diff] [blame] | 9144 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9145 | " Setting up pHYs chunk"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9146 | |
| 9147 | if (image->units == PixelsPerInchResolution) |
| 9148 | { |
glennrp | dfd7080 | 2010-11-14 01:23:35 +0000 | [diff] [blame] | 9149 | ping_pHYs_unit_type=PNG_RESOLUTION_METER; |
glennrp | 823b55c | 2011-03-14 18:46:46 +0000 | [diff] [blame] | 9150 | ping_pHYs_x_resolution= |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 9151 | (png_uint_32) ((100.0*image->resolution.x+0.5)/2.54); |
glennrp | 823b55c | 2011-03-14 18:46:46 +0000 | [diff] [blame] | 9152 | ping_pHYs_y_resolution= |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 9153 | (png_uint_32) ((100.0*image->resolution.y+0.5)/2.54); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9154 | } |
glennrp | dfd7080 | 2010-11-14 01:23:35 +0000 | [diff] [blame] | 9155 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9156 | else if (image->units == PixelsPerCentimeterResolution) |
| 9157 | { |
glennrp | dfd7080 | 2010-11-14 01:23:35 +0000 | [diff] [blame] | 9158 | ping_pHYs_unit_type=PNG_RESOLUTION_METER; |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 9159 | ping_pHYs_x_resolution=(png_uint_32) (100.0*image->resolution.x+0.5); |
| 9160 | 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] | 9161 | } |
glennrp | 991d11d | 2010-11-12 21:55:28 +0000 | [diff] [blame] | 9162 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9163 | else |
| 9164 | { |
glennrp | dfd7080 | 2010-11-14 01:23:35 +0000 | [diff] [blame] | 9165 | ping_pHYs_unit_type=PNG_RESOLUTION_UNKNOWN; |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 9166 | ping_pHYs_x_resolution=(png_uint_32) image->resolution.x; |
| 9167 | ping_pHYs_y_resolution=(png_uint_32) image->resolution.y; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9168 | } |
glennrp | 991d11d | 2010-11-12 21:55:28 +0000 | [diff] [blame] | 9169 | |
glennrp | 823b55c | 2011-03-14 18:46:46 +0000 | [diff] [blame] | 9170 | if (logging != MagickFalse) |
| 9171 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9172 | " Set up PNG pHYs chunk: xres: %.20g, yres: %.20g, units: %d.", |
| 9173 | (double) ping_pHYs_x_resolution,(double) ping_pHYs_y_resolution, |
| 9174 | (int) ping_pHYs_unit_type); |
glennrp | 991d11d | 2010-11-12 21:55:28 +0000 | [diff] [blame] | 9175 | ping_have_pHYs = MagickTrue; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9176 | } |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 9177 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9178 | #endif |
glennrp | a521b2f | 2010-10-29 04:11:03 +0000 | [diff] [blame] | 9179 | |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 9180 | if (ping_exclude_bKGD == MagickFalse) |
| 9181 | { |
glennrp | a521b2f | 2010-10-29 04:11:03 +0000 | [diff] [blame] | 9182 | if ((!mng_info->adjoin || !mng_info->equal_backgrounds)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9183 | { |
glennrp | a521b2f | 2010-10-29 04:11:03 +0000 | [diff] [blame] | 9184 | unsigned int |
| 9185 | mask; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9186 | |
glennrp | a521b2f | 2010-10-29 04:11:03 +0000 | [diff] [blame] | 9187 | mask=0xffff; |
| 9188 | if (ping_bit_depth == 8) |
| 9189 | mask=0x00ff; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9190 | |
glennrp | a521b2f | 2010-10-29 04:11:03 +0000 | [diff] [blame] | 9191 | if (ping_bit_depth == 4) |
| 9192 | mask=0x000f; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9193 | |
glennrp | a521b2f | 2010-10-29 04:11:03 +0000 | [diff] [blame] | 9194 | if (ping_bit_depth == 2) |
| 9195 | mask=0x0003; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9196 | |
glennrp | a521b2f | 2010-10-29 04:11:03 +0000 | [diff] [blame] | 9197 | if (ping_bit_depth == 1) |
| 9198 | mask=0x0001; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9199 | |
glennrp | a521b2f | 2010-10-29 04:11:03 +0000 | [diff] [blame] | 9200 | ping_background.red=(png_uint_16) |
| 9201 | (ScaleQuantumToShort(image->background_color.red) & mask); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9202 | |
glennrp | a521b2f | 2010-10-29 04:11:03 +0000 | [diff] [blame] | 9203 | ping_background.green=(png_uint_16) |
| 9204 | (ScaleQuantumToShort(image->background_color.green) & mask); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9205 | |
glennrp | a521b2f | 2010-10-29 04:11:03 +0000 | [diff] [blame] | 9206 | ping_background.blue=(png_uint_16) |
| 9207 | (ScaleQuantumToShort(image->background_color.blue) & mask); |
glennrp | c6c391a | 2011-04-27 02:23:56 +0000 | [diff] [blame] | 9208 | |
| 9209 | ping_background.gray=(png_uint_16) ping_background.green; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9210 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9211 | |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9212 | if (logging != MagickFalse) |
glennrp | 3b51f0e | 2010-11-27 18:14:08 +0000 | [diff] [blame] | 9213 | { |
| 9214 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9215 | " Setting up bKGD chunk (1)"); |
glennrp | c6c391a | 2011-04-27 02:23:56 +0000 | [diff] [blame] | 9216 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9217 | " background_color index is %d", |
| 9218 | (int) ping_background.index); |
glennrp | 3b51f0e | 2010-11-27 18:14:08 +0000 | [diff] [blame] | 9219 | |
| 9220 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9221 | " ping_bit_depth=%d",ping_bit_depth); |
| 9222 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9223 | |
| 9224 | ping_have_bKGD = MagickTrue; |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 9225 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9226 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9227 | /* |
| 9228 | Select the color type. |
| 9229 | */ |
| 9230 | matte=image_matte; |
| 9231 | old_bit_depth=0; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9232 | |
glennrp | 1273f7b | 2011-02-24 03:20:30 +0000 | [diff] [blame] | 9233 | if (mng_info->IsPalette && mng_info->write_png8) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9234 | { |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9235 | |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 9236 | /* To do: make this a function cause it's used twice, except |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9237 | for reducing the sample depth from 8. */ |
| 9238 | |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9239 | number_colors=image_colors; |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 9240 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 9241 | ping_have_tRNS=MagickFalse; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9242 | |
| 9243 | /* |
| 9244 | Set image palette. |
| 9245 | */ |
| 9246 | ping_color_type=(png_byte) PNG_COLOR_TYPE_PALETTE; |
| 9247 | |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9248 | if (logging != MagickFalse) |
| 9249 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9250 | " Setting up PLTE chunk with %d colors (%d)", |
glennrp | f09bded | 2011-01-08 01:15:59 +0000 | [diff] [blame] | 9251 | number_colors, image_colors); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9252 | |
| 9253 | for (i=0; i < (ssize_t) number_colors; i++) |
| 9254 | { |
| 9255 | palette[i].red=ScaleQuantumToChar(image->colormap[i].red); |
| 9256 | palette[i].green=ScaleQuantumToChar(image->colormap[i].green); |
| 9257 | palette[i].blue=ScaleQuantumToChar(image->colormap[i].blue); |
| 9258 | if (logging != MagickFalse) |
| 9259 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | 67b9c1a | 2011-04-22 18:47:36 +0000 | [diff] [blame] | 9260 | #if MAGICKCORE_QUANTUM_DEPTH == 8 |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9261 | " %3ld (%3d,%3d,%3d)", |
glennrp | 67b9c1a | 2011-04-22 18:47:36 +0000 | [diff] [blame] | 9262 | #else |
| 9263 | " %5ld (%5d,%5d,%5d)", |
| 9264 | #endif |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9265 | (long) i,palette[i].red,palette[i].green,palette[i].blue); |
| 9266 | |
| 9267 | } |
glennrp | 2b013e4 | 2010-11-24 16:55:50 +0000 | [diff] [blame] | 9268 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 9269 | ping_have_PLTE=MagickTrue; |
| 9270 | image_depth=ping_bit_depth; |
| 9271 | ping_num_trans=0; |
glennrp | 2b013e4 | 2010-11-24 16:55:50 +0000 | [diff] [blame] | 9272 | |
glennrp | 58e0176 | 2011-01-07 15:28:54 +0000 | [diff] [blame] | 9273 | if (matte != MagickFalse) |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 9274 | { |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9275 | /* |
| 9276 | Identify which colormap entry is transparent. |
| 9277 | */ |
| 9278 | assert(number_colors <= 256); |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 9279 | assert(image->colormap != NULL); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9280 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 9281 | for (i=0; i < (ssize_t) number_transparent; i++) |
| 9282 | ping_trans_alpha[i]=0; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9283 | |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9284 | |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 9285 | ping_num_trans=(unsigned short) (number_transparent + |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 9286 | number_semitransparent); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9287 | |
| 9288 | if (ping_num_trans == 0) |
| 9289 | ping_have_tRNS=MagickFalse; |
| 9290 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 9291 | else |
| 9292 | ping_have_tRNS=MagickTrue; |
| 9293 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9294 | |
glennrp | 1273f7b | 2011-02-24 03:20:30 +0000 | [diff] [blame] | 9295 | if (ping_exclude_bKGD == MagickFalse) |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 9296 | { |
glennrp | 1273f7b | 2011-02-24 03:20:30 +0000 | [diff] [blame] | 9297 | /* |
| 9298 | * Identify which colormap entry is the background color. |
| 9299 | */ |
| 9300 | |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 9301 | for (i=0; i < (ssize_t) MagickMax(1L*number_colors-1L,1L); i++) |
| 9302 | if (IsPNGColorEqual(ping_background,image->colormap[i])) |
| 9303 | break; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9304 | |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 9305 | ping_background.index=(png_byte) i; |
glennrp | c6c391a | 2011-04-27 02:23:56 +0000 | [diff] [blame] | 9306 | |
| 9307 | if (logging != MagickFalse) |
| 9308 | { |
| 9309 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9310 | " background_color index is %d", |
| 9311 | (int) ping_background.index); |
| 9312 | } |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 9313 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9314 | } /* end of write_png8 */ |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9315 | |
glennrp | 7e65e93 | 2011-08-19 02:31:16 +0000 | [diff] [blame] | 9316 | else if (mng_info->write_png24 || mng_info->write_png_colortype == 3) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9317 | { |
| 9318 | image_matte=MagickFalse; |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 9319 | ping_color_type=(png_byte) PNG_COLOR_TYPE_RGB; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9320 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9321 | |
glennrp | 7e65e93 | 2011-08-19 02:31:16 +0000 | [diff] [blame] | 9322 | else if (mng_info->write_png32 || mng_info->write_png_colortype == 7) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9323 | { |
| 9324 | image_matte=MagickTrue; |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 9325 | ping_color_type=(png_byte) PNG_COLOR_TYPE_RGB_ALPHA; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9326 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9327 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 9328 | else /* mng_info->write_pngNN not specified */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9329 | { |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 9330 | image_depth=ping_bit_depth; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9331 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 9332 | if (mng_info->write_png_colortype != 0) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9333 | { |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 9334 | ping_color_type=(png_byte) mng_info->write_png_colortype-1; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9335 | |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 9336 | if (ping_color_type == PNG_COLOR_TYPE_GRAY_ALPHA || |
| 9337 | ping_color_type == PNG_COLOR_TYPE_RGB_ALPHA) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9338 | image_matte=MagickTrue; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 9339 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 9340 | else |
| 9341 | image_matte=MagickFalse; |
glennrp | 7c4c9e6 | 2011-03-21 20:23:32 +0000 | [diff] [blame] | 9342 | |
| 9343 | if (logging != MagickFalse) |
| 9344 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9345 | " PNG colortype %d was specified:",(int) ping_color_type); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9346 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9347 | |
glennrp | 7c4c9e6 | 2011-03-21 20:23:32 +0000 | [diff] [blame] | 9348 | else /* write_png_colortype not specified */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9349 | { |
| 9350 | if (logging != MagickFalse) |
| 9351 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | 3c21811 | 2010-11-27 15:31:26 +0000 | [diff] [blame] | 9352 | " Selecting PNG colortype:"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9353 | |
glennrp | d6bf161 | 2010-12-17 17:28:54 +0000 | [diff] [blame] | 9354 | ping_color_type=(png_byte) ((matte != MagickFalse)? |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 9355 | PNG_COLOR_TYPE_RGB_ALPHA:PNG_COLOR_TYPE_RGB); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9356 | |
glennrp | d6bf161 | 2010-12-17 17:28:54 +0000 | [diff] [blame] | 9357 | if (image_info->type == TrueColorType) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9358 | { |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 9359 | ping_color_type=(png_byte) PNG_COLOR_TYPE_RGB; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9360 | image_matte=MagickFalse; |
| 9361 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9362 | |
glennrp | d6bf161 | 2010-12-17 17:28:54 +0000 | [diff] [blame] | 9363 | if (image_info->type == TrueColorMatteType) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9364 | { |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 9365 | ping_color_type=(png_byte) PNG_COLOR_TYPE_RGB_ALPHA; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9366 | image_matte=MagickTrue; |
| 9367 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9368 | |
glennrp | 5aa37f6 | 2011-01-02 03:07:57 +0000 | [diff] [blame] | 9369 | if (image_info->type == PaletteType || |
| 9370 | image_info->type == PaletteMatteType) |
| 9371 | ping_color_type=(png_byte) PNG_COLOR_TYPE_PALETTE; |
| 9372 | |
glennrp | 7c4c9e6 | 2011-03-21 20:23:32 +0000 | [diff] [blame] | 9373 | if (mng_info->write_png_colortype == 0 && |
| 9374 | (image_info->type == UndefinedType || |
| 9375 | image_info->type == OptimizeType)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9376 | { |
glennrp | 5aa37f6 | 2011-01-02 03:07:57 +0000 | [diff] [blame] | 9377 | if (ping_have_color == MagickFalse) |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 9378 | { |
glennrp | 5aa37f6 | 2011-01-02 03:07:57 +0000 | [diff] [blame] | 9379 | if (image_matte == MagickFalse) |
| 9380 | { |
| 9381 | ping_color_type=(png_byte) PNG_COLOR_TYPE_GRAY; |
| 9382 | image_matte=MagickFalse; |
| 9383 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9384 | |
glennrp | 0b206f5 | 2011-01-07 04:55:32 +0000 | [diff] [blame] | 9385 | else |
glennrp | 5aa37f6 | 2011-01-02 03:07:57 +0000 | [diff] [blame] | 9386 | { |
| 9387 | ping_color_type=(png_byte) PNG_COLOR_TYPE_GRAY_ALPHA; |
| 9388 | image_matte=MagickTrue; |
| 9389 | } |
| 9390 | } |
| 9391 | else |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 9392 | { |
glennrp | 5aa37f6 | 2011-01-02 03:07:57 +0000 | [diff] [blame] | 9393 | if (image_matte == MagickFalse) |
| 9394 | { |
| 9395 | ping_color_type=(png_byte) PNG_COLOR_TYPE_RGB; |
| 9396 | image_matte=MagickFalse; |
| 9397 | } |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 9398 | |
glennrp | 0b206f5 | 2011-01-07 04:55:32 +0000 | [diff] [blame] | 9399 | else |
glennrp | 5aa37f6 | 2011-01-02 03:07:57 +0000 | [diff] [blame] | 9400 | { |
| 9401 | ping_color_type=(png_byte) PNG_COLOR_TYPE_RGBA; |
| 9402 | image_matte=MagickTrue; |
| 9403 | } |
| 9404 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9405 | } |
glennrp | 5aa37f6 | 2011-01-02 03:07:57 +0000 | [diff] [blame] | 9406 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9407 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9408 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9409 | if (logging != MagickFalse) |
| 9410 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | 8640fb5 | 2010-11-23 15:48:26 +0000 | [diff] [blame] | 9411 | " Selected PNG colortype=%d",ping_color_type); |
glennrp | 26c990a | 2010-11-23 02:23:20 +0000 | [diff] [blame] | 9412 | |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 9413 | if (ping_bit_depth < 8) |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9414 | { |
| 9415 | if (ping_color_type == PNG_COLOR_TYPE_GRAY_ALPHA || |
| 9416 | ping_color_type == PNG_COLOR_TYPE_RGB || |
| 9417 | ping_color_type == PNG_COLOR_TYPE_RGB_ALPHA) |
| 9418 | ping_bit_depth=8; |
| 9419 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9420 | |
glennrp | d6bf161 | 2010-12-17 17:28:54 +0000 | [diff] [blame] | 9421 | old_bit_depth=ping_bit_depth; |
| 9422 | |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 9423 | if (ping_color_type == PNG_COLOR_TYPE_GRAY) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9424 | { |
glennrp | 8d57966 | 2011-02-23 02:05:02 +0000 | [diff] [blame] | 9425 | if (image->matte == MagickFalse && ping_have_non_bw == MagickFalse) |
| 9426 | ping_bit_depth=1; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9427 | } |
glennrp | 8640fb5 | 2010-11-23 15:48:26 +0000 | [diff] [blame] | 9428 | |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 9429 | if (ping_color_type == PNG_COLOR_TYPE_PALETTE) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9430 | { |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 9431 | size_t one = 1; |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 9432 | ping_bit_depth=1; |
glennrp | 0f11198 | 2010-07-07 20:18:33 +0000 | [diff] [blame] | 9433 | |
| 9434 | if (image->colors == 0) |
| 9435 | { |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9436 | /* DO SOMETHING */ |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 9437 | png_error(ping,"image has 0 colors"); |
glennrp | 0f11198 | 2010-07-07 20:18:33 +0000 | [diff] [blame] | 9438 | } |
| 9439 | |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 9440 | while ((int) (one << ping_bit_depth) < (ssize_t) image_colors) |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 9441 | ping_bit_depth <<= 1; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9442 | } |
glennrp | 2b013e4 | 2010-11-24 16:55:50 +0000 | [diff] [blame] | 9443 | |
glennrp | d6bf161 | 2010-12-17 17:28:54 +0000 | [diff] [blame] | 9444 | if (logging != MagickFalse) |
| 9445 | { |
| 9446 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9447 | " Number of colors: %.20g",(double) image_colors); |
| 9448 | |
| 9449 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9450 | " Tentative PNG bit depth: %d",ping_bit_depth); |
| 9451 | } |
| 9452 | |
| 9453 | if (ping_bit_depth < (int) mng_info->write_png_depth) |
| 9454 | ping_bit_depth = mng_info->write_png_depth; |
| 9455 | } |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 9456 | |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 9457 | image_depth=ping_bit_depth; |
glennrp | 2b013e4 | 2010-11-24 16:55:50 +0000 | [diff] [blame] | 9458 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9459 | if (logging != MagickFalse) |
| 9460 | { |
| 9461 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | 1a56e9c | 2012-04-25 17:06:57 +0000 | [diff] [blame] | 9462 | " Tentative PNG color type: %s (%.20g)", |
| 9463 | PngColorTypeToString(ping_color_type), |
| 9464 | (double) ping_color_type); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9465 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9466 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 9467 | " image_info->type: %.20g",(double) image_info->type); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9468 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9469 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 9470 | " image_depth: %.20g",(double) image_depth); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9471 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9472 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | 3c21811 | 2010-11-27 15:31:26 +0000 | [diff] [blame] | 9473 | |
glennrp | 8640fb5 | 2010-11-23 15:48:26 +0000 | [diff] [blame] | 9474 | " image->depth: %.20g",(double) image->depth); |
| 9475 | |
| 9476 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 9477 | " ping_bit_depth: %.20g",(double) ping_bit_depth); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9478 | } |
| 9479 | |
glennrp | 58e0176 | 2011-01-07 15:28:54 +0000 | [diff] [blame] | 9480 | if (matte != MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9481 | { |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 9482 | if (mng_info->IsPalette) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9483 | { |
glennrp | 7c4c9e6 | 2011-03-21 20:23:32 +0000 | [diff] [blame] | 9484 | if (mng_info->write_png_colortype == 0) |
| 9485 | { |
| 9486 | ping_color_type=PNG_COLOR_TYPE_GRAY_ALPHA; |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 9487 | |
glennrp | 7c4c9e6 | 2011-03-21 20:23:32 +0000 | [diff] [blame] | 9488 | if (ping_have_color != MagickFalse) |
| 9489 | ping_color_type=PNG_COLOR_TYPE_RGBA; |
| 9490 | } |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 9491 | |
| 9492 | /* |
| 9493 | * Determine if there is any transparent color. |
| 9494 | */ |
| 9495 | if (number_transparent + number_semitransparent == 0) |
| 9496 | { |
| 9497 | /* |
| 9498 | No transparent pixels are present. Change 4 or 6 to 0 or 2. |
| 9499 | */ |
glennrp | a6a0663 | 2011-01-19 15:15:34 +0000 | [diff] [blame] | 9500 | |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 9501 | image_matte=MagickFalse; |
glennrp | 7c4c9e6 | 2011-03-21 20:23:32 +0000 | [diff] [blame] | 9502 | |
| 9503 | if (mng_info->write_png_colortype == 0) |
| 9504 | ping_color_type&=0x03; |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 9505 | } |
| 9506 | |
| 9507 | else |
| 9508 | { |
| 9509 | unsigned int |
glennrp | bb4f99d | 2011-05-22 11:13:17 +0000 | [diff] [blame] | 9510 | mask; |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 9511 | |
| 9512 | mask=0xffff; |
| 9513 | |
| 9514 | if (ping_bit_depth == 8) |
| 9515 | mask=0x00ff; |
| 9516 | |
| 9517 | if (ping_bit_depth == 4) |
| 9518 | mask=0x000f; |
| 9519 | |
| 9520 | if (ping_bit_depth == 2) |
| 9521 | mask=0x0003; |
| 9522 | |
| 9523 | if (ping_bit_depth == 1) |
| 9524 | mask=0x0001; |
| 9525 | |
| 9526 | ping_trans_color.red=(png_uint_16) |
| 9527 | (ScaleQuantumToShort(image->colormap[0].red) & mask); |
| 9528 | |
| 9529 | ping_trans_color.green=(png_uint_16) |
| 9530 | (ScaleQuantumToShort(image->colormap[0].green) & mask); |
| 9531 | |
| 9532 | ping_trans_color.blue=(png_uint_16) |
| 9533 | (ScaleQuantumToShort(image->colormap[0].blue) & mask); |
| 9534 | |
| 9535 | ping_trans_color.gray=(png_uint_16) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 9536 | (ScaleQuantumToShort(GetPixelInfoIntensity( |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 9537 | image->colormap)) & mask); |
| 9538 | |
| 9539 | ping_trans_color.index=(png_byte) 0; |
| 9540 | |
| 9541 | ping_have_tRNS=MagickTrue; |
| 9542 | } |
| 9543 | |
| 9544 | if (ping_have_tRNS != MagickFalse) |
| 9545 | { |
| 9546 | /* |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 9547 | * Determine if there is one and only one transparent color |
| 9548 | * and if so if it is fully transparent. |
| 9549 | */ |
| 9550 | if (ping_have_cheap_transparency == MagickFalse) |
| 9551 | ping_have_tRNS=MagickFalse; |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 9552 | } |
| 9553 | |
| 9554 | if (ping_have_tRNS != MagickFalse) |
| 9555 | { |
glennrp | 7c4c9e6 | 2011-03-21 20:23:32 +0000 | [diff] [blame] | 9556 | if (mng_info->write_png_colortype == 0) |
| 9557 | ping_color_type &= 0x03; /* changes 4 or 6 to 0 or 2 */ |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 9558 | |
| 9559 | if (image_depth == 8) |
| 9560 | { |
| 9561 | ping_trans_color.red&=0xff; |
| 9562 | ping_trans_color.green&=0xff; |
| 9563 | ping_trans_color.blue&=0xff; |
| 9564 | ping_trans_color.gray&=0xff; |
| 9565 | } |
| 9566 | } |
| 9567 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9568 | else |
| 9569 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9570 | if (image_depth == 8) |
| 9571 | { |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 9572 | ping_trans_color.red&=0xff; |
| 9573 | ping_trans_color.green&=0xff; |
| 9574 | ping_trans_color.blue&=0xff; |
| 9575 | ping_trans_color.gray&=0xff; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9576 | } |
| 9577 | } |
| 9578 | } |
glennrp | 8640fb5 | 2010-11-23 15:48:26 +0000 | [diff] [blame] | 9579 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9580 | matte=image_matte; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9581 | |
glennrp | 2e09f55 | 2010-11-14 00:38:48 +0000 | [diff] [blame] | 9582 | if (ping_have_tRNS != MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9583 | image_matte=MagickFalse; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9584 | |
glennrp | 39992b4 | 2010-11-14 00:03:43 +0000 | [diff] [blame] | 9585 | if ((mng_info->IsPalette) && |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9586 | mng_info->write_png_colortype-1 != PNG_COLOR_TYPE_PALETTE && |
glennrp | 8d57966 | 2011-02-23 02:05:02 +0000 | [diff] [blame] | 9587 | ping_have_color == MagickFalse && |
| 9588 | (image_matte == MagickFalse || image_depth >= 8)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9589 | { |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 9590 | size_t one=1; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9591 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9592 | if (image_matte != MagickFalse) |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 9593 | ping_color_type=PNG_COLOR_TYPE_GRAY_ALPHA; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9594 | |
glennrp | 7c4c9e6 | 2011-03-21 20:23:32 +0000 | [diff] [blame] | 9595 | else if (mng_info->write_png_colortype-1 != PNG_COLOR_TYPE_GRAY_ALPHA) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9596 | { |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 9597 | ping_color_type=PNG_COLOR_TYPE_GRAY; |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 9598 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9599 | if (save_image_depth == 16 && image_depth == 8) |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 9600 | { |
| 9601 | if (logging != MagickFalse) |
| 9602 | { |
| 9603 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9604 | " Scaling ping_trans_color (0)"); |
| 9605 | } |
| 9606 | ping_trans_color.gray*=0x0101; |
| 9607 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9608 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9609 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9610 | if (image_depth > MAGICKCORE_QUANTUM_DEPTH) |
| 9611 | image_depth=MAGICKCORE_QUANTUM_DEPTH; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9612 | |
glennrp | 136ee3a | 2011-04-27 15:47:45 +0000 | [diff] [blame] | 9613 | if ((image_colors == 0) || |
glennrp | d17915c | 2011-04-29 14:24:22 +0000 | [diff] [blame] | 9614 | ((ssize_t) (image_colors-1) > (ssize_t) MaxColormapSize)) |
glennrp | f09bded | 2011-01-08 01:15:59 +0000 | [diff] [blame] | 9615 | image_colors=(int) (one << image_depth); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9616 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9617 | if (image_depth > 8) |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 9618 | ping_bit_depth=16; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9619 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9620 | else |
| 9621 | { |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 9622 | ping_bit_depth=8; |
| 9623 | if ((int) ping_color_type == PNG_COLOR_TYPE_PALETTE) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9624 | { |
| 9625 | if(!mng_info->write_png_depth) |
| 9626 | { |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 9627 | ping_bit_depth=1; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9628 | |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 9629 | while ((int) (one << ping_bit_depth) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 9630 | < (ssize_t) image_colors) |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 9631 | ping_bit_depth <<= 1; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9632 | } |
| 9633 | } |
glennrp | 2b013e4 | 2010-11-24 16:55:50 +0000 | [diff] [blame] | 9634 | |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9635 | else if (ping_color_type == |
| 9636 | PNG_COLOR_TYPE_GRAY && image_colors < 17 && |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9637 | mng_info->IsPalette) |
| 9638 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9639 | /* Check if grayscale is reducible */ |
glennrp | 1a0aaa6 | 2011-03-07 17:40:17 +0000 | [diff] [blame] | 9640 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9641 | int |
| 9642 | depth_4_ok=MagickTrue, |
| 9643 | depth_2_ok=MagickTrue, |
| 9644 | depth_1_ok=MagickTrue; |
| 9645 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 9646 | for (i=0; i < (ssize_t) image_colors; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9647 | { |
| 9648 | unsigned char |
| 9649 | intensity; |
| 9650 | |
| 9651 | intensity=ScaleQuantumToChar(image->colormap[i].red); |
| 9652 | |
| 9653 | if ((intensity & 0x0f) != ((intensity & 0xf0) >> 4)) |
| 9654 | depth_4_ok=depth_2_ok=depth_1_ok=MagickFalse; |
| 9655 | else if ((intensity & 0x03) != ((intensity & 0x0c) >> 2)) |
| 9656 | depth_2_ok=depth_1_ok=MagickFalse; |
glennrp | 4bf8973 | 2011-03-21 13:48:28 +0000 | [diff] [blame] | 9657 | else if ((intensity & 0x01) != ((intensity & 0x02) >> 1)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9658 | depth_1_ok=MagickFalse; |
| 9659 | } |
glennrp | 2b013e4 | 2010-11-24 16:55:50 +0000 | [diff] [blame] | 9660 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9661 | if (depth_1_ok && mng_info->write_png_depth <= 1) |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 9662 | ping_bit_depth=1; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9663 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9664 | else if (depth_2_ok && mng_info->write_png_depth <= 2) |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 9665 | ping_bit_depth=2; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9666 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9667 | else if (depth_4_ok && mng_info->write_png_depth <= 4) |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 9668 | ping_bit_depth=4; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9669 | } |
| 9670 | } |
glennrp | 2b013e4 | 2010-11-24 16:55:50 +0000 | [diff] [blame] | 9671 | |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 9672 | image_depth=ping_bit_depth; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9673 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9674 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9675 | else |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9676 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9677 | if (mng_info->IsPalette) |
| 9678 | { |
glennrp | 17a1485 | 2010-05-10 03:01:59 +0000 | [diff] [blame] | 9679 | number_colors=image_colors; |
| 9680 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9681 | if (image_depth <= 8) |
| 9682 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9683 | /* |
| 9684 | Set image palette. |
| 9685 | */ |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 9686 | ping_color_type=(png_byte) PNG_COLOR_TYPE_PALETTE; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9687 | |
glennrp | 58e0176 | 2011-01-07 15:28:54 +0000 | [diff] [blame] | 9688 | if (mng_info->have_write_global_plte && matte == MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9689 | { |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 9690 | png_set_PLTE(ping,ping_info,NULL,0); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9691 | |
glennrp | 3b51f0e | 2010-11-27 18:14:08 +0000 | [diff] [blame] | 9692 | if (logging != MagickFalse) |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 9693 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9694 | " Setting up empty PLTE chunk"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9695 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9696 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9697 | else |
| 9698 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 9699 | for (i=0; i < (ssize_t) number_colors; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9700 | { |
| 9701 | palette[i].red=ScaleQuantumToChar(image->colormap[i].red); |
| 9702 | palette[i].green=ScaleQuantumToChar(image->colormap[i].green); |
| 9703 | palette[i].blue=ScaleQuantumToChar(image->colormap[i].blue); |
| 9704 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9705 | |
glennrp | 3b51f0e | 2010-11-27 18:14:08 +0000 | [diff] [blame] | 9706 | if (logging != MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9707 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | 98156a3 | 2009-12-09 15:32:44 +0000 | [diff] [blame] | 9708 | " Setting up PLTE chunk with %d colors", |
glennrp | f09bded | 2011-01-08 01:15:59 +0000 | [diff] [blame] | 9709 | number_colors); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9710 | |
glennrp | 39992b4 | 2010-11-14 00:03:43 +0000 | [diff] [blame] | 9711 | ping_have_PLTE=MagickTrue; |
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 | /* color_type is PNG_COLOR_TYPE_PALETTE */ |
glennrp | d6bf161 | 2010-12-17 17:28:54 +0000 | [diff] [blame] | 9715 | if (mng_info->write_png_depth == 0) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9716 | { |
cristy | befe4d2 | 2010-06-07 01:18:58 +0000 | [diff] [blame] | 9717 | size_t |
| 9718 | one; |
| 9719 | |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 9720 | ping_bit_depth=1; |
cristy | befe4d2 | 2010-06-07 01:18:58 +0000 | [diff] [blame] | 9721 | one=1; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9722 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 9723 | while ((one << ping_bit_depth) < (size_t) number_colors) |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 9724 | ping_bit_depth <<= 1; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9725 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9726 | |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 9727 | ping_num_trans=0; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9728 | |
glennrp | 58e0176 | 2011-01-07 15:28:54 +0000 | [diff] [blame] | 9729 | if (matte != MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9730 | { |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9731 | /* |
glennrp | d6bf161 | 2010-12-17 17:28:54 +0000 | [diff] [blame] | 9732 | * Set up trans_colors array. |
| 9733 | */ |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9734 | assert(number_colors <= 256); |
| 9735 | |
glennrp | d6bf161 | 2010-12-17 17:28:54 +0000 | [diff] [blame] | 9736 | ping_num_trans=(unsigned short) (number_transparent + |
| 9737 | number_semitransparent); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9738 | |
| 9739 | if (ping_num_trans == 0) |
| 9740 | ping_have_tRNS=MagickFalse; |
| 9741 | |
glennrp | d6bf161 | 2010-12-17 17:28:54 +0000 | [diff] [blame] | 9742 | else |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9743 | { |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 9744 | if (logging != MagickFalse) |
| 9745 | { |
| 9746 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9747 | " Scaling ping_trans_color (1)"); |
| 9748 | } |
glennrp | d6bf161 | 2010-12-17 17:28:54 +0000 | [diff] [blame] | 9749 | ping_have_tRNS=MagickTrue; |
| 9750 | |
| 9751 | for (i=0; i < ping_num_trans; i++) |
| 9752 | { |
glennrp | 750105b | 2012-04-25 16:20:45 +0000 | [diff] [blame] | 9753 | ping_trans_alpha[i]= (png_byte) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 9754 | ScaleQuantumToChar(image->colormap[i].alpha); |
glennrp | d6bf161 | 2010-12-17 17:28:54 +0000 | [diff] [blame] | 9755 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9756 | } |
| 9757 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9758 | } |
| 9759 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9760 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9761 | else |
| 9762 | { |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 9763 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9764 | if (image_depth < 8) |
| 9765 | image_depth=8; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9766 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9767 | if ((save_image_depth == 16) && (image_depth == 8)) |
| 9768 | { |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 9769 | if (logging != MagickFalse) |
| 9770 | { |
| 9771 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9772 | " Scaling ping_trans_color from (%d,%d,%d)", |
| 9773 | (int) ping_trans_color.red, |
| 9774 | (int) ping_trans_color.green, |
| 9775 | (int) ping_trans_color.blue); |
| 9776 | } |
| 9777 | |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 9778 | ping_trans_color.red*=0x0101; |
| 9779 | ping_trans_color.green*=0x0101; |
| 9780 | ping_trans_color.blue*=0x0101; |
| 9781 | ping_trans_color.gray*=0x0101; |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 9782 | |
| 9783 | if (logging != MagickFalse) |
| 9784 | { |
| 9785 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9786 | " to (%d,%d,%d)", |
| 9787 | (int) ping_trans_color.red, |
| 9788 | (int) ping_trans_color.green, |
| 9789 | (int) ping_trans_color.blue); |
| 9790 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9791 | } |
| 9792 | } |
| 9793 | |
cristy | 4383ec8 | 2011-01-05 15:42:32 +0000 | [diff] [blame] | 9794 | if (ping_bit_depth < (ssize_t) mng_info->write_png_depth) |
| 9795 | ping_bit_depth = (ssize_t) mng_info->write_png_depth; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 9796 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9797 | /* |
| 9798 | Adjust background and transparency samples in sub-8-bit grayscale files. |
| 9799 | */ |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 9800 | if (ping_bit_depth < 8 && ping_color_type == |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9801 | PNG_COLOR_TYPE_GRAY) |
| 9802 | { |
| 9803 | png_uint_16 |
| 9804 | maxval; |
| 9805 | |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 9806 | size_t |
| 9807 | one=1; |
| 9808 | |
cristy | 22ffd97 | 2010-06-03 16:51:47 +0000 | [diff] [blame] | 9809 | maxval=(png_uint_16) ((one << ping_bit_depth)-1); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9810 | |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 9811 | if (ping_exclude_bKGD == MagickFalse) |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 9812 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9813 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 9814 | ping_background.gray=(png_uint_16) ((maxval/65535.)* |
| 9815 | (ScaleQuantumToShort(((GetPixelInfoIntensity( |
| 9816 | &image->background_color))) +.5))); |
| 9817 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9818 | if (logging != MagickFalse) |
| 9819 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 9820 | " Setting up bKGD chunk (2)"); |
| 9821 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9822 | " background_color index is %d", |
| 9823 | (int) ping_background.index); |
glennrp | 3b51f0e | 2010-11-27 18:14:08 +0000 | [diff] [blame] | 9824 | |
glennrp | 991d11d | 2010-11-12 21:55:28 +0000 | [diff] [blame] | 9825 | ping_have_bKGD = MagickTrue; |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 9826 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9827 | |
glennrp | 3e3e20f | 2011-06-09 04:21:43 +0000 | [diff] [blame] | 9828 | if (logging != MagickFalse) |
| 9829 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9830 | " Scaling ping_trans_color.gray from %d", |
| 9831 | (int)ping_trans_color.gray); |
| 9832 | |
glennrp | 9be9b1c | 2011-06-09 12:21:45 +0000 | [diff] [blame] | 9833 | ping_trans_color.gray=(png_uint_16) ((maxval/255.)*( |
glennrp | 3e3e20f | 2011-06-09 04:21:43 +0000 | [diff] [blame] | 9834 | ping_trans_color.gray)+.5); |
| 9835 | |
| 9836 | if (logging != MagickFalse) |
| 9837 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9838 | " to %d", (int)ping_trans_color.gray); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9839 | } |
glennrp | 17a1485 | 2010-05-10 03:01:59 +0000 | [diff] [blame] | 9840 | |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 9841 | if (ping_exclude_bKGD == MagickFalse) |
| 9842 | { |
glennrp | 1273f7b | 2011-02-24 03:20:30 +0000 | [diff] [blame] | 9843 | if (mng_info->IsPalette && (int) ping_color_type == PNG_COLOR_TYPE_PALETTE) |
glennrp | 17a1485 | 2010-05-10 03:01:59 +0000 | [diff] [blame] | 9844 | { |
| 9845 | /* |
| 9846 | Identify which colormap entry is the background color. |
| 9847 | */ |
| 9848 | |
glennrp | 17a1485 | 2010-05-10 03:01:59 +0000 | [diff] [blame] | 9849 | number_colors=image_colors; |
| 9850 | |
glennrp | a521b2f | 2010-10-29 04:11:03 +0000 | [diff] [blame] | 9851 | for (i=0; i < (ssize_t) MagickMax(1L*number_colors,1L); i++) |
| 9852 | if (IsPNGColorEqual(image->background_color,image->colormap[i])) |
glennrp | 17a1485 | 2010-05-10 03:01:59 +0000 | [diff] [blame] | 9853 | break; |
| 9854 | |
| 9855 | ping_background.index=(png_byte) i; |
| 9856 | |
glennrp | 3b51f0e | 2010-11-27 18:14:08 +0000 | [diff] [blame] | 9857 | if (logging != MagickFalse) |
glennrp | a521b2f | 2010-10-29 04:11:03 +0000 | [diff] [blame] | 9858 | { |
| 9859 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9860 | " Setting up bKGD chunk with index=%d",(int) i); |
glennrp | a521b2f | 2010-10-29 04:11:03 +0000 | [diff] [blame] | 9861 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9862 | |
cristy | 13d0704 | 2010-11-21 20:56:18 +0000 | [diff] [blame] | 9863 | if (i < (ssize_t) number_colors) |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9864 | { |
| 9865 | ping_have_bKGD = MagickTrue; |
glennrp | 3b51f0e | 2010-11-27 18:14:08 +0000 | [diff] [blame] | 9866 | |
| 9867 | if (logging != MagickFalse) |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9868 | { |
| 9869 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9870 | " background =(%d,%d,%d)", |
| 9871 | (int) ping_background.red, |
| 9872 | (int) ping_background.green, |
| 9873 | (int) ping_background.blue); |
| 9874 | } |
| 9875 | } |
glennrp | a521b2f | 2010-10-29 04:11:03 +0000 | [diff] [blame] | 9876 | |
glennrp | d6bf161 | 2010-12-17 17:28:54 +0000 | [diff] [blame] | 9877 | else /* Can't happen */ |
glennrp | 3c21811 | 2010-11-27 15:31:26 +0000 | [diff] [blame] | 9878 | { |
glennrp | 3b51f0e | 2010-11-27 18:14:08 +0000 | [diff] [blame] | 9879 | if (logging != MagickFalse) |
| 9880 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9881 | " No room in PLTE to add bKGD color"); |
glennrp | 3c21811 | 2010-11-27 15:31:26 +0000 | [diff] [blame] | 9882 | ping_have_bKGD = MagickFalse; |
| 9883 | } |
glennrp | 17a1485 | 2010-05-10 03:01:59 +0000 | [diff] [blame] | 9884 | } |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 9885 | } |
glennrp | 17a1485 | 2010-05-10 03:01:59 +0000 | [diff] [blame] | 9886 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9887 | if (logging != MagickFalse) |
| 9888 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | 1a56e9c | 2012-04-25 17:06:57 +0000 | [diff] [blame] | 9889 | " PNG color type: %s (%d)", PngColorTypeToString(ping_color_type), |
| 9890 | ping_color_type); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9891 | /* |
| 9892 | Initialize compression level and filtering. |
| 9893 | */ |
| 9894 | if (logging != MagickFalse) |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9895 | { |
| 9896 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9897 | " Setting up deflate compression"); |
| 9898 | |
| 9899 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9900 | " Compression buffer size: 32768"); |
| 9901 | } |
| 9902 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9903 | png_set_compression_buffer_size(ping,32768L); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9904 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9905 | if (logging != MagickFalse) |
| 9906 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9907 | " Compression mem level: 9"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9908 | |
cristy | 4054bfb | 2011-08-29 23:41:39 +0000 | [diff] [blame] | 9909 | png_set_compression_mem_level(ping, 9); |
| 9910 | |
glennrp | 10d739e | 2011-06-29 18:00:52 +0000 | [diff] [blame] | 9911 | /* Untangle the "-quality" setting: |
| 9912 | |
| 9913 | Undefined is 0; the default is used. |
| 9914 | Default is 75 |
| 9915 | |
| 9916 | 10's digit: |
| 9917 | |
| 9918 | 0: Use Z_HUFFMAN_ONLY strategy with the |
| 9919 | zlib default compression level |
| 9920 | |
| 9921 | 1-9: the zlib compression level |
| 9922 | |
| 9923 | 1's digit: |
| 9924 | |
| 9925 | 0-4: the PNG filter method |
| 9926 | |
| 9927 | 5: libpng adaptive filtering if compression level > 5 |
| 9928 | libpng filter type "none" if compression level <= 5 |
| 9929 | or if image is grayscale or palette |
glennrp | 750105b | 2012-04-25 16:20:45 +0000 | [diff] [blame] | 9930 | |
glennrp | 10d739e | 2011-06-29 18:00:52 +0000 | [diff] [blame] | 9931 | 6: libpng adaptive filtering |
| 9932 | |
| 9933 | 7: "LOCO" filtering (intrapixel differing) if writing |
| 9934 | a MNG, othewise "none". Did not work in IM-6.7.0-9 |
| 9935 | and earlier because of a missing "else". |
| 9936 | |
| 9937 | 8: Z_RLE strategy, all filters |
glennrp | 1868258 | 2011-06-30 18:11:47 +0000 | [diff] [blame] | 9938 | Unused prior to IM-6.7.0-10, was same as 6 |
glennrp | 10d739e | 2011-06-29 18:00:52 +0000 | [diff] [blame] | 9939 | |
| 9940 | 9: Z_RLE strategy, no PNG filters |
glennrp | 1868258 | 2011-06-30 18:11:47 +0000 | [diff] [blame] | 9941 | Unused prior to IM-6.7.0-10, was same as 6 |
glennrp | 10d739e | 2011-06-29 18:00:52 +0000 | [diff] [blame] | 9942 | |
| 9943 | Note that using the -quality option, not all combinations of |
| 9944 | PNG filter type, zlib compression level, and zlib compression |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 9945 | strategy are possible. This will be addressed soon in a |
| 9946 | release that accomodates "-define png:compression-strategy", etc. |
glennrp | 10d739e | 2011-06-29 18:00:52 +0000 | [diff] [blame] | 9947 | |
| 9948 | */ |
| 9949 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9950 | quality=image->quality == UndefinedCompressionQuality ? 75UL : |
| 9951 | image->quality; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9952 | |
glennrp | 1868258 | 2011-06-30 18:11:47 +0000 | [diff] [blame] | 9953 | if (quality <= 9) |
| 9954 | { |
| 9955 | if (mng_info->write_png_compression_strategy == 0) |
| 9956 | mng_info->write_png_compression_strategy = Z_HUFFMAN_ONLY+1; |
| 9957 | } |
glennrp | 750105b | 2012-04-25 16:20:45 +0000 | [diff] [blame] | 9958 | |
glennrp | 1868258 | 2011-06-30 18:11:47 +0000 | [diff] [blame] | 9959 | else if (mng_info->write_png_compression_level == 0) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9960 | { |
| 9961 | int |
| 9962 | level; |
| 9963 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 9964 | level=(int) MagickMin((ssize_t) quality/10,9); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9965 | |
glennrp | 1868258 | 2011-06-30 18:11:47 +0000 | [diff] [blame] | 9966 | mng_info->write_png_compression_level = level+1; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9967 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9968 | |
glennrp | 1868258 | 2011-06-30 18:11:47 +0000 | [diff] [blame] | 9969 | if (mng_info->write_png_compression_strategy == 0) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9970 | { |
glennrp | 1868258 | 2011-06-30 18:11:47 +0000 | [diff] [blame] | 9971 | if ((quality %10) == 8 || (quality %10) == 9) |
glennrp | a24b245 | 2012-06-27 11:38:38 +0000 | [diff] [blame] | 9972 | #ifdef Z_RLE /* Z_RLE was added to zlib-1.2.0 */ |
| 9973 | mng_info->write_png_compression_strategy=Z_RLE+1; |
| 9974 | #else |
| 9975 | mng_info->write_png_compression_strategy = Z_DEFAULT_STRATEGY+1; |
| 9976 | #endif |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9977 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9978 | |
glennrp | 1868258 | 2011-06-30 18:11:47 +0000 | [diff] [blame] | 9979 | if (mng_info->write_png_compression_filter == 0) |
| 9980 | mng_info->write_png_compression_filter=((int) quality % 10) + 1; |
| 9981 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9982 | if (logging != MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9983 | { |
glennrp | 1868258 | 2011-06-30 18:11:47 +0000 | [diff] [blame] | 9984 | if (mng_info->write_png_compression_level) |
| 9985 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9986 | " Compression level: %d", |
| 9987 | (int) mng_info->write_png_compression_level-1); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9988 | |
glennrp | 1868258 | 2011-06-30 18:11:47 +0000 | [diff] [blame] | 9989 | if (mng_info->write_png_compression_strategy) |
| 9990 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9991 | " Compression strategy: %d", |
| 9992 | (int) mng_info->write_png_compression_strategy-1); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 9993 | |
glennrp | 1868258 | 2011-06-30 18:11:47 +0000 | [diff] [blame] | 9994 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9995 | " Setting up filtering"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9996 | |
cristy | 4054bfb | 2011-08-29 23:41:39 +0000 | [diff] [blame] | 9997 | if (mng_info->write_png_compression_filter == 6) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 9998 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 9999 | " Base filter method: ADAPTIVE"); |
cristy | 4054bfb | 2011-08-29 23:41:39 +0000 | [diff] [blame] | 10000 | else if (mng_info->write_png_compression_filter == 0 || |
| 10001 | mng_info->write_png_compression_filter == 1) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10002 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10003 | " Base filter method: NONE"); |
glennrp | 1868258 | 2011-06-30 18:11:47 +0000 | [diff] [blame] | 10004 | else |
| 10005 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10006 | " Base filter method: %d", |
| 10007 | (int) mng_info->write_png_compression_filter-1); |
| 10008 | } |
glennrp | 2b013e4 | 2010-11-24 16:55:50 +0000 | [diff] [blame] | 10009 | |
glennrp | 1868258 | 2011-06-30 18:11:47 +0000 | [diff] [blame] | 10010 | if (mng_info->write_png_compression_level != 0) |
| 10011 | png_set_compression_level(ping,mng_info->write_png_compression_level-1); |
| 10012 | |
| 10013 | if (mng_info->write_png_compression_filter == 6) |
| 10014 | { |
| 10015 | if (((int) ping_color_type == PNG_COLOR_TYPE_GRAY) || |
| 10016 | ((int) ping_color_type == PNG_COLOR_TYPE_PALETTE) || |
| 10017 | (quality < 50)) |
| 10018 | png_set_filter(ping,PNG_FILTER_TYPE_BASE,PNG_NO_FILTERS); |
| 10019 | else |
| 10020 | png_set_filter(ping,PNG_FILTER_TYPE_BASE,PNG_ALL_FILTERS); |
| 10021 | } |
cristy | 4054bfb | 2011-08-29 23:41:39 +0000 | [diff] [blame] | 10022 | else if (mng_info->write_png_compression_filter == 7 || |
glennrp | 1868258 | 2011-06-30 18:11:47 +0000 | [diff] [blame] | 10023 | mng_info->write_png_compression_filter == 10) |
| 10024 | png_set_filter(ping,PNG_FILTER_TYPE_BASE,PNG_ALL_FILTERS); |
| 10025 | |
| 10026 | else if (mng_info->write_png_compression_filter == 8) |
| 10027 | { |
| 10028 | #if defined(PNG_MNG_FEATURES_SUPPORTED) && defined(PNG_INTRAPIXEL_DIFFERENCING) |
| 10029 | if (mng_info->write_mng) |
| 10030 | { |
| 10031 | if (((int) ping_color_type == PNG_COLOR_TYPE_RGB) || |
| 10032 | ((int) ping_color_type == PNG_COLOR_TYPE_RGBA)) |
| 10033 | ping_filter_method=PNG_INTRAPIXEL_DIFFERENCING; |
| 10034 | } |
| 10035 | #endif |
cristy | 4054bfb | 2011-08-29 23:41:39 +0000 | [diff] [blame] | 10036 | png_set_filter(ping,PNG_FILTER_TYPE_BASE,PNG_NO_FILTERS); |
glennrp | 1868258 | 2011-06-30 18:11:47 +0000 | [diff] [blame] | 10037 | } |
| 10038 | |
| 10039 | else if (mng_info->write_png_compression_filter == 9) |
| 10040 | png_set_filter(ping,PNG_FILTER_TYPE_BASE,PNG_NO_FILTERS); |
| 10041 | |
| 10042 | else if (mng_info->write_png_compression_filter != 0) |
| 10043 | png_set_filter(ping,PNG_FILTER_TYPE_BASE, |
| 10044 | mng_info->write_png_compression_filter-1); |
| 10045 | |
| 10046 | if (mng_info->write_png_compression_strategy != 0) |
| 10047 | png_set_compression_strategy(ping, |
| 10048 | mng_info->write_png_compression_strategy-1); |
| 10049 | |
cristy | 0d57eec | 2011-09-04 22:13:56 +0000 | [diff] [blame] | 10050 | /* Only write the iCCP chunk if we are not writing the sRGB chunk. */ |
| 10051 | if (ping_exclude_sRGB != MagickFalse || |
| 10052 | (image->rendering_intent == UndefinedIntent)) |
| 10053 | { |
| 10054 | if ((ping_exclude_tEXt == MagickFalse || |
| 10055 | ping_exclude_zTXt == MagickFalse) && |
| 10056 | (ping_exclude_iCCP == MagickFalse || ping_exclude_zCCP == MagickFalse)) |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 10057 | { |
| 10058 | ResetImageProfileIterator(image); |
| 10059 | for (name=GetNextImageProfile(image); name != (const char *) NULL; ) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10060 | { |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 10061 | profile=GetImageProfile(image,name); |
| 10062 | |
| 10063 | if (profile != (StringInfo *) NULL) |
| 10064 | { |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 10065 | #ifdef PNG_WRITE_iCCP_SUPPORTED |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 10066 | if ((LocaleCompare(name,"ICC") == 0) || |
| 10067 | (LocaleCompare(name,"ICM") == 0)) |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10068 | { |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 10069 | |
| 10070 | if (ping_exclude_iCCP == MagickFalse) |
| 10071 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 10072 | png_set_iCCP(ping,ping_info,(png_charp) name,0, |
glennrp | e4017e3 | 2011-01-08 17:16:09 +0000 | [diff] [blame] | 10073 | #if (PNG_LIBPNG_VER < 10500) |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 10074 | (png_charp) GetStringInfoDatum(profile), |
glennrp | e4017e3 | 2011-01-08 17:16:09 +0000 | [diff] [blame] | 10075 | #else |
| 10076 | (png_const_bytep) GetStringInfoDatum(profile), |
| 10077 | #endif |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 10078 | (png_uint_32) GetStringInfoLength(profile)); |
| 10079 | } |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10080 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10081 | |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 10082 | else |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10083 | #endif |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 10084 | if (ping_exclude_zCCP == MagickFalse) |
| 10085 | { |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 10086 | Magick_png_write_raw_profile(image_info,ping,ping_info, |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 10087 | (unsigned char *) name,(unsigned char *) name, |
| 10088 | GetStringInfoDatum(profile), |
| 10089 | (png_uint_32) GetStringInfoLength(profile)); |
| 10090 | } |
| 10091 | } |
glennrp | 0b206f5 | 2011-01-07 04:55:32 +0000 | [diff] [blame] | 10092 | |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 10093 | if (logging != MagickFalse) |
| 10094 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10095 | " Setting up text chunk with %s profile",name); |
| 10096 | |
| 10097 | name=GetNextImageProfile(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10098 | } |
cristy | 0d57eec | 2011-09-04 22:13:56 +0000 | [diff] [blame] | 10099 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10100 | } |
| 10101 | |
| 10102 | #if defined(PNG_WRITE_sRGB_SUPPORTED) |
| 10103 | if ((mng_info->have_write_global_srgb == 0) && |
glennrp | 5f11bf9 | 2012-05-05 03:21:03 +0000 | [diff] [blame] | 10104 | (image->rendering_intent != UndefinedIntent)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10105 | { |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10106 | if (ping_exclude_sRGB == MagickFalse) |
| 10107 | { |
| 10108 | /* |
| 10109 | Note image rendering intent. |
| 10110 | */ |
| 10111 | if (logging != MagickFalse) |
| 10112 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10113 | " Setting up sRGB chunk"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10114 | |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10115 | (void) png_set_sRGB(ping,ping_info,( |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 10116 | Magick_RenderingIntent_to_PNG_RenderingIntent( |
| 10117 | image->rendering_intent))); |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10118 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10119 | } |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10120 | |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 10121 | 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] | 10122 | #endif |
| 10123 | { |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10124 | if (ping_exclude_gAMA == MagickFalse && |
| 10125 | (ping_exclude_sRGB == MagickFalse || |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10126 | (image->gamma < .45 || image->gamma > .46))) |
| 10127 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10128 | if ((mng_info->have_write_global_gama == 0) && (image->gamma != 0.0)) |
| 10129 | { |
| 10130 | /* |
| 10131 | Note image gamma. |
| 10132 | To do: check for cHRM+gAMA == sRGB, and write sRGB instead. |
| 10133 | */ |
| 10134 | if (logging != MagickFalse) |
| 10135 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10136 | " Setting up gAMA chunk"); |
glennrp | 3b51f0e | 2010-11-27 18:14:08 +0000 | [diff] [blame] | 10137 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10138 | png_set_gAMA(ping,ping_info,image->gamma); |
| 10139 | } |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10140 | } |
glennrp | 2b013e4 | 2010-11-24 16:55:50 +0000 | [diff] [blame] | 10141 | |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10142 | if (ping_exclude_cHRM == MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10143 | { |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10144 | if ((mng_info->have_write_global_chrm == 0) && |
| 10145 | (image->chromaticity.red_primary.x != 0.0)) |
| 10146 | { |
| 10147 | /* |
| 10148 | Note image chromaticity. |
| 10149 | To do: check for cHRM+gAMA == sRGB, and write sRGB instead. |
| 10150 | */ |
| 10151 | PrimaryInfo |
| 10152 | bp, |
| 10153 | gp, |
| 10154 | rp, |
| 10155 | wp; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10156 | |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10157 | wp=image->chromaticity.white_point; |
| 10158 | rp=image->chromaticity.red_primary; |
| 10159 | gp=image->chromaticity.green_primary; |
| 10160 | bp=image->chromaticity.blue_primary; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10161 | |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10162 | if (logging != MagickFalse) |
| 10163 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10164 | " Setting up cHRM chunk"); |
glennrp | 3b51f0e | 2010-11-27 18:14:08 +0000 | [diff] [blame] | 10165 | |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10166 | png_set_cHRM(ping,ping_info,wp.x,wp.y,rp.x,rp.y,gp.x,gp.y, |
| 10167 | bp.x,bp.y); |
| 10168 | } |
| 10169 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10170 | } |
glennrp | dfd7080 | 2010-11-14 01:23:35 +0000 | [diff] [blame] | 10171 | |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 10172 | ping_interlace_method=image_info->interlace != NoInterlace; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10173 | |
| 10174 | if (mng_info->write_mng) |
| 10175 | png_set_sig_bytes(ping,8); |
| 10176 | |
cristy | 5d6fc9c | 2011-12-27 03:10:42 +0000 | [diff] [blame] | 10177 | /* Bail out if cannot meet defined png:bit-depth or png:color-type */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10178 | |
glennrp | d6bf161 | 2010-12-17 17:28:54 +0000 | [diff] [blame] | 10179 | if (mng_info->write_png_colortype != 0) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10180 | { |
| 10181 | if (mng_info->write_png_colortype-1 == PNG_COLOR_TYPE_GRAY) |
glennrp | 8d57966 | 2011-02-23 02:05:02 +0000 | [diff] [blame] | 10182 | if (ping_have_color != MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10183 | { |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 10184 | ping_color_type = PNG_COLOR_TYPE_RGB; |
glennrp | 2b013e4 | 2010-11-24 16:55:50 +0000 | [diff] [blame] | 10185 | |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 10186 | if (ping_bit_depth < 8) |
| 10187 | ping_bit_depth=8; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10188 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10189 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10190 | if (mng_info->write_png_colortype-1 == PNG_COLOR_TYPE_GRAY_ALPHA) |
glennrp | 8d57966 | 2011-02-23 02:05:02 +0000 | [diff] [blame] | 10191 | if (ping_have_color != MagickFalse) |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 10192 | ping_color_type = PNG_COLOR_TYPE_RGB_ALPHA; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10193 | } |
| 10194 | |
glennrp | 0e8ea19 | 2010-12-24 18:00:33 +0000 | [diff] [blame] | 10195 | if (ping_need_colortype_warning != MagickFalse || |
| 10196 | ((mng_info->write_png_depth && |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 10197 | (int) mng_info->write_png_depth != ping_bit_depth) || |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10198 | (mng_info->write_png_colortype && |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 10199 | ((int) mng_info->write_png_colortype-1 != ping_color_type && |
glennrp | 991e92a | 2010-01-28 03:09:00 +0000 | [diff] [blame] | 10200 | mng_info->write_png_colortype != 7 && |
glennrp | 0e8ea19 | 2010-12-24 18:00:33 +0000 | [diff] [blame] | 10201 | !(mng_info->write_png_colortype == 5 && ping_color_type == 0))))) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10202 | { |
| 10203 | if (logging != MagickFalse) |
| 10204 | { |
glennrp | 0e8ea19 | 2010-12-24 18:00:33 +0000 | [diff] [blame] | 10205 | if (ping_need_colortype_warning != MagickFalse) |
| 10206 | { |
| 10207 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10208 | " Image has transparency but tRNS chunk was excluded"); |
| 10209 | } |
| 10210 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10211 | if (mng_info->write_png_depth) |
| 10212 | { |
| 10213 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | 5d6fc9c | 2011-12-27 03:10:42 +0000 | [diff] [blame] | 10214 | " Defined png:bit-depth=%u, Computed depth=%u", |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10215 | mng_info->write_png_depth, |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 10216 | ping_bit_depth); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10217 | } |
glennrp | 0e8ea19 | 2010-12-24 18:00:33 +0000 | [diff] [blame] | 10218 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10219 | if (mng_info->write_png_colortype) |
| 10220 | { |
| 10221 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | 5d6fc9c | 2011-12-27 03:10:42 +0000 | [diff] [blame] | 10222 | " Defined png:color-type=%u, Computed color type=%u", |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10223 | mng_info->write_png_colortype-1, |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 10224 | ping_color_type); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10225 | } |
| 10226 | } |
glennrp | 0e8ea19 | 2010-12-24 18:00:33 +0000 | [diff] [blame] | 10227 | |
glennrp | 3bd2e41 | 2010-08-10 13:34:52 +0000 | [diff] [blame] | 10228 | png_warning(ping, |
cristy | 5d6fc9c | 2011-12-27 03:10:42 +0000 | [diff] [blame] | 10229 | "Cannot write image with defined png:bit-depth or png:color-type."); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10230 | } |
| 10231 | |
glennrp | 58e0176 | 2011-01-07 15:28:54 +0000 | [diff] [blame] | 10232 | if (image_matte != MagickFalse && image->matte == MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10233 | { |
| 10234 | /* Add an opaque matte channel */ |
| 10235 | image->matte = MagickTrue; |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 10236 | (void) SetImageAlpha(image,OpaqueAlpha,exception); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10237 | |
glennrp | b4a1341 | 2010-05-05 12:47:19 +0000 | [diff] [blame] | 10238 | if (logging != MagickFalse) |
| 10239 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10240 | " Added an opaque matte channel"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10241 | } |
| 10242 | |
glennrp | 0e31973 | 2011-01-25 21:53:13 +0000 | [diff] [blame] | 10243 | if (number_transparent != 0 || number_semitransparent != 0) |
glennrp | e9c26dc | 2010-05-30 01:56:35 +0000 | [diff] [blame] | 10244 | { |
glennrp | 991d11d | 2010-11-12 21:55:28 +0000 | [diff] [blame] | 10245 | if (ping_color_type < 4) |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 10246 | { |
glennrp | 991d11d | 2010-11-12 21:55:28 +0000 | [diff] [blame] | 10247 | ping_have_tRNS=MagickTrue; |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 10248 | if (logging != MagickFalse) |
| 10249 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10250 | " Setting ping_have_tRNS=MagickTrue."); |
| 10251 | } |
glennrp | e9c26dc | 2010-05-30 01:56:35 +0000 | [diff] [blame] | 10252 | } |
| 10253 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10254 | if (logging != MagickFalse) |
| 10255 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10256 | " Writing PNG header chunks"); |
| 10257 | |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 10258 | png_set_IHDR(ping,ping_info,ping_width,ping_height, |
| 10259 | ping_bit_depth,ping_color_type, |
| 10260 | ping_interlace_method,ping_compression_method, |
| 10261 | ping_filter_method); |
| 10262 | |
glennrp | 39992b4 | 2010-11-14 00:03:43 +0000 | [diff] [blame] | 10263 | if (ping_color_type == 3 && ping_have_PLTE != MagickFalse) |
| 10264 | { |
glennrp | f09bded | 2011-01-08 01:15:59 +0000 | [diff] [blame] | 10265 | png_set_PLTE(ping,ping_info,palette,number_colors); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10266 | |
glennrp | 3b51f0e | 2010-11-27 18:14:08 +0000 | [diff] [blame] | 10267 | if (logging != MagickFalse) |
glennrp | 39992b4 | 2010-11-14 00:03:43 +0000 | [diff] [blame] | 10268 | { |
glennrp | 8640fb5 | 2010-11-23 15:48:26 +0000 | [diff] [blame] | 10269 | for (i=0; i< (ssize_t) number_colors; i++) |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10270 | { |
glennrp | d6bf161 | 2010-12-17 17:28:54 +0000 | [diff] [blame] | 10271 | if (i < ping_num_trans) |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10272 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | d6bf161 | 2010-12-17 17:28:54 +0000 | [diff] [blame] | 10273 | " PLTE[%d] = (%d,%d,%d), tRNS[%d] = (%d)", |
| 10274 | (int) i, |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10275 | (int) palette[i].red, |
| 10276 | (int) palette[i].green, |
| 10277 | (int) palette[i].blue, |
glennrp | d6bf161 | 2010-12-17 17:28:54 +0000 | [diff] [blame] | 10278 | (int) i, |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10279 | (int) ping_trans_alpha[i]); |
| 10280 | else |
| 10281 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | d6bf161 | 2010-12-17 17:28:54 +0000 | [diff] [blame] | 10282 | " PLTE[%d] = (%d,%d,%d)", |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10283 | (int) i, |
| 10284 | (int) palette[i].red, |
| 10285 | (int) palette[i].green, |
| 10286 | (int) palette[i].blue); |
| 10287 | } |
glennrp | 39992b4 | 2010-11-14 00:03:43 +0000 | [diff] [blame] | 10288 | } |
glennrp | 39992b4 | 2010-11-14 00:03:43 +0000 | [diff] [blame] | 10289 | } |
| 10290 | |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10291 | if (ping_exclude_bKGD == MagickFalse) |
glennrp | dfd7080 | 2010-11-14 01:23:35 +0000 | [diff] [blame] | 10292 | { |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10293 | if (ping_have_bKGD != MagickFalse) |
glennrp | c6c391a | 2011-04-27 02:23:56 +0000 | [diff] [blame] | 10294 | { |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10295 | png_set_bKGD(ping,ping_info,&ping_background); |
glennrp | c6c391a | 2011-04-27 02:23:56 +0000 | [diff] [blame] | 10296 | if (logging) |
| 10297 | { |
| 10298 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10299 | " Setting up bKGD chunk"); |
| 10300 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10301 | " background color = (%d,%d,%d)", |
| 10302 | (int) ping_background.red, |
| 10303 | (int) ping_background.green, |
| 10304 | (int) ping_background.blue); |
| 10305 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10306 | " index = %d, gray=%d", |
| 10307 | (int) ping_background.index, |
| 10308 | (int) ping_background.gray); |
| 10309 | } |
| 10310 | } |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10311 | } |
| 10312 | |
| 10313 | if (ping_exclude_pHYs == MagickFalse) |
| 10314 | { |
| 10315 | if (ping_have_pHYs != MagickFalse) |
| 10316 | { |
| 10317 | png_set_pHYs(ping,ping_info, |
| 10318 | ping_pHYs_x_resolution, |
| 10319 | ping_pHYs_y_resolution, |
| 10320 | ping_pHYs_unit_type); |
glennrp | 823b55c | 2011-03-14 18:46:46 +0000 | [diff] [blame] | 10321 | |
| 10322 | if (logging) |
| 10323 | { |
| 10324 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10325 | " Setting up pHYs chunk"); |
| 10326 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10327 | " x_resolution=%lu", |
| 10328 | (unsigned long) ping_pHYs_x_resolution); |
| 10329 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10330 | " y_resolution=%lu", |
| 10331 | (unsigned long) ping_pHYs_y_resolution); |
| 10332 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10333 | " unit_type=%lu", |
| 10334 | (unsigned long) ping_pHYs_unit_type); |
| 10335 | } |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10336 | } |
glennrp | dfd7080 | 2010-11-14 01:23:35 +0000 | [diff] [blame] | 10337 | } |
| 10338 | |
| 10339 | #if defined(PNG_oFFs_SUPPORTED) |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 10340 | if (ping_exclude_oFFs == MagickFalse) |
glennrp | dfd7080 | 2010-11-14 01:23:35 +0000 | [diff] [blame] | 10341 | { |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10342 | if (image->page.x || image->page.y) |
| 10343 | { |
| 10344 | png_set_oFFs(ping,ping_info,(png_int_32) image->page.x, |
| 10345 | (png_int_32) image->page.y, 0); |
glennrp | dfd7080 | 2010-11-14 01:23:35 +0000 | [diff] [blame] | 10346 | |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10347 | if (logging != MagickFalse) |
| 10348 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10349 | " Setting up oFFs chunk with x=%d, y=%d, units=0", |
| 10350 | (int) image->page.x, (int) image->page.y); |
| 10351 | } |
glennrp | dfd7080 | 2010-11-14 01:23:35 +0000 | [diff] [blame] | 10352 | } |
| 10353 | #endif |
| 10354 | |
glennrp | da8f3a7 | 2011-02-27 23:54:12 +0000 | [diff] [blame] | 10355 | if (mng_info->need_blob != MagickFalse) |
| 10356 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 10357 | if (OpenBlob(image_info,image,WriteBinaryBlobMode,exception) == |
glennrp | da8f3a7 | 2011-02-27 23:54:12 +0000 | [diff] [blame] | 10358 | MagickFalse) |
| 10359 | png_error(ping,"WriteBlob Failed"); |
| 10360 | |
| 10361 | ping_have_blob=MagickTrue; |
| 10362 | } |
| 10363 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10364 | png_write_info_before_PLTE(ping, ping_info); |
glennrp | 991d11d | 2010-11-12 21:55:28 +0000 | [diff] [blame] | 10365 | |
glennrp | 39992b4 | 2010-11-14 00:03:43 +0000 | [diff] [blame] | 10366 | if (ping_have_tRNS != MagickFalse && ping_color_type < 4) |
glennrp | 991d11d | 2010-11-12 21:55:28 +0000 | [diff] [blame] | 10367 | { |
glennrp | 3b51f0e | 2010-11-27 18:14:08 +0000 | [diff] [blame] | 10368 | if (logging != MagickFalse) |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10369 | { |
| 10370 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10371 | " Calling png_set_tRNS with num_trans=%d",ping_num_trans); |
| 10372 | } |
| 10373 | |
| 10374 | if (ping_color_type == 3) |
| 10375 | (void) png_set_tRNS(ping, ping_info, |
| 10376 | ping_trans_alpha, |
| 10377 | ping_num_trans, |
| 10378 | NULL); |
| 10379 | |
| 10380 | else |
| 10381 | { |
| 10382 | (void) png_set_tRNS(ping, ping_info, |
| 10383 | NULL, |
| 10384 | 0, |
| 10385 | &ping_trans_color); |
| 10386 | |
glennrp | 3b51f0e | 2010-11-27 18:14:08 +0000 | [diff] [blame] | 10387 | if (logging != MagickFalse) |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10388 | { |
| 10389 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | c8cbc5d | 2011-01-01 00:12:34 +0000 | [diff] [blame] | 10390 | " tRNS color =(%d,%d,%d)", |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10391 | (int) ping_trans_color.red, |
| 10392 | (int) ping_trans_color.green, |
| 10393 | (int) ping_trans_color.blue); |
| 10394 | } |
| 10395 | } |
glennrp | 991d11d | 2010-11-12 21:55:28 +0000 | [diff] [blame] | 10396 | } |
| 10397 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10398 | /* write any png-chunk-b profiles */ |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 10399 | (void) Magick_png_write_chunk_from_profile(image,"PNG-chunk-b",logging); |
glennrp | da8f3a7 | 2011-02-27 23:54:12 +0000 | [diff] [blame] | 10400 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10401 | png_write_info(ping,ping_info); |
glennrp | 991d11d | 2010-11-12 21:55:28 +0000 | [diff] [blame] | 10402 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10403 | /* write any PNG-chunk-m profiles */ |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 10404 | (void) Magick_png_write_chunk_from_profile(image,"PNG-chunk-m",logging); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10405 | |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10406 | if (ping_exclude_vpAg == MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10407 | { |
glennrp | 4f25bd0 | 2011-01-01 18:51:28 +0000 | [diff] [blame] | 10408 | if ((image->page.width != 0 && image->page.width != image->columns) || |
| 10409 | (image->page.height != 0 && image->page.height != image->rows)) |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10410 | { |
| 10411 | unsigned char |
| 10412 | chunk[14]; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10413 | |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10414 | (void) WriteBlobMSBULong(image,9L); /* data length=8 */ |
| 10415 | PNGType(chunk,mng_vpAg); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 10416 | LogPNGChunk(logging,mng_vpAg,9L); |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10417 | PNGLong(chunk+4,(png_uint_32) image->page.width); |
| 10418 | PNGLong(chunk+8,(png_uint_32) image->page.height); |
| 10419 | chunk[12]=0; /* unit = pixels */ |
| 10420 | (void) WriteBlob(image,13,chunk); |
| 10421 | (void) WriteBlobMSBULong(image,crc32(0,chunk,13)); |
| 10422 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10423 | } |
| 10424 | |
| 10425 | #if (PNG_LIBPNG_VER == 10206) |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 10426 | /* avoid libpng-1.2.6 bug by setting PNG_HAVE_IDAT flag */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10427 | #define PNG_HAVE_IDAT 0x04 |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 10428 | ping->mode |= PNG_HAVE_IDAT; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10429 | #undef PNG_HAVE_IDAT |
| 10430 | #endif |
| 10431 | |
| 10432 | png_set_packing(ping); |
| 10433 | /* |
| 10434 | Allocate memory. |
| 10435 | */ |
| 10436 | rowbytes=image->columns; |
glennrp | b4a1341 | 2010-05-05 12:47:19 +0000 | [diff] [blame] | 10437 | if (image_depth > 8) |
| 10438 | rowbytes*=2; |
cristy | 7202c10 | 2010-05-05 19:18:28 +0000 | [diff] [blame] | 10439 | switch (ping_color_type) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10440 | { |
glennrp | b4a1341 | 2010-05-05 12:47:19 +0000 | [diff] [blame] | 10441 | case PNG_COLOR_TYPE_RGB: |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10442 | rowbytes*=3; |
glennrp | b4a1341 | 2010-05-05 12:47:19 +0000 | [diff] [blame] | 10443 | break; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10444 | |
glennrp | b4a1341 | 2010-05-05 12:47:19 +0000 | [diff] [blame] | 10445 | case PNG_COLOR_TYPE_GRAY_ALPHA: |
| 10446 | rowbytes*=2; |
| 10447 | break; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10448 | |
glennrp | b4a1341 | 2010-05-05 12:47:19 +0000 | [diff] [blame] | 10449 | case PNG_COLOR_TYPE_RGBA: |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10450 | rowbytes*=4; |
glennrp | b4a1341 | 2010-05-05 12:47:19 +0000 | [diff] [blame] | 10451 | break; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10452 | |
glennrp | b4a1341 | 2010-05-05 12:47:19 +0000 | [diff] [blame] | 10453 | default: |
| 10454 | break; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10455 | } |
glennrp | 3b51f0e | 2010-11-27 18:14:08 +0000 | [diff] [blame] | 10456 | |
| 10457 | if (logging != MagickFalse) |
glennrp | b4a1341 | 2010-05-05 12:47:19 +0000 | [diff] [blame] | 10458 | { |
| 10459 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10460 | " Writing PNG image data"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10461 | |
glennrp | b4a1341 | 2010-05-05 12:47:19 +0000 | [diff] [blame] | 10462 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 10463 | " Allocating %.20g bytes of memory for pixels",(double) rowbytes); |
glennrp | b4a1341 | 2010-05-05 12:47:19 +0000 | [diff] [blame] | 10464 | } |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 10465 | ping_pixels=(unsigned char *) AcquireQuantumMemory(rowbytes, |
| 10466 | sizeof(*ping_pixels)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10467 | |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 10468 | if (ping_pixels == (unsigned char *) NULL) |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 10469 | png_error(ping,"Allocation of memory for pixels failed"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10470 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10471 | /* |
| 10472 | Initialize image scanlines. |
| 10473 | */ |
cristy | ed55252 | 2009-10-16 14:04:35 +0000 | [diff] [blame] | 10474 | quantum_info=AcquireQuantumInfo(image_info,image); |
| 10475 | if (quantum_info == (QuantumInfo *) NULL) |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 10476 | png_error(ping,"Memory allocation for quantum_info failed"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10477 | quantum_info->format=UndefinedQuantumFormat; |
| 10478 | quantum_info->depth=image_depth; |
| 10479 | num_passes=png_set_interlace_handling(ping); |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 10480 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10481 | if ((!mng_info->write_png8 && !mng_info->write_png24 && |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 10482 | !mng_info->write_png32) && |
| 10483 | (mng_info->IsPalette || |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10484 | (image_info->type == BilevelType)) && |
glennrp | 8d57966 | 2011-02-23 02:05:02 +0000 | [diff] [blame] | 10485 | image_matte == MagickFalse && |
| 10486 | ping_have_non_bw == MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10487 | { |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 10488 | /* Palette, Bilevel, or Opaque Monochrome */ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 10489 | register const Quantum |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10490 | *p; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10491 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10492 | quantum_info->depth=8; |
| 10493 | for (pass=0; pass < num_passes; pass++) |
| 10494 | { |
| 10495 | /* |
| 10496 | Convert PseudoClass image to a PNG monochrome image. |
| 10497 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 10498 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10499 | { |
glennrp | d71e86a | 2011-02-24 01:28:37 +0000 | [diff] [blame] | 10500 | if (logging != MagickFalse && y == 0) |
glennrp | 3b51f0e | 2010-11-27 18:14:08 +0000 | [diff] [blame] | 10501 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10502 | " Writing row of pixels (0)"); |
glennrp | a521b2f | 2010-10-29 04:11:03 +0000 | [diff] [blame] | 10503 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 10504 | p=GetVirtualPixels(image,0,y,image->columns,1,exception); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10505 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 10506 | if (p == (const Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10507 | break; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10508 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10509 | if (mng_info->IsPalette) |
| 10510 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 10511 | (void) ExportQuantumPixels(image,(CacheView *) NULL, |
| 10512 | quantum_info,GrayQuantum,ping_pixels,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10513 | if (mng_info->write_png_colortype-1 == PNG_COLOR_TYPE_PALETTE && |
| 10514 | mng_info->write_png_depth && |
| 10515 | mng_info->write_png_depth != old_bit_depth) |
| 10516 | { |
| 10517 | /* Undo pixel scaling */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 10518 | for (i=0; i < (ssize_t) image->columns; i++) |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 10519 | *(ping_pixels+i)=(unsigned char) (*(ping_pixels+i) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10520 | >> (8-old_bit_depth)); |
| 10521 | } |
| 10522 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10523 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10524 | else |
| 10525 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 10526 | (void) ExportQuantumPixels(image,(CacheView *) NULL, |
| 10527 | quantum_info,RedQuantum,ping_pixels,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10528 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10529 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10530 | if (mng_info->write_png_colortype-1 != PNG_COLOR_TYPE_PALETTE) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 10531 | for (i=0; i < (ssize_t) image->columns; i++) |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 10532 | *(ping_pixels+i)=(unsigned char) ((*(ping_pixels+i) > 127) ? |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10533 | 255 : 0); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10534 | |
glennrp | 3b51f0e | 2010-11-27 18:14:08 +0000 | [diff] [blame] | 10535 | if (logging != MagickFalse && y == 0) |
glennrp | b4a1341 | 2010-05-05 12:47:19 +0000 | [diff] [blame] | 10536 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10537 | " Writing row of pixels (1)"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10538 | |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 10539 | png_write_row(ping,ping_pixels); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10540 | } |
| 10541 | if (image->previous == (Image *) NULL) |
| 10542 | { |
| 10543 | status=SetImageProgress(image,LoadImageTag,pass,num_passes); |
| 10544 | if (status == MagickFalse) |
| 10545 | break; |
| 10546 | } |
| 10547 | } |
| 10548 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10549 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 10550 | else /* Not Palette, Bilevel, or Opaque Monochrome */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10551 | { |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10552 | if ((!mng_info->write_png8 && !mng_info->write_png24 && |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10553 | !mng_info->write_png32) && |
glennrp | 58e0176 | 2011-01-07 15:28:54 +0000 | [diff] [blame] | 10554 | (image_matte != MagickFalse || |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 10555 | (ping_bit_depth >= MAGICKCORE_QUANTUM_DEPTH)) && |
glennrp | 8d57966 | 2011-02-23 02:05:02 +0000 | [diff] [blame] | 10556 | (mng_info->IsPalette) && ping_have_color == MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10557 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 10558 | register const Quantum |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 10559 | *p; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10560 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 10561 | for (pass=0; pass < num_passes; pass++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10562 | { |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 10563 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 10564 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10565 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 10566 | p=GetVirtualPixels(image,0,y,image->columns,1,exception); |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10567 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 10568 | if (p == (const Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10569 | break; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10570 | |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 10571 | if (ping_color_type == PNG_COLOR_TYPE_GRAY) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10572 | { |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 10573 | if (mng_info->IsPalette) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 10574 | (void) ExportQuantumPixels(image,(CacheView *) NULL, |
| 10575 | quantum_info,GrayQuantum,ping_pixels,exception); |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10576 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 10577 | else |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 10578 | (void) ExportQuantumPixels(image,(CacheView *) NULL, |
| 10579 | quantum_info,RedQuantum,ping_pixels,exception); |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10580 | |
glennrp | 3b51f0e | 2010-11-27 18:14:08 +0000 | [diff] [blame] | 10581 | if (logging != MagickFalse && y == 0) |
glennrp | b4a1341 | 2010-05-05 12:47:19 +0000 | [diff] [blame] | 10582 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 10583 | " Writing GRAY PNG pixels (2)"); |
glennrp | b4a1341 | 2010-05-05 12:47:19 +0000 | [diff] [blame] | 10584 | } |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10585 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 10586 | else /* PNG_COLOR_TYPE_GRAY_ALPHA */ |
| 10587 | { |
| 10588 | if (logging != MagickFalse && y == 0) |
| 10589 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10590 | " Writing GRAY_ALPHA PNG pixels (2)"); |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10591 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 10592 | (void) ExportQuantumPixels(image,(CacheView *) NULL, |
| 10593 | quantum_info,GrayAlphaQuantum,ping_pixels,exception); |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 10594 | } |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10595 | |
glennrp | 3b51f0e | 2010-11-27 18:14:08 +0000 | [diff] [blame] | 10596 | if (logging != MagickFalse && y == 0) |
glennrp | b4a1341 | 2010-05-05 12:47:19 +0000 | [diff] [blame] | 10597 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 10598 | " Writing row of pixels (2)"); |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10599 | |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 10600 | png_write_row(ping,ping_pixels); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10601 | } |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10602 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 10603 | if (image->previous == (Image *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10604 | { |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 10605 | status=SetImageProgress(image,LoadImageTag,pass,num_passes); |
| 10606 | if (status == MagickFalse) |
| 10607 | break; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10608 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10609 | } |
| 10610 | } |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 10611 | |
| 10612 | else |
| 10613 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 10614 | register const Quantum |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 10615 | *p; |
| 10616 | |
| 10617 | for (pass=0; pass < num_passes; pass++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10618 | { |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 10619 | if ((image_depth > 8) || (mng_info->write_png24 || |
| 10620 | mng_info->write_png32 || |
| 10621 | (!mng_info->write_png8 && !mng_info->IsPalette))) |
| 10622 | { |
| 10623 | for (y=0; y < (ssize_t) image->rows; y++) |
| 10624 | { |
cristy | 862a33c | 2012-05-17 22:49:37 +0000 | [diff] [blame] | 10625 | p=GetVirtualPixels(image,0,y,image->columns,1, exception); |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10626 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 10627 | if (p == (const Quantum *) NULL) |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 10628 | break; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10629 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 10630 | if (ping_color_type == PNG_COLOR_TYPE_GRAY) |
| 10631 | { |
| 10632 | if (image->storage_class == DirectClass) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 10633 | (void) ExportQuantumPixels(image,(CacheView *) NULL, |
| 10634 | quantum_info,RedQuantum,ping_pixels,exception); |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10635 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 10636 | else |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 10637 | (void) ExportQuantumPixels(image,(CacheView *) NULL, |
| 10638 | quantum_info,GrayQuantum,ping_pixels,exception); |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 10639 | } |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10640 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 10641 | else if (ping_color_type == PNG_COLOR_TYPE_GRAY_ALPHA) |
| 10642 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 10643 | (void) ExportQuantumPixels(image,(CacheView *) NULL, |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 10644 | quantum_info,GrayAlphaQuantum,ping_pixels, |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 10645 | exception); |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10646 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 10647 | if (logging != MagickFalse && y == 0) |
| 10648 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10649 | " Writing GRAY_ALPHA PNG pixels (3)"); |
| 10650 | } |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10651 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 10652 | else if (image_matte != MagickFalse) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 10653 | (void) ExportQuantumPixels(image,(CacheView *) NULL, |
| 10654 | quantum_info,RGBAQuantum,ping_pixels,exception); |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10655 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 10656 | else |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 10657 | (void) ExportQuantumPixels(image,(CacheView *) NULL, |
| 10658 | quantum_info,RGBQuantum,ping_pixels,exception); |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10659 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 10660 | if (logging != MagickFalse && y == 0) |
| 10661 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10662 | " Writing row of pixels (3)"); |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10663 | |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 10664 | png_write_row(ping,ping_pixels); |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 10665 | } |
| 10666 | } |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10667 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 10668 | else |
| 10669 | /* not ((image_depth > 8) || (mng_info->write_png24 || |
| 10670 | mng_info->write_png32 || |
| 10671 | (!mng_info->write_png8 && !mng_info->IsPalette))) */ |
| 10672 | { |
| 10673 | if ((ping_color_type != PNG_COLOR_TYPE_GRAY) && |
| 10674 | (ping_color_type != PNG_COLOR_TYPE_GRAY_ALPHA)) |
| 10675 | { |
| 10676 | if (logging != MagickFalse) |
| 10677 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10678 | " pass %d, Image Is not GRAY or GRAY_ALPHA",pass); |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10679 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 10680 | quantum_info->depth=8; |
| 10681 | image_depth=8; |
| 10682 | } |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10683 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 10684 | for (y=0; y < (ssize_t) image->rows; y++) |
| 10685 | { |
| 10686 | if (logging != MagickFalse && y == 0) |
| 10687 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10688 | " pass %d, Image Is RGB, 16-bit GRAY, or GRAY_ALPHA",pass); |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10689 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 10690 | p=GetVirtualPixels(image,0,y,image->columns,1, exception); |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10691 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 10692 | if (p == (const Quantum *) NULL) |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 10693 | break; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10694 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 10695 | if (ping_color_type == PNG_COLOR_TYPE_GRAY) |
glennrp | 44757ab | 2011-03-17 12:57:03 +0000 | [diff] [blame] | 10696 | { |
glennrp | 4bf8973 | 2011-03-21 13:48:28 +0000 | [diff] [blame] | 10697 | quantum_info->depth=image->depth; |
| 10698 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 10699 | (void) ExportQuantumPixels(image,(CacheView *) NULL, |
| 10700 | quantum_info,GrayQuantum,ping_pixels,exception); |
glennrp | 44757ab | 2011-03-17 12:57:03 +0000 | [diff] [blame] | 10701 | } |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10702 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 10703 | else if (ping_color_type == PNG_COLOR_TYPE_GRAY_ALPHA) |
| 10704 | { |
| 10705 | if (logging != MagickFalse && y == 0) |
| 10706 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10707 | " Writing GRAY_ALPHA PNG pixels (4)"); |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10708 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 10709 | (void) ExportQuantumPixels(image,(CacheView *) NULL, |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 10710 | quantum_info,GrayAlphaQuantum,ping_pixels, |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 10711 | exception); |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 10712 | } |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10713 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 10714 | else |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 10715 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 10716 | (void) ExportQuantumPixels(image,(CacheView *) NULL, |
| 10717 | quantum_info,IndexQuantum,ping_pixels,exception); |
glennrp | 5eae760 | 2011-02-22 15:21:32 +0000 | [diff] [blame] | 10718 | |
| 10719 | if (logging != MagickFalse && y <= 2) |
| 10720 | { |
| 10721 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | 1a7d6db | 2011-03-17 13:24:10 +0000 | [diff] [blame] | 10722 | " Writing row of non-gray pixels (4)"); |
glennrp | 5eae760 | 2011-02-22 15:21:32 +0000 | [diff] [blame] | 10723 | |
| 10724 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10725 | " ping_pixels[0]=%d,ping_pixels[1]=%d", |
| 10726 | (int)ping_pixels[0],(int)ping_pixels[1]); |
| 10727 | } |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 10728 | } |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 10729 | png_write_row(ping,ping_pixels); |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 10730 | } |
| 10731 | } |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10732 | |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 10733 | if (image->previous == (Image *) NULL) |
| 10734 | { |
| 10735 | status=SetImageProgress(image,LoadImageTag,pass,num_passes); |
| 10736 | if (status == MagickFalse) |
| 10737 | break; |
| 10738 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10739 | } |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 10740 | } |
| 10741 | } |
| 10742 | |
cristy | b32b90a | 2009-09-07 21:45:48 +0000 | [diff] [blame] | 10743 | if (quantum_info != (QuantumInfo *) NULL) |
| 10744 | quantum_info=DestroyQuantumInfo(quantum_info); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10745 | |
| 10746 | if (logging != MagickFalse) |
| 10747 | { |
| 10748 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | b4a1341 | 2010-05-05 12:47:19 +0000 | [diff] [blame] | 10749 | " Wrote PNG image data"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10750 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10751 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 10752 | " Width: %.20g",(double) ping_width); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10753 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10754 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 10755 | " Height: %.20g",(double) ping_height); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10756 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10757 | if (mng_info->write_png_depth) |
| 10758 | { |
| 10759 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | 5d6fc9c | 2011-12-27 03:10:42 +0000 | [diff] [blame] | 10760 | " Defined png:bit-depth: %d",mng_info->write_png_depth); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10761 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10762 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10763 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 10764 | " PNG bit-depth written: %d",ping_bit_depth); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10765 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10766 | if (mng_info->write_png_colortype) |
| 10767 | { |
| 10768 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | 5d6fc9c | 2011-12-27 03:10:42 +0000 | [diff] [blame] | 10769 | " Defined png:color-type: %d",mng_info->write_png_colortype-1); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10770 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10771 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10772 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 10773 | " PNG color-type written: %d",ping_color_type); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10774 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10775 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 10776 | " PNG Interlace method: %d",ping_interlace_method); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10777 | } |
| 10778 | /* |
glennrp | a0ed009 | 2011-04-18 16:36:29 +0000 | [diff] [blame] | 10779 | Generate text chunks after IDAT. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10780 | */ |
glennrp | 823b55c | 2011-03-14 18:46:46 +0000 | [diff] [blame] | 10781 | if (ping_exclude_tEXt == MagickFalse || ping_exclude_zTXt == MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10782 | { |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10783 | ResetImagePropertyIterator(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10784 | property=GetNextImageProperty(image); |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10785 | while (property != (const char *) NULL) |
| 10786 | { |
| 10787 | png_textp |
| 10788 | text; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10789 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 10790 | value=GetImageProperty(image,property,exception); |
glennrp | a0ed009 | 2011-04-18 16:36:29 +0000 | [diff] [blame] | 10791 | |
| 10792 | /* Don't write any "png:" properties; those are just for "identify" */ |
| 10793 | if (LocaleNCompare(property,"png:",4) != 0 && |
| 10794 | |
| 10795 | /* Suppress density and units if we wrote a pHYs chunk */ |
| 10796 | (ping_exclude_pHYs != MagickFalse || |
glennrp | 823b55c | 2011-03-14 18:46:46 +0000 | [diff] [blame] | 10797 | LocaleCompare(property,"density") != 0 || |
glennrp | a0ed009 | 2011-04-18 16:36:29 +0000 | [diff] [blame] | 10798 | LocaleCompare(property,"units") != 0) && |
| 10799 | |
| 10800 | /* Suppress the IM-generated Date:create and Date:modify */ |
| 10801 | (ping_exclude_date == MagickFalse || |
| 10802 | LocaleNCompare(property, "Date:",5) != 0)) |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10803 | { |
glennrp | c70af4a | 2011-03-07 00:08:23 +0000 | [diff] [blame] | 10804 | if (value != (const char *) NULL) |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10805 | { |
cristy | a865ccd | 2012-07-28 00:33:10 +0000 | [diff] [blame] | 10806 | |
| 10807 | #if PNG_LIBPNG_VER >= 14000 |
| 10808 | text=(png_textp) png_malloc(ping, |
| 10809 | (png_alloc_size_t) sizeof(png_text)); |
| 10810 | #else |
| 10811 | text=(png_textp) png_malloc(ping,(png_size_t) sizeof(png_text)); |
| 10812 | #endif |
glennrp | c70af4a | 2011-03-07 00:08:23 +0000 | [diff] [blame] | 10813 | text[0].key=(char *) property; |
| 10814 | text[0].text=(char *) value; |
| 10815 | text[0].text_length=strlen(value); |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10816 | |
glennrp | c70af4a | 2011-03-07 00:08:23 +0000 | [diff] [blame] | 10817 | if (ping_exclude_tEXt != MagickFalse) |
| 10818 | text[0].compression=PNG_TEXT_COMPRESSION_zTXt; |
| 10819 | |
| 10820 | else if (ping_exclude_zTXt != MagickFalse) |
| 10821 | text[0].compression=PNG_TEXT_COMPRESSION_NONE; |
| 10822 | |
| 10823 | else |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10824 | { |
glennrp | c70af4a | 2011-03-07 00:08:23 +0000 | [diff] [blame] | 10825 | text[0].compression=image_info->compression == NoCompression || |
| 10826 | (image_info->compression == UndefinedCompression && |
| 10827 | text[0].text_length < 128) ? PNG_TEXT_COMPRESSION_NONE : |
| 10828 | PNG_TEXT_COMPRESSION_zTXt ; |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10829 | } |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 10830 | |
glennrp | c70af4a | 2011-03-07 00:08:23 +0000 | [diff] [blame] | 10831 | if (logging != MagickFalse) |
| 10832 | { |
| 10833 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10834 | " Setting up text chunk"); |
| 10835 | |
| 10836 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10837 | " keyword: %s",text[0].key); |
| 10838 | } |
| 10839 | |
| 10840 | png_set_text(ping,ping_info,text,1); |
| 10841 | png_free(ping,text); |
| 10842 | } |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 10843 | } |
| 10844 | property=GetNextImageProperty(image); |
| 10845 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10846 | } |
| 10847 | |
| 10848 | /* write any PNG-chunk-e profiles */ |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 10849 | (void) Magick_png_write_chunk_from_profile(image,"PNG-chunk-e",logging); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10850 | |
| 10851 | if (logging != MagickFalse) |
| 10852 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10853 | " Writing PNG end info"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10854 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10855 | png_write_end(ping,ping_info); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10856 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10857 | if (mng_info->need_fram && (int) image->dispose == BackgroundDispose) |
| 10858 | { |
| 10859 | if (mng_info->page.x || mng_info->page.y || |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 10860 | (ping_width != mng_info->page.width) || |
| 10861 | (ping_height != mng_info->page.height)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10862 | { |
| 10863 | unsigned char |
| 10864 | chunk[32]; |
| 10865 | |
| 10866 | /* |
| 10867 | Write FRAM 4 with clipping boundaries followed by FRAM 1. |
| 10868 | */ |
| 10869 | (void) WriteBlobMSBULong(image,27L); /* data length=27 */ |
| 10870 | PNGType(chunk,mng_FRAM); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 10871 | LogPNGChunk(logging,mng_FRAM,27L); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10872 | chunk[4]=4; |
| 10873 | chunk[5]=0; /* frame name separator (no name) */ |
| 10874 | chunk[6]=1; /* flag for changing delay, for next frame only */ |
| 10875 | chunk[7]=0; /* flag for changing frame timeout */ |
| 10876 | chunk[8]=1; /* flag for changing frame clipping for next frame */ |
| 10877 | chunk[9]=0; /* flag for changing frame sync_id */ |
| 10878 | PNGLong(chunk+10,(png_uint_32) (0L)); /* temporary 0 delay */ |
| 10879 | chunk[14]=0; /* clipping boundaries delta type */ |
| 10880 | PNGLong(chunk+15,(png_uint_32) (mng_info->page.x)); /* left cb */ |
| 10881 | PNGLong(chunk+19, |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 10882 | (png_uint_32) (mng_info->page.x + ping_width)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10883 | PNGLong(chunk+23,(png_uint_32) (mng_info->page.y)); /* top cb */ |
| 10884 | PNGLong(chunk+27, |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 10885 | (png_uint_32) (mng_info->page.y + ping_height)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10886 | (void) WriteBlob(image,31,chunk); |
| 10887 | (void) WriteBlobMSBULong(image,crc32(0,chunk,31)); |
| 10888 | mng_info->old_framing_mode=4; |
| 10889 | mng_info->framing_mode=1; |
| 10890 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10891 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10892 | else |
| 10893 | mng_info->framing_mode=3; |
| 10894 | } |
| 10895 | if (mng_info->write_mng && !mng_info->need_fram && |
| 10896 | ((int) image->dispose == 3)) |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 10897 | png_error(ping, "Cannot convert GIF with disposal method 3 to MNG-LC"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10898 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10899 | /* |
| 10900 | Free PNG resources. |
| 10901 | */ |
glennrp | 5af765f | 2010-03-30 11:12:18 +0000 | [diff] [blame] | 10902 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10903 | png_destroy_write_struct(&ping,&ping_info); |
| 10904 | |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 10905 | ping_pixels=(unsigned char *) RelinquishMagickMemory(ping_pixels); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10906 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 10907 | if (ping_have_blob != MagickFalse) |
| 10908 | (void) CloseBlob(image); |
| 10909 | |
| 10910 | image_info=DestroyImageInfo(image_info); |
| 10911 | image=DestroyImage(image); |
| 10912 | |
glennrp | b9cfe27 | 2010-12-21 15:08:06 +0000 | [diff] [blame] | 10913 | /* Store bit depth actually written */ |
| 10914 | s[0]=(char) ping_bit_depth; |
| 10915 | s[1]='\0'; |
| 10916 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 10917 | (void) SetImageProperty(IMimage,"png:bit-depth-written",s,exception); |
glennrp | b9cfe27 | 2010-12-21 15:08:06 +0000 | [diff] [blame] | 10918 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10919 | if (logging != MagickFalse) |
| 10920 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 10921 | " exit WriteOnePNGImage()"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 10922 | |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 10923 | #ifdef PNG_SETJMP_NOT_THREAD_SAFE |
| 10924 | UnlockSemaphoreInfo(ping_semaphore); |
| 10925 | #endif |
| 10926 | |
| 10927 | /* } for navigation to beginning of SETJMP-protected block. Revert to |
| 10928 | * Throwing an Exception when an error occurs. |
| 10929 | */ |
| 10930 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10931 | return(MagickTrue); |
| 10932 | /* End write one PNG image */ |
glennrp | edaa038 | 2012-04-12 14:16:21 +0000 | [diff] [blame] | 10933 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10934 | } |
| 10935 | |
| 10936 | /* |
| 10937 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 10938 | % % |
| 10939 | % % |
| 10940 | % % |
| 10941 | % W r i t e P N G I m a g e % |
| 10942 | % % |
| 10943 | % % |
| 10944 | % % |
| 10945 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 10946 | % |
| 10947 | % WritePNGImage() writes a Portable Network Graphics (PNG) or |
| 10948 | % Multiple-image Network Graphics (MNG) image file. |
| 10949 | % |
| 10950 | % MNG support written by Glenn Randers-Pehrson, glennrp@image... |
| 10951 | % |
| 10952 | % The format of the WritePNGImage method is: |
| 10953 | % |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 10954 | % MagickBooleanType WritePNGImage(const ImageInfo *image_info, |
| 10955 | % Image *image,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10956 | % |
| 10957 | % A description of each parameter follows: |
| 10958 | % |
| 10959 | % o image_info: the image info. |
| 10960 | % |
| 10961 | % o image: The image. |
| 10962 | % |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 10963 | % o exception: return any errors or warnings in this structure. |
| 10964 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10965 | % Returns MagickTrue on success, MagickFalse on failure. |
| 10966 | % |
| 10967 | % Communicating with the PNG encoder: |
| 10968 | % |
| 10969 | % While the datastream written is always in PNG format and normally would |
| 10970 | % be given the "png" file extension, this method also writes the following |
cristy | 5d6fc9c | 2011-12-27 03:10:42 +0000 | [diff] [blame] | 10971 | % pseudo-formats which are subsets of png: |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10972 | % |
glennrp | 5a39f37 | 2011-02-25 04:52:16 +0000 | [diff] [blame] | 10973 | % o PNG8: An 8-bit indexed PNG datastream is written. If the image has |
| 10974 | % a depth greater than 8, the depth is reduced. If transparency |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10975 | % is present, the tRNS chunk must only have values 0 and 255 |
| 10976 | % (i.e., transparency is binary: fully opaque or fully |
glennrp | 5a39f37 | 2011-02-25 04:52:16 +0000 | [diff] [blame] | 10977 | % transparent). If other values are present they will be |
| 10978 | % 50%-thresholded to binary transparency. If more than 256 |
glennrp | e9637cb | 2011-03-24 16:34:37 +0000 | [diff] [blame] | 10979 | % colors are present, they will be quantized to the 4-4-4-1, |
glennrp | 130fc45 | 2011-08-20 03:43:18 +0000 | [diff] [blame] | 10980 | % 3-3-3-1, or 3-3-2-1 palette. The underlying RGB color |
| 10981 | % of any resulting fully-transparent pixels is changed to |
| 10982 | % the image's background color. |
glennrp | e9637cb | 2011-03-24 16:34:37 +0000 | [diff] [blame] | 10983 | % |
| 10984 | % If you want better quantization or dithering of the colors |
| 10985 | % or alpha than that, you need to do it before calling the |
glennrp | 5a39f37 | 2011-02-25 04:52:16 +0000 | [diff] [blame] | 10986 | % PNG encoder. The pixels contain 8-bit indices even if |
| 10987 | % they could be represented with 1, 2, or 4 bits. Grayscale |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10988 | % images will be written as indexed PNG files even though the |
glennrp | 5a39f37 | 2011-02-25 04:52:16 +0000 | [diff] [blame] | 10989 | % PNG grayscale type might be slightly more efficient. Please |
| 10990 | % note that writing to the PNG8 format may result in loss |
| 10991 | % of color and alpha data. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 10992 | % |
| 10993 | % o PNG24: An 8-bit per sample RGB PNG datastream is written. The tRNS |
| 10994 | % chunk can be present to convey binary transparency by naming |
glennrp | 5a39f37 | 2011-02-25 04:52:16 +0000 | [diff] [blame] | 10995 | % one of the colors as transparent. The only loss incurred |
| 10996 | % is reduction of sample depth to 8. If the image has more |
| 10997 | % than one transparent color, has semitransparent pixels, or |
| 10998 | % has an opaque pixel with the same RGB components as the |
| 10999 | % transparent color, an image is not written. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11000 | % |
| 11001 | % o PNG32: An 8-bit per sample RGBA PNG is written. Partial |
| 11002 | % transparency is permitted, i.e., the alpha sample for |
| 11003 | % each pixel can have any value from 0 to 255. The alpha |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11004 | % channel is present even if the image is fully opaque. |
glennrp | 5a39f37 | 2011-02-25 04:52:16 +0000 | [diff] [blame] | 11005 | % The only loss in data is the reduction of the sample depth |
| 11006 | % to 8. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11007 | % |
| 11008 | % o -define: For more precise control of the PNG output, you can use the |
| 11009 | % Image options "png:bit-depth" and "png:color-type". These |
| 11010 | % can be set from the commandline with "-define" and also |
glennrp | bb8a733 | 2010-11-13 15:17:35 +0000 | [diff] [blame] | 11011 | % from the application programming interfaces. The options |
| 11012 | % are case-independent and are converted to lowercase before |
| 11013 | % being passed to this encoder. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11014 | % |
| 11015 | % png:color-type can be 0, 2, 3, 4, or 6. |
| 11016 | % |
| 11017 | % When png:color-type is 0 (Grayscale), png:bit-depth can |
| 11018 | % be 1, 2, 4, 8, or 16. |
| 11019 | % |
| 11020 | % When png:color-type is 2 (RGB), png:bit-depth can |
| 11021 | % be 8 or 16. |
| 11022 | % |
| 11023 | % When png:color-type is 3 (Indexed), png:bit-depth can |
| 11024 | % be 1, 2, 4, or 8. This refers to the number of bits |
| 11025 | % used to store the index. The color samples always have |
| 11026 | % bit-depth 8 in indexed PNG files. |
| 11027 | % |
| 11028 | % When png:color-type is 4 (Gray-Matte) or 6 (RGB-Matte), |
| 11029 | % png:bit-depth can be 8 or 16. |
| 11030 | % |
glennrp | 5a39f37 | 2011-02-25 04:52:16 +0000 | [diff] [blame] | 11031 | % If the image cannot be written without loss with the requested bit-depth |
| 11032 | % and color-type, a PNG file will not be written, and the encoder will |
| 11033 | % return MagickFalse. |
| 11034 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11035 | % Since image encoders should not be responsible for the "heavy lifting", |
| 11036 | % the user should make sure that ImageMagick has already reduced the |
| 11037 | % image depth and number of colors and limit transparency to binary |
glennrp | 5a39f37 | 2011-02-25 04:52:16 +0000 | [diff] [blame] | 11038 | % transparency prior to attempting to write the image with depth, color, |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 11039 | % or transparency limitations. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11040 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11041 | % Note that another definition, "png:bit-depth-written" exists, but it |
| 11042 | % is not intended for external use. It is only used internally by the |
| 11043 | % PNG encoder to inform the JNG encoder of the depth of the alpha channel. |
| 11044 | % |
| 11045 | % It is possible to request that the PNG encoder write previously-formatted |
| 11046 | % ancillary chunks in the output PNG file, using the "-profile" commandline |
| 11047 | % option as shown below or by setting the profile via a programming |
| 11048 | % interface: |
| 11049 | % |
| 11050 | % -profile PNG-chunk-x:<file> |
| 11051 | % |
| 11052 | % where x is a location flag and <file> is a file containing the chunk |
| 11053 | % 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] | 11054 | % This encoder will compute the chunk length and CRC, so those must not |
| 11055 | % be included in the file. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11056 | % |
| 11057 | % "x" can be "b" (before PLTE), "m" (middle, i.e., between PLTE and IDAT), |
| 11058 | % or "e" (end, i.e., after IDAT). If you want to write multiple chunks |
| 11059 | % 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] | 11060 | % subsequent profiles from overwriting the preceding ones, e.g., |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11061 | % |
glennrp | bb8a733 | 2010-11-13 15:17:35 +0000 | [diff] [blame] | 11062 | % -profile PNG-chunk-b01:file01 -profile PNG-chunk-b02:file02 |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11063 | % |
glennrp | 3241bd0 | 2010-12-12 04:36:28 +0000 | [diff] [blame] | 11064 | % As of version 6.6.6 the following optimizations are always done: |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11065 | % |
glennrp | d6afd54 | 2010-11-19 01:53:05 +0000 | [diff] [blame] | 11066 | % o 32-bit depth is reduced to 16. |
| 11067 | % o 16-bit depth is reduced to 8 if all pixels contain samples whose |
| 11068 | % high byte and low byte are identical. |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11069 | % o Palette is sorted to remove unused entries and to put a |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 11070 | % transparent color first, if BUILD_PNG_PALETTE is defined. |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11071 | % o Opaque matte channel is removed when writing an indexed PNG. |
glennrp | d6afd54 | 2010-11-19 01:53:05 +0000 | [diff] [blame] | 11072 | % o Grayscale images are reduced to 1, 2, or 4 bit depth if |
| 11073 | % 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] | 11074 | % requested via the "-define png:bit-depth=N" option. |
glennrp | d6afd54 | 2010-11-19 01:53:05 +0000 | [diff] [blame] | 11075 | % o If matte channel is present but only one transparent color is |
| 11076 | % present, RGB+tRNS is written instead of RGBA |
| 11077 | % o Opaque matte channel is removed (or added, if color-type 4 or 6 |
| 11078 | % was requested when converting an opaque image). |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11079 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11080 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 11081 | */ |
| 11082 | static MagickBooleanType WritePNGImage(const ImageInfo *image_info, |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 11083 | Image *image,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11084 | { |
| 11085 | MagickBooleanType |
glennrp | 21f0e62 | 2011-01-07 16:20:57 +0000 | [diff] [blame] | 11086 | excluding, |
| 11087 | logging, |
| 11088 | have_mng_structure, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11089 | status; |
| 11090 | |
| 11091 | MngInfo |
| 11092 | *mng_info; |
| 11093 | |
| 11094 | const char |
| 11095 | *value; |
| 11096 | |
| 11097 | int |
glennrp | 21f0e62 | 2011-01-07 16:20:57 +0000 | [diff] [blame] | 11098 | i, |
glennrp | 5c7cf4e | 2010-12-24 00:30:00 +0000 | [diff] [blame] | 11099 | source; |
| 11100 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11101 | /* |
| 11102 | Open image file. |
| 11103 | */ |
| 11104 | assert(image_info != (const ImageInfo *) NULL); |
| 11105 | assert(image_info->signature == MagickSignature); |
| 11106 | assert(image != (Image *) NULL); |
| 11107 | assert(image->signature == MagickSignature); |
| 11108 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 11109 | logging=LogMagickEvent(CoderEvent,GetMagickModule(),"Enter WritePNGImage()"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11110 | /* |
| 11111 | Allocate a MngInfo structure. |
| 11112 | */ |
| 11113 | have_mng_structure=MagickFalse; |
cristy | 73bd4a5 | 2010-10-05 11:24:23 +0000 | [diff] [blame] | 11114 | mng_info=(MngInfo *) AcquireMagickMemory(sizeof(MngInfo)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11115 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11116 | if (mng_info == (MngInfo *) NULL) |
| 11117 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11118 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11119 | /* |
| 11120 | Initialize members of the MngInfo structure. |
| 11121 | */ |
| 11122 | (void) ResetMagickMemory(mng_info,0,sizeof(MngInfo)); |
| 11123 | mng_info->image=image; |
glennrp | a521b2f | 2010-10-29 04:11:03 +0000 | [diff] [blame] | 11124 | mng_info->equal_backgrounds=MagickTrue; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11125 | have_mng_structure=MagickTrue; |
| 11126 | |
| 11127 | /* See if user has requested a specific PNG subformat */ |
| 11128 | |
| 11129 | mng_info->write_png8=LocaleCompare(image_info->magick,"PNG8") == 0; |
| 11130 | mng_info->write_png24=LocaleCompare(image_info->magick,"PNG24") == 0; |
| 11131 | mng_info->write_png32=LocaleCompare(image_info->magick,"PNG32") == 0; |
| 11132 | |
glennrp | b381a26 | 2012-02-11 17:49:49 +0000 | [diff] [blame] | 11133 | value=GetImageOption(image_info,"png:format"); |
| 11134 | |
| 11135 | if (value != (char *) NULL) |
| 11136 | { |
| 11137 | if (LocaleCompare(value,"png8") == 0) |
| 11138 | { |
| 11139 | mng_info->write_png8 = MagickTrue; |
| 11140 | mng_info->write_png24 = MagickFalse; |
| 11141 | mng_info->write_png32 = MagickFalse; |
| 11142 | } |
| 11143 | |
| 11144 | else if (LocaleCompare(value,"png24") == 0) |
| 11145 | { |
| 11146 | mng_info->write_png8 = MagickFalse; |
| 11147 | mng_info->write_png24 = MagickTrue; |
| 11148 | mng_info->write_png32 = MagickFalse; |
| 11149 | } |
| 11150 | |
| 11151 | else if (LocaleCompare(value,"png32") == 0) |
| 11152 | { |
| 11153 | mng_info->write_png8 = MagickFalse; |
| 11154 | mng_info->write_png24 = MagickFalse; |
| 11155 | mng_info->write_png32 = MagickTrue; |
| 11156 | } |
| 11157 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11158 | if (mng_info->write_png8) |
| 11159 | { |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 11160 | mng_info->write_png_colortype = /* 3 */ 4; |
| 11161 | mng_info->write_png_depth = 8; |
| 11162 | image->depth = 8; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11163 | } |
| 11164 | |
| 11165 | if (mng_info->write_png24) |
| 11166 | { |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 11167 | mng_info->write_png_colortype = /* 2 */ 3; |
| 11168 | mng_info->write_png_depth = 8; |
| 11169 | image->depth = 8; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11170 | |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 11171 | if (image->matte == MagickTrue) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 11172 | (void) SetImageType(image,TrueColorMatteType,exception); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11173 | |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 11174 | else |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 11175 | (void) SetImageType(image,TrueColorType,exception); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11176 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 11177 | (void) SyncImage(image,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11178 | } |
| 11179 | |
| 11180 | if (mng_info->write_png32) |
| 11181 | { |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 11182 | mng_info->write_png_colortype = /* 6 */ 7; |
| 11183 | mng_info->write_png_depth = 8; |
| 11184 | image->depth = 8; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11185 | |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 11186 | if (image->matte == MagickTrue) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 11187 | (void) SetImageType(image,TrueColorMatteType,exception); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11188 | |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 11189 | else |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 11190 | (void) SetImageType(image,TrueColorType,exception); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11191 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 11192 | (void) SyncImage(image,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11193 | } |
| 11194 | |
| 11195 | value=GetImageOption(image_info,"png:bit-depth"); |
glennrp | 8bb3a02 | 2010-12-13 20:40:04 +0000 | [diff] [blame] | 11196 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11197 | if (value != (char *) NULL) |
| 11198 | { |
| 11199 | if (LocaleCompare(value,"1") == 0) |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 11200 | mng_info->write_png_depth = 1; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11201 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11202 | else if (LocaleCompare(value,"2") == 0) |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 11203 | mng_info->write_png_depth = 2; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11204 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11205 | else if (LocaleCompare(value,"4") == 0) |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 11206 | mng_info->write_png_depth = 4; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11207 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11208 | else if (LocaleCompare(value,"8") == 0) |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 11209 | mng_info->write_png_depth = 8; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11210 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11211 | else if (LocaleCompare(value,"16") == 0) |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 11212 | mng_info->write_png_depth = 16; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11213 | |
glennrp | bb8a733 | 2010-11-13 15:17:35 +0000 | [diff] [blame] | 11214 | else |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 11215 | (void) ThrowMagickException(exception, |
glennrp | bb8a733 | 2010-11-13 15:17:35 +0000 | [diff] [blame] | 11216 | GetMagickModule(),CoderWarning, |
| 11217 | "ignoring invalid defined png:bit-depth", |
| 11218 | "=%s",value); |
| 11219 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11220 | if (logging != MagickFalse) |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 11221 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | bb8a733 | 2010-11-13 15:17:35 +0000 | [diff] [blame] | 11222 | " png:bit-depth=%d was defined.\n",mng_info->write_png_depth); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11223 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11224 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11225 | value=GetImageOption(image_info,"png:color-type"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11226 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11227 | if (value != (char *) NULL) |
| 11228 | { |
| 11229 | /* We must store colortype+1 because 0 is a valid colortype */ |
| 11230 | if (LocaleCompare(value,"0") == 0) |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 11231 | mng_info->write_png_colortype = 1; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11232 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 11233 | else if (LocaleCompare(value,"1") == 0) |
| 11234 | mng_info->write_png_colortype = 2; |
| 11235 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11236 | else if (LocaleCompare(value,"2") == 0) |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 11237 | mng_info->write_png_colortype = 3; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11238 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11239 | else if (LocaleCompare(value,"3") == 0) |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 11240 | mng_info->write_png_colortype = 4; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11241 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11242 | else if (LocaleCompare(value,"4") == 0) |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 11243 | mng_info->write_png_colortype = 5; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11244 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11245 | else if (LocaleCompare(value,"6") == 0) |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 11246 | mng_info->write_png_colortype = 7; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11247 | |
glennrp | bb8a733 | 2010-11-13 15:17:35 +0000 | [diff] [blame] | 11248 | else |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 11249 | (void) ThrowMagickException(exception, |
glennrp | bb8a733 | 2010-11-13 15:17:35 +0000 | [diff] [blame] | 11250 | GetMagickModule(),CoderWarning, |
| 11251 | "ignoring invalid defined png:color-type", |
| 11252 | "=%s",value); |
| 11253 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11254 | if (logging != MagickFalse) |
glennrp | 9c1eb07 | 2010-06-06 22:19:15 +0000 | [diff] [blame] | 11255 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | d6bf161 | 2010-12-17 17:28:54 +0000 | [diff] [blame] | 11256 | " png:color-type=%d was defined.\n",mng_info->write_png_colortype-1); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11257 | } |
| 11258 | |
glennrp | 0e8ea19 | 2010-12-24 18:00:33 +0000 | [diff] [blame] | 11259 | /* Check for chunks to be excluded: |
| 11260 | * |
glennrp | 0dff56c | 2011-01-29 19:10:02 +0000 | [diff] [blame] | 11261 | * The default is to not exclude any known chunks except for any |
glennrp | 0e8ea19 | 2010-12-24 18:00:33 +0000 | [diff] [blame] | 11262 | * listed in the "unused_chunks" array, above. |
| 11263 | * |
cristy | 5d6fc9c | 2011-12-27 03:10:42 +0000 | [diff] [blame] | 11264 | * Chunks can be listed for exclusion via a "png:exclude-chunk" |
glennrp | 0e8ea19 | 2010-12-24 18:00:33 +0000 | [diff] [blame] | 11265 | * define (in the image properties or in the image artifacts) |
| 11266 | * or via a mng_info member. For convenience, in addition |
| 11267 | * to or instead of a comma-separated list of chunks, the |
| 11268 | * "exclude-chunk" string can be simply "all" or "none". |
| 11269 | * |
| 11270 | * The exclude-chunk define takes priority over the mng_info. |
| 11271 | * |
cristy | 5d6fc9c | 2011-12-27 03:10:42 +0000 | [diff] [blame] | 11272 | * A "png:include-chunk" define takes priority over both the |
| 11273 | * mng_info and the "png:exclude-chunk" define. Like the |
glennrp | 0e8ea19 | 2010-12-24 18:00:33 +0000 | [diff] [blame] | 11274 | * "exclude-chunk" string, it can define "all" or "none" as |
glennrp | 0dff56c | 2011-01-29 19:10:02 +0000 | [diff] [blame] | 11275 | * well as a comma-separated list. Chunks that are unknown to |
| 11276 | * ImageMagick are always excluded, regardless of their "copy-safe" |
| 11277 | * status according to the PNG specification, and even if they |
glennrp | aa192b1 | 2012-01-17 21:35:21 +0000 | [diff] [blame] | 11278 | * appear in the "include-chunk" list. Such defines appearing among |
| 11279 | * the image options take priority over those found among the image |
| 11280 | * artifacts. |
glennrp | 0e8ea19 | 2010-12-24 18:00:33 +0000 | [diff] [blame] | 11281 | * |
| 11282 | * Finally, all chunks listed in the "unused_chunks" array are |
| 11283 | * automatically excluded, regardless of the other instructions |
| 11284 | * or lack thereof. |
| 11285 | * |
| 11286 | * if you exclude sRGB but not gAMA (recommended), then sRGB chunk |
| 11287 | * will not be written and the gAMA chunk will only be written if it |
| 11288 | * is not between .45 and .46, or approximately (1.0/2.2). |
| 11289 | * |
| 11290 | * If you exclude tRNS and the image has transparency, the colortype |
| 11291 | * is forced to be 4 or 6 (GRAY_ALPHA or RGB_ALPHA). |
| 11292 | * |
| 11293 | * The -strip option causes StripImage() to set the png:include-chunk |
glennrp | 104f206 | 2011-08-20 05:08:53 +0000 | [diff] [blame] | 11294 | * artifact to "none,trns,gama". |
glennrp | 0e8ea19 | 2010-12-24 18:00:33 +0000 | [diff] [blame] | 11295 | */ |
| 11296 | |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 11297 | mng_info->ping_exclude_bKGD=MagickFalse; |
| 11298 | mng_info->ping_exclude_cHRM=MagickFalse; |
glennrp | a0ed009 | 2011-04-18 16:36:29 +0000 | [diff] [blame] | 11299 | mng_info->ping_exclude_date=MagickFalse; |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 11300 | mng_info->ping_exclude_EXIF=MagickFalse; /* hex-encoded EXIF in zTXt */ |
| 11301 | mng_info->ping_exclude_gAMA=MagickFalse; |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 11302 | mng_info->ping_exclude_iCCP=MagickFalse; |
| 11303 | /* mng_info->ping_exclude_iTXt=MagickFalse; */ |
| 11304 | mng_info->ping_exclude_oFFs=MagickFalse; |
| 11305 | mng_info->ping_exclude_pHYs=MagickFalse; |
| 11306 | mng_info->ping_exclude_sRGB=MagickFalse; |
| 11307 | mng_info->ping_exclude_tEXt=MagickFalse; |
glennrp | a1e3b7b | 2010-12-24 16:37:33 +0000 | [diff] [blame] | 11308 | mng_info->ping_exclude_tRNS=MagickFalse; |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 11309 | mng_info->ping_exclude_vpAg=MagickFalse; |
| 11310 | mng_info->ping_exclude_zCCP=MagickFalse; /* hex-encoded iCCP in zTXt */ |
| 11311 | mng_info->ping_exclude_zTXt=MagickFalse; |
| 11312 | |
glennrp | 8d3d6e5 | 2011-04-19 04:39:51 +0000 | [diff] [blame] | 11313 | mng_info->ping_preserve_colormap=MagickFalse; |
| 11314 | |
| 11315 | value=GetImageArtifact(image,"png:preserve-colormap"); |
| 11316 | if (value == NULL) |
| 11317 | value=GetImageOption(image_info,"png:preserve-colormap"); |
| 11318 | if (value != NULL) |
| 11319 | mng_info->ping_preserve_colormap=MagickTrue; |
| 11320 | |
glennrp | 1868258 | 2011-06-30 18:11:47 +0000 | [diff] [blame] | 11321 | /* Thes compression-level, compression-strategy, and compression-filter |
| 11322 | * defines take precedence over values from the -quality option. |
| 11323 | */ |
| 11324 | value=GetImageArtifact(image,"png:compression-level"); |
| 11325 | if (value == NULL) |
| 11326 | value=GetImageOption(image_info,"png:compression-level"); |
| 11327 | if (value != NULL) |
| 11328 | { |
glennrp | 1868258 | 2011-06-30 18:11:47 +0000 | [diff] [blame] | 11329 | /* We have to add 1 to everything because 0 is a valid input, |
| 11330 | * and we want to use 0 (the default) to mean undefined. |
| 11331 | */ |
| 11332 | if (LocaleCompare(value,"0") == 0) |
| 11333 | mng_info->write_png_compression_level = 1; |
| 11334 | |
glennrp | 0ffb95c | 2012-01-30 21:16:22 +0000 | [diff] [blame] | 11335 | else if (LocaleCompare(value,"1") == 0) |
glennrp | 1868258 | 2011-06-30 18:11:47 +0000 | [diff] [blame] | 11336 | mng_info->write_png_compression_level = 2; |
| 11337 | |
| 11338 | else if (LocaleCompare(value,"2") == 0) |
| 11339 | mng_info->write_png_compression_level = 3; |
| 11340 | |
| 11341 | else if (LocaleCompare(value,"3") == 0) |
| 11342 | mng_info->write_png_compression_level = 4; |
| 11343 | |
| 11344 | else if (LocaleCompare(value,"4") == 0) |
| 11345 | mng_info->write_png_compression_level = 5; |
| 11346 | |
| 11347 | else if (LocaleCompare(value,"5") == 0) |
| 11348 | mng_info->write_png_compression_level = 6; |
| 11349 | |
| 11350 | else if (LocaleCompare(value,"6") == 0) |
| 11351 | mng_info->write_png_compression_level = 7; |
| 11352 | |
| 11353 | else if (LocaleCompare(value,"7") == 0) |
| 11354 | mng_info->write_png_compression_level = 8; |
| 11355 | |
| 11356 | else if (LocaleCompare(value,"8") == 0) |
| 11357 | mng_info->write_png_compression_level = 9; |
| 11358 | |
| 11359 | else if (LocaleCompare(value,"9") == 0) |
| 11360 | mng_info->write_png_compression_level = 10; |
| 11361 | |
| 11362 | else |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 11363 | (void) ThrowMagickException(exception, |
glennrp | 1868258 | 2011-06-30 18:11:47 +0000 | [diff] [blame] | 11364 | GetMagickModule(),CoderWarning, |
| 11365 | "ignoring invalid defined png:compression-level", |
| 11366 | "=%s",value); |
| 11367 | } |
| 11368 | |
| 11369 | value=GetImageArtifact(image,"png:compression-strategy"); |
| 11370 | if (value == NULL) |
| 11371 | value=GetImageOption(image_info,"png:compression-strategy"); |
| 11372 | if (value != NULL) |
| 11373 | { |
| 11374 | |
| 11375 | if (LocaleCompare(value,"0") == 0) |
| 11376 | mng_info->write_png_compression_strategy = Z_DEFAULT_STRATEGY+1; |
| 11377 | |
| 11378 | else if (LocaleCompare(value,"1") == 0) |
| 11379 | mng_info->write_png_compression_strategy = Z_FILTERED+1; |
| 11380 | |
| 11381 | else if (LocaleCompare(value,"2") == 0) |
| 11382 | mng_info->write_png_compression_strategy = Z_HUFFMAN_ONLY+1; |
| 11383 | |
| 11384 | else if (LocaleCompare(value,"3") == 0) |
glennrp | 98c07ad | 2011-07-01 02:52:59 +0000 | [diff] [blame] | 11385 | #ifdef Z_RLE /* Z_RLE was added to zlib-1.2.0 */ |
glennrp | 1868258 | 2011-06-30 18:11:47 +0000 | [diff] [blame] | 11386 | mng_info->write_png_compression_strategy = Z_RLE+1; |
glennrp | 98c07ad | 2011-07-01 02:52:59 +0000 | [diff] [blame] | 11387 | #else |
| 11388 | mng_info->write_png_compression_strategy = Z_DEFAULT_STRATEGY+1; |
| 11389 | #endif |
glennrp | 1868258 | 2011-06-30 18:11:47 +0000 | [diff] [blame] | 11390 | |
| 11391 | else if (LocaleCompare(value,"4") == 0) |
glennrp | 98c07ad | 2011-07-01 02:52:59 +0000 | [diff] [blame] | 11392 | #ifdef Z_FIXED /* Z_FIXED was added to zlib-1.2.2.2 */ |
glennrp | 1868258 | 2011-06-30 18:11:47 +0000 | [diff] [blame] | 11393 | mng_info->write_png_compression_strategy = Z_FIXED+1; |
glennrp | 98c07ad | 2011-07-01 02:52:59 +0000 | [diff] [blame] | 11394 | #else |
| 11395 | mng_info->write_png_compression_strategy = Z_DEFAULT_STRATEGY+1; |
| 11396 | #endif |
glennrp | 1868258 | 2011-06-30 18:11:47 +0000 | [diff] [blame] | 11397 | |
| 11398 | else |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 11399 | (void) ThrowMagickException(exception, |
glennrp | 1868258 | 2011-06-30 18:11:47 +0000 | [diff] [blame] | 11400 | GetMagickModule(),CoderWarning, |
| 11401 | "ignoring invalid defined png:compression-strategy", |
| 11402 | "=%s",value); |
| 11403 | } |
| 11404 | |
| 11405 | value=GetImageArtifact(image,"png:compression-filter"); |
| 11406 | if (value == NULL) |
| 11407 | value=GetImageOption(image_info,"png:compression-filter"); |
| 11408 | if (value != NULL) |
| 11409 | { |
| 11410 | |
| 11411 | /* To do: combinations of filters allowed by libpng |
| 11412 | * masks 0x08 through 0xf8 |
| 11413 | * |
| 11414 | * Implement this as a comma-separated list of 0,1,2,3,4,5 |
| 11415 | * where 5 is a special case meaning PNG_ALL_FILTERS. |
| 11416 | */ |
| 11417 | |
| 11418 | if (LocaleCompare(value,"0") == 0) |
| 11419 | mng_info->write_png_compression_filter = 1; |
| 11420 | |
| 11421 | if (LocaleCompare(value,"1") == 0) |
| 11422 | mng_info->write_png_compression_filter = 2; |
| 11423 | |
| 11424 | else if (LocaleCompare(value,"2") == 0) |
| 11425 | mng_info->write_png_compression_filter = 3; |
| 11426 | |
| 11427 | else if (LocaleCompare(value,"3") == 0) |
| 11428 | mng_info->write_png_compression_filter = 4; |
| 11429 | |
| 11430 | else if (LocaleCompare(value,"4") == 0) |
| 11431 | mng_info->write_png_compression_filter = 5; |
| 11432 | |
| 11433 | else if (LocaleCompare(value,"5") == 0) |
| 11434 | mng_info->write_png_compression_filter = 6; |
| 11435 | |
glennrp | 1868258 | 2011-06-30 18:11:47 +0000 | [diff] [blame] | 11436 | else |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 11437 | (void) ThrowMagickException(exception, |
glennrp | 1868258 | 2011-06-30 18:11:47 +0000 | [diff] [blame] | 11438 | GetMagickModule(),CoderWarning, |
| 11439 | "ignoring invalid defined png:compression-filter", |
| 11440 | "=%s",value); |
| 11441 | } |
| 11442 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11443 | excluding=MagickFalse; |
| 11444 | |
glennrp | 5c7cf4e | 2010-12-24 00:30:00 +0000 | [diff] [blame] | 11445 | for (source=0; source<1; source++) |
| 11446 | { |
| 11447 | if (source==0) |
glennrp | acba004 | 2010-12-24 14:27:26 +0000 | [diff] [blame] | 11448 | { |
glennrp | 5c7cf4e | 2010-12-24 00:30:00 +0000 | [diff] [blame] | 11449 | value=GetImageArtifact(image,"png:exclude-chunk"); |
glennrp | acba004 | 2010-12-24 14:27:26 +0000 | [diff] [blame] | 11450 | |
| 11451 | if (value == NULL) |
| 11452 | value=GetImageArtifact(image,"png:exclude-chunks"); |
| 11453 | } |
glennrp | 5c7cf4e | 2010-12-24 00:30:00 +0000 | [diff] [blame] | 11454 | else |
glennrp | acba004 | 2010-12-24 14:27:26 +0000 | [diff] [blame] | 11455 | { |
glennrp | 5c7cf4e | 2010-12-24 00:30:00 +0000 | [diff] [blame] | 11456 | value=GetImageOption(image_info,"png:exclude-chunk"); |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 11457 | |
glennrp | acba004 | 2010-12-24 14:27:26 +0000 | [diff] [blame] | 11458 | if (value == NULL) |
| 11459 | value=GetImageOption(image_info,"png:exclude-chunks"); |
| 11460 | } |
| 11461 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11462 | if (value != NULL) |
glennrp | ce91ed5 | 2010-12-23 22:37:49 +0000 | [diff] [blame] | 11463 | { |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11464 | |
| 11465 | size_t |
| 11466 | last; |
| 11467 | |
| 11468 | excluding=MagickTrue; |
| 11469 | |
| 11470 | if (logging != MagickFalse) |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11471 | { |
| 11472 | if (source == 0) |
| 11473 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 11474 | " png:exclude-chunk=%s found in image artifacts.\n", value); |
| 11475 | else |
| 11476 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 11477 | " png:exclude-chunk=%s found in image properties.\n", value); |
| 11478 | } |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11479 | |
| 11480 | last=strlen(value); |
| 11481 | |
| 11482 | for (i=0; i<(int) last; i+=5) |
glennrp | ce91ed5 | 2010-12-23 22:37:49 +0000 | [diff] [blame] | 11483 | { |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11484 | |
| 11485 | if (LocaleNCompare(value+i,"all",3) == 0) |
| 11486 | { |
| 11487 | mng_info->ping_exclude_bKGD=MagickTrue; |
| 11488 | mng_info->ping_exclude_cHRM=MagickTrue; |
glennrp | a0ed009 | 2011-04-18 16:36:29 +0000 | [diff] [blame] | 11489 | mng_info->ping_exclude_date=MagickTrue; |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11490 | mng_info->ping_exclude_EXIF=MagickTrue; |
| 11491 | mng_info->ping_exclude_gAMA=MagickTrue; |
| 11492 | mng_info->ping_exclude_iCCP=MagickTrue; |
| 11493 | /* mng_info->ping_exclude_iTXt=MagickTrue; */ |
| 11494 | mng_info->ping_exclude_oFFs=MagickTrue; |
| 11495 | mng_info->ping_exclude_pHYs=MagickTrue; |
| 11496 | mng_info->ping_exclude_sRGB=MagickTrue; |
| 11497 | mng_info->ping_exclude_tEXt=MagickTrue; |
glennrp | a1e3b7b | 2010-12-24 16:37:33 +0000 | [diff] [blame] | 11498 | mng_info->ping_exclude_tRNS=MagickTrue; |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11499 | mng_info->ping_exclude_vpAg=MagickTrue; |
| 11500 | mng_info->ping_exclude_zCCP=MagickTrue; |
| 11501 | mng_info->ping_exclude_zTXt=MagickTrue; |
| 11502 | i--; |
| 11503 | } |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11504 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11505 | if (LocaleNCompare(value+i,"none",4) == 0) |
| 11506 | { |
| 11507 | mng_info->ping_exclude_bKGD=MagickFalse; |
| 11508 | mng_info->ping_exclude_cHRM=MagickFalse; |
glennrp | a0ed009 | 2011-04-18 16:36:29 +0000 | [diff] [blame] | 11509 | mng_info->ping_exclude_date=MagickFalse; |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11510 | mng_info->ping_exclude_EXIF=MagickFalse; |
| 11511 | mng_info->ping_exclude_gAMA=MagickFalse; |
| 11512 | mng_info->ping_exclude_iCCP=MagickFalse; |
| 11513 | /* mng_info->ping_exclude_iTXt=MagickFalse; */ |
| 11514 | mng_info->ping_exclude_oFFs=MagickFalse; |
| 11515 | mng_info->ping_exclude_pHYs=MagickFalse; |
| 11516 | mng_info->ping_exclude_sRGB=MagickFalse; |
| 11517 | mng_info->ping_exclude_tEXt=MagickFalse; |
glennrp | a1e3b7b | 2010-12-24 16:37:33 +0000 | [diff] [blame] | 11518 | mng_info->ping_exclude_tRNS=MagickFalse; |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11519 | mng_info->ping_exclude_vpAg=MagickFalse; |
| 11520 | mng_info->ping_exclude_zCCP=MagickFalse; |
| 11521 | mng_info->ping_exclude_zTXt=MagickFalse; |
| 11522 | } |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11523 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11524 | if (LocaleNCompare(value+i,"bkgd",4) == 0) |
| 11525 | mng_info->ping_exclude_bKGD=MagickTrue; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11526 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11527 | if (LocaleNCompare(value+i,"chrm",4) == 0) |
| 11528 | mng_info->ping_exclude_cHRM=MagickTrue; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11529 | |
glennrp | a0ed009 | 2011-04-18 16:36:29 +0000 | [diff] [blame] | 11530 | if (LocaleNCompare(value+i,"date",4) == 0) |
| 11531 | mng_info->ping_exclude_date=MagickTrue; |
| 11532 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11533 | if (LocaleNCompare(value+i,"exif",4) == 0) |
| 11534 | mng_info->ping_exclude_EXIF=MagickTrue; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11535 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11536 | if (LocaleNCompare(value+i,"gama",4) == 0) |
| 11537 | mng_info->ping_exclude_gAMA=MagickTrue; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11538 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11539 | if (LocaleNCompare(value+i,"iccp",4) == 0) |
| 11540 | mng_info->ping_exclude_iCCP=MagickTrue; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11541 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11542 | /* |
| 11543 | if (LocaleNCompare(value+i,"itxt",4) == 0) |
| 11544 | mng_info->ping_exclude_iTXt=MagickTrue; |
| 11545 | */ |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11546 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11547 | if (LocaleNCompare(value+i,"gama",4) == 0) |
| 11548 | mng_info->ping_exclude_gAMA=MagickTrue; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11549 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11550 | if (LocaleNCompare(value+i,"offs",4) == 0) |
| 11551 | mng_info->ping_exclude_oFFs=MagickTrue; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11552 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11553 | if (LocaleNCompare(value+i,"phys",4) == 0) |
| 11554 | mng_info->ping_exclude_pHYs=MagickTrue; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11555 | |
glennrp | a1e3b7b | 2010-12-24 16:37:33 +0000 | [diff] [blame] | 11556 | if (LocaleNCompare(value+i,"srgb",4) == 0) |
| 11557 | mng_info->ping_exclude_sRGB=MagickTrue; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11558 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11559 | if (LocaleNCompare(value+i,"text",4) == 0) |
| 11560 | mng_info->ping_exclude_tEXt=MagickTrue; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11561 | |
glennrp | a1e3b7b | 2010-12-24 16:37:33 +0000 | [diff] [blame] | 11562 | if (LocaleNCompare(value+i,"trns",4) == 0) |
| 11563 | mng_info->ping_exclude_tRNS=MagickTrue; |
| 11564 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11565 | if (LocaleNCompare(value+i,"vpag",4) == 0) |
| 11566 | mng_info->ping_exclude_vpAg=MagickTrue; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11567 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11568 | if (LocaleNCompare(value+i,"zccp",4) == 0) |
| 11569 | mng_info->ping_exclude_zCCP=MagickTrue; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11570 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11571 | if (LocaleNCompare(value+i,"ztxt",4) == 0) |
| 11572 | mng_info->ping_exclude_zTXt=MagickTrue; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11573 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11574 | } |
glennrp | ce91ed5 | 2010-12-23 22:37:49 +0000 | [diff] [blame] | 11575 | } |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 11576 | } |
| 11577 | |
glennrp | 5c7cf4e | 2010-12-24 00:30:00 +0000 | [diff] [blame] | 11578 | for (source=0; source<1; source++) |
| 11579 | { |
| 11580 | if (source==0) |
glennrp | acba004 | 2010-12-24 14:27:26 +0000 | [diff] [blame] | 11581 | { |
glennrp | 5c7cf4e | 2010-12-24 00:30:00 +0000 | [diff] [blame] | 11582 | value=GetImageArtifact(image,"png:include-chunk"); |
glennrp | acba004 | 2010-12-24 14:27:26 +0000 | [diff] [blame] | 11583 | |
| 11584 | if (value == NULL) |
| 11585 | value=GetImageArtifact(image,"png:include-chunks"); |
| 11586 | } |
glennrp | 5c7cf4e | 2010-12-24 00:30:00 +0000 | [diff] [blame] | 11587 | else |
glennrp | acba004 | 2010-12-24 14:27:26 +0000 | [diff] [blame] | 11588 | { |
glennrp | 5c7cf4e | 2010-12-24 00:30:00 +0000 | [diff] [blame] | 11589 | value=GetImageOption(image_info,"png:include-chunk"); |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 11590 | |
glennrp | acba004 | 2010-12-24 14:27:26 +0000 | [diff] [blame] | 11591 | if (value == NULL) |
| 11592 | value=GetImageOption(image_info,"png:include-chunks"); |
| 11593 | } |
| 11594 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11595 | if (value != NULL) |
| 11596 | { |
| 11597 | size_t |
| 11598 | last; |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 11599 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11600 | excluding=MagickTrue; |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 11601 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11602 | if (logging != MagickFalse) |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11603 | { |
| 11604 | if (source == 0) |
| 11605 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 11606 | " png:include-chunk=%s found in image artifacts.\n", value); |
| 11607 | else |
| 11608 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 11609 | " png:include-chunk=%s found in image properties.\n", value); |
| 11610 | } |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11611 | |
| 11612 | last=strlen(value); |
| 11613 | |
| 11614 | for (i=0; i<(int) last; i+=5) |
| 11615 | { |
| 11616 | if (LocaleNCompare(value+i,"all",3) == 0) |
| 11617 | { |
| 11618 | mng_info->ping_exclude_bKGD=MagickFalse; |
| 11619 | mng_info->ping_exclude_cHRM=MagickFalse; |
glennrp | a0ed009 | 2011-04-18 16:36:29 +0000 | [diff] [blame] | 11620 | mng_info->ping_exclude_date=MagickFalse; |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11621 | mng_info->ping_exclude_EXIF=MagickFalse; |
| 11622 | mng_info->ping_exclude_gAMA=MagickFalse; |
| 11623 | mng_info->ping_exclude_iCCP=MagickFalse; |
| 11624 | /* mng_info->ping_exclude_iTXt=MagickFalse; */ |
| 11625 | mng_info->ping_exclude_oFFs=MagickFalse; |
| 11626 | mng_info->ping_exclude_pHYs=MagickFalse; |
| 11627 | mng_info->ping_exclude_sRGB=MagickFalse; |
| 11628 | mng_info->ping_exclude_tEXt=MagickFalse; |
glennrp | a1e3b7b | 2010-12-24 16:37:33 +0000 | [diff] [blame] | 11629 | mng_info->ping_exclude_tRNS=MagickFalse; |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11630 | mng_info->ping_exclude_vpAg=MagickFalse; |
| 11631 | mng_info->ping_exclude_zCCP=MagickFalse; |
| 11632 | mng_info->ping_exclude_zTXt=MagickFalse; |
| 11633 | i--; |
| 11634 | } |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11635 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11636 | if (LocaleNCompare(value+i,"none",4) == 0) |
| 11637 | { |
| 11638 | mng_info->ping_exclude_bKGD=MagickTrue; |
| 11639 | mng_info->ping_exclude_cHRM=MagickTrue; |
glennrp | a0ed009 | 2011-04-18 16:36:29 +0000 | [diff] [blame] | 11640 | mng_info->ping_exclude_date=MagickTrue; |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11641 | mng_info->ping_exclude_EXIF=MagickTrue; |
| 11642 | mng_info->ping_exclude_gAMA=MagickTrue; |
| 11643 | mng_info->ping_exclude_iCCP=MagickTrue; |
| 11644 | /* mng_info->ping_exclude_iTXt=MagickTrue; */ |
| 11645 | mng_info->ping_exclude_oFFs=MagickTrue; |
| 11646 | mng_info->ping_exclude_pHYs=MagickTrue; |
| 11647 | mng_info->ping_exclude_sRGB=MagickTrue; |
| 11648 | mng_info->ping_exclude_tEXt=MagickTrue; |
glennrp | a1e3b7b | 2010-12-24 16:37:33 +0000 | [diff] [blame] | 11649 | mng_info->ping_exclude_tRNS=MagickTrue; |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11650 | mng_info->ping_exclude_vpAg=MagickTrue; |
| 11651 | mng_info->ping_exclude_zCCP=MagickTrue; |
| 11652 | mng_info->ping_exclude_zTXt=MagickTrue; |
| 11653 | } |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11654 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11655 | if (LocaleNCompare(value+i,"bkgd",4) == 0) |
| 11656 | mng_info->ping_exclude_bKGD=MagickFalse; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11657 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11658 | if (LocaleNCompare(value+i,"chrm",4) == 0) |
| 11659 | mng_info->ping_exclude_cHRM=MagickFalse; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11660 | |
glennrp | a0ed009 | 2011-04-18 16:36:29 +0000 | [diff] [blame] | 11661 | if (LocaleNCompare(value+i,"date",4) == 0) |
| 11662 | mng_info->ping_exclude_date=MagickFalse; |
| 11663 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11664 | if (LocaleNCompare(value+i,"exif",4) == 0) |
| 11665 | mng_info->ping_exclude_EXIF=MagickFalse; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11666 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11667 | if (LocaleNCompare(value+i,"gama",4) == 0) |
| 11668 | mng_info->ping_exclude_gAMA=MagickFalse; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11669 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11670 | if (LocaleNCompare(value+i,"iccp",4) == 0) |
| 11671 | mng_info->ping_exclude_iCCP=MagickFalse; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11672 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11673 | /* |
| 11674 | if (LocaleNCompare(value+i,"itxt",4) == 0) |
| 11675 | mng_info->ping_exclude_iTXt=MagickFalse; |
| 11676 | */ |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11677 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11678 | if (LocaleNCompare(value+i,"gama",4) == 0) |
| 11679 | mng_info->ping_exclude_gAMA=MagickFalse; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11680 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11681 | if (LocaleNCompare(value+i,"offs",4) == 0) |
| 11682 | mng_info->ping_exclude_oFFs=MagickFalse; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11683 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11684 | if (LocaleNCompare(value+i,"phys",4) == 0) |
| 11685 | mng_info->ping_exclude_pHYs=MagickFalse; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11686 | |
glennrp | a1e3b7b | 2010-12-24 16:37:33 +0000 | [diff] [blame] | 11687 | if (LocaleNCompare(value+i,"srgb",4) == 0) |
| 11688 | mng_info->ping_exclude_sRGB=MagickFalse; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11689 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11690 | if (LocaleNCompare(value+i,"text",4) == 0) |
| 11691 | mng_info->ping_exclude_tEXt=MagickFalse; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11692 | |
glennrp | a1e3b7b | 2010-12-24 16:37:33 +0000 | [diff] [blame] | 11693 | if (LocaleNCompare(value+i,"trns",4) == 0) |
| 11694 | mng_info->ping_exclude_tRNS=MagickFalse; |
| 11695 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11696 | if (LocaleNCompare(value+i,"vpag",4) == 0) |
| 11697 | mng_info->ping_exclude_vpAg=MagickFalse; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11698 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11699 | if (LocaleNCompare(value+i,"zccp",4) == 0) |
| 11700 | mng_info->ping_exclude_zCCP=MagickFalse; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11701 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11702 | if (LocaleNCompare(value+i,"ztxt",4) == 0) |
| 11703 | mng_info->ping_exclude_zTXt=MagickFalse; |
glennrp | 2cc891a | 2010-12-24 13:44:32 +0000 | [diff] [blame] | 11704 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11705 | } |
glennrp | ce91ed5 | 2010-12-23 22:37:49 +0000 | [diff] [blame] | 11706 | } |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 11707 | } |
| 11708 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11709 | if (excluding != MagickFalse && logging != MagickFalse) |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 11710 | { |
| 11711 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | 5d6fc9c | 2011-12-27 03:10:42 +0000 | [diff] [blame] | 11712 | " Chunks to be excluded from the output png:"); |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 11713 | if (mng_info->ping_exclude_bKGD != MagickFalse) |
| 11714 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 11715 | " bKGD"); |
| 11716 | if (mng_info->ping_exclude_cHRM != MagickFalse) |
| 11717 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 11718 | " cHRM"); |
glennrp | a0ed009 | 2011-04-18 16:36:29 +0000 | [diff] [blame] | 11719 | if (mng_info->ping_exclude_date != MagickFalse) |
| 11720 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 11721 | " date"); |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 11722 | if (mng_info->ping_exclude_EXIF != MagickFalse) |
| 11723 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 11724 | " EXIF"); |
| 11725 | if (mng_info->ping_exclude_gAMA != MagickFalse) |
| 11726 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 11727 | " gAMA"); |
| 11728 | if (mng_info->ping_exclude_iCCP != MagickFalse) |
| 11729 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 11730 | " iCCP"); |
| 11731 | /* |
| 11732 | if (mng_info->ping_exclude_iTXt != MagickFalse) |
| 11733 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 11734 | " iTXt"); |
| 11735 | */ |
| 11736 | if (mng_info->ping_exclude_oFFs != MagickFalse) |
| 11737 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 11738 | " oFFs"); |
| 11739 | if (mng_info->ping_exclude_pHYs != MagickFalse) |
| 11740 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 11741 | " pHYs"); |
| 11742 | if (mng_info->ping_exclude_sRGB != MagickFalse) |
| 11743 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 11744 | " sRGB"); |
| 11745 | if (mng_info->ping_exclude_tEXt != MagickFalse) |
| 11746 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 11747 | " tEXt"); |
glennrp | a1e3b7b | 2010-12-24 16:37:33 +0000 | [diff] [blame] | 11748 | if (mng_info->ping_exclude_tRNS != MagickFalse) |
| 11749 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 11750 | " tRNS"); |
glennrp | 26f3791 | 2010-12-23 16:22:42 +0000 | [diff] [blame] | 11751 | if (mng_info->ping_exclude_vpAg != MagickFalse) |
| 11752 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 11753 | " vpAg"); |
| 11754 | if (mng_info->ping_exclude_zCCP != MagickFalse) |
| 11755 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 11756 | " zCCP"); |
| 11757 | if (mng_info->ping_exclude_zTXt != MagickFalse) |
| 11758 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 11759 | " zTXt"); |
| 11760 | } |
| 11761 | |
glennrp | b9cfe27 | 2010-12-21 15:08:06 +0000 | [diff] [blame] | 11762 | mng_info->need_blob = MagickTrue; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11763 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 11764 | status=WriteOnePNGImage(mng_info,image_info,image,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11765 | |
| 11766 | MngInfoFreeStruct(mng_info,&have_mng_structure); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11767 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11768 | if (logging != MagickFalse) |
| 11769 | (void) LogMagickEvent(CoderEvent,GetMagickModule(),"exit WritePNGImage()"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11770 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11771 | return(status); |
| 11772 | } |
| 11773 | |
| 11774 | #if defined(JNG_SUPPORTED) |
| 11775 | |
| 11776 | /* Write one JNG image */ |
| 11777 | static MagickBooleanType WriteOneJNGImage(MngInfo *mng_info, |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 11778 | const ImageInfo *image_info,Image *image,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11779 | { |
| 11780 | Image |
| 11781 | *jpeg_image; |
| 11782 | |
| 11783 | ImageInfo |
| 11784 | *jpeg_image_info; |
| 11785 | |
| 11786 | MagickBooleanType |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11787 | logging, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11788 | status; |
| 11789 | |
| 11790 | size_t |
| 11791 | length; |
| 11792 | |
| 11793 | unsigned char |
| 11794 | *blob, |
| 11795 | chunk[80], |
| 11796 | *p; |
| 11797 | |
| 11798 | unsigned int |
| 11799 | jng_alpha_compression_method, |
| 11800 | jng_alpha_sample_depth, |
| 11801 | jng_color_type, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11802 | transparent; |
| 11803 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 11804 | size_t |
glennrp | 59575fa | 2011-12-31 21:31:39 +0000 | [diff] [blame] | 11805 | jng_alpha_quality, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11806 | jng_quality; |
| 11807 | |
| 11808 | logging=LogMagickEvent(CoderEvent,GetMagickModule(), |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 11809 | " Enter WriteOneJNGImage()"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11810 | |
| 11811 | blob=(unsigned char *) NULL; |
| 11812 | jpeg_image=(Image *) NULL; |
| 11813 | jpeg_image_info=(ImageInfo *) NULL; |
| 11814 | |
| 11815 | status=MagickTrue; |
| 11816 | transparent=image_info->type==GrayscaleMatteType || |
glennrp | 59575fa | 2011-12-31 21:31:39 +0000 | [diff] [blame] | 11817 | image_info->type==TrueColorMatteType || image->matte != MagickFalse; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11818 | |
glennrp | 59575fa | 2011-12-31 21:31:39 +0000 | [diff] [blame] | 11819 | jng_quality=image_info->quality == 0UL ? 75UL : image_info->quality%1000; |
| 11820 | |
| 11821 | jng_alpha_compression_method=image->compression==JPEGCompression? 8 : 0; |
| 11822 | |
glennrp | 750105b | 2012-04-25 16:20:45 +0000 | [diff] [blame] | 11823 | jng_alpha_quality=image_info->quality == 0UL ? 75UL : |
glennrp | 59575fa | 2011-12-31 21:31:39 +0000 | [diff] [blame] | 11824 | image_info->quality; |
| 11825 | |
| 11826 | if (jng_alpha_quality >= 1000) |
| 11827 | jng_alpha_quality /= 1000; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11828 | |
| 11829 | if (transparent) |
| 11830 | { |
| 11831 | jng_color_type=14; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11832 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11833 | /* Create JPEG blob, image, and image_info */ |
| 11834 | if (logging != MagickFalse) |
| 11835 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 11836 | " Creating jpeg_image_info for alpha."); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11837 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11838 | jpeg_image_info=(ImageInfo *) CloneImageInfo(image_info); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11839 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11840 | if (jpeg_image_info == (ImageInfo *) NULL) |
| 11841 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11842 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11843 | if (logging != MagickFalse) |
| 11844 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 11845 | " Creating jpeg_image."); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11846 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 11847 | jpeg_image=SeparateImage(image,AlphaChannel,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11848 | if (jpeg_image == (Image *) NULL) |
| 11849 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
| 11850 | (void) CopyMagickString(jpeg_image->magick,"JPEG",MaxTextExtent); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11851 | jpeg_image->matte=MagickFalse; |
glennrp | 8f77fdc | 2012-03-21 15:16:37 +0000 | [diff] [blame] | 11852 | jpeg_image->quality=jng_alpha_quality; |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 11853 | jpeg_image_info->type=GrayscaleType; |
| 11854 | (void) SetImageType(jpeg_image,GrayscaleType,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11855 | (void) AcquireUniqueFilename(jpeg_image->filename); |
cristy | 3b6fd2e | 2011-05-20 12:53:50 +0000 | [diff] [blame] | 11856 | (void) FormatLocaleString(jpeg_image_info->filename,MaxTextExtent, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11857 | "%s",jpeg_image->filename); |
| 11858 | } |
glennrp | 59575fa | 2011-12-31 21:31:39 +0000 | [diff] [blame] | 11859 | else |
| 11860 | { |
| 11861 | jng_alpha_compression_method=0; |
| 11862 | jng_color_type=10; |
| 11863 | jng_alpha_sample_depth=0; |
| 11864 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11865 | |
| 11866 | /* To do: check bit depth of PNG alpha channel */ |
| 11867 | |
| 11868 | /* Check if image is grayscale. */ |
| 11869 | if (image_info->type != TrueColorMatteType && image_info->type != |
cristy | 7fb2652 | 2012-06-21 13:02:48 +0000 | [diff] [blame] | 11870 | TrueColorType && IsImageGray(image,exception)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11871 | jng_color_type-=2; |
| 11872 | |
glennrp | 59575fa | 2011-12-31 21:31:39 +0000 | [diff] [blame] | 11873 | if (logging != MagickFalse) |
| 11874 | { |
| 11875 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 11876 | " JNG Quality = %d",(int) jng_quality); |
| 11877 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 11878 | " JNG Color Type = %d",jng_color_type); |
| 11879 | if (transparent) |
| 11880 | { |
| 11881 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 11882 | " JNG Alpha Compression = %d",jng_alpha_compression_method); |
| 11883 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 11884 | " JNG Alpha Depth = %d",jng_alpha_sample_depth); |
| 11885 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 11886 | " JNG Alpha Quality = %d",(int) jng_alpha_quality); |
| 11887 | } |
| 11888 | } |
| 11889 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11890 | if (transparent) |
| 11891 | { |
| 11892 | if (jng_alpha_compression_method==0) |
| 11893 | { |
| 11894 | const char |
| 11895 | *value; |
| 11896 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 11897 | /* Encode alpha as a grayscale PNG blob */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11898 | status=OpenBlob(jpeg_image_info,jpeg_image,WriteBinaryBlobMode, |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 11899 | exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11900 | if (logging != MagickFalse) |
| 11901 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 11902 | " Creating PNG blob."); |
| 11903 | length=0; |
| 11904 | |
| 11905 | (void) CopyMagickString(jpeg_image_info->magick,"PNG",MaxTextExtent); |
| 11906 | (void) CopyMagickString(jpeg_image->magick,"PNG",MaxTextExtent); |
| 11907 | jpeg_image_info->interlace=NoInterlace; |
| 11908 | |
glennrp | cc5d45b | 2012-01-06 04:06:10 +0000 | [diff] [blame] | 11909 | /* Exclude all ancillary chunks */ |
| 11910 | (void) SetImageArtifact(jpeg_image,"png:exclude-chunks","all"); |
| 11911 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11912 | blob=ImageToBlob(jpeg_image_info,jpeg_image,&length, |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 11913 | exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11914 | |
| 11915 | /* Retrieve sample depth used */ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 11916 | value=GetImageProperty(jpeg_image,"png:bit-depth-written",exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11917 | if (value != (char *) NULL) |
| 11918 | jng_alpha_sample_depth= (unsigned int) value[0]; |
| 11919 | } |
| 11920 | else |
| 11921 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 11922 | /* Encode alpha as a grayscale JPEG blob */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11923 | |
| 11924 | status=OpenBlob(jpeg_image_info,jpeg_image,WriteBinaryBlobMode, |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 11925 | exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11926 | |
| 11927 | (void) CopyMagickString(jpeg_image_info->magick,"JPEG",MaxTextExtent); |
| 11928 | (void) CopyMagickString(jpeg_image->magick,"JPEG",MaxTextExtent); |
| 11929 | jpeg_image_info->interlace=NoInterlace; |
| 11930 | if (logging != MagickFalse) |
| 11931 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 11932 | " Creating blob."); |
| 11933 | blob=ImageToBlob(jpeg_image_info,jpeg_image,&length, |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 11934 | exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11935 | jng_alpha_sample_depth=8; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11936 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11937 | if (logging != MagickFalse) |
| 11938 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 11939 | " Successfully read jpeg_image into a blob, length=%.20g.", |
| 11940 | (double) length); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11941 | |
| 11942 | } |
| 11943 | /* Destroy JPEG image and image_info */ |
| 11944 | jpeg_image=DestroyImage(jpeg_image); |
| 11945 | (void) RelinquishUniqueFileResource(jpeg_image_info->filename); |
| 11946 | jpeg_image_info=DestroyImageInfo(jpeg_image_info); |
| 11947 | } |
| 11948 | |
| 11949 | /* Write JHDR chunk */ |
| 11950 | (void) WriteBlobMSBULong(image,16L); /* chunk data length=16 */ |
| 11951 | PNGType(chunk,mng_JHDR); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 11952 | LogPNGChunk(logging,mng_JHDR,16L); |
cristy | 4e5bc84 | 2010-06-09 13:56:01 +0000 | [diff] [blame] | 11953 | PNGLong(chunk+4,(png_uint_32) image->columns); |
| 11954 | PNGLong(chunk+8,(png_uint_32) image->rows); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11955 | chunk[12]=jng_color_type; |
| 11956 | chunk[13]=8; /* sample depth */ |
| 11957 | chunk[14]=8; /*jng_image_compression_method */ |
| 11958 | chunk[15]=(unsigned char) (image_info->interlace == NoInterlace ? 0 : 8); |
| 11959 | chunk[16]=jng_alpha_sample_depth; |
| 11960 | chunk[17]=jng_alpha_compression_method; |
| 11961 | chunk[18]=0; /*jng_alpha_filter_method */ |
| 11962 | chunk[19]=0; /*jng_alpha_interlace_method */ |
| 11963 | (void) WriteBlob(image,20,chunk); |
| 11964 | (void) WriteBlobMSBULong(image,crc32(0,chunk,20)); |
| 11965 | if (logging != MagickFalse) |
| 11966 | { |
| 11967 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | f2faecf | 2010-05-28 19:19:36 +0000 | [diff] [blame] | 11968 | " JNG width:%15lu",(unsigned long) image->columns); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11969 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11970 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | f2faecf | 2010-05-28 19:19:36 +0000 | [diff] [blame] | 11971 | " JNG height:%14lu",(unsigned long) image->rows); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11972 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11973 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 11974 | " JNG color type:%10d",jng_color_type); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11975 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11976 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 11977 | " JNG sample depth:%8d",8); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11978 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11979 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 11980 | " JNG compression:%9d",8); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11981 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11982 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 11983 | " JNG interlace:%11d",0); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11984 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11985 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 11986 | " JNG alpha depth:%9d",jng_alpha_sample_depth); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11987 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11988 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 11989 | " JNG alpha compression:%3d",jng_alpha_compression_method); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11990 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11991 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 11992 | " JNG alpha filter:%8d",0); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11993 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 11994 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 11995 | " JNG alpha interlace:%5d",0); |
| 11996 | } |
| 11997 | |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 11998 | /* Write any JNG-chunk-b profiles */ |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 11999 | (void) Magick_png_write_chunk_from_profile(image,"JNG-chunk-b",logging); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12000 | |
| 12001 | /* |
| 12002 | Write leading ancillary chunks |
| 12003 | */ |
| 12004 | |
| 12005 | if (transparent) |
| 12006 | { |
| 12007 | /* |
| 12008 | Write JNG bKGD chunk |
| 12009 | */ |
| 12010 | |
| 12011 | unsigned char |
| 12012 | blue, |
| 12013 | green, |
| 12014 | red; |
| 12015 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 12016 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12017 | num_bytes; |
| 12018 | |
| 12019 | if (jng_color_type == 8 || jng_color_type == 12) |
| 12020 | num_bytes=6L; |
| 12021 | else |
| 12022 | num_bytes=10L; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 12023 | (void) WriteBlobMSBULong(image,(size_t) (num_bytes-4L)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12024 | PNGType(chunk,mng_bKGD); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12025 | LogPNGChunk(logging,mng_bKGD,(size_t) (num_bytes-4L)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12026 | red=ScaleQuantumToChar(image->background_color.red); |
| 12027 | green=ScaleQuantumToChar(image->background_color.green); |
| 12028 | blue=ScaleQuantumToChar(image->background_color.blue); |
| 12029 | *(chunk+4)=0; |
| 12030 | *(chunk+5)=red; |
| 12031 | *(chunk+6)=0; |
| 12032 | *(chunk+7)=green; |
| 12033 | *(chunk+8)=0; |
| 12034 | *(chunk+9)=blue; |
| 12035 | (void) WriteBlob(image,(size_t) num_bytes,chunk); |
| 12036 | (void) WriteBlobMSBULong(image,crc32(0,chunk,(uInt) num_bytes)); |
| 12037 | } |
| 12038 | |
| 12039 | if ((image->colorspace == sRGBColorspace || image->rendering_intent)) |
| 12040 | { |
| 12041 | /* |
| 12042 | Write JNG sRGB chunk |
| 12043 | */ |
| 12044 | (void) WriteBlobMSBULong(image,1L); |
| 12045 | PNGType(chunk,mng_sRGB); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12046 | LogPNGChunk(logging,mng_sRGB,1L); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12047 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12048 | if (image->rendering_intent != UndefinedIntent) |
glennrp | e610a07 | 2010-08-05 17:08:46 +0000 | [diff] [blame] | 12049 | chunk[4]=(unsigned char) |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 12050 | Magick_RenderingIntent_to_PNG_RenderingIntent( |
glennrp | e610a07 | 2010-08-05 17:08:46 +0000 | [diff] [blame] | 12051 | (image->rendering_intent)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12052 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12053 | else |
glennrp | e610a07 | 2010-08-05 17:08:46 +0000 | [diff] [blame] | 12054 | chunk[4]=(unsigned char) |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 12055 | Magick_RenderingIntent_to_PNG_RenderingIntent( |
glennrp | e610a07 | 2010-08-05 17:08:46 +0000 | [diff] [blame] | 12056 | (PerceptualIntent)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12057 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12058 | (void) WriteBlob(image,5,chunk); |
| 12059 | (void) WriteBlobMSBULong(image,crc32(0,chunk,5)); |
| 12060 | } |
| 12061 | else |
| 12062 | { |
| 12063 | if (image->gamma != 0.0) |
| 12064 | { |
| 12065 | /* |
| 12066 | Write JNG gAMA chunk |
| 12067 | */ |
| 12068 | (void) WriteBlobMSBULong(image,4L); |
| 12069 | PNGType(chunk,mng_gAMA); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12070 | LogPNGChunk(logging,mng_gAMA,4L); |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 12071 | PNGLong(chunk+4,(png_uint_32) (100000*image->gamma+0.5)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12072 | (void) WriteBlob(image,8,chunk); |
| 12073 | (void) WriteBlobMSBULong(image,crc32(0,chunk,8)); |
| 12074 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12075 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12076 | if ((mng_info->equal_chrms == MagickFalse) && |
| 12077 | (image->chromaticity.red_primary.x != 0.0)) |
| 12078 | { |
| 12079 | PrimaryInfo |
| 12080 | primary; |
| 12081 | |
| 12082 | /* |
| 12083 | Write JNG cHRM chunk |
| 12084 | */ |
| 12085 | (void) WriteBlobMSBULong(image,32L); |
| 12086 | PNGType(chunk,mng_cHRM); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12087 | LogPNGChunk(logging,mng_cHRM,32L); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12088 | primary=image->chromaticity.white_point; |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 12089 | PNGLong(chunk+4,(png_uint_32) (100000*primary.x+0.5)); |
| 12090 | PNGLong(chunk+8,(png_uint_32) (100000*primary.y+0.5)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12091 | primary=image->chromaticity.red_primary; |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 12092 | PNGLong(chunk+12,(png_uint_32) (100000*primary.x+0.5)); |
| 12093 | PNGLong(chunk+16,(png_uint_32) (100000*primary.y+0.5)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12094 | primary=image->chromaticity.green_primary; |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 12095 | PNGLong(chunk+20,(png_uint_32) (100000*primary.x+0.5)); |
| 12096 | PNGLong(chunk+24,(png_uint_32) (100000*primary.y+0.5)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12097 | primary=image->chromaticity.blue_primary; |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 12098 | PNGLong(chunk+28,(png_uint_32) (100000*primary.x+0.5)); |
| 12099 | PNGLong(chunk+32,(png_uint_32) (100000*primary.y+0.5)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12100 | (void) WriteBlob(image,36,chunk); |
| 12101 | (void) WriteBlobMSBULong(image,crc32(0,chunk,36)); |
| 12102 | } |
| 12103 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12104 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 12105 | if (image->resolution.x && image->resolution.y && !mng_info->equal_physs) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12106 | { |
| 12107 | /* |
| 12108 | Write JNG pHYs chunk |
| 12109 | */ |
| 12110 | (void) WriteBlobMSBULong(image,9L); |
| 12111 | PNGType(chunk,mng_pHYs); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12112 | LogPNGChunk(logging,mng_pHYs,9L); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12113 | if (image->units == PixelsPerInchResolution) |
| 12114 | { |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 12115 | PNGLong(chunk+4,(png_uint_32) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 12116 | (image->resolution.x*100.0/2.54+0.5)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12117 | |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 12118 | PNGLong(chunk+8,(png_uint_32) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 12119 | (image->resolution.y*100.0/2.54+0.5)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12120 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12121 | chunk[12]=1; |
| 12122 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12123 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12124 | else |
| 12125 | { |
| 12126 | if (image->units == PixelsPerCentimeterResolution) |
| 12127 | { |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 12128 | PNGLong(chunk+4,(png_uint_32) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 12129 | (image->resolution.x*100.0+0.5)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12130 | |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 12131 | PNGLong(chunk+8,(png_uint_32) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 12132 | (image->resolution.y*100.0+0.5)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12133 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12134 | chunk[12]=1; |
| 12135 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12136 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12137 | else |
| 12138 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 12139 | PNGLong(chunk+4,(png_uint_32) (image->resolution.x+0.5)); |
| 12140 | PNGLong(chunk+8,(png_uint_32) (image->resolution.y+0.5)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12141 | chunk[12]=0; |
| 12142 | } |
| 12143 | } |
| 12144 | (void) WriteBlob(image,13,chunk); |
| 12145 | (void) WriteBlobMSBULong(image,crc32(0,chunk,13)); |
| 12146 | } |
| 12147 | |
| 12148 | if (mng_info->write_mng == 0 && (image->page.x || image->page.y)) |
| 12149 | { |
| 12150 | /* |
| 12151 | Write JNG oFFs chunk |
| 12152 | */ |
| 12153 | (void) WriteBlobMSBULong(image,9L); |
| 12154 | PNGType(chunk,mng_oFFs); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12155 | LogPNGChunk(logging,mng_oFFs,9L); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 12156 | PNGsLong(chunk+4,(ssize_t) (image->page.x)); |
| 12157 | PNGsLong(chunk+8,(ssize_t) (image->page.y)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12158 | chunk[12]=0; |
| 12159 | (void) WriteBlob(image,13,chunk); |
| 12160 | (void) WriteBlobMSBULong(image,crc32(0,chunk,13)); |
| 12161 | } |
| 12162 | if (mng_info->write_mng == 0 && (image->page.width || image->page.height)) |
| 12163 | { |
| 12164 | (void) WriteBlobMSBULong(image,9L); /* data length=8 */ |
| 12165 | PNGType(chunk,mng_vpAg); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12166 | LogPNGChunk(logging,mng_vpAg,9L); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12167 | PNGLong(chunk+4,(png_uint_32) image->page.width); |
| 12168 | PNGLong(chunk+8,(png_uint_32) image->page.height); |
| 12169 | chunk[12]=0; /* unit = pixels */ |
| 12170 | (void) WriteBlob(image,13,chunk); |
| 12171 | (void) WriteBlobMSBULong(image,crc32(0,chunk,13)); |
| 12172 | } |
| 12173 | |
| 12174 | |
| 12175 | if (transparent) |
| 12176 | { |
| 12177 | if (jng_alpha_compression_method==0) |
| 12178 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 12179 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12180 | i; |
| 12181 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 12182 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12183 | len; |
| 12184 | |
| 12185 | /* Write IDAT chunk header */ |
| 12186 | if (logging != MagickFalse) |
| 12187 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 12188 | " Write IDAT chunks from blob, length=%.20g.",(double) |
cristy | f2faecf | 2010-05-28 19:19:36 +0000 | [diff] [blame] | 12189 | length); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12190 | |
| 12191 | /* Copy IDAT chunks */ |
| 12192 | len=0; |
| 12193 | p=blob+8; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 12194 | for (i=8; i<(ssize_t) length; i+=len+12) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12195 | { |
| 12196 | len=(*p<<24)|((*(p+1))<<16)|((*(p+2))<<8)|(*(p+3)); |
| 12197 | p+=4; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12198 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12199 | if (*(p)==73 && *(p+1)==68 && *(p+2)==65 && *(p+3)==84) /* IDAT */ |
| 12200 | { |
| 12201 | /* Found an IDAT chunk. */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 12202 | (void) WriteBlobMSBULong(image,(size_t) len); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12203 | LogPNGChunk(logging,mng_IDAT,(size_t) len); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12204 | (void) WriteBlob(image,(size_t) len+4,p); |
| 12205 | (void) WriteBlobMSBULong(image, |
| 12206 | crc32(0,p,(uInt) len+4)); |
| 12207 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12208 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12209 | else |
| 12210 | { |
| 12211 | if (logging != MagickFalse) |
| 12212 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 12213 | " Skipping %c%c%c%c chunk, length=%.20g.", |
| 12214 | *(p),*(p+1),*(p+2),*(p+3),(double) len); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12215 | } |
| 12216 | p+=(8+len); |
| 12217 | } |
| 12218 | } |
| 12219 | else |
| 12220 | { |
| 12221 | /* Write JDAA chunk header */ |
| 12222 | if (logging != MagickFalse) |
| 12223 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 12224 | " Write JDAA chunk, length=%.20g.",(double) length); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 12225 | (void) WriteBlobMSBULong(image,(size_t) length); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12226 | PNGType(chunk,mng_JDAA); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12227 | LogPNGChunk(logging,mng_JDAA,length); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12228 | /* Write JDAT chunk(s) data */ |
| 12229 | (void) WriteBlob(image,4,chunk); |
| 12230 | (void) WriteBlob(image,length,blob); |
| 12231 | (void) WriteBlobMSBULong(image,crc32(crc32(0,chunk,4),blob, |
| 12232 | (uInt) length)); |
| 12233 | } |
| 12234 | blob=(unsigned char *) RelinquishMagickMemory(blob); |
| 12235 | } |
| 12236 | |
| 12237 | /* Encode image as a JPEG blob */ |
| 12238 | if (logging != MagickFalse) |
| 12239 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 12240 | " Creating jpeg_image_info."); |
| 12241 | jpeg_image_info=(ImageInfo *) CloneImageInfo(image_info); |
| 12242 | if (jpeg_image_info == (ImageInfo *) NULL) |
| 12243 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
| 12244 | |
| 12245 | if (logging != MagickFalse) |
| 12246 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 12247 | " Creating jpeg_image."); |
| 12248 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 12249 | jpeg_image=CloneImage(image,0,0,MagickTrue,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12250 | if (jpeg_image == (Image *) NULL) |
| 12251 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
| 12252 | (void) CopyMagickString(jpeg_image->magick,"JPEG",MaxTextExtent); |
| 12253 | |
| 12254 | (void) AcquireUniqueFilename(jpeg_image->filename); |
cristy | 3b6fd2e | 2011-05-20 12:53:50 +0000 | [diff] [blame] | 12255 | (void) FormatLocaleString(jpeg_image_info->filename,MaxTextExtent,"%s", |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12256 | jpeg_image->filename); |
| 12257 | |
| 12258 | status=OpenBlob(jpeg_image_info,jpeg_image,WriteBinaryBlobMode, |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 12259 | exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12260 | |
| 12261 | if (logging != MagickFalse) |
| 12262 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 12263 | " Created jpeg_image, %.20g x %.20g.",(double) jpeg_image->columns, |
| 12264 | (double) jpeg_image->rows); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12265 | |
| 12266 | if (jng_color_type == 8 || jng_color_type == 12) |
| 12267 | jpeg_image_info->type=GrayscaleType; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12268 | |
glennrp | 59575fa | 2011-12-31 21:31:39 +0000 | [diff] [blame] | 12269 | jpeg_image_info->quality=jng_quality; |
| 12270 | jpeg_image->quality=jng_quality; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12271 | (void) CopyMagickString(jpeg_image_info->magick,"JPEG",MaxTextExtent); |
| 12272 | (void) CopyMagickString(jpeg_image->magick,"JPEG",MaxTextExtent); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12273 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12274 | if (logging != MagickFalse) |
| 12275 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 12276 | " Creating blob."); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12277 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 12278 | blob=ImageToBlob(jpeg_image_info,jpeg_image,&length,exception); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12279 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12280 | if (logging != MagickFalse) |
| 12281 | { |
| 12282 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 12283 | " Successfully read jpeg_image into a blob, length=%.20g.", |
| 12284 | (double) length); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12285 | |
| 12286 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 12287 | " Write JDAT chunk, length=%.20g.",(double) length); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12288 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12289 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12290 | /* Write JDAT chunk(s) */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 12291 | (void) WriteBlobMSBULong(image,(size_t) length); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12292 | PNGType(chunk,mng_JDAT); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12293 | LogPNGChunk(logging,mng_JDAT,length); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12294 | (void) WriteBlob(image,4,chunk); |
| 12295 | (void) WriteBlob(image,length,blob); |
| 12296 | (void) WriteBlobMSBULong(image,crc32(crc32(0,chunk,4),blob,(uInt) length)); |
| 12297 | |
| 12298 | jpeg_image=DestroyImage(jpeg_image); |
| 12299 | (void) RelinquishUniqueFileResource(jpeg_image_info->filename); |
| 12300 | jpeg_image_info=DestroyImageInfo(jpeg_image_info); |
| 12301 | blob=(unsigned char *) RelinquishMagickMemory(blob); |
| 12302 | |
| 12303 | /* Write any JNG-chunk-e profiles */ |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 12304 | (void) Magick_png_write_chunk_from_profile(image,"JNG-chunk-e",logging); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12305 | |
| 12306 | /* Write IEND chunk */ |
| 12307 | (void) WriteBlobMSBULong(image,0L); |
| 12308 | PNGType(chunk,mng_IEND); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12309 | LogPNGChunk(logging,mng_IEND,0); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12310 | (void) WriteBlob(image,4,chunk); |
| 12311 | (void) WriteBlobMSBULong(image,crc32(0,chunk,4)); |
| 12312 | |
| 12313 | if (logging != MagickFalse) |
| 12314 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 12315 | " exit WriteOneJNGImage()"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12316 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12317 | return(status); |
| 12318 | } |
| 12319 | |
| 12320 | |
| 12321 | /* |
| 12322 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 12323 | % % |
| 12324 | % % |
| 12325 | % % |
| 12326 | % W r i t e J N G I m a g e % |
| 12327 | % % |
| 12328 | % % |
| 12329 | % % |
| 12330 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 12331 | % |
| 12332 | % WriteJNGImage() writes a JPEG Network Graphics (JNG) image file. |
| 12333 | % |
| 12334 | % JNG support written by Glenn Randers-Pehrson, glennrp@image... |
| 12335 | % |
| 12336 | % The format of the WriteJNGImage method is: |
| 12337 | % |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 12338 | % MagickBooleanType WriteJNGImage(const ImageInfo *image_info, |
| 12339 | % Image *image,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12340 | % |
| 12341 | % A description of each parameter follows: |
| 12342 | % |
| 12343 | % o image_info: the image info. |
| 12344 | % |
| 12345 | % o image: The image. |
| 12346 | % |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 12347 | % o exception: return any errors or warnings in this structure. |
| 12348 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12349 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 12350 | */ |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 12351 | static MagickBooleanType WriteJNGImage(const ImageInfo *image_info,Image *image, |
| 12352 | ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12353 | { |
| 12354 | MagickBooleanType |
glennrp | 21f0e62 | 2011-01-07 16:20:57 +0000 | [diff] [blame] | 12355 | have_mng_structure, |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12356 | logging, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12357 | status; |
| 12358 | |
| 12359 | MngInfo |
| 12360 | *mng_info; |
| 12361 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12362 | /* |
| 12363 | Open image file. |
| 12364 | */ |
| 12365 | assert(image_info != (const ImageInfo *) NULL); |
| 12366 | assert(image_info->signature == MagickSignature); |
| 12367 | assert(image != (Image *) NULL); |
| 12368 | assert(image->signature == MagickSignature); |
| 12369 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 12370 | logging=LogMagickEvent(CoderEvent,GetMagickModule(),"Enter WriteJNGImage()"); |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 12371 | status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12372 | if (status == MagickFalse) |
| 12373 | return(status); |
| 12374 | |
| 12375 | /* |
| 12376 | Allocate a MngInfo structure. |
| 12377 | */ |
| 12378 | have_mng_structure=MagickFalse; |
cristy | 73bd4a5 | 2010-10-05 11:24:23 +0000 | [diff] [blame] | 12379 | mng_info=(MngInfo *) AcquireMagickMemory(sizeof(MngInfo)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12380 | if (mng_info == (MngInfo *) NULL) |
| 12381 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
| 12382 | /* |
| 12383 | Initialize members of the MngInfo structure. |
| 12384 | */ |
| 12385 | (void) ResetMagickMemory(mng_info,0,sizeof(MngInfo)); |
| 12386 | mng_info->image=image; |
| 12387 | have_mng_structure=MagickTrue; |
| 12388 | |
| 12389 | (void) WriteBlob(image,8,(const unsigned char *) "\213JNG\r\n\032\n"); |
| 12390 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 12391 | status=WriteOneJNGImage(mng_info,image_info,image,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12392 | (void) CloseBlob(image); |
| 12393 | |
| 12394 | (void) CatchImageException(image); |
| 12395 | MngInfoFreeStruct(mng_info,&have_mng_structure); |
| 12396 | if (logging != MagickFalse) |
| 12397 | (void) LogMagickEvent(CoderEvent,GetMagickModule(),"exit WriteJNGImage()"); |
| 12398 | return(status); |
| 12399 | } |
| 12400 | #endif |
| 12401 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 12402 | static MagickBooleanType WriteMNGImage(const ImageInfo *image_info,Image *image, |
| 12403 | ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12404 | { |
| 12405 | const char |
| 12406 | *option; |
| 12407 | |
| 12408 | Image |
| 12409 | *next_image; |
| 12410 | |
| 12411 | MagickBooleanType |
glennrp | 21f0e62 | 2011-01-07 16:20:57 +0000 | [diff] [blame] | 12412 | have_mng_structure, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12413 | status; |
| 12414 | |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12415 | volatile MagickBooleanType |
| 12416 | logging; |
| 12417 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12418 | MngInfo |
| 12419 | *mng_info; |
| 12420 | |
| 12421 | int |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12422 | image_count, |
| 12423 | need_iterations, |
| 12424 | need_matte; |
| 12425 | |
| 12426 | volatile int |
| 12427 | #if defined(PNG_WRITE_EMPTY_PLTE_SUPPORTED) || \ |
| 12428 | defined(PNG_MNG_FEATURES_SUPPORTED) |
| 12429 | need_local_plte, |
| 12430 | #endif |
| 12431 | all_images_are_gray, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12432 | need_defi, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12433 | use_global_plte; |
| 12434 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 12435 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12436 | i; |
| 12437 | |
| 12438 | unsigned char |
| 12439 | chunk[800]; |
| 12440 | |
| 12441 | volatile unsigned int |
| 12442 | write_jng, |
| 12443 | write_mng; |
| 12444 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 12445 | volatile size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12446 | scene; |
| 12447 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 12448 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12449 | final_delay=0, |
| 12450 | initial_delay; |
| 12451 | |
glennrp | d5045b4 | 2010-03-24 12:40:35 +0000 | [diff] [blame] | 12452 | #if (PNG_LIBPNG_VER < 10200) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12453 | if (image_info->verbose) |
| 12454 | printf("Your PNG library (libpng-%s) is rather old.\n", |
| 12455 | PNG_LIBPNG_VER_STRING); |
| 12456 | #endif |
| 12457 | |
| 12458 | /* |
| 12459 | Open image file. |
| 12460 | */ |
| 12461 | assert(image_info != (const ImageInfo *) NULL); |
| 12462 | assert(image_info->signature == MagickSignature); |
| 12463 | assert(image != (Image *) NULL); |
| 12464 | assert(image->signature == MagickSignature); |
| 12465 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
glennrp | fd05d62 | 2011-02-25 04:10:33 +0000 | [diff] [blame] | 12466 | logging=LogMagickEvent(CoderEvent,GetMagickModule(),"Enter WriteMNGImage()"); |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 12467 | status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12468 | if (status == MagickFalse) |
| 12469 | return(status); |
| 12470 | |
| 12471 | /* |
| 12472 | Allocate a MngInfo structure. |
| 12473 | */ |
| 12474 | have_mng_structure=MagickFalse; |
cristy | 73bd4a5 | 2010-10-05 11:24:23 +0000 | [diff] [blame] | 12475 | mng_info=(MngInfo *) AcquireMagickMemory(sizeof(MngInfo)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12476 | if (mng_info == (MngInfo *) NULL) |
| 12477 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
| 12478 | /* |
| 12479 | Initialize members of the MngInfo structure. |
| 12480 | */ |
| 12481 | (void) ResetMagickMemory(mng_info,0,sizeof(MngInfo)); |
| 12482 | mng_info->image=image; |
| 12483 | have_mng_structure=MagickTrue; |
| 12484 | write_mng=LocaleCompare(image_info->magick,"MNG") == 0; |
| 12485 | |
| 12486 | /* |
| 12487 | * See if user has requested a specific PNG subformat to be used |
| 12488 | * for all of the PNGs in the MNG being written, e.g., |
| 12489 | * |
| 12490 | * convert *.png png8:animation.mng |
| 12491 | * |
| 12492 | * To do: check -define png:bit_depth and png:color_type as well, |
| 12493 | * or perhaps use mng:bit_depth and mng:color_type instead for |
| 12494 | * global settings. |
| 12495 | */ |
| 12496 | |
| 12497 | mng_info->write_png8=LocaleCompare(image_info->magick,"PNG8") == 0; |
| 12498 | mng_info->write_png24=LocaleCompare(image_info->magick,"PNG24") == 0; |
| 12499 | mng_info->write_png32=LocaleCompare(image_info->magick,"PNG32") == 0; |
| 12500 | |
| 12501 | write_jng=MagickFalse; |
| 12502 | if (image_info->compression == JPEGCompression) |
| 12503 | write_jng=MagickTrue; |
| 12504 | |
| 12505 | mng_info->adjoin=image_info->adjoin && |
| 12506 | (GetNextImageInList(image) != (Image *) NULL) && write_mng; |
| 12507 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12508 | if (logging != MagickFalse) |
| 12509 | { |
| 12510 | /* Log some info about the input */ |
| 12511 | Image |
| 12512 | *p; |
| 12513 | |
| 12514 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 12515 | " Checking input image(s)"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12516 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12517 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 12518 | " Image_info depth: %.20g",(double) image_info->depth); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12519 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12520 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 12521 | " Type: %d",image_info->type); |
| 12522 | |
| 12523 | scene=0; |
| 12524 | for (p=image; p != (Image *) NULL; p=GetNextImageInList(p)) |
| 12525 | { |
| 12526 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 12527 | " Scene: %.20g",(double) scene++); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12528 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12529 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 12530 | " Image depth: %.20g",(double) p->depth); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12531 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12532 | if (p->matte) |
| 12533 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 12534 | " Matte: True"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12535 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12536 | else |
| 12537 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 12538 | " Matte: False"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12539 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12540 | if (p->storage_class == PseudoClass) |
| 12541 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 12542 | " Storage class: PseudoClass"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12543 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12544 | else |
| 12545 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 12546 | " Storage class: DirectClass"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12547 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12548 | if (p->colors) |
| 12549 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 12550 | " Number of colors: %.20g",(double) p->colors); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12551 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12552 | else |
| 12553 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 12554 | " Number of colors: unspecified"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12555 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12556 | if (mng_info->adjoin == MagickFalse) |
| 12557 | break; |
| 12558 | } |
| 12559 | } |
| 12560 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12561 | use_global_plte=MagickFalse; |
| 12562 | all_images_are_gray=MagickFalse; |
| 12563 | #ifdef PNG_WRITE_EMPTY_PLTE_SUPPORTED |
| 12564 | need_local_plte=MagickTrue; |
| 12565 | #endif |
| 12566 | need_defi=MagickFalse; |
| 12567 | need_matte=MagickFalse; |
| 12568 | mng_info->framing_mode=1; |
| 12569 | mng_info->old_framing_mode=1; |
| 12570 | |
| 12571 | if (write_mng) |
| 12572 | if (image_info->page != (char *) NULL) |
| 12573 | { |
| 12574 | /* |
| 12575 | Determine image bounding box. |
| 12576 | */ |
| 12577 | SetGeometry(image,&mng_info->page); |
| 12578 | (void) ParseMetaGeometry(image_info->page,&mng_info->page.x, |
| 12579 | &mng_info->page.y,&mng_info->page.width,&mng_info->page.height); |
| 12580 | } |
| 12581 | if (write_mng) |
| 12582 | { |
| 12583 | unsigned int |
| 12584 | need_geom; |
| 12585 | |
| 12586 | unsigned short |
| 12587 | red, |
| 12588 | green, |
| 12589 | blue; |
| 12590 | |
| 12591 | mng_info->page=image->page; |
| 12592 | need_geom=MagickTrue; |
| 12593 | if (mng_info->page.width || mng_info->page.height) |
| 12594 | need_geom=MagickFalse; |
| 12595 | /* |
| 12596 | Check all the scenes. |
| 12597 | */ |
| 12598 | initial_delay=image->delay; |
| 12599 | need_iterations=MagickFalse; |
| 12600 | mng_info->equal_chrms=image->chromaticity.red_primary.x != 0.0; |
| 12601 | mng_info->equal_physs=MagickTrue, |
| 12602 | mng_info->equal_gammas=MagickTrue; |
| 12603 | mng_info->equal_srgbs=MagickTrue; |
| 12604 | mng_info->equal_backgrounds=MagickTrue; |
| 12605 | image_count=0; |
| 12606 | #if defined(PNG_WRITE_EMPTY_PLTE_SUPPORTED) || \ |
| 12607 | defined(PNG_MNG_FEATURES_SUPPORTED) |
| 12608 | all_images_are_gray=MagickTrue; |
| 12609 | mng_info->equal_palettes=MagickFalse; |
| 12610 | need_local_plte=MagickFalse; |
| 12611 | #endif |
| 12612 | for (next_image=image; next_image != (Image *) NULL; ) |
| 12613 | { |
| 12614 | if (need_geom) |
| 12615 | { |
| 12616 | if ((next_image->columns+next_image->page.x) > mng_info->page.width) |
| 12617 | mng_info->page.width=next_image->columns+next_image->page.x; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12618 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12619 | if ((next_image->rows+next_image->page.y) > mng_info->page.height) |
| 12620 | mng_info->page.height=next_image->rows+next_image->page.y; |
| 12621 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12622 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12623 | if (next_image->page.x || next_image->page.y) |
| 12624 | need_defi=MagickTrue; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12625 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12626 | if (next_image->matte) |
| 12627 | need_matte=MagickTrue; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12628 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12629 | if ((int) next_image->dispose >= BackgroundDispose) |
| 12630 | if (next_image->matte || next_image->page.x || next_image->page.y || |
| 12631 | ((next_image->columns < mng_info->page.width) && |
| 12632 | (next_image->rows < mng_info->page.height))) |
| 12633 | mng_info->need_fram=MagickTrue; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12634 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12635 | if (next_image->iterations) |
| 12636 | need_iterations=MagickTrue; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12637 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12638 | final_delay=next_image->delay; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12639 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12640 | if (final_delay != initial_delay || final_delay > 1UL* |
| 12641 | next_image->ticks_per_second) |
| 12642 | mng_info->need_fram=1; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12643 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12644 | #if defined(PNG_WRITE_EMPTY_PLTE_SUPPORTED) || \ |
| 12645 | defined(PNG_MNG_FEATURES_SUPPORTED) |
| 12646 | /* |
| 12647 | check for global palette possibility. |
| 12648 | */ |
| 12649 | if (image->matte != MagickFalse) |
| 12650 | need_local_plte=MagickTrue; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12651 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12652 | if (need_local_plte == 0) |
| 12653 | { |
cristy | 7fb2652 | 2012-06-21 13:02:48 +0000 | [diff] [blame] | 12654 | if (IsImageGray(image,exception) == MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12655 | all_images_are_gray=MagickFalse; |
| 12656 | mng_info->equal_palettes=PalettesAreEqual(image,next_image); |
| 12657 | if (use_global_plte == 0) |
| 12658 | use_global_plte=mng_info->equal_palettes; |
| 12659 | need_local_plte=!mng_info->equal_palettes; |
| 12660 | } |
| 12661 | #endif |
| 12662 | if (GetNextImageInList(next_image) != (Image *) NULL) |
| 12663 | { |
| 12664 | if (next_image->background_color.red != |
| 12665 | next_image->next->background_color.red || |
| 12666 | next_image->background_color.green != |
| 12667 | next_image->next->background_color.green || |
| 12668 | next_image->background_color.blue != |
| 12669 | next_image->next->background_color.blue) |
| 12670 | mng_info->equal_backgrounds=MagickFalse; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12671 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12672 | if (next_image->gamma != next_image->next->gamma) |
| 12673 | mng_info->equal_gammas=MagickFalse; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12674 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12675 | if (next_image->rendering_intent != |
| 12676 | next_image->next->rendering_intent) |
| 12677 | mng_info->equal_srgbs=MagickFalse; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12678 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12679 | if ((next_image->units != next_image->next->units) || |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 12680 | (next_image->resolution.x != next_image->next->resolution.x) || |
| 12681 | (next_image->resolution.y != next_image->next->resolution.y)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12682 | mng_info->equal_physs=MagickFalse; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12683 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12684 | if (mng_info->equal_chrms) |
| 12685 | { |
| 12686 | if (next_image->chromaticity.red_primary.x != |
| 12687 | next_image->next->chromaticity.red_primary.x || |
| 12688 | next_image->chromaticity.red_primary.y != |
| 12689 | next_image->next->chromaticity.red_primary.y || |
| 12690 | next_image->chromaticity.green_primary.x != |
| 12691 | next_image->next->chromaticity.green_primary.x || |
| 12692 | next_image->chromaticity.green_primary.y != |
| 12693 | next_image->next->chromaticity.green_primary.y || |
| 12694 | next_image->chromaticity.blue_primary.x != |
| 12695 | next_image->next->chromaticity.blue_primary.x || |
| 12696 | next_image->chromaticity.blue_primary.y != |
| 12697 | next_image->next->chromaticity.blue_primary.y || |
| 12698 | next_image->chromaticity.white_point.x != |
| 12699 | next_image->next->chromaticity.white_point.x || |
| 12700 | next_image->chromaticity.white_point.y != |
| 12701 | next_image->next->chromaticity.white_point.y) |
| 12702 | mng_info->equal_chrms=MagickFalse; |
| 12703 | } |
| 12704 | } |
| 12705 | image_count++; |
| 12706 | next_image=GetNextImageInList(next_image); |
| 12707 | } |
| 12708 | if (image_count < 2) |
| 12709 | { |
| 12710 | mng_info->equal_backgrounds=MagickFalse; |
| 12711 | mng_info->equal_chrms=MagickFalse; |
| 12712 | mng_info->equal_gammas=MagickFalse; |
| 12713 | mng_info->equal_srgbs=MagickFalse; |
| 12714 | mng_info->equal_physs=MagickFalse; |
| 12715 | use_global_plte=MagickFalse; |
| 12716 | #ifdef PNG_WRITE_EMPTY_PLTE_SUPPORTED |
| 12717 | need_local_plte=MagickTrue; |
| 12718 | #endif |
| 12719 | need_iterations=MagickFalse; |
| 12720 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12721 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12722 | if (mng_info->need_fram == MagickFalse) |
| 12723 | { |
| 12724 | /* |
| 12725 | Only certain framing rates 100/n are exactly representable without |
| 12726 | the FRAM chunk but we'll allow some slop in VLC files |
| 12727 | */ |
| 12728 | if (final_delay == 0) |
| 12729 | { |
| 12730 | if (need_iterations != MagickFalse) |
| 12731 | { |
| 12732 | /* |
| 12733 | It's probably a GIF with loop; don't run it *too* fast. |
| 12734 | */ |
glennrp | 0261712 | 2010-07-28 13:07:35 +0000 | [diff] [blame] | 12735 | if (mng_info->adjoin) |
glennrp | d908de4 | 2010-07-28 13:28:27 +0000 | [diff] [blame] | 12736 | { |
| 12737 | final_delay=10; |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 12738 | (void) ThrowMagickException(exception,GetMagickModule(), |
| 12739 | CoderWarning, |
glennrp | d908de4 | 2010-07-28 13:28:27 +0000 | [diff] [blame] | 12740 | "input has zero delay between all frames; assuming", |
| 12741 | " 10 cs `%s'",""); |
| 12742 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12743 | } |
| 12744 | else |
| 12745 | mng_info->ticks_per_second=0; |
| 12746 | } |
| 12747 | if (final_delay != 0) |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 12748 | mng_info->ticks_per_second=(png_uint_32) |
| 12749 | (image->ticks_per_second/final_delay); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12750 | if (final_delay > 50) |
| 12751 | mng_info->ticks_per_second=2; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12752 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12753 | if (final_delay > 75) |
| 12754 | mng_info->ticks_per_second=1; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12755 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12756 | if (final_delay > 125) |
| 12757 | mng_info->need_fram=MagickTrue; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12758 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12759 | if (need_defi && final_delay > 2 && (final_delay != 4) && |
| 12760 | (final_delay != 5) && (final_delay != 10) && (final_delay != 20) && |
| 12761 | (final_delay != 25) && (final_delay != 50) && (final_delay != |
| 12762 | 1UL*image->ticks_per_second)) |
| 12763 | mng_info->need_fram=MagickTrue; /* make it exact; cannot be VLC */ |
| 12764 | } |
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 (mng_info->need_fram != MagickFalse) |
| 12767 | mng_info->ticks_per_second=1UL*image->ticks_per_second; |
| 12768 | /* |
| 12769 | If pseudocolor, we should also check to see if all the |
| 12770 | palettes are identical and write a global PLTE if they are. |
| 12771 | ../glennrp Feb 99. |
| 12772 | */ |
| 12773 | /* |
| 12774 | Write the MNG version 1.0 signature and MHDR chunk. |
| 12775 | */ |
| 12776 | (void) WriteBlob(image,8,(const unsigned char *) "\212MNG\r\n\032\n"); |
| 12777 | (void) WriteBlobMSBULong(image,28L); /* chunk data length=28 */ |
| 12778 | PNGType(chunk,mng_MHDR); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12779 | LogPNGChunk(logging,mng_MHDR,28L); |
cristy | 4e5bc84 | 2010-06-09 13:56:01 +0000 | [diff] [blame] | 12780 | PNGLong(chunk+4,(png_uint_32) mng_info->page.width); |
| 12781 | PNGLong(chunk+8,(png_uint_32) mng_info->page.height); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12782 | PNGLong(chunk+12,mng_info->ticks_per_second); |
| 12783 | PNGLong(chunk+16,0L); /* layer count=unknown */ |
| 12784 | PNGLong(chunk+20,0L); /* frame count=unknown */ |
| 12785 | PNGLong(chunk+24,0L); /* play time=unknown */ |
| 12786 | if (write_jng) |
| 12787 | { |
| 12788 | if (need_matte) |
| 12789 | { |
| 12790 | if (need_defi || mng_info->need_fram || use_global_plte) |
| 12791 | PNGLong(chunk+28,27L); /* simplicity=LC+JNG */ |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12792 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12793 | else |
| 12794 | PNGLong(chunk+28,25L); /* simplicity=VLC+JNG */ |
| 12795 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12796 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12797 | else |
| 12798 | { |
| 12799 | if (need_defi || mng_info->need_fram || use_global_plte) |
| 12800 | PNGLong(chunk+28,19L); /* simplicity=LC+JNG, no transparency */ |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12801 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12802 | else |
| 12803 | PNGLong(chunk+28,17L); /* simplicity=VLC+JNG, no transparency */ |
| 12804 | } |
| 12805 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12806 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12807 | else |
| 12808 | { |
| 12809 | if (need_matte) |
| 12810 | { |
| 12811 | if (need_defi || mng_info->need_fram || use_global_plte) |
| 12812 | PNGLong(chunk+28,11L); /* simplicity=LC */ |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12813 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12814 | else |
| 12815 | PNGLong(chunk+28,9L); /* simplicity=VLC */ |
| 12816 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12817 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12818 | else |
| 12819 | { |
| 12820 | if (need_defi || mng_info->need_fram || use_global_plte) |
| 12821 | PNGLong(chunk+28,3L); /* simplicity=LC, no transparency */ |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12822 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12823 | else |
| 12824 | PNGLong(chunk+28,1L); /* simplicity=VLC, no transparency */ |
| 12825 | } |
| 12826 | } |
| 12827 | (void) WriteBlob(image,32,chunk); |
| 12828 | (void) WriteBlobMSBULong(image,crc32(0,chunk,32)); |
| 12829 | option=GetImageOption(image_info,"mng:need-cacheoff"); |
| 12830 | if (option != (const char *) NULL) |
| 12831 | { |
| 12832 | size_t |
| 12833 | length; |
| 12834 | |
| 12835 | /* |
| 12836 | Write "nEED CACHEOFF" to turn playback caching off for streaming MNG. |
| 12837 | */ |
| 12838 | PNGType(chunk,mng_nEED); |
| 12839 | length=CopyMagickString((char *) chunk+4,"CACHEOFF",20); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 12840 | (void) WriteBlobMSBULong(image,(size_t) length); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12841 | LogPNGChunk(logging,mng_nEED,(size_t) length); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12842 | length+=4; |
| 12843 | (void) WriteBlob(image,length,chunk); |
| 12844 | (void) WriteBlobMSBULong(image,crc32(0,chunk,(uInt) length)); |
| 12845 | } |
| 12846 | if ((GetPreviousImageInList(image) == (Image *) NULL) && |
| 12847 | (GetNextImageInList(image) != (Image *) NULL) && |
| 12848 | (image->iterations != 1)) |
| 12849 | { |
| 12850 | /* |
| 12851 | Write MNG TERM chunk |
| 12852 | */ |
| 12853 | (void) WriteBlobMSBULong(image,10L); /* data length=10 */ |
| 12854 | PNGType(chunk,mng_TERM); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12855 | LogPNGChunk(logging,mng_TERM,10L); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12856 | chunk[4]=3; /* repeat animation */ |
| 12857 | chunk[5]=0; /* show last frame when done */ |
| 12858 | PNGLong(chunk+6,(png_uint_32) (mng_info->ticks_per_second* |
| 12859 | final_delay/MagickMax(image->ticks_per_second,1))); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12860 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12861 | if (image->iterations == 0) |
| 12862 | PNGLong(chunk+10,PNG_UINT_31_MAX); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12863 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12864 | else |
| 12865 | PNGLong(chunk+10,(png_uint_32) image->iterations); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12866 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12867 | if (logging != MagickFalse) |
| 12868 | { |
| 12869 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 12870 | " TERM delay: %.20g",(double) (mng_info->ticks_per_second* |
| 12871 | final_delay/MagickMax(image->ticks_per_second,1))); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12872 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12873 | if (image->iterations == 0) |
| 12874 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 12875 | " TERM iterations: %.20g",(double) PNG_UINT_31_MAX); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12876 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12877 | else |
| 12878 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 12879 | " Image iterations: %.20g",(double) image->iterations); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12880 | } |
| 12881 | (void) WriteBlob(image,14,chunk); |
| 12882 | (void) WriteBlobMSBULong(image,crc32(0,chunk,14)); |
| 12883 | } |
| 12884 | /* |
| 12885 | To do: check for cHRM+gAMA == sRGB, and write sRGB instead. |
| 12886 | */ |
| 12887 | if ((image->colorspace == sRGBColorspace || image->rendering_intent) && |
| 12888 | mng_info->equal_srgbs) |
| 12889 | { |
| 12890 | /* |
| 12891 | Write MNG sRGB chunk |
| 12892 | */ |
| 12893 | (void) WriteBlobMSBULong(image,1L); |
| 12894 | PNGType(chunk,mng_sRGB); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12895 | LogPNGChunk(logging,mng_sRGB,1L); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12896 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12897 | if (image->rendering_intent != UndefinedIntent) |
glennrp | e610a07 | 2010-08-05 17:08:46 +0000 | [diff] [blame] | 12898 | chunk[4]=(unsigned char) |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 12899 | Magick_RenderingIntent_to_PNG_RenderingIntent( |
glennrp | e610a07 | 2010-08-05 17:08:46 +0000 | [diff] [blame] | 12900 | (image->rendering_intent)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12901 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12902 | else |
glennrp | e610a07 | 2010-08-05 17:08:46 +0000 | [diff] [blame] | 12903 | chunk[4]=(unsigned char) |
glennrp | cf00202 | 2011-01-30 02:38:15 +0000 | [diff] [blame] | 12904 | Magick_RenderingIntent_to_PNG_RenderingIntent( |
| 12905 | (PerceptualIntent)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12906 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12907 | (void) WriteBlob(image,5,chunk); |
| 12908 | (void) WriteBlobMSBULong(image,crc32(0,chunk,5)); |
| 12909 | mng_info->have_write_global_srgb=MagickTrue; |
| 12910 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12911 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12912 | else |
| 12913 | { |
| 12914 | if (image->gamma && mng_info->equal_gammas) |
| 12915 | { |
| 12916 | /* |
| 12917 | Write MNG gAMA chunk |
| 12918 | */ |
| 12919 | (void) WriteBlobMSBULong(image,4L); |
| 12920 | PNGType(chunk,mng_gAMA); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12921 | LogPNGChunk(logging,mng_gAMA,4L); |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 12922 | PNGLong(chunk+4,(png_uint_32) (100000*image->gamma+0.5)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12923 | (void) WriteBlob(image,8,chunk); |
| 12924 | (void) WriteBlobMSBULong(image,crc32(0,chunk,8)); |
| 12925 | mng_info->have_write_global_gama=MagickTrue; |
| 12926 | } |
| 12927 | if (mng_info->equal_chrms) |
| 12928 | { |
| 12929 | PrimaryInfo |
| 12930 | primary; |
| 12931 | |
| 12932 | /* |
| 12933 | Write MNG cHRM chunk |
| 12934 | */ |
| 12935 | (void) WriteBlobMSBULong(image,32L); |
| 12936 | PNGType(chunk,mng_cHRM); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12937 | LogPNGChunk(logging,mng_cHRM,32L); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12938 | primary=image->chromaticity.white_point; |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 12939 | PNGLong(chunk+4,(png_uint_32) (100000*primary.x+0.5)); |
| 12940 | PNGLong(chunk+8,(png_uint_32) (100000*primary.y+0.5)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12941 | primary=image->chromaticity.red_primary; |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 12942 | PNGLong(chunk+12,(png_uint_32) (100000*primary.x+0.5)); |
| 12943 | PNGLong(chunk+16,(png_uint_32) (100000*primary.y+0.5)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12944 | primary=image->chromaticity.green_primary; |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 12945 | PNGLong(chunk+20,(png_uint_32) (100000*primary.x+0.5)); |
| 12946 | PNGLong(chunk+24,(png_uint_32) (100000*primary.y+0.5)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12947 | primary=image->chromaticity.blue_primary; |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 12948 | PNGLong(chunk+28,(png_uint_32) (100000*primary.x+0.5)); |
| 12949 | PNGLong(chunk+32,(png_uint_32) (100000*primary.y+0.5)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12950 | (void) WriteBlob(image,36,chunk); |
| 12951 | (void) WriteBlobMSBULong(image,crc32(0,chunk,36)); |
| 12952 | mng_info->have_write_global_chrm=MagickTrue; |
| 12953 | } |
| 12954 | } |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 12955 | if (image->resolution.x && image->resolution.y && mng_info->equal_physs) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12956 | { |
| 12957 | /* |
| 12958 | Write MNG pHYs chunk |
| 12959 | */ |
| 12960 | (void) WriteBlobMSBULong(image,9L); |
| 12961 | PNGType(chunk,mng_pHYs); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 12962 | LogPNGChunk(logging,mng_pHYs,9L); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12963 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12964 | if (image->units == PixelsPerInchResolution) |
| 12965 | { |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 12966 | PNGLong(chunk+4,(png_uint_32) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 12967 | (image->resolution.x*100.0/2.54+0.5)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12968 | |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 12969 | PNGLong(chunk+8,(png_uint_32) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 12970 | (image->resolution.y*100.0/2.54+0.5)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12971 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12972 | chunk[12]=1; |
| 12973 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12974 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12975 | else |
| 12976 | { |
| 12977 | if (image->units == PixelsPerCentimeterResolution) |
| 12978 | { |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 12979 | PNGLong(chunk+4,(png_uint_32) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 12980 | (image->resolution.x*100.0+0.5)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12981 | |
cristy | 35ef824 | 2010-06-03 16:24:13 +0000 | [diff] [blame] | 12982 | PNGLong(chunk+8,(png_uint_32) |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 12983 | (image->resolution.y*100.0+0.5)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12984 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12985 | chunk[12]=1; |
| 12986 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 12987 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12988 | else |
| 12989 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 12990 | PNGLong(chunk+4,(png_uint_32) (image->resolution.x+0.5)); |
| 12991 | PNGLong(chunk+8,(png_uint_32) (image->resolution.y+0.5)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12992 | chunk[12]=0; |
| 12993 | } |
| 12994 | } |
| 12995 | (void) WriteBlob(image,13,chunk); |
| 12996 | (void) WriteBlobMSBULong(image,crc32(0,chunk,13)); |
| 12997 | } |
| 12998 | /* |
| 12999 | Write MNG BACK chunk and global bKGD chunk, if the image is transparent |
| 13000 | or does not cover the entire frame. |
| 13001 | */ |
| 13002 | if (write_mng && (image->matte || image->page.x > 0 || |
| 13003 | image->page.y > 0 || (image->page.width && |
| 13004 | (image->page.width+image->page.x < mng_info->page.width)) |
| 13005 | || (image->page.height && (image->page.height+image->page.y |
| 13006 | < mng_info->page.height)))) |
| 13007 | { |
| 13008 | (void) WriteBlobMSBULong(image,6L); |
| 13009 | PNGType(chunk,mng_BACK); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 13010 | LogPNGChunk(logging,mng_BACK,6L); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13011 | red=ScaleQuantumToShort(image->background_color.red); |
| 13012 | green=ScaleQuantumToShort(image->background_color.green); |
| 13013 | blue=ScaleQuantumToShort(image->background_color.blue); |
| 13014 | PNGShort(chunk+4,red); |
| 13015 | PNGShort(chunk+6,green); |
| 13016 | PNGShort(chunk+8,blue); |
| 13017 | (void) WriteBlob(image,10,chunk); |
| 13018 | (void) WriteBlobMSBULong(image,crc32(0,chunk,10)); |
| 13019 | if (mng_info->equal_backgrounds) |
| 13020 | { |
| 13021 | (void) WriteBlobMSBULong(image,6L); |
| 13022 | PNGType(chunk,mng_bKGD); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 13023 | LogPNGChunk(logging,mng_bKGD,6L); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13024 | (void) WriteBlob(image,10,chunk); |
| 13025 | (void) WriteBlobMSBULong(image,crc32(0,chunk,10)); |
| 13026 | } |
| 13027 | } |
| 13028 | |
| 13029 | #ifdef PNG_WRITE_EMPTY_PLTE_SUPPORTED |
| 13030 | if ((need_local_plte == MagickFalse) && |
| 13031 | (image->storage_class == PseudoClass) && |
| 13032 | (all_images_are_gray == MagickFalse)) |
| 13033 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 13034 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13035 | data_length; |
| 13036 | |
| 13037 | /* |
| 13038 | Write MNG PLTE chunk |
| 13039 | */ |
| 13040 | data_length=3*image->colors; |
| 13041 | (void) WriteBlobMSBULong(image,data_length); |
| 13042 | PNGType(chunk,mng_PLTE); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 13043 | LogPNGChunk(logging,mng_PLTE,data_length); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13044 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 13045 | for (i=0; i < (ssize_t) image->colors; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13046 | { |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 13047 | chunk[4+i*3]=(unsigned char) (ScaleQuantumToChar( |
| 13048 | image->colormap[i].red) & 0xff); |
| 13049 | chunk[5+i*3]=(unsigned char) (ScaleQuantumToChar( |
| 13050 | image->colormap[i].green) & 0xff); |
| 13051 | chunk[6+i*3]=(unsigned char) (ScaleQuantumToChar( |
| 13052 | image->colormap[i].blue) & 0xff); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13053 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13054 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13055 | (void) WriteBlob(image,data_length+4,chunk); |
| 13056 | (void) WriteBlobMSBULong(image,crc32(0,chunk,(uInt) (data_length+4))); |
| 13057 | mng_info->have_write_global_plte=MagickTrue; |
| 13058 | } |
| 13059 | #endif |
| 13060 | } |
| 13061 | scene=0; |
| 13062 | mng_info->delay=0; |
| 13063 | #if defined(PNG_WRITE_EMPTY_PLTE_SUPPORTED) || \ |
| 13064 | defined(PNG_MNG_FEATURES_SUPPORTED) |
| 13065 | mng_info->equal_palettes=MagickFalse; |
| 13066 | #endif |
| 13067 | do |
| 13068 | { |
| 13069 | if (mng_info->adjoin) |
| 13070 | { |
| 13071 | #if defined(PNG_WRITE_EMPTY_PLTE_SUPPORTED) || \ |
| 13072 | defined(PNG_MNG_FEATURES_SUPPORTED) |
| 13073 | /* |
| 13074 | If we aren't using a global palette for the entire MNG, check to |
| 13075 | see if we can use one for two or more consecutive images. |
| 13076 | */ |
| 13077 | if (need_local_plte && use_global_plte && !all_images_are_gray) |
| 13078 | { |
| 13079 | if (mng_info->IsPalette) |
| 13080 | { |
| 13081 | /* |
| 13082 | When equal_palettes is true, this image has the same palette |
| 13083 | as the previous PseudoClass image |
| 13084 | */ |
| 13085 | mng_info->have_write_global_plte=mng_info->equal_palettes; |
| 13086 | mng_info->equal_palettes=PalettesAreEqual(image,image->next); |
| 13087 | if (mng_info->equal_palettes && !mng_info->have_write_global_plte) |
| 13088 | { |
| 13089 | /* |
| 13090 | Write MNG PLTE chunk |
| 13091 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 13092 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13093 | data_length; |
| 13094 | |
| 13095 | data_length=3*image->colors; |
| 13096 | (void) WriteBlobMSBULong(image,data_length); |
| 13097 | PNGType(chunk,mng_PLTE); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 13098 | LogPNGChunk(logging,mng_PLTE,data_length); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13099 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 13100 | for (i=0; i < (ssize_t) image->colors; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13101 | { |
| 13102 | chunk[4+i*3]=ScaleQuantumToChar(image->colormap[i].red); |
| 13103 | chunk[5+i*3]=ScaleQuantumToChar(image->colormap[i].green); |
| 13104 | chunk[6+i*3]=ScaleQuantumToChar(image->colormap[i].blue); |
| 13105 | } |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13106 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13107 | (void) WriteBlob(image,data_length+4,chunk); |
| 13108 | (void) WriteBlobMSBULong(image,crc32(0,chunk, |
| 13109 | (uInt) (data_length+4))); |
| 13110 | mng_info->have_write_global_plte=MagickTrue; |
| 13111 | } |
| 13112 | } |
| 13113 | else |
| 13114 | mng_info->have_write_global_plte=MagickFalse; |
| 13115 | } |
| 13116 | #endif |
| 13117 | if (need_defi) |
| 13118 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 13119 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13120 | previous_x, |
| 13121 | previous_y; |
| 13122 | |
| 13123 | if (scene) |
| 13124 | { |
| 13125 | previous_x=mng_info->page.x; |
| 13126 | previous_y=mng_info->page.y; |
| 13127 | } |
| 13128 | else |
| 13129 | { |
| 13130 | previous_x=0; |
| 13131 | previous_y=0; |
| 13132 | } |
| 13133 | mng_info->page=image->page; |
| 13134 | if ((mng_info->page.x != previous_x) || |
| 13135 | (mng_info->page.y != previous_y)) |
| 13136 | { |
| 13137 | (void) WriteBlobMSBULong(image,12L); /* data length=12 */ |
| 13138 | PNGType(chunk,mng_DEFI); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 13139 | LogPNGChunk(logging,mng_DEFI,12L); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13140 | chunk[4]=0; /* object 0 MSB */ |
| 13141 | chunk[5]=0; /* object 0 LSB */ |
| 13142 | chunk[6]=0; /* visible */ |
| 13143 | chunk[7]=0; /* abstract */ |
| 13144 | PNGLong(chunk+8,(png_uint_32) mng_info->page.x); |
| 13145 | PNGLong(chunk+12,(png_uint_32) mng_info->page.y); |
| 13146 | (void) WriteBlob(image,16,chunk); |
| 13147 | (void) WriteBlobMSBULong(image,crc32(0,chunk,16)); |
| 13148 | } |
| 13149 | } |
| 13150 | } |
| 13151 | |
| 13152 | mng_info->write_mng=write_mng; |
| 13153 | |
| 13154 | if ((int) image->dispose >= 3) |
| 13155 | mng_info->framing_mode=3; |
| 13156 | |
| 13157 | if (mng_info->need_fram && mng_info->adjoin && |
| 13158 | ((image->delay != mng_info->delay) || |
| 13159 | (mng_info->framing_mode != mng_info->old_framing_mode))) |
| 13160 | { |
| 13161 | if (image->delay == mng_info->delay) |
| 13162 | { |
| 13163 | /* |
| 13164 | Write a MNG FRAM chunk with the new framing mode. |
| 13165 | */ |
| 13166 | (void) WriteBlobMSBULong(image,1L); /* data length=1 */ |
| 13167 | PNGType(chunk,mng_FRAM); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 13168 | LogPNGChunk(logging,mng_FRAM,1L); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13169 | chunk[4]=(unsigned char) mng_info->framing_mode; |
| 13170 | (void) WriteBlob(image,5,chunk); |
| 13171 | (void) WriteBlobMSBULong(image,crc32(0,chunk,5)); |
| 13172 | } |
| 13173 | else |
| 13174 | { |
| 13175 | /* |
| 13176 | Write a MNG FRAM chunk with the delay. |
| 13177 | */ |
| 13178 | (void) WriteBlobMSBULong(image,10L); /* data length=10 */ |
| 13179 | PNGType(chunk,mng_FRAM); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 13180 | LogPNGChunk(logging,mng_FRAM,10L); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13181 | chunk[4]=(unsigned char) mng_info->framing_mode; |
| 13182 | chunk[5]=0; /* frame name separator (no name) */ |
| 13183 | chunk[6]=2; /* flag for changing default delay */ |
| 13184 | chunk[7]=0; /* flag for changing frame timeout */ |
| 13185 | chunk[8]=0; /* flag for changing frame clipping */ |
| 13186 | chunk[9]=0; /* flag for changing frame sync_id */ |
| 13187 | PNGLong(chunk+10,(png_uint_32) |
| 13188 | ((mng_info->ticks_per_second* |
| 13189 | image->delay)/MagickMax(image->ticks_per_second,1))); |
| 13190 | (void) WriteBlob(image,14,chunk); |
| 13191 | (void) WriteBlobMSBULong(image,crc32(0,chunk,14)); |
cristy | 4e5bc84 | 2010-06-09 13:56:01 +0000 | [diff] [blame] | 13192 | mng_info->delay=(png_uint_32) image->delay; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13193 | } |
| 13194 | mng_info->old_framing_mode=mng_info->framing_mode; |
| 13195 | } |
| 13196 | |
| 13197 | #if defined(JNG_SUPPORTED) |
| 13198 | if (image_info->compression == JPEGCompression) |
| 13199 | { |
| 13200 | ImageInfo |
| 13201 | *write_info; |
| 13202 | |
| 13203 | if (logging != MagickFalse) |
| 13204 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 13205 | " Writing JNG object."); |
| 13206 | /* To do: specify the desired alpha compression method. */ |
| 13207 | write_info=CloneImageInfo(image_info); |
| 13208 | write_info->compression=UndefinedCompression; |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 13209 | status=WriteOneJNGImage(mng_info,write_info,image,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13210 | write_info=DestroyImageInfo(write_info); |
| 13211 | } |
| 13212 | else |
| 13213 | #endif |
| 13214 | { |
| 13215 | if (logging != MagickFalse) |
| 13216 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 13217 | " Writing PNG object."); |
glennrp | 2f2e514 | 2010-12-23 19:13:35 +0000 | [diff] [blame] | 13218 | |
glennrp | b9cfe27 | 2010-12-21 15:08:06 +0000 | [diff] [blame] | 13219 | mng_info->need_blob = MagickFalse; |
glennrp | 8d3d6e5 | 2011-04-19 04:39:51 +0000 | [diff] [blame] | 13220 | mng_info->ping_preserve_colormap = MagickFalse; |
glennrp | 2f2e514 | 2010-12-23 19:13:35 +0000 | [diff] [blame] | 13221 | |
| 13222 | /* We don't want any ancillary chunks written */ |
| 13223 | mng_info->ping_exclude_bKGD=MagickTrue; |
| 13224 | mng_info->ping_exclude_cHRM=MagickTrue; |
glennrp | a0ed009 | 2011-04-18 16:36:29 +0000 | [diff] [blame] | 13225 | mng_info->ping_exclude_date=MagickTrue; |
glennrp | 2f2e514 | 2010-12-23 19:13:35 +0000 | [diff] [blame] | 13226 | mng_info->ping_exclude_EXIF=MagickTrue; |
| 13227 | mng_info->ping_exclude_gAMA=MagickTrue; |
glennrp | 2f2e514 | 2010-12-23 19:13:35 +0000 | [diff] [blame] | 13228 | mng_info->ping_exclude_iCCP=MagickTrue; |
| 13229 | /* mng_info->ping_exclude_iTXt=MagickTrue; */ |
| 13230 | mng_info->ping_exclude_oFFs=MagickTrue; |
| 13231 | mng_info->ping_exclude_pHYs=MagickTrue; |
| 13232 | mng_info->ping_exclude_sRGB=MagickTrue; |
| 13233 | mng_info->ping_exclude_tEXt=MagickTrue; |
glennrp | a1e3b7b | 2010-12-24 16:37:33 +0000 | [diff] [blame] | 13234 | mng_info->ping_exclude_tRNS=MagickTrue; |
glennrp | 2f2e514 | 2010-12-23 19:13:35 +0000 | [diff] [blame] | 13235 | mng_info->ping_exclude_vpAg=MagickTrue; |
| 13236 | mng_info->ping_exclude_zCCP=MagickTrue; |
| 13237 | mng_info->ping_exclude_zTXt=MagickTrue; |
| 13238 | |
cristy | 16ea139 | 2012-03-21 20:38:41 +0000 | [diff] [blame] | 13239 | status=WriteOnePNGImage(mng_info,image_info,image,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13240 | } |
| 13241 | |
| 13242 | if (status == MagickFalse) |
| 13243 | { |
| 13244 | MngInfoFreeStruct(mng_info,&have_mng_structure); |
| 13245 | (void) CloseBlob(image); |
| 13246 | return(MagickFalse); |
| 13247 | } |
| 13248 | (void) CatchImageException(image); |
| 13249 | if (GetNextImageInList(image) == (Image *) NULL) |
| 13250 | break; |
| 13251 | image=SyncNextImageInList(image); |
| 13252 | status=SetImageProgress(image,SaveImagesTag,scene++, |
| 13253 | GetImageListLength(image)); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13254 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13255 | if (status == MagickFalse) |
| 13256 | break; |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13257 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13258 | } while (mng_info->adjoin); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13259 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13260 | if (write_mng) |
| 13261 | { |
| 13262 | while (GetPreviousImageInList(image) != (Image *) NULL) |
| 13263 | image=GetPreviousImageInList(image); |
| 13264 | /* |
| 13265 | Write the MEND chunk. |
| 13266 | */ |
| 13267 | (void) WriteBlobMSBULong(image,0x00000000L); |
| 13268 | PNGType(chunk,mng_MEND); |
glennrp | 03812ae | 2010-12-24 01:31:34 +0000 | [diff] [blame] | 13269 | LogPNGChunk(logging,mng_MEND,0L); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13270 | (void) WriteBlob(image,4,chunk); |
| 13271 | (void) WriteBlobMSBULong(image,crc32(0,chunk,4)); |
| 13272 | } |
| 13273 | /* |
| 13274 | Relinquish resources. |
| 13275 | */ |
| 13276 | (void) CloseBlob(image); |
| 13277 | MngInfoFreeStruct(mng_info,&have_mng_structure); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13278 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13279 | if (logging != MagickFalse) |
| 13280 | (void) LogMagickEvent(CoderEvent,GetMagickModule(),"exit WriteMNGImage()"); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13281 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13282 | return(MagickTrue); |
| 13283 | } |
glennrp | d5045b4 | 2010-03-24 12:40:35 +0000 | [diff] [blame] | 13284 | #else /* PNG_LIBPNG_VER > 10011 */ |
glennrp | 39992b4 | 2010-11-14 00:03:43 +0000 | [diff] [blame] | 13285 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13286 | static MagickBooleanType WritePNGImage(const ImageInfo *image_info,Image *image) |
| 13287 | { |
glennrp | 3bd393f | 2011-12-21 18:54:53 +0000 | [diff] [blame] | 13288 | (void) image; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13289 | printf("Your PNG library is too old: You have libpng-%s\n", |
| 13290 | PNG_LIBPNG_VER_STRING); |
glennrp | 0fe50b4 | 2010-11-16 03:52:51 +0000 | [diff] [blame] | 13291 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13292 | ThrowBinaryException(CoderError,"PNG library is too old", |
| 13293 | image_info->filename); |
| 13294 | } |
glennrp | 39992b4 | 2010-11-14 00:03:43 +0000 | [diff] [blame] | 13295 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13296 | static MagickBooleanType WriteMNGImage(const ImageInfo *image_info,Image *image) |
| 13297 | { |
| 13298 | return(WritePNGImage(image_info,image)); |
| 13299 | } |
glennrp | d5045b4 | 2010-03-24 12:40:35 +0000 | [diff] [blame] | 13300 | #endif /* PNG_LIBPNG_VER > 10011 */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 13301 | #endif |