]> granicus.if.org Git - imagemagick/blob - coders/png.c
Fixed two other instances of throwing exception to image instead if IMimage.
[imagemagick] / coders / png.c
1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %                                                                             %
4 %                                                                             %
5 %                                                                             %
6 %                            PPPP   N   N   GGGG                              %
7 %                            P   P  NN  N  G                                  %
8 %                            PPPP   N N N  G  GG                              %
9 %                            P      N  NN  G   G                              %
10 %                            P      N   N   GGG                               %
11 %                                                                             %
12 %                                                                             %
13 %              Read/Write Portable Network Graphics Image Format              %
14 %                                                                             %
15 %                              Software Design                                %
16 %                                John Cristy                                  %
17 %                           Glenn Randers-Pehrson                             %
18 %                               November 1997                                 %
19 %                                                                             %
20 %                                                                             %
21 %  Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization      %
22 %  dedicated to making software imaging solutions freely available.           %
23 %                                                                             %
24 %  You may not use this file except in compliance with the License.  You may  %
25 %  obtain a copy of the License at                                            %
26 %                                                                             %
27 %    http://www.imagemagick.org/script/license.php                            %
28 %                                                                             %
29 %  Unless required by applicable law or agreed to in writing, software        %
30 %  distributed under the License is distributed on an "AS IS" BASIS,          %
31 %  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   %
32 %  See the License for the specific language governing permissions and        %
33 %  limitations under the License.                                             %
34 %                                                                             %
35 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
36 %
37 %
38 */
39
40 \f
41 /*
42   Include declarations.
43 */
44 #include "magick/studio.h"
45 #include "magick/artifact.h"
46 #include "magick/attribute.h"
47 #include "magick/blob.h"
48 #include "magick/blob-private.h"
49 #include "magick/cache.h"
50 #include "magick/color.h"
51 #include "magick/color-private.h"
52 #include "magick/colormap.h"
53 #include "magick/colorspace.h"
54 #include "magick/constitute.h"
55 #include "magick/enhance.h"
56 #include "magick/exception.h"
57 #include "magick/exception-private.h"
58 #include "magick/geometry.h"
59 #include "magick/histogram.h"
60 #include "magick/image.h"
61 #include "magick/image-private.h"
62 #include "magick/layer.h"
63 #include "magick/list.h"
64 #include "magick/log.h"
65 #include "magick/magick.h"
66 #include "magick/memory_.h"
67 #include "magick/module.h"
68 #include "magick/monitor.h"
69 #include "magick/monitor-private.h"
70 #include "magick/option.h"
71 #include "magick/quantum-private.h"
72 #include "magick/profile.h"
73 #include "magick/property.h"
74 #include "magick/resource_.h"
75 #include "magick/semaphore.h"
76 #include "magick/quantum-private.h"
77 #include "magick/static.h"
78 #include "magick/statistic.h"
79 #include "magick/string_.h"
80 #include "magick/string-private.h"
81 #include "magick/transform.h"
82 #include "magick/utility.h"
83 #if defined(MAGICKCORE_PNG_DELEGATE)
84
85 /* Suppress libpng pedantic warnings that were added in
86  * libpng-1.2.41 and libpng-1.4.0.  If you are working on
87  * migration to libpng-1.5, remove these defines and then
88  * fix any code that generates warnings.
89  */
90 /* #define PNG_DEPRECATED   Use of this function is deprecated */
91 /* #define PNG_USE_RESULT   The result of this function must be checked */
92 /* #define PNG_NORETURN     This function does not return */
93 /* #define PNG_ALLOCATED    The result of the function is new memory */
94 /* #define PNG_DEPSTRUCT    Access to this struct member is deprecated */
95
96 #include "png.h"
97 #include "zlib.h"
98 \f
99 /* ImageMagick differences */
100 #define first_scene scene
101
102 #if PNG_LIBPNG_VER > 10011
103 /*
104   Optional declarations. Define or undefine them as you like.
105 */
106 /* #define PNG_DEBUG -- turning this on breaks VisualC compiling */
107
108 /*
109   Features under construction.  Define these to work on them.
110 */
111 #undef MNG_OBJECT_BUFFERS
112 #undef MNG_BASI_SUPPORTED
113 #define MNG_COALESCE_LAYERS /* In 5.4.4, this interfered with MMAP'ed files. */
114 #define MNG_INSERT_LAYERS   /* Troublesome, but seem to work as of 5.4.4 */
115 #if defined(MAGICKCORE_JPEG_DELEGATE)
116 #  define JNG_SUPPORTED /* Not finished as of 5.5.2.  See "To do" comments. */
117 #endif
118 #if !defined(RGBColorMatchExact)
119 #define IsPNGColorEqual(color,target) \
120    (((color).red == (target).red) && \
121     ((color).green == (target).green) && \
122     ((color).blue == (target).blue))
123 #endif
124
125 /*
126   Establish thread safety.
127   setjmp/longjmp is claimed to be safe on these platforms:
128   setjmp/longjmp is alleged to be unsafe on these platforms:
129 */
130 #ifndef SETJMP_IS_THREAD_SAFE
131 #define PNG_SETJMP_NOT_THREAD_SAFE
132 #endif
133
134 #if defined(PNG_SETJMP_NOT_THREAD_SAFE)
135 static SemaphoreInfo
136   *ping_semaphore = (SemaphoreInfo *) NULL;
137 #endif
138
139 /*
140   This temporary until I set up malloc'ed object attributes array.
141   Recompile with MNG_MAX_OBJECTS=65536L to avoid this limit but
142   waste more memory.
143 */
144 #define MNG_MAX_OBJECTS 256
145
146 /*
147   If this not defined, spec is interpreted strictly.  If it is
148   defined, an attempt will be made to recover from some errors,
149   including
150       o global PLTE too short
151 */
152 #undef MNG_LOOSE
153
154 /*
155   Don't try to define PNG_MNG_FEATURES_SUPPORTED here.  Make sure
156   it's defined in libpng/pngconf.h, version 1.0.9 or later.  It won't work
157   with earlier versions of libpng.  From libpng-1.0.3a to libpng-1.0.8,
158   PNG_READ|WRITE_EMPTY_PLTE were used but those have been deprecated in
159   libpng in favor of PNG_MNG_FEATURES_SUPPORTED, so we set them here.
160   PNG_MNG_FEATURES_SUPPORTED is disabled by default in libpng-1.0.9 and
161   will be enabled by default in libpng-1.2.0.
162 */
163 #ifdef PNG_MNG_FEATURES_SUPPORTED
164 #  ifndef PNG_READ_EMPTY_PLTE_SUPPORTED
165 #    define PNG_READ_EMPTY_PLTE_SUPPORTED
166 #  endif
167 #  ifndef PNG_WRITE_EMPTY_PLTE_SUPPORTED
168 #    define PNG_WRITE_EMPTY_PLTE_SUPPORTED
169 #  endif
170 #endif
171
172 /*
173   Maximum valid size_t in PNG/MNG chunks is (2^31)-1
174   This macro is only defined in libpng-1.0.3 and later.
175   Previously it was PNG_MAX_UINT but that was deprecated in libpng-1.2.6
176 */
177 #ifndef PNG_UINT_31_MAX
178 #define PNG_UINT_31_MAX (png_uint_32) 0x7fffffffL
179 #endif
180
181 /*
182   Constant strings for known chunk types.  If you need to add a chunk,
183   add a string holding the name here.   To make the code more
184   portable, we use ASCII numbers like this, not characters.
185 */
186
187 static png_byte FARDATA mng_MHDR[5]={ 77,  72,  68,  82, (png_byte) '\0'};
188 static png_byte FARDATA mng_BACK[5]={ 66,  65,  67,  75, (png_byte) '\0'};
189 static png_byte FARDATA mng_BASI[5]={ 66,  65,  83,  73, (png_byte) '\0'};
190 static png_byte FARDATA mng_CLIP[5]={ 67,  76,  73,  80, (png_byte) '\0'};
191 static png_byte FARDATA mng_CLON[5]={ 67,  76,  79,  78, (png_byte) '\0'};
192 static png_byte FARDATA mng_DEFI[5]={ 68,  69,  70,  73, (png_byte) '\0'};
193 static png_byte FARDATA mng_DHDR[5]={ 68,  72,  68,  82, (png_byte) '\0'};
194 static png_byte FARDATA mng_DISC[5]={ 68,  73,  83,  67, (png_byte) '\0'};
195 static png_byte FARDATA mng_ENDL[5]={ 69,  78,  68,  76, (png_byte) '\0'};
196 static png_byte FARDATA mng_FRAM[5]={ 70,  82,  65,  77, (png_byte) '\0'};
197 static png_byte FARDATA mng_IEND[5]={ 73,  69,  78,  68, (png_byte) '\0'};
198 static png_byte FARDATA mng_IHDR[5]={ 73,  72,  68,  82, (png_byte) '\0'};
199 static png_byte FARDATA mng_JHDR[5]={ 74,  72,  68,  82, (png_byte) '\0'};
200 static png_byte FARDATA mng_LOOP[5]={ 76,  79,  79,  80, (png_byte) '\0'};
201 static png_byte FARDATA mng_MAGN[5]={ 77,  65,  71,  78, (png_byte) '\0'};
202 static png_byte FARDATA mng_MEND[5]={ 77,  69,  78,  68, (png_byte) '\0'};
203 static png_byte FARDATA mng_MOVE[5]={ 77,  79,  86,  69, (png_byte) '\0'};
204 static png_byte FARDATA mng_PAST[5]={ 80,  65,  83,  84, (png_byte) '\0'};
205 static png_byte FARDATA mng_PLTE[5]={ 80,  76,  84,  69, (png_byte) '\0'};
206 static png_byte FARDATA mng_SAVE[5]={ 83,  65,  86,  69, (png_byte) '\0'};
207 static png_byte FARDATA mng_SEEK[5]={ 83,  69,  69,  75, (png_byte) '\0'};
208 static png_byte FARDATA mng_SHOW[5]={ 83,  72,  79,  87, (png_byte) '\0'};
209 static png_byte FARDATA mng_TERM[5]={ 84,  69,  82,  77, (png_byte) '\0'};
210 static png_byte FARDATA mng_bKGD[5]={ 98,  75,  71,  68, (png_byte) '\0'};
211 static png_byte FARDATA mng_cHRM[5]={ 99,  72,  82,  77, (png_byte) '\0'};
212 static png_byte FARDATA mng_gAMA[5]={103,  65,  77,  65, (png_byte) '\0'};
213 static png_byte FARDATA mng_iCCP[5]={105,  67,  67,  80, (png_byte) '\0'};
214 static png_byte FARDATA mng_nEED[5]={110,  69,  69,  68, (png_byte) '\0'};
215 static png_byte FARDATA mng_pHYg[5]={112,  72,  89, 103, (png_byte) '\0'};
216 static png_byte FARDATA mng_vpAg[5]={118, 112,  65, 103, (png_byte) '\0'};
217 static png_byte FARDATA mng_pHYs[5]={112,  72,  89, 115, (png_byte) '\0'};
218 static png_byte FARDATA mng_sBIT[5]={115,  66,  73,  84, (png_byte) '\0'};
219 static png_byte FARDATA mng_sRGB[5]={115,  82,  71,  66, (png_byte) '\0'};
220 static png_byte FARDATA mng_tRNS[5]={116,  82,  78,  83, (png_byte) '\0'};
221
222 #if defined(JNG_SUPPORTED)
223 static png_byte FARDATA mng_IDAT[5]={ 73,  68,  65,  84, (png_byte) '\0'};
224 static png_byte FARDATA mng_JDAT[5]={ 74,  68,  65,  84, (png_byte) '\0'};
225 static png_byte FARDATA mng_JDAA[5]={ 74,  68,  65,  65, (png_byte) '\0'};
226 static png_byte FARDATA mng_JdAA[5]={ 74, 100,  65,  65, (png_byte) '\0'};
227 static png_byte FARDATA mng_JSEP[5]={ 74,  83,  69,  80, (png_byte) '\0'};
228 static png_byte FARDATA mng_oFFs[5]={111,  70,  70, 115, (png_byte) '\0'};
229 #endif
230
231 /*
232 Other known chunks that are not yet supported by ImageMagick:
233 static png_byte FARDATA mng_hIST[5]={104,  73,  83,  84, (png_byte) '\0'};
234 static png_byte FARDATA mng_iCCP[5]={105,  67,  67,  80, (png_byte) '\0'};
235 static png_byte FARDATA mng_iTXt[5]={105,  84,  88, 116, (png_byte) '\0'};
236 static png_byte FARDATA mng_sPLT[5]={115,  80,  76,  84, (png_byte) '\0'};
237 static png_byte FARDATA mng_sTER[5]={115,  84,  69,  82, (png_byte) '\0'};
238 static png_byte FARDATA mng_tEXt[5]={116,  69,  88, 116, (png_byte) '\0'};
239 static png_byte FARDATA mng_tIME[5]={116,  73,  77,  69, (png_byte) '\0'};
240 static png_byte FARDATA mng_zTXt[5]={122,  84,  88, 116, (png_byte) '\0'};
241 */
242
243 typedef struct _MngBox
244 {
245   long
246     left,
247     right,
248     top,
249     bottom;
250 } MngBox;
251
252 typedef struct _MngPair
253 {
254   volatile long
255     a,
256     b;
257 } MngPair;
258
259 #ifdef MNG_OBJECT_BUFFERS
260 typedef struct _MngBuffer
261 {
262
263   size_t
264     height,
265     width;
266
267   Image
268     *image;
269
270   png_color
271     plte[256];
272
273   int
274     reference_count;
275
276   unsigned char
277     alpha_sample_depth,
278     compression_method,
279     color_type,
280     concrete,
281     filter_method,
282     frozen,
283     image_type,
284     interlace_method,
285     pixel_sample_depth,
286     plte_length,
287     sample_depth,
288     viewable;
289 } MngBuffer;
290 #endif
291
292 typedef struct _MngInfo
293 {
294
295 #ifdef MNG_OBJECT_BUFFERS
296   MngBuffer
297     *ob[MNG_MAX_OBJECTS];
298 #endif
299
300   Image *
301     image;
302
303   RectangleInfo
304     page;
305
306   int
307     adjoin,
308 #ifndef PNG_READ_EMPTY_PLTE_SUPPORTED
309     bytes_in_read_buffer,
310     found_empty_plte,
311 #endif
312     equal_backgrounds,
313     equal_chrms,
314     equal_gammas,
315 #if defined(PNG_WRITE_EMPTY_PLTE_SUPPORTED) || \
316     defined(PNG_MNG_FEATURES_SUPPORTED)
317     equal_palettes,
318 #endif
319     equal_physs,
320     equal_srgbs,
321     framing_mode,
322     have_global_bkgd,
323     have_global_chrm,
324     have_global_gama,
325     have_global_phys,
326     have_global_sbit,
327     have_global_srgb,
328     have_saved_bkgd_index,
329     have_write_global_chrm,
330     have_write_global_gama,
331     have_write_global_plte,
332     have_write_global_srgb,
333     need_fram,
334     object_id,
335     old_framing_mode,
336     saved_bkgd_index;
337
338   int
339     new_number_colors;
340
341   ssize_t
342     image_found,
343     loop_count[256],
344     loop_iteration[256],
345     scenes_found,
346     x_off[MNG_MAX_OBJECTS],
347     y_off[MNG_MAX_OBJECTS];
348
349   MngBox
350     clip,
351     frame,
352     image_box,
353     object_clip[MNG_MAX_OBJECTS];
354
355   unsigned char
356     /* These flags could be combined into one byte */
357     exists[MNG_MAX_OBJECTS],
358     frozen[MNG_MAX_OBJECTS],
359     loop_active[256],
360     invisible[MNG_MAX_OBJECTS],
361     viewable[MNG_MAX_OBJECTS];
362
363   MagickOffsetType
364     loop_jump[256];
365
366   png_colorp
367     global_plte;
368
369   png_color_8
370     global_sbit;
371
372   png_byte
373 #ifndef PNG_READ_EMPTY_PLTE_SUPPORTED
374     read_buffer[8],
375 #endif
376     global_trns[256];
377
378   float
379     global_gamma;
380
381   ChromaticityInfo
382     global_chrm;
383
384   RenderingIntent
385     global_srgb_intent;
386
387   unsigned int
388     delay,
389     global_plte_length,
390     global_trns_length,
391     global_x_pixels_per_unit,
392     global_y_pixels_per_unit,
393     mng_width,
394     mng_height,
395     ticks_per_second;
396
397   MagickBooleanType
398     need_blob;
399
400   unsigned int
401     IsPalette,
402     global_phys_unit_type,
403     basi_warning,
404     clon_warning,
405     dhdr_warning,
406     jhdr_warning,
407     magn_warning,
408     past_warning,
409     phyg_warning,
410     phys_warning,
411     sbit_warning,
412     show_warning,
413     mng_type,
414     write_mng,
415     write_png_colortype,
416     write_png_depth,
417     write_png8,
418     write_png24,
419     write_png32;
420
421 #ifdef MNG_BASI_SUPPORTED
422   size_t
423     basi_width,
424     basi_height;
425
426   unsigned int
427     basi_depth,
428     basi_color_type,
429     basi_compression_method,
430     basi_filter_type,
431     basi_interlace_method,
432     basi_red,
433     basi_green,
434     basi_blue,
435     basi_alpha,
436     basi_viewable;
437 #endif
438
439   png_uint_16
440     magn_first,
441     magn_last,
442     magn_mb,
443     magn_ml,
444     magn_mr,
445     magn_mt,
446     magn_mx,
447     magn_my,
448     magn_methx,
449     magn_methy;
450
451   PixelPacket
452     mng_global_bkgd;
453
454   /* Added at version 6.6.6-7 */
455   MagickBooleanType
456     ping_exclude_bKGD,
457     ping_exclude_cHRM,
458     ping_exclude_EXIF,
459     ping_exclude_gAMA,
460     ping_exclude_iCCP,
461     /* ping_exclude_iTXt, */
462     ping_exclude_oFFs,
463     ping_exclude_pHYs,
464     ping_exclude_sRGB,
465     ping_exclude_tEXt,
466     ping_exclude_tRNS,
467     ping_exclude_vpAg,
468     ping_exclude_zCCP, /* hex-encoded iCCP */
469     ping_exclude_zTXt;
470
471 } MngInfo;
472 #endif /* VER */
473 \f
474 /*
475   Forward declarations.
476 */
477 static MagickBooleanType
478   WritePNGImage(const ImageInfo *,Image *);
479
480 static MagickBooleanType
481   WriteMNGImage(const ImageInfo *,Image *);
482
483 #if defined(JNG_SUPPORTED)
484 static MagickBooleanType
485   WriteJNGImage(const ImageInfo *,Image *);
486 #endif
487
488 #if PNG_LIBPNG_VER > 10011
489
490
491 #if (MAGICKCORE_QUANTUM_DEPTH >= 16)
492 static MagickBooleanType
493 LosslessReduceDepthOK(Image *image)
494 {
495     MagickBooleanType
496       ok_to_reduce=MagickFalse;
497
498     /* Reduce bit depth if it can be reduced losslessly from 16 to 8.
499      * Note that the method GetImageDepth doesn't check background
500      * and doesn't handle PseudoClass specially.  Also it uses
501      * multiplication and division by 257 instead of shifting, so
502      * might be slower.
503      */
504
505     if (image->depth == 16)
506       {
507
508         const PixelPacket
509           *p;
510
511         ok_to_reduce=
512           (((((size_t) image->background_color.red >> 8) & 0xff)
513           == ((size_t) image->background_color.red & 0xff)) &&
514            ((((size_t) image->background_color.green >> 8) & 0xff)
515           == ((size_t) image->background_color.green & 0xff)) &&
516            ((((size_t) image->background_color.blue >> 8) & 0xff)
517           == ((size_t) image->background_color.blue & 0xff))) ? MagickTrue :
518           MagickFalse;
519
520         if (ok_to_reduce != MagickFalse && image->storage_class == PseudoClass)
521           {
522             int indx;
523
524             for (indx=0; indx < (ssize_t) image->colors; indx++)
525               {
526                 ok_to_reduce=(((((size_t) image->colormap[indx].red >>
527                     8) & 0xff)
528                   == ((size_t) image->colormap[indx].red & 0xff)) &&
529                   ((((size_t) image->colormap[indx].green >> 8) & 0xff)
530                   == ((size_t) image->colormap[indx].green & 0xff)) &&
531                   ((((size_t) image->colormap[indx].blue >> 8) & 0xff)
532                   == ((size_t) image->colormap[indx].blue & 0xff)) &&
533                   (image->matte == MagickFalse ||
534                   (((size_t) image->colormap[indx].opacity >> 8) & 0xff)
535                   == ((size_t) image->colormap[indx].opacity & 0xff))) ?
536                   MagickTrue : MagickFalse;
537                 if (ok_to_reduce == MagickFalse)
538                   break;
539               }
540           }
541
542         if ((ok_to_reduce != MagickFalse) &&
543             (image->storage_class != PseudoClass))
544           {
545             ssize_t
546               y;
547
548             register ssize_t
549               x;
550
551             for (y=0; y < (ssize_t) image->rows; y++)
552             {
553               p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
554
555               if (p == (const PixelPacket *) NULL)
556                 {
557                   ok_to_reduce = MagickFalse;
558                   break;
559                 }
560
561               for (x=(ssize_t) image->columns-1; x >= 0; x--)
562               {
563                 ok_to_reduce=((
564                   (((size_t) p->red >> 8) & 0xff) ==
565                   ((size_t) p->red & 0xff)) &&
566                   ((((size_t) p->green >> 8) & 0xff) ==
567                   ((size_t) p->green & 0xff)) &&
568                   ((((size_t) p->blue >> 8) & 0xff) ==
569                   ((size_t) p->blue & 0xff)) &&
570                   (((image->matte == MagickFalse ||
571                   (((size_t) p->opacity >> 8) & 0xff) ==
572                   ((size_t) p->opacity & 0xff))))) ? MagickTrue : MagickFalse;
573
574                 if (ok_to_reduce == MagickFalse)
575                   break;
576
577                 p++;
578               }
579               if (x >= 0)
580                 break;
581             }
582           }
583
584         if (ok_to_reduce != MagickFalse)
585           {
586             (void) LogMagickEvent(CoderEvent,GetMagickModule(),
587                 "    OK to reduce PNG bit depth to 8 without loss of info");
588           }
589         else
590           {
591             (void) LogMagickEvent(CoderEvent,GetMagickModule(),
592                 "    Not OK to reduce PNG bit depth to 8 without loss of info");
593           }
594       }
595
596     return ok_to_reduce;
597 }
598 #endif /* MAGICKCORE_QUANTUM_DEPTH >= 16 */
599
600 static int
601 Magick_RenderingIntent_to_PNG_RenderingIntent(const RenderingIntent intent)
602 {
603   switch (intent)
604   {
605     case PerceptualIntent:
606        return 0;
607
608     case RelativeIntent:
609        return 1;
610
611     case SaturationIntent:
612        return 2;
613
614     case AbsoluteIntent:
615        return 3;
616
617     default:
618        return -1;
619   }
620 }
621
622 static RenderingIntent
623 Magick_RenderingIntent_from_PNG_RenderingIntent(const int ping_intent)
624 {
625   switch (ping_intent)
626   {
627     case 0:
628       return PerceptualIntent;
629
630     case 1:
631       return RelativeIntent;
632
633     case 2:
634       return SaturationIntent;
635
636     case 3:
637       return AbsoluteIntent;
638
639     default:
640       return UndefinedIntent;
641     }
642 }
643
644 static inline ssize_t MagickMax(const ssize_t x,const ssize_t y)
645 {
646   if (x > y)
647     return(x);
648
649   return(y);
650 }
651
652 static inline ssize_t MagickMin(const ssize_t x,const ssize_t y)
653 {
654   if (x < y)
655     return(x);
656
657   return(y);
658 }
659
660 \f
661 /*
662 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
663 %                                                                             %
664 %                                                                             %
665 %                                                                             %
666 %   I m a g e I s G r a y                                                     %
667 %                                                                             %
668 %                                                                             %
669 %                                                                             %
670 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
671 %                                                                             %
672 %   Like IsGrayImage except does not change DirectClass to PseudoClass        %
673 %                                                                             %
674 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
675 */
676 static MagickBooleanType ImageIsGray(Image *image)
677 {
678   register const PixelPacket
679     *p;
680
681   register ssize_t
682     i,
683     x,
684     y;
685
686   assert(image != (Image *) NULL);
687   assert(image->signature == MagickSignature);
688   if (image->debug != MagickFalse)
689     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
690
691   if (image->storage_class == PseudoClass)
692     {
693       for (i=0; i < (ssize_t) image->colors; i++)
694         if (IsGray(image->colormap+i) == MagickFalse)
695           return(MagickFalse);
696       return(MagickTrue);
697     }
698   for (y=0; y < (ssize_t) image->rows; y++)
699   {
700     p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
701     if (p == (const PixelPacket *) NULL)
702       return(MagickFalse);
703     for (x=(ssize_t) image->columns-1; x >= 0; x--)
704     {
705        if (IsGray(p) == MagickFalse)
706           return(MagickFalse);
707        p++;
708     }
709   }
710   return(MagickTrue);
711 }
712 #endif /* PNG_LIBPNG_VER > 10011 */
713 #endif /* MAGICKCORE_PNG_DELEGATE */
714 \f
715 /*
716 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
717 %                                                                             %
718 %                                                                             %
719 %                                                                             %
720 %   I s M N G                                                                 %
721 %                                                                             %
722 %                                                                             %
723 %                                                                             %
724 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
725 %
726 %  IsMNG() returns MagickTrue if the image format type, identified by the
727 %  magick string, is MNG.
728 %
729 %  The format of the IsMNG method is:
730 %
731 %      MagickBooleanType IsMNG(const unsigned char *magick,const size_t length)
732 %
733 %  A description of each parameter follows:
734 %
735 %    o magick: compare image format pattern against these bytes.
736 %
737 %    o length: Specifies the length of the magick string.
738 %
739 %
740 */
741 static MagickBooleanType IsMNG(const unsigned char *magick,const size_t length)
742 {
743   if (length < 8)
744     return(MagickFalse);
745
746   if (memcmp(magick,"\212MNG\r\n\032\n",8) == 0)
747     return(MagickTrue);
748
749   return(MagickFalse);
750 }
751 \f
752 /*
753 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
754 %                                                                             %
755 %                                                                             %
756 %                                                                             %
757 %   I s J N G                                                                 %
758 %                                                                             %
759 %                                                                             %
760 %                                                                             %
761 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
762 %
763 %  IsJNG() returns MagickTrue if the image format type, identified by the
764 %  magick string, is JNG.
765 %
766 %  The format of the IsJNG method is:
767 %
768 %      MagickBooleanType IsJNG(const unsigned char *magick,const size_t length)
769 %
770 %  A description of each parameter follows:
771 %
772 %    o magick: compare image format pattern against these bytes.
773 %
774 %    o length: Specifies the length of the magick string.
775 %
776 %
777 */
778 static MagickBooleanType IsJNG(const unsigned char *magick,const size_t length)
779 {
780   if (length < 8)
781     return(MagickFalse);
782
783   if (memcmp(magick,"\213JNG\r\n\032\n",8) == 0)
784     return(MagickTrue);
785
786   return(MagickFalse);
787 }
788 \f
789 /*
790 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
791 %                                                                             %
792 %                                                                             %
793 %                                                                             %
794 %   I s P N G                                                                 %
795 %                                                                             %
796 %                                                                             %
797 %                                                                             %
798 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
799 %
800 %  IsPNG() returns MagickTrue if the image format type, identified by the
801 %  magick string, is PNG.
802 %
803 %  The format of the IsPNG method is:
804 %
805 %      MagickBooleanType IsPNG(const unsigned char *magick,const size_t length)
806 %
807 %  A description of each parameter follows:
808 %
809 %    o magick: compare image format pattern against these bytes.
810 %
811 %    o length: Specifies the length of the magick string.
812 %
813 */
814 static MagickBooleanType IsPNG(const unsigned char *magick,const size_t length)
815 {
816   if (length < 8)
817     return(MagickFalse);
818
819   if (memcmp(magick,"\211PNG\r\n\032\n",8) == 0)
820     return(MagickTrue);
821
822   return(MagickFalse);
823 }
824 \f
825 #if defined(MAGICKCORE_PNG_DELEGATE)
826 #if defined(__cplusplus) || defined(c_plusplus)
827 extern "C" {
828 #endif
829
830 #if (PNG_LIBPNG_VER > 10011)
831 static size_t WriteBlobMSBULong(Image *image,const size_t value)
832 {
833   unsigned char
834     buffer[4];
835
836   assert(image != (Image *) NULL);
837   assert(image->signature == MagickSignature);
838   buffer[0]=(unsigned char) (value >> 24);
839   buffer[1]=(unsigned char) (value >> 16);
840   buffer[2]=(unsigned char) (value >> 8);
841   buffer[3]=(unsigned char) value;
842   return((size_t) WriteBlob(image,4,buffer));
843 }
844
845 static void PNGLong(png_bytep p,png_uint_32 value)
846 {
847   *p++=(png_byte) ((value >> 24) & 0xff);
848   *p++=(png_byte) ((value >> 16) & 0xff);
849   *p++=(png_byte) ((value >> 8) & 0xff);
850   *p++=(png_byte) (value & 0xff);
851 }
852
853 #if defined(JNG_SUPPORTED)
854 static void PNGsLong(png_bytep p,png_int_32 value)
855 {
856   *p++=(png_byte) ((value >> 24) & 0xff);
857   *p++=(png_byte) ((value >> 16) & 0xff);
858   *p++=(png_byte) ((value >> 8) & 0xff);
859   *p++=(png_byte) (value & 0xff);
860 }
861 #endif
862
863 static void PNGShort(png_bytep p,png_uint_16 value)
864 {
865   *p++=(png_byte) ((value >> 8) & 0xff);
866   *p++=(png_byte) (value & 0xff);
867 }
868
869 static void PNGType(png_bytep p,png_bytep type)
870 {
871   (void) CopyMagickMemory(p,type,4*sizeof(png_byte));
872 }
873
874 static void LogPNGChunk(MagickBooleanType logging, png_bytep type,
875    size_t length)
876 {
877   if (logging != MagickFalse)
878     (void) LogMagickEvent(CoderEvent,GetMagickModule(),
879       "  Writing %c%c%c%c chunk, length: %.20g",
880       type[0],type[1],type[2],type[3],(double) length);
881 }
882 #endif /* PNG_LIBPNG_VER > 10011 */
883
884 #if defined(__cplusplus) || defined(c_plusplus)
885 }
886 #endif
887
888 #if PNG_LIBPNG_VER > 10011
889 /*
890 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
891 %                                                                             %
892 %                                                                             %
893 %                                                                             %
894 %   R e a d P N G I m a g e                                                   %
895 %                                                                             %
896 %                                                                             %
897 %                                                                             %
898 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
899 %
900 %  ReadPNGImage() reads a Portable Network Graphics (PNG) or
901 %  Multiple-image Network Graphics (MNG) image file and returns it.  It
902 %  allocates the memory necessary for the new Image structure and returns a
903 %  pointer to the new image or set of images.
904 %
905 %  MNG support written by Glenn Randers-Pehrson, glennrp@image...
906 %
907 %  The format of the ReadPNGImage method is:
908 %
909 %      Image *ReadPNGImage(const ImageInfo *image_info,ExceptionInfo *exception)
910 %
911 %  A description of each parameter follows:
912 %
913 %    o image_info: the image info.
914 %
915 %    o exception: return any errors or warnings in this structure.
916 %
917 %  To do, more or less in chronological order (as of version 5.5.2,
918 %   November 26, 2002 -- glennrp -- see also "To do" under WriteMNGImage):
919 %
920 %    Get 16-bit cheap transparency working.
921 %
922 %    (At this point, PNG decoding is supposed to be in full MNG-LC compliance)
923 %
924 %    Preserve all unknown and not-yet-handled known chunks found in input
925 %    PNG file and copy them into output PNG files according to the PNG
926 %    copying rules.
927 %
928 %    (At this point, PNG encoding should be in full MNG compliance)
929 %
930 %    Provide options for choice of background to use when the MNG BACK
931 %    chunk is not present or is not mandatory (i.e., leave transparent,
932 %    user specified, MNG BACK, PNG bKGD)
933 %
934 %    Implement LOOP/ENDL [done, but could do discretionary loops more
935 %    efficiently by linking in the duplicate frames.].
936 %
937 %    Decode and act on the MHDR simplicity profile (offer option to reject
938 %    files or attempt to process them anyway when the profile isn't LC or VLC).
939 %
940 %    Upgrade to full MNG without Delta-PNG.
941 %
942 %        o  BACK [done a while ago except for background image ID]
943 %        o  MOVE [done 15 May 1999]
944 %        o  CLIP [done 15 May 1999]
945 %        o  DISC [done 19 May 1999]
946 %        o  SAVE [partially done 19 May 1999 (marks objects frozen)]
947 %        o  SEEK [partially done 19 May 1999 (discard function only)]
948 %        o  SHOW
949 %        o  PAST
950 %        o  BASI
951 %        o  MNG-level tEXt/iTXt/zTXt
952 %        o  pHYg
953 %        o  pHYs
954 %        o  sBIT
955 %        o  bKGD
956 %        o  iTXt (wait for libpng implementation).
957 %
958 %    Use the scene signature to discover when an identical scene is
959 %    being reused, and just point to the original image->exception instead
960 %    of storing another set of pixels.  This not specific to MNG
961 %    but could be applied generally.
962 %
963 %    Upgrade to full MNG with Delta-PNG.
964 %
965 %    JNG tEXt/iTXt/zTXt
966 %
967 %    We will not attempt to read files containing the CgBI chunk.
968 %    They are really Xcode files meant for display on the iPhone.
969 %    These are not valid PNG files and it is impossible to recover
970 %    the orginal PNG from files that have been converted to Xcode-PNG,
971 %    since irretrievable loss of color data has occurred due to the
972 %    use of premultiplied alpha.
973 */
974
975 #if defined(__cplusplus) || defined(c_plusplus)
976 extern "C" {
977 #endif
978
979 /*
980   This the function that does the actual reading of data.  It is
981   the same as the one supplied in libpng, except that it receives the
982   datastream from the ReadBlob() function instead of standard input.
983 */
984 static void png_get_data(png_structp png_ptr,png_bytep data,png_size_t length)
985 {
986   Image
987     *image;
988
989   image=(Image *) png_get_io_ptr(png_ptr);
990   if (length)
991     {
992       png_size_t
993         check;
994
995       check=(png_size_t) ReadBlob(image,(size_t) length,data);
996       if (check != length)
997         {
998           char
999             msg[MaxTextExtent];
1000
1001           (void) FormatMagickString(msg,MaxTextExtent,
1002             "Expected %.20g bytes; found %.20g bytes",(double) length,
1003             (double) check);
1004           png_warning(png_ptr,msg);
1005           png_error(png_ptr,"Read Exception");
1006         }
1007     }
1008 }
1009
1010 #if !defined(PNG_READ_EMPTY_PLTE_SUPPORTED) && \
1011     !defined(PNG_MNG_FEATURES_SUPPORTED)
1012 /* We use mng_get_data() instead of png_get_data() if we have a libpng
1013  * older than libpng-1.0.3a, which was the first to allow the empty
1014  * PLTE, or a newer libpng in which PNG_MNG_FEATURES_SUPPORTED was
1015  * ifdef'ed out.  Earlier versions would crash if the bKGD chunk was
1016  * encountered after an empty PLTE, so we have to look ahead for bKGD
1017  * chunks and remove them from the datastream that is passed to libpng,
1018  * and store their contents for later use.
1019  */
1020 static void mng_get_data(png_structp png_ptr,png_bytep data,png_size_t length)
1021 {
1022   MngInfo
1023     *mng_info;
1024
1025   Image
1026     *image;
1027
1028   png_size_t
1029     check;
1030
1031   register ssize_t
1032     i;
1033
1034   i=0;
1035   mng_info=(MngInfo *) png_get_io_ptr(png_ptr);
1036   image=(Image *) mng_info->image;
1037   while (mng_info->bytes_in_read_buffer && length)
1038   {
1039     data[i]=mng_info->read_buffer[i];
1040     mng_info->bytes_in_read_buffer--;
1041     length--;
1042     i++;
1043   }
1044   if (length)
1045     {
1046       check=(png_size_t) ReadBlob(image,(size_t) length,(char *) data);
1047
1048       if (check != length)
1049         png_error(png_ptr,"Read Exception");
1050
1051       if (length == 4)
1052         {
1053           if ((data[0] == 0) && (data[1] == 0) && (data[2] == 0) &&
1054               (data[3] == 0))
1055             {
1056               check=(png_size_t) ReadBlob(image,(size_t) length,
1057                 (char *) mng_info->read_buffer);
1058               mng_info->read_buffer[4]=0;
1059               mng_info->bytes_in_read_buffer=4;
1060               if (memcmp(mng_info->read_buffer,mng_PLTE,4) == 0)
1061                 mng_info->found_empty_plte=MagickTrue;
1062               if (memcmp(mng_info->read_buffer,mng_IEND,4) == 0)
1063                 {
1064                   mng_info->found_empty_plte=MagickFalse;
1065                   mng_info->have_saved_bkgd_index=MagickFalse;
1066                 }
1067             }
1068
1069           if ((data[0] == 0) && (data[1] == 0) && (data[2] == 0) &&
1070               (data[3] == 1))
1071             {
1072               check=(png_size_t) ReadBlob(image,(size_t) length,
1073                 (char *) mng_info->read_buffer);
1074               mng_info->read_buffer[4]=0;
1075               mng_info->bytes_in_read_buffer=4;
1076               if (memcmp(mng_info->read_buffer,mng_bKGD,4) == 0)
1077                 if (mng_info->found_empty_plte)
1078                   {
1079                     /*
1080                       Skip the bKGD data byte and CRC.
1081                     */
1082                     check=(png_size_t)
1083                       ReadBlob(image,5,(char *) mng_info->read_buffer);
1084                     check=(png_size_t) ReadBlob(image,(size_t) length,
1085                       (char *) mng_info->read_buffer);
1086                     mng_info->saved_bkgd_index=mng_info->read_buffer[0];
1087                     mng_info->have_saved_bkgd_index=MagickTrue;
1088                     mng_info->bytes_in_read_buffer=0;
1089                   }
1090             }
1091         }
1092     }
1093 }
1094 #endif
1095
1096 static void png_put_data(png_structp png_ptr,png_bytep data,png_size_t length)
1097 {
1098   Image
1099     *image;
1100
1101   image=(Image *) png_get_io_ptr(png_ptr);
1102   if (length)
1103     {
1104       png_size_t
1105         check;
1106
1107       check=(png_size_t) WriteBlob(image,(size_t) length,data);
1108
1109       if (check != length)
1110         png_error(png_ptr,"WriteBlob Failed");
1111     }
1112 }
1113
1114 static void png_flush_data(png_structp png_ptr)
1115 {
1116   (void) png_ptr;
1117 }
1118
1119 #ifdef PNG_WRITE_EMPTY_PLTE_SUPPORTED
1120 static int PalettesAreEqual(Image *a,Image *b)
1121 {
1122   ssize_t
1123     i;
1124
1125   if ((a == (Image *) NULL) || (b == (Image *) NULL))
1126     return((int) MagickFalse);
1127
1128   if (a->storage_class != PseudoClass || b->storage_class != PseudoClass)
1129     return((int) MagickFalse);
1130
1131   if (a->colors != b->colors)
1132     return((int) MagickFalse);
1133
1134   for (i=0; i < (ssize_t) a->colors; i++)
1135   {
1136     if ((a->colormap[i].red != b->colormap[i].red) ||
1137         (a->colormap[i].green != b->colormap[i].green) ||
1138         (a->colormap[i].blue != b->colormap[i].blue))
1139       return((int) MagickFalse);
1140   }
1141
1142   return((int) MagickTrue);
1143 }
1144 #endif
1145
1146 static void MngInfoDiscardObject(MngInfo *mng_info,int i)
1147 {
1148   if (i && (i < MNG_MAX_OBJECTS) && (mng_info != (MngInfo *) NULL) &&
1149       mng_info->exists[i] && !mng_info->frozen[i])
1150     {
1151 #ifdef MNG_OBJECT_BUFFERS
1152       if (mng_info->ob[i] != (MngBuffer *) NULL)
1153         {
1154           if (mng_info->ob[i]->reference_count > 0)
1155             mng_info->ob[i]->reference_count--;
1156
1157           if (mng_info->ob[i]->reference_count == 0)
1158             {
1159               if (mng_info->ob[i]->image != (Image *) NULL)
1160                 mng_info->ob[i]->image=DestroyImage(mng_info->ob[i]->image);
1161
1162               mng_info->ob[i]=DestroyString(mng_info->ob[i]);
1163             }
1164         }
1165       mng_info->ob[i]=(MngBuffer *) NULL;
1166 #endif
1167       mng_info->exists[i]=MagickFalse;
1168       mng_info->invisible[i]=MagickFalse;
1169       mng_info->viewable[i]=MagickFalse;
1170       mng_info->frozen[i]=MagickFalse;
1171       mng_info->x_off[i]=0;
1172       mng_info->y_off[i]=0;
1173       mng_info->object_clip[i].left=0;
1174       mng_info->object_clip[i].right=(ssize_t) PNG_UINT_31_MAX;
1175       mng_info->object_clip[i].top=0;
1176       mng_info->object_clip[i].bottom=(ssize_t) PNG_UINT_31_MAX;
1177     }
1178 }
1179
1180 static void MngInfoFreeStruct(MngInfo *mng_info,
1181     MagickBooleanType *have_mng_structure)
1182 {
1183   if (*have_mng_structure != MagickFalse && (mng_info != (MngInfo *) NULL))
1184     {
1185       register ssize_t
1186         i;
1187
1188       for (i=1; i < MNG_MAX_OBJECTS; i++)
1189         MngInfoDiscardObject(mng_info,i);
1190
1191       if (mng_info->global_plte != (png_colorp) NULL)
1192         mng_info->global_plte=(png_colorp)
1193           RelinquishMagickMemory(mng_info->global_plte);
1194
1195       mng_info=(MngInfo *) RelinquishMagickMemory(mng_info);
1196       *have_mng_structure=MagickFalse;
1197     }
1198 }
1199
1200 static MngBox mng_minimum_box(MngBox box1,MngBox box2)
1201 {
1202   MngBox
1203     box;
1204
1205   box=box1;
1206   if (box.left < box2.left)
1207     box.left=box2.left;
1208
1209   if (box.top < box2.top)
1210     box.top=box2.top;
1211
1212   if (box.right > box2.right)
1213     box.right=box2.right;
1214
1215   if (box.bottom > box2.bottom)
1216     box.bottom=box2.bottom;
1217
1218   return box;
1219 }
1220
1221 static MngBox mng_read_box(MngBox previous_box,char delta_type,unsigned char *p)
1222 {
1223    MngBox
1224       box;
1225
1226   /*
1227     Read clipping boundaries from DEFI, CLIP, FRAM, or PAST chunk.
1228   */
1229   box.left=(ssize_t) ((p[0]  << 24) | (p[1]  << 16) | (p[2]  << 8) | p[3]);
1230   box.right=(ssize_t) ((p[4]  << 24) | (p[5]  << 16) | (p[6]  << 8) | p[7]);
1231   box.top=(ssize_t) ((p[8]  << 24) | (p[9]  << 16) | (p[10] << 8) | p[11]);
1232   box.bottom=(ssize_t) ((p[12] << 24) | (p[13] << 16) | (p[14] << 8) | p[15]);
1233   if (delta_type != 0)
1234     {
1235       box.left+=previous_box.left;
1236       box.right+=previous_box.right;
1237       box.top+=previous_box.top;
1238       box.bottom+=previous_box.bottom;
1239     }
1240
1241   return(box);
1242 }
1243
1244 static MngPair mng_read_pair(MngPair previous_pair,int delta_type,
1245   unsigned char *p)
1246 {
1247   MngPair
1248     pair;
1249   /*
1250     Read two ssize_ts from CLON, MOVE or PAST chunk
1251   */
1252   pair.a=(long) ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
1253   pair.b=(long) ((p[4] << 24) | (p[5] << 16) | (p[6] << 8) | p[7]);
1254
1255   if (delta_type != 0)
1256     {
1257       pair.a+=previous_pair.a;
1258       pair.b+=previous_pair.b;
1259     }
1260
1261   return(pair);
1262 }
1263
1264 static long mng_get_long(unsigned char *p)
1265 {
1266   return((long) ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]));
1267 }
1268
1269 static void MagickPNGErrorHandler(png_struct *ping,png_const_charp message)
1270 {
1271   Image
1272     *image;
1273
1274   image=(Image *) png_get_error_ptr(ping);
1275
1276   if (image->debug != MagickFalse)
1277     (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1278       "  libpng-%s error: %s", PNG_LIBPNG_VER_STRING,message);
1279
1280   (void) ThrowMagickException(&image->exception,GetMagickModule(),CoderError,
1281     message,"`%s'",image->filename);
1282
1283 #if (PNG_LIBPNG_VER < 10500)
1284   /* A warning about deprecated use of jmpbuf here is unavoidable if you
1285    * are building with libpng-1.4.x and can be ignored.
1286    */
1287   longjmp(ping->jmpbuf,1);
1288 #else
1289   png_longjmp(ping,1);
1290 #endif
1291 }
1292
1293 static void MagickPNGWarningHandler(png_struct *ping,png_const_charp message)
1294 {
1295   Image
1296     *image;
1297
1298   if (LocaleCompare(message, "Missing PLTE before tRNS") == 0)
1299     png_error(ping, message);
1300
1301   image=(Image *) png_get_error_ptr(ping);
1302   if (image->debug != MagickFalse)
1303     (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1304       "  libpng-%s warning: %s", PNG_LIBPNG_VER_STRING,message);
1305
1306   (void) ThrowMagickException(&image->exception,GetMagickModule(),CoderWarning,
1307     message,"`%s'",image->filename);
1308 }
1309
1310 #ifdef PNG_USER_MEM_SUPPORTED
1311 static png_voidp Magick_png_malloc(png_structp png_ptr,png_uint_32 size)
1312 {
1313 #if (PNG_LIBPNG_VER < 10011)
1314   png_voidp
1315     ret;
1316
1317   png_ptr=png_ptr;
1318   ret=((png_voidp) AcquireMagickMemory((size_t) size));
1319
1320   if (ret == NULL)
1321     png_error("Insufficient memory.");
1322
1323   return(ret);
1324 #else
1325   png_ptr=png_ptr;
1326   return((png_voidp) AcquireMagickMemory((size_t) size));
1327 #endif
1328 }
1329
1330 /*
1331   Free a pointer.  It is removed from the list at the same time.
1332 */
1333 static png_free_ptr Magick_png_free(png_structp png_ptr,png_voidp ptr)
1334 {
1335   png_ptr=png_ptr;
1336   ptr=RelinquishMagickMemory(ptr);
1337   return((png_free_ptr) NULL);
1338 }
1339 #endif
1340
1341 #if defined(__cplusplus) || defined(c_plusplus)
1342 }
1343 #endif
1344
1345 static int
1346 Magick_png_read_raw_profile(Image *image, const ImageInfo *image_info,
1347    png_textp text,int ii)
1348 {
1349   register ssize_t
1350     i;
1351
1352   register unsigned char
1353     *dp;
1354
1355   register png_charp
1356     sp;
1357
1358   png_uint_32
1359     length,
1360     nibbles;
1361
1362   StringInfo
1363     *profile;
1364
1365   const unsigned char
1366     unhex[103]={0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,
1367                  0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,
1368                  0,0,0,0,0,0,0,0,0,1, 2,3,4,5,6,7,8,9,0,0,
1369                  0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,
1370                  0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,10,11,12,
1371                  13,14,15};
1372
1373   sp=text[ii].text+1;
1374   /* look for newline */
1375   while (*sp != '\n')
1376      sp++;
1377
1378   /* look for length */
1379   while (*sp == '\0' || *sp == ' ' || *sp == '\n')
1380      sp++;
1381
1382   length=(png_uint_32) StringToLong(sp);
1383
1384   (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1385        "      length: %lu",(unsigned long) length);
1386
1387   while (*sp != ' ' && *sp != '\n')
1388      sp++;
1389
1390   /* allocate space */
1391   if (length == 0)
1392   {
1393     (void) ThrowMagickException(&image->exception,GetMagickModule(),
1394       CoderWarning,"UnableToCopyProfile","`%s'","invalid profile length");
1395     return(MagickFalse);
1396   }
1397
1398   profile=AcquireStringInfo(length);
1399
1400   if (profile == (StringInfo *) NULL)
1401   {
1402     (void) ThrowMagickException(&image->exception,GetMagickModule(),
1403       ResourceLimitError,"MemoryAllocationFailed","`%s'",
1404       "unable to copy profile");
1405     return(MagickFalse);
1406   }
1407
1408   /* copy profile, skipping white space and column 1 "=" signs */
1409   dp=GetStringInfoDatum(profile);
1410   nibbles=length*2;
1411
1412   for (i=0; i < (ssize_t) nibbles; i++)
1413   {
1414     while (*sp < '0' || (*sp > '9' && *sp < 'a') || *sp > 'f')
1415     {
1416       if (*sp == '\0')
1417         {
1418           (void) ThrowMagickException(&image->exception,GetMagickModule(),
1419             CoderWarning,"UnableToCopyProfile","`%s'","ran out of data");
1420           profile=DestroyStringInfo(profile);
1421           return(MagickFalse);
1422         }
1423       sp++;
1424     }
1425
1426     if (i%2 == 0)
1427       *dp=(unsigned char) (16*unhex[(int) *sp++]);
1428
1429     else
1430       (*dp++)+=unhex[(int) *sp++];
1431   }
1432   /*
1433     We have already read "Raw profile type.
1434   */
1435   (void) SetImageProfile(image,&text[ii].key[17],profile);
1436   profile=DestroyStringInfo(profile);
1437
1438   if (image_info->verbose)
1439     (void) printf(" Found a generic profile, type %s\n",&text[ii].key[17]);
1440
1441   return MagickTrue;
1442 }
1443
1444 #if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
1445 static int read_vpag_chunk_callback(png_struct *ping, png_unknown_chunkp chunk)
1446 {
1447   Image
1448     *image;
1449
1450
1451   /* The unknown chunk structure contains the chunk data:
1452      png_byte name[5];
1453      png_byte *data;
1454      png_size_t size;
1455
1456      Note that libpng has already taken care of the CRC handling.
1457   */
1458
1459
1460   if (chunk->name[0] != 118 || chunk->name[1] != 112 ||
1461       chunk->name[2] != 65 ||chunk-> name[3] != 103)
1462     return(0); /* Did not recognize */
1463
1464   /* recognized vpAg */
1465
1466   if (chunk->size != 9)
1467     return(-1); /* Error return */
1468
1469   if (chunk->data[8] != 0)
1470     return(0);  /* ImageMagick requires pixel units */
1471
1472   image=(Image *) png_get_user_chunk_ptr(ping);
1473
1474   image->page.width=(size_t) ((chunk->data[0] << 24) |
1475      (chunk->data[1] << 16) | (chunk->data[2] << 8) | chunk->data[3]);
1476
1477   image->page.height=(size_t) ((chunk->data[4] << 24) |
1478      (chunk->data[5] << 16) | (chunk->data[6] << 8) | chunk->data[7]);
1479
1480   /* Return one of the following: */
1481      /* return(-n);  chunk had an error */
1482      /* return(0);  did not recognize */
1483      /* return(n);  success */
1484
1485   return(1);
1486
1487 }
1488 #endif
1489
1490 /*
1491 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1492 %                                                                             %
1493 %                                                                             %
1494 %                                                                             %
1495 %   R e a d O n e P N G I m a g e                                             %
1496 %                                                                             %
1497 %                                                                             %
1498 %                                                                             %
1499 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1500 %
1501 %  ReadOnePNGImage() reads a Portable Network Graphics (PNG) image file
1502 %  (minus the 8-byte signature)  and returns it.  It allocates the memory
1503 %  necessary for the new Image structure and returns a pointer to the new
1504 %  image.
1505 %
1506 %  The format of the ReadOnePNGImage method is:
1507 %
1508 %      Image *ReadOnePNGImage(MngInfo *mng_info, const ImageInfo *image_info,
1509 %         ExceptionInfo *exception)
1510 %
1511 %  A description of each parameter follows:
1512 %
1513 %    o mng_info: Specifies a pointer to a MngInfo structure.
1514 %
1515 %    o image_info: the image info.
1516 %
1517 %    o exception: return any errors or warnings in this structure.
1518 %
1519 */
1520 static Image *ReadOnePNGImage(MngInfo *mng_info,
1521     const ImageInfo *image_info, ExceptionInfo *exception)
1522 {
1523   /* Read one PNG image */
1524
1525   Image
1526     *image;
1527
1528   int
1529     num_text,
1530     num_passes,
1531     pass,
1532     ping_bit_depth,
1533     ping_color_type,
1534     ping_interlace_method,
1535     ping_compression_method,
1536     ping_filter_method,
1537     ping_num_trans;
1538
1539   LongPixelPacket
1540     transparent_color;
1541
1542   MagickBooleanType
1543     logging,
1544     status;
1545
1546   png_bytep
1547      ping_trans_alpha;
1548
1549   png_color_16p
1550      ping_background,
1551      ping_trans_color;
1552
1553   png_info
1554     *end_info,
1555     *ping_info;
1556
1557   png_struct
1558     *ping;
1559
1560   png_textp
1561     text;
1562
1563   png_uint_32
1564     ping_height,
1565     ping_width,
1566     ping_rowbytes;
1567
1568   QuantumInfo
1569     *quantum_info;
1570
1571   unsigned char
1572     *ping_pixels;
1573
1574   ssize_t
1575     y;
1576
1577   register unsigned char
1578     *p;
1579
1580   register IndexPacket
1581     *indexes;
1582
1583   register ssize_t
1584     i,
1585     x;
1586
1587   register PixelPacket
1588     *q;
1589
1590   size_t
1591     length,
1592     row_offset;
1593
1594 #if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
1595   png_byte unused_chunks[]=
1596   {
1597     104,  73,  83,  84, (png_byte) '\0',   /* hIST */
1598     105,  84,  88, 116, (png_byte) '\0',   /* iTXt */
1599     112,  67,  65,  76, (png_byte) '\0',   /* pCAL */
1600     115,  67,  65,  76, (png_byte) '\0',   /* sCAL */
1601     115,  80,  76,  84, (png_byte) '\0',   /* sPLT */
1602     116,  73,  77,  69, (png_byte) '\0',   /* tIME */
1603   };
1604 #endif
1605
1606   logging=LogMagickEvent(CoderEvent,GetMagickModule(),
1607     "  Enter ReadOnePNGImage()");
1608
1609 #if defined(PNG_SETJMP_NOT_THREAD_SAFE)
1610   LockSemaphoreInfo(ping_semaphore);
1611 #endif
1612
1613 #if (PNG_LIBPNG_VER < 10200)
1614   if (image_info->verbose)
1615     printf("Your PNG library (libpng-%s) is rather old.\n",
1616        PNG_LIBPNG_VER_STRING);
1617 #endif
1618
1619 #if (PNG_LIBPNG_VER >= 10400)
1620 #  ifndef  PNG_TRANSFORM_GRAY_TO_RGB    /* Added at libpng-1.4.0beta67 */
1621   if (image_info->verbose)
1622     {
1623       printf("Your PNG library (libpng-%s) is an old beta version.\n",
1624            PNG_LIBPNG_VER_STRING);
1625       printf("Please update it.\n");
1626     }
1627 #  endif
1628 #endif
1629
1630
1631   quantum_info = (QuantumInfo *) NULL;
1632   image=mng_info->image;
1633
1634   if (logging != MagickFalse)
1635     (void)LogMagickEvent(CoderEvent,GetMagickModule(),
1636       "  image->matte=%d",(int) image->matte);
1637
1638   /* Set to an out-of-range color unless tRNS chunk is present */
1639   transparent_color.red=65537;
1640   transparent_color.green=65537;
1641   transparent_color.blue=65537;
1642   transparent_color.opacity=65537;
1643
1644   /*
1645     Allocate the PNG structures
1646   */
1647 #ifdef PNG_USER_MEM_SUPPORTED
1648  ping=png_create_read_struct_2(PNG_LIBPNG_VER_STRING, image,
1649    MagickPNGErrorHandler,MagickPNGWarningHandler, NULL,
1650    (png_malloc_ptr) Magick_png_malloc,(png_free_ptr) Magick_png_free);
1651 #else
1652   ping=png_create_read_struct(PNG_LIBPNG_VER_STRING,image,
1653     MagickPNGErrorHandler,MagickPNGWarningHandler);
1654 #endif
1655   if (ping == (png_struct *) NULL)
1656     ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1657
1658   ping_info=png_create_info_struct(ping);
1659
1660   if (ping_info == (png_info *) NULL)
1661     {
1662       png_destroy_read_struct(&ping,(png_info **) NULL,(png_info **) NULL);
1663       ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1664     }
1665
1666   end_info=png_create_info_struct(ping);
1667
1668   if (end_info == (png_info *) NULL)
1669     {
1670       png_destroy_read_struct(&ping,&ping_info,(png_info **) NULL);
1671       ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1672     }
1673
1674   ping_pixels=(unsigned char *) NULL;
1675
1676   if (setjmp(png_jmpbuf(ping)))
1677     {
1678       /*
1679         PNG image is corrupt.
1680       */
1681       png_destroy_read_struct(&ping,&ping_info,&end_info);
1682 #if defined(PNG_SETJMP_NOT_THREAD_SAFE)
1683       UnlockSemaphoreInfo(ping_semaphore);
1684 #endif
1685       if (logging != MagickFalse)
1686         (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1687           "  exit ReadOnePNGImage() with error.");
1688
1689       if (image != (Image *) NULL)
1690         {
1691           InheritException(exception,&image->exception);
1692           image->columns=0;
1693         }
1694
1695       return(GetFirstImageInList(image));
1696     }
1697   /*
1698     Prepare PNG for reading.
1699   */
1700
1701   mng_info->image_found++;
1702   png_set_sig_bytes(ping,8);
1703
1704   if (LocaleCompare(image_info->magick,"MNG") == 0)
1705     {
1706 #if defined(PNG_MNG_FEATURES_SUPPORTED)
1707       (void) png_permit_mng_features(ping,PNG_ALL_MNG_FEATURES);
1708       png_set_read_fn(ping,image,png_get_data);
1709 #else
1710 #if defined(PNG_READ_EMPTY_PLTE_SUPPORTED)
1711       png_permit_empty_plte(ping,MagickTrue);
1712       png_set_read_fn(ping,image,png_get_data);
1713 #else
1714       mng_info->image=image;
1715       mng_info->bytes_in_read_buffer=0;
1716       mng_info->found_empty_plte=MagickFalse;
1717       mng_info->have_saved_bkgd_index=MagickFalse;
1718       png_set_read_fn(ping,mng_info,mng_get_data);
1719 #endif
1720 #endif
1721     }
1722
1723   else
1724     png_set_read_fn(ping,image,png_get_data);
1725
1726 #if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
1727   /* Ignore unused chunks and all unknown chunks except for vpAg */
1728   png_set_keep_unknown_chunks(ping, 1, NULL, 0);
1729   png_set_keep_unknown_chunks(ping, 2, mng_vpAg, 1);
1730   png_set_keep_unknown_chunks(ping, 1, unused_chunks,
1731      (int)sizeof(unused_chunks)/5);
1732   /* Callback for other unknown chunks */
1733   png_set_read_user_chunk_fn(ping, image, read_vpag_chunk_callback);
1734 #endif
1735
1736 #if (PNG_LIBPNG_VER < 10400)
1737 #  if defined(PNG_USE_PNGGCCRD) && defined(PNG_ASSEMBLER_CODE_SUPPORTED) && \
1738    (PNG_LIBPNG_VER >= 10200) && (PNG_LIBPNG_VER < 10220) && defined(__i386__)
1739   /* Disable thread-unsafe features of pnggccrd */
1740   if (png_access_version_number() >= 10200)
1741   {
1742     png_uint_32 mmx_disable_mask=0;
1743     png_uint_32 asm_flags;
1744
1745     mmx_disable_mask |= ( PNG_ASM_FLAG_MMX_READ_COMBINE_ROW  \
1746                         | PNG_ASM_FLAG_MMX_READ_FILTER_SUB   \
1747                         | PNG_ASM_FLAG_MMX_READ_FILTER_AVG   \
1748                         | PNG_ASM_FLAG_MMX_READ_FILTER_PAETH );
1749     asm_flags=png_get_asm_flags(ping);
1750     png_set_asm_flags(ping, asm_flags & ~mmx_disable_mask);
1751   }
1752 #  endif
1753 #endif
1754
1755   png_read_info(ping,ping_info);
1756
1757   png_get_IHDR(ping,ping_info,&ping_width,&ping_height,
1758                &ping_bit_depth,&ping_color_type,
1759                &ping_interlace_method,&ping_compression_method,
1760                &ping_filter_method);
1761
1762   (void) png_get_tRNS(ping, ping_info, &ping_trans_alpha, &ping_num_trans,
1763                       &ping_trans_color);
1764
1765   (void) png_get_bKGD(ping, ping_info, &ping_background);
1766
1767   if (ping_bit_depth < 8)
1768     {
1769       if (((int) ping_color_type == PNG_COLOR_TYPE_PALETTE))
1770         {
1771           png_set_packing(ping);
1772           ping_bit_depth = 8;
1773         }
1774     }
1775
1776   image->depth=ping_bit_depth;
1777   image->depth=GetImageQuantumDepth(image,MagickFalse);
1778   image->interlace=ping_interlace_method != 0 ? PNGInterlace : NoInterlace;
1779   if (logging != MagickFalse)
1780     {
1781       (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1782         "    PNG width: %.20g, height: %.20g",
1783         (double) ping_width, (double) ping_height);
1784
1785       (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1786         "    PNG color_type: %d, bit_depth: %d",
1787         ping_color_type, ping_bit_depth);
1788
1789       (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1790         "    PNG compression_method: %d",
1791         ping_compression_method);
1792
1793       (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1794         "    PNG interlace_method: %d, filter_method: %d",
1795         ping_interlace_method,ping_filter_method);
1796     }
1797
1798 #ifdef PNG_READ_iCCP_SUPPORTED
1799   if (png_get_valid(ping,ping_info,PNG_INFO_iCCP))
1800     {
1801       int
1802         compression;
1803
1804 #if (PNG_LIBPNG_VER < 10500)
1805       png_charp
1806         info;
1807 #else
1808       png_bytep
1809         info;
1810 #endif
1811
1812       png_charp
1813         name;
1814
1815       png_uint_32
1816         profile_length;
1817
1818       (void) png_get_iCCP(ping,ping_info,&name,(int *) &compression,&info,
1819         &profile_length);
1820
1821       if (profile_length != 0)
1822         {
1823           StringInfo
1824             *profile;
1825
1826           if (logging != MagickFalse)
1827             (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1828               "    Reading PNG iCCP chunk.");
1829           profile=AcquireStringInfo(profile_length);
1830           SetStringInfoDatum(profile,(const unsigned char *) info);
1831           (void) SetImageProfile(image,"icc",profile);
1832           profile=DestroyStringInfo(profile);
1833       }
1834     }
1835 #endif
1836 #if defined(PNG_READ_sRGB_SUPPORTED)
1837   {
1838     int
1839       intent;
1840
1841     if (mng_info->have_global_srgb)
1842       image->rendering_intent=Magick_RenderingIntent_from_PNG_RenderingIntent
1843         (mng_info->global_srgb_intent);
1844
1845     if (png_get_sRGB(ping,ping_info,&intent))
1846       {
1847         image->rendering_intent=Magick_RenderingIntent_from_PNG_RenderingIntent
1848           (intent);
1849
1850         if (logging != MagickFalse)
1851           (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1852             "    Reading PNG sRGB chunk: rendering_intent: %d",intent);
1853       }
1854   }
1855 #endif
1856   {
1857      double
1858         file_gamma;
1859
1860      if (!png_get_gAMA(ping,ping_info,&file_gamma))
1861        if (mng_info->have_global_gama)
1862          png_set_gAMA(ping,ping_info,mng_info->global_gamma);
1863
1864      if (png_get_gAMA(ping,ping_info,&file_gamma))
1865        {
1866          image->gamma=(float) file_gamma;
1867          if (logging != MagickFalse)
1868            (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1869              "    Reading PNG gAMA chunk: gamma: %f",file_gamma);
1870        }
1871   }
1872   if (!png_get_valid(ping,ping_info,PNG_INFO_cHRM))
1873     {
1874       if (mng_info->have_global_chrm != MagickFalse)
1875         {
1876           (void) png_set_cHRM(ping,ping_info,
1877             mng_info->global_chrm.white_point.x,
1878             mng_info->global_chrm.white_point.y,
1879             mng_info->global_chrm.red_primary.x,
1880             mng_info->global_chrm.red_primary.y,
1881             mng_info->global_chrm.green_primary.x,
1882             mng_info->global_chrm.green_primary.y,
1883             mng_info->global_chrm.blue_primary.x,
1884             mng_info->global_chrm.blue_primary.y);
1885         }
1886     }
1887
1888   if (png_get_valid(ping,ping_info,PNG_INFO_cHRM))
1889     {
1890       (void) png_get_cHRM(ping,ping_info,
1891         &image->chromaticity.white_point.x,
1892         &image->chromaticity.white_point.y,
1893         &image->chromaticity.red_primary.x,
1894         &image->chromaticity.red_primary.y,
1895         &image->chromaticity.green_primary.x,
1896         &image->chromaticity.green_primary.y,
1897         &image->chromaticity.blue_primary.x,
1898         &image->chromaticity.blue_primary.y);
1899
1900       if (logging != MagickFalse)
1901         (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1902           "    Reading PNG cHRM chunk.");
1903     }
1904
1905   if (image->rendering_intent != UndefinedIntent)
1906     {
1907       png_set_sRGB(ping,ping_info,
1908          Magick_RenderingIntent_to_PNG_RenderingIntent
1909          (image->rendering_intent));
1910       png_set_gAMA(ping,ping_info,0.45455f);
1911       png_set_cHRM(ping,ping_info,
1912                   0.6400f, 0.3300f, 0.3000f, 0.6000f,
1913                   0.1500f, 0.0600f, 0.3127f, 0.3290f);
1914     }
1915 #if defined(PNG_oFFs_SUPPORTED)
1916   if (png_get_valid(ping,ping_info,PNG_INFO_oFFs))
1917     {
1918       image->page.x=(ssize_t) png_get_x_offset_pixels(ping, ping_info);
1919       image->page.y=(ssize_t) png_get_y_offset_pixels(ping, ping_info);
1920
1921       if (logging != MagickFalse)
1922         if (image->page.x || image->page.y)
1923           (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1924             "    Reading PNG oFFs chunk: x: %.20g, y: %.20g.",(double)
1925             image->page.x,(double) image->page.y);
1926     }
1927 #endif
1928 #if defined(PNG_pHYs_SUPPORTED)
1929   if (!png_get_valid(ping,ping_info,PNG_INFO_pHYs))
1930     {
1931       if (mng_info->have_global_phys)
1932         {
1933           png_set_pHYs(ping,ping_info,
1934                        mng_info->global_x_pixels_per_unit,
1935                        mng_info->global_y_pixels_per_unit,
1936                        mng_info->global_phys_unit_type);
1937         }
1938     }
1939
1940   if (png_get_valid(ping,ping_info,PNG_INFO_pHYs))
1941     {
1942       int
1943         unit_type;
1944
1945       png_uint_32
1946         x_resolution,
1947         y_resolution;
1948
1949       /*
1950         Set image resolution.
1951       */
1952       (void) png_get_pHYs(ping,ping_info,&x_resolution,&y_resolution,
1953         &unit_type);
1954       image->x_resolution=(double) x_resolution;
1955       image->y_resolution=(double) y_resolution;
1956
1957       if (unit_type == PNG_RESOLUTION_METER)
1958         {
1959           image->units=PixelsPerCentimeterResolution;
1960           image->x_resolution=(double) x_resolution/100.0;
1961           image->y_resolution=(double) y_resolution/100.0;
1962         }
1963
1964       if (logging != MagickFalse)
1965         (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1966           "    Reading PNG pHYs chunk: xres: %.20g, yres: %.20g, units: %d.",
1967           (double) x_resolution,(double) y_resolution,unit_type);
1968     }
1969 #endif
1970   if (png_get_valid(ping,ping_info,PNG_INFO_PLTE))
1971     {
1972       int
1973         number_colors;
1974
1975       png_colorp
1976         palette;
1977
1978       (void) png_get_PLTE(ping,ping_info,&palette,&number_colors);
1979
1980       if ((number_colors == 0) &&
1981           ((int) ping_color_type == PNG_COLOR_TYPE_PALETTE))
1982         {
1983           if (mng_info->global_plte_length)
1984             {
1985               png_set_PLTE(ping,ping_info,mng_info->global_plte,
1986                 (int) mng_info->global_plte_length);
1987
1988               if (!png_get_valid(ping,ping_info,PNG_INFO_tRNS))
1989                 if (mng_info->global_trns_length)
1990                   {
1991                     if (mng_info->global_trns_length >
1992                         mng_info->global_plte_length)
1993                       (void) ThrowMagickException(&image->exception,
1994                         GetMagickModule(),CoderError,
1995                         "global tRNS has more entries than global PLTE",
1996                         "`%s'",image_info->filename);
1997                     png_set_tRNS(ping,ping_info,mng_info->global_trns,
1998                       (int) mng_info->global_trns_length,NULL);
1999                   }
2000 #if defined(PNG_READ_bKGD_SUPPORTED)
2001               if (
2002 #ifndef PNG_READ_EMPTY_PLTE_SUPPORTED
2003                    mng_info->have_saved_bkgd_index ||
2004 #endif
2005                    png_get_valid(ping,ping_info,PNG_INFO_bKGD))
2006                     {
2007                       png_color_16
2008                          background;
2009
2010 #ifndef PNG_READ_EMPTY_PLTE_SUPPORTED
2011                       if (mng_info->have_saved_bkgd_index)
2012                         background.index=mng_info->saved_bkgd_index;
2013 #endif
2014                       if (png_get_valid(ping, ping_info, PNG_INFO_bKGD))
2015                         background.index=ping_background->index;
2016
2017                       background.red=(png_uint_16)
2018                         mng_info->global_plte[background.index].red;
2019
2020                       background.green=(png_uint_16)
2021                         mng_info->global_plte[background.index].green;
2022
2023                       background.blue=(png_uint_16)
2024                         mng_info->global_plte[background.index].blue;
2025
2026                       png_set_bKGD(ping,ping_info,&background);
2027                     }
2028 #endif
2029                 }
2030               else
2031                 (void) ThrowMagickException(&image->exception,GetMagickModule(),
2032                   CoderError,"No global PLTE in file","`%s'",
2033                   image_info->filename);
2034             }
2035         }
2036
2037 #if defined(PNG_READ_bKGD_SUPPORTED)
2038   if (mng_info->have_global_bkgd &&
2039           (!png_get_valid(ping,ping_info,PNG_INFO_bKGD)))
2040       image->background_color=mng_info->mng_global_bkgd;
2041
2042   if (png_get_valid(ping,ping_info,PNG_INFO_bKGD))
2043     {
2044       /*
2045         Set image background color.
2046       */
2047       if (logging != MagickFalse)
2048         (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2049           "    Reading PNG bKGD chunk.");
2050
2051       if (ping_bit_depth == MAGICKCORE_QUANTUM_DEPTH)
2052         {
2053           image->background_color.red=ping_background->red;
2054           image->background_color.green=ping_background->green;
2055           image->background_color.blue=ping_background->blue;
2056         }
2057
2058       else /* Scale background components to 16-bit */
2059         {
2060           unsigned int
2061             bkgd_scale;
2062
2063           if (logging != MagickFalse)
2064             (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2065               "    raw ping_background=(%d,%d,%d).",ping_background->red,
2066               ping_background->green,ping_background->blue);
2067
2068           bkgd_scale = 1;
2069
2070           if (ping_bit_depth == 1)
2071              bkgd_scale = 255;
2072
2073           else if (ping_bit_depth == 2)
2074              bkgd_scale = 85;
2075
2076           else if (ping_bit_depth == 4)
2077              bkgd_scale = 17;
2078
2079           if (ping_bit_depth <= 8)
2080              bkgd_scale *= 257;
2081
2082           ping_background->red *= bkgd_scale;
2083           ping_background->green *= bkgd_scale;
2084           ping_background->blue *= bkgd_scale;
2085
2086           if (logging != MagickFalse)
2087             {
2088             (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2089               "    bkgd_scale=%d.",bkgd_scale);
2090
2091             (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2092               "    ping_background=(%d,%d,%d).",ping_background->red,
2093               ping_background->green,ping_background->blue);
2094             }
2095
2096           image->background_color.red=
2097             ScaleShortToQuantum(ping_background->red);
2098
2099           image->background_color.green=
2100             ScaleShortToQuantum(ping_background->green);
2101
2102           image->background_color.blue=
2103             ScaleShortToQuantum(ping_background->blue);
2104
2105           image->background_color.opacity=OpaqueOpacity;
2106
2107           if (logging != MagickFalse)
2108             (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2109               "    image->background_color=(%.20g,%.20g,%.20g).",
2110               (double) image->background_color.red,
2111               (double) image->background_color.green,
2112               (double) image->background_color.blue);
2113         }
2114     }
2115 #endif
2116
2117   if (png_get_valid(ping,ping_info,PNG_INFO_tRNS))
2118     {
2119       /*
2120         Image has a tRNS chunk.
2121       */
2122       int
2123         max_sample;
2124
2125       size_t
2126         one=1;
2127
2128       if (logging != MagickFalse)
2129         (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2130           "    Reading PNG tRNS chunk.");
2131
2132       max_sample = (int) ((one << ping_bit_depth) - 1);
2133
2134       if ((ping_color_type == PNG_COLOR_TYPE_GRAY &&
2135           (int)ping_trans_color->gray > max_sample) ||
2136           (ping_color_type == PNG_COLOR_TYPE_RGB &&
2137           ((int)ping_trans_color->red > max_sample ||
2138           (int)ping_trans_color->green > max_sample ||
2139           (int)ping_trans_color->blue > max_sample)))
2140         {
2141           if (logging != MagickFalse)
2142             (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2143               "    Ignoring PNG tRNS chunk with out-of-range sample.");
2144           png_free_data(ping, ping_info, PNG_FREE_TRNS, 0);
2145           png_set_invalid(ping,ping_info,PNG_INFO_tRNS);
2146           image->matte=MagickFalse;
2147         }
2148       else
2149         {
2150           int
2151              scale_to_short;
2152
2153           scale_to_short = 65535L/((1UL << ping_bit_depth)-1);
2154
2155           /* Scale transparent_color to short */
2156           transparent_color.red= scale_to_short*ping_trans_color->red;
2157           transparent_color.green= scale_to_short*ping_trans_color->green;
2158           transparent_color.blue= scale_to_short*ping_trans_color->blue;
2159           transparent_color.opacity= scale_to_short*ping_trans_color->gray;
2160
2161           if (ping_color_type == PNG_COLOR_TYPE_GRAY)
2162             {
2163               if (logging != MagickFalse)
2164               {
2165                 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2166                   "    Raw tRNS graylevel is %d.",ping_trans_color->gray);
2167
2168                 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2169                   "    scaled graylevel is %d.",transparent_color.opacity);
2170               }
2171               transparent_color.red=transparent_color.opacity;
2172               transparent_color.green=transparent_color.opacity;
2173               transparent_color.blue=transparent_color.opacity;
2174             }
2175         }
2176     }
2177 #if defined(PNG_READ_sBIT_SUPPORTED)
2178   if (mng_info->have_global_sbit)
2179     {
2180       if (!png_get_valid(ping,ping_info,PNG_INFO_sBIT))
2181         png_set_sBIT(ping,ping_info,&mng_info->global_sbit);
2182     }
2183 #endif
2184   num_passes=png_set_interlace_handling(ping);
2185
2186   png_read_update_info(ping,ping_info);
2187
2188   ping_rowbytes=png_get_rowbytes(ping,ping_info);
2189
2190   /*
2191     Initialize image structure.
2192   */
2193   mng_info->image_box.left=0;
2194   mng_info->image_box.right=(ssize_t) ping_width;
2195   mng_info->image_box.top=0;
2196   mng_info->image_box.bottom=(ssize_t) ping_height;
2197   if (mng_info->mng_type == 0)
2198     {
2199       mng_info->mng_width=ping_width;
2200       mng_info->mng_height=ping_height;
2201       mng_info->frame=mng_info->image_box;
2202       mng_info->clip=mng_info->image_box;
2203     }
2204
2205   else
2206     {
2207       image->page.y=mng_info->y_off[mng_info->object_id];
2208     }
2209
2210   image->compression=ZipCompression;
2211   image->columns=ping_width;
2212   image->rows=ping_height;
2213   if (((int) ping_color_type == PNG_COLOR_TYPE_PALETTE) ||
2214       ((int) ping_color_type == PNG_COLOR_TYPE_GRAY_ALPHA) ||
2215       ((int) ping_color_type == PNG_COLOR_TYPE_GRAY))
2216     {
2217       size_t
2218         one;
2219
2220       image->storage_class=PseudoClass;
2221       one=1;
2222       image->colors=one << ping_bit_depth;
2223 #if (MAGICKCORE_QUANTUM_DEPTH == 8)
2224       if (image->colors > 256)
2225         image->colors=256;
2226 #else
2227       if (image->colors > 65536L)
2228         image->colors=65536L;
2229 #endif
2230       if ((int) ping_color_type == PNG_COLOR_TYPE_PALETTE)
2231         {
2232           int
2233             number_colors;
2234
2235           png_colorp
2236             palette;
2237
2238           (void) png_get_PLTE(ping,ping_info,&palette,&number_colors);
2239           image->colors=(size_t) number_colors;
2240
2241           if (logging != MagickFalse)
2242             (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2243               "    Reading PNG PLTE chunk: number_colors: %d.",number_colors);
2244         }
2245     }
2246
2247   if (image->storage_class == PseudoClass)
2248     {
2249       /*
2250         Initialize image colormap.
2251       */
2252       if (AcquireImageColormap(image,image->colors) == MagickFalse)
2253         ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
2254
2255       if ((int) ping_color_type == PNG_COLOR_TYPE_PALETTE)
2256         {
2257           int
2258             number_colors;
2259
2260           png_colorp
2261             palette;
2262
2263           (void) png_get_PLTE(ping,ping_info,&palette,&number_colors);
2264
2265           for (i=0; i < (ssize_t) image->colors; i++)
2266           {
2267             image->colormap[i].red=ScaleCharToQuantum(palette[i].red);
2268             image->colormap[i].green=ScaleCharToQuantum(palette[i].green);
2269             image->colormap[i].blue=ScaleCharToQuantum(palette[i].blue);
2270           }
2271         }
2272
2273       else
2274         {
2275           size_t
2276             scale;
2277
2278           scale=(QuantumRange/((1UL << ping_bit_depth)-1));
2279
2280           if (scale < 1)
2281              scale=1;
2282
2283           for (i=0; i < (ssize_t) image->colors; i++)
2284           {
2285             image->colormap[i].red=(Quantum) (i*scale);
2286             image->colormap[i].green=(Quantum) (i*scale);
2287             image->colormap[i].blue=(Quantum) (i*scale);
2288           }
2289        }
2290     }
2291   /*
2292     Read image scanlines.
2293   */
2294   if (image->delay != 0)
2295     mng_info->scenes_found++;
2296
2297   if ((mng_info->mng_type == 0 && (image->ping != MagickFalse)) || (
2298       (image_info->number_scenes != 0) && (mng_info->scenes_found > (ssize_t)
2299       (image_info->first_scene+image_info->number_scenes))))
2300     {
2301       if (logging != MagickFalse)
2302         (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2303           "    Skipping PNG image data for scene %.20g",(double)
2304           mng_info->scenes_found-1);
2305       png_destroy_read_struct(&ping,&ping_info,&end_info);
2306 #if defined(PNG_SETJMP_NOT_THREAD_SAFE)
2307       UnlockSemaphoreInfo(ping_semaphore);
2308 #endif
2309       if (logging != MagickFalse)
2310         (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2311           "  exit ReadOnePNGImage().");
2312
2313       return(image);
2314     }
2315
2316   if (logging != MagickFalse)
2317     (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2318       "    Reading PNG IDAT chunk(s)");
2319
2320   if (num_passes > 1)
2321     ping_pixels=(unsigned char *) AcquireQuantumMemory(image->rows,
2322       ping_rowbytes*sizeof(*ping_pixels));
2323
2324   else
2325     ping_pixels=(unsigned char *) AcquireQuantumMemory(ping_rowbytes,
2326       sizeof(*ping_pixels));
2327
2328   if (ping_pixels == (unsigned char *) NULL)
2329     ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
2330
2331   if (logging != MagickFalse)
2332     (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2333       "    Converting PNG pixels to pixel packets");
2334   /*
2335     Convert PNG pixels to pixel packets.
2336   */
2337   if (setjmp(png_jmpbuf(ping)))
2338     {
2339       /*
2340         PNG image is corrupt.
2341       */
2342       png_destroy_read_struct(&ping,&ping_info,&end_info);
2343 #if defined(PNG_SETJMP_NOT_THREAD_SAFE)
2344       UnlockSemaphoreInfo(ping_semaphore);
2345 #endif
2346       if (quantum_info != (QuantumInfo *) NULL)
2347         quantum_info = DestroyQuantumInfo(quantum_info);
2348
2349       if (ping_pixels != (unsigned char *) NULL)
2350         ping_pixels=(unsigned char *) RelinquishMagickMemory(ping_pixels);
2351
2352       if (logging != MagickFalse)
2353         (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2354           "  exit ReadOnePNGImage() with error.");
2355
2356       if (image != (Image *) NULL)
2357         {
2358           InheritException(exception,&image->exception);
2359           image->columns=0;
2360         }
2361
2362       return(GetFirstImageInList(image));
2363     }
2364
2365   quantum_info=AcquireQuantumInfo(image_info,image);
2366
2367   if (quantum_info == (QuantumInfo *) NULL)
2368     ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
2369
2370   {
2371
2372    MagickBooleanType
2373      found_transparent_pixel;
2374
2375   found_transparent_pixel=MagickFalse;
2376
2377   if (image->storage_class == DirectClass)
2378     {
2379       for (pass=0; pass < num_passes; pass++)
2380       {
2381         /*
2382           Convert image to DirectClass pixel packets.
2383         */
2384 #if  (MAGICKCORE_QUANTUM_DEPTH == 8)
2385         int
2386           depth;
2387
2388         depth=(ssize_t) ping_bit_depth;
2389 #endif
2390         image->matte=(((int) ping_color_type == PNG_COLOR_TYPE_RGB_ALPHA) ||
2391             ((int) ping_color_type == PNG_COLOR_TYPE_GRAY_ALPHA) ||
2392             (png_get_valid(ping,ping_info,PNG_INFO_tRNS))) ?
2393             MagickTrue : MagickFalse;
2394
2395         for (y=0; y < (ssize_t) image->rows; y++)
2396         {
2397           if (num_passes > 1)
2398             row_offset=ping_rowbytes*y;
2399
2400           else
2401             row_offset=0;
2402
2403           png_read_row(ping,ping_pixels+row_offset,NULL);
2404           q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
2405
2406           if (q == (PixelPacket *) NULL)
2407             break;
2408
2409 #if  (0 && (MAGICKCORE_QUANTUM_DEPTH == 8) && !defined(MAGICKCORE_HDRI_SUPPORT))
2410 /* code deleted from version 6.6.6-8 */
2411 #else  /* (MAGICKCORE_QUANTUM_DEPTH != 8) */
2412
2413           if ((int) ping_color_type == PNG_COLOR_TYPE_GRAY)
2414             (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2415               GrayQuantum,ping_pixels+row_offset,exception);
2416
2417           else if ((int) ping_color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
2418             (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2419               GrayAlphaQuantum,ping_pixels+row_offset,exception);
2420
2421           else if ((int) ping_color_type == PNG_COLOR_TYPE_RGB_ALPHA)
2422             (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2423               RGBAQuantum,ping_pixels+row_offset,exception);
2424
2425           else if ((int) ping_color_type == PNG_COLOR_TYPE_PALETTE)
2426             (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2427               IndexQuantum,ping_pixels+row_offset,exception);
2428
2429           else /* ping_color_type == PNG_COLOR_TYPE_RGB */
2430             (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2431               RGBQuantum,ping_pixels+row_offset,exception);
2432 #endif
2433           if (found_transparent_pixel == MagickFalse)
2434             {
2435               /* Is there a transparent pixel in the row? */
2436               if (y== 0 && logging != MagickFalse)
2437                  (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2438                    "    Looking for cheap transparent pixel");
2439
2440               for (x=(ssize_t) image->columns-1; x >= 0; x--)
2441               {
2442                 if ((ping_color_type == PNG_COLOR_TYPE_RGBA ||
2443                     ping_color_type == PNG_COLOR_TYPE_GRAY_ALPHA) &&
2444                    (q->opacity != OpaqueOpacity))
2445                   {
2446                     if (logging != MagickFalse)
2447                       (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2448                         "    ...got one.");
2449
2450                     found_transparent_pixel = MagickTrue;
2451                     break;
2452                   }
2453                 if ((ping_color_type == PNG_COLOR_TYPE_RGB ||
2454                     ping_color_type == PNG_COLOR_TYPE_GRAY) &&
2455                     (ScaleQuantumToShort(q->red) == transparent_color.red &&
2456                     ScaleQuantumToShort(q->green) == transparent_color.green &&
2457                     ScaleQuantumToShort(q->blue) == transparent_color.blue))
2458                   {
2459                     if (logging != MagickFalse)
2460                       (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2461                         "    ...got one.");
2462                     found_transparent_pixel = MagickTrue;
2463                     break;
2464                   }
2465                 q++;
2466               }
2467             }
2468
2469           if ((image->previous == (Image *) NULL) && (num_passes == 1))
2470             {
2471               status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
2472                   image->rows);
2473
2474               if (status == MagickFalse)
2475                 break;
2476             }
2477           if (SyncAuthenticPixels(image,exception) == MagickFalse)
2478             break;
2479         }
2480
2481         if ((image->previous == (Image *) NULL) && (num_passes != 1))
2482           {
2483             status=SetImageProgress(image,LoadImageTag,pass,num_passes);
2484             if (status == MagickFalse)
2485               break;
2486           }
2487       }
2488     }
2489
2490   else /* image->storage_class != DirectClass */
2491
2492     for (pass=0; pass < num_passes; pass++)
2493     {
2494       Quantum
2495         *quantum_scanline;
2496
2497       register Quantum
2498         *r;
2499
2500       /*
2501         Convert grayscale image to PseudoClass pixel packets.
2502       */
2503       image->matte=ping_color_type == PNG_COLOR_TYPE_GRAY_ALPHA ?
2504         MagickTrue : MagickFalse;
2505
2506       quantum_scanline=(Quantum *) AcquireQuantumMemory(image->columns,
2507         (image->matte ?  2 : 1)*sizeof(*quantum_scanline));
2508
2509       if (quantum_scanline == (Quantum *) NULL)
2510         ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
2511
2512       for (y=0; y < (ssize_t) image->rows; y++)
2513       {
2514         if (num_passes > 1)
2515           row_offset=ping_rowbytes*y;
2516
2517         else
2518           row_offset=0;
2519
2520         png_read_row(ping,ping_pixels+row_offset,NULL);
2521         q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
2522
2523         if (q == (PixelPacket *) NULL)
2524           break;
2525
2526         indexes=GetAuthenticIndexQueue(image);
2527         p=ping_pixels+row_offset;
2528         r=quantum_scanline;
2529
2530         switch (ping_bit_depth)
2531         {
2532           case 1:
2533           {
2534             register ssize_t
2535               bit;
2536
2537             for (x=(ssize_t) image->columns-7; x > 0; x-=8)
2538             {
2539               for (bit=7; bit >= 0; bit--)
2540                 *r++=(Quantum) ((*p) & (0x01 << bit) ? 0x01 : 0x00);
2541               p++;
2542             }
2543
2544             if ((image->columns % 8) != 0)
2545               {
2546                 for (bit=7; bit >= (ssize_t) (8-(image->columns % 8)); bit--)
2547                   *r++=(Quantum) ((*p) & (0x01 << bit) ? 0x01 : 0x00);
2548               }
2549
2550             break;
2551           }
2552
2553           case 2:
2554           {
2555             for (x=(ssize_t) image->columns-3; x > 0; x-=4)
2556             {
2557               *r++=(*p >> 6) & 0x03;
2558               *r++=(*p >> 4) & 0x03;
2559               *r++=(*p >> 2) & 0x03;
2560               *r++=(*p++) & 0x03;
2561             }
2562
2563             if ((image->columns % 4) != 0)
2564               {
2565                 for (i=3; i >= (ssize_t) (4-(image->columns % 4)); i--)
2566                   *r++=(Quantum) ((*p >> (i*2)) & 0x03);
2567               }
2568
2569             break;
2570           }
2571
2572           case 4:
2573           {
2574             for (x=(ssize_t) image->columns-1; x > 0; x-=2)
2575             {
2576               *r++=(*p >> 4) & 0x0f;
2577               *r++=(*p++) & 0x0f;
2578             }
2579
2580             if ((image->columns % 2) != 0)
2581               *r++=(*p++ >> 4) & 0x0f;
2582
2583             break;
2584           }
2585
2586           case 8:
2587           {
2588             if (ping_color_type == 4)
2589               for (x=(ssize_t) image->columns-1; x >= 0; x--)
2590               {
2591                 *r++=*p++;
2592                 /* In image.h, OpaqueOpacity is 0
2593                  * TransparentOpacity is QuantumRange
2594                  * In a PNG datastream, Opaque is QuantumRange
2595                  * and Transparent is 0.
2596                  */
2597                 q->opacity=ScaleCharToQuantum((unsigned char) (255-(*p++)));
2598                 if (q->opacity != OpaqueOpacity)
2599                   found_transparent_pixel = MagickTrue;
2600                 q++;
2601               }
2602
2603             else
2604               for (x=(ssize_t) image->columns-1; x >= 0; x--)
2605                 *r++=*p++;
2606
2607             break;
2608           }
2609
2610           case 16:
2611           {
2612             for (x=(ssize_t) image->columns-1; x >= 0; x--)
2613             {
2614 #if (MAGICKCORE_QUANTUM_DEPTH == 16)
2615               size_t
2616                 quantum;
2617
2618               if (image->colors > 256)
2619                 *r=((*p++) << 8);
2620
2621               else
2622                 *r=0;
2623
2624               quantum=(*r);
2625               quantum|=(*p++);
2626               *r=(Quantum) quantum;
2627               r++;
2628
2629               if (ping_color_type == 4)
2630                 {
2631                   quantum=((*p++) << 8);
2632                   quantum|=(*p++);
2633                   q->opacity=(Quantum) (QuantumRange-quantum);
2634                   if (q->opacity != OpaqueOpacity)
2635                     found_transparent_pixel = MagickTrue;
2636                   q++;
2637                 }
2638 #else
2639 #if (MAGICKCORE_QUANTUM_DEPTH == 32)
2640               size_t
2641                 quantum;
2642
2643               if (image->colors > 256)
2644                 *r=((*p++) << 8);
2645
2646               else
2647                 *r=0;
2648
2649               quantum=(*r);
2650               quantum|=(*p++);
2651               *r=quantum;
2652               r++;
2653
2654               if (ping_color_type == 4)
2655                 {
2656                   q->opacity=(*p << 8) | *(p+1);
2657                   q->opacity*=65537L;
2658                   q->opacity=(Quantum) GetAlphaPixelComponent(q);
2659                   if (q->opacity != OpaqueOpacity)
2660                     found_transparent_pixel = MagickTrue;
2661                   p+=2;
2662                   q++;
2663                 }
2664
2665 #else /* MAGICKCORE_QUANTUM_DEPTH == 8 */
2666               *r++=(*p++);
2667               p++; /* strip low byte */
2668
2669               if (ping_color_type == 4)
2670                 {
2671                   q->opacity=(Quantum) (QuantumRange-(*p++));
2672                   if (q->opacity != OpaqueOpacity)
2673                     found_transparent_pixel = MagickTrue;
2674                   p++;
2675                   q++;
2676                 }
2677 #endif
2678 #endif
2679             }
2680
2681             break;
2682           }
2683
2684           default:
2685             break;
2686         }
2687         /*
2688           Transfer image scanline.
2689         */
2690         r=quantum_scanline;
2691
2692         for (x=0; x < (ssize_t) image->columns; x++)
2693           indexes[x]=(IndexPacket) (*r++);
2694
2695         if (SyncAuthenticPixels(image,exception) == MagickFalse)
2696           break;
2697
2698         if ((image->previous == (Image *) NULL) && (num_passes == 1))
2699           {
2700             status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
2701                 image->rows);
2702
2703             if (status == MagickFalse)
2704               break;
2705           }
2706       }
2707
2708       if ((image->previous == (Image *) NULL) && (num_passes != 1))
2709         {
2710           status=SetImageProgress(image,LoadImageTag,pass,num_passes);
2711
2712           if (status == MagickFalse)
2713             break;
2714         }
2715
2716       quantum_scanline=(Quantum *) RelinquishMagickMemory(quantum_scanline);
2717     }
2718
2719     image->matte=found_transparent_pixel;
2720
2721     if (logging != MagickFalse)
2722       {
2723         if (found_transparent_pixel != MagickFalse)
2724           (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2725             "    Found transparent pixel");
2726         else
2727           {
2728             (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2729               "    No transparent pixel was found");
2730           
2731             ping_color_type&=0x03;
2732           }
2733       }
2734     }
2735
2736   if (quantum_info != (QuantumInfo *) NULL)
2737     quantum_info=DestroyQuantumInfo(quantum_info);
2738
2739   if (image->storage_class == PseudoClass)
2740     {
2741       MagickBooleanType
2742         matte;
2743
2744       matte=image->matte;
2745       image->matte=MagickFalse;
2746       (void) SyncImage(image);
2747       image->matte=matte;
2748     }
2749
2750   png_read_end(ping,ping_info);
2751
2752   if (image_info->number_scenes != 0 && mng_info->scenes_found-1 <
2753       (ssize_t) image_info->first_scene && image->delay != 0)
2754     {
2755       png_destroy_read_struct(&ping,&ping_info,&end_info);
2756       ping_pixels=(unsigned char *) RelinquishMagickMemory(ping_pixels);
2757       image->colors=2;
2758       (void) SetImageBackgroundColor(image);
2759 #if defined(PNG_SETJMP_NOT_THREAD_SAFE)
2760       UnlockSemaphoreInfo(ping_semaphore);
2761 #endif
2762       if (logging != MagickFalse)
2763         (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2764           "  exit ReadOnePNGImage() early.");
2765       return(image);
2766     }
2767
2768   if (png_get_valid(ping,ping_info,PNG_INFO_tRNS))
2769     {
2770       ClassType
2771         storage_class;
2772
2773       /*
2774         Image has a transparent background.
2775       */
2776       storage_class=image->storage_class;
2777       image->matte=MagickTrue;
2778
2779 /* Balfour fix from imagemagick discourse server, 5 Feb 2010 */
2780
2781       if (storage_class == PseudoClass)
2782         {
2783           if ((int) ping_color_type == PNG_COLOR_TYPE_PALETTE)
2784             {
2785               for (x=0; x < ping_num_trans; x++)
2786               {
2787                  image->colormap[x].opacity =
2788                    ScaleCharToQuantum((unsigned char)(255-ping_trans_alpha[x]));
2789               }
2790             }
2791
2792           else if (ping_color_type == PNG_COLOR_TYPE_GRAY)
2793             {
2794               for (x=0; x < (int) image->colors; x++)
2795               {
2796                  if (ScaleQuantumToShort(image->colormap[x].red) ==
2797                      transparent_color.opacity)
2798                  {
2799                     image->colormap[x].opacity = (Quantum) TransparentOpacity;
2800                  }
2801               }
2802             }
2803           (void) SyncImage(image);
2804         }
2805
2806 #if 1 /* Should have already been done above, but glennrp problem P10
2807        * needs this.
2808        */
2809       else
2810         {
2811           for (y=0; y < (ssize_t) image->rows; y++)
2812           {
2813             image->storage_class=storage_class;
2814             q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
2815
2816             if (q == (PixelPacket *) NULL)
2817               break;
2818
2819             indexes=GetAuthenticIndexQueue(image);
2820
2821             /* Caution: on a Q8 build, this does not distinguish between
2822              * 16-bit colors that differ only in the low byte
2823              */
2824             for (x=(ssize_t) image->columns-1; x >= 0; x--)
2825             {
2826               if (ScaleQuantumToShort(q->red) == transparent_color.red &&
2827                   ScaleQuantumToShort(q->green) == transparent_color.green &&
2828                   ScaleQuantumToShort(q->blue) == transparent_color.blue)
2829                 {
2830                   q->opacity=(Quantum) TransparentOpacity;
2831                 }
2832
2833 #if 0 /* I have not found a case where this is needed. */
2834               else
2835                 {
2836                   q->opacity=(Quantum) OpaqueOpacity;
2837                 }
2838 #endif
2839
2840               q++;
2841             }
2842
2843             if (SyncAuthenticPixels(image,exception) == MagickFalse)
2844                break;
2845           }
2846         }
2847 #endif
2848
2849       image->storage_class=DirectClass;
2850     }
2851
2852   if ((ping_color_type == PNG_COLOR_TYPE_GRAY) ||
2853       (ping_color_type == PNG_COLOR_TYPE_GRAY_ALPHA))
2854     image->colorspace=GRAYColorspace;
2855
2856   if (png_get_text(ping,ping_info,&text,&num_text) != 0)
2857     for (i=0; i < (ssize_t) num_text; i++)
2858     {
2859       /* Check for a profile */
2860
2861       if (logging != MagickFalse)
2862         (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2863           "    Reading PNG text chunk");
2864
2865       if (memcmp(text[i].key, "Raw profile type ",17) == 0)
2866           (void) Magick_png_read_raw_profile(image,image_info,text,(int) i);
2867
2868       else
2869         {
2870           char
2871             *value;
2872
2873           length=text[i].text_length;
2874           value=(char *) AcquireQuantumMemory(length+MaxTextExtent,
2875             sizeof(*value));
2876           if (value == (char *) NULL)
2877             {
2878               (void) ThrowMagickException(&image->exception,GetMagickModule(),
2879                 ResourceLimitError,"MemoryAllocationFailed","`%s'",
2880                 image->filename);
2881               break;
2882             }
2883           *value='\0';
2884           (void) ConcatenateMagickString(value,text[i].text,length+2);
2885           (void) SetImageProperty(image,text[i].key,value);
2886
2887           if (logging != MagickFalse)
2888           {
2889             (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2890               "      length: %lu",(unsigned long) length);
2891             (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2892               "      Keyword: %s",text[i].key);
2893           }
2894
2895           value=DestroyString(value);
2896         }
2897     }
2898
2899 #ifdef MNG_OBJECT_BUFFERS
2900   /*
2901     Store the object if necessary.
2902   */
2903   if (object_id && !mng_info->frozen[object_id])
2904     {
2905       if (mng_info->ob[object_id] == (MngBuffer *) NULL)
2906         {
2907           /*
2908             create a new object buffer.
2909           */
2910           mng_info->ob[object_id]=(MngBuffer *)
2911             AcquireMagickMemory(sizeof(MngBuffer));
2912
2913           if (mng_info->ob[object_id] != (MngBuffer *) NULL)
2914             {
2915               mng_info->ob[object_id]->image=(Image *) NULL;
2916               mng_info->ob[object_id]->reference_count=1;
2917             }
2918         }
2919
2920       if ((mng_info->ob[object_id] == (MngBuffer *) NULL) ||
2921           mng_info->ob[object_id]->frozen)
2922         {
2923           if (mng_info->ob[object_id] == (MngBuffer *) NULL)
2924             (void) ThrowMagickException(&image->exception,GetMagickModule(),
2925               ResourceLimitError,"MemoryAllocationFailed","`%s'",
2926               image->filename);
2927
2928           if (mng_info->ob[object_id]->frozen)
2929             (void) ThrowMagickException(&image->exception,GetMagickModule(),
2930               ResourceLimitError,"Cannot overwrite frozen MNG object buffer",
2931               "`%s'",image->filename);
2932         }
2933
2934       else
2935         {
2936
2937           if (mng_info->ob[object_id]->image != (Image *) NULL)
2938             mng_info->ob[object_id]->image=DestroyImage
2939                 (mng_info->ob[object_id]->image);
2940
2941           mng_info->ob[object_id]->image=CloneImage(image,0,0,MagickTrue,
2942             &image->exception);
2943
2944           if (mng_info->ob[object_id]->image != (Image *) NULL)
2945             mng_info->ob[object_id]->image->file=(FILE *) NULL;
2946
2947           else
2948             (void) ThrowMagickException(&image->exception,GetMagickModule(),
2949               ResourceLimitError,"Cloning image for object buffer failed",
2950               "`%s'",image->filename);
2951
2952           if (ping_width > 250000L || ping_height > 250000L)
2953              png_error(ping,"PNG Image dimensions are too large.");
2954
2955           mng_info->ob[object_id]->width=ping_width;
2956           mng_info->ob[object_id]->height=ping_height;
2957           mng_info->ob[object_id]->color_type=ping_color_type;
2958           mng_info->ob[object_id]->sample_depth=ping_bit_depth;
2959           mng_info->ob[object_id]->interlace_method=ping_interlace_method;
2960           mng_info->ob[object_id]->compression_method=
2961              ping_compression_method;
2962           mng_info->ob[object_id]->filter_method=ping_filter_method;
2963
2964           if (png_get_valid(ping,ping_info,PNG_INFO_PLTE))
2965             {
2966               int
2967                 number_colors;
2968
2969               png_colorp
2970                 plte;
2971
2972               /*
2973                 Copy the PLTE to the object buffer.
2974               */
2975               png_get_PLTE(ping,ping_info,&plte,&number_colors);
2976               mng_info->ob[object_id]->plte_length=number_colors;
2977
2978               for (i=0; i < number_colors; i++)
2979               {
2980                 mng_info->ob[object_id]->plte[i]=plte[i];
2981               }
2982             }
2983
2984           else
2985               mng_info->ob[object_id]->plte_length=0;
2986         }
2987     }
2988 #endif
2989   /*
2990     Relinquish resources.
2991   */
2992   png_destroy_read_struct(&ping,&ping_info,&end_info);
2993
2994   ping_pixels=(unsigned char *) RelinquishMagickMemory(ping_pixels);
2995 #if defined(PNG_SETJMP_NOT_THREAD_SAFE)
2996   UnlockSemaphoreInfo(ping_semaphore);
2997 #endif
2998
2999   if (logging != MagickFalse)
3000     (void) LogMagickEvent(CoderEvent,GetMagickModule(),
3001       "  exit ReadOnePNGImage()");
3002
3003   return(image);
3004
3005 /* end of reading one PNG image */
3006 }
3007
3008 static Image *ReadPNGImage(const ImageInfo *image_info,ExceptionInfo *exception)
3009 {
3010   Image
3011     *image,
3012     *previous;
3013
3014   MagickBooleanType
3015     have_mng_structure,
3016     logging,
3017     status;
3018
3019   MngInfo
3020     *mng_info;
3021
3022   char
3023     magic_number[MaxTextExtent];
3024
3025   ssize_t
3026     count;
3027
3028   /*
3029     Open image file.
3030   */
3031   assert(image_info != (const ImageInfo *) NULL);
3032   assert(image_info->signature == MagickSignature);
3033
3034   if (image_info->debug != MagickFalse)
3035     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
3036       image_info->filename);
3037
3038   assert(exception != (ExceptionInfo *) NULL);
3039   assert(exception->signature == MagickSignature);
3040   logging=LogMagickEvent(CoderEvent,GetMagickModule(),"Enter ReadPNGImage()");
3041   image=AcquireImage(image_info);
3042   mng_info=(MngInfo *) NULL;
3043   status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
3044
3045   if (status == MagickFalse)
3046     ThrowReaderException(FileOpenError,"UnableToOpenFile");
3047
3048   /*
3049     Verify PNG signature.
3050   */
3051   count=ReadBlob(image,8,(unsigned char *) magic_number);
3052
3053   if (count < 8 || memcmp(magic_number,"\211PNG\r\n\032\n",8) != 0)
3054     ThrowReaderException(CorruptImageError,"ImproperImageHeader");
3055
3056   /*
3057     Allocate a MngInfo structure.
3058   */
3059   have_mng_structure=MagickFalse;
3060   mng_info=(MngInfo *) AcquireMagickMemory(sizeof(MngInfo));
3061
3062   if (mng_info == (MngInfo *) NULL)
3063     ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
3064
3065   /*
3066     Initialize members of the MngInfo structure.
3067   */
3068   (void) ResetMagickMemory(mng_info,0,sizeof(MngInfo));
3069   mng_info->image=image;
3070   have_mng_structure=MagickTrue;
3071
3072   previous=image;
3073   image=ReadOnePNGImage(mng_info,image_info,exception);
3074   MngInfoFreeStruct(mng_info,&have_mng_structure);
3075
3076   if (image == (Image *) NULL)
3077     {
3078       if (previous != (Image *) NULL)
3079         {
3080           if (previous->signature != MagickSignature)
3081             ThrowReaderException(CorruptImageError,"CorruptImage");
3082
3083           (void) CloseBlob(previous);
3084           (void) DestroyImageList(previous);
3085         }
3086
3087       if (logging != MagickFalse)
3088         (void) LogMagickEvent(CoderEvent,GetMagickModule(),
3089           "exit ReadPNGImage() with error");
3090
3091       return((Image *) NULL);
3092     }
3093
3094   (void) CloseBlob(image);
3095
3096   if ((image->columns == 0) || (image->rows == 0))
3097     {
3098       if (logging != MagickFalse)
3099         (void) LogMagickEvent(CoderEvent,GetMagickModule(),
3100           "exit ReadPNGImage() with error.");
3101
3102       ThrowReaderException(CorruptImageError,"CorruptImage");
3103     }
3104
3105   if (LocaleCompare(image_info->magick,"PNG8") == 0)
3106     {
3107       (void) SetImageType(image,PaletteType);
3108
3109       if (image->matte != MagickFalse)
3110         {
3111           /* To do: Reduce to binary transparency */
3112         }
3113     }
3114
3115   if (LocaleCompare(image_info->magick,"PNG24") == 0)
3116     {
3117       (void) SetImageType(image,TrueColorType);
3118       image->matte=MagickFalse;
3119     }
3120
3121   if (LocaleCompare(image_info->magick,"PNG32") == 0)
3122     (void) SetImageType(image,TrueColorMatteType);
3123
3124   if (logging != MagickFalse)
3125     (void) LogMagickEvent(CoderEvent,GetMagickModule(),
3126         "  page.w: %.20g, page.h: %.20g,page.x: %.20g, page.y: %.20g.",
3127             (double) image->page.width,(double) image->page.height,
3128             (double) image->page.x,(double) image->page.y);
3129
3130   if (logging != MagickFalse)
3131     (void) LogMagickEvent(CoderEvent,GetMagickModule(),"exit ReadPNGImage()");
3132
3133   return(image);
3134 }
3135
3136
3137
3138 #if defined(JNG_SUPPORTED)
3139 /*
3140 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3141 %                                                                             %
3142 %                                                                             %
3143 %                                                                             %
3144 %   R e a d O n e J N G I m a g e                                             %
3145 %                                                                             %
3146 %                                                                             %
3147 %                                                                             %
3148 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3149 %
3150 %  ReadOneJNGImage() reads a JPEG Network Graphics (JNG) image file
3151 %  (minus the 8-byte signature)  and returns it.  It allocates the memory
3152 %  necessary for the new Image structure and returns a pointer to the new
3153 %  image.
3154 %
3155 %  JNG support written by Glenn Randers-Pehrson, glennrp@image...
3156 %
3157 %  The format of the ReadOneJNGImage method is:
3158 %
3159 %      Image *ReadOneJNGImage(MngInfo *mng_info, const ImageInfo *image_info,
3160 %         ExceptionInfo *exception)
3161 %
3162 %  A description of each parameter follows:
3163 %
3164 %    o mng_info: Specifies a pointer to a MngInfo structure.
3165 %
3166 %    o image_info: the image info.
3167 %
3168 %    o exception: return any errors or warnings in this structure.
3169 %
3170 */
3171 static Image *ReadOneJNGImage(MngInfo *mng_info,
3172     const ImageInfo *image_info, ExceptionInfo *exception)
3173 {
3174   Image
3175     *alpha_image,
3176     *color_image,
3177     *image,
3178     *jng_image;
3179
3180   ImageInfo
3181     *alpha_image_info,
3182     *color_image_info;
3183
3184   MagickBooleanType
3185     logging;
3186
3187   ssize_t
3188     y;
3189
3190   MagickBooleanType
3191     status;
3192
3193   png_uint_32
3194     jng_height,
3195     jng_width;
3196
3197   png_byte
3198     jng_color_type,
3199     jng_image_sample_depth,
3200     jng_image_compression_method,
3201     jng_image_interlace_method,
3202     jng_alpha_sample_depth,
3203     jng_alpha_compression_method,
3204     jng_alpha_filter_method,
3205     jng_alpha_interlace_method;
3206
3207   register const PixelPacket
3208     *s;
3209
3210   register ssize_t
3211     i,
3212     x;
3213
3214   register PixelPacket
3215     *q;
3216
3217   register unsigned char
3218     *p;
3219
3220   unsigned int
3221     read_JSEP,
3222     reading_idat,
3223     skip_to_iend;
3224
3225   size_t
3226     length;
3227
3228   jng_alpha_compression_method=0;
3229   jng_alpha_sample_depth=8;
3230   jng_color_type=0;
3231   jng_height=0;
3232   jng_width=0;
3233   alpha_image=(Image *) NULL;
3234   color_image=(Image *) NULL;
3235   alpha_image_info=(ImageInfo *) NULL;
3236   color_image_info=(ImageInfo *) NULL;
3237
3238   logging=LogMagickEvent(CoderEvent,GetMagickModule(),
3239     "  Enter ReadOneJNGImage()");
3240
3241   image=mng_info->image;
3242
3243   if (GetAuthenticPixelQueue(image) != (PixelPacket *) NULL)
3244     {
3245       /*
3246         Allocate next image structure.
3247       */
3248       if (logging != MagickFalse)
3249         (void) LogMagickEvent(CoderEvent,GetMagickModule(),
3250            "  AcquireNextImage()");
3251
3252       AcquireNextImage(image_info,image);
3253
3254       if (GetNextImageInList(image) == (Image *) NULL)
3255         return((Image *) NULL);
3256
3257       image=SyncNextImageInList(image);
3258     }
3259   mng_info->image=image;
3260
3261   /*
3262     Signature bytes have already been read.
3263   */
3264
3265   read_JSEP=MagickFalse;
3266   reading_idat=MagickFalse;
3267   skip_to_iend=MagickFalse;
3268   for (;;)
3269   {
3270     char
3271       type[MaxTextExtent];
3272
3273     unsigned char
3274       *chunk;
3275
3276     unsigned int
3277       count;
3278
3279     /*
3280       Read a new JNG chunk.
3281     */
3282     status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
3283       2*GetBlobSize(image));
3284
3285     if (status == MagickFalse)
3286       break;
3287
3288     type[0]='\0';
3289     (void) ConcatenateMagickString(type,"errr",MaxTextExtent);
3290     length=ReadBlobMSBLong(image);
3291     count=(unsigned int) ReadBlob(image,4,(unsigned char *) type);
3292
3293     if (logging != MagickFalse)
3294       (void) LogMagickEvent(CoderEvent,GetMagickModule(),
3295         "  Reading JNG chunk type %c%c%c%c, length: %.20g",
3296         type[0],type[1],type[2],type[3],(double) length);
3297
3298     if (length > PNG_UINT_31_MAX || count == 0)
3299       ThrowReaderException(CorruptImageError,"CorruptImage");
3300
3301     p=NULL;
3302     chunk=(unsigned char *) NULL;
3303
3304     if (length)
3305       {
3306         chunk=(unsigned char *) AcquireQuantumMemory(length,sizeof(*chunk));
3307
3308         if (chunk == (unsigned char *) NULL)
3309           ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
3310
3311         for (i=0; i < (ssize_t) length; i++)
3312           chunk[i]=(unsigned char) ReadBlobByte(image);
3313
3314         p=chunk;
3315       }
3316
3317     (void) ReadBlobMSBLong(image);  /* read crc word */
3318
3319     if (skip_to_iend)
3320       {
3321         if (length)
3322           chunk=(unsigned char *) RelinquishMagickMemory(chunk);
3323
3324         continue;
3325       }
3326
3327     if (memcmp(type,mng_JHDR,4) == 0)
3328       {
3329         if (length == 16)
3330           {
3331             jng_width=(size_t) ((p[0] << 24) | (p[1] << 16) |
3332                 (p[2] << 8) | p[3]);
3333             jng_height=(size_t) ((p[4] << 24) | (p[5] << 16) |
3334                 (p[6] << 8) | p[7]);
3335             jng_color_type=p[8];
3336             jng_image_sample_depth=p[9];
3337             jng_image_compression_method=p[10];
3338             jng_image_interlace_method=p[11];
3339
3340             image->interlace=jng_image_interlace_method != 0 ? PNGInterlace :
3341               NoInterlace;
3342
3343             jng_alpha_sample_depth=p[12];
3344             jng_alpha_compression_method=p[13];
3345             jng_alpha_filter_method=p[14];
3346             jng_alpha_interlace_method=p[15];
3347
3348             if (logging != MagickFalse)
3349               {
3350                 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
3351                   "    jng_width:      %16lu",(unsigned long) jng_width);
3352
3353                 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
3354                   "    jng_width:      %16lu",(unsigned long) jng_height);
3355
3356                 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
3357                   "    jng_color_type: %16d",jng_color_type);
3358
3359                 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
3360                   "    jng_image_sample_depth:      %3d",
3361                   jng_image_sample_depth);
3362
3363                 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
3364                   "    jng_image_compression_method:%3d",
3365                   jng_image_compression_method);
3366
3367                 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
3368                   "    jng_image_interlace_method:  %3d",
3369                   jng_image_interlace_method);
3370
3371                 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
3372                   "    jng_alpha_sample_depth:      %3d",
3373                   jng_alpha_sample_depth);
3374
3375                 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
3376                   "    jng_alpha_compression_method:%3d",
3377                   jng_alpha_compression_method);
3378
3379                 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
3380                   "    jng_alpha_filter_method:     %3d",
3381                   jng_alpha_filter_method);
3382
3383                 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
3384                   "    jng_alpha_interlace_method:  %3d",
3385                   jng_alpha_interlace_method);
3386               }
3387           }
3388
3389         if (length)
3390           chunk=(unsigned char *) RelinquishMagickMemory(chunk);
3391
3392         continue;
3393       }
3394
3395
3396     if ((reading_idat == MagickFalse) && (read_JSEP == MagickFalse) &&
3397         ((memcmp(type,mng_JDAT,4) == 0) || (memcmp(type,mng_JdAA,4) == 0) ||
3398          (memcmp(type,mng_IDAT,4) == 0) || (memcmp(type,mng_JDAA,4) == 0)))
3399       {
3400         /*
3401            o create color_image
3402            o open color_blob, attached to color_image
3403            o if (color type has alpha)
3404                open alpha_blob, attached to alpha_image
3405         */
3406
3407         color_image_info=(ImageInfo *)AcquireMagickMemory(sizeof(ImageInfo));
3408
3409         if (color_image_info == (ImageInfo *) NULL)
3410           ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
3411
3412         GetImageInfo(color_image_info);
3413         color_image=AcquireImage(color_image_info);
3414
3415         if (color_image == (Image *) NULL)
3416           ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
3417
3418         if (logging != MagickFalse)
3419           (void) LogMagickEvent(CoderEvent,GetMagickModule(),
3420             "    Creating color_blob.");
3421
3422         (void) AcquireUniqueFilename(color_image->filename);
3423         status=OpenBlob(color_image_info,color_image,WriteBinaryBlobMode,
3424           exception);
3425
3426         if (status == MagickFalse)
3427           return((Image *) NULL);
3428
3429         if ((image_info->ping == MagickFalse) && (jng_color_type >= 12))
3430           {
3431             alpha_image_info=(ImageInfo *)
3432               AcquireMagickMemory(sizeof(ImageInfo));
3433
3434             if (alpha_image_info == (ImageInfo *) NULL)
3435               ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
3436
3437             GetImageInfo(alpha_image_info);
3438             alpha_image=AcquireImage(alpha_image_info);
3439
3440             if (alpha_image == (Image *) NULL)
3441               {
3442                 alpha_image=DestroyImage(alpha_image);
3443                 ThrowReaderException(ResourceLimitError,
3444                   "MemoryAllocationFailed");
3445               }
3446
3447             if (logging != MagickFalse)
3448               (void) LogMagickEvent(CoderEvent,GetMagickModule(),
3449                 "    Creating alpha_blob.");
3450
3451             (void) AcquireUniqueFilename(alpha_image->filename);
3452             status=OpenBlob(alpha_image_info,alpha_image,WriteBinaryBlobMode,
3453               exception);
3454
3455             if (status == MagickFalse)
3456               return((Image *) NULL);
3457
3458             if (jng_alpha_compression_method == 0)
3459               {
3460                 unsigned char
3461                   data[18];
3462
3463                 if (logging != MagickFalse)
3464                   (void) LogMagickEvent(CoderEvent,GetMagickModule(),
3465                     "    Writing IHDR chunk to alpha_blob.");
3466
3467                 (void) WriteBlob(alpha_image,8,(const unsigned char *)
3468                   "\211PNG\r\n\032\n");
3469
3470                 (void) WriteBlobMSBULong(alpha_image,13L);
3471                 PNGType(data,mng_IHDR);
3472                 LogPNGChunk(logging,mng_IHDR,13L);
3473                 PNGLong(data+4,jng_width);
3474                 PNGLong(data+8,jng_height);
3475                 data[12]=jng_alpha_sample_depth;
3476                 data[13]=0; /* color_type gray */
3477                 data[14]=0; /* compression method 0 */
3478                 data[15]=0; /* filter_method 0 */
3479                 data[16]=0; /* interlace_method 0 */
3480                 (void) WriteBlob(alpha_image,17,data);
3481                 (void) WriteBlobMSBULong(alpha_image,crc32(0,data,17));
3482               }
3483           }
3484         reading_idat=MagickTrue;
3485       }
3486
3487     if (memcmp(type,mng_JDAT,4) == 0)
3488       {
3489         /* Copy chunk to color_image->blob */
3490
3491         if (logging != MagickFalse)
3492           (void) LogMagickEvent(CoderEvent,GetMagickModule(),
3493             "    Copying JDAT chunk data to color_blob.");
3494
3495         (void) WriteBlob(color_image,length,chunk);
3496
3497         if (length)
3498           chunk=(unsigned char *) RelinquishMagickMemory(chunk);
3499
3500         continue;
3501       }
3502
3503     if (memcmp(type,mng_IDAT,4) == 0)
3504       {
3505         png_byte
3506            data[5];
3507
3508         /* Copy IDAT header and chunk data to alpha_image->blob */
3509
3510         if (image_info->ping == MagickFalse)
3511           {
3512             if (logging != MagickFalse)
3513               (void) LogMagickEvent(CoderEvent,GetMagickModule(),
3514                 "    Copying IDAT chunk data to alpha_blob.");
3515
3516             (void) WriteBlobMSBULong(alpha_image,(size_t) length);
3517             PNGType(data,mng_IDAT);
3518             LogPNGChunk(logging,mng_IDAT,length);
3519             (void) WriteBlob(alpha_image,4,data);
3520             (void) WriteBlob(alpha_image,length,chunk);
3521             (void) WriteBlobMSBULong(alpha_image,
3522               crc32(crc32(0,data,4),chunk,(uInt) length));
3523           }
3524
3525         if (length)
3526           chunk=(unsigned char *) RelinquishMagickMemory(chunk);
3527
3528         continue;
3529       }
3530
3531     if ((memcmp(type,mng_JDAA,4) == 0) || (memcmp(type,mng_JdAA,4) == 0))
3532       {
3533         /* Copy chunk data to alpha_image->blob */
3534
3535         if (image_info->ping == MagickFalse)
3536           {
3537             if (logging != MagickFalse)
3538               (void) LogMagickEvent(CoderEvent,GetMagickModule(),
3539                 "    Copying JDAA chunk data to alpha_blob.");
3540
3541             (void) WriteBlob(alpha_image,length,chunk);
3542           }
3543
3544         if (length)
3545           chunk=(unsigned char *) RelinquishMagickMemory(chunk);
3546
3547         continue;
3548       }
3549
3550     if (memcmp(type,mng_JSEP,4) == 0)
3551       {
3552         read_JSEP=MagickTrue;
3553
3554         if (length)
3555           chunk=(unsigned char *) RelinquishMagickMemory(chunk);
3556
3557         continue;
3558       }
3559
3560     if (memcmp(type,mng_bKGD,4) == 0)
3561       {
3562         if (length == 2)
3563           {
3564             image->background_color.red=ScaleCharToQuantum(p[1]);
3565             image->background_color.green=image->background_color.red;
3566             image->background_color.blue=image->background_color.red;
3567           }
3568
3569         if (length == 6)
3570           {
3571             image->background_color.red=ScaleCharToQuantum(p[1]);
3572             image->background_color.green=ScaleCharToQuantum(p[3]);
3573             image->background_color.blue=ScaleCharToQuantum(p[5]);
3574           }
3575
3576         chunk=(unsigned char *) RelinquishMagickMemory(chunk);
3577         continue;
3578       }
3579
3580     if (memcmp(type,mng_gAMA,4) == 0)
3581       {
3582         if (length == 4)
3583           image->gamma=((float) mng_get_long(p))*0.00001;
3584
3585         chunk=(unsigned char *) RelinquishMagickMemory(chunk);
3586         continue;
3587       }
3588
3589     if (memcmp(type,mng_cHRM,4) == 0)
3590       {
3591         if (length == 32)
3592           {
3593             image->chromaticity.white_point.x=0.00001*mng_get_long(p);
3594             image->chromaticity.white_point.y=0.00001*mng_get_long(&p[4]);
3595             image->chromaticity.red_primary.x=0.00001*mng_get_long(&p[8]);
3596             image->chromaticity.red_primary.y=0.00001*mng_get_long(&p[12]);
3597             image->chromaticity.green_primary.x=0.00001*mng_get_long(&p[16]);
3598             image->chromaticity.green_primary.y=0.00001*mng_get_long(&p[20]);
3599             image->chromaticity.blue_primary.x=0.00001*mng_get_long(&p[24]);
3600             image->chromaticity.blue_primary.y=0.00001*mng_get_long(&p[28]);
3601           }
3602
3603         chunk=(unsigned char *) RelinquishMagickMemory(chunk);
3604         continue;
3605       }
3606
3607     if (memcmp(type,mng_sRGB,4) == 0)
3608       {
3609         if (length == 1)
3610           {
3611             image->rendering_intent=
3612               Magick_RenderingIntent_from_PNG_RenderingIntent(p[0]);
3613             image->gamma=0.45455f;
3614             image->chromaticity.red_primary.x=0.6400f;
3615             image->chromaticity.red_primary.y=0.3300f;
3616             image->chromaticity.green_primary.x=0.3000f;
3617             image->chromaticity.green_primary.y=0.6000f;
3618             image->chromaticity.blue_primary.x=0.1500f;
3619             image->chromaticity.blue_primary.y=0.0600f;
3620             image->chromaticity.white_point.x=0.3127f;
3621             image->chromaticity.white_point.y=0.3290f;
3622           }
3623
3624         chunk=(unsigned char *) RelinquishMagickMemory(chunk);
3625         continue;
3626       }
3627
3628     if (memcmp(type,mng_oFFs,4) == 0)
3629       {
3630         if (length > 8)
3631           {
3632             image->page.x=(ssize_t) mng_get_long(p);
3633             image->page.y=(ssize_t) mng_get_long(&p[4]);
3634
3635             if ((int) p[8] != 0)
3636               {
3637                 image->page.x/=10000;
3638                 image->page.y/=10000;
3639               }
3640           }
3641
3642         if (length)
3643           chunk=(unsigned char *) RelinquishMagickMemory(chunk);
3644
3645         continue;
3646       }
3647
3648     if (memcmp(type,mng_pHYs,4) == 0)
3649       {
3650         if (length > 8)
3651           {
3652             image->x_resolution=(double) mng_get_long(p);
3653             image->y_resolution=(double) mng_get_long(&p[4]);
3654             if ((int) p[8] == PNG_RESOLUTION_METER)
3655               {
3656                 image->units=PixelsPerCentimeterResolution;
3657                 image->x_resolution=image->x_resolution/100.0f;
3658                 image->y_resolution=image->y_resolution/100.0f;
3659               }
3660           }
3661
3662         chunk=(unsigned char *) RelinquishMagickMemory(chunk);
3663         continue;
3664       }
3665
3666 #if 0
3667     if (memcmp(type,mng_iCCP,4) == 0)
3668       {
3669         /* To do: */
3670         if (length)
3671           chunk=(unsigned char *) RelinquishMagickMemory(chunk);
3672
3673         continue;
3674       }
3675 #endif
3676
3677     if (length)
3678       chunk=(unsigned char *) RelinquishMagickMemory(chunk);
3679
3680     if (memcmp(type,mng_IEND,4))
3681       continue;
3682
3683     break;
3684   }
3685
3686
3687   /* IEND found */
3688
3689   /*
3690     Finish up reading image data:
3691
3692        o read main image from color_blob.
3693
3694        o close color_blob.
3695
3696        o if (color_type has alpha)
3697             if alpha_encoding is PNG
3698                read secondary image from alpha_blob via ReadPNG
3699             if alpha_encoding is JPEG
3700                read secondary image from alpha_blob via ReadJPEG
3701
3702        o close alpha_blob.
3703
3704        o copy intensity of secondary image into
3705          opacity samples of main image.
3706
3707        o destroy the secondary image.
3708   */
3709
3710   (void) CloseBlob(color_image);
3711
3712   if (logging != MagickFalse)
3713     (void) LogMagickEvent(CoderEvent,GetMagickModule(),
3714       "    Reading jng_image from color_blob.");
3715
3716   (void) FormatMagickString(color_image_info->filename,MaxTextExtent,"%s",
3717     color_image->filename);
3718
3719   color_image_info->ping=MagickFalse;   /* To do: avoid this */
3720   jng_image=ReadImage(color_image_info,exception);
3721
3722   if (jng_image == (Image *) NULL)
3723     return((Image *) NULL);
3724
3725   (void) RelinquishUniqueFileResource(color_image->filename);
3726   color_image=DestroyImage(color_image);
3727   color_image_info=DestroyImageInfo(color_image_info);
3728
3729   if (jng_image == (Image *) NULL)
3730     return((Image *) NULL);
3731
3732   if (logging != MagickFalse)
3733     (void) LogMagickEvent(CoderEvent,GetMagickModule(),
3734       "    Copying jng_image pixels to main image.");
3735
3736   image->rows=jng_height;
3737   image->columns=jng_width;
3738   length=image->columns*sizeof(PixelPacket);
3739
3740   for (y=0; y < (ssize_t) image->rows; y++)
3741   {
3742     s=GetVirtualPixels(jng_image,0,y,image->columns,1,&image->exception);
3743     q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
3744     (void) CopyMagickMemory(q,s,length);
3745
3746     if (SyncAuthenticPixels(image,exception) == MagickFalse)
3747       break;
3748   }
3749
3750   jng_image=DestroyImage(jng_image);
3751
3752   if (image_info->ping == MagickFalse)
3753     {
3754      if (jng_color_type >= 12)
3755        {
3756          if (jng_alpha_compression_method == 0)
3757            {
3758              png_byte
3759                data[5];
3760              (void) WriteBlobMSBULong(alpha_image,0x00000000L);
3761              PNGType(data,mng_IEND);
3762              LogPNGChunk(logging,mng_IEND,0L);
3763              (void) WriteBlob(alpha_image,4,data);
3764              (void) WriteBlobMSBULong(alpha_image,crc32(0,data,4));
3765            }
3766
3767          (void) CloseBlob(alpha_image);
3768
3769          if (logging != MagickFalse)
3770            (void) LogMagickEvent(CoderEvent,GetMagickModule(),
3771              "    Reading opacity from alpha_blob.");
3772
3773          (void) FormatMagickString(alpha_image_info->filename,MaxTextExtent,
3774            "%s",alpha_image->filename);
3775
3776          jng_image=ReadImage(alpha_image_info,exception);
3777
3778          if (jng_image != (Image *) NULL)
3779            for (y=0; y < (ssize_t) image->rows; y++)
3780            {
3781              s=GetVirtualPixels(jng_image,0,y,image->columns,1,
3782                 &image->exception);
3783              q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
3784
3785              if (image->matte != MagickFalse)
3786                for (x=(ssize_t) image->columns; x != 0; x--,q++,s++)
3787                   q->opacity=(Quantum) QuantumRange-s->red;
3788
3789              else
3790                for (x=(ssize_t) image->columns; x != 0; x--,q++,s++)
3791                {
3792                   q->opacity=(Quantum) QuantumRange-s->red;
3793                   if (q->opacity != OpaqueOpacity)
3794                     image->matte=MagickTrue;
3795                }
3796
3797              if (SyncAuthenticPixels(image,exception) == MagickFalse)
3798                break;
3799            }
3800          (void) RelinquishUniqueFileResource(alpha_image->filename);
3801          alpha_image=DestroyImage(alpha_image);
3802          alpha_image_info=DestroyImageInfo(alpha_image_info);
3803          if (jng_image != (Image *) NULL)
3804            jng_image=DestroyImage(jng_image);
3805        }
3806     }
3807
3808   /* Read the JNG image.  */
3809
3810   if (mng_info->mng_type == 0)
3811     {
3812       mng_info->mng_width=jng_width;
3813       mng_info->mng_height=jng_height;
3814     }
3815
3816   if (image->page.width == 0 && image->page.height == 0)
3817     {
3818       image->page.width=jng_width;
3819       image->page.height=jng_height;
3820     }
3821
3822   if (image->page.x == 0 && image->page.y == 0)
3823     {
3824       image->page.x=mng_info->x_off[mng_info->object_id];
3825       image->page.y=mng_info->y_off[mng_info->object_id];
3826     }
3827
3828   else
3829     {
3830       image->page.y=mng_info->y_off[mng_info->object_id];
3831     }
3832
3833   mng_info->image_found++;
3834   status=SetImageProgress(image,LoadImagesTag,2*TellBlob(image),
3835     2*GetBlobSize(image));
3836
3837   if (logging != MagickFalse)
3838     (void) LogMagickEvent(CoderEvent,GetMagickModule(),
3839       "  exit ReadOneJNGImage()");
3840
3841   return(image);
3842 }
3843
3844 /*
3845 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3846 %                                                                             %
3847 %                                                                             %
3848 %                                                                             %
3849 %   R e a d J N G I m a g e                                                   %
3850 %                                                                             %
3851 %                                                                             %
3852 %                                                                             %
3853 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3854 %
3855 %  ReadJNGImage() reads a JPEG Network Graphics (JNG) image file
3856 %  (including the 8-byte signature)  and returns it.  It allocates the memory
3857 %  necessary for the new Image structure and returns a pointer to the new
3858 %  image.
3859 %
3860 %  JNG support written by Glenn Randers-Pehrson, glennrp@image...
3861 %
3862 %  The format of the ReadJNGImage method is:
3863 %
3864 %      Image *ReadJNGImage(const ImageInfo *image_info, ExceptionInfo
3865 %         *exception)
3866 %
3867 %  A description of each parameter follows:
3868 %
3869 %    o image_info: the image info.
3870 %
3871 %    o exception: return any errors or warnings in this structure.
3872 %
3873 */
3874
3875 static Image *ReadJNGImage(const ImageInfo *image_info,ExceptionInfo *exception)
3876 {
3877   Image
3878     *image,
3879     *previous;
3880
3881   MagickBooleanType
3882     have_mng_structure,
3883     logging,
3884     status;
3885
3886   MngInfo
3887     *mng_info;
3888
3889   char
3890     magic_number[MaxTextExtent];
3891
3892   size_t
3893     count;
3894
3895   /*
3896     Open image file.
3897   */
3898   assert(image_info != (const ImageInfo *) NULL);
3899   assert(image_info->signature == MagickSignature);
3900   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image_info->filename);
3901   assert(exception != (ExceptionInfo *) NULL);
3902   assert(exception->signature == MagickSignature);
3903   logging=LogMagickEvent(CoderEvent,GetMagickModule(),"Enter ReadJNGImage()");
3904   image=AcquireImage(image_info);
3905   mng_info=(MngInfo *) NULL;
3906   status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
3907
3908   if (status == MagickFalse)
3909     return((Image *) NULL);
3910
3911   if (LocaleCompare(image_info->magick,"JNG") != 0)
3912     ThrowReaderException(CorruptImageError,"ImproperImageHeader");
3913
3914   /* Verify JNG signature.  */
3915
3916   count=(size_t) ReadBlob(image,8,(unsigned char *) magic_number);
3917
3918   if (count < 8 || memcmp(magic_number,"\213JNG\r\n\032\n",8) != 0)
3919     ThrowReaderException(CorruptImageError,"ImproperImageHeader");
3920
3921   /* Allocate a MngInfo structure.  */
3922
3923   have_mng_structure=MagickFalse;
3924   mng_info=(MngInfo *) AcquireMagickMemory(sizeof(*mng_info));
3925
3926   if (mng_info == (MngInfo *) NULL)
3927     ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
3928
3929   /* Initialize members of the MngInfo structure.  */
3930
3931   (void) ResetMagickMemory(mng_info,0,sizeof(MngInfo));
3932   have_mng_structure=MagickTrue;
3933
3934   mng_info->image=image;
3935   previous=image;
3936   image=ReadOneJNGImage(mng_info,image_info,exception);
3937   MngInfoFreeStruct(mng_info,&have_mng_structure);
3938
3939   if (image == (Image *) NULL)
3940     {
3941       if (IsImageObject(previous) != MagickFalse)
3942         {
3943           (void) CloseBlob(previous);
3944           (void) DestroyImageList(previous);
3945         }
3946
3947       if (logging != MagickFalse)
3948         (void) LogMagickEvent(CoderEvent,GetMagickModule(),
3949           "exit ReadJNGImage() with error");
3950
3951       return((Image *) NULL);
3952     }
3953   (void) CloseBlob(image);
3954
3955   if (image->columns == 0 || image->rows == 0)
3956     {
3957       if (logging != MagickFalse)
3958         (void) LogMagickEvent(CoderEvent,GetMagickModule(),
3959           "exit ReadJNGImage() with error");
3960
3961       ThrowReaderException(CorruptImageError,"CorruptImage");
3962     }
3963
3964   if (logging != MagickFalse)
3965     (void) LogMagickEvent(CoderEvent,GetMagickModule(),"exit ReadJNGImage()");
3966
3967   return(image);
3968 }
3969 #endif
3970
3971 static Image *ReadMNGImage(const ImageInfo *image_info,ExceptionInfo *exception)
3972 {
3973   char
3974     page_geometry[MaxTextExtent];
3975
3976   Image
3977     *image,
3978     *previous;
3979
3980   MagickBooleanType
3981     logging,
3982     have_mng_structure;
3983
3984   volatile int
3985     first_mng_object,
3986     object_id,
3987     term_chunk_found,
3988     skip_to_iend;
3989
3990   volatile ssize_t
3991     image_count=0;
3992
3993   MagickBooleanType
3994     status;
3995
3996   MagickOffsetType
3997     offset;
3998
3999   MngInfo
4000     *mng_info;
4001
4002   MngBox
4003     default_fb,
4004     fb,
4005     previous_fb;
4006
4007 #if defined(MNG_INSERT_LAYERS)
4008   PixelPacket
4009     mng_background_color;
4010 #endif
4011
4012   register unsigned char
4013     *p;
4014
4015   register ssize_t
4016     i;
4017
4018   size_t
4019     count;
4020
4021   ssize_t
4022     loop_level;
4023
4024   volatile short
4025     skipping_loop;
4026
4027 #if defined(MNG_INSERT_LAYERS)
4028   unsigned int
4029     mandatory_back=0;
4030 #endif
4031
4032   volatile unsigned int
4033 #ifdef MNG_OBJECT_BUFFERS
4034     mng_background_object=0,
4035 #endif
4036     mng_type=0;   /* 0: PNG or JNG; 1: MNG; 2: MNG-LC; 3: MNG-VLC */
4037
4038   size_t
4039     default_frame_timeout,
4040     frame_timeout,
4041 #if defined(MNG_INSERT_LAYERS)
4042     image_height,
4043     image_width,
4044 #endif
4045     length;
4046
4047   /* These delays are all measured in image ticks_per_second,
4048    * not in MNG ticks_per_second
4049    */
4050   volatile size_t
4051     default_frame_delay,
4052     final_delay,
4053     final_image_delay,
4054     frame_delay,
4055 #if defined(MNG_INSERT_LAYERS)
4056     insert_layers,
4057 #endif
4058     mng_iterations=1,
4059     simplicity=0,
4060     subframe_height=0,
4061     subframe_width=0;
4062
4063   previous_fb.top=0;
4064   previous_fb.bottom=0;
4065   previous_fb.left=0;
4066   previous_fb.right=0;
4067   default_fb.top=0;
4068   default_fb.bottom=0;
4069   default_fb.left=0;
4070   default_fb.right=0;
4071
4072   /* Open image file.  */
4073
4074   assert(image_info != (const ImageInfo *) NULL);
4075   assert(image_info->signature == MagickSignature);
4076   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image_info->filename);
4077   assert(exception != (ExceptionInfo *) NULL);
4078   assert(exception->signature == MagickSignature);
4079   logging=LogMagickEvent(CoderEvent,GetMagickModule(),"Enter ReadMNGImage()");
4080   image=AcquireImage(image_info);
4081   mng_info=(MngInfo *) NULL;
4082   status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
4083
4084   if (status == MagickFalse)
4085     return((Image *) NULL);
4086
4087   first_mng_object=MagickFalse;
4088   skipping_loop=(-1);
4089   have_mng_structure=MagickFalse;
4090
4091   /* Allocate a MngInfo structure.  */
4092
4093   mng_info=(MngInfo *) AcquireMagickMemory(sizeof(MngInfo));
4094
4095   if (mng_info == (MngInfo *) NULL)
4096     ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
4097
4098   /* Initialize members of the MngInfo structure.  */
4099
4100   (void) ResetMagickMemory(mng_info,0,sizeof(MngInfo));
4101   mng_info->image=image;
4102   have_mng_structure=MagickTrue;
4103
4104   if (LocaleCompare(image_info->magick,"MNG") == 0)
4105     {
4106       char
4107         magic_number[MaxTextExtent];
4108
4109       /* Verify MNG signature.  */
4110       count=(size_t) ReadBlob(image,8,(unsigned char *) magic_number);
4111       if (memcmp(magic_number,"\212MNG\r\n\032\n",8) != 0)
4112         ThrowReaderException(CorruptImageError,"ImproperImageHeader");
4113
4114       /* Initialize some nonzero members of the MngInfo structure.  */
4115       for (i=0; i < MNG_MAX_OBJECTS; i++)
4116       {
4117         mng_info->object_clip[i].right=(ssize_t) PNG_UINT_31_MAX;
4118         mng_info->object_clip[i].bottom=(ssize_t) PNG_UINT_31_MAX;
4119       }
4120       mng_info->exists[0]=MagickTrue;
4121     }
4122
4123   first_mng_object=MagickTrue;
4124   mng_type=0;
4125 #if defined(MNG_INSERT_LAYERS)
4126   insert_layers=MagickFalse; /* should be False when converting or mogrifying */
4127 #endif
4128   default_frame_delay=0;
4129   default_frame_timeout=0;
4130   frame_delay=0;
4131   final_delay=1;
4132   mng_info->ticks_per_second=1UL*image->ticks_per_second;
4133   object_id=0;
4134   skip_to_iend=MagickFalse;
4135   term_chunk_found=MagickFalse;
4136   mng_info->framing_mode=1;
4137 #if defined(MNG_INSERT_LAYERS)
4138   mandatory_back=MagickFalse;
4139 #endif
4140 #if defined(MNG_INSERT_LAYERS)
4141   mng_background_color=image->background_color;
4142 #endif
4143   default_fb=mng_info->frame;
4144   previous_fb=mng_info->frame;
4145   do
4146   {
4147     char
4148       type[MaxTextExtent];
4149
4150     if (LocaleCompare(image_info->magick,"MNG") == 0)
4151       {
4152         unsigned char
4153           *chunk;
4154
4155         /*
4156           Read a new chunk.
4157         */
4158         type[0]='\0';
4159         (void) ConcatenateMagickString(type,"errr",MaxTextExtent);
4160         length=ReadBlobMSBLong(image);
4161         count=(size_t) ReadBlob(image,4,(unsigned char *) type);
4162
4163         if (logging != MagickFalse)
4164           (void) LogMagickEvent(CoderEvent,GetMagickModule(),
4165            "  Reading MNG chunk type %c%c%c%c, length: %.20g",
4166            type[0],type[1],type[2],type[3],(double) length);
4167
4168         if (length > PNG_UINT_31_MAX)
4169           status=MagickFalse;
4170
4171         if (count == 0)
4172           ThrowReaderException(CorruptImageError,"CorruptImage");
4173
4174         p=NULL;
4175         chunk=(unsigned char *) NULL;
4176
4177         if (length)
4178           {
4179             chunk=(unsigned char *) AcquireQuantumMemory(length,sizeof(*chunk));
4180
4181             if (chunk == (unsigned char *) NULL)
4182               ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
4183
4184             for (i=0; i < (ssize_t) length; i++)
4185               chunk[i]=(unsigned char) ReadBlobByte(image);
4186
4187             p=chunk;
4188           }
4189
4190         (void) ReadBlobMSBLong(image);  /* read crc word */
4191
4192 #if !defined(JNG_SUPPORTED)
4193         if (memcmp(type,mng_JHDR,4) == 0)
4194           {
4195             skip_to_iend=MagickTrue;
4196
4197             if (mng_info->jhdr_warning == 0)
4198               (void) ThrowMagickException(&image->exception,GetMagickModule(),
4199                 CoderError,"JNGCompressNotSupported","`%s'",image->filename);
4200
4201             mng_info->jhdr_warning++;
4202           }
4203 #endif
4204         if (memcmp(type,mng_DHDR,4) == 0)
4205           {
4206             skip_to_iend=MagickTrue;
4207
4208             if (mng_info->dhdr_warning == 0)
4209               (void) ThrowMagickException(&image->exception,GetMagickModule(),
4210                 CoderError,"DeltaPNGNotSupported","`%s'",image->filename);
4211
4212             mng_info->dhdr_warning++;
4213           }
4214         if (memcmp(type,mng_MEND,4) == 0)
4215           break;
4216
4217         if (skip_to_iend)
4218           {
4219             if (memcmp(type,mng_IEND,4) == 0)
4220               skip_to_iend=MagickFalse;
4221
4222             if (length)
4223               chunk=(unsigned char *) RelinquishMagickMemory(chunk);
4224
4225             if (logging != MagickFalse)
4226               (void) LogMagickEvent(CoderEvent,GetMagickModule(),
4227                 "  Skip to IEND.");
4228
4229             continue;
4230           }
4231
4232         if (memcmp(type,mng_MHDR,4) == 0)
4233           {
4234             mng_info->mng_width=(size_t) ((p[0] << 24) | (p[1] << 16) |
4235                 (p[2] << 8) | p[3]);
4236
4237             mng_info->mng_height=(size_t) ((p[4] << 24) | (p[5] << 16) |
4238                 (p[6] << 8) | p[7]);
4239
4240             if (logging != MagickFalse)
4241               {
4242                 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
4243                   "  MNG width: %.20g",(double) mng_info->mng_width);
4244                 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
4245                   "  MNG height: %.20g",(double) mng_info->mng_height);
4246               }
4247
4248             p+=8;
4249             mng_info->ticks_per_second=(size_t) mng_get_long(p);
4250
4251             if (mng_info->ticks_per_second == 0)
4252               default_frame_delay=0;
4253
4254             else
4255               default_frame_delay=1UL*image->ticks_per_second/
4256                 mng_info->ticks_per_second;
4257
4258             frame_delay=default_frame_delay;
4259             simplicity=0;
4260
4261             if (length > 16)
4262               {
4263                 p+=16;
4264                 simplicity=(size_t) mng_get_long(p);
4265               }
4266
4267             mng_type=1;    /* Full MNG */
4268
4269             if ((simplicity != 0) && ((simplicity | 11) == 11))
4270               mng_type=2; /* LC */
4271
4272             if ((simplicity != 0) && ((simplicity | 9) == 9))
4273               mng_type=3; /* VLC */
4274
4275 #if defined(MNG_INSERT_LAYERS)
4276             if (mng_type != 3)
4277               insert_layers=MagickTrue;
4278 #endif
4279             if (GetAuthenticPixelQueue(image) != (PixelPacket *) NULL)
4280               {
4281                 /* Allocate next image structure.  */
4282                 AcquireNextImage(image_info,image);
4283
4284                 if (GetNextImageInList(image) == (Image *) NULL)
4285                   return((Image *) NULL);
4286
4287                 image=SyncNextImageInList(image);
4288                 mng_info->image=image;
4289               }
4290
4291             if ((mng_info->mng_width > 65535L) ||
4292                 (mng_info->mng_height > 65535L))
4293               ThrowReaderException(ImageError,"WidthOrHeightExceedsLimit");
4294
4295             (void) FormatMagickString(page_geometry,MaxTextExtent,
4296               "%.20gx%.20g+0+0",(double) mng_info->mng_width,(double)
4297               mng_info->mng_height);
4298
4299             mng_info->frame.left=0;
4300             mng_info->frame.right=(ssize_t) mng_info->mng_width;
4301             mng_info->frame.top=0;
4302             mng_info->frame.bottom=(ssize_t) mng_info->mng_height;
4303             mng_info->clip=default_fb=previous_fb=mng_info->frame;
4304
4305             for (i=0; i < MNG_MAX_OBJECTS; i++)
4306               mng_info->object_clip[i]=mng_info->frame;
4307
4308             chunk=(unsigned char *) RelinquishMagickMemory(chunk);
4309             continue;
4310           }
4311
4312         if (memcmp(type,mng_TERM,4) == 0)
4313           {
4314             int
4315               repeat=0;
4316
4317
4318             if (length)
4319               repeat=p[0];
4320
4321             if (repeat == 3)
4322               {
4323                 final_delay=(png_uint_32) mng_get_long(&p[2]);
4324                 mng_iterations=(png_uint_32) mng_get_long(&p[6]);
4325
4326                 if (mng_iterations == PNG_UINT_31_MAX)
4327                   mng_iterations=0;
4328
4329                 image->iterations=mng_iterations;
4330                 term_chunk_found=MagickTrue;
4331               }
4332
4333             if (logging != MagickFalse)
4334               {
4335                 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
4336                   "    repeat=%d",repeat);
4337
4338                 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
4339                   "    final_delay=%.20g",(double) final_delay);
4340
4341                 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
4342                   "    image->iterations=%.20g",(double) image->iterations);
4343               }
4344
4345             chunk=(unsigned char *) RelinquishMagickMemory(chunk);
4346             continue;
4347           }
4348         if (memcmp(type,mng_DEFI,4) == 0)
4349           {
4350             if (mng_type == 3)
4351               (void) ThrowMagickException(&image->exception,GetMagickModule(),
4352                 CoderError,"DEFI chunk found in MNG-VLC datastream","`%s'",
4353                 image->filename);
4354
4355             object_id=(p[0] << 8) | p[1];
4356
4357             if (mng_type == 2 && object_id != 0)
4358               (void) ThrowMagickException(&image->exception,GetMagickModule(),
4359                 CoderError,"Nonzero object_id in MNG-LC datastream","`%s'",
4360                 image->filename);
4361
4362             if (object_id > MNG_MAX_OBJECTS)
4363               {
4364                 /*
4365                   Instead ofsuing a warning we should allocate a larger
4366                   MngInfo structure and continue.
4367                 */
4368                 (void) ThrowMagickException(&image->exception,GetMagickModule(),
4369                   CoderError,"object id too large","`%s'",image->filename);
4370                 object_id=MNG_MAX_OBJECTS;
4371               }
4372
4373             if (mng_info->exists[object_id])
4374               if (mng_info->frozen[object_id])
4375                 {
4376                   chunk=(unsigned char *) RelinquishMagickMemory(chunk);
4377                   (void) ThrowMagickException(&image->exception,
4378                     GetMagickModule(),CoderError,
4379                     "DEFI cannot redefine a frozen MNG object","`%s'",
4380                     image->filename);
4381                   continue;
4382                 }
4383
4384             mng_info->exists[object_id]=MagickTrue;
4385
4386             if (length > 2)
4387               mng_info->invisible[object_id]=p[2];
4388
4389             /*
4390               Extract object offset info.
4391             */
4392             if (length > 11)
4393               {
4394                 mng_info->x_off[object_id]=(ssize_t) ((p[4] << 24) |
4395                     (p[5] << 16) | (p[6] << 8) | p[7]);
4396
4397                 mng_info->y_off[object_id]=(ssize_t) ((p[8] << 24) |
4398                     (p[9] << 16) | (p[10] << 8) | p[11]);
4399
4400                 if (logging != MagickFalse)
4401                   {
4402                     (void) LogMagickEvent(CoderEvent,GetMagickModule(),
4403                       "  x_off[%d]: %.20g",object_id,(double)
4404                       mng_info->x_off[object_id]);
4405
4406                     (void) LogMagickEvent(CoderEvent,GetMagickModule(),
4407                       "  y_off[%d]: %.20g",object_id,(double)
4408                       mng_info->y_off[object_id]);
4409                   }
4410               }
4411
4412             /*
4413               Extract object clipping info.
4414             */
4415             if (length > 27)
4416               mng_info->object_clip[object_id]=mng_read_box(mng_info->frame,0,
4417                 &p[12]);
4418
4419             chunk=(unsigned char *) RelinquishMagickMemory(chunk);
4420             continue;
4421           }
4422         if (memcmp(type,mng_bKGD,4) == 0)
4423           {
4424             mng_info->have_global_bkgd=MagickFalse;
4425
4426             if (length > 5)
4427               {
4428                 mng_info->mng_global_bkgd.red=
4429                   ScaleShortToQuantum((unsigned short) ((p[0] << 8) | p[1]));
4430
4431                 mng_info->mng_global_bkgd.green=
4432                   ScaleShortToQuantum((unsigned short) ((p[2] << 8) | p[3]));
4433
4434                 mng_info->mng_global_bkgd.blue=
4435                   ScaleShortToQuantum((unsigned short) ((p[4] << 8) | p[5]));
4436
4437                 mng_info->have_global_bkgd=MagickTrue;
4438               }
4439
4440             chunk=(unsigned char *) RelinquishMagickMemory(chunk);
4441             continue;
4442           }
4443         if (memcmp(type,mng_BACK,4) == 0)
4444           {
4445 #if defined(MNG_INSERT_LAYERS)
4446             if (length > 6)
4447               mandatory_back=p[6];
4448
4449             else
4450               mandatory_back=0;
4451
4452             if (mandatory_back && length > 5)
4453               {
4454                 mng_background_color.red=
4455                     ScaleShortToQuantum((unsigned short) ((p[0] << 8) | p[1]));
4456
4457                 mng_background_color.green=
4458                     ScaleShortToQuantum((unsigned short) ((p[2] << 8) | p[3]));
4459
4460                 mng_background_color.blue=
4461                     ScaleShortToQuantum((unsigned short) ((p[4] << 8) | p[5]));
4462
4463                 mng_background_color.opacity=OpaqueOpacity;
4464               }
4465
4466 #ifdef MNG_OBJECT_BUFFERS
4467             if (length > 8)
4468               mng_background_object=(p[7] << 8) | p[8];
4469 #endif
4470 #endif
4471             chunk=(unsigned char *) RelinquishMagickMemory(chunk);
4472             continue;
4473           }
4474
4475         if (memcmp(type,mng_PLTE,4) == 0)
4476           {
4477             /* Read global PLTE.  */
4478
4479             if (length && (length < 769))
4480               {
4481                 if (mng_info->global_plte == (png_colorp) NULL)
4482                   mng_info->global_plte=(png_colorp) AcquireQuantumMemory(256,
4483                     sizeof(*mng_info->global_plte));
4484
4485                 for (i=0; i < (ssize_t) (length/3); i++)
4486                 {
4487                   mng_info->global_plte[i].red=p[3*i];
4488                   mng_info->global_plte[i].green=p[3*i+1];
4489                   mng_info->global_plte[i].blue=p[3*i+2];
4490                 }
4491
4492                 mng_info->global_plte_length=(unsigned int) (length/3);
4493               }
4494 #ifdef MNG_LOOSE
4495             for ( ; i < 256; i++)
4496             {
4497               mng_info->global_plte[i].red=i;
4498               mng_info->global_plte[i].green=i;
4499               mng_info->global_plte[i].blue=i;
4500             }
4501
4502             if (length)
4503               mng_info->global_plte_length=256;
4504 #endif
4505             else
4506               mng_info->global_plte_length=0;
4507
4508             chunk=(unsigned char *) RelinquishMagickMemory(chunk);
4509             continue;
4510           }
4511
4512         if (memcmp(type,mng_tRNS,4) == 0)
4513           {
4514             /* read global tRNS */
4515
4516             if (length < 257)
4517               for (i=0; i < (ssize_t) length; i++)
4518                 mng_info->global_trns[i]=p[i];
4519
4520 #ifdef MNG_LOOSE
4521             for ( ; i < 256; i++)
4522               mng_info->global_trns[i]=255;
4523 #endif
4524             mng_info->global_trns_length=(unsigned int) length;
4525             chunk=(unsigned char *) RelinquishMagickMemory(chunk);
4526             continue;
4527           }
4528         if (memcmp(type,mng_gAMA,4) == 0)
4529           {
4530             if (length == 4)
4531               {
4532                 ssize_t
4533                   igamma;
4534
4535                 igamma=mng_get_long(p);
4536                 mng_info->global_gamma=((float) igamma)*0.00001;
4537                 mng_info->have_global_gama=MagickTrue;
4538               }
4539
4540             else
4541               mng_info->have_global_gama=MagickFalse;
4542
4543             chunk=(unsigned char *) RelinquishMagickMemory(chunk);
4544             continue;
4545           }
4546
4547         if (memcmp(type,mng_cHRM,4) == 0)
4548           {
4549             /* Read global cHRM */
4550
4551             if (length == 32)
4552               {
4553                 mng_info->global_chrm.white_point.x=0.00001*mng_get_long(p);
4554                 mng_info->global_chrm.white_point.y=0.00001*mng_get_long(&p[4]);
4555                 mng_info->global_chrm.red_primary.x=0.00001*mng_get_long(&p[8]);
4556                 mng_info->global_chrm.red_primary.y=0.00001*
4557                   mng_get_long(&p[12]);
4558                 mng_info->global_chrm.green_primary.x=0.00001*
4559                   mng_get_long(&p[16]);
4560                 mng_info->global_chrm.green_primary.y=0.00001*
4561                   mng_get_long(&p[20]);
4562                 mng_info->global_chrm.blue_primary.x=0.00001*
4563                   mng_get_long(&p[24]);
4564                 mng_info->global_chrm.blue_primary.y=0.00001*
4565                   mng_get_long(&p[28]);
4566                 mng_info->have_global_chrm=MagickTrue;
4567               }
4568             else
4569               mng_info->have_global_chrm=MagickFalse;
4570
4571             chunk=(unsigned char *) RelinquishMagickMemory(chunk);
4572             continue;
4573           }
4574
4575         if (memcmp(type,mng_sRGB,4) == 0)
4576           {
4577             /*
4578               Read global sRGB.
4579             */
4580             if (length)
4581               {
4582                 mng_info->global_srgb_intent=
4583                   Magick_RenderingIntent_from_PNG_RenderingIntent(p[0]);
4584                 mng_info->have_global_srgb=MagickTrue;
4585               }
4586             else
4587               mng_info->have_global_srgb=MagickFalse;
4588
4589             chunk=(unsigned char *) RelinquishMagickMemory(chunk);
4590             continue;
4591           }
4592
4593         if (memcmp(type,mng_iCCP,4) == 0)
4594           {
4595             /* To do: */
4596
4597             /*
4598               Read global iCCP.
4599             */
4600             if (length)
4601               chunk=(unsigned char *) RelinquishMagickMemory(chunk);
4602
4603             continue;
4604           }
4605
4606         if (memcmp(type,mng_FRAM,4) == 0)
4607           {
4608             if (mng_type == 3)
4609               (void) ThrowMagickException(&image->exception,GetMagickModule(),
4610                 CoderError,"FRAM chunk found in MNG-VLC datastream","`%s'",
4611                 image->filename);
4612
4613             if ((mng_info->framing_mode == 2) || (mng_info->framing_mode == 4))
4614               image->delay=frame_delay;
4615
4616             frame_delay=default_frame_delay;
4617             frame_timeout=default_frame_timeout;
4618             fb=default_fb;
4619
4620             if (length)
4621               if (p[0])
4622                 mng_info->framing_mode=p[0];
4623
4624             if (logging != MagickFalse)
4625               (void) LogMagickEvent(CoderEvent,GetMagickModule(),
4626                 "    Framing_mode=%d",mng_info->framing_mode);
4627
4628             if (length > 6)
4629               {
4630                 /* Note the delay and frame clipping boundaries.  */
4631
4632                 p++; /* framing mode */
4633
4634                 while (*p && ((p-chunk) < (ssize_t) length))
4635                   p++;  /* frame name */
4636
4637                 p++;  /* frame name terminator */
4638
4639                 if ((p-chunk) < (ssize_t) (length-4))
4640                   {
4641                     int
4642                       change_delay,
4643                       change_timeout,
4644                       change_clipping;
4645
4646                     change_delay=(*p++);
4647                     change_timeout=(*p++);
4648                     change_clipping=(*p++);
4649                     p++; /* change_sync */
4650
4651                     if (change_delay)
4652                       {
4653                         frame_delay=1UL*image->ticks_per_second*
4654                           mng_get_long(p);
4655
4656                         if (mng_info->ticks_per_second != 0)
4657                           frame_delay/=mng_info->ticks_per_second;
4658
4659                         else
4660                           frame_delay=PNG_UINT_31_MAX;
4661
4662                         if (change_delay == 2)
4663                           default_frame_delay=frame_delay;
4664
4665                         p+=4;
4666
4667                         if (logging != MagickFalse)
4668                           (void) LogMagickEvent(CoderEvent,GetMagickModule(),
4669                             "    Framing_delay=%.20g",(double) frame_delay);
4670                       }
4671
4672                     if (change_timeout)
4673                       {
4674                         frame_timeout=1UL*image->ticks_per_second*
4675                           mng_get_long(p);
4676
4677                         if (mng_info->ticks_per_second != 0)
4678                           frame_timeout/=mng_info->ticks_per_second;
4679
4680                         else
4681                           frame_timeout=PNG_UINT_31_MAX;
4682
4683                         if (change_delay == 2)
4684                           default_frame_timeout=frame_timeout;
4685
4686                         p+=4;
4687
4688                         if (logging != MagickFalse)
4689                           (void) LogMagickEvent(CoderEvent,GetMagickModule(),
4690                             "    Framing_timeout=%.20g",(double) frame_timeout);
4691                       }
4692
4693                     if (change_clipping)
4694                       {
4695                         fb=mng_read_box(previous_fb,(char) p[0],&p[1]);
4696                         p+=17;
4697                         previous_fb=fb;
4698
4699                         if (logging != MagickFalse)
4700                           (void) LogMagickEvent(CoderEvent,GetMagickModule(),
4701                             "    Frame_clip: L=%.20g R=%.20g T=%.20g B=%.20g",
4702                             (double) fb.left,(double) fb.right,(double) fb.top,
4703                             (double) fb.bottom);
4704
4705                         if (change_clipping == 2)
4706                           default_fb=fb;
4707                       }
4708                   }
4709               }
4710             mng_info->clip=fb;
4711             mng_info->clip=mng_minimum_box(fb,mng_info->frame);
4712
4713             subframe_width=(size_t) (mng_info->clip.right
4714                -mng_info->clip.left);
4715
4716             subframe_height=(size_t) (mng_info->clip.bottom
4717                -mng_info->clip.top);
4718             /*
4719               Insert a background layer behind the frame if framing_mode is 4.
4720             */
4721 #if defined(MNG_INSERT_LAYERS)
4722             if (logging != MagickFalse)
4723               (void) LogMagickEvent(CoderEvent,GetMagickModule(),
4724                 "   subframe_width=%.20g, subframe_height=%.20g",(double)
4725                 subframe_width,(double) subframe_height);
4726
4727             if (insert_layers && (mng_info->framing_mode == 4) &&
4728                 (subframe_width) && (subframe_height))
4729               {
4730                 /* Allocate next image structure.  */
4731                 if (GetAuthenticPixelQueue(image) != (PixelPacket *) NULL)
4732                   {
4733                     AcquireNextImage(image_info,image);
4734
4735                     if (GetNextImageInList(image) == (Image *) NULL)
4736                       {
4737                         image=DestroyImageList(image);
4738                         MngInfoFreeStruct(mng_info,&have_mng_structure);
4739                         return((Image *) NULL);
4740                       }
4741
4742                     image=SyncNextImageInList(image);
4743                   }
4744
4745                 mng_info->image=image;
4746
4747                 if (term_chunk_found)
4748                   {
4749                     image->start_loop=MagickTrue;
4750                     image->iterations=mng_iterations;
4751                     term_chunk_found=MagickFalse;
4752                   }
4753
4754                 else
4755                     image->start_loop=MagickFalse;
4756
4757                 image->columns=subframe_width;
4758                 image->rows=subframe_height;
4759                 image->page.width=subframe_width;
4760                 image->page.height=subframe_height;
4761                 image->page.x=mng_info->clip.left;
4762                 image->page.y=mng_info->clip.top;
4763                 image->background_color=mng_background_color;
4764                 image->matte=MagickFalse;
4765                 image->delay=0;
4766                 (void) SetImageBackgroundColor(image);
4767
4768                 if (logging != MagickFalse)
4769                   (void) LogMagickEvent(CoderEvent,GetMagickModule(),
4770                     "  Insert backgd layer, L=%.20g, R=%.20g T=%.20g, B=%.20g",
4771                     (double) mng_info->clip.left,(double) mng_info->clip.right,
4772                     (double) mng_info->clip.top,(double) mng_info->clip.bottom);
4773               }
4774 #endif
4775             chunk=(unsigned char *) RelinquishMagickMemory(chunk);
4776             continue;
4777           }
4778         if (memcmp(type,mng_CLIP,4) == 0)
4779           {
4780             unsigned int
4781               first_object,
4782               last_object;
4783
4784             /*
4785               Read CLIP.
4786             */
4787             first_object=(p[0] << 8) | p[1];
4788             last_object=(p[2] << 8) | p[3];
4789
4790             for (i=(int) first_object; i <= (int) last_object; i++)
4791             {
4792               if (mng_info->exists[i] && !mng_info->frozen[i])
4793                 {
4794                   MngBox
4795                     box;
4796
4797                   box=mng_info->object_clip[i];
4798                   mng_info->object_clip[i]=mng_read_box(box,(char) p[4],&p[5]);
4799                 }
4800             }
4801
4802             chunk=(unsigned char *) RelinquishMagickMemory(chunk);
4803             continue;
4804           }
4805         if (memcmp(type,mng_SAVE,4) == 0)
4806           {
4807             for (i=1; i < MNG_MAX_OBJECTS; i++)
4808               if (mng_info->exists[i])
4809                 {
4810                  mng_info->frozen[i]=MagickTrue;
4811 #ifdef MNG_OBJECT_BUFFERS
4812                  if (mng_info->ob[i] != (MngBuffer *) NULL)
4813                     mng_info->ob[i]->frozen=MagickTrue;
4814 #endif
4815                 }
4816
4817             if (length)
4818               chunk=(unsigned char *) RelinquishMagickMemory(chunk);
4819
4820             continue;
4821           }
4822
4823         if ((memcmp(type,mng_DISC,4) == 0) || (memcmp(type,mng_SEEK,4) == 0))
4824           {
4825             /* Read DISC or SEEK.  */
4826
4827             if ((length == 0) || !memcmp(type,mng_SEEK,4))
4828               {
4829                 for (i=1; i < MNG_MAX_OBJECTS; i++)
4830                   MngInfoDiscardObject(mng_info,i);
4831               }
4832
4833             else
4834               {
4835                 register ssize_t
4836                   j;
4837
4838                 for (j=0; j < (ssize_t) length; j+=2)
4839                 {
4840                   i=p[j] << 8 | p[j+1];
4841                   MngInfoDiscardObject(mng_info,i);
4842                 }
4843               }
4844
4845             if (length)
4846               chunk=(unsigned char *) RelinquishMagickMemory(chunk);
4847
4848             continue;
4849           }
4850
4851         if (memcmp(type,mng_MOVE,4) == 0)
4852           {
4853             size_t
4854               first_object,
4855               last_object;
4856
4857             /* read MOVE */
4858
4859             first_object=(p[0] << 8) | p[1];
4860             last_object=(p[2] << 8) | p[3];
4861             for (i=(ssize_t) first_object; i <= (ssize_t) last_object; i++)
4862             {
4863               if (mng_info->exists[i] && !mng_info->frozen[i])
4864                 {
4865                   MngPair
4866                     new_pair;
4867
4868                   MngPair
4869                     old_pair;
4870
4871                   old_pair.a=mng_info->x_off[i];
4872                   old_pair.b=mng_info->y_off[i];
4873                   new_pair=mng_read_pair(old_pair,(int) p[4],&p[5]);
4874                   mng_info->x_off[i]=new_pair.a;
4875                   mng_info->y_off[i]=new_pair.b;
4876                 }
4877             }
4878
4879             chunk=(unsigned char *) RelinquishMagickMemory(chunk);
4880             continue;
4881           }
4882
4883         if (memcmp(type,mng_LOOP,4) == 0)
4884           {
4885             ssize_t loop_iters=1;
4886             loop_level=chunk[0];
4887             mng_info->loop_active[loop_level]=1;  /* mark loop active */
4888
4889             /* Record starting point.  */
4890             loop_iters=mng_get_long(&chunk[1]);
4891
4892             if (logging != MagickFalse)
4893               (void) LogMagickEvent(CoderEvent,GetMagickModule(),
4894                 "  LOOP level %.20g has %.20g iterations ",(double) loop_level,
4895                 (double) loop_iters);
4896
4897             if (loop_iters == 0)
4898               skipping_loop=loop_level;
4899
4900             else
4901               {
4902                 mng_info->loop_jump[loop_level]=TellBlob(image);
4903                 mng_info->loop_count[loop_level]=loop_iters;
4904               }
4905
4906             mng_info->loop_iteration[loop_level]=0;
4907             chunk=(unsigned char *) RelinquishMagickMemory(chunk);
4908             continue;
4909           }
4910
4911         if (memcmp(type,mng_ENDL,4) == 0)
4912           {
4913             loop_level=chunk[0];
4914
4915             if (skipping_loop > 0)
4916               {
4917                 if (skipping_loop == loop_level)
4918                   {
4919                     /*
4920                       Found end of zero-iteration loop.
4921                     */
4922                     skipping_loop=(-1);
4923                     mng_info->loop_active[loop_level]=0;
4924                   }
4925               }
4926
4927             else
4928               {
4929                 if (mng_info->loop_active[loop_level] == 1)
4930                   {
4931                     mng_info->loop_count[loop_level]--;
4932                     mng_info->loop_iteration[loop_level]++;
4933
4934                     if (logging != MagickFalse)
4935                       (void) LogMagickEvent(CoderEvent,GetMagickModule(),
4936                         "  ENDL: LOOP level %.20g has %.20g remaining iters ",
4937                         (double) loop_level,(double)
4938                         mng_info->loop_count[loop_level]);
4939
4940                     if (mng_info->loop_count[loop_level] != 0)
4941                       {
4942                         offset=SeekBlob(image,mng_info->loop_jump[loop_level],
4943                           SEEK_SET);
4944
4945                         if (offset < 0)
4946                           ThrowReaderException(CorruptImageError,
4947                             "ImproperImageHeader");
4948                       }
4949
4950                     else
4951                       {
4952                         short
4953                           last_level;
4954
4955                         /*
4956                           Finished loop.
4957                         */
4958                         mng_info->loop_active[loop_level]=0;
4959                         last_level=(-1);
4960                         for (i=0; i < loop_level; i++)
4961                           if (mng_info->loop_active[i] == 1)
4962                             last_level=(short) i;
4963                         loop_level=last_level;
4964                       }
4965                   }
4966               }
4967
4968             chunk=(unsigned char *) RelinquishMagickMemory(chunk);
4969             continue;
4970           }
4971
4972         if (memcmp(type,mng_CLON,4) == 0)
4973           {
4974             if (mng_info->clon_warning == 0)
4975               (void) ThrowMagickException(&image->exception,GetMagickModule(),
4976                 CoderError,"CLON is not implemented yet","`%s'",
4977                 image->filename);
4978
4979             mng_info->clon_warning++;
4980           }
4981
4982         if (memcmp(type,mng_MAGN,4) == 0)
4983           {
4984             png_uint_16
4985               magn_first,
4986               magn_last,
4987               magn_mb,
4988               magn_ml,
4989               magn_mr,
4990               magn_mt,
4991               magn_mx,
4992               magn_my,
4993               magn_methx,
4994               magn_methy;
4995
4996             if (length > 1)
4997               magn_first=(p[0] << 8) | p[1];
4998
4999             else
5000               magn_first=0;
5001
5002             if (length > 3)
5003               magn_last=(p[2] << 8) | p[3];
5004
5005             else
5006               magn_last=magn_first;
5007 #ifndef MNG_OBJECT_BUFFERS
5008             if (magn_first || magn_last)
5009               if (mng_info->magn_warning == 0)
5010                 {
5011                   (void) ThrowMagickException(&image->exception,
5012                      GetMagickModule(),CoderError,
5013                      "MAGN is not implemented yet for nonzero objects",
5014                      "`%s'",image->filename);
5015
5016                    mng_info->magn_warning++;
5017                 }
5018 #endif
5019             if (length > 4)
5020               magn_methx=p[4];
5021
5022             else
5023               magn_methx=0;
5024
5025             if (length > 6)
5026               magn_mx=(p[5] << 8) | p[6];
5027
5028             else
5029               magn_mx=1;
5030
5031             if (magn_mx == 0)
5032               magn_mx=1;
5033
5034             if (length > 8)
5035               magn_my=(p[7] << 8) | p[8];
5036
5037             else
5038               magn_my=magn_mx;
5039
5040             if (magn_my == 0)
5041               magn_my=1;
5042
5043             if (length > 10)
5044               magn_ml=(p[9] << 8) | p[10];
5045
5046             else
5047               magn_ml=magn_mx;
5048
5049             if (magn_ml == 0)
5050               magn_ml=1;
5051
5052             if (length > 12)
5053               magn_mr=(p[11] << 8) | p[12];
5054
5055             else
5056               magn_mr=magn_mx;
5057
5058             if (magn_mr == 0)
5059               magn_mr=1;
5060
5061             if (length > 14)
5062               magn_mt=(p[13] << 8) | p[14];
5063
5064             else
5065               magn_mt=magn_my;
5066
5067             if (magn_mt == 0)
5068               magn_mt=1;
5069
5070             if (length > 16)
5071               magn_mb=(p[15] << 8) | p[16];
5072
5073             else
5074               magn_mb=magn_my;
5075
5076             if (magn_mb == 0)
5077               magn_mb=1;
5078
5079             if (length > 17)
5080               magn_methy=p[17];
5081
5082             else
5083               magn_methy=magn_methx;
5084
5085
5086             if (magn_methx > 5 || magn_methy > 5)
5087               if (mng_info->magn_warning == 0)
5088                 {
5089                   (void) ThrowMagickException(&image->exception,
5090                      GetMagickModule(),CoderError,
5091                      "Unknown MAGN method in MNG datastream","`%s'",
5092                      image->filename);
5093
5094                    mng_info->magn_warning++;
5095                 }
5096 #ifdef MNG_OBJECT_BUFFERS
5097           /* Magnify existing objects in the range magn_first to magn_last */
5098 #endif
5099             if (magn_first == 0 || magn_last == 0)
5100               {
5101                 /* Save the magnification factors for object 0 */
5102                 mng_info->magn_mb=magn_mb;
5103                 mng_info->magn_ml=magn_ml;
5104                 mng_info->magn_mr=magn_mr;
5105                 mng_info->magn_mt=magn_mt;
5106                 mng_info->magn_mx=magn_mx;
5107                 mng_info->magn_my=magn_my;
5108                 mng_info->magn_methx=magn_methx;
5109                 mng_info->magn_methy=magn_methy;
5110               }
5111           }
5112
5113         if (memcmp(type,mng_PAST,4) == 0)
5114           {
5115             if (mng_info->past_warning == 0)
5116               (void) ThrowMagickException(&image->exception,GetMagickModule(),
5117                 CoderError,"PAST is not implemented yet","`%s'",
5118                 image->filename);
5119
5120             mng_info->past_warning++;
5121           }
5122
5123         if (memcmp(type,mng_SHOW,4) == 0)
5124           {
5125             if (mng_info->show_warning == 0)
5126               (void) ThrowMagickException(&image->exception,GetMagickModule(),
5127                 CoderError,"SHOW is not implemented yet","`%s'",
5128                 image->filename);
5129
5130             mng_info->show_warning++;
5131           }
5132
5133         if (memcmp(type,mng_sBIT,4) == 0)
5134           {
5135             if (length < 4)
5136               mng_info->have_global_sbit=MagickFalse;
5137
5138             else
5139               {
5140                 mng_info->global_sbit.gray=p[0];
5141                 mng_info->global_sbit.red=p[0];
5142                 mng_info->global_sbit.green=p[1];
5143                 mng_info->global_sbit.blue=p[2];
5144                 mng_info->global_sbit.alpha=p[3];
5145                 mng_info->have_global_sbit=MagickTrue;
5146              }
5147           }
5148         if (memcmp(type,mng_pHYs,4) == 0)
5149           {
5150             if (length > 8)
5151               {
5152                 mng_info->global_x_pixels_per_unit=
5153                     (size_t) mng_get_long(p);
5154                 mng_info->global_y_pixels_per_unit=
5155                     (size_t) mng_get_long(&p[4]);
5156                 mng_info->global_phys_unit_type=p[8];
5157                 mng_info->have_global_phys=MagickTrue;
5158               }
5159
5160             else
5161               mng_info->have_global_phys=MagickFalse;
5162           }
5163         if (memcmp(type,mng_pHYg,4) == 0)
5164           {
5165             if (mng_info->phyg_warning == 0)
5166               (void) ThrowMagickException(&image->exception,GetMagickModule(),
5167                 CoderError,"pHYg is not implemented.","`%s'",image->filename);
5168
5169             mng_info->phyg_warning++;
5170           }
5171         if (memcmp(type,mng_BASI,4) == 0)
5172           {
5173             skip_to_iend=MagickTrue;
5174
5175             if (mng_info->basi_warning == 0)
5176               (void) ThrowMagickException(&image->exception,GetMagickModule(),
5177                 CoderError,"BASI is not implemented yet","`%s'",
5178                 image->filename);
5179
5180             mng_info->basi_warning++;
5181 #ifdef MNG_BASI_SUPPORTED
5182             basi_width=(size_t) ((p[0] << 24) | (p[1] << 16) |
5183                (p[2] << 8) | p[3]);
5184             basi_height=(size_t) ((p[4] << 24) | (p[5] << 16) |
5185                (p[6] << 8) | p[7]);
5186             basi_color_type=p[8];
5187             basi_compression_method=p[9];
5188             basi_filter_type=p[10];
5189             basi_interlace_method=p[11];
5190             if (length > 11)
5191               basi_red=(p[12] << 8) & p[13];
5192
5193             else
5194               basi_red=0;
5195
5196             if (length > 13)
5197               basi_green=(p[14] << 8) & p[15];
5198
5199             else
5200               basi_green=0;
5201
5202             if (length > 15)
5203               basi_blue=(p[16] << 8) & p[17];
5204
5205             else
5206               basi_blue=0;
5207
5208             if (length > 17)
5209               basi_alpha=(p[18] << 8) & p[19];
5210
5211             else
5212               {
5213                 if (basi_sample_depth == 16)
5214                   basi_alpha=65535L;
5215                 else
5216                   basi_alpha=255;
5217               }
5218
5219             if (length > 19)
5220               basi_viewable=p[20];
5221
5222             else
5223               basi_viewable=0;
5224
5225 #endif
5226             chunk=(unsigned char *) RelinquishMagickMemory(chunk);
5227             continue;
5228           }
5229
5230         if (memcmp(type,mng_IHDR,4)
5231 #if defined(JNG_SUPPORTED)
5232             && memcmp(type,mng_JHDR,4)
5233 #endif
5234             )
5235           {
5236             /* Not an IHDR or JHDR chunk */
5237             if (length)
5238               chunk=(unsigned char *) RelinquishMagickMemory(chunk);
5239
5240             continue;
5241           }
5242 /* Process IHDR */
5243         if (logging != MagickFalse)
5244           (void) LogMagickEvent(CoderEvent,GetMagickModule(),
5245             "  Processing %c%c%c%c chunk",type[0],type[1],type[2],type[3]);
5246
5247         mng_info->exists[object_id]=MagickTrue;
5248         mng_info->viewable[object_id]=MagickTrue;
5249
5250         if (mng_info->invisible[object_id])
5251           {
5252             if (logging != MagickFalse)
5253               (void) LogMagickEvent(CoderEvent,GetMagickModule(),
5254                 "  Skipping invisible object");
5255
5256             skip_to_iend=MagickTrue;
5257             chunk=(unsigned char *) RelinquishMagickMemory(chunk);
5258             continue;
5259           }
5260 #if defined(MNG_INSERT_LAYERS)
5261         if (length < 8)
5262           ThrowReaderException(CorruptImageError,"ImproperImageHeader");
5263
5264         image_width=(size_t) mng_get_long(p);
5265         image_height=(size_t) mng_get_long(&p[4]);
5266 #endif
5267         chunk=(unsigned char *) RelinquishMagickMemory(chunk);
5268
5269         /*
5270           Insert a transparent background layer behind the entire animation
5271           if it is not full screen.
5272         */
5273 #if defined(MNG_INSERT_LAYERS)
5274         if (insert_layers && mng_type && first_mng_object)
5275           {
5276             if ((mng_info->clip.left > 0) || (mng_info->clip.top > 0) ||
5277                 (image_width < mng_info->mng_width) ||
5278                 (mng_info->clip.right < (ssize_t) mng_info->mng_width) ||
5279                 (image_height < mng_info->mng_height) ||
5280                 (mng_info->clip.bottom < (ssize_t) mng_info->mng_height))
5281               {
5282                 if (GetAuthenticPixelQueue(image) != (PixelPacket *) NULL)
5283                   {
5284                     /*
5285                       Allocate next image structure.
5286                     */
5287                     AcquireNextImage(image_info,image);
5288
5289                     if (GetNextImageInList(image) == (Image *) NULL)
5290                       {
5291                         image=DestroyImageList(image);
5292                         MngInfoFreeStruct(mng_info,&have_mng_structure);
5293                         return((Image *) NULL);
5294                       }
5295
5296                     image=SyncNextImageInList(image);
5297                   }
5298                 mng_info->image=image;
5299
5300                 if (term_chunk_found)
5301                   {
5302                     image->start_loop=MagickTrue;
5303                     image->iterations=mng_iterations;
5304                     term_chunk_found=MagickFalse;
5305                   }
5306
5307                 else
5308                     image->start_loop=MagickFalse;
5309
5310                 /* Make a background rectangle.  */
5311
5312                 image->delay=0;
5313                 image->columns=mng_info->mng_width;
5314                 image->rows=mng_info->mng_height;
5315                 image->page.width=mng_info->mng_width;
5316                 image->page.height=mng_info->mng_height;
5317                 image->page.x=0;
5318                 image->page.y=0;
5319                 image->background_color=mng_background_color;
5320                 (void) SetImageBackgroundColor(image);
5321                 if (logging != MagickFalse)
5322                   (void) LogMagickEvent(CoderEvent,GetMagickModule(),
5323                     "  Inserted transparent background layer, W=%.20g, H=%.20g",
5324                     (double) mng_info->mng_width,(double) mng_info->mng_height);
5325               }
5326           }
5327         /*
5328           Insert a background layer behind the upcoming image if
5329           framing_mode is 3, and we haven't already inserted one.
5330         */
5331         if (insert_layers && (mng_info->framing_mode == 3) &&
5332                 (subframe_width) && (subframe_height) && (simplicity == 0 ||
5333                 (simplicity & 0x08)))
5334           {
5335             if (GetAuthenticPixelQueue(image) != (PixelPacket *) NULL)
5336             {
5337               /*
5338                 Allocate next image structure.
5339               */
5340               AcquireNextImage(image_info,image);
5341
5342               if (GetNextImageInList(image) == (Image *) NULL)
5343                 {
5344                   image=DestroyImageList(image);
5345                   MngInfoFreeStruct(mng_info,&have_mng_structure);
5346                   return((Image *) NULL);
5347                 }
5348
5349               image=SyncNextImageInList(image);
5350             }
5351
5352             mng_info->image=image;
5353
5354             if (term_chunk_found)
5355               {
5356                 image->start_loop=MagickTrue;
5357                 image->iterations=mng_iterations;
5358                 term_chunk_found=MagickFalse;
5359               }
5360
5361             else
5362                 image->start_loop=MagickFalse;
5363
5364             image->delay=0;
5365             image->columns=subframe_width;
5366             image->rows=subframe_height;
5367             image->page.width=subframe_width;
5368             image->page.height=subframe_height;
5369             image->page.x=mng_info->clip.left;
5370             image->page.y=mng_info->clip.top;
5371             image->background_color=mng_background_color;
5372             image->matte=MagickFalse;
5373             (void) SetImageBackgroundColor(image);
5374
5375             if (logging != MagickFalse)
5376               (void) LogMagickEvent(CoderEvent,GetMagickModule(),
5377                 "  Insert background layer, L=%.20g, R=%.20g T=%.20g, B=%.20g",
5378                 (double) mng_info->clip.left,(double) mng_info->clip.right,
5379                 (double) mng_info->clip.top,(double) mng_info->clip.bottom);
5380           }
5381 #endif /* MNG_INSERT_LAYERS */
5382         first_mng_object=MagickFalse;
5383
5384         if (GetAuthenticPixelQueue(image) != (PixelPacket *) NULL)
5385           {
5386             /*
5387               Allocate next image structure.
5388             */
5389             AcquireNextImage(image_info,image);
5390
5391             if (GetNextImageInList(image) == (Image *) NULL)
5392               {
5393                 image=DestroyImageList(image);
5394                 MngInfoFreeStruct(mng_info,&have_mng_structure);
5395                 return((Image *) NULL);
5396               }
5397
5398             image=SyncNextImageInList(image);
5399           }
5400         mng_info->image=image;
5401         status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
5402           GetBlobSize(image));
5403
5404         if (status == MagickFalse)
5405           break;
5406
5407         if (term_chunk_found)
5408           {
5409             image->start_loop=MagickTrue;
5410             term_chunk_found=MagickFalse;
5411           }
5412
5413         else
5414             image->start_loop=MagickFalse;
5415
5416         if (mng_info->framing_mode == 1 || mng_info->framing_mode == 3)
5417           {
5418             image->delay=frame_delay;
5419             frame_delay=default_frame_delay;
5420           }
5421
5422         else
5423           image->delay=0;
5424
5425         image->page.width=mng_info->mng_width;
5426         image->page.height=mng_info->mng_height;
5427         image->page.x=mng_info->x_off[object_id];
5428         image->page.y=mng_info->y_off[object_id];
5429         image->iterations=mng_iterations;
5430
5431         /*
5432           Seek back to the beginning of the IHDR or JHDR chunk's length field.
5433         */
5434
5435         if (logging != MagickFalse)
5436           (void) LogMagickEvent(CoderEvent,GetMagickModule(),
5437             "  Seeking back to beginning of %c%c%c%c chunk",type[0],type[1],
5438             type[2],type[3]);
5439
5440         offset=SeekBlob(image,-((ssize_t) length+12),SEEK_CUR);
5441
5442         if (offset < 0)
5443           ThrowReaderException(CorruptImageError,"ImproperImageHeader");
5444       }
5445
5446     previous=image;
5447     mng_info->image=image;
5448     mng_info->mng_type=mng_type;
5449     mng_info->object_id=object_id;
5450
5451     if (memcmp(type,mng_IHDR,4) == 0)
5452       image=ReadOnePNGImage(mng_info,image_info,exception);
5453
5454 #if defined(JNG_SUPPORTED)
5455     else
5456       image=ReadOneJNGImage(mng_info,image_info,exception);
5457 #endif
5458
5459     if (image == (Image *) NULL)
5460       {
5461         if (IsImageObject(previous) != MagickFalse)
5462           {
5463             (void) DestroyImageList(previous);
5464             (void) CloseBlob(previous);
5465           }
5466
5467         MngInfoFreeStruct(mng_info,&have_mng_structure);
5468         return((Image *) NULL);
5469       }
5470
5471     if (image->columns == 0 || image->rows == 0)
5472       {
5473         (void) CloseBlob(image);
5474         image=DestroyImageList(image);
5475         MngInfoFreeStruct(mng_info,&have_mng_structure);
5476         return((Image *) NULL);
5477       }
5478
5479     mng_info->image=image;
5480
5481     if (mng_type)
5482       {
5483         MngBox
5484           crop_box;
5485
5486         if (mng_info->magn_methx || mng_info->magn_methy)
5487           {
5488             png_uint_32
5489                magnified_height,
5490                magnified_width;
5491
5492             if (logging != MagickFalse)
5493               (void) LogMagickEvent(CoderEvent,GetMagickModule(),
5494                 "  Processing MNG MAGN chunk");
5495
5496             if (mng_info->magn_methx == 1)
5497               {
5498                 magnified_width=mng_info->magn_ml;
5499
5500                 if (image->columns > 1)
5501                    magnified_width += mng_info->magn_mr;
5502
5503                 if (image->columns > 2)
5504                    magnified_width += (png_uint_32)
5505                       ((image->columns-2)*(mng_info->magn_mx));
5506               }
5507
5508             else
5509               {
5510                 magnified_width=(png_uint_32) image->columns;
5511
5512                 if (image->columns > 1)
5513                    magnified_width += mng_info->magn_ml-1;
5514
5515                 if (image->columns > 2)
5516                    magnified_width += mng_info->magn_mr-1;
5517
5518                 if (image->columns > 3)
5519                    magnified_width += (png_uint_32)
5520                       ((image->columns-3)*(mng_info->magn_mx-1));
5521               }
5522
5523             if (mng_info->magn_methy == 1)
5524               {
5525                 magnified_height=mng_info->magn_mt;
5526
5527                 if (image->rows > 1)
5528                    magnified_height += mng_info->magn_mb;
5529
5530                 if (image->rows > 2)
5531                    magnified_height += (png_uint_32)
5532                       ((image->rows-2)*(mng_info->magn_my));
5533               }
5534
5535             else
5536               {
5537                 magnified_height=(png_uint_32) image->rows;
5538
5539                 if (image->rows > 1)
5540                    magnified_height += mng_info->magn_mt-1;
5541
5542                 if (image->rows > 2)
5543                    magnified_height += mng_info->magn_mb-1;
5544
5545                 if (image->rows > 3)
5546                    magnified_height += (png_uint_32)
5547                       ((image->rows-3)*(mng_info->magn_my-1));
5548               }
5549
5550             if (magnified_height > image->rows ||
5551                 magnified_width > image->columns)
5552               {
5553                 Image
5554                   *large_image;
5555
5556                 int
5557                   yy;
5558
5559                 ssize_t
5560                   m,
5561                   y;
5562
5563                 register ssize_t
5564                   x;
5565
5566                 register PixelPacket
5567                   *n,
5568                   *q;
5569
5570                 PixelPacket
5571                   *next,
5572                   *prev;
5573
5574                 png_uint_16
5575                   magn_methx,
5576                   magn_methy;
5577
5578                 /* Allocate next image structure.  */
5579
5580                 if (logging != MagickFalse)
5581                   (void) LogMagickEvent(CoderEvent,GetMagickModule(),
5582                     "    Allocate magnified image");
5583
5584                 AcquireNextImage(image_info,image);
5585
5586                 if (GetNextImageInList(image) == (Image *) NULL)
5587                   {
5588                     image=DestroyImageList(image);
5589                     MngInfoFreeStruct(mng_info,&have_mng_structure);
5590                     return((Image *) NULL);
5591                   }
5592
5593                 large_image=SyncNextImageInList(image);
5594
5595                 large_image->columns=magnified_width;
5596                 large_image->rows=magnified_height;
5597
5598                 magn_methx=mng_info->magn_methx;
5599                 magn_methy=mng_info->magn_methy;
5600
5601 #if (MAGICKCORE_QUANTUM_DEPTH == 32)
5602 #define QM unsigned short
5603                 if (magn_methx != 1 || magn_methy != 1)
5604                   {
5605                   /*
5606                      Scale pixels to unsigned shorts to prevent
5607                      overflow of intermediate values of interpolations
5608                   */
5609                      for (y=0; y < (ssize_t) image->rows; y++)
5610                      {
5611                        q=GetAuthenticPixels(image,0,y,image->columns,1,
5612                           exception);
5613
5614                        for (x=(ssize_t) image->columns-1; x >= 0; x--)
5615                        {
5616                           q->red=ScaleQuantumToShort(q->red);
5617                           q->green=ScaleQuantumToShort(q->green);
5618                           q->blue=ScaleQuantumToShort(q->blue);
5619                           q->opacity=ScaleQuantumToShort(q->opacity);
5620                           q++;
5621                        }
5622
5623                        if (SyncAuthenticPixels(image,exception) == MagickFalse)
5624                          break;
5625                      }
5626                   }
5627 #else
5628 #define QM Quantum
5629 #endif
5630
5631                 if (image->matte != MagickFalse)
5632                    (void) SetImageBackgroundColor(large_image);
5633
5634                 else
5635                   {
5636                     large_image->background_color.opacity=OpaqueOpacity;
5637                     (void) SetImageBackgroundColor(large_image);
5638
5639                     if (magn_methx == 4)
5640                       magn_methx=2;
5641
5642                     if (magn_methx == 5)
5643                       magn_methx=3;
5644
5645                     if (magn_methy == 4)
5646                       magn_methy=2;
5647
5648                     if (magn_methy == 5)
5649                       magn_methy=3;
5650                   }
5651
5652                 /* magnify the rows into the right side of the large image */
5653
5654                 if (logging != MagickFalse)
5655                   (void) LogMagickEvent(CoderEvent,GetMagickModule(),
5656                     "    Magnify the rows to %.20g",(double) large_image->rows);
5657                 m=(ssize_t) mng_info->magn_mt;
5658                 yy=0;
5659                 length=(size_t) image->columns;
5660                 next=(PixelPacket *) AcquireQuantumMemory(length,sizeof(*next));
5661                 prev=(PixelPacket *) AcquireQuantumMemory(length,sizeof(*prev));
5662
5663                 if ((prev == (PixelPacket *) NULL) ||
5664                     (next == (PixelPacket *) NULL))
5665                   {
5666                      image=DestroyImageList(image);
5667                      MngInfoFreeStruct(mng_info,&have_mng_structure);
5668                      ThrowReaderException(ResourceLimitError,
5669                        "MemoryAllocationFailed");
5670                   }
5671
5672                 n=GetAuthenticPixels(image,0,0,image->columns,1,exception);
5673                 (void) CopyMagickMemory(next,n,length);
5674
5675                 for (y=0; y < (ssize_t) image->rows; y++)
5676                 {
5677                   if (y == 0)
5678                     m=(ssize_t) mng_info->magn_mt;
5679
5680                   else if (magn_methy > 1 && y == (ssize_t) image->rows-2)
5681                     m=(ssize_t) mng_info->magn_mb;
5682
5683                   else if (magn_methy <= 1 && y == (ssize_t) image->rows-1)
5684                     m=(ssize_t) mng_info->magn_mb;
5685
5686                   else if (magn_methy > 1 && y == (ssize_t) image->rows-1)
5687                     m=1;
5688
5689                   else
5690                     m=(ssize_t) mng_info->magn_my;
5691
5692                   n=prev;
5693                   prev=next;
5694                   next=n;
5695
5696                   if (y < (ssize_t) image->rows-1)
5697                     {
5698                       n=GetAuthenticPixels(image,0,y+1,image->columns,1,
5699                           exception);
5700                       (void) CopyMagickMemory(next,n,length);
5701                     }
5702
5703                   for (i=0; i < m; i++, yy++)
5704                   {
5705                     register PixelPacket
5706                       *pixels;
5707
5708                     assert(yy < (ssize_t) large_image->rows);
5709                     pixels=prev;
5710                     n=next;
5711                     q=GetAuthenticPixels(large_image,0,yy,large_image->columns,
5712                           1,exception);
5713                     q+=(large_image->columns-image->columns);
5714
5715                     for (x=(ssize_t) image->columns-1; x >= 0; x--)
5716                     {
5717                       /* To do: get color as function of indexes[x] */
5718                       /*
5719                       if (image->storage_class == PseudoClass)
5720                         {
5721                         }
5722                       */
5723
5724                       if (magn_methy <= 1)
5725                         {
5726                           *q=(*pixels); /* replicate previous */
5727                         }
5728
5729                       else if (magn_methy == 2 || magn_methy == 4)
5730                         {
5731                           if (i == 0)
5732                              *q=(*pixels);
5733
5734                           else
5735                             {
5736                               /* Interpolate */
5737                               (*q).red=(QM) (((ssize_t) (2*i*((*n).red
5738                                  -(*pixels).red)+m))/((ssize_t) (m*2))
5739                                  +(*pixels).red);
5740                               (*q).green=(QM) (((ssize_t) (2*i*((*n).green
5741                                  -(*pixels).green)+m))/((ssize_t) (m*2))
5742                                  +(*pixels).green);
5743                               (*q).blue=(QM) (((ssize_t) (2*i*((*n).blue
5744                                  -(*pixels).blue)+m))/((ssize_t) (m*2))
5745                                  +(*pixels).blue);
5746
5747                               if (image->matte != MagickFalse)
5748                                  (*q).opacity=(QM) (((ssize_t)
5749                                  (2*i*((*n).opacity
5750                                  -(*pixels).opacity)+m))
5751                                  /((ssize_t) (m*2))+(*pixels).opacity);
5752                             }
5753
5754                           if (magn_methy == 4)
5755                             {
5756                               /* Replicate nearest */
5757                               if (i <= ((m+1) << 1))
5758                                  (*q).opacity=(*pixels).opacity+0;
5759                               else
5760                                  (*q).opacity=(*n).opacity+0;
5761                             }
5762                         }
5763
5764                       else /* if (magn_methy == 3 || magn_methy == 5) */
5765                         {
5766                           /* Replicate nearest */
5767                           if (i <= ((m+1) << 1))
5768                              *q=(*pixels);
5769
5770                           else
5771                              *q=(*n);
5772
5773                           if (magn_methy == 5)
5774                             {
5775                               (*q).opacity=(QM) (((ssize_t) (2*i*((*n).opacity
5776                                  -(*pixels).opacity)+m))/((ssize_t) (m*2))
5777                                  +(*pixels).opacity);
5778                             }
5779                         }
5780                       n++;
5781                       q++;
5782                       pixels++;
5783                     } /* x */
5784
5785                     if (SyncAuthenticPixels(large_image,exception) == 0)
5786                       break;
5787
5788                   } /* i */
5789                 } /* y */
5790
5791                 prev=(PixelPacket *) RelinquishMagickMemory(prev);
5792                 next=(PixelPacket *) RelinquishMagickMemory(next);
5793
5794                 length=image->columns;
5795
5796                 if (logging != MagickFalse)
5797                   (void) LogMagickEvent(CoderEvent,GetMagickModule(),
5798                     "    Delete original image");
5799
5800                 DeleteImageFromList(&image);
5801
5802                 image=large_image;
5803
5804                 mng_info->image=image;
5805
5806                 /* magnify the columns */
5807                 if (logging != MagickFalse)
5808                   (void) LogMagickEvent(CoderEvent,GetMagickModule(),
5809                     "    Magnify the columns to %.20g",(double) image->columns);
5810
5811                 for (y=0; y < (ssize_t) image->rows; y++)
5812                 {
5813                   register PixelPacket
5814                     *pixels;
5815
5816                   q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
5817                   pixels=q+(image->columns-length);
5818                   n=pixels+1;
5819
5820                   for (x=(ssize_t) (image->columns-length);
5821                     x < (ssize_t) image->columns; x++)
5822                   {
5823                     if (x == (ssize_t) (image->columns-length))
5824                       m=(ssize_t) mng_info->magn_ml;
5825
5826                     else if (magn_methx > 1 && x == (ssize_t) image->columns-2)
5827                       m=(ssize_t) mng_info->magn_mr;
5828
5829                     else if (magn_methx <= 1 && x == (ssize_t) image->columns-1)
5830                       m=(ssize_t) mng_info->magn_mr;
5831
5832                     else if (magn_methx > 1 && x == (ssize_t) image->columns-1)
5833                       m=1;
5834
5835                     else
5836                       m=(ssize_t) mng_info->magn_mx;
5837
5838                     for (i=0; i < m; i++)
5839                     {
5840                       if (magn_methx <= 1)
5841                         {
5842                           /* replicate previous */
5843                           *q=(*pixels);
5844                         }
5845
5846                       else if (magn_methx == 2 || magn_methx == 4)
5847                         {
5848                           if (i == 0)
5849                             *q=(*pixels);
5850
5851                           else
5852                             {
5853                               /* Interpolate */
5854                               (*q).red=(QM) ((2*i*((*n).red
5855                                  -(*pixels).red)+m)
5856                                  /((ssize_t) (m*2))+(*pixels).red);
5857                               (*q).green=(QM) ((2*i*((*n).green
5858                                  -(*pixels).green)
5859                                  +m)/((ssize_t) (m*2))+(*pixels).green);
5860                               (*q).blue=(QM) ((2*i*((*n).blue
5861                                  -(*pixels).blue)+m)
5862                                  /((ssize_t) (m*2))+(*pixels).blue);
5863                               if (image->matte != MagickFalse)
5864                                  (*q).opacity=(QM) ((2*i*((*n).opacity
5865                                    -(*pixels).opacity)+m)/((ssize_t) (m*2))
5866                                    +(*pixels).opacity);
5867                             }
5868
5869                           if (magn_methx == 4)
5870                             {
5871                               /* Replicate nearest */
5872                               if (i <= ((m+1) << 1))
5873                                  (*q).opacity=(*pixels).opacity+0;
5874                               else
5875                                  (*q).opacity=(*n).opacity+0;
5876                             }
5877                         }
5878
5879                       else /* if (magn_methx == 3 || magn_methx == 5) */
5880                         {
5881                           /* Replicate nearest */
5882                           if (i <= ((m+1) << 1))
5883                              *q=(*pixels);
5884
5885                           else
5886                              *q=(*n);
5887
5888                           if (magn_methx == 5)
5889                             {
5890                               /* Interpolate */
5891                               (*q).opacity=(QM) ((2*i*((*n).opacity
5892                                  -(*pixels).opacity)+m) /((ssize_t) (m*2))
5893                                  +(*pixels).opacity);
5894                             }
5895                         }
5896                       q++;
5897                     }
5898                     n++;
5899                     p++;
5900                   }
5901
5902                   if (SyncAuthenticPixels(image,exception) == MagickFalse)
5903                     break;
5904                 }
5905 #if (MAGICKCORE_QUANTUM_DEPTH == 32)
5906               if (magn_methx != 1 || magn_methy != 1)
5907                 {
5908                 /*
5909                    Rescale pixels to Quantum
5910                 */
5911                    for (y=0; y < (ssize_t) image->rows; y++)
5912                    {
5913                      q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
5914
5915                      for (x=(ssize_t) image->columns-1; x >= 0; x--)
5916                      {
5917                         q->red=ScaleShortToQuantum(q->red);
5918                         q->green=ScaleShortToQuantum(q->green);
5919                         q->blue=ScaleShortToQuantum(q->blue);
5920                         q->opacity=ScaleShortToQuantum(q->opacity);
5921                         q++;
5922                      }
5923
5924                      if (SyncAuthenticPixels(image,exception) == MagickFalse)
5925                        break;
5926                    }
5927                 }
5928 #endif
5929                 if (logging != MagickFalse)
5930                   (void) LogMagickEvent(CoderEvent,GetMagickModule(),
5931                     "  Finished MAGN processing");
5932               }
5933           }
5934
5935         /*
5936           Crop_box is with respect to the upper left corner of the MNG.
5937         */
5938         crop_box.left=mng_info->image_box.left+mng_info->x_off[object_id];
5939         crop_box.right=mng_info->image_box.right+mng_info->x_off[object_id];
5940         crop_box.top=mng_info->image_box.top+mng_info->y_off[object_id];
5941         crop_box.bottom=mng_info->image_box.bottom+mng_info->y_off[object_id];
5942         crop_box=mng_minimum_box(crop_box,mng_info->clip);
5943         crop_box=mng_minimum_box(crop_box,mng_info->frame);
5944         crop_box=mng_minimum_box(crop_box,mng_info->object_clip[object_id]);
5945         if ((crop_box.left != (mng_info->image_box.left
5946             +mng_info->x_off[object_id])) ||
5947             (crop_box.right != (mng_info->image_box.right
5948             +mng_info->x_off[object_id])) ||
5949             (crop_box.top != (mng_info->image_box.top
5950             +mng_info->y_off[object_id])) ||
5951             (crop_box.bottom != (mng_info->image_box.bottom
5952             +mng_info->y_off[object_id])))
5953           {
5954             if (logging != MagickFalse)
5955               (void) LogMagickEvent(CoderEvent,GetMagickModule(),
5956                 "  Crop the PNG image");
5957
5958             if ((crop_box.left < crop_box.right) &&
5959                 (crop_box.top < crop_box.bottom))
5960               {
5961                 Image
5962                   *im;
5963
5964                 RectangleInfo
5965                   crop_info;
5966
5967                 /*
5968                   Crop_info is with respect to the upper left corner of
5969                   the image.
5970                 */
5971                 crop_info.x=(crop_box.left-mng_info->x_off[object_id]);
5972                 crop_info.y=(crop_box.top-mng_info->y_off[object_id]);
5973                 crop_info.width=(size_t) (crop_box.right-crop_box.left);
5974                 crop_info.height=(size_t) (crop_box.bottom-crop_box.top);
5975                 image->page.width=image->columns;
5976                 image->page.height=image->rows;
5977                 image->page.x=0;
5978                 image->page.y=0;
5979                 im=CropImage(image,&crop_info,exception);
5980
5981                 if (im != (Image *) NULL)
5982                   {
5983                     image->columns=im->columns;
5984                     image->rows=im->rows;
5985                     im=DestroyImage(im);
5986                     image->page.width=image->columns;
5987                     image->page.height=image->rows;
5988                     image->page.x=crop_box.left;
5989                     image->page.y=crop_box.top;
5990                   }
5991               }
5992
5993             else
5994               {
5995                 /*
5996                   No pixels in crop area.  The MNG spec still requires
5997                   a layer, though, so make a single transparent pixel in
5998                   the top left corner.
5999                 */
6000                 image->columns=1;
6001                 image->rows=1;
6002                 image->colors=2;
6003                 (void) SetImageBackgroundColor(image);
6004                 image->page.width=1;
6005                 image->page.height=1;
6006                 image->page.x=0;
6007                 image->page.y=0;
6008               }
6009           }
6010 #ifndef PNG_READ_EMPTY_PLTE_SUPPORTED
6011         image=mng_info->image;
6012 #endif
6013       }
6014
6015 #if (MAGICKCORE_QUANTUM_DEPTH > 16)
6016       /* PNG does not handle depths greater than 16 so reduce it even
6017        * if lossy
6018        */
6019       if (image->depth > 16)
6020          image->depth=16;
6021 #endif
6022
6023 #if (MAGICKCORE_QUANTUM_DEPTH >= 16)
6024       if (LosslessReduceDepthOK(image) != MagickFalse)
6025          image->depth = 8;
6026 #endif
6027
6028       GetImageException(image,exception);
6029
6030       if (image_info->number_scenes != 0)
6031         {
6032           if (mng_info->scenes_found >
6033              (ssize_t) (image_info->first_scene+image_info->number_scenes))
6034             break;
6035         }
6036
6037       if (logging != MagickFalse)
6038         (void) LogMagickEvent(CoderEvent,GetMagickModule(),
6039           "  Finished reading image datastream.");
6040
6041   } while (LocaleCompare(image_info->magick,"MNG") == 0);
6042
6043   (void) CloseBlob(image);
6044
6045   if (logging != MagickFalse)
6046     (void) LogMagickEvent(CoderEvent,GetMagickModule(),
6047       "  Finished reading all image datastreams.");
6048
6049 #if defined(MNG_INSERT_LAYERS)
6050   if (insert_layers && !mng_info->image_found && (mng_info->mng_width) &&
6051        (mng_info->mng_height))
6052     {
6053       /*
6054         Insert a background layer if nothing else was found.
6055       */
6056       if (logging != MagickFalse)
6057         (void) LogMagickEvent(CoderEvent,GetMagickModule(),
6058           "  No images found.  Inserting a background layer.");
6059
6060       if (GetAuthenticPixelQueue(image) != (PixelPacket *) NULL)
6061         {
6062           /*
6063             Allocate next image structure.
6064           */
6065           AcquireNextImage(image_info,image);
6066           if (GetNextImageInList(image) == (Image *) NULL)
6067             {
6068               image=DestroyImageList(image);
6069               MngInfoFreeStruct(mng_info,&have_mng_structure);
6070
6071               if (logging != MagickFalse)
6072                 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
6073                   "  Allocation failed, returning NULL.");
6074
6075               return((Image *) NULL);
6076             }
6077           image=SyncNextImageInList(image);
6078         }
6079       image->columns=mng_info->mng_width;
6080       image->rows=mng_info->mng_height;
6081       image->page.width=mng_info->mng_width;
6082       image->page.height=mng_info->mng_height;
6083       image->page.x=0;
6084       image->page.y=0;
6085       image->background_color=mng_background_color;
6086       image->matte=MagickFalse;
6087
6088       if (image_info->ping == MagickFalse)
6089         (void) SetImageBackgroundColor(image);
6090
6091       mng_info->image_found++;
6092     }
6093 #endif
6094   image->iterations=mng_iterations;
6095
6096   if (mng_iterations == 1)
6097     image->start_loop=MagickTrue;
6098
6099   while (GetPreviousImageInList(image) != (Image *) NULL)
6100   {
6101     image_count++;
6102     if (image_count > 10*mng_info->image_found)
6103       {
6104         if (logging != MagickFalse)
6105           (void) LogMagickEvent(CoderEvent,GetMagickModule(),"  No beginning");
6106
6107         (void) ThrowMagickException(&image->exception,GetMagickModule(),
6108           CoderError,"Linked list is corrupted, beginning of list not found",
6109           "`%s'",image_info->filename);
6110
6111         return((Image *) NULL);
6112       }
6113
6114     image=GetPreviousImageInList(image);
6115
6116     if (GetNextImageInList(image) == (Image *) NULL)
6117       {
6118         if (logging != MagickFalse)
6119           (void) LogMagickEvent(CoderEvent,GetMagickModule(),"  Corrupt list");
6120
6121         (void) ThrowMagickException(&image->exception,GetMagickModule(),
6122           CoderError,"Linked list is corrupted; next_image is NULL","`%s'",
6123           image_info->filename);
6124       }
6125   }
6126
6127   if (mng_info->ticks_per_second && mng_info->image_found > 1 &&
6128              GetNextImageInList(image) ==
6129      (Image *) NULL)
6130     {
6131       if (logging != MagickFalse)
6132         (void) LogMagickEvent(CoderEvent,GetMagickModule(),
6133             "  First image null");
6134
6135       (void) ThrowMagickException(&image->exception,GetMagickModule(),
6136         CoderError,"image->next for first image is NULL but shouldn't be.",
6137         "`%s'",image_info->filename);
6138     }
6139
6140   if (mng_info->image_found == 0)
6141     {
6142       if (logging != MagickFalse)
6143         (void) LogMagickEvent(CoderEvent,GetMagickModule(),
6144           "  No visible images found.");
6145
6146       (void) ThrowMagickException(&image->exception,GetMagickModule(),
6147         CoderError,"No visible images in file","`%s'",image_info->filename);
6148
6149       if (image != (Image *) NULL)
6150         image=DestroyImageList(image);
6151
6152       MngInfoFreeStruct(mng_info,&have_mng_structure);
6153       return((Image *) NULL);
6154     }
6155
6156   if (mng_info->ticks_per_second)
6157     final_delay=1UL*MagickMax(image->ticks_per_second,1L)*
6158             final_delay/mng_info->ticks_per_second;
6159
6160   else
6161     image->start_loop=MagickTrue;
6162
6163   /* Find final nonzero image delay */
6164   final_image_delay=0;
6165
6166   while (GetNextImageInList(image) != (Image *) NULL)
6167     {
6168       if (image->delay)
6169         final_image_delay=image->delay;
6170
6171       image=GetNextImageInList(image);
6172     }
6173
6174   if (final_delay < final_image_delay)
6175     final_delay=final_image_delay;
6176
6177   image->delay=final_delay;
6178
6179   if (logging != MagickFalse)
6180       (void) LogMagickEvent(CoderEvent,GetMagickModule(),
6181         "  image->delay=%.20g, final_delay=%.20g",(double) image->delay,
6182         (double) final_delay);
6183
6184   if (logging != MagickFalse)
6185     {
6186       int
6187         scene;
6188
6189       scene=0;
6190       image=GetFirstImageInList(image);
6191
6192       (void) LogMagickEvent(CoderEvent,GetMagickModule(),
6193         "  Before coalesce:");
6194
6195       (void) LogMagickEvent(CoderEvent,GetMagickModule(),
6196         "    scene 0 delay=%.20g",(double) image->delay);
6197
6198       while (GetNextImageInList(image) != (Image *) NULL)
6199       {
6200         image=GetNextImageInList(image);
6201         (void) LogMagickEvent(CoderEvent,GetMagickModule(),
6202           "    scene %.20g delay=%.20g",(double) scene++,(double) image->delay);
6203       }
6204     }
6205
6206   image=GetFirstImageInList(image);
6207 #ifdef MNG_COALESCE_LAYERS
6208   if (insert_layers)
6209     {
6210       Image
6211         *next_image,
6212         *next;
6213
6214       size_t
6215         scene;
6216
6217       if (logging != MagickFalse)
6218         (void) LogMagickEvent(CoderEvent,GetMagickModule(),"  Coalesce Images");
6219
6220       scene=image->scene;
6221       next_image=CoalesceImages(image,&image->exception);
6222
6223       if (next_image == (Image *) NULL)
6224         ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
6225
6226       image=DestroyImageList(image);
6227       image=next_image;
6228
6229       for (next=image; next != (Image *) NULL; next=next_image)
6230       {
6231          next->page.width=mng_info->mng_width;
6232          next->page.height=mng_info->mng_height;
6233          next->page.x=0;
6234          next->page.y=0;
6235          next->scene=scene++;
6236          next_image=GetNextImageInList(next);
6237
6238          if (next_image == (Image *) NULL)
6239            break;
6240
6241          if (next->delay == 0)
6242            {
6243              scene--;
6244              next_image->previous=GetPreviousImageInList(next);
6245              if (GetPreviousImageInList(next) == (Image *) NULL)
6246                image=next_image;
6247              else
6248                next->previous->next=next_image;
6249              next=DestroyImage(next);
6250            }
6251       }
6252     }
6253 #endif
6254
6255   while (GetNextImageInList(image) != (Image *) NULL)
6256       image=GetNextImageInList(image);
6257
6258   image->dispose=BackgroundDispose;
6259
6260   if (logging != MagickFalse)
6261     {
6262       int
6263         scene;
6264
6265       scene=0;
6266       image=GetFirstImageInList(image);
6267
6268       (void) LogMagickEvent(CoderEvent,GetMagickModule(),
6269         "  After coalesce:");
6270
6271       (void) LogMagickEvent(CoderEvent,GetMagickModule(),
6272         "    scene 0 delay=%.20g dispose=%.20g",(double) image->delay,
6273         (double) image->dispose);
6274
6275       while (GetNextImageInList(image) != (Image *) NULL)
6276       {
6277         image=GetNextImageInList(image);
6278
6279         (void) LogMagickEvent(CoderEvent,GetMagickModule(),
6280           "    scene %.20g delay=%.20g dispose=%.20g",(double) scene++,
6281           (double) image->delay,(double) image->dispose);
6282       }
6283    }
6284
6285   image=GetFirstImageInList(image);
6286   MngInfoFreeStruct(mng_info,&have_mng_structure);
6287   have_mng_structure=MagickFalse;
6288
6289   if (logging != MagickFalse)
6290     (void) LogMagickEvent(CoderEvent,GetMagickModule(),"exit ReadMNGImage()");
6291
6292   return(GetFirstImageInList(image));
6293 }
6294 #else /* PNG_LIBPNG_VER > 10011 */
6295 static Image *ReadPNGImage(const ImageInfo *image_info,ExceptionInfo *exception)
6296 {
6297   printf("Your PNG library is too old: You have libpng-%s\n",
6298      PNG_LIBPNG_VER_STRING);
6299
6300   (void) ThrowMagickException(exception,GetMagickModule(),CoderError,
6301     "PNG library is too old","`%s'",image_info->filename);
6302
6303   return(Image *) NULL;
6304 }
6305
6306 static Image *ReadMNGImage(const ImageInfo *image_info,ExceptionInfo *exception)
6307 {
6308   return(ReadPNGImage(image_info,exception));
6309 }
6310 #endif /* PNG_LIBPNG_VER > 10011 */
6311 #endif
6312 \f
6313 /*
6314 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6315 %                                                                             %
6316 %                                                                             %
6317 %                                                                             %
6318 %   R e g i s t e r P N G I m a g e                                           %
6319 %                                                                             %
6320 %                                                                             %
6321 %                                                                             %
6322 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6323 %
6324 %  RegisterPNGImage() adds properties for the PNG image format to
6325 %  the list of supported formats.  The properties include the image format
6326 %  tag, a method to read and/or write the format, whether the format
6327 %  supports the saving of more than one frame to the same file or blob,
6328 %  whether the format supports native in-memory I/O, and a brief
6329 %  description of the format.
6330 %
6331 %  The format of the RegisterPNGImage method is:
6332 %
6333 %      size_t RegisterPNGImage(void)
6334 %
6335 */
6336 ModuleExport size_t RegisterPNGImage(void)
6337 {
6338   char
6339     version[MaxTextExtent];
6340
6341   MagickInfo
6342     *entry;
6343
6344   static const char
6345     *PNGNote=
6346     {
6347       "See http://www.libpng.org/ for details about the PNG format."
6348     },
6349
6350     *JNGNote=
6351     {
6352       "See http://www.libpng.org/pub/mng/ for details about the JNG\n"
6353       "format."
6354     },
6355
6356     *MNGNote=
6357     {
6358       "See http://www.libpng.org/pub/mng/ for details about the MNG\n"
6359       "format."
6360     };
6361
6362   *version='\0';
6363
6364 #if defined(PNG_LIBPNG_VER_STRING)
6365   (void) ConcatenateMagickString(version,"libpng ",MaxTextExtent);
6366   (void) ConcatenateMagickString(version,PNG_LIBPNG_VER_STRING,MaxTextExtent);
6367
6368   if (LocaleCompare(PNG_LIBPNG_VER_STRING,png_get_header_ver(NULL)) != 0)
6369     {
6370       (void) ConcatenateMagickString(version,",",MaxTextExtent);
6371       (void) ConcatenateMagickString(version,png_get_libpng_ver(NULL),
6372             MaxTextExtent);
6373     }
6374 #endif
6375
6376   entry=SetMagickInfo("MNG");
6377   entry->seekable_stream=MagickTrue;  /* To do: eliminate this. */
6378
6379 #if defined(MAGICKCORE_PNG_DELEGATE)
6380   entry->decoder=(DecodeImageHandler *) ReadMNGImage;
6381   entry->encoder=(EncodeImageHandler *) WriteMNGImage;
6382 #endif
6383
6384   entry->magick=(IsImageFormatHandler *) IsMNG;
6385   entry->description=ConstantString("Multiple-image Network Graphics");
6386
6387   if (*version != '\0')
6388     entry->version=ConstantString(version);
6389
6390   entry->module=ConstantString("PNG");
6391   entry->note=ConstantString(MNGNote);
6392   (void) RegisterMagickInfo(entry);
6393
6394   entry=SetMagickInfo("PNG");
6395
6396 #if defined(MAGICKCORE_PNG_DELEGATE)
6397   entry->decoder=(DecodeImageHandler *) ReadPNGImage;
6398   entry->encoder=(EncodeImageHandler *) WritePNGImage;
6399 #endif
6400
6401   entry->magick=(IsImageFormatHandler *) IsPNG;
6402   entry->adjoin=MagickFalse;
6403   entry->description=ConstantString("Portable Network Graphics");
6404   entry->module=ConstantString("PNG");
6405
6406   if (*version != '\0')
6407     entry->version=ConstantString(version);
6408
6409   entry->note=ConstantString(PNGNote);
6410   (void) RegisterMagickInfo(entry);
6411
6412   entry=SetMagickInfo("PNG8");
6413
6414 #if defined(MAGICKCORE_PNG_DELEGATE)
6415   entry->decoder=(DecodeImageHandler *) ReadPNGImage;
6416   entry->encoder=(EncodeImageHandler *) WritePNGImage;
6417 #endif
6418
6419   entry->magick=(IsImageFormatHandler *) IsPNG;
6420   entry->adjoin=MagickFalse;
6421   entry->description=ConstantString(
6422             "8-bit indexed with optional binary transparency");
6423   entry->module=ConstantString("PNG");
6424   (void) RegisterMagickInfo(entry);
6425
6426   entry=SetMagickInfo("PNG24");
6427   *version='\0';
6428
6429 #if defined(ZLIB_VERSION)
6430   (void) ConcatenateMagickString(version,"zlib ",MaxTextExtent);
6431   (void) ConcatenateMagickString(version,ZLIB_VERSION,MaxTextExtent);
6432
6433   if (LocaleCompare(ZLIB_VERSION,zlib_version) != 0)
6434     {
6435       (void) ConcatenateMagickString(version,",",MaxTextExtent);
6436       (void) ConcatenateMagickString(version,zlib_version,MaxTextExtent);
6437     }
6438 #endif
6439
6440   if (*version != '\0')
6441     entry->version=ConstantString(version);
6442
6443 #if defined(MAGICKCORE_PNG_DELEGATE)
6444   entry->decoder=(DecodeImageHandler *) ReadPNGImage;
6445   entry->encoder=(EncodeImageHandler *) WritePNGImage;
6446 #endif
6447
6448   entry->magick=(IsImageFormatHandler *) IsPNG;
6449   entry->adjoin=MagickFalse;
6450   entry->description=ConstantString("opaque 24-bit RGB");
6451   entry->module=ConstantString("PNG");
6452   (void) RegisterMagickInfo(entry);
6453
6454   entry=SetMagickInfo("PNG32");
6455
6456 #if defined(MAGICKCORE_PNG_DELEGATE)
6457   entry->decoder=(DecodeImageHandler *) ReadPNGImage;
6458   entry->encoder=(EncodeImageHandler *) WritePNGImage;
6459 #endif
6460
6461   entry->magick=(IsImageFormatHandler *) IsPNG;
6462   entry->adjoin=MagickFalse;
6463   entry->description=ConstantString("opaque or transparent 32-bit RGBA");
6464   entry->module=ConstantString("PNG");
6465   (void) RegisterMagickInfo(entry);
6466
6467   entry=SetMagickInfo("JNG");
6468
6469 #if defined(JNG_SUPPORTED)
6470 #if defined(MAGICKCORE_PNG_DELEGATE)
6471   entry->decoder=(DecodeImageHandler *) ReadJNGImage;
6472   entry->encoder=(EncodeImageHandler *) WriteJNGImage;
6473 #endif
6474 #endif
6475
6476   entry->magick=(IsImageFormatHandler *) IsJNG;
6477   entry->adjoin=MagickFalse;
6478   entry->description=ConstantString("JPEG Network Graphics");
6479   entry->module=ConstantString("PNG");
6480   entry->note=ConstantString(JNGNote);
6481   (void) RegisterMagickInfo(entry);
6482
6483 #if defined(PNG_SETJMP_NOT_THREAD_SAFE)
6484   ping_semaphore=AllocateSemaphoreInfo();
6485 #endif
6486
6487   return(MagickImageCoderSignature);
6488 }
6489 \f
6490 /*
6491 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6492 %                                                                             %
6493 %                                                                             %
6494 %                                                                             %
6495 %   U n r e g i s t e r P N G I m a g e                                       %
6496 %                                                                             %
6497 %                                                                             %
6498 %                                                                             %
6499 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6500 %
6501 %  UnregisterPNGImage() removes format registrations made by the
6502 %  PNG module from the list of supported formats.
6503 %
6504 %  The format of the UnregisterPNGImage method is:
6505 %
6506 %      UnregisterPNGImage(void)
6507 %
6508 */
6509 ModuleExport void UnregisterPNGImage(void)
6510 {
6511   (void) UnregisterMagickInfo("MNG");
6512   (void) UnregisterMagickInfo("PNG");
6513   (void) UnregisterMagickInfo("PNG8");
6514   (void) UnregisterMagickInfo("PNG24");
6515   (void) UnregisterMagickInfo("PNG32");
6516   (void) UnregisterMagickInfo("JNG");
6517
6518 #if defined(PNG_SETJMP_NOT_THREAD_SAFE)
6519   if (ping_semaphore != (SemaphoreInfo *) NULL)
6520     DestroySemaphoreInfo(&ping_semaphore);
6521 #endif
6522 }
6523 \f
6524 #if defined(MAGICKCORE_PNG_DELEGATE)
6525 #if PNG_LIBPNG_VER > 10011
6526 /*
6527 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6528 %                                                                             %
6529 %                                                                             %
6530 %                                                                             %
6531 %   W r i t e M N G I m a g e                                                 %
6532 %                                                                             %
6533 %                                                                             %
6534 %                                                                             %
6535 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6536 %
6537 %  WriteMNGImage() writes an image in the Portable Network Graphics
6538 %  Group's "Multiple-image Network Graphics" encoded image format.
6539 %
6540 %  MNG support written by Glenn Randers-Pehrson, glennrp@image...
6541 %
6542 %  The format of the WriteMNGImage method is:
6543 %
6544 %      MagickBooleanType WriteMNGImage(const ImageInfo *image_info,Image *image)
6545 %
6546 %  A description of each parameter follows.
6547 %
6548 %    o image_info: the image info.
6549 %
6550 %    o image:  The image.
6551 %
6552 %
6553 %  To do (as of version 5.5.2, November 26, 2002 -- glennrp -- see also
6554 %    "To do" under ReadPNGImage):
6555 %
6556 %    Preserve all unknown and not-yet-handled known chunks found in input
6557 %    PNG file and copy them  into output PNG files according to the PNG
6558 %    copying rules.
6559 %
6560 %    Write the iCCP chunk at MNG level when (icc profile length > 0)
6561 %
6562 %    Improve selection of color type (use indexed-colour or indexed-colour
6563 %    with tRNS when 256 or fewer unique RGBA values are present).
6564 %
6565 %    Figure out what to do with "dispose=<restore-to-previous>" (dispose == 3)
6566 %    This will be complicated if we limit ourselves to generating MNG-LC
6567 %    files.  For now we ignore disposal method 3 and simply overlay the next
6568 %    image on it.
6569 %
6570 %    Check for identical PLTE's or PLTE/tRNS combinations and use a
6571 %    global MNG PLTE or PLTE/tRNS combination when appropriate.
6572 %    [mostly done 15 June 1999 but still need to take care of tRNS]
6573 %
6574 %    Check for identical sRGB and replace with a global sRGB (and remove
6575 %    gAMA/cHRM if sRGB is found; check for identical gAMA/cHRM and
6576 %    replace with global gAMA/cHRM (or with sRGB if appropriate; replace
6577 %    local gAMA/cHRM with local sRGB if appropriate).
6578 %
6579 %    Check for identical sBIT chunks and write global ones.
6580 %
6581 %    Provide option to skip writing the signature tEXt chunks.
6582 %
6583 %    Use signatures to detect identical objects and reuse the first
6584 %    instance of such objects instead of writing duplicate objects.
6585 %
6586 %    Use a smaller-than-32k value of compression window size when
6587 %    appropriate.
6588 %
6589 %    Encode JNG datastreams.  Mostly done as of 5.5.2; need to write
6590 %    ancillary text chunks and save profiles.
6591 %
6592 %    Provide an option to force LC files (to ensure exact framing rate)
6593 %    instead of VLC.
6594 %
6595 %    Provide an option to force VLC files instead of LC, even when offsets
6596 %    are present.  This will involve expanding the embedded images with a
6597 %    transparent region at the top and/or left.
6598 */
6599
6600 static void
6601 Magick_png_write_raw_profile(const ImageInfo *image_info,png_struct *ping,
6602    png_info *ping_info, unsigned char *profile_type, unsigned char
6603    *profile_description, unsigned char *profile_data, png_uint_32 length)
6604 {
6605    png_textp
6606      text;
6607
6608    register ssize_t
6609      i;
6610
6611    unsigned char
6612      *sp;
6613
6614    png_charp
6615      dp;
6616
6617    png_uint_32
6618      allocated_length,
6619      description_length;
6620
6621    unsigned char
6622      hex[16]={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
6623
6624    if (LocaleNCompare((char *) profile_type+1, "ng-chunk-",9) == 0)
6625       return;
6626
6627    if (image_info->verbose)
6628      {
6629        (void) printf("writing raw profile: type=%s, length=%.20g\n",
6630          (char *) profile_type, (double) length);
6631      }
6632
6633    text=(png_textp) png_malloc(ping,(png_uint_32) sizeof(png_text));
6634    description_length=(png_uint_32) strlen((const char *) profile_description);
6635    allocated_length=(png_uint_32) (length*2 + (length >> 5) + 20
6636       + description_length);
6637    text[0].text=(png_charp) png_malloc(ping,allocated_length);
6638    text[0].key=(png_charp) png_malloc(ping, (png_uint_32) 80);
6639    text[0].key[0]='\0';
6640    (void) ConcatenateMagickString(text[0].key,
6641       "Raw profile type ",MaxTextExtent);
6642    (void) ConcatenateMagickString(text[0].key,(const char *) profile_type,62);
6643    sp=profile_data;
6644    dp=text[0].text;
6645    *dp++='\n';
6646    (void) CopyMagickString(dp,(const char *) profile_description,
6647      allocated_length);
6648    dp+=description_length;
6649    *dp++='\n';
6650    (void) FormatMagickString(dp,allocated_length-
6651      (png_size_t) (dp-text[0].text),"%8lu ",(unsigned long) length);
6652    dp+=8;
6653
6654    for (i=0; i < (ssize_t) length; i++)
6655    {
6656      if (i%36 == 0)
6657        *dp++='\n';
6658      *(dp++)=(char) hex[((*sp >> 4) & 0x0f)];
6659      *(dp++)=(char) hex[((*sp++ ) & 0x0f)];
6660    }
6661
6662    *dp++='\n';
6663    *dp='\0';
6664    text[0].text_length=(png_size_t) (dp-text[0].text);
6665    text[0].compression=image_info->compression == NoCompression ||
6666      (image_info->compression == UndefinedCompression &&
6667      text[0].text_length < 128) ? -1 : 0;
6668
6669    if (text[0].text_length <= allocated_length)
6670      png_set_text(ping,ping_info,text,1);
6671
6672    png_free(ping,text[0].text);
6673    png_free(ping,text[0].key);
6674    png_free(ping,text);
6675 }
6676
6677 static MagickBooleanType Magick_png_write_chunk_from_profile(Image *image,
6678   const char *string, MagickBooleanType logging)
6679 {
6680   char
6681     *name;
6682
6683   const StringInfo
6684     *profile;
6685
6686   unsigned char
6687     *data;
6688
6689   png_uint_32 length;
6690
6691   ResetImageProfileIterator(image);
6692
6693   for (name=GetNextImageProfile(image); name != (const char *) NULL; )
6694   {
6695     profile=GetImageProfile(image,name);
6696
6697     if (profile != (const StringInfo *) NULL)
6698       {
6699         StringInfo
6700           *ping_profile;
6701
6702         if (LocaleNCompare(name,string,11) == 0)
6703           {
6704             if (logging != MagickFalse)
6705                (void) LogMagickEvent(CoderEvent,GetMagickModule(),
6706                    "  Found %s profile",name);
6707
6708             ping_profile=CloneStringInfo(profile);
6709             data=GetStringInfoDatum(ping_profile),
6710             length=(png_uint_32) GetStringInfoLength(ping_profile);
6711             data[4]=data[3];
6712             data[3]=data[2];
6713             data[2]=data[1];
6714             data[1]=data[0];
6715             (void) WriteBlobMSBULong(image,length-5);  /* data length */
6716             (void) WriteBlob(image,length-1,data+1);
6717             (void) WriteBlobMSBULong(image,crc32(0,data+1,(uInt) length-1));
6718             ping_profile=DestroyStringInfo(ping_profile);
6719           }
6720       }
6721
6722       name=GetNextImageProfile(image);
6723    }
6724
6725    return(MagickTrue);
6726 }
6727
6728
6729 /* Write one PNG image */
6730 static MagickBooleanType WriteOnePNGImage(MngInfo *mng_info,
6731    const ImageInfo *IMimage_info,Image *IMimage)
6732 {
6733   Image
6734     *image;
6735
6736   ImageInfo
6737     *image_info;
6738
6739   char
6740     s[2];
6741
6742   const char
6743     *name,
6744     *property,
6745     *value;
6746
6747   const StringInfo
6748     *profile;
6749
6750   int
6751     num_passes,
6752     pass;
6753
6754   png_byte
6755      ping_trans_alpha[256];
6756
6757   png_color
6758      palette[257];
6759
6760   png_color_16
6761     ping_background,
6762     ping_trans_color;
6763
6764   png_info
6765     *ping_info;
6766
6767   png_struct
6768     *ping;
6769
6770   png_uint_32
6771     ping_height,
6772     ping_width;
6773
6774   ssize_t
6775     y;
6776
6777   MagickBooleanType
6778     image_matte,
6779     logging,
6780     matte,
6781
6782     ping_have_blob,
6783     ping_have_cheap_transparency,
6784     ping_have_color,
6785     ping_have_non_bw,
6786     ping_have_PLTE,
6787     ping_have_bKGD,
6788     ping_have_pHYs,
6789     ping_have_tRNS,
6790
6791     ping_exclude_bKGD,
6792     ping_exclude_cHRM,
6793     /* ping_exclude_EXIF, */
6794     ping_exclude_gAMA,
6795     ping_exclude_iCCP,
6796     /* ping_exclude_iTXt, */
6797     ping_exclude_oFFs,
6798     ping_exclude_pHYs,
6799     ping_exclude_sRGB,
6800     ping_exclude_tEXt,
6801     /* ping_exclude_tRNS, */
6802     ping_exclude_vpAg,
6803     ping_exclude_zCCP, /* hex-encoded iCCP */
6804     ping_exclude_zTXt,
6805
6806     ping_need_colortype_warning,
6807
6808     status;
6809
6810   QuantumInfo
6811     *quantum_info;
6812
6813   register ssize_t
6814     i,
6815     x;
6816
6817   unsigned char
6818     *ping_pixels;
6819
6820   volatile int
6821     image_colors,
6822     ping_bit_depth,
6823     ping_color_type,
6824     ping_interlace_method,
6825     ping_compression_method,
6826     ping_filter_method,
6827     ping_num_trans;
6828
6829   volatile size_t
6830     image_depth,
6831     old_bit_depth;
6832
6833   size_t
6834     quality,
6835     rowbytes,
6836     save_image_depth;
6837
6838   int
6839     j,
6840     number_colors,
6841     number_opaque,
6842     number_semitransparent,
6843     number_transparent,
6844     ping_pHYs_unit_type;
6845
6846   png_uint_32
6847     ping_pHYs_x_resolution,
6848     ping_pHYs_y_resolution;
6849
6850   logging=LogMagickEvent(CoderEvent,GetMagickModule(),
6851     "  Enter WriteOnePNGImage()");
6852
6853   image = CloneImage(IMimage,0,0,MagickFalse,&IMimage->exception);
6854   image_info=(ImageInfo *) CloneImageInfo(IMimage_info);
6855
6856 #if defined(PNG_SETJMP_NOT_THREAD_SAFE)
6857   LockSemaphoreInfo(ping_semaphore);
6858 #endif
6859
6860   /* Initialize some stuff */
6861   ping_bit_depth=0,
6862   ping_color_type=0,
6863   ping_interlace_method=0,
6864   ping_compression_method=0,
6865   ping_filter_method=0,
6866   ping_num_trans = 0;
6867
6868   ping_background.red = 0;
6869   ping_background.green = 0;
6870   ping_background.blue = 0;
6871   ping_background.gray = 0;
6872   ping_background.index = 0;
6873
6874   ping_trans_color.red=0;
6875   ping_trans_color.green=0;
6876   ping_trans_color.blue=0;
6877   ping_trans_color.gray=0;
6878
6879   ping_pHYs_unit_type = 0;
6880   ping_pHYs_x_resolution = 0;
6881   ping_pHYs_y_resolution = 0;
6882
6883   ping_have_blob=MagickFalse;
6884   ping_have_color=MagickTrue;
6885   ping_have_non_bw=MagickTrue;
6886   ping_have_PLTE=MagickFalse;
6887   ping_have_bKGD=MagickFalse;
6888   ping_have_pHYs=MagickFalse;
6889   ping_have_tRNS=MagickFalse;
6890
6891   ping_exclude_bKGD=mng_info->ping_exclude_bKGD;
6892   ping_exclude_cHRM=mng_info->ping_exclude_cHRM;
6893   /* ping_exclude_EXIF=mng_info->ping_exclude_EXIF; */
6894   ping_exclude_gAMA=mng_info->ping_exclude_gAMA;
6895   ping_exclude_cHRM=mng_info->ping_exclude_cHRM;
6896   ping_exclude_iCCP=mng_info->ping_exclude_iCCP;
6897   /* ping_exclude_iTXt=mng_info->ping_exclude_iTXt; */
6898   ping_exclude_oFFs=mng_info->ping_exclude_oFFs;
6899   ping_exclude_pHYs=mng_info->ping_exclude_pHYs;
6900   ping_exclude_sRGB=mng_info->ping_exclude_sRGB;
6901   ping_exclude_tEXt=mng_info->ping_exclude_tEXt;
6902   /* ping_exclude_tRNS=mng_info->ping_exclude_tRNS; */
6903   ping_exclude_vpAg=mng_info->ping_exclude_vpAg;
6904   ping_exclude_zCCP=mng_info->ping_exclude_zCCP; /* hex-encoded iCCP in zTXt */
6905   ping_exclude_zTXt=mng_info->ping_exclude_zTXt;
6906
6907   ping_need_colortype_warning = MagickFalse;
6908
6909   number_opaque = 0;
6910   number_semitransparent = 0;
6911   number_transparent = 0;
6912
6913   if (logging != MagickFalse)
6914     {
6915       if (image->storage_class == UndefinedClass)
6916           (void) LogMagickEvent(CoderEvent,GetMagickModule(),
6917           "    storage_class=UndefinedClass");
6918       if (image->storage_class == DirectClass)
6919           (void) LogMagickEvent(CoderEvent,GetMagickModule(),
6920           "    storage_class=DirectClass");
6921       if (image->storage_class == PseudoClass)
6922           (void) LogMagickEvent(CoderEvent,GetMagickModule(),
6923           "    storage_class=PseudoClass");
6924     }
6925    
6926   if (image->colorspace != RGBColorspace)
6927     (void) TransformImageColorspace(image,RGBColorspace);
6928
6929   /*
6930     Sometimes we get PseudoClass images whose RGB values don't match
6931     the colors in the colormap.  This code syncs the RGB values.
6932   */
6933   if (image->depth <= 8 && image->taint && image->storage_class == PseudoClass)
6934      (void) SyncImage(image);
6935
6936 #if (MAGICKCORE_QUANTUM_DEPTH == 8)
6937   if (image->depth > 8)
6938     {
6939       if (logging != MagickFalse)
6940         (void) LogMagickEvent(CoderEvent,GetMagickModule(),
6941           "    Reducing PNG bit depth to 8 since this is a Q8 build.");
6942
6943       image->depth=8;
6944     }
6945 #endif
6946
6947
6948 #if (MAGICKCORE_QUANTUM_DEPTH > 16)
6949   /* PNG does not handle depths greater than 16 so reduce it even
6950    * if lossy
6951    */
6952   if (image->depth > 16)
6953       image->depth=16;
6954 #endif
6955
6956 #if (MAGICKCORE_QUANTUM_DEPTH >= 16)
6957   if (image->depth == 16 && mng_info->write_png_depth != 16)
6958     if (mng_info->write_png8 || LosslessReduceDepthOK(image) != MagickFalse)
6959       image->depth = 8;
6960 #endif
6961
6962   /* Normally we run this just once, but in the case of writing PNG8
6963    * we reduce the transparency to binary and run again, then reduce
6964    * the colors to a simple 3-3-2 palette and run once more.
6965    */
6966   for (j=0; j<3; j++)
6967   {
6968     /* BUILD_PALETTE
6969      *
6970      * Sometimes we get DirectClass images that have 256 colors or fewer.
6971      * This code will build a colormap.
6972      *
6973      * Also, sometimes we get PseudoClass images with an out-of-date
6974      * colormap.  This code will replace the colormap with a new one.
6975      * Sometimes we get PseudoClass images that have more than 256 colors.
6976      * This code will delete the colormap and change the image to
6977      * DirectClass.
6978      *
6979      * If image->matte is MagickFalse, we ignore the opacity channel
6980      * even though it sometimes contains left-over non-opaque values.
6981      *
6982      * Also we gather some information (number of opaque, transparent,
6983      * and semitransparent pixels, and whether the image has any non-gray
6984      * pixels or only black-and-white pixels) that we might need later.
6985      *
6986      * Even if the user wants to force GrayAlpha or RGBA (colortype 4 or 6)
6987      * we need to check for bogus non-opaque values, at least.
6988      */
6989
6990 #if (MAGICKCORE_QUANTUM_DEPTH == 8)
6991 #  define PNGK 0 /* Shift */
6992 #  define PNGM 1 /* Scale */
6993 #elif (MAGICKCORE_QUANTUM_DEPTH == 16)
6994 #  define PNGK 8
6995 #  define PNGM 0x0101
6996 #else
6997 #  define PNGK 24
6998 #  define PNGM 0x01010101
6999 #endif
7000
7001    ExceptionInfo
7002      *exception;
7003
7004    int
7005      n;
7006
7007    PixelPacket
7008      opaque[260],
7009      semitransparent[260],
7010      transparent[260];
7011
7012    register IndexPacket
7013      *indexes;
7014
7015    register const PixelPacket
7016      *s,
7017      *q;
7018
7019    register PixelPacket
7020      *r;
7021
7022    if (logging != MagickFalse)
7023      (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7024          "    Enter BUILD_PALETTE:");
7025
7026    if (logging != MagickFalse)
7027      {
7028        (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7029              "      image->columns=%.20g",(double) image->columns);
7030        (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7031              "      image->rows=%.20g",(double) image->rows);
7032        (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7033              "      image->matte=%.20g",(double) image->matte);
7034        (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7035              "      image->depth=%.20g",(double) image->depth);
7036
7037        if (image->storage_class == PseudoClass && image->colormap != NULL)
7038        {
7039          (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7040              "      Original colormap:");
7041          (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7042              "        i    (red,green,blue,opacity)");
7043
7044          for (i=0; i < 256; i++)
7045          {
7046                (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7047                    "        %d    (%d,%d,%d,%d)",
7048                     (int) i,
7049                     (int) image->colormap[i].red,
7050                     (int) image->colormap[i].green,
7051                     (int) image->colormap[i].blue,
7052                     (int) image->colormap[i].opacity);
7053          }
7054
7055          for (i=image->colors - 10; i < (ssize_t) image->colors; i++)
7056          {
7057            if (i > 255)
7058              {
7059                (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7060                    "        %d    (%d,%d,%d,%d)",
7061                     (int) i,
7062                     (int) image->colormap[i].red,
7063                     (int) image->colormap[i].green,
7064                     (int) image->colormap[i].blue,
7065                     (int) image->colormap[i].opacity);
7066              }
7067          }
7068        }
7069
7070        (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7071            "      image->colors=%d",(int) image->colors);
7072
7073        if (image->colors == 0)
7074        (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7075            "        (zero means unknown)");
7076
7077        (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7078             "      Regenerate the colormap");
7079      }
7080
7081      exception=(&image->exception);
7082
7083      image_colors=0;
7084      number_opaque = 0;
7085      number_semitransparent = 0;
7086      number_transparent = 0;
7087
7088      for (y=0; y < (ssize_t) image->rows; y++)
7089      {
7090        q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
7091
7092        if (q == (PixelPacket *) NULL)
7093          break;
7094
7095        for (x=0; x < (ssize_t) image->columns; x++)
7096        {
7097            if (image->matte == MagickFalse || q->opacity == OpaqueOpacity)
7098              {
7099                if (number_opaque < 259)
7100                  {
7101                    if (number_opaque == 0)
7102                      {
7103                        opaque[0]=*q;
7104                        opaque[0].opacity=OpaqueOpacity;
7105                        number_opaque=1;
7106                      }
7107
7108                    for (i=0; i< (ssize_t) number_opaque; i++)
7109                      {
7110                        if (IsColorEqual(opaque+i, (PixelPacket *) q))
7111                          break;
7112                      }
7113
7114                    if (i ==  (ssize_t) number_opaque &&
7115                        number_opaque < 259)
7116                      {
7117                        number_opaque++;
7118                        opaque[i] = *q;
7119                        opaque[i].opacity = OpaqueOpacity;
7120                      }
7121                  }
7122              }
7123            else if (q->opacity == TransparentOpacity)
7124              {
7125                if (number_transparent < 259)
7126                  {
7127                    if (number_transparent == 0)
7128                      {
7129                        transparent[0]=*q;
7130                        ping_trans_color.red=(unsigned short)(q->red);
7131                        ping_trans_color.green=(unsigned short) (q->green);
7132                        ping_trans_color.blue=(unsigned short) (q->blue);
7133                        ping_trans_color.gray=(unsigned short) (q->blue);
7134                        number_transparent = 1;
7135                      }
7136
7137                    for (i=0; i< (ssize_t) number_transparent; i++)
7138                      {
7139                        if (IsColorEqual(transparent+i, (PixelPacket *) q))
7140                          break;
7141                      }
7142
7143                    if (i ==  (ssize_t) number_transparent &&
7144                        number_transparent < 259)
7145                      {
7146                        number_transparent++;
7147                        transparent[i] = *q;
7148                      }
7149                  }
7150              }
7151            else
7152              {
7153                if (number_semitransparent < 259)
7154                  {
7155                    if (number_semitransparent == 0)
7156                      {
7157                        semitransparent[0]=*q;
7158                        number_semitransparent = 1;
7159                      }
7160
7161                    for (i=0; i< (ssize_t) number_semitransparent; i++)
7162                      {
7163                        if (IsColorEqual(semitransparent+i,
7164                           (PixelPacket *) q) &&
7165                           q->opacity == semitransparent[i].opacity)
7166                          break;
7167                      }
7168
7169                    if (i ==  (ssize_t) number_semitransparent &&
7170                        number_semitransparent < 259)
7171                      {
7172                        number_semitransparent++;
7173                        semitransparent[i] = *q;
7174                      }
7175                  }
7176              }
7177            q++;
7178         }
7179      }
7180
7181      if (ping_exclude_bKGD == MagickFalse)
7182        {
7183          /* Add the background color to the palette, if it
7184           * isn't already there.
7185           */
7186           for (i=0; i<number_opaque; i++)
7187           {
7188              if (IsColorEqual(opaque+i,
7189                 &image->background_color))
7190              break;
7191           }
7192
7193           if (number_opaque < 259 && i == number_opaque)
7194             {
7195                opaque[i]=image->background_color;
7196                opaque[i].opacity = OpaqueOpacity;
7197                number_opaque++;
7198             }
7199        }
7200
7201      image_colors=number_opaque+number_transparent+number_semitransparent;
7202
7203      if (logging != MagickFalse)
7204        {
7205          if (image_colors > 256)
7206             (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7207                   "      image has more than 256 colors");
7208
7209          else
7210             (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7211                   "      image has %d colors",image_colors);
7212        }
7213
7214      if (mng_info->write_png_colortype != 7) /* We won't need this info */
7215        {
7216          ping_have_color=MagickFalse;
7217          ping_have_non_bw=MagickFalse;
7218
7219          if(image_colors > 256)
7220            {
7221              for (y=0; y < (ssize_t) image->rows; y++)
7222              {
7223                q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
7224
7225                if (q == (PixelPacket *) NULL)
7226                  break;
7227
7228                /* Worst case is black-and-white; we are looking at every
7229                 * pixel twice.
7230                 */
7231
7232                if (ping_have_color == MagickFalse)
7233                  {
7234                    s=q;
7235                    for (x=0; x < (ssize_t) image->columns; x++)
7236                    {
7237                      if (s->red != s->green || s->red != s->blue)
7238                        {
7239                           ping_have_color=MagickTrue;
7240                           ping_have_non_bw=MagickTrue;
7241                           break;
7242                        }
7243                      s++;
7244                    }
7245                  }
7246
7247                if (ping_have_non_bw == MagickFalse)
7248                  {
7249                    s=q;
7250                    for (x=0; x < (ssize_t) image->columns; x++)
7251                    {
7252                      if (s->red != 0 && s->red != QuantumRange)
7253                        {
7254                          ping_have_non_bw=MagickTrue;
7255                        }
7256                      s++;
7257                    }
7258                  }
7259              }
7260            } 
7261        } 
7262
7263      if (image_colors < 257)
7264        {
7265          PixelPacket
7266            colormap[260];
7267              
7268          /*
7269           * Initialize image colormap.
7270           */
7271
7272          if (logging != MagickFalse)
7273             (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7274                   "      Sort the new colormap");
7275
7276         /* Sort palette, transparent first */;
7277
7278          n = 0;
7279
7280          for (i=0; i<number_transparent; i++)
7281             colormap[n++] = transparent[i];
7282
7283          for (i=0; i<number_semitransparent; i++)
7284             colormap[n++] = semitransparent[i];
7285
7286          for (i=0; i<number_opaque; i++)
7287             colormap[n++] = opaque[i];
7288
7289          
7290          /* image_colors < 257; search the colormap instead of the pixels
7291           * to get ping_have_color and ping_have_non_bw
7292           */
7293          for (i=0; i<n; i++)
7294          {
7295            if (ping_have_color == MagickFalse)
7296              {
7297                 if (colormap[i].red != colormap[i].green ||
7298                     colormap[i].red != colormap[i].blue)
7299                   {
7300                      ping_have_color=MagickTrue;
7301                      ping_have_non_bw=MagickTrue;
7302                      break;
7303                   }
7304               }
7305
7306            if (ping_have_non_bw == MagickFalse)
7307              {
7308                if (colormap[i].red != 0 && colormap[i].red != QuantumRange)
7309                    ping_have_non_bw=MagickTrue;
7310              }
7311           }
7312
7313         if ((mng_info->ping_exclude_tRNS == MagickFalse ||
7314             (number_transparent == 0 && number_semitransparent == 0)) &&
7315             (((mng_info->write_png_colortype-1) ==
7316             PNG_COLOR_TYPE_PALETTE) ||
7317             (mng_info->write_png_colortype == 0)))
7318           {
7319             if (logging != MagickFalse)
7320               {
7321                 if (n !=  (ssize_t) image_colors)
7322                    (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7323                    "   image_colors (%d) and n (%d)  don't match",
7324                    image_colors, n);
7325
7326                 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7327                    "      AcquireImageColormap");
7328               }
7329
7330             image->colors = image_colors;
7331
7332             if (AcquireImageColormap(image,image_colors) ==
7333                 MagickFalse)
7334                ThrowWriterException(ResourceLimitError,
7335                   "MemoryAllocationFailed");
7336
7337             for (i=0; i< (ssize_t) image_colors; i++)
7338                image->colormap[i] = colormap[i];
7339
7340             if (logging != MagickFalse)
7341               {
7342                 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7343                       "      image->colors=%d (%d)",
7344                       (int) image->colors, image_colors);
7345  
7346                 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7347                       "      Update the pixel indexes");
7348               }
7349
7350             /* Sync the pixel indices with the new colormap */
7351
7352             for (y=0; y < (ssize_t) image->rows; y++)
7353             {
7354               q=GetAuthenticPixels(image,0,y,image->columns,1,
7355                   exception);
7356
7357               if (q == (PixelPacket *) NULL)
7358                 break;
7359
7360               indexes=GetAuthenticIndexQueue(image);
7361  
7362               for (x=0; x < (ssize_t) image->columns; x++)
7363               {
7364                 for (i=0; i< (ssize_t) image_colors; i++)
7365                 {
7366                   if ((image->matte == MagickFalse ||
7367                       image->colormap[i].opacity == q->opacity) &&
7368                       (IsColorEqual(&image->colormap[i],
7369                          (PixelPacket *) q)))
7370                   {
7371                     indexes[x]=(IndexPacket) i;
7372                     break;
7373                   }
7374                 }
7375                 q++;
7376               }
7377
7378               if (SyncAuthenticPixels(image,exception) == MagickFalse)
7379                  break;
7380             }
7381           }
7382        }
7383
7384      if (logging != MagickFalse)
7385        {
7386          (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7387             "      image->colors=%d", (int) image->colors);
7388
7389          if (image->colormap != NULL)
7390            {
7391              (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7392                  "       i     (red,green,blue,opacity)");
7393
7394              for (i=0; i < (ssize_t) image->colors; i++)
7395              {
7396                if (i < 300 || i >= image->colors - 10)
7397                  {
7398                    (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7399                        "       %d     (%d,%d,%d,%d)",
7400                         (int) i,
7401                         (int) image->colormap[i].red,
7402                         (int) image->colormap[i].green,
7403                         (int) image->colormap[i].blue,
7404                         (int) image->colormap[i].opacity);
7405                  }
7406              }
7407            }
7408
7409            if (number_transparent < 257)
7410              (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7411                    "      number_transparent     = %d",
7412                    number_transparent);
7413            else
7414
7415              (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7416                    "      number_transparent     > 256");
7417
7418            if (number_opaque < 257)
7419              (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7420                    "      number_opaque          = %d",
7421                    number_opaque);
7422
7423            else
7424              (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7425                    "      number_opaque          > 256");
7426
7427            if (number_semitransparent < 257)
7428              (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7429                    "      number_semitransparent = %d",
7430                    number_semitransparent);
7431
7432            else
7433              (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7434                    "      number_semitransparent > 256");
7435
7436            if (ping_have_non_bw == MagickFalse)
7437               (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7438                     "      All pixels and the background are black or white");
7439
7440            else if (ping_have_color == MagickFalse)
7441               (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7442                     "      All pixels and the background are gray");
7443
7444            else
7445               (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7446                     "      At least one pixel or the background is non-gray");
7447
7448            (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7449                "    Exit BUILD_PALETTE:");
7450        }
7451
7452    if (mng_info->write_png8 == MagickFalse)
7453       break;
7454
7455    /* Make any reductions necessary for the PNG8 format */
7456     if (image_colors <= 256 &&
7457         image_colors != 0 && image->colormap != NULL &&
7458         number_semitransparent == 0 &&
7459         number_transparent <= 1)
7460       break;
7461
7462     /* PNG8 can't have semitransparent colors so we threshold the
7463      * opacity to 0 or OpaqueOpacity
7464      */
7465     if (number_semitransparent != 0)
7466       {
7467         (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7468             "    Thresholding the alpha channel to binary");
7469
7470         for (y=0; y < (ssize_t) image->rows; y++)
7471         {
7472           r=GetAuthenticPixels(image,0,y,image->columns,1,
7473               exception);
7474
7475           if (r == (PixelPacket *) NULL)
7476             break;
7477
7478           for (x=0; x < (ssize_t) image->columns; x++)
7479           {
7480               r->opacity = r->opacity > OpaqueOpacity/2 ? 0 :
7481                    OpaqueOpacity;
7482               r++;
7483           }
7484   
7485           if (SyncAuthenticPixels(image,exception) == MagickFalse)
7486              break;
7487
7488           if (image_colors != 0 && image_colors <= 256 &&
7489              image->colormap != NULL)
7490             for (i=0; i<image_colors; i++)
7491                 image->colormap[i].opacity =
7492                     image->colormap[i].opacity > OpaqueOpacity/2 ? 0 :
7493                     OpaqueOpacity;
7494         }
7495       continue;
7496     }
7497
7498     /* PNG8 can't have more than 256 colors so we quantize the pixels and
7499      * background color to the 3-3-2 palette.
7500      */
7501
7502     if (image_colors == 0 || image_colors > 256)
7503       {
7504         if (logging != MagickFalse)
7505            (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7506                "    Quantizing the background color to 3-3-2");
7507
7508         image->background_color.red=
7509             ((((((size_t)
7510             image->background_color.red) >> PNGK) & 0xe0)     )  |
7511             (((((size_t)
7512             image->background_color.red) >> PNGK) & 0xe0) >> 3)  |
7513             (((((size_t)
7514             image->background_color.red) >> PNGK) & 0xc0) >> 6)) * PNGM;
7515         image->background_color.green=
7516             ((((((size_t)
7517             image->background_color.green) >> PNGK) & 0xe0)     )  |
7518             (((((size_t)
7519             image->background_color.green) >> PNGK) & 0xe0) >> 3)  |
7520             (((((size_t)
7521             image->background_color.green) >> PNGK) & 0xc0) >> 6)) * PNGM;
7522         image->background_color.blue=
7523             ((((((size_t)
7524             image->background_color.blue) >> PNGK) & 0xc0)     )  |
7525             (((((size_t)
7526             image->background_color.blue) >> PNGK) & 0xc0) >> 2)  |
7527             (((((size_t)
7528             image->background_color.blue) >> PNGK) & 0xc0) >> 4)  |
7529             (((((size_t)
7530             image->background_color.blue) >> PNGK) & 0xc0) >> 6)) * PNGM;
7531
7532         if (logging != MagickFalse)
7533           (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7534               "    Quantizing the pixel colors to 3-3-2");
7535
7536         if (image->colormap == NULL)
7537         {
7538           for (y=0; y < (ssize_t) image->rows; y++)
7539           {
7540             r=GetAuthenticPixels(image,0,y,image->columns,1,
7541                 exception);
7542
7543             if (r == (PixelPacket *) NULL)
7544               break;
7545
7546             for (x=0; x < (ssize_t) image->columns; x++)
7547             {
7548               r->red=
7549                    ((((((size_t) r->red) >> PNGK) & 0xe0)    )  |
7550                    (((((size_t) r->red) >> PNGK) & 0xe0) >> 3)  |
7551                    (((((size_t) r->red) >> PNGK) & 0xc0) >> 6)) * PNGM;
7552               r->green=
7553                    ((((((size_t) r->green) >> PNGK) & 0xe0)    )  |
7554                    (((((size_t) r->green) >> PNGK) & 0xe0) >> 3)  |
7555                    (((((size_t) r->green) >> PNGK) & 0xc0) >> 6)) * PNGM;
7556               r->blue=
7557                    ((((((size_t) r->blue) >> PNGK) & 0xc0)    )  |
7558                    (((((size_t) r->blue) >> PNGK) & 0xc0) >> 2)  |
7559                    (((((size_t) r->blue) >> PNGK) & 0xc0) >> 4)  |
7560                    (((((size_t) r->blue) >> PNGK) & 0xc0) >> 6)) * PNGM;
7561               r++;
7562             }
7563     
7564             if (SyncAuthenticPixels(image,exception) == MagickFalse)
7565                break;
7566           }
7567         }
7568
7569         else /* Should not reach this; colormap already exists and
7570                 must be <= 256 */
7571         {
7572           if (logging != MagickFalse)
7573               (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7574               "    Quantizing the colormap to 3-3-2");
7575           for (i=0; i<image_colors; i++)
7576           {
7577               image->colormap[i].red=
7578                   ((((((size_t)
7579                   image->colormap[i].red) >> PNGK) & 0xe0)     )  |
7580                   (((((size_t)
7581                   image->colormap[i].red) >> PNGK) & 0xe0) >> 3)  |
7582                   (((((size_t)
7583                   image->colormap[i].red) >> PNGK) & 0xc0) >> 6)) * PNGM;
7584               image->colormap[i].green=
7585                   ((((((size_t)
7586                   image->colormap[i].green) >> PNGK) & 0xe0)     )  |
7587                   (((((size_t)
7588                   image->colormap[i].green) >> PNGK) & 0xe0) >> 3)  |
7589                   (((((size_t)
7590                   image->colormap[i].green) >> PNGK) & 0xc0) >> 6)) * PNGM;
7591               image->colormap[i].blue=
7592                   ((((((size_t)
7593                   image->colormap[i].blue) >> PNGK) & 0xc0)     )  |
7594                   (((((size_t)
7595                   image->colormap[i].blue) >> PNGK) & 0xc0) >> 2)  |
7596                   (((((size_t)
7597                   image->colormap[i].blue) >> PNGK) & 0xc0) >> 4)  |
7598                   (((((size_t)
7599                   image->colormap[i].blue) >> PNGK) & 0xc0) >> 6)) * PNGM;
7600           }
7601       }
7602       continue;
7603     }
7604     break;
7605   }
7606   /* END OF BUILD_PALETTE */
7607
7608   /* If we are excluding the tRNS chunk and there is transparency,
7609    * then we must write a Gray-Alpha (color-type 4) or RGBA (color-type 6)
7610    * PNG.
7611    */
7612   if (mng_info->ping_exclude_tRNS != MagickFalse &&
7613      (number_transparent != 0 || number_semitransparent != 0))
7614     {
7615       int colortype=mng_info->write_png_colortype;
7616
7617       if (ping_have_color == MagickFalse)
7618         mng_info->write_png_colortype = 5;
7619
7620       else
7621         mng_info->write_png_colortype = 7;
7622
7623       if (colortype != 0 &&
7624          mng_info->write_png_colortype != (ssize_t) colortype)
7625         ping_need_colortype_warning=MagickTrue;
7626
7627     }
7628
7629   /* See if cheap transparency is possible.  It is only possible
7630    * when there is a single transparent color, no semitransparent
7631    * color, and no opaque color that has the same RGB components
7632    * as the transparent color.  We only need this information if
7633    * we are writing a PNG with colortype 0 or 2, and we have not
7634    * excluded the tRNS chunk.
7635    */
7636   if (number_transparent == 1 &&
7637       mng_info->write_png_colortype < 4)
7638     {
7639        ping_have_cheap_transparency = MagickTrue;
7640
7641        if (number_semitransparent != 0)
7642          ping_have_cheap_transparency = MagickFalse;
7643
7644        else if (image_colors == 0 || image_colors > 256 ||
7645            image->colormap == NULL)
7646          {
7647            ExceptionInfo
7648              *exception;
7649
7650            register const PixelPacket
7651              *q;
7652
7653            exception=(&image->exception);
7654
7655            for (y=0; y < (ssize_t) image->rows; y++)
7656            {
7657              q=GetVirtualPixels(image,0,y,image->columns,1, exception);
7658
7659              if (q == (PixelPacket *) NULL)
7660                break;
7661
7662              for (x=0; x < (ssize_t) image->columns; x++)
7663              {
7664                  if (q->opacity != TransparentOpacity &&
7665                      (unsigned short) q->red == ping_trans_color.red &&
7666                      (unsigned short) q->green == ping_trans_color.green &&
7667                      (unsigned short) q->blue == ping_trans_color.blue)
7668                    {
7669                      ping_have_cheap_transparency = MagickFalse;
7670                      break;
7671                    }
7672
7673                  q++;
7674              }
7675     
7676              if (ping_have_cheap_transparency == MagickFalse)
7677                 break;
7678            }
7679          }
7680        else
7681          {
7682             /* Assuming that image->colormap[0] is the one transparent color
7683              * and that all others are opaque.
7684              */
7685             if (image_colors > 1)
7686               for (i=1; i<image_colors; i++)
7687                 if (image->colormap[i].red == image->colormap[0].red &&
7688                     image->colormap[i].green == image->colormap[0].green &&
7689                     image->colormap[i].blue == image->colormap[0].blue)
7690                   {
7691                      ping_have_cheap_transparency = MagickFalse;
7692                      break;
7693                   }
7694          }
7695        
7696        if (logging != MagickFalse)
7697          {
7698            if (ping_have_cheap_transparency == MagickFalse)
7699              (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7700                  "   Cheap transparency is not possible.");
7701
7702            else
7703              (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7704                  "   Cheap transparency is possible.");
7705          }
7706      }
7707   else
7708     ping_have_cheap_transparency = MagickFalse;
7709
7710   image_depth=image->depth;
7711
7712   quantum_info = (QuantumInfo *) NULL;
7713   number_colors=0;
7714   image_colors=(int) image->colors;
7715   image_matte=image->matte;
7716
7717   mng_info->IsPalette=image->storage_class == PseudoClass &&
7718     image_colors <= 256 && image->colormap != NULL;
7719
7720   if ((mng_info->write_png_colortype == 4 || mng_info->write_png8) &&
7721      (image->colors == 0 || image->colormap == NULL))
7722     {
7723       image_info=DestroyImageInfo(image_info);
7724       image=DestroyImage(image);
7725       (void) ThrowMagickException(&IMimage->exception,
7726           GetMagickModule(),CoderError,
7727           "Cannot write PNG8 or color-type 3; colormap is NULL",
7728           "`%s'",IMimage->filename);
7729 #if defined(PNG_SETJMP_NOT_THREAD_SAFE)
7730       UnlockSemaphoreInfo(ping_semaphore);
7731 #endif
7732       return(MagickFalse);
7733     }
7734
7735   /*
7736     Allocate the PNG structures
7737   */
7738 #ifdef PNG_USER_MEM_SUPPORTED
7739   ping=png_create_write_struct_2(PNG_LIBPNG_VER_STRING,image,
7740     MagickPNGErrorHandler,MagickPNGWarningHandler,(void *) NULL,
7741     (png_malloc_ptr) Magick_png_malloc,(png_free_ptr) Magick_png_free);
7742
7743 #else
7744   ping=png_create_write_struct(PNG_LIBPNG_VER_STRING,image,
7745     MagickPNGErrorHandler,MagickPNGWarningHandler);
7746
7747 #endif
7748   if (ping == (png_struct *) NULL)
7749     ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
7750
7751   ping_info=png_create_info_struct(ping);
7752
7753   if (ping_info == (png_info *) NULL)
7754     {
7755       png_destroy_write_struct(&ping,(png_info **) NULL);
7756       ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
7757     }
7758
7759   png_set_write_fn(ping,image,png_put_data,png_flush_data);
7760   ping_pixels=(unsigned char *) NULL;
7761
7762   if (setjmp(png_jmpbuf(ping)))
7763     {
7764       /*
7765         PNG write failed.
7766       */
7767 #ifdef PNG_DEBUG
7768      if (image_info->verbose)
7769         (void) printf("PNG write has failed.\n");
7770 #endif
7771       png_destroy_write_struct(&ping,&ping_info);
7772 #if defined(PNG_SETJMP_NOT_THREAD_SAFE)
7773       UnlockSemaphoreInfo(ping_semaphore);
7774 #endif
7775       if (ping_have_blob != MagickFalse)
7776           (void) CloseBlob(image);
7777       image_info=DestroyImageInfo(image_info);
7778       image=DestroyImage(image);
7779       (void) ThrowMagickException(&IMimage->exception,
7780           GetMagickModule(),CoderError,
7781           "PNG write has failed","`%s'",IMimage->filename);
7782       return(MagickFalse);
7783     }
7784   /*
7785     Prepare PNG for writing.
7786   */
7787 #if defined(PNG_MNG_FEATURES_SUPPORTED)
7788   if (mng_info->write_mng)
7789      (void) png_permit_mng_features(ping,PNG_ALL_MNG_FEATURES);
7790
7791 #else
7792 # ifdef PNG_WRITE_EMPTY_PLTE_SUPPORTED
7793   if (mng_info->write_mng)
7794      png_permit_empty_plte(ping,MagickTrue);
7795
7796 # endif
7797 #endif
7798
7799   x=0;
7800
7801   ping_width=(png_uint_32) image->columns;
7802   ping_height=(png_uint_32) image->rows;
7803
7804   if (mng_info->write_png8 || mng_info->write_png24 || mng_info->write_png32)
7805      image_depth=8;
7806
7807   if (mng_info->write_png_depth != 0)
7808      image_depth=mng_info->write_png_depth;
7809
7810   /* Adjust requested depth to next higher valid depth if necessary */
7811   if (image_depth > 8)
7812      image_depth=16;
7813
7814   if ((image_depth > 4) && (image_depth < 8))
7815      image_depth=8;
7816
7817   if (image_depth == 3)
7818      image_depth=4;
7819
7820   if (logging != MagickFalse)
7821     {
7822      (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7823         "    width=%.20g",(double) ping_width);
7824      (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7825         "    height=%.20g",(double) ping_height);
7826      (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7827         "    image_matte=%.20g",(double) image->matte);
7828      (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7829         "    image->depth=%.20g",(double) image->depth);
7830      (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7831         "    Tentative ping_bit_depth=%.20g",(double) image_depth);
7832     }
7833
7834   save_image_depth=image_depth;
7835   ping_bit_depth=(png_byte) save_image_depth;
7836
7837
7838 #if defined(PNG_pHYs_SUPPORTED)
7839   if (ping_exclude_pHYs == MagickFalse)
7840   {
7841   if ((image->x_resolution != 0) && (image->y_resolution != 0) &&
7842       (!mng_info->write_mng || !mng_info->equal_physs))
7843     {
7844       if (logging != MagickFalse)
7845         (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7846             "    Setting up pHYs chunk");
7847
7848       if (image->units == PixelsPerInchResolution)
7849         {
7850           ping_pHYs_unit_type=PNG_RESOLUTION_METER;
7851           ping_pHYs_x_resolution=(png_uint_32) (100.0*image->x_resolution/2.54);
7852           ping_pHYs_y_resolution=(png_uint_32) (100.0*image->y_resolution/2.54);
7853         }
7854
7855       else if (image->units == PixelsPerCentimeterResolution)
7856         {
7857           ping_pHYs_unit_type=PNG_RESOLUTION_METER;
7858           ping_pHYs_x_resolution=(png_uint_32) (100.0*image->x_resolution);
7859           ping_pHYs_y_resolution=(png_uint_32) (100.0*image->y_resolution);
7860         }
7861
7862       else
7863         {
7864           ping_pHYs_unit_type=PNG_RESOLUTION_UNKNOWN;
7865           ping_pHYs_x_resolution=(png_uint_32) image->x_resolution;
7866           ping_pHYs_y_resolution=(png_uint_32) image->y_resolution;
7867         }
7868
7869        ping_have_pHYs = MagickTrue;
7870     }
7871   }
7872 #endif
7873
7874   if (ping_exclude_bKGD == MagickFalse)
7875   {
7876   if ((!mng_info->adjoin || !mng_info->equal_backgrounds))
7877     {
7878        unsigned int
7879          mask;
7880
7881        mask=0xffff;
7882        if (ping_bit_depth == 8)
7883           mask=0x00ff;
7884
7885        if (ping_bit_depth == 4)
7886           mask=0x000f;
7887
7888        if (ping_bit_depth == 2)
7889           mask=0x0003;
7890
7891        if (ping_bit_depth == 1)
7892           mask=0x0001;
7893
7894        ping_background.red=(png_uint_16)
7895          (ScaleQuantumToShort(image->background_color.red) & mask);
7896
7897        ping_background.green=(png_uint_16)
7898          (ScaleQuantumToShort(image->background_color.green) & mask);
7899
7900        ping_background.blue=(png_uint_16)
7901          (ScaleQuantumToShort(image->background_color.blue) & mask);
7902     }
7903
7904   if (logging != MagickFalse)
7905     {
7906       (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7907           "    Setting up bKGD chunk (1)");
7908
7909       (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7910           "    ping_bit_depth=%d",ping_bit_depth);
7911     }
7912
7913   ping_have_bKGD = MagickTrue;
7914   }
7915
7916   /*
7917     Select the color type.
7918   */
7919   matte=image_matte;
7920   old_bit_depth=0;
7921
7922   if (mng_info->IsPalette && mng_info->write_png8)
7923     {
7924
7925       /* To do: make this a function cause it's used twice, except
7926          for reducing the sample depth from 8. */
7927
7928       number_colors=image_colors;
7929
7930       ping_have_tRNS=MagickFalse;
7931
7932       /*
7933         Set image palette.
7934       */
7935       ping_color_type=(png_byte) PNG_COLOR_TYPE_PALETTE;
7936
7937       if (logging != MagickFalse)
7938         (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7939             "  Setting up PLTE chunk with %d colors (%d)",
7940             number_colors, image_colors);
7941
7942       for (i=0; i < (ssize_t) number_colors; i++)
7943       {
7944         palette[i].red=ScaleQuantumToChar(image->colormap[i].red);
7945         palette[i].green=ScaleQuantumToChar(image->colormap[i].green);
7946         palette[i].blue=ScaleQuantumToChar(image->colormap[i].blue);
7947         if (logging != MagickFalse)
7948           (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7949 #if MAGICKCORE_QUANTUM_DEPTH == 8
7950             "    %3ld (%3d,%3d,%3d)",
7951 #else
7952             "    %5ld (%5d,%5d,%5d)",
7953 #endif
7954             (long) i,palette[i].red,palette[i].green,palette[i].blue);
7955
7956       }
7957
7958       ping_have_PLTE=MagickTrue;
7959       image_depth=ping_bit_depth;
7960       ping_num_trans=0;
7961
7962       if (matte != MagickFalse)
7963       {
7964           /*
7965             Identify which colormap entry is transparent.
7966           */
7967           assert(number_colors <= 256);
7968           assert(image->colormap != NULL);
7969
7970           for (i=0; i < (ssize_t) number_transparent; i++)
7971              ping_trans_alpha[i]=0;
7972
7973
7974           ping_num_trans=(unsigned short) (number_transparent +
7975              number_semitransparent);
7976
7977           if (ping_num_trans == 0)
7978              ping_have_tRNS=MagickFalse;
7979
7980           else
7981              ping_have_tRNS=MagickTrue;
7982       }
7983
7984       if (ping_exclude_bKGD == MagickFalse)
7985       {
7986        /*
7987         * Identify which colormap entry is the background color.
7988         */
7989
7990         for (i=0; i < (ssize_t) MagickMax(1L*number_colors-1L,1L); i++)
7991           if (IsPNGColorEqual(ping_background,image->colormap[i]))
7992             break;
7993
7994         ping_background.index=(png_byte) i;
7995       }
7996     } /* end of write_png8 */
7997
7998   else if (mng_info->write_png24)
7999     {
8000       image_matte=MagickFalse;
8001       ping_color_type=(png_byte) PNG_COLOR_TYPE_RGB;
8002     }
8003
8004   else if (mng_info->write_png32)
8005     {
8006       image_matte=MagickTrue;
8007       ping_color_type=(png_byte) PNG_COLOR_TYPE_RGB_ALPHA;
8008     }
8009
8010   else /* mng_info->write_pngNN not specified */
8011     {
8012       image_depth=ping_bit_depth;
8013
8014       if (mng_info->write_png_colortype != 0)
8015         {
8016           ping_color_type=(png_byte) mng_info->write_png_colortype-1;
8017
8018           if (ping_color_type == PNG_COLOR_TYPE_GRAY_ALPHA ||
8019               ping_color_type == PNG_COLOR_TYPE_RGB_ALPHA)
8020             image_matte=MagickTrue;
8021
8022           else
8023             image_matte=MagickFalse;
8024         }
8025
8026       else /* write_ping_colortype not specified */
8027         {
8028           if (logging != MagickFalse)
8029              (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8030              "  Selecting PNG colortype:");
8031
8032           ping_color_type=(png_byte) ((matte != MagickFalse)?
8033             PNG_COLOR_TYPE_RGB_ALPHA:PNG_COLOR_TYPE_RGB);
8034
8035           if (image_info->type == TrueColorType)
8036             {
8037               ping_color_type=(png_byte) PNG_COLOR_TYPE_RGB;
8038               image_matte=MagickFalse;
8039             }
8040
8041           if (image_info->type == TrueColorMatteType)
8042             {
8043               ping_color_type=(png_byte) PNG_COLOR_TYPE_RGB_ALPHA;
8044               image_matte=MagickTrue;
8045             }
8046
8047           if (image_info->type == PaletteType ||
8048               image_info->type == PaletteMatteType)
8049             ping_color_type=(png_byte) PNG_COLOR_TYPE_PALETTE;
8050
8051           if (image_info->type == UndefinedType ||
8052              image_info->type == OptimizeType)
8053             {
8054               if (ping_have_color == MagickFalse)
8055                 {
8056                   if (image_matte == MagickFalse)
8057                     {
8058                       ping_color_type=(png_byte) PNG_COLOR_TYPE_GRAY;
8059                       image_matte=MagickFalse;
8060                     }
8061
8062                   else
8063                     {
8064                       ping_color_type=(png_byte) PNG_COLOR_TYPE_GRAY_ALPHA;
8065                       image_matte=MagickTrue;
8066                     }
8067                 }
8068               else
8069                 {
8070                   if (image_matte == MagickFalse)
8071                     {
8072                       ping_color_type=(png_byte) PNG_COLOR_TYPE_RGB;
8073                       image_matte=MagickFalse;
8074                     }
8075
8076                   else
8077                     {
8078                       ping_color_type=(png_byte) PNG_COLOR_TYPE_RGBA;
8079                       image_matte=MagickTrue;
8080                     }
8081                  }
8082             }
8083
8084         }
8085
8086       if (logging != MagickFalse)
8087          (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8088          "    Selected PNG colortype=%d",ping_color_type);
8089
8090       if (ping_bit_depth < 8)
8091         {
8092           if (ping_color_type == PNG_COLOR_TYPE_GRAY_ALPHA ||
8093               ping_color_type == PNG_COLOR_TYPE_RGB ||
8094               ping_color_type == PNG_COLOR_TYPE_RGB_ALPHA)
8095             ping_bit_depth=8;
8096         }
8097
8098       old_bit_depth=ping_bit_depth;
8099
8100       if (ping_color_type == PNG_COLOR_TYPE_GRAY)
8101         {
8102           if (image->matte == MagickFalse && ping_have_non_bw == MagickFalse)
8103              ping_bit_depth=1;
8104         }
8105
8106       if (ping_color_type == PNG_COLOR_TYPE_PALETTE)
8107         {
8108            size_t one = 1;
8109            ping_bit_depth=1;
8110
8111            if (image->colors == 0)
8112            {
8113               /* DO SOMETHING */
8114               (void) ThrowMagickException(&IMimage->exception,
8115                  GetMagickModule(),CoderError,
8116                 "image has 0 colors", "`%s'",IMimage->filename);
8117            }
8118
8119            while ((int) (one << ping_bit_depth) < (ssize_t) image_colors)
8120              ping_bit_depth <<= 1;
8121         }
8122
8123       if (logging != MagickFalse)
8124          {
8125            (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8126             "    Number of colors: %.20g",(double) image_colors);
8127
8128            (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8129             "    Tentative PNG bit depth: %d",ping_bit_depth);
8130          }
8131
8132       if (ping_bit_depth < (int) mng_info->write_png_depth)
8133          ping_bit_depth = mng_info->write_png_depth;
8134     }
8135
8136   image_depth=ping_bit_depth;
8137
8138   if (logging != MagickFalse)
8139     {
8140       (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8141         "    Tentative PNG color type: %.20g",(double) ping_color_type);
8142
8143       (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8144         "    image_info->type: %.20g",(double) image_info->type);
8145
8146       (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8147         "    image_depth: %.20g",(double) image_depth);
8148
8149       (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8150
8151         "    image->depth: %.20g",(double) image->depth);
8152
8153       (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8154         "    ping_bit_depth: %.20g",(double) ping_bit_depth);
8155     }
8156
8157   if (matte != MagickFalse)
8158     {
8159       if (mng_info->IsPalette)
8160         {
8161           ping_color_type=PNG_COLOR_TYPE_GRAY_ALPHA;
8162
8163           if (ping_have_color != MagickFalse)
8164              ping_color_type=PNG_COLOR_TYPE_RGBA;
8165
8166           /*
8167            * Determine if there is any transparent color.
8168           */
8169           if (number_transparent + number_semitransparent == 0)
8170             {
8171               /*
8172                 No transparent pixels are present.  Change 4 or 6 to 0 or 2.
8173               */
8174
8175               image_matte=MagickFalse;
8176               ping_color_type&=0x03;
8177             }
8178
8179           else
8180             {
8181               unsigned int
8182                 mask; 
8183
8184               mask=0xffff;
8185
8186               if (ping_bit_depth == 8)
8187                  mask=0x00ff;
8188
8189               if (ping_bit_depth == 4)
8190                  mask=0x000f;
8191
8192               if (ping_bit_depth == 2)
8193                  mask=0x0003;
8194
8195               if (ping_bit_depth == 1)
8196                  mask=0x0001;
8197
8198               ping_trans_color.red=(png_uint_16)
8199                 (ScaleQuantumToShort(image->colormap[0].red) & mask);
8200
8201               ping_trans_color.green=(png_uint_16)
8202                 (ScaleQuantumToShort(image->colormap[0].green) & mask);
8203
8204               ping_trans_color.blue=(png_uint_16)
8205                 (ScaleQuantumToShort(image->colormap[0].blue) & mask);
8206
8207               ping_trans_color.gray=(png_uint_16)
8208                 (ScaleQuantumToShort(PixelIntensityToQuantum(
8209                    image->colormap)) & mask);
8210
8211               ping_trans_color.index=(png_byte) 0;
8212
8213               ping_have_tRNS=MagickTrue;
8214             }
8215
8216           if (ping_have_tRNS != MagickFalse)
8217             {
8218               /*
8219                * Determine if there is one and only one transparent color
8220                * and if so if it is fully transparent.
8221                */
8222               if (ping_have_cheap_transparency == MagickFalse)
8223                 ping_have_tRNS=MagickFalse;
8224             }
8225
8226           if (ping_have_tRNS != MagickFalse)
8227             {
8228               ping_color_type &= 0x03;  /* changes 4 or 6 to 0 or 2 */
8229
8230               if (image_depth == 8)
8231                 {
8232                   ping_trans_color.red&=0xff;
8233                   ping_trans_color.green&=0xff;
8234                   ping_trans_color.blue&=0xff;
8235                   ping_trans_color.gray&=0xff;
8236                 }
8237             }
8238         }
8239       else
8240         {
8241           if (image_depth == 8)
8242             {
8243               ping_trans_color.red&=0xff;
8244               ping_trans_color.green&=0xff;
8245               ping_trans_color.blue&=0xff;
8246               ping_trans_color.gray&=0xff;
8247             }
8248         }
8249     }
8250
8251     matte=image_matte;
8252
8253     if (ping_have_tRNS != MagickFalse)
8254       image_matte=MagickFalse;
8255
8256     if ((mng_info->IsPalette) &&
8257         mng_info->write_png_colortype-1 != PNG_COLOR_TYPE_PALETTE &&
8258         ping_have_color == MagickFalse &&
8259         (image_matte == MagickFalse || image_depth >= 8))
8260       {
8261         size_t one=1;
8262
8263         if (image_matte != MagickFalse)
8264           ping_color_type=PNG_COLOR_TYPE_GRAY_ALPHA;
8265
8266         else
8267           {
8268             ping_color_type=PNG_COLOR_TYPE_GRAY;
8269
8270             if (save_image_depth == 16 && image_depth == 8)
8271               {
8272                 if (logging != MagickFalse)
8273                   {
8274                     (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8275                         "  Scaling ping_trans_color (0)");
8276                   }
8277                     ping_trans_color.gray*=0x0101;
8278               }
8279           }
8280
8281         if (image_depth > MAGICKCORE_QUANTUM_DEPTH)
8282           image_depth=MAGICKCORE_QUANTUM_DEPTH;
8283
8284         if (image_colors == 0 || image_colors-1 > MaxColormapSize)
8285           image_colors=(int) (one << image_depth);
8286
8287         if (image_depth > 8)
8288           ping_bit_depth=16;
8289
8290         else
8291           {
8292             ping_bit_depth=8;
8293             if ((int) ping_color_type == PNG_COLOR_TYPE_PALETTE)
8294               {
8295                 if(!mng_info->write_png_depth)
8296                   {
8297                     ping_bit_depth=1;
8298
8299                     while ((int) (one << ping_bit_depth)
8300                         < (ssize_t) image_colors)
8301                       ping_bit_depth <<= 1;
8302                   }
8303               }
8304
8305 #if 1 /* To do: Enable this when low bit-depth grayscale is working */
8306             else if (ping_color_type ==
8307                 PNG_COLOR_TYPE_GRAY && image_colors < 17 &&
8308                 mng_info->IsPalette)
8309               {
8310
8311               /* Check if grayscale is reducible */
8312                 int
8313                   depth_4_ok=MagickTrue,
8314                   depth_2_ok=MagickTrue,
8315                   depth_1_ok=MagickTrue;
8316
8317                 for (i=0; i < (ssize_t) image_colors; i++)
8318                 {
8319                    unsigned char
8320                      intensity;
8321
8322                    intensity=ScaleQuantumToChar(image->colormap[i].red);
8323
8324                    if ((intensity & 0x0f) != ((intensity & 0xf0) >> 4))
8325                      depth_4_ok=depth_2_ok=depth_1_ok=MagickFalse;
8326
8327                    else if ((intensity & 0x03) != ((intensity & 0x0c) >> 2))
8328                      depth_2_ok=depth_1_ok=MagickFalse;
8329
8330                    else if ((intensity & 0x01) != ((intensity & 0x02) >> 1))
8331                      depth_1_ok=MagickFalse;
8332                 }
8333
8334                 if (depth_1_ok && mng_info->write_png_depth <= 1)
8335                   ping_bit_depth=1;
8336
8337 #if 0 /* To do: Enable this when bit depths 2 and 4 are working. */
8338                 else if (depth_2_ok && mng_info->write_png_depth <= 2)
8339                   ping_bit_depth=2;
8340
8341                 else if (depth_4_ok && mng_info->write_png_depth <= 4)
8342                   ping_bit_depth=4;
8343 #endif
8344               }
8345 #endif /* 1 */
8346           }
8347
8348           image_depth=ping_bit_depth;
8349       }
8350
8351     else
8352
8353       if (mng_info->IsPalette)
8354       {
8355         number_colors=image_colors;
8356
8357         if (image_depth <= 8)
8358           {
8359             /*
8360               Set image palette.
8361             */
8362             ping_color_type=(png_byte) PNG_COLOR_TYPE_PALETTE;
8363
8364             if (mng_info->have_write_global_plte && matte == MagickFalse)
8365               {
8366                 png_set_PLTE(ping,ping_info,NULL,0);
8367
8368                 if (logging != MagickFalse)
8369                   (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8370                     "  Setting up empty PLTE chunk");
8371               }
8372
8373             else
8374               {
8375                 for (i=0; i < (ssize_t) number_colors; i++)
8376                 {
8377                   palette[i].red=ScaleQuantumToChar(image->colormap[i].red);
8378                   palette[i].green=ScaleQuantumToChar(image->colormap[i].green);
8379                   palette[i].blue=ScaleQuantumToChar(image->colormap[i].blue);
8380                 }
8381
8382                 if (logging != MagickFalse)
8383                   (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8384                     "  Setting up PLTE chunk with %d colors",
8385                     number_colors);
8386
8387                 ping_have_PLTE=MagickTrue;
8388               }
8389
8390             /* color_type is PNG_COLOR_TYPE_PALETTE */
8391             if (mng_info->write_png_depth == 0)
8392               {
8393                 size_t
8394                   one;
8395
8396                 ping_bit_depth=1;
8397                 one=1;
8398
8399                 while ((one << ping_bit_depth) < number_colors)
8400                   ping_bit_depth <<= 1;
8401               }
8402
8403             ping_num_trans=0;
8404
8405             if (matte != MagickFalse)
8406               {
8407                 /*
8408                  * Set up trans_colors array.
8409                  */
8410                 assert(number_colors <= 256);
8411
8412                 ping_num_trans=(unsigned short) (number_transparent +
8413                   number_semitransparent);
8414
8415                 if (ping_num_trans == 0)
8416                   ping_have_tRNS=MagickFalse;
8417
8418                 else
8419                   {
8420                     if (logging != MagickFalse)
8421                       {
8422                         (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8423                           "  Scaling ping_trans_color (1)");
8424                       }
8425                     ping_have_tRNS=MagickTrue;
8426
8427                     for (i=0; i < ping_num_trans; i++)
8428                     {
8429                        ping_trans_alpha[i]= (png_byte) (255-
8430                           ScaleQuantumToChar(image->colormap[i].opacity));
8431                     }
8432                   }
8433               }
8434           }
8435       }
8436
8437     else
8438       {
8439
8440         if (image_depth < 8)
8441           image_depth=8;
8442
8443         if ((save_image_depth == 16) && (image_depth == 8))
8444           {
8445             if (logging != MagickFalse)
8446               {
8447                 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8448                   "    Scaling ping_trans_color from (%d,%d,%d)",
8449                   (int) ping_trans_color.red,
8450                   (int) ping_trans_color.green,
8451                   (int) ping_trans_color.blue);
8452               }
8453
8454             ping_trans_color.red*=0x0101;
8455             ping_trans_color.green*=0x0101;
8456             ping_trans_color.blue*=0x0101;
8457             ping_trans_color.gray*=0x0101;
8458
8459             if (logging != MagickFalse)
8460               {
8461                 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8462                   "    to (%d,%d,%d)",
8463                   (int) ping_trans_color.red,
8464                   (int) ping_trans_color.green,
8465                   (int) ping_trans_color.blue);
8466               }
8467           }
8468       }
8469
8470     if (ping_bit_depth <  (ssize_t) mng_info->write_png_depth)
8471          ping_bit_depth =  (ssize_t) mng_info->write_png_depth;
8472
8473     /*
8474       Adjust background and transparency samples in sub-8-bit grayscale files.
8475     */
8476     if (ping_bit_depth < 8 && ping_color_type ==
8477         PNG_COLOR_TYPE_GRAY)
8478       {
8479          png_uint_16
8480            maxval;
8481
8482          size_t
8483            one=1;
8484
8485          maxval=(png_uint_16) ((one << ping_bit_depth)-1);
8486
8487          if (ping_exclude_bKGD == MagickFalse)
8488          {
8489
8490          ping_background.gray=(png_uint_16)
8491            (QuantumScale*(maxval*(PixelIntensity(&image->background_color))));
8492
8493          if (logging != MagickFalse)
8494            (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8495              "  Setting up bKGD chunk (2)");
8496
8497          ping_have_bKGD = MagickTrue;
8498          }
8499
8500          ping_trans_color.gray=(png_uint_16) (QuantumScale*(maxval*
8501            ping_trans_color.gray));
8502       }
8503
8504   if (ping_exclude_bKGD == MagickFalse)
8505   {
8506     if (mng_info->IsPalette && (int) ping_color_type == PNG_COLOR_TYPE_PALETTE)
8507       {
8508         /*
8509            Identify which colormap entry is the background color.
8510         */
8511
8512         number_colors=image_colors;
8513
8514         for (i=0; i < (ssize_t) MagickMax(1L*number_colors,1L); i++)
8515           if (IsPNGColorEqual(image->background_color,image->colormap[i]))
8516             break;
8517
8518         ping_background.index=(png_byte) i;
8519
8520         if (logging != MagickFalse)
8521           {
8522             (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8523               "  Setting up bKGD chunk with index=%d",(int) i);
8524           }
8525
8526         if (i < (ssize_t) number_colors)
8527           {
8528             ping_have_bKGD = MagickTrue;
8529
8530             if (logging != MagickFalse)
8531               {
8532                 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8533                   "     background   =(%d,%d,%d)",
8534                         (int) ping_background.red,
8535                         (int) ping_background.green,
8536                         (int) ping_background.blue);
8537               }
8538           }
8539
8540         else  /* Can't happen */
8541           {
8542             if (logging != MagickFalse)
8543               (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8544                   "      No room in PLTE to add bKGD color");
8545             ping_have_bKGD = MagickFalse;
8546           }
8547       }
8548   }
8549
8550   if (logging != MagickFalse)
8551     (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8552       "    PNG color type: %d",ping_color_type);
8553   /*
8554     Initialize compression level and filtering.
8555   */
8556   if (logging != MagickFalse)
8557     {
8558       (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8559         "  Setting up deflate compression");
8560
8561       (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8562         "    Compression buffer size: 32768");
8563     }
8564
8565   png_set_compression_buffer_size(ping,32768L);
8566
8567   if (logging != MagickFalse)
8568     (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8569       "    Compression mem level: 9");
8570
8571   png_set_compression_mem_level(ping, 9);
8572
8573   quality=image->quality == UndefinedCompressionQuality ? 75UL :
8574      image->quality;
8575
8576   if (quality > 9)
8577     {
8578       int
8579         level;
8580
8581       level=(int) MagickMin((ssize_t) quality/10,9);
8582
8583       if (logging != MagickFalse)
8584         (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8585           "    Compression level: %d",level);
8586
8587       png_set_compression_level(ping,level);
8588     }
8589
8590   else
8591     {
8592       if (logging != MagickFalse)
8593         (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8594           "    Compression strategy: Z_HUFFMAN_ONLY");
8595
8596       png_set_compression_strategy(ping, Z_HUFFMAN_ONLY);
8597     }
8598
8599   if (logging != MagickFalse)
8600     (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8601       "  Setting up filtering");
8602
8603 #if defined(PNG_MNG_FEATURES_SUPPORTED) && defined(PNG_INTRAPIXEL_DIFFERENCING)
8604   /* This became available in libpng-1.0.9.  Output must be a MNG. */
8605   if (mng_info->write_mng && ((quality % 10) == 7))
8606     {
8607       if (logging != MagickFalse)
8608         (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8609           "    Filter_type: PNG_INTRAPIXEL_DIFFERENCING");
8610
8611       ping_filter_method=PNG_INTRAPIXEL_DIFFERENCING;
8612     }
8613
8614   else
8615     if (logging != MagickFalse)
8616       (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8617         "    Filter_type: 0");
8618 #endif
8619
8620   {
8621     int
8622       base_filter;
8623
8624     if ((quality % 10) > 5)
8625       base_filter=PNG_ALL_FILTERS;
8626
8627     else
8628       if ((quality % 10) != 5)
8629         base_filter=(int) quality % 10;
8630
8631       else
8632         if (((int) ping_color_type == PNG_COLOR_TYPE_GRAY) ||
8633             ((int) ping_color_type == PNG_COLOR_TYPE_PALETTE) ||
8634             (quality < 50))
8635           base_filter=PNG_NO_FILTERS;
8636
8637         else
8638           base_filter=PNG_ALL_FILTERS;
8639
8640     if (logging != MagickFalse)
8641       {
8642         if (base_filter == PNG_ALL_FILTERS)
8643           (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8644             "    Base filter method: ADAPTIVE");
8645         else
8646           (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8647             "    Base filter method: NONE");
8648       }
8649
8650     png_set_filter(ping,PNG_FILTER_TYPE_BASE,base_filter);
8651   }
8652
8653   if (ping_exclude_iCCP == MagickFalse || ping_exclude_zCCP == MagickFalse)
8654     {
8655       ResetImageProfileIterator(image);
8656       for (name=GetNextImageProfile(image); name != (const char *) NULL; )
8657       {
8658         profile=GetImageProfile(image,name);
8659
8660         if (profile != (StringInfo *) NULL)
8661           {
8662 #ifdef PNG_WRITE_iCCP_SUPPORTED
8663             if ((LocaleCompare(name,"ICC") == 0) ||
8664                 (LocaleCompare(name,"ICM") == 0))
8665              {
8666
8667                if (ping_exclude_iCCP == MagickFalse)
8668                  {
8669                        png_set_iCCP(ping,ping_info,(const png_charp) name,0,
8670 #if (PNG_LIBPNG_VER < 10500)
8671                          (png_charp) GetStringInfoDatum(profile),
8672 #else
8673                          (png_const_bytep) GetStringInfoDatum(profile),
8674 #endif
8675                          (png_uint_32) GetStringInfoLength(profile));
8676                  }
8677              }
8678
8679             else
8680 #endif
8681               if (ping_exclude_zCCP == MagickFalse)
8682                 {
8683                   Magick_png_write_raw_profile(image_info,ping,ping_info,
8684                     (unsigned char *) name,(unsigned char *) name,
8685                     GetStringInfoDatum(profile),
8686                     (png_uint_32) GetStringInfoLength(profile));
8687                 }
8688           }
8689
8690           if (logging != MagickFalse)
8691             (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8692               "  Setting up text chunk with %s profile",name);
8693
8694         name=GetNextImageProfile(image);
8695       }
8696   }
8697
8698 #if defined(PNG_WRITE_sRGB_SUPPORTED)
8699   if ((mng_info->have_write_global_srgb == 0) &&
8700       ((image->rendering_intent != UndefinedIntent) ||
8701       (image->colorspace == sRGBColorspace)))
8702     {
8703       if (ping_exclude_sRGB == MagickFalse)
8704         {
8705           /*
8706             Note image rendering intent.
8707           */
8708           if (logging != MagickFalse)
8709             (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8710                 "  Setting up sRGB chunk");
8711
8712           (void) png_set_sRGB(ping,ping_info,(
8713             Magick_RenderingIntent_to_PNG_RenderingIntent(
8714               image->rendering_intent)));
8715
8716           if (ping_exclude_gAMA == MagickFalse)
8717             png_set_gAMA(ping,ping_info,0.45455);
8718         }
8719     }
8720
8721   if ((!mng_info->write_mng) || (!png_get_valid(ping,ping_info,PNG_INFO_sRGB)))
8722 #endif
8723     {
8724       if (ping_exclude_gAMA == MagickFalse &&
8725           (ping_exclude_sRGB == MagickFalse ||
8726           (image->gamma < .45 || image->gamma > .46)))
8727       {
8728       if ((mng_info->have_write_global_gama == 0) && (image->gamma != 0.0))
8729         {
8730           /*
8731             Note image gamma.
8732             To do: check for cHRM+gAMA == sRGB, and write sRGB instead.
8733           */
8734           if (logging != MagickFalse)
8735             (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8736               "  Setting up gAMA chunk");
8737
8738           png_set_gAMA(ping,ping_info,image->gamma);
8739         }
8740       }
8741
8742       if (ping_exclude_cHRM == MagickFalse)
8743         {
8744           if ((mng_info->have_write_global_chrm == 0) &&
8745               (image->chromaticity.red_primary.x != 0.0))
8746             {
8747               /*
8748                 Note image chromaticity.
8749                 To do: check for cHRM+gAMA == sRGB, and write sRGB instead.
8750               */
8751                PrimaryInfo
8752                  bp,
8753                  gp,
8754                  rp,
8755                  wp;
8756
8757                wp=image->chromaticity.white_point;
8758                rp=image->chromaticity.red_primary;
8759                gp=image->chromaticity.green_primary;
8760                bp=image->chromaticity.blue_primary;
8761
8762                if (logging != MagickFalse)
8763                  (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8764                    "  Setting up cHRM chunk");
8765
8766                png_set_cHRM(ping,ping_info,wp.x,wp.y,rp.x,rp.y,gp.x,gp.y,
8767                    bp.x,bp.y);
8768            }
8769         }
8770     }
8771
8772   ping_interlace_method=image_info->interlace != NoInterlace;
8773
8774   if (mng_info->write_mng)
8775     png_set_sig_bytes(ping,8);
8776
8777   /* Bail out if cannot meet defined PNG:bit-depth or PNG:color-type */
8778
8779   if (mng_info->write_png_colortype != 0)
8780     {
8781      if (mng_info->write_png_colortype-1 == PNG_COLOR_TYPE_GRAY)
8782        if (ping_have_color != MagickFalse)
8783          {
8784            ping_color_type = PNG_COLOR_TYPE_RGB;
8785
8786            if (ping_bit_depth < 8)
8787              ping_bit_depth=8;
8788          }
8789
8790      if (mng_info->write_png_colortype-1 == PNG_COLOR_TYPE_GRAY_ALPHA)
8791        if (ping_have_color != MagickFalse)
8792          ping_color_type = PNG_COLOR_TYPE_RGB_ALPHA;
8793     }
8794
8795   if (ping_need_colortype_warning != MagickFalse ||
8796      ((mng_info->write_png_depth &&
8797      (int) mng_info->write_png_depth != ping_bit_depth) ||
8798      (mng_info->write_png_colortype &&
8799      ((int) mng_info->write_png_colortype-1 != ping_color_type &&
8800       mng_info->write_png_colortype != 7 &&
8801       !(mng_info->write_png_colortype == 5 && ping_color_type == 0)))))
8802     {
8803       if (logging != MagickFalse)
8804         {
8805           if (ping_need_colortype_warning != MagickFalse)
8806             {
8807               (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8808                  "  Image has transparency but tRNS chunk was excluded");
8809             }
8810
8811           if (mng_info->write_png_depth)
8812             {
8813               (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8814                   "  Defined PNG:bit-depth=%u, Computed depth=%u",
8815                   mng_info->write_png_depth,
8816                   ping_bit_depth);
8817             }
8818
8819           if (mng_info->write_png_colortype)
8820             {
8821               (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8822                   "  Defined PNG:color-type=%u, Computed color type=%u",
8823                   mng_info->write_png_colortype-1,
8824                   ping_color_type);
8825             }
8826         }
8827
8828       png_warning(ping,
8829         "Cannot write image with defined PNG:bit-depth or PNG:color-type.");
8830     }
8831
8832   if (image_matte != MagickFalse && image->matte == MagickFalse)
8833     {
8834       /* Add an opaque matte channel */
8835       image->matte = MagickTrue;
8836       (void) SetImageOpacity(image,0);
8837
8838       if (logging != MagickFalse)
8839         (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8840           "  Added an opaque matte channel");
8841     }
8842
8843   if (number_transparent != 0 || number_semitransparent != 0)
8844     {
8845       if (ping_color_type < 4)
8846         {
8847            ping_have_tRNS=MagickTrue;
8848            if (logging != MagickFalse)
8849              (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8850                "  Setting ping_have_tRNS=MagickTrue.");
8851         }
8852     }
8853
8854   if (logging != MagickFalse)
8855     (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8856       "  Writing PNG header chunks");
8857
8858   png_set_IHDR(ping,ping_info,ping_width,ping_height,
8859                ping_bit_depth,ping_color_type,
8860                ping_interlace_method,ping_compression_method,
8861                ping_filter_method);
8862
8863   if (ping_color_type == 3 && ping_have_PLTE != MagickFalse)
8864     {
8865       png_set_PLTE(ping,ping_info,palette,number_colors);
8866
8867       if (logging != MagickFalse)
8868         {
8869           for (i=0; i< (ssize_t) number_colors; i++)
8870           {
8871             if (i < ping_num_trans)
8872               (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8873                 "     PLTE[%d] = (%d,%d,%d), tRNS[%d] = (%d)",
8874                       (int) i,
8875                       (int) palette[i].red,
8876                       (int) palette[i].green,
8877                       (int) palette[i].blue,
8878                       (int) i,
8879                       (int) ping_trans_alpha[i]);
8880              else
8881               (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8882                 "     PLTE[%d] = (%d,%d,%d)",
8883                       (int) i,
8884                       (int) palette[i].red,
8885                       (int) palette[i].green,
8886                       (int) palette[i].blue);
8887            }
8888          }
8889     }
8890
8891   if (ping_exclude_bKGD == MagickFalse)
8892     {
8893       if (ping_have_bKGD != MagickFalse)
8894           png_set_bKGD(ping,ping_info,&ping_background);
8895     }
8896
8897   if (ping_exclude_pHYs == MagickFalse)
8898     {
8899       if (ping_have_pHYs != MagickFalse)
8900         {
8901           png_set_pHYs(ping,ping_info,
8902              ping_pHYs_x_resolution,
8903              ping_pHYs_y_resolution,
8904              ping_pHYs_unit_type);
8905         }
8906     }
8907
8908 #if defined(PNG_oFFs_SUPPORTED)
8909   if (ping_exclude_oFFs == MagickFalse)
8910     {
8911       if (image->page.x || image->page.y)
8912         {
8913            png_set_oFFs(ping,ping_info,(png_int_32) image->page.x,
8914               (png_int_32) image->page.y, 0);
8915
8916            if (logging != MagickFalse)
8917              (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8918                  "    Setting up oFFs chunk with x=%d, y=%d, units=0",
8919                  (int) image->page.x, (int) image->page.y);
8920         }
8921     }
8922 #endif
8923
8924   if (mng_info->need_blob != MagickFalse)
8925   {
8926     if (OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception) ==
8927        MagickFalse)
8928        png_error(ping,"WriteBlob Failed");
8929
8930      ping_have_blob=MagickTrue;
8931   }
8932
8933   png_write_info_before_PLTE(ping, ping_info);
8934
8935   if (ping_have_tRNS != MagickFalse && ping_color_type < 4)
8936     {
8937       if (logging != MagickFalse)
8938         {
8939           (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8940               "  Calling png_set_tRNS with num_trans=%d",ping_num_trans);
8941         }
8942
8943       if (ping_color_type == 3)
8944          (void) png_set_tRNS(ping, ping_info,
8945                 ping_trans_alpha,
8946                 ping_num_trans,
8947                 NULL);
8948
8949       else
8950         {
8951            (void) png_set_tRNS(ping, ping_info,
8952                   NULL,
8953                   0,
8954                   &ping_trans_color);
8955
8956            if (logging != MagickFalse)
8957              {
8958                (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8959                  "     tRNS color   =(%d,%d,%d)",
8960                        (int) ping_trans_color.red,
8961                        (int) ping_trans_color.green,
8962                        (int) ping_trans_color.blue);
8963              }
8964          }
8965     }
8966
8967   /* write any png-chunk-b profiles */
8968   (void) Magick_png_write_chunk_from_profile(image,"PNG-chunk-b",logging);
8969
8970   png_write_info(ping,ping_info);
8971
8972   /* write any PNG-chunk-m profiles */
8973   (void) Magick_png_write_chunk_from_profile(image,"PNG-chunk-m",logging);
8974
8975   if (ping_exclude_vpAg == MagickFalse)
8976     {
8977       if ((image->page.width != 0 && image->page.width != image->columns) ||
8978           (image->page.height != 0 && image->page.height != image->rows))
8979         {
8980           unsigned char
8981             chunk[14];
8982
8983           (void) WriteBlobMSBULong(image,9L);  /* data length=8 */
8984           PNGType(chunk,mng_vpAg);
8985           LogPNGChunk(logging,mng_vpAg,9L);
8986           PNGLong(chunk+4,(png_uint_32) image->page.width);
8987           PNGLong(chunk+8,(png_uint_32) image->page.height);
8988           chunk[12]=0;   /* unit = pixels */
8989           (void) WriteBlob(image,13,chunk);
8990           (void) WriteBlobMSBULong(image,crc32(0,chunk,13));
8991         }
8992     }
8993
8994 #if (PNG_LIBPNG_VER == 10206)
8995     /* avoid libpng-1.2.6 bug by setting PNG_HAVE_IDAT flag */
8996 #define PNG_HAVE_IDAT               0x04
8997     ping->mode |= PNG_HAVE_IDAT;
8998 #undef PNG_HAVE_IDAT
8999 #endif
9000
9001   png_set_packing(ping);
9002   /*
9003     Allocate memory.
9004   */
9005   rowbytes=image->columns;
9006   if (image_depth > 8)
9007     rowbytes*=2;
9008   switch (ping_color_type)
9009     {
9010       case PNG_COLOR_TYPE_RGB:
9011         rowbytes*=3;
9012         break;
9013
9014       case PNG_COLOR_TYPE_GRAY_ALPHA:
9015         rowbytes*=2;
9016         break;
9017
9018       case PNG_COLOR_TYPE_RGBA:
9019         rowbytes*=4;
9020         break;
9021
9022       default:
9023         break;
9024     }
9025
9026   if (logging != MagickFalse)
9027     {
9028       (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9029         "  Writing PNG image data");
9030
9031       (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9032         "    Allocating %.20g bytes of memory for pixels",(double) rowbytes);
9033     }
9034   ping_pixels=(unsigned char *) AcquireQuantumMemory(rowbytes,
9035     sizeof(*ping_pixels));
9036
9037   if (ping_pixels == (unsigned char *) NULL)
9038     ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
9039
9040   /*
9041     Initialize image scanlines.
9042   */
9043   if (setjmp(png_jmpbuf(ping)))
9044     {
9045       /*
9046         PNG write failed.
9047       */
9048 #ifdef PNG_DEBUG
9049      if (image_info->verbose)
9050         (void) printf("PNG write has failed.\n");
9051 #endif
9052       png_destroy_write_struct(&ping,&ping_info);
9053       if (quantum_info != (QuantumInfo *) NULL)
9054         quantum_info=DestroyQuantumInfo(quantum_info);
9055       if (ping_pixels != (unsigned char *) NULL)
9056         ping_pixels=(unsigned char *) RelinquishMagickMemory(ping_pixels);
9057 #if defined(PNG_SETJMP_NOT_THREAD_SAFE)
9058       UnlockSemaphoreInfo(ping_semaphore);
9059 #endif
9060       if (ping_have_blob != MagickFalse)
9061           (void) CloseBlob(image);
9062       image_info=DestroyImageInfo(image_info);
9063       image=DestroyImage(image);
9064       return(MagickFalse);
9065     }
9066   quantum_info=AcquireQuantumInfo(image_info,image);
9067   if (quantum_info == (QuantumInfo *) NULL)
9068     ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
9069   quantum_info->format=UndefinedQuantumFormat;
9070   quantum_info->depth=image_depth;
9071   num_passes=png_set_interlace_handling(ping);
9072
9073   if ((!mng_info->write_png8 && !mng_info->write_png24 &&
9074        !mng_info->write_png32) &&
9075        (mng_info->IsPalette ||
9076        (image_info->type == BilevelType)) &&
9077        image_matte == MagickFalse &&
9078        ping_have_non_bw == MagickFalse)
9079     {
9080       /* Palette, Bilevel, or Opaque Monochrome */
9081       register const PixelPacket
9082         *p;
9083
9084       quantum_info->depth=8;
9085       for (pass=0; pass < num_passes; pass++)
9086       {
9087         /*
9088           Convert PseudoClass image to a PNG monochrome image.
9089         */
9090         for (y=0; y < (ssize_t) image->rows; y++)
9091         {
9092           if (logging != MagickFalse && y == 0)
9093              (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9094                  "    Writing row of pixels (0)");
9095
9096           p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
9097
9098           if (p == (const PixelPacket *) NULL)
9099             break;
9100
9101           if (mng_info->IsPalette)
9102             {
9103               (void) ExportQuantumPixels(image,(const CacheView *) NULL,
9104                 quantum_info,GrayQuantum,ping_pixels,&image->exception);
9105               if (mng_info->write_png_colortype-1 == PNG_COLOR_TYPE_PALETTE &&
9106                   mng_info->write_png_depth &&
9107                   mng_info->write_png_depth != old_bit_depth)
9108                 {
9109                   /* Undo pixel scaling */
9110                   for (i=0; i < (ssize_t) image->columns; i++)
9111                      *(ping_pixels+i)=(unsigned char) (*(ping_pixels+i)
9112                      >> (8-old_bit_depth));
9113                 }
9114             }
9115
9116           else
9117             {
9118               (void) ExportQuantumPixels(image,(const CacheView *) NULL,
9119                 quantum_info,RedQuantum,ping_pixels,&image->exception);
9120             }
9121
9122           if (mng_info->write_png_colortype-1 != PNG_COLOR_TYPE_PALETTE)
9123             for (i=0; i < (ssize_t) image->columns; i++)
9124                *(ping_pixels+i)=(unsigned char) ((*(ping_pixels+i) > 127) ?
9125                       255 : 0);
9126
9127           if (logging != MagickFalse && y == 0)
9128             (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9129                 "    Writing row of pixels (1)");
9130
9131           png_write_row(ping,ping_pixels);
9132         }
9133         if (image->previous == (Image *) NULL)
9134           {
9135             status=SetImageProgress(image,LoadImageTag,pass,num_passes);
9136             if (status == MagickFalse)
9137               break;
9138           }
9139       }
9140     }
9141
9142   else   /* Not Palette, Bilevel, or Opaque Monochrome */
9143     {
9144       if ((!mng_info->write_png8 && !mng_info->write_png24 &&
9145          !mng_info->write_png32) &&
9146          (image_matte != MagickFalse ||
9147          (ping_bit_depth >= MAGICKCORE_QUANTUM_DEPTH)) &&
9148          (mng_info->IsPalette) && ping_have_color == MagickFalse)
9149         {
9150           register const PixelPacket
9151             *p;
9152
9153           for (pass=0; pass < num_passes; pass++)
9154           {
9155
9156           for (y=0; y < (ssize_t) image->rows; y++)
9157           {
9158             p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
9159
9160             if (p == (const PixelPacket *) NULL)
9161               break;
9162
9163             if (ping_color_type == PNG_COLOR_TYPE_GRAY)
9164               {
9165                 if (mng_info->IsPalette)
9166                   (void) ExportQuantumPixels(image,(const CacheView *) NULL,
9167                     quantum_info,GrayQuantum,ping_pixels,&image->exception);
9168
9169                 else
9170                   (void) ExportQuantumPixels(image,(const CacheView *) NULL,
9171                     quantum_info,RedQuantum,ping_pixels,&image->exception);
9172
9173                 if (logging != MagickFalse && y == 0)
9174                   (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9175                        "    Writing GRAY PNG pixels (2)");
9176               }
9177
9178             else /* PNG_COLOR_TYPE_GRAY_ALPHA */
9179               {
9180                 if (logging != MagickFalse && y == 0)
9181                   (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9182                          "    Writing GRAY_ALPHA PNG pixels (2)");
9183
9184                 (void) ExportQuantumPixels(image,(const CacheView *) NULL,
9185                   quantum_info,GrayAlphaQuantum,ping_pixels,&image->exception);
9186               }
9187
9188             if (logging != MagickFalse && y == 0)
9189               (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9190                   "    Writing row of pixels (2)");
9191
9192             png_write_row(ping,ping_pixels);
9193           }
9194
9195           if (image->previous == (Image *) NULL)
9196             {
9197               status=SetImageProgress(image,LoadImageTag,pass,num_passes);
9198               if (status == MagickFalse)
9199                 break;
9200             }
9201           }
9202         }
9203
9204       else
9205         {
9206           register const PixelPacket
9207             *p;
9208
9209           for (pass=0; pass < num_passes; pass++)
9210           {
9211             if ((image_depth > 8) || (mng_info->write_png24 ||
9212                 mng_info->write_png32 ||
9213                 (!mng_info->write_png8 && !mng_info->IsPalette)))
9214             {
9215               for (y=0; y < (ssize_t) image->rows; y++)
9216               {
9217                 p=GetVirtualPixels(image,0,y,image->columns,1,
9218                    &image->exception);
9219
9220                 if (p == (const PixelPacket *) NULL)
9221                   break;
9222
9223                 if (ping_color_type == PNG_COLOR_TYPE_GRAY)
9224                   {
9225                     if (image->storage_class == DirectClass)
9226                       (void) ExportQuantumPixels(image,(const CacheView *) NULL,
9227                         quantum_info,RedQuantum,ping_pixels,&image->exception);
9228
9229                     else
9230                       (void) ExportQuantumPixels(image,(const CacheView *) NULL,
9231                         quantum_info,GrayQuantum,ping_pixels,&image->exception);
9232                   }
9233
9234                 else if (ping_color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
9235                   {
9236                     (void) ExportQuantumPixels(image,(const CacheView *) NULL,
9237                       quantum_info,GrayAlphaQuantum,ping_pixels,
9238                       &image->exception);
9239
9240                     if (logging != MagickFalse && y == 0)
9241                       (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9242                            "    Writing GRAY_ALPHA PNG pixels (3)");
9243                   }
9244
9245                 else if (image_matte != MagickFalse)
9246                   (void) ExportQuantumPixels(image,(const CacheView *) NULL,
9247                     quantum_info,RGBAQuantum,ping_pixels,&image->exception);
9248
9249                 else
9250                   (void) ExportQuantumPixels(image,(const CacheView *) NULL,
9251                     quantum_info,RGBQuantum,ping_pixels,&image->exception);
9252
9253                 if (logging != MagickFalse && y == 0)
9254                   (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9255                       "    Writing row of pixels (3)");
9256
9257                 png_write_row(ping,ping_pixels);
9258               }
9259             }
9260
9261           else
9262             /* not ((image_depth > 8) || (mng_info->write_png24 ||
9263                 mng_info->write_png32 ||
9264                 (!mng_info->write_png8 && !mng_info->IsPalette))) */
9265             {
9266               if ((ping_color_type != PNG_COLOR_TYPE_GRAY) &&
9267                   (ping_color_type != PNG_COLOR_TYPE_GRAY_ALPHA))
9268                 {
9269                   if (logging != MagickFalse)
9270                     (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9271                       "  pass %d, Image Is not GRAY or GRAY_ALPHA",pass);
9272
9273                   quantum_info->depth=8;
9274                   image_depth=8;
9275                 }
9276
9277               for (y=0; y < (ssize_t) image->rows; y++)
9278               {
9279                 if (logging != MagickFalse && y == 0)
9280                   (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9281                     "  pass %d, Image Is RGB, 16-bit GRAY, or GRAY_ALPHA",pass);
9282
9283                 p=GetVirtualPixels(image,0,y,image->columns,1,
9284                    &image->exception);
9285
9286                 if (p == (const PixelPacket *) NULL)
9287                   break;
9288
9289                 if (ping_color_type == PNG_COLOR_TYPE_GRAY)
9290                   (void) ExportQuantumPixels(image,(const CacheView *) NULL,
9291                        quantum_info,GrayQuantum,ping_pixels,&image->exception);
9292
9293                 else if (ping_color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
9294                   {
9295                     if (logging != MagickFalse && y == 0)
9296                       (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9297                            "  Writing GRAY_ALPHA PNG pixels (4)");
9298
9299                     (void) ExportQuantumPixels(image,(const CacheView *) NULL,
9300                          quantum_info,GrayAlphaQuantum,ping_pixels,
9301                          &image->exception);
9302                   }
9303
9304                 else
9305                   {
9306                     /* To do:
9307                      *
9308                      * This is failing to account for 1, 2, 4-bit depths
9309                      * The call to png_set_packing() above is supposed to
9310                      * take care of those.
9311                      */
9312
9313                     /* GrayQuantum does not work here */
9314                     (void) ExportQuantumPixels(image,(const CacheView *) NULL,
9315                       quantum_info,IndexQuantum,ping_pixels,&image->exception);
9316
9317                     if (logging != MagickFalse && y <= 2)
9318                     {
9319                       (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9320                           "  Writing row of pixels (4)");
9321
9322                       (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9323                           "  ping_pixels[0]=%d,ping_pixels[1]=%d",
9324                           (int)ping_pixels[0],(int)ping_pixels[1]);
9325                     }
9326                   }
9327                 png_write_row(ping,ping_pixels);
9328               }
9329             }
9330
9331             if (image->previous == (Image *) NULL)
9332               {
9333                 status=SetImageProgress(image,LoadImageTag,pass,num_passes);
9334                 if (status == MagickFalse)
9335                   break;
9336               }
9337           }
9338         }
9339     }
9340
9341   if (quantum_info != (QuantumInfo *) NULL)
9342     quantum_info=DestroyQuantumInfo(quantum_info);
9343
9344   if (logging != MagickFalse)
9345     {
9346       (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9347         "  Wrote PNG image data");
9348
9349       (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9350         "    Width: %.20g",(double) ping_width);
9351
9352       (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9353         "    Height: %.20g",(double) ping_height);
9354
9355       if (mng_info->write_png_depth)
9356         {
9357           (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9358             "    Defined PNG:bit-depth: %d",mng_info->write_png_depth);
9359         }
9360
9361       (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9362         "    PNG bit-depth written: %d",ping_bit_depth);
9363
9364       if (mng_info->write_png_colortype)
9365         {
9366           (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9367             "    Defined PNG:color-type: %d",mng_info->write_png_colortype-1);
9368         }
9369
9370       (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9371         "    PNG color-type written: %d",ping_color_type);
9372
9373       (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9374         "    PNG Interlace method: %d",ping_interlace_method);
9375     }
9376   /*
9377     Generate text chunks.
9378   */
9379   if (ping_exclude_tEXt == MagickFalse && ping_exclude_zTXt == MagickFalse)
9380   {
9381     ResetImagePropertyIterator(image);
9382     property=GetNextImageProperty(image);
9383     while (property != (const char *) NULL)
9384     {
9385       png_textp
9386         text;
9387
9388       value=GetImageProperty(image,property);
9389       if (value != (const char *) NULL)
9390         {
9391           text=(png_textp) png_malloc(ping,(png_uint_32) sizeof(png_text));
9392           text[0].key=(char *) property;
9393           text[0].text=(char *) value;
9394           text[0].text_length=strlen(value);
9395
9396           if (ping_exclude_tEXt != MagickFalse)
9397              text[0].compression=PNG_TEXT_COMPRESSION_zTXt;
9398
9399           else if (ping_exclude_zTXt != MagickFalse)
9400              text[0].compression=PNG_TEXT_COMPRESSION_NONE;
9401
9402           else
9403           {
9404              text[0].compression=image_info->compression == NoCompression ||
9405                (image_info->compression == UndefinedCompression &&
9406                text[0].text_length < 128) ? PNG_TEXT_COMPRESSION_NONE :
9407                PNG_TEXT_COMPRESSION_zTXt ;
9408           }
9409
9410           if (logging != MagickFalse)
9411             {
9412               (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9413                 "  Setting up text chunk");
9414
9415               (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9416                 "    keyword: %s",text[0].key);
9417             }
9418
9419           png_set_text(ping,ping_info,text,1);
9420           png_free(ping,text);
9421         }
9422       property=GetNextImageProperty(image);
9423     }
9424   }
9425
9426   /* write any PNG-chunk-e profiles */
9427   (void) Magick_png_write_chunk_from_profile(image,"PNG-chunk-e",logging);
9428
9429   if (logging != MagickFalse)
9430     (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9431       "  Writing PNG end info");
9432
9433   png_write_end(ping,ping_info);
9434
9435   if (mng_info->need_fram && (int) image->dispose == BackgroundDispose)
9436     {
9437       if (mng_info->page.x || mng_info->page.y ||
9438           (ping_width != mng_info->page.width) ||
9439           (ping_height != mng_info->page.height))
9440         {
9441           unsigned char
9442             chunk[32];
9443
9444           /*
9445             Write FRAM 4 with clipping boundaries followed by FRAM 1.
9446           */
9447           (void) WriteBlobMSBULong(image,27L);  /* data length=27 */
9448           PNGType(chunk,mng_FRAM);
9449           LogPNGChunk(logging,mng_FRAM,27L);
9450           chunk[4]=4;
9451           chunk[5]=0;  /* frame name separator (no name) */
9452           chunk[6]=1;  /* flag for changing delay, for next frame only */
9453           chunk[7]=0;  /* flag for changing frame timeout */
9454           chunk[8]=1;  /* flag for changing frame clipping for next frame */
9455           chunk[9]=0;  /* flag for changing frame sync_id */
9456           PNGLong(chunk+10,(png_uint_32) (0L)); /* temporary 0 delay */
9457           chunk[14]=0; /* clipping boundaries delta type */
9458           PNGLong(chunk+15,(png_uint_32) (mng_info->page.x)); /* left cb */
9459           PNGLong(chunk+19,
9460              (png_uint_32) (mng_info->page.x + ping_width));
9461           PNGLong(chunk+23,(png_uint_32) (mng_info->page.y)); /* top cb */
9462           PNGLong(chunk+27,
9463              (png_uint_32) (mng_info->page.y + ping_height));
9464           (void) WriteBlob(image,31,chunk);
9465           (void) WriteBlobMSBULong(image,crc32(0,chunk,31));
9466           mng_info->old_framing_mode=4;
9467           mng_info->framing_mode=1;
9468         }
9469
9470       else
9471         mng_info->framing_mode=3;
9472     }
9473   if (mng_info->write_mng && !mng_info->need_fram &&
9474       ((int) image->dispose == 3))
9475      (void) ThrowMagickException(&IMimage->exception,GetMagickModule(),
9476        CoderError,"Cannot convert GIF with disposal method 3 to MNG-LC",
9477        "`%s'",IMimage->filename);
9478
9479   /*
9480     Free PNG resources.
9481   */
9482
9483   png_destroy_write_struct(&ping,&ping_info);
9484
9485   ping_pixels=(unsigned char *) RelinquishMagickMemory(ping_pixels);
9486
9487 #if defined(PNG_SETJMP_NOT_THREAD_SAFE)
9488   UnlockSemaphoreInfo(ping_semaphore);
9489 #endif
9490
9491   if (ping_have_blob != MagickFalse)
9492      (void) CloseBlob(image);
9493
9494   image_info=DestroyImageInfo(image_info);
9495   image=DestroyImage(image);
9496
9497   /* Store bit depth actually written */
9498   s[0]=(char) ping_bit_depth;
9499   s[1]='\0';
9500
9501   (void) SetImageProperty(IMimage,"png:bit-depth-written",s);
9502
9503   if (logging != MagickFalse)
9504     (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9505       "  exit WriteOnePNGImage()");
9506
9507   return(MagickTrue);
9508 /*  End write one PNG image */
9509 }
9510
9511 /*
9512 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
9513 %                                                                             %
9514 %                                                                             %
9515 %                                                                             %
9516 %   W r i t e P N G I m a g e                                                 %
9517 %                                                                             %
9518 %                                                                             %
9519 %                                                                             %
9520 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
9521 %
9522 %  WritePNGImage() writes a Portable Network Graphics (PNG) or
9523 %  Multiple-image Network Graphics (MNG) image file.
9524 %
9525 %  MNG support written by Glenn Randers-Pehrson, glennrp@image...
9526 %
9527 %  The format of the WritePNGImage method is:
9528 %
9529 %      MagickBooleanType WritePNGImage(const ImageInfo *image_info,Image *image)
9530 %
9531 %  A description of each parameter follows:
9532 %
9533 %    o image_info: the image info.
9534 %
9535 %    o image:  The image.
9536 %
9537 %  Returns MagickTrue on success, MagickFalse on failure.
9538 %
9539 %  Communicating with the PNG encoder:
9540 %
9541 %  While the datastream written is always in PNG format and normally would
9542 %  be given the "png" file extension, this method also writes the following
9543 %  pseudo-formats which are subsets of PNG:
9544 %
9545 %    o PNG8:    An 8-bit indexed PNG datastream is written.  If the image has
9546 %               a depth greater than 8, the depth is reduced. If transparency
9547 %               is present, the tRNS chunk must only have values 0 and 255
9548 %               (i.e., transparency is binary: fully opaque or fully
9549 %               transparent).  If other values are present they will be
9550 %               50%-thresholded to binary transparency.  If more than 256
9551 %               colors are present, they will be quantized to the 3-3-2
9552 %               palette.  If you want better quantization or dithering of
9553 %               the colors or alpha, you need to do it before calling the
9554 %               PNG encoder. The pixels contain 8-bit indices even if
9555 %               they could be represented with 1, 2, or 4 bits.  Grayscale
9556 %               images will be written as indexed PNG files even though the
9557 %               PNG grayscale type might be slightly more efficient.  Please
9558 %               note that writing to the PNG8 format may result in loss
9559 %               of color and alpha data.
9560 %
9561 %    o PNG24:   An 8-bit per sample RGB PNG datastream is written.  The tRNS
9562 %               chunk can be present to convey binary transparency by naming
9563 %               one of the colors as transparent.  The only loss incurred
9564 %               is reduction of sample depth to 8.  If the image has more
9565 %               than one transparent color, has semitransparent pixels, or
9566 %               has an opaque pixel with the same RGB components as the
9567 %               transparent color, an image is not written.
9568 %
9569 %    o PNG32:   An 8-bit per sample RGBA PNG is written.  Partial
9570 %               transparency is permitted, i.e., the alpha sample for
9571 %               each pixel can have any value from 0 to 255. The alpha
9572 %               channel is present even if the image is fully opaque.
9573 %               The only loss in data is the reduction of the sample depth
9574 %               to 8.
9575 %
9576 %    o -define: For more precise control of the PNG output, you can use the
9577 %               Image options "png:bit-depth" and "png:color-type".  These
9578 %               can be set from the commandline with "-define" and also
9579 %               from the application programming interfaces.  The options
9580 %               are case-independent and are converted to lowercase before
9581 %               being passed to this encoder.
9582 %
9583 %               png:color-type can be 0, 2, 3, 4, or 6.
9584 %
9585 %               When png:color-type is 0 (Grayscale), png:bit-depth can
9586 %               be 1, 2, 4, 8, or 16.
9587 %
9588 %               When png:color-type is 2 (RGB), png:bit-depth can
9589 %               be 8 or 16.
9590 %
9591 %               When png:color-type is 3 (Indexed), png:bit-depth can
9592 %               be 1, 2, 4, or 8.  This refers to the number of bits
9593 %               used to store the index.  The color samples always have
9594 %               bit-depth 8 in indexed PNG files.
9595 %
9596 %               When png:color-type is 4 (Gray-Matte) or 6 (RGB-Matte),
9597 %               png:bit-depth can be 8 or 16.
9598 %
9599 %  If the image cannot be written without loss with the requested bit-depth
9600 %  and color-type, a PNG file will not be written, and the encoder will
9601 %  return MagickFalse.
9602 %
9603 %  Since image encoders should not be responsible for the "heavy lifting",
9604 %  the user should make sure that ImageMagick has already reduced the
9605 %  image depth and number of colors and limit transparency to binary
9606 %  transparency prior to attempting to write the image with depth, color,
9607 %   or transparency limitations.
9608 %
9609 %  TODO: Enforce the previous paragraph.
9610 %
9611 %  Note that another definition, "png:bit-depth-written" exists, but it
9612 %  is not intended for external use.  It is only used internally by the
9613 %  PNG encoder to inform the JNG encoder of the depth of the alpha channel.
9614 %
9615 %  It is possible to request that the PNG encoder write previously-formatted
9616 %  ancillary chunks in the output PNG file, using the "-profile" commandline
9617 %  option as shown below or by setting the profile via a programming
9618 %  interface:
9619 %
9620 %     -profile PNG-chunk-x:<file>
9621 %
9622 %  where x is a location flag and <file> is a file containing the chunk
9623 %  name in the first 4 bytes, then a colon (":"), followed by the chunk data.
9624 %  This encoder will compute the chunk length and CRC, so those must not
9625 %  be included in the file.
9626 %
9627 %  "x" can be "b" (before PLTE), "m" (middle, i.e., between PLTE and IDAT),
9628 %  or "e" (end, i.e., after IDAT).  If you want to write multiple chunks
9629 %  of the same type, then add a short unique string after the "x" to prevent
9630 %  subsequent profiles from overwriting the preceding ones, e.g.,
9631 %
9632 %     -profile PNG-chunk-b01:file01 -profile PNG-chunk-b02:file02
9633 %
9634 %  As of version 6.6.6 the following optimizations are always done:
9635 %
9636 %   o  32-bit depth is reduced to 16.
9637 %   o  16-bit depth is reduced to 8 if all pixels contain samples whose
9638 %      high byte and low byte are identical.
9639 %   o  Palette is sorted to remove unused entries and to put a
9640 %      transparent color first, if BUILD_PNG_PALETTE is defined.
9641 %   o  Opaque matte channel is removed when writing an indexed PNG.
9642 %   o  Grayscale images are reduced to 1, 2, or 4 bit depth if
9643 %      this can be done without loss and a larger bit depth N was not
9644 %      requested via the "-define PNG:bit-depth=N" option.
9645 %   o  If matte channel is present but only one transparent color is
9646 %      present, RGB+tRNS is written instead of RGBA
9647 %   o  Opaque matte channel is removed (or added, if color-type 4 or 6
9648 %      was requested when converting an opaque image).
9649 %
9650 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
9651 */
9652 static MagickBooleanType WritePNGImage(const ImageInfo *image_info,
9653   Image *image)
9654 {
9655   MagickBooleanType
9656     excluding,
9657     logging,
9658     have_mng_structure,
9659     status;
9660
9661   MngInfo
9662     *mng_info;
9663
9664   const char
9665     *value;
9666
9667   int
9668     i,
9669     source;
9670
9671   /*
9672     Open image file.
9673   */
9674   assert(image_info != (const ImageInfo *) NULL);
9675   assert(image_info->signature == MagickSignature);
9676   assert(image != (Image *) NULL);
9677   assert(image->signature == MagickSignature);
9678   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
9679   logging=LogMagickEvent(CoderEvent,GetMagickModule(),"Enter WritePNGImage()");
9680   /*
9681     Allocate a MngInfo structure.
9682   */
9683   have_mng_structure=MagickFalse;
9684   mng_info=(MngInfo *) AcquireMagickMemory(sizeof(MngInfo));
9685
9686   if (mng_info == (MngInfo *) NULL)
9687     ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
9688
9689   /*
9690     Initialize members of the MngInfo structure.
9691   */
9692   (void) ResetMagickMemory(mng_info,0,sizeof(MngInfo));
9693   mng_info->image=image;
9694   mng_info->equal_backgrounds=MagickTrue;
9695   have_mng_structure=MagickTrue;
9696
9697   /* See if user has requested a specific PNG subformat */
9698
9699   mng_info->write_png8=LocaleCompare(image_info->magick,"PNG8") == 0;
9700   mng_info->write_png24=LocaleCompare(image_info->magick,"PNG24") == 0;
9701   mng_info->write_png32=LocaleCompare(image_info->magick,"PNG32") == 0;
9702
9703   if (mng_info->write_png8)
9704     {
9705       mng_info->write_png_colortype = /* 3 */ 4;
9706       mng_info->write_png_depth = 8;
9707       image->depth = 8;
9708     }
9709
9710   if (mng_info->write_png24)
9711     {
9712       mng_info->write_png_colortype = /* 2 */ 3;
9713       mng_info->write_png_depth = 8;
9714       image->depth = 8;
9715
9716       if (image->matte == MagickTrue)
9717         (void) SetImageType(image,TrueColorMatteType);
9718
9719       else
9720         (void) SetImageType(image,TrueColorType);
9721
9722       (void) SyncImage(image);
9723     }
9724
9725   if (mng_info->write_png32)
9726     {
9727       mng_info->write_png_colortype = /* 6 */  7;
9728       mng_info->write_png_depth = 8;
9729       image->depth = 8;
9730
9731       if (image->matte == MagickTrue)
9732         (void) SetImageType(image,TrueColorMatteType);
9733
9734       else
9735         (void) SetImageType(image,TrueColorType);
9736
9737       (void) SyncImage(image);
9738     }
9739
9740   value=GetImageOption(image_info,"png:bit-depth");
9741
9742   if (value != (char *) NULL)
9743     {
9744       if (LocaleCompare(value,"1") == 0)
9745         mng_info->write_png_depth = 1;
9746
9747       else if (LocaleCompare(value,"2") == 0)
9748         mng_info->write_png_depth = 2;
9749
9750       else if (LocaleCompare(value,"4") == 0)
9751         mng_info->write_png_depth = 4;
9752
9753       else if (LocaleCompare(value,"8") == 0)
9754         mng_info->write_png_depth = 8;
9755
9756       else if (LocaleCompare(value,"16") == 0)
9757         mng_info->write_png_depth = 16;
9758
9759       else
9760         (void) ThrowMagickException(&image->exception,
9761              GetMagickModule(),CoderWarning,
9762              "ignoring invalid defined png:bit-depth",
9763              "=%s",value);
9764
9765       if (logging != MagickFalse)
9766         (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9767           "  png:bit-depth=%d was defined.\n",mng_info->write_png_depth);
9768     }
9769
9770   value=GetImageOption(image_info,"png:color-type");
9771
9772   if (value != (char *) NULL)
9773     {
9774       /* We must store colortype+1 because 0 is a valid colortype */
9775       if (LocaleCompare(value,"0") == 0)
9776         mng_info->write_png_colortype = 1;
9777
9778       else if (LocaleCompare(value,"2") == 0)
9779         mng_info->write_png_colortype = 3;
9780
9781       else if (LocaleCompare(value,"3") == 0)
9782         mng_info->write_png_colortype = 4;
9783
9784       else if (LocaleCompare(value,"4") == 0)
9785         mng_info->write_png_colortype = 5;
9786
9787       else if (LocaleCompare(value,"6") == 0)
9788         mng_info->write_png_colortype = 7;
9789
9790       else
9791         (void) ThrowMagickException(&image->exception,
9792              GetMagickModule(),CoderWarning,
9793              "ignoring invalid defined png:color-type",
9794              "=%s",value);
9795
9796       if (logging != MagickFalse)
9797         (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9798           "  png:color-type=%d was defined.\n",mng_info->write_png_colortype-1);
9799     }
9800
9801   /* Check for chunks to be excluded:
9802    *
9803    * The default is to not exclude any known chunks except for any
9804    * listed in the "unused_chunks" array, above.
9805    *
9806    * Chunks can be listed for exclusion via a "PNG:exclude-chunk"
9807    * define (in the image properties or in the image artifacts)
9808    * or via a mng_info member.  For convenience, in addition
9809    * to or instead of a comma-separated list of chunks, the
9810    * "exclude-chunk" string can be simply "all" or "none".
9811    *
9812    * The exclude-chunk define takes priority over the mng_info.
9813    *
9814    * A "PNG:include-chunk" define takes  priority over both the
9815    * mng_info and the "PNG:exclude-chunk" define.  Like the
9816    * "exclude-chunk" string, it can define "all" or "none" as
9817    * well as a comma-separated list.  Chunks that are unknown to
9818    * ImageMagick are always excluded, regardless of their "copy-safe"
9819    * status according to the PNG specification, and even if they
9820    * appear in the "include-chunk" list.
9821    *
9822    * Finally, all chunks listed in the "unused_chunks" array are
9823    * automatically excluded, regardless of the other instructions
9824    * or lack thereof.
9825    *
9826    * if you exclude sRGB but not gAMA (recommended), then sRGB chunk
9827    * will not be written and the gAMA chunk will only be written if it
9828    * is not between .45 and .46, or approximately (1.0/2.2).
9829    *
9830    * If you exclude tRNS and the image has transparency, the colortype
9831    * is forced to be 4 or 6 (GRAY_ALPHA or RGB_ALPHA).
9832    *
9833    * The -strip option causes StripImage() to set the png:include-chunk
9834    * artifact to "none,gama".
9835    */
9836
9837   mng_info->ping_exclude_bKGD=MagickFalse;
9838   mng_info->ping_exclude_cHRM=MagickFalse;
9839   mng_info->ping_exclude_EXIF=MagickFalse; /* hex-encoded EXIF in zTXt */
9840   mng_info->ping_exclude_gAMA=MagickFalse;
9841   mng_info->ping_exclude_cHRM=MagickFalse;
9842   mng_info->ping_exclude_iCCP=MagickFalse;
9843   /* mng_info->ping_exclude_iTXt=MagickFalse; */
9844   mng_info->ping_exclude_oFFs=MagickFalse;
9845   mng_info->ping_exclude_pHYs=MagickFalse;
9846   mng_info->ping_exclude_sRGB=MagickFalse;
9847   mng_info->ping_exclude_tEXt=MagickFalse;
9848   mng_info->ping_exclude_tRNS=MagickFalse;
9849   mng_info->ping_exclude_vpAg=MagickFalse;
9850   mng_info->ping_exclude_zCCP=MagickFalse; /* hex-encoded iCCP in zTXt */
9851   mng_info->ping_exclude_zTXt=MagickFalse;
9852
9853   excluding=MagickFalse;
9854
9855   for (source=0; source<1; source++)
9856   {
9857     if (source==0)
9858       {
9859        value=GetImageArtifact(image,"png:exclude-chunk");
9860
9861        if (value == NULL)
9862          value=GetImageArtifact(image,"png:exclude-chunks");
9863       }
9864     else
9865       {
9866        value=GetImageOption(image_info,"png:exclude-chunk");
9867
9868        if (value == NULL)
9869          value=GetImageOption(image_info,"png:exclude-chunks");
9870       }
9871
9872     if (value != NULL)
9873     {
9874
9875     size_t
9876       last;
9877
9878     excluding=MagickTrue;
9879
9880     if (logging != MagickFalse)
9881       {
9882         if (source == 0)
9883            (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9884               "  png:exclude-chunk=%s found in image artifacts.\n", value);
9885         else
9886            (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9887               "  png:exclude-chunk=%s found in image properties.\n", value);
9888       }
9889
9890     last=strlen(value);
9891
9892     for (i=0; i<(int) last; i+=5)
9893     {
9894
9895       if (LocaleNCompare(value+i,"all",3) == 0)
9896       {
9897         mng_info->ping_exclude_bKGD=MagickTrue;
9898         mng_info->ping_exclude_cHRM=MagickTrue;
9899         mng_info->ping_exclude_EXIF=MagickTrue;
9900         mng_info->ping_exclude_gAMA=MagickTrue;
9901         mng_info->ping_exclude_iCCP=MagickTrue;
9902         /* mng_info->ping_exclude_iTXt=MagickTrue; */
9903         mng_info->ping_exclude_oFFs=MagickTrue;
9904         mng_info->ping_exclude_pHYs=MagickTrue;
9905         mng_info->ping_exclude_sRGB=MagickTrue;
9906         mng_info->ping_exclude_tEXt=MagickTrue;
9907         mng_info->ping_exclude_tRNS=MagickTrue;
9908         mng_info->ping_exclude_vpAg=MagickTrue;
9909         mng_info->ping_exclude_zCCP=MagickTrue;
9910         mng_info->ping_exclude_zTXt=MagickTrue;
9911         i--;
9912       }
9913
9914       if (LocaleNCompare(value+i,"none",4) == 0)
9915       {
9916         mng_info->ping_exclude_bKGD=MagickFalse;
9917         mng_info->ping_exclude_cHRM=MagickFalse;
9918         mng_info->ping_exclude_EXIF=MagickFalse;
9919         mng_info->ping_exclude_gAMA=MagickFalse;
9920         mng_info->ping_exclude_iCCP=MagickFalse;
9921         /* mng_info->ping_exclude_iTXt=MagickFalse; */
9922         mng_info->ping_exclude_oFFs=MagickFalse;
9923         mng_info->ping_exclude_pHYs=MagickFalse;
9924         mng_info->ping_exclude_sRGB=MagickFalse;
9925         mng_info->ping_exclude_tEXt=MagickFalse;
9926         mng_info->ping_exclude_tRNS=MagickFalse;
9927         mng_info->ping_exclude_vpAg=MagickFalse;
9928         mng_info->ping_exclude_zCCP=MagickFalse;
9929         mng_info->ping_exclude_zTXt=MagickFalse;
9930       }
9931
9932       if (LocaleNCompare(value+i,"bkgd",4) == 0)
9933         mng_info->ping_exclude_bKGD=MagickTrue;
9934
9935       if (LocaleNCompare(value+i,"chrm",4) == 0)
9936         mng_info->ping_exclude_cHRM=MagickTrue;
9937
9938       if (LocaleNCompare(value+i,"exif",4) == 0)
9939         mng_info->ping_exclude_EXIF=MagickTrue;
9940
9941       if (LocaleNCompare(value+i,"gama",4) == 0)
9942         mng_info->ping_exclude_gAMA=MagickTrue;
9943
9944       if (LocaleNCompare(value+i,"iccp",4) == 0)
9945         mng_info->ping_exclude_iCCP=MagickTrue;
9946
9947     /*
9948       if (LocaleNCompare(value+i,"itxt",4) == 0)
9949         mng_info->ping_exclude_iTXt=MagickTrue;
9950      */
9951
9952       if (LocaleNCompare(value+i,"gama",4) == 0)
9953         mng_info->ping_exclude_gAMA=MagickTrue;
9954
9955       if (LocaleNCompare(value+i,"offs",4) == 0)
9956         mng_info->ping_exclude_oFFs=MagickTrue;
9957
9958       if (LocaleNCompare(value+i,"phys",4) == 0)
9959         mng_info->ping_exclude_pHYs=MagickTrue;
9960
9961       if (LocaleNCompare(value+i,"srgb",4) == 0)
9962         mng_info->ping_exclude_sRGB=MagickTrue;
9963
9964       if (LocaleNCompare(value+i,"text",4) == 0)
9965         mng_info->ping_exclude_tEXt=MagickTrue;
9966
9967       if (LocaleNCompare(value+i,"trns",4) == 0)
9968         mng_info->ping_exclude_tRNS=MagickTrue;
9969
9970       if (LocaleNCompare(value+i,"vpag",4) == 0)
9971         mng_info->ping_exclude_vpAg=MagickTrue;
9972
9973       if (LocaleNCompare(value+i,"zccp",4) == 0)
9974         mng_info->ping_exclude_zCCP=MagickTrue;
9975
9976       if (LocaleNCompare(value+i,"ztxt",4) == 0)
9977         mng_info->ping_exclude_zTXt=MagickTrue;
9978
9979       }
9980     }
9981   }
9982
9983   for (source=0; source<1; source++)
9984   {
9985     if (source==0)
9986       {
9987        value=GetImageArtifact(image,"png:include-chunk");
9988
9989        if (value == NULL)
9990          value=GetImageArtifact(image,"png:include-chunks");
9991       }
9992     else
9993       {
9994        value=GetImageOption(image_info,"png:include-chunk");
9995
9996        if (value == NULL)
9997          value=GetImageOption(image_info,"png:include-chunks");
9998       }
9999
10000     if (value != NULL)
10001     {
10002     size_t
10003       last;
10004
10005     excluding=MagickTrue;
10006
10007     if (logging != MagickFalse)
10008       {
10009         if (source == 0)
10010            (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10011               "  png:include-chunk=%s found in image artifacts.\n", value);
10012         else
10013            (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10014               "  png:include-chunk=%s found in image properties.\n", value);
10015       }
10016
10017     last=strlen(value);
10018
10019     for (i=0; i<(int) last; i+=5)
10020       {
10021       if (LocaleNCompare(value+i,"all",3) == 0)
10022         {
10023           mng_info->ping_exclude_bKGD=MagickFalse;
10024           mng_info->ping_exclude_cHRM=MagickFalse;
10025           mng_info->ping_exclude_EXIF=MagickFalse;
10026           mng_info->ping_exclude_gAMA=MagickFalse;
10027           mng_info->ping_exclude_iCCP=MagickFalse;
10028           /* mng_info->ping_exclude_iTXt=MagickFalse; */
10029           mng_info->ping_exclude_oFFs=MagickFalse;
10030           mng_info->ping_exclude_pHYs=MagickFalse;
10031           mng_info->ping_exclude_sRGB=MagickFalse;
10032           mng_info->ping_exclude_tEXt=MagickFalse;
10033           mng_info->ping_exclude_tRNS=MagickFalse;
10034           mng_info->ping_exclude_vpAg=MagickFalse;
10035           mng_info->ping_exclude_zCCP=MagickFalse;
10036           mng_info->ping_exclude_zTXt=MagickFalse;
10037           i--;
10038         }
10039
10040       if (LocaleNCompare(value+i,"none",4) == 0)
10041         {
10042           mng_info->ping_exclude_bKGD=MagickTrue;
10043           mng_info->ping_exclude_cHRM=MagickTrue;
10044           mng_info->ping_exclude_EXIF=MagickTrue;
10045           mng_info->ping_exclude_gAMA=MagickTrue;
10046           mng_info->ping_exclude_iCCP=MagickTrue;
10047           /* mng_info->ping_exclude_iTXt=MagickTrue; */
10048           mng_info->ping_exclude_oFFs=MagickTrue;
10049           mng_info->ping_exclude_pHYs=MagickTrue;
10050           mng_info->ping_exclude_sRGB=MagickTrue;
10051           mng_info->ping_exclude_tEXt=MagickTrue;
10052           mng_info->ping_exclude_tRNS=MagickTrue;
10053           mng_info->ping_exclude_vpAg=MagickTrue;
10054           mng_info->ping_exclude_zCCP=MagickTrue;
10055           mng_info->ping_exclude_zTXt=MagickTrue;
10056         }
10057
10058       if (LocaleNCompare(value+i,"bkgd",4) == 0)
10059         mng_info->ping_exclude_bKGD=MagickFalse;
10060
10061       if (LocaleNCompare(value+i,"chrm",4) == 0)
10062         mng_info->ping_exclude_cHRM=MagickFalse;
10063
10064       if (LocaleNCompare(value+i,"exif",4) == 0)
10065         mng_info->ping_exclude_EXIF=MagickFalse;
10066
10067       if (LocaleNCompare(value+i,"gama",4) == 0)
10068         mng_info->ping_exclude_gAMA=MagickFalse;
10069
10070       if (LocaleNCompare(value+i,"iccp",4) == 0)
10071         mng_info->ping_exclude_iCCP=MagickFalse;
10072
10073     /*
10074       if (LocaleNCompare(value+i,"itxt",4) == 0)
10075         mng_info->ping_exclude_iTXt=MagickFalse;
10076      */
10077
10078       if (LocaleNCompare(value+i,"gama",4) == 0)
10079         mng_info->ping_exclude_gAMA=MagickFalse;
10080
10081       if (LocaleNCompare(value+i,"offs",4) == 0)
10082         mng_info->ping_exclude_oFFs=MagickFalse;
10083
10084       if (LocaleNCompare(value+i,"phys",4) == 0)
10085         mng_info->ping_exclude_pHYs=MagickFalse;
10086
10087       if (LocaleNCompare(value+i,"srgb",4) == 0)
10088         mng_info->ping_exclude_sRGB=MagickFalse;
10089
10090       if (LocaleNCompare(value+i,"text",4) == 0)
10091         mng_info->ping_exclude_tEXt=MagickFalse;
10092
10093       if (LocaleNCompare(value+i,"trns",4) == 0)
10094         mng_info->ping_exclude_tRNS=MagickFalse;
10095
10096       if (LocaleNCompare(value+i,"vpag",4) == 0)
10097         mng_info->ping_exclude_vpAg=MagickFalse;
10098
10099       if (LocaleNCompare(value+i,"zccp",4) == 0)
10100         mng_info->ping_exclude_zCCP=MagickFalse;
10101
10102       if (LocaleNCompare(value+i,"ztxt",4) == 0)
10103         mng_info->ping_exclude_zTXt=MagickFalse;
10104
10105       }
10106     }
10107   }
10108
10109   if (excluding != MagickFalse && logging != MagickFalse)
10110   {
10111     (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10112       "  Chunks to be excluded from the output PNG:");
10113     if (mng_info->ping_exclude_bKGD != MagickFalse)
10114       (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10115           "    bKGD");
10116     if (mng_info->ping_exclude_cHRM != MagickFalse)
10117       (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10118           "    cHRM");
10119     if (mng_info->ping_exclude_EXIF != MagickFalse)
10120       (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10121           "    EXIF");
10122     if (mng_info->ping_exclude_gAMA != MagickFalse)
10123       (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10124           "    gAMA");
10125     if (mng_info->ping_exclude_iCCP != MagickFalse)
10126       (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10127           "    iCCP");
10128 /*
10129     if (mng_info->ping_exclude_iTXt != MagickFalse)
10130       (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10131           "    iTXt");
10132 */
10133     if (mng_info->ping_exclude_oFFs != MagickFalse)
10134       (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10135           "    oFFs");
10136     if (mng_info->ping_exclude_pHYs != MagickFalse)
10137       (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10138           "    pHYs");
10139     if (mng_info->ping_exclude_sRGB != MagickFalse)
10140       (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10141           "    sRGB");
10142     if (mng_info->ping_exclude_tEXt != MagickFalse)
10143       (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10144           "    tEXt");
10145     if (mng_info->ping_exclude_tRNS != MagickFalse)
10146       (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10147           "    tRNS");
10148     if (mng_info->ping_exclude_vpAg != MagickFalse)
10149       (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10150           "    vpAg");
10151     if (mng_info->ping_exclude_zCCP != MagickFalse)
10152       (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10153           "    zCCP");
10154     if (mng_info->ping_exclude_zTXt != MagickFalse)
10155       (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10156           "    zTXt");
10157   }
10158
10159   mng_info->need_blob = MagickTrue;
10160
10161   status=WriteOnePNGImage(mng_info,image_info,image);
10162
10163   MngInfoFreeStruct(mng_info,&have_mng_structure);
10164
10165   if (logging != MagickFalse)
10166     (void) LogMagickEvent(CoderEvent,GetMagickModule(),"exit WritePNGImage()");
10167
10168   return(status);
10169 }
10170
10171 #if defined(JNG_SUPPORTED)
10172
10173 /* Write one JNG image */
10174 static MagickBooleanType WriteOneJNGImage(MngInfo *mng_info,
10175    const ImageInfo *image_info,Image *image)
10176 {
10177   Image
10178     *jpeg_image;
10179
10180   ImageInfo
10181     *jpeg_image_info;
10182
10183   MagickBooleanType
10184     logging,
10185     status;
10186
10187   size_t
10188     length;
10189
10190   unsigned char
10191     *blob,
10192     chunk[80],
10193     *p;
10194
10195   unsigned int
10196     jng_alpha_compression_method,
10197     jng_alpha_sample_depth,
10198     jng_color_type,
10199     transparent;
10200
10201   size_t
10202     jng_quality;
10203
10204   logging=LogMagickEvent(CoderEvent,GetMagickModule(),
10205     "  Enter WriteOneJNGImage()");
10206
10207   blob=(unsigned char *) NULL;
10208   jpeg_image=(Image *) NULL;
10209   jpeg_image_info=(ImageInfo *) NULL;
10210
10211   status=MagickTrue;
10212   transparent=image_info->type==GrayscaleMatteType ||
10213      image_info->type==TrueColorMatteType;
10214   jng_color_type=10;
10215   jng_alpha_sample_depth=0;
10216   jng_quality=image_info->quality == 0UL ? 75UL : image_info->quality;
10217   jng_alpha_compression_method=0;
10218
10219   if (image->matte != MagickFalse)
10220     {
10221       /* if any pixels are transparent */
10222       transparent=MagickTrue;
10223       if (image_info->compression==JPEGCompression)
10224         jng_alpha_compression_method=8;
10225     }
10226
10227   if (transparent)
10228     {
10229       jng_color_type=14;
10230
10231       /* Create JPEG blob, image, and image_info */
10232       if (logging != MagickFalse)
10233         (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10234           "  Creating jpeg_image_info for opacity.");
10235
10236       jpeg_image_info=(ImageInfo *) CloneImageInfo(image_info);
10237
10238       if (jpeg_image_info == (ImageInfo *) NULL)
10239         ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
10240
10241       if (logging != MagickFalse)
10242         (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10243           "  Creating jpeg_image.");
10244
10245       jpeg_image=CloneImage(image,0,0,MagickTrue,&image->exception);
10246
10247       if (jpeg_image == (Image *) NULL)
10248         ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
10249
10250       (void) CopyMagickString(jpeg_image->magick,"JPEG",MaxTextExtent);
10251       status=SeparateImageChannel(jpeg_image,OpacityChannel);
10252       status=NegateImage(jpeg_image,MagickFalse);
10253       jpeg_image->matte=MagickFalse;
10254
10255       if (jng_quality >= 1000)
10256         jpeg_image_info->quality=jng_quality/1000;
10257
10258       else
10259         jpeg_image_info->quality=jng_quality;
10260
10261       jpeg_image_info->type=GrayscaleType;
10262       (void) SetImageType(jpeg_image,GrayscaleType);
10263       (void) AcquireUniqueFilename(jpeg_image->filename);
10264       (void) FormatMagickString(jpeg_image_info->filename,MaxTextExtent,
10265         "%s",jpeg_image->filename);
10266     }
10267
10268   /* To do: check bit depth of PNG alpha channel */
10269
10270   /* Check if image is grayscale. */
10271   if (image_info->type != TrueColorMatteType && image_info->type !=
10272     TrueColorType && ImageIsGray(image))
10273     jng_color_type-=2;
10274
10275   if (transparent)
10276     {
10277       if (jng_alpha_compression_method==0)
10278         {
10279           const char
10280             *value;
10281
10282           /* Encode opacity as a grayscale PNG blob */
10283           status=OpenBlob(jpeg_image_info,jpeg_image,WriteBinaryBlobMode,
10284             &image->exception);
10285           if (logging != MagickFalse)
10286             (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10287               "  Creating PNG blob.");
10288           length=0;
10289
10290           (void) CopyMagickString(jpeg_image_info->magick,"PNG",MaxTextExtent);
10291           (void) CopyMagickString(jpeg_image->magick,"PNG",MaxTextExtent);
10292           jpeg_image_info->interlace=NoInterlace;
10293
10294           blob=ImageToBlob(jpeg_image_info,jpeg_image,&length,
10295             &image->exception);
10296
10297           /* Retrieve sample depth used */
10298           value=GetImageProperty(jpeg_image,"png:bit-depth-written");
10299           if (value != (char *) NULL)
10300             jng_alpha_sample_depth= (unsigned int) value[0];
10301         }
10302       else
10303         {
10304           /* Encode opacity as a grayscale JPEG blob */
10305
10306           status=OpenBlob(jpeg_image_info,jpeg_image,WriteBinaryBlobMode,
10307             &image->exception);
10308
10309           (void) CopyMagickString(jpeg_image_info->magick,"JPEG",MaxTextExtent);
10310           (void) CopyMagickString(jpeg_image->magick,"JPEG",MaxTextExtent);
10311           jpeg_image_info->interlace=NoInterlace;
10312           if (logging != MagickFalse)
10313             (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10314               "  Creating blob.");
10315           blob=ImageToBlob(jpeg_image_info,jpeg_image,&length,
10316            &image->exception);
10317           jng_alpha_sample_depth=8;
10318
10319           if (logging != MagickFalse)
10320             (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10321               "  Successfully read jpeg_image into a blob, length=%.20g.",
10322               (double) length);
10323
10324         }
10325       /* Destroy JPEG image and image_info */
10326       jpeg_image=DestroyImage(jpeg_image);
10327       (void) RelinquishUniqueFileResource(jpeg_image_info->filename);
10328       jpeg_image_info=DestroyImageInfo(jpeg_image_info);
10329     }
10330
10331   /* Write JHDR chunk */
10332   (void) WriteBlobMSBULong(image,16L);  /* chunk data length=16 */
10333   PNGType(chunk,mng_JHDR);
10334   LogPNGChunk(logging,mng_JHDR,16L);
10335   PNGLong(chunk+4,(png_uint_32) image->columns);
10336   PNGLong(chunk+8,(png_uint_32) image->rows);
10337   chunk[12]=jng_color_type;
10338   chunk[13]=8;  /* sample depth */
10339   chunk[14]=8; /*jng_image_compression_method */
10340   chunk[15]=(unsigned char) (image_info->interlace == NoInterlace ? 0 : 8);
10341   chunk[16]=jng_alpha_sample_depth;
10342   chunk[17]=jng_alpha_compression_method;
10343   chunk[18]=0; /*jng_alpha_filter_method */
10344   chunk[19]=0; /*jng_alpha_interlace_method */
10345   (void) WriteBlob(image,20,chunk);
10346   (void) WriteBlobMSBULong(image,crc32(0,chunk,20));
10347   if (logging != MagickFalse)
10348     {
10349       (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10350         "    JNG width:%15lu",(unsigned long) image->columns);
10351
10352       (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10353         "    JNG height:%14lu",(unsigned long) image->rows);
10354
10355       (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10356         "    JNG color type:%10d",jng_color_type);
10357
10358       (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10359         "    JNG sample depth:%8d",8);
10360
10361       (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10362         "    JNG compression:%9d",8);
10363
10364       (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10365         "    JNG interlace:%11d",0);
10366
10367       (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10368         "    JNG alpha depth:%9d",jng_alpha_sample_depth);
10369
10370       (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10371         "    JNG alpha compression:%3d",jng_alpha_compression_method);
10372
10373       (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10374         "    JNG alpha filter:%8d",0);
10375
10376       (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10377         "    JNG alpha interlace:%5d",0);
10378     }
10379
10380   /* Write any JNG-chunk-b profiles */
10381   (void) Magick_png_write_chunk_from_profile(image,"JNG-chunk-b",logging);
10382
10383   /*
10384      Write leading ancillary chunks
10385   */
10386
10387   if (transparent)
10388   {
10389     /*
10390       Write JNG bKGD chunk
10391     */
10392
10393     unsigned char
10394       blue,
10395       green,
10396       red;
10397
10398     ssize_t
10399       num_bytes;
10400
10401     if (jng_color_type == 8 || jng_color_type == 12)
10402       num_bytes=6L;
10403     else
10404       num_bytes=10L;
10405     (void) WriteBlobMSBULong(image,(size_t) (num_bytes-4L));
10406     PNGType(chunk,mng_bKGD);
10407     LogPNGChunk(logging,mng_bKGD,(size_t) (num_bytes-4L));
10408     red=ScaleQuantumToChar(image->background_color.red);
10409     green=ScaleQuantumToChar(image->background_color.green);
10410     blue=ScaleQuantumToChar(image->background_color.blue);
10411     *(chunk+4)=0;
10412     *(chunk+5)=red;
10413     *(chunk+6)=0;
10414     *(chunk+7)=green;
10415     *(chunk+8)=0;
10416     *(chunk+9)=blue;
10417     (void) WriteBlob(image,(size_t) num_bytes,chunk);
10418     (void) WriteBlobMSBULong(image,crc32(0,chunk,(uInt) num_bytes));
10419   }
10420
10421   if ((image->colorspace == sRGBColorspace || image->rendering_intent))
10422     {
10423       /*
10424         Write JNG sRGB chunk
10425       */
10426       (void) WriteBlobMSBULong(image,1L);
10427       PNGType(chunk,mng_sRGB);
10428       LogPNGChunk(logging,mng_sRGB,1L);
10429
10430       if (image->rendering_intent != UndefinedIntent)
10431         chunk[4]=(unsigned char)
10432           Magick_RenderingIntent_to_PNG_RenderingIntent(
10433           (image->rendering_intent));
10434
10435       else
10436         chunk[4]=(unsigned char)
10437           Magick_RenderingIntent_to_PNG_RenderingIntent(
10438           (PerceptualIntent));
10439
10440       (void) WriteBlob(image,5,chunk);
10441       (void) WriteBlobMSBULong(image,crc32(0,chunk,5));
10442     }
10443   else
10444     {
10445       if (image->gamma != 0.0)
10446         {
10447           /*
10448              Write JNG gAMA chunk
10449           */
10450           (void) WriteBlobMSBULong(image,4L);
10451           PNGType(chunk,mng_gAMA);
10452           LogPNGChunk(logging,mng_gAMA,4L);
10453           PNGLong(chunk+4,(png_uint_32) (100000*image->gamma+0.5));
10454           (void) WriteBlob(image,8,chunk);
10455           (void) WriteBlobMSBULong(image,crc32(0,chunk,8));
10456         }
10457
10458       if ((mng_info->equal_chrms == MagickFalse) &&
10459           (image->chromaticity.red_primary.x != 0.0))
10460         {
10461           PrimaryInfo
10462             primary;
10463
10464           /*
10465              Write JNG cHRM chunk
10466           */
10467           (void) WriteBlobMSBULong(image,32L);
10468           PNGType(chunk,mng_cHRM);
10469           LogPNGChunk(logging,mng_cHRM,32L);
10470           primary=image->chromaticity.white_point;
10471           PNGLong(chunk+4,(png_uint_32) (100000*primary.x+0.5));
10472           PNGLong(chunk+8,(png_uint_32) (100000*primary.y+0.5));
10473           primary=image->chromaticity.red_primary;
10474           PNGLong(chunk+12,(png_uint_32) (100000*primary.x+0.5));
10475           PNGLong(chunk+16,(png_uint_32) (100000*primary.y+0.5));
10476           primary=image->chromaticity.green_primary;
10477           PNGLong(chunk+20,(png_uint_32) (100000*primary.x+0.5));
10478           PNGLong(chunk+24,(png_uint_32) (100000*primary.y+0.5));
10479           primary=image->chromaticity.blue_primary;
10480           PNGLong(chunk+28,(png_uint_32) (100000*primary.x+0.5));
10481           PNGLong(chunk+32,(png_uint_32) (100000*primary.y+0.5));
10482           (void) WriteBlob(image,36,chunk);
10483           (void) WriteBlobMSBULong(image,crc32(0,chunk,36));
10484         }
10485     }
10486
10487   if (image->x_resolution && image->y_resolution && !mng_info->equal_physs)
10488     {
10489       /*
10490          Write JNG pHYs chunk
10491       */
10492       (void) WriteBlobMSBULong(image,9L);
10493       PNGType(chunk,mng_pHYs);
10494       LogPNGChunk(logging,mng_pHYs,9L);
10495       if (image->units == PixelsPerInchResolution)
10496         {
10497           PNGLong(chunk+4,(png_uint_32)
10498             (image->x_resolution*100.0/2.54+0.5));
10499
10500           PNGLong(chunk+8,(png_uint_32)
10501             (image->y_resolution*100.0/2.54+0.5));
10502
10503           chunk[12]=1;
10504         }
10505
10506       else
10507         {
10508           if (image->units == PixelsPerCentimeterResolution)
10509             {
10510               PNGLong(chunk+4,(png_uint_32)
10511                 (image->x_resolution*100.0+0.5));
10512
10513               PNGLong(chunk+8,(png_uint_32)
10514                 (image->y_resolution*100.0+0.5));
10515
10516               chunk[12]=1;
10517             }
10518
10519           else
10520             {
10521               PNGLong(chunk+4,(png_uint_32) (image->x_resolution+0.5));
10522               PNGLong(chunk+8,(png_uint_32) (image->y_resolution+0.5));
10523               chunk[12]=0;
10524             }
10525         }
10526       (void) WriteBlob(image,13,chunk);
10527       (void) WriteBlobMSBULong(image,crc32(0,chunk,13));
10528     }
10529
10530   if (mng_info->write_mng == 0 && (image->page.x || image->page.y))
10531     {
10532       /*
10533          Write JNG oFFs chunk
10534       */
10535       (void) WriteBlobMSBULong(image,9L);
10536       PNGType(chunk,mng_oFFs);
10537       LogPNGChunk(logging,mng_oFFs,9L);
10538       PNGsLong(chunk+4,(ssize_t) (image->page.x));
10539       PNGsLong(chunk+8,(ssize_t) (image->page.y));
10540       chunk[12]=0;
10541       (void) WriteBlob(image,13,chunk);
10542       (void) WriteBlobMSBULong(image,crc32(0,chunk,13));
10543     }
10544   if (mng_info->write_mng == 0 && (image->page.width || image->page.height))
10545     {
10546        (void) WriteBlobMSBULong(image,9L);  /* data length=8 */
10547        PNGType(chunk,mng_vpAg);
10548        LogPNGChunk(logging,mng_vpAg,9L);
10549        PNGLong(chunk+4,(png_uint_32) image->page.width);
10550        PNGLong(chunk+8,(png_uint_32) image->page.height);
10551        chunk[12]=0;   /* unit = pixels */
10552        (void) WriteBlob(image,13,chunk);
10553        (void) WriteBlobMSBULong(image,crc32(0,chunk,13));
10554     }
10555
10556
10557   if (transparent)
10558     {
10559       if (jng_alpha_compression_method==0)
10560         {
10561           register ssize_t
10562             i;
10563
10564           ssize_t
10565             len;
10566
10567           /* Write IDAT chunk header */
10568           if (logging != MagickFalse)
10569             (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10570               "  Write IDAT chunks from blob, length=%.20g.",(double)
10571               length);
10572
10573           /* Copy IDAT chunks */
10574           len=0;
10575           p=blob+8;
10576           for (i=8; i<(ssize_t) length; i+=len+12)
10577           {
10578             len=(*p<<24)|((*(p+1))<<16)|((*(p+2))<<8)|(*(p+3));
10579             p+=4;
10580
10581             if (*(p)==73 && *(p+1)==68 && *(p+2)==65 && *(p+3)==84) /* IDAT */
10582               {
10583                 /* Found an IDAT chunk. */
10584                 (void) WriteBlobMSBULong(image,(size_t) len);
10585                 LogPNGChunk(logging,mng_IDAT,(size_t) len);
10586                 (void) WriteBlob(image,(size_t) len+4,p);
10587                 (void) WriteBlobMSBULong(image,
10588                     crc32(0,p,(uInt) len+4));
10589               }
10590
10591             else
10592               {
10593                 if (logging != MagickFalse)
10594                   (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10595                     "    Skipping %c%c%c%c chunk, length=%.20g.",
10596                     *(p),*(p+1),*(p+2),*(p+3),(double) len);
10597               }
10598             p+=(8+len);
10599           }
10600         }
10601       else
10602         {
10603           /* Write JDAA chunk header */
10604           if (logging != MagickFalse)
10605             (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10606               "  Write JDAA chunk, length=%.20g.",(double) length);
10607           (void) WriteBlobMSBULong(image,(size_t) length);
10608           PNGType(chunk,mng_JDAA);
10609           LogPNGChunk(logging,mng_JDAA,length);
10610           /* Write JDAT chunk(s) data */
10611           (void) WriteBlob(image,4,chunk);
10612           (void) WriteBlob(image,length,blob);
10613           (void) WriteBlobMSBULong(image,crc32(crc32(0,chunk,4),blob,
10614              (uInt) length));
10615         }
10616       blob=(unsigned char *) RelinquishMagickMemory(blob);
10617     }
10618
10619   /* Encode image as a JPEG blob */
10620   if (logging != MagickFalse)
10621     (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10622       "  Creating jpeg_image_info.");
10623   jpeg_image_info=(ImageInfo *) CloneImageInfo(image_info);
10624   if (jpeg_image_info == (ImageInfo *) NULL)
10625     ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
10626
10627   if (logging != MagickFalse)
10628     (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10629       "  Creating jpeg_image.");
10630
10631   jpeg_image=CloneImage(image,0,0,MagickTrue,&image->exception);
10632   if (jpeg_image == (Image *) NULL)
10633     ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
10634   (void) CopyMagickString(jpeg_image->magick,"JPEG",MaxTextExtent);
10635
10636   (void) AcquireUniqueFilename(jpeg_image->filename);
10637   (void) FormatMagickString(jpeg_image_info->filename,MaxTextExtent,"%s",
10638     jpeg_image->filename);
10639
10640   status=OpenBlob(jpeg_image_info,jpeg_image,WriteBinaryBlobMode,
10641     &image->exception);
10642
10643   if (logging != MagickFalse)
10644     (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10645       "  Created jpeg_image, %.20g x %.20g.",(double) jpeg_image->columns,
10646       (double) jpeg_image->rows);
10647
10648   if (jng_color_type == 8 || jng_color_type == 12)
10649     jpeg_image_info->type=GrayscaleType;
10650
10651   jpeg_image_info->quality=jng_quality % 1000;
10652   (void) CopyMagickString(jpeg_image_info->magick,"JPEG",MaxTextExtent);
10653   (void) CopyMagickString(jpeg_image->magick,"JPEG",MaxTextExtent);
10654
10655   if (logging != MagickFalse)
10656     (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10657       "  Creating blob.");
10658
10659   blob=ImageToBlob(jpeg_image_info,jpeg_image,&length,&image->exception);
10660
10661   if (logging != MagickFalse)
10662     {
10663       (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10664         "  Successfully read jpeg_image into a blob, length=%.20g.",
10665         (double) length);
10666
10667       (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10668         "  Write JDAT chunk, length=%.20g.",(double) length);
10669     }
10670
10671   /* Write JDAT chunk(s) */
10672   (void) WriteBlobMSBULong(image,(size_t) length);
10673   PNGType(chunk,mng_JDAT);
10674   LogPNGChunk(logging,mng_JDAT,length);
10675   (void) WriteBlob(image,4,chunk);
10676   (void) WriteBlob(image,length,blob);
10677   (void) WriteBlobMSBULong(image,crc32(crc32(0,chunk,4),blob,(uInt) length));
10678
10679   jpeg_image=DestroyImage(jpeg_image);
10680   (void) RelinquishUniqueFileResource(jpeg_image_info->filename);
10681   jpeg_image_info=DestroyImageInfo(jpeg_image_info);
10682   blob=(unsigned char *) RelinquishMagickMemory(blob);
10683
10684   /* Write any JNG-chunk-e profiles */
10685   (void) Magick_png_write_chunk_from_profile(image,"JNG-chunk-e",logging);
10686
10687   /* Write IEND chunk */
10688   (void) WriteBlobMSBULong(image,0L);
10689   PNGType(chunk,mng_IEND);
10690   LogPNGChunk(logging,mng_IEND,0);
10691   (void) WriteBlob(image,4,chunk);
10692   (void) WriteBlobMSBULong(image,crc32(0,chunk,4));
10693
10694   if (logging != MagickFalse)
10695     (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10696       "  exit WriteOneJNGImage()");
10697
10698   return(status);
10699 }
10700
10701
10702 /*
10703 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
10704 %                                                                             %
10705 %                                                                             %
10706 %                                                                             %
10707 %   W r i t e J N G I m a g e                                                 %
10708 %                                                                             %
10709 %                                                                             %
10710 %                                                                             %
10711 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
10712 %
10713 %  WriteJNGImage() writes a JPEG Network Graphics (JNG) image file.
10714 %
10715 %  JNG support written by Glenn Randers-Pehrson, glennrp@image...
10716 %
10717 %  The format of the WriteJNGImage method is:
10718 %
10719 %      MagickBooleanType WriteJNGImage(const ImageInfo *image_info,Image *image)
10720 %
10721 %  A description of each parameter follows:
10722 %
10723 %    o image_info: the image info.
10724 %
10725 %    o image:  The image.
10726 %
10727 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
10728 */
10729 static MagickBooleanType WriteJNGImage(const ImageInfo *image_info,Image *image)
10730 {
10731   MagickBooleanType
10732     have_mng_structure,
10733     logging,
10734     status;
10735
10736   MngInfo
10737     *mng_info;
10738
10739   /*
10740     Open image file.
10741   */
10742   assert(image_info != (const ImageInfo *) NULL);
10743   assert(image_info->signature == MagickSignature);
10744   assert(image != (Image *) NULL);
10745   assert(image->signature == MagickSignature);
10746   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
10747   logging=LogMagickEvent(CoderEvent,GetMagickModule(),"Enter WriteJNGImage()");
10748   status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
10749   if (status == MagickFalse)
10750     return(status);
10751
10752   /*
10753     Allocate a MngInfo structure.
10754   */
10755   have_mng_structure=MagickFalse;
10756   mng_info=(MngInfo *) AcquireMagickMemory(sizeof(MngInfo));
10757   if (mng_info == (MngInfo *) NULL)
10758     ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
10759   /*
10760     Initialize members of the MngInfo structure.
10761   */
10762   (void) ResetMagickMemory(mng_info,0,sizeof(MngInfo));
10763   mng_info->image=image;
10764   have_mng_structure=MagickTrue;
10765
10766   (void) WriteBlob(image,8,(const unsigned char *) "\213JNG\r\n\032\n");
10767
10768   status=WriteOneJNGImage(mng_info,image_info,image);
10769   (void) CloseBlob(image);
10770
10771   (void) CatchImageException(image);
10772   MngInfoFreeStruct(mng_info,&have_mng_structure);
10773   if (logging != MagickFalse)
10774     (void) LogMagickEvent(CoderEvent,GetMagickModule(),"exit WriteJNGImage()");
10775   return(status);
10776 }
10777 #endif
10778
10779
10780
10781 static MagickBooleanType WriteMNGImage(const ImageInfo *image_info,Image *image)
10782 {
10783   const char
10784     *option;
10785
10786   Image
10787     *next_image;
10788
10789   MagickBooleanType
10790     have_mng_structure,
10791     status;
10792
10793   volatile MagickBooleanType
10794     logging;
10795
10796   MngInfo
10797     *mng_info;
10798
10799   int
10800     image_count,
10801     need_iterations,
10802     need_matte;
10803
10804   volatile int
10805 #if defined(PNG_WRITE_EMPTY_PLTE_SUPPORTED) || \
10806     defined(PNG_MNG_FEATURES_SUPPORTED)
10807     need_local_plte,
10808 #endif
10809     all_images_are_gray,
10810     need_defi,
10811     use_global_plte;
10812
10813   register ssize_t
10814     i;
10815
10816   unsigned char
10817     chunk[800];
10818
10819   volatile unsigned int
10820     write_jng,
10821     write_mng;
10822
10823   volatile size_t
10824     scene;
10825
10826   size_t
10827     final_delay=0,
10828     initial_delay;
10829
10830 #if (PNG_LIBPNG_VER < 10200)
10831     if (image_info->verbose)
10832       printf("Your PNG library (libpng-%s) is rather old.\n",
10833          PNG_LIBPNG_VER_STRING);
10834 #endif
10835
10836   /*
10837     Open image file.
10838   */
10839   assert(image_info != (const ImageInfo *) NULL);
10840   assert(image_info->signature == MagickSignature);
10841   assert(image != (Image *) NULL);
10842   assert(image->signature == MagickSignature);
10843   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
10844   logging=LogMagickEvent(CoderEvent,GetMagickModule(),"Enter WriteMNGImage()");
10845   status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
10846   if (status == MagickFalse)
10847     return(status);
10848
10849   /*
10850     Allocate a MngInfo structure.
10851   */
10852   have_mng_structure=MagickFalse;
10853   mng_info=(MngInfo *) AcquireMagickMemory(sizeof(MngInfo));
10854   if (mng_info == (MngInfo *) NULL)
10855     ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
10856   /*
10857     Initialize members of the MngInfo structure.
10858   */
10859   (void) ResetMagickMemory(mng_info,0,sizeof(MngInfo));
10860   mng_info->image=image;
10861   have_mng_structure=MagickTrue;
10862   write_mng=LocaleCompare(image_info->magick,"MNG") == 0;
10863
10864   /*
10865    * See if user has requested a specific PNG subformat to be used
10866    * for all of the PNGs in the MNG being written, e.g.,
10867    *
10868    *    convert *.png png8:animation.mng
10869    *
10870    * To do: check -define png:bit_depth and png:color_type as well,
10871    * or perhaps use mng:bit_depth and mng:color_type instead for
10872    * global settings.
10873    */
10874
10875   mng_info->write_png8=LocaleCompare(image_info->magick,"PNG8") == 0;
10876   mng_info->write_png24=LocaleCompare(image_info->magick,"PNG24") == 0;
10877   mng_info->write_png32=LocaleCompare(image_info->magick,"PNG32") == 0;
10878
10879   write_jng=MagickFalse;
10880   if (image_info->compression == JPEGCompression)
10881     write_jng=MagickTrue;
10882
10883   mng_info->adjoin=image_info->adjoin &&
10884     (GetNextImageInList(image) != (Image *) NULL) && write_mng;
10885
10886   if (logging != MagickFalse)
10887     {
10888       /* Log some info about the input */
10889       Image
10890         *p;
10891
10892       (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10893         "  Checking input image(s)");
10894
10895       (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10896         "    Image_info depth: %.20g",(double) image_info->depth);
10897
10898       (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10899         "    Type: %d",image_info->type);
10900
10901       scene=0;
10902       for (p=image; p != (Image *) NULL; p=GetNextImageInList(p))
10903       {
10904         (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10905           "    Scene: %.20g",(double) scene++);
10906
10907         (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10908           "      Image depth: %.20g",(double) p->depth);
10909
10910         if (p->matte)
10911           (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10912             "      Matte: True");
10913
10914         else
10915           (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10916             "      Matte: False");
10917
10918         if (p->storage_class == PseudoClass)
10919           (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10920             "      Storage class: PseudoClass");
10921
10922         else
10923           (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10924             "      Storage class: DirectClass");
10925
10926         if (p->colors)
10927           (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10928             "      Number of colors: %.20g",(double) p->colors);
10929
10930         else
10931           (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10932             "      Number of colors: unspecified");
10933
10934         if (mng_info->adjoin == MagickFalse)
10935           break;
10936       }
10937     }
10938
10939   use_global_plte=MagickFalse;
10940   all_images_are_gray=MagickFalse;
10941 #ifdef PNG_WRITE_EMPTY_PLTE_SUPPORTED
10942   need_local_plte=MagickTrue;
10943 #endif
10944   need_defi=MagickFalse;
10945   need_matte=MagickFalse;
10946   mng_info->framing_mode=1;
10947   mng_info->old_framing_mode=1;
10948
10949   if (write_mng)
10950       if (image_info->page != (char *) NULL)
10951         {
10952           /*
10953             Determine image bounding box.
10954           */
10955           SetGeometry(image,&mng_info->page);
10956           (void) ParseMetaGeometry(image_info->page,&mng_info->page.x,
10957             &mng_info->page.y,&mng_info->page.width,&mng_info->page.height);
10958         }
10959   if (write_mng)
10960     {
10961       unsigned int
10962         need_geom;
10963
10964       unsigned short
10965         red,
10966         green,
10967         blue;
10968
10969       mng_info->page=image->page;
10970       need_geom=MagickTrue;
10971       if (mng_info->page.width || mng_info->page.height)
10972          need_geom=MagickFalse;
10973       /*
10974         Check all the scenes.
10975       */
10976       initial_delay=image->delay;
10977       need_iterations=MagickFalse;
10978       mng_info->equal_chrms=image->chromaticity.red_primary.x != 0.0;
10979       mng_info->equal_physs=MagickTrue,
10980       mng_info->equal_gammas=MagickTrue;
10981       mng_info->equal_srgbs=MagickTrue;
10982       mng_info->equal_backgrounds=MagickTrue;
10983       image_count=0;
10984 #if defined(PNG_WRITE_EMPTY_PLTE_SUPPORTED) || \
10985     defined(PNG_MNG_FEATURES_SUPPORTED)
10986       all_images_are_gray=MagickTrue;
10987       mng_info->equal_palettes=MagickFalse;
10988       need_local_plte=MagickFalse;
10989 #endif
10990       for (next_image=image; next_image != (Image *) NULL; )
10991       {
10992         if (need_geom)
10993           {
10994             if ((next_image->columns+next_image->page.x) > mng_info->page.width)
10995               mng_info->page.width=next_image->columns+next_image->page.x;
10996
10997             if ((next_image->rows+next_image->page.y) > mng_info->page.height)
10998               mng_info->page.height=next_image->rows+next_image->page.y;
10999           }
11000
11001         if (next_image->page.x || next_image->page.y)
11002           need_defi=MagickTrue;
11003
11004         if (next_image->matte)
11005           need_matte=MagickTrue;
11006
11007         if ((int) next_image->dispose >= BackgroundDispose)
11008           if (next_image->matte || next_image->page.x || next_image->page.y ||
11009               ((next_image->columns < mng_info->page.width) &&
11010                (next_image->rows < mng_info->page.height)))
11011             mng_info->need_fram=MagickTrue;
11012
11013         if (next_image->iterations)
11014           need_iterations=MagickTrue;
11015
11016         final_delay=next_image->delay;
11017
11018         if (final_delay != initial_delay || final_delay > 1UL*
11019            next_image->ticks_per_second)
11020           mng_info->need_fram=1;
11021
11022 #if defined(PNG_WRITE_EMPTY_PLTE_SUPPORTED) || \
11023     defined(PNG_MNG_FEATURES_SUPPORTED)
11024         /*
11025           check for global palette possibility.
11026         */
11027         if (image->matte != MagickFalse)
11028            need_local_plte=MagickTrue;
11029
11030         if (need_local_plte == 0)
11031           {
11032             if (ImageIsGray(image) == MagickFalse)
11033               all_images_are_gray=MagickFalse;
11034             mng_info->equal_palettes=PalettesAreEqual(image,next_image);
11035             if (use_global_plte == 0)
11036               use_global_plte=mng_info->equal_palettes;
11037             need_local_plte=!mng_info->equal_palettes;
11038           }
11039 #endif
11040         if (GetNextImageInList(next_image) != (Image *) NULL)
11041           {
11042             if (next_image->background_color.red !=
11043                 next_image->next->background_color.red ||
11044                 next_image->background_color.green !=
11045                 next_image->next->background_color.green ||
11046                 next_image->background_color.blue !=
11047                 next_image->next->background_color.blue)
11048               mng_info->equal_backgrounds=MagickFalse;
11049
11050             if (next_image->gamma != next_image->next->gamma)
11051               mng_info->equal_gammas=MagickFalse;
11052
11053             if (next_image->rendering_intent !=
11054                 next_image->next->rendering_intent)
11055               mng_info->equal_srgbs=MagickFalse;
11056
11057             if ((next_image->units != next_image->next->units) ||
11058                 (next_image->x_resolution != next_image->next->x_resolution) ||
11059                 (next_image->y_resolution != next_image->next->y_resolution))
11060               mng_info->equal_physs=MagickFalse;
11061
11062             if (mng_info->equal_chrms)
11063               {
11064                 if (next_image->chromaticity.red_primary.x !=
11065                     next_image->next->chromaticity.red_primary.x ||
11066                     next_image->chromaticity.red_primary.y !=
11067                     next_image->next->chromaticity.red_primary.y ||
11068                     next_image->chromaticity.green_primary.x !=
11069                     next_image->next->chromaticity.green_primary.x ||
11070                     next_image->chromaticity.green_primary.y !=
11071                     next_image->next->chromaticity.green_primary.y ||
11072                     next_image->chromaticity.blue_primary.x !=
11073                     next_image->next->chromaticity.blue_primary.x ||
11074                     next_image->chromaticity.blue_primary.y !=
11075                     next_image->next->chromaticity.blue_primary.y ||
11076                     next_image->chromaticity.white_point.x !=
11077                     next_image->next->chromaticity.white_point.x ||
11078                     next_image->chromaticity.white_point.y !=
11079                     next_image->next->chromaticity.white_point.y)
11080                   mng_info->equal_chrms=MagickFalse;
11081               }
11082           }
11083         image_count++;
11084         next_image=GetNextImageInList(next_image);
11085       }
11086       if (image_count < 2)
11087         {
11088           mng_info->equal_backgrounds=MagickFalse;
11089           mng_info->equal_chrms=MagickFalse;
11090           mng_info->equal_gammas=MagickFalse;
11091           mng_info->equal_srgbs=MagickFalse;
11092           mng_info->equal_physs=MagickFalse;
11093           use_global_plte=MagickFalse;
11094 #ifdef PNG_WRITE_EMPTY_PLTE_SUPPORTED
11095           need_local_plte=MagickTrue;
11096 #endif
11097           need_iterations=MagickFalse;
11098         }
11099
11100      if (mng_info->need_fram == MagickFalse)
11101        {
11102          /*
11103            Only certain framing rates 100/n are exactly representable without
11104            the FRAM chunk but we'll allow some slop in VLC files
11105          */
11106          if (final_delay == 0)
11107            {
11108              if (need_iterations != MagickFalse)
11109                {
11110                  /*
11111                    It's probably a GIF with loop; don't run it *too* fast.
11112                  */
11113                  if (mng_info->adjoin)
11114                    {
11115                      final_delay=10;
11116                      (void) ThrowMagickException(&image->exception,
11117                         GetMagickModule(),CoderWarning,
11118                        "input has zero delay between all frames; assuming",
11119                        " 10 cs `%s'","");
11120                    }
11121                }
11122              else
11123                mng_info->ticks_per_second=0;
11124            }
11125          if (final_delay != 0)
11126            mng_info->ticks_per_second=(png_uint_32)
11127               (image->ticks_per_second/final_delay);
11128          if (final_delay > 50)
11129            mng_info->ticks_per_second=2;
11130
11131          if (final_delay > 75)
11132            mng_info->ticks_per_second=1;
11133
11134          if (final_delay > 125)
11135            mng_info->need_fram=MagickTrue;
11136
11137          if (need_defi && final_delay > 2 && (final_delay != 4) &&
11138             (final_delay != 5) && (final_delay != 10) && (final_delay != 20) &&
11139             (final_delay != 25) && (final_delay != 50) && (final_delay !=
11140                1UL*image->ticks_per_second))
11141            mng_info->need_fram=MagickTrue;  /* make it exact; cannot be VLC */
11142        }
11143
11144      if (mng_info->need_fram != MagickFalse)
11145         mng_info->ticks_per_second=1UL*image->ticks_per_second;
11146      /*
11147         If pseudocolor, we should also check to see if all the
11148         palettes are identical and write a global PLTE if they are.
11149         ../glennrp Feb 99.
11150      */
11151      /*
11152         Write the MNG version 1.0 signature and MHDR chunk.
11153      */
11154      (void) WriteBlob(image,8,(const unsigned char *) "\212MNG\r\n\032\n");
11155      (void) WriteBlobMSBULong(image,28L);  /* chunk data length=28 */
11156      PNGType(chunk,mng_MHDR);
11157      LogPNGChunk(logging,mng_MHDR,28L);
11158      PNGLong(chunk+4,(png_uint_32) mng_info->page.width);
11159      PNGLong(chunk+8,(png_uint_32) mng_info->page.height);
11160      PNGLong(chunk+12,mng_info->ticks_per_second);
11161      PNGLong(chunk+16,0L);  /* layer count=unknown */
11162      PNGLong(chunk+20,0L);  /* frame count=unknown */
11163      PNGLong(chunk+24,0L);  /* play time=unknown   */
11164      if (write_jng)
11165        {
11166          if (need_matte)
11167            {
11168              if (need_defi || mng_info->need_fram || use_global_plte)
11169                PNGLong(chunk+28,27L);    /* simplicity=LC+JNG */
11170
11171              else
11172                PNGLong(chunk+28,25L);    /* simplicity=VLC+JNG */
11173            }
11174
11175          else
11176            {
11177              if (need_defi || mng_info->need_fram || use_global_plte)
11178                PNGLong(chunk+28,19L);  /* simplicity=LC+JNG, no transparency */
11179
11180              else
11181                PNGLong(chunk+28,17L);  /* simplicity=VLC+JNG, no transparency */
11182            }
11183        }
11184
11185      else
11186        {
11187          if (need_matte)
11188            {
11189              if (need_defi || mng_info->need_fram || use_global_plte)
11190                PNGLong(chunk+28,11L);    /* simplicity=LC */
11191
11192              else
11193                PNGLong(chunk+28,9L);    /* simplicity=VLC */
11194            }
11195
11196          else
11197            {
11198              if (need_defi || mng_info->need_fram || use_global_plte)
11199                PNGLong(chunk+28,3L);    /* simplicity=LC, no transparency */
11200
11201              else
11202                PNGLong(chunk+28,1L);    /* simplicity=VLC, no transparency */
11203            }
11204        }
11205      (void) WriteBlob(image,32,chunk);
11206      (void) WriteBlobMSBULong(image,crc32(0,chunk,32));
11207      option=GetImageOption(image_info,"mng:need-cacheoff");
11208      if (option != (const char *) NULL)
11209        {
11210          size_t
11211            length;
11212
11213          /*
11214            Write "nEED CACHEOFF" to turn playback caching off for streaming MNG.
11215          */
11216          PNGType(chunk,mng_nEED);
11217          length=CopyMagickString((char *) chunk+4,"CACHEOFF",20);
11218          (void) WriteBlobMSBULong(image,(size_t) length);
11219          LogPNGChunk(logging,mng_nEED,(size_t) length);
11220          length+=4;
11221          (void) WriteBlob(image,length,chunk);
11222          (void) WriteBlobMSBULong(image,crc32(0,chunk,(uInt) length));
11223        }
11224      if ((GetPreviousImageInList(image) == (Image *) NULL) &&
11225          (GetNextImageInList(image) != (Image *) NULL) &&
11226          (image->iterations != 1))
11227        {
11228          /*
11229            Write MNG TERM chunk
11230          */
11231          (void) WriteBlobMSBULong(image,10L);  /* data length=10 */
11232          PNGType(chunk,mng_TERM);
11233          LogPNGChunk(logging,mng_TERM,10L);
11234          chunk[4]=3;  /* repeat animation */
11235          chunk[5]=0;  /* show last frame when done */
11236          PNGLong(chunk+6,(png_uint_32) (mng_info->ticks_per_second*
11237             final_delay/MagickMax(image->ticks_per_second,1)));
11238
11239          if (image->iterations == 0)
11240            PNGLong(chunk+10,PNG_UINT_31_MAX);
11241
11242          else
11243            PNGLong(chunk+10,(png_uint_32) image->iterations);
11244
11245          if (logging != MagickFalse)
11246            {
11247              (void) LogMagickEvent(CoderEvent,GetMagickModule(),
11248                "     TERM delay: %.20g",(double) (mng_info->ticks_per_second*
11249               final_delay/MagickMax(image->ticks_per_second,1)));
11250
11251              if (image->iterations == 0)
11252                (void) LogMagickEvent(CoderEvent,GetMagickModule(),
11253                  "     TERM iterations: %.20g",(double) PNG_UINT_31_MAX);
11254
11255              else
11256                (void) LogMagickEvent(CoderEvent,GetMagickModule(),
11257                  "     Image iterations: %.20g",(double) image->iterations);
11258            }
11259          (void) WriteBlob(image,14,chunk);
11260          (void) WriteBlobMSBULong(image,crc32(0,chunk,14));
11261        }
11262      /*
11263        To do: check for cHRM+gAMA == sRGB, and write sRGB instead.
11264      */
11265      if ((image->colorspace == sRGBColorspace || image->rendering_intent) &&
11266           mng_info->equal_srgbs)
11267        {
11268          /*
11269            Write MNG sRGB chunk
11270          */
11271          (void) WriteBlobMSBULong(image,1L);
11272          PNGType(chunk,mng_sRGB);
11273          LogPNGChunk(logging,mng_sRGB,1L);
11274
11275          if (image->rendering_intent != UndefinedIntent)
11276            chunk[4]=(unsigned char)
11277              Magick_RenderingIntent_to_PNG_RenderingIntent(
11278              (image->rendering_intent));
11279
11280          else
11281            chunk[4]=(unsigned char)
11282              Magick_RenderingIntent_to_PNG_RenderingIntent(
11283                (PerceptualIntent));
11284
11285          (void) WriteBlob(image,5,chunk);
11286          (void) WriteBlobMSBULong(image,crc32(0,chunk,5));
11287          mng_info->have_write_global_srgb=MagickTrue;
11288        }
11289
11290      else
11291        {
11292          if (image->gamma && mng_info->equal_gammas)
11293            {
11294              /*
11295                 Write MNG gAMA chunk
11296              */
11297              (void) WriteBlobMSBULong(image,4L);
11298              PNGType(chunk,mng_gAMA);
11299              LogPNGChunk(logging,mng_gAMA,4L);
11300              PNGLong(chunk+4,(png_uint_32) (100000*image->gamma+0.5));
11301              (void) WriteBlob(image,8,chunk);
11302              (void) WriteBlobMSBULong(image,crc32(0,chunk,8));
11303              mng_info->have_write_global_gama=MagickTrue;
11304            }
11305          if (mng_info->equal_chrms)
11306            {
11307              PrimaryInfo
11308                primary;
11309
11310              /*
11311                 Write MNG cHRM chunk
11312              */
11313              (void) WriteBlobMSBULong(image,32L);
11314              PNGType(chunk,mng_cHRM);
11315              LogPNGChunk(logging,mng_cHRM,32L);
11316              primary=image->chromaticity.white_point;
11317              PNGLong(chunk+4,(png_uint_32) (100000*primary.x+0.5));
11318              PNGLong(chunk+8,(png_uint_32) (100000*primary.y+0.5));
11319              primary=image->chromaticity.red_primary;
11320              PNGLong(chunk+12,(png_uint_32) (100000*primary.x+0.5));
11321              PNGLong(chunk+16,(png_uint_32) (100000*primary.y+0.5));
11322              primary=image->chromaticity.green_primary;
11323              PNGLong(chunk+20,(png_uint_32) (100000*primary.x+0.5));
11324              PNGLong(chunk+24,(png_uint_32) (100000*primary.y+0.5));
11325              primary=image->chromaticity.blue_primary;
11326              PNGLong(chunk+28,(png_uint_32) (100000*primary.x+0.5));
11327              PNGLong(chunk+32,(png_uint_32) (100000*primary.y+0.5));
11328              (void) WriteBlob(image,36,chunk);
11329              (void) WriteBlobMSBULong(image,crc32(0,chunk,36));
11330              mng_info->have_write_global_chrm=MagickTrue;
11331            }
11332        }
11333      if (image->x_resolution && image->y_resolution && mng_info->equal_physs)
11334        {
11335          /*
11336             Write MNG pHYs chunk
11337          */
11338          (void) WriteBlobMSBULong(image,9L);
11339          PNGType(chunk,mng_pHYs);
11340          LogPNGChunk(logging,mng_pHYs,9L);
11341
11342          if (image->units == PixelsPerInchResolution)
11343            {
11344              PNGLong(chunk+4,(png_uint_32)
11345                (image->x_resolution*100.0/2.54+0.5));
11346
11347              PNGLong(chunk+8,(png_uint_32)
11348                (image->y_resolution*100.0/2.54+0.5));
11349
11350              chunk[12]=1;
11351            }
11352
11353          else
11354            {
11355              if (image->units == PixelsPerCentimeterResolution)
11356                {
11357                  PNGLong(chunk+4,(png_uint_32)
11358                    (image->x_resolution*100.0+0.5));
11359
11360                  PNGLong(chunk+8,(png_uint_32)
11361                    (image->y_resolution*100.0+0.5));
11362
11363                  chunk[12]=1;
11364                }
11365
11366              else
11367                {
11368                  PNGLong(chunk+4,(png_uint_32) (image->x_resolution+0.5));
11369                  PNGLong(chunk+8,(png_uint_32) (image->y_resolution+0.5));
11370                  chunk[12]=0;
11371                }
11372            }
11373          (void) WriteBlob(image,13,chunk);
11374          (void) WriteBlobMSBULong(image,crc32(0,chunk,13));
11375        }
11376      /*
11377        Write MNG BACK chunk and global bKGD chunk, if the image is transparent
11378        or does not cover the entire frame.
11379      */
11380      if (write_mng && (image->matte || image->page.x > 0 ||
11381          image->page.y > 0 || (image->page.width &&
11382          (image->page.width+image->page.x < mng_info->page.width))
11383          || (image->page.height && (image->page.height+image->page.y
11384          < mng_info->page.height))))
11385        {
11386          (void) WriteBlobMSBULong(image,6L);
11387          PNGType(chunk,mng_BACK);
11388          LogPNGChunk(logging,mng_BACK,6L);
11389          red=ScaleQuantumToShort(image->background_color.red);
11390          green=ScaleQuantumToShort(image->background_color.green);
11391          blue=ScaleQuantumToShort(image->background_color.blue);
11392          PNGShort(chunk+4,red);
11393          PNGShort(chunk+6,green);
11394          PNGShort(chunk+8,blue);
11395          (void) WriteBlob(image,10,chunk);
11396          (void) WriteBlobMSBULong(image,crc32(0,chunk,10));
11397          if (mng_info->equal_backgrounds)
11398            {
11399              (void) WriteBlobMSBULong(image,6L);
11400              PNGType(chunk,mng_bKGD);
11401              LogPNGChunk(logging,mng_bKGD,6L);
11402              (void) WriteBlob(image,10,chunk);
11403              (void) WriteBlobMSBULong(image,crc32(0,chunk,10));
11404            }
11405        }
11406
11407 #ifdef PNG_WRITE_EMPTY_PLTE_SUPPORTED
11408      if ((need_local_plte == MagickFalse) &&
11409          (image->storage_class == PseudoClass) &&
11410          (all_images_are_gray == MagickFalse))
11411        {
11412          size_t
11413            data_length;
11414
11415          /*
11416            Write MNG PLTE chunk
11417          */
11418          data_length=3*image->colors;
11419          (void) WriteBlobMSBULong(image,data_length);
11420          PNGType(chunk,mng_PLTE);
11421          LogPNGChunk(logging,mng_PLTE,data_length);
11422
11423          for (i=0; i < (ssize_t) image->colors; i++)
11424          {
11425            chunk[4+i*3]=ScaleQuantumToChar(image->colormap[i].red) & 0xff;
11426            chunk[5+i*3]=ScaleQuantumToChar(image->colormap[i].green) & 0xff;
11427            chunk[6+i*3]=ScaleQuantumToChar(image->colormap[i].blue) & 0xff;
11428          }
11429
11430          (void) WriteBlob(image,data_length+4,chunk);
11431          (void) WriteBlobMSBULong(image,crc32(0,chunk,(uInt) (data_length+4)));
11432          mng_info->have_write_global_plte=MagickTrue;
11433        }
11434 #endif
11435     }
11436   scene=0;
11437   mng_info->delay=0;
11438 #if defined(PNG_WRITE_EMPTY_PLTE_SUPPORTED) || \
11439     defined(PNG_MNG_FEATURES_SUPPORTED)
11440   mng_info->equal_palettes=MagickFalse;
11441 #endif
11442   do
11443   {
11444     if (mng_info->adjoin)
11445     {
11446 #if defined(PNG_WRITE_EMPTY_PLTE_SUPPORTED) || \
11447     defined(PNG_MNG_FEATURES_SUPPORTED)
11448     /*
11449       If we aren't using a global palette for the entire MNG, check to
11450       see if we can use one for two or more consecutive images.
11451     */
11452     if (need_local_plte && use_global_plte && !all_images_are_gray)
11453       {
11454         if (mng_info->IsPalette)
11455           {
11456             /*
11457               When equal_palettes is true, this image has the same palette
11458               as the previous PseudoClass image
11459             */
11460             mng_info->have_write_global_plte=mng_info->equal_palettes;
11461             mng_info->equal_palettes=PalettesAreEqual(image,image->next);
11462             if (mng_info->equal_palettes && !mng_info->have_write_global_plte)
11463               {
11464                 /*
11465                   Write MNG PLTE chunk
11466                 */
11467                 size_t
11468                   data_length;
11469
11470                 data_length=3*image->colors;
11471                 (void) WriteBlobMSBULong(image,data_length);
11472                 PNGType(chunk,mng_PLTE);
11473                 LogPNGChunk(logging,mng_PLTE,data_length);
11474
11475                 for (i=0; i < (ssize_t) image->colors; i++)
11476                 {
11477                   chunk[4+i*3]=ScaleQuantumToChar(image->colormap[i].red);
11478                   chunk[5+i*3]=ScaleQuantumToChar(image->colormap[i].green);
11479                   chunk[6+i*3]=ScaleQuantumToChar(image->colormap[i].blue);
11480                 }
11481
11482                 (void) WriteBlob(image,data_length+4,chunk);
11483                 (void) WriteBlobMSBULong(image,crc32(0,chunk,
11484                    (uInt) (data_length+4)));
11485                 mng_info->have_write_global_plte=MagickTrue;
11486               }
11487           }
11488         else
11489           mng_info->have_write_global_plte=MagickFalse;
11490       }
11491 #endif
11492     if (need_defi)
11493       {
11494         ssize_t
11495           previous_x,
11496           previous_y;
11497
11498         if (scene)
11499           {
11500             previous_x=mng_info->page.x;
11501             previous_y=mng_info->page.y;
11502           }
11503         else
11504           {
11505             previous_x=0;
11506             previous_y=0;
11507           }
11508         mng_info->page=image->page;
11509         if ((mng_info->page.x !=  previous_x) ||
11510             (mng_info->page.y != previous_y))
11511           {
11512              (void) WriteBlobMSBULong(image,12L);  /* data length=12 */
11513              PNGType(chunk,mng_DEFI);
11514              LogPNGChunk(logging,mng_DEFI,12L);
11515              chunk[4]=0; /* object 0 MSB */
11516              chunk[5]=0; /* object 0 LSB */
11517              chunk[6]=0; /* visible  */
11518              chunk[7]=0; /* abstract */
11519              PNGLong(chunk+8,(png_uint_32) mng_info->page.x);
11520              PNGLong(chunk+12,(png_uint_32) mng_info->page.y);
11521              (void) WriteBlob(image,16,chunk);
11522              (void) WriteBlobMSBULong(image,crc32(0,chunk,16));
11523           }
11524       }
11525     }
11526
11527    mng_info->write_mng=write_mng;
11528
11529    if ((int) image->dispose >= 3)
11530      mng_info->framing_mode=3;
11531
11532    if (mng_info->need_fram && mng_info->adjoin &&
11533        ((image->delay != mng_info->delay) ||
11534         (mng_info->framing_mode != mng_info->old_framing_mode)))
11535      {
11536        if (image->delay == mng_info->delay)
11537          {
11538            /*
11539              Write a MNG FRAM chunk with the new framing mode.
11540            */
11541            (void) WriteBlobMSBULong(image,1L);  /* data length=1 */
11542            PNGType(chunk,mng_FRAM);
11543            LogPNGChunk(logging,mng_FRAM,1L);
11544            chunk[4]=(unsigned char) mng_info->framing_mode;
11545            (void) WriteBlob(image,5,chunk);
11546            (void) WriteBlobMSBULong(image,crc32(0,chunk,5));
11547          }
11548        else
11549          {
11550            /*
11551              Write a MNG FRAM chunk with the delay.
11552            */
11553            (void) WriteBlobMSBULong(image,10L);  /* data length=10 */
11554            PNGType(chunk,mng_FRAM);
11555            LogPNGChunk(logging,mng_FRAM,10L);
11556            chunk[4]=(unsigned char) mng_info->framing_mode;
11557            chunk[5]=0;  /* frame name separator (no name) */
11558            chunk[6]=2;  /* flag for changing default delay */
11559            chunk[7]=0;  /* flag for changing frame timeout */
11560            chunk[8]=0;  /* flag for changing frame clipping */
11561            chunk[9]=0;  /* flag for changing frame sync_id */
11562            PNGLong(chunk+10,(png_uint_32)
11563              ((mng_info->ticks_per_second*
11564              image->delay)/MagickMax(image->ticks_per_second,1)));
11565            (void) WriteBlob(image,14,chunk);
11566            (void) WriteBlobMSBULong(image,crc32(0,chunk,14));
11567            mng_info->delay=(png_uint_32) image->delay;
11568          }
11569        mng_info->old_framing_mode=mng_info->framing_mode;
11570      }
11571
11572 #if defined(JNG_SUPPORTED)
11573    if (image_info->compression == JPEGCompression)
11574      {
11575        ImageInfo
11576          *write_info;
11577
11578        if (logging != MagickFalse)
11579          (void) LogMagickEvent(CoderEvent,GetMagickModule(),
11580            "  Writing JNG object.");
11581        /* To do: specify the desired alpha compression method. */
11582        write_info=CloneImageInfo(image_info);
11583        write_info->compression=UndefinedCompression;
11584        status=WriteOneJNGImage(mng_info,write_info,image);
11585        write_info=DestroyImageInfo(write_info);
11586      }
11587    else
11588 #endif
11589      {
11590        if (logging != MagickFalse)
11591          (void) LogMagickEvent(CoderEvent,GetMagickModule(),
11592            "  Writing PNG object.");
11593
11594        mng_info->need_blob = MagickFalse;
11595
11596        /* We don't want any ancillary chunks written */
11597        mng_info->ping_exclude_bKGD=MagickTrue;
11598        mng_info->ping_exclude_cHRM=MagickTrue;
11599        mng_info->ping_exclude_EXIF=MagickTrue;
11600        mng_info->ping_exclude_gAMA=MagickTrue;
11601        mng_info->ping_exclude_cHRM=MagickTrue;
11602        mng_info->ping_exclude_iCCP=MagickTrue;
11603        /* mng_info->ping_exclude_iTXt=MagickTrue; */
11604        mng_info->ping_exclude_oFFs=MagickTrue;
11605        mng_info->ping_exclude_pHYs=MagickTrue;
11606        mng_info->ping_exclude_sRGB=MagickTrue;
11607        mng_info->ping_exclude_tEXt=MagickTrue;
11608        mng_info->ping_exclude_tRNS=MagickTrue;
11609        mng_info->ping_exclude_vpAg=MagickTrue;
11610        mng_info->ping_exclude_zCCP=MagickTrue;
11611        mng_info->ping_exclude_zTXt=MagickTrue;
11612
11613        status=WriteOnePNGImage(mng_info,image_info,image);
11614      }
11615
11616     if (status == MagickFalse)
11617       {
11618         MngInfoFreeStruct(mng_info,&have_mng_structure);
11619         (void) CloseBlob(image);
11620         return(MagickFalse);
11621       }
11622     (void) CatchImageException(image);
11623     if (GetNextImageInList(image) == (Image *) NULL)
11624       break;
11625     image=SyncNextImageInList(image);
11626     status=SetImageProgress(image,SaveImagesTag,scene++,
11627       GetImageListLength(image));
11628
11629     if (status == MagickFalse)
11630       break;
11631
11632   } while (mng_info->adjoin);
11633
11634   if (write_mng)
11635     {
11636       while (GetPreviousImageInList(image) != (Image *) NULL)
11637         image=GetPreviousImageInList(image);
11638       /*
11639         Write the MEND chunk.
11640       */
11641       (void) WriteBlobMSBULong(image,0x00000000L);
11642       PNGType(chunk,mng_MEND);
11643       LogPNGChunk(logging,mng_MEND,0L);
11644       (void) WriteBlob(image,4,chunk);
11645       (void) WriteBlobMSBULong(image,crc32(0,chunk,4));
11646     }
11647   /*
11648     Relinquish resources.
11649   */
11650   (void) CloseBlob(image);
11651   MngInfoFreeStruct(mng_info,&have_mng_structure);
11652
11653   if (logging != MagickFalse)
11654     (void) LogMagickEvent(CoderEvent,GetMagickModule(),"exit WriteMNGImage()");
11655
11656   return(MagickTrue);
11657 }
11658 #else /* PNG_LIBPNG_VER > 10011 */
11659
11660 static MagickBooleanType WritePNGImage(const ImageInfo *image_info,Image *image)
11661 {
11662   image=image;
11663   printf("Your PNG library is too old: You have libpng-%s\n",
11664      PNG_LIBPNG_VER_STRING);
11665
11666   ThrowBinaryException(CoderError,"PNG library is too old",
11667      image_info->filename);
11668 }
11669
11670 static MagickBooleanType WriteMNGImage(const ImageInfo *image_info,Image *image)
11671 {
11672   return(WritePNGImage(image_info,image));
11673 }
11674 #endif /* PNG_LIBPNG_VER > 10011 */
11675 #endif