2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write Portable Network Graphics Image Format %
17 % Glenn Randers-Pehrson %
21 % Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization %
22 % dedicated to making software imaging solutions freely available. %
24 % You may not use this file except in compliance with the License. You may %
25 % obtain a copy of the License at %
27 % http://www.imagemagick.org/script/license.php %
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. %
35 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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)
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.
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 */
99 /* ImageMagick differences */
100 #define first_scene scene
102 #if PNG_LIBPNG_VER > 10011
104 Optional declarations. Define or undefine them as you like.
106 /* #define PNG_DEBUG -- turning this on breaks VisualC compiling */
109 Features under construction. Define these to work on them.
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. */
118 #if !defined(RGBColorMatchExact)
119 #define IsPNGColorEqual(color,target) \
120 (((color).red == (target).red) && \
121 ((color).green == (target).green) && \
122 ((color).blue == (target).blue))
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:
130 #ifndef SETJMP_IS_THREAD_SAFE
131 #define PNG_SETJMP_NOT_THREAD_SAFE
134 #if defined(PNG_SETJMP_NOT_THREAD_SAFE)
136 *ping_semaphore = (SemaphoreInfo *) NULL;
140 This temporary until I set up malloc'ed object attributes array.
141 Recompile with MNG_MAX_OBJECTS=65536L to avoid this limit but
144 #define MNG_MAX_OBJECTS 256
147 If this not defined, spec is interpreted strictly. If it is
148 defined, an attempt will be made to recover from some errors,
150 o global PLTE too short
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.
163 #ifdef PNG_MNG_FEATURES_SUPPORTED
164 # ifndef PNG_READ_EMPTY_PLTE_SUPPORTED
165 # define PNG_READ_EMPTY_PLTE_SUPPORTED
167 # ifndef PNG_WRITE_EMPTY_PLTE_SUPPORTED
168 # define PNG_WRITE_EMPTY_PLTE_SUPPORTED
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
177 #ifndef PNG_UINT_31_MAX
178 #define PNG_UINT_31_MAX (png_uint_32) 0x7fffffffL
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.
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'};
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'};
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'};
243 typedef struct _MngBox
252 typedef struct _MngPair
259 #ifdef MNG_OBJECT_BUFFERS
260 typedef struct _MngBuffer
292 typedef struct _MngInfo
295 #ifdef MNG_OBJECT_BUFFERS
297 *ob[MNG_MAX_OBJECTS];
308 #ifndef PNG_READ_EMPTY_PLTE_SUPPORTED
309 bytes_in_read_buffer,
315 #if defined(PNG_WRITE_EMPTY_PLTE_SUPPORTED) || \
316 defined(PNG_MNG_FEATURES_SUPPORTED)
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,
346 x_off[MNG_MAX_OBJECTS],
347 y_off[MNG_MAX_OBJECTS];
353 object_clip[MNG_MAX_OBJECTS];
356 /* These flags could be combined into one byte */
357 exists[MNG_MAX_OBJECTS],
358 frozen[MNG_MAX_OBJECTS],
360 invisible[MNG_MAX_OBJECTS],
361 viewable[MNG_MAX_OBJECTS];
373 #ifndef PNG_READ_EMPTY_PLTE_SUPPORTED
391 global_x_pixels_per_unit,
392 global_y_pixels_per_unit,
402 global_phys_unit_type,
421 #ifdef MNG_BASI_SUPPORTED
429 basi_compression_method,
431 basi_interlace_method,
454 /* Added at version 6.6.6-7 */
461 /* ping_exclude_iTXt, */
468 ping_exclude_zCCP, /* hex-encoded iCCP */
475 Forward declarations.
477 static MagickBooleanType
478 WritePNGImage(const ImageInfo *,Image *);
480 static MagickBooleanType
481 WriteMNGImage(const ImageInfo *,Image *);
483 #if defined(JNG_SUPPORTED)
484 static MagickBooleanType
485 WriteJNGImage(const ImageInfo *,Image *);
488 #if PNG_LIBPNG_VER > 10011
491 #if (MAGICKCORE_QUANTUM_DEPTH >= 16)
492 static MagickBooleanType
493 LosslessReduceDepthOK(Image *image)
496 ok_to_reduce=MagickFalse;
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
505 if (image->depth == 16)
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 :
520 if (ok_to_reduce != MagickFalse && image->storage_class == PseudoClass)
524 for (indx=0; indx < (ssize_t) image->colors; indx++)
526 ok_to_reduce=(((((size_t) image->colormap[indx].red >>
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)
542 if ((ok_to_reduce != MagickFalse) &&
543 (image->storage_class != PseudoClass))
551 for (y=0; y < (ssize_t) image->rows; y++)
553 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
555 if (p == (const PixelPacket *) NULL)
557 ok_to_reduce = MagickFalse;
561 for (x=(ssize_t) image->columns-1; x >= 0; x--)
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;
574 if (ok_to_reduce == MagickFalse)
584 if (ok_to_reduce != MagickFalse)
586 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
587 " OK to reduce PNG bit depth to 8 without loss of info");
591 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
592 " Not OK to reduce PNG bit depth to 8 without loss of info");
598 #endif /* MAGICKCORE_QUANTUM_DEPTH >= 16 */
601 Magick_RenderingIntent_to_PNG_RenderingIntent(const RenderingIntent intent)
605 case PerceptualIntent:
611 case SaturationIntent:
622 static RenderingIntent
623 Magick_RenderingIntent_from_PNG_RenderingIntent(const int ping_intent)
628 return PerceptualIntent;
631 return RelativeIntent;
634 return SaturationIntent;
637 return AbsoluteIntent;
640 return UndefinedIntent;
644 static inline ssize_t MagickMax(const ssize_t x,const ssize_t y)
652 static inline ssize_t MagickMin(const ssize_t x,const ssize_t y)
662 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
666 % I m a g e I s G r a y %
670 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
672 % Like IsGrayImage except does not change DirectClass to PseudoClass %
674 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
676 static MagickBooleanType ImageIsGray(Image *image)
678 register const PixelPacket
686 assert(image != (Image *) NULL);
687 assert(image->signature == MagickSignature);
688 if (image->debug != MagickFalse)
689 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
691 if (image->storage_class == PseudoClass)
693 for (i=0; i < (ssize_t) image->colors; i++)
694 if (IsGray(image->colormap+i) == MagickFalse)
698 for (y=0; y < (ssize_t) image->rows; y++)
700 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
701 if (p == (const PixelPacket *) NULL)
703 for (x=(ssize_t) image->columns-1; x >= 0; x--)
705 if (IsGray(p) == MagickFalse)
712 #endif /* PNG_LIBPNG_VER > 10011 */
713 #endif /* MAGICKCORE_PNG_DELEGATE */
716 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
724 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
726 % IsMNG() returns MagickTrue if the image format type, identified by the
727 % magick string, is MNG.
729 % The format of the IsMNG method is:
731 % MagickBooleanType IsMNG(const unsigned char *magick,const size_t length)
733 % A description of each parameter follows:
735 % o magick: compare image format pattern against these bytes.
737 % o length: Specifies the length of the magick string.
741 static MagickBooleanType IsMNG(const unsigned char *magick,const size_t length)
746 if (memcmp(magick,"\212MNG\r\n\032\n",8) == 0)
753 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
761 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
763 % IsJNG() returns MagickTrue if the image format type, identified by the
764 % magick string, is JNG.
766 % The format of the IsJNG method is:
768 % MagickBooleanType IsJNG(const unsigned char *magick,const size_t length)
770 % A description of each parameter follows:
772 % o magick: compare image format pattern against these bytes.
774 % o length: Specifies the length of the magick string.
778 static MagickBooleanType IsJNG(const unsigned char *magick,const size_t length)
783 if (memcmp(magick,"\213JNG\r\n\032\n",8) == 0)
790 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
798 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
800 % IsPNG() returns MagickTrue if the image format type, identified by the
801 % magick string, is PNG.
803 % The format of the IsPNG method is:
805 % MagickBooleanType IsPNG(const unsigned char *magick,const size_t length)
807 % A description of each parameter follows:
809 % o magick: compare image format pattern against these bytes.
811 % o length: Specifies the length of the magick string.
814 static MagickBooleanType IsPNG(const unsigned char *magick,const size_t length)
819 if (memcmp(magick,"\211PNG\r\n\032\n",8) == 0)
825 #if defined(MAGICKCORE_PNG_DELEGATE)
826 #if defined(__cplusplus) || defined(c_plusplus)
830 #if (PNG_LIBPNG_VER > 10011)
831 static size_t WriteBlobMSBULong(Image *image,const size_t value)
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));
845 static void PNGLong(png_bytep p,png_uint_32 value)
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);
853 #if defined(JNG_SUPPORTED)
854 static void PNGsLong(png_bytep p,png_int_32 value)
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);
863 static void PNGShort(png_bytep p,png_uint_16 value)
865 *p++=(png_byte) ((value >> 8) & 0xff);
866 *p++=(png_byte) (value & 0xff);
869 static void PNGType(png_bytep p,png_bytep type)
871 (void) CopyMagickMemory(p,type,4*sizeof(png_byte));
874 static void LogPNGChunk(MagickBooleanType logging, png_bytep type,
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);
882 #endif /* PNG_LIBPNG_VER > 10011 */
884 #if defined(__cplusplus) || defined(c_plusplus)
888 #if PNG_LIBPNG_VER > 10011
890 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
894 % R e a d P N G I m a g e %
898 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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.
905 % MNG support written by Glenn Randers-Pehrson, glennrp@image...
907 % The format of the ReadPNGImage method is:
909 % Image *ReadPNGImage(const ImageInfo *image_info,ExceptionInfo *exception)
911 % A description of each parameter follows:
913 % o image_info: the image info.
915 % o exception: return any errors or warnings in this structure.
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):
920 % Get 16-bit cheap transparency working.
922 % (At this point, PNG decoding is supposed to be in full MNG-LC compliance)
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
928 % (At this point, PNG encoding should be in full MNG compliance)
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)
934 % Implement LOOP/ENDL [done, but could do discretionary loops more
935 % efficiently by linking in the duplicate frames.].
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).
940 % Upgrade to full MNG without Delta-PNG.
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)]
951 % o MNG-level tEXt/iTXt/zTXt
956 % o iTXt (wait for libpng implementation).
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.
963 % Upgrade to full MNG with Delta-PNG.
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.
975 #if defined(__cplusplus) || defined(c_plusplus)
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.
984 static void png_get_data(png_structp png_ptr,png_bytep data,png_size_t length)
989 image=(Image *) png_get_io_ptr(png_ptr);
995 check=(png_size_t) ReadBlob(image,(size_t) length,data);
1001 (void) FormatMagickString(msg,MaxTextExtent,
1002 "Expected %.20g bytes; found %.20g bytes",(double) length,
1004 png_warning(png_ptr,msg);
1005 png_error(png_ptr,"Read Exception");
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.
1020 static void mng_get_data(png_structp png_ptr,png_bytep data,png_size_t length)
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)
1039 data[i]=mng_info->read_buffer[i];
1040 mng_info->bytes_in_read_buffer--;
1046 check=(png_size_t) ReadBlob(image,(size_t) length,(char *) data);
1048 if (check != length)
1049 png_error(png_ptr,"Read Exception");
1053 if ((data[0] == 0) && (data[1] == 0) && (data[2] == 0) &&
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)
1064 mng_info->found_empty_plte=MagickFalse;
1065 mng_info->have_saved_bkgd_index=MagickFalse;
1069 if ((data[0] == 0) && (data[1] == 0) && (data[2] == 0) &&
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)
1080 Skip the bKGD data byte and CRC.
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;
1096 static void png_put_data(png_structp png_ptr,png_bytep data,png_size_t length)
1101 image=(Image *) png_get_io_ptr(png_ptr);
1107 check=(png_size_t) WriteBlob(image,(size_t) length,data);
1109 if (check != length)
1110 png_error(png_ptr,"WriteBlob Failed");
1114 static void png_flush_data(png_structp png_ptr)
1119 #ifdef PNG_WRITE_EMPTY_PLTE_SUPPORTED
1120 static int PalettesAreEqual(Image *a,Image *b)
1125 if ((a == (Image *) NULL) || (b == (Image *) NULL))
1126 return((int) MagickFalse);
1128 if (a->storage_class != PseudoClass || b->storage_class != PseudoClass)
1129 return((int) MagickFalse);
1131 if (a->colors != b->colors)
1132 return((int) MagickFalse);
1134 for (i=0; i < (ssize_t) a->colors; i++)
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);
1142 return((int) MagickTrue);
1146 static void MngInfoDiscardObject(MngInfo *mng_info,int i)
1148 if (i && (i < MNG_MAX_OBJECTS) && (mng_info != (MngInfo *) NULL) &&
1149 mng_info->exists[i] && !mng_info->frozen[i])
1151 #ifdef MNG_OBJECT_BUFFERS
1152 if (mng_info->ob[i] != (MngBuffer *) NULL)
1154 if (mng_info->ob[i]->reference_count > 0)
1155 mng_info->ob[i]->reference_count--;
1157 if (mng_info->ob[i]->reference_count == 0)
1159 if (mng_info->ob[i]->image != (Image *) NULL)
1160 mng_info->ob[i]->image=DestroyImage(mng_info->ob[i]->image);
1162 mng_info->ob[i]=DestroyString(mng_info->ob[i]);
1165 mng_info->ob[i]=(MngBuffer *) NULL;
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;
1180 static void MngInfoFreeStruct(MngInfo *mng_info,
1181 MagickBooleanType *have_mng_structure)
1183 if (*have_mng_structure != MagickFalse && (mng_info != (MngInfo *) NULL))
1188 for (i=1; i < MNG_MAX_OBJECTS; i++)
1189 MngInfoDiscardObject(mng_info,i);
1191 if (mng_info->global_plte != (png_colorp) NULL)
1192 mng_info->global_plte=(png_colorp)
1193 RelinquishMagickMemory(mng_info->global_plte);
1195 mng_info=(MngInfo *) RelinquishMagickMemory(mng_info);
1196 *have_mng_structure=MagickFalse;
1200 static MngBox mng_minimum_box(MngBox box1,MngBox box2)
1206 if (box.left < box2.left)
1209 if (box.top < box2.top)
1212 if (box.right > box2.right)
1213 box.right=box2.right;
1215 if (box.bottom > box2.bottom)
1216 box.bottom=box2.bottom;
1221 static MngBox mng_read_box(MngBox previous_box,char delta_type,unsigned char *p)
1227 Read clipping boundaries from DEFI, CLIP, FRAM, or PAST chunk.
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)
1235 box.left+=previous_box.left;
1236 box.right+=previous_box.right;
1237 box.top+=previous_box.top;
1238 box.bottom+=previous_box.bottom;
1244 static MngPair mng_read_pair(MngPair previous_pair,int delta_type,
1250 Read two ssize_ts from CLON, MOVE or PAST chunk
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]);
1255 if (delta_type != 0)
1257 pair.a+=previous_pair.a;
1258 pair.b+=previous_pair.b;
1264 static long mng_get_long(unsigned char *p)
1266 return((long) ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]));
1269 static void MagickPNGErrorHandler(png_struct *ping,png_const_charp message)
1274 image=(Image *) png_get_error_ptr(ping);
1276 if (image->debug != MagickFalse)
1277 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1278 " libpng-%s error: %s", PNG_LIBPNG_VER_STRING,message);
1280 (void) ThrowMagickException(&image->exception,GetMagickModule(),CoderError,
1281 message,"`%s'",image->filename);
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.
1287 longjmp(ping->jmpbuf,1);
1289 png_longjmp(ping,1);
1293 static void MagickPNGWarningHandler(png_struct *ping,png_const_charp message)
1298 if (LocaleCompare(message, "Missing PLTE before tRNS") == 0)
1299 png_error(ping, message);
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);
1306 (void) ThrowMagickException(&image->exception,GetMagickModule(),CoderWarning,
1307 message,"`%s'",image->filename);
1310 #ifdef PNG_USER_MEM_SUPPORTED
1311 static png_voidp Magick_png_malloc(png_structp png_ptr,png_uint_32 size)
1313 #if (PNG_LIBPNG_VER < 10011)
1318 ret=((png_voidp) AcquireMagickMemory((size_t) size));
1321 png_error("Insufficient memory.");
1326 return((png_voidp) AcquireMagickMemory((size_t) size));
1331 Free a pointer. It is removed from the list at the same time.
1333 static png_free_ptr Magick_png_free(png_structp png_ptr,png_voidp ptr)
1336 ptr=RelinquishMagickMemory(ptr);
1337 return((png_free_ptr) NULL);
1341 #if defined(__cplusplus) || defined(c_plusplus)
1346 Magick_png_read_raw_profile(Image *image, const ImageInfo *image_info,
1347 png_textp text,int ii)
1352 register 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,
1374 /* look for newline */
1378 /* look for length */
1379 while (*sp == '\0' || *sp == ' ' || *sp == '\n')
1382 length=(png_uint_32) StringToLong(sp);
1384 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1385 " length: %lu",(unsigned long) length);
1387 while (*sp != ' ' && *sp != '\n')
1390 /* allocate space */
1393 (void) ThrowMagickException(&image->exception,GetMagickModule(),
1394 CoderWarning,"UnableToCopyProfile","`%s'","invalid profile length");
1395 return(MagickFalse);
1398 profile=AcquireStringInfo(length);
1400 if (profile == (StringInfo *) NULL)
1402 (void) ThrowMagickException(&image->exception,GetMagickModule(),
1403 ResourceLimitError,"MemoryAllocationFailed","`%s'",
1404 "unable to copy profile");
1405 return(MagickFalse);
1408 /* copy profile, skipping white space and column 1 "=" signs */
1409 dp=GetStringInfoDatum(profile);
1412 for (i=0; i < (ssize_t) nibbles; i++)
1414 while (*sp < '0' || (*sp > '9' && *sp < 'a') || *sp > 'f')
1418 (void) ThrowMagickException(&image->exception,GetMagickModule(),
1419 CoderWarning,"UnableToCopyProfile","`%s'","ran out of data");
1420 profile=DestroyStringInfo(profile);
1421 return(MagickFalse);
1427 *dp=(unsigned char) (16*unhex[(int) *sp++]);
1430 (*dp++)+=unhex[(int) *sp++];
1433 We have already read "Raw profile type.
1435 (void) SetImageProfile(image,&text[ii].key[17],profile);
1436 profile=DestroyStringInfo(profile);
1438 if (image_info->verbose)
1439 (void) printf(" Found a generic profile, type %s\n",&text[ii].key[17]);
1444 #if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
1445 static int read_vpag_chunk_callback(png_struct *ping, png_unknown_chunkp chunk)
1451 /* The unknown chunk structure contains the chunk data:
1456 Note that libpng has already taken care of the CRC handling.
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 */
1464 /* recognized vpAg */
1466 if (chunk->size != 9)
1467 return(-1); /* Error return */
1469 if (chunk->data[8] != 0)
1470 return(0); /* ImageMagick requires pixel units */
1472 image=(Image *) png_get_user_chunk_ptr(ping);
1474 image->page.width=(size_t) ((chunk->data[0] << 24) |
1475 (chunk->data[1] << 16) | (chunk->data[2] << 8) | chunk->data[3]);
1477 image->page.height=(size_t) ((chunk->data[4] << 24) |
1478 (chunk->data[5] << 16) | (chunk->data[6] << 8) | chunk->data[7]);
1480 /* Return one of the following: */
1481 /* return(-n); chunk had an error */
1482 /* return(0); did not recognize */
1483 /* return(n); success */
1491 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1495 % R e a d O n e P N G I m a g e %
1499 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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
1506 % The format of the ReadOnePNGImage method is:
1508 % Image *ReadOnePNGImage(MngInfo *mng_info, const ImageInfo *image_info,
1509 % ExceptionInfo *exception)
1511 % A description of each parameter follows:
1513 % o mng_info: Specifies a pointer to a MngInfo structure.
1515 % o image_info: the image info.
1517 % o exception: return any errors or warnings in this structure.
1520 static Image *ReadOnePNGImage(MngInfo *mng_info,
1521 const ImageInfo *image_info, ExceptionInfo *exception)
1523 /* Read one PNG image */
1534 ping_interlace_method,
1535 ping_compression_method,
1577 register unsigned char
1580 register IndexPacket
1587 register PixelPacket
1594 #if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
1595 png_byte unused_chunks[]=
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 */
1606 logging=LogMagickEvent(CoderEvent,GetMagickModule(),
1607 " Enter ReadOnePNGImage()");
1609 #if defined(PNG_SETJMP_NOT_THREAD_SAFE)
1610 LockSemaphoreInfo(ping_semaphore);
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);
1619 #if (PNG_LIBPNG_VER >= 10400)
1620 # ifndef PNG_TRANSFORM_GRAY_TO_RGB /* Added at libpng-1.4.0beta67 */
1621 if (image_info->verbose)
1623 printf("Your PNG library (libpng-%s) is an old beta version.\n",
1624 PNG_LIBPNG_VER_STRING);
1625 printf("Please update it.\n");
1631 quantum_info = (QuantumInfo *) NULL;
1632 image=mng_info->image;
1634 if (logging != MagickFalse)
1635 (void)LogMagickEvent(CoderEvent,GetMagickModule(),
1636 " image->matte=%d",(int) image->matte);
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;
1645 Allocate the PNG structures
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);
1652 ping=png_create_read_struct(PNG_LIBPNG_VER_STRING,image,
1653 MagickPNGErrorHandler,MagickPNGWarningHandler);
1655 if (ping == (png_struct *) NULL)
1656 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1658 ping_info=png_create_info_struct(ping);
1660 if (ping_info == (png_info *) NULL)
1662 png_destroy_read_struct(&ping,(png_info **) NULL,(png_info **) NULL);
1663 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1666 end_info=png_create_info_struct(ping);
1668 if (end_info == (png_info *) NULL)
1670 png_destroy_read_struct(&ping,&ping_info,(png_info **) NULL);
1671 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1674 ping_pixels=(unsigned char *) NULL;
1676 if (setjmp(png_jmpbuf(ping)))
1679 PNG image is corrupt.
1681 png_destroy_read_struct(&ping,&ping_info,&end_info);
1682 #if defined(PNG_SETJMP_NOT_THREAD_SAFE)
1683 UnlockSemaphoreInfo(ping_semaphore);
1685 if (logging != MagickFalse)
1686 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1687 " exit ReadOnePNGImage() with error.");
1689 if (image != (Image *) NULL)
1691 InheritException(exception,&image->exception);
1695 return(GetFirstImageInList(image));
1698 Prepare PNG for reading.
1701 mng_info->image_found++;
1702 png_set_sig_bytes(ping,8);
1704 if (LocaleCompare(image_info->magick,"MNG") == 0)
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);
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);
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);
1724 png_set_read_fn(ping,image,png_get_data);
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);
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)
1742 png_uint_32 mmx_disable_mask=0;
1743 png_uint_32 asm_flags;
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);
1755 png_read_info(ping,ping_info);
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);
1762 (void) png_get_tRNS(ping, ping_info, &ping_trans_alpha, &ping_num_trans,
1765 (void) png_get_bKGD(ping, ping_info, &ping_background);
1767 if (ping_bit_depth < 8)
1769 if (((int) ping_color_type == PNG_COLOR_TYPE_PALETTE))
1771 png_set_packing(ping);
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)
1781 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1782 " PNG width: %.20g, height: %.20g",
1783 (double) ping_width, (double) ping_height);
1785 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1786 " PNG color_type: %d, bit_depth: %d",
1787 ping_color_type, ping_bit_depth);
1789 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1790 " PNG compression_method: %d",
1791 ping_compression_method);
1793 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1794 " PNG interlace_method: %d, filter_method: %d",
1795 ping_interlace_method,ping_filter_method);
1798 #ifdef PNG_READ_iCCP_SUPPORTED
1799 if (png_get_valid(ping,ping_info,PNG_INFO_iCCP))
1804 #if (PNG_LIBPNG_VER < 10500)
1818 (void) png_get_iCCP(ping,ping_info,&name,(int *) &compression,&info,
1821 if (profile_length != 0)
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);
1836 #if defined(PNG_READ_sRGB_SUPPORTED)
1841 if (mng_info->have_global_srgb)
1842 image->rendering_intent=Magick_RenderingIntent_from_PNG_RenderingIntent
1843 (mng_info->global_srgb_intent);
1845 if (png_get_sRGB(ping,ping_info,&intent))
1847 image->rendering_intent=Magick_RenderingIntent_from_PNG_RenderingIntent
1850 if (logging != MagickFalse)
1851 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1852 " Reading PNG sRGB chunk: rendering_intent: %d",intent);
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);
1864 if (png_get_gAMA(ping,ping_info,&file_gamma))
1866 image->gamma=(float) file_gamma;
1867 if (logging != MagickFalse)
1868 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1869 " Reading PNG gAMA chunk: gamma: %f",file_gamma);
1872 if (!png_get_valid(ping,ping_info,PNG_INFO_cHRM))
1874 if (mng_info->have_global_chrm != MagickFalse)
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);
1888 if (png_get_valid(ping,ping_info,PNG_INFO_cHRM))
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);
1900 if (logging != MagickFalse)
1901 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1902 " Reading PNG cHRM chunk.");
1905 if (image->rendering_intent != UndefinedIntent)
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);
1915 #if defined(PNG_oFFs_SUPPORTED)
1916 if (png_get_valid(ping,ping_info,PNG_INFO_oFFs))
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);
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);
1928 #if defined(PNG_pHYs_SUPPORTED)
1929 if (!png_get_valid(ping,ping_info,PNG_INFO_pHYs))
1931 if (mng_info->have_global_phys)
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);
1940 if (png_get_valid(ping,ping_info,PNG_INFO_pHYs))
1950 Set image resolution.
1952 (void) png_get_pHYs(ping,ping_info,&x_resolution,&y_resolution,
1954 image->x_resolution=(double) x_resolution;
1955 image->y_resolution=(double) y_resolution;
1957 if (unit_type == PNG_RESOLUTION_METER)
1959 image->units=PixelsPerCentimeterResolution;
1960 image->x_resolution=(double) x_resolution/100.0;
1961 image->y_resolution=(double) y_resolution/100.0;
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);
1971 if (png_get_valid(ping,ping_info,PNG_INFO_PLTE))
1979 (void) png_get_PLTE(ping,ping_info,&palette,&number_colors);
1981 if ((number_colors == 0) &&
1982 ((int) ping_color_type == PNG_COLOR_TYPE_PALETTE))
1984 if (mng_info->global_plte_length)
1986 png_set_PLTE(ping,ping_info,mng_info->global_plte,
1987 (int) mng_info->global_plte_length);
1989 if (!png_get_valid(ping,ping_info,PNG_INFO_tRNS))
1990 if (mng_info->global_trns_length)
1992 if (mng_info->global_trns_length >
1993 mng_info->global_plte_length)
1994 (void) ThrowMagickException(&image->exception,
1995 GetMagickModule(),CoderError,
1996 "global tRNS has more entries than global PLTE",
1997 "`%s'",image_info->filename);
1998 png_set_tRNS(ping,ping_info,mng_info->global_trns,
1999 (int) mng_info->global_trns_length,NULL);
2001 #if defined(PNG_READ_bKGD_SUPPORTED)
2003 #ifndef PNG_READ_EMPTY_PLTE_SUPPORTED
2004 mng_info->have_saved_bkgd_index ||
2006 png_get_valid(ping,ping_info,PNG_INFO_bKGD))
2011 #ifndef PNG_READ_EMPTY_PLTE_SUPPORTED
2012 if (mng_info->have_saved_bkgd_index)
2013 background.index=mng_info->saved_bkgd_index;
2015 if (png_get_valid(ping, ping_info, PNG_INFO_bKGD))
2016 background.index=ping_background->index;
2018 background.red=(png_uint_16)
2019 mng_info->global_plte[background.index].red;
2021 background.green=(png_uint_16)
2022 mng_info->global_plte[background.index].green;
2024 background.blue=(png_uint_16)
2025 mng_info->global_plte[background.index].blue;
2027 png_set_bKGD(ping,ping_info,&background);
2032 (void) ThrowMagickException(&image->exception,GetMagickModule(),
2033 CoderError,"No global PLTE in file","`%s'",
2034 image_info->filename);
2038 #if defined(PNG_READ_bKGD_SUPPORTED)
2039 if (mng_info->have_global_bkgd &&
2040 (!png_get_valid(ping,ping_info,PNG_INFO_bKGD)))
2041 image->background_color=mng_info->mng_global_bkgd;
2043 if (png_get_valid(ping,ping_info,PNG_INFO_bKGD))
2046 Set image background color.
2048 if (logging != MagickFalse)
2049 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2050 " Reading PNG bKGD chunk.");
2052 if (ping_bit_depth == MAGICKCORE_QUANTUM_DEPTH)
2054 image->background_color.red=ping_background->red;
2055 image->background_color.green=ping_background->green;
2056 image->background_color.blue=ping_background->blue;
2059 else /* Scale background components to 16-bit */
2064 if (logging != MagickFalse)
2065 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2066 " raw ping_background=(%d,%d,%d).",ping_background->red,
2067 ping_background->green,ping_background->blue);
2071 if (ping_bit_depth == 1)
2074 else if (ping_bit_depth == 2)
2077 else if (ping_bit_depth == 4)
2080 if (ping_bit_depth <= 8)
2083 ping_background->red *= bkgd_scale;
2084 ping_background->green *= bkgd_scale;
2085 ping_background->blue *= bkgd_scale;
2087 if (logging != MagickFalse)
2089 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2090 " bkgd_scale=%d.",bkgd_scale);
2092 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2093 " ping_background=(%d,%d,%d).",ping_background->red,
2094 ping_background->green,ping_background->blue);
2097 image->background_color.red=
2098 ScaleShortToQuantum(ping_background->red);
2100 image->background_color.green=
2101 ScaleShortToQuantum(ping_background->green);
2103 image->background_color.blue=
2104 ScaleShortToQuantum(ping_background->blue);
2106 image->background_color.opacity=OpaqueOpacity;
2108 if (logging != MagickFalse)
2109 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2110 " image->background_color=(%.20g,%.20g,%.20g).",
2111 (double) image->background_color.red,
2112 (double) image->background_color.green,
2113 (double) image->background_color.blue);
2118 if (png_get_valid(ping,ping_info,PNG_INFO_tRNS))
2121 Image has a tRNS chunk.
2129 if (logging != MagickFalse)
2130 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2131 " Reading PNG tRNS chunk.");
2133 max_sample = (int) ((one << ping_bit_depth) - 1);
2135 if ((ping_color_type == PNG_COLOR_TYPE_GRAY &&
2136 (int)ping_trans_color->gray > max_sample) ||
2137 (ping_color_type == PNG_COLOR_TYPE_RGB &&
2138 ((int)ping_trans_color->red > max_sample ||
2139 (int)ping_trans_color->green > max_sample ||
2140 (int)ping_trans_color->blue > max_sample)))
2142 if (logging != MagickFalse)
2143 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2144 " Ignoring PNG tRNS chunk with out-of-range sample.");
2145 png_free_data(ping, ping_info, PNG_FREE_TRNS, 0);
2146 png_set_invalid(ping,ping_info,PNG_INFO_tRNS);
2147 image->matte=MagickFalse;
2154 scale_to_short = 65535L/((1UL << ping_bit_depth)-1);
2156 /* Scale transparent_color to short */
2157 transparent_color.red= scale_to_short*ping_trans_color->red;
2158 transparent_color.green= scale_to_short*ping_trans_color->green;
2159 transparent_color.blue= scale_to_short*ping_trans_color->blue;
2160 transparent_color.opacity= scale_to_short*ping_trans_color->gray;
2162 if (ping_color_type == PNG_COLOR_TYPE_GRAY)
2164 if (logging != MagickFalse)
2166 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2167 " Raw tRNS graylevel is %d.",ping_trans_color->gray);
2169 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2170 " scaled graylevel is %d.",transparent_color.opacity);
2172 transparent_color.red=transparent_color.opacity;
2173 transparent_color.green=transparent_color.opacity;
2174 transparent_color.blue=transparent_color.opacity;
2178 #if defined(PNG_READ_sBIT_SUPPORTED)
2179 if (mng_info->have_global_sbit)
2181 if (!png_get_valid(ping,ping_info,PNG_INFO_sBIT))
2182 png_set_sBIT(ping,ping_info,&mng_info->global_sbit);
2185 num_passes=png_set_interlace_handling(ping);
2187 png_read_update_info(ping,ping_info);
2189 ping_rowbytes=png_get_rowbytes(ping,ping_info);
2192 Initialize image structure.
2194 mng_info->image_box.left=0;
2195 mng_info->image_box.right=(ssize_t) ping_width;
2196 mng_info->image_box.top=0;
2197 mng_info->image_box.bottom=(ssize_t) ping_height;
2198 if (mng_info->mng_type == 0)
2200 mng_info->mng_width=ping_width;
2201 mng_info->mng_height=ping_height;
2202 mng_info->frame=mng_info->image_box;
2203 mng_info->clip=mng_info->image_box;
2208 image->page.y=mng_info->y_off[mng_info->object_id];
2211 image->compression=ZipCompression;
2212 image->columns=ping_width;
2213 image->rows=ping_height;
2214 if (((int) ping_color_type == PNG_COLOR_TYPE_PALETTE) ||
2215 ((int) ping_color_type == PNG_COLOR_TYPE_GRAY_ALPHA) ||
2216 ((int) ping_color_type == PNG_COLOR_TYPE_GRAY))
2221 image->storage_class=PseudoClass;
2223 image->colors=one << ping_bit_depth;
2224 #if (MAGICKCORE_QUANTUM_DEPTH == 8)
2225 if (image->colors > 256)
2228 if (image->colors > 65536L)
2229 image->colors=65536L;
2231 if ((int) ping_color_type == PNG_COLOR_TYPE_PALETTE)
2239 (void) png_get_PLTE(ping,ping_info,&palette,&number_colors);
2240 image->colors=(size_t) number_colors;
2242 if (logging != MagickFalse)
2243 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2244 " Reading PNG PLTE chunk: number_colors: %d.",number_colors);
2248 if (image->storage_class == PseudoClass)
2251 Initialize image colormap.
2253 if (AcquireImageColormap(image,image->colors) == MagickFalse)
2254 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
2256 if ((int) ping_color_type == PNG_COLOR_TYPE_PALETTE)
2264 (void) png_get_PLTE(ping,ping_info,&palette,&number_colors);
2266 for (i=0; i < (ssize_t) image->colors; i++)
2268 image->colormap[i].red=ScaleCharToQuantum(palette[i].red);
2269 image->colormap[i].green=ScaleCharToQuantum(palette[i].green);
2270 image->colormap[i].blue=ScaleCharToQuantum(palette[i].blue);
2279 scale=(QuantumRange/((1UL << ping_bit_depth)-1));
2284 for (i=0; i < (ssize_t) image->colors; i++)
2286 image->colormap[i].red=(Quantum) (i*scale);
2287 image->colormap[i].green=(Quantum) (i*scale);
2288 image->colormap[i].blue=(Quantum) (i*scale);
2293 Read image scanlines.
2295 if (image->delay != 0)
2296 mng_info->scenes_found++;
2298 if ((mng_info->mng_type == 0 && (image->ping != MagickFalse)) || (
2299 (image_info->number_scenes != 0) && (mng_info->scenes_found > (ssize_t)
2300 (image_info->first_scene+image_info->number_scenes))))
2302 if (logging != MagickFalse)
2303 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2304 " Skipping PNG image data for scene %.20g",(double)
2305 mng_info->scenes_found-1);
2306 png_destroy_read_struct(&ping,&ping_info,&end_info);
2307 #if defined(PNG_SETJMP_NOT_THREAD_SAFE)
2308 UnlockSemaphoreInfo(ping_semaphore);
2310 if (logging != MagickFalse)
2311 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2312 " exit ReadOnePNGImage().");
2317 if (logging != MagickFalse)
2318 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2319 " Reading PNG IDAT chunk(s)");
2322 ping_pixels=(unsigned char *) AcquireQuantumMemory(image->rows,
2323 ping_rowbytes*sizeof(*ping_pixels));
2326 ping_pixels=(unsigned char *) AcquireQuantumMemory(ping_rowbytes,
2327 sizeof(*ping_pixels));
2329 if (ping_pixels == (unsigned char *) NULL)
2330 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
2332 if (logging != MagickFalse)
2333 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2334 " Converting PNG pixels to pixel packets");
2336 Convert PNG pixels to pixel packets.
2338 if (setjmp(png_jmpbuf(ping)))
2341 PNG image is corrupt.
2343 png_destroy_read_struct(&ping,&ping_info,&end_info);
2344 #if defined(PNG_SETJMP_NOT_THREAD_SAFE)
2345 UnlockSemaphoreInfo(ping_semaphore);
2347 if (quantum_info != (QuantumInfo *) NULL)
2348 quantum_info = DestroyQuantumInfo(quantum_info);
2350 if (ping_pixels != (unsigned char *) NULL)
2351 ping_pixels=(unsigned char *) RelinquishMagickMemory(ping_pixels);
2353 if (logging != MagickFalse)
2354 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2355 " exit ReadOnePNGImage() with error.");
2357 if (image != (Image *) NULL)
2359 InheritException(exception,&image->exception);
2363 return(GetFirstImageInList(image));
2366 quantum_info=AcquireQuantumInfo(image_info,image);
2368 if (quantum_info == (QuantumInfo *) NULL)
2369 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
2374 found_transparent_pixel;
2376 found_transparent_pixel=MagickFalse;
2378 if (image->storage_class == DirectClass)
2380 for (pass=0; pass < num_passes; pass++)
2383 Convert image to DirectClass pixel packets.
2385 #if (MAGICKCORE_QUANTUM_DEPTH == 8)
2389 depth=(ssize_t) ping_bit_depth;
2391 image->matte=(((int) ping_color_type == PNG_COLOR_TYPE_RGB_ALPHA) ||
2392 ((int) ping_color_type == PNG_COLOR_TYPE_GRAY_ALPHA) ||
2393 (png_get_valid(ping,ping_info,PNG_INFO_tRNS))) ?
2394 MagickTrue : MagickFalse;
2396 for (y=0; y < (ssize_t) image->rows; y++)
2399 row_offset=ping_rowbytes*y;
2404 png_read_row(ping,ping_pixels+row_offset,NULL);
2405 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
2407 if (q == (PixelPacket *) NULL)
2410 #if (0 && (MAGICKCORE_QUANTUM_DEPTH == 8) && !defined(MAGICKCORE_HDRI_SUPPORT))
2411 /* code deleted from version 6.6.6-8 */
2412 #else /* (MAGICKCORE_QUANTUM_DEPTH != 8) */
2414 if ((int) ping_color_type == PNG_COLOR_TYPE_GRAY)
2415 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2416 GrayQuantum,ping_pixels+row_offset,exception);
2418 else if ((int) ping_color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
2419 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2420 GrayAlphaQuantum,ping_pixels+row_offset,exception);
2422 else if ((int) ping_color_type == PNG_COLOR_TYPE_RGB_ALPHA)
2423 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2424 RGBAQuantum,ping_pixels+row_offset,exception);
2426 else if ((int) ping_color_type == PNG_COLOR_TYPE_PALETTE)
2427 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2428 IndexQuantum,ping_pixels+row_offset,exception);
2430 else /* ping_color_type == PNG_COLOR_TYPE_RGB */
2431 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2432 RGBQuantum,ping_pixels+row_offset,exception);
2434 if (found_transparent_pixel == MagickFalse)
2436 /* Is there a transparent pixel in the row? */
2437 if (y== 0 && logging != MagickFalse)
2438 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2439 " Looking for cheap transparent pixel");
2441 for (x=(ssize_t) image->columns-1; x >= 0; x--)
2443 if ((ping_color_type == PNG_COLOR_TYPE_RGBA ||
2444 ping_color_type == PNG_COLOR_TYPE_GRAY_ALPHA) &&
2445 (q->opacity != OpaqueOpacity))
2447 if (logging != MagickFalse)
2448 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2451 found_transparent_pixel = MagickTrue;
2454 if ((ping_color_type == PNG_COLOR_TYPE_RGB ||
2455 ping_color_type == PNG_COLOR_TYPE_GRAY) &&
2456 (ScaleQuantumToShort(q->red) == transparent_color.red &&
2457 ScaleQuantumToShort(q->green) == transparent_color.green &&
2458 ScaleQuantumToShort(q->blue) == transparent_color.blue))
2460 if (logging != MagickFalse)
2461 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2463 found_transparent_pixel = MagickTrue;
2470 if ((image->previous == (Image *) NULL) && (num_passes == 1))
2472 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
2475 if (status == MagickFalse)
2478 if (SyncAuthenticPixels(image,exception) == MagickFalse)
2482 if ((image->previous == (Image *) NULL) && (num_passes != 1))
2484 status=SetImageProgress(image,LoadImageTag,pass,num_passes);
2485 if (status == MagickFalse)
2491 else /* image->storage_class != DirectClass */
2493 for (pass=0; pass < num_passes; pass++)
2502 Convert grayscale image to PseudoClass pixel packets.
2504 image->matte=ping_color_type == PNG_COLOR_TYPE_GRAY_ALPHA ?
2505 MagickTrue : MagickFalse;
2507 quantum_scanline=(Quantum *) AcquireQuantumMemory(image->columns,
2508 (image->matte ? 2 : 1)*sizeof(*quantum_scanline));
2510 if (quantum_scanline == (Quantum *) NULL)
2511 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
2513 for (y=0; y < (ssize_t) image->rows; y++)
2516 row_offset=ping_rowbytes*y;
2521 png_read_row(ping,ping_pixels+row_offset,NULL);
2522 q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
2524 if (q == (PixelPacket *) NULL)
2527 indexes=GetAuthenticIndexQueue(image);
2528 p=ping_pixels+row_offset;
2531 switch (ping_bit_depth)
2538 for (x=(ssize_t) image->columns-7; x > 0; x-=8)
2540 for (bit=7; bit >= 0; bit--)
2541 *r++=(Quantum) ((*p) & (0x01 << bit) ? 0x01 : 0x00);
2545 if ((image->columns % 8) != 0)
2547 for (bit=7; bit >= (ssize_t) (8-(image->columns % 8)); bit--)
2548 *r++=(Quantum) ((*p) & (0x01 << bit) ? 0x01 : 0x00);
2556 for (x=(ssize_t) image->columns-3; x > 0; x-=4)
2558 *r++=(*p >> 6) & 0x03;
2559 *r++=(*p >> 4) & 0x03;
2560 *r++=(*p >> 2) & 0x03;
2564 if ((image->columns % 4) != 0)
2566 for (i=3; i >= (ssize_t) (4-(image->columns % 4)); i--)
2567 *r++=(Quantum) ((*p >> (i*2)) & 0x03);
2575 for (x=(ssize_t) image->columns-1; x > 0; x-=2)
2577 *r++=(*p >> 4) & 0x0f;
2581 if ((image->columns % 2) != 0)
2582 *r++=(*p++ >> 4) & 0x0f;
2589 if (ping_color_type == 4)
2590 for (x=(ssize_t) image->columns-1; x >= 0; x--)
2593 /* In image.h, OpaqueOpacity is 0
2594 * TransparentOpacity is QuantumRange
2595 * In a PNG datastream, Opaque is QuantumRange
2596 * and Transparent is 0.
2598 q->opacity=ScaleCharToQuantum((unsigned char) (255-(*p++)));
2599 if (q->opacity != OpaqueOpacity)
2600 found_transparent_pixel = MagickTrue;
2605 for (x=(ssize_t) image->columns-1; x >= 0; x--)
2613 for (x=(ssize_t) image->columns-1; x >= 0; x--)
2615 #if (MAGICKCORE_QUANTUM_DEPTH == 16)
2619 if (image->colors > 256)
2627 *r=(Quantum) quantum;
2630 if (ping_color_type == 4)
2632 quantum=((*p++) << 8);
2634 q->opacity=(Quantum) (QuantumRange-quantum);
2635 if (q->opacity != OpaqueOpacity)
2636 found_transparent_pixel = MagickTrue;
2640 #if (MAGICKCORE_QUANTUM_DEPTH == 32)
2644 if (image->colors > 256)
2655 if (ping_color_type == 4)
2657 q->opacity=(*p << 8) | *(p+1);
2659 q->opacity=(Quantum) GetAlphaPixelComponent(q);
2660 if (q->opacity != OpaqueOpacity)
2661 found_transparent_pixel = MagickTrue;
2666 #else /* MAGICKCORE_QUANTUM_DEPTH == 8 */
2668 p++; /* strip low byte */
2670 if (ping_color_type == 4)
2672 q->opacity=(Quantum) (QuantumRange-(*p++));
2673 if (q->opacity != OpaqueOpacity)
2674 found_transparent_pixel = MagickTrue;
2689 Transfer image scanline.
2693 for (x=0; x < (ssize_t) image->columns; x++)
2694 indexes[x]=(IndexPacket) (*r++);
2696 if (SyncAuthenticPixels(image,exception) == MagickFalse)
2699 if ((image->previous == (Image *) NULL) && (num_passes == 1))
2701 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
2704 if (status == MagickFalse)
2709 if ((image->previous == (Image *) NULL) && (num_passes != 1))
2711 status=SetImageProgress(image,LoadImageTag,pass,num_passes);
2713 if (status == MagickFalse)
2717 quantum_scanline=(Quantum *) RelinquishMagickMemory(quantum_scanline);
2720 image->matte=found_transparent_pixel;
2722 if (logging != MagickFalse)
2724 if (found_transparent_pixel != MagickFalse)
2725 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2726 " Found transparent pixel");
2729 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2730 " No transparent pixel was found");
2732 ping_color_type&=0x03;
2737 if (quantum_info != (QuantumInfo *) NULL)
2738 quantum_info=DestroyQuantumInfo(quantum_info);
2740 if (image->storage_class == PseudoClass)
2746 image->matte=MagickFalse;
2747 (void) SyncImage(image);
2751 png_read_end(ping,ping_info);
2753 if (image_info->number_scenes != 0 && mng_info->scenes_found-1 <
2754 (ssize_t) image_info->first_scene && image->delay != 0)
2756 png_destroy_read_struct(&ping,&ping_info,&end_info);
2757 ping_pixels=(unsigned char *) RelinquishMagickMemory(ping_pixels);
2759 (void) SetImageBackgroundColor(image);
2760 #if defined(PNG_SETJMP_NOT_THREAD_SAFE)
2761 UnlockSemaphoreInfo(ping_semaphore);
2763 if (logging != MagickFalse)
2764 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2765 " exit ReadOnePNGImage() early.");
2769 if (png_get_valid(ping,ping_info,PNG_INFO_tRNS))
2775 Image has a transparent background.
2777 storage_class=image->storage_class;
2778 image->matte=MagickTrue;
2780 /* Balfour fix from imagemagick discourse server, 5 Feb 2010 */
2782 if (storage_class == PseudoClass)
2784 if ((int) ping_color_type == PNG_COLOR_TYPE_PALETTE)
2786 for (x=0; x < ping_num_trans; x++)
2788 image->colormap[x].opacity =
2789 ScaleCharToQuantum((unsigned char)(255-ping_trans_alpha[x]));
2793 else if (ping_color_type == PNG_COLOR_TYPE_GRAY)
2795 for (x=0; x < (int) image->colors; x++)
2797 if (ScaleQuantumToShort(image->colormap[x].red) ==
2798 transparent_color.opacity)
2800 image->colormap[x].opacity = (Quantum) TransparentOpacity;
2804 (void) SyncImage(image);
2807 #if 1 /* Should have already been done above, but glennrp problem P10
2812 for (y=0; y < (ssize_t) image->rows; y++)
2814 image->storage_class=storage_class;
2815 q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
2817 if (q == (PixelPacket *) NULL)
2820 indexes=GetAuthenticIndexQueue(image);
2822 /* Caution: on a Q8 build, this does not distinguish between
2823 * 16-bit colors that differ only in the low byte
2825 for (x=(ssize_t) image->columns-1; x >= 0; x--)
2827 if (ScaleQuantumToShort(q->red) == transparent_color.red &&
2828 ScaleQuantumToShort(q->green) == transparent_color.green &&
2829 ScaleQuantumToShort(q->blue) == transparent_color.blue)
2831 q->opacity=(Quantum) TransparentOpacity;
2834 #if 0 /* I have not found a case where this is needed. */
2837 q->opacity=(Quantum) OpaqueOpacity;
2844 if (SyncAuthenticPixels(image,exception) == MagickFalse)
2850 image->storage_class=DirectClass;
2853 if ((ping_color_type == PNG_COLOR_TYPE_GRAY) ||
2854 (ping_color_type == PNG_COLOR_TYPE_GRAY_ALPHA))
2855 image->colorspace=GRAYColorspace;
2857 if (png_get_text(ping,ping_info,&text,&num_text) != 0)
2858 for (i=0; i < (ssize_t) num_text; i++)
2860 /* Check for a profile */
2862 if (logging != MagickFalse)
2863 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2864 " Reading PNG text chunk");
2866 if (memcmp(text[i].key, "Raw profile type ",17) == 0)
2867 (void) Magick_png_read_raw_profile(image,image_info,text,(int) i);
2874 length=text[i].text_length;
2875 value=(char *) AcquireQuantumMemory(length+MaxTextExtent,
2877 if (value == (char *) NULL)
2879 (void) ThrowMagickException(&image->exception,GetMagickModule(),
2880 ResourceLimitError,"MemoryAllocationFailed","`%s'",
2885 (void) ConcatenateMagickString(value,text[i].text,length+2);
2887 /* Don't save "density" or "units" property if we have a pHYs
2890 if (!png_get_valid(ping,ping_info,PNG_INFO_pHYs) ||
2891 (LocaleCompare(text[i].key,"density") != 0 &&
2892 LocaleCompare(text[i].key,"units") != 0))
2893 (void) SetImageProperty(image,text[i].key,value);
2895 if (logging != MagickFalse)
2897 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2898 " length: %lu",(unsigned long) length);
2899 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2900 " Keyword: %s",text[i].key);
2903 value=DestroyString(value);
2907 #ifdef MNG_OBJECT_BUFFERS
2909 Store the object if necessary.
2911 if (object_id && !mng_info->frozen[object_id])
2913 if (mng_info->ob[object_id] == (MngBuffer *) NULL)
2916 create a new object buffer.
2918 mng_info->ob[object_id]=(MngBuffer *)
2919 AcquireMagickMemory(sizeof(MngBuffer));
2921 if (mng_info->ob[object_id] != (MngBuffer *) NULL)
2923 mng_info->ob[object_id]->image=(Image *) NULL;
2924 mng_info->ob[object_id]->reference_count=1;
2928 if ((mng_info->ob[object_id] == (MngBuffer *) NULL) ||
2929 mng_info->ob[object_id]->frozen)
2931 if (mng_info->ob[object_id] == (MngBuffer *) NULL)
2932 (void) ThrowMagickException(&image->exception,GetMagickModule(),
2933 ResourceLimitError,"MemoryAllocationFailed","`%s'",
2936 if (mng_info->ob[object_id]->frozen)
2937 (void) ThrowMagickException(&image->exception,GetMagickModule(),
2938 ResourceLimitError,"Cannot overwrite frozen MNG object buffer",
2939 "`%s'",image->filename);
2945 if (mng_info->ob[object_id]->image != (Image *) NULL)
2946 mng_info->ob[object_id]->image=DestroyImage
2947 (mng_info->ob[object_id]->image);
2949 mng_info->ob[object_id]->image=CloneImage(image,0,0,MagickTrue,
2952 if (mng_info->ob[object_id]->image != (Image *) NULL)
2953 mng_info->ob[object_id]->image->file=(FILE *) NULL;
2956 (void) ThrowMagickException(&image->exception,GetMagickModule(),
2957 ResourceLimitError,"Cloning image for object buffer failed",
2958 "`%s'",image->filename);
2960 if (ping_width > 250000L || ping_height > 250000L)
2961 png_error(ping,"PNG Image dimensions are too large.");
2963 mng_info->ob[object_id]->width=ping_width;
2964 mng_info->ob[object_id]->height=ping_height;
2965 mng_info->ob[object_id]->color_type=ping_color_type;
2966 mng_info->ob[object_id]->sample_depth=ping_bit_depth;
2967 mng_info->ob[object_id]->interlace_method=ping_interlace_method;
2968 mng_info->ob[object_id]->compression_method=
2969 ping_compression_method;
2970 mng_info->ob[object_id]->filter_method=ping_filter_method;
2972 if (png_get_valid(ping,ping_info,PNG_INFO_PLTE))
2981 Copy the PLTE to the object buffer.
2983 png_get_PLTE(ping,ping_info,&plte,&number_colors);
2984 mng_info->ob[object_id]->plte_length=number_colors;
2986 for (i=0; i < number_colors; i++)
2988 mng_info->ob[object_id]->plte[i]=plte[i];
2993 mng_info->ob[object_id]->plte_length=0;
2998 Relinquish resources.
3000 png_destroy_read_struct(&ping,&ping_info,&end_info);
3002 ping_pixels=(unsigned char *) RelinquishMagickMemory(ping_pixels);
3003 #if defined(PNG_SETJMP_NOT_THREAD_SAFE)
3004 UnlockSemaphoreInfo(ping_semaphore);
3007 if (logging != MagickFalse)
3008 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
3009 " exit ReadOnePNGImage()");
3013 /* end of reading one PNG image */
3016 static Image *ReadPNGImage(const ImageInfo *image_info,ExceptionInfo *exception)
3031 magic_number[MaxTextExtent];
3039 assert(image_info != (const ImageInfo *) NULL);
3040 assert(image_info->signature == MagickSignature);
3042 if (image_info->debug != MagickFalse)
3043 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
3044 image_info->filename);
3046 assert(exception != (ExceptionInfo *) NULL);
3047 assert(exception->signature == MagickSignature);
3048 logging=LogMagickEvent(CoderEvent,GetMagickModule(),"Enter ReadPNGImage()");
3049 image=AcquireImage(image_info);
3050 mng_info=(MngInfo *) NULL;
3051 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
3053 if (status == MagickFalse)
3054 ThrowReaderException(FileOpenError,"UnableToOpenFile");
3057 Verify PNG signature.
3059 count=ReadBlob(image,8,(unsigned char *) magic_number);
3061 if (count < 8 || memcmp(magic_number,"\211PNG\r\n\032\n",8) != 0)
3062 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
3065 Allocate a MngInfo structure.
3067 have_mng_structure=MagickFalse;
3068 mng_info=(MngInfo *) AcquireMagickMemory(sizeof(MngInfo));
3070 if (mng_info == (MngInfo *) NULL)
3071 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
3074 Initialize members of the MngInfo structure.
3076 (void) ResetMagickMemory(mng_info,0,sizeof(MngInfo));
3077 mng_info->image=image;
3078 have_mng_structure=MagickTrue;
3081 image=ReadOnePNGImage(mng_info,image_info,exception);
3082 MngInfoFreeStruct(mng_info,&have_mng_structure);
3084 if (image == (Image *) NULL)
3086 if (previous != (Image *) NULL)
3088 if (previous->signature != MagickSignature)
3089 ThrowReaderException(CorruptImageError,"CorruptImage");
3091 (void) CloseBlob(previous);
3092 (void) DestroyImageList(previous);
3095 if (logging != MagickFalse)
3096 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
3097 "exit ReadPNGImage() with error");
3099 return((Image *) NULL);
3102 (void) CloseBlob(image);
3104 if ((image->columns == 0) || (image->rows == 0))
3106 if (logging != MagickFalse)
3107 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
3108 "exit ReadPNGImage() with error.");
3110 ThrowReaderException(CorruptImageError,"CorruptImage");
3113 if (LocaleCompare(image_info->magick,"PNG8") == 0)
3115 (void) SetImageType(image,PaletteType);
3117 if (image->matte != MagickFalse)
3119 /* To do: Reduce to binary transparency */
3123 if (LocaleCompare(image_info->magick,"PNG24") == 0)
3125 (void) SetImageType(image,TrueColorType);
3126 image->matte=MagickFalse;
3129 if (LocaleCompare(image_info->magick,"PNG32") == 0)
3130 (void) SetImageType(image,TrueColorMatteType);
3132 if (logging != MagickFalse)
3133 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
3134 " page.w: %.20g, page.h: %.20g,page.x: %.20g, page.y: %.20g.",
3135 (double) image->page.width,(double) image->page.height,
3136 (double) image->page.x,(double) image->page.y);
3138 if (logging != MagickFalse)
3139 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"exit ReadPNGImage()");
3146 #if defined(JNG_SUPPORTED)
3148 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3152 % R e a d O n e J N G I m a g e %
3156 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3158 % ReadOneJNGImage() reads a JPEG Network Graphics (JNG) image file
3159 % (minus the 8-byte signature) and returns it. It allocates the memory
3160 % necessary for the new Image structure and returns a pointer to the new
3163 % JNG support written by Glenn Randers-Pehrson, glennrp@image...
3165 % The format of the ReadOneJNGImage method is:
3167 % Image *ReadOneJNGImage(MngInfo *mng_info, const ImageInfo *image_info,
3168 % ExceptionInfo *exception)
3170 % A description of each parameter follows:
3172 % o mng_info: Specifies a pointer to a MngInfo structure.
3174 % o image_info: the image info.
3176 % o exception: return any errors or warnings in this structure.
3179 static Image *ReadOneJNGImage(MngInfo *mng_info,
3180 const ImageInfo *image_info, ExceptionInfo *exception)
3207 jng_image_sample_depth,
3208 jng_image_compression_method,
3209 jng_image_interlace_method,
3210 jng_alpha_sample_depth,
3211 jng_alpha_compression_method,
3212 jng_alpha_filter_method,
3213 jng_alpha_interlace_method;
3215 register const PixelPacket
3222 register PixelPacket
3225 register unsigned char
3236 jng_alpha_compression_method=0;
3237 jng_alpha_sample_depth=8;
3241 alpha_image=(Image *) NULL;
3242 color_image=(Image *) NULL;
3243 alpha_image_info=(ImageInfo *) NULL;
3244 color_image_info=(ImageInfo *) NULL;
3246 logging=LogMagickEvent(CoderEvent,GetMagickModule(),
3247 " Enter ReadOneJNGImage()");
3249 image=mng_info->image;
3251 if (GetAuthenticPixelQueue(image) != (PixelPacket *) NULL)
3254 Allocate next image structure.
3256 if (logging != MagickFalse)
3257 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
3258 " AcquireNextImage()");
3260 AcquireNextImage(image_info,image);
3262 if (GetNextImageInList(image) == (Image *) NULL)
3263 return((Image *) NULL);
3265 image=SyncNextImageInList(image);
3267 mng_info->image=image;
3270 Signature bytes have already been read.
3273 read_JSEP=MagickFalse;
3274 reading_idat=MagickFalse;
3275 skip_to_iend=MagickFalse;
3279 type[MaxTextExtent];
3288 Read a new JNG chunk.
3290 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
3291 2*GetBlobSize(image));
3293 if (status == MagickFalse)
3297 (void) ConcatenateMagickString(type,"errr",MaxTextExtent);
3298 length=ReadBlobMSBLong(image);
3299 count=(unsigned int) ReadBlob(image,4,(unsigned char *) type);
3301 if (logging != MagickFalse)
3302 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
3303 " Reading JNG chunk type %c%c%c%c, length: %.20g",
3304 type[0],type[1],type[2],type[3],(double) length);
3306 if (length > PNG_UINT_31_MAX || count == 0)
3307 ThrowReaderException(CorruptImageError,"CorruptImage");
3310 chunk=(unsigned char *) NULL;
3314 chunk=(unsigned char *) AcquireQuantumMemory(length,sizeof(*chunk));
3316 if (chunk == (unsigned char *) NULL)
3317 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
3319 for (i=0; i < (ssize_t) length; i++)
3320 chunk[i]=(unsigned char) ReadBlobByte(image);
3325 (void) ReadBlobMSBLong(image); /* read crc word */
3330 chunk=(unsigned char *) RelinquishMagickMemory(chunk);
3335 if (memcmp(type,mng_JHDR,4) == 0)
3339 jng_width=(size_t) ((p[0] << 24) | (p[1] << 16) |
3340 (p[2] << 8) | p[3]);
3341 jng_height=(size_t) ((p[4] << 24) | (p[5] << 16) |
3342 (p[6] << 8) | p[7]);
3343 jng_color_type=p[8];
3344 jng_image_sample_depth=p[9];
3345 jng_image_compression_method=p[10];
3346 jng_image_interlace_method=p[11];
3348 image->interlace=jng_image_interlace_method != 0 ? PNGInterlace :
3351 jng_alpha_sample_depth=p[12];
3352 jng_alpha_compression_method=p[13];
3353 jng_alpha_filter_method=p[14];
3354 jng_alpha_interlace_method=p[15];
3356 if (logging != MagickFalse)
3358 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
3359 " jng_width: %16lu",(unsigned long) jng_width);
3361 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
3362 " jng_width: %16lu",(unsigned long) jng_height);
3364 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
3365 " jng_color_type: %16d",jng_color_type);
3367 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
3368 " jng_image_sample_depth: %3d",
3369 jng_image_sample_depth);
3371 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
3372 " jng_image_compression_method:%3d",
3373 jng_image_compression_method);
3375 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
3376 " jng_image_interlace_method: %3d",
3377 jng_image_interlace_method);
3379 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
3380 " jng_alpha_sample_depth: %3d",
3381 jng_alpha_sample_depth);
3383 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
3384 " jng_alpha_compression_method:%3d",
3385 jng_alpha_compression_method);
3387 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
3388 " jng_alpha_filter_method: %3d",
3389 jng_alpha_filter_method);
3391 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
3392 " jng_alpha_interlace_method: %3d",
3393 jng_alpha_interlace_method);
3398 chunk=(unsigned char *) RelinquishMagickMemory(chunk);
3404 if ((reading_idat == MagickFalse) && (read_JSEP == MagickFalse) &&
3405 ((memcmp(type,mng_JDAT,4) == 0) || (memcmp(type,mng_JdAA,4) == 0) ||
3406 (memcmp(type,mng_IDAT,4) == 0) || (memcmp(type,mng_JDAA,4) == 0)))
3409 o create color_image
3410 o open color_blob, attached to color_image
3411 o if (color type has alpha)
3412 open alpha_blob, attached to alpha_image
3415 color_image_info=(ImageInfo *)AcquireMagickMemory(sizeof(ImageInfo));
3417 if (color_image_info == (ImageInfo *) NULL)
3418 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
3420 GetImageInfo(color_image_info);
3421 color_image=AcquireImage(color_image_info);
3423 if (color_image == (Image *) NULL)
3424 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
3426 if (logging != MagickFalse)
3427 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
3428 " Creating color_blob.");
3430 (void) AcquireUniqueFilename(color_image->filename);
3431 status=OpenBlob(color_image_info,color_image,WriteBinaryBlobMode,
3434 if (status == MagickFalse)
3435 return((Image *) NULL);
3437 if ((image_info->ping == MagickFalse) && (jng_color_type >= 12))
3439 alpha_image_info=(ImageInfo *)
3440 AcquireMagickMemory(sizeof(ImageInfo));
3442 if (alpha_image_info == (ImageInfo *) NULL)
3443 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
3445 GetImageInfo(alpha_image_info);
3446 alpha_image=AcquireImage(alpha_image_info);
3448 if (alpha_image == (Image *) NULL)
3450 alpha_image=DestroyImage(alpha_image);
3451 ThrowReaderException(ResourceLimitError,
3452 "MemoryAllocationFailed");
3455 if (logging != MagickFalse)
3456 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
3457 " Creating alpha_blob.");
3459 (void) AcquireUniqueFilename(alpha_image->filename);
3460 status=OpenBlob(alpha_image_info,alpha_image,WriteBinaryBlobMode,
3463 if (status == MagickFalse)
3464 return((Image *) NULL);
3466 if (jng_alpha_compression_method == 0)
3471 if (logging != MagickFalse)
3472 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
3473 " Writing IHDR chunk to alpha_blob.");
3475 (void) WriteBlob(alpha_image,8,(const unsigned char *)
3476 "\211PNG\r\n\032\n");
3478 (void) WriteBlobMSBULong(alpha_image,13L);
3479 PNGType(data,mng_IHDR);
3480 LogPNGChunk(logging,mng_IHDR,13L);
3481 PNGLong(data+4,jng_width);
3482 PNGLong(data+8,jng_height);
3483 data[12]=jng_alpha_sample_depth;
3484 data[13]=0; /* color_type gray */
3485 data[14]=0; /* compression method 0 */
3486 data[15]=0; /* filter_method 0 */
3487 data[16]=0; /* interlace_method 0 */
3488 (void) WriteBlob(alpha_image,17,data);
3489 (void) WriteBlobMSBULong(alpha_image,crc32(0,data,17));
3492 reading_idat=MagickTrue;
3495 if (memcmp(type,mng_JDAT,4) == 0)
3497 /* Copy chunk to color_image->blob */
3499 if (logging != MagickFalse)
3500 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
3501 " Copying JDAT chunk data to color_blob.");
3503 (void) WriteBlob(color_image,length,chunk);
3506 chunk=(unsigned char *) RelinquishMagickMemory(chunk);
3511 if (memcmp(type,mng_IDAT,4) == 0)
3516 /* Copy IDAT header and chunk data to alpha_image->blob */
3518 if (image_info->ping == MagickFalse)
3520 if (logging != MagickFalse)
3521 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
3522 " Copying IDAT chunk data to alpha_blob.");
3524 (void) WriteBlobMSBULong(alpha_image,(size_t) length);
3525 PNGType(data,mng_IDAT);
3526 LogPNGChunk(logging,mng_IDAT,length);
3527 (void) WriteBlob(alpha_image,4,data);
3528 (void) WriteBlob(alpha_image,length,chunk);
3529 (void) WriteBlobMSBULong(alpha_image,
3530 crc32(crc32(0,data,4),chunk,(uInt) length));
3534 chunk=(unsigned char *) RelinquishMagickMemory(chunk);
3539 if ((memcmp(type,mng_JDAA,4) == 0) || (memcmp(type,mng_JdAA,4) == 0))
3541 /* Copy chunk data to alpha_image->blob */
3543 if (image_info->ping == MagickFalse)
3545 if (logging != MagickFalse)
3546 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
3547 " Copying JDAA chunk data to alpha_blob.");
3549 (void) WriteBlob(alpha_image,length,chunk);
3553 chunk=(unsigned char *) RelinquishMagickMemory(chunk);
3558 if (memcmp(type,mng_JSEP,4) == 0)
3560 read_JSEP=MagickTrue;
3563 chunk=(unsigned char *) RelinquishMagickMemory(chunk);
3568 if (memcmp(type,mng_bKGD,4) == 0)
3572 image->background_color.red=ScaleCharToQuantum(p[1]);
3573 image->background_color.green=image->background_color.red;
3574 image->background_color.blue=image->background_color.red;
3579 image->background_color.red=ScaleCharToQuantum(p[1]);
3580 image->background_color.green=ScaleCharToQuantum(p[3]);
3581 image->background_color.blue=ScaleCharToQuantum(p[5]);
3584 chunk=(unsigned char *) RelinquishMagickMemory(chunk);
3588 if (memcmp(type,mng_gAMA,4) == 0)
3591 image->gamma=((float) mng_get_long(p))*0.00001;
3593 chunk=(unsigned char *) RelinquishMagickMemory(chunk);
3597 if (memcmp(type,mng_cHRM,4) == 0)
3601 image->chromaticity.white_point.x=0.00001*mng_get_long(p);
3602 image->chromaticity.white_point.y=0.00001*mng_get_long(&p[4]);
3603 image->chromaticity.red_primary.x=0.00001*mng_get_long(&p[8]);
3604 image->chromaticity.red_primary.y=0.00001*mng_get_long(&p[12]);
3605 image->chromaticity.green_primary.x=0.00001*mng_get_long(&p[16]);
3606 image->chromaticity.green_primary.y=0.00001*mng_get_long(&p[20]);
3607 image->chromaticity.blue_primary.x=0.00001*mng_get_long(&p[24]);
3608 image->chromaticity.blue_primary.y=0.00001*mng_get_long(&p[28]);
3611 chunk=(unsigned char *) RelinquishMagickMemory(chunk);
3615 if (memcmp(type,mng_sRGB,4) == 0)
3619 image->rendering_intent=
3620 Magick_RenderingIntent_from_PNG_RenderingIntent(p[0]);
3621 image->gamma=0.45455f;
3622 image->chromaticity.red_primary.x=0.6400f;
3623 image->chromaticity.red_primary.y=0.3300f;
3624 image->chromaticity.green_primary.x=0.3000f;
3625 image->chromaticity.green_primary.y=0.6000f;
3626 image->chromaticity.blue_primary.x=0.1500f;
3627 image->chromaticity.blue_primary.y=0.0600f;
3628 image->chromaticity.white_point.x=0.3127f;
3629 image->chromaticity.white_point.y=0.3290f;
3632 chunk=(unsigned char *) RelinquishMagickMemory(chunk);
3636 if (memcmp(type,mng_oFFs,4) == 0)
3640 image->page.x=(ssize_t) mng_get_long(p);
3641 image->page.y=(ssize_t) mng_get_long(&p[4]);
3643 if ((int) p[8] != 0)
3645 image->page.x/=10000;
3646 image->page.y/=10000;
3651 chunk=(unsigned char *) RelinquishMagickMemory(chunk);
3656 if (memcmp(type,mng_pHYs,4) == 0)
3660 image->x_resolution=(double) mng_get_long(p);
3661 image->y_resolution=(double) mng_get_long(&p[4]);
3662 if ((int) p[8] == PNG_RESOLUTION_METER)
3664 image->units=PixelsPerCentimeterResolution;
3665 image->x_resolution=image->x_resolution/100.0f;
3666 image->y_resolution=image->y_resolution/100.0f;
3670 chunk=(unsigned char *) RelinquishMagickMemory(chunk);
3675 if (memcmp(type,mng_iCCP,4) == 0)
3679 chunk=(unsigned char *) RelinquishMagickMemory(chunk);
3686 chunk=(unsigned char *) RelinquishMagickMemory(chunk);
3688 if (memcmp(type,mng_IEND,4))
3698 Finish up reading image data:
3700 o read main image from color_blob.
3704 o if (color_type has alpha)
3705 if alpha_encoding is PNG
3706 read secondary image from alpha_blob via ReadPNG
3707 if alpha_encoding is JPEG
3708 read secondary image from alpha_blob via ReadJPEG
3712 o copy intensity of secondary image into
3713 opacity samples of main image.
3715 o destroy the secondary image.
3718 (void) CloseBlob(color_image);
3720 if (logging != MagickFalse)
3721 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
3722 " Reading jng_image from color_blob.");
3724 (void) FormatMagickString(color_image_info->filename,MaxTextExtent,"%s",
3725 color_image->filename);
3727 color_image_info->ping=MagickFalse; /* To do: avoid this */
3728 jng_image=ReadImage(color_image_info,exception);
3730 if (jng_image == (Image *) NULL)
3731 return((Image *) NULL);
3733 (void) RelinquishUniqueFileResource(color_image->filename);
3734 color_image=DestroyImage(color_image);
3735 color_image_info=DestroyImageInfo(color_image_info);
3737 if (jng_image == (Image *) NULL)
3738 return((Image *) NULL);
3740 if (logging != MagickFalse)
3741 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
3742 " Copying jng_image pixels to main image.");
3744 image->rows=jng_height;
3745 image->columns=jng_width;
3746 length=image->columns*sizeof(PixelPacket);
3748 for (y=0; y < (ssize_t) image->rows; y++)
3750 s=GetVirtualPixels(jng_image,0,y,image->columns,1,&image->exception);
3751 q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
3752 (void) CopyMagickMemory(q,s,length);
3754 if (SyncAuthenticPixels(image,exception) == MagickFalse)
3758 jng_image=DestroyImage(jng_image);
3760 if (image_info->ping == MagickFalse)
3762 if (jng_color_type >= 12)
3764 if (jng_alpha_compression_method == 0)
3768 (void) WriteBlobMSBULong(alpha_image,0x00000000L);
3769 PNGType(data,mng_IEND);
3770 LogPNGChunk(logging,mng_IEND,0L);
3771 (void) WriteBlob(alpha_image,4,data);
3772 (void) WriteBlobMSBULong(alpha_image,crc32(0,data,4));
3775 (void) CloseBlob(alpha_image);
3777 if (logging != MagickFalse)
3778 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
3779 " Reading opacity from alpha_blob.");
3781 (void) FormatMagickString(alpha_image_info->filename,MaxTextExtent,
3782 "%s",alpha_image->filename);
3784 jng_image=ReadImage(alpha_image_info,exception);
3786 if (jng_image != (Image *) NULL)
3787 for (y=0; y < (ssize_t) image->rows; y++)
3789 s=GetVirtualPixels(jng_image,0,y,image->columns,1,
3791 q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
3793 if (image->matte != MagickFalse)
3794 for (x=(ssize_t) image->columns; x != 0; x--,q++,s++)
3795 q->opacity=(Quantum) QuantumRange-s->red;
3798 for (x=(ssize_t) image->columns; x != 0; x--,q++,s++)
3800 q->opacity=(Quantum) QuantumRange-s->red;
3801 if (q->opacity != OpaqueOpacity)
3802 image->matte=MagickTrue;
3805 if (SyncAuthenticPixels(image,exception) == MagickFalse)
3808 (void) RelinquishUniqueFileResource(alpha_image->filename);
3809 alpha_image=DestroyImage(alpha_image);
3810 alpha_image_info=DestroyImageInfo(alpha_image_info);
3811 if (jng_image != (Image *) NULL)
3812 jng_image=DestroyImage(jng_image);
3816 /* Read the JNG image. */
3818 if (mng_info->mng_type == 0)
3820 mng_info->mng_width=jng_width;
3821 mng_info->mng_height=jng_height;
3824 if (image->page.width == 0 && image->page.height == 0)
3826 image->page.width=jng_width;
3827 image->page.height=jng_height;
3830 if (image->page.x == 0 && image->page.y == 0)
3832 image->page.x=mng_info->x_off[mng_info->object_id];
3833 image->page.y=mng_info->y_off[mng_info->object_id];
3838 image->page.y=mng_info->y_off[mng_info->object_id];
3841 mng_info->image_found++;
3842 status=SetImageProgress(image,LoadImagesTag,2*TellBlob(image),
3843 2*GetBlobSize(image));
3845 if (logging != MagickFalse)
3846 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
3847 " exit ReadOneJNGImage()");
3853 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3857 % R e a d J N G I m a g e %
3861 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3863 % ReadJNGImage() reads a JPEG Network Graphics (JNG) image file
3864 % (including the 8-byte signature) and returns it. It allocates the memory
3865 % necessary for the new Image structure and returns a pointer to the new
3868 % JNG support written by Glenn Randers-Pehrson, glennrp@image...
3870 % The format of the ReadJNGImage method is:
3872 % Image *ReadJNGImage(const ImageInfo *image_info, ExceptionInfo
3875 % A description of each parameter follows:
3877 % o image_info: the image info.
3879 % o exception: return any errors or warnings in this structure.
3883 static Image *ReadJNGImage(const ImageInfo *image_info,ExceptionInfo *exception)
3898 magic_number[MaxTextExtent];
3906 assert(image_info != (const ImageInfo *) NULL);
3907 assert(image_info->signature == MagickSignature);
3908 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image_info->filename);
3909 assert(exception != (ExceptionInfo *) NULL);
3910 assert(exception->signature == MagickSignature);
3911 logging=LogMagickEvent(CoderEvent,GetMagickModule(),"Enter ReadJNGImage()");
3912 image=AcquireImage(image_info);
3913 mng_info=(MngInfo *) NULL;
3914 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
3916 if (status == MagickFalse)
3917 return((Image *) NULL);
3919 if (LocaleCompare(image_info->magick,"JNG") != 0)
3920 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
3922 /* Verify JNG signature. */
3924 count=(size_t) ReadBlob(image,8,(unsigned char *) magic_number);
3926 if (count < 8 || memcmp(magic_number,"\213JNG\r\n\032\n",8) != 0)
3927 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
3929 /* Allocate a MngInfo structure. */
3931 have_mng_structure=MagickFalse;
3932 mng_info=(MngInfo *) AcquireMagickMemory(sizeof(*mng_info));
3934 if (mng_info == (MngInfo *) NULL)
3935 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
3937 /* Initialize members of the MngInfo structure. */
3939 (void) ResetMagickMemory(mng_info,0,sizeof(MngInfo));
3940 have_mng_structure=MagickTrue;
3942 mng_info->image=image;
3944 image=ReadOneJNGImage(mng_info,image_info,exception);
3945 MngInfoFreeStruct(mng_info,&have_mng_structure);
3947 if (image == (Image *) NULL)
3949 if (IsImageObject(previous) != MagickFalse)
3951 (void) CloseBlob(previous);
3952 (void) DestroyImageList(previous);
3955 if (logging != MagickFalse)
3956 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
3957 "exit ReadJNGImage() with error");
3959 return((Image *) NULL);
3961 (void) CloseBlob(image);
3963 if (image->columns == 0 || image->rows == 0)
3965 if (logging != MagickFalse)
3966 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
3967 "exit ReadJNGImage() with error");
3969 ThrowReaderException(CorruptImageError,"CorruptImage");
3972 if (logging != MagickFalse)
3973 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"exit ReadJNGImage()");
3979 static Image *ReadMNGImage(const ImageInfo *image_info,ExceptionInfo *exception)
3982 page_geometry[MaxTextExtent];
4015 #if defined(MNG_INSERT_LAYERS)
4017 mng_background_color;
4020 register unsigned char
4035 #if defined(MNG_INSERT_LAYERS)
4040 volatile unsigned int
4041 #ifdef MNG_OBJECT_BUFFERS
4042 mng_background_object=0,
4044 mng_type=0; /* 0: PNG or JNG; 1: MNG; 2: MNG-LC; 3: MNG-VLC */
4047 default_frame_timeout,
4049 #if defined(MNG_INSERT_LAYERS)
4055 /* These delays are all measured in image ticks_per_second,
4056 * not in MNG ticks_per_second
4059 default_frame_delay,
4063 #if defined(MNG_INSERT_LAYERS)
4072 previous_fb.bottom=0;
4074 previous_fb.right=0;
4076 default_fb.bottom=0;
4080 /* Open image file. */
4082 assert(image_info != (const ImageInfo *) NULL);
4083 assert(image_info->signature == MagickSignature);
4084 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image_info->filename);
4085 assert(exception != (ExceptionInfo *) NULL);
4086 assert(exception->signature == MagickSignature);
4087 logging=LogMagickEvent(CoderEvent,GetMagickModule(),"Enter ReadMNGImage()");
4088 image=AcquireImage(image_info);
4089 mng_info=(MngInfo *) NULL;
4090 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
4092 if (status == MagickFalse)
4093 return((Image *) NULL);
4095 first_mng_object=MagickFalse;
4097 have_mng_structure=MagickFalse;
4099 /* Allocate a MngInfo structure. */
4101 mng_info=(MngInfo *) AcquireMagickMemory(sizeof(MngInfo));
4103 if (mng_info == (MngInfo *) NULL)
4104 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
4106 /* Initialize members of the MngInfo structure. */
4108 (void) ResetMagickMemory(mng_info,0,sizeof(MngInfo));
4109 mng_info->image=image;
4110 have_mng_structure=MagickTrue;
4112 if (LocaleCompare(image_info->magick,"MNG") == 0)
4115 magic_number[MaxTextExtent];
4117 /* Verify MNG signature. */
4118 count=(size_t) ReadBlob(image,8,(unsigned char *) magic_number);
4119 if (memcmp(magic_number,"\212MNG\r\n\032\n",8) != 0)
4120 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
4122 /* Initialize some nonzero members of the MngInfo structure. */
4123 for (i=0; i < MNG_MAX_OBJECTS; i++)
4125 mng_info->object_clip[i].right=(ssize_t) PNG_UINT_31_MAX;
4126 mng_info->object_clip[i].bottom=(ssize_t) PNG_UINT_31_MAX;
4128 mng_info->exists[0]=MagickTrue;
4131 first_mng_object=MagickTrue;
4133 #if defined(MNG_INSERT_LAYERS)
4134 insert_layers=MagickFalse; /* should be False when converting or mogrifying */
4136 default_frame_delay=0;
4137 default_frame_timeout=0;
4140 mng_info->ticks_per_second=1UL*image->ticks_per_second;
4142 skip_to_iend=MagickFalse;
4143 term_chunk_found=MagickFalse;
4144 mng_info->framing_mode=1;
4145 #if defined(MNG_INSERT_LAYERS)
4146 mandatory_back=MagickFalse;
4148 #if defined(MNG_INSERT_LAYERS)
4149 mng_background_color=image->background_color;
4151 default_fb=mng_info->frame;
4152 previous_fb=mng_info->frame;
4156 type[MaxTextExtent];
4158 if (LocaleCompare(image_info->magick,"MNG") == 0)
4167 (void) ConcatenateMagickString(type,"errr",MaxTextExtent);
4168 length=ReadBlobMSBLong(image);
4169 count=(size_t) ReadBlob(image,4,(unsigned char *) type);
4171 if (logging != MagickFalse)
4172 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
4173 " Reading MNG chunk type %c%c%c%c, length: %.20g",
4174 type[0],type[1],type[2],type[3],(double) length);
4176 if (length > PNG_UINT_31_MAX)
4180 ThrowReaderException(CorruptImageError,"CorruptImage");
4183 chunk=(unsigned char *) NULL;
4187 chunk=(unsigned char *) AcquireQuantumMemory(length,sizeof(*chunk));
4189 if (chunk == (unsigned char *) NULL)
4190 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
4192 for (i=0; i < (ssize_t) length; i++)
4193 chunk[i]=(unsigned char) ReadBlobByte(image);
4198 (void) ReadBlobMSBLong(image); /* read crc word */
4200 #if !defined(JNG_SUPPORTED)
4201 if (memcmp(type,mng_JHDR,4) == 0)
4203 skip_to_iend=MagickTrue;
4205 if (mng_info->jhdr_warning == 0)
4206 (void) ThrowMagickException(&image->exception,GetMagickModule(),
4207 CoderError,"JNGCompressNotSupported","`%s'",image->filename);
4209 mng_info->jhdr_warning++;
4212 if (memcmp(type,mng_DHDR,4) == 0)
4214 skip_to_iend=MagickTrue;
4216 if (mng_info->dhdr_warning == 0)
4217 (void) ThrowMagickException(&image->exception,GetMagickModule(),
4218 CoderError,"DeltaPNGNotSupported","`%s'",image->filename);
4220 mng_info->dhdr_warning++;
4222 if (memcmp(type,mng_MEND,4) == 0)
4227 if (memcmp(type,mng_IEND,4) == 0)
4228 skip_to_iend=MagickFalse;
4231 chunk=(unsigned char *) RelinquishMagickMemory(chunk);
4233 if (logging != MagickFalse)
4234 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
4240 if (memcmp(type,mng_MHDR,4) == 0)
4242 mng_info->mng_width=(size_t) ((p[0] << 24) | (p[1] << 16) |
4243 (p[2] << 8) | p[3]);
4245 mng_info->mng_height=(size_t) ((p[4] << 24) | (p[5] << 16) |
4246 (p[6] << 8) | p[7]);
4248 if (logging != MagickFalse)
4250 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
4251 " MNG width: %.20g",(double) mng_info->mng_width);
4252 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
4253 " MNG height: %.20g",(double) mng_info->mng_height);
4257 mng_info->ticks_per_second=(size_t) mng_get_long(p);
4259 if (mng_info->ticks_per_second == 0)
4260 default_frame_delay=0;
4263 default_frame_delay=1UL*image->ticks_per_second/
4264 mng_info->ticks_per_second;
4266 frame_delay=default_frame_delay;
4272 simplicity=(size_t) mng_get_long(p);
4275 mng_type=1; /* Full MNG */
4277 if ((simplicity != 0) && ((simplicity | 11) == 11))
4278 mng_type=2; /* LC */
4280 if ((simplicity != 0) && ((simplicity | 9) == 9))
4281 mng_type=3; /* VLC */
4283 #if defined(MNG_INSERT_LAYERS)
4285 insert_layers=MagickTrue;
4287 if (GetAuthenticPixelQueue(image) != (PixelPacket *) NULL)
4289 /* Allocate next image structure. */
4290 AcquireNextImage(image_info,image);
4292 if (GetNextImageInList(image) == (Image *) NULL)
4293 return((Image *) NULL);
4295 image=SyncNextImageInList(image);
4296 mng_info->image=image;
4299 if ((mng_info->mng_width > 65535L) ||
4300 (mng_info->mng_height > 65535L))
4301 ThrowReaderException(ImageError,"WidthOrHeightExceedsLimit");
4303 (void) FormatMagickString(page_geometry,MaxTextExtent,
4304 "%.20gx%.20g+0+0",(double) mng_info->mng_width,(double)
4305 mng_info->mng_height);
4307 mng_info->frame.left=0;
4308 mng_info->frame.right=(ssize_t) mng_info->mng_width;
4309 mng_info->frame.top=0;
4310 mng_info->frame.bottom=(ssize_t) mng_info->mng_height;
4311 mng_info->clip=default_fb=previous_fb=mng_info->frame;
4313 for (i=0; i < MNG_MAX_OBJECTS; i++)
4314 mng_info->object_clip[i]=mng_info->frame;
4316 chunk=(unsigned char *) RelinquishMagickMemory(chunk);
4320 if (memcmp(type,mng_TERM,4) == 0)
4331 final_delay=(png_uint_32) mng_get_long(&p[2]);
4332 mng_iterations=(png_uint_32) mng_get_long(&p[6]);
4334 if (mng_iterations == PNG_UINT_31_MAX)
4337 image->iterations=mng_iterations;
4338 term_chunk_found=MagickTrue;
4341 if (logging != MagickFalse)
4343 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
4344 " repeat=%d",repeat);
4346 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
4347 " final_delay=%.20g",(double) final_delay);
4349 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
4350 " image->iterations=%.20g",(double) image->iterations);
4353 chunk=(unsigned char *) RelinquishMagickMemory(chunk);
4356 if (memcmp(type,mng_DEFI,4) == 0)
4359 (void) ThrowMagickException(&image->exception,GetMagickModule(),
4360 CoderError,"DEFI chunk found in MNG-VLC datastream","`%s'",
4363 object_id=(p[0] << 8) | p[1];
4365 if (mng_type == 2 && object_id != 0)
4366 (void) ThrowMagickException(&image->exception,GetMagickModule(),
4367 CoderError,"Nonzero object_id in MNG-LC datastream","`%s'",
4370 if (object_id > MNG_MAX_OBJECTS)
4373 Instead ofsuing a warning we should allocate a larger
4374 MngInfo structure and continue.
4376 (void) ThrowMagickException(&image->exception,GetMagickModule(),
4377 CoderError,"object id too large","`%s'",image->filename);
4378 object_id=MNG_MAX_OBJECTS;
4381 if (mng_info->exists[object_id])
4382 if (mng_info->frozen[object_id])
4384 chunk=(unsigned char *) RelinquishMagickMemory(chunk);
4385 (void) ThrowMagickException(&image->exception,
4386 GetMagickModule(),CoderError,
4387 "DEFI cannot redefine a frozen MNG object","`%s'",
4392 mng_info->exists[object_id]=MagickTrue;
4395 mng_info->invisible[object_id]=p[2];
4398 Extract object offset info.
4402 mng_info->x_off[object_id]=(ssize_t) ((p[4] << 24) |
4403 (p[5] << 16) | (p[6] << 8) | p[7]);
4405 mng_info->y_off[object_id]=(ssize_t) ((p[8] << 24) |
4406 (p[9] << 16) | (p[10] << 8) | p[11]);
4408 if (logging != MagickFalse)
4410 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
4411 " x_off[%d]: %.20g",object_id,(double)
4412 mng_info->x_off[object_id]);
4414 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
4415 " y_off[%d]: %.20g",object_id,(double)
4416 mng_info->y_off[object_id]);
4421 Extract object clipping info.
4424 mng_info->object_clip[object_id]=mng_read_box(mng_info->frame,0,
4427 chunk=(unsigned char *) RelinquishMagickMemory(chunk);
4430 if (memcmp(type,mng_bKGD,4) == 0)
4432 mng_info->have_global_bkgd=MagickFalse;
4436 mng_info->mng_global_bkgd.red=
4437 ScaleShortToQuantum((unsigned short) ((p[0] << 8) | p[1]));
4439 mng_info->mng_global_bkgd.green=
4440 ScaleShortToQuantum((unsigned short) ((p[2] << 8) | p[3]));
4442 mng_info->mng_global_bkgd.blue=
4443 ScaleShortToQuantum((unsigned short) ((p[4] << 8) | p[5]));
4445 mng_info->have_global_bkgd=MagickTrue;
4448 chunk=(unsigned char *) RelinquishMagickMemory(chunk);
4451 if (memcmp(type,mng_BACK,4) == 0)
4453 #if defined(MNG_INSERT_LAYERS)
4455 mandatory_back=p[6];
4460 if (mandatory_back && length > 5)
4462 mng_background_color.red=
4463 ScaleShortToQuantum((unsigned short) ((p[0] << 8) | p[1]));
4465 mng_background_color.green=
4466 ScaleShortToQuantum((unsigned short) ((p[2] << 8) | p[3]));
4468 mng_background_color.blue=
4469 ScaleShortToQuantum((unsigned short) ((p[4] << 8) | p[5]));
4471 mng_background_color.opacity=OpaqueOpacity;
4474 #ifdef MNG_OBJECT_BUFFERS
4476 mng_background_object=(p[7] << 8) | p[8];
4479 chunk=(unsigned char *) RelinquishMagickMemory(chunk);
4483 if (memcmp(type,mng_PLTE,4) == 0)
4485 /* Read global PLTE. */
4487 if (length && (length < 769))
4489 if (mng_info->global_plte == (png_colorp) NULL)
4490 mng_info->global_plte=(png_colorp) AcquireQuantumMemory(256,
4491 sizeof(*mng_info->global_plte));
4493 for (i=0; i < (ssize_t) (length/3); i++)
4495 mng_info->global_plte[i].red=p[3*i];
4496 mng_info->global_plte[i].green=p[3*i+1];
4497 mng_info->global_plte[i].blue=p[3*i+2];
4500 mng_info->global_plte_length=(unsigned int) (length/3);
4503 for ( ; i < 256; i++)
4505 mng_info->global_plte[i].red=i;
4506 mng_info->global_plte[i].green=i;
4507 mng_info->global_plte[i].blue=i;
4511 mng_info->global_plte_length=256;
4514 mng_info->global_plte_length=0;
4516 chunk=(unsigned char *) RelinquishMagickMemory(chunk);
4520 if (memcmp(type,mng_tRNS,4) == 0)
4522 /* read global tRNS */
4525 for (i=0; i < (ssize_t) length; i++)
4526 mng_info->global_trns[i]=p[i];
4529 for ( ; i < 256; i++)
4530 mng_info->global_trns[i]=255;
4532 mng_info->global_trns_length=(unsigned int) length;
4533 chunk=(unsigned char *) RelinquishMagickMemory(chunk);
4536 if (memcmp(type,mng_gAMA,4) == 0)
4543 igamma=mng_get_long(p);
4544 mng_info->global_gamma=((float) igamma)*0.00001;
4545 mng_info->have_global_gama=MagickTrue;
4549 mng_info->have_global_gama=MagickFalse;
4551 chunk=(unsigned char *) RelinquishMagickMemory(chunk);
4555 if (memcmp(type,mng_cHRM,4) == 0)
4557 /* Read global cHRM */
4561 mng_info->global_chrm.white_point.x=0.00001*mng_get_long(p);
4562 mng_info->global_chrm.white_point.y=0.00001*mng_get_long(&p[4]);
4563 mng_info->global_chrm.red_primary.x=0.00001*mng_get_long(&p[8]);
4564 mng_info->global_chrm.red_primary.y=0.00001*
4565 mng_get_long(&p[12]);
4566 mng_info->global_chrm.green_primary.x=0.00001*
4567 mng_get_long(&p[16]);
4568 mng_info->global_chrm.green_primary.y=0.00001*
4569 mng_get_long(&p[20]);
4570 mng_info->global_chrm.blue_primary.x=0.00001*
4571 mng_get_long(&p[24]);
4572 mng_info->global_chrm.blue_primary.y=0.00001*
4573 mng_get_long(&p[28]);
4574 mng_info->have_global_chrm=MagickTrue;
4577 mng_info->have_global_chrm=MagickFalse;
4579 chunk=(unsigned char *) RelinquishMagickMemory(chunk);
4583 if (memcmp(type,mng_sRGB,4) == 0)
4590 mng_info->global_srgb_intent=
4591 Magick_RenderingIntent_from_PNG_RenderingIntent(p[0]);
4592 mng_info->have_global_srgb=MagickTrue;
4595 mng_info->have_global_srgb=MagickFalse;
4597 chunk=(unsigned char *) RelinquishMagickMemory(chunk);
4601 if (memcmp(type,mng_iCCP,4) == 0)
4609 chunk=(unsigned char *) RelinquishMagickMemory(chunk);
4614 if (memcmp(type,mng_FRAM,4) == 0)
4617 (void) ThrowMagickException(&image->exception,GetMagickModule(),
4618 CoderError,"FRAM chunk found in MNG-VLC datastream","`%s'",
4621 if ((mng_info->framing_mode == 2) || (mng_info->framing_mode == 4))
4622 image->delay=frame_delay;
4624 frame_delay=default_frame_delay;
4625 frame_timeout=default_frame_timeout;
4630 mng_info->framing_mode=p[0];
4632 if (logging != MagickFalse)
4633 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
4634 " Framing_mode=%d",mng_info->framing_mode);
4638 /* Note the delay and frame clipping boundaries. */
4640 p++; /* framing mode */
4642 while (*p && ((p-chunk) < (ssize_t) length))
4643 p++; /* frame name */
4645 p++; /* frame name terminator */
4647 if ((p-chunk) < (ssize_t) (length-4))
4654 change_delay=(*p++);
4655 change_timeout=(*p++);
4656 change_clipping=(*p++);
4657 p++; /* change_sync */
4661 frame_delay=1UL*image->ticks_per_second*
4664 if (mng_info->ticks_per_second != 0)
4665 frame_delay/=mng_info->ticks_per_second;
4668 frame_delay=PNG_UINT_31_MAX;
4670 if (change_delay == 2)
4671 default_frame_delay=frame_delay;
4675 if (logging != MagickFalse)
4676 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
4677 " Framing_delay=%.20g",(double) frame_delay);
4682 frame_timeout=1UL*image->ticks_per_second*
4685 if (mng_info->ticks_per_second != 0)
4686 frame_timeout/=mng_info->ticks_per_second;
4689 frame_timeout=PNG_UINT_31_MAX;
4691 if (change_delay == 2)
4692 default_frame_timeout=frame_timeout;
4696 if (logging != MagickFalse)
4697 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
4698 " Framing_timeout=%.20g",(double) frame_timeout);
4701 if (change_clipping)
4703 fb=mng_read_box(previous_fb,(char) p[0],&p[1]);
4707 if (logging != MagickFalse)
4708 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
4709 " Frame_clip: L=%.20g R=%.20g T=%.20g B=%.20g",
4710 (double) fb.left,(double) fb.right,(double) fb.top,
4711 (double) fb.bottom);
4713 if (change_clipping == 2)
4719 mng_info->clip=mng_minimum_box(fb,mng_info->frame);
4721 subframe_width=(size_t) (mng_info->clip.right
4722 -mng_info->clip.left);
4724 subframe_height=(size_t) (mng_info->clip.bottom
4725 -mng_info->clip.top);
4727 Insert a background layer behind the frame if framing_mode is 4.
4729 #if defined(MNG_INSERT_LAYERS)
4730 if (logging != MagickFalse)
4731 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
4732 " subframe_width=%.20g, subframe_height=%.20g",(double)
4733 subframe_width,(double) subframe_height);
4735 if (insert_layers && (mng_info->framing_mode == 4) &&
4736 (subframe_width) && (subframe_height))
4738 /* Allocate next image structure. */
4739 if (GetAuthenticPixelQueue(image) != (PixelPacket *) NULL)
4741 AcquireNextImage(image_info,image);
4743 if (GetNextImageInList(image) == (Image *) NULL)
4745 image=DestroyImageList(image);
4746 MngInfoFreeStruct(mng_info,&have_mng_structure);
4747 return((Image *) NULL);
4750 image=SyncNextImageInList(image);
4753 mng_info->image=image;
4755 if (term_chunk_found)
4757 image->start_loop=MagickTrue;
4758 image->iterations=mng_iterations;
4759 term_chunk_found=MagickFalse;
4763 image->start_loop=MagickFalse;
4765 image->columns=subframe_width;
4766 image->rows=subframe_height;
4767 image->page.width=subframe_width;
4768 image->page.height=subframe_height;
4769 image->page.x=mng_info->clip.left;
4770 image->page.y=mng_info->clip.top;
4771 image->background_color=mng_background_color;
4772 image->matte=MagickFalse;
4774 (void) SetImageBackgroundColor(image);
4776 if (logging != MagickFalse)
4777 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
4778 " Insert backgd layer, L=%.20g, R=%.20g T=%.20g, B=%.20g",
4779 (double) mng_info->clip.left,(double) mng_info->clip.right,
4780 (double) mng_info->clip.top,(double) mng_info->clip.bottom);
4783 chunk=(unsigned char *) RelinquishMagickMemory(chunk);
4786 if (memcmp(type,mng_CLIP,4) == 0)
4795 first_object=(p[0] << 8) | p[1];
4796 last_object=(p[2] << 8) | p[3];
4798 for (i=(int) first_object; i <= (int) last_object; i++)
4800 if (mng_info->exists[i] && !mng_info->frozen[i])
4805 box=mng_info->object_clip[i];
4806 mng_info->object_clip[i]=mng_read_box(box,(char) p[4],&p[5]);
4810 chunk=(unsigned char *) RelinquishMagickMemory(chunk);
4813 if (memcmp(type,mng_SAVE,4) == 0)
4815 for (i=1; i < MNG_MAX_OBJECTS; i++)
4816 if (mng_info->exists[i])
4818 mng_info->frozen[i]=MagickTrue;
4819 #ifdef MNG_OBJECT_BUFFERS
4820 if (mng_info->ob[i] != (MngBuffer *) NULL)
4821 mng_info->ob[i]->frozen=MagickTrue;
4826 chunk=(unsigned char *) RelinquishMagickMemory(chunk);
4831 if ((memcmp(type,mng_DISC,4) == 0) || (memcmp(type,mng_SEEK,4) == 0))
4833 /* Read DISC or SEEK. */
4835 if ((length == 0) || !memcmp(type,mng_SEEK,4))
4837 for (i=1; i < MNG_MAX_OBJECTS; i++)
4838 MngInfoDiscardObject(mng_info,i);
4846 for (j=0; j < (ssize_t) length; j+=2)
4848 i=p[j] << 8 | p[j+1];
4849 MngInfoDiscardObject(mng_info,i);
4854 chunk=(unsigned char *) RelinquishMagickMemory(chunk);
4859 if (memcmp(type,mng_MOVE,4) == 0)
4867 first_object=(p[0] << 8) | p[1];
4868 last_object=(p[2] << 8) | p[3];
4869 for (i=(ssize_t) first_object; i <= (ssize_t) last_object; i++)
4871 if (mng_info->exists[i] && !mng_info->frozen[i])
4879 old_pair.a=mng_info->x_off[i];
4880 old_pair.b=mng_info->y_off[i];
4881 new_pair=mng_read_pair(old_pair,(int) p[4],&p[5]);
4882 mng_info->x_off[i]=new_pair.a;
4883 mng_info->y_off[i]=new_pair.b;
4887 chunk=(unsigned char *) RelinquishMagickMemory(chunk);
4891 if (memcmp(type,mng_LOOP,4) == 0)
4893 ssize_t loop_iters=1;
4894 loop_level=chunk[0];
4895 mng_info->loop_active[loop_level]=1; /* mark loop active */
4897 /* Record starting point. */
4898 loop_iters=mng_get_long(&chunk[1]);
4900 if (logging != MagickFalse)
4901 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
4902 " LOOP level %.20g has %.20g iterations ",(double) loop_level,
4903 (double) loop_iters);
4905 if (loop_iters == 0)
4906 skipping_loop=loop_level;
4910 mng_info->loop_jump[loop_level]=TellBlob(image);
4911 mng_info->loop_count[loop_level]=loop_iters;
4914 mng_info->loop_iteration[loop_level]=0;
4915 chunk=(unsigned char *) RelinquishMagickMemory(chunk);
4919 if (memcmp(type,mng_ENDL,4) == 0)
4921 loop_level=chunk[0];
4923 if (skipping_loop > 0)
4925 if (skipping_loop == loop_level)
4928 Found end of zero-iteration loop.
4931 mng_info->loop_active[loop_level]=0;
4937 if (mng_info->loop_active[loop_level] == 1)
4939 mng_info->loop_count[loop_level]--;
4940 mng_info->loop_iteration[loop_level]++;
4942 if (logging != MagickFalse)
4943 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
4944 " ENDL: LOOP level %.20g has %.20g remaining iters ",
4945 (double) loop_level,(double)
4946 mng_info->loop_count[loop_level]);
4948 if (mng_info->loop_count[loop_level] != 0)
4950 offset=SeekBlob(image,mng_info->loop_jump[loop_level],
4954 ThrowReaderException(CorruptImageError,
4955 "ImproperImageHeader");
4966 mng_info->loop_active[loop_level]=0;
4968 for (i=0; i < loop_level; i++)
4969 if (mng_info->loop_active[i] == 1)
4970 last_level=(short) i;
4971 loop_level=last_level;
4976 chunk=(unsigned char *) RelinquishMagickMemory(chunk);
4980 if (memcmp(type,mng_CLON,4) == 0)
4982 if (mng_info->clon_warning == 0)
4983 (void) ThrowMagickException(&image->exception,GetMagickModule(),
4984 CoderError,"CLON is not implemented yet","`%s'",
4987 mng_info->clon_warning++;
4990 if (memcmp(type,mng_MAGN,4) == 0)
5005 magn_first=(p[0] << 8) | p[1];
5011 magn_last=(p[2] << 8) | p[3];
5014 magn_last=magn_first;
5015 #ifndef MNG_OBJECT_BUFFERS
5016 if (magn_first || magn_last)
5017 if (mng_info->magn_warning == 0)
5019 (void) ThrowMagickException(&image->exception,
5020 GetMagickModule(),CoderError,
5021 "MAGN is not implemented yet for nonzero objects",
5022 "`%s'",image->filename);
5024 mng_info->magn_warning++;
5034 magn_mx=(p[5] << 8) | p[6];
5043 magn_my=(p[7] << 8) | p[8];
5052 magn_ml=(p[9] << 8) | p[10];
5061 magn_mr=(p[11] << 8) | p[12];
5070 magn_mt=(p[13] << 8) | p[14];
5079 magn_mb=(p[15] << 8) | p[16];
5091 magn_methy=magn_methx;
5094 if (magn_methx > 5 || magn_methy > 5)
5095 if (mng_info->magn_warning == 0)
5097 (void) ThrowMagickException(&image->exception,
5098 GetMagickModule(),CoderError,
5099 "Unknown MAGN method in MNG datastream","`%s'",
5102 mng_info->magn_warning++;
5104 #ifdef MNG_OBJECT_BUFFERS
5105 /* Magnify existing objects in the range magn_first to magn_last */
5107 if (magn_first == 0 || magn_last == 0)
5109 /* Save the magnification factors for object 0 */
5110 mng_info->magn_mb=magn_mb;
5111 mng_info->magn_ml=magn_ml;
5112 mng_info->magn_mr=magn_mr;
5113 mng_info->magn_mt=magn_mt;
5114 mng_info->magn_mx=magn_mx;
5115 mng_info->magn_my=magn_my;
5116 mng_info->magn_methx=magn_methx;
5117 mng_info->magn_methy=magn_methy;
5121 if (memcmp(type,mng_PAST,4) == 0)
5123 if (mng_info->past_warning == 0)
5124 (void) ThrowMagickException(&image->exception,GetMagickModule(),
5125 CoderError,"PAST is not implemented yet","`%s'",
5128 mng_info->past_warning++;
5131 if (memcmp(type,mng_SHOW,4) == 0)
5133 if (mng_info->show_warning == 0)
5134 (void) ThrowMagickException(&image->exception,GetMagickModule(),
5135 CoderError,"SHOW is not implemented yet","`%s'",
5138 mng_info->show_warning++;
5141 if (memcmp(type,mng_sBIT,4) == 0)
5144 mng_info->have_global_sbit=MagickFalse;
5148 mng_info->global_sbit.gray=p[0];
5149 mng_info->global_sbit.red=p[0];
5150 mng_info->global_sbit.green=p[1];
5151 mng_info->global_sbit.blue=p[2];
5152 mng_info->global_sbit.alpha=p[3];
5153 mng_info->have_global_sbit=MagickTrue;
5156 if (memcmp(type,mng_pHYs,4) == 0)
5160 mng_info->global_x_pixels_per_unit=
5161 (size_t) mng_get_long(p);
5162 mng_info->global_y_pixels_per_unit=
5163 (size_t) mng_get_long(&p[4]);
5164 mng_info->global_phys_unit_type=p[8];
5165 mng_info->have_global_phys=MagickTrue;
5169 mng_info->have_global_phys=MagickFalse;
5171 if (memcmp(type,mng_pHYg,4) == 0)
5173 if (mng_info->phyg_warning == 0)
5174 (void) ThrowMagickException(&image->exception,GetMagickModule(),
5175 CoderError,"pHYg is not implemented.","`%s'",image->filename);
5177 mng_info->phyg_warning++;
5179 if (memcmp(type,mng_BASI,4) == 0)
5181 skip_to_iend=MagickTrue;
5183 if (mng_info->basi_warning == 0)
5184 (void) ThrowMagickException(&image->exception,GetMagickModule(),
5185 CoderError,"BASI is not implemented yet","`%s'",
5188 mng_info->basi_warning++;
5189 #ifdef MNG_BASI_SUPPORTED
5190 basi_width=(size_t) ((p[0] << 24) | (p[1] << 16) |
5191 (p[2] << 8) | p[3]);
5192 basi_height=(size_t) ((p[4] << 24) | (p[5] << 16) |
5193 (p[6] << 8) | p[7]);
5194 basi_color_type=p[8];
5195 basi_compression_method=p[9];
5196 basi_filter_type=p[10];
5197 basi_interlace_method=p[11];
5199 basi_red=(p[12] << 8) & p[13];
5205 basi_green=(p[14] << 8) & p[15];
5211 basi_blue=(p[16] << 8) & p[17];
5217 basi_alpha=(p[18] << 8) & p[19];
5221 if (basi_sample_depth == 16)
5228 basi_viewable=p[20];
5234 chunk=(unsigned char *) RelinquishMagickMemory(chunk);
5238 if (memcmp(type,mng_IHDR,4)
5239 #if defined(JNG_SUPPORTED)
5240 && memcmp(type,mng_JHDR,4)
5244 /* Not an IHDR or JHDR chunk */
5246 chunk=(unsigned char *) RelinquishMagickMemory(chunk);
5251 if (logging != MagickFalse)
5252 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
5253 " Processing %c%c%c%c chunk",type[0],type[1],type[2],type[3]);
5255 mng_info->exists[object_id]=MagickTrue;
5256 mng_info->viewable[object_id]=MagickTrue;
5258 if (mng_info->invisible[object_id])
5260 if (logging != MagickFalse)
5261 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
5262 " Skipping invisible object");
5264 skip_to_iend=MagickTrue;
5265 chunk=(unsigned char *) RelinquishMagickMemory(chunk);
5268 #if defined(MNG_INSERT_LAYERS)
5270 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
5272 image_width=(size_t) mng_get_long(p);
5273 image_height=(size_t) mng_get_long(&p[4]);
5275 chunk=(unsigned char *) RelinquishMagickMemory(chunk);
5278 Insert a transparent background layer behind the entire animation
5279 if it is not full screen.
5281 #if defined(MNG_INSERT_LAYERS)
5282 if (insert_layers && mng_type && first_mng_object)
5284 if ((mng_info->clip.left > 0) || (mng_info->clip.top > 0) ||
5285 (image_width < mng_info->mng_width) ||
5286 (mng_info->clip.right < (ssize_t) mng_info->mng_width) ||
5287 (image_height < mng_info->mng_height) ||
5288 (mng_info->clip.bottom < (ssize_t) mng_info->mng_height))
5290 if (GetAuthenticPixelQueue(image) != (PixelPacket *) NULL)
5293 Allocate next image structure.
5295 AcquireNextImage(image_info,image);
5297 if (GetNextImageInList(image) == (Image *) NULL)
5299 image=DestroyImageList(image);
5300 MngInfoFreeStruct(mng_info,&have_mng_structure);
5301 return((Image *) NULL);
5304 image=SyncNextImageInList(image);
5306 mng_info->image=image;
5308 if (term_chunk_found)
5310 image->start_loop=MagickTrue;
5311 image->iterations=mng_iterations;
5312 term_chunk_found=MagickFalse;
5316 image->start_loop=MagickFalse;
5318 /* Make a background rectangle. */
5321 image->columns=mng_info->mng_width;
5322 image->rows=mng_info->mng_height;
5323 image->page.width=mng_info->mng_width;
5324 image->page.height=mng_info->mng_height;
5327 image->background_color=mng_background_color;
5328 (void) SetImageBackgroundColor(image);
5329 if (logging != MagickFalse)
5330 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
5331 " Inserted transparent background layer, W=%.20g, H=%.20g",
5332 (double) mng_info->mng_width,(double) mng_info->mng_height);
5336 Insert a background layer behind the upcoming image if
5337 framing_mode is 3, and we haven't already inserted one.
5339 if (insert_layers && (mng_info->framing_mode == 3) &&
5340 (subframe_width) && (subframe_height) && (simplicity == 0 ||
5341 (simplicity & 0x08)))
5343 if (GetAuthenticPixelQueue(image) != (PixelPacket *) NULL)
5346 Allocate next image structure.
5348 AcquireNextImage(image_info,image);
5350 if (GetNextImageInList(image) == (Image *) NULL)
5352 image=DestroyImageList(image);
5353 MngInfoFreeStruct(mng_info,&have_mng_structure);
5354 return((Image *) NULL);
5357 image=SyncNextImageInList(image);
5360 mng_info->image=image;
5362 if (term_chunk_found)
5364 image->start_loop=MagickTrue;
5365 image->iterations=mng_iterations;
5366 term_chunk_found=MagickFalse;
5370 image->start_loop=MagickFalse;
5373 image->columns=subframe_width;
5374 image->rows=subframe_height;
5375 image->page.width=subframe_width;
5376 image->page.height=subframe_height;
5377 image->page.x=mng_info->clip.left;
5378 image->page.y=mng_info->clip.top;
5379 image->background_color=mng_background_color;
5380 image->matte=MagickFalse;
5381 (void) SetImageBackgroundColor(image);
5383 if (logging != MagickFalse)
5384 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
5385 " Insert background layer, L=%.20g, R=%.20g T=%.20g, B=%.20g",
5386 (double) mng_info->clip.left,(double) mng_info->clip.right,
5387 (double) mng_info->clip.top,(double) mng_info->clip.bottom);
5389 #endif /* MNG_INSERT_LAYERS */
5390 first_mng_object=MagickFalse;
5392 if (GetAuthenticPixelQueue(image) != (PixelPacket *) NULL)
5395 Allocate next image structure.
5397 AcquireNextImage(image_info,image);
5399 if (GetNextImageInList(image) == (Image *) NULL)
5401 image=DestroyImageList(image);
5402 MngInfoFreeStruct(mng_info,&have_mng_structure);
5403 return((Image *) NULL);
5406 image=SyncNextImageInList(image);
5408 mng_info->image=image;
5409 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
5410 GetBlobSize(image));
5412 if (status == MagickFalse)
5415 if (term_chunk_found)
5417 image->start_loop=MagickTrue;
5418 term_chunk_found=MagickFalse;
5422 image->start_loop=MagickFalse;
5424 if (mng_info->framing_mode == 1 || mng_info->framing_mode == 3)
5426 image->delay=frame_delay;
5427 frame_delay=default_frame_delay;
5433 image->page.width=mng_info->mng_width;
5434 image->page.height=mng_info->mng_height;
5435 image->page.x=mng_info->x_off[object_id];
5436 image->page.y=mng_info->y_off[object_id];
5437 image->iterations=mng_iterations;
5440 Seek back to the beginning of the IHDR or JHDR chunk's length field.
5443 if (logging != MagickFalse)
5444 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
5445 " Seeking back to beginning of %c%c%c%c chunk",type[0],type[1],
5448 offset=SeekBlob(image,-((ssize_t) length+12),SEEK_CUR);
5451 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
5455 mng_info->image=image;
5456 mng_info->mng_type=mng_type;
5457 mng_info->object_id=object_id;
5459 if (memcmp(type,mng_IHDR,4) == 0)
5460 image=ReadOnePNGImage(mng_info,image_info,exception);
5462 #if defined(JNG_SUPPORTED)
5464 image=ReadOneJNGImage(mng_info,image_info,exception);
5467 if (image == (Image *) NULL)
5469 if (IsImageObject(previous) != MagickFalse)
5471 (void) DestroyImageList(previous);
5472 (void) CloseBlob(previous);
5475 MngInfoFreeStruct(mng_info,&have_mng_structure);
5476 return((Image *) NULL);
5479 if (image->columns == 0 || image->rows == 0)
5481 (void) CloseBlob(image);
5482 image=DestroyImageList(image);
5483 MngInfoFreeStruct(mng_info,&have_mng_structure);
5484 return((Image *) NULL);
5487 mng_info->image=image;
5494 if (mng_info->magn_methx || mng_info->magn_methy)
5500 if (logging != MagickFalse)
5501 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
5502 " Processing MNG MAGN chunk");
5504 if (mng_info->magn_methx == 1)
5506 magnified_width=mng_info->magn_ml;
5508 if (image->columns > 1)
5509 magnified_width += mng_info->magn_mr;
5511 if (image->columns > 2)
5512 magnified_width += (png_uint_32)
5513 ((image->columns-2)*(mng_info->magn_mx));
5518 magnified_width=(png_uint_32) image->columns;
5520 if (image->columns > 1)
5521 magnified_width += mng_info->magn_ml-1;
5523 if (image->columns > 2)
5524 magnified_width += mng_info->magn_mr-1;
5526 if (image->columns > 3)
5527 magnified_width += (png_uint_32)
5528 ((image->columns-3)*(mng_info->magn_mx-1));
5531 if (mng_info->magn_methy == 1)
5533 magnified_height=mng_info->magn_mt;
5535 if (image->rows > 1)
5536 magnified_height += mng_info->magn_mb;
5538 if (image->rows > 2)
5539 magnified_height += (png_uint_32)
5540 ((image->rows-2)*(mng_info->magn_my));
5545 magnified_height=(png_uint_32) image->rows;
5547 if (image->rows > 1)
5548 magnified_height += mng_info->magn_mt-1;
5550 if (image->rows > 2)
5551 magnified_height += mng_info->magn_mb-1;
5553 if (image->rows > 3)
5554 magnified_height += (png_uint_32)
5555 ((image->rows-3)*(mng_info->magn_my-1));
5558 if (magnified_height > image->rows ||
5559 magnified_width > image->columns)
5574 register PixelPacket
5586 /* Allocate next image structure. */
5588 if (logging != MagickFalse)
5589 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
5590 " Allocate magnified image");
5592 AcquireNextImage(image_info,image);
5594 if (GetNextImageInList(image) == (Image *) NULL)
5596 image=DestroyImageList(image);
5597 MngInfoFreeStruct(mng_info,&have_mng_structure);
5598 return((Image *) NULL);
5601 large_image=SyncNextImageInList(image);
5603 large_image->columns=magnified_width;
5604 large_image->rows=magnified_height;
5606 magn_methx=mng_info->magn_methx;
5607 magn_methy=mng_info->magn_methy;
5609 #if (MAGICKCORE_QUANTUM_DEPTH == 32)
5610 #define QM unsigned short
5611 if (magn_methx != 1 || magn_methy != 1)
5614 Scale pixels to unsigned shorts to prevent
5615 overflow of intermediate values of interpolations
5617 for (y=0; y < (ssize_t) image->rows; y++)
5619 q=GetAuthenticPixels(image,0,y,image->columns,1,
5622 for (x=(ssize_t) image->columns-1; x >= 0; x--)
5624 q->red=ScaleQuantumToShort(q->red);
5625 q->green=ScaleQuantumToShort(q->green);
5626 q->blue=ScaleQuantumToShort(q->blue);
5627 q->opacity=ScaleQuantumToShort(q->opacity);
5631 if (SyncAuthenticPixels(image,exception) == MagickFalse)
5639 if (image->matte != MagickFalse)
5640 (void) SetImageBackgroundColor(large_image);
5644 large_image->background_color.opacity=OpaqueOpacity;
5645 (void) SetImageBackgroundColor(large_image);
5647 if (magn_methx == 4)
5650 if (magn_methx == 5)
5653 if (magn_methy == 4)
5656 if (magn_methy == 5)
5660 /* magnify the rows into the right side of the large image */
5662 if (logging != MagickFalse)
5663 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
5664 " Magnify the rows to %.20g",(double) large_image->rows);
5665 m=(ssize_t) mng_info->magn_mt;
5667 length=(size_t) image->columns;
5668 next=(PixelPacket *) AcquireQuantumMemory(length,sizeof(*next));
5669 prev=(PixelPacket *) AcquireQuantumMemory(length,sizeof(*prev));
5671 if ((prev == (PixelPacket *) NULL) ||
5672 (next == (PixelPacket *) NULL))
5674 image=DestroyImageList(image);
5675 MngInfoFreeStruct(mng_info,&have_mng_structure);
5676 ThrowReaderException(ResourceLimitError,
5677 "MemoryAllocationFailed");
5680 n=GetAuthenticPixels(image,0,0,image->columns,1,exception);
5681 (void) CopyMagickMemory(next,n,length);
5683 for (y=0; y < (ssize_t) image->rows; y++)
5686 m=(ssize_t) mng_info->magn_mt;
5688 else if (magn_methy > 1 && y == (ssize_t) image->rows-2)
5689 m=(ssize_t) mng_info->magn_mb;
5691 else if (magn_methy <= 1 && y == (ssize_t) image->rows-1)
5692 m=(ssize_t) mng_info->magn_mb;
5694 else if (magn_methy > 1 && y == (ssize_t) image->rows-1)
5698 m=(ssize_t) mng_info->magn_my;
5704 if (y < (ssize_t) image->rows-1)
5706 n=GetAuthenticPixels(image,0,y+1,image->columns,1,
5708 (void) CopyMagickMemory(next,n,length);
5711 for (i=0; i < m; i++, yy++)
5713 register PixelPacket
5716 assert(yy < (ssize_t) large_image->rows);
5719 q=GetAuthenticPixels(large_image,0,yy,large_image->columns,
5721 q+=(large_image->columns-image->columns);
5723 for (x=(ssize_t) image->columns-1; x >= 0; x--)
5725 /* To do: get color as function of indexes[x] */
5727 if (image->storage_class == PseudoClass)
5732 if (magn_methy <= 1)
5734 *q=(*pixels); /* replicate previous */
5737 else if (magn_methy == 2 || magn_methy == 4)
5745 (*q).red=(QM) (((ssize_t) (2*i*((*n).red
5746 -(*pixels).red)+m))/((ssize_t) (m*2))
5748 (*q).green=(QM) (((ssize_t) (2*i*((*n).green
5749 -(*pixels).green)+m))/((ssize_t) (m*2))
5751 (*q).blue=(QM) (((ssize_t) (2*i*((*n).blue
5752 -(*pixels).blue)+m))/((ssize_t) (m*2))
5755 if (image->matte != MagickFalse)
5756 (*q).opacity=(QM) (((ssize_t)
5758 -(*pixels).opacity)+m))
5759 /((ssize_t) (m*2))+(*pixels).opacity);
5762 if (magn_methy == 4)
5764 /* Replicate nearest */
5765 if (i <= ((m+1) << 1))
5766 (*q).opacity=(*pixels).opacity+0;
5768 (*q).opacity=(*n).opacity+0;
5772 else /* if (magn_methy == 3 || magn_methy == 5) */
5774 /* Replicate nearest */
5775 if (i <= ((m+1) << 1))
5781 if (magn_methy == 5)
5783 (*q).opacity=(QM) (((ssize_t) (2*i*((*n).opacity
5784 -(*pixels).opacity)+m))/((ssize_t) (m*2))
5785 +(*pixels).opacity);
5793 if (SyncAuthenticPixels(large_image,exception) == 0)
5799 prev=(PixelPacket *) RelinquishMagickMemory(prev);
5800 next=(PixelPacket *) RelinquishMagickMemory(next);
5802 length=image->columns;
5804 if (logging != MagickFalse)
5805 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
5806 " Delete original image");
5808 DeleteImageFromList(&image);
5812 mng_info->image=image;
5814 /* magnify the columns */
5815 if (logging != MagickFalse)
5816 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
5817 " Magnify the columns to %.20g",(double) image->columns);
5819 for (y=0; y < (ssize_t) image->rows; y++)
5821 register PixelPacket
5824 q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
5825 pixels=q+(image->columns-length);
5828 for (x=(ssize_t) (image->columns-length);
5829 x < (ssize_t) image->columns; x++)
5831 if (x == (ssize_t) (image->columns-length))
5832 m=(ssize_t) mng_info->magn_ml;
5834 else if (magn_methx > 1 && x == (ssize_t) image->columns-2)
5835 m=(ssize_t) mng_info->magn_mr;
5837 else if (magn_methx <= 1 && x == (ssize_t) image->columns-1)
5838 m=(ssize_t) mng_info->magn_mr;
5840 else if (magn_methx > 1 && x == (ssize_t) image->columns-1)
5844 m=(ssize_t) mng_info->magn_mx;
5846 for (i=0; i < m; i++)
5848 if (magn_methx <= 1)
5850 /* replicate previous */
5854 else if (magn_methx == 2 || magn_methx == 4)
5862 (*q).red=(QM) ((2*i*((*n).red
5864 /((ssize_t) (m*2))+(*pixels).red);
5865 (*q).green=(QM) ((2*i*((*n).green
5867 +m)/((ssize_t) (m*2))+(*pixels).green);
5868 (*q).blue=(QM) ((2*i*((*n).blue
5870 /((ssize_t) (m*2))+(*pixels).blue);
5871 if (image->matte != MagickFalse)
5872 (*q).opacity=(QM) ((2*i*((*n).opacity
5873 -(*pixels).opacity)+m)/((ssize_t) (m*2))
5874 +(*pixels).opacity);
5877 if (magn_methx == 4)
5879 /* Replicate nearest */
5880 if (i <= ((m+1) << 1))
5881 (*q).opacity=(*pixels).opacity+0;
5883 (*q).opacity=(*n).opacity+0;
5887 else /* if (magn_methx == 3 || magn_methx == 5) */
5889 /* Replicate nearest */
5890 if (i <= ((m+1) << 1))
5896 if (magn_methx == 5)
5899 (*q).opacity=(QM) ((2*i*((*n).opacity
5900 -(*pixels).opacity)+m) /((ssize_t) (m*2))
5901 +(*pixels).opacity);
5910 if (SyncAuthenticPixels(image,exception) == MagickFalse)
5913 #if (MAGICKCORE_QUANTUM_DEPTH == 32)
5914 if (magn_methx != 1 || magn_methy != 1)
5917 Rescale pixels to Quantum
5919 for (y=0; y < (ssize_t) image->rows; y++)
5921 q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
5923 for (x=(ssize_t) image->columns-1; x >= 0; x--)
5925 q->red=ScaleShortToQuantum(q->red);
5926 q->green=ScaleShortToQuantum(q->green);
5927 q->blue=ScaleShortToQuantum(q->blue);
5928 q->opacity=ScaleShortToQuantum(q->opacity);
5932 if (SyncAuthenticPixels(image,exception) == MagickFalse)
5937 if (logging != MagickFalse)
5938 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
5939 " Finished MAGN processing");
5944 Crop_box is with respect to the upper left corner of the MNG.
5946 crop_box.left=mng_info->image_box.left+mng_info->x_off[object_id];
5947 crop_box.right=mng_info->image_box.right+mng_info->x_off[object_id];
5948 crop_box.top=mng_info->image_box.top+mng_info->y_off[object_id];
5949 crop_box.bottom=mng_info->image_box.bottom+mng_info->y_off[object_id];
5950 crop_box=mng_minimum_box(crop_box,mng_info->clip);
5951 crop_box=mng_minimum_box(crop_box,mng_info->frame);
5952 crop_box=mng_minimum_box(crop_box,mng_info->object_clip[object_id]);
5953 if ((crop_box.left != (mng_info->image_box.left
5954 +mng_info->x_off[object_id])) ||
5955 (crop_box.right != (mng_info->image_box.right
5956 +mng_info->x_off[object_id])) ||
5957 (crop_box.top != (mng_info->image_box.top
5958 +mng_info->y_off[object_id])) ||
5959 (crop_box.bottom != (mng_info->image_box.bottom
5960 +mng_info->y_off[object_id])))
5962 if (logging != MagickFalse)
5963 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
5964 " Crop the PNG image");
5966 if ((crop_box.left < crop_box.right) &&
5967 (crop_box.top < crop_box.bottom))
5976 Crop_info is with respect to the upper left corner of
5979 crop_info.x=(crop_box.left-mng_info->x_off[object_id]);
5980 crop_info.y=(crop_box.top-mng_info->y_off[object_id]);
5981 crop_info.width=(size_t) (crop_box.right-crop_box.left);
5982 crop_info.height=(size_t) (crop_box.bottom-crop_box.top);
5983 image->page.width=image->columns;
5984 image->page.height=image->rows;
5987 im=CropImage(image,&crop_info,exception);
5989 if (im != (Image *) NULL)
5991 image->columns=im->columns;
5992 image->rows=im->rows;
5993 im=DestroyImage(im);
5994 image->page.width=image->columns;
5995 image->page.height=image->rows;
5996 image->page.x=crop_box.left;
5997 image->page.y=crop_box.top;
6004 No pixels in crop area. The MNG spec still requires
6005 a layer, though, so make a single transparent pixel in
6006 the top left corner.
6011 (void) SetImageBackgroundColor(image);
6012 image->page.width=1;
6013 image->page.height=1;
6018 #ifndef PNG_READ_EMPTY_PLTE_SUPPORTED
6019 image=mng_info->image;
6023 #if (MAGICKCORE_QUANTUM_DEPTH > 16)
6024 /* PNG does not handle depths greater than 16 so reduce it even
6027 if (image->depth > 16)
6031 #if (MAGICKCORE_QUANTUM_DEPTH >= 16)
6032 if (LosslessReduceDepthOK(image) != MagickFalse)
6036 GetImageException(image,exception);
6038 if (image_info->number_scenes != 0)
6040 if (mng_info->scenes_found >
6041 (ssize_t) (image_info->first_scene+image_info->number_scenes))
6045 if (logging != MagickFalse)
6046 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
6047 " Finished reading image datastream.");
6049 } while (LocaleCompare(image_info->magick,"MNG") == 0);
6051 (void) CloseBlob(image);
6053 if (logging != MagickFalse)
6054 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
6055 " Finished reading all image datastreams.");
6057 #if defined(MNG_INSERT_LAYERS)
6058 if (insert_layers && !mng_info->image_found && (mng_info->mng_width) &&
6059 (mng_info->mng_height))
6062 Insert a background layer if nothing else was found.
6064 if (logging != MagickFalse)
6065 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
6066 " No images found. Inserting a background layer.");
6068 if (GetAuthenticPixelQueue(image) != (PixelPacket *) NULL)
6071 Allocate next image structure.
6073 AcquireNextImage(image_info,image);
6074 if (GetNextImageInList(image) == (Image *) NULL)
6076 image=DestroyImageList(image);
6077 MngInfoFreeStruct(mng_info,&have_mng_structure);
6079 if (logging != MagickFalse)
6080 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
6081 " Allocation failed, returning NULL.");
6083 return((Image *) NULL);
6085 image=SyncNextImageInList(image);
6087 image->columns=mng_info->mng_width;
6088 image->rows=mng_info->mng_height;
6089 image->page.width=mng_info->mng_width;
6090 image->page.height=mng_info->mng_height;
6093 image->background_color=mng_background_color;
6094 image->matte=MagickFalse;
6096 if (image_info->ping == MagickFalse)
6097 (void) SetImageBackgroundColor(image);
6099 mng_info->image_found++;
6102 image->iterations=mng_iterations;
6104 if (mng_iterations == 1)
6105 image->start_loop=MagickTrue;
6107 while (GetPreviousImageInList(image) != (Image *) NULL)
6110 if (image_count > 10*mng_info->image_found)
6112 if (logging != MagickFalse)
6113 (void) LogMagickEvent(CoderEvent,GetMagickModule()," No beginning");
6115 (void) ThrowMagickException(&image->exception,GetMagickModule(),
6116 CoderError,"Linked list is corrupted, beginning of list not found",
6117 "`%s'",image_info->filename);
6119 return((Image *) NULL);
6122 image=GetPreviousImageInList(image);
6124 if (GetNextImageInList(image) == (Image *) NULL)
6126 if (logging != MagickFalse)
6127 (void) LogMagickEvent(CoderEvent,GetMagickModule()," Corrupt list");
6129 (void) ThrowMagickException(&image->exception,GetMagickModule(),
6130 CoderError,"Linked list is corrupted; next_image is NULL","`%s'",
6131 image_info->filename);
6135 if (mng_info->ticks_per_second && mng_info->image_found > 1 &&
6136 GetNextImageInList(image) ==
6139 if (logging != MagickFalse)
6140 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
6141 " First image null");
6143 (void) ThrowMagickException(&image->exception,GetMagickModule(),
6144 CoderError,"image->next for first image is NULL but shouldn't be.",
6145 "`%s'",image_info->filename);
6148 if (mng_info->image_found == 0)
6150 if (logging != MagickFalse)
6151 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
6152 " No visible images found.");
6154 (void) ThrowMagickException(&image->exception,GetMagickModule(),
6155 CoderError,"No visible images in file","`%s'",image_info->filename);
6157 if (image != (Image *) NULL)
6158 image=DestroyImageList(image);
6160 MngInfoFreeStruct(mng_info,&have_mng_structure);
6161 return((Image *) NULL);
6164 if (mng_info->ticks_per_second)
6165 final_delay=1UL*MagickMax(image->ticks_per_second,1L)*
6166 final_delay/mng_info->ticks_per_second;
6169 image->start_loop=MagickTrue;
6171 /* Find final nonzero image delay */
6172 final_image_delay=0;
6174 while (GetNextImageInList(image) != (Image *) NULL)
6177 final_image_delay=image->delay;
6179 image=GetNextImageInList(image);
6182 if (final_delay < final_image_delay)
6183 final_delay=final_image_delay;
6185 image->delay=final_delay;
6187 if (logging != MagickFalse)
6188 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
6189 " image->delay=%.20g, final_delay=%.20g",(double) image->delay,
6190 (double) final_delay);
6192 if (logging != MagickFalse)
6198 image=GetFirstImageInList(image);
6200 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
6201 " Before coalesce:");
6203 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
6204 " scene 0 delay=%.20g",(double) image->delay);
6206 while (GetNextImageInList(image) != (Image *) NULL)
6208 image=GetNextImageInList(image);
6209 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
6210 " scene %.20g delay=%.20g",(double) scene++,(double) image->delay);
6214 image=GetFirstImageInList(image);
6215 #ifdef MNG_COALESCE_LAYERS
6225 if (logging != MagickFalse)
6226 (void) LogMagickEvent(CoderEvent,GetMagickModule()," Coalesce Images");
6229 next_image=CoalesceImages(image,&image->exception);
6231 if (next_image == (Image *) NULL)
6232 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
6234 image=DestroyImageList(image);
6237 for (next=image; next != (Image *) NULL; next=next_image)
6239 next->page.width=mng_info->mng_width;
6240 next->page.height=mng_info->mng_height;
6243 next->scene=scene++;
6244 next_image=GetNextImageInList(next);
6246 if (next_image == (Image *) NULL)
6249 if (next->delay == 0)
6252 next_image->previous=GetPreviousImageInList(next);
6253 if (GetPreviousImageInList(next) == (Image *) NULL)
6256 next->previous->next=next_image;
6257 next=DestroyImage(next);
6263 while (GetNextImageInList(image) != (Image *) NULL)
6264 image=GetNextImageInList(image);
6266 image->dispose=BackgroundDispose;
6268 if (logging != MagickFalse)
6274 image=GetFirstImageInList(image);
6276 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
6277 " After coalesce:");
6279 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
6280 " scene 0 delay=%.20g dispose=%.20g",(double) image->delay,
6281 (double) image->dispose);
6283 while (GetNextImageInList(image) != (Image *) NULL)
6285 image=GetNextImageInList(image);
6287 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
6288 " scene %.20g delay=%.20g dispose=%.20g",(double) scene++,
6289 (double) image->delay,(double) image->dispose);
6293 image=GetFirstImageInList(image);
6294 MngInfoFreeStruct(mng_info,&have_mng_structure);
6295 have_mng_structure=MagickFalse;
6297 if (logging != MagickFalse)
6298 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"exit ReadMNGImage()");
6300 return(GetFirstImageInList(image));
6302 #else /* PNG_LIBPNG_VER > 10011 */
6303 static Image *ReadPNGImage(const ImageInfo *image_info,ExceptionInfo *exception)
6305 printf("Your PNG library is too old: You have libpng-%s\n",
6306 PNG_LIBPNG_VER_STRING);
6308 (void) ThrowMagickException(exception,GetMagickModule(),CoderError,
6309 "PNG library is too old","`%s'",image_info->filename);
6311 return(Image *) NULL;
6314 static Image *ReadMNGImage(const ImageInfo *image_info,ExceptionInfo *exception)
6316 return(ReadPNGImage(image_info,exception));
6318 #endif /* PNG_LIBPNG_VER > 10011 */
6322 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6326 % R e g i s t e r P N G I m a g e %
6330 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6332 % RegisterPNGImage() adds properties for the PNG image format to
6333 % the list of supported formats. The properties include the image format
6334 % tag, a method to read and/or write the format, whether the format
6335 % supports the saving of more than one frame to the same file or blob,
6336 % whether the format supports native in-memory I/O, and a brief
6337 % description of the format.
6339 % The format of the RegisterPNGImage method is:
6341 % size_t RegisterPNGImage(void)
6344 ModuleExport size_t RegisterPNGImage(void)
6347 version[MaxTextExtent];
6355 "See http://www.libpng.org/ for details about the PNG format."
6360 "See http://www.libpng.org/pub/mng/ for details about the JNG\n"
6366 "See http://www.libpng.org/pub/mng/ for details about the MNG\n"
6372 #if defined(PNG_LIBPNG_VER_STRING)
6373 (void) ConcatenateMagickString(version,"libpng ",MaxTextExtent);
6374 (void) ConcatenateMagickString(version,PNG_LIBPNG_VER_STRING,MaxTextExtent);
6376 if (LocaleCompare(PNG_LIBPNG_VER_STRING,png_get_header_ver(NULL)) != 0)
6378 (void) ConcatenateMagickString(version,",",MaxTextExtent);
6379 (void) ConcatenateMagickString(version,png_get_libpng_ver(NULL),
6384 entry=SetMagickInfo("MNG");
6385 entry->seekable_stream=MagickTrue; /* To do: eliminate this. */
6387 #if defined(MAGICKCORE_PNG_DELEGATE)
6388 entry->decoder=(DecodeImageHandler *) ReadMNGImage;
6389 entry->encoder=(EncodeImageHandler *) WriteMNGImage;
6392 entry->magick=(IsImageFormatHandler *) IsMNG;
6393 entry->description=ConstantString("Multiple-image Network Graphics");
6395 if (*version != '\0')
6396 entry->version=ConstantString(version);
6398 entry->module=ConstantString("PNG");
6399 entry->note=ConstantString(MNGNote);
6400 (void) RegisterMagickInfo(entry);
6402 entry=SetMagickInfo("PNG");
6404 #if defined(MAGICKCORE_PNG_DELEGATE)
6405 entry->decoder=(DecodeImageHandler *) ReadPNGImage;
6406 entry->encoder=(EncodeImageHandler *) WritePNGImage;
6409 entry->magick=(IsImageFormatHandler *) IsPNG;
6410 entry->adjoin=MagickFalse;
6411 entry->description=ConstantString("Portable Network Graphics");
6412 entry->module=ConstantString("PNG");
6414 if (*version != '\0')
6415 entry->version=ConstantString(version);
6417 entry->note=ConstantString(PNGNote);
6418 (void) RegisterMagickInfo(entry);
6420 entry=SetMagickInfo("PNG8");
6422 #if defined(MAGICKCORE_PNG_DELEGATE)
6423 entry->decoder=(DecodeImageHandler *) ReadPNGImage;
6424 entry->encoder=(EncodeImageHandler *) WritePNGImage;
6427 entry->magick=(IsImageFormatHandler *) IsPNG;
6428 entry->adjoin=MagickFalse;
6429 entry->description=ConstantString(
6430 "8-bit indexed with optional binary transparency");
6431 entry->module=ConstantString("PNG");
6432 (void) RegisterMagickInfo(entry);
6434 entry=SetMagickInfo("PNG24");
6437 #if defined(ZLIB_VERSION)
6438 (void) ConcatenateMagickString(version,"zlib ",MaxTextExtent);
6439 (void) ConcatenateMagickString(version,ZLIB_VERSION,MaxTextExtent);
6441 if (LocaleCompare(ZLIB_VERSION,zlib_version) != 0)
6443 (void) ConcatenateMagickString(version,",",MaxTextExtent);
6444 (void) ConcatenateMagickString(version,zlib_version,MaxTextExtent);
6448 if (*version != '\0')
6449 entry->version=ConstantString(version);
6451 #if defined(MAGICKCORE_PNG_DELEGATE)
6452 entry->decoder=(DecodeImageHandler *) ReadPNGImage;
6453 entry->encoder=(EncodeImageHandler *) WritePNGImage;
6456 entry->magick=(IsImageFormatHandler *) IsPNG;
6457 entry->adjoin=MagickFalse;
6458 entry->description=ConstantString("opaque 24-bit RGB");
6459 entry->module=ConstantString("PNG");
6460 (void) RegisterMagickInfo(entry);
6462 entry=SetMagickInfo("PNG32");
6464 #if defined(MAGICKCORE_PNG_DELEGATE)
6465 entry->decoder=(DecodeImageHandler *) ReadPNGImage;
6466 entry->encoder=(EncodeImageHandler *) WritePNGImage;
6469 entry->magick=(IsImageFormatHandler *) IsPNG;
6470 entry->adjoin=MagickFalse;
6471 entry->description=ConstantString("opaque or transparent 32-bit RGBA");
6472 entry->module=ConstantString("PNG");
6473 (void) RegisterMagickInfo(entry);
6475 entry=SetMagickInfo("JNG");
6477 #if defined(JNG_SUPPORTED)
6478 #if defined(MAGICKCORE_PNG_DELEGATE)
6479 entry->decoder=(DecodeImageHandler *) ReadJNGImage;
6480 entry->encoder=(EncodeImageHandler *) WriteJNGImage;
6484 entry->magick=(IsImageFormatHandler *) IsJNG;
6485 entry->adjoin=MagickFalse;
6486 entry->description=ConstantString("JPEG Network Graphics");
6487 entry->module=ConstantString("PNG");
6488 entry->note=ConstantString(JNGNote);
6489 (void) RegisterMagickInfo(entry);
6491 #if defined(PNG_SETJMP_NOT_THREAD_SAFE)
6492 ping_semaphore=AllocateSemaphoreInfo();
6495 return(MagickImageCoderSignature);
6499 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6503 % U n r e g i s t e r P N G I m a g e %
6507 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6509 % UnregisterPNGImage() removes format registrations made by the
6510 % PNG module from the list of supported formats.
6512 % The format of the UnregisterPNGImage method is:
6514 % UnregisterPNGImage(void)
6517 ModuleExport void UnregisterPNGImage(void)
6519 (void) UnregisterMagickInfo("MNG");
6520 (void) UnregisterMagickInfo("PNG");
6521 (void) UnregisterMagickInfo("PNG8");
6522 (void) UnregisterMagickInfo("PNG24");
6523 (void) UnregisterMagickInfo("PNG32");
6524 (void) UnregisterMagickInfo("JNG");
6526 #if defined(PNG_SETJMP_NOT_THREAD_SAFE)
6527 if (ping_semaphore != (SemaphoreInfo *) NULL)
6528 DestroySemaphoreInfo(&ping_semaphore);
6532 #if defined(MAGICKCORE_PNG_DELEGATE)
6533 #if PNG_LIBPNG_VER > 10011
6535 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6539 % W r i t e M N G I m a g e %
6543 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6545 % WriteMNGImage() writes an image in the Portable Network Graphics
6546 % Group's "Multiple-image Network Graphics" encoded image format.
6548 % MNG support written by Glenn Randers-Pehrson, glennrp@image...
6550 % The format of the WriteMNGImage method is:
6552 % MagickBooleanType WriteMNGImage(const ImageInfo *image_info,Image *image)
6554 % A description of each parameter follows.
6556 % o image_info: the image info.
6558 % o image: The image.
6561 % To do (as of version 5.5.2, November 26, 2002 -- glennrp -- see also
6562 % "To do" under ReadPNGImage):
6564 % Preserve all unknown and not-yet-handled known chunks found in input
6565 % PNG file and copy them into output PNG files according to the PNG
6568 % Write the iCCP chunk at MNG level when (icc profile length > 0)
6570 % Improve selection of color type (use indexed-colour or indexed-colour
6571 % with tRNS when 256 or fewer unique RGBA values are present).
6573 % Figure out what to do with "dispose=<restore-to-previous>" (dispose == 3)
6574 % This will be complicated if we limit ourselves to generating MNG-LC
6575 % files. For now we ignore disposal method 3 and simply overlay the next
6578 % Check for identical PLTE's or PLTE/tRNS combinations and use a
6579 % global MNG PLTE or PLTE/tRNS combination when appropriate.
6580 % [mostly done 15 June 1999 but still need to take care of tRNS]
6582 % Check for identical sRGB and replace with a global sRGB (and remove
6583 % gAMA/cHRM if sRGB is found; check for identical gAMA/cHRM and
6584 % replace with global gAMA/cHRM (or with sRGB if appropriate; replace
6585 % local gAMA/cHRM with local sRGB if appropriate).
6587 % Check for identical sBIT chunks and write global ones.
6589 % Provide option to skip writing the signature tEXt chunks.
6591 % Use signatures to detect identical objects and reuse the first
6592 % instance of such objects instead of writing duplicate objects.
6594 % Use a smaller-than-32k value of compression window size when
6597 % Encode JNG datastreams. Mostly done as of 5.5.2; need to write
6598 % ancillary text chunks and save profiles.
6600 % Provide an option to force LC files (to ensure exact framing rate)
6603 % Provide an option to force VLC files instead of LC, even when offsets
6604 % are present. This will involve expanding the embedded images with a
6605 % transparent region at the top and/or left.
6609 Magick_png_write_raw_profile(const ImageInfo *image_info,png_struct *ping,
6610 png_info *ping_info, unsigned char *profile_type, unsigned char
6611 *profile_description, unsigned char *profile_data, png_uint_32 length)
6630 hex[16]={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
6632 if (LocaleNCompare((char *) profile_type+1, "ng-chunk-",9) == 0)
6635 if (image_info->verbose)
6637 (void) printf("writing raw profile: type=%s, length=%.20g\n",
6638 (char *) profile_type, (double) length);
6641 text=(png_textp) png_malloc(ping,(png_uint_32) sizeof(png_text));
6642 description_length=(png_uint_32) strlen((const char *) profile_description);
6643 allocated_length=(png_uint_32) (length*2 + (length >> 5) + 20
6644 + description_length);
6645 text[0].text=(png_charp) png_malloc(ping,allocated_length);
6646 text[0].key=(png_charp) png_malloc(ping, (png_uint_32) 80);
6647 text[0].key[0]='\0';
6648 (void) ConcatenateMagickString(text[0].key,
6649 "Raw profile type ",MaxTextExtent);
6650 (void) ConcatenateMagickString(text[0].key,(const char *) profile_type,62);
6654 (void) CopyMagickString(dp,(const char *) profile_description,
6656 dp+=description_length;
6658 (void) FormatMagickString(dp,allocated_length-
6659 (png_size_t) (dp-text[0].text),"%8lu ",(unsigned long) length);
6662 for (i=0; i < (ssize_t) length; i++)
6666 *(dp++)=(char) hex[((*sp >> 4) & 0x0f)];
6667 *(dp++)=(char) hex[((*sp++ ) & 0x0f)];
6672 text[0].text_length=(png_size_t) (dp-text[0].text);
6673 text[0].compression=image_info->compression == NoCompression ||
6674 (image_info->compression == UndefinedCompression &&
6675 text[0].text_length < 128) ? -1 : 0;
6677 if (text[0].text_length <= allocated_length)
6678 png_set_text(ping,ping_info,text,1);
6680 png_free(ping,text[0].text);
6681 png_free(ping,text[0].key);
6682 png_free(ping,text);
6685 static MagickBooleanType Magick_png_write_chunk_from_profile(Image *image,
6686 const char *string, MagickBooleanType logging)
6699 ResetImageProfileIterator(image);
6701 for (name=GetNextImageProfile(image); name != (const char *) NULL; )
6703 profile=GetImageProfile(image,name);
6705 if (profile != (const StringInfo *) NULL)
6710 if (LocaleNCompare(name,string,11) == 0)
6712 if (logging != MagickFalse)
6713 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
6714 " Found %s profile",name);
6716 ping_profile=CloneStringInfo(profile);
6717 data=GetStringInfoDatum(ping_profile),
6718 length=(png_uint_32) GetStringInfoLength(ping_profile);
6723 (void) WriteBlobMSBULong(image,length-5); /* data length */
6724 (void) WriteBlob(image,length-1,data+1);
6725 (void) WriteBlobMSBULong(image,crc32(0,data+1,(uInt) length-1));
6726 ping_profile=DestroyStringInfo(ping_profile);
6730 name=GetNextImageProfile(image);
6737 /* Write one PNG image */
6738 static MagickBooleanType WriteOnePNGImage(MngInfo *mng_info,
6739 const ImageInfo *IMimage_info,Image *IMimage)
6763 ping_trans_alpha[256];
6791 ping_have_cheap_transparency,
6801 /* ping_exclude_EXIF, */
6804 /* ping_exclude_iTXt, */
6809 /* ping_exclude_tRNS, */
6811 ping_exclude_zCCP, /* hex-encoded iCCP */
6814 ping_need_colortype_warning,
6833 ping_interlace_method,
6834 ping_compression_method,
6851 number_semitransparent,
6853 ping_pHYs_unit_type;
6856 ping_pHYs_x_resolution,
6857 ping_pHYs_y_resolution;
6859 logging=LogMagickEvent(CoderEvent,GetMagickModule(),
6860 " Enter WriteOnePNGImage()");
6862 image = CloneImage(IMimage,0,0,MagickFalse,&IMimage->exception);
6863 image_info=(ImageInfo *) CloneImageInfo(IMimage_info);
6865 #if defined(PNG_SETJMP_NOT_THREAD_SAFE)
6866 LockSemaphoreInfo(ping_semaphore);
6869 /* Initialize some stuff */
6872 ping_interlace_method=0,
6873 ping_compression_method=0,
6874 ping_filter_method=0,
6877 ping_background.red = 0;
6878 ping_background.green = 0;
6879 ping_background.blue = 0;
6880 ping_background.gray = 0;
6881 ping_background.index = 0;
6883 ping_trans_color.red=0;
6884 ping_trans_color.green=0;
6885 ping_trans_color.blue=0;
6886 ping_trans_color.gray=0;
6888 ping_pHYs_unit_type = 0;
6889 ping_pHYs_x_resolution = 0;
6890 ping_pHYs_y_resolution = 0;
6892 ping_have_blob=MagickFalse;
6893 ping_have_color=MagickTrue;
6894 ping_have_non_bw=MagickTrue;
6895 ping_have_PLTE=MagickFalse;
6896 ping_have_bKGD=MagickFalse;
6897 ping_have_pHYs=MagickFalse;
6898 ping_have_tRNS=MagickFalse;
6900 ping_exclude_bKGD=mng_info->ping_exclude_bKGD;
6901 ping_exclude_cHRM=mng_info->ping_exclude_cHRM;
6902 /* ping_exclude_EXIF=mng_info->ping_exclude_EXIF; */
6903 ping_exclude_gAMA=mng_info->ping_exclude_gAMA;
6904 ping_exclude_cHRM=mng_info->ping_exclude_cHRM;
6905 ping_exclude_iCCP=mng_info->ping_exclude_iCCP;
6906 /* ping_exclude_iTXt=mng_info->ping_exclude_iTXt; */
6907 ping_exclude_oFFs=mng_info->ping_exclude_oFFs;
6908 ping_exclude_pHYs=mng_info->ping_exclude_pHYs;
6909 ping_exclude_sRGB=mng_info->ping_exclude_sRGB;
6910 ping_exclude_tEXt=mng_info->ping_exclude_tEXt;
6911 /* ping_exclude_tRNS=mng_info->ping_exclude_tRNS; */
6912 ping_exclude_vpAg=mng_info->ping_exclude_vpAg;
6913 ping_exclude_zCCP=mng_info->ping_exclude_zCCP; /* hex-encoded iCCP in zTXt */
6914 ping_exclude_zTXt=mng_info->ping_exclude_zTXt;
6916 ping_need_colortype_warning = MagickFalse;
6919 number_semitransparent = 0;
6920 number_transparent = 0;
6922 if (logging != MagickFalse)
6924 if (image->storage_class == UndefinedClass)
6925 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
6926 " storage_class=UndefinedClass");
6927 if (image->storage_class == DirectClass)
6928 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
6929 " storage_class=DirectClass");
6930 if (image->storage_class == PseudoClass)
6931 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
6932 " storage_class=PseudoClass");
6935 if (image->colorspace != RGBColorspace)
6936 (void) TransformImageColorspace(image,RGBColorspace);
6939 Sometimes we get PseudoClass images whose RGB values don't match
6940 the colors in the colormap. This code syncs the RGB values.
6942 if (image->depth <= 8 && image->taint && image->storage_class == PseudoClass)
6943 (void) SyncImage(image);
6945 #if (MAGICKCORE_QUANTUM_DEPTH == 8)
6946 if (image->depth > 8)
6948 if (logging != MagickFalse)
6949 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
6950 " Reducing PNG bit depth to 8 since this is a Q8 build.");
6957 #if (MAGICKCORE_QUANTUM_DEPTH > 16)
6958 /* PNG does not handle depths greater than 16 so reduce it even
6961 if (image->depth > 16)
6965 #if (MAGICKCORE_QUANTUM_DEPTH >= 16)
6966 if (image->depth == 16 && mng_info->write_png_depth != 16)
6967 if (mng_info->write_png8 || LosslessReduceDepthOK(image) != MagickFalse)
6971 /* Normally we run this just once, but in the case of writing PNG8
6972 * we reduce the transparency to binary and run again, then reduce
6973 * the colors to a simple 3-3-3 palette and run once more, and finally
6974 * to a simple 3-3-2 palette.
6977 tried_333 = MagickFalse;
6983 * Sometimes we get DirectClass images that have 256 colors or fewer.
6984 * This code will build a colormap.
6986 * Also, sometimes we get PseudoClass images with an out-of-date
6987 * colormap. This code will replace the colormap with a new one.
6988 * Sometimes we get PseudoClass images that have more than 256 colors.
6989 * This code will delete the colormap and change the image to
6992 * If image->matte is MagickFalse, we ignore the opacity channel
6993 * even though it sometimes contains left-over non-opaque values.
6995 * Also we gather some information (number of opaque, transparent,
6996 * and semitransparent pixels, and whether the image has any non-gray
6997 * pixels or only black-and-white pixels) that we might need later.
6999 * Even if the user wants to force GrayAlpha or RGBA (colortype 4 or 6)
7000 * we need to check for bogus non-opaque values, at least.
7003 #if (MAGICKCORE_QUANTUM_DEPTH == 8)
7004 # define PNGK 0 /* Shift */
7005 # define PNGM 1 /* Scale */
7006 #elif (MAGICKCORE_QUANTUM_DEPTH == 16)
7008 # define PNGM 0x0101
7011 # define PNGM 0x01010101
7022 semitransparent[260],
7025 register IndexPacket
7028 register const PixelPacket
7032 register PixelPacket
7035 if (logging != MagickFalse)
7036 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7037 " Enter BUILD_PALETTE:");
7039 if (logging != MagickFalse)
7041 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7042 " image->columns=%.20g",(double) image->columns);
7043 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7044 " image->rows=%.20g",(double) image->rows);
7045 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7046 " image->matte=%.20g",(double) image->matte);
7047 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7048 " image->depth=%.20g",(double) image->depth);
7050 if (image->storage_class == PseudoClass && image->colormap != NULL)
7052 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7053 " Original colormap:");
7054 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7055 " i (red,green,blue,opacity)");
7057 for (i=0; i < 256; i++)
7059 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7060 " %d (%d,%d,%d,%d)",
7062 (int) image->colormap[i].red,
7063 (int) image->colormap[i].green,
7064 (int) image->colormap[i].blue,
7065 (int) image->colormap[i].opacity);
7068 for (i=image->colors - 10; i < (ssize_t) image->colors; i++)
7072 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7073 " %d (%d,%d,%d,%d)",
7075 (int) image->colormap[i].red,
7076 (int) image->colormap[i].green,
7077 (int) image->colormap[i].blue,
7078 (int) image->colormap[i].opacity);
7083 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7084 " image->colors=%d",(int) image->colors);
7086 if (image->colors == 0)
7087 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7088 " (zero means unknown)");
7090 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7091 " Regenerate the colormap");
7094 exception=(&image->exception);
7098 number_semitransparent = 0;
7099 number_transparent = 0;
7101 for (y=0; y < (ssize_t) image->rows; y++)
7103 q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
7105 if (q == (PixelPacket *) NULL)
7108 for (x=0; x < (ssize_t) image->columns; x++)
7110 if (image->matte == MagickFalse || q->opacity == OpaqueOpacity)
7112 if (number_opaque < 259)
7114 if (number_opaque == 0)
7117 opaque[0].opacity=OpaqueOpacity;
7121 for (i=0; i< (ssize_t) number_opaque; i++)
7123 if (IsColorEqual(opaque+i, (PixelPacket *) q))
7127 if (i == (ssize_t) number_opaque &&
7128 number_opaque < 259)
7132 opaque[i].opacity = OpaqueOpacity;
7136 else if (q->opacity == TransparentOpacity)
7138 if (number_transparent < 259)
7140 if (number_transparent == 0)
7143 ping_trans_color.red=(unsigned short)(q->red);
7144 ping_trans_color.green=(unsigned short) (q->green);
7145 ping_trans_color.blue=(unsigned short) (q->blue);
7146 ping_trans_color.gray=(unsigned short) (q->blue);
7147 number_transparent = 1;
7150 for (i=0; i< (ssize_t) number_transparent; i++)
7152 if (IsColorEqual(transparent+i, (PixelPacket *) q))
7156 if (i == (ssize_t) number_transparent &&
7157 number_transparent < 259)
7159 number_transparent++;
7160 transparent[i] = *q;
7166 if (number_semitransparent < 259)
7168 if (number_semitransparent == 0)
7170 semitransparent[0]=*q;
7171 number_semitransparent = 1;
7174 for (i=0; i< (ssize_t) number_semitransparent; i++)
7176 if (IsColorEqual(semitransparent+i,
7177 (PixelPacket *) q) &&
7178 q->opacity == semitransparent[i].opacity)
7182 if (i == (ssize_t) number_semitransparent &&
7183 number_semitransparent < 259)
7185 number_semitransparent++;
7186 semitransparent[i] = *q;
7194 if (ping_exclude_bKGD == MagickFalse)
7196 /* Add the background color to the palette, if it
7197 * isn't already there.
7199 for (i=0; i<number_opaque; i++)
7201 if (IsColorEqual(opaque+i, &image->background_color))
7205 if (number_opaque < 259 && i == number_opaque)
7207 opaque[i]=image->background_color;
7208 opaque[i].opacity = OpaqueOpacity;
7211 else if (logging != MagickFalse)
7212 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7213 " No room in the colormap to add background color");
7216 image_colors=number_opaque+number_transparent+number_semitransparent;
7218 if (mng_info->write_png8 != MagickFalse && image_colors > 256)
7220 /* No room for the background color; remove it. */
7225 if (logging != MagickFalse)
7227 if (image_colors > 256)
7228 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7229 " image has more than 256 colors");
7232 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7233 " image has %d colors",image_colors);
7236 if (mng_info->write_png_colortype != 7) /* We won't need this info */
7238 ping_have_color=MagickFalse;
7239 ping_have_non_bw=MagickFalse;
7241 if(image_colors > 256)
7243 for (y=0; y < (ssize_t) image->rows; y++)
7245 q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
7247 if (q == (PixelPacket *) NULL)
7250 /* Worst case is black-and-white; we are looking at every
7254 if (ping_have_color == MagickFalse)
7257 for (x=0; x < (ssize_t) image->columns; x++)
7259 if (s->red != s->green || s->red != s->blue)
7261 ping_have_color=MagickTrue;
7262 ping_have_non_bw=MagickTrue;
7269 if (ping_have_non_bw == MagickFalse)
7272 for (x=0; x < (ssize_t) image->columns; x++)
7274 if (s->red != 0 && s->red != QuantumRange)
7276 ping_have_non_bw=MagickTrue;
7285 if (image_colors < 257)
7291 * Initialize image colormap.
7294 if (logging != MagickFalse)
7295 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7296 " Sort the new colormap");
7298 /* Sort palette, transparent first */;
7302 for (i=0; i<number_transparent; i++)
7303 colormap[n++] = transparent[i];
7305 for (i=0; i<number_semitransparent; i++)
7306 colormap[n++] = semitransparent[i];
7308 for (i=0; i<number_opaque; i++)
7309 colormap[n++] = opaque[i];
7312 /* image_colors < 257; search the colormap instead of the pixels
7313 * to get ping_have_color and ping_have_non_bw
7317 if (ping_have_color == MagickFalse)
7319 if (colormap[i].red != colormap[i].green ||
7320 colormap[i].red != colormap[i].blue)
7322 ping_have_color=MagickTrue;
7323 ping_have_non_bw=MagickTrue;
7328 if (ping_have_non_bw == MagickFalse)
7330 if (colormap[i].red != 0 && colormap[i].red != QuantumRange)
7331 ping_have_non_bw=MagickTrue;
7335 if ((mng_info->ping_exclude_tRNS == MagickFalse ||
7336 (number_transparent == 0 && number_semitransparent == 0)) &&
7337 (((mng_info->write_png_colortype-1) ==
7338 PNG_COLOR_TYPE_PALETTE) ||
7339 (mng_info->write_png_colortype == 0)))
7341 if (logging != MagickFalse)
7343 if (n != (ssize_t) image_colors)
7344 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7345 " image_colors (%d) and n (%d) don't match",
7348 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7349 " AcquireImageColormap");
7352 image->colors = image_colors;
7354 if (AcquireImageColormap(image,image_colors) ==
7356 ThrowWriterException(ResourceLimitError,
7357 "MemoryAllocationFailed");
7359 for (i=0; i< (ssize_t) image_colors; i++)
7360 image->colormap[i] = colormap[i];
7362 if (logging != MagickFalse)
7364 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7365 " image->colors=%d (%d)",
7366 (int) image->colors, image_colors);
7368 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7369 " Update the pixel indexes");
7372 /* Sync the pixel indices with the new colormap */
7374 for (y=0; y < (ssize_t) image->rows; y++)
7376 q=GetAuthenticPixels(image,0,y,image->columns,1,
7379 if (q == (PixelPacket *) NULL)
7382 indexes=GetAuthenticIndexQueue(image);
7384 for (x=0; x < (ssize_t) image->columns; x++)
7386 for (i=0; i< (ssize_t) image_colors; i++)
7388 if ((image->matte == MagickFalse ||
7389 image->colormap[i].opacity == q->opacity) &&
7390 (IsColorEqual(&image->colormap[i],
7391 (PixelPacket *) q)))
7393 indexes[x]=(IndexPacket) i;
7400 if (SyncAuthenticPixels(image,exception) == MagickFalse)
7406 if (logging != MagickFalse)
7408 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7409 " image->colors=%d", (int) image->colors);
7411 if (image->colormap != NULL)
7413 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7414 " i (red,green,blue,opacity)");
7416 for (i=0; i < (ssize_t) image->colors; i++)
7418 if (i < 300 || i >= image->colors - 10)
7420 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7421 " %d (%d,%d,%d,%d)",
7423 (int) image->colormap[i].red,
7424 (int) image->colormap[i].green,
7425 (int) image->colormap[i].blue,
7426 (int) image->colormap[i].opacity);
7431 if (number_transparent < 257)
7432 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7433 " number_transparent = %d",
7434 number_transparent);
7437 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7438 " number_transparent > 256");
7440 if (number_opaque < 257)
7441 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7442 " number_opaque = %d",
7446 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7447 " number_opaque > 256");
7449 if (number_semitransparent < 257)
7450 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7451 " number_semitransparent = %d",
7452 number_semitransparent);
7455 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7456 " number_semitransparent > 256");
7458 if (ping_have_non_bw == MagickFalse)
7459 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7460 " All pixels and the background are black or white");
7462 else if (ping_have_color == MagickFalse)
7463 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7464 " All pixels and the background are gray");
7467 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7468 " At least one pixel or the background is non-gray");
7470 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7471 " Exit BUILD_PALETTE:");
7474 if (mng_info->write_png8 == MagickFalse)
7477 /* Make any reductions necessary for the PNG8 format */
7478 if (image_colors <= 256 &&
7479 image_colors != 0 && image->colormap != NULL &&
7480 number_semitransparent == 0 &&
7481 number_transparent <= 1)
7484 /* PNG8 can't have semitransparent colors so we threshold the
7485 * opacity to 0 or OpaqueOpacity
7487 if (number_semitransparent != 0)
7489 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7490 " Thresholding the alpha channel to binary");
7492 for (y=0; y < (ssize_t) image->rows; y++)
7494 r=GetAuthenticPixels(image,0,y,image->columns,1,
7497 if (r == (PixelPacket *) NULL)
7500 for (x=0; x < (ssize_t) image->columns; x++)
7502 r->opacity = r->opacity > TransparentOpacity/2 ?
7503 TransparentOpacity : OpaqueOpacity;
7507 if (SyncAuthenticPixels(image,exception) == MagickFalse)
7510 if (image_colors != 0 && image_colors <= 256 &&
7511 image->colormap != NULL)
7512 for (i=0; i<image_colors; i++)
7513 image->colormap[i].opacity =
7514 image->colormap[i].opacity > TransparentOpacity/2 ?
7515 TransparentOpacity : OpaqueOpacity;
7520 /* PNG8 can't have more than 256 colors so we quantize the pixels and
7521 * background color to the 3-3-3 or 3-3-2 palette. If the image is
7522 * mostly gray, the 3-3-3 palette should end up with 256 colors or less.
7524 if (tried_333 == MagickFalse && (image_colors == 0 || image_colors > 256))
7526 if (logging != MagickFalse)
7527 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7528 " Quantizing the background color to 3-3-3");
7530 tried_333 = MagickTrue;
7532 image->background_color.red=
7534 image->background_color.red) >> PNGK) & 0xe0) ) |
7536 image->background_color.red) >> PNGK) & 0xe0) >> 3) |
7538 image->background_color.red) >> PNGK) & 0xc0) >> 6)) * PNGM;
7539 image->background_color.green=
7541 image->background_color.green) >> PNGK) & 0xe0) ) |
7543 image->background_color.green) >> PNGK) & 0xe0) >> 3) |
7545 image->background_color.green) >> PNGK) & 0xc0) >> 6)) * PNGM;
7546 image->background_color.blue=
7548 image->background_color.blue) >> PNGK) & 0xe0) ) |
7550 image->background_color.blue) >> PNGK) & 0xe0) >> 3) |
7552 image->background_color.blue) >> PNGK) & 0xc0) >> 6)) * PNGM;
7554 if (logging != MagickFalse)
7555 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7556 " Quantizing the pixel colors to 3-3-3");
7558 if (image->colormap == NULL)
7560 for (y=0; y < (ssize_t) image->rows; y++)
7562 r=GetAuthenticPixels(image,0,y,image->columns,1,
7565 if (r == (PixelPacket *) NULL)
7568 for (x=0; x < (ssize_t) image->columns; x++)
7570 if (r->opacity == TransparentOpacity)
7572 r->red = image->background_color.red;
7573 r->green = image->background_color.green;
7574 r->blue = image->background_color.blue;
7579 ((((((size_t) r->red) >> PNGK) & 0xe0) ) |
7580 (((((size_t) r->red) >> PNGK) & 0xe0) >> 3) |
7581 (((((size_t) r->red) >> PNGK) & 0xc0) >> 6)) * PNGM;
7583 ((((((size_t) r->green) >> PNGK) & 0xe0) ) |
7584 (((((size_t) r->green) >> PNGK) & 0xe0) >> 3) |
7585 (((((size_t) r->green) >> PNGK) & 0xc0) >> 6)) * PNGM;
7587 ((((((size_t) r->blue) >> PNGK) & 0xe0) ) |
7588 (((((size_t) r->blue) >> PNGK) & 0xe0) >> 3) |
7589 (((((size_t) r->blue) >> PNGK) & 0xc0) >> 6)) * PNGM;
7594 if (SyncAuthenticPixels(image,exception) == MagickFalse)
7599 else /* Should not reach this; colormap already exists and
7602 if (logging != MagickFalse)
7603 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7604 " Quantizing the colormap to 3-3-3");
7605 for (i=0; i<image_colors; i++)
7607 image->colormap[i].red=
7609 image->colormap[i].red) >> PNGK) & 0xe0) ) |
7611 image->colormap[i].red) >> PNGK) & 0xe0) >> 3) |
7613 image->colormap[i].red) >> PNGK) & 0xc0) >> 6)) * PNGM;
7614 image->colormap[i].green=
7616 image->colormap[i].green) >> PNGK) & 0xe0) ) |
7618 image->colormap[i].green) >> PNGK) & 0xe0) >> 3) |
7620 image->colormap[i].green) >> PNGK) & 0xc0) >> 6)) * PNGM;
7621 image->colormap[i].blue=
7623 image->colormap[i].blue) >> PNGK) & 0xe0) ) |
7625 image->colormap[i].blue) >> PNGK) & 0xe0) >> 3) |
7627 image->colormap[i].blue) >> PNGK) & 0xc0) >> 6)) * PNGM;
7633 if (image_colors == 0 || image_colors > 256)
7635 if (logging != MagickFalse)
7636 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7637 " Quantizing the background color to 3-3-2");
7639 image->background_color.red=
7641 image->background_color.red) >> PNGK) & 0xe0) ) |
7643 image->background_color.red) >> PNGK) & 0xe0) >> 3) |
7645 image->background_color.red) >> PNGK) & 0xc0) >> 6)) * PNGM;
7646 image->background_color.green=
7648 image->background_color.green) >> PNGK) & 0xe0) ) |
7650 image->background_color.green) >> PNGK) & 0xe0) >> 3) |
7652 image->background_color.green) >> PNGK) & 0xc0) >> 6)) * PNGM;
7653 image->background_color.blue=
7655 image->background_color.blue) >> PNGK) & 0xc0) ) |
7657 image->background_color.blue) >> PNGK) & 0xc0) >> 2) |
7659 image->background_color.blue) >> PNGK) & 0xc0) >> 4) |
7661 image->background_color.blue) >> PNGK) & 0xc0) >> 6)) * PNGM;
7663 if (logging != MagickFalse)
7664 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7665 " Quantizing the pixel colors to 3-3-2");
7667 if (image->colormap == NULL)
7669 for (y=0; y < (ssize_t) image->rows; y++)
7671 r=GetAuthenticPixels(image,0,y,image->columns,1,
7674 if (r == (PixelPacket *) NULL)
7677 for (x=0; x < (ssize_t) image->columns; x++)
7679 if (r->opacity == TransparentOpacity)
7681 r->red = image->background_color.red;
7682 r->green = image->background_color.green;
7683 r->blue = image->background_color.blue;
7688 ((((((size_t) r->red) >> PNGK) & 0xe0) ) |
7689 (((((size_t) r->red) >> PNGK) & 0xe0) >> 3) |
7690 (((((size_t) r->red) >> PNGK) & 0xc0) >> 6)) * PNGM;
7692 ((((((size_t) r->green) >> PNGK) & 0xe0) ) |
7693 (((((size_t) r->green) >> PNGK) & 0xe0) >> 3) |
7694 (((((size_t) r->green) >> PNGK) & 0xc0) >> 6)) * PNGM;
7696 ((((((size_t) r->blue) >> PNGK) & 0xc0) ) |
7697 (((((size_t) r->blue) >> PNGK) & 0xc0) >> 2) |
7698 (((((size_t) r->blue) >> PNGK) & 0xc0) >> 4) |
7699 (((((size_t) r->blue) >> PNGK) & 0xc0) >> 6)) * PNGM;
7704 if (SyncAuthenticPixels(image,exception) == MagickFalse)
7709 else /* Should not reach this; colormap already exists and
7712 if (logging != MagickFalse)
7713 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7714 " Quantizing the colormap to 3-3-2");
7715 for (i=0; i<image_colors; i++)
7717 image->colormap[i].red=
7719 image->colormap[i].red) >> PNGK) & 0xe0) ) |
7721 image->colormap[i].red) >> PNGK) & 0xe0) >> 3) |
7723 image->colormap[i].red) >> PNGK) & 0xc0) >> 6)) * PNGM;
7724 image->colormap[i].green=
7726 image->colormap[i].green) >> PNGK) & 0xe0) ) |
7728 image->colormap[i].green) >> PNGK) & 0xe0) >> 3) |
7730 image->colormap[i].green) >> PNGK) & 0xc0) >> 6)) * PNGM;
7731 image->colormap[i].blue=
7733 image->colormap[i].blue) >> PNGK) & 0xc0) ) |
7735 image->colormap[i].blue) >> PNGK) & 0xc0) >> 2) |
7737 image->colormap[i].blue) >> PNGK) & 0xc0) >> 4) |
7739 image->colormap[i].blue) >> PNGK) & 0xc0) >> 6)) * PNGM;
7746 /* END OF BUILD_PALETTE */
7748 /* If we are excluding the tRNS chunk and there is transparency,
7749 * then we must write a Gray-Alpha (color-type 4) or RGBA (color-type 6)
7752 if (mng_info->ping_exclude_tRNS != MagickFalse &&
7753 (number_transparent != 0 || number_semitransparent != 0))
7755 int colortype=mng_info->write_png_colortype;
7757 if (ping_have_color == MagickFalse)
7758 mng_info->write_png_colortype = 5;
7761 mng_info->write_png_colortype = 7;
7763 if (colortype != 0 &&
7764 mng_info->write_png_colortype != (ssize_t) colortype)
7765 ping_need_colortype_warning=MagickTrue;
7769 /* See if cheap transparency is possible. It is only possible
7770 * when there is a single transparent color, no semitransparent
7771 * color, and no opaque color that has the same RGB components
7772 * as the transparent color. We only need this information if
7773 * we are writing a PNG with colortype 0 or 2, and we have not
7774 * excluded the tRNS chunk.
7776 if (number_transparent == 1 &&
7777 mng_info->write_png_colortype < 4)
7779 ping_have_cheap_transparency = MagickTrue;
7781 if (number_semitransparent != 0)
7782 ping_have_cheap_transparency = MagickFalse;
7784 else if (image_colors == 0 || image_colors > 256 ||
7785 image->colormap == NULL)
7790 register const PixelPacket
7793 exception=(&image->exception);
7795 for (y=0; y < (ssize_t) image->rows; y++)
7797 q=GetVirtualPixels(image,0,y,image->columns,1, exception);
7799 if (q == (PixelPacket *) NULL)
7802 for (x=0; x < (ssize_t) image->columns; x++)
7804 if (q->opacity != TransparentOpacity &&
7805 (unsigned short) q->red == ping_trans_color.red &&
7806 (unsigned short) q->green == ping_trans_color.green &&
7807 (unsigned short) q->blue == ping_trans_color.blue)
7809 ping_have_cheap_transparency = MagickFalse;
7816 if (ping_have_cheap_transparency == MagickFalse)
7822 /* Assuming that image->colormap[0] is the one transparent color
7823 * and that all others are opaque.
7825 if (image_colors > 1)
7826 for (i=1; i<image_colors; i++)
7827 if (image->colormap[i].red == image->colormap[0].red &&
7828 image->colormap[i].green == image->colormap[0].green &&
7829 image->colormap[i].blue == image->colormap[0].blue)
7831 ping_have_cheap_transparency = MagickFalse;
7836 if (logging != MagickFalse)
7838 if (ping_have_cheap_transparency == MagickFalse)
7839 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7840 " Cheap transparency is not possible.");
7843 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7844 " Cheap transparency is possible.");
7848 ping_have_cheap_transparency = MagickFalse;
7850 image_depth=image->depth;
7852 quantum_info = (QuantumInfo *) NULL;
7854 image_colors=(int) image->colors;
7855 image_matte=image->matte;
7857 mng_info->IsPalette=image->storage_class == PseudoClass &&
7858 image_colors <= 256 && image->colormap != NULL;
7860 if ((mng_info->write_png_colortype == 4 || mng_info->write_png8) &&
7861 (image->colors == 0 || image->colormap == NULL))
7863 image_info=DestroyImageInfo(image_info);
7864 image=DestroyImage(image);
7865 (void) ThrowMagickException(&IMimage->exception,
7866 GetMagickModule(),CoderError,
7867 "Cannot write PNG8 or color-type 3; colormap is NULL",
7868 "`%s'",IMimage->filename);
7869 #if defined(PNG_SETJMP_NOT_THREAD_SAFE)
7870 UnlockSemaphoreInfo(ping_semaphore);
7872 return(MagickFalse);
7876 Allocate the PNG structures
7878 #ifdef PNG_USER_MEM_SUPPORTED
7879 ping=png_create_write_struct_2(PNG_LIBPNG_VER_STRING,image,
7880 MagickPNGErrorHandler,MagickPNGWarningHandler,(void *) NULL,
7881 (png_malloc_ptr) Magick_png_malloc,(png_free_ptr) Magick_png_free);
7884 ping=png_create_write_struct(PNG_LIBPNG_VER_STRING,image,
7885 MagickPNGErrorHandler,MagickPNGWarningHandler);
7888 if (ping == (png_struct *) NULL)
7889 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
7891 ping_info=png_create_info_struct(ping);
7893 if (ping_info == (png_info *) NULL)
7895 png_destroy_write_struct(&ping,(png_info **) NULL);
7896 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
7899 png_set_write_fn(ping,image,png_put_data,png_flush_data);
7900 ping_pixels=(unsigned char *) NULL;
7902 if (setjmp(png_jmpbuf(ping)))
7908 if (image_info->verbose)
7909 (void) printf("PNG write has failed.\n");
7911 png_destroy_write_struct(&ping,&ping_info);
7912 #if defined(PNG_SETJMP_NOT_THREAD_SAFE)
7913 UnlockSemaphoreInfo(ping_semaphore);
7915 if (ping_have_blob != MagickFalse)
7916 (void) CloseBlob(image);
7917 image_info=DestroyImageInfo(image_info);
7918 image=DestroyImage(image);
7919 return(MagickFalse);
7922 Prepare PNG for writing.
7924 #if defined(PNG_MNG_FEATURES_SUPPORTED)
7925 if (mng_info->write_mng)
7926 (void) png_permit_mng_features(ping,PNG_ALL_MNG_FEATURES);
7929 # ifdef PNG_WRITE_EMPTY_PLTE_SUPPORTED
7930 if (mng_info->write_mng)
7931 png_permit_empty_plte(ping,MagickTrue);
7938 ping_width=(png_uint_32) image->columns;
7939 ping_height=(png_uint_32) image->rows;
7941 if (mng_info->write_png8 || mng_info->write_png24 || mng_info->write_png32)
7944 if (mng_info->write_png_depth != 0)
7945 image_depth=mng_info->write_png_depth;
7947 /* Adjust requested depth to next higher valid depth if necessary */
7948 if (image_depth > 8)
7951 if ((image_depth > 4) && (image_depth < 8))
7954 if (image_depth == 3)
7957 if (logging != MagickFalse)
7959 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7960 " width=%.20g",(double) ping_width);
7961 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7962 " height=%.20g",(double) ping_height);
7963 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7964 " image_matte=%.20g",(double) image->matte);
7965 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7966 " image->depth=%.20g",(double) image->depth);
7967 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7968 " Tentative ping_bit_depth=%.20g",(double) image_depth);
7971 save_image_depth=image_depth;
7972 ping_bit_depth=(png_byte) save_image_depth;
7975 #if defined(PNG_pHYs_SUPPORTED)
7976 if (ping_exclude_pHYs == MagickFalse)
7978 if ((image->x_resolution != 0) && (image->y_resolution != 0) &&
7979 (!mng_info->write_mng || !mng_info->equal_physs))
7981 if (logging != MagickFalse)
7982 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7983 " Setting up pHYs chunk");
7985 if (image->units == PixelsPerInchResolution)
7987 ping_pHYs_unit_type=PNG_RESOLUTION_METER;
7988 ping_pHYs_x_resolution=
7989 (png_uint_32) ((100.0*image->x_resolution+0.5)/2.54);
7990 ping_pHYs_y_resolution=
7991 (png_uint_32) ((100.0*image->y_resolution+0.5)/2.54);
7994 else if (image->units == PixelsPerCentimeterResolution)
7996 ping_pHYs_unit_type=PNG_RESOLUTION_METER;
7997 ping_pHYs_x_resolution=(png_uint_32) (100.0*image->x_resolution+0.5);
7998 ping_pHYs_y_resolution=(png_uint_32) (100.0*image->y_resolution+0.5);
8003 ping_pHYs_unit_type=PNG_RESOLUTION_UNKNOWN;
8004 ping_pHYs_x_resolution=(png_uint_32) image->x_resolution;
8005 ping_pHYs_y_resolution=(png_uint_32) image->y_resolution;
8008 if (logging != MagickFalse)
8009 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8010 " Set up PNG pHYs chunk: xres: %.20g, yres: %.20g, units: %d.",
8011 (double) ping_pHYs_x_resolution,(double) ping_pHYs_y_resolution,
8012 (int) ping_pHYs_unit_type);
8013 ping_have_pHYs = MagickTrue;
8018 if (ping_exclude_bKGD == MagickFalse)
8020 if ((!mng_info->adjoin || !mng_info->equal_backgrounds))
8026 if (ping_bit_depth == 8)
8029 if (ping_bit_depth == 4)
8032 if (ping_bit_depth == 2)
8035 if (ping_bit_depth == 1)
8038 ping_background.red=(png_uint_16)
8039 (ScaleQuantumToShort(image->background_color.red) & mask);
8041 ping_background.green=(png_uint_16)
8042 (ScaleQuantumToShort(image->background_color.green) & mask);
8044 ping_background.blue=(png_uint_16)
8045 (ScaleQuantumToShort(image->background_color.blue) & mask);
8048 if (logging != MagickFalse)
8050 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8051 " Setting up bKGD chunk (1)");
8053 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8054 " ping_bit_depth=%d",ping_bit_depth);
8057 ping_have_bKGD = MagickTrue;
8061 Select the color type.
8066 if (mng_info->IsPalette && mng_info->write_png8)
8069 /* To do: make this a function cause it's used twice, except
8070 for reducing the sample depth from 8. */
8072 number_colors=image_colors;
8074 ping_have_tRNS=MagickFalse;
8079 ping_color_type=(png_byte) PNG_COLOR_TYPE_PALETTE;
8081 if (logging != MagickFalse)
8082 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8083 " Setting up PLTE chunk with %d colors (%d)",
8084 number_colors, image_colors);
8086 for (i=0; i < (ssize_t) number_colors; i++)
8088 palette[i].red=ScaleQuantumToChar(image->colormap[i].red);
8089 palette[i].green=ScaleQuantumToChar(image->colormap[i].green);
8090 palette[i].blue=ScaleQuantumToChar(image->colormap[i].blue);
8091 if (logging != MagickFalse)
8092 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8093 #if MAGICKCORE_QUANTUM_DEPTH == 8
8094 " %3ld (%3d,%3d,%3d)",
8096 " %5ld (%5d,%5d,%5d)",
8098 (long) i,palette[i].red,palette[i].green,palette[i].blue);
8102 ping_have_PLTE=MagickTrue;
8103 image_depth=ping_bit_depth;
8106 if (matte != MagickFalse)
8109 Identify which colormap entry is transparent.
8111 assert(number_colors <= 256);
8112 assert(image->colormap != NULL);
8114 for (i=0; i < (ssize_t) number_transparent; i++)
8115 ping_trans_alpha[i]=0;
8118 ping_num_trans=(unsigned short) (number_transparent +
8119 number_semitransparent);
8121 if (ping_num_trans == 0)
8122 ping_have_tRNS=MagickFalse;
8125 ping_have_tRNS=MagickTrue;
8128 if (ping_exclude_bKGD == MagickFalse)
8131 * Identify which colormap entry is the background color.
8134 for (i=0; i < (ssize_t) MagickMax(1L*number_colors-1L,1L); i++)
8135 if (IsPNGColorEqual(ping_background,image->colormap[i]))
8138 ping_background.index=(png_byte) i;
8140 } /* end of write_png8 */
8142 else if (mng_info->write_png24)
8144 image_matte=MagickFalse;
8145 ping_color_type=(png_byte) PNG_COLOR_TYPE_RGB;
8148 else if (mng_info->write_png32)
8150 image_matte=MagickTrue;
8151 ping_color_type=(png_byte) PNG_COLOR_TYPE_RGB_ALPHA;
8154 else /* mng_info->write_pngNN not specified */
8156 image_depth=ping_bit_depth;
8158 if (mng_info->write_png_colortype != 0)
8160 ping_color_type=(png_byte) mng_info->write_png_colortype-1;
8162 if (ping_color_type == PNG_COLOR_TYPE_GRAY_ALPHA ||
8163 ping_color_type == PNG_COLOR_TYPE_RGB_ALPHA)
8164 image_matte=MagickTrue;
8167 image_matte=MagickFalse;
8169 if (logging != MagickFalse)
8170 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8171 " PNG colortype %d was specified:",(int) ping_color_type);
8174 else /* write_png_colortype not specified */
8176 if (logging != MagickFalse)
8177 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8178 " Selecting PNG colortype:");
8180 ping_color_type=(png_byte) ((matte != MagickFalse)?
8181 PNG_COLOR_TYPE_RGB_ALPHA:PNG_COLOR_TYPE_RGB);
8183 if (image_info->type == TrueColorType)
8185 ping_color_type=(png_byte) PNG_COLOR_TYPE_RGB;
8186 image_matte=MagickFalse;
8189 if (image_info->type == TrueColorMatteType)
8191 ping_color_type=(png_byte) PNG_COLOR_TYPE_RGB_ALPHA;
8192 image_matte=MagickTrue;
8195 if (image_info->type == PaletteType ||
8196 image_info->type == PaletteMatteType)
8197 ping_color_type=(png_byte) PNG_COLOR_TYPE_PALETTE;
8199 if (mng_info->write_png_colortype == 0 &&
8200 (image_info->type == UndefinedType ||
8201 image_info->type == OptimizeType))
8203 if (ping_have_color == MagickFalse)
8205 if (image_matte == MagickFalse)
8207 ping_color_type=(png_byte) PNG_COLOR_TYPE_GRAY;
8208 image_matte=MagickFalse;
8213 ping_color_type=(png_byte) PNG_COLOR_TYPE_GRAY_ALPHA;
8214 image_matte=MagickTrue;
8219 if (image_matte == MagickFalse)
8221 ping_color_type=(png_byte) PNG_COLOR_TYPE_RGB;
8222 image_matte=MagickFalse;
8227 ping_color_type=(png_byte) PNG_COLOR_TYPE_RGBA;
8228 image_matte=MagickTrue;
8235 if (logging != MagickFalse)
8236 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8237 " Selected PNG colortype=%d",ping_color_type);
8239 if (ping_bit_depth < 8)
8241 if (ping_color_type == PNG_COLOR_TYPE_GRAY_ALPHA ||
8242 ping_color_type == PNG_COLOR_TYPE_RGB ||
8243 ping_color_type == PNG_COLOR_TYPE_RGB_ALPHA)
8247 old_bit_depth=ping_bit_depth;
8249 if (ping_color_type == PNG_COLOR_TYPE_GRAY)
8251 if (image->matte == MagickFalse && ping_have_non_bw == MagickFalse)
8255 if (ping_color_type == PNG_COLOR_TYPE_PALETTE)
8260 if (image->colors == 0)
8263 (void) ThrowMagickException(&image->exception,
8264 GetMagickModule(),CoderError,
8265 "image has 0 colors", "`%s'","");
8268 while ((int) (one << ping_bit_depth) < (ssize_t) image_colors)
8269 ping_bit_depth <<= 1;
8272 if (logging != MagickFalse)
8274 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8275 " Number of colors: %.20g",(double) image_colors);
8277 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8278 " Tentative PNG bit depth: %d",ping_bit_depth);
8281 if (ping_bit_depth < (int) mng_info->write_png_depth)
8282 ping_bit_depth = mng_info->write_png_depth;
8285 image_depth=ping_bit_depth;
8287 if (logging != MagickFalse)
8289 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8290 " Tentative PNG color type: %.20g",(double) ping_color_type);
8292 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8293 " image_info->type: %.20g",(double) image_info->type);
8295 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8296 " image_depth: %.20g",(double) image_depth);
8298 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8300 " image->depth: %.20g",(double) image->depth);
8302 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8303 " ping_bit_depth: %.20g",(double) ping_bit_depth);
8306 if (matte != MagickFalse)
8308 if (mng_info->IsPalette)
8310 if (mng_info->write_png_colortype == 0)
8312 ping_color_type=PNG_COLOR_TYPE_GRAY_ALPHA;
8314 if (ping_have_color != MagickFalse)
8315 ping_color_type=PNG_COLOR_TYPE_RGBA;
8319 * Determine if there is any transparent color.
8321 if (number_transparent + number_semitransparent == 0)
8324 No transparent pixels are present. Change 4 or 6 to 0 or 2.
8327 image_matte=MagickFalse;
8329 if (mng_info->write_png_colortype == 0)
8330 ping_color_type&=0x03;
8340 if (ping_bit_depth == 8)
8343 if (ping_bit_depth == 4)
8346 if (ping_bit_depth == 2)
8349 if (ping_bit_depth == 1)
8352 ping_trans_color.red=(png_uint_16)
8353 (ScaleQuantumToShort(image->colormap[0].red) & mask);
8355 ping_trans_color.green=(png_uint_16)
8356 (ScaleQuantumToShort(image->colormap[0].green) & mask);
8358 ping_trans_color.blue=(png_uint_16)
8359 (ScaleQuantumToShort(image->colormap[0].blue) & mask);
8361 ping_trans_color.gray=(png_uint_16)
8362 (ScaleQuantumToShort(PixelIntensityToQuantum(
8363 image->colormap)) & mask);
8365 ping_trans_color.index=(png_byte) 0;
8367 ping_have_tRNS=MagickTrue;
8370 if (ping_have_tRNS != MagickFalse)
8373 * Determine if there is one and only one transparent color
8374 * and if so if it is fully transparent.
8376 if (ping_have_cheap_transparency == MagickFalse)
8377 ping_have_tRNS=MagickFalse;
8380 if (ping_have_tRNS != MagickFalse)
8382 if (mng_info->write_png_colortype == 0)
8383 ping_color_type &= 0x03; /* changes 4 or 6 to 0 or 2 */
8385 if (image_depth == 8)
8387 ping_trans_color.red&=0xff;
8388 ping_trans_color.green&=0xff;
8389 ping_trans_color.blue&=0xff;
8390 ping_trans_color.gray&=0xff;
8396 if (image_depth == 8)
8398 ping_trans_color.red&=0xff;
8399 ping_trans_color.green&=0xff;
8400 ping_trans_color.blue&=0xff;
8401 ping_trans_color.gray&=0xff;
8408 if (ping_have_tRNS != MagickFalse)
8409 image_matte=MagickFalse;
8411 if ((mng_info->IsPalette) &&
8412 mng_info->write_png_colortype-1 != PNG_COLOR_TYPE_PALETTE &&
8413 ping_have_color == MagickFalse &&
8414 (image_matte == MagickFalse || image_depth >= 8))
8418 if (image_matte != MagickFalse)
8419 ping_color_type=PNG_COLOR_TYPE_GRAY_ALPHA;
8421 else if (mng_info->write_png_colortype-1 != PNG_COLOR_TYPE_GRAY_ALPHA)
8423 ping_color_type=PNG_COLOR_TYPE_GRAY;
8425 if (save_image_depth == 16 && image_depth == 8)
8427 if (logging != MagickFalse)
8429 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8430 " Scaling ping_trans_color (0)");
8432 ping_trans_color.gray*=0x0101;
8436 if (image_depth > MAGICKCORE_QUANTUM_DEPTH)
8437 image_depth=MAGICKCORE_QUANTUM_DEPTH;
8439 if (image_colors == 0 || image_colors-1 > MaxColormapSize)
8440 image_colors=(int) (one << image_depth);
8442 if (image_depth > 8)
8448 if ((int) ping_color_type == PNG_COLOR_TYPE_PALETTE)
8450 if(!mng_info->write_png_depth)
8454 while ((int) (one << ping_bit_depth)
8455 < (ssize_t) image_colors)
8456 ping_bit_depth <<= 1;
8460 else if (ping_color_type ==
8461 PNG_COLOR_TYPE_GRAY && image_colors < 17 &&
8462 mng_info->IsPalette)
8464 /* Check if grayscale is reducible */
8467 depth_4_ok=MagickTrue,
8468 depth_2_ok=MagickTrue,
8469 depth_1_ok=MagickTrue;
8471 for (i=0; i < (ssize_t) image_colors; i++)
8476 intensity=ScaleQuantumToChar(image->colormap[i].red);
8478 if ((intensity & 0x0f) != ((intensity & 0xf0) >> 4))
8479 depth_4_ok=depth_2_ok=depth_1_ok=MagickFalse;
8480 else if ((intensity & 0x03) != ((intensity & 0x0c) >> 2))
8481 depth_2_ok=depth_1_ok=MagickFalse;
8482 else if ((intensity & 0x01) != ((intensity & 0x02) >> 1))
8483 depth_1_ok=MagickFalse;
8486 if (depth_1_ok && mng_info->write_png_depth <= 1)
8489 else if (depth_2_ok && mng_info->write_png_depth <= 2)
8492 else if (depth_4_ok && mng_info->write_png_depth <= 4)
8497 image_depth=ping_bit_depth;
8502 if (mng_info->IsPalette)
8504 number_colors=image_colors;
8506 if (image_depth <= 8)
8511 ping_color_type=(png_byte) PNG_COLOR_TYPE_PALETTE;
8513 if (mng_info->have_write_global_plte && matte == MagickFalse)
8515 png_set_PLTE(ping,ping_info,NULL,0);
8517 if (logging != MagickFalse)
8518 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8519 " Setting up empty PLTE chunk");
8524 for (i=0; i < (ssize_t) number_colors; i++)
8526 palette[i].red=ScaleQuantumToChar(image->colormap[i].red);
8527 palette[i].green=ScaleQuantumToChar(image->colormap[i].green);
8528 palette[i].blue=ScaleQuantumToChar(image->colormap[i].blue);
8531 if (logging != MagickFalse)
8532 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8533 " Setting up PLTE chunk with %d colors",
8536 ping_have_PLTE=MagickTrue;
8539 /* color_type is PNG_COLOR_TYPE_PALETTE */
8540 if (mng_info->write_png_depth == 0)
8548 while ((one << ping_bit_depth) < number_colors)
8549 ping_bit_depth <<= 1;
8554 if (matte != MagickFalse)
8557 * Set up trans_colors array.
8559 assert(number_colors <= 256);
8561 ping_num_trans=(unsigned short) (number_transparent +
8562 number_semitransparent);
8564 if (ping_num_trans == 0)
8565 ping_have_tRNS=MagickFalse;
8569 if (logging != MagickFalse)
8571 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8572 " Scaling ping_trans_color (1)");
8574 ping_have_tRNS=MagickTrue;
8576 for (i=0; i < ping_num_trans; i++)
8578 ping_trans_alpha[i]= (png_byte) (255-
8579 ScaleQuantumToChar(image->colormap[i].opacity));
8589 if (image_depth < 8)
8592 if ((save_image_depth == 16) && (image_depth == 8))
8594 if (logging != MagickFalse)
8596 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8597 " Scaling ping_trans_color from (%d,%d,%d)",
8598 (int) ping_trans_color.red,
8599 (int) ping_trans_color.green,
8600 (int) ping_trans_color.blue);
8603 ping_trans_color.red*=0x0101;
8604 ping_trans_color.green*=0x0101;
8605 ping_trans_color.blue*=0x0101;
8606 ping_trans_color.gray*=0x0101;
8608 if (logging != MagickFalse)
8610 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8612 (int) ping_trans_color.red,
8613 (int) ping_trans_color.green,
8614 (int) ping_trans_color.blue);
8619 if (ping_bit_depth < (ssize_t) mng_info->write_png_depth)
8620 ping_bit_depth = (ssize_t) mng_info->write_png_depth;
8623 Adjust background and transparency samples in sub-8-bit grayscale files.
8625 if (ping_bit_depth < 8 && ping_color_type ==
8626 PNG_COLOR_TYPE_GRAY)
8634 maxval=(png_uint_16) ((one << ping_bit_depth)-1);
8636 if (ping_exclude_bKGD == MagickFalse)
8639 ping_background.gray=(png_uint_16)
8640 (QuantumScale*(maxval*(PixelIntensity(&image->background_color))));
8642 if (logging != MagickFalse)
8643 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8644 " Setting up bKGD chunk (2)");
8646 ping_have_bKGD = MagickTrue;
8649 ping_trans_color.gray=(png_uint_16) (QuantumScale*(maxval*
8650 ping_trans_color.gray));
8653 if (ping_exclude_bKGD == MagickFalse)
8655 if (mng_info->IsPalette && (int) ping_color_type == PNG_COLOR_TYPE_PALETTE)
8658 Identify which colormap entry is the background color.
8661 number_colors=image_colors;
8663 for (i=0; i < (ssize_t) MagickMax(1L*number_colors,1L); i++)
8664 if (IsPNGColorEqual(image->background_color,image->colormap[i]))
8667 ping_background.index=(png_byte) i;
8669 if (logging != MagickFalse)
8671 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8672 " Setting up bKGD chunk with index=%d",(int) i);
8675 if (i < (ssize_t) number_colors)
8677 ping_have_bKGD = MagickTrue;
8679 if (logging != MagickFalse)
8681 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8682 " background =(%d,%d,%d)",
8683 (int) ping_background.red,
8684 (int) ping_background.green,
8685 (int) ping_background.blue);
8689 else /* Can't happen */
8691 if (logging != MagickFalse)
8692 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8693 " No room in PLTE to add bKGD color");
8694 ping_have_bKGD = MagickFalse;
8699 if (logging != MagickFalse)
8700 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8701 " PNG color type: %d",ping_color_type);
8703 Initialize compression level and filtering.
8705 if (logging != MagickFalse)
8707 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8708 " Setting up deflate compression");
8710 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8711 " Compression buffer size: 32768");
8714 png_set_compression_buffer_size(ping,32768L);
8716 if (logging != MagickFalse)
8717 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8718 " Compression mem level: 9");
8720 png_set_compression_mem_level(ping, 9);
8722 quality=image->quality == UndefinedCompressionQuality ? 75UL :
8730 level=(int) MagickMin((ssize_t) quality/10,9);
8732 if (logging != MagickFalse)
8733 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8734 " Compression level: %d",level);
8736 png_set_compression_level(ping,level);
8741 if (logging != MagickFalse)
8742 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8743 " Compression strategy: Z_HUFFMAN_ONLY");
8745 png_set_compression_strategy(ping, Z_HUFFMAN_ONLY);
8748 if (logging != MagickFalse)
8749 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8750 " Setting up filtering");
8752 #if defined(PNG_MNG_FEATURES_SUPPORTED) && defined(PNG_INTRAPIXEL_DIFFERENCING)
8753 /* This became available in libpng-1.0.9. Output must be a MNG. */
8754 if (mng_info->write_mng && ((quality % 10) == 7))
8756 if (logging != MagickFalse)
8757 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8758 " Filter_type: PNG_INTRAPIXEL_DIFFERENCING");
8760 ping_filter_method=PNG_INTRAPIXEL_DIFFERENCING;
8764 if (logging != MagickFalse)
8765 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8773 if ((quality % 10) > 5)
8774 base_filter=PNG_ALL_FILTERS;
8777 if ((quality % 10) != 5)
8778 base_filter=(int) quality % 10;
8781 if (((int) ping_color_type == PNG_COLOR_TYPE_GRAY) ||
8782 ((int) ping_color_type == PNG_COLOR_TYPE_PALETTE) ||
8784 base_filter=PNG_NO_FILTERS;
8787 base_filter=PNG_ALL_FILTERS;
8789 if (logging != MagickFalse)
8791 if (base_filter == PNG_ALL_FILTERS)
8792 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8793 " Base filter method: ADAPTIVE");
8795 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8796 " Base filter method: NONE");
8799 png_set_filter(ping,PNG_FILTER_TYPE_BASE,base_filter);
8802 if ((ping_exclude_tEXt == MagickFalse || ping_exclude_zTXt == MagickFalse) &&
8803 (ping_exclude_iCCP == MagickFalse || ping_exclude_zCCP == MagickFalse))
8805 ResetImageProfileIterator(image);
8806 for (name=GetNextImageProfile(image); name != (const char *) NULL; )
8808 profile=GetImageProfile(image,name);
8810 if (profile != (StringInfo *) NULL)
8812 #ifdef PNG_WRITE_iCCP_SUPPORTED
8813 if ((LocaleCompare(name,"ICC") == 0) ||
8814 (LocaleCompare(name,"ICM") == 0))
8817 if (ping_exclude_iCCP == MagickFalse)
8819 png_set_iCCP(ping,ping_info,(const png_charp) name,0,
8820 #if (PNG_LIBPNG_VER < 10500)
8821 (png_charp) GetStringInfoDatum(profile),
8823 (png_const_bytep) GetStringInfoDatum(profile),
8825 (png_uint_32) GetStringInfoLength(profile));
8831 if (ping_exclude_zCCP == MagickFalse)
8833 Magick_png_write_raw_profile(image_info,ping,ping_info,
8834 (unsigned char *) name,(unsigned char *) name,
8835 GetStringInfoDatum(profile),
8836 (png_uint_32) GetStringInfoLength(profile));
8840 if (logging != MagickFalse)
8841 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8842 " Setting up text chunk with %s profile",name);
8844 name=GetNextImageProfile(image);
8848 #if defined(PNG_WRITE_sRGB_SUPPORTED)
8849 if ((mng_info->have_write_global_srgb == 0) &&
8850 ((image->rendering_intent != UndefinedIntent) ||
8851 (image->colorspace == sRGBColorspace)))
8853 if (ping_exclude_sRGB == MagickFalse)
8856 Note image rendering intent.
8858 if (logging != MagickFalse)
8859 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8860 " Setting up sRGB chunk");
8862 (void) png_set_sRGB(ping,ping_info,(
8863 Magick_RenderingIntent_to_PNG_RenderingIntent(
8864 image->rendering_intent)));
8866 if (ping_exclude_gAMA == MagickFalse)
8867 png_set_gAMA(ping,ping_info,0.45455);
8871 if ((!mng_info->write_mng) || (!png_get_valid(ping,ping_info,PNG_INFO_sRGB)))
8874 if (ping_exclude_gAMA == MagickFalse &&
8875 (ping_exclude_sRGB == MagickFalse ||
8876 (image->gamma < .45 || image->gamma > .46)))
8878 if ((mng_info->have_write_global_gama == 0) && (image->gamma != 0.0))
8882 To do: check for cHRM+gAMA == sRGB, and write sRGB instead.
8884 if (logging != MagickFalse)
8885 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8886 " Setting up gAMA chunk");
8888 png_set_gAMA(ping,ping_info,image->gamma);
8892 if (ping_exclude_cHRM == MagickFalse)
8894 if ((mng_info->have_write_global_chrm == 0) &&
8895 (image->chromaticity.red_primary.x != 0.0))
8898 Note image chromaticity.
8899 To do: check for cHRM+gAMA == sRGB, and write sRGB instead.
8907 wp=image->chromaticity.white_point;
8908 rp=image->chromaticity.red_primary;
8909 gp=image->chromaticity.green_primary;
8910 bp=image->chromaticity.blue_primary;
8912 if (logging != MagickFalse)
8913 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8914 " Setting up cHRM chunk");
8916 png_set_cHRM(ping,ping_info,wp.x,wp.y,rp.x,rp.y,gp.x,gp.y,
8922 ping_interlace_method=image_info->interlace != NoInterlace;
8924 if (mng_info->write_mng)
8925 png_set_sig_bytes(ping,8);
8927 /* Bail out if cannot meet defined PNG:bit-depth or PNG:color-type */
8929 if (mng_info->write_png_colortype != 0)
8931 if (mng_info->write_png_colortype-1 == PNG_COLOR_TYPE_GRAY)
8932 if (ping_have_color != MagickFalse)
8934 ping_color_type = PNG_COLOR_TYPE_RGB;
8936 if (ping_bit_depth < 8)
8940 if (mng_info->write_png_colortype-1 == PNG_COLOR_TYPE_GRAY_ALPHA)
8941 if (ping_have_color != MagickFalse)
8942 ping_color_type = PNG_COLOR_TYPE_RGB_ALPHA;
8945 if (ping_need_colortype_warning != MagickFalse ||
8946 ((mng_info->write_png_depth &&
8947 (int) mng_info->write_png_depth != ping_bit_depth) ||
8948 (mng_info->write_png_colortype &&
8949 ((int) mng_info->write_png_colortype-1 != ping_color_type &&
8950 mng_info->write_png_colortype != 7 &&
8951 !(mng_info->write_png_colortype == 5 && ping_color_type == 0)))))
8953 if (logging != MagickFalse)
8955 if (ping_need_colortype_warning != MagickFalse)
8957 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8958 " Image has transparency but tRNS chunk was excluded");
8961 if (mng_info->write_png_depth)
8963 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8964 " Defined PNG:bit-depth=%u, Computed depth=%u",
8965 mng_info->write_png_depth,
8969 if (mng_info->write_png_colortype)
8971 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8972 " Defined PNG:color-type=%u, Computed color type=%u",
8973 mng_info->write_png_colortype-1,
8979 "Cannot write image with defined PNG:bit-depth or PNG:color-type.");
8982 if (image_matte != MagickFalse && image->matte == MagickFalse)
8984 /* Add an opaque matte channel */
8985 image->matte = MagickTrue;
8986 (void) SetImageOpacity(image,0);
8988 if (logging != MagickFalse)
8989 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8990 " Added an opaque matte channel");
8993 if (number_transparent != 0 || number_semitransparent != 0)
8995 if (ping_color_type < 4)
8997 ping_have_tRNS=MagickTrue;
8998 if (logging != MagickFalse)
8999 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9000 " Setting ping_have_tRNS=MagickTrue.");
9004 if (logging != MagickFalse)
9005 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9006 " Writing PNG header chunks");
9008 png_set_IHDR(ping,ping_info,ping_width,ping_height,
9009 ping_bit_depth,ping_color_type,
9010 ping_interlace_method,ping_compression_method,
9011 ping_filter_method);
9013 if (ping_color_type == 3 && ping_have_PLTE != MagickFalse)
9015 png_set_PLTE(ping,ping_info,palette,number_colors);
9017 if (logging != MagickFalse)
9019 for (i=0; i< (ssize_t) number_colors; i++)
9021 if (i < ping_num_trans)
9022 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9023 " PLTE[%d] = (%d,%d,%d), tRNS[%d] = (%d)",
9025 (int) palette[i].red,
9026 (int) palette[i].green,
9027 (int) palette[i].blue,
9029 (int) ping_trans_alpha[i]);
9031 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9032 " PLTE[%d] = (%d,%d,%d)",
9034 (int) palette[i].red,
9035 (int) palette[i].green,
9036 (int) palette[i].blue);
9041 if (ping_exclude_bKGD == MagickFalse)
9043 if (ping_have_bKGD != MagickFalse)
9044 png_set_bKGD(ping,ping_info,&ping_background);
9047 if (ping_exclude_pHYs == MagickFalse)
9049 if (ping_have_pHYs != MagickFalse)
9051 png_set_pHYs(ping,ping_info,
9052 ping_pHYs_x_resolution,
9053 ping_pHYs_y_resolution,
9054 ping_pHYs_unit_type);
9058 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9059 " Setting up pHYs chunk");
9060 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9061 " x_resolution=%lu",
9062 (unsigned long) ping_pHYs_x_resolution);
9063 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9064 " y_resolution=%lu",
9065 (unsigned long) ping_pHYs_y_resolution);
9066 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9068 (unsigned long) ping_pHYs_unit_type);
9073 #if defined(PNG_oFFs_SUPPORTED)
9074 if (ping_exclude_oFFs == MagickFalse)
9076 if (image->page.x || image->page.y)
9078 png_set_oFFs(ping,ping_info,(png_int_32) image->page.x,
9079 (png_int_32) image->page.y, 0);
9081 if (logging != MagickFalse)
9082 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9083 " Setting up oFFs chunk with x=%d, y=%d, units=0",
9084 (int) image->page.x, (int) image->page.y);
9089 if (mng_info->need_blob != MagickFalse)
9091 if (OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception) ==
9093 png_error(ping,"WriteBlob Failed");
9095 ping_have_blob=MagickTrue;
9098 png_write_info_before_PLTE(ping, ping_info);
9100 if (ping_have_tRNS != MagickFalse && ping_color_type < 4)
9102 if (logging != MagickFalse)
9104 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9105 " Calling png_set_tRNS with num_trans=%d",ping_num_trans);
9108 if (ping_color_type == 3)
9109 (void) png_set_tRNS(ping, ping_info,
9116 (void) png_set_tRNS(ping, ping_info,
9121 if (logging != MagickFalse)
9123 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9124 " tRNS color =(%d,%d,%d)",
9125 (int) ping_trans_color.red,
9126 (int) ping_trans_color.green,
9127 (int) ping_trans_color.blue);
9132 /* write any png-chunk-b profiles */
9133 (void) Magick_png_write_chunk_from_profile(image,"PNG-chunk-b",logging);
9135 png_write_info(ping,ping_info);
9137 /* write any PNG-chunk-m profiles */
9138 (void) Magick_png_write_chunk_from_profile(image,"PNG-chunk-m",logging);
9140 if (ping_exclude_vpAg == MagickFalse)
9142 if ((image->page.width != 0 && image->page.width != image->columns) ||
9143 (image->page.height != 0 && image->page.height != image->rows))
9148 (void) WriteBlobMSBULong(image,9L); /* data length=8 */
9149 PNGType(chunk,mng_vpAg);
9150 LogPNGChunk(logging,mng_vpAg,9L);
9151 PNGLong(chunk+4,(png_uint_32) image->page.width);
9152 PNGLong(chunk+8,(png_uint_32) image->page.height);
9153 chunk[12]=0; /* unit = pixels */
9154 (void) WriteBlob(image,13,chunk);
9155 (void) WriteBlobMSBULong(image,crc32(0,chunk,13));
9159 #if (PNG_LIBPNG_VER == 10206)
9160 /* avoid libpng-1.2.6 bug by setting PNG_HAVE_IDAT flag */
9161 #define PNG_HAVE_IDAT 0x04
9162 ping->mode |= PNG_HAVE_IDAT;
9163 #undef PNG_HAVE_IDAT
9166 png_set_packing(ping);
9170 rowbytes=image->columns;
9171 if (image_depth > 8)
9173 switch (ping_color_type)
9175 case PNG_COLOR_TYPE_RGB:
9179 case PNG_COLOR_TYPE_GRAY_ALPHA:
9183 case PNG_COLOR_TYPE_RGBA:
9191 if (logging != MagickFalse)
9193 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9194 " Writing PNG image data");
9196 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9197 " Allocating %.20g bytes of memory for pixels",(double) rowbytes);
9199 ping_pixels=(unsigned char *) AcquireQuantumMemory(rowbytes,
9200 sizeof(*ping_pixels));
9202 if (ping_pixels == (unsigned char *) NULL)
9203 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
9206 Initialize image scanlines.
9208 if (setjmp(png_jmpbuf(ping)))
9214 if (image_info->verbose)
9215 (void) printf("PNG write has failed.\n");
9217 png_destroy_write_struct(&ping,&ping_info);
9218 if (quantum_info != (QuantumInfo *) NULL)
9219 quantum_info=DestroyQuantumInfo(quantum_info);
9220 if (ping_pixels != (unsigned char *) NULL)
9221 ping_pixels=(unsigned char *) RelinquishMagickMemory(ping_pixels);
9222 #if defined(PNG_SETJMP_NOT_THREAD_SAFE)
9223 UnlockSemaphoreInfo(ping_semaphore);
9225 if (ping_have_blob != MagickFalse)
9226 (void) CloseBlob(image);
9227 image_info=DestroyImageInfo(image_info);
9228 image=DestroyImage(image);
9229 return(MagickFalse);
9231 quantum_info=AcquireQuantumInfo(image_info,image);
9232 if (quantum_info == (QuantumInfo *) NULL)
9233 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
9234 quantum_info->format=UndefinedQuantumFormat;
9235 quantum_info->depth=image_depth;
9236 num_passes=png_set_interlace_handling(ping);
9238 if ((!mng_info->write_png8 && !mng_info->write_png24 &&
9239 !mng_info->write_png32) &&
9240 (mng_info->IsPalette ||
9241 (image_info->type == BilevelType)) &&
9242 image_matte == MagickFalse &&
9243 ping_have_non_bw == MagickFalse)
9245 /* Palette, Bilevel, or Opaque Monochrome */
9246 register const PixelPacket
9249 quantum_info->depth=8;
9250 for (pass=0; pass < num_passes; pass++)
9253 Convert PseudoClass image to a PNG monochrome image.
9255 for (y=0; y < (ssize_t) image->rows; y++)
9257 if (logging != MagickFalse && y == 0)
9258 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9259 " Writing row of pixels (0)");
9261 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
9263 if (p == (const PixelPacket *) NULL)
9266 if (mng_info->IsPalette)
9268 (void) ExportQuantumPixels(image,(const CacheView *) NULL,
9269 quantum_info,GrayQuantum,ping_pixels,&image->exception);
9270 if (mng_info->write_png_colortype-1 == PNG_COLOR_TYPE_PALETTE &&
9271 mng_info->write_png_depth &&
9272 mng_info->write_png_depth != old_bit_depth)
9274 /* Undo pixel scaling */
9275 for (i=0; i < (ssize_t) image->columns; i++)
9276 *(ping_pixels+i)=(unsigned char) (*(ping_pixels+i)
9277 >> (8-old_bit_depth));
9283 (void) ExportQuantumPixels(image,(const CacheView *) NULL,
9284 quantum_info,RedQuantum,ping_pixels,&image->exception);
9287 if (mng_info->write_png_colortype-1 != PNG_COLOR_TYPE_PALETTE)
9288 for (i=0; i < (ssize_t) image->columns; i++)
9289 *(ping_pixels+i)=(unsigned char) ((*(ping_pixels+i) > 127) ?
9292 if (logging != MagickFalse && y == 0)
9293 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9294 " Writing row of pixels (1)");
9296 png_write_row(ping,ping_pixels);
9298 if (image->previous == (Image *) NULL)
9300 status=SetImageProgress(image,LoadImageTag,pass,num_passes);
9301 if (status == MagickFalse)
9307 else /* Not Palette, Bilevel, or Opaque Monochrome */
9309 if ((!mng_info->write_png8 && !mng_info->write_png24 &&
9310 !mng_info->write_png32) &&
9311 (image_matte != MagickFalse ||
9312 (ping_bit_depth >= MAGICKCORE_QUANTUM_DEPTH)) &&
9313 (mng_info->IsPalette) && ping_have_color == MagickFalse)
9315 register const PixelPacket
9318 for (pass=0; pass < num_passes; pass++)
9321 for (y=0; y < (ssize_t) image->rows; y++)
9323 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
9325 if (p == (const PixelPacket *) NULL)
9328 if (ping_color_type == PNG_COLOR_TYPE_GRAY)
9330 if (mng_info->IsPalette)
9331 (void) ExportQuantumPixels(image,(const CacheView *) NULL,
9332 quantum_info,GrayQuantum,ping_pixels,&image->exception);
9335 (void) ExportQuantumPixels(image,(const CacheView *) NULL,
9336 quantum_info,RedQuantum,ping_pixels,&image->exception);
9338 if (logging != MagickFalse && y == 0)
9339 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9340 " Writing GRAY PNG pixels (2)");
9343 else /* PNG_COLOR_TYPE_GRAY_ALPHA */
9345 if (logging != MagickFalse && y == 0)
9346 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9347 " Writing GRAY_ALPHA PNG pixels (2)");
9349 (void) ExportQuantumPixels(image,(const CacheView *) NULL,
9350 quantum_info,GrayAlphaQuantum,ping_pixels,&image->exception);
9353 if (logging != MagickFalse && y == 0)
9354 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9355 " Writing row of pixels (2)");
9357 png_write_row(ping,ping_pixels);
9360 if (image->previous == (Image *) NULL)
9362 status=SetImageProgress(image,LoadImageTag,pass,num_passes);
9363 if (status == MagickFalse)
9371 register const PixelPacket
9374 for (pass=0; pass < num_passes; pass++)
9376 if ((image_depth > 8) || (mng_info->write_png24 ||
9377 mng_info->write_png32 ||
9378 (!mng_info->write_png8 && !mng_info->IsPalette)))
9380 for (y=0; y < (ssize_t) image->rows; y++)
9382 p=GetVirtualPixels(image,0,y,image->columns,1,
9385 if (p == (const PixelPacket *) NULL)
9388 if (ping_color_type == PNG_COLOR_TYPE_GRAY)
9390 if (image->storage_class == DirectClass)
9391 (void) ExportQuantumPixels(image,(const CacheView *) NULL,
9392 quantum_info,RedQuantum,ping_pixels,&image->exception);
9395 (void) ExportQuantumPixels(image,(const CacheView *) NULL,
9396 quantum_info,GrayQuantum,ping_pixels,&image->exception);
9399 else if (ping_color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
9401 (void) ExportQuantumPixels(image,(const CacheView *) NULL,
9402 quantum_info,GrayAlphaQuantum,ping_pixels,
9405 if (logging != MagickFalse && y == 0)
9406 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9407 " Writing GRAY_ALPHA PNG pixels (3)");
9410 else if (image_matte != MagickFalse)
9411 (void) ExportQuantumPixels(image,(const CacheView *) NULL,
9412 quantum_info,RGBAQuantum,ping_pixels,&image->exception);
9415 (void) ExportQuantumPixels(image,(const CacheView *) NULL,
9416 quantum_info,RGBQuantum,ping_pixels,&image->exception);
9418 if (logging != MagickFalse && y == 0)
9419 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9420 " Writing row of pixels (3)");
9422 png_write_row(ping,ping_pixels);
9427 /* not ((image_depth > 8) || (mng_info->write_png24 ||
9428 mng_info->write_png32 ||
9429 (!mng_info->write_png8 && !mng_info->IsPalette))) */
9431 if ((ping_color_type != PNG_COLOR_TYPE_GRAY) &&
9432 (ping_color_type != PNG_COLOR_TYPE_GRAY_ALPHA))
9434 if (logging != MagickFalse)
9435 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9436 " pass %d, Image Is not GRAY or GRAY_ALPHA",pass);
9438 quantum_info->depth=8;
9442 for (y=0; y < (ssize_t) image->rows; y++)
9444 if (logging != MagickFalse && y == 0)
9445 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9446 " pass %d, Image Is RGB, 16-bit GRAY, or GRAY_ALPHA",pass);
9448 p=GetVirtualPixels(image,0,y,image->columns,1,
9451 if (p == (const PixelPacket *) NULL)
9454 if (ping_color_type == PNG_COLOR_TYPE_GRAY)
9456 quantum_info->depth=image->depth;
9458 (void) ExportQuantumPixels(image,(const CacheView *) NULL,
9459 quantum_info,GrayQuantum,ping_pixels,&image->exception);
9462 else if (ping_color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
9464 if (logging != MagickFalse && y == 0)
9465 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9466 " Writing GRAY_ALPHA PNG pixels (4)");
9468 (void) ExportQuantumPixels(image,(const CacheView *) NULL,
9469 quantum_info,GrayAlphaQuantum,ping_pixels,
9475 (void) ExportQuantumPixels(image,(const CacheView *) NULL,
9476 quantum_info,IndexQuantum,ping_pixels,&image->exception);
9478 if (logging != MagickFalse && y <= 2)
9480 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9481 " Writing row of non-gray pixels (4)");
9483 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9484 " ping_pixels[0]=%d,ping_pixels[1]=%d",
9485 (int)ping_pixels[0],(int)ping_pixels[1]);
9488 png_write_row(ping,ping_pixels);
9492 if (image->previous == (Image *) NULL)
9494 status=SetImageProgress(image,LoadImageTag,pass,num_passes);
9495 if (status == MagickFalse)
9502 if (quantum_info != (QuantumInfo *) NULL)
9503 quantum_info=DestroyQuantumInfo(quantum_info);
9505 if (logging != MagickFalse)
9507 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9508 " Wrote PNG image data");
9510 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9511 " Width: %.20g",(double) ping_width);
9513 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9514 " Height: %.20g",(double) ping_height);
9516 if (mng_info->write_png_depth)
9518 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9519 " Defined PNG:bit-depth: %d",mng_info->write_png_depth);
9522 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9523 " PNG bit-depth written: %d",ping_bit_depth);
9525 if (mng_info->write_png_colortype)
9527 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9528 " Defined PNG:color-type: %d",mng_info->write_png_colortype-1);
9531 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9532 " PNG color-type written: %d",ping_color_type);
9534 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9535 " PNG Interlace method: %d",ping_interlace_method);
9538 Generate text chunks.
9540 if (ping_exclude_tEXt == MagickFalse || ping_exclude_zTXt == MagickFalse)
9542 ResetImagePropertyIterator(image);
9543 property=GetNextImageProperty(image);
9544 while (property != (const char *) NULL)
9549 value=GetImageProperty(image,property);
9550 if (ping_exclude_pHYs != MagickFalse ||
9551 LocaleCompare(property,"density") != 0 ||
9552 LocaleCompare(property,"units") != 0)
9554 if (value != (const char *) NULL)
9556 text=(png_textp) png_malloc(ping,(png_uint_32) sizeof(png_text));
9557 text[0].key=(char *) property;
9558 text[0].text=(char *) value;
9559 text[0].text_length=strlen(value);
9561 if (ping_exclude_tEXt != MagickFalse)
9562 text[0].compression=PNG_TEXT_COMPRESSION_zTXt;
9564 else if (ping_exclude_zTXt != MagickFalse)
9565 text[0].compression=PNG_TEXT_COMPRESSION_NONE;
9569 text[0].compression=image_info->compression == NoCompression ||
9570 (image_info->compression == UndefinedCompression &&
9571 text[0].text_length < 128) ? PNG_TEXT_COMPRESSION_NONE :
9572 PNG_TEXT_COMPRESSION_zTXt ;
9575 if (logging != MagickFalse)
9577 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9578 " Setting up text chunk");
9580 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9581 " keyword: %s",text[0].key);
9584 png_set_text(ping,ping_info,text,1);
9585 png_free(ping,text);
9588 property=GetNextImageProperty(image);
9592 /* write any PNG-chunk-e profiles */
9593 (void) Magick_png_write_chunk_from_profile(image,"PNG-chunk-e",logging);
9595 if (logging != MagickFalse)
9596 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9597 " Writing PNG end info");
9599 png_write_end(ping,ping_info);
9601 if (mng_info->need_fram && (int) image->dispose == BackgroundDispose)
9603 if (mng_info->page.x || mng_info->page.y ||
9604 (ping_width != mng_info->page.width) ||
9605 (ping_height != mng_info->page.height))
9611 Write FRAM 4 with clipping boundaries followed by FRAM 1.
9613 (void) WriteBlobMSBULong(image,27L); /* data length=27 */
9614 PNGType(chunk,mng_FRAM);
9615 LogPNGChunk(logging,mng_FRAM,27L);
9617 chunk[5]=0; /* frame name separator (no name) */
9618 chunk[6]=1; /* flag for changing delay, for next frame only */
9619 chunk[7]=0; /* flag for changing frame timeout */
9620 chunk[8]=1; /* flag for changing frame clipping for next frame */
9621 chunk[9]=0; /* flag for changing frame sync_id */
9622 PNGLong(chunk+10,(png_uint_32) (0L)); /* temporary 0 delay */
9623 chunk[14]=0; /* clipping boundaries delta type */
9624 PNGLong(chunk+15,(png_uint_32) (mng_info->page.x)); /* left cb */
9626 (png_uint_32) (mng_info->page.x + ping_width));
9627 PNGLong(chunk+23,(png_uint_32) (mng_info->page.y)); /* top cb */
9629 (png_uint_32) (mng_info->page.y + ping_height));
9630 (void) WriteBlob(image,31,chunk);
9631 (void) WriteBlobMSBULong(image,crc32(0,chunk,31));
9632 mng_info->old_framing_mode=4;
9633 mng_info->framing_mode=1;
9637 mng_info->framing_mode=3;
9639 if (mng_info->write_mng && !mng_info->need_fram &&
9640 ((int) image->dispose == 3))
9641 (void) ThrowMagickException(&image->exception,GetMagickModule(),
9642 CoderError,"Cannot convert GIF with disposal method 3 to MNG-LC",
9643 "`%s'",image->filename);
9649 png_destroy_write_struct(&ping,&ping_info);
9651 ping_pixels=(unsigned char *) RelinquishMagickMemory(ping_pixels);
9653 #if defined(PNG_SETJMP_NOT_THREAD_SAFE)
9654 UnlockSemaphoreInfo(ping_semaphore);
9657 if (ping_have_blob != MagickFalse)
9658 (void) CloseBlob(image);
9660 image_info=DestroyImageInfo(image_info);
9661 image=DestroyImage(image);
9663 /* Store bit depth actually written */
9664 s[0]=(char) ping_bit_depth;
9667 (void) SetImageProperty(IMimage,"png:bit-depth-written",s);
9669 if (logging != MagickFalse)
9670 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9671 " exit WriteOnePNGImage()");
9674 /* End write one PNG image */
9678 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
9682 % W r i t e P N G I m a g e %
9686 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
9688 % WritePNGImage() writes a Portable Network Graphics (PNG) or
9689 % Multiple-image Network Graphics (MNG) image file.
9691 % MNG support written by Glenn Randers-Pehrson, glennrp@image...
9693 % The format of the WritePNGImage method is:
9695 % MagickBooleanType WritePNGImage(const ImageInfo *image_info,Image *image)
9697 % A description of each parameter follows:
9699 % o image_info: the image info.
9701 % o image: The image.
9703 % Returns MagickTrue on success, MagickFalse on failure.
9705 % Communicating with the PNG encoder:
9707 % While the datastream written is always in PNG format and normally would
9708 % be given the "png" file extension, this method also writes the following
9709 % pseudo-formats which are subsets of PNG:
9711 % o PNG8: An 8-bit indexed PNG datastream is written. If the image has
9712 % a depth greater than 8, the depth is reduced. If transparency
9713 % is present, the tRNS chunk must only have values 0 and 255
9714 % (i.e., transparency is binary: fully opaque or fully
9715 % transparent). If other values are present they will be
9716 % 50%-thresholded to binary transparency. If more than 256
9717 % colors are present, they will be quantized to the 3-3-2
9718 % palette. If you want better quantization or dithering of
9719 % the colors or alpha, you need to do it before calling the
9720 % PNG encoder. The pixels contain 8-bit indices even if
9721 % they could be represented with 1, 2, or 4 bits. Grayscale
9722 % images will be written as indexed PNG files even though the
9723 % PNG grayscale type might be slightly more efficient. Please
9724 % note that writing to the PNG8 format may result in loss
9725 % of color and alpha data.
9727 % o PNG24: An 8-bit per sample RGB PNG datastream is written. The tRNS
9728 % chunk can be present to convey binary transparency by naming
9729 % one of the colors as transparent. The only loss incurred
9730 % is reduction of sample depth to 8. If the image has more
9731 % than one transparent color, has semitransparent pixels, or
9732 % has an opaque pixel with the same RGB components as the
9733 % transparent color, an image is not written.
9735 % o PNG32: An 8-bit per sample RGBA PNG is written. Partial
9736 % transparency is permitted, i.e., the alpha sample for
9737 % each pixel can have any value from 0 to 255. The alpha
9738 % channel is present even if the image is fully opaque.
9739 % The only loss in data is the reduction of the sample depth
9742 % o -define: For more precise control of the PNG output, you can use the
9743 % Image options "png:bit-depth" and "png:color-type". These
9744 % can be set from the commandline with "-define" and also
9745 % from the application programming interfaces. The options
9746 % are case-independent and are converted to lowercase before
9747 % being passed to this encoder.
9749 % png:color-type can be 0, 2, 3, 4, or 6.
9751 % When png:color-type is 0 (Grayscale), png:bit-depth can
9752 % be 1, 2, 4, 8, or 16.
9754 % When png:color-type is 2 (RGB), png:bit-depth can
9757 % When png:color-type is 3 (Indexed), png:bit-depth can
9758 % be 1, 2, 4, or 8. This refers to the number of bits
9759 % used to store the index. The color samples always have
9760 % bit-depth 8 in indexed PNG files.
9762 % When png:color-type is 4 (Gray-Matte) or 6 (RGB-Matte),
9763 % png:bit-depth can be 8 or 16.
9765 % If the image cannot be written without loss with the requested bit-depth
9766 % and color-type, a PNG file will not be written, and the encoder will
9767 % return MagickFalse.
9769 % Since image encoders should not be responsible for the "heavy lifting",
9770 % the user should make sure that ImageMagick has already reduced the
9771 % image depth and number of colors and limit transparency to binary
9772 % transparency prior to attempting to write the image with depth, color,
9773 % or transparency limitations.
9775 % TODO: Enforce the previous paragraph.
9777 % Note that another definition, "png:bit-depth-written" exists, but it
9778 % is not intended for external use. It is only used internally by the
9779 % PNG encoder to inform the JNG encoder of the depth of the alpha channel.
9781 % It is possible to request that the PNG encoder write previously-formatted
9782 % ancillary chunks in the output PNG file, using the "-profile" commandline
9783 % option as shown below or by setting the profile via a programming
9786 % -profile PNG-chunk-x:<file>
9788 % where x is a location flag and <file> is a file containing the chunk
9789 % name in the first 4 bytes, then a colon (":"), followed by the chunk data.
9790 % This encoder will compute the chunk length and CRC, so those must not
9791 % be included in the file.
9793 % "x" can be "b" (before PLTE), "m" (middle, i.e., between PLTE and IDAT),
9794 % or "e" (end, i.e., after IDAT). If you want to write multiple chunks
9795 % of the same type, then add a short unique string after the "x" to prevent
9796 % subsequent profiles from overwriting the preceding ones, e.g.,
9798 % -profile PNG-chunk-b01:file01 -profile PNG-chunk-b02:file02
9800 % As of version 6.6.6 the following optimizations are always done:
9802 % o 32-bit depth is reduced to 16.
9803 % o 16-bit depth is reduced to 8 if all pixels contain samples whose
9804 % high byte and low byte are identical.
9805 % o Palette is sorted to remove unused entries and to put a
9806 % transparent color first, if BUILD_PNG_PALETTE is defined.
9807 % o Opaque matte channel is removed when writing an indexed PNG.
9808 % o Grayscale images are reduced to 1, 2, or 4 bit depth if
9809 % this can be done without loss and a larger bit depth N was not
9810 % requested via the "-define PNG:bit-depth=N" option.
9811 % o If matte channel is present but only one transparent color is
9812 % present, RGB+tRNS is written instead of RGBA
9813 % o Opaque matte channel is removed (or added, if color-type 4 or 6
9814 % was requested when converting an opaque image).
9816 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
9818 static MagickBooleanType WritePNGImage(const ImageInfo *image_info,
9840 assert(image_info != (const ImageInfo *) NULL);
9841 assert(image_info->signature == MagickSignature);
9842 assert(image != (Image *) NULL);
9843 assert(image->signature == MagickSignature);
9844 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
9845 logging=LogMagickEvent(CoderEvent,GetMagickModule(),"Enter WritePNGImage()");
9847 Allocate a MngInfo structure.
9849 have_mng_structure=MagickFalse;
9850 mng_info=(MngInfo *) AcquireMagickMemory(sizeof(MngInfo));
9852 if (mng_info == (MngInfo *) NULL)
9853 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
9856 Initialize members of the MngInfo structure.
9858 (void) ResetMagickMemory(mng_info,0,sizeof(MngInfo));
9859 mng_info->image=image;
9860 mng_info->equal_backgrounds=MagickTrue;
9861 have_mng_structure=MagickTrue;
9863 /* See if user has requested a specific PNG subformat */
9865 mng_info->write_png8=LocaleCompare(image_info->magick,"PNG8") == 0;
9866 mng_info->write_png24=LocaleCompare(image_info->magick,"PNG24") == 0;
9867 mng_info->write_png32=LocaleCompare(image_info->magick,"PNG32") == 0;
9869 if (mng_info->write_png8)
9871 mng_info->write_png_colortype = /* 3 */ 4;
9872 mng_info->write_png_depth = 8;
9876 if (mng_info->write_png24)
9878 mng_info->write_png_colortype = /* 2 */ 3;
9879 mng_info->write_png_depth = 8;
9882 if (image->matte == MagickTrue)
9883 (void) SetImageType(image,TrueColorMatteType);
9886 (void) SetImageType(image,TrueColorType);
9888 (void) SyncImage(image);
9891 if (mng_info->write_png32)
9893 mng_info->write_png_colortype = /* 6 */ 7;
9894 mng_info->write_png_depth = 8;
9897 if (image->matte == MagickTrue)
9898 (void) SetImageType(image,TrueColorMatteType);
9901 (void) SetImageType(image,TrueColorType);
9903 (void) SyncImage(image);
9906 value=GetImageOption(image_info,"png:bit-depth");
9908 if (value != (char *) NULL)
9910 if (LocaleCompare(value,"1") == 0)
9911 mng_info->write_png_depth = 1;
9913 else if (LocaleCompare(value,"2") == 0)
9914 mng_info->write_png_depth = 2;
9916 else if (LocaleCompare(value,"4") == 0)
9917 mng_info->write_png_depth = 4;
9919 else if (LocaleCompare(value,"8") == 0)
9920 mng_info->write_png_depth = 8;
9922 else if (LocaleCompare(value,"16") == 0)
9923 mng_info->write_png_depth = 16;
9926 (void) ThrowMagickException(&image->exception,
9927 GetMagickModule(),CoderWarning,
9928 "ignoring invalid defined png:bit-depth",
9931 if (logging != MagickFalse)
9932 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9933 " png:bit-depth=%d was defined.\n",mng_info->write_png_depth);
9936 value=GetImageOption(image_info,"png:color-type");
9938 if (value != (char *) NULL)
9940 /* We must store colortype+1 because 0 is a valid colortype */
9941 if (LocaleCompare(value,"0") == 0)
9942 mng_info->write_png_colortype = 1;
9944 else if (LocaleCompare(value,"2") == 0)
9945 mng_info->write_png_colortype = 3;
9947 else if (LocaleCompare(value,"3") == 0)
9948 mng_info->write_png_colortype = 4;
9950 else if (LocaleCompare(value,"4") == 0)
9951 mng_info->write_png_colortype = 5;
9953 else if (LocaleCompare(value,"6") == 0)
9954 mng_info->write_png_colortype = 7;
9957 (void) ThrowMagickException(&image->exception,
9958 GetMagickModule(),CoderWarning,
9959 "ignoring invalid defined png:color-type",
9962 if (logging != MagickFalse)
9963 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9964 " png:color-type=%d was defined.\n",mng_info->write_png_colortype-1);
9967 /* Check for chunks to be excluded:
9969 * The default is to not exclude any known chunks except for any
9970 * listed in the "unused_chunks" array, above.
9972 * Chunks can be listed for exclusion via a "PNG:exclude-chunk"
9973 * define (in the image properties or in the image artifacts)
9974 * or via a mng_info member. For convenience, in addition
9975 * to or instead of a comma-separated list of chunks, the
9976 * "exclude-chunk" string can be simply "all" or "none".
9978 * The exclude-chunk define takes priority over the mng_info.
9980 * A "PNG:include-chunk" define takes priority over both the
9981 * mng_info and the "PNG:exclude-chunk" define. Like the
9982 * "exclude-chunk" string, it can define "all" or "none" as
9983 * well as a comma-separated list. Chunks that are unknown to
9984 * ImageMagick are always excluded, regardless of their "copy-safe"
9985 * status according to the PNG specification, and even if they
9986 * appear in the "include-chunk" list.
9988 * Finally, all chunks listed in the "unused_chunks" array are
9989 * automatically excluded, regardless of the other instructions
9992 * if you exclude sRGB but not gAMA (recommended), then sRGB chunk
9993 * will not be written and the gAMA chunk will only be written if it
9994 * is not between .45 and .46, or approximately (1.0/2.2).
9996 * If you exclude tRNS and the image has transparency, the colortype
9997 * is forced to be 4 or 6 (GRAY_ALPHA or RGB_ALPHA).
9999 * The -strip option causes StripImage() to set the png:include-chunk
10000 * artifact to "none,gama".
10003 mng_info->ping_exclude_bKGD=MagickFalse;
10004 mng_info->ping_exclude_cHRM=MagickFalse;
10005 mng_info->ping_exclude_EXIF=MagickFalse; /* hex-encoded EXIF in zTXt */
10006 mng_info->ping_exclude_gAMA=MagickFalse;
10007 mng_info->ping_exclude_cHRM=MagickFalse;
10008 mng_info->ping_exclude_iCCP=MagickFalse;
10009 /* mng_info->ping_exclude_iTXt=MagickFalse; */
10010 mng_info->ping_exclude_oFFs=MagickFalse;
10011 mng_info->ping_exclude_pHYs=MagickFalse;
10012 mng_info->ping_exclude_sRGB=MagickFalse;
10013 mng_info->ping_exclude_tEXt=MagickFalse;
10014 mng_info->ping_exclude_tRNS=MagickFalse;
10015 mng_info->ping_exclude_vpAg=MagickFalse;
10016 mng_info->ping_exclude_zCCP=MagickFalse; /* hex-encoded iCCP in zTXt */
10017 mng_info->ping_exclude_zTXt=MagickFalse;
10019 excluding=MagickFalse;
10021 for (source=0; source<1; source++)
10025 value=GetImageArtifact(image,"png:exclude-chunk");
10028 value=GetImageArtifact(image,"png:exclude-chunks");
10032 value=GetImageOption(image_info,"png:exclude-chunk");
10035 value=GetImageOption(image_info,"png:exclude-chunks");
10044 excluding=MagickTrue;
10046 if (logging != MagickFalse)
10049 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10050 " png:exclude-chunk=%s found in image artifacts.\n", value);
10052 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10053 " png:exclude-chunk=%s found in image properties.\n", value);
10056 last=strlen(value);
10058 for (i=0; i<(int) last; i+=5)
10061 if (LocaleNCompare(value+i,"all",3) == 0)
10063 mng_info->ping_exclude_bKGD=MagickTrue;
10064 mng_info->ping_exclude_cHRM=MagickTrue;
10065 mng_info->ping_exclude_EXIF=MagickTrue;
10066 mng_info->ping_exclude_gAMA=MagickTrue;
10067 mng_info->ping_exclude_iCCP=MagickTrue;
10068 /* mng_info->ping_exclude_iTXt=MagickTrue; */
10069 mng_info->ping_exclude_oFFs=MagickTrue;
10070 mng_info->ping_exclude_pHYs=MagickTrue;
10071 mng_info->ping_exclude_sRGB=MagickTrue;
10072 mng_info->ping_exclude_tEXt=MagickTrue;
10073 mng_info->ping_exclude_tRNS=MagickTrue;
10074 mng_info->ping_exclude_vpAg=MagickTrue;
10075 mng_info->ping_exclude_zCCP=MagickTrue;
10076 mng_info->ping_exclude_zTXt=MagickTrue;
10080 if (LocaleNCompare(value+i,"none",4) == 0)
10082 mng_info->ping_exclude_bKGD=MagickFalse;
10083 mng_info->ping_exclude_cHRM=MagickFalse;
10084 mng_info->ping_exclude_EXIF=MagickFalse;
10085 mng_info->ping_exclude_gAMA=MagickFalse;
10086 mng_info->ping_exclude_iCCP=MagickFalse;
10087 /* mng_info->ping_exclude_iTXt=MagickFalse; */
10088 mng_info->ping_exclude_oFFs=MagickFalse;
10089 mng_info->ping_exclude_pHYs=MagickFalse;
10090 mng_info->ping_exclude_sRGB=MagickFalse;
10091 mng_info->ping_exclude_tEXt=MagickFalse;
10092 mng_info->ping_exclude_tRNS=MagickFalse;
10093 mng_info->ping_exclude_vpAg=MagickFalse;
10094 mng_info->ping_exclude_zCCP=MagickFalse;
10095 mng_info->ping_exclude_zTXt=MagickFalse;
10098 if (LocaleNCompare(value+i,"bkgd",4) == 0)
10099 mng_info->ping_exclude_bKGD=MagickTrue;
10101 if (LocaleNCompare(value+i,"chrm",4) == 0)
10102 mng_info->ping_exclude_cHRM=MagickTrue;
10104 if (LocaleNCompare(value+i,"exif",4) == 0)
10105 mng_info->ping_exclude_EXIF=MagickTrue;
10107 if (LocaleNCompare(value+i,"gama",4) == 0)
10108 mng_info->ping_exclude_gAMA=MagickTrue;
10110 if (LocaleNCompare(value+i,"iccp",4) == 0)
10111 mng_info->ping_exclude_iCCP=MagickTrue;
10114 if (LocaleNCompare(value+i,"itxt",4) == 0)
10115 mng_info->ping_exclude_iTXt=MagickTrue;
10118 if (LocaleNCompare(value+i,"gama",4) == 0)
10119 mng_info->ping_exclude_gAMA=MagickTrue;
10121 if (LocaleNCompare(value+i,"offs",4) == 0)
10122 mng_info->ping_exclude_oFFs=MagickTrue;
10124 if (LocaleNCompare(value+i,"phys",4) == 0)
10125 mng_info->ping_exclude_pHYs=MagickTrue;
10127 if (LocaleNCompare(value+i,"srgb",4) == 0)
10128 mng_info->ping_exclude_sRGB=MagickTrue;
10130 if (LocaleNCompare(value+i,"text",4) == 0)
10131 mng_info->ping_exclude_tEXt=MagickTrue;
10133 if (LocaleNCompare(value+i,"trns",4) == 0)
10134 mng_info->ping_exclude_tRNS=MagickTrue;
10136 if (LocaleNCompare(value+i,"vpag",4) == 0)
10137 mng_info->ping_exclude_vpAg=MagickTrue;
10139 if (LocaleNCompare(value+i,"zccp",4) == 0)
10140 mng_info->ping_exclude_zCCP=MagickTrue;
10142 if (LocaleNCompare(value+i,"ztxt",4) == 0)
10143 mng_info->ping_exclude_zTXt=MagickTrue;
10149 for (source=0; source<1; source++)
10153 value=GetImageArtifact(image,"png:include-chunk");
10156 value=GetImageArtifact(image,"png:include-chunks");
10160 value=GetImageOption(image_info,"png:include-chunk");
10163 value=GetImageOption(image_info,"png:include-chunks");
10171 excluding=MagickTrue;
10173 if (logging != MagickFalse)
10176 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10177 " png:include-chunk=%s found in image artifacts.\n", value);
10179 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10180 " png:include-chunk=%s found in image properties.\n", value);
10183 last=strlen(value);
10185 for (i=0; i<(int) last; i+=5)
10187 if (LocaleNCompare(value+i,"all",3) == 0)
10189 mng_info->ping_exclude_bKGD=MagickFalse;
10190 mng_info->ping_exclude_cHRM=MagickFalse;
10191 mng_info->ping_exclude_EXIF=MagickFalse;
10192 mng_info->ping_exclude_gAMA=MagickFalse;
10193 mng_info->ping_exclude_iCCP=MagickFalse;
10194 /* mng_info->ping_exclude_iTXt=MagickFalse; */
10195 mng_info->ping_exclude_oFFs=MagickFalse;
10196 mng_info->ping_exclude_pHYs=MagickFalse;
10197 mng_info->ping_exclude_sRGB=MagickFalse;
10198 mng_info->ping_exclude_tEXt=MagickFalse;
10199 mng_info->ping_exclude_tRNS=MagickFalse;
10200 mng_info->ping_exclude_vpAg=MagickFalse;
10201 mng_info->ping_exclude_zCCP=MagickFalse;
10202 mng_info->ping_exclude_zTXt=MagickFalse;
10206 if (LocaleNCompare(value+i,"none",4) == 0)
10208 mng_info->ping_exclude_bKGD=MagickTrue;
10209 mng_info->ping_exclude_cHRM=MagickTrue;
10210 mng_info->ping_exclude_EXIF=MagickTrue;
10211 mng_info->ping_exclude_gAMA=MagickTrue;
10212 mng_info->ping_exclude_iCCP=MagickTrue;
10213 /* mng_info->ping_exclude_iTXt=MagickTrue; */
10214 mng_info->ping_exclude_oFFs=MagickTrue;
10215 mng_info->ping_exclude_pHYs=MagickTrue;
10216 mng_info->ping_exclude_sRGB=MagickTrue;
10217 mng_info->ping_exclude_tEXt=MagickTrue;
10218 mng_info->ping_exclude_tRNS=MagickTrue;
10219 mng_info->ping_exclude_vpAg=MagickTrue;
10220 mng_info->ping_exclude_zCCP=MagickTrue;
10221 mng_info->ping_exclude_zTXt=MagickTrue;
10224 if (LocaleNCompare(value+i,"bkgd",4) == 0)
10225 mng_info->ping_exclude_bKGD=MagickFalse;
10227 if (LocaleNCompare(value+i,"chrm",4) == 0)
10228 mng_info->ping_exclude_cHRM=MagickFalse;
10230 if (LocaleNCompare(value+i,"exif",4) == 0)
10231 mng_info->ping_exclude_EXIF=MagickFalse;
10233 if (LocaleNCompare(value+i,"gama",4) == 0)
10234 mng_info->ping_exclude_gAMA=MagickFalse;
10236 if (LocaleNCompare(value+i,"iccp",4) == 0)
10237 mng_info->ping_exclude_iCCP=MagickFalse;
10240 if (LocaleNCompare(value+i,"itxt",4) == 0)
10241 mng_info->ping_exclude_iTXt=MagickFalse;
10244 if (LocaleNCompare(value+i,"gama",4) == 0)
10245 mng_info->ping_exclude_gAMA=MagickFalse;
10247 if (LocaleNCompare(value+i,"offs",4) == 0)
10248 mng_info->ping_exclude_oFFs=MagickFalse;
10250 if (LocaleNCompare(value+i,"phys",4) == 0)
10251 mng_info->ping_exclude_pHYs=MagickFalse;
10253 if (LocaleNCompare(value+i,"srgb",4) == 0)
10254 mng_info->ping_exclude_sRGB=MagickFalse;
10256 if (LocaleNCompare(value+i,"text",4) == 0)
10257 mng_info->ping_exclude_tEXt=MagickFalse;
10259 if (LocaleNCompare(value+i,"trns",4) == 0)
10260 mng_info->ping_exclude_tRNS=MagickFalse;
10262 if (LocaleNCompare(value+i,"vpag",4) == 0)
10263 mng_info->ping_exclude_vpAg=MagickFalse;
10265 if (LocaleNCompare(value+i,"zccp",4) == 0)
10266 mng_info->ping_exclude_zCCP=MagickFalse;
10268 if (LocaleNCompare(value+i,"ztxt",4) == 0)
10269 mng_info->ping_exclude_zTXt=MagickFalse;
10275 if (excluding != MagickFalse && logging != MagickFalse)
10277 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10278 " Chunks to be excluded from the output PNG:");
10279 if (mng_info->ping_exclude_bKGD != MagickFalse)
10280 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10282 if (mng_info->ping_exclude_cHRM != MagickFalse)
10283 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10285 if (mng_info->ping_exclude_EXIF != MagickFalse)
10286 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10288 if (mng_info->ping_exclude_gAMA != MagickFalse)
10289 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10291 if (mng_info->ping_exclude_iCCP != MagickFalse)
10292 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10295 if (mng_info->ping_exclude_iTXt != MagickFalse)
10296 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10299 if (mng_info->ping_exclude_oFFs != MagickFalse)
10300 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10302 if (mng_info->ping_exclude_pHYs != MagickFalse)
10303 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10305 if (mng_info->ping_exclude_sRGB != MagickFalse)
10306 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10308 if (mng_info->ping_exclude_tEXt != MagickFalse)
10309 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10311 if (mng_info->ping_exclude_tRNS != MagickFalse)
10312 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10314 if (mng_info->ping_exclude_vpAg != MagickFalse)
10315 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10317 if (mng_info->ping_exclude_zCCP != MagickFalse)
10318 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10320 if (mng_info->ping_exclude_zTXt != MagickFalse)
10321 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10325 mng_info->need_blob = MagickTrue;
10327 status=WriteOnePNGImage(mng_info,image_info,image);
10329 MngInfoFreeStruct(mng_info,&have_mng_structure);
10331 if (logging != MagickFalse)
10332 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"exit WritePNGImage()");
10337 #if defined(JNG_SUPPORTED)
10339 /* Write one JNG image */
10340 static MagickBooleanType WriteOneJNGImage(MngInfo *mng_info,
10341 const ImageInfo *image_info,Image *image)
10362 jng_alpha_compression_method,
10363 jng_alpha_sample_depth,
10370 logging=LogMagickEvent(CoderEvent,GetMagickModule(),
10371 " Enter WriteOneJNGImage()");
10373 blob=(unsigned char *) NULL;
10374 jpeg_image=(Image *) NULL;
10375 jpeg_image_info=(ImageInfo *) NULL;
10378 transparent=image_info->type==GrayscaleMatteType ||
10379 image_info->type==TrueColorMatteType;
10381 jng_alpha_sample_depth=0;
10382 jng_quality=image_info->quality == 0UL ? 75UL : image_info->quality;
10383 jng_alpha_compression_method=0;
10385 if (image->matte != MagickFalse)
10387 /* if any pixels are transparent */
10388 transparent=MagickTrue;
10389 if (image_info->compression==JPEGCompression)
10390 jng_alpha_compression_method=8;
10397 /* Create JPEG blob, image, and image_info */
10398 if (logging != MagickFalse)
10399 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10400 " Creating jpeg_image_info for opacity.");
10402 jpeg_image_info=(ImageInfo *) CloneImageInfo(image_info);
10404 if (jpeg_image_info == (ImageInfo *) NULL)
10405 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
10407 if (logging != MagickFalse)
10408 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10409 " Creating jpeg_image.");
10411 jpeg_image=CloneImage(image,0,0,MagickTrue,&image->exception);
10413 if (jpeg_image == (Image *) NULL)
10414 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
10416 (void) CopyMagickString(jpeg_image->magick,"JPEG",MaxTextExtent);
10417 status=SeparateImageChannel(jpeg_image,OpacityChannel);
10418 status=NegateImage(jpeg_image,MagickFalse);
10419 jpeg_image->matte=MagickFalse;
10421 if (jng_quality >= 1000)
10422 jpeg_image_info->quality=jng_quality/1000;
10425 jpeg_image_info->quality=jng_quality;
10427 jpeg_image_info->type=GrayscaleType;
10428 (void) SetImageType(jpeg_image,GrayscaleType);
10429 (void) AcquireUniqueFilename(jpeg_image->filename);
10430 (void) FormatMagickString(jpeg_image_info->filename,MaxTextExtent,
10431 "%s",jpeg_image->filename);
10434 /* To do: check bit depth of PNG alpha channel */
10436 /* Check if image is grayscale. */
10437 if (image_info->type != TrueColorMatteType && image_info->type !=
10438 TrueColorType && ImageIsGray(image))
10443 if (jng_alpha_compression_method==0)
10448 /* Encode opacity as a grayscale PNG blob */
10449 status=OpenBlob(jpeg_image_info,jpeg_image,WriteBinaryBlobMode,
10450 &image->exception);
10451 if (logging != MagickFalse)
10452 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10453 " Creating PNG blob.");
10456 (void) CopyMagickString(jpeg_image_info->magick,"PNG",MaxTextExtent);
10457 (void) CopyMagickString(jpeg_image->magick,"PNG",MaxTextExtent);
10458 jpeg_image_info->interlace=NoInterlace;
10460 blob=ImageToBlob(jpeg_image_info,jpeg_image,&length,
10461 &image->exception);
10463 /* Retrieve sample depth used */
10464 value=GetImageProperty(jpeg_image,"png:bit-depth-written");
10465 if (value != (char *) NULL)
10466 jng_alpha_sample_depth= (unsigned int) value[0];
10470 /* Encode opacity as a grayscale JPEG blob */
10472 status=OpenBlob(jpeg_image_info,jpeg_image,WriteBinaryBlobMode,
10473 &image->exception);
10475 (void) CopyMagickString(jpeg_image_info->magick,"JPEG",MaxTextExtent);
10476 (void) CopyMagickString(jpeg_image->magick,"JPEG",MaxTextExtent);
10477 jpeg_image_info->interlace=NoInterlace;
10478 if (logging != MagickFalse)
10479 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10480 " Creating blob.");
10481 blob=ImageToBlob(jpeg_image_info,jpeg_image,&length,
10482 &image->exception);
10483 jng_alpha_sample_depth=8;
10485 if (logging != MagickFalse)
10486 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10487 " Successfully read jpeg_image into a blob, length=%.20g.",
10491 /* Destroy JPEG image and image_info */
10492 jpeg_image=DestroyImage(jpeg_image);
10493 (void) RelinquishUniqueFileResource(jpeg_image_info->filename);
10494 jpeg_image_info=DestroyImageInfo(jpeg_image_info);
10497 /* Write JHDR chunk */
10498 (void) WriteBlobMSBULong(image,16L); /* chunk data length=16 */
10499 PNGType(chunk,mng_JHDR);
10500 LogPNGChunk(logging,mng_JHDR,16L);
10501 PNGLong(chunk+4,(png_uint_32) image->columns);
10502 PNGLong(chunk+8,(png_uint_32) image->rows);
10503 chunk[12]=jng_color_type;
10504 chunk[13]=8; /* sample depth */
10505 chunk[14]=8; /*jng_image_compression_method */
10506 chunk[15]=(unsigned char) (image_info->interlace == NoInterlace ? 0 : 8);
10507 chunk[16]=jng_alpha_sample_depth;
10508 chunk[17]=jng_alpha_compression_method;
10509 chunk[18]=0; /*jng_alpha_filter_method */
10510 chunk[19]=0; /*jng_alpha_interlace_method */
10511 (void) WriteBlob(image,20,chunk);
10512 (void) WriteBlobMSBULong(image,crc32(0,chunk,20));
10513 if (logging != MagickFalse)
10515 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10516 " JNG width:%15lu",(unsigned long) image->columns);
10518 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10519 " JNG height:%14lu",(unsigned long) image->rows);
10521 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10522 " JNG color type:%10d",jng_color_type);
10524 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10525 " JNG sample depth:%8d",8);
10527 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10528 " JNG compression:%9d",8);
10530 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10531 " JNG interlace:%11d",0);
10533 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10534 " JNG alpha depth:%9d",jng_alpha_sample_depth);
10536 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10537 " JNG alpha compression:%3d",jng_alpha_compression_method);
10539 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10540 " JNG alpha filter:%8d",0);
10542 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10543 " JNG alpha interlace:%5d",0);
10546 /* Write any JNG-chunk-b profiles */
10547 (void) Magick_png_write_chunk_from_profile(image,"JNG-chunk-b",logging);
10550 Write leading ancillary chunks
10556 Write JNG bKGD chunk
10567 if (jng_color_type == 8 || jng_color_type == 12)
10571 (void) WriteBlobMSBULong(image,(size_t) (num_bytes-4L));
10572 PNGType(chunk,mng_bKGD);
10573 LogPNGChunk(logging,mng_bKGD,(size_t) (num_bytes-4L));
10574 red=ScaleQuantumToChar(image->background_color.red);
10575 green=ScaleQuantumToChar(image->background_color.green);
10576 blue=ScaleQuantumToChar(image->background_color.blue);
10583 (void) WriteBlob(image,(size_t) num_bytes,chunk);
10584 (void) WriteBlobMSBULong(image,crc32(0,chunk,(uInt) num_bytes));
10587 if ((image->colorspace == sRGBColorspace || image->rendering_intent))
10590 Write JNG sRGB chunk
10592 (void) WriteBlobMSBULong(image,1L);
10593 PNGType(chunk,mng_sRGB);
10594 LogPNGChunk(logging,mng_sRGB,1L);
10596 if (image->rendering_intent != UndefinedIntent)
10597 chunk[4]=(unsigned char)
10598 Magick_RenderingIntent_to_PNG_RenderingIntent(
10599 (image->rendering_intent));
10602 chunk[4]=(unsigned char)
10603 Magick_RenderingIntent_to_PNG_RenderingIntent(
10604 (PerceptualIntent));
10606 (void) WriteBlob(image,5,chunk);
10607 (void) WriteBlobMSBULong(image,crc32(0,chunk,5));
10611 if (image->gamma != 0.0)
10614 Write JNG gAMA chunk
10616 (void) WriteBlobMSBULong(image,4L);
10617 PNGType(chunk,mng_gAMA);
10618 LogPNGChunk(logging,mng_gAMA,4L);
10619 PNGLong(chunk+4,(png_uint_32) (100000*image->gamma+0.5));
10620 (void) WriteBlob(image,8,chunk);
10621 (void) WriteBlobMSBULong(image,crc32(0,chunk,8));
10624 if ((mng_info->equal_chrms == MagickFalse) &&
10625 (image->chromaticity.red_primary.x != 0.0))
10631 Write JNG cHRM chunk
10633 (void) WriteBlobMSBULong(image,32L);
10634 PNGType(chunk,mng_cHRM);
10635 LogPNGChunk(logging,mng_cHRM,32L);
10636 primary=image->chromaticity.white_point;
10637 PNGLong(chunk+4,(png_uint_32) (100000*primary.x+0.5));
10638 PNGLong(chunk+8,(png_uint_32) (100000*primary.y+0.5));
10639 primary=image->chromaticity.red_primary;
10640 PNGLong(chunk+12,(png_uint_32) (100000*primary.x+0.5));
10641 PNGLong(chunk+16,(png_uint_32) (100000*primary.y+0.5));
10642 primary=image->chromaticity.green_primary;
10643 PNGLong(chunk+20,(png_uint_32) (100000*primary.x+0.5));
10644 PNGLong(chunk+24,(png_uint_32) (100000*primary.y+0.5));
10645 primary=image->chromaticity.blue_primary;
10646 PNGLong(chunk+28,(png_uint_32) (100000*primary.x+0.5));
10647 PNGLong(chunk+32,(png_uint_32) (100000*primary.y+0.5));
10648 (void) WriteBlob(image,36,chunk);
10649 (void) WriteBlobMSBULong(image,crc32(0,chunk,36));
10653 if (image->x_resolution && image->y_resolution && !mng_info->equal_physs)
10656 Write JNG pHYs chunk
10658 (void) WriteBlobMSBULong(image,9L);
10659 PNGType(chunk,mng_pHYs);
10660 LogPNGChunk(logging,mng_pHYs,9L);
10661 if (image->units == PixelsPerInchResolution)
10663 PNGLong(chunk+4,(png_uint_32)
10664 (image->x_resolution*100.0/2.54+0.5));
10666 PNGLong(chunk+8,(png_uint_32)
10667 (image->y_resolution*100.0/2.54+0.5));
10674 if (image->units == PixelsPerCentimeterResolution)
10676 PNGLong(chunk+4,(png_uint_32)
10677 (image->x_resolution*100.0+0.5));
10679 PNGLong(chunk+8,(png_uint_32)
10680 (image->y_resolution*100.0+0.5));
10687 PNGLong(chunk+4,(png_uint_32) (image->x_resolution+0.5));
10688 PNGLong(chunk+8,(png_uint_32) (image->y_resolution+0.5));
10692 (void) WriteBlob(image,13,chunk);
10693 (void) WriteBlobMSBULong(image,crc32(0,chunk,13));
10696 if (mng_info->write_mng == 0 && (image->page.x || image->page.y))
10699 Write JNG oFFs chunk
10701 (void) WriteBlobMSBULong(image,9L);
10702 PNGType(chunk,mng_oFFs);
10703 LogPNGChunk(logging,mng_oFFs,9L);
10704 PNGsLong(chunk+4,(ssize_t) (image->page.x));
10705 PNGsLong(chunk+8,(ssize_t) (image->page.y));
10707 (void) WriteBlob(image,13,chunk);
10708 (void) WriteBlobMSBULong(image,crc32(0,chunk,13));
10710 if (mng_info->write_mng == 0 && (image->page.width || image->page.height))
10712 (void) WriteBlobMSBULong(image,9L); /* data length=8 */
10713 PNGType(chunk,mng_vpAg);
10714 LogPNGChunk(logging,mng_vpAg,9L);
10715 PNGLong(chunk+4,(png_uint_32) image->page.width);
10716 PNGLong(chunk+8,(png_uint_32) image->page.height);
10717 chunk[12]=0; /* unit = pixels */
10718 (void) WriteBlob(image,13,chunk);
10719 (void) WriteBlobMSBULong(image,crc32(0,chunk,13));
10725 if (jng_alpha_compression_method==0)
10733 /* Write IDAT chunk header */
10734 if (logging != MagickFalse)
10735 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10736 " Write IDAT chunks from blob, length=%.20g.",(double)
10739 /* Copy IDAT chunks */
10742 for (i=8; i<(ssize_t) length; i+=len+12)
10744 len=(*p<<24)|((*(p+1))<<16)|((*(p+2))<<8)|(*(p+3));
10747 if (*(p)==73 && *(p+1)==68 && *(p+2)==65 && *(p+3)==84) /* IDAT */
10749 /* Found an IDAT chunk. */
10750 (void) WriteBlobMSBULong(image,(size_t) len);
10751 LogPNGChunk(logging,mng_IDAT,(size_t) len);
10752 (void) WriteBlob(image,(size_t) len+4,p);
10753 (void) WriteBlobMSBULong(image,
10754 crc32(0,p,(uInt) len+4));
10759 if (logging != MagickFalse)
10760 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10761 " Skipping %c%c%c%c chunk, length=%.20g.",
10762 *(p),*(p+1),*(p+2),*(p+3),(double) len);
10769 /* Write JDAA chunk header */
10770 if (logging != MagickFalse)
10771 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10772 " Write JDAA chunk, length=%.20g.",(double) length);
10773 (void) WriteBlobMSBULong(image,(size_t) length);
10774 PNGType(chunk,mng_JDAA);
10775 LogPNGChunk(logging,mng_JDAA,length);
10776 /* Write JDAT chunk(s) data */
10777 (void) WriteBlob(image,4,chunk);
10778 (void) WriteBlob(image,length,blob);
10779 (void) WriteBlobMSBULong(image,crc32(crc32(0,chunk,4),blob,
10782 blob=(unsigned char *) RelinquishMagickMemory(blob);
10785 /* Encode image as a JPEG blob */
10786 if (logging != MagickFalse)
10787 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10788 " Creating jpeg_image_info.");
10789 jpeg_image_info=(ImageInfo *) CloneImageInfo(image_info);
10790 if (jpeg_image_info == (ImageInfo *) NULL)
10791 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
10793 if (logging != MagickFalse)
10794 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10795 " Creating jpeg_image.");
10797 jpeg_image=CloneImage(image,0,0,MagickTrue,&image->exception);
10798 if (jpeg_image == (Image *) NULL)
10799 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
10800 (void) CopyMagickString(jpeg_image->magick,"JPEG",MaxTextExtent);
10802 (void) AcquireUniqueFilename(jpeg_image->filename);
10803 (void) FormatMagickString(jpeg_image_info->filename,MaxTextExtent,"%s",
10804 jpeg_image->filename);
10806 status=OpenBlob(jpeg_image_info,jpeg_image,WriteBinaryBlobMode,
10807 &image->exception);
10809 if (logging != MagickFalse)
10810 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10811 " Created jpeg_image, %.20g x %.20g.",(double) jpeg_image->columns,
10812 (double) jpeg_image->rows);
10814 if (jng_color_type == 8 || jng_color_type == 12)
10815 jpeg_image_info->type=GrayscaleType;
10817 jpeg_image_info->quality=jng_quality % 1000;
10818 (void) CopyMagickString(jpeg_image_info->magick,"JPEG",MaxTextExtent);
10819 (void) CopyMagickString(jpeg_image->magick,"JPEG",MaxTextExtent);
10821 if (logging != MagickFalse)
10822 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10823 " Creating blob.");
10825 blob=ImageToBlob(jpeg_image_info,jpeg_image,&length,&image->exception);
10827 if (logging != MagickFalse)
10829 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10830 " Successfully read jpeg_image into a blob, length=%.20g.",
10833 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10834 " Write JDAT chunk, length=%.20g.",(double) length);
10837 /* Write JDAT chunk(s) */
10838 (void) WriteBlobMSBULong(image,(size_t) length);
10839 PNGType(chunk,mng_JDAT);
10840 LogPNGChunk(logging,mng_JDAT,length);
10841 (void) WriteBlob(image,4,chunk);
10842 (void) WriteBlob(image,length,blob);
10843 (void) WriteBlobMSBULong(image,crc32(crc32(0,chunk,4),blob,(uInt) length));
10845 jpeg_image=DestroyImage(jpeg_image);
10846 (void) RelinquishUniqueFileResource(jpeg_image_info->filename);
10847 jpeg_image_info=DestroyImageInfo(jpeg_image_info);
10848 blob=(unsigned char *) RelinquishMagickMemory(blob);
10850 /* Write any JNG-chunk-e profiles */
10851 (void) Magick_png_write_chunk_from_profile(image,"JNG-chunk-e",logging);
10853 /* Write IEND chunk */
10854 (void) WriteBlobMSBULong(image,0L);
10855 PNGType(chunk,mng_IEND);
10856 LogPNGChunk(logging,mng_IEND,0);
10857 (void) WriteBlob(image,4,chunk);
10858 (void) WriteBlobMSBULong(image,crc32(0,chunk,4));
10860 if (logging != MagickFalse)
10861 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
10862 " exit WriteOneJNGImage()");
10869 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
10873 % W r i t e J N G I m a g e %
10877 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
10879 % WriteJNGImage() writes a JPEG Network Graphics (JNG) image file.
10881 % JNG support written by Glenn Randers-Pehrson, glennrp@image...
10883 % The format of the WriteJNGImage method is:
10885 % MagickBooleanType WriteJNGImage(const ImageInfo *image_info,Image *image)
10887 % A description of each parameter follows:
10889 % o image_info: the image info.
10891 % o image: The image.
10893 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
10895 static MagickBooleanType WriteJNGImage(const ImageInfo *image_info,Image *image)
10898 have_mng_structure,
10908 assert(image_info != (const ImageInfo *) NULL);
10909 assert(image_info->signature == MagickSignature);
10910 assert(image != (Image *) NULL);
10911 assert(image->signature == MagickSignature);
10912 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
10913 logging=LogMagickEvent(CoderEvent,GetMagickModule(),"Enter WriteJNGImage()");
10914 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
10915 if (status == MagickFalse)
10919 Allocate a MngInfo structure.
10921 have_mng_structure=MagickFalse;
10922 mng_info=(MngInfo *) AcquireMagickMemory(sizeof(MngInfo));
10923 if (mng_info == (MngInfo *) NULL)
10924 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
10926 Initialize members of the MngInfo structure.
10928 (void) ResetMagickMemory(mng_info,0,sizeof(MngInfo));
10929 mng_info->image=image;
10930 have_mng_structure=MagickTrue;
10932 (void) WriteBlob(image,8,(const unsigned char *) "\213JNG\r\n\032\n");
10934 status=WriteOneJNGImage(mng_info,image_info,image);
10935 (void) CloseBlob(image);
10937 (void) CatchImageException(image);
10938 MngInfoFreeStruct(mng_info,&have_mng_structure);
10939 if (logging != MagickFalse)
10940 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"exit WriteJNGImage()");
10947 static MagickBooleanType WriteMNGImage(const ImageInfo *image_info,Image *image)
10956 have_mng_structure,
10959 volatile MagickBooleanType
10971 #if defined(PNG_WRITE_EMPTY_PLTE_SUPPORTED) || \
10972 defined(PNG_MNG_FEATURES_SUPPORTED)
10975 all_images_are_gray,
10985 volatile unsigned int
10996 #if (PNG_LIBPNG_VER < 10200)
10997 if (image_info->verbose)
10998 printf("Your PNG library (libpng-%s) is rather old.\n",
10999 PNG_LIBPNG_VER_STRING);
11005 assert(image_info != (const ImageInfo *) NULL);
11006 assert(image_info->signature == MagickSignature);
11007 assert(image != (Image *) NULL);
11008 assert(image->signature == MagickSignature);
11009 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
11010 logging=LogMagickEvent(CoderEvent,GetMagickModule(),"Enter WriteMNGImage()");
11011 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
11012 if (status == MagickFalse)
11016 Allocate a MngInfo structure.
11018 have_mng_structure=MagickFalse;
11019 mng_info=(MngInfo *) AcquireMagickMemory(sizeof(MngInfo));
11020 if (mng_info == (MngInfo *) NULL)
11021 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
11023 Initialize members of the MngInfo structure.
11025 (void) ResetMagickMemory(mng_info,0,sizeof(MngInfo));
11026 mng_info->image=image;
11027 have_mng_structure=MagickTrue;
11028 write_mng=LocaleCompare(image_info->magick,"MNG") == 0;
11031 * See if user has requested a specific PNG subformat to be used
11032 * for all of the PNGs in the MNG being written, e.g.,
11034 * convert *.png png8:animation.mng
11036 * To do: check -define png:bit_depth and png:color_type as well,
11037 * or perhaps use mng:bit_depth and mng:color_type instead for
11041 mng_info->write_png8=LocaleCompare(image_info->magick,"PNG8") == 0;
11042 mng_info->write_png24=LocaleCompare(image_info->magick,"PNG24") == 0;
11043 mng_info->write_png32=LocaleCompare(image_info->magick,"PNG32") == 0;
11045 write_jng=MagickFalse;
11046 if (image_info->compression == JPEGCompression)
11047 write_jng=MagickTrue;
11049 mng_info->adjoin=image_info->adjoin &&
11050 (GetNextImageInList(image) != (Image *) NULL) && write_mng;
11052 if (logging != MagickFalse)
11054 /* Log some info about the input */
11058 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
11059 " Checking input image(s)");
11061 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
11062 " Image_info depth: %.20g",(double) image_info->depth);
11064 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
11065 " Type: %d",image_info->type);
11068 for (p=image; p != (Image *) NULL; p=GetNextImageInList(p))
11070 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
11071 " Scene: %.20g",(double) scene++);
11073 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
11074 " Image depth: %.20g",(double) p->depth);
11077 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
11081 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
11084 if (p->storage_class == PseudoClass)
11085 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
11086 " Storage class: PseudoClass");
11089 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
11090 " Storage class: DirectClass");
11093 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
11094 " Number of colors: %.20g",(double) p->colors);
11097 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
11098 " Number of colors: unspecified");
11100 if (mng_info->adjoin == MagickFalse)
11105 use_global_plte=MagickFalse;
11106 all_images_are_gray=MagickFalse;
11107 #ifdef PNG_WRITE_EMPTY_PLTE_SUPPORTED
11108 need_local_plte=MagickTrue;
11110 need_defi=MagickFalse;
11111 need_matte=MagickFalse;
11112 mng_info->framing_mode=1;
11113 mng_info->old_framing_mode=1;
11116 if (image_info->page != (char *) NULL)
11119 Determine image bounding box.
11121 SetGeometry(image,&mng_info->page);
11122 (void) ParseMetaGeometry(image_info->page,&mng_info->page.x,
11123 &mng_info->page.y,&mng_info->page.width,&mng_info->page.height);
11135 mng_info->page=image->page;
11136 need_geom=MagickTrue;
11137 if (mng_info->page.width || mng_info->page.height)
11138 need_geom=MagickFalse;
11140 Check all the scenes.
11142 initial_delay=image->delay;
11143 need_iterations=MagickFalse;
11144 mng_info->equal_chrms=image->chromaticity.red_primary.x != 0.0;
11145 mng_info->equal_physs=MagickTrue,
11146 mng_info->equal_gammas=MagickTrue;
11147 mng_info->equal_srgbs=MagickTrue;
11148 mng_info->equal_backgrounds=MagickTrue;
11150 #if defined(PNG_WRITE_EMPTY_PLTE_SUPPORTED) || \
11151 defined(PNG_MNG_FEATURES_SUPPORTED)
11152 all_images_are_gray=MagickTrue;
11153 mng_info->equal_palettes=MagickFalse;
11154 need_local_plte=MagickFalse;
11156 for (next_image=image; next_image != (Image *) NULL; )
11160 if ((next_image->columns+next_image->page.x) > mng_info->page.width)
11161 mng_info->page.width=next_image->columns+next_image->page.x;
11163 if ((next_image->rows+next_image->page.y) > mng_info->page.height)
11164 mng_info->page.height=next_image->rows+next_image->page.y;
11167 if (next_image->page.x || next_image->page.y)
11168 need_defi=MagickTrue;
11170 if (next_image->matte)
11171 need_matte=MagickTrue;
11173 if ((int) next_image->dispose >= BackgroundDispose)
11174 if (next_image->matte || next_image->page.x || next_image->page.y ||
11175 ((next_image->columns < mng_info->page.width) &&
11176 (next_image->rows < mng_info->page.height)))
11177 mng_info->need_fram=MagickTrue;
11179 if (next_image->iterations)
11180 need_iterations=MagickTrue;
11182 final_delay=next_image->delay;
11184 if (final_delay != initial_delay || final_delay > 1UL*
11185 next_image->ticks_per_second)
11186 mng_info->need_fram=1;
11188 #if defined(PNG_WRITE_EMPTY_PLTE_SUPPORTED) || \
11189 defined(PNG_MNG_FEATURES_SUPPORTED)
11191 check for global palette possibility.
11193 if (image->matte != MagickFalse)
11194 need_local_plte=MagickTrue;
11196 if (need_local_plte == 0)
11198 if (ImageIsGray(image) == MagickFalse)
11199 all_images_are_gray=MagickFalse;
11200 mng_info->equal_palettes=PalettesAreEqual(image,next_image);
11201 if (use_global_plte == 0)
11202 use_global_plte=mng_info->equal_palettes;
11203 need_local_plte=!mng_info->equal_palettes;
11206 if (GetNextImageInList(next_image) != (Image *) NULL)
11208 if (next_image->background_color.red !=
11209 next_image->next->background_color.red ||
11210 next_image->background_color.green !=
11211 next_image->next->background_color.green ||
11212 next_image->background_color.blue !=
11213 next_image->next->background_color.blue)
11214 mng_info->equal_backgrounds=MagickFalse;
11216 if (next_image->gamma != next_image->next->gamma)
11217 mng_info->equal_gammas=MagickFalse;
11219 if (next_image->rendering_intent !=
11220 next_image->next->rendering_intent)
11221 mng_info->equal_srgbs=MagickFalse;
11223 if ((next_image->units != next_image->next->units) ||
11224 (next_image->x_resolution != next_image->next->x_resolution) ||
11225 (next_image->y_resolution != next_image->next->y_resolution))
11226 mng_info->equal_physs=MagickFalse;
11228 if (mng_info->equal_chrms)
11230 if (next_image->chromaticity.red_primary.x !=
11231 next_image->next->chromaticity.red_primary.x ||
11232 next_image->chromaticity.red_primary.y !=
11233 next_image->next->chromaticity.red_primary.y ||
11234 next_image->chromaticity.green_primary.x !=
11235 next_image->next->chromaticity.green_primary.x ||
11236 next_image->chromaticity.green_primary.y !=
11237 next_image->next->chromaticity.green_primary.y ||
11238 next_image->chromaticity.blue_primary.x !=
11239 next_image->next->chromaticity.blue_primary.x ||
11240 next_image->chromaticity.blue_primary.y !=
11241 next_image->next->chromaticity.blue_primary.y ||
11242 next_image->chromaticity.white_point.x !=
11243 next_image->next->chromaticity.white_point.x ||
11244 next_image->chromaticity.white_point.y !=
11245 next_image->next->chromaticity.white_point.y)
11246 mng_info->equal_chrms=MagickFalse;
11250 next_image=GetNextImageInList(next_image);
11252 if (image_count < 2)
11254 mng_info->equal_backgrounds=MagickFalse;
11255 mng_info->equal_chrms=MagickFalse;
11256 mng_info->equal_gammas=MagickFalse;
11257 mng_info->equal_srgbs=MagickFalse;
11258 mng_info->equal_physs=MagickFalse;
11259 use_global_plte=MagickFalse;
11260 #ifdef PNG_WRITE_EMPTY_PLTE_SUPPORTED
11261 need_local_plte=MagickTrue;
11263 need_iterations=MagickFalse;
11266 if (mng_info->need_fram == MagickFalse)
11269 Only certain framing rates 100/n are exactly representable without
11270 the FRAM chunk but we'll allow some slop in VLC files
11272 if (final_delay == 0)
11274 if (need_iterations != MagickFalse)
11277 It's probably a GIF with loop; don't run it *too* fast.
11279 if (mng_info->adjoin)
11282 (void) ThrowMagickException(&image->exception,
11283 GetMagickModule(),CoderWarning,
11284 "input has zero delay between all frames; assuming",
11289 mng_info->ticks_per_second=0;
11291 if (final_delay != 0)
11292 mng_info->ticks_per_second=(png_uint_32)
11293 (image->ticks_per_second/final_delay);
11294 if (final_delay > 50)
11295 mng_info->ticks_per_second=2;
11297 if (final_delay > 75)
11298 mng_info->ticks_per_second=1;
11300 if (final_delay > 125)
11301 mng_info->need_fram=MagickTrue;
11303 if (need_defi && final_delay > 2 && (final_delay != 4) &&
11304 (final_delay != 5) && (final_delay != 10) && (final_delay != 20) &&
11305 (final_delay != 25) && (final_delay != 50) && (final_delay !=
11306 1UL*image->ticks_per_second))
11307 mng_info->need_fram=MagickTrue; /* make it exact; cannot be VLC */
11310 if (mng_info->need_fram != MagickFalse)
11311 mng_info->ticks_per_second=1UL*image->ticks_per_second;
11313 If pseudocolor, we should also check to see if all the
11314 palettes are identical and write a global PLTE if they are.
11318 Write the MNG version 1.0 signature and MHDR chunk.
11320 (void) WriteBlob(image,8,(const unsigned char *) "\212MNG\r\n\032\n");
11321 (void) WriteBlobMSBULong(image,28L); /* chunk data length=28 */
11322 PNGType(chunk,mng_MHDR);
11323 LogPNGChunk(logging,mng_MHDR,28L);
11324 PNGLong(chunk+4,(png_uint_32) mng_info->page.width);
11325 PNGLong(chunk+8,(png_uint_32) mng_info->page.height);
11326 PNGLong(chunk+12,mng_info->ticks_per_second);
11327 PNGLong(chunk+16,0L); /* layer count=unknown */
11328 PNGLong(chunk+20,0L); /* frame count=unknown */
11329 PNGLong(chunk+24,0L); /* play time=unknown */
11334 if (need_defi || mng_info->need_fram || use_global_plte)
11335 PNGLong(chunk+28,27L); /* simplicity=LC+JNG */
11338 PNGLong(chunk+28,25L); /* simplicity=VLC+JNG */
11343 if (need_defi || mng_info->need_fram || use_global_plte)
11344 PNGLong(chunk+28,19L); /* simplicity=LC+JNG, no transparency */
11347 PNGLong(chunk+28,17L); /* simplicity=VLC+JNG, no transparency */
11355 if (need_defi || mng_info->need_fram || use_global_plte)
11356 PNGLong(chunk+28,11L); /* simplicity=LC */
11359 PNGLong(chunk+28,9L); /* simplicity=VLC */
11364 if (need_defi || mng_info->need_fram || use_global_plte)
11365 PNGLong(chunk+28,3L); /* simplicity=LC, no transparency */
11368 PNGLong(chunk+28,1L); /* simplicity=VLC, no transparency */
11371 (void) WriteBlob(image,32,chunk);
11372 (void) WriteBlobMSBULong(image,crc32(0,chunk,32));
11373 option=GetImageOption(image_info,"mng:need-cacheoff");
11374 if (option != (const char *) NULL)
11380 Write "nEED CACHEOFF" to turn playback caching off for streaming MNG.
11382 PNGType(chunk,mng_nEED);
11383 length=CopyMagickString((char *) chunk+4,"CACHEOFF",20);
11384 (void) WriteBlobMSBULong(image,(size_t) length);
11385 LogPNGChunk(logging,mng_nEED,(size_t) length);
11387 (void) WriteBlob(image,length,chunk);
11388 (void) WriteBlobMSBULong(image,crc32(0,chunk,(uInt) length));
11390 if ((GetPreviousImageInList(image) == (Image *) NULL) &&
11391 (GetNextImageInList(image) != (Image *) NULL) &&
11392 (image->iterations != 1))
11395 Write MNG TERM chunk
11397 (void) WriteBlobMSBULong(image,10L); /* data length=10 */
11398 PNGType(chunk,mng_TERM);
11399 LogPNGChunk(logging,mng_TERM,10L);
11400 chunk[4]=3; /* repeat animation */
11401 chunk[5]=0; /* show last frame when done */
11402 PNGLong(chunk+6,(png_uint_32) (mng_info->ticks_per_second*
11403 final_delay/MagickMax(image->ticks_per_second,1)));
11405 if (image->iterations == 0)
11406 PNGLong(chunk+10,PNG_UINT_31_MAX);
11409 PNGLong(chunk+10,(png_uint_32) image->iterations);
11411 if (logging != MagickFalse)
11413 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
11414 " TERM delay: %.20g",(double) (mng_info->ticks_per_second*
11415 final_delay/MagickMax(image->ticks_per_second,1)));
11417 if (image->iterations == 0)
11418 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
11419 " TERM iterations: %.20g",(double) PNG_UINT_31_MAX);
11422 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
11423 " Image iterations: %.20g",(double) image->iterations);
11425 (void) WriteBlob(image,14,chunk);
11426 (void) WriteBlobMSBULong(image,crc32(0,chunk,14));
11429 To do: check for cHRM+gAMA == sRGB, and write sRGB instead.
11431 if ((image->colorspace == sRGBColorspace || image->rendering_intent) &&
11432 mng_info->equal_srgbs)
11435 Write MNG sRGB chunk
11437 (void) WriteBlobMSBULong(image,1L);
11438 PNGType(chunk,mng_sRGB);
11439 LogPNGChunk(logging,mng_sRGB,1L);
11441 if (image->rendering_intent != UndefinedIntent)
11442 chunk[4]=(unsigned char)
11443 Magick_RenderingIntent_to_PNG_RenderingIntent(
11444 (image->rendering_intent));
11447 chunk[4]=(unsigned char)
11448 Magick_RenderingIntent_to_PNG_RenderingIntent(
11449 (PerceptualIntent));
11451 (void) WriteBlob(image,5,chunk);
11452 (void) WriteBlobMSBULong(image,crc32(0,chunk,5));
11453 mng_info->have_write_global_srgb=MagickTrue;
11458 if (image->gamma && mng_info->equal_gammas)
11461 Write MNG gAMA chunk
11463 (void) WriteBlobMSBULong(image,4L);
11464 PNGType(chunk,mng_gAMA);
11465 LogPNGChunk(logging,mng_gAMA,4L);
11466 PNGLong(chunk+4,(png_uint_32) (100000*image->gamma+0.5));
11467 (void) WriteBlob(image,8,chunk);
11468 (void) WriteBlobMSBULong(image,crc32(0,chunk,8));
11469 mng_info->have_write_global_gama=MagickTrue;
11471 if (mng_info->equal_chrms)
11477 Write MNG cHRM chunk
11479 (void) WriteBlobMSBULong(image,32L);
11480 PNGType(chunk,mng_cHRM);
11481 LogPNGChunk(logging,mng_cHRM,32L);
11482 primary=image->chromaticity.white_point;
11483 PNGLong(chunk+4,(png_uint_32) (100000*primary.x+0.5));
11484 PNGLong(chunk+8,(png_uint_32) (100000*primary.y+0.5));
11485 primary=image->chromaticity.red_primary;
11486 PNGLong(chunk+12,(png_uint_32) (100000*primary.x+0.5));
11487 PNGLong(chunk+16,(png_uint_32) (100000*primary.y+0.5));
11488 primary=image->chromaticity.green_primary;
11489 PNGLong(chunk+20,(png_uint_32) (100000*primary.x+0.5));
11490 PNGLong(chunk+24,(png_uint_32) (100000*primary.y+0.5));
11491 primary=image->chromaticity.blue_primary;
11492 PNGLong(chunk+28,(png_uint_32) (100000*primary.x+0.5));
11493 PNGLong(chunk+32,(png_uint_32) (100000*primary.y+0.5));
11494 (void) WriteBlob(image,36,chunk);
11495 (void) WriteBlobMSBULong(image,crc32(0,chunk,36));
11496 mng_info->have_write_global_chrm=MagickTrue;
11499 if (image->x_resolution && image->y_resolution && mng_info->equal_physs)
11502 Write MNG pHYs chunk
11504 (void) WriteBlobMSBULong(image,9L);
11505 PNGType(chunk,mng_pHYs);
11506 LogPNGChunk(logging,mng_pHYs,9L);
11508 if (image->units == PixelsPerInchResolution)
11510 PNGLong(chunk+4,(png_uint_32)
11511 (image->x_resolution*100.0/2.54+0.5));
11513 PNGLong(chunk+8,(png_uint_32)
11514 (image->y_resolution*100.0/2.54+0.5));
11521 if (image->units == PixelsPerCentimeterResolution)
11523 PNGLong(chunk+4,(png_uint_32)
11524 (image->x_resolution*100.0+0.5));
11526 PNGLong(chunk+8,(png_uint_32)
11527 (image->y_resolution*100.0+0.5));
11534 PNGLong(chunk+4,(png_uint_32) (image->x_resolution+0.5));
11535 PNGLong(chunk+8,(png_uint_32) (image->y_resolution+0.5));
11539 (void) WriteBlob(image,13,chunk);
11540 (void) WriteBlobMSBULong(image,crc32(0,chunk,13));
11543 Write MNG BACK chunk and global bKGD chunk, if the image is transparent
11544 or does not cover the entire frame.
11546 if (write_mng && (image->matte || image->page.x > 0 ||
11547 image->page.y > 0 || (image->page.width &&
11548 (image->page.width+image->page.x < mng_info->page.width))
11549 || (image->page.height && (image->page.height+image->page.y
11550 < mng_info->page.height))))
11552 (void) WriteBlobMSBULong(image,6L);
11553 PNGType(chunk,mng_BACK);
11554 LogPNGChunk(logging,mng_BACK,6L);
11555 red=ScaleQuantumToShort(image->background_color.red);
11556 green=ScaleQuantumToShort(image->background_color.green);
11557 blue=ScaleQuantumToShort(image->background_color.blue);
11558 PNGShort(chunk+4,red);
11559 PNGShort(chunk+6,green);
11560 PNGShort(chunk+8,blue);
11561 (void) WriteBlob(image,10,chunk);
11562 (void) WriteBlobMSBULong(image,crc32(0,chunk,10));
11563 if (mng_info->equal_backgrounds)
11565 (void) WriteBlobMSBULong(image,6L);
11566 PNGType(chunk,mng_bKGD);
11567 LogPNGChunk(logging,mng_bKGD,6L);
11568 (void) WriteBlob(image,10,chunk);
11569 (void) WriteBlobMSBULong(image,crc32(0,chunk,10));
11573 #ifdef PNG_WRITE_EMPTY_PLTE_SUPPORTED
11574 if ((need_local_plte == MagickFalse) &&
11575 (image->storage_class == PseudoClass) &&
11576 (all_images_are_gray == MagickFalse))
11582 Write MNG PLTE chunk
11584 data_length=3*image->colors;
11585 (void) WriteBlobMSBULong(image,data_length);
11586 PNGType(chunk,mng_PLTE);
11587 LogPNGChunk(logging,mng_PLTE,data_length);
11589 for (i=0; i < (ssize_t) image->colors; i++)
11591 chunk[4+i*3]=ScaleQuantumToChar(image->colormap[i].red) & 0xff;
11592 chunk[5+i*3]=ScaleQuantumToChar(image->colormap[i].green) & 0xff;
11593 chunk[6+i*3]=ScaleQuantumToChar(image->colormap[i].blue) & 0xff;
11596 (void) WriteBlob(image,data_length+4,chunk);
11597 (void) WriteBlobMSBULong(image,crc32(0,chunk,(uInt) (data_length+4)));
11598 mng_info->have_write_global_plte=MagickTrue;
11604 #if defined(PNG_WRITE_EMPTY_PLTE_SUPPORTED) || \
11605 defined(PNG_MNG_FEATURES_SUPPORTED)
11606 mng_info->equal_palettes=MagickFalse;
11610 if (mng_info->adjoin)
11612 #if defined(PNG_WRITE_EMPTY_PLTE_SUPPORTED) || \
11613 defined(PNG_MNG_FEATURES_SUPPORTED)
11615 If we aren't using a global palette for the entire MNG, check to
11616 see if we can use one for two or more consecutive images.
11618 if (need_local_plte && use_global_plte && !all_images_are_gray)
11620 if (mng_info->IsPalette)
11623 When equal_palettes is true, this image has the same palette
11624 as the previous PseudoClass image
11626 mng_info->have_write_global_plte=mng_info->equal_palettes;
11627 mng_info->equal_palettes=PalettesAreEqual(image,image->next);
11628 if (mng_info->equal_palettes && !mng_info->have_write_global_plte)
11631 Write MNG PLTE chunk
11636 data_length=3*image->colors;
11637 (void) WriteBlobMSBULong(image,data_length);
11638 PNGType(chunk,mng_PLTE);
11639 LogPNGChunk(logging,mng_PLTE,data_length);
11641 for (i=0; i < (ssize_t) image->colors; i++)
11643 chunk[4+i*3]=ScaleQuantumToChar(image->colormap[i].red);
11644 chunk[5+i*3]=ScaleQuantumToChar(image->colormap[i].green);
11645 chunk[6+i*3]=ScaleQuantumToChar(image->colormap[i].blue);
11648 (void) WriteBlob(image,data_length+4,chunk);
11649 (void) WriteBlobMSBULong(image,crc32(0,chunk,
11650 (uInt) (data_length+4)));
11651 mng_info->have_write_global_plte=MagickTrue;
11655 mng_info->have_write_global_plte=MagickFalse;
11666 previous_x=mng_info->page.x;
11667 previous_y=mng_info->page.y;
11674 mng_info->page=image->page;
11675 if ((mng_info->page.x != previous_x) ||
11676 (mng_info->page.y != previous_y))
11678 (void) WriteBlobMSBULong(image,12L); /* data length=12 */
11679 PNGType(chunk,mng_DEFI);
11680 LogPNGChunk(logging,mng_DEFI,12L);
11681 chunk[4]=0; /* object 0 MSB */
11682 chunk[5]=0; /* object 0 LSB */
11683 chunk[6]=0; /* visible */
11684 chunk[7]=0; /* abstract */
11685 PNGLong(chunk+8,(png_uint_32) mng_info->page.x);
11686 PNGLong(chunk+12,(png_uint_32) mng_info->page.y);
11687 (void) WriteBlob(image,16,chunk);
11688 (void) WriteBlobMSBULong(image,crc32(0,chunk,16));
11693 mng_info->write_mng=write_mng;
11695 if ((int) image->dispose >= 3)
11696 mng_info->framing_mode=3;
11698 if (mng_info->need_fram && mng_info->adjoin &&
11699 ((image->delay != mng_info->delay) ||
11700 (mng_info->framing_mode != mng_info->old_framing_mode)))
11702 if (image->delay == mng_info->delay)
11705 Write a MNG FRAM chunk with the new framing mode.
11707 (void) WriteBlobMSBULong(image,1L); /* data length=1 */
11708 PNGType(chunk,mng_FRAM);
11709 LogPNGChunk(logging,mng_FRAM,1L);
11710 chunk[4]=(unsigned char) mng_info->framing_mode;
11711 (void) WriteBlob(image,5,chunk);
11712 (void) WriteBlobMSBULong(image,crc32(0,chunk,5));
11717 Write a MNG FRAM chunk with the delay.
11719 (void) WriteBlobMSBULong(image,10L); /* data length=10 */
11720 PNGType(chunk,mng_FRAM);
11721 LogPNGChunk(logging,mng_FRAM,10L);
11722 chunk[4]=(unsigned char) mng_info->framing_mode;
11723 chunk[5]=0; /* frame name separator (no name) */
11724 chunk[6]=2; /* flag for changing default delay */
11725 chunk[7]=0; /* flag for changing frame timeout */
11726 chunk[8]=0; /* flag for changing frame clipping */
11727 chunk[9]=0; /* flag for changing frame sync_id */
11728 PNGLong(chunk+10,(png_uint_32)
11729 ((mng_info->ticks_per_second*
11730 image->delay)/MagickMax(image->ticks_per_second,1)));
11731 (void) WriteBlob(image,14,chunk);
11732 (void) WriteBlobMSBULong(image,crc32(0,chunk,14));
11733 mng_info->delay=(png_uint_32) image->delay;
11735 mng_info->old_framing_mode=mng_info->framing_mode;
11738 #if defined(JNG_SUPPORTED)
11739 if (image_info->compression == JPEGCompression)
11744 if (logging != MagickFalse)
11745 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
11746 " Writing JNG object.");
11747 /* To do: specify the desired alpha compression method. */
11748 write_info=CloneImageInfo(image_info);
11749 write_info->compression=UndefinedCompression;
11750 status=WriteOneJNGImage(mng_info,write_info,image);
11751 write_info=DestroyImageInfo(write_info);
11756 if (logging != MagickFalse)
11757 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
11758 " Writing PNG object.");
11760 mng_info->need_blob = MagickFalse;
11762 /* We don't want any ancillary chunks written */
11763 mng_info->ping_exclude_bKGD=MagickTrue;
11764 mng_info->ping_exclude_cHRM=MagickTrue;
11765 mng_info->ping_exclude_EXIF=MagickTrue;
11766 mng_info->ping_exclude_gAMA=MagickTrue;
11767 mng_info->ping_exclude_cHRM=MagickTrue;
11768 mng_info->ping_exclude_iCCP=MagickTrue;
11769 /* mng_info->ping_exclude_iTXt=MagickTrue; */
11770 mng_info->ping_exclude_oFFs=MagickTrue;
11771 mng_info->ping_exclude_pHYs=MagickTrue;
11772 mng_info->ping_exclude_sRGB=MagickTrue;
11773 mng_info->ping_exclude_tEXt=MagickTrue;
11774 mng_info->ping_exclude_tRNS=MagickTrue;
11775 mng_info->ping_exclude_vpAg=MagickTrue;
11776 mng_info->ping_exclude_zCCP=MagickTrue;
11777 mng_info->ping_exclude_zTXt=MagickTrue;
11779 status=WriteOnePNGImage(mng_info,image_info,image);
11782 if (status == MagickFalse)
11784 MngInfoFreeStruct(mng_info,&have_mng_structure);
11785 (void) CloseBlob(image);
11786 return(MagickFalse);
11788 (void) CatchImageException(image);
11789 if (GetNextImageInList(image) == (Image *) NULL)
11791 image=SyncNextImageInList(image);
11792 status=SetImageProgress(image,SaveImagesTag,scene++,
11793 GetImageListLength(image));
11795 if (status == MagickFalse)
11798 } while (mng_info->adjoin);
11802 while (GetPreviousImageInList(image) != (Image *) NULL)
11803 image=GetPreviousImageInList(image);
11805 Write the MEND chunk.
11807 (void) WriteBlobMSBULong(image,0x00000000L);
11808 PNGType(chunk,mng_MEND);
11809 LogPNGChunk(logging,mng_MEND,0L);
11810 (void) WriteBlob(image,4,chunk);
11811 (void) WriteBlobMSBULong(image,crc32(0,chunk,4));
11814 Relinquish resources.
11816 (void) CloseBlob(image);
11817 MngInfoFreeStruct(mng_info,&have_mng_structure);
11819 if (logging != MagickFalse)
11820 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"exit WriteMNGImage()");
11822 return(MagickTrue);
11824 #else /* PNG_LIBPNG_VER > 10011 */
11826 static MagickBooleanType WritePNGImage(const ImageInfo *image_info,Image *image)
11829 printf("Your PNG library is too old: You have libpng-%s\n",
11830 PNG_LIBPNG_VER_STRING);
11832 ThrowBinaryException(CoderError,"PNG library is too old",
11833 image_info->filename);
11836 static MagickBooleanType WriteMNGImage(const ImageInfo *image_info,Image *image)
11838 return(WritePNGImage(image_info,image));
11840 #endif /* PNG_LIBPNG_VER > 10011 */