blob: 324b41a6740bcc5f3b9fde4a6ed2cd95440df54b [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
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% %
cristy1454be72011-12-19 01:52:48 +000021% Copyright 1999-2012 ImageMagick Studio LLC, a non-profit organization %
cristy3ed852e2009-09-05 21:47:34 +000022% 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*/
cristy4c08aed2011-07-01 19:47:50 +000044#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"
50#include "MagickCore/color.h"
51#include "MagickCore/color-private.h"
52#include "MagickCore/colormap.h"
53#include "MagickCore/colorspace.h"
cristy510d06a2011-07-06 23:43:54 +000054#include "MagickCore/colorspace-private.h"
cristy4c08aed2011-07-01 19:47:50 +000055#include "MagickCore/constitute.h"
56#include "MagickCore/enhance.h"
57#include "MagickCore/exception.h"
58#include "MagickCore/exception-private.h"
59#include "MagickCore/geometry.h"
60#include "MagickCore/histogram.h"
61#include "MagickCore/image.h"
62#include "MagickCore/image-private.h"
63#include "MagickCore/layer.h"
64#include "MagickCore/list.h"
65#include "MagickCore/log.h"
66#include "MagickCore/MagickCore.h"
67#include "MagickCore/memory_.h"
68#include "MagickCore/module.h"
69#include "MagickCore/monitor.h"
70#include "MagickCore/monitor-private.h"
71#include "MagickCore/option.h"
72#include "MagickCore/pixel.h"
73#include "MagickCore/pixel-accessor.h"
74#include "MagickCore/profile.h"
75#include "MagickCore/property.h"
76#include "MagickCore/quantum-private.h"
77#include "MagickCore/resource_.h"
78#include "MagickCore/semaphore.h"
79#include "MagickCore/quantum-private.h"
80#include "MagickCore/static.h"
81#include "MagickCore/statistic.h"
82#include "MagickCore/string_.h"
83#include "MagickCore/string-private.h"
84#include "MagickCore/transform.h"
85#include "MagickCore/utility.h"
cristy3ed852e2009-09-05 21:47:34 +000086#if defined(MAGICKCORE_PNG_DELEGATE)
glennrp286a6352009-11-09 02:58:50 +000087
glennrp7ef138c2009-11-10 13:50:20 +000088/* Suppress libpng pedantic warnings that were added in
89 * libpng-1.2.41 and libpng-1.4.0. If you are working on
glennrpfaa852b2010-03-30 12:17:00 +000090 * migration to libpng-1.5, remove these defines and then
glennrp7ef138c2009-11-10 13:50:20 +000091 * fix any code that generates warnings.
92 */
glennrp991e92a2010-01-28 03:09:00 +000093/* #define PNG_DEPRECATED Use of this function is deprecated */
glennrpfaa852b2010-03-30 12:17:00 +000094/* #define PNG_USE_RESULT The result of this function must be checked */
95/* #define PNG_NORETURN This function does not return */
96/* #define PNG_ALLOCATED The result of the function is new memory */
glennrp8371ecc2011-02-19 19:15:38 +000097/* #define PNG_DEPSTRUCT Access to this struct member is deprecated */
glennrp5b927342011-04-14 14:31:48 +000098
99/* PNG_PTR_NORETURN does not work on some platforms, in libpng-1.5.x */
cristy75cfe702011-04-14 14:27:17 +0000100#define PNG_PTR_NORETURN
glennrp286a6352009-11-09 02:58:50 +0000101
cristy3ed852e2009-09-05 21:47:34 +0000102#include "png.h"
103#include "zlib.h"
104
105/* ImageMagick differences */
106#define first_scene scene
107
glennrpd5045b42010-03-24 12:40:35 +0000108#if PNG_LIBPNG_VER > 10011
cristy3ed852e2009-09-05 21:47:34 +0000109/*
110 Optional declarations. Define or undefine them as you like.
111*/
112/* #define PNG_DEBUG -- turning this on breaks VisualC compiling */
113
114/*
115 Features under construction. Define these to work on them.
116*/
117#undef MNG_OBJECT_BUFFERS
118#undef MNG_BASI_SUPPORTED
119#define MNG_COALESCE_LAYERS /* In 5.4.4, this interfered with MMAP'ed files. */
120#define MNG_INSERT_LAYERS /* Troublesome, but seem to work as of 5.4.4 */
cristy3ed852e2009-09-05 21:47:34 +0000121#if defined(MAGICKCORE_JPEG_DELEGATE)
122# define JNG_SUPPORTED /* Not finished as of 5.5.2. See "To do" comments. */
123#endif
124#if !defined(RGBColorMatchExact)
125#define IsPNGColorEqual(color,target) \
glennrp8e045c82011-04-27 16:40:27 +0000126 (((color).red == (target).red) && \
127 ((color).green == (target).green) && \
128 ((color).blue == (target).blue))
cristy3ed852e2009-09-05 21:47:34 +0000129#endif
130
glennrp8e58efd2011-05-20 12:16:29 +0000131/* Macros for left-bit-replication to ensure that pixels
glennrp1a2061f2011-10-18 12:30:45 +0000132 * and PixelInfos all have the same image->depth, and for use
glennrp8e58efd2011-05-20 12:16:29 +0000133 * in PNG8 quantization.
134 */
135
136
137/* LBR01: Replicate top bit */
138
glennrp05001c32011-08-06 13:04:16 +0000139#define LBR01PacketRed(pixelpacket) \
glennrp8e58efd2011-05-20 12:16:29 +0000140 (pixelpacket).red=(ScaleQuantumToChar((pixelpacket).red) < 0x10 ? \
141 0 : QuantumRange);
142
glennrp91d99252011-06-25 14:30:13 +0000143#define LBR01PacketGreen(pixelpacket) \
glennrp8e58efd2011-05-20 12:16:29 +0000144 (pixelpacket).green=(ScaleQuantumToChar((pixelpacket).green) < 0x10 ? \
145 0 : QuantumRange);
146
glennrp91d99252011-06-25 14:30:13 +0000147#define LBR01PacketBlue(pixelpacket) \
glennrp8e58efd2011-05-20 12:16:29 +0000148 (pixelpacket).blue=(ScaleQuantumToChar((pixelpacket).blue) < 0x10 ? \
149 0 : QuantumRange);
150
cristy4c08aed2011-07-01 19:47:50 +0000151#define LBR01PacketAlpha(pixelpacket) \
152 (pixelpacket).alpha=(ScaleQuantumToChar((pixelpacket).alpha) < 0x10 ? \
glennrp8e58efd2011-05-20 12:16:29 +0000153 0 : QuantumRange);
154
glennrp91d99252011-06-25 14:30:13 +0000155#define LBR01PacketRGB(pixelpacket) \
glennrpbb4f99d2011-05-22 11:13:17 +0000156 { \
glennrp05001c32011-08-06 13:04:16 +0000157 LBR01PacketRed((pixelpacket)); \
glennrp91d99252011-06-25 14:30:13 +0000158 LBR01PacketGreen((pixelpacket)); \
159 LBR01PacketBlue((pixelpacket)); \
glennrpbb4f99d2011-05-22 11:13:17 +0000160 }
glennrp8e58efd2011-05-20 12:16:29 +0000161
glennrp91d99252011-06-25 14:30:13 +0000162#define LBR01PacketRGBO(pixelpacket) \
glennrpbb4f99d2011-05-22 11:13:17 +0000163 { \
glennrp91d99252011-06-25 14:30:13 +0000164 LBR01PacketRGB((pixelpacket)); \
cristy4c08aed2011-07-01 19:47:50 +0000165 LBR01PacketAlpha((pixelpacket)); \
glennrpbb4f99d2011-05-22 11:13:17 +0000166 }
glennrp8e58efd2011-05-20 12:16:29 +0000167
cristyef618312011-06-25 12:26:44 +0000168#define LBR01PixelRed(pixel) \
cristy4c08aed2011-07-01 19:47:50 +0000169 (ScaleQuantumToChar(GetPixelRed(image,(pixel))) < 0x10 ? \
glennrp8e58efd2011-05-20 12:16:29 +0000170 0 : QuantumRange);
171
glennrp54cf7972011-08-06 14:28:09 +0000172#define LBR01PixelGreen(pixel) \
cristy4c08aed2011-07-01 19:47:50 +0000173 (ScaleQuantumToChar(GetPixelGreen(image,(pixel))) < 0x10 ? \
glennrp8e58efd2011-05-20 12:16:29 +0000174 0 : QuantumRange);
175
glennrp54cf7972011-08-06 14:28:09 +0000176#define LBR01PixelBlue(pixel) \
cristy4c08aed2011-07-01 19:47:50 +0000177 (ScaleQuantumToChar(GetPixelBlue(image,(pixel))) < 0x10 ? \
glennrp8e58efd2011-05-20 12:16:29 +0000178 0 : QuantumRange);
179
glennrp54cf7972011-08-06 14:28:09 +0000180#define LBR01PixelAlpha(pixel) \
cristy4c08aed2011-07-01 19:47:50 +0000181 (ScaleQuantumToChar(GetPixelAlpha(image,(pixel))) < 0x10 ? \
glennrp8e58efd2011-05-20 12:16:29 +0000182 0 : QuantumRange);
183
glennrp54cf7972011-08-06 14:28:09 +0000184#define LBR01PixelRGB(pixel) \
glennrpbb4f99d2011-05-22 11:13:17 +0000185 { \
cristyef618312011-06-25 12:26:44 +0000186 LBR01PixelRed((pixel)); \
glennrp54cf7972011-08-06 14:28:09 +0000187 LBR01PixelGreen((pixel)); \
188 LBR01PixelBlue((pixel)); \
glennrpbb4f99d2011-05-22 11:13:17 +0000189 }
glennrp8e58efd2011-05-20 12:16:29 +0000190
glennrp54cf7972011-08-06 14:28:09 +0000191#define LBR01PixelRGBA(pixel) \
glennrpbb4f99d2011-05-22 11:13:17 +0000192 { \
glennrp54cf7972011-08-06 14:28:09 +0000193 LBR01PixelRGB((pixel)); \
194 LBR01PixelAlpha((pixel)); \
glennrpbb4f99d2011-05-22 11:13:17 +0000195 }
glennrp8e58efd2011-05-20 12:16:29 +0000196
197/* LBR02: Replicate top 2 bits */
198
glennrp05001c32011-08-06 13:04:16 +0000199#define LBR02PacketRed(pixelpacket) \
glennrp8e58efd2011-05-20 12:16:29 +0000200 { \
201 unsigned char lbr_bits=ScaleQuantumToChar((pixelpacket).red) & 0xc0; \
202 (pixelpacket).red=ScaleCharToQuantum( \
203 (lbr_bits | (lbr_bits >> 2) | (lbr_bits >> 4) | (lbr_bits >> 6))); \
204 }
glennrp91d99252011-06-25 14:30:13 +0000205#define LBR02PacketGreen(pixelpacket) \
glennrp8e58efd2011-05-20 12:16:29 +0000206 { \
207 unsigned char lbr_bits=ScaleQuantumToChar((pixelpacket).green) & 0xc0; \
208 (pixelpacket).green=ScaleCharToQuantum( \
209 (lbr_bits | (lbr_bits >> 2) | (lbr_bits >> 4) | (lbr_bits >> 6))); \
210 }
glennrp91d99252011-06-25 14:30:13 +0000211#define LBR02PacketBlue(pixelpacket) \
glennrp8e58efd2011-05-20 12:16:29 +0000212 { \
213 unsigned char lbr_bits=ScaleQuantumToChar((pixelpacket).blue) & 0xc0; \
214 (pixelpacket).blue=ScaleCharToQuantum( \
215 (lbr_bits | (lbr_bits >> 2) | (lbr_bits >> 4) | (lbr_bits >> 6))); \
216 }
cristy4c08aed2011-07-01 19:47:50 +0000217#define LBR02PacketAlpha(pixelpacket) \
glennrp8e58efd2011-05-20 12:16:29 +0000218 { \
cristy4c08aed2011-07-01 19:47:50 +0000219 unsigned char lbr_bits=ScaleQuantumToChar((pixelpacket).alpha) & 0xc0; \
220 (pixelpacket).alpha=ScaleCharToQuantum( \
glennrp8e58efd2011-05-20 12:16:29 +0000221 (lbr_bits | (lbr_bits >> 2) | (lbr_bits >> 4) | (lbr_bits >> 6))); \
222 }
223
glennrp91d99252011-06-25 14:30:13 +0000224#define LBR02PacketRGB(pixelpacket) \
glennrpbb4f99d2011-05-22 11:13:17 +0000225 { \
glennrp05001c32011-08-06 13:04:16 +0000226 LBR02PacketRed((pixelpacket)); \
glennrp91d99252011-06-25 14:30:13 +0000227 LBR02PacketGreen((pixelpacket)); \
228 LBR02PacketBlue((pixelpacket)); \
glennrpbb4f99d2011-05-22 11:13:17 +0000229 }
glennrp8e58efd2011-05-20 12:16:29 +0000230
glennrp91d99252011-06-25 14:30:13 +0000231#define LBR02PacketRGBO(pixelpacket) \
glennrpbb4f99d2011-05-22 11:13:17 +0000232 { \
glennrp91d99252011-06-25 14:30:13 +0000233 LBR02PacketRGB((pixelpacket)); \
cristy4c08aed2011-07-01 19:47:50 +0000234 LBR02PacketAlpha((pixelpacket)); \
glennrpbb4f99d2011-05-22 11:13:17 +0000235 }
glennrp8e58efd2011-05-20 12:16:29 +0000236
cristyef618312011-06-25 12:26:44 +0000237#define LBR02PixelRed(pixel) \
glennrp8e58efd2011-05-20 12:16:29 +0000238 { \
cristy4c08aed2011-07-01 19:47:50 +0000239 unsigned char lbr_bits=ScaleQuantumToChar(GetPixelRed(image,(pixel))) \
glennrp8e58efd2011-05-20 12:16:29 +0000240 & 0xc0; \
cristy4c08aed2011-07-01 19:47:50 +0000241 SetPixelRed(image, ScaleCharToQuantum( \
glennrp847370c2011-07-05 17:37:15 +0000242 (lbr_bits | (lbr_bits >> 2) | (lbr_bits >> 4) | (lbr_bits >> 6))), \
243 (pixel)); \
glennrp8e58efd2011-05-20 12:16:29 +0000244 }
glennrp54cf7972011-08-06 14:28:09 +0000245#define LBR02PixelGreen(pixel) \
glennrp8e58efd2011-05-20 12:16:29 +0000246 { \
cristy4c08aed2011-07-01 19:47:50 +0000247 unsigned char lbr_bits=ScaleQuantumToChar(GetPixelGreen(image,(pixel)))\
glennrp8e58efd2011-05-20 12:16:29 +0000248 & 0xc0; \
cristy4c08aed2011-07-01 19:47:50 +0000249 SetPixelGreen(image, ScaleCharToQuantum( \
glennrp847370c2011-07-05 17:37:15 +0000250 (lbr_bits | (lbr_bits >> 2) | (lbr_bits >> 4) | (lbr_bits >> 6))), \
251 (pixel)); \
glennrp8e58efd2011-05-20 12:16:29 +0000252 }
glennrp54cf7972011-08-06 14:28:09 +0000253#define LBR02PixelBlue(pixel) \
glennrp8e58efd2011-05-20 12:16:29 +0000254 { \
255 unsigned char lbr_bits= \
cristy4c08aed2011-07-01 19:47:50 +0000256 ScaleQuantumToChar(GetPixelBlue(image,(pixel))) & 0xc0; \
257 SetPixelBlue(image, ScaleCharToQuantum( \
glennrp847370c2011-07-05 17:37:15 +0000258 (lbr_bits | (lbr_bits >> 2) | (lbr_bits >> 4) | (lbr_bits >> 6))), \
259 (pixel)); \
glennrp8e58efd2011-05-20 12:16:29 +0000260 }
glennrp54cf7972011-08-06 14:28:09 +0000261#define LBR02PixelAlpha(pixel) \
glennrp8e58efd2011-05-20 12:16:29 +0000262 { \
263 unsigned char lbr_bits= \
cristy4c08aed2011-07-01 19:47:50 +0000264 ScaleQuantumToChar(GetPixelAlpha(image,(pixel))) & 0xc0; \
265 SetPixelAlpha(image, ScaleCharToQuantum( \
glennrp847370c2011-07-05 17:37:15 +0000266 (lbr_bits | (lbr_bits >> 2) | (lbr_bits >> 4) | (lbr_bits >> 6))), \
267 (pixel) ); \
glennrp8e58efd2011-05-20 12:16:29 +0000268 }
269
glennrp54cf7972011-08-06 14:28:09 +0000270#define LBR02PixelRGB(pixel) \
glennrpbb4f99d2011-05-22 11:13:17 +0000271 { \
cristyef618312011-06-25 12:26:44 +0000272 LBR02PixelRed((pixel)); \
glennrp54cf7972011-08-06 14:28:09 +0000273 LBR02PixelGreen((pixel)); \
274 LBR02PixelBlue((pixel)); \
glennrpbb4f99d2011-05-22 11:13:17 +0000275 }
glennrp8e58efd2011-05-20 12:16:29 +0000276
glennrp54cf7972011-08-06 14:28:09 +0000277#define LBR02PixelRGBA(pixel) \
glennrpbb4f99d2011-05-22 11:13:17 +0000278 { \
glennrp54cf7972011-08-06 14:28:09 +0000279 LBR02PixelRGB((pixel)); \
280 LBR02PixelAlpha((pixel)); \
glennrpbb4f99d2011-05-22 11:13:17 +0000281 }
glennrp8e58efd2011-05-20 12:16:29 +0000282
283/* LBR03: Replicate top 3 bits (only used with opaque pixels during
284 PNG8 quantization) */
285
glennrp05001c32011-08-06 13:04:16 +0000286#define LBR03PacketRed(pixelpacket) \
glennrp8e58efd2011-05-20 12:16:29 +0000287 { \
288 unsigned char lbr_bits=ScaleQuantumToChar((pixelpacket).red) & 0xe0; \
289 (pixelpacket).red=ScaleCharToQuantum( \
290 (lbr_bits | (lbr_bits >> 3) | (lbr_bits >> 6))); \
291 }
glennrp91d99252011-06-25 14:30:13 +0000292#define LBR03PacketGreen(pixelpacket) \
glennrp8e58efd2011-05-20 12:16:29 +0000293 { \
294 unsigned char lbr_bits=ScaleQuantumToChar((pixelpacket).green) & 0xe0; \
295 (pixelpacket).green=ScaleCharToQuantum( \
296 (lbr_bits | (lbr_bits >> 3) | (lbr_bits >> 6))); \
297 }
glennrp91d99252011-06-25 14:30:13 +0000298#define LBR03PacketBlue(pixelpacket) \
glennrp8e58efd2011-05-20 12:16:29 +0000299 { \
300 unsigned char lbr_bits=ScaleQuantumToChar((pixelpacket).blue) & 0xe0; \
301 (pixelpacket).blue=ScaleCharToQuantum( \
302 (lbr_bits | (lbr_bits >> 3) | (lbr_bits >> 6))); \
303 }
304
glennrp91d99252011-06-25 14:30:13 +0000305#define LBR03PacketRGB(pixelpacket) \
glennrpbb4f99d2011-05-22 11:13:17 +0000306 { \
glennrp05001c32011-08-06 13:04:16 +0000307 LBR03PacketRed((pixelpacket)); \
glennrp91d99252011-06-25 14:30:13 +0000308 LBR03PacketGreen((pixelpacket)); \
309 LBR03PacketBlue((pixelpacket)); \
glennrpbb4f99d2011-05-22 11:13:17 +0000310 }
glennrp8e58efd2011-05-20 12:16:29 +0000311
cristyef618312011-06-25 12:26:44 +0000312#define LBR03PixelRed(pixel) \
glennrp8e58efd2011-05-20 12:16:29 +0000313 { \
cristy4c08aed2011-07-01 19:47:50 +0000314 unsigned char lbr_bits=ScaleQuantumToChar(GetPixelRed(image,(pixel))) \
glennrp8e58efd2011-05-20 12:16:29 +0000315 & 0xe0; \
cristy4c08aed2011-07-01 19:47:50 +0000316 SetPixelRed(image, ScaleCharToQuantum( \
317 (lbr_bits | (lbr_bits >> 3) | (lbr_bits >> 6))), (pixel)); \
glennrp8e58efd2011-05-20 12:16:29 +0000318 }
cristy4c08aed2011-07-01 19:47:50 +0000319#define LBR03Green(pixel) \
glennrp8e58efd2011-05-20 12:16:29 +0000320 { \
cristy4c08aed2011-07-01 19:47:50 +0000321 unsigned char lbr_bits=ScaleQuantumToChar(GetPixelGreen(image,(pixel)))\
glennrp8e58efd2011-05-20 12:16:29 +0000322 & 0xe0; \
cristy4c08aed2011-07-01 19:47:50 +0000323 SetPixelGreen(image, ScaleCharToQuantum( \
324 (lbr_bits | (lbr_bits >> 3) | (lbr_bits >> 6))), (pixel)); \
glennrp8e58efd2011-05-20 12:16:29 +0000325 }
cristy4c08aed2011-07-01 19:47:50 +0000326#define LBR03Blue(pixel) \
glennrp8e58efd2011-05-20 12:16:29 +0000327 { \
cristy4c08aed2011-07-01 19:47:50 +0000328 unsigned char lbr_bits=ScaleQuantumToChar(GetPixelBlue(image,(pixel))) \
glennrp8e58efd2011-05-20 12:16:29 +0000329 & 0xe0; \
cristy4c08aed2011-07-01 19:47:50 +0000330 SetPixelBlue(image, ScaleCharToQuantum( \
331 (lbr_bits | (lbr_bits >> 3) | (lbr_bits >> 6))), (pixel)); \
glennrp8e58efd2011-05-20 12:16:29 +0000332 }
333
cristy4c08aed2011-07-01 19:47:50 +0000334#define LBR03RGB(pixel) \
glennrpbb4f99d2011-05-22 11:13:17 +0000335 { \
cristyef618312011-06-25 12:26:44 +0000336 LBR03PixelRed((pixel)); \
cristy4c08aed2011-07-01 19:47:50 +0000337 LBR03Green((pixel)); \
338 LBR03Blue((pixel)); \
glennrpbb4f99d2011-05-22 11:13:17 +0000339 }
glennrp8e58efd2011-05-20 12:16:29 +0000340
341/* LBR04: Replicate top 4 bits */
342
glennrp05001c32011-08-06 13:04:16 +0000343#define LBR04PacketRed(pixelpacket) \
glennrp8e58efd2011-05-20 12:16:29 +0000344 { \
345 unsigned char lbr_bits=ScaleQuantumToChar((pixelpacket).red) & 0xf0; \
346 (pixelpacket).red=ScaleCharToQuantum((lbr_bits | (lbr_bits >> 4))); \
347 }
glennrp91d99252011-06-25 14:30:13 +0000348#define LBR04PacketGreen(pixelpacket) \
glennrp8e58efd2011-05-20 12:16:29 +0000349 { \
350 unsigned char lbr_bits=ScaleQuantumToChar((pixelpacket).green) & 0xf0; \
351 (pixelpacket).green=ScaleCharToQuantum((lbr_bits | (lbr_bits >> 4))); \
352 }
glennrp91d99252011-06-25 14:30:13 +0000353#define LBR04PacketBlue(pixelpacket) \
glennrp8e58efd2011-05-20 12:16:29 +0000354 { \
355 unsigned char lbr_bits=ScaleQuantumToChar((pixelpacket).blue) & 0xf0; \
356 (pixelpacket).blue=ScaleCharToQuantum((lbr_bits | (lbr_bits >> 4))); \
357 }
cristy4c08aed2011-07-01 19:47:50 +0000358#define LBR04PacketAlpha(pixelpacket) \
glennrp8e58efd2011-05-20 12:16:29 +0000359 { \
cristy4c08aed2011-07-01 19:47:50 +0000360 unsigned char lbr_bits=ScaleQuantumToChar((pixelpacket).alpha) & 0xf0; \
361 (pixelpacket).alpha=ScaleCharToQuantum((lbr_bits | (lbr_bits >> 4))); \
glennrp8e58efd2011-05-20 12:16:29 +0000362 }
363
glennrp91d99252011-06-25 14:30:13 +0000364#define LBR04PacketRGB(pixelpacket) \
glennrpbb4f99d2011-05-22 11:13:17 +0000365 { \
glennrp05001c32011-08-06 13:04:16 +0000366 LBR04PacketRed((pixelpacket)); \
glennrp91d99252011-06-25 14:30:13 +0000367 LBR04PacketGreen((pixelpacket)); \
368 LBR04PacketBlue((pixelpacket)); \
glennrpbb4f99d2011-05-22 11:13:17 +0000369 }
glennrp8e58efd2011-05-20 12:16:29 +0000370
glennrp91d99252011-06-25 14:30:13 +0000371#define LBR04PacketRGBO(pixelpacket) \
glennrpbb4f99d2011-05-22 11:13:17 +0000372 { \
glennrp91d99252011-06-25 14:30:13 +0000373 LBR04PacketRGB((pixelpacket)); \
cristy4c08aed2011-07-01 19:47:50 +0000374 LBR04PacketAlpha((pixelpacket)); \
glennrpbb4f99d2011-05-22 11:13:17 +0000375 }
glennrp8e58efd2011-05-20 12:16:29 +0000376
cristyef618312011-06-25 12:26:44 +0000377#define LBR04PixelRed(pixel) \
glennrp8e58efd2011-05-20 12:16:29 +0000378 { \
cristy4c08aed2011-07-01 19:47:50 +0000379 unsigned char lbr_bits=ScaleQuantumToChar(GetPixelRed(image,(pixel))) \
glennrp8e58efd2011-05-20 12:16:29 +0000380 & 0xf0; \
cristy4c08aed2011-07-01 19:47:50 +0000381 SetPixelRed(image,\
382 ScaleCharToQuantum((lbr_bits | (lbr_bits >> 4))), (pixel)); \
glennrp8e58efd2011-05-20 12:16:29 +0000383 }
glennrp54cf7972011-08-06 14:28:09 +0000384#define LBR04PixelGreen(pixel) \
glennrp8e58efd2011-05-20 12:16:29 +0000385 { \
cristy4c08aed2011-07-01 19:47:50 +0000386 unsigned char lbr_bits=ScaleQuantumToChar(GetPixelGreen(image,(pixel)))\
glennrp8e58efd2011-05-20 12:16:29 +0000387 & 0xf0; \
cristy4c08aed2011-07-01 19:47:50 +0000388 SetPixelGreen(image,\
389 ScaleCharToQuantum((lbr_bits | (lbr_bits >> 4))), (pixel)); \
glennrp8e58efd2011-05-20 12:16:29 +0000390 }
glennrp54cf7972011-08-06 14:28:09 +0000391#define LBR04PixelBlue(pixel) \
glennrp8e58efd2011-05-20 12:16:29 +0000392 { \
393 unsigned char lbr_bits= \
cristy4c08aed2011-07-01 19:47:50 +0000394 ScaleQuantumToChar(GetPixelBlue(image,(pixel))) & 0xf0; \
395 SetPixelBlue(image,\
396 ScaleCharToQuantum((lbr_bits | (lbr_bits >> 4))), (pixel)); \
glennrp8e58efd2011-05-20 12:16:29 +0000397 }
glennrp54cf7972011-08-06 14:28:09 +0000398#define LBR04PixelAlpha(pixel) \
glennrp8e58efd2011-05-20 12:16:29 +0000399 { \
400 unsigned char lbr_bits= \
cristy4c08aed2011-07-01 19:47:50 +0000401 ScaleQuantumToChar(GetPixelAlpha(image,(pixel))) & 0xf0; \
402 SetPixelAlpha(image,\
403 ScaleCharToQuantum((lbr_bits | (lbr_bits >> 4))), (pixel)); \
glennrp8e58efd2011-05-20 12:16:29 +0000404 }
405
glennrp54cf7972011-08-06 14:28:09 +0000406#define LBR04PixelRGB(pixel) \
glennrpbb4f99d2011-05-22 11:13:17 +0000407 { \
cristyef618312011-06-25 12:26:44 +0000408 LBR04PixelRed((pixel)); \
glennrp54cf7972011-08-06 14:28:09 +0000409 LBR04PixelGreen((pixel)); \
410 LBR04PixelBlue((pixel)); \
glennrpbb4f99d2011-05-22 11:13:17 +0000411 }
glennrp8e58efd2011-05-20 12:16:29 +0000412
glennrp54cf7972011-08-06 14:28:09 +0000413#define LBR04PixelRGBA(pixel) \
glennrpbb4f99d2011-05-22 11:13:17 +0000414 { \
glennrp54cf7972011-08-06 14:28:09 +0000415 LBR04PixelRGB((pixel)); \
416 LBR04PixelAlpha((pixel)); \
glennrpbb4f99d2011-05-22 11:13:17 +0000417 }
glennrp8e58efd2011-05-20 12:16:29 +0000418
419
420/* LBR08: Replicate top 8 bits */
421
glennrp05001c32011-08-06 13:04:16 +0000422#define LBR08PacketRed(pixelpacket) \
glennrp8e58efd2011-05-20 12:16:29 +0000423 { \
424 unsigned char lbr_bits=ScaleQuantumToChar((pixelpacket).red); \
425 (pixelpacket).red=ScaleCharToQuantum((lbr_bits)); \
426 }
glennrp91d99252011-06-25 14:30:13 +0000427#define LBR08PacketGreen(pixelpacket) \
glennrp8e58efd2011-05-20 12:16:29 +0000428 { \
429 unsigned char lbr_bits=ScaleQuantumToChar((pixelpacket).green); \
430 (pixelpacket).green=ScaleCharToQuantum((lbr_bits)); \
431 }
glennrp91d99252011-06-25 14:30:13 +0000432#define LBR08PacketBlue(pixelpacket) \
glennrp8e58efd2011-05-20 12:16:29 +0000433 { \
434 unsigned char lbr_bits=ScaleQuantumToChar((pixelpacket).blue); \
435 (pixelpacket).blue=ScaleCharToQuantum((lbr_bits)); \
436 }
cristy4c08aed2011-07-01 19:47:50 +0000437#define LBR08PacketAlpha(pixelpacket) \
glennrp8e58efd2011-05-20 12:16:29 +0000438 { \
cristy4c08aed2011-07-01 19:47:50 +0000439 unsigned char lbr_bits=ScaleQuantumToChar((pixelpacket).alpha); \
440 (pixelpacket).alpha=ScaleCharToQuantum((lbr_bits)); \
glennrp8e58efd2011-05-20 12:16:29 +0000441 }
442
glennrp91d99252011-06-25 14:30:13 +0000443#define LBR08PacketRGB(pixelpacket) \
glennrpbb4f99d2011-05-22 11:13:17 +0000444 { \
glennrp05001c32011-08-06 13:04:16 +0000445 LBR08PacketRed((pixelpacket)); \
glennrp91d99252011-06-25 14:30:13 +0000446 LBR08PacketGreen((pixelpacket)); \
447 LBR08PacketBlue((pixelpacket)); \
glennrpbb4f99d2011-05-22 11:13:17 +0000448 }
glennrp8e58efd2011-05-20 12:16:29 +0000449
glennrp91d99252011-06-25 14:30:13 +0000450#define LBR08PacketRGBO(pixelpacket) \
glennrpbb4f99d2011-05-22 11:13:17 +0000451 { \
glennrp91d99252011-06-25 14:30:13 +0000452 LBR08PacketRGB((pixelpacket)); \
cristy4c08aed2011-07-01 19:47:50 +0000453 LBR08PacketAlpha((pixelpacket)); \
glennrpbb4f99d2011-05-22 11:13:17 +0000454 }
glennrp8e58efd2011-05-20 12:16:29 +0000455
cristyef618312011-06-25 12:26:44 +0000456#define LBR08PixelRed(pixel) \
glennrp8e58efd2011-05-20 12:16:29 +0000457 { \
458 unsigned char lbr_bits= \
cristy4c08aed2011-07-01 19:47:50 +0000459 ScaleQuantumToChar(GetPixelRed(image,(pixel))); \
460 SetPixelRed(image,\
461 ScaleCharToQuantum((lbr_bits)), (pixel)); \
glennrp8e58efd2011-05-20 12:16:29 +0000462 }
glennrp54cf7972011-08-06 14:28:09 +0000463#define LBR08PixelGreen(pixel) \
glennrp8e58efd2011-05-20 12:16:29 +0000464 { \
465 unsigned char lbr_bits= \
cristy4c08aed2011-07-01 19:47:50 +0000466 ScaleQuantumToChar(GetPixelGreen(image,(pixel))); \
467 SetPixelGreen(image,\
468 ScaleCharToQuantum((lbr_bits)), (pixel)); \
glennrp8e58efd2011-05-20 12:16:29 +0000469 }
glennrp54cf7972011-08-06 14:28:09 +0000470#define LBR08PixelBlue(pixel) \
glennrp8e58efd2011-05-20 12:16:29 +0000471 { \
472 unsigned char lbr_bits= \
cristy4c08aed2011-07-01 19:47:50 +0000473 ScaleQuantumToChar(GetPixelBlue(image,(pixel))); \
474 SetPixelBlue(image,\
475 ScaleCharToQuantum((lbr_bits)), (pixel)); \
glennrp8e58efd2011-05-20 12:16:29 +0000476 }
glennrp54cf7972011-08-06 14:28:09 +0000477#define LBR08PixelAlpha(pixel) \
glennrp8e58efd2011-05-20 12:16:29 +0000478 { \
479 unsigned char lbr_bits= \
cristy4c08aed2011-07-01 19:47:50 +0000480 ScaleQuantumToChar(GetPixelAlpha(image,(pixel))); \
481 SetPixelAlpha(image,\
482 ScaleCharToQuantum((lbr_bits)), (pixel)); \
glennrp8e58efd2011-05-20 12:16:29 +0000483 }
484
glennrp54cf7972011-08-06 14:28:09 +0000485#define LBR08PixelRGB(pixel) \
glennrpbb4f99d2011-05-22 11:13:17 +0000486 { \
cristyef618312011-06-25 12:26:44 +0000487 LBR08PixelRed((pixel)); \
glennrp54cf7972011-08-06 14:28:09 +0000488 LBR08PixelGreen((pixel)); \
489 LBR08PixelBlue((pixel)); \
glennrpbb4f99d2011-05-22 11:13:17 +0000490 }
glennrp8e58efd2011-05-20 12:16:29 +0000491
glennrp54cf7972011-08-06 14:28:09 +0000492#define LBR08PixelRGBA(pixel) \
glennrpbb4f99d2011-05-22 11:13:17 +0000493 { \
glennrp54cf7972011-08-06 14:28:09 +0000494 LBR08PixelRGB((pixel)); \
495 LBR08PixelAlpha((pixel)); \
glennrpbb4f99d2011-05-22 11:13:17 +0000496 }
glennrp8e58efd2011-05-20 12:16:29 +0000497
498
499/* LBR16: Replicate top 16 bits */
500
glennrp05001c32011-08-06 13:04:16 +0000501#define LBR16PacketRed(pixelpacket) \
glennrp8e58efd2011-05-20 12:16:29 +0000502 { \
503 unsigned short lbr_bits=ScaleQuantumToShort((pixelpacket).red); \
504 (pixelpacket).red=ScaleShortToQuantum((lbr_bits)); \
505 }
glennrp91d99252011-06-25 14:30:13 +0000506#define LBR16PacketGreen(pixelpacket) \
glennrp8e58efd2011-05-20 12:16:29 +0000507 { \
508 unsigned short lbr_bits=ScaleQuantumToShort((pixelpacket).green); \
509 (pixelpacket).green=ScaleShortToQuantum((lbr_bits)); \
510 }
glennrp91d99252011-06-25 14:30:13 +0000511#define LBR16PacketBlue(pixelpacket) \
glennrp8e58efd2011-05-20 12:16:29 +0000512 { \
513 unsigned short lbr_bits=ScaleQuantumToShort((pixelpacket).blue); \
514 (pixelpacket).blue=ScaleShortToQuantum((lbr_bits)); \
515 }
cristy4c08aed2011-07-01 19:47:50 +0000516#define LBR16PacketAlpha(pixelpacket) \
glennrp8e58efd2011-05-20 12:16:29 +0000517 { \
cristy4c08aed2011-07-01 19:47:50 +0000518 unsigned short lbr_bits=ScaleQuantumToShort((pixelpacket).alpha); \
519 (pixelpacket).alpha=ScaleShortToQuantum((lbr_bits)); \
glennrp8e58efd2011-05-20 12:16:29 +0000520 }
521
glennrp91d99252011-06-25 14:30:13 +0000522#define LBR16PacketRGB(pixelpacket) \
glennrpbb4f99d2011-05-22 11:13:17 +0000523 { \
glennrp05001c32011-08-06 13:04:16 +0000524 LBR16PacketRed((pixelpacket)); \
glennrp91d99252011-06-25 14:30:13 +0000525 LBR16PacketGreen((pixelpacket)); \
526 LBR16PacketBlue((pixelpacket)); \
glennrpbb4f99d2011-05-22 11:13:17 +0000527 }
glennrp8e58efd2011-05-20 12:16:29 +0000528
glennrp91d99252011-06-25 14:30:13 +0000529#define LBR16PacketRGBO(pixelpacket) \
glennrpbb4f99d2011-05-22 11:13:17 +0000530 { \
glennrp91d99252011-06-25 14:30:13 +0000531 LBR16PacketRGB((pixelpacket)); \
cristy4c08aed2011-07-01 19:47:50 +0000532 LBR16PacketAlpha((pixelpacket)); \
glennrpbb4f99d2011-05-22 11:13:17 +0000533 }
glennrp8e58efd2011-05-20 12:16:29 +0000534
cristyef618312011-06-25 12:26:44 +0000535#define LBR16PixelRed(pixel) \
glennrp8e58efd2011-05-20 12:16:29 +0000536 { \
537 unsigned short lbr_bits= \
cristy4c08aed2011-07-01 19:47:50 +0000538 ScaleQuantumToShort(GetPixelRed(image,(pixel))); \
539 SetPixelRed(image,\
540 ScaleShortToQuantum((lbr_bits)),(pixel)); \
glennrp8e58efd2011-05-20 12:16:29 +0000541 }
glennrp54cf7972011-08-06 14:28:09 +0000542#define LBR16PixelGreen(pixel) \
glennrp8e58efd2011-05-20 12:16:29 +0000543 { \
544 unsigned short lbr_bits= \
cristy4c08aed2011-07-01 19:47:50 +0000545 ScaleQuantumToShort(GetPixelGreen(image,(pixel))); \
546 SetPixelGreen(image,\
547 ScaleShortToQuantum((lbr_bits)),(pixel)); \
glennrp8e58efd2011-05-20 12:16:29 +0000548 }
glennrp54cf7972011-08-06 14:28:09 +0000549#define LBR16PixelBlue(pixel) \
glennrp8e58efd2011-05-20 12:16:29 +0000550 { \
551 unsigned short lbr_bits= \
cristy4c08aed2011-07-01 19:47:50 +0000552 ScaleQuantumToShort(GetPixelBlue(image,(pixel))); \
553 SetPixelBlue(image,\
554 ScaleShortToQuantum((lbr_bits)),(pixel)); \
glennrp8e58efd2011-05-20 12:16:29 +0000555 }
glennrp54cf7972011-08-06 14:28:09 +0000556#define LBR16PixelAlpha(pixel) \
glennrp8e58efd2011-05-20 12:16:29 +0000557 { \
558 unsigned short lbr_bits= \
cristy4c08aed2011-07-01 19:47:50 +0000559 ScaleQuantumToShort(GetPixelAlpha(image,(pixel))); \
560 SetPixelAlpha(image,\
561 ScaleShortToQuantum((lbr_bits)),(pixel)); \
glennrp8e58efd2011-05-20 12:16:29 +0000562 }
563
glennrp54cf7972011-08-06 14:28:09 +0000564#define LBR16PixelRGB(pixel) \
glennrpbb4f99d2011-05-22 11:13:17 +0000565 { \
cristyef618312011-06-25 12:26:44 +0000566 LBR16PixelRed((pixel)); \
glennrp54cf7972011-08-06 14:28:09 +0000567 LBR16PixelGreen((pixel)); \
568 LBR16PixelBlue((pixel)); \
glennrpbb4f99d2011-05-22 11:13:17 +0000569 }
glennrp8e58efd2011-05-20 12:16:29 +0000570
glennrp54cf7972011-08-06 14:28:09 +0000571#define LBR16PixelRGBA(pixel) \
glennrpbb4f99d2011-05-22 11:13:17 +0000572 { \
glennrp54cf7972011-08-06 14:28:09 +0000573 LBR16PixelRGB((pixel)); \
574 LBR16PixelAlpha((pixel)); \
glennrpbb4f99d2011-05-22 11:13:17 +0000575 }
glennrp8e58efd2011-05-20 12:16:29 +0000576
cristy3ed852e2009-09-05 21:47:34 +0000577/*
578 Establish thread safety.
579 setjmp/longjmp is claimed to be safe on these platforms:
580 setjmp/longjmp is alleged to be unsafe on these platforms:
581*/
582#ifndef SETJMP_IS_THREAD_SAFE
583#define PNG_SETJMP_NOT_THREAD_SAFE
584#endif
585
586#if defined(PNG_SETJMP_NOT_THREAD_SAFE)
587static SemaphoreInfo
glennrpcf002022011-01-30 02:38:15 +0000588 *ping_semaphore = (SemaphoreInfo *) NULL;
cristy3ed852e2009-09-05 21:47:34 +0000589#endif
590
591/*
592 This temporary until I set up malloc'ed object attributes array.
593 Recompile with MNG_MAX_OBJECTS=65536L to avoid this limit but
594 waste more memory.
595*/
596#define MNG_MAX_OBJECTS 256
597
598/*
599 If this not defined, spec is interpreted strictly. If it is
600 defined, an attempt will be made to recover from some errors,
601 including
602 o global PLTE too short
603*/
604#undef MNG_LOOSE
605
606/*
607 Don't try to define PNG_MNG_FEATURES_SUPPORTED here. Make sure
608 it's defined in libpng/pngconf.h, version 1.0.9 or later. It won't work
609 with earlier versions of libpng. From libpng-1.0.3a to libpng-1.0.8,
610 PNG_READ|WRITE_EMPTY_PLTE were used but those have been deprecated in
611 libpng in favor of PNG_MNG_FEATURES_SUPPORTED, so we set them here.
612 PNG_MNG_FEATURES_SUPPORTED is disabled by default in libpng-1.0.9 and
613 will be enabled by default in libpng-1.2.0.
614*/
cristy3ed852e2009-09-05 21:47:34 +0000615#ifdef PNG_MNG_FEATURES_SUPPORTED
616# ifndef PNG_READ_EMPTY_PLTE_SUPPORTED
617# define PNG_READ_EMPTY_PLTE_SUPPORTED
618# endif
619# ifndef PNG_WRITE_EMPTY_PLTE_SUPPORTED
620# define PNG_WRITE_EMPTY_PLTE_SUPPORTED
621# endif
622#endif
623
624/*
cristybb503372010-05-27 20:51:26 +0000625 Maximum valid size_t in PNG/MNG chunks is (2^31)-1
cristy3ed852e2009-09-05 21:47:34 +0000626 This macro is only defined in libpng-1.0.3 and later.
627 Previously it was PNG_MAX_UINT but that was deprecated in libpng-1.2.6
628*/
629#ifndef PNG_UINT_31_MAX
630#define PNG_UINT_31_MAX (png_uint_32) 0x7fffffffL
631#endif
632
633/*
634 Constant strings for known chunk types. If you need to add a chunk,
635 add a string holding the name here. To make the code more
636 portable, we use ASCII numbers like this, not characters.
637*/
638
glennrp85dcf872011-12-07 02:51:47 +0000639static png_byte mng_MHDR[5]={ 77, 72, 68, 82, (png_byte) '\0'};
640static png_byte mng_BACK[5]={ 66, 65, 67, 75, (png_byte) '\0'};
641static png_byte mng_BASI[5]={ 66, 65, 83, 73, (png_byte) '\0'};
642static png_byte mng_CLIP[5]={ 67, 76, 73, 80, (png_byte) '\0'};
643static png_byte mng_CLON[5]={ 67, 76, 79, 78, (png_byte) '\0'};
644static png_byte mng_DEFI[5]={ 68, 69, 70, 73, (png_byte) '\0'};
645static png_byte mng_DHDR[5]={ 68, 72, 68, 82, (png_byte) '\0'};
646static png_byte mng_DISC[5]={ 68, 73, 83, 67, (png_byte) '\0'};
647static png_byte mng_ENDL[5]={ 69, 78, 68, 76, (png_byte) '\0'};
648static png_byte mng_FRAM[5]={ 70, 82, 65, 77, (png_byte) '\0'};
649static png_byte mng_IEND[5]={ 73, 69, 78, 68, (png_byte) '\0'};
650static png_byte mng_IHDR[5]={ 73, 72, 68, 82, (png_byte) '\0'};
651static png_byte mng_JHDR[5]={ 74, 72, 68, 82, (png_byte) '\0'};
652static png_byte mng_LOOP[5]={ 76, 79, 79, 80, (png_byte) '\0'};
653static png_byte mng_MAGN[5]={ 77, 65, 71, 78, (png_byte) '\0'};
654static png_byte mng_MEND[5]={ 77, 69, 78, 68, (png_byte) '\0'};
655static png_byte mng_MOVE[5]={ 77, 79, 86, 69, (png_byte) '\0'};
656static png_byte mng_PAST[5]={ 80, 65, 83, 84, (png_byte) '\0'};
657static png_byte mng_PLTE[5]={ 80, 76, 84, 69, (png_byte) '\0'};
658static png_byte mng_SAVE[5]={ 83, 65, 86, 69, (png_byte) '\0'};
659static png_byte mng_SEEK[5]={ 83, 69, 69, 75, (png_byte) '\0'};
660static png_byte mng_SHOW[5]={ 83, 72, 79, 87, (png_byte) '\0'};
661static png_byte mng_TERM[5]={ 84, 69, 82, 77, (png_byte) '\0'};
662static png_byte mng_bKGD[5]={ 98, 75, 71, 68, (png_byte) '\0'};
663static png_byte mng_cHRM[5]={ 99, 72, 82, 77, (png_byte) '\0'};
664static png_byte mng_gAMA[5]={103, 65, 77, 65, (png_byte) '\0'};
665static png_byte mng_iCCP[5]={105, 67, 67, 80, (png_byte) '\0'};
666static png_byte mng_nEED[5]={110, 69, 69, 68, (png_byte) '\0'};
667static png_byte mng_pHYg[5]={112, 72, 89, 103, (png_byte) '\0'};
668static png_byte mng_vpAg[5]={118, 112, 65, 103, (png_byte) '\0'};
669static png_byte mng_pHYs[5]={112, 72, 89, 115, (png_byte) '\0'};
670static png_byte mng_sBIT[5]={115, 66, 73, 84, (png_byte) '\0'};
671static png_byte mng_sRGB[5]={115, 82, 71, 66, (png_byte) '\0'};
672static png_byte mng_tRNS[5]={116, 82, 78, 83, (png_byte) '\0'};
cristy3ed852e2009-09-05 21:47:34 +0000673
674#if defined(JNG_SUPPORTED)
glennrp85dcf872011-12-07 02:51:47 +0000675static png_byte mng_IDAT[5]={ 73, 68, 65, 84, (png_byte) '\0'};
676static png_byte mng_JDAT[5]={ 74, 68, 65, 84, (png_byte) '\0'};
677static png_byte mng_JDAA[5]={ 74, 68, 65, 65, (png_byte) '\0'};
678static png_byte mng_JdAA[5]={ 74, 100, 65, 65, (png_byte) '\0'};
679static png_byte mng_JSEP[5]={ 74, 83, 69, 80, (png_byte) '\0'};
680static png_byte mng_oFFs[5]={111, 70, 70, 115, (png_byte) '\0'};
cristy3ed852e2009-09-05 21:47:34 +0000681#endif
682
683/*
684Other known chunks that are not yet supported by ImageMagick:
glennrp85dcf872011-12-07 02:51:47 +0000685static png_byte mng_hIST[5]={104, 73, 83, 84, (png_byte) '\0'};
686static png_byte mng_iCCP[5]={105, 67, 67, 80, (png_byte) '\0'};
687static png_byte mng_iTXt[5]={105, 84, 88, 116, (png_byte) '\0'};
688static png_byte mng_sPLT[5]={115, 80, 76, 84, (png_byte) '\0'};
689static png_byte mng_sTER[5]={115, 84, 69, 82, (png_byte) '\0'};
690static png_byte mng_tEXt[5]={116, 69, 88, 116, (png_byte) '\0'};
691static png_byte mng_tIME[5]={116, 73, 77, 69, (png_byte) '\0'};
692static png_byte mng_zTXt[5]={122, 84, 88, 116, (png_byte) '\0'};
cristy3ed852e2009-09-05 21:47:34 +0000693*/
694
695typedef struct _MngBox
696{
cristy8182b072010-05-30 20:10:53 +0000697 long
cristy3ed852e2009-09-05 21:47:34 +0000698 left,
699 right,
700 top,
701 bottom;
702} MngBox;
703
704typedef struct _MngPair
705{
cristy8182b072010-05-30 20:10:53 +0000706 volatile long
cristy3ed852e2009-09-05 21:47:34 +0000707 a,
708 b;
709} MngPair;
710
711#ifdef MNG_OBJECT_BUFFERS
712typedef struct _MngBuffer
713{
714
cristybb503372010-05-27 20:51:26 +0000715 size_t
cristy3ed852e2009-09-05 21:47:34 +0000716 height,
717 width;
718
719 Image
720 *image;
721
722 png_color
723 plte[256];
724
725 int
726 reference_count;
727
728 unsigned char
729 alpha_sample_depth,
730 compression_method,
731 color_type,
732 concrete,
733 filter_method,
734 frozen,
735 image_type,
736 interlace_method,
737 pixel_sample_depth,
738 plte_length,
739 sample_depth,
740 viewable;
741} MngBuffer;
742#endif
743
744typedef struct _MngInfo
745{
746
747#ifdef MNG_OBJECT_BUFFERS
748 MngBuffer
749 *ob[MNG_MAX_OBJECTS];
750#endif
751
752 Image *
753 image;
754
755 RectangleInfo
756 page;
757
758 int
759 adjoin,
760#ifndef PNG_READ_EMPTY_PLTE_SUPPORTED
761 bytes_in_read_buffer,
762 found_empty_plte,
763#endif
764 equal_backgrounds,
765 equal_chrms,
766 equal_gammas,
767#if defined(PNG_WRITE_EMPTY_PLTE_SUPPORTED) || \
768 defined(PNG_MNG_FEATURES_SUPPORTED)
769 equal_palettes,
770#endif
771 equal_physs,
772 equal_srgbs,
773 framing_mode,
774 have_global_bkgd,
775 have_global_chrm,
776 have_global_gama,
777 have_global_phys,
778 have_global_sbit,
779 have_global_srgb,
780 have_saved_bkgd_index,
781 have_write_global_chrm,
782 have_write_global_gama,
783 have_write_global_plte,
784 have_write_global_srgb,
785 need_fram,
786 object_id,
787 old_framing_mode,
cristy3ed852e2009-09-05 21:47:34 +0000788 saved_bkgd_index;
789
790 int
791 new_number_colors;
792
cristybb503372010-05-27 20:51:26 +0000793 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000794 image_found,
795 loop_count[256],
796 loop_iteration[256],
797 scenes_found,
798 x_off[MNG_MAX_OBJECTS],
799 y_off[MNG_MAX_OBJECTS];
800
801 MngBox
802 clip,
803 frame,
804 image_box,
805 object_clip[MNG_MAX_OBJECTS];
806
807 unsigned char
808 /* These flags could be combined into one byte */
809 exists[MNG_MAX_OBJECTS],
810 frozen[MNG_MAX_OBJECTS],
811 loop_active[256],
812 invisible[MNG_MAX_OBJECTS],
813 viewable[MNG_MAX_OBJECTS];
814
815 MagickOffsetType
816 loop_jump[256];
817
818 png_colorp
819 global_plte;
820
821 png_color_8
822 global_sbit;
823
824 png_byte
825#ifndef PNG_READ_EMPTY_PLTE_SUPPORTED
826 read_buffer[8],
827#endif
828 global_trns[256];
829
830 float
831 global_gamma;
832
833 ChromaticityInfo
834 global_chrm;
835
836 RenderingIntent
837 global_srgb_intent;
838
cristy35ef8242010-06-03 16:24:13 +0000839 unsigned int
cristy3ed852e2009-09-05 21:47:34 +0000840 delay,
841 global_plte_length,
842 global_trns_length,
843 global_x_pixels_per_unit,
844 global_y_pixels_per_unit,
845 mng_width,
846 mng_height,
847 ticks_per_second;
848
glennrpb9cfe272010-12-21 15:08:06 +0000849 MagickBooleanType
850 need_blob;
851
cristy3ed852e2009-09-05 21:47:34 +0000852 unsigned int
853 IsPalette,
854 global_phys_unit_type,
855 basi_warning,
856 clon_warning,
857 dhdr_warning,
858 jhdr_warning,
859 magn_warning,
860 past_warning,
861 phyg_warning,
862 phys_warning,
863 sbit_warning,
864 show_warning,
865 mng_type,
866 write_mng,
867 write_png_colortype,
868 write_png_depth,
glennrp18682582011-06-30 18:11:47 +0000869 write_png_compression_level,
870 write_png_compression_strategy,
871 write_png_compression_filter,
cristy3ed852e2009-09-05 21:47:34 +0000872 write_png8,
873 write_png24,
874 write_png32;
875
876#ifdef MNG_BASI_SUPPORTED
cristybb503372010-05-27 20:51:26 +0000877 size_t
cristy3ed852e2009-09-05 21:47:34 +0000878 basi_width,
879 basi_height;
880
881 unsigned int
882 basi_depth,
883 basi_color_type,
884 basi_compression_method,
885 basi_filter_type,
886 basi_interlace_method,
887 basi_red,
888 basi_green,
889 basi_blue,
890 basi_alpha,
891 basi_viewable;
892#endif
893
894 png_uint_16
895 magn_first,
896 magn_last,
897 magn_mb,
898 magn_ml,
899 magn_mr,
900 magn_mt,
901 magn_mx,
902 magn_my,
903 magn_methx,
904 magn_methy;
905
cristy101ab702011-10-13 13:06:32 +0000906 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000907 mng_global_bkgd;
908
glennrp26f37912010-12-23 16:22:42 +0000909 /* Added at version 6.6.6-7 */
910 MagickBooleanType
911 ping_exclude_bKGD,
912 ping_exclude_cHRM,
glennrpa0ed0092011-04-18 16:36:29 +0000913 ping_exclude_date,
glennrp26f37912010-12-23 16:22:42 +0000914 ping_exclude_EXIF,
915 ping_exclude_gAMA,
916 ping_exclude_iCCP,
917 /* ping_exclude_iTXt, */
918 ping_exclude_oFFs,
919 ping_exclude_pHYs,
920 ping_exclude_sRGB,
921 ping_exclude_tEXt,
glennrpa1e3b7b2010-12-24 16:37:33 +0000922 ping_exclude_tRNS,
glennrp26f37912010-12-23 16:22:42 +0000923 ping_exclude_vpAg,
924 ping_exclude_zCCP, /* hex-encoded iCCP */
glennrp8d3d6e52011-04-19 04:39:51 +0000925 ping_exclude_zTXt,
926 ping_preserve_colormap;
glennrp26f37912010-12-23 16:22:42 +0000927
cristy3ed852e2009-09-05 21:47:34 +0000928} MngInfo;
929#endif /* VER */
930
931/*
932 Forward declarations.
933*/
934static MagickBooleanType
cristy1e178e72011-08-28 19:44:34 +0000935 WritePNGImage(const ImageInfo *,Image *,ExceptionInfo *);
glennrp0c3e06b2010-11-19 13:45:02 +0000936
cristy3ed852e2009-09-05 21:47:34 +0000937static MagickBooleanType
cristy1e178e72011-08-28 19:44:34 +0000938 WriteMNGImage(const ImageInfo *,Image *,ExceptionInfo *);
glennrp0c3e06b2010-11-19 13:45:02 +0000939
cristy3ed852e2009-09-05 21:47:34 +0000940#if defined(JNG_SUPPORTED)
941static MagickBooleanType
cristy1e178e72011-08-28 19:44:34 +0000942 WriteJNGImage(const ImageInfo *,Image *,ExceptionInfo *);
cristy3ed852e2009-09-05 21:47:34 +0000943#endif
944
glennrp0c3e06b2010-11-19 13:45:02 +0000945#if PNG_LIBPNG_VER > 10011
946
glennrpfd05d622011-02-25 04:10:33 +0000947
glennrp0c3e06b2010-11-19 13:45:02 +0000948#if (MAGICKCORE_QUANTUM_DEPTH >= 16)
949static MagickBooleanType
cristyc82a27b2011-10-21 01:07:16 +0000950LosslessReduceDepthOK(Image *image,ExceptionInfo *exception)
glennrp0c3e06b2010-11-19 13:45:02 +0000951{
glennrp67b9c1a2011-04-22 18:47:36 +0000952 /* Reduce bit depth if it can be reduced losslessly from 16+ to 8.
953 *
954 * This is true if the high byte and the next highest byte of
955 * each sample of the image, the colormap, and the background color
glennrp3faa9a32011-04-23 14:00:25 +0000956 * are equal to each other. We check this by seeing if the samples
957 * are unchanged when we scale them down to 8 and back up to Quantum.
glennrp67b9c1a2011-04-22 18:47:36 +0000958 *
959 * We don't use the method GetImageDepth() because it doesn't check
glennrp3faa9a32011-04-23 14:00:25 +0000960 * background and doesn't handle PseudoClass specially.
glennrp67b9c1a2011-04-22 18:47:36 +0000961 */
962
glennrp3faa9a32011-04-23 14:00:25 +0000963#define QuantumToCharToQuantumEqQuantum(quantum) \
964 ((ScaleCharToQuantum((unsigned char) ScaleQuantumToChar(quantum))) == quantum)
965
glennrp0c3e06b2010-11-19 13:45:02 +0000966 MagickBooleanType
967 ok_to_reduce=MagickFalse;
glennrp0c3e06b2010-11-19 13:45:02 +0000968
glennrp03e11f62011-04-22 13:30:16 +0000969 if (image->depth >= 16)
glennrp0c3e06b2010-11-19 13:45:02 +0000970 {
971
cristy4c08aed2011-07-01 19:47:50 +0000972 const Quantum
glennrp0c3e06b2010-11-19 13:45:02 +0000973 *p;
974
975 ok_to_reduce=
glennrp3faa9a32011-04-23 14:00:25 +0000976 QuantumToCharToQuantumEqQuantum(image->background_color.red) &&
977 QuantumToCharToQuantumEqQuantum(image->background_color.green) &&
978 QuantumToCharToQuantumEqQuantum(image->background_color.blue) ?
979 MagickTrue : MagickFalse;
glennrp0c3e06b2010-11-19 13:45:02 +0000980
981 if (ok_to_reduce != MagickFalse && image->storage_class == PseudoClass)
982 {
983 int indx;
984
985 for (indx=0; indx < (ssize_t) image->colors; indx++)
986 {
glennrp3faa9a32011-04-23 14:00:25 +0000987 ok_to_reduce=(
988 QuantumToCharToQuantumEqQuantum(
989 image->colormap[indx].red) &&
990 QuantumToCharToQuantumEqQuantum(
991 image->colormap[indx].green) &&
992 QuantumToCharToQuantumEqQuantum(
993 image->colormap[indx].blue)) ?
994 MagickTrue : MagickFalse;
995
glennrp0c3e06b2010-11-19 13:45:02 +0000996 if (ok_to_reduce == MagickFalse)
glennrp3faa9a32011-04-23 14:00:25 +0000997 break;
glennrp0c3e06b2010-11-19 13:45:02 +0000998 }
999 }
1000
1001 if ((ok_to_reduce != MagickFalse) &&
1002 (image->storage_class != PseudoClass))
1003 {
1004 ssize_t
1005 y;
1006
1007 register ssize_t
1008 x;
1009
1010 for (y=0; y < (ssize_t) image->rows; y++)
1011 {
cristyc82a27b2011-10-21 01:07:16 +00001012 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
glennrp0c3e06b2010-11-19 13:45:02 +00001013
cristy4c08aed2011-07-01 19:47:50 +00001014 if (p == (const Quantum *) NULL)
glennrp0c3e06b2010-11-19 13:45:02 +00001015 {
1016 ok_to_reduce = MagickFalse;
1017 break;
1018 }
1019
1020 for (x=(ssize_t) image->columns-1; x >= 0; x--)
1021 {
glennrp3faa9a32011-04-23 14:00:25 +00001022 ok_to_reduce=
cristy4c08aed2011-07-01 19:47:50 +00001023 QuantumToCharToQuantumEqQuantum(GetPixelRed(image,p)) &&
1024 QuantumToCharToQuantumEqQuantum(GetPixelGreen(image,p)) &&
1025 QuantumToCharToQuantumEqQuantum(GetPixelBlue(image,p)) ?
glennrp3faa9a32011-04-23 14:00:25 +00001026 MagickTrue : MagickFalse;
glennrp0c3e06b2010-11-19 13:45:02 +00001027
1028 if (ok_to_reduce == MagickFalse)
1029 break;
1030
cristyed231572011-07-14 02:18:59 +00001031 p+=GetPixelChannels(image);
glennrp0c3e06b2010-11-19 13:45:02 +00001032 }
glennrp8640fb52010-11-23 15:48:26 +00001033 if (x >= 0)
glennrp0c3e06b2010-11-19 13:45:02 +00001034 break;
1035 }
1036 }
1037
1038 if (ok_to_reduce != MagickFalse)
1039 {
glennrp0c3e06b2010-11-19 13:45:02 +00001040 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
glennrpfd05d622011-02-25 04:10:33 +00001041 " OK to reduce PNG bit depth to 8 without loss of info");
glennrp0c3e06b2010-11-19 13:45:02 +00001042 }
glennrpa6a06632011-01-19 15:15:34 +00001043 else
1044 {
1045 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
glennrpfd05d622011-02-25 04:10:33 +00001046 " Not OK to reduce PNG bit depth to 8 without loss of info");
glennrpa6a06632011-01-19 15:15:34 +00001047 }
glennrp0c3e06b2010-11-19 13:45:02 +00001048 }
1049
1050 return ok_to_reduce;
1051}
1052#endif /* MAGICKCORE_QUANTUM_DEPTH >= 16 */
1053
glennrpe610a072010-08-05 17:08:46 +00001054static int
glennrpcf002022011-01-30 02:38:15 +00001055Magick_RenderingIntent_to_PNG_RenderingIntent(const RenderingIntent intent)
glennrp0fe50b42010-11-16 03:52:51 +00001056{
glennrpe610a072010-08-05 17:08:46 +00001057 switch (intent)
1058 {
1059 case PerceptualIntent:
1060 return 0;
glennrp0fe50b42010-11-16 03:52:51 +00001061
glennrpe610a072010-08-05 17:08:46 +00001062 case RelativeIntent:
1063 return 1;
glennrp0fe50b42010-11-16 03:52:51 +00001064
glennrpe610a072010-08-05 17:08:46 +00001065 case SaturationIntent:
1066 return 2;
glennrp0fe50b42010-11-16 03:52:51 +00001067
glennrpe610a072010-08-05 17:08:46 +00001068 case AbsoluteIntent:
1069 return 3;
glennrp0fe50b42010-11-16 03:52:51 +00001070
glennrpe610a072010-08-05 17:08:46 +00001071 default:
1072 return -1;
1073 }
1074}
1075
1076static RenderingIntent
glennrpcf002022011-01-30 02:38:15 +00001077Magick_RenderingIntent_from_PNG_RenderingIntent(const int ping_intent)
glennrp0fe50b42010-11-16 03:52:51 +00001078{
glennrpcf002022011-01-30 02:38:15 +00001079 switch (ping_intent)
glennrpe610a072010-08-05 17:08:46 +00001080 {
1081 case 0:
1082 return PerceptualIntent;
glennrp0fe50b42010-11-16 03:52:51 +00001083
glennrpe610a072010-08-05 17:08:46 +00001084 case 1:
1085 return RelativeIntent;
glennrp0fe50b42010-11-16 03:52:51 +00001086
glennrpe610a072010-08-05 17:08:46 +00001087 case 2:
1088 return SaturationIntent;
glennrp0fe50b42010-11-16 03:52:51 +00001089
glennrpe610a072010-08-05 17:08:46 +00001090 case 3:
1091 return AbsoluteIntent;
glennrp0fe50b42010-11-16 03:52:51 +00001092
glennrpe610a072010-08-05 17:08:46 +00001093 default:
1094 return UndefinedIntent;
1095 }
1096}
1097
cristybb503372010-05-27 20:51:26 +00001098static inline ssize_t MagickMax(const ssize_t x,const ssize_t y)
cristy3ed852e2009-09-05 21:47:34 +00001099{
1100 if (x > y)
1101 return(x);
glennrp0fe50b42010-11-16 03:52:51 +00001102
cristy3ed852e2009-09-05 21:47:34 +00001103 return(y);
1104}
glennrp0c3e06b2010-11-19 13:45:02 +00001105
cristybb503372010-05-27 20:51:26 +00001106static inline ssize_t MagickMin(const ssize_t x,const ssize_t y)
cristy3ed852e2009-09-05 21:47:34 +00001107{
1108 if (x < y)
1109 return(x);
glennrp0fe50b42010-11-16 03:52:51 +00001110
cristy3ed852e2009-09-05 21:47:34 +00001111 return(y);
1112}
glennrp0c3e06b2010-11-19 13:45:02 +00001113
cristy3ed852e2009-09-05 21:47:34 +00001114
1115/*
1116%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1117% %
1118% %
1119% %
1120% I m a g e I s G r a y %
1121% %
1122% %
1123% %
1124%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1125% %
cristy4c08aed2011-07-01 19:47:50 +00001126% Like IsImageGray except does not change DirectClass to PseudoClass %
cristy3ed852e2009-09-05 21:47:34 +00001127% %
1128%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1129*/
cristyc82a27b2011-10-21 01:07:16 +00001130static MagickBooleanType ImageIsGray(Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001131{
cristy4c08aed2011-07-01 19:47:50 +00001132 register const Quantum
cristy3ed852e2009-09-05 21:47:34 +00001133 *p;
1134
cristybb503372010-05-27 20:51:26 +00001135 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001136 i,
1137 x,
1138 y;
1139
1140 assert(image != (Image *) NULL);
1141 assert(image->signature == MagickSignature);
1142 if (image->debug != MagickFalse)
1143 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1144
1145 if (image->storage_class == PseudoClass)
1146 {
cristybb503372010-05-27 20:51:26 +00001147 for (i=0; i < (ssize_t) image->colors; i++)
cristy101ab702011-10-13 13:06:32 +00001148 if (IsPixelInfoGray(image->colormap+i) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001149 return(MagickFalse);
1150 return(MagickTrue);
1151 }
cristybb503372010-05-27 20:51:26 +00001152 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001153 {
cristyc82a27b2011-10-21 01:07:16 +00001154 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00001155 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001156 return(MagickFalse);
cristybb503372010-05-27 20:51:26 +00001157 for (x=(ssize_t) image->columns-1; x >= 0; x--)
cristy3ed852e2009-09-05 21:47:34 +00001158 {
cristy4c08aed2011-07-01 19:47:50 +00001159 if (IsPixelGray(image,p) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001160 return(MagickFalse);
cristyed231572011-07-14 02:18:59 +00001161 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001162 }
1163 }
1164 return(MagickTrue);
1165}
glennrpd5045b42010-03-24 12:40:35 +00001166#endif /* PNG_LIBPNG_VER > 10011 */
cristy3ed852e2009-09-05 21:47:34 +00001167#endif /* MAGICKCORE_PNG_DELEGATE */
1168
1169/*
1170%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1171% %
1172% %
1173% %
1174% I s M N G %
1175% %
1176% %
1177% %
1178%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1179%
1180% IsMNG() returns MagickTrue if the image format type, identified by the
1181% magick string, is MNG.
1182%
1183% The format of the IsMNG method is:
1184%
1185% MagickBooleanType IsMNG(const unsigned char *magick,const size_t length)
1186%
1187% A description of each parameter follows:
1188%
1189% o magick: compare image format pattern against these bytes.
1190%
1191% o length: Specifies the length of the magick string.
1192%
1193%
1194*/
1195static MagickBooleanType IsMNG(const unsigned char *magick,const size_t length)
1196{
1197 if (length < 8)
1198 return(MagickFalse);
glennrp0fe50b42010-11-16 03:52:51 +00001199
cristy3ed852e2009-09-05 21:47:34 +00001200 if (memcmp(magick,"\212MNG\r\n\032\n",8) == 0)
1201 return(MagickTrue);
glennrp0fe50b42010-11-16 03:52:51 +00001202
cristy3ed852e2009-09-05 21:47:34 +00001203 return(MagickFalse);
1204}
1205
1206/*
1207%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1208% %
1209% %
1210% %
1211% I s J N G %
1212% %
1213% %
1214% %
1215%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1216%
1217% IsJNG() returns MagickTrue if the image format type, identified by the
1218% magick string, is JNG.
1219%
1220% The format of the IsJNG method is:
1221%
1222% MagickBooleanType IsJNG(const unsigned char *magick,const size_t length)
1223%
1224% A description of each parameter follows:
1225%
1226% o magick: compare image format pattern against these bytes.
1227%
1228% o length: Specifies the length of the magick string.
1229%
1230%
1231*/
1232static MagickBooleanType IsJNG(const unsigned char *magick,const size_t length)
1233{
1234 if (length < 8)
1235 return(MagickFalse);
glennrp0fe50b42010-11-16 03:52:51 +00001236
cristy3ed852e2009-09-05 21:47:34 +00001237 if (memcmp(magick,"\213JNG\r\n\032\n",8) == 0)
1238 return(MagickTrue);
glennrp0fe50b42010-11-16 03:52:51 +00001239
cristy3ed852e2009-09-05 21:47:34 +00001240 return(MagickFalse);
1241}
1242
1243/*
1244%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1245% %
1246% %
1247% %
1248% I s P N G %
1249% %
1250% %
1251% %
1252%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1253%
1254% IsPNG() returns MagickTrue if the image format type, identified by the
1255% magick string, is PNG.
1256%
1257% The format of the IsPNG method is:
1258%
1259% MagickBooleanType IsPNG(const unsigned char *magick,const size_t length)
1260%
1261% A description of each parameter follows:
1262%
1263% o magick: compare image format pattern against these bytes.
1264%
1265% o length: Specifies the length of the magick string.
1266%
1267*/
1268static MagickBooleanType IsPNG(const unsigned char *magick,const size_t length)
1269{
1270 if (length < 8)
1271 return(MagickFalse);
glennrp0fe50b42010-11-16 03:52:51 +00001272
cristy3ed852e2009-09-05 21:47:34 +00001273 if (memcmp(magick,"\211PNG\r\n\032\n",8) == 0)
1274 return(MagickTrue);
glennrp0fe50b42010-11-16 03:52:51 +00001275
cristy3ed852e2009-09-05 21:47:34 +00001276 return(MagickFalse);
1277}
1278
1279#if defined(MAGICKCORE_PNG_DELEGATE)
1280#if defined(__cplusplus) || defined(c_plusplus)
1281extern "C" {
1282#endif
1283
glennrpd5045b42010-03-24 12:40:35 +00001284#if (PNG_LIBPNG_VER > 10011)
cristybb503372010-05-27 20:51:26 +00001285static size_t WriteBlobMSBULong(Image *image,const size_t value)
cristy3ed852e2009-09-05 21:47:34 +00001286{
1287 unsigned char
1288 buffer[4];
1289
1290 assert(image != (Image *) NULL);
1291 assert(image->signature == MagickSignature);
1292 buffer[0]=(unsigned char) (value >> 24);
1293 buffer[1]=(unsigned char) (value >> 16);
1294 buffer[2]=(unsigned char) (value >> 8);
1295 buffer[3]=(unsigned char) value;
1296 return((size_t) WriteBlob(image,4,buffer));
1297}
1298
1299static void PNGLong(png_bytep p,png_uint_32 value)
1300{
1301 *p++=(png_byte) ((value >> 24) & 0xff);
1302 *p++=(png_byte) ((value >> 16) & 0xff);
1303 *p++=(png_byte) ((value >> 8) & 0xff);
1304 *p++=(png_byte) (value & 0xff);
1305}
1306
glennrpa521b2f2010-10-29 04:11:03 +00001307#if defined(JNG_SUPPORTED)
cristy3ed852e2009-09-05 21:47:34 +00001308static void PNGsLong(png_bytep p,png_int_32 value)
1309{
1310 *p++=(png_byte) ((value >> 24) & 0xff);
1311 *p++=(png_byte) ((value >> 16) & 0xff);
1312 *p++=(png_byte) ((value >> 8) & 0xff);
1313 *p++=(png_byte) (value & 0xff);
1314}
glennrpa521b2f2010-10-29 04:11:03 +00001315#endif
cristy3ed852e2009-09-05 21:47:34 +00001316
1317static void PNGShort(png_bytep p,png_uint_16 value)
1318{
1319 *p++=(png_byte) ((value >> 8) & 0xff);
1320 *p++=(png_byte) (value & 0xff);
1321}
1322
1323static void PNGType(png_bytep p,png_bytep type)
1324{
1325 (void) CopyMagickMemory(p,type,4*sizeof(png_byte));
1326}
1327
glennrp03812ae2010-12-24 01:31:34 +00001328static void LogPNGChunk(MagickBooleanType logging, png_bytep type,
1329 size_t length)
cristy3ed852e2009-09-05 21:47:34 +00001330{
1331 if (logging != MagickFalse)
1332 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00001333 " Writing %c%c%c%c chunk, length: %.20g",
1334 type[0],type[1],type[2],type[3],(double) length);
cristy3ed852e2009-09-05 21:47:34 +00001335}
glennrpd5045b42010-03-24 12:40:35 +00001336#endif /* PNG_LIBPNG_VER > 10011 */
cristy3ed852e2009-09-05 21:47:34 +00001337
1338#if defined(__cplusplus) || defined(c_plusplus)
1339}
1340#endif
1341
glennrpd5045b42010-03-24 12:40:35 +00001342#if PNG_LIBPNG_VER > 10011
cristy3ed852e2009-09-05 21:47:34 +00001343/*
1344%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1345% %
1346% %
1347% %
1348% R e a d P N G I m a g e %
1349% %
1350% %
1351% %
1352%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1353%
1354% ReadPNGImage() reads a Portable Network Graphics (PNG) or
1355% Multiple-image Network Graphics (MNG) image file and returns it. It
1356% allocates the memory necessary for the new Image structure and returns a
1357% pointer to the new image or set of images.
1358%
1359% MNG support written by Glenn Randers-Pehrson, glennrp@image...
1360%
1361% The format of the ReadPNGImage method is:
1362%
1363% Image *ReadPNGImage(const ImageInfo *image_info,ExceptionInfo *exception)
1364%
1365% A description of each parameter follows:
1366%
1367% o image_info: the image info.
1368%
1369% o exception: return any errors or warnings in this structure.
1370%
1371% To do, more or less in chronological order (as of version 5.5.2,
1372% November 26, 2002 -- glennrp -- see also "To do" under WriteMNGImage):
1373%
1374% Get 16-bit cheap transparency working.
1375%
1376% (At this point, PNG decoding is supposed to be in full MNG-LC compliance)
1377%
1378% Preserve all unknown and not-yet-handled known chunks found in input
1379% PNG file and copy them into output PNG files according to the PNG
1380% copying rules.
1381%
1382% (At this point, PNG encoding should be in full MNG compliance)
1383%
1384% Provide options for choice of background to use when the MNG BACK
1385% chunk is not present or is not mandatory (i.e., leave transparent,
1386% user specified, MNG BACK, PNG bKGD)
1387%
1388% Implement LOOP/ENDL [done, but could do discretionary loops more
1389% efficiently by linking in the duplicate frames.].
1390%
1391% Decode and act on the MHDR simplicity profile (offer option to reject
1392% files or attempt to process them anyway when the profile isn't LC or VLC).
1393%
1394% Upgrade to full MNG without Delta-PNG.
1395%
1396% o BACK [done a while ago except for background image ID]
1397% o MOVE [done 15 May 1999]
1398% o CLIP [done 15 May 1999]
1399% o DISC [done 19 May 1999]
1400% o SAVE [partially done 19 May 1999 (marks objects frozen)]
1401% o SEEK [partially done 19 May 1999 (discard function only)]
1402% o SHOW
1403% o PAST
1404% o BASI
1405% o MNG-level tEXt/iTXt/zTXt
1406% o pHYg
1407% o pHYs
1408% o sBIT
1409% o bKGD
1410% o iTXt (wait for libpng implementation).
1411%
1412% Use the scene signature to discover when an identical scene is
1413% being reused, and just point to the original image->exception instead
1414% of storing another set of pixels. This not specific to MNG
1415% but could be applied generally.
1416%
1417% Upgrade to full MNG with Delta-PNG.
1418%
1419% JNG tEXt/iTXt/zTXt
1420%
1421% We will not attempt to read files containing the CgBI chunk.
1422% They are really Xcode files meant for display on the iPhone.
1423% These are not valid PNG files and it is impossible to recover
glennrpf54e2952011-06-29 14:20:44 +00001424% the original PNG from files that have been converted to Xcode-PNG,
cristy3ed852e2009-09-05 21:47:34 +00001425% since irretrievable loss of color data has occurred due to the
1426% use of premultiplied alpha.
1427*/
1428
1429#if defined(__cplusplus) || defined(c_plusplus)
1430extern "C" {
1431#endif
1432
1433/*
1434 This the function that does the actual reading of data. It is
1435 the same as the one supplied in libpng, except that it receives the
1436 datastream from the ReadBlob() function instead of standard input.
1437*/
1438static void png_get_data(png_structp png_ptr,png_bytep data,png_size_t length)
1439{
1440 Image
1441 *image;
1442
1443 image=(Image *) png_get_io_ptr(png_ptr);
1444 if (length)
1445 {
1446 png_size_t
1447 check;
1448
1449 check=(png_size_t) ReadBlob(image,(size_t) length,data);
1450 if (check != length)
1451 {
1452 char
1453 msg[MaxTextExtent];
1454
cristy3b6fd2e2011-05-20 12:53:50 +00001455 (void) FormatLocaleString(msg,MaxTextExtent,
cristye8c25f92010-06-03 00:53:06 +00001456 "Expected %.20g bytes; found %.20g bytes",(double) length,
1457 (double) check);
cristy3ed852e2009-09-05 21:47:34 +00001458 png_warning(png_ptr,msg);
1459 png_error(png_ptr,"Read Exception");
1460 }
1461 }
1462}
1463
1464#if !defined(PNG_READ_EMPTY_PLTE_SUPPORTED) && \
1465 !defined(PNG_MNG_FEATURES_SUPPORTED)
1466/* We use mng_get_data() instead of png_get_data() if we have a libpng
1467 * older than libpng-1.0.3a, which was the first to allow the empty
1468 * PLTE, or a newer libpng in which PNG_MNG_FEATURES_SUPPORTED was
1469 * ifdef'ed out. Earlier versions would crash if the bKGD chunk was
1470 * encountered after an empty PLTE, so we have to look ahead for bKGD
1471 * chunks and remove them from the datastream that is passed to libpng,
1472 * and store their contents for later use.
1473 */
1474static void mng_get_data(png_structp png_ptr,png_bytep data,png_size_t length)
1475{
1476 MngInfo
1477 *mng_info;
1478
1479 Image
1480 *image;
1481
1482 png_size_t
1483 check;
1484
cristybb503372010-05-27 20:51:26 +00001485 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001486 i;
1487
1488 i=0;
1489 mng_info=(MngInfo *) png_get_io_ptr(png_ptr);
1490 image=(Image *) mng_info->image;
1491 while (mng_info->bytes_in_read_buffer && length)
1492 {
1493 data[i]=mng_info->read_buffer[i];
1494 mng_info->bytes_in_read_buffer--;
1495 length--;
1496 i++;
1497 }
1498 if (length)
1499 {
1500 check=(png_size_t) ReadBlob(image,(size_t) length,(char *) data);
glennrp0fe50b42010-11-16 03:52:51 +00001501
cristy3ed852e2009-09-05 21:47:34 +00001502 if (check != length)
1503 png_error(png_ptr,"Read Exception");
glennrp0fe50b42010-11-16 03:52:51 +00001504
cristy3ed852e2009-09-05 21:47:34 +00001505 if (length == 4)
1506 {
1507 if ((data[0] == 0) && (data[1] == 0) && (data[2] == 0) &&
1508 (data[3] == 0))
1509 {
1510 check=(png_size_t) ReadBlob(image,(size_t) length,
1511 (char *) mng_info->read_buffer);
1512 mng_info->read_buffer[4]=0;
1513 mng_info->bytes_in_read_buffer=4;
1514 if (memcmp(mng_info->read_buffer,mng_PLTE,4) == 0)
1515 mng_info->found_empty_plte=MagickTrue;
1516 if (memcmp(mng_info->read_buffer,mng_IEND,4) == 0)
1517 {
1518 mng_info->found_empty_plte=MagickFalse;
1519 mng_info->have_saved_bkgd_index=MagickFalse;
1520 }
1521 }
glennrp0fe50b42010-11-16 03:52:51 +00001522
cristy3ed852e2009-09-05 21:47:34 +00001523 if ((data[0] == 0) && (data[1] == 0) && (data[2] == 0) &&
1524 (data[3] == 1))
1525 {
1526 check=(png_size_t) ReadBlob(image,(size_t) length,
1527 (char *) mng_info->read_buffer);
1528 mng_info->read_buffer[4]=0;
1529 mng_info->bytes_in_read_buffer=4;
1530 if (memcmp(mng_info->read_buffer,mng_bKGD,4) == 0)
1531 if (mng_info->found_empty_plte)
1532 {
1533 /*
1534 Skip the bKGD data byte and CRC.
1535 */
1536 check=(png_size_t)
1537 ReadBlob(image,5,(char *) mng_info->read_buffer);
1538 check=(png_size_t) ReadBlob(image,(size_t) length,
1539 (char *) mng_info->read_buffer);
1540 mng_info->saved_bkgd_index=mng_info->read_buffer[0];
1541 mng_info->have_saved_bkgd_index=MagickTrue;
1542 mng_info->bytes_in_read_buffer=0;
1543 }
1544 }
1545 }
1546 }
1547}
1548#endif
1549
1550static void png_put_data(png_structp png_ptr,png_bytep data,png_size_t length)
1551{
1552 Image
1553 *image;
1554
1555 image=(Image *) png_get_io_ptr(png_ptr);
1556 if (length)
1557 {
1558 png_size_t
1559 check;
1560
cristybb503372010-05-27 20:51:26 +00001561 check=(png_size_t) WriteBlob(image,(size_t) length,data);
glennrp0fe50b42010-11-16 03:52:51 +00001562
cristy3ed852e2009-09-05 21:47:34 +00001563 if (check != length)
1564 png_error(png_ptr,"WriteBlob Failed");
1565 }
1566}
1567
1568static void png_flush_data(png_structp png_ptr)
1569{
1570 (void) png_ptr;
1571}
1572
1573#ifdef PNG_WRITE_EMPTY_PLTE_SUPPORTED
1574static int PalettesAreEqual(Image *a,Image *b)
1575{
cristybb503372010-05-27 20:51:26 +00001576 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001577 i;
1578
1579 if ((a == (Image *) NULL) || (b == (Image *) NULL))
1580 return((int) MagickFalse);
glennrp0fe50b42010-11-16 03:52:51 +00001581
cristy3ed852e2009-09-05 21:47:34 +00001582 if (a->storage_class != PseudoClass || b->storage_class != PseudoClass)
1583 return((int) MagickFalse);
glennrp0fe50b42010-11-16 03:52:51 +00001584
cristy3ed852e2009-09-05 21:47:34 +00001585 if (a->colors != b->colors)
1586 return((int) MagickFalse);
glennrp0fe50b42010-11-16 03:52:51 +00001587
cristybb503372010-05-27 20:51:26 +00001588 for (i=0; i < (ssize_t) a->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00001589 {
1590 if ((a->colormap[i].red != b->colormap[i].red) ||
1591 (a->colormap[i].green != b->colormap[i].green) ||
1592 (a->colormap[i].blue != b->colormap[i].blue))
1593 return((int) MagickFalse);
1594 }
glennrp0fe50b42010-11-16 03:52:51 +00001595
cristy3ed852e2009-09-05 21:47:34 +00001596 return((int) MagickTrue);
1597}
1598#endif
1599
1600static void MngInfoDiscardObject(MngInfo *mng_info,int i)
1601{
1602 if (i && (i < MNG_MAX_OBJECTS) && (mng_info != (MngInfo *) NULL) &&
1603 mng_info->exists[i] && !mng_info->frozen[i])
1604 {
1605#ifdef MNG_OBJECT_BUFFERS
1606 if (mng_info->ob[i] != (MngBuffer *) NULL)
1607 {
1608 if (mng_info->ob[i]->reference_count > 0)
1609 mng_info->ob[i]->reference_count--;
glennrp0fe50b42010-11-16 03:52:51 +00001610
cristy3ed852e2009-09-05 21:47:34 +00001611 if (mng_info->ob[i]->reference_count == 0)
1612 {
1613 if (mng_info->ob[i]->image != (Image *) NULL)
1614 mng_info->ob[i]->image=DestroyImage(mng_info->ob[i]->image);
glennrp0fe50b42010-11-16 03:52:51 +00001615
cristy3ed852e2009-09-05 21:47:34 +00001616 mng_info->ob[i]=DestroyString(mng_info->ob[i]);
1617 }
1618 }
1619 mng_info->ob[i]=(MngBuffer *) NULL;
1620#endif
1621 mng_info->exists[i]=MagickFalse;
1622 mng_info->invisible[i]=MagickFalse;
1623 mng_info->viewable[i]=MagickFalse;
1624 mng_info->frozen[i]=MagickFalse;
1625 mng_info->x_off[i]=0;
1626 mng_info->y_off[i]=0;
1627 mng_info->object_clip[i].left=0;
cristybb503372010-05-27 20:51:26 +00001628 mng_info->object_clip[i].right=(ssize_t) PNG_UINT_31_MAX;
cristy3ed852e2009-09-05 21:47:34 +00001629 mng_info->object_clip[i].top=0;
cristybb503372010-05-27 20:51:26 +00001630 mng_info->object_clip[i].bottom=(ssize_t) PNG_UINT_31_MAX;
cristy3ed852e2009-09-05 21:47:34 +00001631 }
1632}
1633
glennrp21f0e622011-01-07 16:20:57 +00001634static void MngInfoFreeStruct(MngInfo *mng_info,
1635 MagickBooleanType *have_mng_structure)
cristy3ed852e2009-09-05 21:47:34 +00001636{
glennrp21f0e622011-01-07 16:20:57 +00001637 if (*have_mng_structure != MagickFalse && (mng_info != (MngInfo *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00001638 {
cristybb503372010-05-27 20:51:26 +00001639 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001640 i;
1641
1642 for (i=1; i < MNG_MAX_OBJECTS; i++)
1643 MngInfoDiscardObject(mng_info,i);
glennrp0fe50b42010-11-16 03:52:51 +00001644
cristy3ed852e2009-09-05 21:47:34 +00001645 if (mng_info->global_plte != (png_colorp) NULL)
1646 mng_info->global_plte=(png_colorp)
1647 RelinquishMagickMemory(mng_info->global_plte);
glennrp0fe50b42010-11-16 03:52:51 +00001648
cristy3ed852e2009-09-05 21:47:34 +00001649 mng_info=(MngInfo *) RelinquishMagickMemory(mng_info);
1650 *have_mng_structure=MagickFalse;
1651 }
1652}
1653
1654static MngBox mng_minimum_box(MngBox box1,MngBox box2)
1655{
1656 MngBox
1657 box;
1658
1659 box=box1;
1660 if (box.left < box2.left)
1661 box.left=box2.left;
glennrp0fe50b42010-11-16 03:52:51 +00001662
cristy3ed852e2009-09-05 21:47:34 +00001663 if (box.top < box2.top)
1664 box.top=box2.top;
glennrp0fe50b42010-11-16 03:52:51 +00001665
cristy3ed852e2009-09-05 21:47:34 +00001666 if (box.right > box2.right)
1667 box.right=box2.right;
glennrp0fe50b42010-11-16 03:52:51 +00001668
cristy3ed852e2009-09-05 21:47:34 +00001669 if (box.bottom > box2.bottom)
1670 box.bottom=box2.bottom;
glennrp0fe50b42010-11-16 03:52:51 +00001671
cristy3ed852e2009-09-05 21:47:34 +00001672 return box;
1673}
1674
1675static MngBox mng_read_box(MngBox previous_box,char delta_type,unsigned char *p)
1676{
1677 MngBox
1678 box;
1679
1680 /*
1681 Read clipping boundaries from DEFI, CLIP, FRAM, or PAST chunk.
1682 */
cristybb503372010-05-27 20:51:26 +00001683 box.left=(ssize_t) ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
1684 box.right=(ssize_t) ((p[4] << 24) | (p[5] << 16) | (p[6] << 8) | p[7]);
1685 box.top=(ssize_t) ((p[8] << 24) | (p[9] << 16) | (p[10] << 8) | p[11]);
1686 box.bottom=(ssize_t) ((p[12] << 24) | (p[13] << 16) | (p[14] << 8) | p[15]);
cristy3ed852e2009-09-05 21:47:34 +00001687 if (delta_type != 0)
1688 {
1689 box.left+=previous_box.left;
1690 box.right+=previous_box.right;
1691 box.top+=previous_box.top;
1692 box.bottom+=previous_box.bottom;
1693 }
glennrp0fe50b42010-11-16 03:52:51 +00001694
cristy3ed852e2009-09-05 21:47:34 +00001695 return(box);
1696}
1697
1698static MngPair mng_read_pair(MngPair previous_pair,int delta_type,
1699 unsigned char *p)
1700{
1701 MngPair
1702 pair;
1703 /*
cristybb503372010-05-27 20:51:26 +00001704 Read two ssize_ts from CLON, MOVE or PAST chunk
cristy3ed852e2009-09-05 21:47:34 +00001705 */
cristy8182b072010-05-30 20:10:53 +00001706 pair.a=(long) ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
1707 pair.b=(long) ((p[4] << 24) | (p[5] << 16) | (p[6] << 8) | p[7]);
glennrp0fe50b42010-11-16 03:52:51 +00001708
cristy3ed852e2009-09-05 21:47:34 +00001709 if (delta_type != 0)
1710 {
1711 pair.a+=previous_pair.a;
1712 pair.b+=previous_pair.b;
1713 }
glennrp0fe50b42010-11-16 03:52:51 +00001714
cristy3ed852e2009-09-05 21:47:34 +00001715 return(pair);
1716}
1717
cristy8182b072010-05-30 20:10:53 +00001718static long mng_get_long(unsigned char *p)
cristy3ed852e2009-09-05 21:47:34 +00001719{
cristy8182b072010-05-30 20:10:53 +00001720 return((long) ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]));
cristy3ed852e2009-09-05 21:47:34 +00001721}
1722
cristyc82a27b2011-10-21 01:07:16 +00001723typedef struct _PNGErrorInfo
cristy3ed852e2009-09-05 21:47:34 +00001724{
1725 Image
1726 *image;
1727
cristyc82a27b2011-10-21 01:07:16 +00001728 ExceptionInfo
1729 *exception;
1730} PNGErrorInfo;
glennrp0fe50b42010-11-16 03:52:51 +00001731
cristyc82a27b2011-10-21 01:07:16 +00001732static void MagickPNGErrorHandler(png_struct *ping,png_const_charp message)
1733{
1734 ExceptionInfo
1735 *exception;
glennrp0fe50b42010-11-16 03:52:51 +00001736
cristyc82a27b2011-10-21 01:07:16 +00001737 Image
1738 *image;
1739
1740 PNGErrorInfo
1741 *error_info;
1742
1743 error_info=(PNGErrorInfo *) png_get_error_ptr(ping);
1744 image=error_info->image;
1745 exception=error_info->exception;
1746
1747 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1748 " libpng-%s error: %s", PNG_LIBPNG_VER_STRING,message);
1749
1750 (void) ThrowMagickException(exception,GetMagickModule(),CoderError,message,
1751 "`%s'",image->filename);
glennrp0fe50b42010-11-16 03:52:51 +00001752
glennrpe4017e32011-01-08 17:16:09 +00001753#if (PNG_LIBPNG_VER < 10500)
glennrp8371ecc2011-02-19 19:15:38 +00001754 /* A warning about deprecated use of jmpbuf here is unavoidable if you
1755 * are building with libpng-1.4.x and can be ignored.
1756 */
cristy3ed852e2009-09-05 21:47:34 +00001757 longjmp(ping->jmpbuf,1);
glennrpfaa852b2010-03-30 12:17:00 +00001758#else
1759 png_longjmp(ping,1);
1760#endif
cristy3ed852e2009-09-05 21:47:34 +00001761}
1762
glennrpcf002022011-01-30 02:38:15 +00001763static void MagickPNGWarningHandler(png_struct *ping,png_const_charp message)
cristy3ed852e2009-09-05 21:47:34 +00001764{
cristyc82a27b2011-10-21 01:07:16 +00001765 ExceptionInfo
1766 *exception;
1767
cristy3ed852e2009-09-05 21:47:34 +00001768 Image
1769 *image;
1770
cristyc82a27b2011-10-21 01:07:16 +00001771 PNGErrorInfo
1772 *error_info;
1773
cristy3ed852e2009-09-05 21:47:34 +00001774 if (LocaleCompare(message, "Missing PLTE before tRNS") == 0)
1775 png_error(ping, message);
glennrp0fe50b42010-11-16 03:52:51 +00001776
cristyc82a27b2011-10-21 01:07:16 +00001777 error_info=(PNGErrorInfo *) png_get_error_ptr(ping);
1778 image=error_info->image;
1779 exception=error_info->exception;
1780 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1781 " libpng-%s warning: %s", PNG_LIBPNG_VER_STRING,message);
glennrp0fe50b42010-11-16 03:52:51 +00001782
cristyc82a27b2011-10-21 01:07:16 +00001783 (void) ThrowMagickException(exception,GetMagickModule(),CoderWarning,
cristy3ed852e2009-09-05 21:47:34 +00001784 message,"`%s'",image->filename);
1785}
1786
1787#ifdef PNG_USER_MEM_SUPPORTED
glennrpcf002022011-01-30 02:38:15 +00001788static png_voidp Magick_png_malloc(png_structp png_ptr,png_uint_32 size)
cristy3ed852e2009-09-05 21:47:34 +00001789{
1790#if (PNG_LIBPNG_VER < 10011)
1791 png_voidp
1792 ret;
1793
cristydf0d90e2011-12-12 01:03:55 +00001794 (void) png_ptr;
cristy3ed852e2009-09-05 21:47:34 +00001795 ret=((png_voidp) AcquireMagickMemory((size_t) size));
glennrp0fe50b42010-11-16 03:52:51 +00001796
cristy3ed852e2009-09-05 21:47:34 +00001797 if (ret == NULL)
1798 png_error("Insufficient memory.");
glennrp0fe50b42010-11-16 03:52:51 +00001799
cristy3ed852e2009-09-05 21:47:34 +00001800 return(ret);
1801#else
cristydf0d90e2011-12-12 01:03:55 +00001802 (void) png_ptr;
cristy3ed852e2009-09-05 21:47:34 +00001803 return((png_voidp) AcquireMagickMemory((size_t) size));
1804#endif
1805}
1806
1807/*
1808 Free a pointer. It is removed from the list at the same time.
1809*/
glennrpcf002022011-01-30 02:38:15 +00001810static png_free_ptr Magick_png_free(png_structp png_ptr,png_voidp ptr)
cristy3ed852e2009-09-05 21:47:34 +00001811{
glennrp3bd393f2011-12-21 18:54:53 +00001812 (void) png_ptr;
cristy3ed852e2009-09-05 21:47:34 +00001813 ptr=RelinquishMagickMemory(ptr);
1814 return((png_free_ptr) NULL);
1815}
1816#endif
1817
1818#if defined(__cplusplus) || defined(c_plusplus)
1819}
1820#endif
1821
1822static int
glennrpcf002022011-01-30 02:38:15 +00001823Magick_png_read_raw_profile(Image *image, const ImageInfo *image_info,
cristyd15e6592011-10-15 00:13:06 +00001824 png_textp text,int ii,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001825{
cristybb503372010-05-27 20:51:26 +00001826 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001827 i;
1828
1829 register unsigned char
1830 *dp;
1831
1832 register png_charp
1833 sp;
1834
1835 png_uint_32
1836 length,
1837 nibbles;
1838
1839 StringInfo
1840 *profile;
1841
glennrp0c3e06b2010-11-19 13:45:02 +00001842 const unsigned char
cristy3ed852e2009-09-05 21:47:34 +00001843 unhex[103]={0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,
1844 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,
1845 0,0,0,0,0,0,0,0,0,1, 2,3,4,5,6,7,8,9,0,0,
1846 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,
1847 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,10,11,12,
1848 13,14,15};
1849
1850 sp=text[ii].text+1;
1851 /* look for newline */
1852 while (*sp != '\n')
1853 sp++;
glennrp0fe50b42010-11-16 03:52:51 +00001854
cristy3ed852e2009-09-05 21:47:34 +00001855 /* look for length */
1856 while (*sp == '\0' || *sp == ' ' || *sp == '\n')
1857 sp++;
glennrp0fe50b42010-11-16 03:52:51 +00001858
cristyf2f27272009-12-17 14:48:46 +00001859 length=(png_uint_32) StringToLong(sp);
glennrp0fe50b42010-11-16 03:52:51 +00001860
glennrp97f90e22011-02-22 05:47:58 +00001861 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1862 " length: %lu",(unsigned long) length);
1863
cristy3ed852e2009-09-05 21:47:34 +00001864 while (*sp != ' ' && *sp != '\n')
1865 sp++;
glennrp0fe50b42010-11-16 03:52:51 +00001866
cristy3ed852e2009-09-05 21:47:34 +00001867 /* allocate space */
1868 if (length == 0)
1869 {
cristyc82a27b2011-10-21 01:07:16 +00001870 (void) ThrowMagickException(exception,GetMagickModule(),
cristy3ed852e2009-09-05 21:47:34 +00001871 CoderWarning,"UnableToCopyProfile","`%s'","invalid profile length");
1872 return(MagickFalse);
1873 }
glennrp0fe50b42010-11-16 03:52:51 +00001874
cristy8723e4b2011-09-01 13:11:19 +00001875 profile=BlobToStringInfo((const void *) NULL,length);
glennrp0fe50b42010-11-16 03:52:51 +00001876
cristy3ed852e2009-09-05 21:47:34 +00001877 if (profile == (StringInfo *) NULL)
1878 {
cristyc82a27b2011-10-21 01:07:16 +00001879 (void) ThrowMagickException(exception,GetMagickModule(),
cristy3ed852e2009-09-05 21:47:34 +00001880 ResourceLimitError,"MemoryAllocationFailed","`%s'",
1881 "unable to copy profile");
1882 return(MagickFalse);
1883 }
glennrp0fe50b42010-11-16 03:52:51 +00001884
cristy3ed852e2009-09-05 21:47:34 +00001885 /* copy profile, skipping white space and column 1 "=" signs */
1886 dp=GetStringInfoDatum(profile);
1887 nibbles=length*2;
glennrp0fe50b42010-11-16 03:52:51 +00001888
cristybb503372010-05-27 20:51:26 +00001889 for (i=0; i < (ssize_t) nibbles; i++)
cristy3ed852e2009-09-05 21:47:34 +00001890 {
1891 while (*sp < '0' || (*sp > '9' && *sp < 'a') || *sp > 'f')
1892 {
1893 if (*sp == '\0')
1894 {
cristyc82a27b2011-10-21 01:07:16 +00001895 (void) ThrowMagickException(exception,GetMagickModule(),
cristy3ed852e2009-09-05 21:47:34 +00001896 CoderWarning,"UnableToCopyProfile","`%s'","ran out of data");
1897 profile=DestroyStringInfo(profile);
1898 return(MagickFalse);
1899 }
1900 sp++;
1901 }
glennrp0fe50b42010-11-16 03:52:51 +00001902
cristy3ed852e2009-09-05 21:47:34 +00001903 if (i%2 == 0)
1904 *dp=(unsigned char) (16*unhex[(int) *sp++]);
glennrp0fe50b42010-11-16 03:52:51 +00001905
cristy3ed852e2009-09-05 21:47:34 +00001906 else
1907 (*dp++)+=unhex[(int) *sp++];
1908 }
1909 /*
1910 We have already read "Raw profile type.
1911 */
cristyd15e6592011-10-15 00:13:06 +00001912 (void) SetImageProfile(image,&text[ii].key[17],profile,exception);
cristy3ed852e2009-09-05 21:47:34 +00001913 profile=DestroyStringInfo(profile);
glennrp0fe50b42010-11-16 03:52:51 +00001914
cristy3ed852e2009-09-05 21:47:34 +00001915 if (image_info->verbose)
1916 (void) printf(" Found a generic profile, type %s\n",&text[ii].key[17]);
glennrp0fe50b42010-11-16 03:52:51 +00001917
cristy3ed852e2009-09-05 21:47:34 +00001918 return MagickTrue;
1919}
1920
1921#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
1922static int read_vpag_chunk_callback(png_struct *ping, png_unknown_chunkp chunk)
1923{
1924 Image
1925 *image;
1926
1927
1928 /* The unknown chunk structure contains the chunk data:
1929 png_byte name[5];
1930 png_byte *data;
1931 png_size_t size;
1932
1933 Note that libpng has already taken care of the CRC handling.
1934 */
1935
1936
1937 if (chunk->name[0] != 118 || chunk->name[1] != 112 ||
1938 chunk->name[2] != 65 ||chunk-> name[3] != 103)
1939 return(0); /* Did not recognize */
1940
1941 /* recognized vpAg */
1942
1943 if (chunk->size != 9)
1944 return(-1); /* Error return */
1945
1946 if (chunk->data[8] != 0)
1947 return(0); /* ImageMagick requires pixel units */
1948
1949 image=(Image *) png_get_user_chunk_ptr(ping);
1950
cristybb503372010-05-27 20:51:26 +00001951 image->page.width=(size_t) ((chunk->data[0] << 24) |
cristy3ed852e2009-09-05 21:47:34 +00001952 (chunk->data[1] << 16) | (chunk->data[2] << 8) | chunk->data[3]);
glennrp0fe50b42010-11-16 03:52:51 +00001953
cristybb503372010-05-27 20:51:26 +00001954 image->page.height=(size_t) ((chunk->data[4] << 24) |
cristy3ed852e2009-09-05 21:47:34 +00001955 (chunk->data[5] << 16) | (chunk->data[6] << 8) | chunk->data[7]);
1956
1957 /* Return one of the following: */
1958 /* return(-n); chunk had an error */
1959 /* return(0); did not recognize */
1960 /* return(n); success */
1961
1962 return(1);
1963
1964}
1965#endif
1966
1967/*
1968%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1969% %
1970% %
1971% %
1972% R e a d O n e P N G I m a g e %
1973% %
1974% %
1975% %
1976%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1977%
1978% ReadOnePNGImage() reads a Portable Network Graphics (PNG) image file
1979% (minus the 8-byte signature) and returns it. It allocates the memory
1980% necessary for the new Image structure and returns a pointer to the new
1981% image.
1982%
1983% The format of the ReadOnePNGImage method is:
1984%
1985% Image *ReadOnePNGImage(MngInfo *mng_info, const ImageInfo *image_info,
1986% ExceptionInfo *exception)
1987%
1988% A description of each parameter follows:
1989%
1990% o mng_info: Specifies a pointer to a MngInfo structure.
1991%
1992% o image_info: the image info.
1993%
1994% o exception: return any errors or warnings in this structure.
1995%
1996*/
1997static Image *ReadOnePNGImage(MngInfo *mng_info,
1998 const ImageInfo *image_info, ExceptionInfo *exception)
1999{
2000 /* Read one PNG image */
2001
glennrpcc95c3f2011-04-18 16:46:48 +00002002 /* To do: Read the tIME chunk into the date:modify property */
2003 /* To do: Read the tEXt/Creation Time chunk into the date:create property */
2004
cristy3ed852e2009-09-05 21:47:34 +00002005 Image
2006 *image;
2007
2008 int
glennrp4eb39312011-03-30 21:34:55 +00002009 intent,
glennrpcb395ac2011-03-30 19:50:23 +00002010 num_raw_profiles,
cristy3ed852e2009-09-05 21:47:34 +00002011 num_text,
glennrp4eb39312011-03-30 21:34:55 +00002012 num_text_total,
cristy3ed852e2009-09-05 21:47:34 +00002013 num_passes,
glennrpfaa852b2010-03-30 12:17:00 +00002014 pass,
2015 ping_bit_depth,
2016 ping_color_type,
2017 ping_interlace_method,
2018 ping_compression_method,
2019 ping_filter_method,
glennrp4eb39312011-03-30 21:34:55 +00002020 ping_num_trans,
2021 unit_type;
2022
2023 double
2024 file_gamma;
cristy3ed852e2009-09-05 21:47:34 +00002025
2026 MagickBooleanType
cristy4383ec82011-01-05 15:42:32 +00002027 logging,
cristy3ed852e2009-09-05 21:47:34 +00002028 status;
2029
cristyc82a27b2011-10-21 01:07:16 +00002030 PixelInfo
2031 transparent_color;
2032
2033 PNGErrorInfo
2034 error_info;
2035
glennrpfaa852b2010-03-30 12:17:00 +00002036 png_bytep
2037 ping_trans_alpha;
2038
2039 png_color_16p
2040 ping_background,
2041 ping_trans_color;
2042
cristy3ed852e2009-09-05 21:47:34 +00002043 png_info
2044 *end_info,
2045 *ping_info;
2046
2047 png_struct
2048 *ping;
2049
2050 png_textp
2051 text;
2052
glennrpfaa852b2010-03-30 12:17:00 +00002053 png_uint_32
2054 ping_height,
2055 ping_width,
glennrp4eb39312011-03-30 21:34:55 +00002056 x_resolution,
2057 y_resolution;
glennrpfaa852b2010-03-30 12:17:00 +00002058
cristy3ed852e2009-09-05 21:47:34 +00002059 QuantumInfo
2060 *quantum_info;
2061
2062 unsigned char
glennrpcf002022011-01-30 02:38:15 +00002063 *ping_pixels;
cristy3ed852e2009-09-05 21:47:34 +00002064
cristybb503372010-05-27 20:51:26 +00002065 ssize_t
cristy756ae432011-11-19 02:18:25 +00002066 ping_rowbytes,
cristy3ed852e2009-09-05 21:47:34 +00002067 y;
2068
2069 register unsigned char
2070 *p;
2071
cristybb503372010-05-27 20:51:26 +00002072 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002073 i,
2074 x;
2075
cristy4c08aed2011-07-01 19:47:50 +00002076 register Quantum
cristy3ed852e2009-09-05 21:47:34 +00002077 *q;
2078
2079 size_t
glennrp39992b42010-11-14 00:03:43 +00002080 length,
cristy3ed852e2009-09-05 21:47:34 +00002081 row_offset;
2082
cristyeb3b22a2011-03-31 20:16:11 +00002083 ssize_t
2084 j;
2085
cristy3ed852e2009-09-05 21:47:34 +00002086#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
2087 png_byte unused_chunks[]=
2088 {
2089 104, 73, 83, 84, (png_byte) '\0', /* hIST */
2090 105, 84, 88, 116, (png_byte) '\0', /* iTXt */
2091 112, 67, 65, 76, (png_byte) '\0', /* pCAL */
2092 115, 67, 65, 76, (png_byte) '\0', /* sCAL */
2093 115, 80, 76, 84, (png_byte) '\0', /* sPLT */
2094 116, 73, 77, 69, (png_byte) '\0', /* tIME */
2095 };
2096#endif
2097
2098 logging=LogMagickEvent(CoderEvent,GetMagickModule(),
glennrpfd05d622011-02-25 04:10:33 +00002099 " Enter ReadOnePNGImage()");
cristy3ed852e2009-09-05 21:47:34 +00002100
2101#if defined(PNG_SETJMP_NOT_THREAD_SAFE)
glennrpcf002022011-01-30 02:38:15 +00002102 LockSemaphoreInfo(ping_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00002103#endif
2104
glennrp25c1e2b2010-03-25 01:39:56 +00002105#if (PNG_LIBPNG_VER < 10200)
cristy3ed852e2009-09-05 21:47:34 +00002106 if (image_info->verbose)
2107 printf("Your PNG library (libpng-%s) is rather old.\n",
2108 PNG_LIBPNG_VER_STRING);
2109#endif
2110
glennrp61b4c952009-11-10 20:40:41 +00002111#if (PNG_LIBPNG_VER >= 10400)
2112# ifndef PNG_TRANSFORM_GRAY_TO_RGB /* Added at libpng-1.4.0beta67 */
2113 if (image_info->verbose)
2114 {
2115 printf("Your PNG library (libpng-%s) is an old beta version.\n",
2116 PNG_LIBPNG_VER_STRING);
2117 printf("Please update it.\n");
2118 }
2119# endif
2120#endif
2121
2122
cristyed552522009-10-16 14:04:35 +00002123 quantum_info = (QuantumInfo *) NULL;
cristy3ed852e2009-09-05 21:47:34 +00002124 image=mng_info->image;
2125
glennrpa6a06632011-01-19 15:15:34 +00002126 if (logging != MagickFalse)
2127 (void)LogMagickEvent(CoderEvent,GetMagickModule(),
2128 " image->matte=%d",(int) image->matte);
2129
glennrp0e319732011-01-25 21:53:13 +00002130 /* Set to an out-of-range color unless tRNS chunk is present */
2131 transparent_color.red=65537;
2132 transparent_color.green=65537;
2133 transparent_color.blue=65537;
cristy4c08aed2011-07-01 19:47:50 +00002134 transparent_color.alpha=65537;
glennrp0e319732011-01-25 21:53:13 +00002135
glennrpcb395ac2011-03-30 19:50:23 +00002136 num_text = 0;
glennrp4eb39312011-03-30 21:34:55 +00002137 num_text_total = 0;
glennrpcb395ac2011-03-30 19:50:23 +00002138 num_raw_profiles = 0;
2139
cristy3ed852e2009-09-05 21:47:34 +00002140 /*
2141 Allocate the PNG structures
2142 */
2143#ifdef PNG_USER_MEM_SUPPORTED
cristyc82a27b2011-10-21 01:07:16 +00002144 error_info.image=image;
2145 error_info.exception=exception;
2146 ping=png_create_read_struct_2(PNG_LIBPNG_VER_STRING,&error_info,
glennrpcf002022011-01-30 02:38:15 +00002147 MagickPNGErrorHandler,MagickPNGWarningHandler, NULL,
2148 (png_malloc_ptr) Magick_png_malloc,(png_free_ptr) Magick_png_free);
cristy3ed852e2009-09-05 21:47:34 +00002149#else
cristyc82a27b2011-10-21 01:07:16 +00002150 ping=png_create_read_struct(PNG_LIBPNG_VER_STRING,&error_info,
glennrpcf002022011-01-30 02:38:15 +00002151 MagickPNGErrorHandler,MagickPNGWarningHandler);
cristy3ed852e2009-09-05 21:47:34 +00002152#endif
2153 if (ping == (png_struct *) NULL)
2154 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
glennrp0fe50b42010-11-16 03:52:51 +00002155
cristy3ed852e2009-09-05 21:47:34 +00002156 ping_info=png_create_info_struct(ping);
glennrp0fe50b42010-11-16 03:52:51 +00002157
cristy3ed852e2009-09-05 21:47:34 +00002158 if (ping_info == (png_info *) NULL)
2159 {
2160 png_destroy_read_struct(&ping,(png_info **) NULL,(png_info **) NULL);
2161 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
2162 }
glennrp0fe50b42010-11-16 03:52:51 +00002163
cristy3ed852e2009-09-05 21:47:34 +00002164 end_info=png_create_info_struct(ping);
glennrp0fe50b42010-11-16 03:52:51 +00002165
cristy3ed852e2009-09-05 21:47:34 +00002166 if (end_info == (png_info *) NULL)
2167 {
2168 png_destroy_read_struct(&ping,&ping_info,(png_info **) NULL);
2169 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
2170 }
glennrp0fe50b42010-11-16 03:52:51 +00002171
glennrpcf002022011-01-30 02:38:15 +00002172 ping_pixels=(unsigned char *) NULL;
glennrp0fe50b42010-11-16 03:52:51 +00002173
glennrpfaa852b2010-03-30 12:17:00 +00002174 if (setjmp(png_jmpbuf(ping)))
cristy3ed852e2009-09-05 21:47:34 +00002175 {
2176 /*
2177 PNG image is corrupt.
2178 */
2179 png_destroy_read_struct(&ping,&ping_info,&end_info);
2180#if defined(PNG_SETJMP_NOT_THREAD_SAFE)
glennrpcf002022011-01-30 02:38:15 +00002181 UnlockSemaphoreInfo(ping_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00002182#endif
2183 if (logging != MagickFalse)
2184 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2185 " exit ReadOnePNGImage() with error.");
glennrp0fe50b42010-11-16 03:52:51 +00002186
cristy3ed852e2009-09-05 21:47:34 +00002187 if (image != (Image *) NULL)
cristy7b755eb2009-12-05 14:51:29 +00002188 {
cristyc82a27b2011-10-21 01:07:16 +00002189 InheritException(exception,exception);
cristy7b755eb2009-12-05 14:51:29 +00002190 image->columns=0;
2191 }
glennrp0fe50b42010-11-16 03:52:51 +00002192
cristy3ed852e2009-09-05 21:47:34 +00002193 return(GetFirstImageInList(image));
2194 }
2195 /*
2196 Prepare PNG for reading.
2197 */
glennrpfaa852b2010-03-30 12:17:00 +00002198
cristy3ed852e2009-09-05 21:47:34 +00002199 mng_info->image_found++;
2200 png_set_sig_bytes(ping,8);
glennrp0fe50b42010-11-16 03:52:51 +00002201
cristy3ed852e2009-09-05 21:47:34 +00002202 if (LocaleCompare(image_info->magick,"MNG") == 0)
2203 {
2204#if defined(PNG_MNG_FEATURES_SUPPORTED)
2205 (void) png_permit_mng_features(ping,PNG_ALL_MNG_FEATURES);
2206 png_set_read_fn(ping,image,png_get_data);
2207#else
2208#if defined(PNG_READ_EMPTY_PLTE_SUPPORTED)
2209 png_permit_empty_plte(ping,MagickTrue);
2210 png_set_read_fn(ping,image,png_get_data);
2211#else
2212 mng_info->image=image;
2213 mng_info->bytes_in_read_buffer=0;
2214 mng_info->found_empty_plte=MagickFalse;
2215 mng_info->have_saved_bkgd_index=MagickFalse;
2216 png_set_read_fn(ping,mng_info,mng_get_data);
2217#endif
2218#endif
2219 }
glennrp0fe50b42010-11-16 03:52:51 +00002220
cristy3ed852e2009-09-05 21:47:34 +00002221 else
2222 png_set_read_fn(ping,image,png_get_data);
2223
2224#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
2225 /* Ignore unused chunks and all unknown chunks except for vpAg */
2226 png_set_keep_unknown_chunks(ping, 1, NULL, 0);
2227 png_set_keep_unknown_chunks(ping, 2, mng_vpAg, 1);
2228 png_set_keep_unknown_chunks(ping, 1, unused_chunks,
2229 (int)sizeof(unused_chunks)/5);
2230 /* Callback for other unknown chunks */
2231 png_set_read_user_chunk_fn(ping, image, read_vpag_chunk_callback);
2232#endif
2233
glennrp991e92a2010-01-28 03:09:00 +00002234#if (PNG_LIBPNG_VER < 10400)
2235# if defined(PNG_USE_PNGGCCRD) && defined(PNG_ASSEMBLER_CODE_SUPPORTED) && \
2236 (PNG_LIBPNG_VER >= 10200) && (PNG_LIBPNG_VER < 10220) && defined(__i386__)
cristy3ed852e2009-09-05 21:47:34 +00002237 /* Disable thread-unsafe features of pnggccrd */
2238 if (png_access_version_number() >= 10200)
2239 {
2240 png_uint_32 mmx_disable_mask=0;
2241 png_uint_32 asm_flags;
2242
2243 mmx_disable_mask |= ( PNG_ASM_FLAG_MMX_READ_COMBINE_ROW \
2244 | PNG_ASM_FLAG_MMX_READ_FILTER_SUB \
2245 | PNG_ASM_FLAG_MMX_READ_FILTER_AVG \
2246 | PNG_ASM_FLAG_MMX_READ_FILTER_PAETH );
2247 asm_flags=png_get_asm_flags(ping);
2248 png_set_asm_flags(ping, asm_flags & ~mmx_disable_mask);
2249 }
glennrp991e92a2010-01-28 03:09:00 +00002250# endif
cristy3ed852e2009-09-05 21:47:34 +00002251#endif
2252
2253 png_read_info(ping,ping_info);
glennrpfaa852b2010-03-30 12:17:00 +00002254
2255 png_get_IHDR(ping,ping_info,&ping_width,&ping_height,
2256 &ping_bit_depth,&ping_color_type,
2257 &ping_interlace_method,&ping_compression_method,
2258 &ping_filter_method);
2259
2260 (void) png_get_tRNS(ping, ping_info, &ping_trans_alpha, &ping_num_trans,
2261 &ping_trans_color);
2262
2263 (void) png_get_bKGD(ping, ping_info, &ping_background);
2264
2265 if (ping_bit_depth < 8)
cristy3ed852e2009-09-05 21:47:34 +00002266 {
glennrpfaa852b2010-03-30 12:17:00 +00002267 if (((int) ping_color_type == PNG_COLOR_TYPE_PALETTE))
2268 {
2269 png_set_packing(ping);
2270 ping_bit_depth = 8;
2271 }
cristy3ed852e2009-09-05 21:47:34 +00002272 }
glennrpfaa852b2010-03-30 12:17:00 +00002273
2274 image->depth=ping_bit_depth;
cristy3ed852e2009-09-05 21:47:34 +00002275 image->depth=GetImageQuantumDepth(image,MagickFalse);
glennrpfaa852b2010-03-30 12:17:00 +00002276 image->interlace=ping_interlace_method != 0 ? PNGInterlace : NoInterlace;
cristy3ed852e2009-09-05 21:47:34 +00002277 if (logging != MagickFalse)
2278 {
2279 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00002280 " PNG width: %.20g, height: %.20g",
2281 (double) ping_width, (double) ping_height);
glennrp0fe50b42010-11-16 03:52:51 +00002282
cristy3ed852e2009-09-05 21:47:34 +00002283 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2284 " PNG color_type: %d, bit_depth: %d",
glennrpfaa852b2010-03-30 12:17:00 +00002285 ping_color_type, ping_bit_depth);
glennrp0fe50b42010-11-16 03:52:51 +00002286
cristy3ed852e2009-09-05 21:47:34 +00002287 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2288 " PNG compression_method: %d",
glennrpfaa852b2010-03-30 12:17:00 +00002289 ping_compression_method);
glennrp0fe50b42010-11-16 03:52:51 +00002290
cristy3ed852e2009-09-05 21:47:34 +00002291 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2292 " PNG interlace_method: %d, filter_method: %d",
glennrpfaa852b2010-03-30 12:17:00 +00002293 ping_interlace_method,ping_filter_method);
cristy3ed852e2009-09-05 21:47:34 +00002294 }
2295
glennrpfaa852b2010-03-30 12:17:00 +00002296#ifdef PNG_READ_iCCP_SUPPORTED
2297 if (png_get_valid(ping,ping_info,PNG_INFO_iCCP))
cristy3ed852e2009-09-05 21:47:34 +00002298 {
2299 int
2300 compression;
2301
glennrpe4017e32011-01-08 17:16:09 +00002302#if (PNG_LIBPNG_VER < 10500)
cristy3ed852e2009-09-05 21:47:34 +00002303 png_charp
glennrpe4017e32011-01-08 17:16:09 +00002304 info;
2305#else
2306 png_bytep
2307 info;
2308#endif
2309
2310 png_charp
cristy3ed852e2009-09-05 21:47:34 +00002311 name;
2312
2313 png_uint_32
2314 profile_length;
2315
2316 (void) png_get_iCCP(ping,ping_info,&name,(int *) &compression,&info,
2317 &profile_length);
glennrp0fe50b42010-11-16 03:52:51 +00002318
cristy3ed852e2009-09-05 21:47:34 +00002319 if (profile_length != 0)
2320 {
2321 StringInfo
2322 *profile;
2323
2324 if (logging != MagickFalse)
2325 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2326 " Reading PNG iCCP chunk.");
cristye8f8f382011-09-01 13:32:37 +00002327 profile=BlobToStringInfo(info,profile_length);
2328 if (profile == (StringInfo *) NULL)
2329 {
cristyc82a27b2011-10-21 01:07:16 +00002330 (void) ThrowMagickException(exception,GetMagickModule(),
cristye8f8f382011-09-01 13:32:37 +00002331 ResourceLimitError,"MemoryAllocationFailed","`%s'",
2332 "unable to copy profile");
cristyad5b0852011-09-08 02:01:21 +00002333 return((Image *) NULL);
cristye8f8f382011-09-01 13:32:37 +00002334 }
cristy3ed852e2009-09-05 21:47:34 +00002335 SetStringInfoDatum(profile,(const unsigned char *) info);
cristyd15e6592011-10-15 00:13:06 +00002336 (void) SetImageProfile(image,"icc",profile,exception);
cristy3ed852e2009-09-05 21:47:34 +00002337 profile=DestroyStringInfo(profile);
2338 }
2339 }
2340#endif
2341#if defined(PNG_READ_sRGB_SUPPORTED)
2342 {
cristy3ed852e2009-09-05 21:47:34 +00002343 if (mng_info->have_global_srgb)
glennrpcf002022011-01-30 02:38:15 +00002344 image->rendering_intent=Magick_RenderingIntent_from_PNG_RenderingIntent
2345 (mng_info->global_srgb_intent);
glennrp0fe50b42010-11-16 03:52:51 +00002346
cristy3ed852e2009-09-05 21:47:34 +00002347 if (png_get_sRGB(ping,ping_info,&intent))
2348 {
glennrpcf002022011-01-30 02:38:15 +00002349 image->rendering_intent=Magick_RenderingIntent_from_PNG_RenderingIntent
2350 (intent);
glennrp0fe50b42010-11-16 03:52:51 +00002351
cristy3ed852e2009-09-05 21:47:34 +00002352 if (logging != MagickFalse)
2353 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
glennrpe610a072010-08-05 17:08:46 +00002354 " Reading PNG sRGB chunk: rendering_intent: %d",intent);
cristy3ed852e2009-09-05 21:47:34 +00002355 }
2356 }
2357#endif
2358 {
glennrpfaa852b2010-03-30 12:17:00 +00002359 if (!png_get_gAMA(ping,ping_info,&file_gamma))
2360 if (mng_info->have_global_gama)
2361 png_set_gAMA(ping,ping_info,mng_info->global_gamma);
glennrp0fe50b42010-11-16 03:52:51 +00002362
cristy3ed852e2009-09-05 21:47:34 +00002363 if (png_get_gAMA(ping,ping_info,&file_gamma))
2364 {
2365 image->gamma=(float) file_gamma;
2366 if (logging != MagickFalse)
2367 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2368 " Reading PNG gAMA chunk: gamma: %f",file_gamma);
2369 }
2370 }
glennrpfaa852b2010-03-30 12:17:00 +00002371 if (!png_get_valid(ping,ping_info,PNG_INFO_cHRM))
2372 {
2373 if (mng_info->have_global_chrm != MagickFalse)
2374 {
2375 (void) png_set_cHRM(ping,ping_info,
2376 mng_info->global_chrm.white_point.x,
2377 mng_info->global_chrm.white_point.y,
2378 mng_info->global_chrm.red_primary.x,
2379 mng_info->global_chrm.red_primary.y,
2380 mng_info->global_chrm.green_primary.x,
2381 mng_info->global_chrm.green_primary.y,
2382 mng_info->global_chrm.blue_primary.x,
2383 mng_info->global_chrm.blue_primary.y);
2384 }
2385 }
glennrp0fe50b42010-11-16 03:52:51 +00002386
glennrpfaa852b2010-03-30 12:17:00 +00002387 if (png_get_valid(ping,ping_info,PNG_INFO_cHRM))
cristy3ed852e2009-09-05 21:47:34 +00002388 {
2389 (void) png_get_cHRM(ping,ping_info,
2390 &image->chromaticity.white_point.x,
2391 &image->chromaticity.white_point.y,
2392 &image->chromaticity.red_primary.x,
2393 &image->chromaticity.red_primary.y,
2394 &image->chromaticity.green_primary.x,
2395 &image->chromaticity.green_primary.y,
2396 &image->chromaticity.blue_primary.x,
2397 &image->chromaticity.blue_primary.y);
glennrp0fe50b42010-11-16 03:52:51 +00002398
cristy3ed852e2009-09-05 21:47:34 +00002399 if (logging != MagickFalse)
2400 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2401 " Reading PNG cHRM chunk.");
2402 }
glennrp0fe50b42010-11-16 03:52:51 +00002403
glennrpe610a072010-08-05 17:08:46 +00002404 if (image->rendering_intent != UndefinedIntent)
cristy3ed852e2009-09-05 21:47:34 +00002405 {
glennrpe610a072010-08-05 17:08:46 +00002406 png_set_sRGB(ping,ping_info,
glennrpcf002022011-01-30 02:38:15 +00002407 Magick_RenderingIntent_to_PNG_RenderingIntent
2408 (image->rendering_intent));
glennrpfaa852b2010-03-30 12:17:00 +00002409 png_set_gAMA(ping,ping_info,0.45455f);
2410 png_set_cHRM(ping,ping_info,
2411 0.6400f, 0.3300f, 0.3000f, 0.6000f,
2412 0.1500f, 0.0600f, 0.3127f, 0.3290f);
cristy3ed852e2009-09-05 21:47:34 +00002413 }
cristy3ed852e2009-09-05 21:47:34 +00002414#if defined(PNG_oFFs_SUPPORTED)
glennrpfaa852b2010-03-30 12:17:00 +00002415 if (png_get_valid(ping,ping_info,PNG_INFO_oFFs))
cristy3ed852e2009-09-05 21:47:34 +00002416 {
cristy905ef802011-02-23 00:29:18 +00002417 image->page.x=(ssize_t) png_get_x_offset_pixels(ping, ping_info);
2418 image->page.y=(ssize_t) png_get_y_offset_pixels(ping, ping_info);
glennrp0fe50b42010-11-16 03:52:51 +00002419
cristy3ed852e2009-09-05 21:47:34 +00002420 if (logging != MagickFalse)
2421 if (image->page.x || image->page.y)
2422 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00002423 " Reading PNG oFFs chunk: x: %.20g, y: %.20g.",(double)
2424 image->page.x,(double) image->page.y);
cristy3ed852e2009-09-05 21:47:34 +00002425 }
2426#endif
2427#if defined(PNG_pHYs_SUPPORTED)
glennrpfaa852b2010-03-30 12:17:00 +00002428 if (!png_get_valid(ping,ping_info,PNG_INFO_pHYs))
2429 {
2430 if (mng_info->have_global_phys)
2431 {
2432 png_set_pHYs(ping,ping_info,
2433 mng_info->global_x_pixels_per_unit,
2434 mng_info->global_y_pixels_per_unit,
2435 mng_info->global_phys_unit_type);
2436 }
2437 }
2438
2439 if (png_get_valid(ping,ping_info,PNG_INFO_pHYs))
cristy3ed852e2009-09-05 21:47:34 +00002440 {
cristy3ed852e2009-09-05 21:47:34 +00002441 /*
2442 Set image resolution.
2443 */
2444 (void) png_get_pHYs(ping,ping_info,&x_resolution,&y_resolution,
cristy0881b522010-04-24 23:45:19 +00002445 &unit_type);
cristy2a11bef2011-10-28 18:33:11 +00002446 image->resolution.x=(double) x_resolution;
2447 image->resolution.y=(double) y_resolution;
glennrp0fe50b42010-11-16 03:52:51 +00002448
cristy3ed852e2009-09-05 21:47:34 +00002449 if (unit_type == PNG_RESOLUTION_METER)
2450 {
2451 image->units=PixelsPerCentimeterResolution;
cristy2a11bef2011-10-28 18:33:11 +00002452 image->resolution.x=(double) x_resolution/100.0;
2453 image->resolution.y=(double) y_resolution/100.0;
cristy3ed852e2009-09-05 21:47:34 +00002454 }
glennrp0fe50b42010-11-16 03:52:51 +00002455
cristy3ed852e2009-09-05 21:47:34 +00002456 if (logging != MagickFalse)
2457 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00002458 " Reading PNG pHYs chunk: xres: %.20g, yres: %.20g, units: %d.",
2459 (double) x_resolution,(double) y_resolution,unit_type);
cristy3ed852e2009-09-05 21:47:34 +00002460 }
cristy3ed852e2009-09-05 21:47:34 +00002461#endif
glennrp823b55c2011-03-14 18:46:46 +00002462
glennrpfaa852b2010-03-30 12:17:00 +00002463 if (png_get_valid(ping,ping_info,PNG_INFO_PLTE))
cristy3ed852e2009-09-05 21:47:34 +00002464 {
2465 int
2466 number_colors;
2467
2468 png_colorp
2469 palette;
2470
2471 (void) png_get_PLTE(ping,ping_info,&palette,&number_colors);
glennrp0fe50b42010-11-16 03:52:51 +00002472
cristy3ed852e2009-09-05 21:47:34 +00002473 if ((number_colors == 0) &&
glennrpfaa852b2010-03-30 12:17:00 +00002474 ((int) ping_color_type == PNG_COLOR_TYPE_PALETTE))
cristy3ed852e2009-09-05 21:47:34 +00002475 {
2476 if (mng_info->global_plte_length)
2477 {
2478 png_set_PLTE(ping,ping_info,mng_info->global_plte,
2479 (int) mng_info->global_plte_length);
glennrp0fe50b42010-11-16 03:52:51 +00002480
glennrpfaa852b2010-03-30 12:17:00 +00002481 if (!png_get_valid(ping,ping_info,PNG_INFO_tRNS))
cristy3ed852e2009-09-05 21:47:34 +00002482 if (mng_info->global_trns_length)
2483 {
2484 if (mng_info->global_trns_length >
2485 mng_info->global_plte_length)
cristyc82a27b2011-10-21 01:07:16 +00002486 (void) ThrowMagickException(exception,
cristy3ed852e2009-09-05 21:47:34 +00002487 GetMagickModule(),CoderError,
2488 "global tRNS has more entries than global PLTE",
2489 "`%s'",image_info->filename);
2490 png_set_tRNS(ping,ping_info,mng_info->global_trns,
2491 (int) mng_info->global_trns_length,NULL);
2492 }
glennrpbfd9e612011-04-22 14:02:20 +00002493#ifdef PNG_READ_bKGD_SUPPORTED
cristy3ed852e2009-09-05 21:47:34 +00002494 if (
2495#ifndef PNG_READ_EMPTY_PLTE_SUPPORTED
2496 mng_info->have_saved_bkgd_index ||
2497#endif
glennrpfaa852b2010-03-30 12:17:00 +00002498 png_get_valid(ping,ping_info,PNG_INFO_bKGD))
cristy3ed852e2009-09-05 21:47:34 +00002499 {
2500 png_color_16
2501 background;
2502
2503#ifndef PNG_READ_EMPTY_PLTE_SUPPORTED
2504 if (mng_info->have_saved_bkgd_index)
2505 background.index=mng_info->saved_bkgd_index;
cristy3ed852e2009-09-05 21:47:34 +00002506#endif
glennrpfaa852b2010-03-30 12:17:00 +00002507 if (png_get_valid(ping, ping_info, PNG_INFO_bKGD))
2508 background.index=ping_background->index;
glennrp0fe50b42010-11-16 03:52:51 +00002509
cristy3ed852e2009-09-05 21:47:34 +00002510 background.red=(png_uint_16)
2511 mng_info->global_plte[background.index].red;
glennrp0fe50b42010-11-16 03:52:51 +00002512
cristy3ed852e2009-09-05 21:47:34 +00002513 background.green=(png_uint_16)
2514 mng_info->global_plte[background.index].green;
glennrp0fe50b42010-11-16 03:52:51 +00002515
cristy3ed852e2009-09-05 21:47:34 +00002516 background.blue=(png_uint_16)
2517 mng_info->global_plte[background.index].blue;
glennrp0fe50b42010-11-16 03:52:51 +00002518
glennrpc6c391a2011-04-27 02:23:56 +00002519 background.gray=(png_uint_16)
2520 mng_info->global_plte[background.index].green;
2521
cristy3ed852e2009-09-05 21:47:34 +00002522 png_set_bKGD(ping,ping_info,&background);
2523 }
2524#endif
2525 }
2526 else
cristyc82a27b2011-10-21 01:07:16 +00002527 (void) ThrowMagickException(exception,GetMagickModule(),
cristy3ed852e2009-09-05 21:47:34 +00002528 CoderError,"No global PLTE in file","`%s'",
2529 image_info->filename);
2530 }
2531 }
2532
glennrpbfd9e612011-04-22 14:02:20 +00002533#ifdef PNG_READ_bKGD_SUPPORTED
glennrpfaa852b2010-03-30 12:17:00 +00002534 if (mng_info->have_global_bkgd &&
2535 (!png_get_valid(ping,ping_info,PNG_INFO_bKGD)))
cristy3ed852e2009-09-05 21:47:34 +00002536 image->background_color=mng_info->mng_global_bkgd;
glennrp0fe50b42010-11-16 03:52:51 +00002537
glennrpfaa852b2010-03-30 12:17:00 +00002538 if (png_get_valid(ping,ping_info,PNG_INFO_bKGD))
cristy3ed852e2009-09-05 21:47:34 +00002539 {
glennrpbfd9e612011-04-22 14:02:20 +00002540 unsigned int
2541 bkgd_scale;
2542
cristy3ed852e2009-09-05 21:47:34 +00002543 /*
2544 Set image background color.
2545 */
2546 if (logging != MagickFalse)
2547 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2548 " Reading PNG bKGD chunk.");
glennrp0fe50b42010-11-16 03:52:51 +00002549
glennrpbfd9e612011-04-22 14:02:20 +00002550 /* Scale background components to 16-bit, then scale
2551 * to quantum depth
2552 */
2553 if (logging != MagickFalse)
2554 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2555 " raw ping_background=(%d,%d,%d).",ping_background->red,
2556 ping_background->green,ping_background->blue);
glennrp0fe50b42010-11-16 03:52:51 +00002557
glennrpbfd9e612011-04-22 14:02:20 +00002558 bkgd_scale = 1;
glennrp2cbb4482010-06-02 04:37:24 +00002559
glennrpbfd9e612011-04-22 14:02:20 +00002560 if (ping_bit_depth == 1)
2561 bkgd_scale = 255;
glennrp2cbb4482010-06-02 04:37:24 +00002562
glennrpbfd9e612011-04-22 14:02:20 +00002563 else if (ping_bit_depth == 2)
2564 bkgd_scale = 85;
glennrp0fe50b42010-11-16 03:52:51 +00002565
glennrpbfd9e612011-04-22 14:02:20 +00002566 else if (ping_bit_depth == 4)
2567 bkgd_scale = 17;
glennrp0fe50b42010-11-16 03:52:51 +00002568
glennrpbfd9e612011-04-22 14:02:20 +00002569 if (ping_bit_depth <= 8)
2570 bkgd_scale *= 257;
glennrp0fe50b42010-11-16 03:52:51 +00002571
glennrpbfd9e612011-04-22 14:02:20 +00002572 ping_background->red *= bkgd_scale;
2573 ping_background->green *= bkgd_scale;
2574 ping_background->blue *= bkgd_scale;
glennrp0fe50b42010-11-16 03:52:51 +00002575
glennrpbfd9e612011-04-22 14:02:20 +00002576 if (logging != MagickFalse)
2577 {
glennrp2cbb4482010-06-02 04:37:24 +00002578 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2579 " bkgd_scale=%d.",bkgd_scale);
glennrp0fe50b42010-11-16 03:52:51 +00002580
glennrp2cbb4482010-06-02 04:37:24 +00002581 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2582 " ping_background=(%d,%d,%d).",ping_background->red,
2583 ping_background->green,ping_background->blue);
glennrpbfd9e612011-04-22 14:02:20 +00002584 }
glennrp2cbb4482010-06-02 04:37:24 +00002585
glennrpbfd9e612011-04-22 14:02:20 +00002586 image->background_color.red=
glennrpfaa852b2010-03-30 12:17:00 +00002587 ScaleShortToQuantum(ping_background->red);
glennrp0fe50b42010-11-16 03:52:51 +00002588
glennrpbfd9e612011-04-22 14:02:20 +00002589 image->background_color.green=
glennrpfaa852b2010-03-30 12:17:00 +00002590 ScaleShortToQuantum(ping_background->green);
glennrp0fe50b42010-11-16 03:52:51 +00002591
glennrpbfd9e612011-04-22 14:02:20 +00002592 image->background_color.blue=
2593 ScaleShortToQuantum(ping_background->blue);
glennrp0fe50b42010-11-16 03:52:51 +00002594
cristy4c08aed2011-07-01 19:47:50 +00002595 image->background_color.alpha=OpaqueAlpha;
glennrp2cbb4482010-06-02 04:37:24 +00002596
glennrpbfd9e612011-04-22 14:02:20 +00002597 if (logging != MagickFalse)
2598 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2599 " image->background_color=(%.20g,%.20g,%.20g).",
2600 (double) image->background_color.red,
2601 (double) image->background_color.green,
2602 (double) image->background_color.blue);
cristy3ed852e2009-09-05 21:47:34 +00002603 }
glennrpbfd9e612011-04-22 14:02:20 +00002604#endif /* PNG_READ_bKGD_SUPPORTED */
glennrpa6a06632011-01-19 15:15:34 +00002605
glennrpfaa852b2010-03-30 12:17:00 +00002606 if (png_get_valid(ping,ping_info,PNG_INFO_tRNS))
cristy3ed852e2009-09-05 21:47:34 +00002607 {
2608 /*
glennrpa6a06632011-01-19 15:15:34 +00002609 Image has a tRNS chunk.
cristy3ed852e2009-09-05 21:47:34 +00002610 */
2611 int
2612 max_sample;
2613
cristy35ef8242010-06-03 16:24:13 +00002614 size_t
2615 one=1;
2616
cristy3ed852e2009-09-05 21:47:34 +00002617 if (logging != MagickFalse)
2618 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2619 " Reading PNG tRNS chunk.");
2620
cristyf9cca6a2010-06-04 23:49:28 +00002621 max_sample = (int) ((one << ping_bit_depth) - 1);
cristy3ed852e2009-09-05 21:47:34 +00002622
glennrpfaa852b2010-03-30 12:17:00 +00002623 if ((ping_color_type == PNG_COLOR_TYPE_GRAY &&
2624 (int)ping_trans_color->gray > max_sample) ||
2625 (ping_color_type == PNG_COLOR_TYPE_RGB &&
2626 ((int)ping_trans_color->red > max_sample ||
2627 (int)ping_trans_color->green > max_sample ||
2628 (int)ping_trans_color->blue > max_sample)))
cristy3ed852e2009-09-05 21:47:34 +00002629 {
2630 if (logging != MagickFalse)
2631 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2632 " Ignoring PNG tRNS chunk with out-of-range sample.");
cristy3ed852e2009-09-05 21:47:34 +00002633 png_free_data(ping, ping_info, PNG_FREE_TRNS, 0);
glennrpfaa852b2010-03-30 12:17:00 +00002634 png_set_invalid(ping,ping_info,PNG_INFO_tRNS);
cristy3ed852e2009-09-05 21:47:34 +00002635 image->matte=MagickFalse;
2636 }
2637 else
2638 {
glennrpa6a06632011-01-19 15:15:34 +00002639 int
2640 scale_to_short;
2641
2642 scale_to_short = 65535L/((1UL << ping_bit_depth)-1);
2643
2644 /* Scale transparent_color to short */
2645 transparent_color.red= scale_to_short*ping_trans_color->red;
2646 transparent_color.green= scale_to_short*ping_trans_color->green;
2647 transparent_color.blue= scale_to_short*ping_trans_color->blue;
cristy4c08aed2011-07-01 19:47:50 +00002648 transparent_color.alpha= scale_to_short*ping_trans_color->gray;
glennrp05eb4a92010-07-08 02:21:09 +00002649
glennrpfaa852b2010-03-30 12:17:00 +00002650 if (ping_color_type == PNG_COLOR_TYPE_GRAY)
cristy3ed852e2009-09-05 21:47:34 +00002651 {
glennrp0f111982010-07-07 20:18:33 +00002652 if (logging != MagickFalse)
2653 {
2654 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2655 " Raw tRNS graylevel is %d.",ping_trans_color->gray);
glennrp0fe50b42010-11-16 03:52:51 +00002656
glennrp0f111982010-07-07 20:18:33 +00002657 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristy101ab702011-10-13 13:06:32 +00002658 " scaled graylevel is %.20g.",transparent_color.alpha);
glennrp0f111982010-07-07 20:18:33 +00002659 }
cristy4c08aed2011-07-01 19:47:50 +00002660 transparent_color.red=transparent_color.alpha;
2661 transparent_color.green=transparent_color.alpha;
2662 transparent_color.blue=transparent_color.alpha;
cristy3ed852e2009-09-05 21:47:34 +00002663 }
2664 }
2665 }
2666#if defined(PNG_READ_sBIT_SUPPORTED)
2667 if (mng_info->have_global_sbit)
2668 {
glennrpfaa852b2010-03-30 12:17:00 +00002669 if (!png_get_valid(ping,ping_info,PNG_INFO_sBIT))
cristy3ed852e2009-09-05 21:47:34 +00002670 png_set_sBIT(ping,ping_info,&mng_info->global_sbit);
2671 }
2672#endif
2673 num_passes=png_set_interlace_handling(ping);
glennrpfaa852b2010-03-30 12:17:00 +00002674
cristy3ed852e2009-09-05 21:47:34 +00002675 png_read_update_info(ping,ping_info);
glennrpfaa852b2010-03-30 12:17:00 +00002676
2677 ping_rowbytes=png_get_rowbytes(ping,ping_info);
2678
cristy3ed852e2009-09-05 21:47:34 +00002679 /*
2680 Initialize image structure.
2681 */
2682 mng_info->image_box.left=0;
cristybb503372010-05-27 20:51:26 +00002683 mng_info->image_box.right=(ssize_t) ping_width;
cristy3ed852e2009-09-05 21:47:34 +00002684 mng_info->image_box.top=0;
cristybb503372010-05-27 20:51:26 +00002685 mng_info->image_box.bottom=(ssize_t) ping_height;
cristy3ed852e2009-09-05 21:47:34 +00002686 if (mng_info->mng_type == 0)
2687 {
glennrpfaa852b2010-03-30 12:17:00 +00002688 mng_info->mng_width=ping_width;
2689 mng_info->mng_height=ping_height;
cristy3ed852e2009-09-05 21:47:34 +00002690 mng_info->frame=mng_info->image_box;
2691 mng_info->clip=mng_info->image_box;
2692 }
glennrp0fe50b42010-11-16 03:52:51 +00002693
cristy3ed852e2009-09-05 21:47:34 +00002694 else
2695 {
2696 image->page.y=mng_info->y_off[mng_info->object_id];
2697 }
glennrp0fe50b42010-11-16 03:52:51 +00002698
cristy3ed852e2009-09-05 21:47:34 +00002699 image->compression=ZipCompression;
glennrpfaa852b2010-03-30 12:17:00 +00002700 image->columns=ping_width;
2701 image->rows=ping_height;
2702 if (((int) ping_color_type == PNG_COLOR_TYPE_PALETTE) ||
glennrpfaa852b2010-03-30 12:17:00 +00002703 ((int) ping_color_type == PNG_COLOR_TYPE_GRAY))
cristy3ed852e2009-09-05 21:47:34 +00002704 {
cristybefe4d22010-06-07 01:18:58 +00002705 size_t
2706 one;
2707
cristy3ed852e2009-09-05 21:47:34 +00002708 image->storage_class=PseudoClass;
cristybefe4d22010-06-07 01:18:58 +00002709 one=1;
2710 image->colors=one << ping_bit_depth;
cristy3ed852e2009-09-05 21:47:34 +00002711#if (MAGICKCORE_QUANTUM_DEPTH == 8)
2712 if (image->colors > 256)
glennrp67b9c1a2011-04-22 18:47:36 +00002713 image->colors=256;
2714#else
2715 if (image->colors > 65536L)
2716 image->colors=65536L;
cristy3ed852e2009-09-05 21:47:34 +00002717#endif
glennrpfaa852b2010-03-30 12:17:00 +00002718 if ((int) ping_color_type == PNG_COLOR_TYPE_PALETTE)
cristy3ed852e2009-09-05 21:47:34 +00002719 {
2720 int
2721 number_colors;
2722
2723 png_colorp
2724 palette;
2725
2726 (void) png_get_PLTE(ping,ping_info,&palette,&number_colors);
cristybb503372010-05-27 20:51:26 +00002727 image->colors=(size_t) number_colors;
glennrp0fe50b42010-11-16 03:52:51 +00002728
cristy3ed852e2009-09-05 21:47:34 +00002729 if (logging != MagickFalse)
2730 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2731 " Reading PNG PLTE chunk: number_colors: %d.",number_colors);
2732 }
2733 }
2734
2735 if (image->storage_class == PseudoClass)
2736 {
2737 /*
2738 Initialize image colormap.
2739 */
cristy018f07f2011-09-04 21:15:19 +00002740 if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00002741 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
glennrp0fe50b42010-11-16 03:52:51 +00002742
glennrpfaa852b2010-03-30 12:17:00 +00002743 if ((int) ping_color_type == PNG_COLOR_TYPE_PALETTE)
cristy3ed852e2009-09-05 21:47:34 +00002744 {
2745 int
2746 number_colors;
2747
2748 png_colorp
2749 palette;
2750
2751 (void) png_get_PLTE(ping,ping_info,&palette,&number_colors);
glennrp0fe50b42010-11-16 03:52:51 +00002752
glennrp6af6cf12011-04-22 13:05:16 +00002753 for (i=0; i < (ssize_t) number_colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00002754 {
2755 image->colormap[i].red=ScaleCharToQuantum(palette[i].red);
2756 image->colormap[i].green=ScaleCharToQuantum(palette[i].green);
2757 image->colormap[i].blue=ScaleCharToQuantum(palette[i].blue);
2758 }
glennrp6af6cf12011-04-22 13:05:16 +00002759
glennrp67b9c1a2011-04-22 18:47:36 +00002760 for ( ; i < (ssize_t) image->colors; i++)
glennrp6af6cf12011-04-22 13:05:16 +00002761 {
2762 image->colormap[i].red=0;
2763 image->colormap[i].green=0;
2764 image->colormap[i].blue=0;
2765 }
cristy3ed852e2009-09-05 21:47:34 +00002766 }
glennrp0fe50b42010-11-16 03:52:51 +00002767
cristy3ed852e2009-09-05 21:47:34 +00002768 else
2769 {
cristybb503372010-05-27 20:51:26 +00002770 size_t
cristy3ed852e2009-09-05 21:47:34 +00002771 scale;
2772
glennrpfaa852b2010-03-30 12:17:00 +00002773 scale=(QuantumRange/((1UL << ping_bit_depth)-1));
glennrp0fe50b42010-11-16 03:52:51 +00002774
cristy3ed852e2009-09-05 21:47:34 +00002775 if (scale < 1)
2776 scale=1;
glennrp0fe50b42010-11-16 03:52:51 +00002777
cristybb503372010-05-27 20:51:26 +00002778 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00002779 {
2780 image->colormap[i].red=(Quantum) (i*scale);
2781 image->colormap[i].green=(Quantum) (i*scale);
2782 image->colormap[i].blue=(Quantum) (i*scale);
2783 }
2784 }
2785 }
glennrp147bc912011-03-30 18:47:21 +00002786
glennrpcb395ac2011-03-30 19:50:23 +00002787 /* Set some properties for reporting by "identify" */
2788 {
glennrp147bc912011-03-30 18:47:21 +00002789 char
2790 msg[MaxTextExtent];
2791
2792 /* encode ping_width, ping_height, ping_bit_depth, ping_color_type,
2793 ping_interlace_method in value */
2794
cristy3b6fd2e2011-05-20 12:53:50 +00002795 (void) FormatLocaleString(msg,MaxTextExtent,
glennrp7cdb11c2011-03-31 18:17:25 +00002796 "%d, %d",(int) ping_width, (int) ping_height);
cristy5d6fc9c2011-12-27 03:10:42 +00002797 (void) SetImageProperty(image,"png:IHDR.width,height ",msg,exception);
glennrp147bc912011-03-30 18:47:21 +00002798
cristy3b6fd2e2011-05-20 12:53:50 +00002799 (void) FormatLocaleString(msg,MaxTextExtent,"%d",(int) ping_bit_depth);
cristy5d6fc9c2011-12-27 03:10:42 +00002800 (void) SetImageProperty(image,"png:IHDR.bit_depth ",msg,exception);
glennrp147bc912011-03-30 18:47:21 +00002801
cristy3b6fd2e2011-05-20 12:53:50 +00002802 (void) FormatLocaleString(msg,MaxTextExtent,"%d",(int) ping_color_type);
cristy5d6fc9c2011-12-27 03:10:42 +00002803 (void) SetImageProperty(image,"png:IHDR.color_type ",msg,exception);
glennrp147bc912011-03-30 18:47:21 +00002804
cristy3b6fd2e2011-05-20 12:53:50 +00002805 (void) FormatLocaleString(msg,MaxTextExtent,"%d",
glennrp147bc912011-03-30 18:47:21 +00002806 (int) ping_interlace_method);
cristy5d6fc9c2011-12-27 03:10:42 +00002807 (void) SetImageProperty(image,"png:IHDR.interlace_method",msg,exception);
glennrpcb395ac2011-03-30 19:50:23 +00002808 }
glennrp147bc912011-03-30 18:47:21 +00002809
cristy3ed852e2009-09-05 21:47:34 +00002810 /*
2811 Read image scanlines.
2812 */
2813 if (image->delay != 0)
2814 mng_info->scenes_found++;
glennrp0fe50b42010-11-16 03:52:51 +00002815
glennrp0ca69b12010-07-26 01:57:52 +00002816 if ((mng_info->mng_type == 0 && (image->ping != MagickFalse)) || (
glennrp347e40f2010-06-06 11:27:30 +00002817 (image_info->number_scenes != 0) && (mng_info->scenes_found > (ssize_t)
2818 (image_info->first_scene+image_info->number_scenes))))
cristy3ed852e2009-09-05 21:47:34 +00002819 {
2820 if (logging != MagickFalse)
2821 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00002822 " Skipping PNG image data for scene %.20g",(double)
cristy3ed852e2009-09-05 21:47:34 +00002823 mng_info->scenes_found-1);
2824 png_destroy_read_struct(&ping,&ping_info,&end_info);
2825#if defined(PNG_SETJMP_NOT_THREAD_SAFE)
glennrpcf002022011-01-30 02:38:15 +00002826 UnlockSemaphoreInfo(ping_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00002827#endif
2828 if (logging != MagickFalse)
2829 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2830 " exit ReadOnePNGImage().");
glennrp0fe50b42010-11-16 03:52:51 +00002831
cristy3ed852e2009-09-05 21:47:34 +00002832 return(image);
2833 }
glennrp0fe50b42010-11-16 03:52:51 +00002834
cristy3ed852e2009-09-05 21:47:34 +00002835 if (logging != MagickFalse)
2836 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2837 " Reading PNG IDAT chunk(s)");
glennrp0fe50b42010-11-16 03:52:51 +00002838
cristy3ed852e2009-09-05 21:47:34 +00002839 if (num_passes > 1)
glennrpcf002022011-01-30 02:38:15 +00002840 ping_pixels=(unsigned char *) AcquireQuantumMemory(image->rows,
2841 ping_rowbytes*sizeof(*ping_pixels));
glennrp0fe50b42010-11-16 03:52:51 +00002842
cristy3ed852e2009-09-05 21:47:34 +00002843 else
glennrpcf002022011-01-30 02:38:15 +00002844 ping_pixels=(unsigned char *) AcquireQuantumMemory(ping_rowbytes,
2845 sizeof(*ping_pixels));
glennrp0fe50b42010-11-16 03:52:51 +00002846
glennrpcf002022011-01-30 02:38:15 +00002847 if (ping_pixels == (unsigned char *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002848 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
glennrp0fe50b42010-11-16 03:52:51 +00002849
cristy3ed852e2009-09-05 21:47:34 +00002850 if (logging != MagickFalse)
2851 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2852 " Converting PNG pixels to pixel packets");
2853 /*
2854 Convert PNG pixels to pixel packets.
2855 */
glennrpfaa852b2010-03-30 12:17:00 +00002856 if (setjmp(png_jmpbuf(ping)))
cristy3ed852e2009-09-05 21:47:34 +00002857 {
2858 /*
2859 PNG image is corrupt.
2860 */
2861 png_destroy_read_struct(&ping,&ping_info,&end_info);
2862#if defined(PNG_SETJMP_NOT_THREAD_SAFE)
glennrpcf002022011-01-30 02:38:15 +00002863 UnlockSemaphoreInfo(ping_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00002864#endif
2865 if (quantum_info != (QuantumInfo *) NULL)
2866 quantum_info = DestroyQuantumInfo(quantum_info);
glennrp0fe50b42010-11-16 03:52:51 +00002867
glennrpcf002022011-01-30 02:38:15 +00002868 if (ping_pixels != (unsigned char *) NULL)
2869 ping_pixels=(unsigned char *) RelinquishMagickMemory(ping_pixels);
glennrp0fe50b42010-11-16 03:52:51 +00002870
cristy3ed852e2009-09-05 21:47:34 +00002871 if (logging != MagickFalse)
2872 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2873 " exit ReadOnePNGImage() with error.");
glennrp0fe50b42010-11-16 03:52:51 +00002874
cristy3ed852e2009-09-05 21:47:34 +00002875 if (image != (Image *) NULL)
cristy7b755eb2009-12-05 14:51:29 +00002876 {
cristyc82a27b2011-10-21 01:07:16 +00002877 InheritException(exception,exception);
cristy7b755eb2009-12-05 14:51:29 +00002878 image->columns=0;
2879 }
glennrp0fe50b42010-11-16 03:52:51 +00002880
cristy3ed852e2009-09-05 21:47:34 +00002881 return(GetFirstImageInList(image));
2882 }
glennrp0fe50b42010-11-16 03:52:51 +00002883
cristyed552522009-10-16 14:04:35 +00002884 quantum_info=AcquireQuantumInfo(image_info,image);
glennrp0fe50b42010-11-16 03:52:51 +00002885
cristyed552522009-10-16 14:04:35 +00002886 if (quantum_info == (QuantumInfo *) NULL)
2887 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
glennrp0fe50b42010-11-16 03:52:51 +00002888
glennrpc8cbc5d2011-01-01 00:12:34 +00002889 {
2890
2891 MagickBooleanType
2892 found_transparent_pixel;
2893
2894 found_transparent_pixel=MagickFalse;
2895
cristy3ed852e2009-09-05 21:47:34 +00002896 if (image->storage_class == DirectClass)
cristy3ed852e2009-09-05 21:47:34 +00002897 {
glennrpc8cbc5d2011-01-01 00:12:34 +00002898 for (pass=0; pass < num_passes; pass++)
cristy3ed852e2009-09-05 21:47:34 +00002899 {
glennrpc8cbc5d2011-01-01 00:12:34 +00002900 /*
2901 Convert image to DirectClass pixel packets.
2902 */
glennrp67b9c1a2011-04-22 18:47:36 +00002903#if (MAGICKCORE_QUANTUM_DEPTH == 8)
2904 int
2905 depth;
2906
2907 depth=(ssize_t) ping_bit_depth;
2908#endif
glennrpc8cbc5d2011-01-01 00:12:34 +00002909 image->matte=(((int) ping_color_type == PNG_COLOR_TYPE_RGB_ALPHA) ||
2910 ((int) ping_color_type == PNG_COLOR_TYPE_GRAY_ALPHA) ||
2911 (png_get_valid(ping,ping_info,PNG_INFO_tRNS))) ?
2912 MagickTrue : MagickFalse;
glennrp0fe50b42010-11-16 03:52:51 +00002913
glennrpc8cbc5d2011-01-01 00:12:34 +00002914 for (y=0; y < (ssize_t) image->rows; y++)
2915 {
2916 if (num_passes > 1)
2917 row_offset=ping_rowbytes*y;
2918
2919 else
2920 row_offset=0;
2921
glennrpcf002022011-01-30 02:38:15 +00002922 png_read_row(ping,ping_pixels+row_offset,NULL);
glennrpc8cbc5d2011-01-01 00:12:34 +00002923 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
2924
cristyacd2ed22011-08-30 01:44:23 +00002925 if (q == (Quantum *) NULL)
glennrpc8cbc5d2011-01-01 00:12:34 +00002926 break;
2927
glennrpc8cbc5d2011-01-01 00:12:34 +00002928 if ((int) ping_color_type == PNG_COLOR_TYPE_GRAY)
2929 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
glennrpcf002022011-01-30 02:38:15 +00002930 GrayQuantum,ping_pixels+row_offset,exception);
glennrpc8cbc5d2011-01-01 00:12:34 +00002931
2932 else if ((int) ping_color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
2933 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
glennrpcf002022011-01-30 02:38:15 +00002934 GrayAlphaQuantum,ping_pixels+row_offset,exception);
glennrpc8cbc5d2011-01-01 00:12:34 +00002935
2936 else if ((int) ping_color_type == PNG_COLOR_TYPE_RGB_ALPHA)
2937 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
glennrpcf002022011-01-30 02:38:15 +00002938 RGBAQuantum,ping_pixels+row_offset,exception);
glennrpc8cbc5d2011-01-01 00:12:34 +00002939
2940 else if ((int) ping_color_type == PNG_COLOR_TYPE_PALETTE)
2941 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
glennrpcf002022011-01-30 02:38:15 +00002942 IndexQuantum,ping_pixels+row_offset,exception);
glennrpc8cbc5d2011-01-01 00:12:34 +00002943
2944 else /* ping_color_type == PNG_COLOR_TYPE_RGB */
2945 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
glennrpcf002022011-01-30 02:38:15 +00002946 RGBQuantum,ping_pixels+row_offset,exception);
glennrp3faa9a32011-04-23 14:00:25 +00002947
glennrpc8cbc5d2011-01-01 00:12:34 +00002948 if (found_transparent_pixel == MagickFalse)
2949 {
2950 /* Is there a transparent pixel in the row? */
glennrpa6a06632011-01-19 15:15:34 +00002951 if (y== 0 && logging != MagickFalse)
2952 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2953 " Looking for cheap transparent pixel");
2954
glennrpc8cbc5d2011-01-01 00:12:34 +00002955 for (x=(ssize_t) image->columns-1; x >= 0; x--)
2956 {
glennrp5aa37f62011-01-02 03:07:57 +00002957 if ((ping_color_type == PNG_COLOR_TYPE_RGBA ||
2958 ping_color_type == PNG_COLOR_TYPE_GRAY_ALPHA) &&
cristy4c08aed2011-07-01 19:47:50 +00002959 (GetPixelAlpha(image,q) != OpaqueAlpha))
glennrpc8cbc5d2011-01-01 00:12:34 +00002960 {
glennrpa6a06632011-01-19 15:15:34 +00002961 if (logging != MagickFalse)
2962 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2963 " ...got one.");
2964
glennrpc8cbc5d2011-01-01 00:12:34 +00002965 found_transparent_pixel = MagickTrue;
2966 break;
2967 }
glennrp4f25bd02011-01-01 18:51:28 +00002968 if ((ping_color_type == PNG_COLOR_TYPE_RGB ||
2969 ping_color_type == PNG_COLOR_TYPE_GRAY) &&
glennrp847370c2011-07-05 17:37:15 +00002970 (ScaleQuantumToShort(GetPixelRed(image,q)) ==
2971 transparent_color.red &&
2972 ScaleQuantumToShort(GetPixelGreen(image,q)) ==
2973 transparent_color.green &&
2974 ScaleQuantumToShort(GetPixelBlue(image,q)) ==
2975 transparent_color.blue))
glennrp4f25bd02011-01-01 18:51:28 +00002976 {
glennrpa6a06632011-01-19 15:15:34 +00002977 if (logging != MagickFalse)
2978 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2979 " ...got one.");
glennrp4f25bd02011-01-01 18:51:28 +00002980 found_transparent_pixel = MagickTrue;
2981 break;
2982 }
cristyed231572011-07-14 02:18:59 +00002983 q+=GetPixelChannels(image);
glennrpc8cbc5d2011-01-01 00:12:34 +00002984 }
2985 }
2986
2987 if ((image->previous == (Image *) NULL) && (num_passes == 1))
2988 {
2989 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
2990 image->rows);
2991
2992 if (status == MagickFalse)
2993 break;
2994 }
2995 if (SyncAuthenticPixels(image,exception) == MagickFalse)
2996 break;
2997 }
2998
2999 if ((image->previous == (Image *) NULL) && (num_passes != 1))
3000 {
3001 status=SetImageProgress(image,LoadImageTag,pass,num_passes);
cristy7a287bf2010-02-14 02:18:09 +00003002 if (status == MagickFalse)
3003 break;
3004 }
cristy3ed852e2009-09-05 21:47:34 +00003005 }
cristy3ed852e2009-09-05 21:47:34 +00003006 }
glennrp0fe50b42010-11-16 03:52:51 +00003007
cristy3ed852e2009-09-05 21:47:34 +00003008 else /* image->storage_class != DirectClass */
glennrpc8cbc5d2011-01-01 00:12:34 +00003009
cristy3ed852e2009-09-05 21:47:34 +00003010 for (pass=0; pass < num_passes; pass++)
3011 {
3012 Quantum
3013 *quantum_scanline;
3014
3015 register Quantum
3016 *r;
3017
3018 /*
3019 Convert grayscale image to PseudoClass pixel packets.
3020 */
glennrpc17d96f2011-06-27 01:20:11 +00003021 if (logging != MagickFalse)
3022 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
3023 " Converting grayscale pixels to pixel packets");
cristy4c08aed2011-07-01 19:47:50 +00003024
glennrpfaa852b2010-03-30 12:17:00 +00003025 image->matte=ping_color_type == PNG_COLOR_TYPE_GRAY_ALPHA ?
cristy3ed852e2009-09-05 21:47:34 +00003026 MagickTrue : MagickFalse;
glennrp0fe50b42010-11-16 03:52:51 +00003027
cristy3ed852e2009-09-05 21:47:34 +00003028 quantum_scanline=(Quantum *) AcquireQuantumMemory(image->columns,
3029 (image->matte ? 2 : 1)*sizeof(*quantum_scanline));
glennrp0fe50b42010-11-16 03:52:51 +00003030
cristy3ed852e2009-09-05 21:47:34 +00003031 if (quantum_scanline == (Quantum *) NULL)
3032 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
glennrp0fe50b42010-11-16 03:52:51 +00003033
cristybb503372010-05-27 20:51:26 +00003034 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003035 {
3036 if (num_passes > 1)
glennrpfaa852b2010-03-30 12:17:00 +00003037 row_offset=ping_rowbytes*y;
glennrpc8cbc5d2011-01-01 00:12:34 +00003038
cristy3ed852e2009-09-05 21:47:34 +00003039 else
3040 row_offset=0;
glennrpc8cbc5d2011-01-01 00:12:34 +00003041
glennrpcf002022011-01-30 02:38:15 +00003042 png_read_row(ping,ping_pixels+row_offset,NULL);
cristy3ed852e2009-09-05 21:47:34 +00003043 q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
glennrp0fe50b42010-11-16 03:52:51 +00003044
cristyacd2ed22011-08-30 01:44:23 +00003045 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003046 break;
glennrp0fe50b42010-11-16 03:52:51 +00003047
glennrpcf002022011-01-30 02:38:15 +00003048 p=ping_pixels+row_offset;
cristy3ed852e2009-09-05 21:47:34 +00003049 r=quantum_scanline;
glennrpc8cbc5d2011-01-01 00:12:34 +00003050
glennrpfaa852b2010-03-30 12:17:00 +00003051 switch (ping_bit_depth)
cristy3ed852e2009-09-05 21:47:34 +00003052 {
3053 case 1:
3054 {
cristybb503372010-05-27 20:51:26 +00003055 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003056 bit;
3057
cristybb503372010-05-27 20:51:26 +00003058 for (x=(ssize_t) image->columns-7; x > 0; x-=8)
cristy3ed852e2009-09-05 21:47:34 +00003059 {
3060 for (bit=7; bit >= 0; bit--)
glennrpa18d5bc2011-04-23 14:51:34 +00003061 *r++=(Quantum) ((*p) & (0x01 << bit) ? 0x01 : 0x00);
cristy3ed852e2009-09-05 21:47:34 +00003062 p++;
3063 }
glennrp0fe50b42010-11-16 03:52:51 +00003064
cristy3ed852e2009-09-05 21:47:34 +00003065 if ((image->columns % 8) != 0)
3066 {
cristybb503372010-05-27 20:51:26 +00003067 for (bit=7; bit >= (ssize_t) (8-(image->columns % 8)); bit--)
glennrpa18d5bc2011-04-23 14:51:34 +00003068 *r++=(Quantum) ((*p) & (0x01 << bit) ? 0x01 : 0x00);
cristy3ed852e2009-09-05 21:47:34 +00003069 }
glennrp0fe50b42010-11-16 03:52:51 +00003070
cristy3ed852e2009-09-05 21:47:34 +00003071 break;
3072 }
glennrp47b9dd52010-11-24 18:12:06 +00003073
cristy3ed852e2009-09-05 21:47:34 +00003074 case 2:
3075 {
cristybb503372010-05-27 20:51:26 +00003076 for (x=(ssize_t) image->columns-3; x > 0; x-=4)
cristy3ed852e2009-09-05 21:47:34 +00003077 {
glennrpa18d5bc2011-04-23 14:51:34 +00003078 *r++=(*p >> 6) & 0x03;
3079 *r++=(*p >> 4) & 0x03;
3080 *r++=(*p >> 2) & 0x03;
3081 *r++=(*p++) & 0x03;
cristy3ed852e2009-09-05 21:47:34 +00003082 }
glennrp0fe50b42010-11-16 03:52:51 +00003083
cristy3ed852e2009-09-05 21:47:34 +00003084 if ((image->columns % 4) != 0)
3085 {
cristybb503372010-05-27 20:51:26 +00003086 for (i=3; i >= (ssize_t) (4-(image->columns % 4)); i--)
glennrpa18d5bc2011-04-23 14:51:34 +00003087 *r++=(Quantum) ((*p >> (i*2)) & 0x03);
cristy3ed852e2009-09-05 21:47:34 +00003088 }
glennrp0fe50b42010-11-16 03:52:51 +00003089
cristy3ed852e2009-09-05 21:47:34 +00003090 break;
3091 }
glennrp47b9dd52010-11-24 18:12:06 +00003092
cristy3ed852e2009-09-05 21:47:34 +00003093 case 4:
3094 {
cristybb503372010-05-27 20:51:26 +00003095 for (x=(ssize_t) image->columns-1; x > 0; x-=2)
cristy3ed852e2009-09-05 21:47:34 +00003096 {
glennrpa18d5bc2011-04-23 14:51:34 +00003097 *r++=(*p >> 4) & 0x0f;
3098 *r++=(*p++) & 0x0f;
cristy3ed852e2009-09-05 21:47:34 +00003099 }
glennrp0fe50b42010-11-16 03:52:51 +00003100
cristy3ed852e2009-09-05 21:47:34 +00003101 if ((image->columns % 2) != 0)
glennrpa18d5bc2011-04-23 14:51:34 +00003102 *r++=(*p++ >> 4) & 0x0f;
glennrp0fe50b42010-11-16 03:52:51 +00003103
cristy3ed852e2009-09-05 21:47:34 +00003104 break;
3105 }
glennrp47b9dd52010-11-24 18:12:06 +00003106
cristy3ed852e2009-09-05 21:47:34 +00003107 case 8:
3108 {
glennrpfaa852b2010-03-30 12:17:00 +00003109 if (ping_color_type == 4)
cristybb503372010-05-27 20:51:26 +00003110 for (x=(ssize_t) image->columns-1; x >= 0; x--)
cristy3ed852e2009-09-05 21:47:34 +00003111 {
glennrpa18d5bc2011-04-23 14:51:34 +00003112 *r++=*p++;
cristy4c08aed2011-07-01 19:47:50 +00003113 SetPixelAlpha(image,ScaleCharToQuantum((unsigned char) *p++),q);
3114 if (GetPixelAlpha(image,q) != OpaqueAlpha)
glennrp0b206f52011-01-07 04:55:32 +00003115 found_transparent_pixel = MagickTrue;
cristyed231572011-07-14 02:18:59 +00003116 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00003117 }
glennrp0fe50b42010-11-16 03:52:51 +00003118
cristy3ed852e2009-09-05 21:47:34 +00003119 else
cristybb503372010-05-27 20:51:26 +00003120 for (x=(ssize_t) image->columns-1; x >= 0; x--)
glennrpa18d5bc2011-04-23 14:51:34 +00003121 *r++=*p++;
glennrp0fe50b42010-11-16 03:52:51 +00003122
cristy3ed852e2009-09-05 21:47:34 +00003123 break;
3124 }
glennrp47b9dd52010-11-24 18:12:06 +00003125
cristy3ed852e2009-09-05 21:47:34 +00003126 case 16:
3127 {
cristybb503372010-05-27 20:51:26 +00003128 for (x=(ssize_t) image->columns-1; x >= 0; x--)
glennrpa18d5bc2011-04-23 14:51:34 +00003129 {
glennrpc17d96f2011-06-27 01:20:11 +00003130#if (MAGICKCORE_QUANTUM_DEPTH == 16) || (MAGICKCORE_QUANTUM_DEPTH == 32)
glennrp58f77c72011-04-23 14:09:09 +00003131 size_t
3132 quantum;
3133
3134 if (image->colors > 256)
glennrpc17d96f2011-06-27 01:20:11 +00003135 quantum=((*p++) << 8);
glennrp58f77c72011-04-23 14:09:09 +00003136
3137 else
glennrpc17d96f2011-06-27 01:20:11 +00003138 quantum=0;
glennrp58f77c72011-04-23 14:09:09 +00003139
glennrp58f77c72011-04-23 14:09:09 +00003140 quantum|=(*p++);
glennrpc17d96f2011-06-27 01:20:11 +00003141 *r=ScaleShortToQuantum(quantum);
glennrp58f77c72011-04-23 14:09:09 +00003142 r++;
glennrp9d0ea4d2011-04-22 18:35:57 +00003143
3144 if (ping_color_type == 4)
3145 {
glennrpc17d96f2011-06-27 01:20:11 +00003146 if (image->colors > 256)
3147 quantum=((*p++) << 8);
3148 else
3149 quantum=0;
3150
glennrp58f77c72011-04-23 14:09:09 +00003151 quantum|=(*p++);
cristy4c08aed2011-07-01 19:47:50 +00003152 SetPixelAlpha(image,ScaleShortToQuantum(quantum),q);
3153 if (GetPixelAlpha(image,q) != OpaqueAlpha)
glennrp58f77c72011-04-23 14:09:09 +00003154 found_transparent_pixel = MagickTrue;
cristyed231572011-07-14 02:18:59 +00003155 q+=GetPixelChannels(image);
glennrp58f77c72011-04-23 14:09:09 +00003156 }
glennrp58f77c72011-04-23 14:09:09 +00003157
3158#else /* MAGICKCORE_QUANTUM_DEPTH == 8 */
3159 *r++=(*p++);
3160 p++; /* strip low byte */
3161
3162 if (ping_color_type == 4)
3163 {
cristy4c08aed2011-07-01 19:47:50 +00003164 SetPixelAlpha(image,*p++,q);
3165 if (GetPixelAlpha(image,q) != OpaqueAlpha)
glennrp9d0ea4d2011-04-22 18:35:57 +00003166 found_transparent_pixel = MagickTrue;
3167 p++;
cristyed231572011-07-14 02:18:59 +00003168 q+=GetPixelChannels(image);
glennrp9d0ea4d2011-04-22 18:35:57 +00003169 }
cristy3ed852e2009-09-05 21:47:34 +00003170#endif
glennrpa18d5bc2011-04-23 14:51:34 +00003171 }
glennrp47b9dd52010-11-24 18:12:06 +00003172
cristy3ed852e2009-09-05 21:47:34 +00003173 break;
3174 }
glennrp47b9dd52010-11-24 18:12:06 +00003175
cristy3ed852e2009-09-05 21:47:34 +00003176 default:
3177 break;
3178 }
glennrp3faa9a32011-04-23 14:00:25 +00003179
cristy3ed852e2009-09-05 21:47:34 +00003180 /*
3181 Transfer image scanline.
3182 */
3183 r=quantum_scanline;
glennrp0fe50b42010-11-16 03:52:51 +00003184
cristy4c08aed2011-07-01 19:47:50 +00003185 q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
3186
cristyacd2ed22011-08-30 01:44:23 +00003187 if (q == (Quantum *) NULL)
cristy4c08aed2011-07-01 19:47:50 +00003188 break;
cristybb503372010-05-27 20:51:26 +00003189 for (x=0; x < (ssize_t) image->columns; x++)
cristy4c08aed2011-07-01 19:47:50 +00003190 {
3191 SetPixelIndex(image,*r++,q);
cristyed231572011-07-14 02:18:59 +00003192 q+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00003193 }
glennrp0fe50b42010-11-16 03:52:51 +00003194
cristy3ed852e2009-09-05 21:47:34 +00003195 if (SyncAuthenticPixels(image,exception) == MagickFalse)
3196 break;
glennrp0fe50b42010-11-16 03:52:51 +00003197
cristy7a287bf2010-02-14 02:18:09 +00003198 if ((image->previous == (Image *) NULL) && (num_passes == 1))
3199 {
cristycee97112010-05-28 00:44:52 +00003200 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
cristy9fff7b42011-04-29 01:09:31 +00003201 image->rows);
glennrp47b9dd52010-11-24 18:12:06 +00003202
cristy7a287bf2010-02-14 02:18:09 +00003203 if (status == MagickFalse)
3204 break;
3205 }
cristy3ed852e2009-09-05 21:47:34 +00003206 }
glennrpc8cbc5d2011-01-01 00:12:34 +00003207
cristy7a287bf2010-02-14 02:18:09 +00003208 if ((image->previous == (Image *) NULL) && (num_passes != 1))
cristy3ed852e2009-09-05 21:47:34 +00003209 {
3210 status=SetImageProgress(image,LoadImageTag,pass,num_passes);
glennrp47b9dd52010-11-24 18:12:06 +00003211
cristy3ed852e2009-09-05 21:47:34 +00003212 if (status == MagickFalse)
3213 break;
3214 }
glennrpc8cbc5d2011-01-01 00:12:34 +00003215
cristy3ed852e2009-09-05 21:47:34 +00003216 quantum_scanline=(Quantum *) RelinquishMagickMemory(quantum_scanline);
3217 }
glennrpc8cbc5d2011-01-01 00:12:34 +00003218
3219 image->matte=found_transparent_pixel;
3220
3221 if (logging != MagickFalse)
3222 {
3223 if (found_transparent_pixel != MagickFalse)
3224 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
3225 " Found transparent pixel");
3226 else
glennrp5aa37f62011-01-02 03:07:57 +00003227 {
3228 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
3229 " No transparent pixel was found");
glennrpbb4f99d2011-05-22 11:13:17 +00003230
glennrp5aa37f62011-01-02 03:07:57 +00003231 ping_color_type&=0x03;
3232 }
glennrpc8cbc5d2011-01-01 00:12:34 +00003233 }
3234 }
3235
cristyb32b90a2009-09-07 21:45:48 +00003236 if (quantum_info != (QuantumInfo *) NULL)
3237 quantum_info=DestroyQuantumInfo(quantum_info);
glennrp0fe50b42010-11-16 03:52:51 +00003238
cristy5c6f7892010-05-05 22:53:29 +00003239 if (image->storage_class == PseudoClass)
3240 {
cristyaeb2cbc2010-05-07 13:28:58 +00003241 MagickBooleanType
cristy5c6f7892010-05-05 22:53:29 +00003242 matte;
3243
3244 matte=image->matte;
3245 image->matte=MagickFalse;
cristyea1a8aa2011-10-20 13:24:06 +00003246 (void) SyncImage(image,exception);
cristyaeb2cbc2010-05-07 13:28:58 +00003247 image->matte=matte;
cristy5c6f7892010-05-05 22:53:29 +00003248 }
glennrp47b9dd52010-11-24 18:12:06 +00003249
glennrp4eb39312011-03-30 21:34:55 +00003250 png_read_end(ping,end_info);
cristy3ed852e2009-09-05 21:47:34 +00003251
3252 if (image_info->number_scenes != 0 && mng_info->scenes_found-1 <
cristybb503372010-05-27 20:51:26 +00003253 (ssize_t) image_info->first_scene && image->delay != 0)
cristy3ed852e2009-09-05 21:47:34 +00003254 {
3255 png_destroy_read_struct(&ping,&ping_info,&end_info);
glennrpcf002022011-01-30 02:38:15 +00003256 ping_pixels=(unsigned char *) RelinquishMagickMemory(ping_pixels);
cristy3ed852e2009-09-05 21:47:34 +00003257 image->colors=2;
cristyea1a8aa2011-10-20 13:24:06 +00003258 (void) SetImageBackgroundColor(image,exception);
cristy3ed852e2009-09-05 21:47:34 +00003259#if defined(PNG_SETJMP_NOT_THREAD_SAFE)
glennrpcf002022011-01-30 02:38:15 +00003260 UnlockSemaphoreInfo(ping_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00003261#endif
3262 if (logging != MagickFalse)
3263 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
3264 " exit ReadOnePNGImage() early.");
3265 return(image);
3266 }
glennrp47b9dd52010-11-24 18:12:06 +00003267
glennrpfaa852b2010-03-30 12:17:00 +00003268 if (png_get_valid(ping,ping_info,PNG_INFO_tRNS))
cristy3ed852e2009-09-05 21:47:34 +00003269 {
3270 ClassType
3271 storage_class;
3272
3273 /*
3274 Image has a transparent background.
3275 */
3276 storage_class=image->storage_class;
3277 image->matte=MagickTrue;
glennrpc11cf6a2010-03-20 16:46:19 +00003278
glennrp3c218112010-11-27 15:31:26 +00003279/* Balfour fix from imagemagick discourse server, 5 Feb 2010 */
glennrpc11cf6a2010-03-20 16:46:19 +00003280
glennrp0fe50b42010-11-16 03:52:51 +00003281 if (storage_class == PseudoClass)
3282 {
3283 if ((int) ping_color_type == PNG_COLOR_TYPE_PALETTE)
glennrpc11cf6a2010-03-20 16:46:19 +00003284 {
glennrp0fe50b42010-11-16 03:52:51 +00003285 for (x=0; x < ping_num_trans; x++)
3286 {
cristy6b7677c2012-01-01 20:59:57 +00003287 image->colormap[x].matte=MagickTrue;
cristy4c08aed2011-07-01 19:47:50 +00003288 image->colormap[x].alpha =
3289 ScaleCharToQuantum((unsigned char)ping_trans_alpha[x]);
glennrp0fe50b42010-11-16 03:52:51 +00003290 }
glennrpc11cf6a2010-03-20 16:46:19 +00003291 }
glennrp47b9dd52010-11-24 18:12:06 +00003292
glennrp0fe50b42010-11-16 03:52:51 +00003293 else if (ping_color_type == PNG_COLOR_TYPE_GRAY)
3294 {
3295 for (x=0; x < (int) image->colors; x++)
3296 {
3297 if (ScaleQuantumToShort(image->colormap[x].red) ==
cristy4c08aed2011-07-01 19:47:50 +00003298 transparent_color.alpha)
glennrp0fe50b42010-11-16 03:52:51 +00003299 {
cristy6b7677c2012-01-01 20:59:57 +00003300 image->colormap[x].matte=MagickTrue;
cristy4c08aed2011-07-01 19:47:50 +00003301 image->colormap[x].alpha = (Quantum) TransparentAlpha;
glennrp0fe50b42010-11-16 03:52:51 +00003302 }
3303 }
3304 }
cristyea1a8aa2011-10-20 13:24:06 +00003305 (void) SyncImage(image,exception);
glennrp0fe50b42010-11-16 03:52:51 +00003306 }
glennrp47b9dd52010-11-24 18:12:06 +00003307
glennrpa6a06632011-01-19 15:15:34 +00003308#if 1 /* Should have already been done above, but glennrp problem P10
3309 * needs this.
3310 */
glennrp0fe50b42010-11-16 03:52:51 +00003311 else
3312 {
3313 for (y=0; y < (ssize_t) image->rows; y++)
glennrpc11cf6a2010-03-20 16:46:19 +00003314 {
glennrp0fe50b42010-11-16 03:52:51 +00003315 image->storage_class=storage_class;
3316 q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
3317
cristyacd2ed22011-08-30 01:44:23 +00003318 if (q == (Quantum *) NULL)
glennrp0fe50b42010-11-16 03:52:51 +00003319 break;
3320
glennrp0fe50b42010-11-16 03:52:51 +00003321
glennrpa6a06632011-01-19 15:15:34 +00003322 /* Caution: on a Q8 build, this does not distinguish between
3323 * 16-bit colors that differ only in the low byte
3324 */
glennrp0fe50b42010-11-16 03:52:51 +00003325 for (x=(ssize_t) image->columns-1; x >= 0; x--)
3326 {
glennrp847370c2011-07-05 17:37:15 +00003327 if (ScaleQuantumToShort(GetPixelRed(image,q)) ==
3328 transparent_color.red &&
3329 ScaleQuantumToShort(GetPixelGreen(image,q)) ==
3330 transparent_color.green &&
3331 ScaleQuantumToShort(GetPixelBlue(image,q)) ==
3332 transparent_color.blue)
glennrp4f25bd02011-01-01 18:51:28 +00003333 {
cristy4c08aed2011-07-01 19:47:50 +00003334 SetPixelAlpha(image,TransparentAlpha,q);
glennrp4f25bd02011-01-01 18:51:28 +00003335 }
glennrp0fe50b42010-11-16 03:52:51 +00003336
glennrp67b9c1a2011-04-22 18:47:36 +00003337#if 0 /* I have not found a case where this is needed. */
glennrp0fe50b42010-11-16 03:52:51 +00003338 else
glennrp4f25bd02011-01-01 18:51:28 +00003339 {
cristy4c08aed2011-07-01 19:47:50 +00003340 SetPixelAlpha(image,q)=(Quantum) OpaqueAlpha;
glennrp4f25bd02011-01-01 18:51:28 +00003341 }
glennrpa6a06632011-01-19 15:15:34 +00003342#endif
glennrp0fe50b42010-11-16 03:52:51 +00003343
cristyed231572011-07-14 02:18:59 +00003344 q+=GetPixelChannels(image);
glennrp0fe50b42010-11-16 03:52:51 +00003345 }
3346
3347 if (SyncAuthenticPixels(image,exception) == MagickFalse)
3348 break;
glennrpc11cf6a2010-03-20 16:46:19 +00003349 }
glennrp0fe50b42010-11-16 03:52:51 +00003350 }
glennrpa6a06632011-01-19 15:15:34 +00003351#endif
glennrpc11cf6a2010-03-20 16:46:19 +00003352
cristy3ed852e2009-09-05 21:47:34 +00003353 image->storage_class=DirectClass;
3354 }
glennrp3c218112010-11-27 15:31:26 +00003355
cristyb40fc462010-08-08 00:49:49 +00003356 if ((ping_color_type == PNG_COLOR_TYPE_GRAY) ||
3357 (ping_color_type == PNG_COLOR_TYPE_GRAY_ALPHA))
3358 image->colorspace=GRAYColorspace;
glennrp47b9dd52010-11-24 18:12:06 +00003359
cristyeb3b22a2011-03-31 20:16:11 +00003360 for (j = 0; j < 2; j++)
glennrp4eb39312011-03-30 21:34:55 +00003361 {
3362 if (j == 0)
glennrpa0ed0092011-04-18 16:36:29 +00003363 status = png_get_text(ping,ping_info,&text,&num_text) != 0 ?
3364 MagickTrue : MagickFalse;
glennrp4eb39312011-03-30 21:34:55 +00003365 else
glennrpa0ed0092011-04-18 16:36:29 +00003366 status = png_get_text(ping,end_info,&text,&num_text) != 0 ?
3367 MagickTrue : MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00003368
glennrp4eb39312011-03-30 21:34:55 +00003369 if (status != MagickFalse)
3370 for (i=0; i < (ssize_t) num_text; i++)
3371 {
3372 /* Check for a profile */
glennrp0fe50b42010-11-16 03:52:51 +00003373
glennrp4eb39312011-03-30 21:34:55 +00003374 if (logging != MagickFalse)
3375 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
3376 " Reading PNG text chunk");
glennrp0fe50b42010-11-16 03:52:51 +00003377
glennrp4eb39312011-03-30 21:34:55 +00003378 if (memcmp(text[i].key, "Raw profile type ",17) == 0)
glennrp97f90e22011-02-22 05:47:58 +00003379 {
cristyd15e6592011-10-15 00:13:06 +00003380 (void) Magick_png_read_raw_profile(image,image_info,text,(int) i,
3381 exception);
glennrp4eb39312011-03-30 21:34:55 +00003382 num_raw_profiles++;
glennrp97f90e22011-02-22 05:47:58 +00003383 }
glennrp0fe50b42010-11-16 03:52:51 +00003384
glennrp4eb39312011-03-30 21:34:55 +00003385 else
3386 {
3387 char
3388 *value;
3389
3390 length=text[i].text_length;
3391 value=(char *) AcquireQuantumMemory(length+MaxTextExtent,
3392 sizeof(*value));
3393 if (value == (char *) NULL)
3394 {
cristyc82a27b2011-10-21 01:07:16 +00003395 (void) ThrowMagickException(exception,GetMagickModule(),
glennrp4eb39312011-03-30 21:34:55 +00003396 ResourceLimitError,"MemoryAllocationFailed","`%s'",
3397 image->filename);
3398 break;
3399 }
3400 *value='\0';
3401 (void) ConcatenateMagickString(value,text[i].text,length+2);
3402
3403 /* Don't save "density" or "units" property if we have a pHYs
3404 * chunk
3405 */
3406 if (!png_get_valid(ping,ping_info,PNG_INFO_pHYs) ||
3407 (LocaleCompare(text[i].key,"density") != 0 &&
3408 LocaleCompare(text[i].key,"units") != 0))
cristyd15e6592011-10-15 00:13:06 +00003409 (void) SetImageProperty(image,text[i].key,value,exception);
glennrp4eb39312011-03-30 21:34:55 +00003410
3411 if (logging != MagickFalse)
3412 {
3413 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
3414 " length: %lu",(unsigned long) length);
3415 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
3416 " Keyword: %s",text[i].key);
3417 }
3418
3419 value=DestroyString(value);
3420 }
3421 }
3422 num_text_total += num_text;
cristy3ed852e2009-09-05 21:47:34 +00003423 }
glennrp3c218112010-11-27 15:31:26 +00003424
cristy3ed852e2009-09-05 21:47:34 +00003425#ifdef MNG_OBJECT_BUFFERS
3426 /*
3427 Store the object if necessary.
3428 */
3429 if (object_id && !mng_info->frozen[object_id])
3430 {
3431 if (mng_info->ob[object_id] == (MngBuffer *) NULL)
3432 {
3433 /*
3434 create a new object buffer.
3435 */
3436 mng_info->ob[object_id]=(MngBuffer *)
cristy73bd4a52010-10-05 11:24:23 +00003437 AcquireMagickMemory(sizeof(MngBuffer));
glennrp0fe50b42010-11-16 03:52:51 +00003438
cristy3ed852e2009-09-05 21:47:34 +00003439 if (mng_info->ob[object_id] != (MngBuffer *) NULL)
3440 {
3441 mng_info->ob[object_id]->image=(Image *) NULL;
3442 mng_info->ob[object_id]->reference_count=1;
3443 }
3444 }
glennrp47b9dd52010-11-24 18:12:06 +00003445
cristy3ed852e2009-09-05 21:47:34 +00003446 if ((mng_info->ob[object_id] == (MngBuffer *) NULL) ||
3447 mng_info->ob[object_id]->frozen)
3448 {
3449 if (mng_info->ob[object_id] == (MngBuffer *) NULL)
cristyc82a27b2011-10-21 01:07:16 +00003450 (void) ThrowMagickException(exception,GetMagickModule(),
cristy3ed852e2009-09-05 21:47:34 +00003451 ResourceLimitError,"MemoryAllocationFailed","`%s'",
3452 image->filename);
glennrp0fe50b42010-11-16 03:52:51 +00003453
cristy3ed852e2009-09-05 21:47:34 +00003454 if (mng_info->ob[object_id]->frozen)
cristyc82a27b2011-10-21 01:07:16 +00003455 (void) ThrowMagickException(exception,GetMagickModule(),
cristy3ed852e2009-09-05 21:47:34 +00003456 ResourceLimitError,"Cannot overwrite frozen MNG object buffer",
3457 "`%s'",image->filename);
3458 }
glennrp0fe50b42010-11-16 03:52:51 +00003459
cristy3ed852e2009-09-05 21:47:34 +00003460 else
3461 {
cristy3ed852e2009-09-05 21:47:34 +00003462
3463 if (mng_info->ob[object_id]->image != (Image *) NULL)
3464 mng_info->ob[object_id]->image=DestroyImage
3465 (mng_info->ob[object_id]->image);
glennrp0fe50b42010-11-16 03:52:51 +00003466
cristy3ed852e2009-09-05 21:47:34 +00003467 mng_info->ob[object_id]->image=CloneImage(image,0,0,MagickTrue,
cristyc82a27b2011-10-21 01:07:16 +00003468 exception);
glennrp0fe50b42010-11-16 03:52:51 +00003469
cristy3ed852e2009-09-05 21:47:34 +00003470 if (mng_info->ob[object_id]->image != (Image *) NULL)
3471 mng_info->ob[object_id]->image->file=(FILE *) NULL;
glennrp0fe50b42010-11-16 03:52:51 +00003472
cristy3ed852e2009-09-05 21:47:34 +00003473 else
cristyc82a27b2011-10-21 01:07:16 +00003474 (void) ThrowMagickException(exception,GetMagickModule(),
cristy3ed852e2009-09-05 21:47:34 +00003475 ResourceLimitError,"Cloning image for object buffer failed",
3476 "`%s'",image->filename);
glennrp0fe50b42010-11-16 03:52:51 +00003477
glennrpfaa852b2010-03-30 12:17:00 +00003478 if (ping_width > 250000L || ping_height > 250000L)
cristy3ed852e2009-09-05 21:47:34 +00003479 png_error(ping,"PNG Image dimensions are too large.");
glennrp0fe50b42010-11-16 03:52:51 +00003480
glennrpfaa852b2010-03-30 12:17:00 +00003481 mng_info->ob[object_id]->width=ping_width;
3482 mng_info->ob[object_id]->height=ping_height;
3483 mng_info->ob[object_id]->color_type=ping_color_type;
3484 mng_info->ob[object_id]->sample_depth=ping_bit_depth;
3485 mng_info->ob[object_id]->interlace_method=ping_interlace_method;
3486 mng_info->ob[object_id]->compression_method=
3487 ping_compression_method;
3488 mng_info->ob[object_id]->filter_method=ping_filter_method;
glennrp0fe50b42010-11-16 03:52:51 +00003489
glennrpfaa852b2010-03-30 12:17:00 +00003490 if (png_get_valid(ping,ping_info,PNG_INFO_PLTE))
cristy3ed852e2009-09-05 21:47:34 +00003491 {
3492 int
3493 number_colors;
3494
3495 png_colorp
3496 plte;
3497
3498 /*
3499 Copy the PLTE to the object buffer.
3500 */
3501 png_get_PLTE(ping,ping_info,&plte,&number_colors);
3502 mng_info->ob[object_id]->plte_length=number_colors;
glennrp3c218112010-11-27 15:31:26 +00003503
cristy3ed852e2009-09-05 21:47:34 +00003504 for (i=0; i < number_colors; i++)
3505 {
3506 mng_info->ob[object_id]->plte[i]=plte[i];
3507 }
3508 }
glennrp47b9dd52010-11-24 18:12:06 +00003509
cristy3ed852e2009-09-05 21:47:34 +00003510 else
3511 mng_info->ob[object_id]->plte_length=0;
3512 }
3513 }
3514#endif
glennrp0a55b4c2011-03-23 12:38:38 +00003515
3516 /* Set image->matte to MagickTrue if the input colortype supports
3517 * alpha or if a valid tRNS chunk is present, no matter whether there
3518 * is actual transparency present.
3519 */
3520 image->matte=(((int) ping_color_type == PNG_COLOR_TYPE_RGB_ALPHA) ||
3521 ((int) ping_color_type == PNG_COLOR_TYPE_GRAY_ALPHA) ||
3522 (png_get_valid(ping,ping_info,PNG_INFO_tRNS))) ?
3523 MagickTrue : MagickFalse;
3524
glennrpcb395ac2011-03-30 19:50:23 +00003525 /* Set more properties for identify to retrieve */
3526 {
3527 char
3528 msg[MaxTextExtent];
3529
glennrp4eb39312011-03-30 21:34:55 +00003530 if (num_text_total != 0)
glennrpcb395ac2011-03-30 19:50:23 +00003531 {
3532 /* libpng doesn't tell us whether they were tEXt, zTXt, or iTXt */
cristy3b6fd2e2011-05-20 12:53:50 +00003533 (void) FormatLocaleString(msg,MaxTextExtent,
glennrp613276d2011-03-30 21:46:49 +00003534 "%d tEXt/zTXt/iTXt chunks were found", num_text_total);
cristy5d6fc9c2011-12-27 03:10:42 +00003535 (void) SetImageProperty(image,"png:text ",msg,
glennrp1a2061f2011-10-18 12:30:45 +00003536 exception);
glennrpcb395ac2011-03-30 19:50:23 +00003537 }
3538
3539 if (num_raw_profiles != 0)
3540 {
cristy3b6fd2e2011-05-20 12:53:50 +00003541 (void) FormatLocaleString(msg,MaxTextExtent,
glennrpcb395ac2011-03-30 19:50:23 +00003542 "%d were found", num_raw_profiles);
cristy5d6fc9c2011-12-27 03:10:42 +00003543 (void) SetImageProperty(image,"png:text-encoded profiles",msg,
glennrp1a2061f2011-10-18 12:30:45 +00003544 exception);
glennrpcb395ac2011-03-30 19:50:23 +00003545 }
3546
glennrpcb395ac2011-03-30 19:50:23 +00003547 if (png_get_valid(ping,ping_info,PNG_INFO_cHRM))
glennrp59612252011-03-30 21:45:21 +00003548 {
cristy3b6fd2e2011-05-20 12:53:50 +00003549 (void) FormatLocaleString(msg,MaxTextExtent,"%s",
glennrp59612252011-03-30 21:45:21 +00003550 "chunk was found (see Chromaticity, above)");
cristy5d6fc9c2011-12-27 03:10:42 +00003551 (void) SetImageProperty(image,"png:cHRM ",msg,
glennrp1a2061f2011-10-18 12:30:45 +00003552 exception);
glennrp59612252011-03-30 21:45:21 +00003553 }
glennrpcb395ac2011-03-30 19:50:23 +00003554
3555 if (png_get_valid(ping,ping_info,PNG_INFO_bKGD))
glennrp59612252011-03-30 21:45:21 +00003556 {
cristy3b6fd2e2011-05-20 12:53:50 +00003557 (void) FormatLocaleString(msg,MaxTextExtent,"%s",
glennrp59612252011-03-30 21:45:21 +00003558 "chunk was found (see Background color, above)");
cristy5d6fc9c2011-12-27 03:10:42 +00003559 (void) SetImageProperty(image,"png:bKGD ",msg,
glennrp1a2061f2011-10-18 12:30:45 +00003560 exception);
glennrp59612252011-03-30 21:45:21 +00003561 }
3562
cristy3b6fd2e2011-05-20 12:53:50 +00003563 (void) FormatLocaleString(msg,MaxTextExtent,"%s",
glennrp59612252011-03-30 21:45:21 +00003564 "chunk was found");
glennrpcb395ac2011-03-30 19:50:23 +00003565
3566 if (png_get_valid(ping,ping_info,PNG_INFO_iCCP))
cristy5d6fc9c2011-12-27 03:10:42 +00003567 (void) SetImageProperty(image,"png:iCCP ",msg,
glennrp1a2061f2011-10-18 12:30:45 +00003568 exception);
glennrpcb395ac2011-03-30 19:50:23 +00003569
glennrpcb395ac2011-03-30 19:50:23 +00003570 if (png_get_valid(ping,ping_info,PNG_INFO_tRNS))
cristy5d6fc9c2011-12-27 03:10:42 +00003571 (void) SetImageProperty(image,"png:tRNS ",msg,
glennrp1a2061f2011-10-18 12:30:45 +00003572 exception);
glennrp4eb39312011-03-30 21:34:55 +00003573
3574#if defined(PNG_sRGB_SUPPORTED)
3575 if (png_get_valid(ping,ping_info,PNG_INFO_sRGB))
3576 {
cristy3b6fd2e2011-05-20 12:53:50 +00003577 (void) FormatLocaleString(msg,MaxTextExtent,
glennrp07523c72011-03-31 18:12:10 +00003578 "intent=%d (See Rendering intent)",
glennrp4eb39312011-03-30 21:34:55 +00003579 (int) intent);
cristy5d6fc9c2011-12-27 03:10:42 +00003580 (void) SetImageProperty(image,"png:sRGB ",msg,
glennrp1a2061f2011-10-18 12:30:45 +00003581 exception);
glennrp4eb39312011-03-30 21:34:55 +00003582 }
3583#endif
3584
3585 if (png_get_valid(ping,ping_info,PNG_INFO_gAMA))
3586 {
cristy3b6fd2e2011-05-20 12:53:50 +00003587 (void) FormatLocaleString(msg,MaxTextExtent,
glennrp07523c72011-03-31 18:12:10 +00003588 "gamma=%.8g (See Gamma, above)",
glennrp4eb39312011-03-30 21:34:55 +00003589 file_gamma);
cristy5d6fc9c2011-12-27 03:10:42 +00003590 (void) SetImageProperty(image,"png:gAMA ",msg,
glennrp1a2061f2011-10-18 12:30:45 +00003591 exception);
glennrp4eb39312011-03-30 21:34:55 +00003592 }
3593
3594#if defined(PNG_pHYs_SUPPORTED)
3595 if (png_get_valid(ping,ping_info,PNG_INFO_pHYs))
3596 {
cristy3b6fd2e2011-05-20 12:53:50 +00003597 (void) FormatLocaleString(msg,MaxTextExtent,
glennrp07523c72011-03-31 18:12:10 +00003598 "x_res=%.10g, y_res=%.10g, units=%d",
glennrp4eb39312011-03-30 21:34:55 +00003599 (double) x_resolution,(double) y_resolution, unit_type);
cristy5d6fc9c2011-12-27 03:10:42 +00003600 (void) SetImageProperty(image,"png:pHYs ",msg,
glennrp1a2061f2011-10-18 12:30:45 +00003601 exception);
glennrp4eb39312011-03-30 21:34:55 +00003602 }
3603#endif
3604
3605#if defined(PNG_oFFs_SUPPORTED)
3606 if (png_get_valid(ping,ping_info,PNG_INFO_oFFs))
3607 {
cristy3b6fd2e2011-05-20 12:53:50 +00003608 (void) FormatLocaleString(msg,MaxTextExtent,"x_off=%.20g, y_off=%.20g",
glennrp4eb39312011-03-30 21:34:55 +00003609 (double) image->page.x,(double) image->page.y);
cristy5d6fc9c2011-12-27 03:10:42 +00003610 (void) SetImageProperty(image,"png:oFFs ",msg,
glennrp1a2061f2011-10-18 12:30:45 +00003611 exception);
glennrp4eb39312011-03-30 21:34:55 +00003612 }
3613#endif
3614
glennrp07523c72011-03-31 18:12:10 +00003615 if ((image->page.width != 0 && image->page.width != image->columns) ||
3616 (image->page.height != 0 && image->page.height != image->rows))
3617 {
cristy3b6fd2e2011-05-20 12:53:50 +00003618 (void) FormatLocaleString(msg,MaxTextExtent,
glennrp07523c72011-03-31 18:12:10 +00003619 "width=%.20g, height=%.20g",
3620 (double) image->page.width,(double) image->page.height);
cristy5d6fc9c2011-12-27 03:10:42 +00003621 (void) SetImageProperty(image,"png:vpAg ",msg,
glennrp1a2061f2011-10-18 12:30:45 +00003622 exception);
glennrp07523c72011-03-31 18:12:10 +00003623 }
glennrpcb395ac2011-03-30 19:50:23 +00003624 }
3625
cristy3ed852e2009-09-05 21:47:34 +00003626 /*
3627 Relinquish resources.
3628 */
3629 png_destroy_read_struct(&ping,&ping_info,&end_info);
3630
glennrpcf002022011-01-30 02:38:15 +00003631 ping_pixels=(unsigned char *) RelinquishMagickMemory(ping_pixels);
cristy3ed852e2009-09-05 21:47:34 +00003632#if defined(PNG_SETJMP_NOT_THREAD_SAFE)
glennrpcf002022011-01-30 02:38:15 +00003633 UnlockSemaphoreInfo(ping_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00003634#endif
3635
3636 if (logging != MagickFalse)
3637 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
3638 " exit ReadOnePNGImage()");
glennrp0fe50b42010-11-16 03:52:51 +00003639
cristy3ed852e2009-09-05 21:47:34 +00003640 return(image);
3641
3642/* end of reading one PNG image */
3643}
3644
3645static Image *ReadPNGImage(const ImageInfo *image_info,ExceptionInfo *exception)
3646{
3647 Image
3648 *image,
3649 *previous;
3650
3651 MagickBooleanType
glennrp21f0e622011-01-07 16:20:57 +00003652 have_mng_structure,
3653 logging,
cristy3ed852e2009-09-05 21:47:34 +00003654 status;
3655
3656 MngInfo
3657 *mng_info;
3658
3659 char
3660 magic_number[MaxTextExtent];
3661
cristy3ed852e2009-09-05 21:47:34 +00003662 ssize_t
3663 count;
3664
3665 /*
3666 Open image file.
3667 */
3668 assert(image_info != (const ImageInfo *) NULL);
3669 assert(image_info->signature == MagickSignature);
glennrp47b9dd52010-11-24 18:12:06 +00003670
cristy3ed852e2009-09-05 21:47:34 +00003671 if (image_info->debug != MagickFalse)
3672 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
3673 image_info->filename);
glennrp47b9dd52010-11-24 18:12:06 +00003674
cristy3ed852e2009-09-05 21:47:34 +00003675 assert(exception != (ExceptionInfo *) NULL);
3676 assert(exception->signature == MagickSignature);
glennrpfd05d622011-02-25 04:10:33 +00003677 logging=LogMagickEvent(CoderEvent,GetMagickModule(),"Enter ReadPNGImage()");
cristy9950d572011-10-01 18:22:35 +00003678 image=AcquireImage(image_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00003679 mng_info=(MngInfo *) NULL;
3680 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
glennrp47b9dd52010-11-24 18:12:06 +00003681
cristy3ed852e2009-09-05 21:47:34 +00003682 if (status == MagickFalse)
3683 ThrowReaderException(FileOpenError,"UnableToOpenFile");
glennrp47b9dd52010-11-24 18:12:06 +00003684
cristy3ed852e2009-09-05 21:47:34 +00003685 /*
3686 Verify PNG signature.
3687 */
3688 count=ReadBlob(image,8,(unsigned char *) magic_number);
glennrp47b9dd52010-11-24 18:12:06 +00003689
glennrpdde35db2011-02-21 12:06:32 +00003690 if (count < 8 || memcmp(magic_number,"\211PNG\r\n\032\n",8) != 0)
cristy3ed852e2009-09-05 21:47:34 +00003691 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
glennrp47b9dd52010-11-24 18:12:06 +00003692
cristy3ed852e2009-09-05 21:47:34 +00003693 /*
3694 Allocate a MngInfo structure.
3695 */
3696 have_mng_structure=MagickFalse;
cristy73bd4a52010-10-05 11:24:23 +00003697 mng_info=(MngInfo *) AcquireMagickMemory(sizeof(MngInfo));
glennrp47b9dd52010-11-24 18:12:06 +00003698
cristy3ed852e2009-09-05 21:47:34 +00003699 if (mng_info == (MngInfo *) NULL)
3700 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
glennrp47b9dd52010-11-24 18:12:06 +00003701
cristy3ed852e2009-09-05 21:47:34 +00003702 /*
3703 Initialize members of the MngInfo structure.
3704 */
3705 (void) ResetMagickMemory(mng_info,0,sizeof(MngInfo));
3706 mng_info->image=image;
3707 have_mng_structure=MagickTrue;
3708
3709 previous=image;
3710 image=ReadOnePNGImage(mng_info,image_info,exception);
3711 MngInfoFreeStruct(mng_info,&have_mng_structure);
glennrp47b9dd52010-11-24 18:12:06 +00003712
cristy3ed852e2009-09-05 21:47:34 +00003713 if (image == (Image *) NULL)
3714 {
3715 if (previous != (Image *) NULL)
3716 {
3717 if (previous->signature != MagickSignature)
3718 ThrowReaderException(CorruptImageError,"CorruptImage");
glennrp0fe50b42010-11-16 03:52:51 +00003719
cristy3ed852e2009-09-05 21:47:34 +00003720 (void) CloseBlob(previous);
3721 (void) DestroyImageList(previous);
3722 }
glennrp0fe50b42010-11-16 03:52:51 +00003723
cristy3ed852e2009-09-05 21:47:34 +00003724 if (logging != MagickFalse)
3725 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
3726 "exit ReadPNGImage() with error");
glennrp0fe50b42010-11-16 03:52:51 +00003727
cristy3ed852e2009-09-05 21:47:34 +00003728 return((Image *) NULL);
3729 }
glennrp47b9dd52010-11-24 18:12:06 +00003730
cristy3ed852e2009-09-05 21:47:34 +00003731 (void) CloseBlob(image);
glennrp47b9dd52010-11-24 18:12:06 +00003732
cristy3ed852e2009-09-05 21:47:34 +00003733 if ((image->columns == 0) || (image->rows == 0))
3734 {
3735 if (logging != MagickFalse)
3736 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
3737 "exit ReadPNGImage() with error.");
glennrp0fe50b42010-11-16 03:52:51 +00003738
cristy3ed852e2009-09-05 21:47:34 +00003739 ThrowReaderException(CorruptImageError,"CorruptImage");
3740 }
glennrp47b9dd52010-11-24 18:12:06 +00003741
cristy3ed852e2009-09-05 21:47:34 +00003742 if (LocaleCompare(image_info->magick,"PNG24") == 0)
3743 {
cristy018f07f2011-09-04 21:15:19 +00003744 (void) SetImageType(image,TrueColorType,exception);
cristy3ed852e2009-09-05 21:47:34 +00003745 image->matte=MagickFalse;
3746 }
glennrp0fe50b42010-11-16 03:52:51 +00003747
cristy3ed852e2009-09-05 21:47:34 +00003748 if (LocaleCompare(image_info->magick,"PNG32") == 0)
cristy018f07f2011-09-04 21:15:19 +00003749 (void) SetImageType(image,TrueColorMatteType,exception);
glennrp0fe50b42010-11-16 03:52:51 +00003750
cristy3ed852e2009-09-05 21:47:34 +00003751 if (logging != MagickFalse)
glennrp97f90e22011-02-22 05:47:58 +00003752 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
3753 " page.w: %.20g, page.h: %.20g,page.x: %.20g, page.y: %.20g.",
3754 (double) image->page.width,(double) image->page.height,
3755 (double) image->page.x,(double) image->page.y);
3756
3757 if (logging != MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003758 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"exit ReadPNGImage()");
glennrp0fe50b42010-11-16 03:52:51 +00003759
cristy3ed852e2009-09-05 21:47:34 +00003760 return(image);
3761}
3762
3763
3764
3765#if defined(JNG_SUPPORTED)
3766/*
3767%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3768% %
3769% %
3770% %
3771% R e a d O n e J N G I m a g e %
3772% %
3773% %
3774% %
3775%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3776%
3777% ReadOneJNGImage() reads a JPEG Network Graphics (JNG) image file
3778% (minus the 8-byte signature) and returns it. It allocates the memory
3779% necessary for the new Image structure and returns a pointer to the new
3780% image.
3781%
3782% JNG support written by Glenn Randers-Pehrson, glennrp@image...
3783%
3784% The format of the ReadOneJNGImage method is:
3785%
3786% Image *ReadOneJNGImage(MngInfo *mng_info, const ImageInfo *image_info,
3787% ExceptionInfo *exception)
3788%
3789% A description of each parameter follows:
3790%
3791% o mng_info: Specifies a pointer to a MngInfo structure.
3792%
3793% o image_info: the image info.
3794%
3795% o exception: return any errors or warnings in this structure.
3796%
3797*/
3798static Image *ReadOneJNGImage(MngInfo *mng_info,
3799 const ImageInfo *image_info, ExceptionInfo *exception)
3800{
3801 Image
3802 *alpha_image,
3803 *color_image,
3804 *image,
3805 *jng_image;
3806
3807 ImageInfo
3808 *alpha_image_info,
3809 *color_image_info;
3810
cristy4383ec82011-01-05 15:42:32 +00003811 MagickBooleanType
3812 logging;
3813
cristybb503372010-05-27 20:51:26 +00003814 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003815 y;
3816
3817 MagickBooleanType
3818 status;
3819
3820 png_uint_32
3821 jng_height,
3822 jng_width;
3823
3824 png_byte
3825 jng_color_type,
3826 jng_image_sample_depth,
3827 jng_image_compression_method,
3828 jng_image_interlace_method,
3829 jng_alpha_sample_depth,
3830 jng_alpha_compression_method,
3831 jng_alpha_filter_method,
3832 jng_alpha_interlace_method;
3833
cristy4c08aed2011-07-01 19:47:50 +00003834 register const Quantum
cristy3ed852e2009-09-05 21:47:34 +00003835 *s;
3836
cristybb503372010-05-27 20:51:26 +00003837 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003838 i,
3839 x;
3840
cristy4c08aed2011-07-01 19:47:50 +00003841 register Quantum
cristy3ed852e2009-09-05 21:47:34 +00003842 *q;
3843
3844 register unsigned char
3845 *p;
3846
3847 unsigned int
cristy3ed852e2009-09-05 21:47:34 +00003848 read_JSEP,
3849 reading_idat,
3850 skip_to_iend;
3851
cristybb503372010-05-27 20:51:26 +00003852 size_t
cristy3ed852e2009-09-05 21:47:34 +00003853 length;
3854
3855 jng_alpha_compression_method=0;
3856 jng_alpha_sample_depth=8;
3857 jng_color_type=0;
3858 jng_height=0;
3859 jng_width=0;
3860 alpha_image=(Image *) NULL;
3861 color_image=(Image *) NULL;
3862 alpha_image_info=(ImageInfo *) NULL;
3863 color_image_info=(ImageInfo *) NULL;
3864
3865 logging=LogMagickEvent(CoderEvent,GetMagickModule(),
glennrpfd05d622011-02-25 04:10:33 +00003866 " Enter ReadOneJNGImage()");
cristy3ed852e2009-09-05 21:47:34 +00003867
3868 image=mng_info->image;
glennrp0fe50b42010-11-16 03:52:51 +00003869
cristy4c08aed2011-07-01 19:47:50 +00003870 if (GetAuthenticPixelQueue(image) != (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003871 {
3872 /*
3873 Allocate next image structure.
3874 */
3875 if (logging != MagickFalse)
3876 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
3877 " AcquireNextImage()");
glennrp0fe50b42010-11-16 03:52:51 +00003878
cristy9950d572011-10-01 18:22:35 +00003879 AcquireNextImage(image_info,image,exception);
glennrp0fe50b42010-11-16 03:52:51 +00003880
cristy3ed852e2009-09-05 21:47:34 +00003881 if (GetNextImageInList(image) == (Image *) NULL)
3882 return((Image *) NULL);
glennrp0fe50b42010-11-16 03:52:51 +00003883
cristy3ed852e2009-09-05 21:47:34 +00003884 image=SyncNextImageInList(image);
3885 }
3886 mng_info->image=image;
3887
3888 /*
3889 Signature bytes have already been read.
3890 */
3891
3892 read_JSEP=MagickFalse;
3893 reading_idat=MagickFalse;
3894 skip_to_iend=MagickFalse;
3895 for (;;)
3896 {
3897 char
3898 type[MaxTextExtent];
3899
3900 unsigned char
3901 *chunk;
3902
3903 unsigned int
3904 count;
3905
3906 /*
3907 Read a new JNG chunk.
3908 */
3909 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
3910 2*GetBlobSize(image));
glennrp0fe50b42010-11-16 03:52:51 +00003911
cristy3ed852e2009-09-05 21:47:34 +00003912 if (status == MagickFalse)
3913 break;
glennrp0fe50b42010-11-16 03:52:51 +00003914
cristy3ed852e2009-09-05 21:47:34 +00003915 type[0]='\0';
3916 (void) ConcatenateMagickString(type,"errr",MaxTextExtent);
3917 length=ReadBlobMSBLong(image);
3918 count=(unsigned int) ReadBlob(image,4,(unsigned char *) type);
3919
3920 if (logging != MagickFalse)
3921 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00003922 " Reading JNG chunk type %c%c%c%c, length: %.20g",
3923 type[0],type[1],type[2],type[3],(double) length);
cristy3ed852e2009-09-05 21:47:34 +00003924
3925 if (length > PNG_UINT_31_MAX || count == 0)
3926 ThrowReaderException(CorruptImageError,"CorruptImage");
glennrp0fe50b42010-11-16 03:52:51 +00003927
cristy3ed852e2009-09-05 21:47:34 +00003928 p=NULL;
3929 chunk=(unsigned char *) NULL;
glennrp47b9dd52010-11-24 18:12:06 +00003930
cristy3ed852e2009-09-05 21:47:34 +00003931 if (length)
3932 {
3933 chunk=(unsigned char *) AcquireQuantumMemory(length,sizeof(*chunk));
glennrp0fe50b42010-11-16 03:52:51 +00003934
cristy3ed852e2009-09-05 21:47:34 +00003935 if (chunk == (unsigned char *) NULL)
3936 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
glennrp0fe50b42010-11-16 03:52:51 +00003937
cristybb503372010-05-27 20:51:26 +00003938 for (i=0; i < (ssize_t) length; i++)
cristy3ed852e2009-09-05 21:47:34 +00003939 chunk[i]=(unsigned char) ReadBlobByte(image);
glennrp0fe50b42010-11-16 03:52:51 +00003940
cristy3ed852e2009-09-05 21:47:34 +00003941 p=chunk;
3942 }
glennrp47b9dd52010-11-24 18:12:06 +00003943
cristy3ed852e2009-09-05 21:47:34 +00003944 (void) ReadBlobMSBLong(image); /* read crc word */
3945
3946 if (skip_to_iend)
3947 {
3948 if (length)
3949 chunk=(unsigned char *) RelinquishMagickMemory(chunk);
glennrp47b9dd52010-11-24 18:12:06 +00003950
cristy3ed852e2009-09-05 21:47:34 +00003951 continue;
3952 }
3953
3954 if (memcmp(type,mng_JHDR,4) == 0)
3955 {
3956 if (length == 16)
3957 {
cristybb503372010-05-27 20:51:26 +00003958 jng_width=(size_t) ((p[0] << 24) | (p[1] << 16) |
cristy3ed852e2009-09-05 21:47:34 +00003959 (p[2] << 8) | p[3]);
cristybb503372010-05-27 20:51:26 +00003960 jng_height=(size_t) ((p[4] << 24) | (p[5] << 16) |
cristy3ed852e2009-09-05 21:47:34 +00003961 (p[6] << 8) | p[7]);
3962 jng_color_type=p[8];
3963 jng_image_sample_depth=p[9];
3964 jng_image_compression_method=p[10];
3965 jng_image_interlace_method=p[11];
glennrp47b9dd52010-11-24 18:12:06 +00003966
cristy3ed852e2009-09-05 21:47:34 +00003967 image->interlace=jng_image_interlace_method != 0 ? PNGInterlace :
3968 NoInterlace;
glennrp47b9dd52010-11-24 18:12:06 +00003969
cristy3ed852e2009-09-05 21:47:34 +00003970 jng_alpha_sample_depth=p[12];
3971 jng_alpha_compression_method=p[13];
3972 jng_alpha_filter_method=p[14];
3973 jng_alpha_interlace_method=p[15];
glennrp47b9dd52010-11-24 18:12:06 +00003974
cristy3ed852e2009-09-05 21:47:34 +00003975 if (logging != MagickFalse)
3976 {
3977 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristyf2faecf2010-05-28 19:19:36 +00003978 " jng_width: %16lu",(unsigned long) jng_width);
glennrp47b9dd52010-11-24 18:12:06 +00003979
cristy3ed852e2009-09-05 21:47:34 +00003980 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristyf2faecf2010-05-28 19:19:36 +00003981 " jng_width: %16lu",(unsigned long) jng_height);
glennrp47b9dd52010-11-24 18:12:06 +00003982
cristy3ed852e2009-09-05 21:47:34 +00003983 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
3984 " jng_color_type: %16d",jng_color_type);
glennrp47b9dd52010-11-24 18:12:06 +00003985
cristy3ed852e2009-09-05 21:47:34 +00003986 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
3987 " jng_image_sample_depth: %3d",
3988 jng_image_sample_depth);
glennrp47b9dd52010-11-24 18:12:06 +00003989
cristy3ed852e2009-09-05 21:47:34 +00003990 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
3991 " jng_image_compression_method:%3d",
3992 jng_image_compression_method);
glennrp47b9dd52010-11-24 18:12:06 +00003993
cristy3ed852e2009-09-05 21:47:34 +00003994 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
3995 " jng_image_interlace_method: %3d",
3996 jng_image_interlace_method);
glennrp47b9dd52010-11-24 18:12:06 +00003997
cristy3ed852e2009-09-05 21:47:34 +00003998 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
3999 " jng_alpha_sample_depth: %3d",
4000 jng_alpha_sample_depth);
glennrp47b9dd52010-11-24 18:12:06 +00004001
cristy3ed852e2009-09-05 21:47:34 +00004002 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
4003 " jng_alpha_compression_method:%3d",
4004 jng_alpha_compression_method);
glennrp47b9dd52010-11-24 18:12:06 +00004005
cristy3ed852e2009-09-05 21:47:34 +00004006 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
4007 " jng_alpha_filter_method: %3d",
4008 jng_alpha_filter_method);
glennrp47b9dd52010-11-24 18:12:06 +00004009
cristy3ed852e2009-09-05 21:47:34 +00004010 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
4011 " jng_alpha_interlace_method: %3d",
4012 jng_alpha_interlace_method);
4013 }
4014 }
glennrp47b9dd52010-11-24 18:12:06 +00004015
cristy3ed852e2009-09-05 21:47:34 +00004016 if (length)
4017 chunk=(unsigned char *) RelinquishMagickMemory(chunk);
glennrp47b9dd52010-11-24 18:12:06 +00004018
cristy3ed852e2009-09-05 21:47:34 +00004019 continue;
4020 }
4021
4022
4023 if ((reading_idat == MagickFalse) && (read_JSEP == MagickFalse) &&
4024 ((memcmp(type,mng_JDAT,4) == 0) || (memcmp(type,mng_JdAA,4) == 0) ||
4025 (memcmp(type,mng_IDAT,4) == 0) || (memcmp(type,mng_JDAA,4) == 0)))
4026 {
4027 /*
4028 o create color_image
4029 o open color_blob, attached to color_image
4030 o if (color type has alpha)
4031 open alpha_blob, attached to alpha_image
4032 */
4033
cristy73bd4a52010-10-05 11:24:23 +00004034 color_image_info=(ImageInfo *)AcquireMagickMemory(sizeof(ImageInfo));
glennrp47b9dd52010-11-24 18:12:06 +00004035
cristy3ed852e2009-09-05 21:47:34 +00004036 if (color_image_info == (ImageInfo *) NULL)
4037 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
glennrp47b9dd52010-11-24 18:12:06 +00004038
cristy3ed852e2009-09-05 21:47:34 +00004039 GetImageInfo(color_image_info);
cristy9950d572011-10-01 18:22:35 +00004040 color_image=AcquireImage(color_image_info,exception);
glennrp0fe50b42010-11-16 03:52:51 +00004041
cristy3ed852e2009-09-05 21:47:34 +00004042 if (color_image == (Image *) NULL)
4043 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
4044
4045 if (logging != MagickFalse)
4046 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
4047 " Creating color_blob.");
glennrp0fe50b42010-11-16 03:52:51 +00004048
cristy3ed852e2009-09-05 21:47:34 +00004049 (void) AcquireUniqueFilename(color_image->filename);
4050 status=OpenBlob(color_image_info,color_image,WriteBinaryBlobMode,
4051 exception);
glennrp0fe50b42010-11-16 03:52:51 +00004052
cristy3ed852e2009-09-05 21:47:34 +00004053 if (status == MagickFalse)
4054 return((Image *) NULL);
4055
4056 if ((image_info->ping == MagickFalse) && (jng_color_type >= 12))
4057 {
4058 alpha_image_info=(ImageInfo *)
cristy73bd4a52010-10-05 11:24:23 +00004059 AcquireMagickMemory(sizeof(ImageInfo));
glennrp0fe50b42010-11-16 03:52:51 +00004060
cristy3ed852e2009-09-05 21:47:34 +00004061 if (alpha_image_info == (ImageInfo *) NULL)
4062 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
glennrp0fe50b42010-11-16 03:52:51 +00004063
cristy3ed852e2009-09-05 21:47:34 +00004064 GetImageInfo(alpha_image_info);
cristy9950d572011-10-01 18:22:35 +00004065 alpha_image=AcquireImage(alpha_image_info,exception);
glennrp0fe50b42010-11-16 03:52:51 +00004066
cristy3ed852e2009-09-05 21:47:34 +00004067 if (alpha_image == (Image *) NULL)
4068 {
4069 alpha_image=DestroyImage(alpha_image);
4070 ThrowReaderException(ResourceLimitError,
4071 "MemoryAllocationFailed");
4072 }
glennrp0fe50b42010-11-16 03:52:51 +00004073
cristy3ed852e2009-09-05 21:47:34 +00004074 if (logging != MagickFalse)
4075 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
4076 " Creating alpha_blob.");
glennrp0fe50b42010-11-16 03:52:51 +00004077
cristy3ed852e2009-09-05 21:47:34 +00004078 (void) AcquireUniqueFilename(alpha_image->filename);
4079 status=OpenBlob(alpha_image_info,alpha_image,WriteBinaryBlobMode,
4080 exception);
glennrp0fe50b42010-11-16 03:52:51 +00004081
cristy3ed852e2009-09-05 21:47:34 +00004082 if (status == MagickFalse)
4083 return((Image *) NULL);
glennrp0fe50b42010-11-16 03:52:51 +00004084
cristy3ed852e2009-09-05 21:47:34 +00004085 if (jng_alpha_compression_method == 0)
4086 {
4087 unsigned char
4088 data[18];
4089
4090 if (logging != MagickFalse)
4091 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
4092 " Writing IHDR chunk to alpha_blob.");
glennrp0fe50b42010-11-16 03:52:51 +00004093
cristy3ed852e2009-09-05 21:47:34 +00004094 (void) WriteBlob(alpha_image,8,(const unsigned char *)
4095 "\211PNG\r\n\032\n");
glennrp0fe50b42010-11-16 03:52:51 +00004096
cristy3ed852e2009-09-05 21:47:34 +00004097 (void) WriteBlobMSBULong(alpha_image,13L);
4098 PNGType(data,mng_IHDR);
glennrp03812ae2010-12-24 01:31:34 +00004099 LogPNGChunk(logging,mng_IHDR,13L);
cristy3ed852e2009-09-05 21:47:34 +00004100 PNGLong(data+4,jng_width);
4101 PNGLong(data+8,jng_height);
4102 data[12]=jng_alpha_sample_depth;
4103 data[13]=0; /* color_type gray */
4104 data[14]=0; /* compression method 0 */
4105 data[15]=0; /* filter_method 0 */
4106 data[16]=0; /* interlace_method 0 */
4107 (void) WriteBlob(alpha_image,17,data);
4108 (void) WriteBlobMSBULong(alpha_image,crc32(0,data,17));
4109 }
4110 }
4111 reading_idat=MagickTrue;
4112 }
4113
4114 if (memcmp(type,mng_JDAT,4) == 0)
4115 {
glennrp47b9dd52010-11-24 18:12:06 +00004116 /* Copy chunk to color_image->blob */
cristy3ed852e2009-09-05 21:47:34 +00004117
4118 if (logging != MagickFalse)
4119 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
4120 " Copying JDAT chunk data to color_blob.");
4121
4122 (void) WriteBlob(color_image,length,chunk);
glennrp47b9dd52010-11-24 18:12:06 +00004123
cristy3ed852e2009-09-05 21:47:34 +00004124 if (length)
4125 chunk=(unsigned char *) RelinquishMagickMemory(chunk);
glennrp47b9dd52010-11-24 18:12:06 +00004126
cristy3ed852e2009-09-05 21:47:34 +00004127 continue;
4128 }
4129
4130 if (memcmp(type,mng_IDAT,4) == 0)
4131 {
4132 png_byte
4133 data[5];
4134
glennrp47b9dd52010-11-24 18:12:06 +00004135 /* Copy IDAT header and chunk data to alpha_image->blob */
cristy3ed852e2009-09-05 21:47:34 +00004136
4137 if (image_info->ping == MagickFalse)
4138 {
4139 if (logging != MagickFalse)
4140 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
4141 " Copying IDAT chunk data to alpha_blob.");
4142
cristybb503372010-05-27 20:51:26 +00004143 (void) WriteBlobMSBULong(alpha_image,(size_t) length);
cristy3ed852e2009-09-05 21:47:34 +00004144 PNGType(data,mng_IDAT);
glennrp03812ae2010-12-24 01:31:34 +00004145 LogPNGChunk(logging,mng_IDAT,length);
cristy3ed852e2009-09-05 21:47:34 +00004146 (void) WriteBlob(alpha_image,4,data);
4147 (void) WriteBlob(alpha_image,length,chunk);
4148 (void) WriteBlobMSBULong(alpha_image,
4149 crc32(crc32(0,data,4),chunk,(uInt) length));
4150 }
glennrp0fe50b42010-11-16 03:52:51 +00004151
cristy3ed852e2009-09-05 21:47:34 +00004152 if (length)
4153 chunk=(unsigned char *) RelinquishMagickMemory(chunk);
glennrp0fe50b42010-11-16 03:52:51 +00004154
cristy3ed852e2009-09-05 21:47:34 +00004155 continue;
4156 }
4157
4158 if ((memcmp(type,mng_JDAA,4) == 0) || (memcmp(type,mng_JdAA,4) == 0))
4159 {
glennrp47b9dd52010-11-24 18:12:06 +00004160 /* Copy chunk data to alpha_image->blob */
cristy3ed852e2009-09-05 21:47:34 +00004161
4162 if (image_info->ping == MagickFalse)
4163 {
4164 if (logging != MagickFalse)
4165 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
4166 " Copying JDAA chunk data to alpha_blob.");
4167
4168 (void) WriteBlob(alpha_image,length,chunk);
4169 }
glennrp0fe50b42010-11-16 03:52:51 +00004170
cristy3ed852e2009-09-05 21:47:34 +00004171 if (length)
4172 chunk=(unsigned char *) RelinquishMagickMemory(chunk);
glennrp0fe50b42010-11-16 03:52:51 +00004173
cristy3ed852e2009-09-05 21:47:34 +00004174 continue;
4175 }
4176
4177 if (memcmp(type,mng_JSEP,4) == 0)
4178 {
4179 read_JSEP=MagickTrue;
glennrp0fe50b42010-11-16 03:52:51 +00004180
cristy3ed852e2009-09-05 21:47:34 +00004181 if (length)
4182 chunk=(unsigned char *) RelinquishMagickMemory(chunk);
glennrp0fe50b42010-11-16 03:52:51 +00004183
cristy3ed852e2009-09-05 21:47:34 +00004184 continue;
4185 }
4186
4187 if (memcmp(type,mng_bKGD,4) == 0)
4188 {
4189 if (length == 2)
4190 {
4191 image->background_color.red=ScaleCharToQuantum(p[1]);
4192 image->background_color.green=image->background_color.red;
4193 image->background_color.blue=image->background_color.red;
4194 }
glennrp0fe50b42010-11-16 03:52:51 +00004195
cristy3ed852e2009-09-05 21:47:34 +00004196 if (length == 6)
4197 {
4198 image->background_color.red=ScaleCharToQuantum(p[1]);
4199 image->background_color.green=ScaleCharToQuantum(p[3]);
4200 image->background_color.blue=ScaleCharToQuantum(p[5]);
4201 }
glennrp0fe50b42010-11-16 03:52:51 +00004202
cristy3ed852e2009-09-05 21:47:34 +00004203 chunk=(unsigned char *) RelinquishMagickMemory(chunk);
4204 continue;
4205 }
4206
4207 if (memcmp(type,mng_gAMA,4) == 0)
4208 {
4209 if (length == 4)
cristy8182b072010-05-30 20:10:53 +00004210 image->gamma=((float) mng_get_long(p))*0.00001;
glennrp0fe50b42010-11-16 03:52:51 +00004211
cristy3ed852e2009-09-05 21:47:34 +00004212 chunk=(unsigned char *) RelinquishMagickMemory(chunk);
4213 continue;
4214 }
4215
4216 if (memcmp(type,mng_cHRM,4) == 0)
4217 {
4218 if (length == 32)
4219 {
cristy8182b072010-05-30 20:10:53 +00004220 image->chromaticity.white_point.x=0.00001*mng_get_long(p);
4221 image->chromaticity.white_point.y=0.00001*mng_get_long(&p[4]);
4222 image->chromaticity.red_primary.x=0.00001*mng_get_long(&p[8]);
4223 image->chromaticity.red_primary.y=0.00001*mng_get_long(&p[12]);
4224 image->chromaticity.green_primary.x=0.00001*mng_get_long(&p[16]);
4225 image->chromaticity.green_primary.y=0.00001*mng_get_long(&p[20]);
4226 image->chromaticity.blue_primary.x=0.00001*mng_get_long(&p[24]);
4227 image->chromaticity.blue_primary.y=0.00001*mng_get_long(&p[28]);
cristy3ed852e2009-09-05 21:47:34 +00004228 }
glennrp47b9dd52010-11-24 18:12:06 +00004229
cristy3ed852e2009-09-05 21:47:34 +00004230 chunk=(unsigned char *) RelinquishMagickMemory(chunk);
4231 continue;
4232 }
4233
4234 if (memcmp(type,mng_sRGB,4) == 0)
4235 {
4236 if (length == 1)
4237 {
glennrpe610a072010-08-05 17:08:46 +00004238 image->rendering_intent=
glennrpcf002022011-01-30 02:38:15 +00004239 Magick_RenderingIntent_from_PNG_RenderingIntent(p[0]);
cristy3ed852e2009-09-05 21:47:34 +00004240 image->gamma=0.45455f;
4241 image->chromaticity.red_primary.x=0.6400f;
4242 image->chromaticity.red_primary.y=0.3300f;
4243 image->chromaticity.green_primary.x=0.3000f;
4244 image->chromaticity.green_primary.y=0.6000f;
4245 image->chromaticity.blue_primary.x=0.1500f;
4246 image->chromaticity.blue_primary.y=0.0600f;
4247 image->chromaticity.white_point.x=0.3127f;
4248 image->chromaticity.white_point.y=0.3290f;
4249 }
glennrp47b9dd52010-11-24 18:12:06 +00004250
cristy3ed852e2009-09-05 21:47:34 +00004251 chunk=(unsigned char *) RelinquishMagickMemory(chunk);
4252 continue;
4253 }
4254
4255 if (memcmp(type,mng_oFFs,4) == 0)
4256 {
4257 if (length > 8)
4258 {
glennrp5eae7602011-02-22 15:21:32 +00004259 image->page.x=(ssize_t) mng_get_long(p);
4260 image->page.y=(ssize_t) mng_get_long(&p[4]);
glennrp0fe50b42010-11-16 03:52:51 +00004261
cristy3ed852e2009-09-05 21:47:34 +00004262 if ((int) p[8] != 0)
4263 {
4264 image->page.x/=10000;
4265 image->page.y/=10000;
4266 }
4267 }
glennrp47b9dd52010-11-24 18:12:06 +00004268
cristy3ed852e2009-09-05 21:47:34 +00004269 if (length)
4270 chunk=(unsigned char *) RelinquishMagickMemory(chunk);
glennrp0fe50b42010-11-16 03:52:51 +00004271
cristy3ed852e2009-09-05 21:47:34 +00004272 continue;
4273 }
4274
4275 if (memcmp(type,mng_pHYs,4) == 0)
4276 {
4277 if (length > 8)
4278 {
cristy2a11bef2011-10-28 18:33:11 +00004279 image->resolution.x=(double) mng_get_long(p);
4280 image->resolution.y=(double) mng_get_long(&p[4]);
cristy3ed852e2009-09-05 21:47:34 +00004281 if ((int) p[8] == PNG_RESOLUTION_METER)
4282 {
4283 image->units=PixelsPerCentimeterResolution;
cristy2a11bef2011-10-28 18:33:11 +00004284 image->resolution.x=image->resolution.x/100.0f;
4285 image->resolution.y=image->resolution.y/100.0f;
cristy3ed852e2009-09-05 21:47:34 +00004286 }
4287 }
glennrp0fe50b42010-11-16 03:52:51 +00004288
cristy3ed852e2009-09-05 21:47:34 +00004289 chunk=(unsigned char *) RelinquishMagickMemory(chunk);
4290 continue;
4291 }
4292
4293#if 0
4294 if (memcmp(type,mng_iCCP,4) == 0)
4295 {
glennrpfd05d622011-02-25 04:10:33 +00004296 /* To do: */
cristy3ed852e2009-09-05 21:47:34 +00004297 if (length)
4298 chunk=(unsigned char *) RelinquishMagickMemory(chunk);
glennrp0fe50b42010-11-16 03:52:51 +00004299
cristy3ed852e2009-09-05 21:47:34 +00004300 continue;
4301 }
4302#endif
4303
4304 if (length)
4305 chunk=(unsigned char *) RelinquishMagickMemory(chunk);
4306
4307 if (memcmp(type,mng_IEND,4))
4308 continue;
glennrp0fe50b42010-11-16 03:52:51 +00004309
cristy3ed852e2009-09-05 21:47:34 +00004310 break;
4311 }
4312
4313
4314 /* IEND found */
4315
4316 /*
4317 Finish up reading image data:
4318
4319 o read main image from color_blob.
4320
4321 o close color_blob.
4322
4323 o if (color_type has alpha)
4324 if alpha_encoding is PNG
4325 read secondary image from alpha_blob via ReadPNG
4326 if alpha_encoding is JPEG
4327 read secondary image from alpha_blob via ReadJPEG
4328
4329 o close alpha_blob.
4330
4331 o copy intensity of secondary image into
cristy4c08aed2011-07-01 19:47:50 +00004332 alpha samples of main image.
cristy3ed852e2009-09-05 21:47:34 +00004333
4334 o destroy the secondary image.
4335 */
4336
4337 (void) CloseBlob(color_image);
glennrp47b9dd52010-11-24 18:12:06 +00004338
cristy3ed852e2009-09-05 21:47:34 +00004339 if (logging != MagickFalse)
4340 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
4341 " Reading jng_image from color_blob.");
glennrp0fe50b42010-11-16 03:52:51 +00004342
cristy3b6fd2e2011-05-20 12:53:50 +00004343 (void) FormatLocaleString(color_image_info->filename,MaxTextExtent,"%s",
cristy3ed852e2009-09-05 21:47:34 +00004344 color_image->filename);
glennrp0fe50b42010-11-16 03:52:51 +00004345
cristy3ed852e2009-09-05 21:47:34 +00004346 color_image_info->ping=MagickFalse; /* To do: avoid this */
4347 jng_image=ReadImage(color_image_info,exception);
glennrp0fe50b42010-11-16 03:52:51 +00004348
cristy3ed852e2009-09-05 21:47:34 +00004349 if (jng_image == (Image *) NULL)
4350 return((Image *) NULL);
4351
4352 (void) RelinquishUniqueFileResource(color_image->filename);
4353 color_image=DestroyImage(color_image);
4354 color_image_info=DestroyImageInfo(color_image_info);
4355
4356 if (jng_image == (Image *) NULL)
4357 return((Image *) NULL);
4358
4359 if (logging != MagickFalse)
4360 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
4361 " Copying jng_image pixels to main image.");
glennrp0fe50b42010-11-16 03:52:51 +00004362
cristy3ed852e2009-09-05 21:47:34 +00004363 image->rows=jng_height;
4364 image->columns=jng_width;
glennrp0fe50b42010-11-16 03:52:51 +00004365
cristybb503372010-05-27 20:51:26 +00004366 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004367 {
cristyc82a27b2011-10-21 01:07:16 +00004368 s=GetVirtualPixels(jng_image,0,y,image->columns,1,exception);
cristy3ed852e2009-09-05 21:47:34 +00004369 q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00004370 for (x=(ssize_t) image->columns; x != 0; x--)
4371 {
4372 SetPixelRed(image,GetPixelRed(jng_image,s),q);
4373 SetPixelGreen(image,GetPixelGreen(jng_image,s),q);
4374 SetPixelBlue(image,GetPixelBlue(jng_image,s),q);
cristyed231572011-07-14 02:18:59 +00004375 q+=GetPixelChannels(image);
4376 s+=GetPixelChannels(jng_image);
cristy4c08aed2011-07-01 19:47:50 +00004377 }
glennrp47b9dd52010-11-24 18:12:06 +00004378
cristy3ed852e2009-09-05 21:47:34 +00004379 if (SyncAuthenticPixels(image,exception) == MagickFalse)
4380 break;
4381 }
glennrp0fe50b42010-11-16 03:52:51 +00004382
cristy3ed852e2009-09-05 21:47:34 +00004383 jng_image=DestroyImage(jng_image);
glennrp0fe50b42010-11-16 03:52:51 +00004384
cristy3ed852e2009-09-05 21:47:34 +00004385 if (image_info->ping == MagickFalse)
4386 {
4387 if (jng_color_type >= 12)
4388 {
4389 if (jng_alpha_compression_method == 0)
4390 {
4391 png_byte
4392 data[5];
4393 (void) WriteBlobMSBULong(alpha_image,0x00000000L);
4394 PNGType(data,mng_IEND);
glennrp03812ae2010-12-24 01:31:34 +00004395 LogPNGChunk(logging,mng_IEND,0L);
cristy3ed852e2009-09-05 21:47:34 +00004396 (void) WriteBlob(alpha_image,4,data);
4397 (void) WriteBlobMSBULong(alpha_image,crc32(0,data,4));
4398 }
glennrp0fe50b42010-11-16 03:52:51 +00004399
cristy3ed852e2009-09-05 21:47:34 +00004400 (void) CloseBlob(alpha_image);
glennrp0fe50b42010-11-16 03:52:51 +00004401
cristy3ed852e2009-09-05 21:47:34 +00004402 if (logging != MagickFalse)
4403 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristy4c08aed2011-07-01 19:47:50 +00004404 " Reading alpha from alpha_blob.");
cristy3ed852e2009-09-05 21:47:34 +00004405
cristy3b6fd2e2011-05-20 12:53:50 +00004406 (void) FormatLocaleString(alpha_image_info->filename,MaxTextExtent,
cristy3ed852e2009-09-05 21:47:34 +00004407 "%s",alpha_image->filename);
4408
4409 jng_image=ReadImage(alpha_image_info,exception);
glennrp0fe50b42010-11-16 03:52:51 +00004410
cristy3ed852e2009-09-05 21:47:34 +00004411 if (jng_image != (Image *) NULL)
cristybb503372010-05-27 20:51:26 +00004412 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00004413 {
4414 s=GetVirtualPixels(jng_image,0,y,image->columns,1,
cristyc82a27b2011-10-21 01:07:16 +00004415 exception);
cristy3ed852e2009-09-05 21:47:34 +00004416 q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
glennrp47b9dd52010-11-24 18:12:06 +00004417
cristy3ed852e2009-09-05 21:47:34 +00004418 if (image->matte != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +00004419 for (x=(ssize_t) image->columns; x != 0; x--)
4420 {
4421 SetPixelAlpha(image,GetPixelRed(jng_image,s),q);
cristyed231572011-07-14 02:18:59 +00004422 q+=GetPixelChannels(image);
4423 s+=GetPixelChannels(jng_image);
cristy4c08aed2011-07-01 19:47:50 +00004424 }
glennrp0fe50b42010-11-16 03:52:51 +00004425
cristy3ed852e2009-09-05 21:47:34 +00004426 else
cristy4c08aed2011-07-01 19:47:50 +00004427 for (x=(ssize_t) image->columns; x != 0; x--)
cristy3ed852e2009-09-05 21:47:34 +00004428 {
cristy4c08aed2011-07-01 19:47:50 +00004429 SetPixelAlpha(image,GetPixelRed(jng_image,s),q);
4430 if (GetPixelAlpha(image,q) != OpaqueAlpha)
cristy3ed852e2009-09-05 21:47:34 +00004431 image->matte=MagickTrue;
cristyed231572011-07-14 02:18:59 +00004432 q+=GetPixelChannels(image);
4433 s+=GetPixelChannels(jng_image);
cristy3ed852e2009-09-05 21:47:34 +00004434 }
glennrp0fe50b42010-11-16 03:52:51 +00004435
cristy3ed852e2009-09-05 21:47:34 +00004436 if (SyncAuthenticPixels(image,exception) == MagickFalse)
4437 break;
4438 }
4439 (void) RelinquishUniqueFileResource(alpha_image->filename);
4440 alpha_image=DestroyImage(alpha_image);
4441 alpha_image_info=DestroyImageInfo(alpha_image_info);
4442 if (jng_image != (Image *) NULL)
4443 jng_image=DestroyImage(jng_image);
4444 }
4445 }
4446
glennrp47b9dd52010-11-24 18:12:06 +00004447 /* Read the JNG image. */
4448
cristy3ed852e2009-09-05 21:47:34 +00004449 if (mng_info->mng_type == 0)
4450 {
4451 mng_info->mng_width=jng_width;
4452 mng_info->mng_height=jng_height;
4453 }
glennrp0fe50b42010-11-16 03:52:51 +00004454
cristy3ed852e2009-09-05 21:47:34 +00004455 if (image->page.width == 0 && image->page.height == 0)
glennrp0fe50b42010-11-16 03:52:51 +00004456 {
4457 image->page.width=jng_width;
4458 image->page.height=jng_height;
4459 }
4460
cristy3ed852e2009-09-05 21:47:34 +00004461 if (image->page.x == 0 && image->page.y == 0)
glennrp0fe50b42010-11-16 03:52:51 +00004462 {
4463 image->page.x=mng_info->x_off[mng_info->object_id];
4464 image->page.y=mng_info->y_off[mng_info->object_id];
4465 }
4466
cristy3ed852e2009-09-05 21:47:34 +00004467 else
glennrp0fe50b42010-11-16 03:52:51 +00004468 {
4469 image->page.y=mng_info->y_off[mng_info->object_id];
4470 }
4471
cristy3ed852e2009-09-05 21:47:34 +00004472 mng_info->image_found++;
4473 status=SetImageProgress(image,LoadImagesTag,2*TellBlob(image),
4474 2*GetBlobSize(image));
glennrp0fe50b42010-11-16 03:52:51 +00004475
cristy3ed852e2009-09-05 21:47:34 +00004476 if (logging != MagickFalse)
4477 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
4478 " exit ReadOneJNGImage()");
glennrp0fe50b42010-11-16 03:52:51 +00004479
cristy3ed852e2009-09-05 21:47:34 +00004480 return(image);
4481}
4482
4483/*
4484%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4485% %
4486% %
4487% %
4488% R e a d J N G I m a g e %
4489% %
4490% %
4491% %
4492%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4493%
4494% ReadJNGImage() reads a JPEG Network Graphics (JNG) image file
4495% (including the 8-byte signature) and returns it. It allocates the memory
4496% necessary for the new Image structure and returns a pointer to the new
4497% image.
4498%
4499% JNG support written by Glenn Randers-Pehrson, glennrp@image...
4500%
4501% The format of the ReadJNGImage method is:
4502%
4503% Image *ReadJNGImage(const ImageInfo *image_info, ExceptionInfo
4504% *exception)
4505%
4506% A description of each parameter follows:
4507%
4508% o image_info: the image info.
4509%
4510% o exception: return any errors or warnings in this structure.
4511%
4512*/
4513
4514static Image *ReadJNGImage(const ImageInfo *image_info,ExceptionInfo *exception)
4515{
4516 Image
4517 *image,
4518 *previous;
4519
4520 MagickBooleanType
glennrp21f0e622011-01-07 16:20:57 +00004521 have_mng_structure,
4522 logging,
cristy3ed852e2009-09-05 21:47:34 +00004523 status;
4524
4525 MngInfo
4526 *mng_info;
4527
4528 char
4529 magic_number[MaxTextExtent];
4530
cristy3ed852e2009-09-05 21:47:34 +00004531 size_t
4532 count;
4533
4534 /*
4535 Open image file.
4536 */
4537 assert(image_info != (const ImageInfo *) NULL);
4538 assert(image_info->signature == MagickSignature);
4539 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image_info->filename);
4540 assert(exception != (ExceptionInfo *) NULL);
4541 assert(exception->signature == MagickSignature);
glennrpfd05d622011-02-25 04:10:33 +00004542 logging=LogMagickEvent(CoderEvent,GetMagickModule(),"Enter ReadJNGImage()");
cristy9950d572011-10-01 18:22:35 +00004543 image=AcquireImage(image_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00004544 mng_info=(MngInfo *) NULL;
4545 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
glennrp0fe50b42010-11-16 03:52:51 +00004546
cristy3ed852e2009-09-05 21:47:34 +00004547 if (status == MagickFalse)
4548 return((Image *) NULL);
glennrp0fe50b42010-11-16 03:52:51 +00004549
cristy3ed852e2009-09-05 21:47:34 +00004550 if (LocaleCompare(image_info->magick,"JNG") != 0)
4551 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
glennrp0fe50b42010-11-16 03:52:51 +00004552
glennrp47b9dd52010-11-24 18:12:06 +00004553 /* Verify JNG signature. */
4554
cristy3ed852e2009-09-05 21:47:34 +00004555 count=(size_t) ReadBlob(image,8,(unsigned char *) magic_number);
glennrp47b9dd52010-11-24 18:12:06 +00004556
glennrp3b8763e2011-02-21 12:08:18 +00004557 if (count < 8 || memcmp(magic_number,"\213JNG\r\n\032\n",8) != 0)
cristy3ed852e2009-09-05 21:47:34 +00004558 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
glennrp0fe50b42010-11-16 03:52:51 +00004559
glennrp47b9dd52010-11-24 18:12:06 +00004560 /* Allocate a MngInfo structure. */
4561
cristy3ed852e2009-09-05 21:47:34 +00004562 have_mng_structure=MagickFalse;
cristy73bd4a52010-10-05 11:24:23 +00004563 mng_info=(MngInfo *) AcquireMagickMemory(sizeof(*mng_info));
glennrp0fe50b42010-11-16 03:52:51 +00004564
cristy3ed852e2009-09-05 21:47:34 +00004565 if (mng_info == (MngInfo *) NULL)
4566 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
glennrp0fe50b42010-11-16 03:52:51 +00004567
glennrp47b9dd52010-11-24 18:12:06 +00004568 /* Initialize members of the MngInfo structure. */
4569
cristy3ed852e2009-09-05 21:47:34 +00004570 (void) ResetMagickMemory(mng_info,0,sizeof(MngInfo));
4571 have_mng_structure=MagickTrue;
4572
4573 mng_info->image=image;
4574 previous=image;
4575 image=ReadOneJNGImage(mng_info,image_info,exception);
4576 MngInfoFreeStruct(mng_info,&have_mng_structure);
glennrp0fe50b42010-11-16 03:52:51 +00004577
cristy3ed852e2009-09-05 21:47:34 +00004578 if (image == (Image *) NULL)
4579 {
4580 if (IsImageObject(previous) != MagickFalse)
4581 {
4582 (void) CloseBlob(previous);
4583 (void) DestroyImageList(previous);
4584 }
glennrp0fe50b42010-11-16 03:52:51 +00004585
cristy3ed852e2009-09-05 21:47:34 +00004586 if (logging != MagickFalse)
4587 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
4588 "exit ReadJNGImage() with error");
glennrp0fe50b42010-11-16 03:52:51 +00004589
cristy3ed852e2009-09-05 21:47:34 +00004590 return((Image *) NULL);
4591 }
4592 (void) CloseBlob(image);
glennrp47b9dd52010-11-24 18:12:06 +00004593
cristy3ed852e2009-09-05 21:47:34 +00004594 if (image->columns == 0 || image->rows == 0)
4595 {
4596 if (logging != MagickFalse)
4597 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
4598 "exit ReadJNGImage() with error");
glennrp0fe50b42010-11-16 03:52:51 +00004599
cristy3ed852e2009-09-05 21:47:34 +00004600 ThrowReaderException(CorruptImageError,"CorruptImage");
4601 }
glennrp0fe50b42010-11-16 03:52:51 +00004602
cristy3ed852e2009-09-05 21:47:34 +00004603 if (logging != MagickFalse)
4604 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"exit ReadJNGImage()");
glennrp0fe50b42010-11-16 03:52:51 +00004605
cristy3ed852e2009-09-05 21:47:34 +00004606 return(image);
4607}
4608#endif
4609
4610static Image *ReadMNGImage(const ImageInfo *image_info,ExceptionInfo *exception)
4611{
4612 char
4613 page_geometry[MaxTextExtent];
4614
4615 Image
4616 *image,
4617 *previous;
4618
cristy4383ec82011-01-05 15:42:32 +00004619 MagickBooleanType
glennrp21f0e622011-01-07 16:20:57 +00004620 logging,
4621 have_mng_structure;
cristy4383ec82011-01-05 15:42:32 +00004622
cristy3ed852e2009-09-05 21:47:34 +00004623 volatile int
4624 first_mng_object,
cristy3ed852e2009-09-05 21:47:34 +00004625 object_id,
4626 term_chunk_found,
4627 skip_to_iend;
4628
cristybb503372010-05-27 20:51:26 +00004629 volatile ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004630 image_count=0;
4631
4632 MagickBooleanType
4633 status;
4634
4635 MagickOffsetType
4636 offset;
4637
4638 MngInfo
4639 *mng_info;
4640
4641 MngBox
4642 default_fb,
4643 fb,
4644 previous_fb;
4645
4646#if defined(MNG_INSERT_LAYERS)
cristy101ab702011-10-13 13:06:32 +00004647 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00004648 mng_background_color;
4649#endif
4650
4651 register unsigned char
4652 *p;
4653
cristybb503372010-05-27 20:51:26 +00004654 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004655 i;
4656
4657 size_t
4658 count;
4659
cristybb503372010-05-27 20:51:26 +00004660 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00004661 loop_level;
4662
4663 volatile short
4664 skipping_loop;
4665
4666#if defined(MNG_INSERT_LAYERS)
4667 unsigned int
4668 mandatory_back=0;
4669#endif
4670
4671 volatile unsigned int
4672#ifdef MNG_OBJECT_BUFFERS
4673 mng_background_object=0,
4674#endif
4675 mng_type=0; /* 0: PNG or JNG; 1: MNG; 2: MNG-LC; 3: MNG-VLC */
4676
cristybb503372010-05-27 20:51:26 +00004677 size_t
cristy3ed852e2009-09-05 21:47:34 +00004678 default_frame_timeout,
4679 frame_timeout,
4680#if defined(MNG_INSERT_LAYERS)
4681 image_height,
4682 image_width,
4683#endif
4684 length;
4685
glennrp38ea0832010-06-02 18:50:28 +00004686 /* These delays are all measured in image ticks_per_second,
4687 * not in MNG ticks_per_second
4688 */
cristybb503372010-05-27 20:51:26 +00004689 volatile size_t
cristy3ed852e2009-09-05 21:47:34 +00004690 default_frame_delay,
4691 final_delay,
4692 final_image_delay,
4693 frame_delay,
4694#if defined(MNG_INSERT_LAYERS)
4695 insert_layers,
4696#endif
4697 mng_iterations=1,
4698 simplicity=0,
4699 subframe_height=0,
4700 subframe_width=0;
4701
4702 previous_fb.top=0;
4703 previous_fb.bottom=0;
4704 previous_fb.left=0;
4705 previous_fb.right=0;
4706 default_fb.top=0;
4707 default_fb.bottom=0;
4708 default_fb.left=0;
4709 default_fb.right=0;
4710
glennrp47b9dd52010-11-24 18:12:06 +00004711 /* Open image file. */
4712
cristy3ed852e2009-09-05 21:47:34 +00004713 assert(image_info != (const ImageInfo *) NULL);
4714 assert(image_info->signature == MagickSignature);
4715 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image_info->filename);
4716 assert(exception != (ExceptionInfo *) NULL);
4717 assert(exception->signature == MagickSignature);
glennrpfd05d622011-02-25 04:10:33 +00004718 logging=LogMagickEvent(CoderEvent,GetMagickModule(),"Enter ReadMNGImage()");
cristy9950d572011-10-01 18:22:35 +00004719 image=AcquireImage(image_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00004720 mng_info=(MngInfo *) NULL;
4721 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
glennrp0fe50b42010-11-16 03:52:51 +00004722
cristy3ed852e2009-09-05 21:47:34 +00004723 if (status == MagickFalse)
4724 return((Image *) NULL);
glennrp0fe50b42010-11-16 03:52:51 +00004725
cristy3ed852e2009-09-05 21:47:34 +00004726 first_mng_object=MagickFalse;
4727 skipping_loop=(-1);
4728 have_mng_structure=MagickFalse;
glennrp47b9dd52010-11-24 18:12:06 +00004729
4730 /* Allocate a MngInfo structure. */
4731
cristy73bd4a52010-10-05 11:24:23 +00004732 mng_info=(MngInfo *) AcquireMagickMemory(sizeof(MngInfo));
glennrp0fe50b42010-11-16 03:52:51 +00004733
cristy3ed852e2009-09-05 21:47:34 +00004734 if (mng_info == (MngInfo *) NULL)
4735 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
glennrp0fe50b42010-11-16 03:52:51 +00004736
glennrp47b9dd52010-11-24 18:12:06 +00004737 /* Initialize members of the MngInfo structure. */
4738
cristy3ed852e2009-09-05 21:47:34 +00004739 (void) ResetMagickMemory(mng_info,0,sizeof(MngInfo));
4740 mng_info->image=image;
4741 have_mng_structure=MagickTrue;
cristy3ed852e2009-09-05 21:47:34 +00004742
4743 if (LocaleCompare(image_info->magick,"MNG") == 0)
4744 {
4745 char
4746 magic_number[MaxTextExtent];
4747
glennrp47b9dd52010-11-24 18:12:06 +00004748 /* Verify MNG signature. */
cristy3ed852e2009-09-05 21:47:34 +00004749 count=(size_t) ReadBlob(image,8,(unsigned char *) magic_number);
4750 if (memcmp(magic_number,"\212MNG\r\n\032\n",8) != 0)
4751 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
glennrp47b9dd52010-11-24 18:12:06 +00004752
4753 /* Initialize some nonzero members of the MngInfo structure. */
cristy3ed852e2009-09-05 21:47:34 +00004754 for (i=0; i < MNG_MAX_OBJECTS; i++)
4755 {
cristybb503372010-05-27 20:51:26 +00004756 mng_info->object_clip[i].right=(ssize_t) PNG_UINT_31_MAX;
4757 mng_info->object_clip[i].bottom=(ssize_t) PNG_UINT_31_MAX;
cristy3ed852e2009-09-05 21:47:34 +00004758 }
4759 mng_info->exists[0]=MagickTrue;
4760 }
glennrp47b9dd52010-11-24 18:12:06 +00004761
cristy3ed852e2009-09-05 21:47:34 +00004762 first_mng_object=MagickTrue;
4763 mng_type=0;
4764#if defined(MNG_INSERT_LAYERS)
4765 insert_layers=MagickFalse; /* should be False when converting or mogrifying */
4766#endif
4767 default_frame_delay=0;
4768 default_frame_timeout=0;
4769 frame_delay=0;
4770 final_delay=1;
4771 mng_info->ticks_per_second=1UL*image->ticks_per_second;
4772 object_id=0;
4773 skip_to_iend=MagickFalse;
4774 term_chunk_found=MagickFalse;
4775 mng_info->framing_mode=1;
4776#if defined(MNG_INSERT_LAYERS)
4777 mandatory_back=MagickFalse;
4778#endif
4779#if defined(MNG_INSERT_LAYERS)
4780 mng_background_color=image->background_color;
4781#endif
4782 default_fb=mng_info->frame;
4783 previous_fb=mng_info->frame;
4784 do
4785 {
4786 char
4787 type[MaxTextExtent];
4788
4789 if (LocaleCompare(image_info->magick,"MNG") == 0)
4790 {
4791 unsigned char
4792 *chunk;
4793
4794 /*
4795 Read a new chunk.
4796 */
4797 type[0]='\0';
4798 (void) ConcatenateMagickString(type,"errr",MaxTextExtent);
4799 length=ReadBlobMSBLong(image);
4800 count=(size_t) ReadBlob(image,4,(unsigned char *) type);
4801
4802 if (logging != MagickFalse)
4803 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00004804 " Reading MNG chunk type %c%c%c%c, length: %.20g",
4805 type[0],type[1],type[2],type[3],(double) length);
cristy3ed852e2009-09-05 21:47:34 +00004806
4807 if (length > PNG_UINT_31_MAX)
4808 status=MagickFalse;
glennrp0fe50b42010-11-16 03:52:51 +00004809
cristy3ed852e2009-09-05 21:47:34 +00004810 if (count == 0)
4811 ThrowReaderException(CorruptImageError,"CorruptImage");
glennrp0fe50b42010-11-16 03:52:51 +00004812
cristy3ed852e2009-09-05 21:47:34 +00004813 p=NULL;
4814 chunk=(unsigned char *) NULL;
glennrp0fe50b42010-11-16 03:52:51 +00004815
cristy3ed852e2009-09-05 21:47:34 +00004816 if (length)
4817 {
4818 chunk=(unsigned char *) AcquireQuantumMemory(length,sizeof(*chunk));
glennrp47b9dd52010-11-24 18:12:06 +00004819
cristy3ed852e2009-09-05 21:47:34 +00004820 if (chunk == (unsigned char *) NULL)
4821 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
glennrp47b9dd52010-11-24 18:12:06 +00004822
cristybb503372010-05-27 20:51:26 +00004823 for (i=0; i < (ssize_t) length; i++)
cristy3ed852e2009-09-05 21:47:34 +00004824 chunk[i]=(unsigned char) ReadBlobByte(image);
glennrp47b9dd52010-11-24 18:12:06 +00004825
cristy3ed852e2009-09-05 21:47:34 +00004826 p=chunk;
4827 }
glennrp0fe50b42010-11-16 03:52:51 +00004828
cristy3ed852e2009-09-05 21:47:34 +00004829 (void) ReadBlobMSBLong(image); /* read crc word */
4830
4831#if !defined(JNG_SUPPORTED)
4832 if (memcmp(type,mng_JHDR,4) == 0)
4833 {
4834 skip_to_iend=MagickTrue;
glennrp0fe50b42010-11-16 03:52:51 +00004835
cristy3ed852e2009-09-05 21:47:34 +00004836 if (mng_info->jhdr_warning == 0)
cristyc82a27b2011-10-21 01:07:16 +00004837 (void) ThrowMagickException(exception,GetMagickModule(),
cristy3ed852e2009-09-05 21:47:34 +00004838 CoderError,"JNGCompressNotSupported","`%s'",image->filename);
glennrp0fe50b42010-11-16 03:52:51 +00004839
cristy3ed852e2009-09-05 21:47:34 +00004840 mng_info->jhdr_warning++;
4841 }
4842#endif
4843 if (memcmp(type,mng_DHDR,4) == 0)
4844 {
4845 skip_to_iend=MagickTrue;
glennrp0fe50b42010-11-16 03:52:51 +00004846
cristy3ed852e2009-09-05 21:47:34 +00004847 if (mng_info->dhdr_warning == 0)
cristyc82a27b2011-10-21 01:07:16 +00004848 (void) ThrowMagickException(exception,GetMagickModule(),
cristy3ed852e2009-09-05 21:47:34 +00004849 CoderError,"DeltaPNGNotSupported","`%s'",image->filename);
glennrp0fe50b42010-11-16 03:52:51 +00004850
cristy3ed852e2009-09-05 21:47:34 +00004851 mng_info->dhdr_warning++;
4852 }
4853 if (memcmp(type,mng_MEND,4) == 0)
4854 break;
glennrp47b9dd52010-11-24 18:12:06 +00004855
cristy3ed852e2009-09-05 21:47:34 +00004856 if (skip_to_iend)
4857 {
4858 if (memcmp(type,mng_IEND,4) == 0)
4859 skip_to_iend=MagickFalse;
glennrp0fe50b42010-11-16 03:52:51 +00004860
cristy3ed852e2009-09-05 21:47:34 +00004861 if (length)
4862 chunk=(unsigned char *) RelinquishMagickMemory(chunk);
glennrp0fe50b42010-11-16 03:52:51 +00004863
cristy3ed852e2009-09-05 21:47:34 +00004864 if (logging != MagickFalse)
4865 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
4866 " Skip to IEND.");
glennrp0fe50b42010-11-16 03:52:51 +00004867
cristy3ed852e2009-09-05 21:47:34 +00004868 continue;
4869 }
glennrp0fe50b42010-11-16 03:52:51 +00004870
cristy3ed852e2009-09-05 21:47:34 +00004871 if (memcmp(type,mng_MHDR,4) == 0)
4872 {
cristybb503372010-05-27 20:51:26 +00004873 mng_info->mng_width=(size_t) ((p[0] << 24) | (p[1] << 16) |
cristy3ed852e2009-09-05 21:47:34 +00004874 (p[2] << 8) | p[3]);
glennrp0fe50b42010-11-16 03:52:51 +00004875
cristybb503372010-05-27 20:51:26 +00004876 mng_info->mng_height=(size_t) ((p[4] << 24) | (p[5] << 16) |
cristy3ed852e2009-09-05 21:47:34 +00004877 (p[6] << 8) | p[7]);
glennrp0fe50b42010-11-16 03:52:51 +00004878
cristy3ed852e2009-09-05 21:47:34 +00004879 if (logging != MagickFalse)
4880 {
4881 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00004882 " MNG width: %.20g",(double) mng_info->mng_width);
cristy3ed852e2009-09-05 21:47:34 +00004883 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00004884 " MNG height: %.20g",(double) mng_info->mng_height);
cristy3ed852e2009-09-05 21:47:34 +00004885 }
glennrp0fe50b42010-11-16 03:52:51 +00004886
cristy3ed852e2009-09-05 21:47:34 +00004887 p+=8;
cristy8182b072010-05-30 20:10:53 +00004888 mng_info->ticks_per_second=(size_t) mng_get_long(p);
glennrp0fe50b42010-11-16 03:52:51 +00004889
cristy3ed852e2009-09-05 21:47:34 +00004890 if (mng_info->ticks_per_second == 0)
4891 default_frame_delay=0;
glennrp0fe50b42010-11-16 03:52:51 +00004892
cristy3ed852e2009-09-05 21:47:34 +00004893 else
4894 default_frame_delay=1UL*image->ticks_per_second/
4895 mng_info->ticks_per_second;
glennrp0fe50b42010-11-16 03:52:51 +00004896
cristy3ed852e2009-09-05 21:47:34 +00004897 frame_delay=default_frame_delay;
4898 simplicity=0;
glennrp0fe50b42010-11-16 03:52:51 +00004899
cristy3ed852e2009-09-05 21:47:34 +00004900 if (length > 16)
4901 {
4902 p+=16;
cristy8182b072010-05-30 20:10:53 +00004903 simplicity=(size_t) mng_get_long(p);
cristy3ed852e2009-09-05 21:47:34 +00004904 }
glennrp0fe50b42010-11-16 03:52:51 +00004905
cristy3ed852e2009-09-05 21:47:34 +00004906 mng_type=1; /* Full MNG */
glennrp0fe50b42010-11-16 03:52:51 +00004907
cristy3ed852e2009-09-05 21:47:34 +00004908 if ((simplicity != 0) && ((simplicity | 11) == 11))
4909 mng_type=2; /* LC */
glennrp0fe50b42010-11-16 03:52:51 +00004910
cristy3ed852e2009-09-05 21:47:34 +00004911 if ((simplicity != 0) && ((simplicity | 9) == 9))
4912 mng_type=3; /* VLC */
glennrp0fe50b42010-11-16 03:52:51 +00004913
cristy3ed852e2009-09-05 21:47:34 +00004914#if defined(MNG_INSERT_LAYERS)
4915 if (mng_type != 3)
4916 insert_layers=MagickTrue;
4917#endif
cristy4c08aed2011-07-01 19:47:50 +00004918 if (GetAuthenticPixelQueue(image) != (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00004919 {
glennrp47b9dd52010-11-24 18:12:06 +00004920 /* Allocate next image structure. */
cristy9950d572011-10-01 18:22:35 +00004921 AcquireNextImage(image_info,image,exception);
glennrp0fe50b42010-11-16 03:52:51 +00004922
cristy3ed852e2009-09-05 21:47:34 +00004923 if (GetNextImageInList(image) == (Image *) NULL)
4924 return((Image *) NULL);
glennrp0fe50b42010-11-16 03:52:51 +00004925
cristy3ed852e2009-09-05 21:47:34 +00004926 image=SyncNextImageInList(image);
4927 mng_info->image=image;
4928 }
4929
4930 if ((mng_info->mng_width > 65535L) ||
4931 (mng_info->mng_height > 65535L))
4932 ThrowReaderException(ImageError,"WidthOrHeightExceedsLimit");
glennrp0fe50b42010-11-16 03:52:51 +00004933
cristy3b6fd2e2011-05-20 12:53:50 +00004934 (void) FormatLocaleString(page_geometry,MaxTextExtent,
cristye8c25f92010-06-03 00:53:06 +00004935 "%.20gx%.20g+0+0",(double) mng_info->mng_width,(double)
cristyf2faecf2010-05-28 19:19:36 +00004936 mng_info->mng_height);
glennrp0fe50b42010-11-16 03:52:51 +00004937
cristy3ed852e2009-09-05 21:47:34 +00004938 mng_info->frame.left=0;
cristybb503372010-05-27 20:51:26 +00004939 mng_info->frame.right=(ssize_t) mng_info->mng_width;
cristy3ed852e2009-09-05 21:47:34 +00004940 mng_info->frame.top=0;
cristybb503372010-05-27 20:51:26 +00004941 mng_info->frame.bottom=(ssize_t) mng_info->mng_height;
cristy3ed852e2009-09-05 21:47:34 +00004942 mng_info->clip=default_fb=previous_fb=mng_info->frame;
glennrp0fe50b42010-11-16 03:52:51 +00004943
cristy3ed852e2009-09-05 21:47:34 +00004944 for (i=0; i < MNG_MAX_OBJECTS; i++)
4945 mng_info->object_clip[i]=mng_info->frame;
glennrp0fe50b42010-11-16 03:52:51 +00004946
cristy3ed852e2009-09-05 21:47:34 +00004947 chunk=(unsigned char *) RelinquishMagickMemory(chunk);
4948 continue;
4949 }
4950
4951 if (memcmp(type,mng_TERM,4) == 0)
4952 {
4953 int
4954 repeat=0;
4955
4956
4957 if (length)
4958 repeat=p[0];
glennrp0fe50b42010-11-16 03:52:51 +00004959
cristy3ed852e2009-09-05 21:47:34 +00004960 if (repeat == 3)
4961 {
cristy8182b072010-05-30 20:10:53 +00004962 final_delay=(png_uint_32) mng_get_long(&p[2]);
4963 mng_iterations=(png_uint_32) mng_get_long(&p[6]);
glennrp0fe50b42010-11-16 03:52:51 +00004964
cristy3ed852e2009-09-05 21:47:34 +00004965 if (mng_iterations == PNG_UINT_31_MAX)
4966 mng_iterations=0;
glennrp0fe50b42010-11-16 03:52:51 +00004967
cristy3ed852e2009-09-05 21:47:34 +00004968 image->iterations=mng_iterations;
4969 term_chunk_found=MagickTrue;
4970 }
glennrp0fe50b42010-11-16 03:52:51 +00004971
cristy3ed852e2009-09-05 21:47:34 +00004972 if (logging != MagickFalse)
4973 {
4974 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
4975 " repeat=%d",repeat);
glennrp0fe50b42010-11-16 03:52:51 +00004976
cristy3ed852e2009-09-05 21:47:34 +00004977 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00004978 " final_delay=%.20g",(double) final_delay);
glennrp0fe50b42010-11-16 03:52:51 +00004979
cristy3ed852e2009-09-05 21:47:34 +00004980 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00004981 " image->iterations=%.20g",(double) image->iterations);
cristy3ed852e2009-09-05 21:47:34 +00004982 }
glennrp0fe50b42010-11-16 03:52:51 +00004983
cristy3ed852e2009-09-05 21:47:34 +00004984 chunk=(unsigned char *) RelinquishMagickMemory(chunk);
4985 continue;
4986 }
4987 if (memcmp(type,mng_DEFI,4) == 0)
4988 {
4989 if (mng_type == 3)
cristyc82a27b2011-10-21 01:07:16 +00004990 (void) ThrowMagickException(exception,GetMagickModule(),
cristy3ed852e2009-09-05 21:47:34 +00004991 CoderError,"DEFI chunk found in MNG-VLC datastream","`%s'",
4992 image->filename);
glennrp0fe50b42010-11-16 03:52:51 +00004993
cristy3ed852e2009-09-05 21:47:34 +00004994 object_id=(p[0] << 8) | p[1];
glennrp0fe50b42010-11-16 03:52:51 +00004995
cristy3ed852e2009-09-05 21:47:34 +00004996 if (mng_type == 2 && object_id != 0)
cristyc82a27b2011-10-21 01:07:16 +00004997 (void) ThrowMagickException(exception,GetMagickModule(),
cristy3ed852e2009-09-05 21:47:34 +00004998 CoderError,"Nonzero object_id in MNG-LC datastream","`%s'",
4999 image->filename);
glennrp0fe50b42010-11-16 03:52:51 +00005000
cristy3ed852e2009-09-05 21:47:34 +00005001 if (object_id > MNG_MAX_OBJECTS)
5002 {
5003 /*
5004 Instead ofsuing a warning we should allocate a larger
5005 MngInfo structure and continue.
5006 */
cristyc82a27b2011-10-21 01:07:16 +00005007 (void) ThrowMagickException(exception,GetMagickModule(),
cristy3ed852e2009-09-05 21:47:34 +00005008 CoderError,"object id too large","`%s'",image->filename);
5009 object_id=MNG_MAX_OBJECTS;
5010 }
glennrp0fe50b42010-11-16 03:52:51 +00005011
cristy3ed852e2009-09-05 21:47:34 +00005012 if (mng_info->exists[object_id])
5013 if (mng_info->frozen[object_id])
5014 {
5015 chunk=(unsigned char *) RelinquishMagickMemory(chunk);
cristyc82a27b2011-10-21 01:07:16 +00005016 (void) ThrowMagickException(exception,
cristy3ed852e2009-09-05 21:47:34 +00005017 GetMagickModule(),CoderError,
5018 "DEFI cannot redefine a frozen MNG object","`%s'",
5019 image->filename);
5020 continue;
5021 }
glennrp0fe50b42010-11-16 03:52:51 +00005022
cristy3ed852e2009-09-05 21:47:34 +00005023 mng_info->exists[object_id]=MagickTrue;
glennrp0fe50b42010-11-16 03:52:51 +00005024
cristy3ed852e2009-09-05 21:47:34 +00005025 if (length > 2)
5026 mng_info->invisible[object_id]=p[2];
glennrp0fe50b42010-11-16 03:52:51 +00005027
cristy3ed852e2009-09-05 21:47:34 +00005028 /*
5029 Extract object offset info.
5030 */
5031 if (length > 11)
5032 {
glennrp0fe50b42010-11-16 03:52:51 +00005033 mng_info->x_off[object_id]=(ssize_t) ((p[4] << 24) |
5034 (p[5] << 16) | (p[6] << 8) | p[7]);
5035
5036 mng_info->y_off[object_id]=(ssize_t) ((p[8] << 24) |
5037 (p[9] << 16) | (p[10] << 8) | p[11]);
5038
cristy3ed852e2009-09-05 21:47:34 +00005039 if (logging != MagickFalse)
5040 {
5041 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00005042 " x_off[%d]: %.20g",object_id,(double)
cristyf2faecf2010-05-28 19:19:36 +00005043 mng_info->x_off[object_id]);
glennrp0fe50b42010-11-16 03:52:51 +00005044
cristy3ed852e2009-09-05 21:47:34 +00005045 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00005046 " y_off[%d]: %.20g",object_id,(double)
cristyf2faecf2010-05-28 19:19:36 +00005047 mng_info->y_off[object_id]);
cristy3ed852e2009-09-05 21:47:34 +00005048 }
5049 }
glennrp0fe50b42010-11-16 03:52:51 +00005050
cristy3ed852e2009-09-05 21:47:34 +00005051 /*
5052 Extract object clipping info.
5053 */
5054 if (length > 27)
5055 mng_info->object_clip[object_id]=mng_read_box(mng_info->frame,0,
5056 &p[12]);
glennrp0fe50b42010-11-16 03:52:51 +00005057
cristy3ed852e2009-09-05 21:47:34 +00005058 chunk=(unsigned char *) RelinquishMagickMemory(chunk);
5059 continue;
5060 }
5061 if (memcmp(type,mng_bKGD,4) == 0)
5062 {
5063 mng_info->have_global_bkgd=MagickFalse;
glennrp0fe50b42010-11-16 03:52:51 +00005064
cristy3ed852e2009-09-05 21:47:34 +00005065 if (length > 5)
5066 {
5067 mng_info->mng_global_bkgd.red=
5068 ScaleShortToQuantum((unsigned short) ((p[0] << 8) | p[1]));
glennrp0fe50b42010-11-16 03:52:51 +00005069
cristy3ed852e2009-09-05 21:47:34 +00005070 mng_info->mng_global_bkgd.green=
5071 ScaleShortToQuantum((unsigned short) ((p[2] << 8) | p[3]));
glennrp0fe50b42010-11-16 03:52:51 +00005072
cristy3ed852e2009-09-05 21:47:34 +00005073 mng_info->mng_global_bkgd.blue=
5074 ScaleShortToQuantum((unsigned short) ((p[4] << 8) | p[5]));
glennrp0fe50b42010-11-16 03:52:51 +00005075
cristy3ed852e2009-09-05 21:47:34 +00005076 mng_info->have_global_bkgd=MagickTrue;
5077 }
glennrp0fe50b42010-11-16 03:52:51 +00005078
cristy3ed852e2009-09-05 21:47:34 +00005079 chunk=(unsigned char *) RelinquishMagickMemory(chunk);
5080 continue;
5081 }
5082 if (memcmp(type,mng_BACK,4) == 0)
5083 {
5084#if defined(MNG_INSERT_LAYERS)
5085 if (length > 6)
5086 mandatory_back=p[6];
glennrp0fe50b42010-11-16 03:52:51 +00005087
cristy3ed852e2009-09-05 21:47:34 +00005088 else
5089 mandatory_back=0;
glennrp0fe50b42010-11-16 03:52:51 +00005090
cristy3ed852e2009-09-05 21:47:34 +00005091 if (mandatory_back && length > 5)
5092 {
5093 mng_background_color.red=
5094 ScaleShortToQuantum((unsigned short) ((p[0] << 8) | p[1]));
glennrp0fe50b42010-11-16 03:52:51 +00005095
cristy3ed852e2009-09-05 21:47:34 +00005096 mng_background_color.green=
5097 ScaleShortToQuantum((unsigned short) ((p[2] << 8) | p[3]));
glennrp0fe50b42010-11-16 03:52:51 +00005098
cristy3ed852e2009-09-05 21:47:34 +00005099 mng_background_color.blue=
5100 ScaleShortToQuantum((unsigned short) ((p[4] << 8) | p[5]));
glennrp0fe50b42010-11-16 03:52:51 +00005101
cristy4c08aed2011-07-01 19:47:50 +00005102 mng_background_color.alpha=OpaqueAlpha;
cristy3ed852e2009-09-05 21:47:34 +00005103 }
glennrp0fe50b42010-11-16 03:52:51 +00005104
cristy3ed852e2009-09-05 21:47:34 +00005105#ifdef MNG_OBJECT_BUFFERS
5106 if (length > 8)
5107 mng_background_object=(p[7] << 8) | p[8];
5108#endif
5109#endif
5110 chunk=(unsigned char *) RelinquishMagickMemory(chunk);
5111 continue;
5112 }
glennrp47b9dd52010-11-24 18:12:06 +00005113
cristy3ed852e2009-09-05 21:47:34 +00005114 if (memcmp(type,mng_PLTE,4) == 0)
5115 {
glennrp47b9dd52010-11-24 18:12:06 +00005116 /* Read global PLTE. */
5117
cristy3ed852e2009-09-05 21:47:34 +00005118 if (length && (length < 769))
5119 {
5120 if (mng_info->global_plte == (png_colorp) NULL)
5121 mng_info->global_plte=(png_colorp) AcquireQuantumMemory(256,
5122 sizeof(*mng_info->global_plte));
glennrp0fe50b42010-11-16 03:52:51 +00005123
cristybb503372010-05-27 20:51:26 +00005124 for (i=0; i < (ssize_t) (length/3); i++)
cristy3ed852e2009-09-05 21:47:34 +00005125 {
5126 mng_info->global_plte[i].red=p[3*i];
5127 mng_info->global_plte[i].green=p[3*i+1];
5128 mng_info->global_plte[i].blue=p[3*i+2];
5129 }
glennrp0fe50b42010-11-16 03:52:51 +00005130
cristy35ef8242010-06-03 16:24:13 +00005131 mng_info->global_plte_length=(unsigned int) (length/3);
cristy3ed852e2009-09-05 21:47:34 +00005132 }
5133#ifdef MNG_LOOSE
5134 for ( ; i < 256; i++)
5135 {
5136 mng_info->global_plte[i].red=i;
5137 mng_info->global_plte[i].green=i;
5138 mng_info->global_plte[i].blue=i;
5139 }
glennrp0fe50b42010-11-16 03:52:51 +00005140
cristy3ed852e2009-09-05 21:47:34 +00005141 if (length)
5142 mng_info->global_plte_length=256;
5143#endif
5144 else
5145 mng_info->global_plte_length=0;
glennrp0fe50b42010-11-16 03:52:51 +00005146
cristy3ed852e2009-09-05 21:47:34 +00005147 chunk=(unsigned char *) RelinquishMagickMemory(chunk);
5148 continue;
5149 }
glennrp47b9dd52010-11-24 18:12:06 +00005150
cristy3ed852e2009-09-05 21:47:34 +00005151 if (memcmp(type,mng_tRNS,4) == 0)
5152 {
5153 /* read global tRNS */
5154
5155 if (length < 257)
cristybb503372010-05-27 20:51:26 +00005156 for (i=0; i < (ssize_t) length; i++)
cristy3ed852e2009-09-05 21:47:34 +00005157 mng_info->global_trns[i]=p[i];
5158
5159#ifdef MNG_LOOSE
5160 for ( ; i < 256; i++)
5161 mng_info->global_trns[i]=255;
5162#endif
cristy12560f32010-06-03 16:51:08 +00005163 mng_info->global_trns_length=(unsigned int) length;
cristy3ed852e2009-09-05 21:47:34 +00005164 chunk=(unsigned char *) RelinquishMagickMemory(chunk);
5165 continue;
5166 }
5167 if (memcmp(type,mng_gAMA,4) == 0)
5168 {
5169 if (length == 4)
5170 {
cristybb503372010-05-27 20:51:26 +00005171 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00005172 igamma;
5173
cristy8182b072010-05-30 20:10:53 +00005174 igamma=mng_get_long(p);
cristy3ed852e2009-09-05 21:47:34 +00005175 mng_info->global_gamma=((float) igamma)*0.00001;
5176 mng_info->have_global_gama=MagickTrue;
5177 }
glennrp0fe50b42010-11-16 03:52:51 +00005178
cristy3ed852e2009-09-05 21:47:34 +00005179 else
5180 mng_info->have_global_gama=MagickFalse;
glennrp0fe50b42010-11-16 03:52:51 +00005181
cristy3ed852e2009-09-05 21:47:34 +00005182 chunk=(unsigned char *) RelinquishMagickMemory(chunk);
5183 continue;
5184 }
5185
5186 if (memcmp(type,mng_cHRM,4) == 0)
5187 {
glennrp47b9dd52010-11-24 18:12:06 +00005188 /* Read global cHRM */
5189
cristy3ed852e2009-09-05 21:47:34 +00005190 if (length == 32)
5191 {
cristy8182b072010-05-30 20:10:53 +00005192 mng_info->global_chrm.white_point.x=0.00001*mng_get_long(p);
5193 mng_info->global_chrm.white_point.y=0.00001*mng_get_long(&p[4]);
5194 mng_info->global_chrm.red_primary.x=0.00001*mng_get_long(&p[8]);
cristy3ed852e2009-09-05 21:47:34 +00005195 mng_info->global_chrm.red_primary.y=0.00001*
cristy8182b072010-05-30 20:10:53 +00005196 mng_get_long(&p[12]);
cristy3ed852e2009-09-05 21:47:34 +00005197 mng_info->global_chrm.green_primary.x=0.00001*
cristy8182b072010-05-30 20:10:53 +00005198 mng_get_long(&p[16]);
cristy3ed852e2009-09-05 21:47:34 +00005199 mng_info->global_chrm.green_primary.y=0.00001*
cristy8182b072010-05-30 20:10:53 +00005200 mng_get_long(&p[20]);
cristy3ed852e2009-09-05 21:47:34 +00005201 mng_info->global_chrm.blue_primary.x=0.00001*
cristy8182b072010-05-30 20:10:53 +00005202 mng_get_long(&p[24]);
cristy3ed852e2009-09-05 21:47:34 +00005203 mng_info->global_chrm.blue_primary.y=0.00001*
cristy8182b072010-05-30 20:10:53 +00005204 mng_get_long(&p[28]);
cristy3ed852e2009-09-05 21:47:34 +00005205 mng_info->have_global_chrm=MagickTrue;
5206 }
5207 else
5208 mng_info->have_global_chrm=MagickFalse;
glennrp47b9dd52010-11-24 18:12:06 +00005209
cristy3ed852e2009-09-05 21:47:34 +00005210 chunk=(unsigned char *) RelinquishMagickMemory(chunk);
5211 continue;
5212 }
glennrp47b9dd52010-11-24 18:12:06 +00005213
cristy3ed852e2009-09-05 21:47:34 +00005214 if (memcmp(type,mng_sRGB,4) == 0)
5215 {
5216 /*
5217 Read global sRGB.
5218 */
5219 if (length)
5220 {
glennrpe610a072010-08-05 17:08:46 +00005221 mng_info->global_srgb_intent=
glennrpcf002022011-01-30 02:38:15 +00005222 Magick_RenderingIntent_from_PNG_RenderingIntent(p[0]);
cristy3ed852e2009-09-05 21:47:34 +00005223 mng_info->have_global_srgb=MagickTrue;
5224 }
5225 else
5226 mng_info->have_global_srgb=MagickFalse;
glennrp47b9dd52010-11-24 18:12:06 +00005227
cristy3ed852e2009-09-05 21:47:34 +00005228 chunk=(unsigned char *) RelinquishMagickMemory(chunk);
5229 continue;
5230 }
glennrp47b9dd52010-11-24 18:12:06 +00005231
cristy3ed852e2009-09-05 21:47:34 +00005232 if (memcmp(type,mng_iCCP,4) == 0)
5233 {
glennrpfd05d622011-02-25 04:10:33 +00005234 /* To do: */
cristy3ed852e2009-09-05 21:47:34 +00005235
5236 /*
5237 Read global iCCP.
5238 */
5239 if (length)
5240 chunk=(unsigned char *) RelinquishMagickMemory(chunk);
glennrp47b9dd52010-11-24 18:12:06 +00005241
cristy3ed852e2009-09-05 21:47:34 +00005242 continue;
5243 }
glennrp47b9dd52010-11-24 18:12:06 +00005244
cristy3ed852e2009-09-05 21:47:34 +00005245 if (memcmp(type,mng_FRAM,4) == 0)
5246 {
5247 if (mng_type == 3)
cristyc82a27b2011-10-21 01:07:16 +00005248 (void) ThrowMagickException(exception,GetMagickModule(),
cristy3ed852e2009-09-05 21:47:34 +00005249 CoderError,"FRAM chunk found in MNG-VLC datastream","`%s'",
5250 image->filename);
glennrp0fe50b42010-11-16 03:52:51 +00005251
cristy3ed852e2009-09-05 21:47:34 +00005252 if ((mng_info->framing_mode == 2) || (mng_info->framing_mode == 4))
5253 image->delay=frame_delay;
glennrp0fe50b42010-11-16 03:52:51 +00005254
cristy3ed852e2009-09-05 21:47:34 +00005255 frame_delay=default_frame_delay;
5256 frame_timeout=default_frame_timeout;
5257 fb=default_fb;
glennrp47b9dd52010-11-24 18:12:06 +00005258
cristy3ed852e2009-09-05 21:47:34 +00005259 if (length)
5260 if (p[0])
5261 mng_info->framing_mode=p[0];
glennrp0fe50b42010-11-16 03:52:51 +00005262
cristy3ed852e2009-09-05 21:47:34 +00005263 if (logging != MagickFalse)
5264 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
5265 " Framing_mode=%d",mng_info->framing_mode);
glennrp0fe50b42010-11-16 03:52:51 +00005266
cristy3ed852e2009-09-05 21:47:34 +00005267 if (length > 6)
5268 {
glennrp47b9dd52010-11-24 18:12:06 +00005269 /* Note the delay and frame clipping boundaries. */
5270
cristy3ed852e2009-09-05 21:47:34 +00005271 p++; /* framing mode */
glennrp47b9dd52010-11-24 18:12:06 +00005272
cristybb503372010-05-27 20:51:26 +00005273 while (*p && ((p-chunk) < (ssize_t) length))
cristy3ed852e2009-09-05 21:47:34 +00005274 p++; /* frame name */
glennrp47b9dd52010-11-24 18:12:06 +00005275
cristy3ed852e2009-09-05 21:47:34 +00005276 p++; /* frame name terminator */
glennrp47b9dd52010-11-24 18:12:06 +00005277
cristybb503372010-05-27 20:51:26 +00005278 if ((p-chunk) < (ssize_t) (length-4))
cristy3ed852e2009-09-05 21:47:34 +00005279 {
5280 int
5281 change_delay,
5282 change_timeout,
5283 change_clipping;
5284
5285 change_delay=(*p++);
5286 change_timeout=(*p++);
5287 change_clipping=(*p++);
5288 p++; /* change_sync */
glennrp47b9dd52010-11-24 18:12:06 +00005289
cristy3ed852e2009-09-05 21:47:34 +00005290 if (change_delay)
5291 {
cristy8182b072010-05-30 20:10:53 +00005292 frame_delay=1UL*image->ticks_per_second*
5293 mng_get_long(p);
glennrp0fe50b42010-11-16 03:52:51 +00005294
cristy8182b072010-05-30 20:10:53 +00005295 if (mng_info->ticks_per_second != 0)
5296 frame_delay/=mng_info->ticks_per_second;
glennrp0fe50b42010-11-16 03:52:51 +00005297
glennrpbb010dd2010-06-01 13:07:15 +00005298 else
5299 frame_delay=PNG_UINT_31_MAX;
glennrp0fe50b42010-11-16 03:52:51 +00005300
cristy3ed852e2009-09-05 21:47:34 +00005301 if (change_delay == 2)
5302 default_frame_delay=frame_delay;
glennrp0fe50b42010-11-16 03:52:51 +00005303
cristy3ed852e2009-09-05 21:47:34 +00005304 p+=4;
glennrp0fe50b42010-11-16 03:52:51 +00005305
cristy3ed852e2009-09-05 21:47:34 +00005306 if (logging != MagickFalse)
5307 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00005308 " Framing_delay=%.20g",(double) frame_delay);
cristy3ed852e2009-09-05 21:47:34 +00005309 }
glennrp47b9dd52010-11-24 18:12:06 +00005310
cristy3ed852e2009-09-05 21:47:34 +00005311 if (change_timeout)
5312 {
glennrpbb010dd2010-06-01 13:07:15 +00005313 frame_timeout=1UL*image->ticks_per_second*
5314 mng_get_long(p);
glennrp0fe50b42010-11-16 03:52:51 +00005315
glennrpbb010dd2010-06-01 13:07:15 +00005316 if (mng_info->ticks_per_second != 0)
5317 frame_timeout/=mng_info->ticks_per_second;
glennrp0fe50b42010-11-16 03:52:51 +00005318
glennrpbb010dd2010-06-01 13:07:15 +00005319 else
5320 frame_timeout=PNG_UINT_31_MAX;
glennrp0fe50b42010-11-16 03:52:51 +00005321
cristy3ed852e2009-09-05 21:47:34 +00005322 if (change_delay == 2)
5323 default_frame_timeout=frame_timeout;
glennrp0fe50b42010-11-16 03:52:51 +00005324
cristy3ed852e2009-09-05 21:47:34 +00005325 p+=4;
glennrp0fe50b42010-11-16 03:52:51 +00005326
cristy3ed852e2009-09-05 21:47:34 +00005327 if (logging != MagickFalse)
5328 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00005329 " Framing_timeout=%.20g",(double) frame_timeout);
cristy3ed852e2009-09-05 21:47:34 +00005330 }
glennrp47b9dd52010-11-24 18:12:06 +00005331
cristy3ed852e2009-09-05 21:47:34 +00005332 if (change_clipping)
5333 {
5334 fb=mng_read_box(previous_fb,(char) p[0],&p[1]);
5335 p+=17;
5336 previous_fb=fb;
glennrp0fe50b42010-11-16 03:52:51 +00005337
cristy3ed852e2009-09-05 21:47:34 +00005338 if (logging != MagickFalse)
5339 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
glennrp0fe50b42010-11-16 03:52:51 +00005340 " Frame_clip: L=%.20g R=%.20g T=%.20g B=%.20g",
cristye8c25f92010-06-03 00:53:06 +00005341 (double) fb.left,(double) fb.right,(double) fb.top,
5342 (double) fb.bottom);
glennrp47b9dd52010-11-24 18:12:06 +00005343
cristy3ed852e2009-09-05 21:47:34 +00005344 if (change_clipping == 2)
5345 default_fb=fb;
5346 }
5347 }
5348 }
5349 mng_info->clip=fb;
5350 mng_info->clip=mng_minimum_box(fb,mng_info->frame);
glennrp0fe50b42010-11-16 03:52:51 +00005351
cristybb503372010-05-27 20:51:26 +00005352 subframe_width=(size_t) (mng_info->clip.right
cristy3ed852e2009-09-05 21:47:34 +00005353 -mng_info->clip.left);
glennrp0fe50b42010-11-16 03:52:51 +00005354
cristybb503372010-05-27 20:51:26 +00005355 subframe_height=(size_t) (mng_info->clip.bottom
cristy3ed852e2009-09-05 21:47:34 +00005356 -mng_info->clip.top);
5357 /*
5358 Insert a background layer behind the frame if framing_mode is 4.
5359 */
5360#if defined(MNG_INSERT_LAYERS)
5361 if (logging != MagickFalse)
5362 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00005363 " subframe_width=%.20g, subframe_height=%.20g",(double)
5364 subframe_width,(double) subframe_height);
glennrp0fe50b42010-11-16 03:52:51 +00005365
cristy3ed852e2009-09-05 21:47:34 +00005366 if (insert_layers && (mng_info->framing_mode == 4) &&
5367 (subframe_width) && (subframe_height))
5368 {
glennrp47b9dd52010-11-24 18:12:06 +00005369 /* Allocate next image structure. */
cristy4c08aed2011-07-01 19:47:50 +00005370 if (GetAuthenticPixelQueue(image) != (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00005371 {
cristy9950d572011-10-01 18:22:35 +00005372 AcquireNextImage(image_info,image,exception);
glennrp47b9dd52010-11-24 18:12:06 +00005373
cristy3ed852e2009-09-05 21:47:34 +00005374 if (GetNextImageInList(image) == (Image *) NULL)
5375 {
5376 image=DestroyImageList(image);
5377 MngInfoFreeStruct(mng_info,&have_mng_structure);
5378 return((Image *) NULL);
5379 }
glennrp47b9dd52010-11-24 18:12:06 +00005380
cristy3ed852e2009-09-05 21:47:34 +00005381 image=SyncNextImageInList(image);
5382 }
glennrp0fe50b42010-11-16 03:52:51 +00005383
cristy3ed852e2009-09-05 21:47:34 +00005384 mng_info->image=image;
glennrp0fe50b42010-11-16 03:52:51 +00005385
cristy3ed852e2009-09-05 21:47:34 +00005386 if (term_chunk_found)
5387 {
5388 image->start_loop=MagickTrue;
5389 image->iterations=mng_iterations;
5390 term_chunk_found=MagickFalse;
5391 }
glennrp0fe50b42010-11-16 03:52:51 +00005392
cristy3ed852e2009-09-05 21:47:34 +00005393 else
5394 image->start_loop=MagickFalse;
glennrp0fe50b42010-11-16 03:52:51 +00005395
cristy3ed852e2009-09-05 21:47:34 +00005396 image->columns=subframe_width;
5397 image->rows=subframe_height;
5398 image->page.width=subframe_width;
5399 image->page.height=subframe_height;
5400 image->page.x=mng_info->clip.left;
5401 image->page.y=mng_info->clip.top;
5402 image->background_color=mng_background_color;
5403 image->matte=MagickFalse;
5404 image->delay=0;
cristyea1a8aa2011-10-20 13:24:06 +00005405 (void) SetImageBackgroundColor(image,exception);
glennrp0fe50b42010-11-16 03:52:51 +00005406
cristy3ed852e2009-09-05 21:47:34 +00005407 if (logging != MagickFalse)
5408 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
glennrp0fe50b42010-11-16 03:52:51 +00005409 " Insert backgd layer, L=%.20g, R=%.20g T=%.20g, B=%.20g",
cristye8c25f92010-06-03 00:53:06 +00005410 (double) mng_info->clip.left,(double) mng_info->clip.right,
5411 (double) mng_info->clip.top,(double) mng_info->clip.bottom);
cristy3ed852e2009-09-05 21:47:34 +00005412 }
5413#endif
5414 chunk=(unsigned char *) RelinquishMagickMemory(chunk);
5415 continue;
5416 }
5417 if (memcmp(type,mng_CLIP,4) == 0)
5418 {
5419 unsigned int
5420 first_object,
5421 last_object;
5422
5423 /*
5424 Read CLIP.
5425 */
5426 first_object=(p[0] << 8) | p[1];
5427 last_object=(p[2] << 8) | p[3];
glennrp47b9dd52010-11-24 18:12:06 +00005428
cristy3ed852e2009-09-05 21:47:34 +00005429 for (i=(int) first_object; i <= (int) last_object; i++)
5430 {
5431 if (mng_info->exists[i] && !mng_info->frozen[i])
5432 {
5433 MngBox
5434 box;
5435
5436 box=mng_info->object_clip[i];
5437 mng_info->object_clip[i]=mng_read_box(box,(char) p[4],&p[5]);
5438 }
5439 }
glennrp47b9dd52010-11-24 18:12:06 +00005440
cristy3ed852e2009-09-05 21:47:34 +00005441 chunk=(unsigned char *) RelinquishMagickMemory(chunk);
5442 continue;
5443 }
5444 if (memcmp(type,mng_SAVE,4) == 0)
5445 {
5446 for (i=1; i < MNG_MAX_OBJECTS; i++)
5447 if (mng_info->exists[i])
5448 {
5449 mng_info->frozen[i]=MagickTrue;
5450#ifdef MNG_OBJECT_BUFFERS
5451 if (mng_info->ob[i] != (MngBuffer *) NULL)
5452 mng_info->ob[i]->frozen=MagickTrue;
5453#endif
5454 }
glennrp0fe50b42010-11-16 03:52:51 +00005455
cristy3ed852e2009-09-05 21:47:34 +00005456 if (length)
5457 chunk=(unsigned char *) RelinquishMagickMemory(chunk);
glennrp0fe50b42010-11-16 03:52:51 +00005458
cristy3ed852e2009-09-05 21:47:34 +00005459 continue;
5460 }
5461
5462 if ((memcmp(type,mng_DISC,4) == 0) || (memcmp(type,mng_SEEK,4) == 0))
5463 {
glennrp47b9dd52010-11-24 18:12:06 +00005464 /* Read DISC or SEEK. */
5465
cristy3ed852e2009-09-05 21:47:34 +00005466 if ((length == 0) || !memcmp(type,mng_SEEK,4))
5467 {
5468 for (i=1; i < MNG_MAX_OBJECTS; i++)
5469 MngInfoDiscardObject(mng_info,i);
5470 }
glennrp0fe50b42010-11-16 03:52:51 +00005471
cristy3ed852e2009-09-05 21:47:34 +00005472 else
5473 {
cristybb503372010-05-27 20:51:26 +00005474 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00005475 j;
5476
cristybb503372010-05-27 20:51:26 +00005477 for (j=0; j < (ssize_t) length; j+=2)
cristy3ed852e2009-09-05 21:47:34 +00005478 {
5479 i=p[j] << 8 | p[j+1];
5480 MngInfoDiscardObject(mng_info,i);
5481 }
5482 }
glennrp0fe50b42010-11-16 03:52:51 +00005483
cristy3ed852e2009-09-05 21:47:34 +00005484 if (length)
5485 chunk=(unsigned char *) RelinquishMagickMemory(chunk);
glennrp0fe50b42010-11-16 03:52:51 +00005486
cristy3ed852e2009-09-05 21:47:34 +00005487 continue;
5488 }
glennrp47b9dd52010-11-24 18:12:06 +00005489
cristy3ed852e2009-09-05 21:47:34 +00005490 if (memcmp(type,mng_MOVE,4) == 0)
5491 {
cristybb503372010-05-27 20:51:26 +00005492 size_t
cristy3ed852e2009-09-05 21:47:34 +00005493 first_object,
5494 last_object;
5495
glennrp47b9dd52010-11-24 18:12:06 +00005496 /* read MOVE */
5497
cristy3ed852e2009-09-05 21:47:34 +00005498 first_object=(p[0] << 8) | p[1];
5499 last_object=(p[2] << 8) | p[3];
cristybb503372010-05-27 20:51:26 +00005500 for (i=(ssize_t) first_object; i <= (ssize_t) last_object; i++)
cristy3ed852e2009-09-05 21:47:34 +00005501 {
5502 if (mng_info->exists[i] && !mng_info->frozen[i])
5503 {
5504 MngPair
5505 new_pair;
5506
5507 MngPair
5508 old_pair;
5509
5510 old_pair.a=mng_info->x_off[i];
5511 old_pair.b=mng_info->y_off[i];
5512 new_pair=mng_read_pair(old_pair,(int) p[4],&p[5]);
5513 mng_info->x_off[i]=new_pair.a;
5514 mng_info->y_off[i]=new_pair.b;
5515 }
5516 }
glennrp47b9dd52010-11-24 18:12:06 +00005517
cristy3ed852e2009-09-05 21:47:34 +00005518 chunk=(unsigned char *) RelinquishMagickMemory(chunk);
5519 continue;
5520 }
5521
5522 if (memcmp(type,mng_LOOP,4) == 0)
5523 {
cristybb503372010-05-27 20:51:26 +00005524 ssize_t loop_iters=1;
cristy3ed852e2009-09-05 21:47:34 +00005525 loop_level=chunk[0];
5526 mng_info->loop_active[loop_level]=1; /* mark loop active */
glennrp47b9dd52010-11-24 18:12:06 +00005527
5528 /* Record starting point. */
cristy8182b072010-05-30 20:10:53 +00005529 loop_iters=mng_get_long(&chunk[1]);
glennrp0fe50b42010-11-16 03:52:51 +00005530
cristy3ed852e2009-09-05 21:47:34 +00005531 if (logging != MagickFalse)
5532 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00005533 " LOOP level %.20g has %.20g iterations ",(double) loop_level,
5534 (double) loop_iters);
glennrp0fe50b42010-11-16 03:52:51 +00005535
cristy3ed852e2009-09-05 21:47:34 +00005536 if (loop_iters == 0)
5537 skipping_loop=loop_level;
glennrp0fe50b42010-11-16 03:52:51 +00005538
cristy3ed852e2009-09-05 21:47:34 +00005539 else
5540 {
5541 mng_info->loop_jump[loop_level]=TellBlob(image);
5542 mng_info->loop_count[loop_level]=loop_iters;
5543 }
glennrp0fe50b42010-11-16 03:52:51 +00005544
cristy3ed852e2009-09-05 21:47:34 +00005545 mng_info->loop_iteration[loop_level]=0;
5546 chunk=(unsigned char *) RelinquishMagickMemory(chunk);
5547 continue;
5548 }
glennrp47b9dd52010-11-24 18:12:06 +00005549
cristy3ed852e2009-09-05 21:47:34 +00005550 if (memcmp(type,mng_ENDL,4) == 0)
5551 {
5552 loop_level=chunk[0];
glennrp47b9dd52010-11-24 18:12:06 +00005553
cristy3ed852e2009-09-05 21:47:34 +00005554 if (skipping_loop > 0)
5555 {
5556 if (skipping_loop == loop_level)
5557 {
5558 /*
5559 Found end of zero-iteration loop.
5560 */
5561 skipping_loop=(-1);
5562 mng_info->loop_active[loop_level]=0;
5563 }
5564 }
glennrp47b9dd52010-11-24 18:12:06 +00005565
cristy3ed852e2009-09-05 21:47:34 +00005566 else
5567 {
5568 if (mng_info->loop_active[loop_level] == 1)
5569 {
5570 mng_info->loop_count[loop_level]--;
5571 mng_info->loop_iteration[loop_level]++;
glennrp0fe50b42010-11-16 03:52:51 +00005572
cristy3ed852e2009-09-05 21:47:34 +00005573 if (logging != MagickFalse)
5574 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
glennrp0fe50b42010-11-16 03:52:51 +00005575 " ENDL: LOOP level %.20g has %.20g remaining iters ",
cristye8c25f92010-06-03 00:53:06 +00005576 (double) loop_level,(double)
cristyf2faecf2010-05-28 19:19:36 +00005577 mng_info->loop_count[loop_level]);
glennrp47b9dd52010-11-24 18:12:06 +00005578
cristy3ed852e2009-09-05 21:47:34 +00005579 if (mng_info->loop_count[loop_level] != 0)
5580 {
5581 offset=SeekBlob(image,mng_info->loop_jump[loop_level],
5582 SEEK_SET);
glennrp0fe50b42010-11-16 03:52:51 +00005583
cristy3ed852e2009-09-05 21:47:34 +00005584 if (offset < 0)
5585 ThrowReaderException(CorruptImageError,
5586 "ImproperImageHeader");
5587 }
glennrp47b9dd52010-11-24 18:12:06 +00005588
cristy3ed852e2009-09-05 21:47:34 +00005589 else
5590 {
5591 short
5592 last_level;
5593
5594 /*
5595 Finished loop.
5596 */
5597 mng_info->loop_active[loop_level]=0;
5598 last_level=(-1);
5599 for (i=0; i < loop_level; i++)
5600 if (mng_info->loop_active[i] == 1)
5601 last_level=(short) i;
5602 loop_level=last_level;
5603 }
5604 }
5605 }
glennrp47b9dd52010-11-24 18:12:06 +00005606
cristy3ed852e2009-09-05 21:47:34 +00005607 chunk=(unsigned char *) RelinquishMagickMemory(chunk);
5608 continue;
5609 }
glennrp47b9dd52010-11-24 18:12:06 +00005610
cristy3ed852e2009-09-05 21:47:34 +00005611 if (memcmp(type,mng_CLON,4) == 0)
5612 {
5613 if (mng_info->clon_warning == 0)
cristyc82a27b2011-10-21 01:07:16 +00005614 (void) ThrowMagickException(exception,GetMagickModule(),
cristy3ed852e2009-09-05 21:47:34 +00005615 CoderError,"CLON is not implemented yet","`%s'",
5616 image->filename);
glennrp47b9dd52010-11-24 18:12:06 +00005617
cristy3ed852e2009-09-05 21:47:34 +00005618 mng_info->clon_warning++;
5619 }
glennrp47b9dd52010-11-24 18:12:06 +00005620
cristy3ed852e2009-09-05 21:47:34 +00005621 if (memcmp(type,mng_MAGN,4) == 0)
5622 {
5623 png_uint_16
5624 magn_first,
5625 magn_last,
5626 magn_mb,
5627 magn_ml,
5628 magn_mr,
5629 magn_mt,
5630 magn_mx,
5631 magn_my,
5632 magn_methx,
5633 magn_methy;
5634
5635 if (length > 1)
5636 magn_first=(p[0] << 8) | p[1];
glennrp0fe50b42010-11-16 03:52:51 +00005637
cristy3ed852e2009-09-05 21:47:34 +00005638 else
5639 magn_first=0;
glennrp0fe50b42010-11-16 03:52:51 +00005640
cristy3ed852e2009-09-05 21:47:34 +00005641 if (length > 3)
5642 magn_last=(p[2] << 8) | p[3];
glennrp0fe50b42010-11-16 03:52:51 +00005643
cristy3ed852e2009-09-05 21:47:34 +00005644 else
5645 magn_last=magn_first;
5646#ifndef MNG_OBJECT_BUFFERS
5647 if (magn_first || magn_last)
5648 if (mng_info->magn_warning == 0)
5649 {
cristyc82a27b2011-10-21 01:07:16 +00005650 (void) ThrowMagickException(exception,
cristy3ed852e2009-09-05 21:47:34 +00005651 GetMagickModule(),CoderError,
5652 "MAGN is not implemented yet for nonzero objects",
5653 "`%s'",image->filename);
glennrp47b9dd52010-11-24 18:12:06 +00005654
cristy3ed852e2009-09-05 21:47:34 +00005655 mng_info->magn_warning++;
5656 }
5657#endif
5658 if (length > 4)
5659 magn_methx=p[4];
glennrp47b9dd52010-11-24 18:12:06 +00005660
cristy3ed852e2009-09-05 21:47:34 +00005661 else
5662 magn_methx=0;
5663
5664 if (length > 6)
5665 magn_mx=(p[5] << 8) | p[6];
glennrp47b9dd52010-11-24 18:12:06 +00005666
cristy3ed852e2009-09-05 21:47:34 +00005667 else
5668 magn_mx=1;
glennrp47b9dd52010-11-24 18:12:06 +00005669
cristy3ed852e2009-09-05 21:47:34 +00005670 if (magn_mx == 0)
5671 magn_mx=1;
5672
5673 if (length > 8)
5674 magn_my=(p[7] << 8) | p[8];
glennrp47b9dd52010-11-24 18:12:06 +00005675
cristy3ed852e2009-09-05 21:47:34 +00005676 else
5677 magn_my=magn_mx;
glennrp47b9dd52010-11-24 18:12:06 +00005678
cristy3ed852e2009-09-05 21:47:34 +00005679 if (magn_my == 0)
5680 magn_my=1;
5681
5682 if (length > 10)
5683 magn_ml=(p[9] << 8) | p[10];
glennrp47b9dd52010-11-24 18:12:06 +00005684
cristy3ed852e2009-09-05 21:47:34 +00005685 else
5686 magn_ml=magn_mx;
glennrp47b9dd52010-11-24 18:12:06 +00005687
cristy3ed852e2009-09-05 21:47:34 +00005688 if (magn_ml == 0)
5689 magn_ml=1;
5690
5691 if (length > 12)
5692 magn_mr=(p[11] << 8) | p[12];
glennrp47b9dd52010-11-24 18:12:06 +00005693
cristy3ed852e2009-09-05 21:47:34 +00005694 else
5695 magn_mr=magn_mx;
glennrp47b9dd52010-11-24 18:12:06 +00005696
cristy3ed852e2009-09-05 21:47:34 +00005697 if (magn_mr == 0)
5698 magn_mr=1;
5699
5700 if (length > 14)
5701 magn_mt=(p[13] << 8) | p[14];
glennrp47b9dd52010-11-24 18:12:06 +00005702
cristy3ed852e2009-09-05 21:47:34 +00005703 else
5704 magn_mt=magn_my;
glennrp47b9dd52010-11-24 18:12:06 +00005705
cristy3ed852e2009-09-05 21:47:34 +00005706 if (magn_mt == 0)
5707 magn_mt=1;
5708
5709 if (length > 16)
5710 magn_mb=(p[15] << 8) | p[16];
glennrp47b9dd52010-11-24 18:12:06 +00005711
cristy3ed852e2009-09-05 21:47:34 +00005712 else
5713 magn_mb=magn_my;
glennrp47b9dd52010-11-24 18:12:06 +00005714
cristy3ed852e2009-09-05 21:47:34 +00005715 if (magn_mb == 0)
5716 magn_mb=1;
5717
5718 if (length > 17)
5719 magn_methy=p[17];
glennrp47b9dd52010-11-24 18:12:06 +00005720
cristy3ed852e2009-09-05 21:47:34 +00005721 else
5722 magn_methy=magn_methx;
5723
glennrp47b9dd52010-11-24 18:12:06 +00005724
cristy3ed852e2009-09-05 21:47:34 +00005725 if (magn_methx > 5 || magn_methy > 5)
5726 if (mng_info->magn_warning == 0)
5727 {
cristyc82a27b2011-10-21 01:07:16 +00005728 (void) ThrowMagickException(exception,
cristy3ed852e2009-09-05 21:47:34 +00005729 GetMagickModule(),CoderError,
5730 "Unknown MAGN method in MNG datastream","`%s'",
5731 image->filename);
glennrp47b9dd52010-11-24 18:12:06 +00005732
cristy3ed852e2009-09-05 21:47:34 +00005733 mng_info->magn_warning++;
5734 }
5735#ifdef MNG_OBJECT_BUFFERS
5736 /* Magnify existing objects in the range magn_first to magn_last */
5737#endif
5738 if (magn_first == 0 || magn_last == 0)
5739 {
5740 /* Save the magnification factors for object 0 */
5741 mng_info->magn_mb=magn_mb;
5742 mng_info->magn_ml=magn_ml;
5743 mng_info->magn_mr=magn_mr;
5744 mng_info->magn_mt=magn_mt;
5745 mng_info->magn_mx=magn_mx;
5746 mng_info->magn_my=magn_my;
5747 mng_info->magn_methx=magn_methx;
5748 mng_info->magn_methy=magn_methy;
5749 }
5750 }
glennrp47b9dd52010-11-24 18:12:06 +00005751
cristy3ed852e2009-09-05 21:47:34 +00005752 if (memcmp(type,mng_PAST,4) == 0)
5753 {
5754 if (mng_info->past_warning == 0)
cristyc82a27b2011-10-21 01:07:16 +00005755 (void) ThrowMagickException(exception,GetMagickModule(),
cristy3ed852e2009-09-05 21:47:34 +00005756 CoderError,"PAST is not implemented yet","`%s'",
5757 image->filename);
glennrp47b9dd52010-11-24 18:12:06 +00005758
cristy3ed852e2009-09-05 21:47:34 +00005759 mng_info->past_warning++;
5760 }
glennrp47b9dd52010-11-24 18:12:06 +00005761
cristy3ed852e2009-09-05 21:47:34 +00005762 if (memcmp(type,mng_SHOW,4) == 0)
5763 {
5764 if (mng_info->show_warning == 0)
cristyc82a27b2011-10-21 01:07:16 +00005765 (void) ThrowMagickException(exception,GetMagickModule(),
cristy3ed852e2009-09-05 21:47:34 +00005766 CoderError,"SHOW is not implemented yet","`%s'",
5767 image->filename);
glennrp47b9dd52010-11-24 18:12:06 +00005768
cristy3ed852e2009-09-05 21:47:34 +00005769 mng_info->show_warning++;
5770 }
glennrp47b9dd52010-11-24 18:12:06 +00005771
cristy3ed852e2009-09-05 21:47:34 +00005772 if (memcmp(type,mng_sBIT,4) == 0)
5773 {
5774 if (length < 4)
5775 mng_info->have_global_sbit=MagickFalse;
glennrp47b9dd52010-11-24 18:12:06 +00005776
cristy3ed852e2009-09-05 21:47:34 +00005777 else
5778 {
5779 mng_info->global_sbit.gray=p[0];
5780 mng_info->global_sbit.red=p[0];
5781 mng_info->global_sbit.green=p[1];
5782 mng_info->global_sbit.blue=p[2];
5783 mng_info->global_sbit.alpha=p[3];
5784 mng_info->have_global_sbit=MagickTrue;
5785 }
5786 }
5787 if (memcmp(type,mng_pHYs,4) == 0)
5788 {
5789 if (length > 8)
5790 {
5791 mng_info->global_x_pixels_per_unit=
cristy8182b072010-05-30 20:10:53 +00005792 (size_t) mng_get_long(p);
cristy3ed852e2009-09-05 21:47:34 +00005793 mng_info->global_y_pixels_per_unit=
cristy8182b072010-05-30 20:10:53 +00005794 (size_t) mng_get_long(&p[4]);
cristy3ed852e2009-09-05 21:47:34 +00005795 mng_info->global_phys_unit_type=p[8];
5796 mng_info->have_global_phys=MagickTrue;
5797 }
glennrp47b9dd52010-11-24 18:12:06 +00005798
cristy3ed852e2009-09-05 21:47:34 +00005799 else
5800 mng_info->have_global_phys=MagickFalse;
5801 }
5802 if (memcmp(type,mng_pHYg,4) == 0)
5803 {
5804 if (mng_info->phyg_warning == 0)
cristyc82a27b2011-10-21 01:07:16 +00005805 (void) ThrowMagickException(exception,GetMagickModule(),
cristy3ed852e2009-09-05 21:47:34 +00005806 CoderError,"pHYg is not implemented.","`%s'",image->filename);
glennrp47b9dd52010-11-24 18:12:06 +00005807
cristy3ed852e2009-09-05 21:47:34 +00005808 mng_info->phyg_warning++;
5809 }
5810 if (memcmp(type,mng_BASI,4) == 0)
5811 {
5812 skip_to_iend=MagickTrue;
glennrp47b9dd52010-11-24 18:12:06 +00005813
cristy3ed852e2009-09-05 21:47:34 +00005814 if (mng_info->basi_warning == 0)
cristyc82a27b2011-10-21 01:07:16 +00005815 (void) ThrowMagickException(exception,GetMagickModule(),
cristy3ed852e2009-09-05 21:47:34 +00005816 CoderError,"BASI is not implemented yet","`%s'",
5817 image->filename);
glennrp47b9dd52010-11-24 18:12:06 +00005818
cristy3ed852e2009-09-05 21:47:34 +00005819 mng_info->basi_warning++;
5820#ifdef MNG_BASI_SUPPORTED
cristybb503372010-05-27 20:51:26 +00005821 basi_width=(size_t) ((p[0] << 24) | (p[1] << 16) |
cristy3ed852e2009-09-05 21:47:34 +00005822 (p[2] << 8) | p[3]);
cristybb503372010-05-27 20:51:26 +00005823 basi_height=(size_t) ((p[4] << 24) | (p[5] << 16) |
cristy3ed852e2009-09-05 21:47:34 +00005824 (p[6] << 8) | p[7]);
5825 basi_color_type=p[8];
5826 basi_compression_method=p[9];
5827 basi_filter_type=p[10];
5828 basi_interlace_method=p[11];
5829 if (length > 11)
5830 basi_red=(p[12] << 8) & p[13];
glennrp47b9dd52010-11-24 18:12:06 +00005831
cristy3ed852e2009-09-05 21:47:34 +00005832 else
5833 basi_red=0;
glennrp47b9dd52010-11-24 18:12:06 +00005834
cristy3ed852e2009-09-05 21:47:34 +00005835 if (length > 13)
5836 basi_green=(p[14] << 8) & p[15];
glennrp47b9dd52010-11-24 18:12:06 +00005837
cristy3ed852e2009-09-05 21:47:34 +00005838 else
5839 basi_green=0;
glennrp47b9dd52010-11-24 18:12:06 +00005840
cristy3ed852e2009-09-05 21:47:34 +00005841 if (length > 15)
5842 basi_blue=(p[16] << 8) & p[17];
glennrp47b9dd52010-11-24 18:12:06 +00005843
cristy3ed852e2009-09-05 21:47:34 +00005844 else
5845 basi_blue=0;
glennrp47b9dd52010-11-24 18:12:06 +00005846
cristy3ed852e2009-09-05 21:47:34 +00005847 if (length > 17)
5848 basi_alpha=(p[18] << 8) & p[19];
glennrp47b9dd52010-11-24 18:12:06 +00005849
cristy3ed852e2009-09-05 21:47:34 +00005850 else
5851 {
5852 if (basi_sample_depth == 16)
5853 basi_alpha=65535L;
5854 else
5855 basi_alpha=255;
5856 }
glennrp47b9dd52010-11-24 18:12:06 +00005857
cristy3ed852e2009-09-05 21:47:34 +00005858 if (length > 19)
5859 basi_viewable=p[20];
glennrp47b9dd52010-11-24 18:12:06 +00005860
cristy3ed852e2009-09-05 21:47:34 +00005861 else
5862 basi_viewable=0;
glennrp47b9dd52010-11-24 18:12:06 +00005863
cristy3ed852e2009-09-05 21:47:34 +00005864#endif
5865 chunk=(unsigned char *) RelinquishMagickMemory(chunk);
5866 continue;
5867 }
glennrp47b9dd52010-11-24 18:12:06 +00005868
cristy3ed852e2009-09-05 21:47:34 +00005869 if (memcmp(type,mng_IHDR,4)
5870#if defined(JNG_SUPPORTED)
5871 && memcmp(type,mng_JHDR,4)
5872#endif
5873 )
5874 {
5875 /* Not an IHDR or JHDR chunk */
5876 if (length)
5877 chunk=(unsigned char *) RelinquishMagickMemory(chunk);
glennrp47b9dd52010-11-24 18:12:06 +00005878
cristy3ed852e2009-09-05 21:47:34 +00005879 continue;
5880 }
5881/* Process IHDR */
5882 if (logging != MagickFalse)
5883 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
5884 " Processing %c%c%c%c chunk",type[0],type[1],type[2],type[3]);
glennrp47b9dd52010-11-24 18:12:06 +00005885
cristy3ed852e2009-09-05 21:47:34 +00005886 mng_info->exists[object_id]=MagickTrue;
5887 mng_info->viewable[object_id]=MagickTrue;
glennrp47b9dd52010-11-24 18:12:06 +00005888
cristy3ed852e2009-09-05 21:47:34 +00005889 if (mng_info->invisible[object_id])
5890 {
5891 if (logging != MagickFalse)
5892 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
5893 " Skipping invisible object");
glennrp47b9dd52010-11-24 18:12:06 +00005894
cristy3ed852e2009-09-05 21:47:34 +00005895 skip_to_iend=MagickTrue;
5896 chunk=(unsigned char *) RelinquishMagickMemory(chunk);
5897 continue;
5898 }
5899#if defined(MNG_INSERT_LAYERS)
5900 if (length < 8)
5901 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
glennrp47b9dd52010-11-24 18:12:06 +00005902
cristy8182b072010-05-30 20:10:53 +00005903 image_width=(size_t) mng_get_long(p);
5904 image_height=(size_t) mng_get_long(&p[4]);
cristy3ed852e2009-09-05 21:47:34 +00005905#endif
5906 chunk=(unsigned char *) RelinquishMagickMemory(chunk);
5907
5908 /*
5909 Insert a transparent background layer behind the entire animation
5910 if it is not full screen.
5911 */
5912#if defined(MNG_INSERT_LAYERS)
5913 if (insert_layers && mng_type && first_mng_object)
5914 {
5915 if ((mng_info->clip.left > 0) || (mng_info->clip.top > 0) ||
5916 (image_width < mng_info->mng_width) ||
cristybb503372010-05-27 20:51:26 +00005917 (mng_info->clip.right < (ssize_t) mng_info->mng_width) ||
cristy3ed852e2009-09-05 21:47:34 +00005918 (image_height < mng_info->mng_height) ||
cristybb503372010-05-27 20:51:26 +00005919 (mng_info->clip.bottom < (ssize_t) mng_info->mng_height))
cristy3ed852e2009-09-05 21:47:34 +00005920 {
cristy4c08aed2011-07-01 19:47:50 +00005921 if (GetAuthenticPixelQueue(image) != (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00005922 {
5923 /*
5924 Allocate next image structure.
5925 */
cristy9950d572011-10-01 18:22:35 +00005926 AcquireNextImage(image_info,image,exception);
glennrp47b9dd52010-11-24 18:12:06 +00005927
cristy3ed852e2009-09-05 21:47:34 +00005928 if (GetNextImageInList(image) == (Image *) NULL)
5929 {
5930 image=DestroyImageList(image);
5931 MngInfoFreeStruct(mng_info,&have_mng_structure);
5932 return((Image *) NULL);
5933 }
glennrp47b9dd52010-11-24 18:12:06 +00005934
cristy3ed852e2009-09-05 21:47:34 +00005935 image=SyncNextImageInList(image);
5936 }
5937 mng_info->image=image;
glennrp47b9dd52010-11-24 18:12:06 +00005938
cristy3ed852e2009-09-05 21:47:34 +00005939 if (term_chunk_found)
5940 {
5941 image->start_loop=MagickTrue;
5942 image->iterations=mng_iterations;
5943 term_chunk_found=MagickFalse;
5944 }
glennrp47b9dd52010-11-24 18:12:06 +00005945
cristy3ed852e2009-09-05 21:47:34 +00005946 else
5947 image->start_loop=MagickFalse;
glennrp47b9dd52010-11-24 18:12:06 +00005948
5949 /* Make a background rectangle. */
5950
cristy3ed852e2009-09-05 21:47:34 +00005951 image->delay=0;
5952 image->columns=mng_info->mng_width;
5953 image->rows=mng_info->mng_height;
5954 image->page.width=mng_info->mng_width;
5955 image->page.height=mng_info->mng_height;
5956 image->page.x=0;
5957 image->page.y=0;
5958 image->background_color=mng_background_color;
cristyea1a8aa2011-10-20 13:24:06 +00005959 (void) SetImageBackgroundColor(image,exception);
cristy3ed852e2009-09-05 21:47:34 +00005960 if (logging != MagickFalse)
5961 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00005962 " Inserted transparent background layer, W=%.20g, H=%.20g",
5963 (double) mng_info->mng_width,(double) mng_info->mng_height);
cristy3ed852e2009-09-05 21:47:34 +00005964 }
5965 }
5966 /*
5967 Insert a background layer behind the upcoming image if
5968 framing_mode is 3, and we haven't already inserted one.
5969 */
5970 if (insert_layers && (mng_info->framing_mode == 3) &&
5971 (subframe_width) && (subframe_height) && (simplicity == 0 ||
5972 (simplicity & 0x08)))
5973 {
cristy4c08aed2011-07-01 19:47:50 +00005974 if (GetAuthenticPixelQueue(image) != (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00005975 {
5976 /*
5977 Allocate next image structure.
5978 */
cristy9950d572011-10-01 18:22:35 +00005979 AcquireNextImage(image_info,image,exception);
glennrp47b9dd52010-11-24 18:12:06 +00005980
cristy3ed852e2009-09-05 21:47:34 +00005981 if (GetNextImageInList(image) == (Image *) NULL)
5982 {
5983 image=DestroyImageList(image);
5984 MngInfoFreeStruct(mng_info,&have_mng_structure);
5985 return((Image *) NULL);
5986 }
glennrp47b9dd52010-11-24 18:12:06 +00005987
cristy3ed852e2009-09-05 21:47:34 +00005988 image=SyncNextImageInList(image);
5989 }
glennrp0fe50b42010-11-16 03:52:51 +00005990
cristy3ed852e2009-09-05 21:47:34 +00005991 mng_info->image=image;
glennrp0fe50b42010-11-16 03:52:51 +00005992
cristy3ed852e2009-09-05 21:47:34 +00005993 if (term_chunk_found)
5994 {
5995 image->start_loop=MagickTrue;
5996 image->iterations=mng_iterations;
5997 term_chunk_found=MagickFalse;
5998 }
glennrp0fe50b42010-11-16 03:52:51 +00005999
cristy3ed852e2009-09-05 21:47:34 +00006000 else
6001 image->start_loop=MagickFalse;
glennrp47b9dd52010-11-24 18:12:06 +00006002
cristy3ed852e2009-09-05 21:47:34 +00006003 image->delay=0;
6004 image->columns=subframe_width;
6005 image->rows=subframe_height;
6006 image->page.width=subframe_width;
6007 image->page.height=subframe_height;
6008 image->page.x=mng_info->clip.left;
6009 image->page.y=mng_info->clip.top;
6010 image->background_color=mng_background_color;
6011 image->matte=MagickFalse;
cristyea1a8aa2011-10-20 13:24:06 +00006012 (void) SetImageBackgroundColor(image,exception);
glennrp0fe50b42010-11-16 03:52:51 +00006013
cristy3ed852e2009-09-05 21:47:34 +00006014 if (logging != MagickFalse)
6015 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
glennrp0fe50b42010-11-16 03:52:51 +00006016 " Insert background layer, L=%.20g, R=%.20g T=%.20g, B=%.20g",
cristye8c25f92010-06-03 00:53:06 +00006017 (double) mng_info->clip.left,(double) mng_info->clip.right,
6018 (double) mng_info->clip.top,(double) mng_info->clip.bottom);
cristy3ed852e2009-09-05 21:47:34 +00006019 }
6020#endif /* MNG_INSERT_LAYERS */
6021 first_mng_object=MagickFalse;
glennrp47b9dd52010-11-24 18:12:06 +00006022
cristy4c08aed2011-07-01 19:47:50 +00006023 if (GetAuthenticPixelQueue(image) != (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00006024 {
6025 /*
6026 Allocate next image structure.
6027 */
cristy9950d572011-10-01 18:22:35 +00006028 AcquireNextImage(image_info,image,exception);
glennrp47b9dd52010-11-24 18:12:06 +00006029
cristy3ed852e2009-09-05 21:47:34 +00006030 if (GetNextImageInList(image) == (Image *) NULL)
6031 {
6032 image=DestroyImageList(image);
6033 MngInfoFreeStruct(mng_info,&have_mng_structure);
6034 return((Image *) NULL);
6035 }
glennrp47b9dd52010-11-24 18:12:06 +00006036
cristy3ed852e2009-09-05 21:47:34 +00006037 image=SyncNextImageInList(image);
6038 }
6039 mng_info->image=image;
6040 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
6041 GetBlobSize(image));
glennrp0fe50b42010-11-16 03:52:51 +00006042
cristy3ed852e2009-09-05 21:47:34 +00006043 if (status == MagickFalse)
6044 break;
glennrp0fe50b42010-11-16 03:52:51 +00006045
cristy3ed852e2009-09-05 21:47:34 +00006046 if (term_chunk_found)
6047 {
6048 image->start_loop=MagickTrue;
6049 term_chunk_found=MagickFalse;
6050 }
glennrp0fe50b42010-11-16 03:52:51 +00006051
cristy3ed852e2009-09-05 21:47:34 +00006052 else
6053 image->start_loop=MagickFalse;
glennrp0fe50b42010-11-16 03:52:51 +00006054
cristy3ed852e2009-09-05 21:47:34 +00006055 if (mng_info->framing_mode == 1 || mng_info->framing_mode == 3)
6056 {
6057 image->delay=frame_delay;
6058 frame_delay=default_frame_delay;
6059 }
glennrp0fe50b42010-11-16 03:52:51 +00006060
cristy3ed852e2009-09-05 21:47:34 +00006061 else
6062 image->delay=0;
glennrp0fe50b42010-11-16 03:52:51 +00006063
cristy3ed852e2009-09-05 21:47:34 +00006064 image->page.width=mng_info->mng_width;
6065 image->page.height=mng_info->mng_height;
6066 image->page.x=mng_info->x_off[object_id];
6067 image->page.y=mng_info->y_off[object_id];
6068 image->iterations=mng_iterations;
glennrp47b9dd52010-11-24 18:12:06 +00006069
cristy3ed852e2009-09-05 21:47:34 +00006070 /*
6071 Seek back to the beginning of the IHDR or JHDR chunk's length field.
6072 */
glennrp47b9dd52010-11-24 18:12:06 +00006073
cristy3ed852e2009-09-05 21:47:34 +00006074 if (logging != MagickFalse)
6075 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
6076 " Seeking back to beginning of %c%c%c%c chunk",type[0],type[1],
6077 type[2],type[3]);
glennrp47b9dd52010-11-24 18:12:06 +00006078
cristybb503372010-05-27 20:51:26 +00006079 offset=SeekBlob(image,-((ssize_t) length+12),SEEK_CUR);
glennrp47b9dd52010-11-24 18:12:06 +00006080
cristy3ed852e2009-09-05 21:47:34 +00006081 if (offset < 0)
6082 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
6083 }
6084
6085 previous=image;
6086 mng_info->image=image;
6087 mng_info->mng_type=mng_type;
6088 mng_info->object_id=object_id;
6089
6090 if (memcmp(type,mng_IHDR,4) == 0)
6091 image=ReadOnePNGImage(mng_info,image_info,exception);
glennrp47b9dd52010-11-24 18:12:06 +00006092
cristy3ed852e2009-09-05 21:47:34 +00006093#if defined(JNG_SUPPORTED)
6094 else
6095 image=ReadOneJNGImage(mng_info,image_info,exception);
6096#endif
6097
6098 if (image == (Image *) NULL)
6099 {
6100 if (IsImageObject(previous) != MagickFalse)
6101 {
6102 (void) DestroyImageList(previous);
6103 (void) CloseBlob(previous);
6104 }
glennrp47b9dd52010-11-24 18:12:06 +00006105
cristy3ed852e2009-09-05 21:47:34 +00006106 MngInfoFreeStruct(mng_info,&have_mng_structure);
6107 return((Image *) NULL);
6108 }
glennrp0fe50b42010-11-16 03:52:51 +00006109
cristy3ed852e2009-09-05 21:47:34 +00006110 if (image->columns == 0 || image->rows == 0)
6111 {
6112 (void) CloseBlob(image);
6113 image=DestroyImageList(image);
6114 MngInfoFreeStruct(mng_info,&have_mng_structure);
6115 return((Image *) NULL);
6116 }
glennrp0fe50b42010-11-16 03:52:51 +00006117
cristy3ed852e2009-09-05 21:47:34 +00006118 mng_info->image=image;
6119
6120 if (mng_type)
6121 {
6122 MngBox
6123 crop_box;
6124
6125 if (mng_info->magn_methx || mng_info->magn_methy)
6126 {
6127 png_uint_32
6128 magnified_height,
6129 magnified_width;
6130
6131 if (logging != MagickFalse)
6132 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
6133 " Processing MNG MAGN chunk");
6134
6135 if (mng_info->magn_methx == 1)
6136 {
6137 magnified_width=mng_info->magn_ml;
glennrp47b9dd52010-11-24 18:12:06 +00006138
cristy3ed852e2009-09-05 21:47:34 +00006139 if (image->columns > 1)
6140 magnified_width += mng_info->magn_mr;
glennrp47b9dd52010-11-24 18:12:06 +00006141
cristy3ed852e2009-09-05 21:47:34 +00006142 if (image->columns > 2)
glennrp47b9dd52010-11-24 18:12:06 +00006143 magnified_width += (png_uint_32)
6144 ((image->columns-2)*(mng_info->magn_mx));
cristy3ed852e2009-09-05 21:47:34 +00006145 }
glennrp47b9dd52010-11-24 18:12:06 +00006146
cristy3ed852e2009-09-05 21:47:34 +00006147 else
6148 {
cristy4e5bc842010-06-09 13:56:01 +00006149 magnified_width=(png_uint_32) image->columns;
glennrp47b9dd52010-11-24 18:12:06 +00006150
cristy3ed852e2009-09-05 21:47:34 +00006151 if (image->columns > 1)
6152 magnified_width += mng_info->magn_ml-1;
glennrp47b9dd52010-11-24 18:12:06 +00006153
cristy3ed852e2009-09-05 21:47:34 +00006154 if (image->columns > 2)
6155 magnified_width += mng_info->magn_mr-1;
glennrp47b9dd52010-11-24 18:12:06 +00006156
cristy3ed852e2009-09-05 21:47:34 +00006157 if (image->columns > 3)
glennrp47b9dd52010-11-24 18:12:06 +00006158 magnified_width += (png_uint_32)
6159 ((image->columns-3)*(mng_info->magn_mx-1));
cristy3ed852e2009-09-05 21:47:34 +00006160 }
glennrp47b9dd52010-11-24 18:12:06 +00006161
cristy3ed852e2009-09-05 21:47:34 +00006162 if (mng_info->magn_methy == 1)
6163 {
6164 magnified_height=mng_info->magn_mt;
glennrp47b9dd52010-11-24 18:12:06 +00006165
cristy3ed852e2009-09-05 21:47:34 +00006166 if (image->rows > 1)
6167 magnified_height += mng_info->magn_mb;
glennrp47b9dd52010-11-24 18:12:06 +00006168
cristy3ed852e2009-09-05 21:47:34 +00006169 if (image->rows > 2)
glennrp47b9dd52010-11-24 18:12:06 +00006170 magnified_height += (png_uint_32)
6171 ((image->rows-2)*(mng_info->magn_my));
cristy3ed852e2009-09-05 21:47:34 +00006172 }
glennrp47b9dd52010-11-24 18:12:06 +00006173
cristy3ed852e2009-09-05 21:47:34 +00006174 else
6175 {
cristy4e5bc842010-06-09 13:56:01 +00006176 magnified_height=(png_uint_32) image->rows;
glennrp47b9dd52010-11-24 18:12:06 +00006177
cristy3ed852e2009-09-05 21:47:34 +00006178 if (image->rows > 1)
6179 magnified_height += mng_info->magn_mt-1;
glennrp47b9dd52010-11-24 18:12:06 +00006180
cristy3ed852e2009-09-05 21:47:34 +00006181 if (image->rows > 2)
6182 magnified_height += mng_info->magn_mb-1;
glennrp47b9dd52010-11-24 18:12:06 +00006183
cristy3ed852e2009-09-05 21:47:34 +00006184 if (image->rows > 3)
glennrp47b9dd52010-11-24 18:12:06 +00006185 magnified_height += (png_uint_32)
6186 ((image->rows-3)*(mng_info->magn_my-1));
cristy3ed852e2009-09-05 21:47:34 +00006187 }
glennrp47b9dd52010-11-24 18:12:06 +00006188
cristy3ed852e2009-09-05 21:47:34 +00006189 if (magnified_height > image->rows ||
6190 magnified_width > image->columns)
6191 {
6192 Image
6193 *large_image;
6194
6195 int
6196 yy;
6197
cristy4c08aed2011-07-01 19:47:50 +00006198 Quantum
cristy3ed852e2009-09-05 21:47:34 +00006199 *next,
6200 *prev;
6201
6202 png_uint_16
6203 magn_methx,
6204 magn_methy;
6205
cristy4c08aed2011-07-01 19:47:50 +00006206 ssize_t
6207 m,
6208 y;
6209
6210 register Quantum
6211 *n,
6212 *q;
6213
6214 register ssize_t
6215 x;
6216
glennrp47b9dd52010-11-24 18:12:06 +00006217 /* Allocate next image structure. */
6218
cristy3ed852e2009-09-05 21:47:34 +00006219 if (logging != MagickFalse)
6220 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
6221 " Allocate magnified image");
glennrp47b9dd52010-11-24 18:12:06 +00006222
cristy9950d572011-10-01 18:22:35 +00006223 AcquireNextImage(image_info,image,exception);
glennrp47b9dd52010-11-24 18:12:06 +00006224
cristy3ed852e2009-09-05 21:47:34 +00006225 if (GetNextImageInList(image) == (Image *) NULL)
6226 {
6227 image=DestroyImageList(image);
6228 MngInfoFreeStruct(mng_info,&have_mng_structure);
6229 return((Image *) NULL);
6230 }
6231
6232 large_image=SyncNextImageInList(image);
6233
6234 large_image->columns=magnified_width;
6235 large_image->rows=magnified_height;
6236
6237 magn_methx=mng_info->magn_methx;
6238 magn_methy=mng_info->magn_methy;
6239
glennrp3faa9a32011-04-23 14:00:25 +00006240#if (MAGICKCORE_QUANTUM_DEPTH > 16)
cristy3ed852e2009-09-05 21:47:34 +00006241#define QM unsigned short
6242 if (magn_methx != 1 || magn_methy != 1)
6243 {
6244 /*
6245 Scale pixels to unsigned shorts to prevent
6246 overflow of intermediate values of interpolations
6247 */
cristybb503372010-05-27 20:51:26 +00006248 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00006249 {
6250 q=GetAuthenticPixels(image,0,y,image->columns,1,
6251 exception);
glennrp47b9dd52010-11-24 18:12:06 +00006252
cristybb503372010-05-27 20:51:26 +00006253 for (x=(ssize_t) image->columns-1; x >= 0; x--)
cristy3ed852e2009-09-05 21:47:34 +00006254 {
cristy4c08aed2011-07-01 19:47:50 +00006255 SetPixelRed(image,ScaleQuantumToShort(
6256 GetPixelRed(image,q)),q);
6257 SetPixelGreen(image,ScaleQuantumToShort(
6258 GetPixelGreen(image,q)),q);
6259 SetPixelBlue(image,ScaleQuantumToShort(
6260 GetPixelBlue(image,q)),q);
6261 SetPixelAlpha(image,ScaleQuantumToShort(
6262 GetPixelAlpha(image,q)),q);
cristyed231572011-07-14 02:18:59 +00006263 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00006264 }
glennrp47b9dd52010-11-24 18:12:06 +00006265
cristy3ed852e2009-09-05 21:47:34 +00006266 if (SyncAuthenticPixels(image,exception) == MagickFalse)
6267 break;
6268 }
6269 }
6270#else
6271#define QM Quantum
6272#endif
6273
6274 if (image->matte != MagickFalse)
cristyea1a8aa2011-10-20 13:24:06 +00006275 (void) SetImageBackgroundColor(large_image,exception);
glennrp47b9dd52010-11-24 18:12:06 +00006276
cristy3ed852e2009-09-05 21:47:34 +00006277 else
6278 {
cristy4c08aed2011-07-01 19:47:50 +00006279 large_image->background_color.alpha=OpaqueAlpha;
cristyea1a8aa2011-10-20 13:24:06 +00006280 (void) SetImageBackgroundColor(large_image,exception);
glennrp47b9dd52010-11-24 18:12:06 +00006281
cristy3ed852e2009-09-05 21:47:34 +00006282 if (magn_methx == 4)
6283 magn_methx=2;
glennrp47b9dd52010-11-24 18:12:06 +00006284
cristy3ed852e2009-09-05 21:47:34 +00006285 if (magn_methx == 5)
6286 magn_methx=3;
glennrp47b9dd52010-11-24 18:12:06 +00006287
cristy3ed852e2009-09-05 21:47:34 +00006288 if (magn_methy == 4)
6289 magn_methy=2;
glennrp47b9dd52010-11-24 18:12:06 +00006290
cristy3ed852e2009-09-05 21:47:34 +00006291 if (magn_methy == 5)
6292 magn_methy=3;
6293 }
6294
6295 /* magnify the rows into the right side of the large image */
6296
6297 if (logging != MagickFalse)
6298 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00006299 " Magnify the rows to %.20g",(double) large_image->rows);
cristybb503372010-05-27 20:51:26 +00006300 m=(ssize_t) mng_info->magn_mt;
cristy3ed852e2009-09-05 21:47:34 +00006301 yy=0;
cristy8a20fa02011-12-27 15:54:31 +00006302 length=(size_t) image->columns*GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00006303 next=(Quantum *) AcquireQuantumMemory(length,sizeof(*next));
6304 prev=(Quantum *) AcquireQuantumMemory(length,sizeof(*prev));
glennrp47b9dd52010-11-24 18:12:06 +00006305
cristy4c08aed2011-07-01 19:47:50 +00006306 if ((prev == (Quantum *) NULL) ||
6307 (next == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00006308 {
6309 image=DestroyImageList(image);
6310 MngInfoFreeStruct(mng_info,&have_mng_structure);
6311 ThrowReaderException(ResourceLimitError,
6312 "MemoryAllocationFailed");
6313 }
glennrp47b9dd52010-11-24 18:12:06 +00006314
cristy3ed852e2009-09-05 21:47:34 +00006315 n=GetAuthenticPixels(image,0,0,image->columns,1,exception);
6316 (void) CopyMagickMemory(next,n,length);
glennrp47b9dd52010-11-24 18:12:06 +00006317
cristybb503372010-05-27 20:51:26 +00006318 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00006319 {
6320 if (y == 0)
cristybb503372010-05-27 20:51:26 +00006321 m=(ssize_t) mng_info->magn_mt;
glennrp47b9dd52010-11-24 18:12:06 +00006322
cristybb503372010-05-27 20:51:26 +00006323 else if (magn_methy > 1 && y == (ssize_t) image->rows-2)
6324 m=(ssize_t) mng_info->magn_mb;
glennrp47b9dd52010-11-24 18:12:06 +00006325
cristybb503372010-05-27 20:51:26 +00006326 else if (magn_methy <= 1 && y == (ssize_t) image->rows-1)
6327 m=(ssize_t) mng_info->magn_mb;
glennrp47b9dd52010-11-24 18:12:06 +00006328
cristybb503372010-05-27 20:51:26 +00006329 else if (magn_methy > 1 && y == (ssize_t) image->rows-1)
cristy3ed852e2009-09-05 21:47:34 +00006330 m=1;
glennrp47b9dd52010-11-24 18:12:06 +00006331
cristy3ed852e2009-09-05 21:47:34 +00006332 else
cristybb503372010-05-27 20:51:26 +00006333 m=(ssize_t) mng_info->magn_my;
glennrp47b9dd52010-11-24 18:12:06 +00006334
cristy3ed852e2009-09-05 21:47:34 +00006335 n=prev;
6336 prev=next;
6337 next=n;
glennrp47b9dd52010-11-24 18:12:06 +00006338
cristybb503372010-05-27 20:51:26 +00006339 if (y < (ssize_t) image->rows-1)
cristy3ed852e2009-09-05 21:47:34 +00006340 {
6341 n=GetAuthenticPixels(image,0,y+1,image->columns,1,
6342 exception);
6343 (void) CopyMagickMemory(next,n,length);
6344 }
glennrp47b9dd52010-11-24 18:12:06 +00006345
cristy3ed852e2009-09-05 21:47:34 +00006346 for (i=0; i < m; i++, yy++)
6347 {
cristy4c08aed2011-07-01 19:47:50 +00006348 register Quantum
cristy3ed852e2009-09-05 21:47:34 +00006349 *pixels;
6350
cristybb503372010-05-27 20:51:26 +00006351 assert(yy < (ssize_t) large_image->rows);
cristy3ed852e2009-09-05 21:47:34 +00006352 pixels=prev;
6353 n=next;
6354 q=GetAuthenticPixels(large_image,0,yy,large_image->columns,
cristy9fff7b42011-04-29 01:09:31 +00006355 1,exception);
cristy97707062011-12-27 18:25:00 +00006356 q+=(large_image->columns-image->columns)*
6357 GetPixelChannels(large_image);
glennrp47b9dd52010-11-24 18:12:06 +00006358
cristybb503372010-05-27 20:51:26 +00006359 for (x=(ssize_t) image->columns-1; x >= 0; x--)
cristy3ed852e2009-09-05 21:47:34 +00006360 {
glennrpfd05d622011-02-25 04:10:33 +00006361 /* To do: get color as function of indexes[x] */
cristy3ed852e2009-09-05 21:47:34 +00006362 /*
6363 if (image->storage_class == PseudoClass)
6364 {
6365 }
6366 */
6367
6368 if (magn_methy <= 1)
6369 {
glennrpbb4f99d2011-05-22 11:13:17 +00006370 /* replicate previous */
cristy4c08aed2011-07-01 19:47:50 +00006371 SetPixelRed(large_image,GetPixelRed(image,pixels),q);
glennrp847370c2011-07-05 17:37:15 +00006372 SetPixelGreen(large_image,GetPixelGreen(image,
6373 pixels),q);
6374 SetPixelBlue(large_image,GetPixelBlue(image,
6375 pixels),q);
6376 SetPixelAlpha(large_image,GetPixelAlpha(image,
6377 pixels),q);
cristy3ed852e2009-09-05 21:47:34 +00006378 }
glennrp47b9dd52010-11-24 18:12:06 +00006379
cristy3ed852e2009-09-05 21:47:34 +00006380 else if (magn_methy == 2 || magn_methy == 4)
6381 {
6382 if (i == 0)
glennrpbb4f99d2011-05-22 11:13:17 +00006383 {
glennrp847370c2011-07-05 17:37:15 +00006384 SetPixelRed(large_image,GetPixelRed(image,
6385 pixels),q);
6386 SetPixelGreen(large_image,GetPixelGreen(image,
6387 pixels),q);
6388 SetPixelBlue(large_image,GetPixelBlue(image,
6389 pixels),q);
6390 SetPixelAlpha(large_image,GetPixelAlpha(image,
6391 pixels),q);
glennrpbb4f99d2011-05-22 11:13:17 +00006392 }
glennrp47b9dd52010-11-24 18:12:06 +00006393
cristy3ed852e2009-09-05 21:47:34 +00006394 else
6395 {
6396 /* Interpolate */
cristy4c08aed2011-07-01 19:47:50 +00006397 SetPixelRed(large_image,((QM) (((ssize_t)
6398 (2*i*(GetPixelRed(image,n)
6399 -GetPixelRed(image,pixels)+m))/
glennrpbb4f99d2011-05-22 11:13:17 +00006400 ((ssize_t) (m*2))
cristy4c08aed2011-07-01 19:47:50 +00006401 +GetPixelRed(image,pixels)))),q);
6402 SetPixelGreen(large_image,((QM) (((ssize_t)
6403 (2*i*(GetPixelGreen(image,n)
6404 -GetPixelGreen(image,pixels)+m))/
glennrpbb4f99d2011-05-22 11:13:17 +00006405 ((ssize_t) (m*2))
cristy4c08aed2011-07-01 19:47:50 +00006406 +GetPixelGreen(image,pixels)))),q);
6407 SetPixelBlue(large_image,((QM) (((ssize_t)
6408 (2*i*(GetPixelBlue(image,n)
6409 -GetPixelBlue(image,pixels)+m))/
glennrpbb4f99d2011-05-22 11:13:17 +00006410 ((ssize_t) (m*2))
cristy4c08aed2011-07-01 19:47:50 +00006411 +GetPixelBlue(image,pixels)))),q);
glennrp47b9dd52010-11-24 18:12:06 +00006412
cristy3ed852e2009-09-05 21:47:34 +00006413 if (image->matte != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +00006414 SetPixelAlpha(large_image, ((QM) (((ssize_t)
6415 (2*i*(GetPixelAlpha(image,n)
6416 -GetPixelAlpha(image,pixels)+m))
glennrpbb4f99d2011-05-22 11:13:17 +00006417 /((ssize_t) (m*2))+
cristy4c08aed2011-07-01 19:47:50 +00006418 GetPixelAlpha(image,pixels)))),q);
cristy3ed852e2009-09-05 21:47:34 +00006419 }
glennrp47b9dd52010-11-24 18:12:06 +00006420
cristy3ed852e2009-09-05 21:47:34 +00006421 if (magn_methy == 4)
6422 {
6423 /* Replicate nearest */
6424 if (i <= ((m+1) << 1))
glennrp847370c2011-07-05 17:37:15 +00006425 SetPixelAlpha(large_image,GetPixelAlpha(image,
6426 pixels),q);
cristy3ed852e2009-09-05 21:47:34 +00006427 else
glennrp847370c2011-07-05 17:37:15 +00006428 SetPixelAlpha(large_image,GetPixelAlpha(image,
6429 n),q);
cristy3ed852e2009-09-05 21:47:34 +00006430 }
6431 }
glennrp47b9dd52010-11-24 18:12:06 +00006432
cristy3ed852e2009-09-05 21:47:34 +00006433 else /* if (magn_methy == 3 || magn_methy == 5) */
6434 {
6435 /* Replicate nearest */
6436 if (i <= ((m+1) << 1))
glennrpbb4f99d2011-05-22 11:13:17 +00006437 {
glennrp847370c2011-07-05 17:37:15 +00006438 SetPixelRed(large_image,GetPixelRed(image,
6439 pixels),q);
6440 SetPixelGreen(large_image,GetPixelGreen(image,
6441 pixels),q);
6442 SetPixelBlue(large_image,GetPixelBlue(image,
6443 pixels),q);
6444 SetPixelAlpha(large_image,GetPixelAlpha(image,
6445 pixels),q);
glennrpbb4f99d2011-05-22 11:13:17 +00006446 }
glennrp47b9dd52010-11-24 18:12:06 +00006447
cristy3ed852e2009-09-05 21:47:34 +00006448 else
glennrpbb4f99d2011-05-22 11:13:17 +00006449 {
cristy4c08aed2011-07-01 19:47:50 +00006450 SetPixelRed(large_image,GetPixelRed(image,n),q);
glennrp847370c2011-07-05 17:37:15 +00006451 SetPixelGreen(large_image,GetPixelGreen(image,n),
6452 q);
6453 SetPixelBlue(large_image,GetPixelBlue(image,n),
6454 q);
6455 SetPixelAlpha(large_image,GetPixelAlpha(image,n),
6456 q);
glennrpbb4f99d2011-05-22 11:13:17 +00006457 }
glennrp47b9dd52010-11-24 18:12:06 +00006458
cristy3ed852e2009-09-05 21:47:34 +00006459 if (magn_methy == 5)
6460 {
cristy4c08aed2011-07-01 19:47:50 +00006461 SetPixelAlpha(large_image,(QM) (((ssize_t) (2*i*
6462 (GetPixelAlpha(image,n)
6463 -GetPixelAlpha(image,pixels))
glennrpbb4f99d2011-05-22 11:13:17 +00006464 +m))/((ssize_t) (m*2))
cristy4c08aed2011-07-01 19:47:50 +00006465 +GetPixelAlpha(image,pixels)),q);
cristy3ed852e2009-09-05 21:47:34 +00006466 }
6467 }
cristyed231572011-07-14 02:18:59 +00006468 n+=GetPixelChannels(image);
6469 q+=GetPixelChannels(large_image);
6470 pixels+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00006471 } /* x */
glennrp47b9dd52010-11-24 18:12:06 +00006472
cristy3ed852e2009-09-05 21:47:34 +00006473 if (SyncAuthenticPixels(large_image,exception) == 0)
6474 break;
glennrp47b9dd52010-11-24 18:12:06 +00006475
cristy3ed852e2009-09-05 21:47:34 +00006476 } /* i */
6477 } /* y */
glennrp47b9dd52010-11-24 18:12:06 +00006478
cristy4c08aed2011-07-01 19:47:50 +00006479 prev=(Quantum *) RelinquishMagickMemory(prev);
6480 next=(Quantum *) RelinquishMagickMemory(next);
cristy3ed852e2009-09-05 21:47:34 +00006481
6482 length=image->columns;
6483
6484 if (logging != MagickFalse)
6485 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
6486 " Delete original image");
6487
6488 DeleteImageFromList(&image);
6489
6490 image=large_image;
6491
6492 mng_info->image=image;
6493
6494 /* magnify the columns */
6495 if (logging != MagickFalse)
6496 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00006497 " Magnify the columns to %.20g",(double) image->columns);
cristy3ed852e2009-09-05 21:47:34 +00006498
cristybb503372010-05-27 20:51:26 +00006499 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00006500 {
cristy4c08aed2011-07-01 19:47:50 +00006501 register Quantum
cristy3ed852e2009-09-05 21:47:34 +00006502 *pixels;
6503
6504 q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
cristyed231572011-07-14 02:18:59 +00006505 pixels=q+(image->columns-length)*GetPixelChannels(image);
6506 n=pixels+GetPixelChannels(image);
glennrp47b9dd52010-11-24 18:12:06 +00006507
cristybb503372010-05-27 20:51:26 +00006508 for (x=(ssize_t) (image->columns-length);
6509 x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00006510 {
cristyed231572011-07-14 02:18:59 +00006511 /* To do: Rewrite using Get/Set***PixelChannel() */
glennrp7c7b3152011-04-26 04:01:27 +00006512
cristybb503372010-05-27 20:51:26 +00006513 if (x == (ssize_t) (image->columns-length))
6514 m=(ssize_t) mng_info->magn_ml;
glennrp47b9dd52010-11-24 18:12:06 +00006515
cristybb503372010-05-27 20:51:26 +00006516 else if (magn_methx > 1 && x == (ssize_t) image->columns-2)
6517 m=(ssize_t) mng_info->magn_mr;
glennrp47b9dd52010-11-24 18:12:06 +00006518
cristybb503372010-05-27 20:51:26 +00006519 else if (magn_methx <= 1 && x == (ssize_t) image->columns-1)
6520 m=(ssize_t) mng_info->magn_mr;
glennrp47b9dd52010-11-24 18:12:06 +00006521
cristybb503372010-05-27 20:51:26 +00006522 else if (magn_methx > 1 && x == (ssize_t) image->columns-1)
cristy3ed852e2009-09-05 21:47:34 +00006523 m=1;
glennrp47b9dd52010-11-24 18:12:06 +00006524
cristy3ed852e2009-09-05 21:47:34 +00006525 else
cristybb503372010-05-27 20:51:26 +00006526 m=(ssize_t) mng_info->magn_mx;
glennrp47b9dd52010-11-24 18:12:06 +00006527
cristy3ed852e2009-09-05 21:47:34 +00006528 for (i=0; i < m; i++)
6529 {
6530 if (magn_methx <= 1)
6531 {
6532 /* replicate previous */
cristy4c08aed2011-07-01 19:47:50 +00006533 SetPixelRed(image,GetPixelRed(image,pixels),q);
6534 SetPixelGreen(image,GetPixelGreen(image,pixels),q);
6535 SetPixelBlue(image,GetPixelBlue(image,pixels),q);
6536 SetPixelAlpha(image,GetPixelAlpha(image,pixels),q);
cristy3ed852e2009-09-05 21:47:34 +00006537 }
glennrp47b9dd52010-11-24 18:12:06 +00006538
cristy3ed852e2009-09-05 21:47:34 +00006539 else if (magn_methx == 2 || magn_methx == 4)
6540 {
6541 if (i == 0)
glennrpbb4f99d2011-05-22 11:13:17 +00006542 {
cristy4c08aed2011-07-01 19:47:50 +00006543 SetPixelRed(image,GetPixelRed(image,pixels),q);
6544 SetPixelGreen(image,GetPixelGreen(image,pixels),q);
6545 SetPixelBlue(image,GetPixelBlue(image,pixels),q);
6546 SetPixelAlpha(image,GetPixelAlpha(image,pixels),q);
glennrpbb4f99d2011-05-22 11:13:17 +00006547 }
glennrp47b9dd52010-11-24 18:12:06 +00006548
cristyed231572011-07-14 02:18:59 +00006549 /* To do: Rewrite using Get/Set***PixelChannel() */
cristy3ed852e2009-09-05 21:47:34 +00006550 else
6551 {
6552 /* Interpolate */
cristy4c08aed2011-07-01 19:47:50 +00006553 SetPixelRed(image,(QM) ((2*i*(
6554 GetPixelRed(image,n)
6555 -GetPixelRed(image,pixels))+m)
glennrpbb4f99d2011-05-22 11:13:17 +00006556 /((ssize_t) (m*2))+
cristy4c08aed2011-07-01 19:47:50 +00006557 GetPixelRed(image,pixels)),q);
glennrpbb4f99d2011-05-22 11:13:17 +00006558
cristy4c08aed2011-07-01 19:47:50 +00006559 SetPixelGreen(image,(QM) ((2*i*(
6560 GetPixelGreen(image,n)
6561 -GetPixelGreen(image,pixels))+m)
glennrpbb4f99d2011-05-22 11:13:17 +00006562 /((ssize_t) (m*2))+
cristy4c08aed2011-07-01 19:47:50 +00006563 GetPixelGreen(image,pixels)),q);
glennrpbb4f99d2011-05-22 11:13:17 +00006564
cristy4c08aed2011-07-01 19:47:50 +00006565 SetPixelBlue(image,(QM) ((2*i*(
6566 GetPixelBlue(image,n)
6567 -GetPixelBlue(image,pixels))+m)
glennrpbb4f99d2011-05-22 11:13:17 +00006568 /((ssize_t) (m*2))+
cristy4c08aed2011-07-01 19:47:50 +00006569 GetPixelBlue(image,pixels)),q);
cristy3ed852e2009-09-05 21:47:34 +00006570 if (image->matte != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +00006571 SetPixelAlpha(image,(QM) ((2*i*(
6572 GetPixelAlpha(image,n)
6573 -GetPixelAlpha(image,pixels))+m)
glennrpbb4f99d2011-05-22 11:13:17 +00006574 /((ssize_t) (m*2))+
cristy4c08aed2011-07-01 19:47:50 +00006575 GetPixelAlpha(image,pixels)),q);
cristy3ed852e2009-09-05 21:47:34 +00006576 }
glennrp47b9dd52010-11-24 18:12:06 +00006577
cristy3ed852e2009-09-05 21:47:34 +00006578 if (magn_methx == 4)
6579 {
6580 /* Replicate nearest */
6581 if (i <= ((m+1) << 1))
glennrpbb4f99d2011-05-22 11:13:17 +00006582 {
cristy4c08aed2011-07-01 19:47:50 +00006583 SetPixelAlpha(image,
6584 GetPixelAlpha(image,pixels)+0,q);
glennrpbb4f99d2011-05-22 11:13:17 +00006585 }
cristy3ed852e2009-09-05 21:47:34 +00006586 else
glennrpbb4f99d2011-05-22 11:13:17 +00006587 {
cristy4c08aed2011-07-01 19:47:50 +00006588 SetPixelAlpha(image,
6589 GetPixelAlpha(image,n)+0,q);
glennrpbb4f99d2011-05-22 11:13:17 +00006590 }
cristy3ed852e2009-09-05 21:47:34 +00006591 }
6592 }
glennrp47b9dd52010-11-24 18:12:06 +00006593
cristy3ed852e2009-09-05 21:47:34 +00006594 else /* if (magn_methx == 3 || magn_methx == 5) */
6595 {
6596 /* Replicate nearest */
6597 if (i <= ((m+1) << 1))
glennrpbb4f99d2011-05-22 11:13:17 +00006598 {
cristy4c08aed2011-07-01 19:47:50 +00006599 SetPixelRed(image,GetPixelRed(image,pixels),q);
6600 SetPixelGreen(image,GetPixelGreen(image,pixels),q);
6601 SetPixelBlue(image,GetPixelBlue(image,pixels),q);
6602 SetPixelAlpha(image,GetPixelAlpha(image,pixels),q);
glennrpbb4f99d2011-05-22 11:13:17 +00006603 }
glennrp47b9dd52010-11-24 18:12:06 +00006604
cristy3ed852e2009-09-05 21:47:34 +00006605 else
glennrpbb4f99d2011-05-22 11:13:17 +00006606 {
cristy4c08aed2011-07-01 19:47:50 +00006607 SetPixelRed(image,GetPixelRed(image,n),q);
6608 SetPixelGreen(image,GetPixelGreen(image,n),q);
6609 SetPixelBlue(image,GetPixelBlue(image,n),q);
6610 SetPixelAlpha(image,GetPixelAlpha(image,n),q);
glennrpbb4f99d2011-05-22 11:13:17 +00006611 }
glennrp47b9dd52010-11-24 18:12:06 +00006612
cristy3ed852e2009-09-05 21:47:34 +00006613 if (magn_methx == 5)
6614 {
6615 /* Interpolate */
cristy4c08aed2011-07-01 19:47:50 +00006616 SetPixelAlpha(image,
6617 (QM) ((2*i*( GetPixelAlpha(image,n)
6618 -GetPixelAlpha(image,pixels))+m)/
glennrpbb4f99d2011-05-22 11:13:17 +00006619 ((ssize_t) (m*2))
cristy4c08aed2011-07-01 19:47:50 +00006620 +GetPixelAlpha(image,pixels)),q);
cristy3ed852e2009-09-05 21:47:34 +00006621 }
6622 }
cristyed231572011-07-14 02:18:59 +00006623 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00006624 }
cristyed231572011-07-14 02:18:59 +00006625 n+=GetPixelChannels(image);
6626 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00006627 }
glennrp47b9dd52010-11-24 18:12:06 +00006628
cristy3ed852e2009-09-05 21:47:34 +00006629 if (SyncAuthenticPixels(image,exception) == MagickFalse)
6630 break;
6631 }
glennrp3faa9a32011-04-23 14:00:25 +00006632#if (MAGICKCORE_QUANTUM_DEPTH > 16)
cristy3ed852e2009-09-05 21:47:34 +00006633 if (magn_methx != 1 || magn_methy != 1)
6634 {
6635 /*
6636 Rescale pixels to Quantum
6637 */
cristybb503372010-05-27 20:51:26 +00006638 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00006639 {
6640 q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
glennrp47b9dd52010-11-24 18:12:06 +00006641
cristybb503372010-05-27 20:51:26 +00006642 for (x=(ssize_t) image->columns-1; x >= 0; x--)
cristy3ed852e2009-09-05 21:47:34 +00006643 {
cristy4c08aed2011-07-01 19:47:50 +00006644 SetPixelRed(image,ScaleShortToQuantum(
6645 GetPixelRed(image,q)),q);
6646 SetPixelGreen(image,ScaleShortToQuantum(
6647 GetPixelGreen(image,q)),q);
6648 SetPixelBlue(image,ScaleShortToQuantum(
6649 GetPixelBlue(image,q)),q);
6650 SetPixelAlpha(image,ScaleShortToQuantum(
6651 GetPixelAlpha(image,q)),q);
cristyed231572011-07-14 02:18:59 +00006652 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00006653 }
glennrp47b9dd52010-11-24 18:12:06 +00006654
cristy3ed852e2009-09-05 21:47:34 +00006655 if (SyncAuthenticPixels(image,exception) == MagickFalse)
6656 break;
6657 }
6658 }
6659#endif
6660 if (logging != MagickFalse)
6661 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
6662 " Finished MAGN processing");
6663 }
6664 }
6665
6666 /*
6667 Crop_box is with respect to the upper left corner of the MNG.
6668 */
6669 crop_box.left=mng_info->image_box.left+mng_info->x_off[object_id];
6670 crop_box.right=mng_info->image_box.right+mng_info->x_off[object_id];
6671 crop_box.top=mng_info->image_box.top+mng_info->y_off[object_id];
6672 crop_box.bottom=mng_info->image_box.bottom+mng_info->y_off[object_id];
6673 crop_box=mng_minimum_box(crop_box,mng_info->clip);
6674 crop_box=mng_minimum_box(crop_box,mng_info->frame);
6675 crop_box=mng_minimum_box(crop_box,mng_info->object_clip[object_id]);
6676 if ((crop_box.left != (mng_info->image_box.left
6677 +mng_info->x_off[object_id])) ||
6678 (crop_box.right != (mng_info->image_box.right
6679 +mng_info->x_off[object_id])) ||
6680 (crop_box.top != (mng_info->image_box.top
6681 +mng_info->y_off[object_id])) ||
6682 (crop_box.bottom != (mng_info->image_box.bottom
6683 +mng_info->y_off[object_id])))
6684 {
6685 if (logging != MagickFalse)
6686 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
6687 " Crop the PNG image");
glennrp47b9dd52010-11-24 18:12:06 +00006688
cristy3ed852e2009-09-05 21:47:34 +00006689 if ((crop_box.left < crop_box.right) &&
6690 (crop_box.top < crop_box.bottom))
6691 {
6692 Image
6693 *im;
6694
6695 RectangleInfo
6696 crop_info;
6697
6698 /*
6699 Crop_info is with respect to the upper left corner of
6700 the image.
6701 */
6702 crop_info.x=(crop_box.left-mng_info->x_off[object_id]);
6703 crop_info.y=(crop_box.top-mng_info->y_off[object_id]);
cristybb503372010-05-27 20:51:26 +00006704 crop_info.width=(size_t) (crop_box.right-crop_box.left);
6705 crop_info.height=(size_t) (crop_box.bottom-crop_box.top);
cristy3ed852e2009-09-05 21:47:34 +00006706 image->page.width=image->columns;
6707 image->page.height=image->rows;
6708 image->page.x=0;
6709 image->page.y=0;
6710 im=CropImage(image,&crop_info,exception);
glennrp47b9dd52010-11-24 18:12:06 +00006711
cristy3ed852e2009-09-05 21:47:34 +00006712 if (im != (Image *) NULL)
6713 {
6714 image->columns=im->columns;
6715 image->rows=im->rows;
6716 im=DestroyImage(im);
6717 image->page.width=image->columns;
6718 image->page.height=image->rows;
6719 image->page.x=crop_box.left;
6720 image->page.y=crop_box.top;
6721 }
6722 }
glennrp47b9dd52010-11-24 18:12:06 +00006723
cristy3ed852e2009-09-05 21:47:34 +00006724 else
6725 {
6726 /*
6727 No pixels in crop area. The MNG spec still requires
6728 a layer, though, so make a single transparent pixel in
6729 the top left corner.
6730 */
6731 image->columns=1;
6732 image->rows=1;
6733 image->colors=2;
cristyea1a8aa2011-10-20 13:24:06 +00006734 (void) SetImageBackgroundColor(image,exception);
cristy3ed852e2009-09-05 21:47:34 +00006735 image->page.width=1;
6736 image->page.height=1;
6737 image->page.x=0;
6738 image->page.y=0;
6739 }
6740 }
6741#ifndef PNG_READ_EMPTY_PLTE_SUPPORTED
6742 image=mng_info->image;
6743#endif
6744 }
6745
glennrp2b013e42010-11-24 16:55:50 +00006746#if (MAGICKCORE_QUANTUM_DEPTH > 16)
6747 /* PNG does not handle depths greater than 16 so reduce it even
6748 * if lossy
6749 */
6750 if (image->depth > 16)
6751 image->depth=16;
6752#endif
6753
glennrp3faa9a32011-04-23 14:00:25 +00006754#if (MAGICKCORE_QUANTUM_DEPTH > 8)
cristyc82a27b2011-10-21 01:07:16 +00006755 if (LosslessReduceDepthOK(image,exception) != MagickFalse)
glennrp8640fb52010-11-23 15:48:26 +00006756 image->depth = 8;
cristy3ed852e2009-09-05 21:47:34 +00006757#endif
glennrpd6afd542010-11-19 01:53:05 +00006758
cristy3ed852e2009-09-05 21:47:34 +00006759 if (image_info->number_scenes != 0)
6760 {
6761 if (mng_info->scenes_found >
cristybb503372010-05-27 20:51:26 +00006762 (ssize_t) (image_info->first_scene+image_info->number_scenes))
cristy3ed852e2009-09-05 21:47:34 +00006763 break;
6764 }
glennrpd6afd542010-11-19 01:53:05 +00006765
cristy3ed852e2009-09-05 21:47:34 +00006766 if (logging != MagickFalse)
6767 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
6768 " Finished reading image datastream.");
glennrpd6afd542010-11-19 01:53:05 +00006769
cristy3ed852e2009-09-05 21:47:34 +00006770 } while (LocaleCompare(image_info->magick,"MNG") == 0);
glennrp47b9dd52010-11-24 18:12:06 +00006771
cristy3ed852e2009-09-05 21:47:34 +00006772 (void) CloseBlob(image);
glennrp47b9dd52010-11-24 18:12:06 +00006773
cristy3ed852e2009-09-05 21:47:34 +00006774 if (logging != MagickFalse)
6775 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
6776 " Finished reading all image datastreams.");
glennrp47b9dd52010-11-24 18:12:06 +00006777
cristy3ed852e2009-09-05 21:47:34 +00006778#if defined(MNG_INSERT_LAYERS)
6779 if (insert_layers && !mng_info->image_found && (mng_info->mng_width) &&
6780 (mng_info->mng_height))
6781 {
6782 /*
6783 Insert a background layer if nothing else was found.
6784 */
6785 if (logging != MagickFalse)
6786 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
6787 " No images found. Inserting a background layer.");
glennrp0fe50b42010-11-16 03:52:51 +00006788
cristy4c08aed2011-07-01 19:47:50 +00006789 if (GetAuthenticPixelQueue(image) != (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00006790 {
6791 /*
6792 Allocate next image structure.
6793 */
cristy9950d572011-10-01 18:22:35 +00006794 AcquireNextImage(image_info,image,exception);
cristy3ed852e2009-09-05 21:47:34 +00006795 if (GetNextImageInList(image) == (Image *) NULL)
6796 {
6797 image=DestroyImageList(image);
6798 MngInfoFreeStruct(mng_info,&have_mng_structure);
glennrp47b9dd52010-11-24 18:12:06 +00006799
cristy3ed852e2009-09-05 21:47:34 +00006800 if (logging != MagickFalse)
6801 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
6802 " Allocation failed, returning NULL.");
glennrp47b9dd52010-11-24 18:12:06 +00006803
cristy3ed852e2009-09-05 21:47:34 +00006804 return((Image *) NULL);
6805 }
6806 image=SyncNextImageInList(image);
6807 }
6808 image->columns=mng_info->mng_width;
6809 image->rows=mng_info->mng_height;
6810 image->page.width=mng_info->mng_width;
6811 image->page.height=mng_info->mng_height;
6812 image->page.x=0;
6813 image->page.y=0;
6814 image->background_color=mng_background_color;
6815 image->matte=MagickFalse;
glennrp0fe50b42010-11-16 03:52:51 +00006816
cristy3ed852e2009-09-05 21:47:34 +00006817 if (image_info->ping == MagickFalse)
cristyea1a8aa2011-10-20 13:24:06 +00006818 (void) SetImageBackgroundColor(image,exception);
glennrp0fe50b42010-11-16 03:52:51 +00006819
cristy3ed852e2009-09-05 21:47:34 +00006820 mng_info->image_found++;
6821 }
6822#endif
6823 image->iterations=mng_iterations;
glennrp0fe50b42010-11-16 03:52:51 +00006824
cristy3ed852e2009-09-05 21:47:34 +00006825 if (mng_iterations == 1)
6826 image->start_loop=MagickTrue;
glennrp0fe50b42010-11-16 03:52:51 +00006827
cristy3ed852e2009-09-05 21:47:34 +00006828 while (GetPreviousImageInList(image) != (Image *) NULL)
6829 {
6830 image_count++;
6831 if (image_count > 10*mng_info->image_found)
6832 {
6833 if (logging != MagickFalse)
6834 (void) LogMagickEvent(CoderEvent,GetMagickModule()," No beginning");
glennrp47b9dd52010-11-24 18:12:06 +00006835
cristyc82a27b2011-10-21 01:07:16 +00006836 (void) ThrowMagickException(exception,GetMagickModule(),
cristy3ed852e2009-09-05 21:47:34 +00006837 CoderError,"Linked list is corrupted, beginning of list not found",
6838 "`%s'",image_info->filename);
glennrp47b9dd52010-11-24 18:12:06 +00006839
cristy3ed852e2009-09-05 21:47:34 +00006840 return((Image *) NULL);
6841 }
glennrp0fe50b42010-11-16 03:52:51 +00006842
cristy3ed852e2009-09-05 21:47:34 +00006843 image=GetPreviousImageInList(image);
glennrp0fe50b42010-11-16 03:52:51 +00006844
cristy3ed852e2009-09-05 21:47:34 +00006845 if (GetNextImageInList(image) == (Image *) NULL)
6846 {
6847 if (logging != MagickFalse)
6848 (void) LogMagickEvent(CoderEvent,GetMagickModule()," Corrupt list");
glennrp47b9dd52010-11-24 18:12:06 +00006849
cristyc82a27b2011-10-21 01:07:16 +00006850 (void) ThrowMagickException(exception,GetMagickModule(),
cristy3ed852e2009-09-05 21:47:34 +00006851 CoderError,"Linked list is corrupted; next_image is NULL","`%s'",
6852 image_info->filename);
6853 }
6854 }
glennrp47b9dd52010-11-24 18:12:06 +00006855
cristy3ed852e2009-09-05 21:47:34 +00006856 if (mng_info->ticks_per_second && mng_info->image_found > 1 &&
6857 GetNextImageInList(image) ==
6858 (Image *) NULL)
6859 {
6860 if (logging != MagickFalse)
6861 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
6862 " First image null");
glennrp47b9dd52010-11-24 18:12:06 +00006863
cristyc82a27b2011-10-21 01:07:16 +00006864 (void) ThrowMagickException(exception,GetMagickModule(),
cristy3ed852e2009-09-05 21:47:34 +00006865 CoderError,"image->next for first image is NULL but shouldn't be.",
6866 "`%s'",image_info->filename);
6867 }
glennrp47b9dd52010-11-24 18:12:06 +00006868
cristy3ed852e2009-09-05 21:47:34 +00006869 if (mng_info->image_found == 0)
6870 {
6871 if (logging != MagickFalse)
6872 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
6873 " No visible images found.");
glennrp47b9dd52010-11-24 18:12:06 +00006874
cristyc82a27b2011-10-21 01:07:16 +00006875 (void) ThrowMagickException(exception,GetMagickModule(),
cristy3ed852e2009-09-05 21:47:34 +00006876 CoderError,"No visible images in file","`%s'",image_info->filename);
glennrp47b9dd52010-11-24 18:12:06 +00006877
cristy3ed852e2009-09-05 21:47:34 +00006878 if (image != (Image *) NULL)
6879 image=DestroyImageList(image);
glennrp47b9dd52010-11-24 18:12:06 +00006880
cristy3ed852e2009-09-05 21:47:34 +00006881 MngInfoFreeStruct(mng_info,&have_mng_structure);
6882 return((Image *) NULL);
6883 }
6884
6885 if (mng_info->ticks_per_second)
6886 final_delay=1UL*MagickMax(image->ticks_per_second,1L)*
6887 final_delay/mng_info->ticks_per_second;
glennrp0fe50b42010-11-16 03:52:51 +00006888
cristy3ed852e2009-09-05 21:47:34 +00006889 else
6890 image->start_loop=MagickTrue;
glennrp0fe50b42010-11-16 03:52:51 +00006891
cristy3ed852e2009-09-05 21:47:34 +00006892 /* Find final nonzero image delay */
6893 final_image_delay=0;
glennrp0fe50b42010-11-16 03:52:51 +00006894
cristy3ed852e2009-09-05 21:47:34 +00006895 while (GetNextImageInList(image) != (Image *) NULL)
6896 {
6897 if (image->delay)
6898 final_image_delay=image->delay;
glennrp47b9dd52010-11-24 18:12:06 +00006899
cristy3ed852e2009-09-05 21:47:34 +00006900 image=GetNextImageInList(image);
6901 }
glennrp0fe50b42010-11-16 03:52:51 +00006902
cristy3ed852e2009-09-05 21:47:34 +00006903 if (final_delay < final_image_delay)
6904 final_delay=final_image_delay;
glennrp0fe50b42010-11-16 03:52:51 +00006905
cristy3ed852e2009-09-05 21:47:34 +00006906 image->delay=final_delay;
glennrp0fe50b42010-11-16 03:52:51 +00006907
cristy3ed852e2009-09-05 21:47:34 +00006908 if (logging != MagickFalse)
6909 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00006910 " image->delay=%.20g, final_delay=%.20g",(double) image->delay,
6911 (double) final_delay);
glennrp0fe50b42010-11-16 03:52:51 +00006912
cristy3ed852e2009-09-05 21:47:34 +00006913 if (logging != MagickFalse)
6914 {
6915 int
6916 scene;
6917
6918 scene=0;
6919 image=GetFirstImageInList(image);
glennrp47b9dd52010-11-24 18:12:06 +00006920
cristy3ed852e2009-09-05 21:47:34 +00006921 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
6922 " Before coalesce:");
glennrp47b9dd52010-11-24 18:12:06 +00006923
cristy3ed852e2009-09-05 21:47:34 +00006924 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00006925 " scene 0 delay=%.20g",(double) image->delay);
glennrp47b9dd52010-11-24 18:12:06 +00006926
cristy3ed852e2009-09-05 21:47:34 +00006927 while (GetNextImageInList(image) != (Image *) NULL)
6928 {
6929 image=GetNextImageInList(image);
6930 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00006931 " scene %.20g delay=%.20g",(double) scene++,(double) image->delay);
cristy3ed852e2009-09-05 21:47:34 +00006932 }
6933 }
6934
6935 image=GetFirstImageInList(image);
6936#ifdef MNG_COALESCE_LAYERS
6937 if (insert_layers)
6938 {
6939 Image
6940 *next_image,
6941 *next;
6942
cristybb503372010-05-27 20:51:26 +00006943 size_t
cristy3ed852e2009-09-05 21:47:34 +00006944 scene;
6945
6946 if (logging != MagickFalse)
6947 (void) LogMagickEvent(CoderEvent,GetMagickModule()," Coalesce Images");
glennrp47b9dd52010-11-24 18:12:06 +00006948
cristy3ed852e2009-09-05 21:47:34 +00006949 scene=image->scene;
cristyc82a27b2011-10-21 01:07:16 +00006950 next_image=CoalesceImages(image,exception);
glennrp47b9dd52010-11-24 18:12:06 +00006951
cristy3ed852e2009-09-05 21:47:34 +00006952 if (next_image == (Image *) NULL)
6953 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
glennrp47b9dd52010-11-24 18:12:06 +00006954
cristy3ed852e2009-09-05 21:47:34 +00006955 image=DestroyImageList(image);
6956 image=next_image;
glennrp47b9dd52010-11-24 18:12:06 +00006957
cristy3ed852e2009-09-05 21:47:34 +00006958 for (next=image; next != (Image *) NULL; next=next_image)
6959 {
6960 next->page.width=mng_info->mng_width;
6961 next->page.height=mng_info->mng_height;
6962 next->page.x=0;
6963 next->page.y=0;
6964 next->scene=scene++;
6965 next_image=GetNextImageInList(next);
glennrp47b9dd52010-11-24 18:12:06 +00006966
cristy3ed852e2009-09-05 21:47:34 +00006967 if (next_image == (Image *) NULL)
6968 break;
glennrp47b9dd52010-11-24 18:12:06 +00006969
cristy3ed852e2009-09-05 21:47:34 +00006970 if (next->delay == 0)
6971 {
6972 scene--;
6973 next_image->previous=GetPreviousImageInList(next);
6974 if (GetPreviousImageInList(next) == (Image *) NULL)
6975 image=next_image;
6976 else
6977 next->previous->next=next_image;
6978 next=DestroyImage(next);
6979 }
6980 }
6981 }
6982#endif
6983
6984 while (GetNextImageInList(image) != (Image *) NULL)
6985 image=GetNextImageInList(image);
glennrp47b9dd52010-11-24 18:12:06 +00006986
cristy3ed852e2009-09-05 21:47:34 +00006987 image->dispose=BackgroundDispose;
6988
6989 if (logging != MagickFalse)
6990 {
6991 int
6992 scene;
6993
6994 scene=0;
6995 image=GetFirstImageInList(image);
glennrp47b9dd52010-11-24 18:12:06 +00006996
cristy3ed852e2009-09-05 21:47:34 +00006997 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
6998 " After coalesce:");
glennrp47b9dd52010-11-24 18:12:06 +00006999
cristy3ed852e2009-09-05 21:47:34 +00007000 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00007001 " scene 0 delay=%.20g dispose=%.20g",(double) image->delay,
7002 (double) image->dispose);
glennrp47b9dd52010-11-24 18:12:06 +00007003
cristy3ed852e2009-09-05 21:47:34 +00007004 while (GetNextImageInList(image) != (Image *) NULL)
cristyf2faecf2010-05-28 19:19:36 +00007005 {
7006 image=GetNextImageInList(image);
glennrp47b9dd52010-11-24 18:12:06 +00007007
cristyf2faecf2010-05-28 19:19:36 +00007008 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00007009 " scene %.20g delay=%.20g dispose=%.20g",(double) scene++,
7010 (double) image->delay,(double) image->dispose);
cristyf2faecf2010-05-28 19:19:36 +00007011 }
7012 }
glennrp47b9dd52010-11-24 18:12:06 +00007013
cristy3ed852e2009-09-05 21:47:34 +00007014 image=GetFirstImageInList(image);
7015 MngInfoFreeStruct(mng_info,&have_mng_structure);
7016 have_mng_structure=MagickFalse;
glennrp47b9dd52010-11-24 18:12:06 +00007017
cristy3ed852e2009-09-05 21:47:34 +00007018 if (logging != MagickFalse)
7019 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"exit ReadMNGImage()");
glennrp47b9dd52010-11-24 18:12:06 +00007020
cristy3ed852e2009-09-05 21:47:34 +00007021 return(GetFirstImageInList(image));
7022}
glennrp25c1e2b2010-03-25 01:39:56 +00007023#else /* PNG_LIBPNG_VER > 10011 */
cristy3ed852e2009-09-05 21:47:34 +00007024static Image *ReadPNGImage(const ImageInfo *image_info,ExceptionInfo *exception)
7025{
7026 printf("Your PNG library is too old: You have libpng-%s\n",
7027 PNG_LIBPNG_VER_STRING);
glennrp47b9dd52010-11-24 18:12:06 +00007028
cristy3ed852e2009-09-05 21:47:34 +00007029 (void) ThrowMagickException(exception,GetMagickModule(),CoderError,
7030 "PNG library is too old","`%s'",image_info->filename);
glennrp47b9dd52010-11-24 18:12:06 +00007031
cristy3ed852e2009-09-05 21:47:34 +00007032 return(Image *) NULL;
7033}
glennrp47b9dd52010-11-24 18:12:06 +00007034
cristy3ed852e2009-09-05 21:47:34 +00007035static Image *ReadMNGImage(const ImageInfo *image_info,ExceptionInfo *exception)
7036{
7037 return(ReadPNGImage(image_info,exception));
7038}
glennrp25c1e2b2010-03-25 01:39:56 +00007039#endif /* PNG_LIBPNG_VER > 10011 */
cristy3ed852e2009-09-05 21:47:34 +00007040#endif
7041
7042/*
7043%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
7044% %
7045% %
7046% %
7047% R e g i s t e r P N G I m a g e %
7048% %
7049% %
7050% %
7051%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
7052%
7053% RegisterPNGImage() adds properties for the PNG image format to
7054% the list of supported formats. The properties include the image format
7055% tag, a method to read and/or write the format, whether the format
7056% supports the saving of more than one frame to the same file or blob,
7057% whether the format supports native in-memory I/O, and a brief
7058% description of the format.
7059%
7060% The format of the RegisterPNGImage method is:
7061%
cristybb503372010-05-27 20:51:26 +00007062% size_t RegisterPNGImage(void)
cristy3ed852e2009-09-05 21:47:34 +00007063%
7064*/
cristybb503372010-05-27 20:51:26 +00007065ModuleExport size_t RegisterPNGImage(void)
cristy3ed852e2009-09-05 21:47:34 +00007066{
7067 char
7068 version[MaxTextExtent];
7069
7070 MagickInfo
7071 *entry;
7072
7073 static const char
7074 *PNGNote=
7075 {
7076 "See http://www.libpng.org/ for details about the PNG format."
7077 },
glennrp47b9dd52010-11-24 18:12:06 +00007078
cristy3ed852e2009-09-05 21:47:34 +00007079 *JNGNote=
7080 {
7081 "See http://www.libpng.org/pub/mng/ for details about the JNG\n"
7082 "format."
7083 },
glennrp47b9dd52010-11-24 18:12:06 +00007084
cristy3ed852e2009-09-05 21:47:34 +00007085 *MNGNote=
7086 {
7087 "See http://www.libpng.org/pub/mng/ for details about the MNG\n"
7088 "format."
7089 };
7090
7091 *version='\0';
glennrp47b9dd52010-11-24 18:12:06 +00007092
cristy3ed852e2009-09-05 21:47:34 +00007093#if defined(PNG_LIBPNG_VER_STRING)
7094 (void) ConcatenateMagickString(version,"libpng ",MaxTextExtent);
7095 (void) ConcatenateMagickString(version,PNG_LIBPNG_VER_STRING,MaxTextExtent);
glennrp47b9dd52010-11-24 18:12:06 +00007096
cristy3ed852e2009-09-05 21:47:34 +00007097 if (LocaleCompare(PNG_LIBPNG_VER_STRING,png_get_header_ver(NULL)) != 0)
7098 {
7099 (void) ConcatenateMagickString(version,",",MaxTextExtent);
7100 (void) ConcatenateMagickString(version,png_get_libpng_ver(NULL),
7101 MaxTextExtent);
7102 }
7103#endif
glennrp47b9dd52010-11-24 18:12:06 +00007104
cristy3ed852e2009-09-05 21:47:34 +00007105 entry=SetMagickInfo("MNG");
7106 entry->seekable_stream=MagickTrue; /* To do: eliminate this. */
glennrp47b9dd52010-11-24 18:12:06 +00007107
cristy3ed852e2009-09-05 21:47:34 +00007108#if defined(MAGICKCORE_PNG_DELEGATE)
7109 entry->decoder=(DecodeImageHandler *) ReadMNGImage;
7110 entry->encoder=(EncodeImageHandler *) WriteMNGImage;
7111#endif
glennrp47b9dd52010-11-24 18:12:06 +00007112
cristy3ed852e2009-09-05 21:47:34 +00007113 entry->magick=(IsImageFormatHandler *) IsMNG;
7114 entry->description=ConstantString("Multiple-image Network Graphics");
glennrp47b9dd52010-11-24 18:12:06 +00007115
cristy3ed852e2009-09-05 21:47:34 +00007116 if (*version != '\0')
7117 entry->version=ConstantString(version);
glennrp47b9dd52010-11-24 18:12:06 +00007118
cristy3ed852e2009-09-05 21:47:34 +00007119 entry->module=ConstantString("PNG");
7120 entry->note=ConstantString(MNGNote);
7121 (void) RegisterMagickInfo(entry);
7122
7123 entry=SetMagickInfo("PNG");
glennrp47b9dd52010-11-24 18:12:06 +00007124
cristy3ed852e2009-09-05 21:47:34 +00007125#if defined(MAGICKCORE_PNG_DELEGATE)
7126 entry->decoder=(DecodeImageHandler *) ReadPNGImage;
7127 entry->encoder=(EncodeImageHandler *) WritePNGImage;
7128#endif
glennrp47b9dd52010-11-24 18:12:06 +00007129
cristy3ed852e2009-09-05 21:47:34 +00007130 entry->magick=(IsImageFormatHandler *) IsPNG;
7131 entry->adjoin=MagickFalse;
7132 entry->description=ConstantString("Portable Network Graphics");
7133 entry->module=ConstantString("PNG");
glennrp47b9dd52010-11-24 18:12:06 +00007134
cristy3ed852e2009-09-05 21:47:34 +00007135 if (*version != '\0')
7136 entry->version=ConstantString(version);
glennrp47b9dd52010-11-24 18:12:06 +00007137
cristy3ed852e2009-09-05 21:47:34 +00007138 entry->note=ConstantString(PNGNote);
7139 (void) RegisterMagickInfo(entry);
7140
7141 entry=SetMagickInfo("PNG8");
glennrp47b9dd52010-11-24 18:12:06 +00007142
cristy3ed852e2009-09-05 21:47:34 +00007143#if defined(MAGICKCORE_PNG_DELEGATE)
7144 entry->decoder=(DecodeImageHandler *) ReadPNGImage;
7145 entry->encoder=(EncodeImageHandler *) WritePNGImage;
7146#endif
glennrp47b9dd52010-11-24 18:12:06 +00007147
cristy3ed852e2009-09-05 21:47:34 +00007148 entry->magick=(IsImageFormatHandler *) IsPNG;
7149 entry->adjoin=MagickFalse;
7150 entry->description=ConstantString(
7151 "8-bit indexed with optional binary transparency");
7152 entry->module=ConstantString("PNG");
7153 (void) RegisterMagickInfo(entry);
7154
7155 entry=SetMagickInfo("PNG24");
7156 *version='\0';
glennrp47b9dd52010-11-24 18:12:06 +00007157
cristy3ed852e2009-09-05 21:47:34 +00007158#if defined(ZLIB_VERSION)
7159 (void) ConcatenateMagickString(version,"zlib ",MaxTextExtent);
7160 (void) ConcatenateMagickString(version,ZLIB_VERSION,MaxTextExtent);
glennrp47b9dd52010-11-24 18:12:06 +00007161
cristy3ed852e2009-09-05 21:47:34 +00007162 if (LocaleCompare(ZLIB_VERSION,zlib_version) != 0)
7163 {
7164 (void) ConcatenateMagickString(version,",",MaxTextExtent);
7165 (void) ConcatenateMagickString(version,zlib_version,MaxTextExtent);
7166 }
7167#endif
glennrp47b9dd52010-11-24 18:12:06 +00007168
cristy3ed852e2009-09-05 21:47:34 +00007169 if (*version != '\0')
7170 entry->version=ConstantString(version);
glennrp47b9dd52010-11-24 18:12:06 +00007171
cristy3ed852e2009-09-05 21:47:34 +00007172#if defined(MAGICKCORE_PNG_DELEGATE)
7173 entry->decoder=(DecodeImageHandler *) ReadPNGImage;
7174 entry->encoder=(EncodeImageHandler *) WritePNGImage;
7175#endif
glennrp47b9dd52010-11-24 18:12:06 +00007176
cristy3ed852e2009-09-05 21:47:34 +00007177 entry->magick=(IsImageFormatHandler *) IsPNG;
7178 entry->adjoin=MagickFalse;
7179 entry->description=ConstantString("opaque 24-bit RGB");
7180 entry->module=ConstantString("PNG");
7181 (void) RegisterMagickInfo(entry);
7182
7183 entry=SetMagickInfo("PNG32");
glennrp47b9dd52010-11-24 18:12:06 +00007184
cristy3ed852e2009-09-05 21:47:34 +00007185#if defined(MAGICKCORE_PNG_DELEGATE)
7186 entry->decoder=(DecodeImageHandler *) ReadPNGImage;
7187 entry->encoder=(EncodeImageHandler *) WritePNGImage;
7188#endif
glennrp47b9dd52010-11-24 18:12:06 +00007189
cristy3ed852e2009-09-05 21:47:34 +00007190 entry->magick=(IsImageFormatHandler *) IsPNG;
7191 entry->adjoin=MagickFalse;
7192 entry->description=ConstantString("opaque or transparent 32-bit RGBA");
7193 entry->module=ConstantString("PNG");
7194 (void) RegisterMagickInfo(entry);
7195
7196 entry=SetMagickInfo("JNG");
glennrp47b9dd52010-11-24 18:12:06 +00007197
cristy3ed852e2009-09-05 21:47:34 +00007198#if defined(JNG_SUPPORTED)
7199#if defined(MAGICKCORE_PNG_DELEGATE)
7200 entry->decoder=(DecodeImageHandler *) ReadJNGImage;
7201 entry->encoder=(EncodeImageHandler *) WriteJNGImage;
7202#endif
7203#endif
glennrp47b9dd52010-11-24 18:12:06 +00007204
cristy3ed852e2009-09-05 21:47:34 +00007205 entry->magick=(IsImageFormatHandler *) IsJNG;
7206 entry->adjoin=MagickFalse;
7207 entry->description=ConstantString("JPEG Network Graphics");
7208 entry->module=ConstantString("PNG");
7209 entry->note=ConstantString(JNGNote);
7210 (void) RegisterMagickInfo(entry);
glennrp47b9dd52010-11-24 18:12:06 +00007211
cristy18b17442009-10-25 18:36:48 +00007212#if defined(PNG_SETJMP_NOT_THREAD_SAFE)
glennrpcf002022011-01-30 02:38:15 +00007213 ping_semaphore=AllocateSemaphoreInfo();
cristy18b17442009-10-25 18:36:48 +00007214#endif
glennrp47b9dd52010-11-24 18:12:06 +00007215
cristy3ed852e2009-09-05 21:47:34 +00007216 return(MagickImageCoderSignature);
7217}
7218
7219/*
7220%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
7221% %
7222% %
7223% %
7224% U n r e g i s t e r P N G I m a g e %
7225% %
7226% %
7227% %
7228%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
7229%
7230% UnregisterPNGImage() removes format registrations made by the
7231% PNG module from the list of supported formats.
7232%
7233% The format of the UnregisterPNGImage method is:
7234%
7235% UnregisterPNGImage(void)
7236%
7237*/
7238ModuleExport void UnregisterPNGImage(void)
7239{
7240 (void) UnregisterMagickInfo("MNG");
7241 (void) UnregisterMagickInfo("PNG");
7242 (void) UnregisterMagickInfo("PNG8");
7243 (void) UnregisterMagickInfo("PNG24");
7244 (void) UnregisterMagickInfo("PNG32");
7245 (void) UnregisterMagickInfo("JNG");
glennrp47b9dd52010-11-24 18:12:06 +00007246
cristy3ed852e2009-09-05 21:47:34 +00007247#if defined(PNG_SETJMP_NOT_THREAD_SAFE)
glennrpcf002022011-01-30 02:38:15 +00007248 if (ping_semaphore != (SemaphoreInfo *) NULL)
7249 DestroySemaphoreInfo(&ping_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00007250#endif
7251}
7252
7253#if defined(MAGICKCORE_PNG_DELEGATE)
glennrp25c1e2b2010-03-25 01:39:56 +00007254#if PNG_LIBPNG_VER > 10011
cristy3ed852e2009-09-05 21:47:34 +00007255/*
7256%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
7257% %
7258% %
7259% %
7260% W r i t e M N G I m a g e %
7261% %
7262% %
7263% %
7264%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
7265%
7266% WriteMNGImage() writes an image in the Portable Network Graphics
7267% Group's "Multiple-image Network Graphics" encoded image format.
7268%
7269% MNG support written by Glenn Randers-Pehrson, glennrp@image...
7270%
7271% The format of the WriteMNGImage method is:
7272%
cristy1e178e72011-08-28 19:44:34 +00007273% MagickBooleanType WriteMNGImage(const ImageInfo *image_info,
7274% Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00007275%
7276% A description of each parameter follows.
7277%
7278% o image_info: the image info.
7279%
7280% o image: The image.
7281%
cristy1e178e72011-08-28 19:44:34 +00007282% o exception: return any errors or warnings in this structure.
cristy3ed852e2009-09-05 21:47:34 +00007283%
7284% To do (as of version 5.5.2, November 26, 2002 -- glennrp -- see also
7285% "To do" under ReadPNGImage):
7286%
cristy3ed852e2009-09-05 21:47:34 +00007287% Preserve all unknown and not-yet-handled known chunks found in input
7288% PNG file and copy them into output PNG files according to the PNG
7289% copying rules.
7290%
7291% Write the iCCP chunk at MNG level when (icc profile length > 0)
7292%
7293% Improve selection of color type (use indexed-colour or indexed-colour
7294% with tRNS when 256 or fewer unique RGBA values are present).
7295%
7296% Figure out what to do with "dispose=<restore-to-previous>" (dispose == 3)
7297% This will be complicated if we limit ourselves to generating MNG-LC
7298% files. For now we ignore disposal method 3 and simply overlay the next
7299% image on it.
7300%
7301% Check for identical PLTE's or PLTE/tRNS combinations and use a
7302% global MNG PLTE or PLTE/tRNS combination when appropriate.
7303% [mostly done 15 June 1999 but still need to take care of tRNS]
7304%
7305% Check for identical sRGB and replace with a global sRGB (and remove
7306% gAMA/cHRM if sRGB is found; check for identical gAMA/cHRM and
7307% replace with global gAMA/cHRM (or with sRGB if appropriate; replace
7308% local gAMA/cHRM with local sRGB if appropriate).
7309%
7310% Check for identical sBIT chunks and write global ones.
7311%
7312% Provide option to skip writing the signature tEXt chunks.
7313%
7314% Use signatures to detect identical objects and reuse the first
7315% instance of such objects instead of writing duplicate objects.
7316%
7317% Use a smaller-than-32k value of compression window size when
7318% appropriate.
7319%
7320% Encode JNG datastreams. Mostly done as of 5.5.2; need to write
7321% ancillary text chunks and save profiles.
7322%
7323% Provide an option to force LC files (to ensure exact framing rate)
7324% instead of VLC.
7325%
7326% Provide an option to force VLC files instead of LC, even when offsets
7327% are present. This will involve expanding the embedded images with a
7328% transparent region at the top and/or left.
7329*/
7330
cristy3ed852e2009-09-05 21:47:34 +00007331static void
glennrpcf002022011-01-30 02:38:15 +00007332Magick_png_write_raw_profile(const ImageInfo *image_info,png_struct *ping,
cristy3ed852e2009-09-05 21:47:34 +00007333 png_info *ping_info, unsigned char *profile_type, unsigned char
7334 *profile_description, unsigned char *profile_data, png_uint_32 length)
7335{
cristy3ed852e2009-09-05 21:47:34 +00007336 png_textp
7337 text;
7338
cristybb503372010-05-27 20:51:26 +00007339 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00007340 i;
7341
7342 unsigned char
7343 *sp;
7344
7345 png_charp
7346 dp;
7347
7348 png_uint_32
7349 allocated_length,
7350 description_length;
7351
7352 unsigned char
7353 hex[16]={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
cristy3ed852e2009-09-05 21:47:34 +00007354
cristy3ed852e2009-09-05 21:47:34 +00007355 if (LocaleNCompare((char *) profile_type+1, "ng-chunk-",9) == 0)
7356 return;
7357
7358 if (image_info->verbose)
7359 {
glennrp0fe50b42010-11-16 03:52:51 +00007360 (void) printf("writing raw profile: type=%s, length=%.20g\n",
7361 (char *) profile_type, (double) length);
cristy3ed852e2009-09-05 21:47:34 +00007362 }
glennrp0fe50b42010-11-16 03:52:51 +00007363
cristy3ed852e2009-09-05 21:47:34 +00007364 text=(png_textp) png_malloc(ping,(png_uint_32) sizeof(png_text));
7365 description_length=(png_uint_32) strlen((const char *) profile_description);
7366 allocated_length=(png_uint_32) (length*2 + (length >> 5) + 20
7367 + description_length);
7368 text[0].text=(png_charp) png_malloc(ping,allocated_length);
7369 text[0].key=(png_charp) png_malloc(ping, (png_uint_32) 80);
7370 text[0].key[0]='\0';
7371 (void) ConcatenateMagickString(text[0].key,
7372 "Raw profile type ",MaxTextExtent);
7373 (void) ConcatenateMagickString(text[0].key,(const char *) profile_type,62);
7374 sp=profile_data;
7375 dp=text[0].text;
7376 *dp++='\n';
7377 (void) CopyMagickString(dp,(const char *) profile_description,
7378 allocated_length);
7379 dp+=description_length;
7380 *dp++='\n';
cristy3b6fd2e2011-05-20 12:53:50 +00007381 (void) FormatLocaleString(dp,allocated_length-
cristyf2faecf2010-05-28 19:19:36 +00007382 (png_size_t) (dp-text[0].text),"%8lu ",(unsigned long) length);
cristy3ed852e2009-09-05 21:47:34 +00007383 dp+=8;
glennrp47b9dd52010-11-24 18:12:06 +00007384
cristybb503372010-05-27 20:51:26 +00007385 for (i=0; i < (ssize_t) length; i++)
cristy3ed852e2009-09-05 21:47:34 +00007386 {
7387 if (i%36 == 0)
7388 *dp++='\n';
7389 *(dp++)=(char) hex[((*sp >> 4) & 0x0f)];
7390 *(dp++)=(char) hex[((*sp++ ) & 0x0f)];
7391 }
glennrp47b9dd52010-11-24 18:12:06 +00007392
cristy3ed852e2009-09-05 21:47:34 +00007393 *dp++='\n';
7394 *dp='\0';
7395 text[0].text_length=(png_size_t) (dp-text[0].text);
7396 text[0].compression=image_info->compression == NoCompression ||
7397 (image_info->compression == UndefinedCompression &&
7398 text[0].text_length < 128) ? -1 : 0;
glennrp47b9dd52010-11-24 18:12:06 +00007399
cristy3ed852e2009-09-05 21:47:34 +00007400 if (text[0].text_length <= allocated_length)
7401 png_set_text(ping,ping_info,text,1);
glennrp47b9dd52010-11-24 18:12:06 +00007402
cristy3ed852e2009-09-05 21:47:34 +00007403 png_free(ping,text[0].text);
7404 png_free(ping,text[0].key);
7405 png_free(ping,text);
cristy3ed852e2009-09-05 21:47:34 +00007406}
7407
glennrpcf002022011-01-30 02:38:15 +00007408static MagickBooleanType Magick_png_write_chunk_from_profile(Image *image,
cristy4383ec82011-01-05 15:42:32 +00007409 const char *string, MagickBooleanType logging)
cristy3ed852e2009-09-05 21:47:34 +00007410{
7411 char
7412 *name;
7413
7414 const StringInfo
7415 *profile;
7416
7417 unsigned char
7418 *data;
7419
7420 png_uint_32 length;
7421
7422 ResetImageProfileIterator(image);
glennrp47b9dd52010-11-24 18:12:06 +00007423
7424 for (name=GetNextImageProfile(image); name != (const char *) NULL; )
7425 {
cristy3ed852e2009-09-05 21:47:34 +00007426 profile=GetImageProfile(image,name);
glennrp47b9dd52010-11-24 18:12:06 +00007427
cristy3ed852e2009-09-05 21:47:34 +00007428 if (profile != (const StringInfo *) NULL)
7429 {
7430 StringInfo
glennrpcf002022011-01-30 02:38:15 +00007431 *ping_profile;
cristy3ed852e2009-09-05 21:47:34 +00007432
glennrp47b9dd52010-11-24 18:12:06 +00007433 if (LocaleNCompare(name,string,11) == 0)
7434 {
7435 if (logging != MagickFalse)
7436 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7437 " Found %s profile",name);
cristy3ed852e2009-09-05 21:47:34 +00007438
glennrpcf002022011-01-30 02:38:15 +00007439 ping_profile=CloneStringInfo(profile);
7440 data=GetStringInfoDatum(ping_profile),
7441 length=(png_uint_32) GetStringInfoLength(ping_profile);
glennrp47b9dd52010-11-24 18:12:06 +00007442 data[4]=data[3];
7443 data[3]=data[2];
7444 data[2]=data[1];
7445 data[1]=data[0];
7446 (void) WriteBlobMSBULong(image,length-5); /* data length */
7447 (void) WriteBlob(image,length-1,data+1);
7448 (void) WriteBlobMSBULong(image,crc32(0,data+1,(uInt) length-1));
glennrpcf002022011-01-30 02:38:15 +00007449 ping_profile=DestroyStringInfo(ping_profile);
glennrp47b9dd52010-11-24 18:12:06 +00007450 }
cristy3ed852e2009-09-05 21:47:34 +00007451 }
glennrp47b9dd52010-11-24 18:12:06 +00007452
cristy3ed852e2009-09-05 21:47:34 +00007453 name=GetNextImageProfile(image);
7454 }
glennrp47b9dd52010-11-24 18:12:06 +00007455
cristy3ed852e2009-09-05 21:47:34 +00007456 return(MagickTrue);
7457}
7458
glennrpb9cfe272010-12-21 15:08:06 +00007459
cristy3ed852e2009-09-05 21:47:34 +00007460/* Write one PNG image */
glennrpb9cfe272010-12-21 15:08:06 +00007461static MagickBooleanType WriteOnePNGImage(MngInfo *mng_info,
cristy018f07f2011-09-04 21:15:19 +00007462 const ImageInfo *IMimage_info,Image *IMimage,ExceptionInfo *exception)
glennrpb9cfe272010-12-21 15:08:06 +00007463{
7464 Image
7465 *image;
7466
7467 ImageInfo
7468 *image_info;
7469
cristy3ed852e2009-09-05 21:47:34 +00007470 char
7471 s[2];
7472
7473 const char
7474 *name,
7475 *property,
7476 *value;
7477
7478 const StringInfo
7479 *profile;
7480
cristy3ed852e2009-09-05 21:47:34 +00007481 int
cristy3ed852e2009-09-05 21:47:34 +00007482 num_passes,
glennrpcecd5762010-03-23 12:07:49 +00007483 pass;
7484
glennrpe9c26dc2010-05-30 01:56:35 +00007485 png_byte
7486 ping_trans_alpha[256];
glennrp5af765f2010-03-30 11:12:18 +00007487
glennrp39992b42010-11-14 00:03:43 +00007488 png_color
7489 palette[257];
cristy3ed852e2009-09-05 21:47:34 +00007490
glennrp5af765f2010-03-30 11:12:18 +00007491 png_color_16
7492 ping_background,
7493 ping_trans_color;
7494
cristy3ed852e2009-09-05 21:47:34 +00007495 png_info
7496 *ping_info;
7497
7498 png_struct
7499 *ping;
7500
glennrp5af765f2010-03-30 11:12:18 +00007501 png_uint_32
7502 ping_height,
7503 ping_width;
7504
cristybb503372010-05-27 20:51:26 +00007505 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00007506 y;
7507
7508 MagickBooleanType
glennrp58e01762011-01-07 15:28:54 +00007509 image_matte,
glennrp21f0e622011-01-07 16:20:57 +00007510 logging,
glennrp58e01762011-01-07 15:28:54 +00007511 matte,
7512
glennrpda8f3a72011-02-27 23:54:12 +00007513 ping_have_blob,
glennrpfd05d622011-02-25 04:10:33 +00007514 ping_have_cheap_transparency,
glennrpd6bf1612010-12-17 17:28:54 +00007515 ping_have_color,
glennrp8d579662011-02-23 02:05:02 +00007516 ping_have_non_bw,
glennrp39992b42010-11-14 00:03:43 +00007517 ping_have_PLTE,
glennrp991d11d2010-11-12 21:55:28 +00007518 ping_have_bKGD,
7519 ping_have_pHYs,
7520 ping_have_tRNS,
glennrp26f37912010-12-23 16:22:42 +00007521
7522 ping_exclude_bKGD,
7523 ping_exclude_cHRM,
glennrpa0ed0092011-04-18 16:36:29 +00007524 ping_exclude_date,
glennrpe4e2d792011-02-21 12:11:27 +00007525 /* ping_exclude_EXIF, */
glennrp26f37912010-12-23 16:22:42 +00007526 ping_exclude_gAMA,
7527 ping_exclude_iCCP,
7528 /* ping_exclude_iTXt, */
7529 ping_exclude_oFFs,
7530 ping_exclude_pHYs,
7531 ping_exclude_sRGB,
7532 ping_exclude_tEXt,
glennrpe4e2d792011-02-21 12:11:27 +00007533 /* ping_exclude_tRNS, */
glennrp26f37912010-12-23 16:22:42 +00007534 ping_exclude_vpAg,
7535 ping_exclude_zCCP, /* hex-encoded iCCP */
7536 ping_exclude_zTXt,
7537
glennrp8d3d6e52011-04-19 04:39:51 +00007538 ping_preserve_colormap,
glennrp0e8ea192010-12-24 18:00:33 +00007539 ping_need_colortype_warning,
7540
glennrp82b3c532011-03-22 19:20:54 +00007541 status,
glennrp8ca51ad2011-05-12 21:22:32 +00007542 tried_332,
glennrpd3371642011-03-22 19:42:23 +00007543 tried_333,
7544 tried_444;
cristy3ed852e2009-09-05 21:47:34 +00007545
7546 QuantumInfo
7547 *quantum_info;
7548
cristyc82a27b2011-10-21 01:07:16 +00007549 PNGErrorInfo
7550 error_info;
7551
cristybb503372010-05-27 20:51:26 +00007552 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00007553 i,
7554 x;
7555
7556 unsigned char
glennrpcf002022011-01-30 02:38:15 +00007557 *ping_pixels;
cristy3ed852e2009-09-05 21:47:34 +00007558
glennrp5af765f2010-03-30 11:12:18 +00007559 volatile int
glennrpf09bded2011-01-08 01:15:59 +00007560 image_colors,
glennrp0fe50b42010-11-16 03:52:51 +00007561 ping_bit_depth,
glennrp5af765f2010-03-30 11:12:18 +00007562 ping_color_type,
7563 ping_interlace_method,
7564 ping_compression_method,
7565 ping_filter_method,
7566 ping_num_trans;
7567
cristybb503372010-05-27 20:51:26 +00007568 volatile size_t
glennrp5af765f2010-03-30 11:12:18 +00007569 image_depth,
7570 old_bit_depth;
cristy3ed852e2009-09-05 21:47:34 +00007571
cristybb503372010-05-27 20:51:26 +00007572 size_t
cristy3ed852e2009-09-05 21:47:34 +00007573 quality,
7574 rowbytes,
7575 save_image_depth;
7576
glennrpdfd70802010-11-14 01:23:35 +00007577 int
glennrpfd05d622011-02-25 04:10:33 +00007578 j,
glennrpf09bded2011-01-08 01:15:59 +00007579 number_colors,
glennrp8bb3a022010-12-13 20:40:04 +00007580 number_opaque,
7581 number_semitransparent,
7582 number_transparent,
glennrpdfd70802010-11-14 01:23:35 +00007583 ping_pHYs_unit_type;
7584
7585 png_uint_32
7586 ping_pHYs_x_resolution,
7587 ping_pHYs_y_resolution;
7588
cristy3ed852e2009-09-05 21:47:34 +00007589 logging=LogMagickEvent(CoderEvent,GetMagickModule(),
glennrpfd05d622011-02-25 04:10:33 +00007590 " Enter WriteOnePNGImage()");
cristy3ed852e2009-09-05 21:47:34 +00007591
cristyc82a27b2011-10-21 01:07:16 +00007592 image = CloneImage(IMimage,0,0,MagickFalse,exception);
glennrpb9cfe272010-12-21 15:08:06 +00007593 image_info=(ImageInfo *) CloneImageInfo(IMimage_info);
glennrp8d3d6e52011-04-19 04:39:51 +00007594 if (image_info == (ImageInfo *) NULL)
glennrp7b2cc792011-04-18 16:46:02 +00007595 ThrowWriterException(ResourceLimitError, "MemoryAllocationFailed");
glennrpb9cfe272010-12-21 15:08:06 +00007596
cristy3ed852e2009-09-05 21:47:34 +00007597#if defined(PNG_SETJMP_NOT_THREAD_SAFE)
glennrpcf002022011-01-30 02:38:15 +00007598 LockSemaphoreInfo(ping_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00007599#endif
7600
glennrp5af765f2010-03-30 11:12:18 +00007601 /* Initialize some stuff */
glennrp0fe50b42010-11-16 03:52:51 +00007602 ping_bit_depth=0,
glennrp5af765f2010-03-30 11:12:18 +00007603 ping_color_type=0,
7604 ping_interlace_method=0,
7605 ping_compression_method=0,
7606 ping_filter_method=0,
7607 ping_num_trans = 0;
7608
7609 ping_background.red = 0;
7610 ping_background.green = 0;
7611 ping_background.blue = 0;
7612 ping_background.gray = 0;
7613 ping_background.index = 0;
7614
7615 ping_trans_color.red=0;
7616 ping_trans_color.green=0;
7617 ping_trans_color.blue=0;
7618 ping_trans_color.gray=0;
7619
glennrpdfd70802010-11-14 01:23:35 +00007620 ping_pHYs_unit_type = 0;
7621 ping_pHYs_x_resolution = 0;
7622 ping_pHYs_y_resolution = 0;
7623
glennrpda8f3a72011-02-27 23:54:12 +00007624 ping_have_blob=MagickFalse;
glennrpd6bf1612010-12-17 17:28:54 +00007625 ping_have_color=MagickTrue;
glennrp8d579662011-02-23 02:05:02 +00007626 ping_have_non_bw=MagickTrue;
glennrp39992b42010-11-14 00:03:43 +00007627 ping_have_PLTE=MagickFalse;
glennrp991d11d2010-11-12 21:55:28 +00007628 ping_have_bKGD=MagickFalse;
7629 ping_have_pHYs=MagickFalse;
7630 ping_have_tRNS=MagickFalse;
7631
glennrp0e8ea192010-12-24 18:00:33 +00007632 ping_exclude_bKGD=mng_info->ping_exclude_bKGD;
7633 ping_exclude_cHRM=mng_info->ping_exclude_cHRM;
glennrpa0ed0092011-04-18 16:36:29 +00007634 ping_exclude_date=mng_info->ping_exclude_date;
glennrpdde35db2011-02-21 12:06:32 +00007635 /* ping_exclude_EXIF=mng_info->ping_exclude_EXIF; */
glennrp0e8ea192010-12-24 18:00:33 +00007636 ping_exclude_gAMA=mng_info->ping_exclude_gAMA;
glennrp0e8ea192010-12-24 18:00:33 +00007637 ping_exclude_iCCP=mng_info->ping_exclude_iCCP;
7638 /* ping_exclude_iTXt=mng_info->ping_exclude_iTXt; */
7639 ping_exclude_oFFs=mng_info->ping_exclude_oFFs;
7640 ping_exclude_pHYs=mng_info->ping_exclude_pHYs;
7641 ping_exclude_sRGB=mng_info->ping_exclude_sRGB;
7642 ping_exclude_tEXt=mng_info->ping_exclude_tEXt;
glennrpdde35db2011-02-21 12:06:32 +00007643 /* ping_exclude_tRNS=mng_info->ping_exclude_tRNS; */
glennrp0e8ea192010-12-24 18:00:33 +00007644 ping_exclude_vpAg=mng_info->ping_exclude_vpAg;
7645 ping_exclude_zCCP=mng_info->ping_exclude_zCCP; /* hex-encoded iCCP in zTXt */
7646 ping_exclude_zTXt=mng_info->ping_exclude_zTXt;
7647
glennrp8d3d6e52011-04-19 04:39:51 +00007648 ping_preserve_colormap = mng_info->ping_preserve_colormap;
glennrp0e8ea192010-12-24 18:00:33 +00007649 ping_need_colortype_warning = MagickFalse;
7650
cristy0d57eec2011-09-04 22:13:56 +00007651 /* Recognize the ICC sRGB profile and convert it to the sRGB chunk,
7652 * i.e., eliminate the ICC profile and set image->rendering_intent.
7653 * Note that this will not involve any changes to the actual pixels
7654 * but merely passes information to applications that read the resulting
7655 * PNG image.
7656 */
7657 if (ping_exclude_sRGB == MagickFalse)
7658 {
7659 char
7660 *name;
7661
7662 const StringInfo
7663 *profile;
7664
7665 ResetImageProfileIterator(image);
7666 for (name=GetNextImageProfile(image); name != (const char *) NULL; )
7667 {
7668 profile=GetImageProfile(image,name);
7669
7670 if (profile != (StringInfo *) NULL)
7671 {
7672 if ((LocaleCompare(name,"ICC") == 0) ||
glennrp29a106e2011-09-06 17:11:42 +00007673 (LocaleCompare(name,"ICM") == 0))
7674 {
glennrpee7b4c02011-10-04 01:21:09 +00007675 int
7676 icheck;
7677
7678 /* 0: not a known sRGB profile
7679 * 1: HP-Microsoft sRGB v2
7680 * 2: ICC sRGB v4 perceptual
7681 * 3: ICC sRGB v2 perceptual no black-compensation
7682 */
7683 png_uint_32
7684 check_crc[4] = {0, 0xf29e526dUL, 0xbbef7812UL, 0x427ebb21UL},
7685 check_len[4] = {0, 3144, 60960, 3052};
7686
7687 png_uint_32
7688 length,
7689 profile_crc;
7690
cristy0d57eec2011-09-04 22:13:56 +00007691 unsigned char
7692 *data;
7693
glennrp29a106e2011-09-06 17:11:42 +00007694 length=(png_uint_32) GetStringInfoLength(profile);
7695
glennrpee7b4c02011-10-04 01:21:09 +00007696 for (icheck=3; icheck > 0; icheck--)
cristy0d57eec2011-09-04 22:13:56 +00007697 {
glennrpee7b4c02011-10-04 01:21:09 +00007698 if (length == check_len[icheck])
glennrp29a106e2011-09-06 17:11:42 +00007699 {
glennrpee7b4c02011-10-04 01:21:09 +00007700 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7701 " Got a %lu-byte ICC profile (potentially sRGB)",
7702 (unsigned long) length);
glennrp29a106e2011-09-06 17:11:42 +00007703
glennrpee7b4c02011-10-04 01:21:09 +00007704 data=GetStringInfoDatum(profile);
7705 profile_crc=crc32(0,data,length);
glennrp29a106e2011-09-06 17:11:42 +00007706
glennrpee7b4c02011-10-04 01:21:09 +00007707 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
glennrpe54cd4b2011-10-04 01:31:35 +00007708 " with crc=%8x",(unsigned int) profile_crc);
glennrpee7b4c02011-10-04 01:21:09 +00007709
7710 if (profile_crc == check_crc[icheck])
7711 {
7712 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7713 " It is sRGB.");
7714 if (image->rendering_intent==UndefinedIntent)
7715 image->rendering_intent=PerceptualIntent;
7716 break;
7717 }
glennrp29a106e2011-09-06 17:11:42 +00007718 }
glennrp29a106e2011-09-06 17:11:42 +00007719 }
glennrpee7b4c02011-10-04 01:21:09 +00007720 if (icheck == 0)
glennrp29a106e2011-09-06 17:11:42 +00007721 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
glennrpee7b4c02011-10-04 01:21:09 +00007722 " Got a %lu-byte ICC profile",
glennrp29a106e2011-09-06 17:11:42 +00007723 (unsigned long) length);
7724 }
cristy0d57eec2011-09-04 22:13:56 +00007725 }
7726 name=GetNextImageProfile(image);
7727 }
7728 }
7729
glennrp8bb3a022010-12-13 20:40:04 +00007730 number_opaque = 0;
7731 number_semitransparent = 0;
7732 number_transparent = 0;
7733
glennrpfd05d622011-02-25 04:10:33 +00007734 if (logging != MagickFalse)
7735 {
7736 if (image->storage_class == UndefinedClass)
7737 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7738 " storage_class=UndefinedClass");
7739 if (image->storage_class == DirectClass)
7740 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7741 " storage_class=DirectClass");
7742 if (image->storage_class == PseudoClass)
7743 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7744 " storage_class=PseudoClass");
7745 }
glennrp28af3712011-04-06 18:07:30 +00007746
glennrp7e65e932011-08-19 02:31:16 +00007747 if (image->storage_class == PseudoClass &&
7748 (mng_info->write_png8 || mng_info->write_png24 || mng_info->write_png32 ||
7749 (mng_info->write_png_colortype != 0 &&
7750 mng_info->write_png_colortype != 4)))
7751 {
cristyea1a8aa2011-10-20 13:24:06 +00007752 (void) SyncImage(image,exception);
glennrp7e65e932011-08-19 02:31:16 +00007753 image->storage_class = DirectClass;
7754 }
7755
glennrpc6c391a2011-04-27 02:23:56 +00007756 if (ping_preserve_colormap == MagickFalse)
glennrp28af3712011-04-06 18:07:30 +00007757 {
glennrpc6c391a2011-04-27 02:23:56 +00007758 if (image->storage_class != PseudoClass && image->colormap != NULL)
7759 {
7760 /* Free the bogus colormap; it can cause trouble later */
7761 if (logging != MagickFalse)
7762 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7763 " Freeing bogus colormap");
cristye9ac4c32011-09-26 18:47:22 +00007764 (void) RelinquishMagickMemory(image->colormap);
glennrpc6c391a2011-04-27 02:23:56 +00007765 image->colormap=NULL;
7766 }
glennrp28af3712011-04-06 18:07:30 +00007767 }
glennrpbb4f99d2011-05-22 11:13:17 +00007768
cristy510d06a2011-07-06 23:43:54 +00007769 if (IsRGBColorspace(image->colorspace) == MagickFalse)
cristye941a752011-10-15 01:52:48 +00007770 (void) TransformImageColorspace(image,RGBColorspace,exception);
glennrp0fe50b42010-11-16 03:52:51 +00007771
glennrp3241bd02010-12-12 04:36:28 +00007772 /*
7773 Sometimes we get PseudoClass images whose RGB values don't match
7774 the colors in the colormap. This code syncs the RGB values.
7775 */
7776 if (image->depth <= 8 && image->taint && image->storage_class == PseudoClass)
cristyea1a8aa2011-10-20 13:24:06 +00007777 (void) SyncImage(image,exception);
glennrp3241bd02010-12-12 04:36:28 +00007778
glennrpa6a06632011-01-19 15:15:34 +00007779#if (MAGICKCORE_QUANTUM_DEPTH == 8)
7780 if (image->depth > 8)
7781 {
7782 if (logging != MagickFalse)
7783 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7784 " Reducing PNG bit depth to 8 since this is a Q8 build.");
7785
7786 image->depth=8;
7787 }
7788#endif
7789
glennrp8e58efd2011-05-20 12:16:29 +00007790 /* Respect the -depth option */
glennrpcc95c3f2011-04-18 16:46:48 +00007791 if (image->depth < MAGICKCORE_QUANTUM_DEPTH)
7792 {
cristy4c08aed2011-07-01 19:47:50 +00007793 register Quantum
glennrp8e58efd2011-05-20 12:16:29 +00007794 *r;
7795
glennrp8e58efd2011-05-20 12:16:29 +00007796 if (image->depth > 8)
7797 {
7798#if MAGICKCORE_QUANTUM_DEPTH > 16
7799 /* Scale to 16-bit */
glennrp91d99252011-06-25 14:30:13 +00007800 LBR16PacketRGBO(image->background_color);
glennrp8e58efd2011-05-20 12:16:29 +00007801
7802 for (y=0; y < (ssize_t) image->rows; y++)
7803 {
cristy8a20fa02011-12-27 15:54:31 +00007804 r=GetAuthenticPixels(image,0,y,image->columns,1,exception);
glennrp8e58efd2011-05-20 12:16:29 +00007805
cristy4c08aed2011-07-01 19:47:50 +00007806 if (r == (Quantum *) NULL)
glennrp8e58efd2011-05-20 12:16:29 +00007807 break;
7808
7809 for (x=0; x < (ssize_t) image->columns; x++)
7810 {
glennrp54cf7972011-08-06 14:28:09 +00007811 LBR16PixelRGBA(r);
cristy8a20fa02011-12-27 15:54:31 +00007812 r+=GetPixelChannels(image);
glennrp8e58efd2011-05-20 12:16:29 +00007813 }
glennrpbb4f99d2011-05-22 11:13:17 +00007814
glennrp8e58efd2011-05-20 12:16:29 +00007815 if (SyncAuthenticPixels(image,exception) == MagickFalse)
7816 break;
7817 }
7818
7819 if (image->storage_class == PseudoClass && image->colormap != NULL)
7820 {
cristy3e08f112011-05-24 13:19:30 +00007821 for (i=0; i < (ssize_t) image->colors; i++)
glennrp8e58efd2011-05-20 12:16:29 +00007822 {
glennrp91d99252011-06-25 14:30:13 +00007823 LBR16PacketRGBO(image->colormap[i]);
glennrp8e58efd2011-05-20 12:16:29 +00007824 }
7825 }
7826#endif /* MAGICKCORE_QUANTUM_DEPTH > 16 */
7827 }
7828
7829 else if (image->depth > 4)
7830 {
7831#if MAGICKCORE_QUANTUM_DEPTH > 8
7832 /* Scale to 8-bit */
glennrp91d99252011-06-25 14:30:13 +00007833 LBR08PacketRGBO(image->background_color);
glennrp8e58efd2011-05-20 12:16:29 +00007834
7835 for (y=0; y < (ssize_t) image->rows; y++)
7836 {
cristyc82a27b2011-10-21 01:07:16 +00007837 r=GetAuthenticPixels(image,0,y,image->columns,1,exception);
glennrp8e58efd2011-05-20 12:16:29 +00007838
cristy4c08aed2011-07-01 19:47:50 +00007839 if (r == (Quantum *) NULL)
glennrp8e58efd2011-05-20 12:16:29 +00007840 break;
7841
7842 for (x=0; x < (ssize_t) image->columns; x++)
7843 {
glennrp54cf7972011-08-06 14:28:09 +00007844 LBR08PixelRGBA(r);
cristy8a20fa02011-12-27 15:54:31 +00007845 r+=GetPixelChannels(image);
glennrp8e58efd2011-05-20 12:16:29 +00007846 }
glennrpbb4f99d2011-05-22 11:13:17 +00007847
glennrp8e58efd2011-05-20 12:16:29 +00007848 if (SyncAuthenticPixels(image,exception) == MagickFalse)
7849 break;
7850 }
7851
7852 if (image->storage_class == PseudoClass && image->colormap != NULL)
7853 {
cristy3e08f112011-05-24 13:19:30 +00007854 for (i=0; i < (ssize_t) image->colors; i++)
glennrp8e58efd2011-05-20 12:16:29 +00007855 {
glennrp91d99252011-06-25 14:30:13 +00007856 LBR08PacketRGBO(image->colormap[i]);
glennrp8e58efd2011-05-20 12:16:29 +00007857 }
7858 }
7859#endif /* MAGICKCORE_QUANTUM_DEPTH > 8 */
7860 }
7861 else
7862 if (image->depth > 2)
7863 {
7864 /* Scale to 4-bit */
glennrp91d99252011-06-25 14:30:13 +00007865 LBR04PacketRGBO(image->background_color);
glennrp8e58efd2011-05-20 12:16:29 +00007866
7867 for (y=0; y < (ssize_t) image->rows; y++)
7868 {
cristy8a20fa02011-12-27 15:54:31 +00007869 r=GetAuthenticPixels(image,0,y,image->columns,1,exception);
glennrp8e58efd2011-05-20 12:16:29 +00007870
cristy4c08aed2011-07-01 19:47:50 +00007871 if (r == (Quantum *) NULL)
glennrp8e58efd2011-05-20 12:16:29 +00007872 break;
7873
7874 for (x=0; x < (ssize_t) image->columns; x++)
7875 {
glennrp54cf7972011-08-06 14:28:09 +00007876 LBR04PixelRGBA(r);
cristy8a20fa02011-12-27 15:54:31 +00007877 r+=GetPixelChannels(image);
glennrp8e58efd2011-05-20 12:16:29 +00007878 }
glennrpbb4f99d2011-05-22 11:13:17 +00007879
glennrp8e58efd2011-05-20 12:16:29 +00007880 if (SyncAuthenticPixels(image,exception) == MagickFalse)
7881 break;
7882 }
7883
7884 if (image->storage_class == PseudoClass && image->colormap != NULL)
7885 {
cristy3e08f112011-05-24 13:19:30 +00007886 for (i=0; i < (ssize_t) image->colors; i++)
glennrp8e58efd2011-05-20 12:16:29 +00007887 {
glennrp91d99252011-06-25 14:30:13 +00007888 LBR04PacketRGBO(image->colormap[i]);
glennrp8e58efd2011-05-20 12:16:29 +00007889 }
7890 }
7891 }
7892
7893 else if (image->depth > 1)
7894 {
7895 /* Scale to 2-bit */
glennrp91d99252011-06-25 14:30:13 +00007896 LBR02PacketRGBO(image->background_color);
glennrp8e58efd2011-05-20 12:16:29 +00007897
7898 for (y=0; y < (ssize_t) image->rows; y++)
7899 {
cristy8a20fa02011-12-27 15:54:31 +00007900 r=GetAuthenticPixels(image,0,y,image->columns,1,exception);
glennrp8e58efd2011-05-20 12:16:29 +00007901
cristy4c08aed2011-07-01 19:47:50 +00007902 if (r == (Quantum *) NULL)
glennrp8e58efd2011-05-20 12:16:29 +00007903 break;
7904
7905 for (x=0; x < (ssize_t) image->columns; x++)
7906 {
glennrp54cf7972011-08-06 14:28:09 +00007907 LBR02PixelRGBA(r);
cristy8a20fa02011-12-27 15:54:31 +00007908 r+=GetPixelChannels(image);
glennrp8e58efd2011-05-20 12:16:29 +00007909 }
glennrpbb4f99d2011-05-22 11:13:17 +00007910
glennrp8e58efd2011-05-20 12:16:29 +00007911 if (SyncAuthenticPixels(image,exception) == MagickFalse)
7912 break;
7913 }
7914
7915 if (image->storage_class == PseudoClass && image->colormap != NULL)
7916 {
cristy3e08f112011-05-24 13:19:30 +00007917 for (i=0; i < (ssize_t) image->colors; i++)
glennrp8e58efd2011-05-20 12:16:29 +00007918 {
glennrp91d99252011-06-25 14:30:13 +00007919 LBR02PacketRGBO(image->colormap[i]);
glennrp8e58efd2011-05-20 12:16:29 +00007920 }
7921 }
7922 }
7923 else
7924 {
7925 /* Scale to 1-bit */
glennrp91d99252011-06-25 14:30:13 +00007926 LBR01PacketRGBO(image->background_color);
glennrp8e58efd2011-05-20 12:16:29 +00007927
7928 for (y=0; y < (ssize_t) image->rows; y++)
7929 {
cristy8a20fa02011-12-27 15:54:31 +00007930 r=GetAuthenticPixels(image,0,y,image->columns,1,exception);
glennrp8e58efd2011-05-20 12:16:29 +00007931
cristy4c08aed2011-07-01 19:47:50 +00007932 if (r == (Quantum *) NULL)
glennrp8e58efd2011-05-20 12:16:29 +00007933 break;
7934
7935 for (x=0; x < (ssize_t) image->columns; x++)
7936 {
glennrp54cf7972011-08-06 14:28:09 +00007937 LBR01PixelRGBA(r);
cristy8a20fa02011-12-27 15:54:31 +00007938 r+=GetPixelChannels(image);
glennrp8e58efd2011-05-20 12:16:29 +00007939 }
glennrpbb4f99d2011-05-22 11:13:17 +00007940
glennrp8e58efd2011-05-20 12:16:29 +00007941 if (SyncAuthenticPixels(image,exception) == MagickFalse)
7942 break;
7943 }
7944
7945 if (image->storage_class == PseudoClass && image->colormap != NULL)
7946 {
cristy3e08f112011-05-24 13:19:30 +00007947 for (i=0; i < (ssize_t) image->colors; i++)
glennrp8e58efd2011-05-20 12:16:29 +00007948 {
glennrp91d99252011-06-25 14:30:13 +00007949 LBR01PacketRGBO(image->colormap[i]);
glennrp8e58efd2011-05-20 12:16:29 +00007950 }
7951 }
7952 }
glennrp9d0ea4d2011-04-22 18:35:57 +00007953 }
7954
glennrp67b9c1a2011-04-22 18:47:36 +00007955 /* To do: set to next higher multiple of 8 */
7956 if (image->depth < 8)
glennrp70e68a82011-04-01 22:51:14 +00007957 image->depth=8;
glennrpa6a06632011-01-19 15:15:34 +00007958
glennrp2b013e42010-11-24 16:55:50 +00007959#if (MAGICKCORE_QUANTUM_DEPTH > 16)
7960 /* PNG does not handle depths greater than 16 so reduce it even
7961 * if lossy
7962 */
glennrp8e58efd2011-05-20 12:16:29 +00007963 if (image->depth > 8)
glennrp2b013e42010-11-24 16:55:50 +00007964 image->depth=16;
7965#endif
7966
glennrp3faa9a32011-04-23 14:00:25 +00007967#if (MAGICKCORE_QUANTUM_DEPTH > 8)
glennrpc722dd82011-02-24 05:13:21 +00007968 if (image->depth == 16 && mng_info->write_png_depth != 16)
cristyc82a27b2011-10-21 01:07:16 +00007969 if (mng_info->write_png8 || LosslessReduceDepthOK(image,exception) != MagickFalse)
glennrp8640fb52010-11-23 15:48:26 +00007970 image->depth = 8;
7971#endif
7972
glennrpc8c2f062011-02-25 19:00:33 +00007973 /* Normally we run this just once, but in the case of writing PNG8
glennrpe9637cb2011-03-24 16:34:37 +00007974 * we reduce the transparency to binary and run again, then if there
7975 * are still too many colors we reduce to a simple 4-4-4-1, then 3-3-3-1
glennrp8ca51ad2011-05-12 21:22:32 +00007976 * RGBA palette and run again, and then to a simple 3-3-2-1 RGBA
7977 * palette. Then (To do) we take care of a final reduction that is only
7978 * needed if there are still 256 colors present and one of them has both
7979 * transparent and opaque instances.
glennrpc8c2f062011-02-25 19:00:33 +00007980 */
glennrp82b3c532011-03-22 19:20:54 +00007981
glennrp8ca51ad2011-05-12 21:22:32 +00007982 tried_332 = MagickFalse;
glennrp82b3c532011-03-22 19:20:54 +00007983 tried_333 = MagickFalse;
glennrpd3371642011-03-22 19:42:23 +00007984 tried_444 = MagickFalse;
glennrp82b3c532011-03-22 19:20:54 +00007985
glennrp8ca51ad2011-05-12 21:22:32 +00007986 for (j=0; j<6; j++)
glennrpd71e86a2011-02-24 01:28:37 +00007987 {
7988 /* BUILD_PALETTE
7989 *
7990 * Sometimes we get DirectClass images that have 256 colors or fewer.
7991 * This code will build a colormap.
7992 *
7993 * Also, sometimes we get PseudoClass images with an out-of-date
7994 * colormap. This code will replace the colormap with a new one.
7995 * Sometimes we get PseudoClass images that have more than 256 colors.
7996 * This code will delete the colormap and change the image to
7997 * DirectClass.
7998 *
cristy4c08aed2011-07-01 19:47:50 +00007999 * If image->matte is MagickFalse, we ignore the alpha channel
glennrpd71e86a2011-02-24 01:28:37 +00008000 * even though it sometimes contains left-over non-opaque values.
8001 *
8002 * Also we gather some information (number of opaque, transparent,
8003 * and semitransparent pixels, and whether the image has any non-gray
8004 * pixels or only black-and-white pixels) that we might need later.
8005 *
8006 * Even if the user wants to force GrayAlpha or RGBA (colortype 4 or 6)
8007 * we need to check for bogus non-opaque values, at least.
8008 */
glennrp3c218112010-11-27 15:31:26 +00008009
glennrpd71e86a2011-02-24 01:28:37 +00008010 int
8011 n;
glennrp3c218112010-11-27 15:31:26 +00008012
cristy101ab702011-10-13 13:06:32 +00008013 PixelInfo
glennrpd71e86a2011-02-24 01:28:37 +00008014 opaque[260],
8015 semitransparent[260],
8016 transparent[260];
glennrp8bb3a022010-12-13 20:40:04 +00008017
cristy4c08aed2011-07-01 19:47:50 +00008018 register const Quantum
8019 *s;
glennrp8bb3a022010-12-13 20:40:04 +00008020
cristy4c08aed2011-07-01 19:47:50 +00008021 register Quantum
8022 *q,
glennrpfd05d622011-02-25 04:10:33 +00008023 *r;
8024
glennrpd71e86a2011-02-24 01:28:37 +00008025 if (logging != MagickFalse)
8026 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8027 " Enter BUILD_PALETTE:");
8028
8029 if (logging != MagickFalse)
8030 {
glennrp03812ae2010-12-24 01:31:34 +00008031 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
glennrpd71e86a2011-02-24 01:28:37 +00008032 " image->columns=%.20g",(double) image->columns);
8033 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8034 " image->rows=%.20g",(double) image->rows);
8035 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8036 " image->matte=%.20g",(double) image->matte);
8037 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8038 " image->depth=%.20g",(double) image->depth);
glennrp8bb3a022010-12-13 20:40:04 +00008039
glennrpfd05d622011-02-25 04:10:33 +00008040 if (image->storage_class == PseudoClass && image->colormap != NULL)
glennrp7ddcc222010-12-11 05:01:05 +00008041 {
8042 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
glennrpd71e86a2011-02-24 01:28:37 +00008043 " Original colormap:");
glennrp8bb3a022010-12-13 20:40:04 +00008044 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristy4c08aed2011-07-01 19:47:50 +00008045 " i (red,green,blue,alpha)");
glennrp2cc891a2010-12-24 13:44:32 +00008046
glennrpd71e86a2011-02-24 01:28:37 +00008047 for (i=0; i < 256; i++)
glennrp7ddcc222010-12-11 05:01:05 +00008048 {
glennrpd71e86a2011-02-24 01:28:37 +00008049 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8050 " %d (%d,%d,%d,%d)",
8051 (int) i,
8052 (int) image->colormap[i].red,
8053 (int) image->colormap[i].green,
8054 (int) image->colormap[i].blue,
cristy4c08aed2011-07-01 19:47:50 +00008055 (int) image->colormap[i].alpha);
glennrp7ddcc222010-12-11 05:01:05 +00008056 }
glennrp2cc891a2010-12-24 13:44:32 +00008057
glennrpd71e86a2011-02-24 01:28:37 +00008058 for (i=image->colors - 10; i < (ssize_t) image->colors; i++)
8059 {
8060 if (i > 255)
8061 {
8062 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8063 " %d (%d,%d,%d,%d)",
8064 (int) i,
8065 (int) image->colormap[i].red,
8066 (int) image->colormap[i].green,
8067 (int) image->colormap[i].blue,
cristy4c08aed2011-07-01 19:47:50 +00008068 (int) image->colormap[i].alpha);
glennrpd71e86a2011-02-24 01:28:37 +00008069 }
8070 }
glennrp03812ae2010-12-24 01:31:34 +00008071 }
glennrp7ddcc222010-12-11 05:01:05 +00008072
glennrpd71e86a2011-02-24 01:28:37 +00008073 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8074 " image->colors=%d",(int) image->colors);
glennrp7ddcc222010-12-11 05:01:05 +00008075
glennrpd71e86a2011-02-24 01:28:37 +00008076 if (image->colors == 0)
cristyc458f912011-12-27 20:26:40 +00008077 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8078 " (zero means unknown)");
glennrpd6bf1612010-12-17 17:28:54 +00008079
glennrp8d3d6e52011-04-19 04:39:51 +00008080 if (ping_preserve_colormap == MagickFalse)
8081 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8082 " Regenerate the colormap");
glennrpd71e86a2011-02-24 01:28:37 +00008083 }
8084
glennrpd71e86a2011-02-24 01:28:37 +00008085 image_colors=0;
glennrpfd05d622011-02-25 04:10:33 +00008086 number_opaque = 0;
8087 number_semitransparent = 0;
8088 number_transparent = 0;
glennrpd71e86a2011-02-24 01:28:37 +00008089
8090 for (y=0; y < (ssize_t) image->rows; y++)
8091 {
8092 q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
8093
cristyacd2ed22011-08-30 01:44:23 +00008094 if (q == (Quantum *) NULL)
glennrpd71e86a2011-02-24 01:28:37 +00008095 break;
8096
8097 for (x=0; x < (ssize_t) image->columns; x++)
glennrp8bb3a022010-12-13 20:40:04 +00008098 {
glennrp4737d522011-04-29 03:33:42 +00008099 if (image->matte == MagickFalse ||
cristy4c08aed2011-07-01 19:47:50 +00008100 GetPixelAlpha(image,q) == OpaqueAlpha)
glennrpd71e86a2011-02-24 01:28:37 +00008101 {
8102 if (number_opaque < 259)
8103 {
8104 if (number_opaque == 0)
8105 {
cristy101ab702011-10-13 13:06:32 +00008106 GetPixelInfoPixel(image, q, opaque);
cristy4c08aed2011-07-01 19:47:50 +00008107 opaque[0].alpha=OpaqueAlpha;
glennrpd71e86a2011-02-24 01:28:37 +00008108 number_opaque=1;
8109 }
glennrp2cc891a2010-12-24 13:44:32 +00008110
glennrpd71e86a2011-02-24 01:28:37 +00008111 for (i=0; i< (ssize_t) number_opaque; i++)
8112 {
cristy4c08aed2011-07-01 19:47:50 +00008113 if (IsPixelEquivalent(image,q, opaque+i))
glennrpd71e86a2011-02-24 01:28:37 +00008114 break;
8115 }
glennrp7ddcc222010-12-11 05:01:05 +00008116
cristyc458f912011-12-27 20:26:40 +00008117 if (i == (ssize_t) number_opaque && number_opaque < 259)
glennrpd71e86a2011-02-24 01:28:37 +00008118 {
8119 number_opaque++;
cristy101ab702011-10-13 13:06:32 +00008120 GetPixelInfoPixel(image, q, opaque+i);
cristy4c08aed2011-07-01 19:47:50 +00008121 opaque[i].alpha=OpaqueAlpha;
glennrpd71e86a2011-02-24 01:28:37 +00008122 }
8123 }
8124 }
cristy4c08aed2011-07-01 19:47:50 +00008125 else if (GetPixelAlpha(image,q) == TransparentAlpha)
glennrpd71e86a2011-02-24 01:28:37 +00008126 {
8127 if (number_transparent < 259)
8128 {
8129 if (number_transparent == 0)
8130 {
cristy101ab702011-10-13 13:06:32 +00008131 GetPixelInfoPixel(image, q, transparent);
cristy4c08aed2011-07-01 19:47:50 +00008132 ping_trans_color.red=(unsigned short)
8133 GetPixelRed(image,q);
8134 ping_trans_color.green=(unsigned short)
8135 GetPixelGreen(image,q);
8136 ping_trans_color.blue=(unsigned short)
8137 GetPixelBlue(image,q);
8138 ping_trans_color.gray=(unsigned short)
8139 GetPixelRed(image,q);
glennrpd71e86a2011-02-24 01:28:37 +00008140 number_transparent = 1;
8141 }
8142
8143 for (i=0; i< (ssize_t) number_transparent; i++)
8144 {
cristy4c08aed2011-07-01 19:47:50 +00008145 if (IsPixelEquivalent(image,q, transparent+i))
glennrpd71e86a2011-02-24 01:28:37 +00008146 break;
8147 }
8148
8149 if (i == (ssize_t) number_transparent &&
8150 number_transparent < 259)
8151 {
8152 number_transparent++;
cristy101ab702011-10-13 13:06:32 +00008153 GetPixelInfoPixel(image,q,transparent+i);
glennrpd71e86a2011-02-24 01:28:37 +00008154 }
8155 }
8156 }
8157 else
8158 {
8159 if (number_semitransparent < 259)
8160 {
8161 if (number_semitransparent == 0)
8162 {
cristy101ab702011-10-13 13:06:32 +00008163 GetPixelInfoPixel(image,q,semitransparent);
glennrpd71e86a2011-02-24 01:28:37 +00008164 number_semitransparent = 1;
8165 }
8166
8167 for (i=0; i< (ssize_t) number_semitransparent; i++)
8168 {
cristy4c08aed2011-07-01 19:47:50 +00008169 if (IsPixelEquivalent(image,q, semitransparent+i)
8170 && GetPixelAlpha(image,q) ==
8171 semitransparent[i].alpha)
glennrpd71e86a2011-02-24 01:28:37 +00008172 break;
8173 }
8174
8175 if (i == (ssize_t) number_semitransparent &&
8176 number_semitransparent < 259)
8177 {
8178 number_semitransparent++;
cristy101ab702011-10-13 13:06:32 +00008179 GetPixelInfoPixel(image, q, semitransparent+i);
glennrpd71e86a2011-02-24 01:28:37 +00008180 }
8181 }
8182 }
cristyed231572011-07-14 02:18:59 +00008183 q+=GetPixelChannels(image);
glennrpd71e86a2011-02-24 01:28:37 +00008184 }
8185 }
8186
cristy4054bfb2011-08-29 23:41:39 +00008187 if (mng_info->write_png8 == MagickFalse &&
8188 ping_exclude_bKGD == MagickFalse)
glennrpd71e86a2011-02-24 01:28:37 +00008189 {
8190 /* Add the background color to the palette, if it
8191 * isn't already there.
8192 */
glennrpc6c391a2011-04-27 02:23:56 +00008193 if (logging != MagickFalse)
8194 {
8195 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8196 " Check colormap for background (%d,%d,%d)",
8197 (int) image->background_color.red,
8198 (int) image->background_color.green,
8199 (int) image->background_color.blue);
8200 }
glennrpd71e86a2011-02-24 01:28:37 +00008201 for (i=0; i<number_opaque; i++)
8202 {
glennrpca7ad3a2011-04-26 04:44:54 +00008203 if (opaque[i].red == image->background_color.red &&
8204 opaque[i].green == image->background_color.green &&
8205 opaque[i].blue == image->background_color.blue)
8206 break;
glennrpd71e86a2011-02-24 01:28:37 +00008207 }
glennrpd71e86a2011-02-24 01:28:37 +00008208 if (number_opaque < 259 && i == number_opaque)
8209 {
glennrp8e045c82011-04-27 16:40:27 +00008210 opaque[i] = image->background_color;
glennrpc6c391a2011-04-27 02:23:56 +00008211 ping_background.index = i;
8212 if (logging != MagickFalse)
8213 {
8214 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8215 " background_color index is %d",(int) i);
8216 }
8217
glennrpd71e86a2011-02-24 01:28:37 +00008218 }
glennrpa080bc32011-03-11 18:03:44 +00008219 else if (logging != MagickFalse)
8220 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8221 " No room in the colormap to add background color");
glennrpd71e86a2011-02-24 01:28:37 +00008222 }
8223
8224 image_colors=number_opaque+number_transparent+number_semitransparent;
8225
glennrpa080bc32011-03-11 18:03:44 +00008226 if (mng_info->write_png8 != MagickFalse && image_colors > 256)
8227 {
8228 /* No room for the background color; remove it. */
8229 number_opaque--;
8230 image_colors--;
8231 }
8232
glennrpd71e86a2011-02-24 01:28:37 +00008233 if (logging != MagickFalse)
8234 {
8235 if (image_colors > 256)
8236 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8237 " image has more than 256 colors");
8238
8239 else
8240 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8241 " image has %d colors",image_colors);
8242 }
8243
glennrp8d3d6e52011-04-19 04:39:51 +00008244 if (ping_preserve_colormap != MagickFalse)
8245 break;
glennrp8d3d6e52011-04-19 04:39:51 +00008246
glennrpfd05d622011-02-25 04:10:33 +00008247 if (mng_info->write_png_colortype != 7) /* We won't need this info */
glennrpd71e86a2011-02-24 01:28:37 +00008248 {
8249 ping_have_color=MagickFalse;
8250 ping_have_non_bw=MagickFalse;
8251
8252 if(image_colors > 256)
8253 {
8254 for (y=0; y < (ssize_t) image->rows; y++)
8255 {
8256 q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
8257
cristyacd2ed22011-08-30 01:44:23 +00008258 if (q == (Quantum *) NULL)
glennrpd71e86a2011-02-24 01:28:37 +00008259 break;
8260
glennrpe5e6b802011-07-20 14:44:40 +00008261 s=q;
8262 for (x=0; x < (ssize_t) image->columns; x++)
8263 {
8264 if (GetPixelRed(image,s) != GetPixelGreen(image,s) ||
8265 GetPixelRed(image,s) != GetPixelBlue(image,s))
8266 {
8267 ping_have_color=MagickTrue;
8268 ping_have_non_bw=MagickTrue;
8269 break;
8270 }
8271 s+=GetPixelChannels(image);
8272 }
8273
8274 if (ping_have_color != MagickFalse)
8275 break;
8276
glennrpd71e86a2011-02-24 01:28:37 +00008277 /* Worst case is black-and-white; we are looking at every
8278 * pixel twice.
8279 */
8280
glennrpd71e86a2011-02-24 01:28:37 +00008281 if (ping_have_non_bw == MagickFalse)
8282 {
8283 s=q;
8284 for (x=0; x < (ssize_t) image->columns; x++)
8285 {
cristy4c08aed2011-07-01 19:47:50 +00008286 if (GetPixelRed(image,s) != 0 &&
8287 GetPixelRed(image,s) != QuantumRange)
glennrpd71e86a2011-02-24 01:28:37 +00008288 {
8289 ping_have_non_bw=MagickTrue;
glennrpe5e6b802011-07-20 14:44:40 +00008290 break;
glennrpd71e86a2011-02-24 01:28:37 +00008291 }
cristyed231572011-07-14 02:18:59 +00008292 s+=GetPixelChannels(image);
glennrpd71e86a2011-02-24 01:28:37 +00008293 }
glennrpe5e6b802011-07-20 14:44:40 +00008294 }
glennrpd71e86a2011-02-24 01:28:37 +00008295 }
glennrpbb4f99d2011-05-22 11:13:17 +00008296 }
8297 }
glennrpd71e86a2011-02-24 01:28:37 +00008298
8299 if (image_colors < 257)
8300 {
cristy101ab702011-10-13 13:06:32 +00008301 PixelInfo
glennrpd71e86a2011-02-24 01:28:37 +00008302 colormap[260];
glennrpbb4f99d2011-05-22 11:13:17 +00008303
glennrpd71e86a2011-02-24 01:28:37 +00008304 /*
8305 * Initialize image colormap.
glennrp97fd3d02011-02-23 14:58:06 +00008306 */
8307
glennrpd71e86a2011-02-24 01:28:37 +00008308 if (logging != MagickFalse)
8309 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8310 " Sort the new colormap");
glennrp8d579662011-02-23 02:05:02 +00008311
glennrpd71e86a2011-02-24 01:28:37 +00008312 /* Sort palette, transparent first */;
8313
8314 n = 0;
8315
8316 for (i=0; i<number_transparent; i++)
8317 colormap[n++] = transparent[i];
8318
8319 for (i=0; i<number_semitransparent; i++)
8320 colormap[n++] = semitransparent[i];
8321
8322 for (i=0; i<number_opaque; i++)
8323 colormap[n++] = opaque[i];
8324
glennrpc6c391a2011-04-27 02:23:56 +00008325 ping_background.index +=
8326 (number_transparent + number_semitransparent);
glennrpbb4f99d2011-05-22 11:13:17 +00008327
glennrpd71e86a2011-02-24 01:28:37 +00008328 /* image_colors < 257; search the colormap instead of the pixels
8329 * to get ping_have_color and ping_have_non_bw
8330 */
8331 for (i=0; i<n; i++)
8332 {
8333 if (ping_have_color == MagickFalse)
glennrp8d579662011-02-23 02:05:02 +00008334 {
glennrpd71e86a2011-02-24 01:28:37 +00008335 if (colormap[i].red != colormap[i].green ||
8336 colormap[i].red != colormap[i].blue)
8337 {
8338 ping_have_color=MagickTrue;
8339 ping_have_non_bw=MagickTrue;
8340 break;
8341 }
8342 }
8343
8344 if (ping_have_non_bw == MagickFalse)
8345 {
8346 if (colormap[i].red != 0 && colormap[i].red != QuantumRange)
glennrp8d579662011-02-23 02:05:02 +00008347 ping_have_non_bw=MagickTrue;
glennrp8bb3a022010-12-13 20:40:04 +00008348 }
glennrp8bb3a022010-12-13 20:40:04 +00008349 }
8350
glennrpd71e86a2011-02-24 01:28:37 +00008351 if ((mng_info->ping_exclude_tRNS == MagickFalse ||
8352 (number_transparent == 0 && number_semitransparent == 0)) &&
8353 (((mng_info->write_png_colortype-1) ==
8354 PNG_COLOR_TYPE_PALETTE) ||
8355 (mng_info->write_png_colortype == 0)))
8356 {
glennrp6185c532011-01-14 17:58:40 +00008357 if (logging != MagickFalse)
glennrp4f25bd02011-01-01 18:51:28 +00008358 {
glennrpd71e86a2011-02-24 01:28:37 +00008359 if (n != (ssize_t) image_colors)
8360 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8361 " image_colors (%d) and n (%d) don't match",
8362 image_colors, n);
glennrp6185c532011-01-14 17:58:40 +00008363
glennrpd71e86a2011-02-24 01:28:37 +00008364 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8365 " AcquireImageColormap");
glennrp03812ae2010-12-24 01:31:34 +00008366 }
glennrp03812ae2010-12-24 01:31:34 +00008367
glennrpd71e86a2011-02-24 01:28:37 +00008368 image->colors = image_colors;
8369
cristy018f07f2011-09-04 21:15:19 +00008370 if (AcquireImageColormap(image,image_colors,exception) ==
glennrpd71e86a2011-02-24 01:28:37 +00008371 MagickFalse)
glennrp3faa9a32011-04-23 14:00:25 +00008372 ThrowWriterException(ResourceLimitError,
8373 "MemoryAllocationFailed");
glennrpd71e86a2011-02-24 01:28:37 +00008374
8375 for (i=0; i< (ssize_t) image_colors; i++)
8376 image->colormap[i] = colormap[i];
8377
8378 if (logging != MagickFalse)
glennrp6185c532011-01-14 17:58:40 +00008379 {
glennrpd71e86a2011-02-24 01:28:37 +00008380 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8381 " image->colors=%d (%d)",
8382 (int) image->colors, image_colors);
glennrpbb4f99d2011-05-22 11:13:17 +00008383
glennrpd71e86a2011-02-24 01:28:37 +00008384 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8385 " Update the pixel indexes");
8386 }
glennrp6185c532011-01-14 17:58:40 +00008387
glennrpfd05d622011-02-25 04:10:33 +00008388 /* Sync the pixel indices with the new colormap */
8389
glennrpd71e86a2011-02-24 01:28:37 +00008390 for (y=0; y < (ssize_t) image->rows; y++)
8391 {
cristy97707062011-12-27 18:25:00 +00008392 q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
glennrp6185c532011-01-14 17:58:40 +00008393
cristyacd2ed22011-08-30 01:44:23 +00008394 if (q == (Quantum *) NULL)
glennrpd71e86a2011-02-24 01:28:37 +00008395 break;
glennrp6185c532011-01-14 17:58:40 +00008396
glennrpd71e86a2011-02-24 01:28:37 +00008397 for (x=0; x < (ssize_t) image->columns; x++)
glennrp6185c532011-01-14 17:58:40 +00008398 {
glennrpd71e86a2011-02-24 01:28:37 +00008399 for (i=0; i< (ssize_t) image_colors; i++)
glennrp6185c532011-01-14 17:58:40 +00008400 {
glennrpd71e86a2011-02-24 01:28:37 +00008401 if ((image->matte == MagickFalse ||
cristy4c08aed2011-07-01 19:47:50 +00008402 image->colormap[i].alpha == GetPixelAlpha(image,q)) &&
8403 image->colormap[i].red == GetPixelRed(image,q) &&
8404 image->colormap[i].green == GetPixelGreen(image,q) &&
8405 image->colormap[i].blue == GetPixelBlue(image,q))
glennrp6185c532011-01-14 17:58:40 +00008406 {
cristy4c08aed2011-07-01 19:47:50 +00008407 SetPixelIndex(image,i,q);
glennrpd71e86a2011-02-24 01:28:37 +00008408 break;
glennrp6185c532011-01-14 17:58:40 +00008409 }
glennrp6185c532011-01-14 17:58:40 +00008410 }
cristyed231572011-07-14 02:18:59 +00008411 q+=GetPixelChannels(image);
glennrpd71e86a2011-02-24 01:28:37 +00008412 }
glennrp6185c532011-01-14 17:58:40 +00008413
glennrpd71e86a2011-02-24 01:28:37 +00008414 if (SyncAuthenticPixels(image,exception) == MagickFalse)
8415 break;
8416 }
8417 }
8418 }
8419
8420 if (logging != MagickFalse)
8421 {
8422 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8423 " image->colors=%d", (int) image->colors);
8424
8425 if (image->colormap != NULL)
8426 {
8427 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristy4c08aed2011-07-01 19:47:50 +00008428 " i (red,green,blue,alpha)");
glennrpd71e86a2011-02-24 01:28:37 +00008429
8430 for (i=0; i < (ssize_t) image->colors; i++)
8431 {
cristy72988482011-03-29 16:34:38 +00008432 if (i < 300 || i >= (ssize_t) image->colors - 10)
glennrpd71e86a2011-02-24 01:28:37 +00008433 {
8434 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8435 " %d (%d,%d,%d,%d)",
8436 (int) i,
8437 (int) image->colormap[i].red,
8438 (int) image->colormap[i].green,
8439 (int) image->colormap[i].blue,
cristy4c08aed2011-07-01 19:47:50 +00008440 (int) image->colormap[i].alpha);
glennrpd71e86a2011-02-24 01:28:37 +00008441 }
glennrp6185c532011-01-14 17:58:40 +00008442 }
8443 }
glennrp03812ae2010-12-24 01:31:34 +00008444
glennrpd71e86a2011-02-24 01:28:37 +00008445 if (number_transparent < 257)
8446 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8447 " number_transparent = %d",
8448 number_transparent);
8449 else
glennrp03812ae2010-12-24 01:31:34 +00008450
glennrpd71e86a2011-02-24 01:28:37 +00008451 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8452 " number_transparent > 256");
glennrp03812ae2010-12-24 01:31:34 +00008453
glennrpd71e86a2011-02-24 01:28:37 +00008454 if (number_opaque < 257)
8455 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8456 " number_opaque = %d",
8457 number_opaque);
glennrp03812ae2010-12-24 01:31:34 +00008458
glennrpd71e86a2011-02-24 01:28:37 +00008459 else
8460 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8461 " number_opaque > 256");
glennrp6185c532011-01-14 17:58:40 +00008462
glennrpd71e86a2011-02-24 01:28:37 +00008463 if (number_semitransparent < 257)
8464 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8465 " number_semitransparent = %d",
8466 number_semitransparent);
glennrp6185c532011-01-14 17:58:40 +00008467
glennrpd71e86a2011-02-24 01:28:37 +00008468 else
8469 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8470 " number_semitransparent > 256");
glennrpa6a06632011-01-19 15:15:34 +00008471
glennrpd71e86a2011-02-24 01:28:37 +00008472 if (ping_have_non_bw == MagickFalse)
8473 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8474 " All pixels and the background are black or white");
glennrpa6a06632011-01-19 15:15:34 +00008475
glennrpd71e86a2011-02-24 01:28:37 +00008476 else if (ping_have_color == MagickFalse)
8477 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8478 " All pixels and the background are gray");
8479
8480 else
8481 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8482 " At least one pixel or the background is non-gray");
glennrp6185c532011-01-14 17:58:40 +00008483
glennrp03812ae2010-12-24 01:31:34 +00008484 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8485 " Exit BUILD_PALETTE:");
glennrpd71e86a2011-02-24 01:28:37 +00008486 }
glennrpfd05d622011-02-25 04:10:33 +00008487
glennrpc8c2f062011-02-25 19:00:33 +00008488 if (mng_info->write_png8 == MagickFalse)
8489 break;
glennrpfd05d622011-02-25 04:10:33 +00008490
glennrpc8c2f062011-02-25 19:00:33 +00008491 /* Make any reductions necessary for the PNG8 format */
8492 if (image_colors <= 256 &&
8493 image_colors != 0 && image->colormap != NULL &&
8494 number_semitransparent == 0 &&
8495 number_transparent <= 1)
8496 break;
8497
8498 /* PNG8 can't have semitransparent colors so we threshold the
glennrp130fc452011-08-20 03:43:18 +00008499 * opacity to 0 or OpaqueOpacity, and PNG8 can only have one
8500 * transparent color so if more than one is transparent we merge
8501 * them into image->background_color.
glennrpc8c2f062011-02-25 19:00:33 +00008502 */
glennrp130fc452011-08-20 03:43:18 +00008503 if (number_semitransparent != 0 || number_transparent > 1)
glennrpc8c2f062011-02-25 19:00:33 +00008504 {
8505 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8506 " Thresholding the alpha channel to binary");
8507
8508 for (y=0; y < (ssize_t) image->rows; y++)
8509 {
cristy8a20fa02011-12-27 15:54:31 +00008510 r=GetAuthenticPixels(image,0,y,image->columns,1,exception);
glennrpc8c2f062011-02-25 19:00:33 +00008511
cristy4c08aed2011-07-01 19:47:50 +00008512 if (r == (Quantum *) NULL)
glennrpc8c2f062011-02-25 19:00:33 +00008513 break;
8514
8515 for (x=0; x < (ssize_t) image->columns; x++)
8516 {
glennrpf73547f2011-08-20 04:40:26 +00008517 if (GetPixelAlpha(image,r) < OpaqueAlpha/2)
glennrp8ca51ad2011-05-12 21:22:32 +00008518 {
cristy803640d2011-11-17 02:11:32 +00008519 SetPixelInfoPixel(image,&image->background_color,r);
cristy4c08aed2011-07-01 19:47:50 +00008520 SetPixelAlpha(image,TransparentAlpha,r);
glennrp8ca51ad2011-05-12 21:22:32 +00008521 }
8522 else
cristy4c08aed2011-07-01 19:47:50 +00008523 SetPixelAlpha(image,OpaqueAlpha,r);
cristyed231572011-07-14 02:18:59 +00008524 r+=GetPixelChannels(image);
glennrpc8c2f062011-02-25 19:00:33 +00008525 }
glennrpbb4f99d2011-05-22 11:13:17 +00008526
glennrpc8c2f062011-02-25 19:00:33 +00008527 if (SyncAuthenticPixels(image,exception) == MagickFalse)
8528 break;
8529
8530 if (image_colors != 0 && image_colors <= 256 &&
8531 image->colormap != NULL)
8532 for (i=0; i<image_colors; i++)
cristy4c08aed2011-07-01 19:47:50 +00008533 image->colormap[i].alpha =
8534 (image->colormap[i].alpha > TransparentAlpha/2 ?
8535 TransparentAlpha : OpaqueAlpha);
glennrpc8c2f062011-02-25 19:00:33 +00008536 }
8537 continue;
8538 }
8539
8540 /* PNG8 can't have more than 256 colors so we quantize the pixels and
glennrpe9637cb2011-03-24 16:34:37 +00008541 * background color to the 4-4-4-1, 3-3-3-1 or 3-3-2-1 palette. If the
8542 * image is mostly gray, the 4-4-4-1 palette is likely to end up with 256
8543 * colors or less.
glennrpc8c2f062011-02-25 19:00:33 +00008544 */
glennrpd3371642011-03-22 19:42:23 +00008545 if (tried_444 == MagickFalse && (image_colors == 0 || image_colors > 256))
8546 {
8547 if (logging != MagickFalse)
8548 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8549 " Quantizing the background color to 4-4-4");
8550
8551 tried_444 = MagickTrue;
8552
glennrp91d99252011-06-25 14:30:13 +00008553 LBR04PacketRGB(image->background_color);
glennrpd3371642011-03-22 19:42:23 +00008554
8555 if (logging != MagickFalse)
8556 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8557 " Quantizing the pixel colors to 4-4-4");
8558
8559 if (image->colormap == NULL)
8560 {
8561 for (y=0; y < (ssize_t) image->rows; y++)
8562 {
cristy8a20fa02011-12-27 15:54:31 +00008563 r=GetAuthenticPixels(image,0,y,image->columns,1,exception);
glennrpd3371642011-03-22 19:42:23 +00008564
cristy4c08aed2011-07-01 19:47:50 +00008565 if (r == (Quantum *) NULL)
glennrpd3371642011-03-22 19:42:23 +00008566 break;
8567
8568 for (x=0; x < (ssize_t) image->columns; x++)
8569 {
cristy4c08aed2011-07-01 19:47:50 +00008570 if (GetPixelAlpha(image,r) == OpaqueAlpha)
glennrp54cf7972011-08-06 14:28:09 +00008571 LBR04PixelRGB(r);
cristy8a20fa02011-12-27 15:54:31 +00008572 r+=GetPixelChannels(image);
glennrpd3371642011-03-22 19:42:23 +00008573 }
glennrpbb4f99d2011-05-22 11:13:17 +00008574
glennrpd3371642011-03-22 19:42:23 +00008575 if (SyncAuthenticPixels(image,exception) == MagickFalse)
8576 break;
8577 }
8578 }
8579
8580 else /* Should not reach this; colormap already exists and
8581 must be <= 256 */
8582 {
8583 if (logging != MagickFalse)
8584 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8585 " Quantizing the colormap to 4-4-4");
glennrp8e58efd2011-05-20 12:16:29 +00008586
glennrpd3371642011-03-22 19:42:23 +00008587 for (i=0; i<image_colors; i++)
8588 {
glennrp91d99252011-06-25 14:30:13 +00008589 LBR04PacketRGB(image->colormap[i]);
glennrpd3371642011-03-22 19:42:23 +00008590 }
8591 }
8592 continue;
8593 }
8594
glennrp82b3c532011-03-22 19:20:54 +00008595 if (tried_333 == MagickFalse && (image_colors == 0 || image_colors > 256))
8596 {
8597 if (logging != MagickFalse)
8598 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8599 " Quantizing the background color to 3-3-3");
8600
8601 tried_333 = MagickTrue;
8602
glennrp91d99252011-06-25 14:30:13 +00008603 LBR03PacketRGB(image->background_color);
glennrp82b3c532011-03-22 19:20:54 +00008604
8605 if (logging != MagickFalse)
8606 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
glennrpe9637cb2011-03-24 16:34:37 +00008607 " Quantizing the pixel colors to 3-3-3-1");
glennrp82b3c532011-03-22 19:20:54 +00008608
8609 if (image->colormap == NULL)
8610 {
8611 for (y=0; y < (ssize_t) image->rows; y++)
8612 {
cristy8a20fa02011-12-27 15:54:31 +00008613 r=GetAuthenticPixels(image,0,y,image->columns,1,exception);
glennrp82b3c532011-03-22 19:20:54 +00008614
cristy4c08aed2011-07-01 19:47:50 +00008615 if (r == (Quantum *) NULL)
glennrp82b3c532011-03-22 19:20:54 +00008616 break;
8617
8618 for (x=0; x < (ssize_t) image->columns; x++)
8619 {
cristy4c08aed2011-07-01 19:47:50 +00008620 if (GetPixelAlpha(image,r) == OpaqueAlpha)
8621 LBR03RGB(r);
cristy8a20fa02011-12-27 15:54:31 +00008622 r+=GetPixelChannels(image);
glennrp82b3c532011-03-22 19:20:54 +00008623 }
glennrpbb4f99d2011-05-22 11:13:17 +00008624
glennrp82b3c532011-03-22 19:20:54 +00008625 if (SyncAuthenticPixels(image,exception) == MagickFalse)
8626 break;
8627 }
8628 }
8629
8630 else /* Should not reach this; colormap already exists and
8631 must be <= 256 */
8632 {
8633 if (logging != MagickFalse)
8634 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
glennrpe9637cb2011-03-24 16:34:37 +00008635 " Quantizing the colormap to 3-3-3-1");
glennrp82b3c532011-03-22 19:20:54 +00008636 for (i=0; i<image_colors; i++)
8637 {
glennrp91d99252011-06-25 14:30:13 +00008638 LBR03PacketRGB(image->colormap[i]);
glennrp82b3c532011-03-22 19:20:54 +00008639 }
glennrpd3371642011-03-22 19:42:23 +00008640 }
8641 continue;
glennrp82b3c532011-03-22 19:20:54 +00008642 }
glennrpc8c2f062011-02-25 19:00:33 +00008643
glennrp8ca51ad2011-05-12 21:22:32 +00008644 if (tried_332 == MagickFalse && (image_colors == 0 || image_colors > 256))
glennrpc8c2f062011-02-25 19:00:33 +00008645 {
8646 if (logging != MagickFalse)
glennrpfd05d622011-02-25 04:10:33 +00008647 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
glennrpc8c2f062011-02-25 19:00:33 +00008648 " Quantizing the background color to 3-3-2");
glennrpfd05d622011-02-25 04:10:33 +00008649
glennrp8ca51ad2011-05-12 21:22:32 +00008650 tried_332 = MagickTrue;
8651
glennrp3faa9a32011-04-23 14:00:25 +00008652 /* Red and green were already done so we only quantize the blue
8653 * channel
8654 */
8655
glennrp91d99252011-06-25 14:30:13 +00008656 LBR02PacketBlue(image->background_color);
glennrpfd05d622011-02-25 04:10:33 +00008657
glennrpc8c2f062011-02-25 19:00:33 +00008658 if (logging != MagickFalse)
8659 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
glennrpe9637cb2011-03-24 16:34:37 +00008660 " Quantizing the pixel colors to 3-3-2-1");
glennrpfd05d622011-02-25 04:10:33 +00008661
glennrpc8c2f062011-02-25 19:00:33 +00008662 if (image->colormap == NULL)
8663 {
8664 for (y=0; y < (ssize_t) image->rows; y++)
8665 {
cristy8a20fa02011-12-27 15:54:31 +00008666 r=GetAuthenticPixels(image,0,y,image->columns,1,exception);
glennrpc8c2f062011-02-25 19:00:33 +00008667
cristy4c08aed2011-07-01 19:47:50 +00008668 if (r == (Quantum *) NULL)
glennrpc8c2f062011-02-25 19:00:33 +00008669 break;
8670
8671 for (x=0; x < (ssize_t) image->columns; x++)
8672 {
cristy4c08aed2011-07-01 19:47:50 +00008673 if (GetPixelAlpha(image,r) == OpaqueAlpha)
glennrp54cf7972011-08-06 14:28:09 +00008674 LBR02PixelBlue(r);
cristy8a20fa02011-12-27 15:54:31 +00008675 r+=GetPixelChannels(image);
glennrpc8c2f062011-02-25 19:00:33 +00008676 }
glennrpbb4f99d2011-05-22 11:13:17 +00008677
glennrpc8c2f062011-02-25 19:00:33 +00008678 if (SyncAuthenticPixels(image,exception) == MagickFalse)
8679 break;
8680 }
8681 }
glennrpfd05d622011-02-25 04:10:33 +00008682
glennrpc8c2f062011-02-25 19:00:33 +00008683 else /* Should not reach this; colormap already exists and
8684 must be <= 256 */
8685 {
8686 if (logging != MagickFalse)
glennrpfd05d622011-02-25 04:10:33 +00008687 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
glennrpe9637cb2011-03-24 16:34:37 +00008688 " Quantizing the colormap to 3-3-2-1");
glennrpc8c2f062011-02-25 19:00:33 +00008689 for (i=0; i<image_colors; i++)
8690 {
glennrp91d99252011-06-25 14:30:13 +00008691 LBR02PacketBlue(image->colormap[i]);
glennrpc8c2f062011-02-25 19:00:33 +00008692 }
8693 }
8694 continue;
8695 }
8696 break;
glennrp8ca51ad2011-05-12 21:22:32 +00008697
8698 if (image_colors == 0 || image_colors > 256)
8699 {
8700 /* Take care of special case with 256 colors + 1 transparent
8701 * color. We don't need to quantize to 2-3-2-1; we only need to
8702 * eliminate one color, so we'll merge the two darkest red
8703 * colors (0x49, 0, 0) -> (0x24, 0, 0).
8704 */
8705 if (ScaleQuantumToChar(image->background_color.red) == 0x49 &&
8706 ScaleQuantumToChar(image->background_color.green) == 0x00 &&
8707 ScaleQuantumToChar(image->background_color.blue) == 0x00)
8708 {
8709 image->background_color.red=ScaleCharToQuantum(0x24);
8710 }
glennrpbb4f99d2011-05-22 11:13:17 +00008711
glennrp8ca51ad2011-05-12 21:22:32 +00008712 if (image->colormap == NULL)
8713 {
8714 for (y=0; y < (ssize_t) image->rows; y++)
8715 {
cristy8a20fa02011-12-27 15:54:31 +00008716 r=GetAuthenticPixels(image,0,y,image->columns,1,exception);
glennrpbb4f99d2011-05-22 11:13:17 +00008717
cristy4c08aed2011-07-01 19:47:50 +00008718 if (r == (Quantum *) NULL)
glennrp8ca51ad2011-05-12 21:22:32 +00008719 break;
glennrpbb4f99d2011-05-22 11:13:17 +00008720
glennrp8ca51ad2011-05-12 21:22:32 +00008721 for (x=0; x < (ssize_t) image->columns; x++)
8722 {
cristy4c08aed2011-07-01 19:47:50 +00008723 if (ScaleQuantumToChar(GetPixelRed(image,r)) == 0x49 &&
8724 ScaleQuantumToChar(GetPixelGreen(image,r)) == 0x00 &&
8725 ScaleQuantumToChar(GetPixelBlue(image,r)) == 0x00 &&
8726 GetPixelAlpha(image,r) == OpaqueAlpha)
glennrp8ca51ad2011-05-12 21:22:32 +00008727 {
cristy4c08aed2011-07-01 19:47:50 +00008728 SetPixelRed(image,ScaleCharToQuantum(0x24),r);
glennrp8ca51ad2011-05-12 21:22:32 +00008729 }
cristyed231572011-07-14 02:18:59 +00008730 r+=GetPixelChannels(image);
glennrp8ca51ad2011-05-12 21:22:32 +00008731 }
glennrpbb4f99d2011-05-22 11:13:17 +00008732
glennrp8ca51ad2011-05-12 21:22:32 +00008733 if (SyncAuthenticPixels(image,exception) == MagickFalse)
8734 break;
glennrpbb4f99d2011-05-22 11:13:17 +00008735
glennrp8ca51ad2011-05-12 21:22:32 +00008736 }
8737 }
8738
8739 else
8740 {
8741 for (i=0; i<image_colors; i++)
8742 {
8743 if (ScaleQuantumToChar(image->colormap[i].red) == 0x49 &&
8744 ScaleQuantumToChar(image->colormap[i].green) == 0x00 &&
8745 ScaleQuantumToChar(image->colormap[i].blue) == 0x00)
8746 {
8747 image->colormap[i].red=ScaleCharToQuantum(0x24);
8748 }
8749 }
8750 }
8751 }
glennrpd71e86a2011-02-24 01:28:37 +00008752 }
glennrpfd05d622011-02-25 04:10:33 +00008753 /* END OF BUILD_PALETTE */
glennrp3c218112010-11-27 15:31:26 +00008754
glennrpfd05d622011-02-25 04:10:33 +00008755 /* If we are excluding the tRNS chunk and there is transparency,
8756 * then we must write a Gray-Alpha (color-type 4) or RGBA (color-type 6)
8757 * PNG.
glennrp8d579662011-02-23 02:05:02 +00008758 */
glennrp0e8ea192010-12-24 18:00:33 +00008759 if (mng_info->ping_exclude_tRNS != MagickFalse &&
8760 (number_transparent != 0 || number_semitransparent != 0))
8761 {
glennrpd17915c2011-04-29 14:24:22 +00008762 unsigned int colortype=mng_info->write_png_colortype;
glennrp0e8ea192010-12-24 18:00:33 +00008763
8764 if (ping_have_color == MagickFalse)
8765 mng_info->write_png_colortype = 5;
8766
8767 else
8768 mng_info->write_png_colortype = 7;
8769
glennrp8d579662011-02-23 02:05:02 +00008770 if (colortype != 0 &&
glennrpd17915c2011-04-29 14:24:22 +00008771 mng_info->write_png_colortype != colortype)
glennrp0e8ea192010-12-24 18:00:33 +00008772 ping_need_colortype_warning=MagickTrue;
glennrp0b206f52011-01-07 04:55:32 +00008773
glennrp0e8ea192010-12-24 18:00:33 +00008774 }
8775
glennrpfd05d622011-02-25 04:10:33 +00008776 /* See if cheap transparency is possible. It is only possible
8777 * when there is a single transparent color, no semitransparent
8778 * color, and no opaque color that has the same RGB components
glennrp5a39f372011-02-25 04:52:16 +00008779 * as the transparent color. We only need this information if
8780 * we are writing a PNG with colortype 0 or 2, and we have not
8781 * excluded the tRNS chunk.
glennrpfd05d622011-02-25 04:10:33 +00008782 */
glennrp5a39f372011-02-25 04:52:16 +00008783 if (number_transparent == 1 &&
8784 mng_info->write_png_colortype < 4)
glennrpfd05d622011-02-25 04:10:33 +00008785 {
8786 ping_have_cheap_transparency = MagickTrue;
8787
8788 if (number_semitransparent != 0)
8789 ping_have_cheap_transparency = MagickFalse;
8790
8791 else if (image_colors == 0 || image_colors > 256 ||
8792 image->colormap == NULL)
8793 {
cristy4c08aed2011-07-01 19:47:50 +00008794 register const Quantum
glennrpfd05d622011-02-25 04:10:33 +00008795 *q;
8796
glennrpfd05d622011-02-25 04:10:33 +00008797 for (y=0; y < (ssize_t) image->rows; y++)
8798 {
8799 q=GetVirtualPixels(image,0,y,image->columns,1, exception);
8800
cristyacd2ed22011-08-30 01:44:23 +00008801 if (q == (Quantum *) NULL)
glennrpfd05d622011-02-25 04:10:33 +00008802 break;
8803
8804 for (x=0; x < (ssize_t) image->columns; x++)
8805 {
cristy4c08aed2011-07-01 19:47:50 +00008806 if (GetPixelAlpha(image,q) != TransparentAlpha &&
glennrp847370c2011-07-05 17:37:15 +00008807 (unsigned short) GetPixelRed(image,q) ==
8808 ping_trans_color.red &&
8809 (unsigned short) GetPixelGreen(image,q) ==
8810 ping_trans_color.green &&
8811 (unsigned short) GetPixelBlue(image,q) ==
8812 ping_trans_color.blue)
glennrpfd05d622011-02-25 04:10:33 +00008813 {
8814 ping_have_cheap_transparency = MagickFalse;
8815 break;
8816 }
8817
cristyed231572011-07-14 02:18:59 +00008818 q+=GetPixelChannels(image);
glennrpfd05d622011-02-25 04:10:33 +00008819 }
glennrpbb4f99d2011-05-22 11:13:17 +00008820
glennrpfd05d622011-02-25 04:10:33 +00008821 if (ping_have_cheap_transparency == MagickFalse)
8822 break;
8823 }
8824 }
8825 else
8826 {
glennrp67b9c1a2011-04-22 18:47:36 +00008827 /* Assuming that image->colormap[0] is the one transparent color
8828 * and that all others are opaque.
8829 */
glennrpfd05d622011-02-25 04:10:33 +00008830 if (image_colors > 1)
glennrp67b9c1a2011-04-22 18:47:36 +00008831 for (i=1; i<image_colors; i++)
8832 if (image->colormap[i].red == image->colormap[0].red &&
8833 image->colormap[i].green == image->colormap[0].green &&
8834 image->colormap[i].blue == image->colormap[0].blue)
glennrpfd05d622011-02-25 04:10:33 +00008835 {
glennrp67b9c1a2011-04-22 18:47:36 +00008836 ping_have_cheap_transparency = MagickFalse;
8837 break;
glennrpfd05d622011-02-25 04:10:33 +00008838 }
8839 }
glennrpbb4f99d2011-05-22 11:13:17 +00008840
glennrpfd05d622011-02-25 04:10:33 +00008841 if (logging != MagickFalse)
8842 {
8843 if (ping_have_cheap_transparency == MagickFalse)
8844 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8845 " Cheap transparency is not possible.");
8846
8847 else
8848 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8849 " Cheap transparency is possible.");
8850 }
8851 }
8852 else
8853 ping_have_cheap_transparency = MagickFalse;
8854
glennrp8640fb52010-11-23 15:48:26 +00008855 image_depth=image->depth;
8856
glennrp26c990a2010-11-23 02:23:20 +00008857 quantum_info = (QuantumInfo *) NULL;
8858 number_colors=0;
glennrpf09bded2011-01-08 01:15:59 +00008859 image_colors=(int) image->colors;
glennrp26c990a2010-11-23 02:23:20 +00008860 image_matte=image->matte;
8861
glennrp0fe50b42010-11-16 03:52:51 +00008862 mng_info->IsPalette=image->storage_class == PseudoClass &&
glennrp1273f7b2011-02-24 03:20:30 +00008863 image_colors <= 256 && image->colormap != NULL;
cristy3ed852e2009-09-05 21:47:34 +00008864
glennrp52a479c2011-02-26 21:14:38 +00008865 if ((mng_info->write_png_colortype == 4 || mng_info->write_png8) &&
8866 (image->colors == 0 || image->colormap == NULL))
8867 {
glennrp52a479c2011-02-26 21:14:38 +00008868 image_info=DestroyImageInfo(image_info);
8869 image=DestroyImage(image);
cristyc82a27b2011-10-21 01:07:16 +00008870 (void) ThrowMagickException(exception,GetMagickModule(),CoderError,
glennrp15e01552011-03-06 23:29:17 +00008871 "Cannot write PNG8 or color-type 3; colormap is NULL",
8872 "`%s'",IMimage->filename);
glennrp52a479c2011-02-26 21:14:38 +00008873#if defined(PNG_SETJMP_NOT_THREAD_SAFE)
8874 UnlockSemaphoreInfo(ping_semaphore);
8875#endif
8876 return(MagickFalse);
8877 }
8878
cristy3ed852e2009-09-05 21:47:34 +00008879 /*
8880 Allocate the PNG structures
8881 */
8882#ifdef PNG_USER_MEM_SUPPORTED
cristyc82a27b2011-10-21 01:07:16 +00008883 error_info.image=image;
8884 error_info.exception=exception;
8885 ping=png_create_write_struct_2(PNG_LIBPNG_VER_STRING,&error_info,
glennrpcf002022011-01-30 02:38:15 +00008886 MagickPNGErrorHandler,MagickPNGWarningHandler,(void *) NULL,
8887 (png_malloc_ptr) Magick_png_malloc,(png_free_ptr) Magick_png_free);
glennrp0fe50b42010-11-16 03:52:51 +00008888
cristy3ed852e2009-09-05 21:47:34 +00008889#else
cristyc82a27b2011-10-21 01:07:16 +00008890 ping=png_create_write_struct(PNG_LIBPNG_VER_STRING,&error_info,
glennrpcf002022011-01-30 02:38:15 +00008891 MagickPNGErrorHandler,MagickPNGWarningHandler);
glennrp0fe50b42010-11-16 03:52:51 +00008892
cristy3ed852e2009-09-05 21:47:34 +00008893#endif
8894 if (ping == (png_struct *) NULL)
8895 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
glennrp0fe50b42010-11-16 03:52:51 +00008896
cristy3ed852e2009-09-05 21:47:34 +00008897 ping_info=png_create_info_struct(ping);
glennrp0fe50b42010-11-16 03:52:51 +00008898
cristy3ed852e2009-09-05 21:47:34 +00008899 if (ping_info == (png_info *) NULL)
8900 {
8901 png_destroy_write_struct(&ping,(png_info **) NULL);
8902 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
8903 }
glennrp0fe50b42010-11-16 03:52:51 +00008904
cristy3ed852e2009-09-05 21:47:34 +00008905 png_set_write_fn(ping,image,png_put_data,png_flush_data);
glennrpcf002022011-01-30 02:38:15 +00008906 ping_pixels=(unsigned char *) NULL;
cristy3ed852e2009-09-05 21:47:34 +00008907
glennrp5af765f2010-03-30 11:12:18 +00008908 if (setjmp(png_jmpbuf(ping)))
cristy3ed852e2009-09-05 21:47:34 +00008909 {
8910 /*
8911 PNG write failed.
8912 */
8913#ifdef PNG_DEBUG
8914 if (image_info->verbose)
8915 (void) printf("PNG write has failed.\n");
8916#endif
8917 png_destroy_write_struct(&ping,&ping_info);
8918#if defined(PNG_SETJMP_NOT_THREAD_SAFE)
glennrpcf002022011-01-30 02:38:15 +00008919 UnlockSemaphoreInfo(ping_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00008920#endif
glennrpda8f3a72011-02-27 23:54:12 +00008921 if (ping_have_blob != MagickFalse)
glennrpb9cfe272010-12-21 15:08:06 +00008922 (void) CloseBlob(image);
8923 image_info=DestroyImageInfo(image_info);
8924 image=DestroyImage(image);
cristy3ed852e2009-09-05 21:47:34 +00008925 return(MagickFalse);
8926 }
8927 /*
8928 Prepare PNG for writing.
8929 */
8930#if defined(PNG_MNG_FEATURES_SUPPORTED)
8931 if (mng_info->write_mng)
8932 (void) png_permit_mng_features(ping,PNG_ALL_MNG_FEATURES);
glennrp2b013e42010-11-24 16:55:50 +00008933
cristy3ed852e2009-09-05 21:47:34 +00008934#else
8935# ifdef PNG_WRITE_EMPTY_PLTE_SUPPORTED
8936 if (mng_info->write_mng)
8937 png_permit_empty_plte(ping,MagickTrue);
glennrp2b013e42010-11-24 16:55:50 +00008938
cristy3ed852e2009-09-05 21:47:34 +00008939# endif
8940#endif
glennrp2b013e42010-11-24 16:55:50 +00008941
cristy3ed852e2009-09-05 21:47:34 +00008942 x=0;
glennrp2b013e42010-11-24 16:55:50 +00008943
cristy4e5bc842010-06-09 13:56:01 +00008944 ping_width=(png_uint_32) image->columns;
8945 ping_height=(png_uint_32) image->rows;
glennrp2b013e42010-11-24 16:55:50 +00008946
cristy3ed852e2009-09-05 21:47:34 +00008947 if (mng_info->write_png8 || mng_info->write_png24 || mng_info->write_png32)
8948 image_depth=8;
glennrp0fe50b42010-11-16 03:52:51 +00008949
cristy3ed852e2009-09-05 21:47:34 +00008950 if (mng_info->write_png_depth != 0)
8951 image_depth=mng_info->write_png_depth;
glennrp0fe50b42010-11-16 03:52:51 +00008952
cristy3ed852e2009-09-05 21:47:34 +00008953 /* Adjust requested depth to next higher valid depth if necessary */
8954 if (image_depth > 8)
8955 image_depth=16;
glennrp0fe50b42010-11-16 03:52:51 +00008956
cristy3ed852e2009-09-05 21:47:34 +00008957 if ((image_depth > 4) && (image_depth < 8))
8958 image_depth=8;
glennrp0fe50b42010-11-16 03:52:51 +00008959
cristy3ed852e2009-09-05 21:47:34 +00008960 if (image_depth == 3)
8961 image_depth=4;
glennrp0fe50b42010-11-16 03:52:51 +00008962
cristy3ed852e2009-09-05 21:47:34 +00008963 if (logging != MagickFalse)
8964 {
8965 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00008966 " width=%.20g",(double) ping_width);
cristy3ed852e2009-09-05 21:47:34 +00008967 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00008968 " height=%.20g",(double) ping_height);
cristy3ed852e2009-09-05 21:47:34 +00008969 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00008970 " image_matte=%.20g",(double) image->matte);
cristy3ed852e2009-09-05 21:47:34 +00008971 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
glennrp8640fb52010-11-23 15:48:26 +00008972 " image->depth=%.20g",(double) image->depth);
cristy3ed852e2009-09-05 21:47:34 +00008973 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
glennrp8640fb52010-11-23 15:48:26 +00008974 " Tentative ping_bit_depth=%.20g",(double) image_depth);
cristy3ed852e2009-09-05 21:47:34 +00008975 }
glennrp8640fb52010-11-23 15:48:26 +00008976
cristy3ed852e2009-09-05 21:47:34 +00008977 save_image_depth=image_depth;
glennrp5af765f2010-03-30 11:12:18 +00008978 ping_bit_depth=(png_byte) save_image_depth;
glennrpdfd70802010-11-14 01:23:35 +00008979
glennrp26f37912010-12-23 16:22:42 +00008980
cristy3ed852e2009-09-05 21:47:34 +00008981#if defined(PNG_pHYs_SUPPORTED)
glennrp26f37912010-12-23 16:22:42 +00008982 if (ping_exclude_pHYs == MagickFalse)
8983 {
cristy2a11bef2011-10-28 18:33:11 +00008984 if ((image->resolution.x != 0) && (image->resolution.y != 0) &&
cristy3ed852e2009-09-05 21:47:34 +00008985 (!mng_info->write_mng || !mng_info->equal_physs))
8986 {
glennrp0fe50b42010-11-16 03:52:51 +00008987 if (logging != MagickFalse)
glennrpdfd70802010-11-14 01:23:35 +00008988 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8989 " Setting up pHYs chunk");
cristy3ed852e2009-09-05 21:47:34 +00008990
8991 if (image->units == PixelsPerInchResolution)
8992 {
glennrpdfd70802010-11-14 01:23:35 +00008993 ping_pHYs_unit_type=PNG_RESOLUTION_METER;
glennrp823b55c2011-03-14 18:46:46 +00008994 ping_pHYs_x_resolution=
cristy2a11bef2011-10-28 18:33:11 +00008995 (png_uint_32) ((100.0*image->resolution.x+0.5)/2.54);
glennrp823b55c2011-03-14 18:46:46 +00008996 ping_pHYs_y_resolution=
cristy2a11bef2011-10-28 18:33:11 +00008997 (png_uint_32) ((100.0*image->resolution.y+0.5)/2.54);
cristy3ed852e2009-09-05 21:47:34 +00008998 }
glennrpdfd70802010-11-14 01:23:35 +00008999
cristy3ed852e2009-09-05 21:47:34 +00009000 else if (image->units == PixelsPerCentimeterResolution)
9001 {
glennrpdfd70802010-11-14 01:23:35 +00009002 ping_pHYs_unit_type=PNG_RESOLUTION_METER;
cristy2a11bef2011-10-28 18:33:11 +00009003 ping_pHYs_x_resolution=(png_uint_32) (100.0*image->resolution.x+0.5);
9004 ping_pHYs_y_resolution=(png_uint_32) (100.0*image->resolution.y+0.5);
cristy3ed852e2009-09-05 21:47:34 +00009005 }
glennrp991d11d2010-11-12 21:55:28 +00009006
cristy3ed852e2009-09-05 21:47:34 +00009007 else
9008 {
glennrpdfd70802010-11-14 01:23:35 +00009009 ping_pHYs_unit_type=PNG_RESOLUTION_UNKNOWN;
cristy2a11bef2011-10-28 18:33:11 +00009010 ping_pHYs_x_resolution=(png_uint_32) image->resolution.x;
9011 ping_pHYs_y_resolution=(png_uint_32) image->resolution.y;
cristy3ed852e2009-09-05 21:47:34 +00009012 }
glennrp991d11d2010-11-12 21:55:28 +00009013
glennrp823b55c2011-03-14 18:46:46 +00009014 if (logging != MagickFalse)
9015 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9016 " Set up PNG pHYs chunk: xres: %.20g, yres: %.20g, units: %d.",
9017 (double) ping_pHYs_x_resolution,(double) ping_pHYs_y_resolution,
9018 (int) ping_pHYs_unit_type);
glennrp991d11d2010-11-12 21:55:28 +00009019 ping_have_pHYs = MagickTrue;
cristy3ed852e2009-09-05 21:47:34 +00009020 }
glennrp26f37912010-12-23 16:22:42 +00009021 }
cristy3ed852e2009-09-05 21:47:34 +00009022#endif
glennrpa521b2f2010-10-29 04:11:03 +00009023
glennrp26f37912010-12-23 16:22:42 +00009024 if (ping_exclude_bKGD == MagickFalse)
9025 {
glennrpa521b2f2010-10-29 04:11:03 +00009026 if ((!mng_info->adjoin || !mng_info->equal_backgrounds))
cristy3ed852e2009-09-05 21:47:34 +00009027 {
glennrpa521b2f2010-10-29 04:11:03 +00009028 unsigned int
9029 mask;
cristy3ed852e2009-09-05 21:47:34 +00009030
glennrpa521b2f2010-10-29 04:11:03 +00009031 mask=0xffff;
9032 if (ping_bit_depth == 8)
9033 mask=0x00ff;
glennrp0fe50b42010-11-16 03:52:51 +00009034
glennrpa521b2f2010-10-29 04:11:03 +00009035 if (ping_bit_depth == 4)
9036 mask=0x000f;
glennrp0fe50b42010-11-16 03:52:51 +00009037
glennrpa521b2f2010-10-29 04:11:03 +00009038 if (ping_bit_depth == 2)
9039 mask=0x0003;
glennrp0fe50b42010-11-16 03:52:51 +00009040
glennrpa521b2f2010-10-29 04:11:03 +00009041 if (ping_bit_depth == 1)
9042 mask=0x0001;
glennrp0fe50b42010-11-16 03:52:51 +00009043
glennrpa521b2f2010-10-29 04:11:03 +00009044 ping_background.red=(png_uint_16)
9045 (ScaleQuantumToShort(image->background_color.red) & mask);
glennrp0fe50b42010-11-16 03:52:51 +00009046
glennrpa521b2f2010-10-29 04:11:03 +00009047 ping_background.green=(png_uint_16)
9048 (ScaleQuantumToShort(image->background_color.green) & mask);
glennrp0fe50b42010-11-16 03:52:51 +00009049
glennrpa521b2f2010-10-29 04:11:03 +00009050 ping_background.blue=(png_uint_16)
9051 (ScaleQuantumToShort(image->background_color.blue) & mask);
glennrpc6c391a2011-04-27 02:23:56 +00009052
9053 ping_background.gray=(png_uint_16) ping_background.green;
glennrp0fe50b42010-11-16 03:52:51 +00009054 }
cristy3ed852e2009-09-05 21:47:34 +00009055
glennrp0fe50b42010-11-16 03:52:51 +00009056 if (logging != MagickFalse)
glennrp3b51f0e2010-11-27 18:14:08 +00009057 {
9058 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9059 " Setting up bKGD chunk (1)");
glennrpc6c391a2011-04-27 02:23:56 +00009060 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9061 " background_color index is %d",
9062 (int) ping_background.index);
glennrp3b51f0e2010-11-27 18:14:08 +00009063
9064 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9065 " ping_bit_depth=%d",ping_bit_depth);
9066 }
glennrp0fe50b42010-11-16 03:52:51 +00009067
9068 ping_have_bKGD = MagickTrue;
glennrp26f37912010-12-23 16:22:42 +00009069 }
glennrp0fe50b42010-11-16 03:52:51 +00009070
cristy3ed852e2009-09-05 21:47:34 +00009071 /*
9072 Select the color type.
9073 */
9074 matte=image_matte;
9075 old_bit_depth=0;
glennrp0fe50b42010-11-16 03:52:51 +00009076
glennrp1273f7b2011-02-24 03:20:30 +00009077 if (mng_info->IsPalette && mng_info->write_png8)
cristy3ed852e2009-09-05 21:47:34 +00009078 {
glennrp0fe50b42010-11-16 03:52:51 +00009079
glennrpfd05d622011-02-25 04:10:33 +00009080 /* To do: make this a function cause it's used twice, except
glennrp0fe50b42010-11-16 03:52:51 +00009081 for reducing the sample depth from 8. */
9082
glennrp0fe50b42010-11-16 03:52:51 +00009083 number_colors=image_colors;
glennrp8bb3a022010-12-13 20:40:04 +00009084
glennrp8bb3a022010-12-13 20:40:04 +00009085 ping_have_tRNS=MagickFalse;
glennrp0fe50b42010-11-16 03:52:51 +00009086
9087 /*
9088 Set image palette.
9089 */
9090 ping_color_type=(png_byte) PNG_COLOR_TYPE_PALETTE;
9091
glennrp0fe50b42010-11-16 03:52:51 +00009092 if (logging != MagickFalse)
9093 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9094 " Setting up PLTE chunk with %d colors (%d)",
glennrpf09bded2011-01-08 01:15:59 +00009095 number_colors, image_colors);
glennrp0fe50b42010-11-16 03:52:51 +00009096
9097 for (i=0; i < (ssize_t) number_colors; i++)
9098 {
9099 palette[i].red=ScaleQuantumToChar(image->colormap[i].red);
9100 palette[i].green=ScaleQuantumToChar(image->colormap[i].green);
9101 palette[i].blue=ScaleQuantumToChar(image->colormap[i].blue);
9102 if (logging != MagickFalse)
9103 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
glennrp67b9c1a2011-04-22 18:47:36 +00009104#if MAGICKCORE_QUANTUM_DEPTH == 8
glennrp0fe50b42010-11-16 03:52:51 +00009105 " %3ld (%3d,%3d,%3d)",
glennrp67b9c1a2011-04-22 18:47:36 +00009106#else
9107 " %5ld (%5d,%5d,%5d)",
9108#endif
glennrp0fe50b42010-11-16 03:52:51 +00009109 (long) i,palette[i].red,palette[i].green,palette[i].blue);
9110
9111 }
glennrp2b013e42010-11-24 16:55:50 +00009112
glennrp8bb3a022010-12-13 20:40:04 +00009113 ping_have_PLTE=MagickTrue;
9114 image_depth=ping_bit_depth;
9115 ping_num_trans=0;
glennrp2b013e42010-11-24 16:55:50 +00009116
glennrp58e01762011-01-07 15:28:54 +00009117 if (matte != MagickFalse)
glennrp8bb3a022010-12-13 20:40:04 +00009118 {
glennrp0fe50b42010-11-16 03:52:51 +00009119 /*
9120 Identify which colormap entry is transparent.
9121 */
9122 assert(number_colors <= 256);
glennrp8bb3a022010-12-13 20:40:04 +00009123 assert(image->colormap != NULL);
glennrp0fe50b42010-11-16 03:52:51 +00009124
glennrp8bb3a022010-12-13 20:40:04 +00009125 for (i=0; i < (ssize_t) number_transparent; i++)
9126 ping_trans_alpha[i]=0;
glennrp0fe50b42010-11-16 03:52:51 +00009127
glennrp0fe50b42010-11-16 03:52:51 +00009128
glennrp2cc891a2010-12-24 13:44:32 +00009129 ping_num_trans=(unsigned short) (number_transparent +
glennrp8bb3a022010-12-13 20:40:04 +00009130 number_semitransparent);
glennrp0fe50b42010-11-16 03:52:51 +00009131
9132 if (ping_num_trans == 0)
9133 ping_have_tRNS=MagickFalse;
9134
glennrp8bb3a022010-12-13 20:40:04 +00009135 else
9136 ping_have_tRNS=MagickTrue;
9137 }
glennrp0fe50b42010-11-16 03:52:51 +00009138
glennrp1273f7b2011-02-24 03:20:30 +00009139 if (ping_exclude_bKGD == MagickFalse)
glennrp4f25bd02011-01-01 18:51:28 +00009140 {
glennrp1273f7b2011-02-24 03:20:30 +00009141 /*
9142 * Identify which colormap entry is the background color.
9143 */
9144
glennrp4f25bd02011-01-01 18:51:28 +00009145 for (i=0; i < (ssize_t) MagickMax(1L*number_colors-1L,1L); i++)
9146 if (IsPNGColorEqual(ping_background,image->colormap[i]))
9147 break;
glennrp0fe50b42010-11-16 03:52:51 +00009148
glennrp4f25bd02011-01-01 18:51:28 +00009149 ping_background.index=(png_byte) i;
glennrpc6c391a2011-04-27 02:23:56 +00009150
9151 if (logging != MagickFalse)
9152 {
9153 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9154 " background_color index is %d",
9155 (int) ping_background.index);
9156 }
glennrp4f25bd02011-01-01 18:51:28 +00009157 }
cristy3ed852e2009-09-05 21:47:34 +00009158 } /* end of write_png8 */
glennrp0fe50b42010-11-16 03:52:51 +00009159
glennrp7e65e932011-08-19 02:31:16 +00009160 else if (mng_info->write_png24 || mng_info->write_png_colortype == 3)
cristy3ed852e2009-09-05 21:47:34 +00009161 {
9162 image_matte=MagickFalse;
glennrp5af765f2010-03-30 11:12:18 +00009163 ping_color_type=(png_byte) PNG_COLOR_TYPE_RGB;
cristy3ed852e2009-09-05 21:47:34 +00009164 }
glennrp0fe50b42010-11-16 03:52:51 +00009165
glennrp7e65e932011-08-19 02:31:16 +00009166 else if (mng_info->write_png32 || mng_info->write_png_colortype == 7)
cristy3ed852e2009-09-05 21:47:34 +00009167 {
9168 image_matte=MagickTrue;
glennrp5af765f2010-03-30 11:12:18 +00009169 ping_color_type=(png_byte) PNG_COLOR_TYPE_RGB_ALPHA;
cristy3ed852e2009-09-05 21:47:34 +00009170 }
glennrp0fe50b42010-11-16 03:52:51 +00009171
glennrp8bb3a022010-12-13 20:40:04 +00009172 else /* mng_info->write_pngNN not specified */
cristy3ed852e2009-09-05 21:47:34 +00009173 {
glennrp5af765f2010-03-30 11:12:18 +00009174 image_depth=ping_bit_depth;
glennrp0fe50b42010-11-16 03:52:51 +00009175
glennrp8bb3a022010-12-13 20:40:04 +00009176 if (mng_info->write_png_colortype != 0)
cristy3ed852e2009-09-05 21:47:34 +00009177 {
glennrp5af765f2010-03-30 11:12:18 +00009178 ping_color_type=(png_byte) mng_info->write_png_colortype-1;
glennrp0fe50b42010-11-16 03:52:51 +00009179
glennrp5af765f2010-03-30 11:12:18 +00009180 if (ping_color_type == PNG_COLOR_TYPE_GRAY_ALPHA ||
9181 ping_color_type == PNG_COLOR_TYPE_RGB_ALPHA)
cristy3ed852e2009-09-05 21:47:34 +00009182 image_matte=MagickTrue;
glennrp2cc891a2010-12-24 13:44:32 +00009183
glennrp8bb3a022010-12-13 20:40:04 +00009184 else
9185 image_matte=MagickFalse;
glennrp7c4c9e62011-03-21 20:23:32 +00009186
9187 if (logging != MagickFalse)
9188 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9189 " PNG colortype %d was specified:",(int) ping_color_type);
cristy3ed852e2009-09-05 21:47:34 +00009190 }
glennrp0fe50b42010-11-16 03:52:51 +00009191
glennrp7c4c9e62011-03-21 20:23:32 +00009192 else /* write_png_colortype not specified */
cristy3ed852e2009-09-05 21:47:34 +00009193 {
9194 if (logging != MagickFalse)
9195 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
glennrp3c218112010-11-27 15:31:26 +00009196 " Selecting PNG colortype:");
glennrp0fe50b42010-11-16 03:52:51 +00009197
glennrpd6bf1612010-12-17 17:28:54 +00009198 ping_color_type=(png_byte) ((matte != MagickFalse)?
glennrp8bb3a022010-12-13 20:40:04 +00009199 PNG_COLOR_TYPE_RGB_ALPHA:PNG_COLOR_TYPE_RGB);
glennrp0fe50b42010-11-16 03:52:51 +00009200
glennrpd6bf1612010-12-17 17:28:54 +00009201 if (image_info->type == TrueColorType)
cristy3ed852e2009-09-05 21:47:34 +00009202 {
glennrp5af765f2010-03-30 11:12:18 +00009203 ping_color_type=(png_byte) PNG_COLOR_TYPE_RGB;
cristy3ed852e2009-09-05 21:47:34 +00009204 image_matte=MagickFalse;
9205 }
glennrp0fe50b42010-11-16 03:52:51 +00009206
glennrpd6bf1612010-12-17 17:28:54 +00009207 if (image_info->type == TrueColorMatteType)
cristy3ed852e2009-09-05 21:47:34 +00009208 {
glennrp5af765f2010-03-30 11:12:18 +00009209 ping_color_type=(png_byte) PNG_COLOR_TYPE_RGB_ALPHA;
cristy3ed852e2009-09-05 21:47:34 +00009210 image_matte=MagickTrue;
9211 }
glennrp0fe50b42010-11-16 03:52:51 +00009212
glennrp5aa37f62011-01-02 03:07:57 +00009213 if (image_info->type == PaletteType ||
9214 image_info->type == PaletteMatteType)
9215 ping_color_type=(png_byte) PNG_COLOR_TYPE_PALETTE;
9216
glennrp7c4c9e62011-03-21 20:23:32 +00009217 if (mng_info->write_png_colortype == 0 &&
9218 (image_info->type == UndefinedType ||
9219 image_info->type == OptimizeType))
cristy3ed852e2009-09-05 21:47:34 +00009220 {
glennrp5aa37f62011-01-02 03:07:57 +00009221 if (ping_have_color == MagickFalse)
glennrp8bb3a022010-12-13 20:40:04 +00009222 {
glennrp5aa37f62011-01-02 03:07:57 +00009223 if (image_matte == MagickFalse)
9224 {
9225 ping_color_type=(png_byte) PNG_COLOR_TYPE_GRAY;
9226 image_matte=MagickFalse;
9227 }
glennrp0fe50b42010-11-16 03:52:51 +00009228
glennrp0b206f52011-01-07 04:55:32 +00009229 else
glennrp5aa37f62011-01-02 03:07:57 +00009230 {
9231 ping_color_type=(png_byte) PNG_COLOR_TYPE_GRAY_ALPHA;
9232 image_matte=MagickTrue;
9233 }
9234 }
9235 else
glennrp8bb3a022010-12-13 20:40:04 +00009236 {
glennrp5aa37f62011-01-02 03:07:57 +00009237 if (image_matte == MagickFalse)
9238 {
9239 ping_color_type=(png_byte) PNG_COLOR_TYPE_RGB;
9240 image_matte=MagickFalse;
9241 }
glennrp8bb3a022010-12-13 20:40:04 +00009242
glennrp0b206f52011-01-07 04:55:32 +00009243 else
glennrp5aa37f62011-01-02 03:07:57 +00009244 {
9245 ping_color_type=(png_byte) PNG_COLOR_TYPE_RGBA;
9246 image_matte=MagickTrue;
9247 }
9248 }
glennrp0fe50b42010-11-16 03:52:51 +00009249 }
glennrp5aa37f62011-01-02 03:07:57 +00009250
cristy3ed852e2009-09-05 21:47:34 +00009251 }
glennrp0fe50b42010-11-16 03:52:51 +00009252
cristy3ed852e2009-09-05 21:47:34 +00009253 if (logging != MagickFalse)
9254 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
glennrp8640fb52010-11-23 15:48:26 +00009255 " Selected PNG colortype=%d",ping_color_type);
glennrp26c990a2010-11-23 02:23:20 +00009256
glennrp5af765f2010-03-30 11:12:18 +00009257 if (ping_bit_depth < 8)
glennrp0fe50b42010-11-16 03:52:51 +00009258 {
9259 if (ping_color_type == PNG_COLOR_TYPE_GRAY_ALPHA ||
9260 ping_color_type == PNG_COLOR_TYPE_RGB ||
9261 ping_color_type == PNG_COLOR_TYPE_RGB_ALPHA)
9262 ping_bit_depth=8;
9263 }
cristy3ed852e2009-09-05 21:47:34 +00009264
glennrpd6bf1612010-12-17 17:28:54 +00009265 old_bit_depth=ping_bit_depth;
9266
glennrp5af765f2010-03-30 11:12:18 +00009267 if (ping_color_type == PNG_COLOR_TYPE_GRAY)
cristy3ed852e2009-09-05 21:47:34 +00009268 {
glennrp8d579662011-02-23 02:05:02 +00009269 if (image->matte == MagickFalse && ping_have_non_bw == MagickFalse)
9270 ping_bit_depth=1;
cristy3ed852e2009-09-05 21:47:34 +00009271 }
glennrp8640fb52010-11-23 15:48:26 +00009272
glennrp5af765f2010-03-30 11:12:18 +00009273 if (ping_color_type == PNG_COLOR_TYPE_PALETTE)
cristy3ed852e2009-09-05 21:47:34 +00009274 {
cristy35ef8242010-06-03 16:24:13 +00009275 size_t one = 1;
glennrp5af765f2010-03-30 11:12:18 +00009276 ping_bit_depth=1;
glennrp0f111982010-07-07 20:18:33 +00009277
9278 if (image->colors == 0)
9279 {
glennrp0fe50b42010-11-16 03:52:51 +00009280 /* DO SOMETHING */
cristyc82a27b2011-10-21 01:07:16 +00009281 (void) ThrowMagickException(exception,
glennrp0f111982010-07-07 20:18:33 +00009282 GetMagickModule(),CoderError,
glennrpc70af4a2011-03-07 00:08:23 +00009283 "image has 0 colors", "`%s'","");
glennrp0f111982010-07-07 20:18:33 +00009284 }
9285
cristy35ef8242010-06-03 16:24:13 +00009286 while ((int) (one << ping_bit_depth) < (ssize_t) image_colors)
glennrp5af765f2010-03-30 11:12:18 +00009287 ping_bit_depth <<= 1;
cristy3ed852e2009-09-05 21:47:34 +00009288 }
glennrp2b013e42010-11-24 16:55:50 +00009289
glennrpd6bf1612010-12-17 17:28:54 +00009290 if (logging != MagickFalse)
9291 {
9292 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9293 " Number of colors: %.20g",(double) image_colors);
9294
9295 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9296 " Tentative PNG bit depth: %d",ping_bit_depth);
9297 }
9298
9299 if (ping_bit_depth < (int) mng_info->write_png_depth)
9300 ping_bit_depth = mng_info->write_png_depth;
9301 }
glennrp2cc891a2010-12-24 13:44:32 +00009302
glennrp5af765f2010-03-30 11:12:18 +00009303 image_depth=ping_bit_depth;
glennrp2b013e42010-11-24 16:55:50 +00009304
cristy3ed852e2009-09-05 21:47:34 +00009305 if (logging != MagickFalse)
9306 {
9307 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00009308 " Tentative PNG color type: %.20g",(double) ping_color_type);
glennrp0fe50b42010-11-16 03:52:51 +00009309
cristy3ed852e2009-09-05 21:47:34 +00009310 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00009311 " image_info->type: %.20g",(double) image_info->type);
glennrp0fe50b42010-11-16 03:52:51 +00009312
cristy3ed852e2009-09-05 21:47:34 +00009313 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00009314 " image_depth: %.20g",(double) image_depth);
glennrp0fe50b42010-11-16 03:52:51 +00009315
cristy3ed852e2009-09-05 21:47:34 +00009316 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
glennrp3c218112010-11-27 15:31:26 +00009317
glennrp8640fb52010-11-23 15:48:26 +00009318 " image->depth: %.20g",(double) image->depth);
9319
9320 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +00009321 " ping_bit_depth: %.20g",(double) ping_bit_depth);
cristy3ed852e2009-09-05 21:47:34 +00009322 }
9323
glennrp58e01762011-01-07 15:28:54 +00009324 if (matte != MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00009325 {
glennrp4f25bd02011-01-01 18:51:28 +00009326 if (mng_info->IsPalette)
cristy3ed852e2009-09-05 21:47:34 +00009327 {
glennrp7c4c9e62011-03-21 20:23:32 +00009328 if (mng_info->write_png_colortype == 0)
9329 {
9330 ping_color_type=PNG_COLOR_TYPE_GRAY_ALPHA;
glennrp4f25bd02011-01-01 18:51:28 +00009331
glennrp7c4c9e62011-03-21 20:23:32 +00009332 if (ping_have_color != MagickFalse)
9333 ping_color_type=PNG_COLOR_TYPE_RGBA;
9334 }
glennrp4f25bd02011-01-01 18:51:28 +00009335
9336 /*
9337 * Determine if there is any transparent color.
9338 */
9339 if (number_transparent + number_semitransparent == 0)
9340 {
9341 /*
9342 No transparent pixels are present. Change 4 or 6 to 0 or 2.
9343 */
glennrpa6a06632011-01-19 15:15:34 +00009344
glennrp4f25bd02011-01-01 18:51:28 +00009345 image_matte=MagickFalse;
glennrp7c4c9e62011-03-21 20:23:32 +00009346
9347 if (mng_info->write_png_colortype == 0)
9348 ping_color_type&=0x03;
glennrp4f25bd02011-01-01 18:51:28 +00009349 }
9350
9351 else
9352 {
9353 unsigned int
glennrpbb4f99d2011-05-22 11:13:17 +00009354 mask;
glennrp4f25bd02011-01-01 18:51:28 +00009355
9356 mask=0xffff;
9357
9358 if (ping_bit_depth == 8)
9359 mask=0x00ff;
9360
9361 if (ping_bit_depth == 4)
9362 mask=0x000f;
9363
9364 if (ping_bit_depth == 2)
9365 mask=0x0003;
9366
9367 if (ping_bit_depth == 1)
9368 mask=0x0001;
9369
9370 ping_trans_color.red=(png_uint_16)
9371 (ScaleQuantumToShort(image->colormap[0].red) & mask);
9372
9373 ping_trans_color.green=(png_uint_16)
9374 (ScaleQuantumToShort(image->colormap[0].green) & mask);
9375
9376 ping_trans_color.blue=(png_uint_16)
9377 (ScaleQuantumToShort(image->colormap[0].blue) & mask);
9378
9379 ping_trans_color.gray=(png_uint_16)
cristy101ab702011-10-13 13:06:32 +00009380 (ScaleQuantumToShort(GetPixelInfoIntensity(
glennrp4f25bd02011-01-01 18:51:28 +00009381 image->colormap)) & mask);
9382
9383 ping_trans_color.index=(png_byte) 0;
9384
9385 ping_have_tRNS=MagickTrue;
9386 }
9387
9388 if (ping_have_tRNS != MagickFalse)
9389 {
9390 /*
glennrpfd05d622011-02-25 04:10:33 +00009391 * Determine if there is one and only one transparent color
9392 * and if so if it is fully transparent.
9393 */
9394 if (ping_have_cheap_transparency == MagickFalse)
9395 ping_have_tRNS=MagickFalse;
glennrp4f25bd02011-01-01 18:51:28 +00009396 }
9397
9398 if (ping_have_tRNS != MagickFalse)
9399 {
glennrp7c4c9e62011-03-21 20:23:32 +00009400 if (mng_info->write_png_colortype == 0)
9401 ping_color_type &= 0x03; /* changes 4 or 6 to 0 or 2 */
glennrp4f25bd02011-01-01 18:51:28 +00009402
9403 if (image_depth == 8)
9404 {
9405 ping_trans_color.red&=0xff;
9406 ping_trans_color.green&=0xff;
9407 ping_trans_color.blue&=0xff;
9408 ping_trans_color.gray&=0xff;
9409 }
9410 }
9411 }
cristy3ed852e2009-09-05 21:47:34 +00009412 else
9413 {
cristy3ed852e2009-09-05 21:47:34 +00009414 if (image_depth == 8)
9415 {
glennrp5af765f2010-03-30 11:12:18 +00009416 ping_trans_color.red&=0xff;
9417 ping_trans_color.green&=0xff;
9418 ping_trans_color.blue&=0xff;
9419 ping_trans_color.gray&=0xff;
cristy3ed852e2009-09-05 21:47:34 +00009420 }
9421 }
9422 }
glennrp8640fb52010-11-23 15:48:26 +00009423
cristy3ed852e2009-09-05 21:47:34 +00009424 matte=image_matte;
glennrp0fe50b42010-11-16 03:52:51 +00009425
glennrp2e09f552010-11-14 00:38:48 +00009426 if (ping_have_tRNS != MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00009427 image_matte=MagickFalse;
glennrp0fe50b42010-11-16 03:52:51 +00009428
glennrp39992b42010-11-14 00:03:43 +00009429 if ((mng_info->IsPalette) &&
cristy3ed852e2009-09-05 21:47:34 +00009430 mng_info->write_png_colortype-1 != PNG_COLOR_TYPE_PALETTE &&
glennrp8d579662011-02-23 02:05:02 +00009431 ping_have_color == MagickFalse &&
9432 (image_matte == MagickFalse || image_depth >= 8))
cristy3ed852e2009-09-05 21:47:34 +00009433 {
cristy35ef8242010-06-03 16:24:13 +00009434 size_t one=1;
glennrp0fe50b42010-11-16 03:52:51 +00009435
cristy3ed852e2009-09-05 21:47:34 +00009436 if (image_matte != MagickFalse)
glennrp9c1eb072010-06-06 22:19:15 +00009437 ping_color_type=PNG_COLOR_TYPE_GRAY_ALPHA;
glennrp0fe50b42010-11-16 03:52:51 +00009438
glennrp7c4c9e62011-03-21 20:23:32 +00009439 else if (mng_info->write_png_colortype-1 != PNG_COLOR_TYPE_GRAY_ALPHA)
cristy3ed852e2009-09-05 21:47:34 +00009440 {
glennrp5af765f2010-03-30 11:12:18 +00009441 ping_color_type=PNG_COLOR_TYPE_GRAY;
glennrp4f25bd02011-01-01 18:51:28 +00009442
cristy3ed852e2009-09-05 21:47:34 +00009443 if (save_image_depth == 16 && image_depth == 8)
glennrp4f25bd02011-01-01 18:51:28 +00009444 {
9445 if (logging != MagickFalse)
9446 {
9447 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9448 " Scaling ping_trans_color (0)");
9449 }
9450 ping_trans_color.gray*=0x0101;
9451 }
cristy3ed852e2009-09-05 21:47:34 +00009452 }
glennrp0fe50b42010-11-16 03:52:51 +00009453
cristy3ed852e2009-09-05 21:47:34 +00009454 if (image_depth > MAGICKCORE_QUANTUM_DEPTH)
9455 image_depth=MAGICKCORE_QUANTUM_DEPTH;
glennrp0fe50b42010-11-16 03:52:51 +00009456
glennrp136ee3a2011-04-27 15:47:45 +00009457 if ((image_colors == 0) ||
glennrpd17915c2011-04-29 14:24:22 +00009458 ((ssize_t) (image_colors-1) > (ssize_t) MaxColormapSize))
glennrpf09bded2011-01-08 01:15:59 +00009459 image_colors=(int) (one << image_depth);
glennrp0fe50b42010-11-16 03:52:51 +00009460
cristy3ed852e2009-09-05 21:47:34 +00009461 if (image_depth > 8)
glennrp5af765f2010-03-30 11:12:18 +00009462 ping_bit_depth=16;
glennrp0fe50b42010-11-16 03:52:51 +00009463
cristy3ed852e2009-09-05 21:47:34 +00009464 else
9465 {
glennrp5af765f2010-03-30 11:12:18 +00009466 ping_bit_depth=8;
9467 if ((int) ping_color_type == PNG_COLOR_TYPE_PALETTE)
cristy3ed852e2009-09-05 21:47:34 +00009468 {
9469 if(!mng_info->write_png_depth)
9470 {
glennrp5af765f2010-03-30 11:12:18 +00009471 ping_bit_depth=1;
glennrp0fe50b42010-11-16 03:52:51 +00009472
cristy35ef8242010-06-03 16:24:13 +00009473 while ((int) (one << ping_bit_depth)
cristybb503372010-05-27 20:51:26 +00009474 < (ssize_t) image_colors)
glennrp5af765f2010-03-30 11:12:18 +00009475 ping_bit_depth <<= 1;
cristy3ed852e2009-09-05 21:47:34 +00009476 }
9477 }
glennrp2b013e42010-11-24 16:55:50 +00009478
glennrp0fe50b42010-11-16 03:52:51 +00009479 else if (ping_color_type ==
9480 PNG_COLOR_TYPE_GRAY && image_colors < 17 &&
cristy3ed852e2009-09-05 21:47:34 +00009481 mng_info->IsPalette)
9482 {
cristy3ed852e2009-09-05 21:47:34 +00009483 /* Check if grayscale is reducible */
glennrp1a0aaa62011-03-07 17:40:17 +00009484
cristy3ed852e2009-09-05 21:47:34 +00009485 int
9486 depth_4_ok=MagickTrue,
9487 depth_2_ok=MagickTrue,
9488 depth_1_ok=MagickTrue;
9489
cristybb503372010-05-27 20:51:26 +00009490 for (i=0; i < (ssize_t) image_colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00009491 {
9492 unsigned char
9493 intensity;
9494
9495 intensity=ScaleQuantumToChar(image->colormap[i].red);
9496
9497 if ((intensity & 0x0f) != ((intensity & 0xf0) >> 4))
9498 depth_4_ok=depth_2_ok=depth_1_ok=MagickFalse;
9499 else if ((intensity & 0x03) != ((intensity & 0x0c) >> 2))
9500 depth_2_ok=depth_1_ok=MagickFalse;
glennrp4bf89732011-03-21 13:48:28 +00009501 else if ((intensity & 0x01) != ((intensity & 0x02) >> 1))
cristy3ed852e2009-09-05 21:47:34 +00009502 depth_1_ok=MagickFalse;
9503 }
glennrp2b013e42010-11-24 16:55:50 +00009504
cristy3ed852e2009-09-05 21:47:34 +00009505 if (depth_1_ok && mng_info->write_png_depth <= 1)
glennrp9c1eb072010-06-06 22:19:15 +00009506 ping_bit_depth=1;
glennrp0fe50b42010-11-16 03:52:51 +00009507
cristy3ed852e2009-09-05 21:47:34 +00009508 else if (depth_2_ok && mng_info->write_png_depth <= 2)
glennrp9c1eb072010-06-06 22:19:15 +00009509 ping_bit_depth=2;
glennrp0fe50b42010-11-16 03:52:51 +00009510
cristy3ed852e2009-09-05 21:47:34 +00009511 else if (depth_4_ok && mng_info->write_png_depth <= 4)
glennrp9c1eb072010-06-06 22:19:15 +00009512 ping_bit_depth=4;
cristy3ed852e2009-09-05 21:47:34 +00009513 }
9514 }
glennrp2b013e42010-11-24 16:55:50 +00009515
glennrp5af765f2010-03-30 11:12:18 +00009516 image_depth=ping_bit_depth;
cristy3ed852e2009-09-05 21:47:34 +00009517 }
glennrp0fe50b42010-11-16 03:52:51 +00009518
cristy3ed852e2009-09-05 21:47:34 +00009519 else
glennrp0fe50b42010-11-16 03:52:51 +00009520
cristy3ed852e2009-09-05 21:47:34 +00009521 if (mng_info->IsPalette)
9522 {
glennrp17a14852010-05-10 03:01:59 +00009523 number_colors=image_colors;
9524
cristy3ed852e2009-09-05 21:47:34 +00009525 if (image_depth <= 8)
9526 {
cristy3ed852e2009-09-05 21:47:34 +00009527 /*
9528 Set image palette.
9529 */
glennrp5af765f2010-03-30 11:12:18 +00009530 ping_color_type=(png_byte) PNG_COLOR_TYPE_PALETTE;
glennrp0fe50b42010-11-16 03:52:51 +00009531
glennrp58e01762011-01-07 15:28:54 +00009532 if (mng_info->have_write_global_plte && matte == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00009533 {
glennrp9c1eb072010-06-06 22:19:15 +00009534 png_set_PLTE(ping,ping_info,NULL,0);
glennrp0fe50b42010-11-16 03:52:51 +00009535
glennrp3b51f0e2010-11-27 18:14:08 +00009536 if (logging != MagickFalse)
glennrp9c1eb072010-06-06 22:19:15 +00009537 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9538 " Setting up empty PLTE chunk");
cristy3ed852e2009-09-05 21:47:34 +00009539 }
glennrp0fe50b42010-11-16 03:52:51 +00009540
cristy3ed852e2009-09-05 21:47:34 +00009541 else
9542 {
cristybb503372010-05-27 20:51:26 +00009543 for (i=0; i < (ssize_t) number_colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00009544 {
9545 palette[i].red=ScaleQuantumToChar(image->colormap[i].red);
9546 palette[i].green=ScaleQuantumToChar(image->colormap[i].green);
9547 palette[i].blue=ScaleQuantumToChar(image->colormap[i].blue);
9548 }
glennrp0fe50b42010-11-16 03:52:51 +00009549
glennrp3b51f0e2010-11-27 18:14:08 +00009550 if (logging != MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00009551 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
glennrp98156a32009-12-09 15:32:44 +00009552 " Setting up PLTE chunk with %d colors",
glennrpf09bded2011-01-08 01:15:59 +00009553 number_colors);
glennrp0fe50b42010-11-16 03:52:51 +00009554
glennrp39992b42010-11-14 00:03:43 +00009555 ping_have_PLTE=MagickTrue;
cristy3ed852e2009-09-05 21:47:34 +00009556 }
glennrp0fe50b42010-11-16 03:52:51 +00009557
cristy3ed852e2009-09-05 21:47:34 +00009558 /* color_type is PNG_COLOR_TYPE_PALETTE */
glennrpd6bf1612010-12-17 17:28:54 +00009559 if (mng_info->write_png_depth == 0)
cristy3ed852e2009-09-05 21:47:34 +00009560 {
cristybefe4d22010-06-07 01:18:58 +00009561 size_t
9562 one;
9563
glennrp5af765f2010-03-30 11:12:18 +00009564 ping_bit_depth=1;
cristybefe4d22010-06-07 01:18:58 +00009565 one=1;
glennrp0fe50b42010-11-16 03:52:51 +00009566
cristy94b11832011-09-08 19:46:03 +00009567 while ((one << ping_bit_depth) < (size_t) number_colors)
glennrp5af765f2010-03-30 11:12:18 +00009568 ping_bit_depth <<= 1;
cristy3ed852e2009-09-05 21:47:34 +00009569 }
glennrp0fe50b42010-11-16 03:52:51 +00009570
glennrp5af765f2010-03-30 11:12:18 +00009571 ping_num_trans=0;
glennrp0fe50b42010-11-16 03:52:51 +00009572
glennrp58e01762011-01-07 15:28:54 +00009573 if (matte != MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00009574 {
glennrp0fe50b42010-11-16 03:52:51 +00009575 /*
glennrpd6bf1612010-12-17 17:28:54 +00009576 * Set up trans_colors array.
9577 */
glennrp0fe50b42010-11-16 03:52:51 +00009578 assert(number_colors <= 256);
9579
glennrpd6bf1612010-12-17 17:28:54 +00009580 ping_num_trans=(unsigned short) (number_transparent +
9581 number_semitransparent);
glennrp0fe50b42010-11-16 03:52:51 +00009582
9583 if (ping_num_trans == 0)
9584 ping_have_tRNS=MagickFalse;
9585
glennrpd6bf1612010-12-17 17:28:54 +00009586 else
glennrp0fe50b42010-11-16 03:52:51 +00009587 {
glennrpc8cbc5d2011-01-01 00:12:34 +00009588 if (logging != MagickFalse)
9589 {
9590 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9591 " Scaling ping_trans_color (1)");
9592 }
glennrpd6bf1612010-12-17 17:28:54 +00009593 ping_have_tRNS=MagickTrue;
9594
9595 for (i=0; i < ping_num_trans; i++)
9596 {
cristy4c08aed2011-07-01 19:47:50 +00009597 ping_trans_alpha[i]= (png_byte)
9598 ScaleQuantumToChar(image->colormap[i].alpha);
glennrpd6bf1612010-12-17 17:28:54 +00009599 }
glennrp0fe50b42010-11-16 03:52:51 +00009600 }
9601 }
cristy3ed852e2009-09-05 21:47:34 +00009602 }
9603 }
glennrp0fe50b42010-11-16 03:52:51 +00009604
cristy3ed852e2009-09-05 21:47:34 +00009605 else
9606 {
glennrpc8cbc5d2011-01-01 00:12:34 +00009607
cristy3ed852e2009-09-05 21:47:34 +00009608 if (image_depth < 8)
9609 image_depth=8;
glennrp0fe50b42010-11-16 03:52:51 +00009610
cristy3ed852e2009-09-05 21:47:34 +00009611 if ((save_image_depth == 16) && (image_depth == 8))
9612 {
glennrp4f25bd02011-01-01 18:51:28 +00009613 if (logging != MagickFalse)
9614 {
9615 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9616 " Scaling ping_trans_color from (%d,%d,%d)",
9617 (int) ping_trans_color.red,
9618 (int) ping_trans_color.green,
9619 (int) ping_trans_color.blue);
9620 }
9621
glennrp5af765f2010-03-30 11:12:18 +00009622 ping_trans_color.red*=0x0101;
9623 ping_trans_color.green*=0x0101;
9624 ping_trans_color.blue*=0x0101;
9625 ping_trans_color.gray*=0x0101;
glennrp4f25bd02011-01-01 18:51:28 +00009626
9627 if (logging != MagickFalse)
9628 {
9629 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9630 " to (%d,%d,%d)",
9631 (int) ping_trans_color.red,
9632 (int) ping_trans_color.green,
9633 (int) ping_trans_color.blue);
9634 }
cristy3ed852e2009-09-05 21:47:34 +00009635 }
9636 }
9637
cristy4383ec82011-01-05 15:42:32 +00009638 if (ping_bit_depth < (ssize_t) mng_info->write_png_depth)
9639 ping_bit_depth = (ssize_t) mng_info->write_png_depth;
glennrp2cc891a2010-12-24 13:44:32 +00009640
cristy3ed852e2009-09-05 21:47:34 +00009641 /*
9642 Adjust background and transparency samples in sub-8-bit grayscale files.
9643 */
glennrp5af765f2010-03-30 11:12:18 +00009644 if (ping_bit_depth < 8 && ping_color_type ==
cristy3ed852e2009-09-05 21:47:34 +00009645 PNG_COLOR_TYPE_GRAY)
9646 {
9647 png_uint_16
9648 maxval;
9649
cristy35ef8242010-06-03 16:24:13 +00009650 size_t
9651 one=1;
9652
cristy22ffd972010-06-03 16:51:47 +00009653 maxval=(png_uint_16) ((one << ping_bit_depth)-1);
cristy3ed852e2009-09-05 21:47:34 +00009654
glennrp4f25bd02011-01-01 18:51:28 +00009655 if (ping_exclude_bKGD == MagickFalse)
glennrp26f37912010-12-23 16:22:42 +00009656 {
cristy3ed852e2009-09-05 21:47:34 +00009657
glennrp9f0fa852011-12-15 12:20:50 +00009658 ping_background.gray=(png_uint_16) ((maxval/65535.)*
9659 (ScaleQuantumToShort(((GetPixelInfoIntensity(
9660 &image->background_color))) +.5)));
cristy3ed852e2009-09-05 21:47:34 +00009661
9662 if (logging != MagickFalse)
9663 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
glennrp3c218112010-11-27 15:31:26 +00009664 " Setting up bKGD chunk (2)");
glennrpc6c391a2011-04-27 02:23:56 +00009665 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9666 " background_color index is %d",
9667 (int) ping_background.index);
glennrp3b51f0e2010-11-27 18:14:08 +00009668
glennrp991d11d2010-11-12 21:55:28 +00009669 ping_have_bKGD = MagickTrue;
glennrp26f37912010-12-23 16:22:42 +00009670 }
cristy3ed852e2009-09-05 21:47:34 +00009671
glennrp3e3e20f2011-06-09 04:21:43 +00009672 if (logging != MagickFalse)
9673 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9674 " Scaling ping_trans_color.gray from %d",
9675 (int)ping_trans_color.gray);
9676
glennrp9be9b1c2011-06-09 12:21:45 +00009677 ping_trans_color.gray=(png_uint_16) ((maxval/255.)*(
glennrp3e3e20f2011-06-09 04:21:43 +00009678 ping_trans_color.gray)+.5);
9679
9680 if (logging != MagickFalse)
9681 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9682 " to %d", (int)ping_trans_color.gray);
cristy3ed852e2009-09-05 21:47:34 +00009683 }
glennrp17a14852010-05-10 03:01:59 +00009684
glennrp26f37912010-12-23 16:22:42 +00009685 if (ping_exclude_bKGD == MagickFalse)
9686 {
glennrp1273f7b2011-02-24 03:20:30 +00009687 if (mng_info->IsPalette && (int) ping_color_type == PNG_COLOR_TYPE_PALETTE)
glennrp17a14852010-05-10 03:01:59 +00009688 {
9689 /*
9690 Identify which colormap entry is the background color.
9691 */
9692
glennrp17a14852010-05-10 03:01:59 +00009693 number_colors=image_colors;
9694
glennrpa521b2f2010-10-29 04:11:03 +00009695 for (i=0; i < (ssize_t) MagickMax(1L*number_colors,1L); i++)
9696 if (IsPNGColorEqual(image->background_color,image->colormap[i]))
glennrp17a14852010-05-10 03:01:59 +00009697 break;
9698
9699 ping_background.index=(png_byte) i;
9700
glennrp3b51f0e2010-11-27 18:14:08 +00009701 if (logging != MagickFalse)
glennrpa521b2f2010-10-29 04:11:03 +00009702 {
9703 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
glennrp0fe50b42010-11-16 03:52:51 +00009704 " Setting up bKGD chunk with index=%d",(int) i);
glennrpa521b2f2010-10-29 04:11:03 +00009705 }
glennrp0fe50b42010-11-16 03:52:51 +00009706
cristy13d07042010-11-21 20:56:18 +00009707 if (i < (ssize_t) number_colors)
glennrp0fe50b42010-11-16 03:52:51 +00009708 {
9709 ping_have_bKGD = MagickTrue;
glennrp3b51f0e2010-11-27 18:14:08 +00009710
9711 if (logging != MagickFalse)
glennrp0fe50b42010-11-16 03:52:51 +00009712 {
9713 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9714 " background =(%d,%d,%d)",
9715 (int) ping_background.red,
9716 (int) ping_background.green,
9717 (int) ping_background.blue);
9718 }
9719 }
glennrpa521b2f2010-10-29 04:11:03 +00009720
glennrpd6bf1612010-12-17 17:28:54 +00009721 else /* Can't happen */
glennrp3c218112010-11-27 15:31:26 +00009722 {
glennrp3b51f0e2010-11-27 18:14:08 +00009723 if (logging != MagickFalse)
9724 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9725 " No room in PLTE to add bKGD color");
glennrp3c218112010-11-27 15:31:26 +00009726 ping_have_bKGD = MagickFalse;
9727 }
glennrp17a14852010-05-10 03:01:59 +00009728 }
glennrp26f37912010-12-23 16:22:42 +00009729 }
glennrp17a14852010-05-10 03:01:59 +00009730
cristy3ed852e2009-09-05 21:47:34 +00009731 if (logging != MagickFalse)
9732 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
glennrp5af765f2010-03-30 11:12:18 +00009733 " PNG color type: %d",ping_color_type);
cristy3ed852e2009-09-05 21:47:34 +00009734 /*
9735 Initialize compression level and filtering.
9736 */
9737 if (logging != MagickFalse)
glennrp0fe50b42010-11-16 03:52:51 +00009738 {
9739 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9740 " Setting up deflate compression");
9741
9742 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9743 " Compression buffer size: 32768");
9744 }
9745
cristy3ed852e2009-09-05 21:47:34 +00009746 png_set_compression_buffer_size(ping,32768L);
glennrp0fe50b42010-11-16 03:52:51 +00009747
cristy3ed852e2009-09-05 21:47:34 +00009748 if (logging != MagickFalse)
9749 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9750 " Compression mem level: 9");
glennrp0fe50b42010-11-16 03:52:51 +00009751
cristy4054bfb2011-08-29 23:41:39 +00009752 png_set_compression_mem_level(ping, 9);
9753
glennrp10d739e2011-06-29 18:00:52 +00009754 /* Untangle the "-quality" setting:
9755
9756 Undefined is 0; the default is used.
9757 Default is 75
9758
9759 10's digit:
9760
9761 0: Use Z_HUFFMAN_ONLY strategy with the
9762 zlib default compression level
9763
9764 1-9: the zlib compression level
9765
9766 1's digit:
9767
9768 0-4: the PNG filter method
9769
9770 5: libpng adaptive filtering if compression level > 5
9771 libpng filter type "none" if compression level <= 5
9772 or if image is grayscale or palette
9773
9774 6: libpng adaptive filtering
9775
9776 7: "LOCO" filtering (intrapixel differing) if writing
9777 a MNG, othewise "none". Did not work in IM-6.7.0-9
9778 and earlier because of a missing "else".
9779
9780 8: Z_RLE strategy, all filters
glennrp18682582011-06-30 18:11:47 +00009781 Unused prior to IM-6.7.0-10, was same as 6
glennrp10d739e2011-06-29 18:00:52 +00009782
9783 9: Z_RLE strategy, no PNG filters
glennrp18682582011-06-30 18:11:47 +00009784 Unused prior to IM-6.7.0-10, was same as 6
glennrp10d739e2011-06-29 18:00:52 +00009785
9786 Note that using the -quality option, not all combinations of
9787 PNG filter type, zlib compression level, and zlib compression
cristy5e6be1e2011-07-16 01:23:39 +00009788 strategy are possible. This will be addressed soon in a
cristy5d6fc9c2011-12-27 03:10:42 +00009789 release that accomodates "-define png:compression-strategy", etc.
glennrp10d739e2011-06-29 18:00:52 +00009790
9791 */
9792
cristy3ed852e2009-09-05 21:47:34 +00009793 quality=image->quality == UndefinedCompressionQuality ? 75UL :
9794 image->quality;
glennrp0fe50b42010-11-16 03:52:51 +00009795
glennrp18682582011-06-30 18:11:47 +00009796 if (quality <= 9)
9797 {
9798 if (mng_info->write_png_compression_strategy == 0)
9799 mng_info->write_png_compression_strategy = Z_HUFFMAN_ONLY+1;
9800 }
9801
9802 else if (mng_info->write_png_compression_level == 0)
cristy3ed852e2009-09-05 21:47:34 +00009803 {
9804 int
9805 level;
9806
cristybb503372010-05-27 20:51:26 +00009807 level=(int) MagickMin((ssize_t) quality/10,9);
glennrp0fe50b42010-11-16 03:52:51 +00009808
glennrp18682582011-06-30 18:11:47 +00009809 mng_info->write_png_compression_level = level+1;
cristy3ed852e2009-09-05 21:47:34 +00009810 }
glennrp0fe50b42010-11-16 03:52:51 +00009811
glennrp18682582011-06-30 18:11:47 +00009812 if (mng_info->write_png_compression_strategy == 0)
cristy3ed852e2009-09-05 21:47:34 +00009813 {
glennrp18682582011-06-30 18:11:47 +00009814 if ((quality %10) == 8 || (quality %10) == 9)
9815 mng_info->write_png_compression_strategy=Z_RLE;
cristy3ed852e2009-09-05 21:47:34 +00009816 }
glennrp0fe50b42010-11-16 03:52:51 +00009817
glennrp18682582011-06-30 18:11:47 +00009818 if (mng_info->write_png_compression_filter == 0)
9819 mng_info->write_png_compression_filter=((int) quality % 10) + 1;
9820
cristy3ed852e2009-09-05 21:47:34 +00009821 if (logging != MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00009822 {
glennrp18682582011-06-30 18:11:47 +00009823 if (mng_info->write_png_compression_level)
9824 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9825 " Compression level: %d",
9826 (int) mng_info->write_png_compression_level-1);
glennrp0fe50b42010-11-16 03:52:51 +00009827
glennrp18682582011-06-30 18:11:47 +00009828 if (mng_info->write_png_compression_strategy)
9829 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9830 " Compression strategy: %d",
9831 (int) mng_info->write_png_compression_strategy-1);
glennrp0fe50b42010-11-16 03:52:51 +00009832
glennrp18682582011-06-30 18:11:47 +00009833 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9834 " Setting up filtering");
cristy3ed852e2009-09-05 21:47:34 +00009835
cristy4054bfb2011-08-29 23:41:39 +00009836 if (mng_info->write_png_compression_filter == 6)
cristy3ed852e2009-09-05 21:47:34 +00009837 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9838 " Base filter method: ADAPTIVE");
cristy4054bfb2011-08-29 23:41:39 +00009839 else if (mng_info->write_png_compression_filter == 0 ||
9840 mng_info->write_png_compression_filter == 1)
cristy3ed852e2009-09-05 21:47:34 +00009841 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9842 " Base filter method: NONE");
glennrp18682582011-06-30 18:11:47 +00009843 else
9844 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9845 " Base filter method: %d",
9846 (int) mng_info->write_png_compression_filter-1);
9847 }
glennrp2b013e42010-11-24 16:55:50 +00009848
glennrp18682582011-06-30 18:11:47 +00009849 if (mng_info->write_png_compression_level != 0)
9850 png_set_compression_level(ping,mng_info->write_png_compression_level-1);
9851
9852 if (mng_info->write_png_compression_filter == 6)
9853 {
9854 if (((int) ping_color_type == PNG_COLOR_TYPE_GRAY) ||
9855 ((int) ping_color_type == PNG_COLOR_TYPE_PALETTE) ||
9856 (quality < 50))
9857 png_set_filter(ping,PNG_FILTER_TYPE_BASE,PNG_NO_FILTERS);
9858 else
9859 png_set_filter(ping,PNG_FILTER_TYPE_BASE,PNG_ALL_FILTERS);
9860 }
cristy4054bfb2011-08-29 23:41:39 +00009861 else if (mng_info->write_png_compression_filter == 7 ||
glennrp18682582011-06-30 18:11:47 +00009862 mng_info->write_png_compression_filter == 10)
9863 png_set_filter(ping,PNG_FILTER_TYPE_BASE,PNG_ALL_FILTERS);
9864
9865 else if (mng_info->write_png_compression_filter == 8)
9866 {
9867#if defined(PNG_MNG_FEATURES_SUPPORTED) && defined(PNG_INTRAPIXEL_DIFFERENCING)
9868 if (mng_info->write_mng)
9869 {
9870 if (((int) ping_color_type == PNG_COLOR_TYPE_RGB) ||
9871 ((int) ping_color_type == PNG_COLOR_TYPE_RGBA))
9872 ping_filter_method=PNG_INTRAPIXEL_DIFFERENCING;
9873 }
9874#endif
cristy4054bfb2011-08-29 23:41:39 +00009875 png_set_filter(ping,PNG_FILTER_TYPE_BASE,PNG_NO_FILTERS);
glennrp18682582011-06-30 18:11:47 +00009876 }
9877
9878 else if (mng_info->write_png_compression_filter == 9)
9879 png_set_filter(ping,PNG_FILTER_TYPE_BASE,PNG_NO_FILTERS);
9880
9881 else if (mng_info->write_png_compression_filter != 0)
9882 png_set_filter(ping,PNG_FILTER_TYPE_BASE,
9883 mng_info->write_png_compression_filter-1);
9884
9885 if (mng_info->write_png_compression_strategy != 0)
9886 png_set_compression_strategy(ping,
9887 mng_info->write_png_compression_strategy-1);
9888
cristy0d57eec2011-09-04 22:13:56 +00009889 /* Only write the iCCP chunk if we are not writing the sRGB chunk. */
9890 if (ping_exclude_sRGB != MagickFalse ||
9891 (image->rendering_intent == UndefinedIntent))
9892 {
9893 if ((ping_exclude_tEXt == MagickFalse ||
9894 ping_exclude_zTXt == MagickFalse) &&
9895 (ping_exclude_iCCP == MagickFalse || ping_exclude_zCCP == MagickFalse))
glennrpc8cbc5d2011-01-01 00:12:34 +00009896 {
9897 ResetImageProfileIterator(image);
9898 for (name=GetNextImageProfile(image); name != (const char *) NULL; )
cristy3ed852e2009-09-05 21:47:34 +00009899 {
glennrpc8cbc5d2011-01-01 00:12:34 +00009900 profile=GetImageProfile(image,name);
9901
9902 if (profile != (StringInfo *) NULL)
9903 {
glennrp5af765f2010-03-30 11:12:18 +00009904#ifdef PNG_WRITE_iCCP_SUPPORTED
glennrpc8cbc5d2011-01-01 00:12:34 +00009905 if ((LocaleCompare(name,"ICC") == 0) ||
9906 (LocaleCompare(name,"ICM") == 0))
glennrp26f37912010-12-23 16:22:42 +00009907 {
glennrpc8cbc5d2011-01-01 00:12:34 +00009908
9909 if (ping_exclude_iCCP == MagickFalse)
9910 {
cristy9f027d12011-09-21 01:17:17 +00009911 png_set_iCCP(ping,ping_info,(png_charp) name,0,
glennrpe4017e32011-01-08 17:16:09 +00009912#if (PNG_LIBPNG_VER < 10500)
glennrpc8cbc5d2011-01-01 00:12:34 +00009913 (png_charp) GetStringInfoDatum(profile),
glennrpe4017e32011-01-08 17:16:09 +00009914#else
9915 (png_const_bytep) GetStringInfoDatum(profile),
9916#endif
glennrpc8cbc5d2011-01-01 00:12:34 +00009917 (png_uint_32) GetStringInfoLength(profile));
9918 }
glennrp26f37912010-12-23 16:22:42 +00009919 }
glennrp0fe50b42010-11-16 03:52:51 +00009920
glennrpc8cbc5d2011-01-01 00:12:34 +00009921 else
cristy3ed852e2009-09-05 21:47:34 +00009922#endif
glennrpc8cbc5d2011-01-01 00:12:34 +00009923 if (ping_exclude_zCCP == MagickFalse)
9924 {
glennrpcf002022011-01-30 02:38:15 +00009925 Magick_png_write_raw_profile(image_info,ping,ping_info,
glennrpc8cbc5d2011-01-01 00:12:34 +00009926 (unsigned char *) name,(unsigned char *) name,
9927 GetStringInfoDatum(profile),
9928 (png_uint_32) GetStringInfoLength(profile));
9929 }
9930 }
glennrp0b206f52011-01-07 04:55:32 +00009931
glennrpc8cbc5d2011-01-01 00:12:34 +00009932 if (logging != MagickFalse)
9933 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9934 " Setting up text chunk with %s profile",name);
9935
9936 name=GetNextImageProfile(image);
cristy3ed852e2009-09-05 21:47:34 +00009937 }
cristy0d57eec2011-09-04 22:13:56 +00009938 }
cristy3ed852e2009-09-05 21:47:34 +00009939 }
9940
9941#if defined(PNG_WRITE_sRGB_SUPPORTED)
9942 if ((mng_info->have_write_global_srgb == 0) &&
9943 ((image->rendering_intent != UndefinedIntent) ||
9944 (image->colorspace == sRGBColorspace)))
9945 {
glennrp26f37912010-12-23 16:22:42 +00009946 if (ping_exclude_sRGB == MagickFalse)
9947 {
9948 /*
9949 Note image rendering intent.
9950 */
9951 if (logging != MagickFalse)
9952 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9953 " Setting up sRGB chunk");
glennrp0fe50b42010-11-16 03:52:51 +00009954
glennrp26f37912010-12-23 16:22:42 +00009955 (void) png_set_sRGB(ping,ping_info,(
glennrpcf002022011-01-30 02:38:15 +00009956 Magick_RenderingIntent_to_PNG_RenderingIntent(
9957 image->rendering_intent)));
glennrp26f37912010-12-23 16:22:42 +00009958 }
cristy3ed852e2009-09-05 21:47:34 +00009959 }
glennrp26f37912010-12-23 16:22:42 +00009960
glennrp5af765f2010-03-30 11:12:18 +00009961 if ((!mng_info->write_mng) || (!png_get_valid(ping,ping_info,PNG_INFO_sRGB)))
cristy3ed852e2009-09-05 21:47:34 +00009962#endif
9963 {
glennrp2cc891a2010-12-24 13:44:32 +00009964 if (ping_exclude_gAMA == MagickFalse &&
9965 (ping_exclude_sRGB == MagickFalse ||
glennrp26f37912010-12-23 16:22:42 +00009966 (image->gamma < .45 || image->gamma > .46)))
9967 {
cristy3ed852e2009-09-05 21:47:34 +00009968 if ((mng_info->have_write_global_gama == 0) && (image->gamma != 0.0))
9969 {
9970 /*
9971 Note image gamma.
9972 To do: check for cHRM+gAMA == sRGB, and write sRGB instead.
9973 */
9974 if (logging != MagickFalse)
9975 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9976 " Setting up gAMA chunk");
glennrp3b51f0e2010-11-27 18:14:08 +00009977
cristy3ed852e2009-09-05 21:47:34 +00009978 png_set_gAMA(ping,ping_info,image->gamma);
9979 }
glennrp26f37912010-12-23 16:22:42 +00009980 }
glennrp2b013e42010-11-24 16:55:50 +00009981
glennrp26f37912010-12-23 16:22:42 +00009982 if (ping_exclude_cHRM == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00009983 {
glennrp26f37912010-12-23 16:22:42 +00009984 if ((mng_info->have_write_global_chrm == 0) &&
9985 (image->chromaticity.red_primary.x != 0.0))
9986 {
9987 /*
9988 Note image chromaticity.
9989 To do: check for cHRM+gAMA == sRGB, and write sRGB instead.
9990 */
9991 PrimaryInfo
9992 bp,
9993 gp,
9994 rp,
9995 wp;
cristy3ed852e2009-09-05 21:47:34 +00009996
glennrp26f37912010-12-23 16:22:42 +00009997 wp=image->chromaticity.white_point;
9998 rp=image->chromaticity.red_primary;
9999 gp=image->chromaticity.green_primary;
10000 bp=image->chromaticity.blue_primary;
cristy3ed852e2009-09-05 21:47:34 +000010001
glennrp26f37912010-12-23 16:22:42 +000010002 if (logging != MagickFalse)
10003 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10004 " Setting up cHRM chunk");
glennrp3b51f0e2010-11-27 18:14:08 +000010005
glennrp26f37912010-12-23 16:22:42 +000010006 png_set_cHRM(ping,ping_info,wp.x,wp.y,rp.x,rp.y,gp.x,gp.y,
10007 bp.x,bp.y);
10008 }
10009 }
cristy3ed852e2009-09-05 21:47:34 +000010010 }
glennrpdfd70802010-11-14 01:23:35 +000010011
glennrp5af765f2010-03-30 11:12:18 +000010012 ping_interlace_method=image_info->interlace != NoInterlace;
cristy3ed852e2009-09-05 21:47:34 +000010013
10014 if (mng_info->write_mng)
10015 png_set_sig_bytes(ping,8);
10016
cristy5d6fc9c2011-12-27 03:10:42 +000010017 /* Bail out if cannot meet defined png:bit-depth or png:color-type */
cristy3ed852e2009-09-05 21:47:34 +000010018
glennrpd6bf1612010-12-17 17:28:54 +000010019 if (mng_info->write_png_colortype != 0)
cristy3ed852e2009-09-05 21:47:34 +000010020 {
10021 if (mng_info->write_png_colortype-1 == PNG_COLOR_TYPE_GRAY)
glennrp8d579662011-02-23 02:05:02 +000010022 if (ping_have_color != MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +000010023 {
glennrp5af765f2010-03-30 11:12:18 +000010024 ping_color_type = PNG_COLOR_TYPE_RGB;
glennrp2b013e42010-11-24 16:55:50 +000010025
glennrp5af765f2010-03-30 11:12:18 +000010026 if (ping_bit_depth < 8)
10027 ping_bit_depth=8;
cristy3ed852e2009-09-05 21:47:34 +000010028 }
glennrp0fe50b42010-11-16 03:52:51 +000010029
cristy3ed852e2009-09-05 21:47:34 +000010030 if (mng_info->write_png_colortype-1 == PNG_COLOR_TYPE_GRAY_ALPHA)
glennrp8d579662011-02-23 02:05:02 +000010031 if (ping_have_color != MagickFalse)
glennrp5af765f2010-03-30 11:12:18 +000010032 ping_color_type = PNG_COLOR_TYPE_RGB_ALPHA;
cristy3ed852e2009-09-05 21:47:34 +000010033 }
10034
glennrp0e8ea192010-12-24 18:00:33 +000010035 if (ping_need_colortype_warning != MagickFalse ||
10036 ((mng_info->write_png_depth &&
glennrp5af765f2010-03-30 11:12:18 +000010037 (int) mng_info->write_png_depth != ping_bit_depth) ||
cristy3ed852e2009-09-05 21:47:34 +000010038 (mng_info->write_png_colortype &&
glennrp5af765f2010-03-30 11:12:18 +000010039 ((int) mng_info->write_png_colortype-1 != ping_color_type &&
glennrp991e92a2010-01-28 03:09:00 +000010040 mng_info->write_png_colortype != 7 &&
glennrp0e8ea192010-12-24 18:00:33 +000010041 !(mng_info->write_png_colortype == 5 && ping_color_type == 0)))))
cristy3ed852e2009-09-05 21:47:34 +000010042 {
10043 if (logging != MagickFalse)
10044 {
glennrp0e8ea192010-12-24 18:00:33 +000010045 if (ping_need_colortype_warning != MagickFalse)
10046 {
10047 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10048 " Image has transparency but tRNS chunk was excluded");
10049 }
10050
cristy3ed852e2009-09-05 21:47:34 +000010051 if (mng_info->write_png_depth)
10052 {
10053 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristy5d6fc9c2011-12-27 03:10:42 +000010054 " Defined png:bit-depth=%u, Computed depth=%u",
cristy3ed852e2009-09-05 21:47:34 +000010055 mng_info->write_png_depth,
glennrp5af765f2010-03-30 11:12:18 +000010056 ping_bit_depth);
cristy3ed852e2009-09-05 21:47:34 +000010057 }
glennrp0e8ea192010-12-24 18:00:33 +000010058
cristy3ed852e2009-09-05 21:47:34 +000010059 if (mng_info->write_png_colortype)
10060 {
10061 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristy5d6fc9c2011-12-27 03:10:42 +000010062 " Defined png:color-type=%u, Computed color type=%u",
cristy3ed852e2009-09-05 21:47:34 +000010063 mng_info->write_png_colortype-1,
glennrp5af765f2010-03-30 11:12:18 +000010064 ping_color_type);
cristy3ed852e2009-09-05 21:47:34 +000010065 }
10066 }
glennrp0e8ea192010-12-24 18:00:33 +000010067
glennrp3bd2e412010-08-10 13:34:52 +000010068 png_warning(ping,
cristy5d6fc9c2011-12-27 03:10:42 +000010069 "Cannot write image with defined png:bit-depth or png:color-type.");
cristy3ed852e2009-09-05 21:47:34 +000010070 }
10071
glennrp58e01762011-01-07 15:28:54 +000010072 if (image_matte != MagickFalse && image->matte == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +000010073 {
10074 /* Add an opaque matte channel */
10075 image->matte = MagickTrue;
cristye941a752011-10-15 01:52:48 +000010076 (void) SetImageAlpha(image,OpaqueAlpha,exception);
glennrp0fe50b42010-11-16 03:52:51 +000010077
glennrpb4a13412010-05-05 12:47:19 +000010078 if (logging != MagickFalse)
10079 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10080 " Added an opaque matte channel");
cristy3ed852e2009-09-05 21:47:34 +000010081 }
10082
glennrp0e319732011-01-25 21:53:13 +000010083 if (number_transparent != 0 || number_semitransparent != 0)
glennrpe9c26dc2010-05-30 01:56:35 +000010084 {
glennrp991d11d2010-11-12 21:55:28 +000010085 if (ping_color_type < 4)
glennrpc8cbc5d2011-01-01 00:12:34 +000010086 {
glennrp991d11d2010-11-12 21:55:28 +000010087 ping_have_tRNS=MagickTrue;
glennrpc8cbc5d2011-01-01 00:12:34 +000010088 if (logging != MagickFalse)
10089 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10090 " Setting ping_have_tRNS=MagickTrue.");
10091 }
glennrpe9c26dc2010-05-30 01:56:35 +000010092 }
10093
cristy3ed852e2009-09-05 21:47:34 +000010094 if (logging != MagickFalse)
10095 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10096 " Writing PNG header chunks");
10097
glennrp5af765f2010-03-30 11:12:18 +000010098 png_set_IHDR(ping,ping_info,ping_width,ping_height,
10099 ping_bit_depth,ping_color_type,
10100 ping_interlace_method,ping_compression_method,
10101 ping_filter_method);
10102
glennrp39992b42010-11-14 00:03:43 +000010103 if (ping_color_type == 3 && ping_have_PLTE != MagickFalse)
10104 {
glennrpf09bded2011-01-08 01:15:59 +000010105 png_set_PLTE(ping,ping_info,palette,number_colors);
glennrp0fe50b42010-11-16 03:52:51 +000010106
glennrp3b51f0e2010-11-27 18:14:08 +000010107 if (logging != MagickFalse)
glennrp39992b42010-11-14 00:03:43 +000010108 {
glennrp8640fb52010-11-23 15:48:26 +000010109 for (i=0; i< (ssize_t) number_colors; i++)
glennrp0fe50b42010-11-16 03:52:51 +000010110 {
glennrpd6bf1612010-12-17 17:28:54 +000010111 if (i < ping_num_trans)
glennrp0fe50b42010-11-16 03:52:51 +000010112 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
glennrpd6bf1612010-12-17 17:28:54 +000010113 " PLTE[%d] = (%d,%d,%d), tRNS[%d] = (%d)",
10114 (int) i,
glennrp0fe50b42010-11-16 03:52:51 +000010115 (int) palette[i].red,
10116 (int) palette[i].green,
10117 (int) palette[i].blue,
glennrpd6bf1612010-12-17 17:28:54 +000010118 (int) i,
glennrp0fe50b42010-11-16 03:52:51 +000010119 (int) ping_trans_alpha[i]);
10120 else
10121 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
glennrpd6bf1612010-12-17 17:28:54 +000010122 " PLTE[%d] = (%d,%d,%d)",
glennrp0fe50b42010-11-16 03:52:51 +000010123 (int) i,
10124 (int) palette[i].red,
10125 (int) palette[i].green,
10126 (int) palette[i].blue);
10127 }
glennrp39992b42010-11-14 00:03:43 +000010128 }
glennrp39992b42010-11-14 00:03:43 +000010129 }
10130
glennrp26f37912010-12-23 16:22:42 +000010131 if (ping_exclude_bKGD == MagickFalse)
glennrpdfd70802010-11-14 01:23:35 +000010132 {
glennrp26f37912010-12-23 16:22:42 +000010133 if (ping_have_bKGD != MagickFalse)
glennrpc6c391a2011-04-27 02:23:56 +000010134 {
glennrp26f37912010-12-23 16:22:42 +000010135 png_set_bKGD(ping,ping_info,&ping_background);
glennrpc6c391a2011-04-27 02:23:56 +000010136 if (logging)
10137 {
10138 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10139 " Setting up bKGD chunk");
10140 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10141 " background color = (%d,%d,%d)",
10142 (int) ping_background.red,
10143 (int) ping_background.green,
10144 (int) ping_background.blue);
10145 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10146 " index = %d, gray=%d",
10147 (int) ping_background.index,
10148 (int) ping_background.gray);
10149 }
10150 }
glennrp26f37912010-12-23 16:22:42 +000010151 }
10152
10153 if (ping_exclude_pHYs == MagickFalse)
10154 {
10155 if (ping_have_pHYs != MagickFalse)
10156 {
10157 png_set_pHYs(ping,ping_info,
10158 ping_pHYs_x_resolution,
10159 ping_pHYs_y_resolution,
10160 ping_pHYs_unit_type);
glennrp823b55c2011-03-14 18:46:46 +000010161
10162 if (logging)
10163 {
10164 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10165 " Setting up pHYs chunk");
10166 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10167 " x_resolution=%lu",
10168 (unsigned long) ping_pHYs_x_resolution);
10169 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10170 " y_resolution=%lu",
10171 (unsigned long) ping_pHYs_y_resolution);
10172 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10173 " unit_type=%lu",
10174 (unsigned long) ping_pHYs_unit_type);
10175 }
glennrp26f37912010-12-23 16:22:42 +000010176 }
glennrpdfd70802010-11-14 01:23:35 +000010177 }
10178
10179#if defined(PNG_oFFs_SUPPORTED)
glennrp4f25bd02011-01-01 18:51:28 +000010180 if (ping_exclude_oFFs == MagickFalse)
glennrpdfd70802010-11-14 01:23:35 +000010181 {
glennrp26f37912010-12-23 16:22:42 +000010182 if (image->page.x || image->page.y)
10183 {
10184 png_set_oFFs(ping,ping_info,(png_int_32) image->page.x,
10185 (png_int_32) image->page.y, 0);
glennrpdfd70802010-11-14 01:23:35 +000010186
glennrp26f37912010-12-23 16:22:42 +000010187 if (logging != MagickFalse)
10188 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10189 " Setting up oFFs chunk with x=%d, y=%d, units=0",
10190 (int) image->page.x, (int) image->page.y);
10191 }
glennrpdfd70802010-11-14 01:23:35 +000010192 }
10193#endif
10194
glennrpda8f3a72011-02-27 23:54:12 +000010195 if (mng_info->need_blob != MagickFalse)
10196 {
cristyc82a27b2011-10-21 01:07:16 +000010197 if (OpenBlob(image_info,image,WriteBinaryBlobMode,exception) ==
glennrpda8f3a72011-02-27 23:54:12 +000010198 MagickFalse)
10199 png_error(ping,"WriteBlob Failed");
10200
10201 ping_have_blob=MagickTrue;
10202 }
10203
cristy3ed852e2009-09-05 21:47:34 +000010204 png_write_info_before_PLTE(ping, ping_info);
glennrp991d11d2010-11-12 21:55:28 +000010205
glennrp39992b42010-11-14 00:03:43 +000010206 if (ping_have_tRNS != MagickFalse && ping_color_type < 4)
glennrp991d11d2010-11-12 21:55:28 +000010207 {
glennrp3b51f0e2010-11-27 18:14:08 +000010208 if (logging != MagickFalse)
glennrp0fe50b42010-11-16 03:52:51 +000010209 {
10210 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10211 " Calling png_set_tRNS with num_trans=%d",ping_num_trans);
10212 }
10213
10214 if (ping_color_type == 3)
10215 (void) png_set_tRNS(ping, ping_info,
10216 ping_trans_alpha,
10217 ping_num_trans,
10218 NULL);
10219
10220 else
10221 {
10222 (void) png_set_tRNS(ping, ping_info,
10223 NULL,
10224 0,
10225 &ping_trans_color);
10226
glennrp3b51f0e2010-11-27 18:14:08 +000010227 if (logging != MagickFalse)
glennrp0fe50b42010-11-16 03:52:51 +000010228 {
10229 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
glennrpc8cbc5d2011-01-01 00:12:34 +000010230 " tRNS color =(%d,%d,%d)",
glennrp0fe50b42010-11-16 03:52:51 +000010231 (int) ping_trans_color.red,
10232 (int) ping_trans_color.green,
10233 (int) ping_trans_color.blue);
10234 }
10235 }
glennrp991d11d2010-11-12 21:55:28 +000010236 }
10237
cristy3ed852e2009-09-05 21:47:34 +000010238 /* write any png-chunk-b profiles */
glennrpcf002022011-01-30 02:38:15 +000010239 (void) Magick_png_write_chunk_from_profile(image,"PNG-chunk-b",logging);
glennrpda8f3a72011-02-27 23:54:12 +000010240
cristy3ed852e2009-09-05 21:47:34 +000010241 png_write_info(ping,ping_info);
glennrp991d11d2010-11-12 21:55:28 +000010242
cristy3ed852e2009-09-05 21:47:34 +000010243 /* write any PNG-chunk-m profiles */
glennrpcf002022011-01-30 02:38:15 +000010244 (void) Magick_png_write_chunk_from_profile(image,"PNG-chunk-m",logging);
cristy3ed852e2009-09-05 21:47:34 +000010245
glennrp26f37912010-12-23 16:22:42 +000010246 if (ping_exclude_vpAg == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +000010247 {
glennrp4f25bd02011-01-01 18:51:28 +000010248 if ((image->page.width != 0 && image->page.width != image->columns) ||
10249 (image->page.height != 0 && image->page.height != image->rows))
glennrp26f37912010-12-23 16:22:42 +000010250 {
10251 unsigned char
10252 chunk[14];
cristy3ed852e2009-09-05 21:47:34 +000010253
glennrp26f37912010-12-23 16:22:42 +000010254 (void) WriteBlobMSBULong(image,9L); /* data length=8 */
10255 PNGType(chunk,mng_vpAg);
glennrp03812ae2010-12-24 01:31:34 +000010256 LogPNGChunk(logging,mng_vpAg,9L);
glennrp26f37912010-12-23 16:22:42 +000010257 PNGLong(chunk+4,(png_uint_32) image->page.width);
10258 PNGLong(chunk+8,(png_uint_32) image->page.height);
10259 chunk[12]=0; /* unit = pixels */
10260 (void) WriteBlob(image,13,chunk);
10261 (void) WriteBlobMSBULong(image,crc32(0,chunk,13));
10262 }
cristy3ed852e2009-09-05 21:47:34 +000010263 }
10264
10265#if (PNG_LIBPNG_VER == 10206)
glennrp9c1eb072010-06-06 22:19:15 +000010266 /* avoid libpng-1.2.6 bug by setting PNG_HAVE_IDAT flag */
cristy3ed852e2009-09-05 21:47:34 +000010267#define PNG_HAVE_IDAT 0x04
glennrp9c1eb072010-06-06 22:19:15 +000010268 ping->mode |= PNG_HAVE_IDAT;
cristy3ed852e2009-09-05 21:47:34 +000010269#undef PNG_HAVE_IDAT
10270#endif
10271
10272 png_set_packing(ping);
10273 /*
10274 Allocate memory.
10275 */
10276 rowbytes=image->columns;
glennrpb4a13412010-05-05 12:47:19 +000010277 if (image_depth > 8)
10278 rowbytes*=2;
cristy7202c102010-05-05 19:18:28 +000010279 switch (ping_color_type)
cristy3ed852e2009-09-05 21:47:34 +000010280 {
glennrpb4a13412010-05-05 12:47:19 +000010281 case PNG_COLOR_TYPE_RGB:
cristy3ed852e2009-09-05 21:47:34 +000010282 rowbytes*=3;
glennrpb4a13412010-05-05 12:47:19 +000010283 break;
glennrp0fe50b42010-11-16 03:52:51 +000010284
glennrpb4a13412010-05-05 12:47:19 +000010285 case PNG_COLOR_TYPE_GRAY_ALPHA:
10286 rowbytes*=2;
10287 break;
glennrp0fe50b42010-11-16 03:52:51 +000010288
glennrpb4a13412010-05-05 12:47:19 +000010289 case PNG_COLOR_TYPE_RGBA:
cristy3ed852e2009-09-05 21:47:34 +000010290 rowbytes*=4;
glennrpb4a13412010-05-05 12:47:19 +000010291 break;
glennrp0fe50b42010-11-16 03:52:51 +000010292
glennrpb4a13412010-05-05 12:47:19 +000010293 default:
10294 break;
cristy3ed852e2009-09-05 21:47:34 +000010295 }
glennrp3b51f0e2010-11-27 18:14:08 +000010296
10297 if (logging != MagickFalse)
glennrpb4a13412010-05-05 12:47:19 +000010298 {
10299 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10300 " Writing PNG image data");
glennrp0fe50b42010-11-16 03:52:51 +000010301
glennrpb4a13412010-05-05 12:47:19 +000010302 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +000010303 " Allocating %.20g bytes of memory for pixels",(double) rowbytes);
glennrpb4a13412010-05-05 12:47:19 +000010304 }
glennrpcf002022011-01-30 02:38:15 +000010305 ping_pixels=(unsigned char *) AcquireQuantumMemory(rowbytes,
10306 sizeof(*ping_pixels));
glennrp0fe50b42010-11-16 03:52:51 +000010307
glennrpcf002022011-01-30 02:38:15 +000010308 if (ping_pixels == (unsigned char *) NULL)
cristy3ed852e2009-09-05 21:47:34 +000010309 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
glennrp0fe50b42010-11-16 03:52:51 +000010310
cristy3ed852e2009-09-05 21:47:34 +000010311 /*
10312 Initialize image scanlines.
10313 */
glennrp5af765f2010-03-30 11:12:18 +000010314 if (setjmp(png_jmpbuf(ping)))
cristy3ed852e2009-09-05 21:47:34 +000010315 {
10316 /*
10317 PNG write failed.
10318 */
10319#ifdef PNG_DEBUG
10320 if (image_info->verbose)
10321 (void) printf("PNG write has failed.\n");
10322#endif
10323 png_destroy_write_struct(&ping,&ping_info);
10324 if (quantum_info != (QuantumInfo *) NULL)
10325 quantum_info=DestroyQuantumInfo(quantum_info);
glennrpcf002022011-01-30 02:38:15 +000010326 if (ping_pixels != (unsigned char *) NULL)
10327 ping_pixels=(unsigned char *) RelinquishMagickMemory(ping_pixels);
cristy3ed852e2009-09-05 21:47:34 +000010328#if defined(PNG_SETJMP_NOT_THREAD_SAFE)
glennrpcf002022011-01-30 02:38:15 +000010329 UnlockSemaphoreInfo(ping_semaphore);
cristy3ed852e2009-09-05 21:47:34 +000010330#endif
glennrpda8f3a72011-02-27 23:54:12 +000010331 if (ping_have_blob != MagickFalse)
glennrpb9cfe272010-12-21 15:08:06 +000010332 (void) CloseBlob(image);
10333 image_info=DestroyImageInfo(image_info);
10334 image=DestroyImage(image);
cristy3ed852e2009-09-05 21:47:34 +000010335 return(MagickFalse);
10336 }
cristyed552522009-10-16 14:04:35 +000010337 quantum_info=AcquireQuantumInfo(image_info,image);
10338 if (quantum_info == (QuantumInfo *) NULL)
10339 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
cristy3ed852e2009-09-05 21:47:34 +000010340 quantum_info->format=UndefinedQuantumFormat;
10341 quantum_info->depth=image_depth;
10342 num_passes=png_set_interlace_handling(ping);
glennrp8bb3a022010-12-13 20:40:04 +000010343
cristy3ed852e2009-09-05 21:47:34 +000010344 if ((!mng_info->write_png8 && !mng_info->write_png24 &&
glennrp8bb3a022010-12-13 20:40:04 +000010345 !mng_info->write_png32) &&
10346 (mng_info->IsPalette ||
cristy3ed852e2009-09-05 21:47:34 +000010347 (image_info->type == BilevelType)) &&
glennrp8d579662011-02-23 02:05:02 +000010348 image_matte == MagickFalse &&
10349 ping_have_non_bw == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +000010350 {
glennrp8bb3a022010-12-13 20:40:04 +000010351 /* Palette, Bilevel, or Opaque Monochrome */
cristy4c08aed2011-07-01 19:47:50 +000010352 register const Quantum
cristy3ed852e2009-09-05 21:47:34 +000010353 *p;
glennrp0fe50b42010-11-16 03:52:51 +000010354
cristy3ed852e2009-09-05 21:47:34 +000010355 quantum_info->depth=8;
10356 for (pass=0; pass < num_passes; pass++)
10357 {
10358 /*
10359 Convert PseudoClass image to a PNG monochrome image.
10360 */
cristybb503372010-05-27 20:51:26 +000010361 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +000010362 {
glennrpd71e86a2011-02-24 01:28:37 +000010363 if (logging != MagickFalse && y == 0)
glennrp3b51f0e2010-11-27 18:14:08 +000010364 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10365 " Writing row of pixels (0)");
glennrpa521b2f2010-10-29 04:11:03 +000010366
cristyc82a27b2011-10-21 01:07:16 +000010367 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
glennrp0fe50b42010-11-16 03:52:51 +000010368
cristy4c08aed2011-07-01 19:47:50 +000010369 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +000010370 break;
glennrp0fe50b42010-11-16 03:52:51 +000010371
cristy3ed852e2009-09-05 21:47:34 +000010372 if (mng_info->IsPalette)
10373 {
cristy4c08aed2011-07-01 19:47:50 +000010374 (void) ExportQuantumPixels(image,(CacheView *) NULL,
cristyc82a27b2011-10-21 01:07:16 +000010375 quantum_info,GrayQuantum,ping_pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +000010376 if (mng_info->write_png_colortype-1 == PNG_COLOR_TYPE_PALETTE &&
10377 mng_info->write_png_depth &&
10378 mng_info->write_png_depth != old_bit_depth)
10379 {
10380 /* Undo pixel scaling */
cristybb503372010-05-27 20:51:26 +000010381 for (i=0; i < (ssize_t) image->columns; i++)
glennrpcf002022011-01-30 02:38:15 +000010382 *(ping_pixels+i)=(unsigned char) (*(ping_pixels+i)
cristy3ed852e2009-09-05 21:47:34 +000010383 >> (8-old_bit_depth));
10384 }
10385 }
glennrp0fe50b42010-11-16 03:52:51 +000010386
cristy3ed852e2009-09-05 21:47:34 +000010387 else
10388 {
cristy4c08aed2011-07-01 19:47:50 +000010389 (void) ExportQuantumPixels(image,(CacheView *) NULL,
cristyc82a27b2011-10-21 01:07:16 +000010390 quantum_info,RedQuantum,ping_pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +000010391 }
glennrp0fe50b42010-11-16 03:52:51 +000010392
cristy3ed852e2009-09-05 21:47:34 +000010393 if (mng_info->write_png_colortype-1 != PNG_COLOR_TYPE_PALETTE)
cristybb503372010-05-27 20:51:26 +000010394 for (i=0; i < (ssize_t) image->columns; i++)
glennrpcf002022011-01-30 02:38:15 +000010395 *(ping_pixels+i)=(unsigned char) ((*(ping_pixels+i) > 127) ?
cristy3ed852e2009-09-05 21:47:34 +000010396 255 : 0);
glennrp0fe50b42010-11-16 03:52:51 +000010397
glennrp3b51f0e2010-11-27 18:14:08 +000010398 if (logging != MagickFalse && y == 0)
glennrpb4a13412010-05-05 12:47:19 +000010399 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10400 " Writing row of pixels (1)");
glennrp0fe50b42010-11-16 03:52:51 +000010401
glennrpcf002022011-01-30 02:38:15 +000010402 png_write_row(ping,ping_pixels);
cristy3ed852e2009-09-05 21:47:34 +000010403 }
10404 if (image->previous == (Image *) NULL)
10405 {
10406 status=SetImageProgress(image,LoadImageTag,pass,num_passes);
10407 if (status == MagickFalse)
10408 break;
10409 }
10410 }
10411 }
glennrp0fe50b42010-11-16 03:52:51 +000010412
glennrp8bb3a022010-12-13 20:40:04 +000010413 else /* Not Palette, Bilevel, or Opaque Monochrome */
cristy3ed852e2009-09-05 21:47:34 +000010414 {
glennrp0fe50b42010-11-16 03:52:51 +000010415 if ((!mng_info->write_png8 && !mng_info->write_png24 &&
cristy3ed852e2009-09-05 21:47:34 +000010416 !mng_info->write_png32) &&
glennrp58e01762011-01-07 15:28:54 +000010417 (image_matte != MagickFalse ||
glennrp5af765f2010-03-30 11:12:18 +000010418 (ping_bit_depth >= MAGICKCORE_QUANTUM_DEPTH)) &&
glennrp8d579662011-02-23 02:05:02 +000010419 (mng_info->IsPalette) && ping_have_color == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +000010420 {
cristy4c08aed2011-07-01 19:47:50 +000010421 register const Quantum
glennrp8bb3a022010-12-13 20:40:04 +000010422 *p;
glennrp0fe50b42010-11-16 03:52:51 +000010423
glennrp8bb3a022010-12-13 20:40:04 +000010424 for (pass=0; pass < num_passes; pass++)
cristy3ed852e2009-09-05 21:47:34 +000010425 {
glennrp8bb3a022010-12-13 20:40:04 +000010426
cristybb503372010-05-27 20:51:26 +000010427 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +000010428 {
cristyc82a27b2011-10-21 01:07:16 +000010429 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
glennrp2cc891a2010-12-24 13:44:32 +000010430
cristy4c08aed2011-07-01 19:47:50 +000010431 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +000010432 break;
glennrp2cc891a2010-12-24 13:44:32 +000010433
glennrp5af765f2010-03-30 11:12:18 +000010434 if (ping_color_type == PNG_COLOR_TYPE_GRAY)
cristy3ed852e2009-09-05 21:47:34 +000010435 {
glennrp8bb3a022010-12-13 20:40:04 +000010436 if (mng_info->IsPalette)
cristy4c08aed2011-07-01 19:47:50 +000010437 (void) ExportQuantumPixels(image,(CacheView *) NULL,
cristyc82a27b2011-10-21 01:07:16 +000010438 quantum_info,GrayQuantum,ping_pixels,exception);
glennrp2cc891a2010-12-24 13:44:32 +000010439
glennrp8bb3a022010-12-13 20:40:04 +000010440 else
cristy4c08aed2011-07-01 19:47:50 +000010441 (void) ExportQuantumPixels(image,(CacheView *) NULL,
cristyc82a27b2011-10-21 01:07:16 +000010442 quantum_info,RedQuantum,ping_pixels,exception);
glennrp2cc891a2010-12-24 13:44:32 +000010443
glennrp3b51f0e2010-11-27 18:14:08 +000010444 if (logging != MagickFalse && y == 0)
glennrpb4a13412010-05-05 12:47:19 +000010445 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
glennrp8bb3a022010-12-13 20:40:04 +000010446 " Writing GRAY PNG pixels (2)");
glennrpb4a13412010-05-05 12:47:19 +000010447 }
glennrp2cc891a2010-12-24 13:44:32 +000010448
glennrp8bb3a022010-12-13 20:40:04 +000010449 else /* PNG_COLOR_TYPE_GRAY_ALPHA */
10450 {
10451 if (logging != MagickFalse && y == 0)
10452 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10453 " Writing GRAY_ALPHA PNG pixels (2)");
glennrp2cc891a2010-12-24 13:44:32 +000010454
cristy4c08aed2011-07-01 19:47:50 +000010455 (void) ExportQuantumPixels(image,(CacheView *) NULL,
cristyc82a27b2011-10-21 01:07:16 +000010456 quantum_info,GrayAlphaQuantum,ping_pixels,exception);
glennrp8bb3a022010-12-13 20:40:04 +000010457 }
glennrp2cc891a2010-12-24 13:44:32 +000010458
glennrp3b51f0e2010-11-27 18:14:08 +000010459 if (logging != MagickFalse && y == 0)
glennrpb4a13412010-05-05 12:47:19 +000010460 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
glennrp8bb3a022010-12-13 20:40:04 +000010461 " Writing row of pixels (2)");
glennrp2cc891a2010-12-24 13:44:32 +000010462
glennrpcf002022011-01-30 02:38:15 +000010463 png_write_row(ping,ping_pixels);
cristy3ed852e2009-09-05 21:47:34 +000010464 }
glennrp2cc891a2010-12-24 13:44:32 +000010465
glennrp8bb3a022010-12-13 20:40:04 +000010466 if (image->previous == (Image *) NULL)
cristy3ed852e2009-09-05 21:47:34 +000010467 {
glennrp8bb3a022010-12-13 20:40:04 +000010468 status=SetImageProgress(image,LoadImageTag,pass,num_passes);
10469 if (status == MagickFalse)
10470 break;
cristy3ed852e2009-09-05 21:47:34 +000010471 }
cristy3ed852e2009-09-05 21:47:34 +000010472 }
10473 }
glennrp8bb3a022010-12-13 20:40:04 +000010474
10475 else
10476 {
cristy4c08aed2011-07-01 19:47:50 +000010477 register const Quantum
glennrp8bb3a022010-12-13 20:40:04 +000010478 *p;
10479
10480 for (pass=0; pass < num_passes; pass++)
cristy3ed852e2009-09-05 21:47:34 +000010481 {
glennrp8bb3a022010-12-13 20:40:04 +000010482 if ((image_depth > 8) || (mng_info->write_png24 ||
10483 mng_info->write_png32 ||
10484 (!mng_info->write_png8 && !mng_info->IsPalette)))
10485 {
10486 for (y=0; y < (ssize_t) image->rows; y++)
10487 {
10488 p=GetVirtualPixels(image,0,y,image->columns,1,
cristyc82a27b2011-10-21 01:07:16 +000010489 exception);
glennrp2cc891a2010-12-24 13:44:32 +000010490
cristy4c08aed2011-07-01 19:47:50 +000010491 if (p == (const Quantum *) NULL)
glennrp8bb3a022010-12-13 20:40:04 +000010492 break;
glennrp2cc891a2010-12-24 13:44:32 +000010493
glennrp8bb3a022010-12-13 20:40:04 +000010494 if (ping_color_type == PNG_COLOR_TYPE_GRAY)
10495 {
10496 if (image->storage_class == DirectClass)
cristy4c08aed2011-07-01 19:47:50 +000010497 (void) ExportQuantumPixels(image,(CacheView *) NULL,
cristyc82a27b2011-10-21 01:07:16 +000010498 quantum_info,RedQuantum,ping_pixels,exception);
glennrp2cc891a2010-12-24 13:44:32 +000010499
glennrp8bb3a022010-12-13 20:40:04 +000010500 else
cristy4c08aed2011-07-01 19:47:50 +000010501 (void) ExportQuantumPixels(image,(CacheView *) NULL,
cristyc82a27b2011-10-21 01:07:16 +000010502 quantum_info,GrayQuantum,ping_pixels,exception);
glennrp8bb3a022010-12-13 20:40:04 +000010503 }
glennrp2cc891a2010-12-24 13:44:32 +000010504
glennrp8bb3a022010-12-13 20:40:04 +000010505 else if (ping_color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
10506 {
cristy4c08aed2011-07-01 19:47:50 +000010507 (void) ExportQuantumPixels(image,(CacheView *) NULL,
glennrpcf002022011-01-30 02:38:15 +000010508 quantum_info,GrayAlphaQuantum,ping_pixels,
cristyc82a27b2011-10-21 01:07:16 +000010509 exception);
glennrp2cc891a2010-12-24 13:44:32 +000010510
glennrp8bb3a022010-12-13 20:40:04 +000010511 if (logging != MagickFalse && y == 0)
10512 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10513 " Writing GRAY_ALPHA PNG pixels (3)");
10514 }
glennrp2cc891a2010-12-24 13:44:32 +000010515
glennrp8bb3a022010-12-13 20:40:04 +000010516 else if (image_matte != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +000010517 (void) ExportQuantumPixels(image,(CacheView *) NULL,
cristyc82a27b2011-10-21 01:07:16 +000010518 quantum_info,RGBAQuantum,ping_pixels,exception);
glennrp2cc891a2010-12-24 13:44:32 +000010519
glennrp8bb3a022010-12-13 20:40:04 +000010520 else
cristy4c08aed2011-07-01 19:47:50 +000010521 (void) ExportQuantumPixels(image,(CacheView *) NULL,
cristyc82a27b2011-10-21 01:07:16 +000010522 quantum_info,RGBQuantum,ping_pixels,exception);
glennrp2cc891a2010-12-24 13:44:32 +000010523
glennrp8bb3a022010-12-13 20:40:04 +000010524 if (logging != MagickFalse && y == 0)
10525 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10526 " Writing row of pixels (3)");
glennrp2cc891a2010-12-24 13:44:32 +000010527
glennrpcf002022011-01-30 02:38:15 +000010528 png_write_row(ping,ping_pixels);
glennrp8bb3a022010-12-13 20:40:04 +000010529 }
10530 }
glennrp2cc891a2010-12-24 13:44:32 +000010531
glennrp8bb3a022010-12-13 20:40:04 +000010532 else
10533 /* not ((image_depth > 8) || (mng_info->write_png24 ||
10534 mng_info->write_png32 ||
10535 (!mng_info->write_png8 && !mng_info->IsPalette))) */
10536 {
10537 if ((ping_color_type != PNG_COLOR_TYPE_GRAY) &&
10538 (ping_color_type != PNG_COLOR_TYPE_GRAY_ALPHA))
10539 {
10540 if (logging != MagickFalse)
10541 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10542 " pass %d, Image Is not GRAY or GRAY_ALPHA",pass);
glennrp2cc891a2010-12-24 13:44:32 +000010543
glennrp8bb3a022010-12-13 20:40:04 +000010544 quantum_info->depth=8;
10545 image_depth=8;
10546 }
glennrp2cc891a2010-12-24 13:44:32 +000010547
glennrp8bb3a022010-12-13 20:40:04 +000010548 for (y=0; y < (ssize_t) image->rows; y++)
10549 {
10550 if (logging != MagickFalse && y == 0)
10551 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10552 " pass %d, Image Is RGB, 16-bit GRAY, or GRAY_ALPHA",pass);
glennrp2cc891a2010-12-24 13:44:32 +000010553
cristy97707062011-12-27 18:25:00 +000010554 p=GetVirtualPixels(image,0,y,image->columns,1, exception);
glennrp2cc891a2010-12-24 13:44:32 +000010555
cristy4c08aed2011-07-01 19:47:50 +000010556 if (p == (const Quantum *) NULL)
glennrp8bb3a022010-12-13 20:40:04 +000010557 break;
glennrp2cc891a2010-12-24 13:44:32 +000010558
glennrp8bb3a022010-12-13 20:40:04 +000010559 if (ping_color_type == PNG_COLOR_TYPE_GRAY)
glennrp44757ab2011-03-17 12:57:03 +000010560 {
glennrp4bf89732011-03-21 13:48:28 +000010561 quantum_info->depth=image->depth;
10562
cristy4c08aed2011-07-01 19:47:50 +000010563 (void) ExportQuantumPixels(image,(CacheView *) NULL,
cristyc82a27b2011-10-21 01:07:16 +000010564 quantum_info,GrayQuantum,ping_pixels,exception);
glennrp44757ab2011-03-17 12:57:03 +000010565 }
glennrp2cc891a2010-12-24 13:44:32 +000010566
glennrp8bb3a022010-12-13 20:40:04 +000010567 else if (ping_color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
10568 {
10569 if (logging != MagickFalse && y == 0)
10570 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10571 " Writing GRAY_ALPHA PNG pixels (4)");
glennrp2cc891a2010-12-24 13:44:32 +000010572
cristy4c08aed2011-07-01 19:47:50 +000010573 (void) ExportQuantumPixels(image,(CacheView *) NULL,
glennrpcf002022011-01-30 02:38:15 +000010574 quantum_info,GrayAlphaQuantum,ping_pixels,
cristyc82a27b2011-10-21 01:07:16 +000010575 exception);
glennrp8bb3a022010-12-13 20:40:04 +000010576 }
glennrp2cc891a2010-12-24 13:44:32 +000010577
glennrp8bb3a022010-12-13 20:40:04 +000010578 else
glennrp8bb3a022010-12-13 20:40:04 +000010579 {
cristy4c08aed2011-07-01 19:47:50 +000010580 (void) ExportQuantumPixels(image,(CacheView *) NULL,
cristyc82a27b2011-10-21 01:07:16 +000010581 quantum_info,IndexQuantum,ping_pixels,exception);
glennrp5eae7602011-02-22 15:21:32 +000010582
10583 if (logging != MagickFalse && y <= 2)
10584 {
10585 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
glennrp1a7d6db2011-03-17 13:24:10 +000010586 " Writing row of non-gray pixels (4)");
glennrp5eae7602011-02-22 15:21:32 +000010587
10588 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10589 " ping_pixels[0]=%d,ping_pixels[1]=%d",
10590 (int)ping_pixels[0],(int)ping_pixels[1]);
10591 }
glennrp8bb3a022010-12-13 20:40:04 +000010592 }
glennrpcf002022011-01-30 02:38:15 +000010593 png_write_row(ping,ping_pixels);
glennrp8bb3a022010-12-13 20:40:04 +000010594 }
10595 }
glennrp2cc891a2010-12-24 13:44:32 +000010596
glennrp8bb3a022010-12-13 20:40:04 +000010597 if (image->previous == (Image *) NULL)
10598 {
10599 status=SetImageProgress(image,LoadImageTag,pass,num_passes);
10600 if (status == MagickFalse)
10601 break;
10602 }
cristy3ed852e2009-09-05 21:47:34 +000010603 }
glennrp8bb3a022010-12-13 20:40:04 +000010604 }
10605 }
10606
cristyb32b90a2009-09-07 21:45:48 +000010607 if (quantum_info != (QuantumInfo *) NULL)
10608 quantum_info=DestroyQuantumInfo(quantum_info);
cristy3ed852e2009-09-05 21:47:34 +000010609
10610 if (logging != MagickFalse)
10611 {
10612 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
glennrpb4a13412010-05-05 12:47:19 +000010613 " Wrote PNG image data");
glennrp0fe50b42010-11-16 03:52:51 +000010614
cristy3ed852e2009-09-05 21:47:34 +000010615 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +000010616 " Width: %.20g",(double) ping_width);
glennrp0fe50b42010-11-16 03:52:51 +000010617
cristy3ed852e2009-09-05 21:47:34 +000010618 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +000010619 " Height: %.20g",(double) ping_height);
glennrp0fe50b42010-11-16 03:52:51 +000010620
cristy3ed852e2009-09-05 21:47:34 +000010621 if (mng_info->write_png_depth)
10622 {
10623 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristy5d6fc9c2011-12-27 03:10:42 +000010624 " Defined png:bit-depth: %d",mng_info->write_png_depth);
cristy3ed852e2009-09-05 21:47:34 +000010625 }
glennrp0fe50b42010-11-16 03:52:51 +000010626
cristy3ed852e2009-09-05 21:47:34 +000010627 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
glennrp5af765f2010-03-30 11:12:18 +000010628 " PNG bit-depth written: %d",ping_bit_depth);
glennrp0fe50b42010-11-16 03:52:51 +000010629
cristy3ed852e2009-09-05 21:47:34 +000010630 if (mng_info->write_png_colortype)
10631 {
10632 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristy5d6fc9c2011-12-27 03:10:42 +000010633 " Defined png:color-type: %d",mng_info->write_png_colortype-1);
cristy3ed852e2009-09-05 21:47:34 +000010634 }
glennrp0fe50b42010-11-16 03:52:51 +000010635
cristy3ed852e2009-09-05 21:47:34 +000010636 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
glennrp5af765f2010-03-30 11:12:18 +000010637 " PNG color-type written: %d",ping_color_type);
glennrp0fe50b42010-11-16 03:52:51 +000010638
cristy3ed852e2009-09-05 21:47:34 +000010639 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
glennrp5af765f2010-03-30 11:12:18 +000010640 " PNG Interlace method: %d",ping_interlace_method);
cristy3ed852e2009-09-05 21:47:34 +000010641 }
10642 /*
glennrpa0ed0092011-04-18 16:36:29 +000010643 Generate text chunks after IDAT.
cristy3ed852e2009-09-05 21:47:34 +000010644 */
glennrp823b55c2011-03-14 18:46:46 +000010645 if (ping_exclude_tEXt == MagickFalse || ping_exclude_zTXt == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +000010646 {
glennrp26f37912010-12-23 16:22:42 +000010647 ResetImagePropertyIterator(image);
cristy3ed852e2009-09-05 21:47:34 +000010648 property=GetNextImageProperty(image);
glennrp26f37912010-12-23 16:22:42 +000010649 while (property != (const char *) NULL)
10650 {
10651 png_textp
10652 text;
glennrp2cc891a2010-12-24 13:44:32 +000010653
cristyd15e6592011-10-15 00:13:06 +000010654 value=GetImageProperty(image,property,exception);
glennrpa0ed0092011-04-18 16:36:29 +000010655
10656 /* Don't write any "png:" properties; those are just for "identify" */
10657 if (LocaleNCompare(property,"png:",4) != 0 &&
10658
10659 /* Suppress density and units if we wrote a pHYs chunk */
10660 (ping_exclude_pHYs != MagickFalse ||
glennrp823b55c2011-03-14 18:46:46 +000010661 LocaleCompare(property,"density") != 0 ||
glennrpa0ed0092011-04-18 16:36:29 +000010662 LocaleCompare(property,"units") != 0) &&
10663
10664 /* Suppress the IM-generated Date:create and Date:modify */
10665 (ping_exclude_date == MagickFalse ||
10666 LocaleNCompare(property, "Date:",5) != 0))
glennrp26f37912010-12-23 16:22:42 +000010667 {
glennrpc70af4a2011-03-07 00:08:23 +000010668 if (value != (const char *) NULL)
glennrp26f37912010-12-23 16:22:42 +000010669 {
glennrpc70af4a2011-03-07 00:08:23 +000010670 text=(png_textp) png_malloc(ping,(png_uint_32) sizeof(png_text));
10671 text[0].key=(char *) property;
10672 text[0].text=(char *) value;
10673 text[0].text_length=strlen(value);
glennrp2cc891a2010-12-24 13:44:32 +000010674
glennrpc70af4a2011-03-07 00:08:23 +000010675 if (ping_exclude_tEXt != MagickFalse)
10676 text[0].compression=PNG_TEXT_COMPRESSION_zTXt;
10677
10678 else if (ping_exclude_zTXt != MagickFalse)
10679 text[0].compression=PNG_TEXT_COMPRESSION_NONE;
10680
10681 else
glennrp26f37912010-12-23 16:22:42 +000010682 {
glennrpc70af4a2011-03-07 00:08:23 +000010683 text[0].compression=image_info->compression == NoCompression ||
10684 (image_info->compression == UndefinedCompression &&
10685 text[0].text_length < 128) ? PNG_TEXT_COMPRESSION_NONE :
10686 PNG_TEXT_COMPRESSION_zTXt ;
glennrp26f37912010-12-23 16:22:42 +000010687 }
glennrp2cc891a2010-12-24 13:44:32 +000010688
glennrpc70af4a2011-03-07 00:08:23 +000010689 if (logging != MagickFalse)
10690 {
10691 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10692 " Setting up text chunk");
10693
10694 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10695 " keyword: %s",text[0].key);
10696 }
10697
10698 png_set_text(ping,ping_info,text,1);
10699 png_free(ping,text);
10700 }
glennrp26f37912010-12-23 16:22:42 +000010701 }
10702 property=GetNextImageProperty(image);
10703 }
cristy3ed852e2009-09-05 21:47:34 +000010704 }
10705
10706 /* write any PNG-chunk-e profiles */
glennrpcf002022011-01-30 02:38:15 +000010707 (void) Magick_png_write_chunk_from_profile(image,"PNG-chunk-e",logging);
cristy3ed852e2009-09-05 21:47:34 +000010708
10709 if (logging != MagickFalse)
10710 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10711 " Writing PNG end info");
glennrp0fe50b42010-11-16 03:52:51 +000010712
cristy3ed852e2009-09-05 21:47:34 +000010713 png_write_end(ping,ping_info);
glennrp0fe50b42010-11-16 03:52:51 +000010714
cristy3ed852e2009-09-05 21:47:34 +000010715 if (mng_info->need_fram && (int) image->dispose == BackgroundDispose)
10716 {
10717 if (mng_info->page.x || mng_info->page.y ||
glennrp5af765f2010-03-30 11:12:18 +000010718 (ping_width != mng_info->page.width) ||
10719 (ping_height != mng_info->page.height))
cristy3ed852e2009-09-05 21:47:34 +000010720 {
10721 unsigned char
10722 chunk[32];
10723
10724 /*
10725 Write FRAM 4 with clipping boundaries followed by FRAM 1.
10726 */
10727 (void) WriteBlobMSBULong(image,27L); /* data length=27 */
10728 PNGType(chunk,mng_FRAM);
glennrp03812ae2010-12-24 01:31:34 +000010729 LogPNGChunk(logging,mng_FRAM,27L);
cristy3ed852e2009-09-05 21:47:34 +000010730 chunk[4]=4;
10731 chunk[5]=0; /* frame name separator (no name) */
10732 chunk[6]=1; /* flag for changing delay, for next frame only */
10733 chunk[7]=0; /* flag for changing frame timeout */
10734 chunk[8]=1; /* flag for changing frame clipping for next frame */
10735 chunk[9]=0; /* flag for changing frame sync_id */
10736 PNGLong(chunk+10,(png_uint_32) (0L)); /* temporary 0 delay */
10737 chunk[14]=0; /* clipping boundaries delta type */
10738 PNGLong(chunk+15,(png_uint_32) (mng_info->page.x)); /* left cb */
10739 PNGLong(chunk+19,
glennrp5af765f2010-03-30 11:12:18 +000010740 (png_uint_32) (mng_info->page.x + ping_width));
cristy3ed852e2009-09-05 21:47:34 +000010741 PNGLong(chunk+23,(png_uint_32) (mng_info->page.y)); /* top cb */
10742 PNGLong(chunk+27,
glennrp5af765f2010-03-30 11:12:18 +000010743 (png_uint_32) (mng_info->page.y + ping_height));
cristy3ed852e2009-09-05 21:47:34 +000010744 (void) WriteBlob(image,31,chunk);
10745 (void) WriteBlobMSBULong(image,crc32(0,chunk,31));
10746 mng_info->old_framing_mode=4;
10747 mng_info->framing_mode=1;
10748 }
glennrp0fe50b42010-11-16 03:52:51 +000010749
cristy3ed852e2009-09-05 21:47:34 +000010750 else
10751 mng_info->framing_mode=3;
10752 }
10753 if (mng_info->write_mng && !mng_info->need_fram &&
10754 ((int) image->dispose == 3))
cristyc82a27b2011-10-21 01:07:16 +000010755 (void) ThrowMagickException(exception,GetMagickModule(),
cristy3ed852e2009-09-05 21:47:34 +000010756 CoderError,"Cannot convert GIF with disposal method 3 to MNG-LC",
glennrpc70af4a2011-03-07 00:08:23 +000010757 "`%s'",image->filename);
glennrp0fe50b42010-11-16 03:52:51 +000010758
cristy3ed852e2009-09-05 21:47:34 +000010759 /*
10760 Free PNG resources.
10761 */
glennrp5af765f2010-03-30 11:12:18 +000010762
cristy3ed852e2009-09-05 21:47:34 +000010763 png_destroy_write_struct(&ping,&ping_info);
10764
glennrpcf002022011-01-30 02:38:15 +000010765 ping_pixels=(unsigned char *) RelinquishMagickMemory(ping_pixels);
cristy3ed852e2009-09-05 21:47:34 +000010766
10767#if defined(PNG_SETJMP_NOT_THREAD_SAFE)
glennrpcf002022011-01-30 02:38:15 +000010768 UnlockSemaphoreInfo(ping_semaphore);
cristy3ed852e2009-09-05 21:47:34 +000010769#endif
10770
glennrpda8f3a72011-02-27 23:54:12 +000010771 if (ping_have_blob != MagickFalse)
glennrpb9cfe272010-12-21 15:08:06 +000010772 (void) CloseBlob(image);
10773
10774 image_info=DestroyImageInfo(image_info);
10775 image=DestroyImage(image);
10776
10777 /* Store bit depth actually written */
10778 s[0]=(char) ping_bit_depth;
10779 s[1]='\0';
10780
cristyd15e6592011-10-15 00:13:06 +000010781 (void) SetImageProperty(IMimage,"png:bit-depth-written",s,exception);
glennrpb9cfe272010-12-21 15:08:06 +000010782
cristy3ed852e2009-09-05 21:47:34 +000010783 if (logging != MagickFalse)
10784 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10785 " exit WriteOnePNGImage()");
glennrp0fe50b42010-11-16 03:52:51 +000010786
cristy3ed852e2009-09-05 21:47:34 +000010787 return(MagickTrue);
10788/* End write one PNG image */
10789}
10790
10791/*
10792%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
10793% %
10794% %
10795% %
10796% W r i t e P N G I m a g e %
10797% %
10798% %
10799% %
10800%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
10801%
10802% WritePNGImage() writes a Portable Network Graphics (PNG) or
10803% Multiple-image Network Graphics (MNG) image file.
10804%
10805% MNG support written by Glenn Randers-Pehrson, glennrp@image...
10806%
10807% The format of the WritePNGImage method is:
10808%
cristy1e178e72011-08-28 19:44:34 +000010809% MagickBooleanType WritePNGImage(const ImageInfo *image_info,
10810% Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +000010811%
10812% A description of each parameter follows:
10813%
10814% o image_info: the image info.
10815%
10816% o image: The image.
10817%
cristy1e178e72011-08-28 19:44:34 +000010818% o exception: return any errors or warnings in this structure.
10819%
cristy3ed852e2009-09-05 21:47:34 +000010820% Returns MagickTrue on success, MagickFalse on failure.
10821%
10822% Communicating with the PNG encoder:
10823%
10824% While the datastream written is always in PNG format and normally would
10825% be given the "png" file extension, this method also writes the following
cristy5d6fc9c2011-12-27 03:10:42 +000010826% pseudo-formats which are subsets of png:
cristy3ed852e2009-09-05 21:47:34 +000010827%
glennrp5a39f372011-02-25 04:52:16 +000010828% o PNG8: An 8-bit indexed PNG datastream is written. If the image has
10829% a depth greater than 8, the depth is reduced. If transparency
cristy3ed852e2009-09-05 21:47:34 +000010830% is present, the tRNS chunk must only have values 0 and 255
10831% (i.e., transparency is binary: fully opaque or fully
glennrp5a39f372011-02-25 04:52:16 +000010832% transparent). If other values are present they will be
10833% 50%-thresholded to binary transparency. If more than 256
glennrpe9637cb2011-03-24 16:34:37 +000010834% colors are present, they will be quantized to the 4-4-4-1,
glennrp130fc452011-08-20 03:43:18 +000010835% 3-3-3-1, or 3-3-2-1 palette. The underlying RGB color
10836% of any resulting fully-transparent pixels is changed to
10837% the image's background color.
glennrpe9637cb2011-03-24 16:34:37 +000010838%
10839% If you want better quantization or dithering of the colors
10840% or alpha than that, you need to do it before calling the
glennrp5a39f372011-02-25 04:52:16 +000010841% PNG encoder. The pixels contain 8-bit indices even if
10842% they could be represented with 1, 2, or 4 bits. Grayscale
cristy3ed852e2009-09-05 21:47:34 +000010843% images will be written as indexed PNG files even though the
glennrp5a39f372011-02-25 04:52:16 +000010844% PNG grayscale type might be slightly more efficient. Please
10845% note that writing to the PNG8 format may result in loss
10846% of color and alpha data.
cristy3ed852e2009-09-05 21:47:34 +000010847%
10848% o PNG24: An 8-bit per sample RGB PNG datastream is written. The tRNS
10849% chunk can be present to convey binary transparency by naming
glennrp5a39f372011-02-25 04:52:16 +000010850% one of the colors as transparent. The only loss incurred
10851% is reduction of sample depth to 8. If the image has more
10852% than one transparent color, has semitransparent pixels, or
10853% has an opaque pixel with the same RGB components as the
10854% transparent color, an image is not written.
cristy3ed852e2009-09-05 21:47:34 +000010855%
10856% o PNG32: An 8-bit per sample RGBA PNG is written. Partial
10857% transparency is permitted, i.e., the alpha sample for
10858% each pixel can have any value from 0 to 255. The alpha
glennrp0fe50b42010-11-16 03:52:51 +000010859% channel is present even if the image is fully opaque.
glennrp5a39f372011-02-25 04:52:16 +000010860% The only loss in data is the reduction of the sample depth
10861% to 8.
cristy3ed852e2009-09-05 21:47:34 +000010862%
10863% o -define: For more precise control of the PNG output, you can use the
10864% Image options "png:bit-depth" and "png:color-type". These
10865% can be set from the commandline with "-define" and also
glennrpbb8a7332010-11-13 15:17:35 +000010866% from the application programming interfaces. The options
10867% are case-independent and are converted to lowercase before
10868% being passed to this encoder.
cristy3ed852e2009-09-05 21:47:34 +000010869%
10870% png:color-type can be 0, 2, 3, 4, or 6.
10871%
10872% When png:color-type is 0 (Grayscale), png:bit-depth can
10873% be 1, 2, 4, 8, or 16.
10874%
10875% When png:color-type is 2 (RGB), png:bit-depth can
10876% be 8 or 16.
10877%
10878% When png:color-type is 3 (Indexed), png:bit-depth can
10879% be 1, 2, 4, or 8. This refers to the number of bits
10880% used to store the index. The color samples always have
10881% bit-depth 8 in indexed PNG files.
10882%
10883% When png:color-type is 4 (Gray-Matte) or 6 (RGB-Matte),
10884% png:bit-depth can be 8 or 16.
10885%
glennrp5a39f372011-02-25 04:52:16 +000010886% If the image cannot be written without loss with the requested bit-depth
10887% and color-type, a PNG file will not be written, and the encoder will
10888% return MagickFalse.
10889%
cristy3ed852e2009-09-05 21:47:34 +000010890% Since image encoders should not be responsible for the "heavy lifting",
10891% the user should make sure that ImageMagick has already reduced the
10892% image depth and number of colors and limit transparency to binary
glennrp5a39f372011-02-25 04:52:16 +000010893% transparency prior to attempting to write the image with depth, color,
glennrp54dc0692011-07-01 19:02:13 +000010894% or transparency limitations.
cristy3ed852e2009-09-05 21:47:34 +000010895%
cristy3ed852e2009-09-05 21:47:34 +000010896% Note that another definition, "png:bit-depth-written" exists, but it
10897% is not intended for external use. It is only used internally by the
10898% PNG encoder to inform the JNG encoder of the depth of the alpha channel.
10899%
10900% It is possible to request that the PNG encoder write previously-formatted
10901% ancillary chunks in the output PNG file, using the "-profile" commandline
10902% option as shown below or by setting the profile via a programming
10903% interface:
10904%
10905% -profile PNG-chunk-x:<file>
10906%
10907% where x is a location flag and <file> is a file containing the chunk
10908% name in the first 4 bytes, then a colon (":"), followed by the chunk data.
glennrpbb8a7332010-11-13 15:17:35 +000010909% This encoder will compute the chunk length and CRC, so those must not
10910% be included in the file.
cristy3ed852e2009-09-05 21:47:34 +000010911%
10912% "x" can be "b" (before PLTE), "m" (middle, i.e., between PLTE and IDAT),
10913% or "e" (end, i.e., after IDAT). If you want to write multiple chunks
10914% of the same type, then add a short unique string after the "x" to prevent
glennrpbb8a7332010-11-13 15:17:35 +000010915% subsequent profiles from overwriting the preceding ones, e.g.,
cristy3ed852e2009-09-05 21:47:34 +000010916%
glennrpbb8a7332010-11-13 15:17:35 +000010917% -profile PNG-chunk-b01:file01 -profile PNG-chunk-b02:file02
cristy3ed852e2009-09-05 21:47:34 +000010918%
glennrp3241bd02010-12-12 04:36:28 +000010919% As of version 6.6.6 the following optimizations are always done:
glennrp0fe50b42010-11-16 03:52:51 +000010920%
glennrpd6afd542010-11-19 01:53:05 +000010921% o 32-bit depth is reduced to 16.
10922% o 16-bit depth is reduced to 8 if all pixels contain samples whose
10923% high byte and low byte are identical.
glennrp0fe50b42010-11-16 03:52:51 +000010924% o Palette is sorted to remove unused entries and to put a
glennrpcf002022011-01-30 02:38:15 +000010925% transparent color first, if BUILD_PNG_PALETTE is defined.
glennrp0fe50b42010-11-16 03:52:51 +000010926% o Opaque matte channel is removed when writing an indexed PNG.
glennrpd6afd542010-11-19 01:53:05 +000010927% o Grayscale images are reduced to 1, 2, or 4 bit depth if
10928% this can be done without loss and a larger bit depth N was not
cristy5d6fc9c2011-12-27 03:10:42 +000010929% requested via the "-define png:bit-depth=N" option.
glennrpd6afd542010-11-19 01:53:05 +000010930% o If matte channel is present but only one transparent color is
10931% present, RGB+tRNS is written instead of RGBA
10932% o Opaque matte channel is removed (or added, if color-type 4 or 6
10933% was requested when converting an opaque image).
glennrp0fe50b42010-11-16 03:52:51 +000010934%
cristy3ed852e2009-09-05 21:47:34 +000010935%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
10936*/
10937static MagickBooleanType WritePNGImage(const ImageInfo *image_info,
cristy1e178e72011-08-28 19:44:34 +000010938 Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +000010939{
10940 MagickBooleanType
glennrp21f0e622011-01-07 16:20:57 +000010941 excluding,
10942 logging,
10943 have_mng_structure,
cristy3ed852e2009-09-05 21:47:34 +000010944 status;
10945
10946 MngInfo
10947 *mng_info;
10948
10949 const char
10950 *value;
10951
10952 int
glennrp21f0e622011-01-07 16:20:57 +000010953 i,
glennrp5c7cf4e2010-12-24 00:30:00 +000010954 source;
10955
cristy3ed852e2009-09-05 21:47:34 +000010956 /*
10957 Open image file.
10958 */
10959 assert(image_info != (const ImageInfo *) NULL);
10960 assert(image_info->signature == MagickSignature);
10961 assert(image != (Image *) NULL);
10962 assert(image->signature == MagickSignature);
10963 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
glennrpfd05d622011-02-25 04:10:33 +000010964 logging=LogMagickEvent(CoderEvent,GetMagickModule(),"Enter WritePNGImage()");
cristy3ed852e2009-09-05 21:47:34 +000010965 /*
10966 Allocate a MngInfo structure.
10967 */
10968 have_mng_structure=MagickFalse;
cristy73bd4a52010-10-05 11:24:23 +000010969 mng_info=(MngInfo *) AcquireMagickMemory(sizeof(MngInfo));
glennrp0fe50b42010-11-16 03:52:51 +000010970
cristy3ed852e2009-09-05 21:47:34 +000010971 if (mng_info == (MngInfo *) NULL)
10972 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
glennrp0fe50b42010-11-16 03:52:51 +000010973
cristy3ed852e2009-09-05 21:47:34 +000010974 /*
10975 Initialize members of the MngInfo structure.
10976 */
10977 (void) ResetMagickMemory(mng_info,0,sizeof(MngInfo));
10978 mng_info->image=image;
glennrpa521b2f2010-10-29 04:11:03 +000010979 mng_info->equal_backgrounds=MagickTrue;
cristy3ed852e2009-09-05 21:47:34 +000010980 have_mng_structure=MagickTrue;
10981
10982 /* See if user has requested a specific PNG subformat */
10983
10984 mng_info->write_png8=LocaleCompare(image_info->magick,"PNG8") == 0;
10985 mng_info->write_png24=LocaleCompare(image_info->magick,"PNG24") == 0;
10986 mng_info->write_png32=LocaleCompare(image_info->magick,"PNG32") == 0;
10987
10988 if (mng_info->write_png8)
10989 {
glennrp9c1eb072010-06-06 22:19:15 +000010990 mng_info->write_png_colortype = /* 3 */ 4;
10991 mng_info->write_png_depth = 8;
10992 image->depth = 8;
cristy3ed852e2009-09-05 21:47:34 +000010993 }
10994
10995 if (mng_info->write_png24)
10996 {
glennrp9c1eb072010-06-06 22:19:15 +000010997 mng_info->write_png_colortype = /* 2 */ 3;
10998 mng_info->write_png_depth = 8;
10999 image->depth = 8;
glennrp0fe50b42010-11-16 03:52:51 +000011000
glennrp9c1eb072010-06-06 22:19:15 +000011001 if (image->matte == MagickTrue)
cristy018f07f2011-09-04 21:15:19 +000011002 (void) SetImageType(image,TrueColorMatteType,exception);
glennrp0fe50b42010-11-16 03:52:51 +000011003
glennrp9c1eb072010-06-06 22:19:15 +000011004 else
cristy018f07f2011-09-04 21:15:19 +000011005 (void) SetImageType(image,TrueColorType,exception);
glennrp0fe50b42010-11-16 03:52:51 +000011006
cristyea1a8aa2011-10-20 13:24:06 +000011007 (void) SyncImage(image,exception);
cristy3ed852e2009-09-05 21:47:34 +000011008 }
11009
11010 if (mng_info->write_png32)
11011 {
glennrp9c1eb072010-06-06 22:19:15 +000011012 mng_info->write_png_colortype = /* 6 */ 7;
11013 mng_info->write_png_depth = 8;
11014 image->depth = 8;
glennrp0fe50b42010-11-16 03:52:51 +000011015
glennrp9c1eb072010-06-06 22:19:15 +000011016 if (image->matte == MagickTrue)
cristy018f07f2011-09-04 21:15:19 +000011017 (void) SetImageType(image,TrueColorMatteType,exception);
glennrp0fe50b42010-11-16 03:52:51 +000011018
glennrp9c1eb072010-06-06 22:19:15 +000011019 else
cristy018f07f2011-09-04 21:15:19 +000011020 (void) SetImageType(image,TrueColorType,exception);
glennrp0fe50b42010-11-16 03:52:51 +000011021
cristyea1a8aa2011-10-20 13:24:06 +000011022 (void) SyncImage(image,exception);
cristy3ed852e2009-09-05 21:47:34 +000011023 }
11024
11025 value=GetImageOption(image_info,"png:bit-depth");
glennrp8bb3a022010-12-13 20:40:04 +000011026
cristy3ed852e2009-09-05 21:47:34 +000011027 if (value != (char *) NULL)
11028 {
11029 if (LocaleCompare(value,"1") == 0)
glennrp9c1eb072010-06-06 22:19:15 +000011030 mng_info->write_png_depth = 1;
glennrp0fe50b42010-11-16 03:52:51 +000011031
cristy3ed852e2009-09-05 21:47:34 +000011032 else if (LocaleCompare(value,"2") == 0)
glennrp9c1eb072010-06-06 22:19:15 +000011033 mng_info->write_png_depth = 2;
glennrp0fe50b42010-11-16 03:52:51 +000011034
cristy3ed852e2009-09-05 21:47:34 +000011035 else if (LocaleCompare(value,"4") == 0)
glennrp9c1eb072010-06-06 22:19:15 +000011036 mng_info->write_png_depth = 4;
glennrp0fe50b42010-11-16 03:52:51 +000011037
cristy3ed852e2009-09-05 21:47:34 +000011038 else if (LocaleCompare(value,"8") == 0)
glennrp9c1eb072010-06-06 22:19:15 +000011039 mng_info->write_png_depth = 8;
glennrp0fe50b42010-11-16 03:52:51 +000011040
cristy3ed852e2009-09-05 21:47:34 +000011041 else if (LocaleCompare(value,"16") == 0)
glennrp9c1eb072010-06-06 22:19:15 +000011042 mng_info->write_png_depth = 16;
glennrp0fe50b42010-11-16 03:52:51 +000011043
glennrpbb8a7332010-11-13 15:17:35 +000011044 else
cristyc82a27b2011-10-21 01:07:16 +000011045 (void) ThrowMagickException(exception,
glennrpbb8a7332010-11-13 15:17:35 +000011046 GetMagickModule(),CoderWarning,
11047 "ignoring invalid defined png:bit-depth",
11048 "=%s",value);
11049
cristy3ed852e2009-09-05 21:47:34 +000011050 if (logging != MagickFalse)
glennrp9c1eb072010-06-06 22:19:15 +000011051 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
glennrpbb8a7332010-11-13 15:17:35 +000011052 " png:bit-depth=%d was defined.\n",mng_info->write_png_depth);
cristy3ed852e2009-09-05 21:47:34 +000011053 }
glennrp0fe50b42010-11-16 03:52:51 +000011054
cristy3ed852e2009-09-05 21:47:34 +000011055 value=GetImageOption(image_info,"png:color-type");
glennrp0fe50b42010-11-16 03:52:51 +000011056
cristy3ed852e2009-09-05 21:47:34 +000011057 if (value != (char *) NULL)
11058 {
11059 /* We must store colortype+1 because 0 is a valid colortype */
11060 if (LocaleCompare(value,"0") == 0)
glennrp9c1eb072010-06-06 22:19:15 +000011061 mng_info->write_png_colortype = 1;
glennrp0fe50b42010-11-16 03:52:51 +000011062
cristy3ed852e2009-09-05 21:47:34 +000011063 else if (LocaleCompare(value,"2") == 0)
glennrp9c1eb072010-06-06 22:19:15 +000011064 mng_info->write_png_colortype = 3;
glennrp0fe50b42010-11-16 03:52:51 +000011065
cristy3ed852e2009-09-05 21:47:34 +000011066 else if (LocaleCompare(value,"3") == 0)
glennrp9c1eb072010-06-06 22:19:15 +000011067 mng_info->write_png_colortype = 4;
glennrp0fe50b42010-11-16 03:52:51 +000011068
cristy3ed852e2009-09-05 21:47:34 +000011069 else if (LocaleCompare(value,"4") == 0)
glennrp9c1eb072010-06-06 22:19:15 +000011070 mng_info->write_png_colortype = 5;
glennrp0fe50b42010-11-16 03:52:51 +000011071
cristy3ed852e2009-09-05 21:47:34 +000011072 else if (LocaleCompare(value,"6") == 0)
glennrp9c1eb072010-06-06 22:19:15 +000011073 mng_info->write_png_colortype = 7;
glennrp0fe50b42010-11-16 03:52:51 +000011074
glennrpbb8a7332010-11-13 15:17:35 +000011075 else
cristyc82a27b2011-10-21 01:07:16 +000011076 (void) ThrowMagickException(exception,
glennrpbb8a7332010-11-13 15:17:35 +000011077 GetMagickModule(),CoderWarning,
11078 "ignoring invalid defined png:color-type",
11079 "=%s",value);
11080
cristy3ed852e2009-09-05 21:47:34 +000011081 if (logging != MagickFalse)
glennrp9c1eb072010-06-06 22:19:15 +000011082 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
glennrpd6bf1612010-12-17 17:28:54 +000011083 " png:color-type=%d was defined.\n",mng_info->write_png_colortype-1);
cristy3ed852e2009-09-05 21:47:34 +000011084 }
11085
glennrp0e8ea192010-12-24 18:00:33 +000011086 /* Check for chunks to be excluded:
11087 *
glennrp0dff56c2011-01-29 19:10:02 +000011088 * The default is to not exclude any known chunks except for any
glennrp0e8ea192010-12-24 18:00:33 +000011089 * listed in the "unused_chunks" array, above.
11090 *
cristy5d6fc9c2011-12-27 03:10:42 +000011091 * Chunks can be listed for exclusion via a "png:exclude-chunk"
glennrp0e8ea192010-12-24 18:00:33 +000011092 * define (in the image properties or in the image artifacts)
11093 * or via a mng_info member. For convenience, in addition
11094 * to or instead of a comma-separated list of chunks, the
11095 * "exclude-chunk" string can be simply "all" or "none".
11096 *
11097 * The exclude-chunk define takes priority over the mng_info.
11098 *
cristy5d6fc9c2011-12-27 03:10:42 +000011099 * A "png:include-chunk" define takes priority over both the
11100 * mng_info and the "png:exclude-chunk" define. Like the
glennrp0e8ea192010-12-24 18:00:33 +000011101 * "exclude-chunk" string, it can define "all" or "none" as
glennrp0dff56c2011-01-29 19:10:02 +000011102 * well as a comma-separated list. Chunks that are unknown to
11103 * ImageMagick are always excluded, regardless of their "copy-safe"
11104 * status according to the PNG specification, and even if they
11105 * appear in the "include-chunk" list.
glennrp0e8ea192010-12-24 18:00:33 +000011106 *
11107 * Finally, all chunks listed in the "unused_chunks" array are
11108 * automatically excluded, regardless of the other instructions
11109 * or lack thereof.
11110 *
11111 * if you exclude sRGB but not gAMA (recommended), then sRGB chunk
11112 * will not be written and the gAMA chunk will only be written if it
11113 * is not between .45 and .46, or approximately (1.0/2.2).
11114 *
11115 * If you exclude tRNS and the image has transparency, the colortype
11116 * is forced to be 4 or 6 (GRAY_ALPHA or RGB_ALPHA).
11117 *
11118 * The -strip option causes StripImage() to set the png:include-chunk
glennrp104f2062011-08-20 05:08:53 +000011119 * artifact to "none,trns,gama".
glennrp0e8ea192010-12-24 18:00:33 +000011120 */
11121
glennrp26f37912010-12-23 16:22:42 +000011122 mng_info->ping_exclude_bKGD=MagickFalse;
11123 mng_info->ping_exclude_cHRM=MagickFalse;
glennrpa0ed0092011-04-18 16:36:29 +000011124 mng_info->ping_exclude_date=MagickFalse;
glennrp26f37912010-12-23 16:22:42 +000011125 mng_info->ping_exclude_EXIF=MagickFalse; /* hex-encoded EXIF in zTXt */
11126 mng_info->ping_exclude_gAMA=MagickFalse;
glennrp26f37912010-12-23 16:22:42 +000011127 mng_info->ping_exclude_iCCP=MagickFalse;
11128 /* mng_info->ping_exclude_iTXt=MagickFalse; */
11129 mng_info->ping_exclude_oFFs=MagickFalse;
11130 mng_info->ping_exclude_pHYs=MagickFalse;
11131 mng_info->ping_exclude_sRGB=MagickFalse;
11132 mng_info->ping_exclude_tEXt=MagickFalse;
glennrpa1e3b7b2010-12-24 16:37:33 +000011133 mng_info->ping_exclude_tRNS=MagickFalse;
glennrp26f37912010-12-23 16:22:42 +000011134 mng_info->ping_exclude_vpAg=MagickFalse;
11135 mng_info->ping_exclude_zCCP=MagickFalse; /* hex-encoded iCCP in zTXt */
11136 mng_info->ping_exclude_zTXt=MagickFalse;
11137
glennrp8d3d6e52011-04-19 04:39:51 +000011138 mng_info->ping_preserve_colormap=MagickFalse;
11139
11140 value=GetImageArtifact(image,"png:preserve-colormap");
11141 if (value == NULL)
11142 value=GetImageOption(image_info,"png:preserve-colormap");
11143 if (value != NULL)
11144 mng_info->ping_preserve_colormap=MagickTrue;
11145
glennrp18682582011-06-30 18:11:47 +000011146 /* Thes compression-level, compression-strategy, and compression-filter
11147 * defines take precedence over values from the -quality option.
11148 */
11149 value=GetImageArtifact(image,"png:compression-level");
11150 if (value == NULL)
11151 value=GetImageOption(image_info,"png:compression-level");
11152 if (value != NULL)
11153 {
glennrp18682582011-06-30 18:11:47 +000011154 /* We have to add 1 to everything because 0 is a valid input,
11155 * and we want to use 0 (the default) to mean undefined.
11156 */
11157 if (LocaleCompare(value,"0") == 0)
11158 mng_info->write_png_compression_level = 1;
11159
11160 if (LocaleCompare(value,"1") == 0)
11161 mng_info->write_png_compression_level = 2;
11162
11163 else if (LocaleCompare(value,"2") == 0)
11164 mng_info->write_png_compression_level = 3;
11165
11166 else if (LocaleCompare(value,"3") == 0)
11167 mng_info->write_png_compression_level = 4;
11168
11169 else if (LocaleCompare(value,"4") == 0)
11170 mng_info->write_png_compression_level = 5;
11171
11172 else if (LocaleCompare(value,"5") == 0)
11173 mng_info->write_png_compression_level = 6;
11174
11175 else if (LocaleCompare(value,"6") == 0)
11176 mng_info->write_png_compression_level = 7;
11177
11178 else if (LocaleCompare(value,"7") == 0)
11179 mng_info->write_png_compression_level = 8;
11180
11181 else if (LocaleCompare(value,"8") == 0)
11182 mng_info->write_png_compression_level = 9;
11183
11184 else if (LocaleCompare(value,"9") == 0)
11185 mng_info->write_png_compression_level = 10;
11186
11187 else
cristyc82a27b2011-10-21 01:07:16 +000011188 (void) ThrowMagickException(exception,
glennrp18682582011-06-30 18:11:47 +000011189 GetMagickModule(),CoderWarning,
11190 "ignoring invalid defined png:compression-level",
11191 "=%s",value);
11192 }
11193
11194 value=GetImageArtifact(image,"png:compression-strategy");
11195 if (value == NULL)
11196 value=GetImageOption(image_info,"png:compression-strategy");
11197 if (value != NULL)
11198 {
11199
11200 if (LocaleCompare(value,"0") == 0)
11201 mng_info->write_png_compression_strategy = Z_DEFAULT_STRATEGY+1;
11202
11203 else if (LocaleCompare(value,"1") == 0)
11204 mng_info->write_png_compression_strategy = Z_FILTERED+1;
11205
11206 else if (LocaleCompare(value,"2") == 0)
11207 mng_info->write_png_compression_strategy = Z_HUFFMAN_ONLY+1;
11208
11209 else if (LocaleCompare(value,"3") == 0)
glennrp98c07ad2011-07-01 02:52:59 +000011210#ifdef Z_RLE /* Z_RLE was added to zlib-1.2.0 */
glennrp18682582011-06-30 18:11:47 +000011211 mng_info->write_png_compression_strategy = Z_RLE+1;
glennrp98c07ad2011-07-01 02:52:59 +000011212#else
11213 mng_info->write_png_compression_strategy = Z_DEFAULT_STRATEGY+1;
11214#endif
glennrp18682582011-06-30 18:11:47 +000011215
11216 else if (LocaleCompare(value,"4") == 0)
glennrp98c07ad2011-07-01 02:52:59 +000011217#ifdef Z_FIXED /* Z_FIXED was added to zlib-1.2.2.2 */
glennrp18682582011-06-30 18:11:47 +000011218 mng_info->write_png_compression_strategy = Z_FIXED+1;
glennrp98c07ad2011-07-01 02:52:59 +000011219#else
11220 mng_info->write_png_compression_strategy = Z_DEFAULT_STRATEGY+1;
11221#endif
glennrp18682582011-06-30 18:11:47 +000011222
11223 else
cristyc82a27b2011-10-21 01:07:16 +000011224 (void) ThrowMagickException(exception,
glennrp18682582011-06-30 18:11:47 +000011225 GetMagickModule(),CoderWarning,
11226 "ignoring invalid defined png:compression-strategy",
11227 "=%s",value);
11228 }
11229
11230 value=GetImageArtifact(image,"png:compression-filter");
11231 if (value == NULL)
11232 value=GetImageOption(image_info,"png:compression-filter");
11233 if (value != NULL)
11234 {
11235
11236 /* To do: combinations of filters allowed by libpng
11237 * masks 0x08 through 0xf8
11238 *
11239 * Implement this as a comma-separated list of 0,1,2,3,4,5
11240 * where 5 is a special case meaning PNG_ALL_FILTERS.
11241 */
11242
11243 if (LocaleCompare(value,"0") == 0)
11244 mng_info->write_png_compression_filter = 1;
11245
11246 if (LocaleCompare(value,"1") == 0)
11247 mng_info->write_png_compression_filter = 2;
11248
11249 else if (LocaleCompare(value,"2") == 0)
11250 mng_info->write_png_compression_filter = 3;
11251
11252 else if (LocaleCompare(value,"3") == 0)
11253 mng_info->write_png_compression_filter = 4;
11254
11255 else if (LocaleCompare(value,"4") == 0)
11256 mng_info->write_png_compression_filter = 5;
11257
11258 else if (LocaleCompare(value,"5") == 0)
11259 mng_info->write_png_compression_filter = 6;
11260
glennrp18682582011-06-30 18:11:47 +000011261 else
cristyc82a27b2011-10-21 01:07:16 +000011262 (void) ThrowMagickException(exception,
glennrp18682582011-06-30 18:11:47 +000011263 GetMagickModule(),CoderWarning,
11264 "ignoring invalid defined png:compression-filter",
11265 "=%s",value);
11266 }
11267
glennrp03812ae2010-12-24 01:31:34 +000011268 excluding=MagickFalse;
11269
glennrp5c7cf4e2010-12-24 00:30:00 +000011270 for (source=0; source<1; source++)
11271 {
11272 if (source==0)
glennrpacba0042010-12-24 14:27:26 +000011273 {
glennrp5c7cf4e2010-12-24 00:30:00 +000011274 value=GetImageArtifact(image,"png:exclude-chunk");
glennrpacba0042010-12-24 14:27:26 +000011275
11276 if (value == NULL)
11277 value=GetImageArtifact(image,"png:exclude-chunks");
11278 }
glennrp5c7cf4e2010-12-24 00:30:00 +000011279 else
glennrpacba0042010-12-24 14:27:26 +000011280 {
glennrp5c7cf4e2010-12-24 00:30:00 +000011281 value=GetImageOption(image_info,"png:exclude-chunk");
glennrp26f37912010-12-23 16:22:42 +000011282
glennrpacba0042010-12-24 14:27:26 +000011283 if (value == NULL)
11284 value=GetImageOption(image_info,"png:exclude-chunks");
11285 }
11286
glennrp03812ae2010-12-24 01:31:34 +000011287 if (value != NULL)
glennrpce91ed52010-12-23 22:37:49 +000011288 {
glennrp03812ae2010-12-24 01:31:34 +000011289
11290 size_t
11291 last;
11292
11293 excluding=MagickTrue;
11294
11295 if (logging != MagickFalse)
glennrp2cc891a2010-12-24 13:44:32 +000011296 {
11297 if (source == 0)
11298 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
11299 " png:exclude-chunk=%s found in image artifacts.\n", value);
11300 else
11301 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
11302 " png:exclude-chunk=%s found in image properties.\n", value);
11303 }
glennrp03812ae2010-12-24 01:31:34 +000011304
11305 last=strlen(value);
11306
11307 for (i=0; i<(int) last; i+=5)
glennrpce91ed52010-12-23 22:37:49 +000011308 {
glennrp03812ae2010-12-24 01:31:34 +000011309
11310 if (LocaleNCompare(value+i,"all",3) == 0)
11311 {
11312 mng_info->ping_exclude_bKGD=MagickTrue;
11313 mng_info->ping_exclude_cHRM=MagickTrue;
glennrpa0ed0092011-04-18 16:36:29 +000011314 mng_info->ping_exclude_date=MagickTrue;
glennrp03812ae2010-12-24 01:31:34 +000011315 mng_info->ping_exclude_EXIF=MagickTrue;
11316 mng_info->ping_exclude_gAMA=MagickTrue;
11317 mng_info->ping_exclude_iCCP=MagickTrue;
11318 /* mng_info->ping_exclude_iTXt=MagickTrue; */
11319 mng_info->ping_exclude_oFFs=MagickTrue;
11320 mng_info->ping_exclude_pHYs=MagickTrue;
11321 mng_info->ping_exclude_sRGB=MagickTrue;
11322 mng_info->ping_exclude_tEXt=MagickTrue;
glennrpa1e3b7b2010-12-24 16:37:33 +000011323 mng_info->ping_exclude_tRNS=MagickTrue;
glennrp03812ae2010-12-24 01:31:34 +000011324 mng_info->ping_exclude_vpAg=MagickTrue;
11325 mng_info->ping_exclude_zCCP=MagickTrue;
11326 mng_info->ping_exclude_zTXt=MagickTrue;
11327 i--;
11328 }
glennrp2cc891a2010-12-24 13:44:32 +000011329
glennrp03812ae2010-12-24 01:31:34 +000011330 if (LocaleNCompare(value+i,"none",4) == 0)
11331 {
11332 mng_info->ping_exclude_bKGD=MagickFalse;
11333 mng_info->ping_exclude_cHRM=MagickFalse;
glennrpa0ed0092011-04-18 16:36:29 +000011334 mng_info->ping_exclude_date=MagickFalse;
glennrp03812ae2010-12-24 01:31:34 +000011335 mng_info->ping_exclude_EXIF=MagickFalse;
11336 mng_info->ping_exclude_gAMA=MagickFalse;
11337 mng_info->ping_exclude_iCCP=MagickFalse;
11338 /* mng_info->ping_exclude_iTXt=MagickFalse; */
11339 mng_info->ping_exclude_oFFs=MagickFalse;
11340 mng_info->ping_exclude_pHYs=MagickFalse;
11341 mng_info->ping_exclude_sRGB=MagickFalse;
11342 mng_info->ping_exclude_tEXt=MagickFalse;
glennrpa1e3b7b2010-12-24 16:37:33 +000011343 mng_info->ping_exclude_tRNS=MagickFalse;
glennrp03812ae2010-12-24 01:31:34 +000011344 mng_info->ping_exclude_vpAg=MagickFalse;
11345 mng_info->ping_exclude_zCCP=MagickFalse;
11346 mng_info->ping_exclude_zTXt=MagickFalse;
11347 }
glennrp2cc891a2010-12-24 13:44:32 +000011348
glennrp03812ae2010-12-24 01:31:34 +000011349 if (LocaleNCompare(value+i,"bkgd",4) == 0)
11350 mng_info->ping_exclude_bKGD=MagickTrue;
glennrp2cc891a2010-12-24 13:44:32 +000011351
glennrp03812ae2010-12-24 01:31:34 +000011352 if (LocaleNCompare(value+i,"chrm",4) == 0)
11353 mng_info->ping_exclude_cHRM=MagickTrue;
glennrp2cc891a2010-12-24 13:44:32 +000011354
glennrpa0ed0092011-04-18 16:36:29 +000011355 if (LocaleNCompare(value+i,"date",4) == 0)
11356 mng_info->ping_exclude_date=MagickTrue;
11357
glennrp03812ae2010-12-24 01:31:34 +000011358 if (LocaleNCompare(value+i,"exif",4) == 0)
11359 mng_info->ping_exclude_EXIF=MagickTrue;
glennrp2cc891a2010-12-24 13:44:32 +000011360
glennrp03812ae2010-12-24 01:31:34 +000011361 if (LocaleNCompare(value+i,"gama",4) == 0)
11362 mng_info->ping_exclude_gAMA=MagickTrue;
glennrp2cc891a2010-12-24 13:44:32 +000011363
glennrp03812ae2010-12-24 01:31:34 +000011364 if (LocaleNCompare(value+i,"iccp",4) == 0)
11365 mng_info->ping_exclude_iCCP=MagickTrue;
glennrp2cc891a2010-12-24 13:44:32 +000011366
glennrp03812ae2010-12-24 01:31:34 +000011367 /*
11368 if (LocaleNCompare(value+i,"itxt",4) == 0)
11369 mng_info->ping_exclude_iTXt=MagickTrue;
11370 */
glennrp2cc891a2010-12-24 13:44:32 +000011371
glennrp03812ae2010-12-24 01:31:34 +000011372 if (LocaleNCompare(value+i,"gama",4) == 0)
11373 mng_info->ping_exclude_gAMA=MagickTrue;
glennrp2cc891a2010-12-24 13:44:32 +000011374
glennrp03812ae2010-12-24 01:31:34 +000011375 if (LocaleNCompare(value+i,"offs",4) == 0)
11376 mng_info->ping_exclude_oFFs=MagickTrue;
glennrp2cc891a2010-12-24 13:44:32 +000011377
glennrp03812ae2010-12-24 01:31:34 +000011378 if (LocaleNCompare(value+i,"phys",4) == 0)
11379 mng_info->ping_exclude_pHYs=MagickTrue;
glennrp2cc891a2010-12-24 13:44:32 +000011380
glennrpa1e3b7b2010-12-24 16:37:33 +000011381 if (LocaleNCompare(value+i,"srgb",4) == 0)
11382 mng_info->ping_exclude_sRGB=MagickTrue;
glennrp2cc891a2010-12-24 13:44:32 +000011383
glennrp03812ae2010-12-24 01:31:34 +000011384 if (LocaleNCompare(value+i,"text",4) == 0)
11385 mng_info->ping_exclude_tEXt=MagickTrue;
glennrp2cc891a2010-12-24 13:44:32 +000011386
glennrpa1e3b7b2010-12-24 16:37:33 +000011387 if (LocaleNCompare(value+i,"trns",4) == 0)
11388 mng_info->ping_exclude_tRNS=MagickTrue;
11389
glennrp03812ae2010-12-24 01:31:34 +000011390 if (LocaleNCompare(value+i,"vpag",4) == 0)
11391 mng_info->ping_exclude_vpAg=MagickTrue;
glennrp2cc891a2010-12-24 13:44:32 +000011392
glennrp03812ae2010-12-24 01:31:34 +000011393 if (LocaleNCompare(value+i,"zccp",4) == 0)
11394 mng_info->ping_exclude_zCCP=MagickTrue;
glennrp2cc891a2010-12-24 13:44:32 +000011395
glennrp03812ae2010-12-24 01:31:34 +000011396 if (LocaleNCompare(value+i,"ztxt",4) == 0)
11397 mng_info->ping_exclude_zTXt=MagickTrue;
glennrp2cc891a2010-12-24 13:44:32 +000011398
glennrp03812ae2010-12-24 01:31:34 +000011399 }
glennrpce91ed52010-12-23 22:37:49 +000011400 }
glennrp26f37912010-12-23 16:22:42 +000011401 }
11402
glennrp5c7cf4e2010-12-24 00:30:00 +000011403 for (source=0; source<1; source++)
11404 {
11405 if (source==0)
glennrpacba0042010-12-24 14:27:26 +000011406 {
glennrp5c7cf4e2010-12-24 00:30:00 +000011407 value=GetImageArtifact(image,"png:include-chunk");
glennrpacba0042010-12-24 14:27:26 +000011408
11409 if (value == NULL)
11410 value=GetImageArtifact(image,"png:include-chunks");
11411 }
glennrp5c7cf4e2010-12-24 00:30:00 +000011412 else
glennrpacba0042010-12-24 14:27:26 +000011413 {
glennrp5c7cf4e2010-12-24 00:30:00 +000011414 value=GetImageOption(image_info,"png:include-chunk");
glennrp26f37912010-12-23 16:22:42 +000011415
glennrpacba0042010-12-24 14:27:26 +000011416 if (value == NULL)
11417 value=GetImageOption(image_info,"png:include-chunks");
11418 }
11419
glennrp03812ae2010-12-24 01:31:34 +000011420 if (value != NULL)
11421 {
11422 size_t
11423 last;
glennrp26f37912010-12-23 16:22:42 +000011424
glennrp03812ae2010-12-24 01:31:34 +000011425 excluding=MagickTrue;
glennrp26f37912010-12-23 16:22:42 +000011426
glennrp03812ae2010-12-24 01:31:34 +000011427 if (logging != MagickFalse)
glennrp2cc891a2010-12-24 13:44:32 +000011428 {
11429 if (source == 0)
11430 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
11431 " png:include-chunk=%s found in image artifacts.\n", value);
11432 else
11433 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
11434 " png:include-chunk=%s found in image properties.\n", value);
11435 }
glennrp03812ae2010-12-24 01:31:34 +000011436
11437 last=strlen(value);
11438
11439 for (i=0; i<(int) last; i+=5)
11440 {
11441 if (LocaleNCompare(value+i,"all",3) == 0)
11442 {
11443 mng_info->ping_exclude_bKGD=MagickFalse;
11444 mng_info->ping_exclude_cHRM=MagickFalse;
glennrpa0ed0092011-04-18 16:36:29 +000011445 mng_info->ping_exclude_date=MagickFalse;
glennrp03812ae2010-12-24 01:31:34 +000011446 mng_info->ping_exclude_EXIF=MagickFalse;
11447 mng_info->ping_exclude_gAMA=MagickFalse;
11448 mng_info->ping_exclude_iCCP=MagickFalse;
11449 /* mng_info->ping_exclude_iTXt=MagickFalse; */
11450 mng_info->ping_exclude_oFFs=MagickFalse;
11451 mng_info->ping_exclude_pHYs=MagickFalse;
11452 mng_info->ping_exclude_sRGB=MagickFalse;
11453 mng_info->ping_exclude_tEXt=MagickFalse;
glennrpa1e3b7b2010-12-24 16:37:33 +000011454 mng_info->ping_exclude_tRNS=MagickFalse;
glennrp03812ae2010-12-24 01:31:34 +000011455 mng_info->ping_exclude_vpAg=MagickFalse;
11456 mng_info->ping_exclude_zCCP=MagickFalse;
11457 mng_info->ping_exclude_zTXt=MagickFalse;
11458 i--;
11459 }
glennrp2cc891a2010-12-24 13:44:32 +000011460
glennrp03812ae2010-12-24 01:31:34 +000011461 if (LocaleNCompare(value+i,"none",4) == 0)
11462 {
11463 mng_info->ping_exclude_bKGD=MagickTrue;
11464 mng_info->ping_exclude_cHRM=MagickTrue;
glennrpa0ed0092011-04-18 16:36:29 +000011465 mng_info->ping_exclude_date=MagickTrue;
glennrp03812ae2010-12-24 01:31:34 +000011466 mng_info->ping_exclude_EXIF=MagickTrue;
11467 mng_info->ping_exclude_gAMA=MagickTrue;
11468 mng_info->ping_exclude_iCCP=MagickTrue;
11469 /* mng_info->ping_exclude_iTXt=MagickTrue; */
11470 mng_info->ping_exclude_oFFs=MagickTrue;
11471 mng_info->ping_exclude_pHYs=MagickTrue;
11472 mng_info->ping_exclude_sRGB=MagickTrue;
11473 mng_info->ping_exclude_tEXt=MagickTrue;
glennrpa1e3b7b2010-12-24 16:37:33 +000011474 mng_info->ping_exclude_tRNS=MagickTrue;
glennrp03812ae2010-12-24 01:31:34 +000011475 mng_info->ping_exclude_vpAg=MagickTrue;
11476 mng_info->ping_exclude_zCCP=MagickTrue;
11477 mng_info->ping_exclude_zTXt=MagickTrue;
11478 }
glennrp2cc891a2010-12-24 13:44:32 +000011479
glennrp03812ae2010-12-24 01:31:34 +000011480 if (LocaleNCompare(value+i,"bkgd",4) == 0)
11481 mng_info->ping_exclude_bKGD=MagickFalse;
glennrp2cc891a2010-12-24 13:44:32 +000011482
glennrp03812ae2010-12-24 01:31:34 +000011483 if (LocaleNCompare(value+i,"chrm",4) == 0)
11484 mng_info->ping_exclude_cHRM=MagickFalse;
glennrp2cc891a2010-12-24 13:44:32 +000011485
glennrpa0ed0092011-04-18 16:36:29 +000011486 if (LocaleNCompare(value+i,"date",4) == 0)
11487 mng_info->ping_exclude_date=MagickFalse;
11488
glennrp03812ae2010-12-24 01:31:34 +000011489 if (LocaleNCompare(value+i,"exif",4) == 0)
11490 mng_info->ping_exclude_EXIF=MagickFalse;
glennrp2cc891a2010-12-24 13:44:32 +000011491
glennrp03812ae2010-12-24 01:31:34 +000011492 if (LocaleNCompare(value+i,"gama",4) == 0)
11493 mng_info->ping_exclude_gAMA=MagickFalse;
glennrp2cc891a2010-12-24 13:44:32 +000011494
glennrp03812ae2010-12-24 01:31:34 +000011495 if (LocaleNCompare(value+i,"iccp",4) == 0)
11496 mng_info->ping_exclude_iCCP=MagickFalse;
glennrp2cc891a2010-12-24 13:44:32 +000011497
glennrp03812ae2010-12-24 01:31:34 +000011498 /*
11499 if (LocaleNCompare(value+i,"itxt",4) == 0)
11500 mng_info->ping_exclude_iTXt=MagickFalse;
11501 */
glennrp2cc891a2010-12-24 13:44:32 +000011502
glennrp03812ae2010-12-24 01:31:34 +000011503 if (LocaleNCompare(value+i,"gama",4) == 0)
11504 mng_info->ping_exclude_gAMA=MagickFalse;
glennrp2cc891a2010-12-24 13:44:32 +000011505
glennrp03812ae2010-12-24 01:31:34 +000011506 if (LocaleNCompare(value+i,"offs",4) == 0)
11507 mng_info->ping_exclude_oFFs=MagickFalse;
glennrp2cc891a2010-12-24 13:44:32 +000011508
glennrp03812ae2010-12-24 01:31:34 +000011509 if (LocaleNCompare(value+i,"phys",4) == 0)
11510 mng_info->ping_exclude_pHYs=MagickFalse;
glennrp2cc891a2010-12-24 13:44:32 +000011511
glennrpa1e3b7b2010-12-24 16:37:33 +000011512 if (LocaleNCompare(value+i,"srgb",4) == 0)
11513 mng_info->ping_exclude_sRGB=MagickFalse;
glennrp2cc891a2010-12-24 13:44:32 +000011514
glennrp03812ae2010-12-24 01:31:34 +000011515 if (LocaleNCompare(value+i,"text",4) == 0)
11516 mng_info->ping_exclude_tEXt=MagickFalse;
glennrp2cc891a2010-12-24 13:44:32 +000011517
glennrpa1e3b7b2010-12-24 16:37:33 +000011518 if (LocaleNCompare(value+i,"trns",4) == 0)
11519 mng_info->ping_exclude_tRNS=MagickFalse;
11520
glennrp03812ae2010-12-24 01:31:34 +000011521 if (LocaleNCompare(value+i,"vpag",4) == 0)
11522 mng_info->ping_exclude_vpAg=MagickFalse;
glennrp2cc891a2010-12-24 13:44:32 +000011523
glennrp03812ae2010-12-24 01:31:34 +000011524 if (LocaleNCompare(value+i,"zccp",4) == 0)
11525 mng_info->ping_exclude_zCCP=MagickFalse;
glennrp2cc891a2010-12-24 13:44:32 +000011526
glennrp03812ae2010-12-24 01:31:34 +000011527 if (LocaleNCompare(value+i,"ztxt",4) == 0)
11528 mng_info->ping_exclude_zTXt=MagickFalse;
glennrp2cc891a2010-12-24 13:44:32 +000011529
glennrp03812ae2010-12-24 01:31:34 +000011530 }
glennrpce91ed52010-12-23 22:37:49 +000011531 }
glennrp26f37912010-12-23 16:22:42 +000011532 }
11533
glennrp03812ae2010-12-24 01:31:34 +000011534 if (excluding != MagickFalse && logging != MagickFalse)
glennrp26f37912010-12-23 16:22:42 +000011535 {
11536 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristy5d6fc9c2011-12-27 03:10:42 +000011537 " Chunks to be excluded from the output png:");
glennrp26f37912010-12-23 16:22:42 +000011538 if (mng_info->ping_exclude_bKGD != MagickFalse)
11539 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
11540 " bKGD");
11541 if (mng_info->ping_exclude_cHRM != MagickFalse)
11542 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
11543 " cHRM");
glennrpa0ed0092011-04-18 16:36:29 +000011544 if (mng_info->ping_exclude_date != MagickFalse)
11545 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
11546 " date");
glennrp26f37912010-12-23 16:22:42 +000011547 if (mng_info->ping_exclude_EXIF != MagickFalse)
11548 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
11549 " EXIF");
11550 if (mng_info->ping_exclude_gAMA != MagickFalse)
11551 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
11552 " gAMA");
11553 if (mng_info->ping_exclude_iCCP != MagickFalse)
11554 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
11555 " iCCP");
11556/*
11557 if (mng_info->ping_exclude_iTXt != MagickFalse)
11558 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
11559 " iTXt");
11560*/
11561 if (mng_info->ping_exclude_oFFs != MagickFalse)
11562 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
11563 " oFFs");
11564 if (mng_info->ping_exclude_pHYs != MagickFalse)
11565 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
11566 " pHYs");
11567 if (mng_info->ping_exclude_sRGB != MagickFalse)
11568 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
11569 " sRGB");
11570 if (mng_info->ping_exclude_tEXt != MagickFalse)
11571 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
11572 " tEXt");
glennrpa1e3b7b2010-12-24 16:37:33 +000011573 if (mng_info->ping_exclude_tRNS != MagickFalse)
11574 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
11575 " tRNS");
glennrp26f37912010-12-23 16:22:42 +000011576 if (mng_info->ping_exclude_vpAg != MagickFalse)
11577 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
11578 " vpAg");
11579 if (mng_info->ping_exclude_zCCP != MagickFalse)
11580 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
11581 " zCCP");
11582 if (mng_info->ping_exclude_zTXt != MagickFalse)
11583 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
11584 " zTXt");
11585 }
11586
glennrpb9cfe272010-12-21 15:08:06 +000011587 mng_info->need_blob = MagickTrue;
cristy3ed852e2009-09-05 21:47:34 +000011588
cristy018f07f2011-09-04 21:15:19 +000011589 status=WriteOnePNGImage(mng_info,image_info,image,exception);
cristy3ed852e2009-09-05 21:47:34 +000011590
11591 MngInfoFreeStruct(mng_info,&have_mng_structure);
glennrp0fe50b42010-11-16 03:52:51 +000011592
cristy3ed852e2009-09-05 21:47:34 +000011593 if (logging != MagickFalse)
11594 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"exit WritePNGImage()");
glennrp0fe50b42010-11-16 03:52:51 +000011595
cristy3ed852e2009-09-05 21:47:34 +000011596 return(status);
11597}
11598
11599#if defined(JNG_SUPPORTED)
11600
11601/* Write one JNG image */
11602static MagickBooleanType WriteOneJNGImage(MngInfo *mng_info,
cristy018f07f2011-09-04 21:15:19 +000011603 const ImageInfo *image_info,Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +000011604{
11605 Image
11606 *jpeg_image;
11607
11608 ImageInfo
11609 *jpeg_image_info;
11610
11611 MagickBooleanType
glennrp03812ae2010-12-24 01:31:34 +000011612 logging,
cristy3ed852e2009-09-05 21:47:34 +000011613 status;
11614
11615 size_t
11616 length;
11617
11618 unsigned char
11619 *blob,
11620 chunk[80],
11621 *p;
11622
11623 unsigned int
11624 jng_alpha_compression_method,
11625 jng_alpha_sample_depth,
11626 jng_color_type,
cristy3ed852e2009-09-05 21:47:34 +000011627 transparent;
11628
cristybb503372010-05-27 20:51:26 +000011629 size_t
glennrp59575fa2011-12-31 21:31:39 +000011630 jng_alpha_quality,
cristy3ed852e2009-09-05 21:47:34 +000011631 jng_quality;
11632
11633 logging=LogMagickEvent(CoderEvent,GetMagickModule(),
glennrpfd05d622011-02-25 04:10:33 +000011634 " Enter WriteOneJNGImage()");
cristy3ed852e2009-09-05 21:47:34 +000011635
11636 blob=(unsigned char *) NULL;
11637 jpeg_image=(Image *) NULL;
11638 jpeg_image_info=(ImageInfo *) NULL;
11639
11640 status=MagickTrue;
11641 transparent=image_info->type==GrayscaleMatteType ||
glennrp59575fa2011-12-31 21:31:39 +000011642 image_info->type==TrueColorMatteType || image->matte != MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +000011643
glennrp59575fa2011-12-31 21:31:39 +000011644 jng_quality=image_info->quality == 0UL ? 75UL : image_info->quality%1000;
11645
11646 jng_alpha_compression_method=image->compression==JPEGCompression? 8 : 0;
11647
11648 jng_alpha_quality=image_info->quality == 0UL ? 75UL :
11649 image_info->quality;
11650
11651 if (jng_alpha_quality >= 1000)
11652 jng_alpha_quality /= 1000;
cristy3ed852e2009-09-05 21:47:34 +000011653
11654 if (transparent)
11655 {
cristybd5a96c2011-08-21 00:04:26 +000011656 ChannelType
11657 channel_mask;
11658
cristy3ed852e2009-09-05 21:47:34 +000011659 jng_color_type=14;
glennrp0fe50b42010-11-16 03:52:51 +000011660
cristy3ed852e2009-09-05 21:47:34 +000011661 /* Create JPEG blob, image, and image_info */
11662 if (logging != MagickFalse)
11663 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristy4c08aed2011-07-01 19:47:50 +000011664 " Creating jpeg_image_info for alpha.");
glennrp0fe50b42010-11-16 03:52:51 +000011665
cristy3ed852e2009-09-05 21:47:34 +000011666 jpeg_image_info=(ImageInfo *) CloneImageInfo(image_info);
glennrp0fe50b42010-11-16 03:52:51 +000011667
cristy3ed852e2009-09-05 21:47:34 +000011668 if (jpeg_image_info == (ImageInfo *) NULL)
11669 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
glennrp0fe50b42010-11-16 03:52:51 +000011670
cristy3ed852e2009-09-05 21:47:34 +000011671 if (logging != MagickFalse)
11672 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
11673 " Creating jpeg_image.");
glennrp0fe50b42010-11-16 03:52:51 +000011674
cristyc82a27b2011-10-21 01:07:16 +000011675 jpeg_image=CloneImage(image,0,0,MagickTrue,exception);
glennrp0fe50b42010-11-16 03:52:51 +000011676
cristy3ed852e2009-09-05 21:47:34 +000011677 if (jpeg_image == (Image *) NULL)
11678 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
glennrp0fe50b42010-11-16 03:52:51 +000011679
cristy3ed852e2009-09-05 21:47:34 +000011680 (void) CopyMagickString(jpeg_image->magick,"JPEG",MaxTextExtent);
cristybd5a96c2011-08-21 00:04:26 +000011681 channel_mask=SetPixelChannelMask(jpeg_image,AlphaChannel);
cristye941a752011-10-15 01:52:48 +000011682 status=SeparateImage(jpeg_image,exception);
cristye2a912b2011-12-05 20:02:07 +000011683 (void) SetPixelChannelMapMask(jpeg_image,channel_mask);
cristy3ed852e2009-09-05 21:47:34 +000011684 jpeg_image->matte=MagickFalse;
glennrp59575fa2011-12-31 21:31:39 +000011685 jpeg_image->quality=jng_alpha_quality;
cristy3ed852e2009-09-05 21:47:34 +000011686 jpeg_image_info->type=GrayscaleType;
cristy018f07f2011-09-04 21:15:19 +000011687 (void) SetImageType(jpeg_image,GrayscaleType,exception);
cristy3ed852e2009-09-05 21:47:34 +000011688 (void) AcquireUniqueFilename(jpeg_image->filename);
cristy3b6fd2e2011-05-20 12:53:50 +000011689 (void) FormatLocaleString(jpeg_image_info->filename,MaxTextExtent,
cristy3ed852e2009-09-05 21:47:34 +000011690 "%s",jpeg_image->filename);
11691 }
glennrp59575fa2011-12-31 21:31:39 +000011692 else
11693 {
11694 jng_alpha_compression_method=0;
11695 jng_color_type=10;
11696 jng_alpha_sample_depth=0;
11697 }
cristy3ed852e2009-09-05 21:47:34 +000011698
11699 /* To do: check bit depth of PNG alpha channel */
11700
11701 /* Check if image is grayscale. */
11702 if (image_info->type != TrueColorMatteType && image_info->type !=
cristyc82a27b2011-10-21 01:07:16 +000011703 TrueColorType && ImageIsGray(image,exception))
cristy3ed852e2009-09-05 21:47:34 +000011704 jng_color_type-=2;
11705
glennrp59575fa2011-12-31 21:31:39 +000011706 if (logging != MagickFalse)
11707 {
11708 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
11709 " JNG Quality = %d",(int) jng_quality);
11710 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
11711 " JNG Color Type = %d",jng_color_type);
11712 if (transparent)
11713 {
11714 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
11715 " JNG Alpha Compression = %d",jng_alpha_compression_method);
11716 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
11717 " JNG Alpha Depth = %d",jng_alpha_sample_depth);
11718 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
11719 " JNG Alpha Quality = %d",(int) jng_alpha_quality);
11720 }
11721 }
11722
cristy3ed852e2009-09-05 21:47:34 +000011723 if (transparent)
11724 {
11725 if (jng_alpha_compression_method==0)
11726 {
11727 const char
11728 *value;
11729
cristy4c08aed2011-07-01 19:47:50 +000011730 /* Encode alpha as a grayscale PNG blob */
cristy3ed852e2009-09-05 21:47:34 +000011731 status=OpenBlob(jpeg_image_info,jpeg_image,WriteBinaryBlobMode,
cristyc82a27b2011-10-21 01:07:16 +000011732 exception);
cristy3ed852e2009-09-05 21:47:34 +000011733 if (logging != MagickFalse)
11734 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
11735 " Creating PNG blob.");
11736 length=0;
11737
11738 (void) CopyMagickString(jpeg_image_info->magick,"PNG",MaxTextExtent);
11739 (void) CopyMagickString(jpeg_image->magick,"PNG",MaxTextExtent);
11740 jpeg_image_info->interlace=NoInterlace;
11741
11742 blob=ImageToBlob(jpeg_image_info,jpeg_image,&length,
cristyc82a27b2011-10-21 01:07:16 +000011743 exception);
cristy3ed852e2009-09-05 21:47:34 +000011744
11745 /* Retrieve sample depth used */
cristyd15e6592011-10-15 00:13:06 +000011746 value=GetImageProperty(jpeg_image,"png:bit-depth-written",exception);
cristy3ed852e2009-09-05 21:47:34 +000011747 if (value != (char *) NULL)
11748 jng_alpha_sample_depth= (unsigned int) value[0];
11749 }
11750 else
11751 {
cristy4c08aed2011-07-01 19:47:50 +000011752 /* Encode alpha as a grayscale JPEG blob */
cristy3ed852e2009-09-05 21:47:34 +000011753
11754 status=OpenBlob(jpeg_image_info,jpeg_image,WriteBinaryBlobMode,
cristyc82a27b2011-10-21 01:07:16 +000011755 exception);
cristy3ed852e2009-09-05 21:47:34 +000011756
11757 (void) CopyMagickString(jpeg_image_info->magick,"JPEG",MaxTextExtent);
11758 (void) CopyMagickString(jpeg_image->magick,"JPEG",MaxTextExtent);
11759 jpeg_image_info->interlace=NoInterlace;
11760 if (logging != MagickFalse)
11761 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
11762 " Creating blob.");
11763 blob=ImageToBlob(jpeg_image_info,jpeg_image,&length,
cristyc82a27b2011-10-21 01:07:16 +000011764 exception);
cristy3ed852e2009-09-05 21:47:34 +000011765 jng_alpha_sample_depth=8;
glennrp0fe50b42010-11-16 03:52:51 +000011766
cristy3ed852e2009-09-05 21:47:34 +000011767 if (logging != MagickFalse)
11768 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +000011769 " Successfully read jpeg_image into a blob, length=%.20g.",
11770 (double) length);
cristy3ed852e2009-09-05 21:47:34 +000011771
11772 }
11773 /* Destroy JPEG image and image_info */
11774 jpeg_image=DestroyImage(jpeg_image);
11775 (void) RelinquishUniqueFileResource(jpeg_image_info->filename);
11776 jpeg_image_info=DestroyImageInfo(jpeg_image_info);
11777 }
11778
11779 /* Write JHDR chunk */
11780 (void) WriteBlobMSBULong(image,16L); /* chunk data length=16 */
11781 PNGType(chunk,mng_JHDR);
glennrp03812ae2010-12-24 01:31:34 +000011782 LogPNGChunk(logging,mng_JHDR,16L);
cristy4e5bc842010-06-09 13:56:01 +000011783 PNGLong(chunk+4,(png_uint_32) image->columns);
11784 PNGLong(chunk+8,(png_uint_32) image->rows);
cristy3ed852e2009-09-05 21:47:34 +000011785 chunk[12]=jng_color_type;
11786 chunk[13]=8; /* sample depth */
11787 chunk[14]=8; /*jng_image_compression_method */
11788 chunk[15]=(unsigned char) (image_info->interlace == NoInterlace ? 0 : 8);
11789 chunk[16]=jng_alpha_sample_depth;
11790 chunk[17]=jng_alpha_compression_method;
11791 chunk[18]=0; /*jng_alpha_filter_method */
11792 chunk[19]=0; /*jng_alpha_interlace_method */
11793 (void) WriteBlob(image,20,chunk);
11794 (void) WriteBlobMSBULong(image,crc32(0,chunk,20));
11795 if (logging != MagickFalse)
11796 {
11797 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristyf2faecf2010-05-28 19:19:36 +000011798 " JNG width:%15lu",(unsigned long) image->columns);
glennrp0fe50b42010-11-16 03:52:51 +000011799
cristy3ed852e2009-09-05 21:47:34 +000011800 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristyf2faecf2010-05-28 19:19:36 +000011801 " JNG height:%14lu",(unsigned long) image->rows);
glennrp0fe50b42010-11-16 03:52:51 +000011802
cristy3ed852e2009-09-05 21:47:34 +000011803 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
11804 " JNG color type:%10d",jng_color_type);
glennrp0fe50b42010-11-16 03:52:51 +000011805
cristy3ed852e2009-09-05 21:47:34 +000011806 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
11807 " JNG sample depth:%8d",8);
glennrp0fe50b42010-11-16 03:52:51 +000011808
cristy3ed852e2009-09-05 21:47:34 +000011809 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
11810 " JNG compression:%9d",8);
glennrp0fe50b42010-11-16 03:52:51 +000011811
cristy3ed852e2009-09-05 21:47:34 +000011812 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
11813 " JNG interlace:%11d",0);
glennrp0fe50b42010-11-16 03:52:51 +000011814
cristy3ed852e2009-09-05 21:47:34 +000011815 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
11816 " JNG alpha depth:%9d",jng_alpha_sample_depth);
glennrp0fe50b42010-11-16 03:52:51 +000011817
cristy3ed852e2009-09-05 21:47:34 +000011818 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
11819 " JNG alpha compression:%3d",jng_alpha_compression_method);
glennrp0fe50b42010-11-16 03:52:51 +000011820
cristy3ed852e2009-09-05 21:47:34 +000011821 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
11822 " JNG alpha filter:%8d",0);
glennrp0fe50b42010-11-16 03:52:51 +000011823
cristy3ed852e2009-09-05 21:47:34 +000011824 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
11825 " JNG alpha interlace:%5d",0);
11826 }
11827
glennrp0fe50b42010-11-16 03:52:51 +000011828 /* Write any JNG-chunk-b profiles */
glennrpcf002022011-01-30 02:38:15 +000011829 (void) Magick_png_write_chunk_from_profile(image,"JNG-chunk-b",logging);
cristy3ed852e2009-09-05 21:47:34 +000011830
11831 /*
11832 Write leading ancillary chunks
11833 */
11834
11835 if (transparent)
11836 {
11837 /*
11838 Write JNG bKGD chunk
11839 */
11840
11841 unsigned char
11842 blue,
11843 green,
11844 red;
11845
cristybb503372010-05-27 20:51:26 +000011846 ssize_t
cristy3ed852e2009-09-05 21:47:34 +000011847 num_bytes;
11848
11849 if (jng_color_type == 8 || jng_color_type == 12)
11850 num_bytes=6L;
11851 else
11852 num_bytes=10L;
cristybb503372010-05-27 20:51:26 +000011853 (void) WriteBlobMSBULong(image,(size_t) (num_bytes-4L));
cristy3ed852e2009-09-05 21:47:34 +000011854 PNGType(chunk,mng_bKGD);
glennrp03812ae2010-12-24 01:31:34 +000011855 LogPNGChunk(logging,mng_bKGD,(size_t) (num_bytes-4L));
cristy3ed852e2009-09-05 21:47:34 +000011856 red=ScaleQuantumToChar(image->background_color.red);
11857 green=ScaleQuantumToChar(image->background_color.green);
11858 blue=ScaleQuantumToChar(image->background_color.blue);
11859 *(chunk+4)=0;
11860 *(chunk+5)=red;
11861 *(chunk+6)=0;
11862 *(chunk+7)=green;
11863 *(chunk+8)=0;
11864 *(chunk+9)=blue;
11865 (void) WriteBlob(image,(size_t) num_bytes,chunk);
11866 (void) WriteBlobMSBULong(image,crc32(0,chunk,(uInt) num_bytes));
11867 }
11868
11869 if ((image->colorspace == sRGBColorspace || image->rendering_intent))
11870 {
11871 /*
11872 Write JNG sRGB chunk
11873 */
11874 (void) WriteBlobMSBULong(image,1L);
11875 PNGType(chunk,mng_sRGB);
glennrp03812ae2010-12-24 01:31:34 +000011876 LogPNGChunk(logging,mng_sRGB,1L);
glennrp0fe50b42010-11-16 03:52:51 +000011877
cristy3ed852e2009-09-05 21:47:34 +000011878 if (image->rendering_intent != UndefinedIntent)
glennrpe610a072010-08-05 17:08:46 +000011879 chunk[4]=(unsigned char)
glennrpcf002022011-01-30 02:38:15 +000011880 Magick_RenderingIntent_to_PNG_RenderingIntent(
glennrpe610a072010-08-05 17:08:46 +000011881 (image->rendering_intent));
glennrp0fe50b42010-11-16 03:52:51 +000011882
cristy3ed852e2009-09-05 21:47:34 +000011883 else
glennrpe610a072010-08-05 17:08:46 +000011884 chunk[4]=(unsigned char)
glennrpcf002022011-01-30 02:38:15 +000011885 Magick_RenderingIntent_to_PNG_RenderingIntent(
glennrpe610a072010-08-05 17:08:46 +000011886 (PerceptualIntent));
glennrp0fe50b42010-11-16 03:52:51 +000011887
cristy3ed852e2009-09-05 21:47:34 +000011888 (void) WriteBlob(image,5,chunk);
11889 (void) WriteBlobMSBULong(image,crc32(0,chunk,5));
11890 }
11891 else
11892 {
11893 if (image->gamma != 0.0)
11894 {
11895 /*
11896 Write JNG gAMA chunk
11897 */
11898 (void) WriteBlobMSBULong(image,4L);
11899 PNGType(chunk,mng_gAMA);
glennrp03812ae2010-12-24 01:31:34 +000011900 LogPNGChunk(logging,mng_gAMA,4L);
cristy35ef8242010-06-03 16:24:13 +000011901 PNGLong(chunk+4,(png_uint_32) (100000*image->gamma+0.5));
cristy3ed852e2009-09-05 21:47:34 +000011902 (void) WriteBlob(image,8,chunk);
11903 (void) WriteBlobMSBULong(image,crc32(0,chunk,8));
11904 }
glennrp0fe50b42010-11-16 03:52:51 +000011905
cristy3ed852e2009-09-05 21:47:34 +000011906 if ((mng_info->equal_chrms == MagickFalse) &&
11907 (image->chromaticity.red_primary.x != 0.0))
11908 {
11909 PrimaryInfo
11910 primary;
11911
11912 /*
11913 Write JNG cHRM chunk
11914 */
11915 (void) WriteBlobMSBULong(image,32L);
11916 PNGType(chunk,mng_cHRM);
glennrp03812ae2010-12-24 01:31:34 +000011917 LogPNGChunk(logging,mng_cHRM,32L);
cristy3ed852e2009-09-05 21:47:34 +000011918 primary=image->chromaticity.white_point;
cristy35ef8242010-06-03 16:24:13 +000011919 PNGLong(chunk+4,(png_uint_32) (100000*primary.x+0.5));
11920 PNGLong(chunk+8,(png_uint_32) (100000*primary.y+0.5));
cristy3ed852e2009-09-05 21:47:34 +000011921 primary=image->chromaticity.red_primary;
cristy35ef8242010-06-03 16:24:13 +000011922 PNGLong(chunk+12,(png_uint_32) (100000*primary.x+0.5));
11923 PNGLong(chunk+16,(png_uint_32) (100000*primary.y+0.5));
cristy3ed852e2009-09-05 21:47:34 +000011924 primary=image->chromaticity.green_primary;
cristy35ef8242010-06-03 16:24:13 +000011925 PNGLong(chunk+20,(png_uint_32) (100000*primary.x+0.5));
11926 PNGLong(chunk+24,(png_uint_32) (100000*primary.y+0.5));
cristy3ed852e2009-09-05 21:47:34 +000011927 primary=image->chromaticity.blue_primary;
cristy35ef8242010-06-03 16:24:13 +000011928 PNGLong(chunk+28,(png_uint_32) (100000*primary.x+0.5));
11929 PNGLong(chunk+32,(png_uint_32) (100000*primary.y+0.5));
cristy3ed852e2009-09-05 21:47:34 +000011930 (void) WriteBlob(image,36,chunk);
11931 (void) WriteBlobMSBULong(image,crc32(0,chunk,36));
11932 }
11933 }
glennrp0fe50b42010-11-16 03:52:51 +000011934
cristy2a11bef2011-10-28 18:33:11 +000011935 if (image->resolution.x && image->resolution.y && !mng_info->equal_physs)
cristy3ed852e2009-09-05 21:47:34 +000011936 {
11937 /*
11938 Write JNG pHYs chunk
11939 */
11940 (void) WriteBlobMSBULong(image,9L);
11941 PNGType(chunk,mng_pHYs);
glennrp03812ae2010-12-24 01:31:34 +000011942 LogPNGChunk(logging,mng_pHYs,9L);
cristy3ed852e2009-09-05 21:47:34 +000011943 if (image->units == PixelsPerInchResolution)
11944 {
cristy35ef8242010-06-03 16:24:13 +000011945 PNGLong(chunk+4,(png_uint_32)
cristy2a11bef2011-10-28 18:33:11 +000011946 (image->resolution.x*100.0/2.54+0.5));
glennrp0fe50b42010-11-16 03:52:51 +000011947
cristy35ef8242010-06-03 16:24:13 +000011948 PNGLong(chunk+8,(png_uint_32)
cristy2a11bef2011-10-28 18:33:11 +000011949 (image->resolution.y*100.0/2.54+0.5));
glennrp0fe50b42010-11-16 03:52:51 +000011950
cristy3ed852e2009-09-05 21:47:34 +000011951 chunk[12]=1;
11952 }
glennrp0fe50b42010-11-16 03:52:51 +000011953
cristy3ed852e2009-09-05 21:47:34 +000011954 else
11955 {
11956 if (image->units == PixelsPerCentimeterResolution)
11957 {
cristy35ef8242010-06-03 16:24:13 +000011958 PNGLong(chunk+4,(png_uint_32)
cristy2a11bef2011-10-28 18:33:11 +000011959 (image->resolution.x*100.0+0.5));
glennrp0fe50b42010-11-16 03:52:51 +000011960
cristy35ef8242010-06-03 16:24:13 +000011961 PNGLong(chunk+8,(png_uint_32)
cristy2a11bef2011-10-28 18:33:11 +000011962 (image->resolution.y*100.0+0.5));
glennrp0fe50b42010-11-16 03:52:51 +000011963
cristy3ed852e2009-09-05 21:47:34 +000011964 chunk[12]=1;
11965 }
glennrp0fe50b42010-11-16 03:52:51 +000011966
cristy3ed852e2009-09-05 21:47:34 +000011967 else
11968 {
cristy2a11bef2011-10-28 18:33:11 +000011969 PNGLong(chunk+4,(png_uint_32) (image->resolution.x+0.5));
11970 PNGLong(chunk+8,(png_uint_32) (image->resolution.y+0.5));
cristy3ed852e2009-09-05 21:47:34 +000011971 chunk[12]=0;
11972 }
11973 }
11974 (void) WriteBlob(image,13,chunk);
11975 (void) WriteBlobMSBULong(image,crc32(0,chunk,13));
11976 }
11977
11978 if (mng_info->write_mng == 0 && (image->page.x || image->page.y))
11979 {
11980 /*
11981 Write JNG oFFs chunk
11982 */
11983 (void) WriteBlobMSBULong(image,9L);
11984 PNGType(chunk,mng_oFFs);
glennrp03812ae2010-12-24 01:31:34 +000011985 LogPNGChunk(logging,mng_oFFs,9L);
cristybb503372010-05-27 20:51:26 +000011986 PNGsLong(chunk+4,(ssize_t) (image->page.x));
11987 PNGsLong(chunk+8,(ssize_t) (image->page.y));
cristy3ed852e2009-09-05 21:47:34 +000011988 chunk[12]=0;
11989 (void) WriteBlob(image,13,chunk);
11990 (void) WriteBlobMSBULong(image,crc32(0,chunk,13));
11991 }
11992 if (mng_info->write_mng == 0 && (image->page.width || image->page.height))
11993 {
11994 (void) WriteBlobMSBULong(image,9L); /* data length=8 */
11995 PNGType(chunk,mng_vpAg);
glennrp03812ae2010-12-24 01:31:34 +000011996 LogPNGChunk(logging,mng_vpAg,9L);
cristy3ed852e2009-09-05 21:47:34 +000011997 PNGLong(chunk+4,(png_uint_32) image->page.width);
11998 PNGLong(chunk+8,(png_uint_32) image->page.height);
11999 chunk[12]=0; /* unit = pixels */
12000 (void) WriteBlob(image,13,chunk);
12001 (void) WriteBlobMSBULong(image,crc32(0,chunk,13));
12002 }
12003
12004
12005 if (transparent)
12006 {
12007 if (jng_alpha_compression_method==0)
12008 {
cristybb503372010-05-27 20:51:26 +000012009 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +000012010 i;
12011
cristybb503372010-05-27 20:51:26 +000012012 ssize_t
cristy3ed852e2009-09-05 21:47:34 +000012013 len;
12014
12015 /* Write IDAT chunk header */
12016 if (logging != MagickFalse)
12017 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +000012018 " Write IDAT chunks from blob, length=%.20g.",(double)
cristyf2faecf2010-05-28 19:19:36 +000012019 length);
cristy3ed852e2009-09-05 21:47:34 +000012020
12021 /* Copy IDAT chunks */
12022 len=0;
12023 p=blob+8;
cristybb503372010-05-27 20:51:26 +000012024 for (i=8; i<(ssize_t) length; i+=len+12)
cristy3ed852e2009-09-05 21:47:34 +000012025 {
12026 len=(*p<<24)|((*(p+1))<<16)|((*(p+2))<<8)|(*(p+3));
12027 p+=4;
glennrp0fe50b42010-11-16 03:52:51 +000012028
cristy3ed852e2009-09-05 21:47:34 +000012029 if (*(p)==73 && *(p+1)==68 && *(p+2)==65 && *(p+3)==84) /* IDAT */
12030 {
12031 /* Found an IDAT chunk. */
cristybb503372010-05-27 20:51:26 +000012032 (void) WriteBlobMSBULong(image,(size_t) len);
glennrp03812ae2010-12-24 01:31:34 +000012033 LogPNGChunk(logging,mng_IDAT,(size_t) len);
cristy3ed852e2009-09-05 21:47:34 +000012034 (void) WriteBlob(image,(size_t) len+4,p);
12035 (void) WriteBlobMSBULong(image,
12036 crc32(0,p,(uInt) len+4));
12037 }
glennrp0fe50b42010-11-16 03:52:51 +000012038
cristy3ed852e2009-09-05 21:47:34 +000012039 else
12040 {
12041 if (logging != MagickFalse)
12042 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +000012043 " Skipping %c%c%c%c chunk, length=%.20g.",
12044 *(p),*(p+1),*(p+2),*(p+3),(double) len);
cristy3ed852e2009-09-05 21:47:34 +000012045 }
12046 p+=(8+len);
12047 }
12048 }
12049 else
12050 {
12051 /* Write JDAA chunk header */
12052 if (logging != MagickFalse)
12053 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +000012054 " Write JDAA chunk, length=%.20g.",(double) length);
cristybb503372010-05-27 20:51:26 +000012055 (void) WriteBlobMSBULong(image,(size_t) length);
cristy3ed852e2009-09-05 21:47:34 +000012056 PNGType(chunk,mng_JDAA);
glennrp03812ae2010-12-24 01:31:34 +000012057 LogPNGChunk(logging,mng_JDAA,length);
cristy3ed852e2009-09-05 21:47:34 +000012058 /* Write JDAT chunk(s) data */
12059 (void) WriteBlob(image,4,chunk);
12060 (void) WriteBlob(image,length,blob);
12061 (void) WriteBlobMSBULong(image,crc32(crc32(0,chunk,4),blob,
12062 (uInt) length));
12063 }
12064 blob=(unsigned char *) RelinquishMagickMemory(blob);
12065 }
12066
12067 /* Encode image as a JPEG blob */
12068 if (logging != MagickFalse)
12069 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
12070 " Creating jpeg_image_info.");
12071 jpeg_image_info=(ImageInfo *) CloneImageInfo(image_info);
12072 if (jpeg_image_info == (ImageInfo *) NULL)
12073 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
12074
12075 if (logging != MagickFalse)
12076 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
12077 " Creating jpeg_image.");
12078
cristyc82a27b2011-10-21 01:07:16 +000012079 jpeg_image=CloneImage(image,0,0,MagickTrue,exception);
cristy3ed852e2009-09-05 21:47:34 +000012080 if (jpeg_image == (Image *) NULL)
12081 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
12082 (void) CopyMagickString(jpeg_image->magick,"JPEG",MaxTextExtent);
12083
12084 (void) AcquireUniqueFilename(jpeg_image->filename);
cristy3b6fd2e2011-05-20 12:53:50 +000012085 (void) FormatLocaleString(jpeg_image_info->filename,MaxTextExtent,"%s",
cristy3ed852e2009-09-05 21:47:34 +000012086 jpeg_image->filename);
12087
12088 status=OpenBlob(jpeg_image_info,jpeg_image,WriteBinaryBlobMode,
cristyc82a27b2011-10-21 01:07:16 +000012089 exception);
cristy3ed852e2009-09-05 21:47:34 +000012090
12091 if (logging != MagickFalse)
12092 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +000012093 " Created jpeg_image, %.20g x %.20g.",(double) jpeg_image->columns,
12094 (double) jpeg_image->rows);
cristy3ed852e2009-09-05 21:47:34 +000012095
12096 if (jng_color_type == 8 || jng_color_type == 12)
12097 jpeg_image_info->type=GrayscaleType;
glennrp0fe50b42010-11-16 03:52:51 +000012098
glennrp59575fa2011-12-31 21:31:39 +000012099 jpeg_image_info->quality=jng_quality;
12100 jpeg_image->quality=jng_quality;
cristy3ed852e2009-09-05 21:47:34 +000012101 (void) CopyMagickString(jpeg_image_info->magick,"JPEG",MaxTextExtent);
12102 (void) CopyMagickString(jpeg_image->magick,"JPEG",MaxTextExtent);
glennrp0fe50b42010-11-16 03:52:51 +000012103
cristy3ed852e2009-09-05 21:47:34 +000012104 if (logging != MagickFalse)
12105 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
12106 " Creating blob.");
glennrp0fe50b42010-11-16 03:52:51 +000012107
cristyc82a27b2011-10-21 01:07:16 +000012108 blob=ImageToBlob(jpeg_image_info,jpeg_image,&length,exception);
glennrp0fe50b42010-11-16 03:52:51 +000012109
cristy3ed852e2009-09-05 21:47:34 +000012110 if (logging != MagickFalse)
12111 {
12112 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +000012113 " Successfully read jpeg_image into a blob, length=%.20g.",
12114 (double) length);
cristy3ed852e2009-09-05 21:47:34 +000012115
12116 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +000012117 " Write JDAT chunk, length=%.20g.",(double) length);
cristy3ed852e2009-09-05 21:47:34 +000012118 }
glennrp0fe50b42010-11-16 03:52:51 +000012119
cristy3ed852e2009-09-05 21:47:34 +000012120 /* Write JDAT chunk(s) */
cristybb503372010-05-27 20:51:26 +000012121 (void) WriteBlobMSBULong(image,(size_t) length);
cristy3ed852e2009-09-05 21:47:34 +000012122 PNGType(chunk,mng_JDAT);
glennrp03812ae2010-12-24 01:31:34 +000012123 LogPNGChunk(logging,mng_JDAT,length);
cristy3ed852e2009-09-05 21:47:34 +000012124 (void) WriteBlob(image,4,chunk);
12125 (void) WriteBlob(image,length,blob);
12126 (void) WriteBlobMSBULong(image,crc32(crc32(0,chunk,4),blob,(uInt) length));
12127
12128 jpeg_image=DestroyImage(jpeg_image);
12129 (void) RelinquishUniqueFileResource(jpeg_image_info->filename);
12130 jpeg_image_info=DestroyImageInfo(jpeg_image_info);
12131 blob=(unsigned char *) RelinquishMagickMemory(blob);
12132
12133 /* Write any JNG-chunk-e profiles */
glennrpcf002022011-01-30 02:38:15 +000012134 (void) Magick_png_write_chunk_from_profile(image,"JNG-chunk-e",logging);
cristy3ed852e2009-09-05 21:47:34 +000012135
12136 /* Write IEND chunk */
12137 (void) WriteBlobMSBULong(image,0L);
12138 PNGType(chunk,mng_IEND);
glennrp03812ae2010-12-24 01:31:34 +000012139 LogPNGChunk(logging,mng_IEND,0);
cristy3ed852e2009-09-05 21:47:34 +000012140 (void) WriteBlob(image,4,chunk);
12141 (void) WriteBlobMSBULong(image,crc32(0,chunk,4));
12142
12143 if (logging != MagickFalse)
12144 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
12145 " exit WriteOneJNGImage()");
glennrp0fe50b42010-11-16 03:52:51 +000012146
cristy3ed852e2009-09-05 21:47:34 +000012147 return(status);
12148}
12149
12150
12151/*
12152%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
12153% %
12154% %
12155% %
12156% W r i t e J N G I m a g e %
12157% %
12158% %
12159% %
12160%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
12161%
12162% WriteJNGImage() writes a JPEG Network Graphics (JNG) image file.
12163%
12164% JNG support written by Glenn Randers-Pehrson, glennrp@image...
12165%
12166% The format of the WriteJNGImage method is:
12167%
cristy1e178e72011-08-28 19:44:34 +000012168% MagickBooleanType WriteJNGImage(const ImageInfo *image_info,
12169% Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +000012170%
12171% A description of each parameter follows:
12172%
12173% o image_info: the image info.
12174%
12175% o image: The image.
12176%
cristy1e178e72011-08-28 19:44:34 +000012177% o exception: return any errors or warnings in this structure.
12178%
cristy3ed852e2009-09-05 21:47:34 +000012179%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
12180*/
cristy1e178e72011-08-28 19:44:34 +000012181static MagickBooleanType WriteJNGImage(const ImageInfo *image_info,Image *image,
12182 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +000012183{
12184 MagickBooleanType
glennrp21f0e622011-01-07 16:20:57 +000012185 have_mng_structure,
glennrp03812ae2010-12-24 01:31:34 +000012186 logging,
cristy3ed852e2009-09-05 21:47:34 +000012187 status;
12188
12189 MngInfo
12190 *mng_info;
12191
cristy3ed852e2009-09-05 21:47:34 +000012192 /*
12193 Open image file.
12194 */
12195 assert(image_info != (const ImageInfo *) NULL);
12196 assert(image_info->signature == MagickSignature);
12197 assert(image != (Image *) NULL);
12198 assert(image->signature == MagickSignature);
12199 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
glennrpfd05d622011-02-25 04:10:33 +000012200 logging=LogMagickEvent(CoderEvent,GetMagickModule(),"Enter WriteJNGImage()");
cristyc82a27b2011-10-21 01:07:16 +000012201 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
cristy3ed852e2009-09-05 21:47:34 +000012202 if (status == MagickFalse)
12203 return(status);
12204
12205 /*
12206 Allocate a MngInfo structure.
12207 */
12208 have_mng_structure=MagickFalse;
cristy73bd4a52010-10-05 11:24:23 +000012209 mng_info=(MngInfo *) AcquireMagickMemory(sizeof(MngInfo));
cristy3ed852e2009-09-05 21:47:34 +000012210 if (mng_info == (MngInfo *) NULL)
12211 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
12212 /*
12213 Initialize members of the MngInfo structure.
12214 */
12215 (void) ResetMagickMemory(mng_info,0,sizeof(MngInfo));
12216 mng_info->image=image;
12217 have_mng_structure=MagickTrue;
12218
12219 (void) WriteBlob(image,8,(const unsigned char *) "\213JNG\r\n\032\n");
12220
cristy018f07f2011-09-04 21:15:19 +000012221 status=WriteOneJNGImage(mng_info,image_info,image,exception);
cristy3ed852e2009-09-05 21:47:34 +000012222 (void) CloseBlob(image);
12223
12224 (void) CatchImageException(image);
12225 MngInfoFreeStruct(mng_info,&have_mng_structure);
12226 if (logging != MagickFalse)
12227 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"exit WriteJNGImage()");
12228 return(status);
12229}
12230#endif
12231
cristy1e178e72011-08-28 19:44:34 +000012232static MagickBooleanType WriteMNGImage(const ImageInfo *image_info,Image *image,
12233 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +000012234{
12235 const char
12236 *option;
12237
12238 Image
12239 *next_image;
12240
12241 MagickBooleanType
glennrp21f0e622011-01-07 16:20:57 +000012242 have_mng_structure,
cristy3ed852e2009-09-05 21:47:34 +000012243 status;
12244
glennrp03812ae2010-12-24 01:31:34 +000012245 volatile MagickBooleanType
12246 logging;
12247
cristy3ed852e2009-09-05 21:47:34 +000012248 MngInfo
12249 *mng_info;
12250
12251 int
cristy3ed852e2009-09-05 21:47:34 +000012252 image_count,
12253 need_iterations,
12254 need_matte;
12255
12256 volatile int
12257#if defined(PNG_WRITE_EMPTY_PLTE_SUPPORTED) || \
12258 defined(PNG_MNG_FEATURES_SUPPORTED)
12259 need_local_plte,
12260#endif
12261 all_images_are_gray,
cristy3ed852e2009-09-05 21:47:34 +000012262 need_defi,
cristy3ed852e2009-09-05 21:47:34 +000012263 use_global_plte;
12264
cristybb503372010-05-27 20:51:26 +000012265 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +000012266 i;
12267
12268 unsigned char
12269 chunk[800];
12270
12271 volatile unsigned int
12272 write_jng,
12273 write_mng;
12274
cristybb503372010-05-27 20:51:26 +000012275 volatile size_t
cristy3ed852e2009-09-05 21:47:34 +000012276 scene;
12277
cristybb503372010-05-27 20:51:26 +000012278 size_t
cristy3ed852e2009-09-05 21:47:34 +000012279 final_delay=0,
12280 initial_delay;
12281
glennrpd5045b42010-03-24 12:40:35 +000012282#if (PNG_LIBPNG_VER < 10200)
cristy3ed852e2009-09-05 21:47:34 +000012283 if (image_info->verbose)
12284 printf("Your PNG library (libpng-%s) is rather old.\n",
12285 PNG_LIBPNG_VER_STRING);
12286#endif
12287
12288 /*
12289 Open image file.
12290 */
12291 assert(image_info != (const ImageInfo *) NULL);
12292 assert(image_info->signature == MagickSignature);
12293 assert(image != (Image *) NULL);
12294 assert(image->signature == MagickSignature);
12295 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
glennrpfd05d622011-02-25 04:10:33 +000012296 logging=LogMagickEvent(CoderEvent,GetMagickModule(),"Enter WriteMNGImage()");
cristyc82a27b2011-10-21 01:07:16 +000012297 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
cristy3ed852e2009-09-05 21:47:34 +000012298 if (status == MagickFalse)
12299 return(status);
12300
12301 /*
12302 Allocate a MngInfo structure.
12303 */
12304 have_mng_structure=MagickFalse;
cristy73bd4a52010-10-05 11:24:23 +000012305 mng_info=(MngInfo *) AcquireMagickMemory(sizeof(MngInfo));
cristy3ed852e2009-09-05 21:47:34 +000012306 if (mng_info == (MngInfo *) NULL)
12307 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
12308 /*
12309 Initialize members of the MngInfo structure.
12310 */
12311 (void) ResetMagickMemory(mng_info,0,sizeof(MngInfo));
12312 mng_info->image=image;
12313 have_mng_structure=MagickTrue;
12314 write_mng=LocaleCompare(image_info->magick,"MNG") == 0;
12315
12316 /*
12317 * See if user has requested a specific PNG subformat to be used
12318 * for all of the PNGs in the MNG being written, e.g.,
12319 *
12320 * convert *.png png8:animation.mng
12321 *
12322 * To do: check -define png:bit_depth and png:color_type as well,
12323 * or perhaps use mng:bit_depth and mng:color_type instead for
12324 * global settings.
12325 */
12326
12327 mng_info->write_png8=LocaleCompare(image_info->magick,"PNG8") == 0;
12328 mng_info->write_png24=LocaleCompare(image_info->magick,"PNG24") == 0;
12329 mng_info->write_png32=LocaleCompare(image_info->magick,"PNG32") == 0;
12330
12331 write_jng=MagickFalse;
12332 if (image_info->compression == JPEGCompression)
12333 write_jng=MagickTrue;
12334
12335 mng_info->adjoin=image_info->adjoin &&
12336 (GetNextImageInList(image) != (Image *) NULL) && write_mng;
12337
cristy3ed852e2009-09-05 21:47:34 +000012338 if (logging != MagickFalse)
12339 {
12340 /* Log some info about the input */
12341 Image
12342 *p;
12343
12344 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
12345 " Checking input image(s)");
glennrp0fe50b42010-11-16 03:52:51 +000012346
cristy3ed852e2009-09-05 21:47:34 +000012347 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +000012348 " Image_info depth: %.20g",(double) image_info->depth);
glennrp0fe50b42010-11-16 03:52:51 +000012349
cristy3ed852e2009-09-05 21:47:34 +000012350 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
12351 " Type: %d",image_info->type);
12352
12353 scene=0;
12354 for (p=image; p != (Image *) NULL; p=GetNextImageInList(p))
12355 {
12356 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +000012357 " Scene: %.20g",(double) scene++);
glennrp0fe50b42010-11-16 03:52:51 +000012358
cristy3ed852e2009-09-05 21:47:34 +000012359 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +000012360 " Image depth: %.20g",(double) p->depth);
glennrp0fe50b42010-11-16 03:52:51 +000012361
cristy3ed852e2009-09-05 21:47:34 +000012362 if (p->matte)
12363 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
12364 " Matte: True");
glennrp0fe50b42010-11-16 03:52:51 +000012365
cristy3ed852e2009-09-05 21:47:34 +000012366 else
12367 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
12368 " Matte: False");
glennrp0fe50b42010-11-16 03:52:51 +000012369
cristy3ed852e2009-09-05 21:47:34 +000012370 if (p->storage_class == PseudoClass)
12371 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
12372 " Storage class: PseudoClass");
glennrp0fe50b42010-11-16 03:52:51 +000012373
cristy3ed852e2009-09-05 21:47:34 +000012374 else
12375 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
12376 " Storage class: DirectClass");
glennrp0fe50b42010-11-16 03:52:51 +000012377
cristy3ed852e2009-09-05 21:47:34 +000012378 if (p->colors)
12379 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +000012380 " Number of colors: %.20g",(double) p->colors);
glennrp0fe50b42010-11-16 03:52:51 +000012381
cristy3ed852e2009-09-05 21:47:34 +000012382 else
12383 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
12384 " Number of colors: unspecified");
glennrp0fe50b42010-11-16 03:52:51 +000012385
cristy3ed852e2009-09-05 21:47:34 +000012386 if (mng_info->adjoin == MagickFalse)
12387 break;
12388 }
12389 }
12390
cristy3ed852e2009-09-05 21:47:34 +000012391 use_global_plte=MagickFalse;
12392 all_images_are_gray=MagickFalse;
12393#ifdef PNG_WRITE_EMPTY_PLTE_SUPPORTED
12394 need_local_plte=MagickTrue;
12395#endif
12396 need_defi=MagickFalse;
12397 need_matte=MagickFalse;
12398 mng_info->framing_mode=1;
12399 mng_info->old_framing_mode=1;
12400
12401 if (write_mng)
12402 if (image_info->page != (char *) NULL)
12403 {
12404 /*
12405 Determine image bounding box.
12406 */
12407 SetGeometry(image,&mng_info->page);
12408 (void) ParseMetaGeometry(image_info->page,&mng_info->page.x,
12409 &mng_info->page.y,&mng_info->page.width,&mng_info->page.height);
12410 }
12411 if (write_mng)
12412 {
12413 unsigned int
12414 need_geom;
12415
12416 unsigned short
12417 red,
12418 green,
12419 blue;
12420
12421 mng_info->page=image->page;
12422 need_geom=MagickTrue;
12423 if (mng_info->page.width || mng_info->page.height)
12424 need_geom=MagickFalse;
12425 /*
12426 Check all the scenes.
12427 */
12428 initial_delay=image->delay;
12429 need_iterations=MagickFalse;
12430 mng_info->equal_chrms=image->chromaticity.red_primary.x != 0.0;
12431 mng_info->equal_physs=MagickTrue,
12432 mng_info->equal_gammas=MagickTrue;
12433 mng_info->equal_srgbs=MagickTrue;
12434 mng_info->equal_backgrounds=MagickTrue;
12435 image_count=0;
12436#if defined(PNG_WRITE_EMPTY_PLTE_SUPPORTED) || \
12437 defined(PNG_MNG_FEATURES_SUPPORTED)
12438 all_images_are_gray=MagickTrue;
12439 mng_info->equal_palettes=MagickFalse;
12440 need_local_plte=MagickFalse;
12441#endif
12442 for (next_image=image; next_image != (Image *) NULL; )
12443 {
12444 if (need_geom)
12445 {
12446 if ((next_image->columns+next_image->page.x) > mng_info->page.width)
12447 mng_info->page.width=next_image->columns+next_image->page.x;
glennrp0fe50b42010-11-16 03:52:51 +000012448
cristy3ed852e2009-09-05 21:47:34 +000012449 if ((next_image->rows+next_image->page.y) > mng_info->page.height)
12450 mng_info->page.height=next_image->rows+next_image->page.y;
12451 }
glennrp0fe50b42010-11-16 03:52:51 +000012452
cristy3ed852e2009-09-05 21:47:34 +000012453 if (next_image->page.x || next_image->page.y)
12454 need_defi=MagickTrue;
glennrp0fe50b42010-11-16 03:52:51 +000012455
cristy3ed852e2009-09-05 21:47:34 +000012456 if (next_image->matte)
12457 need_matte=MagickTrue;
glennrp0fe50b42010-11-16 03:52:51 +000012458
cristy3ed852e2009-09-05 21:47:34 +000012459 if ((int) next_image->dispose >= BackgroundDispose)
12460 if (next_image->matte || next_image->page.x || next_image->page.y ||
12461 ((next_image->columns < mng_info->page.width) &&
12462 (next_image->rows < mng_info->page.height)))
12463 mng_info->need_fram=MagickTrue;
glennrp0fe50b42010-11-16 03:52:51 +000012464
cristy3ed852e2009-09-05 21:47:34 +000012465 if (next_image->iterations)
12466 need_iterations=MagickTrue;
glennrp0fe50b42010-11-16 03:52:51 +000012467
cristy3ed852e2009-09-05 21:47:34 +000012468 final_delay=next_image->delay;
glennrp0fe50b42010-11-16 03:52:51 +000012469
cristy3ed852e2009-09-05 21:47:34 +000012470 if (final_delay != initial_delay || final_delay > 1UL*
12471 next_image->ticks_per_second)
12472 mng_info->need_fram=1;
glennrp0fe50b42010-11-16 03:52:51 +000012473
cristy3ed852e2009-09-05 21:47:34 +000012474#if defined(PNG_WRITE_EMPTY_PLTE_SUPPORTED) || \
12475 defined(PNG_MNG_FEATURES_SUPPORTED)
12476 /*
12477 check for global palette possibility.
12478 */
12479 if (image->matte != MagickFalse)
12480 need_local_plte=MagickTrue;
glennrp0fe50b42010-11-16 03:52:51 +000012481
cristy3ed852e2009-09-05 21:47:34 +000012482 if (need_local_plte == 0)
12483 {
cristyc82a27b2011-10-21 01:07:16 +000012484 if (ImageIsGray(image,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +000012485 all_images_are_gray=MagickFalse;
12486 mng_info->equal_palettes=PalettesAreEqual(image,next_image);
12487 if (use_global_plte == 0)
12488 use_global_plte=mng_info->equal_palettes;
12489 need_local_plte=!mng_info->equal_palettes;
12490 }
12491#endif
12492 if (GetNextImageInList(next_image) != (Image *) NULL)
12493 {
12494 if (next_image->background_color.red !=
12495 next_image->next->background_color.red ||
12496 next_image->background_color.green !=
12497 next_image->next->background_color.green ||
12498 next_image->background_color.blue !=
12499 next_image->next->background_color.blue)
12500 mng_info->equal_backgrounds=MagickFalse;
glennrp0fe50b42010-11-16 03:52:51 +000012501
cristy3ed852e2009-09-05 21:47:34 +000012502 if (next_image->gamma != next_image->next->gamma)
12503 mng_info->equal_gammas=MagickFalse;
glennrp0fe50b42010-11-16 03:52:51 +000012504
cristy3ed852e2009-09-05 21:47:34 +000012505 if (next_image->rendering_intent !=
12506 next_image->next->rendering_intent)
12507 mng_info->equal_srgbs=MagickFalse;
glennrp0fe50b42010-11-16 03:52:51 +000012508
cristy3ed852e2009-09-05 21:47:34 +000012509 if ((next_image->units != next_image->next->units) ||
cristy2a11bef2011-10-28 18:33:11 +000012510 (next_image->resolution.x != next_image->next->resolution.x) ||
12511 (next_image->resolution.y != next_image->next->resolution.y))
cristy3ed852e2009-09-05 21:47:34 +000012512 mng_info->equal_physs=MagickFalse;
glennrp0fe50b42010-11-16 03:52:51 +000012513
cristy3ed852e2009-09-05 21:47:34 +000012514 if (mng_info->equal_chrms)
12515 {
12516 if (next_image->chromaticity.red_primary.x !=
12517 next_image->next->chromaticity.red_primary.x ||
12518 next_image->chromaticity.red_primary.y !=
12519 next_image->next->chromaticity.red_primary.y ||
12520 next_image->chromaticity.green_primary.x !=
12521 next_image->next->chromaticity.green_primary.x ||
12522 next_image->chromaticity.green_primary.y !=
12523 next_image->next->chromaticity.green_primary.y ||
12524 next_image->chromaticity.blue_primary.x !=
12525 next_image->next->chromaticity.blue_primary.x ||
12526 next_image->chromaticity.blue_primary.y !=
12527 next_image->next->chromaticity.blue_primary.y ||
12528 next_image->chromaticity.white_point.x !=
12529 next_image->next->chromaticity.white_point.x ||
12530 next_image->chromaticity.white_point.y !=
12531 next_image->next->chromaticity.white_point.y)
12532 mng_info->equal_chrms=MagickFalse;
12533 }
12534 }
12535 image_count++;
12536 next_image=GetNextImageInList(next_image);
12537 }
12538 if (image_count < 2)
12539 {
12540 mng_info->equal_backgrounds=MagickFalse;
12541 mng_info->equal_chrms=MagickFalse;
12542 mng_info->equal_gammas=MagickFalse;
12543 mng_info->equal_srgbs=MagickFalse;
12544 mng_info->equal_physs=MagickFalse;
12545 use_global_plte=MagickFalse;
12546#ifdef PNG_WRITE_EMPTY_PLTE_SUPPORTED
12547 need_local_plte=MagickTrue;
12548#endif
12549 need_iterations=MagickFalse;
12550 }
glennrp0fe50b42010-11-16 03:52:51 +000012551
cristy3ed852e2009-09-05 21:47:34 +000012552 if (mng_info->need_fram == MagickFalse)
12553 {
12554 /*
12555 Only certain framing rates 100/n are exactly representable without
12556 the FRAM chunk but we'll allow some slop in VLC files
12557 */
12558 if (final_delay == 0)
12559 {
12560 if (need_iterations != MagickFalse)
12561 {
12562 /*
12563 It's probably a GIF with loop; don't run it *too* fast.
12564 */
glennrp02617122010-07-28 13:07:35 +000012565 if (mng_info->adjoin)
glennrpd908de42010-07-28 13:28:27 +000012566 {
12567 final_delay=10;
cristyc82a27b2011-10-21 01:07:16 +000012568 (void) ThrowMagickException(exception,GetMagickModule(),
12569 CoderWarning,
glennrpd908de42010-07-28 13:28:27 +000012570 "input has zero delay between all frames; assuming",
12571 " 10 cs `%s'","");
12572 }
cristy3ed852e2009-09-05 21:47:34 +000012573 }
12574 else
12575 mng_info->ticks_per_second=0;
12576 }
12577 if (final_delay != 0)
glennrpcf002022011-01-30 02:38:15 +000012578 mng_info->ticks_per_second=(png_uint_32)
12579 (image->ticks_per_second/final_delay);
cristy3ed852e2009-09-05 21:47:34 +000012580 if (final_delay > 50)
12581 mng_info->ticks_per_second=2;
glennrp0fe50b42010-11-16 03:52:51 +000012582
cristy3ed852e2009-09-05 21:47:34 +000012583 if (final_delay > 75)
12584 mng_info->ticks_per_second=1;
glennrp0fe50b42010-11-16 03:52:51 +000012585
cristy3ed852e2009-09-05 21:47:34 +000012586 if (final_delay > 125)
12587 mng_info->need_fram=MagickTrue;
glennrp0fe50b42010-11-16 03:52:51 +000012588
cristy3ed852e2009-09-05 21:47:34 +000012589 if (need_defi && final_delay > 2 && (final_delay != 4) &&
12590 (final_delay != 5) && (final_delay != 10) && (final_delay != 20) &&
12591 (final_delay != 25) && (final_delay != 50) && (final_delay !=
12592 1UL*image->ticks_per_second))
12593 mng_info->need_fram=MagickTrue; /* make it exact; cannot be VLC */
12594 }
glennrp0fe50b42010-11-16 03:52:51 +000012595
cristy3ed852e2009-09-05 21:47:34 +000012596 if (mng_info->need_fram != MagickFalse)
12597 mng_info->ticks_per_second=1UL*image->ticks_per_second;
12598 /*
12599 If pseudocolor, we should also check to see if all the
12600 palettes are identical and write a global PLTE if they are.
12601 ../glennrp Feb 99.
12602 */
12603 /*
12604 Write the MNG version 1.0 signature and MHDR chunk.
12605 */
12606 (void) WriteBlob(image,8,(const unsigned char *) "\212MNG\r\n\032\n");
12607 (void) WriteBlobMSBULong(image,28L); /* chunk data length=28 */
12608 PNGType(chunk,mng_MHDR);
glennrp03812ae2010-12-24 01:31:34 +000012609 LogPNGChunk(logging,mng_MHDR,28L);
cristy4e5bc842010-06-09 13:56:01 +000012610 PNGLong(chunk+4,(png_uint_32) mng_info->page.width);
12611 PNGLong(chunk+8,(png_uint_32) mng_info->page.height);
cristy3ed852e2009-09-05 21:47:34 +000012612 PNGLong(chunk+12,mng_info->ticks_per_second);
12613 PNGLong(chunk+16,0L); /* layer count=unknown */
12614 PNGLong(chunk+20,0L); /* frame count=unknown */
12615 PNGLong(chunk+24,0L); /* play time=unknown */
12616 if (write_jng)
12617 {
12618 if (need_matte)
12619 {
12620 if (need_defi || mng_info->need_fram || use_global_plte)
12621 PNGLong(chunk+28,27L); /* simplicity=LC+JNG */
glennrp0fe50b42010-11-16 03:52:51 +000012622
cristy3ed852e2009-09-05 21:47:34 +000012623 else
12624 PNGLong(chunk+28,25L); /* simplicity=VLC+JNG */
12625 }
glennrp0fe50b42010-11-16 03:52:51 +000012626
cristy3ed852e2009-09-05 21:47:34 +000012627 else
12628 {
12629 if (need_defi || mng_info->need_fram || use_global_plte)
12630 PNGLong(chunk+28,19L); /* simplicity=LC+JNG, no transparency */
glennrp0fe50b42010-11-16 03:52:51 +000012631
cristy3ed852e2009-09-05 21:47:34 +000012632 else
12633 PNGLong(chunk+28,17L); /* simplicity=VLC+JNG, no transparency */
12634 }
12635 }
glennrp0fe50b42010-11-16 03:52:51 +000012636
cristy3ed852e2009-09-05 21:47:34 +000012637 else
12638 {
12639 if (need_matte)
12640 {
12641 if (need_defi || mng_info->need_fram || use_global_plte)
12642 PNGLong(chunk+28,11L); /* simplicity=LC */
glennrp0fe50b42010-11-16 03:52:51 +000012643
cristy3ed852e2009-09-05 21:47:34 +000012644 else
12645 PNGLong(chunk+28,9L); /* simplicity=VLC */
12646 }
glennrp0fe50b42010-11-16 03:52:51 +000012647
cristy3ed852e2009-09-05 21:47:34 +000012648 else
12649 {
12650 if (need_defi || mng_info->need_fram || use_global_plte)
12651 PNGLong(chunk+28,3L); /* simplicity=LC, no transparency */
glennrp0fe50b42010-11-16 03:52:51 +000012652
cristy3ed852e2009-09-05 21:47:34 +000012653 else
12654 PNGLong(chunk+28,1L); /* simplicity=VLC, no transparency */
12655 }
12656 }
12657 (void) WriteBlob(image,32,chunk);
12658 (void) WriteBlobMSBULong(image,crc32(0,chunk,32));
12659 option=GetImageOption(image_info,"mng:need-cacheoff");
12660 if (option != (const char *) NULL)
12661 {
12662 size_t
12663 length;
12664
12665 /*
12666 Write "nEED CACHEOFF" to turn playback caching off for streaming MNG.
12667 */
12668 PNGType(chunk,mng_nEED);
12669 length=CopyMagickString((char *) chunk+4,"CACHEOFF",20);
cristybb503372010-05-27 20:51:26 +000012670 (void) WriteBlobMSBULong(image,(size_t) length);
glennrp03812ae2010-12-24 01:31:34 +000012671 LogPNGChunk(logging,mng_nEED,(size_t) length);
cristy3ed852e2009-09-05 21:47:34 +000012672 length+=4;
12673 (void) WriteBlob(image,length,chunk);
12674 (void) WriteBlobMSBULong(image,crc32(0,chunk,(uInt) length));
12675 }
12676 if ((GetPreviousImageInList(image) == (Image *) NULL) &&
12677 (GetNextImageInList(image) != (Image *) NULL) &&
12678 (image->iterations != 1))
12679 {
12680 /*
12681 Write MNG TERM chunk
12682 */
12683 (void) WriteBlobMSBULong(image,10L); /* data length=10 */
12684 PNGType(chunk,mng_TERM);
glennrp03812ae2010-12-24 01:31:34 +000012685 LogPNGChunk(logging,mng_TERM,10L);
cristy3ed852e2009-09-05 21:47:34 +000012686 chunk[4]=3; /* repeat animation */
12687 chunk[5]=0; /* show last frame when done */
12688 PNGLong(chunk+6,(png_uint_32) (mng_info->ticks_per_second*
12689 final_delay/MagickMax(image->ticks_per_second,1)));
glennrp0fe50b42010-11-16 03:52:51 +000012690
cristy3ed852e2009-09-05 21:47:34 +000012691 if (image->iterations == 0)
12692 PNGLong(chunk+10,PNG_UINT_31_MAX);
glennrp0fe50b42010-11-16 03:52:51 +000012693
cristy3ed852e2009-09-05 21:47:34 +000012694 else
12695 PNGLong(chunk+10,(png_uint_32) image->iterations);
glennrp0fe50b42010-11-16 03:52:51 +000012696
cristy3ed852e2009-09-05 21:47:34 +000012697 if (logging != MagickFalse)
12698 {
12699 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +000012700 " TERM delay: %.20g",(double) (mng_info->ticks_per_second*
12701 final_delay/MagickMax(image->ticks_per_second,1)));
glennrp0fe50b42010-11-16 03:52:51 +000012702
cristy3ed852e2009-09-05 21:47:34 +000012703 if (image->iterations == 0)
12704 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +000012705 " TERM iterations: %.20g",(double) PNG_UINT_31_MAX);
glennrp0fe50b42010-11-16 03:52:51 +000012706
cristy3ed852e2009-09-05 21:47:34 +000012707 else
12708 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
cristye8c25f92010-06-03 00:53:06 +000012709 " Image iterations: %.20g",(double) image->iterations);
cristy3ed852e2009-09-05 21:47:34 +000012710 }
12711 (void) WriteBlob(image,14,chunk);
12712 (void) WriteBlobMSBULong(image,crc32(0,chunk,14));
12713 }
12714 /*
12715 To do: check for cHRM+gAMA == sRGB, and write sRGB instead.
12716 */
12717 if ((image->colorspace == sRGBColorspace || image->rendering_intent) &&
12718 mng_info->equal_srgbs)
12719 {
12720 /*
12721 Write MNG sRGB chunk
12722 */
12723 (void) WriteBlobMSBULong(image,1L);
12724 PNGType(chunk,mng_sRGB);
glennrp03812ae2010-12-24 01:31:34 +000012725 LogPNGChunk(logging,mng_sRGB,1L);
glennrp0fe50b42010-11-16 03:52:51 +000012726
cristy3ed852e2009-09-05 21:47:34 +000012727 if (image->rendering_intent != UndefinedIntent)
glennrpe610a072010-08-05 17:08:46 +000012728 chunk[4]=(unsigned char)
glennrpcf002022011-01-30 02:38:15 +000012729 Magick_RenderingIntent_to_PNG_RenderingIntent(
glennrpe610a072010-08-05 17:08:46 +000012730 (image->rendering_intent));
glennrp0fe50b42010-11-16 03:52:51 +000012731
cristy3ed852e2009-09-05 21:47:34 +000012732 else
glennrpe610a072010-08-05 17:08:46 +000012733 chunk[4]=(unsigned char)
glennrpcf002022011-01-30 02:38:15 +000012734 Magick_RenderingIntent_to_PNG_RenderingIntent(
12735 (PerceptualIntent));
glennrp0fe50b42010-11-16 03:52:51 +000012736
cristy3ed852e2009-09-05 21:47:34 +000012737 (void) WriteBlob(image,5,chunk);
12738 (void) WriteBlobMSBULong(image,crc32(0,chunk,5));
12739 mng_info->have_write_global_srgb=MagickTrue;
12740 }
glennrp0fe50b42010-11-16 03:52:51 +000012741
cristy3ed852e2009-09-05 21:47:34 +000012742 else
12743 {
12744 if (image->gamma && mng_info->equal_gammas)
12745 {
12746 /*
12747 Write MNG gAMA chunk
12748 */
12749 (void) WriteBlobMSBULong(image,4L);
12750 PNGType(chunk,mng_gAMA);
glennrp03812ae2010-12-24 01:31:34 +000012751 LogPNGChunk(logging,mng_gAMA,4L);
cristy35ef8242010-06-03 16:24:13 +000012752 PNGLong(chunk+4,(png_uint_32) (100000*image->gamma+0.5));
cristy3ed852e2009-09-05 21:47:34 +000012753 (void) WriteBlob(image,8,chunk);
12754 (void) WriteBlobMSBULong(image,crc32(0,chunk,8));
12755 mng_info->have_write_global_gama=MagickTrue;
12756 }
12757 if (mng_info->equal_chrms)
12758 {
12759 PrimaryInfo
12760 primary;
12761
12762 /*
12763 Write MNG cHRM chunk
12764 */
12765 (void) WriteBlobMSBULong(image,32L);
12766 PNGType(chunk,mng_cHRM);
glennrp03812ae2010-12-24 01:31:34 +000012767 LogPNGChunk(logging,mng_cHRM,32L);
cristy3ed852e2009-09-05 21:47:34 +000012768 primary=image->chromaticity.white_point;
cristy35ef8242010-06-03 16:24:13 +000012769 PNGLong(chunk+4,(png_uint_32) (100000*primary.x+0.5));
12770 PNGLong(chunk+8,(png_uint_32) (100000*primary.y+0.5));
cristy3ed852e2009-09-05 21:47:34 +000012771 primary=image->chromaticity.red_primary;
cristy35ef8242010-06-03 16:24:13 +000012772 PNGLong(chunk+12,(png_uint_32) (100000*primary.x+0.5));
12773 PNGLong(chunk+16,(png_uint_32) (100000*primary.y+0.5));
cristy3ed852e2009-09-05 21:47:34 +000012774 primary=image->chromaticity.green_primary;
cristy35ef8242010-06-03 16:24:13 +000012775 PNGLong(chunk+20,(png_uint_32) (100000*primary.x+0.5));
12776 PNGLong(chunk+24,(png_uint_32) (100000*primary.y+0.5));
cristy3ed852e2009-09-05 21:47:34 +000012777 primary=image->chromaticity.blue_primary;
cristy35ef8242010-06-03 16:24:13 +000012778 PNGLong(chunk+28,(png_uint_32) (100000*primary.x+0.5));
12779 PNGLong(chunk+32,(png_uint_32) (100000*primary.y+0.5));
cristy3ed852e2009-09-05 21:47:34 +000012780 (void) WriteBlob(image,36,chunk);
12781 (void) WriteBlobMSBULong(image,crc32(0,chunk,36));
12782 mng_info->have_write_global_chrm=MagickTrue;
12783 }
12784 }
cristy2a11bef2011-10-28 18:33:11 +000012785 if (image->resolution.x && image->resolution.y && mng_info->equal_physs)
cristy3ed852e2009-09-05 21:47:34 +000012786 {
12787 /*
12788 Write MNG pHYs chunk
12789 */
12790 (void) WriteBlobMSBULong(image,9L);
12791 PNGType(chunk,mng_pHYs);
glennrp03812ae2010-12-24 01:31:34 +000012792 LogPNGChunk(logging,mng_pHYs,9L);
glennrp0fe50b42010-11-16 03:52:51 +000012793
cristy3ed852e2009-09-05 21:47:34 +000012794 if (image->units == PixelsPerInchResolution)
12795 {
cristy35ef8242010-06-03 16:24:13 +000012796 PNGLong(chunk+4,(png_uint_32)
cristy2a11bef2011-10-28 18:33:11 +000012797 (image->resolution.x*100.0/2.54+0.5));
glennrp0fe50b42010-11-16 03:52:51 +000012798
cristy35ef8242010-06-03 16:24:13 +000012799 PNGLong(chunk+8,(png_uint_32)
cristy2a11bef2011-10-28 18:33:11 +000012800 (image->resolution.y*100.0/2.54+0.5));
glennrp0fe50b42010-11-16 03:52:51 +000012801
cristy3ed852e2009-09-05 21:47:34 +000012802 chunk[12]=1;
12803 }
glennrp0fe50b42010-11-16 03:52:51 +000012804
cristy3ed852e2009-09-05 21:47:34 +000012805 else
12806 {
12807 if (image->units == PixelsPerCentimeterResolution)
12808 {
cristy35ef8242010-06-03 16:24:13 +000012809 PNGLong(chunk+4,(png_uint_32)
cristy2a11bef2011-10-28 18:33:11 +000012810 (image->resolution.x*100.0+0.5));
glennrp0fe50b42010-11-16 03:52:51 +000012811
cristy35ef8242010-06-03 16:24:13 +000012812 PNGLong(chunk+8,(png_uint_32)
cristy2a11bef2011-10-28 18:33:11 +000012813 (image->resolution.y*100.0+0.5));
glennrp0fe50b42010-11-16 03:52:51 +000012814
cristy3ed852e2009-09-05 21:47:34 +000012815 chunk[12]=1;
12816 }
glennrp0fe50b42010-11-16 03:52:51 +000012817
cristy3ed852e2009-09-05 21:47:34 +000012818 else
12819 {
cristy2a11bef2011-10-28 18:33:11 +000012820 PNGLong(chunk+4,(png_uint_32) (image->resolution.x+0.5));
12821 PNGLong(chunk+8,(png_uint_32) (image->resolution.y+0.5));
cristy3ed852e2009-09-05 21:47:34 +000012822 chunk[12]=0;
12823 }
12824 }
12825 (void) WriteBlob(image,13,chunk);
12826 (void) WriteBlobMSBULong(image,crc32(0,chunk,13));
12827 }
12828 /*
12829 Write MNG BACK chunk and global bKGD chunk, if the image is transparent
12830 or does not cover the entire frame.
12831 */
12832 if (write_mng && (image->matte || image->page.x > 0 ||
12833 image->page.y > 0 || (image->page.width &&
12834 (image->page.width+image->page.x < mng_info->page.width))
12835 || (image->page.height && (image->page.height+image->page.y
12836 < mng_info->page.height))))
12837 {
12838 (void) WriteBlobMSBULong(image,6L);
12839 PNGType(chunk,mng_BACK);
glennrp03812ae2010-12-24 01:31:34 +000012840 LogPNGChunk(logging,mng_BACK,6L);
cristy3ed852e2009-09-05 21:47:34 +000012841 red=ScaleQuantumToShort(image->background_color.red);
12842 green=ScaleQuantumToShort(image->background_color.green);
12843 blue=ScaleQuantumToShort(image->background_color.blue);
12844 PNGShort(chunk+4,red);
12845 PNGShort(chunk+6,green);
12846 PNGShort(chunk+8,blue);
12847 (void) WriteBlob(image,10,chunk);
12848 (void) WriteBlobMSBULong(image,crc32(0,chunk,10));
12849 if (mng_info->equal_backgrounds)
12850 {
12851 (void) WriteBlobMSBULong(image,6L);
12852 PNGType(chunk,mng_bKGD);
glennrp03812ae2010-12-24 01:31:34 +000012853 LogPNGChunk(logging,mng_bKGD,6L);
cristy3ed852e2009-09-05 21:47:34 +000012854 (void) WriteBlob(image,10,chunk);
12855 (void) WriteBlobMSBULong(image,crc32(0,chunk,10));
12856 }
12857 }
12858
12859#ifdef PNG_WRITE_EMPTY_PLTE_SUPPORTED
12860 if ((need_local_plte == MagickFalse) &&
12861 (image->storage_class == PseudoClass) &&
12862 (all_images_are_gray == MagickFalse))
12863 {
cristybb503372010-05-27 20:51:26 +000012864 size_t
cristy3ed852e2009-09-05 21:47:34 +000012865 data_length;
12866
12867 /*
12868 Write MNG PLTE chunk
12869 */
12870 data_length=3*image->colors;
12871 (void) WriteBlobMSBULong(image,data_length);
12872 PNGType(chunk,mng_PLTE);
glennrp03812ae2010-12-24 01:31:34 +000012873 LogPNGChunk(logging,mng_PLTE,data_length);
glennrp0fe50b42010-11-16 03:52:51 +000012874
cristybb503372010-05-27 20:51:26 +000012875 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +000012876 {
cristy5f07f702011-09-26 17:29:10 +000012877 chunk[4+i*3]=(unsigned char) (ScaleQuantumToChar(
12878 image->colormap[i].red) & 0xff);
12879 chunk[5+i*3]=(unsigned char) (ScaleQuantumToChar(
12880 image->colormap[i].green) & 0xff);
12881 chunk[6+i*3]=(unsigned char) (ScaleQuantumToChar(
12882 image->colormap[i].blue) & 0xff);
cristy3ed852e2009-09-05 21:47:34 +000012883 }
glennrp0fe50b42010-11-16 03:52:51 +000012884
cristy3ed852e2009-09-05 21:47:34 +000012885 (void) WriteBlob(image,data_length+4,chunk);
12886 (void) WriteBlobMSBULong(image,crc32(0,chunk,(uInt) (data_length+4)));
12887 mng_info->have_write_global_plte=MagickTrue;
12888 }
12889#endif
12890 }
12891 scene=0;
12892 mng_info->delay=0;
12893#if defined(PNG_WRITE_EMPTY_PLTE_SUPPORTED) || \
12894 defined(PNG_MNG_FEATURES_SUPPORTED)
12895 mng_info->equal_palettes=MagickFalse;
12896#endif
12897 do
12898 {
12899 if (mng_info->adjoin)
12900 {
12901#if defined(PNG_WRITE_EMPTY_PLTE_SUPPORTED) || \
12902 defined(PNG_MNG_FEATURES_SUPPORTED)
12903 /*
12904 If we aren't using a global palette for the entire MNG, check to
12905 see if we can use one for two or more consecutive images.
12906 */
12907 if (need_local_plte && use_global_plte && !all_images_are_gray)
12908 {
12909 if (mng_info->IsPalette)
12910 {
12911 /*
12912 When equal_palettes is true, this image has the same palette
12913 as the previous PseudoClass image
12914 */
12915 mng_info->have_write_global_plte=mng_info->equal_palettes;
12916 mng_info->equal_palettes=PalettesAreEqual(image,image->next);
12917 if (mng_info->equal_palettes && !mng_info->have_write_global_plte)
12918 {
12919 /*
12920 Write MNG PLTE chunk
12921 */
cristybb503372010-05-27 20:51:26 +000012922 size_t
cristy3ed852e2009-09-05 21:47:34 +000012923 data_length;
12924
12925 data_length=3*image->colors;
12926 (void) WriteBlobMSBULong(image,data_length);
12927 PNGType(chunk,mng_PLTE);
glennrp03812ae2010-12-24 01:31:34 +000012928 LogPNGChunk(logging,mng_PLTE,data_length);
glennrp0fe50b42010-11-16 03:52:51 +000012929
cristybb503372010-05-27 20:51:26 +000012930 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +000012931 {
12932 chunk[4+i*3]=ScaleQuantumToChar(image->colormap[i].red);
12933 chunk[5+i*3]=ScaleQuantumToChar(image->colormap[i].green);
12934 chunk[6+i*3]=ScaleQuantumToChar(image->colormap[i].blue);
12935 }
glennrp0fe50b42010-11-16 03:52:51 +000012936
cristy3ed852e2009-09-05 21:47:34 +000012937 (void) WriteBlob(image,data_length+4,chunk);
12938 (void) WriteBlobMSBULong(image,crc32(0,chunk,
12939 (uInt) (data_length+4)));
12940 mng_info->have_write_global_plte=MagickTrue;
12941 }
12942 }
12943 else
12944 mng_info->have_write_global_plte=MagickFalse;
12945 }
12946#endif
12947 if (need_defi)
12948 {
cristybb503372010-05-27 20:51:26 +000012949 ssize_t
cristy3ed852e2009-09-05 21:47:34 +000012950 previous_x,
12951 previous_y;
12952
12953 if (scene)
12954 {
12955 previous_x=mng_info->page.x;
12956 previous_y=mng_info->page.y;
12957 }
12958 else
12959 {
12960 previous_x=0;
12961 previous_y=0;
12962 }
12963 mng_info->page=image->page;
12964 if ((mng_info->page.x != previous_x) ||
12965 (mng_info->page.y != previous_y))
12966 {
12967 (void) WriteBlobMSBULong(image,12L); /* data length=12 */
12968 PNGType(chunk,mng_DEFI);
glennrp03812ae2010-12-24 01:31:34 +000012969 LogPNGChunk(logging,mng_DEFI,12L);
cristy3ed852e2009-09-05 21:47:34 +000012970 chunk[4]=0; /* object 0 MSB */
12971 chunk[5]=0; /* object 0 LSB */
12972 chunk[6]=0; /* visible */
12973 chunk[7]=0; /* abstract */
12974 PNGLong(chunk+8,(png_uint_32) mng_info->page.x);
12975 PNGLong(chunk+12,(png_uint_32) mng_info->page.y);
12976 (void) WriteBlob(image,16,chunk);
12977 (void) WriteBlobMSBULong(image,crc32(0,chunk,16));
12978 }
12979 }
12980 }
12981
12982 mng_info->write_mng=write_mng;
12983
12984 if ((int) image->dispose >= 3)
12985 mng_info->framing_mode=3;
12986
12987 if (mng_info->need_fram && mng_info->adjoin &&
12988 ((image->delay != mng_info->delay) ||
12989 (mng_info->framing_mode != mng_info->old_framing_mode)))
12990 {
12991 if (image->delay == mng_info->delay)
12992 {
12993 /*
12994 Write a MNG FRAM chunk with the new framing mode.
12995 */
12996 (void) WriteBlobMSBULong(image,1L); /* data length=1 */
12997 PNGType(chunk,mng_FRAM);
glennrp03812ae2010-12-24 01:31:34 +000012998 LogPNGChunk(logging,mng_FRAM,1L);
cristy3ed852e2009-09-05 21:47:34 +000012999 chunk[4]=(unsigned char) mng_info->framing_mode;
13000 (void) WriteBlob(image,5,chunk);
13001 (void) WriteBlobMSBULong(image,crc32(0,chunk,5));
13002 }
13003 else
13004 {
13005 /*
13006 Write a MNG FRAM chunk with the delay.
13007 */
13008 (void) WriteBlobMSBULong(image,10L); /* data length=10 */
13009 PNGType(chunk,mng_FRAM);
glennrp03812ae2010-12-24 01:31:34 +000013010 LogPNGChunk(logging,mng_FRAM,10L);
cristy3ed852e2009-09-05 21:47:34 +000013011 chunk[4]=(unsigned char) mng_info->framing_mode;
13012 chunk[5]=0; /* frame name separator (no name) */
13013 chunk[6]=2; /* flag for changing default delay */
13014 chunk[7]=0; /* flag for changing frame timeout */
13015 chunk[8]=0; /* flag for changing frame clipping */
13016 chunk[9]=0; /* flag for changing frame sync_id */
13017 PNGLong(chunk+10,(png_uint_32)
13018 ((mng_info->ticks_per_second*
13019 image->delay)/MagickMax(image->ticks_per_second,1)));
13020 (void) WriteBlob(image,14,chunk);
13021 (void) WriteBlobMSBULong(image,crc32(0,chunk,14));
cristy4e5bc842010-06-09 13:56:01 +000013022 mng_info->delay=(png_uint_32) image->delay;
cristy3ed852e2009-09-05 21:47:34 +000013023 }
13024 mng_info->old_framing_mode=mng_info->framing_mode;
13025 }
13026
13027#if defined(JNG_SUPPORTED)
13028 if (image_info->compression == JPEGCompression)
13029 {
13030 ImageInfo
13031 *write_info;
13032
13033 if (logging != MagickFalse)
13034 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
13035 " Writing JNG object.");
13036 /* To do: specify the desired alpha compression method. */
13037 write_info=CloneImageInfo(image_info);
13038 write_info->compression=UndefinedCompression;
cristy018f07f2011-09-04 21:15:19 +000013039 status=WriteOneJNGImage(mng_info,write_info,image,exception);
cristy3ed852e2009-09-05 21:47:34 +000013040 write_info=DestroyImageInfo(write_info);
13041 }
13042 else
13043#endif
13044 {
13045 if (logging != MagickFalse)
13046 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
13047 " Writing PNG object.");
glennrp2f2e5142010-12-23 19:13:35 +000013048
glennrpb9cfe272010-12-21 15:08:06 +000013049 mng_info->need_blob = MagickFalse;
glennrp8d3d6e52011-04-19 04:39:51 +000013050 mng_info->ping_preserve_colormap = MagickFalse;
glennrp2f2e5142010-12-23 19:13:35 +000013051
13052 /* We don't want any ancillary chunks written */
13053 mng_info->ping_exclude_bKGD=MagickTrue;
13054 mng_info->ping_exclude_cHRM=MagickTrue;
glennrpa0ed0092011-04-18 16:36:29 +000013055 mng_info->ping_exclude_date=MagickTrue;
glennrp2f2e5142010-12-23 19:13:35 +000013056 mng_info->ping_exclude_EXIF=MagickTrue;
13057 mng_info->ping_exclude_gAMA=MagickTrue;
glennrp2f2e5142010-12-23 19:13:35 +000013058 mng_info->ping_exclude_iCCP=MagickTrue;
13059 /* mng_info->ping_exclude_iTXt=MagickTrue; */
13060 mng_info->ping_exclude_oFFs=MagickTrue;
13061 mng_info->ping_exclude_pHYs=MagickTrue;
13062 mng_info->ping_exclude_sRGB=MagickTrue;
13063 mng_info->ping_exclude_tEXt=MagickTrue;
glennrpa1e3b7b2010-12-24 16:37:33 +000013064 mng_info->ping_exclude_tRNS=MagickTrue;
glennrp2f2e5142010-12-23 19:13:35 +000013065 mng_info->ping_exclude_vpAg=MagickTrue;
13066 mng_info->ping_exclude_zCCP=MagickTrue;
13067 mng_info->ping_exclude_zTXt=MagickTrue;
13068
cristy018f07f2011-09-04 21:15:19 +000013069 status=WriteOnePNGImage(mng_info,image_info,image,exception);
cristy3ed852e2009-09-05 21:47:34 +000013070 }
13071
13072 if (status == MagickFalse)
13073 {
13074 MngInfoFreeStruct(mng_info,&have_mng_structure);
13075 (void) CloseBlob(image);
13076 return(MagickFalse);
13077 }
13078 (void) CatchImageException(image);
13079 if (GetNextImageInList(image) == (Image *) NULL)
13080 break;
13081 image=SyncNextImageInList(image);
13082 status=SetImageProgress(image,SaveImagesTag,scene++,
13083 GetImageListLength(image));
glennrp0fe50b42010-11-16 03:52:51 +000013084
cristy3ed852e2009-09-05 21:47:34 +000013085 if (status == MagickFalse)
13086 break;
glennrp0fe50b42010-11-16 03:52:51 +000013087
cristy3ed852e2009-09-05 21:47:34 +000013088 } while (mng_info->adjoin);
glennrp0fe50b42010-11-16 03:52:51 +000013089
cristy3ed852e2009-09-05 21:47:34 +000013090 if (write_mng)
13091 {
13092 while (GetPreviousImageInList(image) != (Image *) NULL)
13093 image=GetPreviousImageInList(image);
13094 /*
13095 Write the MEND chunk.
13096 */
13097 (void) WriteBlobMSBULong(image,0x00000000L);
13098 PNGType(chunk,mng_MEND);
glennrp03812ae2010-12-24 01:31:34 +000013099 LogPNGChunk(logging,mng_MEND,0L);
cristy3ed852e2009-09-05 21:47:34 +000013100 (void) WriteBlob(image,4,chunk);
13101 (void) WriteBlobMSBULong(image,crc32(0,chunk,4));
13102 }
13103 /*
13104 Relinquish resources.
13105 */
13106 (void) CloseBlob(image);
13107 MngInfoFreeStruct(mng_info,&have_mng_structure);
glennrp0fe50b42010-11-16 03:52:51 +000013108
cristy3ed852e2009-09-05 21:47:34 +000013109 if (logging != MagickFalse)
13110 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"exit WriteMNGImage()");
glennrp0fe50b42010-11-16 03:52:51 +000013111
cristy3ed852e2009-09-05 21:47:34 +000013112 return(MagickTrue);
13113}
glennrpd5045b42010-03-24 12:40:35 +000013114#else /* PNG_LIBPNG_VER > 10011 */
glennrp39992b42010-11-14 00:03:43 +000013115
cristy3ed852e2009-09-05 21:47:34 +000013116static MagickBooleanType WritePNGImage(const ImageInfo *image_info,Image *image)
13117{
glennrp3bd393f2011-12-21 18:54:53 +000013118 (void) image;
cristy3ed852e2009-09-05 21:47:34 +000013119 printf("Your PNG library is too old: You have libpng-%s\n",
13120 PNG_LIBPNG_VER_STRING);
glennrp0fe50b42010-11-16 03:52:51 +000013121
cristy3ed852e2009-09-05 21:47:34 +000013122 ThrowBinaryException(CoderError,"PNG library is too old",
13123 image_info->filename);
13124}
glennrp39992b42010-11-14 00:03:43 +000013125
cristy3ed852e2009-09-05 21:47:34 +000013126static MagickBooleanType WriteMNGImage(const ImageInfo *image_info,Image *image)
13127{
13128 return(WritePNGImage(image_info,image));
13129}
glennrpd5045b42010-03-24 12:40:35 +000013130#endif /* PNG_LIBPNG_VER > 10011 */
cristy3ed852e2009-09-05 21:47:34 +000013131#endif