• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %                                                                             %
4 %                                                                             %
5 %                                                                             %
6 %     CCCC   OOO   N   N  SSSSS  TTTTT  IIIII  TTTTT  U   U  TTTTT  EEEEE     %
7 %    C      O   O  NN  N  SS       T      I      T    U   U    T    E         %
8 %    C      O   O  N N N  ESSS     T      I      T    U   U    T    EEE       %
9 %    C      O   O  N  NN     SS    T      I      T    U   U    T    E         %
10 %     CCCC   OOO   N   N  SSSSS    T    IIIII    T     UUU     T    EEEEE     %
11 %                                                                             %
12 %                                                                             %
13 %                  MagickCore Methods to Consitute an Image                   %
14 %                                                                             %
15 %                             Software Design                                 %
16 %                                  Cristy                                     %
17 %                               October 1998                                  %
18 %                                                                             %
19 %                                                                             %
20 %  Copyright 1999-2016 ImageMagick Studio LLC, a non-profit organization      %
21 %  dedicated to making software imaging solutions freely available.           %
22 %                                                                             %
23 %  You may not use this file except in compliance with the License.  You may  %
24 %  obtain a copy of the License at                                            %
25 %                                                                             %
26 %    http://www.imagemagick.org/script/license.php                            %
27 %                                                                             %
28 %  Unless required by applicable law or agreed to in writing, software        %
29 %  distributed under the License is distributed on an "AS IS" BASIS,          %
30 %  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   %
31 %  See the License for the specific language governing permissions and        %
32 %  limitations under the License.                                             %
33 %                                                                             %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35 %
36 %
37 */
38 
39 /*
40   Include declarations.
41 */
42 #include "MagickCore/studio.h"
43 #include "MagickCore/attribute.h"
44 #include "MagickCore/blob.h"
45 #include "MagickCore/blob-private.h"
46 #include "MagickCore/exception.h"
47 #include "MagickCore/exception-private.h"
48 #include "MagickCore/cache.h"
49 #include "MagickCore/client.h"
50 #include "MagickCore/colorspace-private.h"
51 #include "MagickCore/constitute.h"
52 #include "MagickCore/constitute-private.h"
53 #include "MagickCore/delegate.h"
54 #include "MagickCore/geometry.h"
55 #include "MagickCore/identify.h"
56 #include "MagickCore/image-private.h"
57 #include "MagickCore/list.h"
58 #include "MagickCore/magick.h"
59 #include "MagickCore/memory_.h"
60 #include "MagickCore/monitor.h"
61 #include "MagickCore/monitor-private.h"
62 #include "MagickCore/option.h"
63 #include "MagickCore/pixel.h"
64 #include "MagickCore/pixel-accessor.h"
65 #include "MagickCore/policy.h"
66 #include "MagickCore/profile.h"
67 #include "MagickCore/profile-private.h"
68 #include "MagickCore/property.h"
69 #include "MagickCore/quantum.h"
70 #include "MagickCore/resize.h"
71 #include "MagickCore/resource_.h"
72 #include "MagickCore/semaphore.h"
73 #include "MagickCore/statistic.h"
74 #include "MagickCore/stream.h"
75 #include "MagickCore/string_.h"
76 #include "MagickCore/string-private.h"
77 #include "MagickCore/timer.h"
78 #include "MagickCore/token.h"
79 #include "MagickCore/transform.h"
80 #include "MagickCore/utility.h"
81 #include "MagickCore/utility-private.h"
82 
83 /*
84 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
85 %                                                                             %
86 %                                                                             %
87 %                                                                             %
88 %   C o n s t i t u t e I m a g e                                             %
89 %                                                                             %
90 %                                                                             %
91 %                                                                             %
92 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
93 %
94 %  ConstituteImage() returns an image from the pixel data you supply.
95 %  The pixel data must be in scanline order top-to-bottom.  The data can be
96 %  char, short int, int, float, or double.  Float and double require the
97 %  pixels to be normalized [0..1], otherwise [0..QuantumRange].  For example, to
98 %  create a 640x480 image from unsigned red-green-blue character data, use:
99 %
100 %      image = ConstituteImage(640,480,"RGB",CharPixel,pixels,&exception);
101 %
102 %  The format of the ConstituteImage method is:
103 %
104 %      Image *ConstituteImage(const size_t columns,const size_t rows,
105 %        const char *map,const StorageType storage,const void *pixels,
106 %        ExceptionInfo *exception)
107 %
108 %  A description of each parameter follows:
109 %
110 %    o columns: width in pixels of the image.
111 %
112 %    o rows: height in pixels of the image.
113 %
114 %    o map:  This string reflects the expected ordering of the pixel array.
115 %      It can be any combination or order of R = red, G = green, B = blue,
116 %      A = alpha (0 is transparent), O = opacity (0 is opaque), C = cyan,
117 %      Y = yellow, M = magenta, K = black, I = intensity (for grayscale),
118 %      P = pad.
119 %
120 %    o storage: Define the data type of the pixels.  Float and double types are
121 %      expected to be normalized [0..1] otherwise [0..QuantumRange].  Choose
122 %      from these types: CharPixel, DoublePixel, FloatPixel, IntegerPixel,
123 %      LongPixel, QuantumPixel, or ShortPixel.
124 %
125 %    o pixels: This array of values contain the pixel components as defined by
126 %      map and type.  You must preallocate this array where the expected
127 %      length varies depending on the values of width, height, map, and type.
128 %
129 %    o exception: return any errors or warnings in this structure.
130 %
131 */
ConstituteImage(const size_t columns,const size_t rows,const char * map,const StorageType storage,const void * pixels,ExceptionInfo * exception)132 MagickExport Image *ConstituteImage(const size_t columns,const size_t rows,
133   const char *map,const StorageType storage,const void *pixels,
134   ExceptionInfo *exception)
135 {
136   Image
137     *image;
138 
139   MagickBooleanType
140     status;
141 
142   /*
143     Allocate image structure.
144   */
145   assert(map != (const char *) NULL);
146   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",map);
147   assert(pixels != (void *) NULL);
148   assert(exception != (ExceptionInfo *) NULL);
149   assert(exception->signature == MagickCoreSignature);
150   image=AcquireImage((ImageInfo *) NULL,exception);
151   if (image == (Image *) NULL)
152     return((Image *) NULL);
153   if ((columns == 0) || (rows == 0))
154     ThrowImageException(OptionError,"NonZeroWidthAndHeightRequired");
155   image->columns=columns;
156   image->rows=rows;
157   (void) SetImageBackgroundColor(image,exception);
158   status=ImportImagePixels(image,0,0,columns,rows,map,storage,pixels,exception);
159   if (status == MagickFalse)
160      image=DestroyImage(image);
161   return(image);
162 }
163 
164 /*
165 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
166 %                                                                             %
167 %                                                                             %
168 %                                                                             %
169 %   P i n g I m a g e                                                         %
170 %                                                                             %
171 %                                                                             %
172 %                                                                             %
173 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
174 %
175 %  PingImage() returns all the properties of an image or image sequence
176 %  except for the pixels.  It is much faster and consumes far less memory
177 %  than ReadImage().  On failure, a NULL image is returned and exception
178 %  describes the reason for the failure.
179 %
180 %  The format of the PingImage method is:
181 %
182 %      Image *PingImage(const ImageInfo *image_info,ExceptionInfo *exception)
183 %
184 %  A description of each parameter follows:
185 %
186 %    o image_info: Ping the image defined by the file or filename members of
187 %      this structure.
188 %
189 %    o exception: return any errors or warnings in this structure.
190 %
191 */
192 
193 #if defined(__cplusplus) || defined(c_plusplus)
194 extern "C" {
195 #endif
196 
PingStream(const Image * magick_unused (image),const void * magick_unused (pixels),const size_t columns)197 static size_t PingStream(const Image *magick_unused(image),
198   const void *magick_unused(pixels),const size_t columns)
199 {
200   magick_unreferenced(image);
201   magick_unreferenced(pixels);
202   return(columns);
203 }
204 
205 #if defined(__cplusplus) || defined(c_plusplus)
206 }
207 #endif
208 
PingImage(const ImageInfo * image_info,ExceptionInfo * exception)209 MagickExport Image *PingImage(const ImageInfo *image_info,
210   ExceptionInfo *exception)
211 {
212   Image
213     *image;
214 
215   ImageInfo
216     *ping_info;
217 
218   assert(image_info != (ImageInfo *) NULL);
219   assert(image_info->signature == MagickCoreSignature);
220   if (image_info->debug != MagickFalse)
221     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
222       image_info->filename);
223   assert(exception != (ExceptionInfo *) NULL);
224   ping_info=CloneImageInfo(image_info);
225   ping_info->ping=MagickTrue;
226   image=ReadStream(ping_info,&PingStream,exception);
227   if (image != (Image *) NULL)
228     {
229       ResetTimer(&image->timer);
230       if (ping_info->verbose != MagickFalse)
231         (void) IdentifyImage(image,stdout,MagickFalse,exception);
232     }
233   ping_info=DestroyImageInfo(ping_info);
234   return(image);
235 }
236 
237 /*
238 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
239 %                                                                             %
240 %                                                                             %
241 %                                                                             %
242 %   P i n g I m a g e s                                                       %
243 %                                                                             %
244 %                                                                             %
245 %                                                                             %
246 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
247 %
248 %  PingImages() pings one or more images and returns them as an image list.
249 %
250 %  The format of the PingImage method is:
251 %
252 %      Image *PingImages(ImageInfo *image_info,const char *filename,
253 %        ExceptionInfo *exception)
254 %
255 %  A description of each parameter follows:
256 %
257 %    o image_info: the image info.
258 %
259 %    o filename: the image filename.
260 %
261 %    o exception: return any errors or warnings in this structure.
262 %
263 */
PingImages(ImageInfo * image_info,const char * filename,ExceptionInfo * exception)264 MagickExport Image *PingImages(ImageInfo *image_info,const char *filename,
265   ExceptionInfo *exception)
266 {
267   char
268     ping_filename[MagickPathExtent];
269 
270   Image
271     *image,
272     *images;
273 
274   ImageInfo
275     *read_info;
276 
277   /*
278     Ping image list from a file.
279   */
280   assert(image_info != (ImageInfo *) NULL);
281   assert(image_info->signature == MagickCoreSignature);
282   if (image_info->debug != MagickFalse)
283     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
284       image_info->filename);
285   assert(exception != (ExceptionInfo *) NULL);
286   (void) SetImageOption(image_info,"filename",filename);
287   (void) CopyMagickString(image_info->filename,filename,MagickPathExtent);
288   (void) InterpretImageFilename(image_info,(Image *) NULL,image_info->filename,
289     (int) image_info->scene,ping_filename,exception);
290   if (LocaleCompare(ping_filename,image_info->filename) != 0)
291     {
292       ExceptionInfo
293         *sans;
294 
295       ssize_t
296         extent,
297         scene;
298 
299       /*
300         Images of the form image-%d.png[1-5].
301       */
302       read_info=CloneImageInfo(image_info);
303       sans=AcquireExceptionInfo();
304       (void) SetImageInfo(read_info,0,sans);
305       sans=DestroyExceptionInfo(sans);
306       if (read_info->number_scenes == 0)
307         {
308           read_info=DestroyImageInfo(read_info);
309           return(PingImage(image_info,exception));
310         }
311       (void) CopyMagickString(ping_filename,read_info->filename,MagickPathExtent);
312       images=NewImageList();
313       extent=(ssize_t) (read_info->scene+read_info->number_scenes);
314       for (scene=(ssize_t) read_info->scene; scene < (ssize_t) extent; scene++)
315       {
316         (void) InterpretImageFilename(image_info,(Image *) NULL,ping_filename,
317           (int) scene,read_info->filename,exception);
318         image=PingImage(read_info,exception);
319         if (image == (Image *) NULL)
320           continue;
321         AppendImageToList(&images,image);
322       }
323       read_info=DestroyImageInfo(read_info);
324       return(images);
325     }
326   return(PingImage(image_info,exception));
327 }
328 
329 /*
330 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
331 %                                                                             %
332 %                                                                             %
333 %                                                                             %
334 %   R e a d I m a g e                                                         %
335 %                                                                             %
336 %                                                                             %
337 %                                                                             %
338 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
339 %
340 %  ReadImage() reads an image or image sequence from a file or file handle.
341 %  The method returns a NULL if there is a memory shortage or if the image
342 %  cannot be read.  On failure, a NULL image is returned and exception
343 %  describes the reason for the failure.
344 %
345 %  The format of the ReadImage method is:
346 %
347 %      Image *ReadImage(const ImageInfo *image_info,ExceptionInfo *exception)
348 %
349 %  A description of each parameter follows:
350 %
351 %    o image_info: Read the image defined by the file or filename members of
352 %      this structure.
353 %
354 %    o exception: return any errors or warnings in this structure.
355 %
356 */
ReadImage(const ImageInfo * image_info,ExceptionInfo * exception)357 MagickExport Image *ReadImage(const ImageInfo *image_info,
358   ExceptionInfo *exception)
359 {
360   char
361     filename[MagickPathExtent],
362     magick[MagickPathExtent],
363     magick_filename[MagickPathExtent];
364 
365   const char
366     *value;
367 
368   const DelegateInfo
369     *delegate_info;
370 
371   const MagickInfo
372     *magick_info;
373 
374   ExceptionInfo
375     *sans_exception;
376 
377   GeometryInfo
378     geometry_info;
379 
380   Image
381     *image,
382     *next;
383 
384   ImageInfo
385     *read_info;
386 
387   MagickStatusType
388     flags;
389 
390   PolicyDomain
391     domain;
392 
393   PolicyRights
394     rights;
395 
396   /*
397     Determine image type from filename prefix or suffix (e.g. image.jpg).
398   */
399   assert(image_info != (ImageInfo *) NULL);
400   assert(image_info->signature == MagickCoreSignature);
401   assert(image_info->filename != (char *) NULL);
402   if (image_info->debug != MagickFalse)
403     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
404       image_info->filename);
405   assert(exception != (ExceptionInfo *) NULL);
406   read_info=CloneImageInfo(image_info);
407   (void) CopyMagickString(magick_filename,read_info->filename,MagickPathExtent);
408   (void) SetImageInfo(read_info,0,exception);
409   (void) CopyMagickString(filename,read_info->filename,MagickPathExtent);
410   (void) CopyMagickString(magick,read_info->magick,MagickPathExtent);
411   domain=CoderPolicyDomain;
412   rights=ReadPolicyRights;
413   if (IsRightsAuthorized(domain,rights,read_info->magick) == MagickFalse)
414     {
415       errno=EPERM;
416       (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
417         "NotAuthorized","`%s'",read_info->filename);
418       read_info=DestroyImageInfo(read_info);
419       return((Image *) NULL);
420     }
421   /*
422     Call appropriate image reader based on image type.
423   */
424   sans_exception=AcquireExceptionInfo();
425   magick_info=GetMagickInfo(read_info->magick,sans_exception);
426   sans_exception=DestroyExceptionInfo(sans_exception);
427   if (magick_info != (const MagickInfo *) NULL)
428     {
429       if (GetMagickEndianSupport(magick_info) == MagickFalse)
430         read_info->endian=UndefinedEndian;
431       else
432         if ((image_info->endian == UndefinedEndian) &&
433             (GetMagickRawSupport(magick_info) != MagickFalse))
434           {
435             unsigned long
436               lsb_first;
437 
438             lsb_first=1;
439             read_info->endian=(*(char *) &lsb_first) == 1 ? LSBEndian :
440               MSBEndian;
441          }
442     }
443   if ((magick_info != (const MagickInfo *) NULL) &&
444       (GetMagickSeekableStream(magick_info) != MagickFalse))
445     {
446       MagickBooleanType
447         status;
448 
449       image=AcquireImage(read_info,exception);
450       (void) CopyMagickString(image->filename,read_info->filename,
451         MagickPathExtent);
452       status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
453       if (status == MagickFalse)
454         {
455           read_info=DestroyImageInfo(read_info);
456           image=DestroyImage(image);
457           return((Image *) NULL);
458         }
459       if (IsBlobSeekable(image) == MagickFalse)
460         {
461           /*
462             Coder requires a seekable stream.
463           */
464           *read_info->filename='\0';
465           status=ImageToFile(image,read_info->filename,exception);
466           if (status == MagickFalse)
467             {
468               (void) CloseBlob(image);
469               read_info=DestroyImageInfo(read_info);
470               image=DestroyImage(image);
471               return((Image *) NULL);
472             }
473           read_info->temporary=MagickTrue;
474         }
475       (void) CloseBlob(image);
476       image=DestroyImage(image);
477     }
478   image=NewImageList();
479   if ((magick_info == (const MagickInfo *) NULL) ||
480       (GetImageDecoder(magick_info) == (DecodeImageHandler *) NULL))
481     {
482       delegate_info=GetDelegateInfo(read_info->magick,(char *) NULL,exception);
483       if (delegate_info == (const DelegateInfo *) NULL)
484         {
485           (void) SetImageInfo(read_info,0,exception);
486           (void) CopyMagickString(read_info->filename,filename,
487             MagickPathExtent);
488           magick_info=GetMagickInfo(read_info->magick,exception);
489         }
490     }
491   if ((magick_info != (const MagickInfo *) NULL) &&
492       (GetImageDecoder(magick_info) != (DecodeImageHandler *) NULL))
493     {
494       if (GetMagickDecoderThreadSupport(magick_info) == MagickFalse)
495         LockSemaphoreInfo(magick_info->semaphore);
496       image=GetImageDecoder(magick_info)(read_info,exception);
497       if (GetMagickDecoderThreadSupport(magick_info) == MagickFalse)
498         UnlockSemaphoreInfo(magick_info->semaphore);
499     }
500   else
501     {
502       MagickBooleanType
503         status;
504 
505       delegate_info=GetDelegateInfo(read_info->magick,(char *) NULL,exception);
506       if (delegate_info == (const DelegateInfo *) NULL)
507         {
508           (void) ThrowMagickException(exception,GetMagickModule(),
509             MissingDelegateError,"NoDecodeDelegateForThisImageFormat","`%s'",
510             read_info->magick);
511           if (read_info->temporary != MagickFalse)
512             (void) RelinquishUniqueFileResource(read_info->filename);
513           read_info=DestroyImageInfo(read_info);
514           return((Image *) NULL);
515         }
516       /*
517         Let our decoding delegate process the image.
518       */
519       image=AcquireImage(read_info,exception);
520       if (image == (Image *) NULL)
521         {
522           read_info=DestroyImageInfo(read_info);
523           return((Image *) NULL);
524         }
525       (void) CopyMagickString(image->filename,read_info->filename,
526         MagickPathExtent);
527       *read_info->filename='\0';
528       if (GetDelegateThreadSupport(delegate_info) == MagickFalse)
529         LockSemaphoreInfo(delegate_info->semaphore);
530       status=InvokeDelegate(read_info,image,read_info->magick,(char *) NULL,
531         exception);
532       if (GetDelegateThreadSupport(delegate_info) == MagickFalse)
533         UnlockSemaphoreInfo(delegate_info->semaphore);
534       image=DestroyImageList(image);
535       read_info->temporary=MagickTrue;
536       if (status != MagickFalse)
537         (void) SetImageInfo(read_info,0,exception);
538       magick_info=GetMagickInfo(read_info->magick,exception);
539       if ((magick_info == (const MagickInfo *) NULL) ||
540           (GetImageDecoder(magick_info) == (DecodeImageHandler *) NULL))
541         {
542           if (IsPathAccessible(read_info->filename) != MagickFalse)
543             (void) ThrowMagickException(exception,GetMagickModule(),
544               MissingDelegateError,"NoDecodeDelegateForThisImageFormat","`%s'",
545               read_info->magick);
546           else
547             ThrowFileException(exception,FileOpenError,"UnableToOpenFile",
548               read_info->filename);
549           read_info=DestroyImageInfo(read_info);
550           return((Image *) NULL);
551         }
552       if (GetMagickDecoderThreadSupport(magick_info) == MagickFalse)
553         LockSemaphoreInfo(magick_info->semaphore);
554       image=(Image *) (GetImageDecoder(magick_info))(read_info,exception);
555       if (GetMagickDecoderThreadSupport(magick_info) == MagickFalse)
556         UnlockSemaphoreInfo(magick_info->semaphore);
557     }
558   if (read_info->temporary != MagickFalse)
559     {
560       (void) RelinquishUniqueFileResource(read_info->filename);
561       read_info->temporary=MagickFalse;
562       if (image != (Image *) NULL)
563         (void) CopyMagickString(image->filename,filename,MagickPathExtent);
564     }
565   if (image == (Image *) NULL)
566     {
567       read_info=DestroyImageInfo(read_info);
568       return(image);
569     }
570   if (exception->severity >= ErrorException)
571     (void) LogMagickEvent(ExceptionEvent,GetMagickModule(),
572       "Coder (%s) generated an image despite an error (%d), "
573       "notify the developers",image->magick,exception->severity);
574   if (IsBlobTemporary(image) != MagickFalse)
575     (void) RelinquishUniqueFileResource(read_info->filename);
576   if ((GetNextImageInList(image) != (Image *) NULL) &&
577       (IsSceneGeometry(read_info->scenes,MagickFalse) != MagickFalse))
578     {
579       Image
580         *clones;
581 
582       clones=CloneImages(image,read_info->scenes,exception);
583       if (clones == (Image *) NULL)
584         (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
585           "SubimageSpecificationReturnsNoImages","`%s'",read_info->filename);
586       else
587         {
588           image=DestroyImageList(image);
589           image=GetFirstImageInList(clones);
590         }
591     }
592   for (next=image; next != (Image *) NULL; next=GetNextImageInList(next))
593   {
594     char
595       magick_path[MagickPathExtent],
596       *property,
597       timestamp[MagickPathExtent];
598 
599     const char
600       *option;
601 
602     const StringInfo
603       *profile;
604 
605     next->taint=MagickFalse;
606     GetPathComponent(magick_filename,MagickPath,magick_path);
607     if (*magick_path == '\0' && *next->magick == '\0')
608       (void) CopyMagickString(next->magick,magick,MagickPathExtent);
609     (void) CopyMagickString(next->magick_filename,magick_filename,
610       MagickPathExtent);
611     if (IsBlobTemporary(image) != MagickFalse)
612       (void) CopyMagickString(next->filename,filename,MagickPathExtent);
613     if (next->magick_columns == 0)
614       next->magick_columns=next->columns;
615     if (next->magick_rows == 0)
616       next->magick_rows=next->rows;
617     value=GetImageProperty(next,"tiff:Orientation",exception);
618     if (value == (char *) NULL)
619       value=GetImageProperty(next,"exif:Orientation",exception);
620     if (value != (char *) NULL)
621       {
622         next->orientation=(OrientationType) StringToLong(value);
623         (void) DeleteImageProperty(next,"tiff:Orientation");
624         (void) DeleteImageProperty(next,"exif:Orientation");
625       }
626     value=GetImageProperty(next,"exif:XResolution",exception);
627     if (value != (char *) NULL)
628       {
629         geometry_info.rho=next->resolution.x;
630         geometry_info.sigma=1.0;
631         flags=ParseGeometry(value,&geometry_info);
632         if (geometry_info.sigma != 0)
633           next->resolution.x=geometry_info.rho/geometry_info.sigma;
634         (void) DeleteImageProperty(next,"exif:XResolution");
635       }
636     value=GetImageProperty(next,"exif:YResolution",exception);
637     if (value != (char *) NULL)
638       {
639         geometry_info.rho=next->resolution.y;
640         geometry_info.sigma=1.0;
641         flags=ParseGeometry(value,&geometry_info);
642         if (geometry_info.sigma != 0)
643           next->resolution.y=geometry_info.rho/geometry_info.sigma;
644         (void) DeleteImageProperty(next,"exif:YResolution");
645       }
646     value=GetImageProperty(next,"tiff:ResolutionUnit",exception);
647     if (value == (char *) NULL)
648       value=GetImageProperty(next,"exif:ResolutionUnit",exception);
649     if (value != (char *) NULL)
650       {
651         next->units=(ResolutionType) (StringToLong(value)-1);
652         (void) DeleteImageProperty(next,"exif:ResolutionUnit");
653         (void) DeleteImageProperty(next,"tiff:ResolutionUnit");
654       }
655     if (next->page.width == 0)
656       next->page.width=next->columns;
657     if (next->page.height == 0)
658       next->page.height=next->rows;
659     option=GetImageOption(read_info,"caption");
660     if (option != (const char *) NULL)
661       {
662         property=InterpretImageProperties(read_info,next,option,exception);
663         (void) SetImageProperty(next,"caption",property,exception);
664         property=DestroyString(property);
665       }
666     option=GetImageOption(read_info,"comment");
667     if (option != (const char *) NULL)
668       {
669         property=InterpretImageProperties(read_info,next,option,exception);
670         (void) SetImageProperty(next,"comment",property,exception);
671         property=DestroyString(property);
672       }
673     option=GetImageOption(read_info,"label");
674     if (option != (const char *) NULL)
675       {
676         property=InterpretImageProperties(read_info,next,option,exception);
677         (void) SetImageProperty(next,"label",property,exception);
678         property=DestroyString(property);
679       }
680     if (LocaleCompare(next->magick,"TEXT") == 0)
681       (void) ParseAbsoluteGeometry("0x0+0+0",&next->page);
682     if ((read_info->extract != (char *) NULL) &&
683         (read_info->stream == (StreamHandler) NULL))
684       {
685         RectangleInfo
686           geometry;
687 
688         flags=ParseAbsoluteGeometry(read_info->extract,&geometry);
689         if ((next->columns != geometry.width) ||
690             (next->rows != geometry.height))
691           {
692             if (((flags & XValue) != 0) || ((flags & YValue) != 0))
693               {
694                 Image
695                   *crop_image;
696 
697                 crop_image=CropImage(next,&geometry,exception);
698                 if (crop_image != (Image *) NULL)
699                   ReplaceImageInList(&next,crop_image);
700               }
701             else
702               if (((flags & WidthValue) != 0) || ((flags & HeightValue) != 0))
703                 {
704                   Image
705                     *size_image;
706 
707                   flags=ParseRegionGeometry(next,read_info->extract,&geometry,
708                     exception);
709                   size_image=ResizeImage(next,geometry.width,geometry.height,
710                     next->filter,exception);
711                   if (size_image != (Image *) NULL)
712                     ReplaceImageInList(&next,size_image);
713                 }
714           }
715       }
716     profile=GetImageProfile(next,"icc");
717     if (profile == (const StringInfo *) NULL)
718       profile=GetImageProfile(next,"icm");
719     profile=GetImageProfile(next,"iptc");
720     if (profile == (const StringInfo *) NULL)
721       profile=GetImageProfile(next,"8bim");
722     (void) FormatMagickTime(GetBlobProperties(next)->st_mtime,MagickPathExtent,
723       timestamp);
724     (void) SetImageProperty(next,"date:modify",timestamp,exception);
725     (void) FormatMagickTime(GetBlobProperties(next)->st_ctime,MagickPathExtent,
726       timestamp);
727     (void) SetImageProperty(next,"date:create",timestamp,exception);
728     option=GetImageOption(image_info,"delay");
729     if (option != (const char *) NULL)
730       {
731         flags=ParseGeometry(option,&geometry_info);
732         if ((flags & GreaterValue) != 0)
733           {
734             if (next->delay > (size_t) floor(geometry_info.rho+0.5))
735               next->delay=(size_t) floor(geometry_info.rho+0.5);
736           }
737         else
738           if ((flags & LessValue) != 0)
739             {
740               if (next->delay < (size_t) floor(geometry_info.rho+0.5))
741                 next->ticks_per_second=(ssize_t) floor(geometry_info.sigma+0.5);
742             }
743           else
744             next->delay=(size_t) floor(geometry_info.rho+0.5);
745         if ((flags & SigmaValue) != 0)
746           next->ticks_per_second=(ssize_t) floor(geometry_info.sigma+0.5);
747       }
748     option=GetImageOption(image_info,"dispose");
749     if (option != (const char *) NULL)
750       next->dispose=(DisposeType) ParseCommandOption(MagickDisposeOptions,
751         MagickFalse,option);
752     if (read_info->verbose != MagickFalse)
753       (void) IdentifyImage(next,stderr,MagickFalse,exception);
754     image=next;
755   }
756   read_info=DestroyImageInfo(read_info);
757   return(GetFirstImageInList(image));
758 }
759 
760 /*
761 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
762 %                                                                             %
763 %                                                                             %
764 %                                                                             %
765 %   R e a d I m a g e s                                                       %
766 %                                                                             %
767 %                                                                             %
768 %                                                                             %
769 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
770 %
771 %  ReadImages() reads one or more images and returns them as an image list.
772 %
773 %  The format of the ReadImage method is:
774 %
775 %      Image *ReadImages(ImageInfo *image_info,const char *filename,
776 %        ExceptionInfo *exception)
777 %
778 %  A description of each parameter follows:
779 %
780 %    o image_info: the image info.
781 %
782 %    o filename: the image filename.
783 %
784 %    o exception: return any errors or warnings in this structure.
785 %
786 */
ReadImages(ImageInfo * image_info,const char * filename,ExceptionInfo * exception)787 MagickExport Image *ReadImages(ImageInfo *image_info,const char *filename,
788   ExceptionInfo *exception)
789 {
790   char
791     read_filename[MagickPathExtent];
792 
793   Image
794     *image,
795     *images;
796 
797   ImageInfo
798     *read_info;
799 
800   /*
801     Read image list from a file.
802   */
803   assert(image_info != (ImageInfo *) NULL);
804   assert(image_info->signature == MagickCoreSignature);
805   if (image_info->debug != MagickFalse)
806     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
807       image_info->filename);
808   assert(exception != (ExceptionInfo *) NULL);
809   read_info=CloneImageInfo(image_info);
810   *read_info->magick='\0';
811   (void) SetImageOption(read_info,"filename",filename);
812   (void) CopyMagickString(read_info->filename,filename,MagickPathExtent);
813   (void) InterpretImageFilename(read_info,(Image *) NULL,filename,
814     (int) read_info->scene,read_filename,exception);
815   if (LocaleCompare(read_filename,read_info->filename) != 0)
816     {
817       ExceptionInfo
818         *sans;
819 
820       ssize_t
821         extent,
822         scene;
823 
824       /*
825         Images of the form image-%d.png[1-5].
826       */
827       sans=AcquireExceptionInfo();
828       (void) SetImageInfo(read_info,0,sans);
829       sans=DestroyExceptionInfo(sans);
830       if (read_info->number_scenes == 0)
831         {
832           read_info=DestroyImageInfo(read_info);
833           return(ReadImage(image_info,exception));
834         }
835       (void) CopyMagickString(read_filename,read_info->filename,
836         MagickPathExtent);
837       images=NewImageList();
838       extent=(ssize_t) (read_info->scene+read_info->number_scenes);
839       for (scene=(ssize_t) read_info->scene; scene < (ssize_t) extent; scene++)
840       {
841         (void) InterpretImageFilename(image_info,(Image *) NULL,read_filename,
842           (int) scene,read_info->filename,exception);
843         image=ReadImage(read_info,exception);
844         if (image == (Image *) NULL)
845           continue;
846         AppendImageToList(&images,image);
847       }
848       read_info=DestroyImageInfo(read_info);
849       return(images);
850     }
851   image=ReadImage(read_info,exception);
852   read_info=DestroyImageInfo(read_info);
853   return(image);
854 }
855 
856 /*
857 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
858 %                                                                             %
859 %                                                                             %
860 %                                                                             %
861 +   R e a d I n l i n e I m a g e                                             %
862 %                                                                             %
863 %                                                                             %
864 %                                                                             %
865 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
866 %
867 %  ReadInlineImage() reads a Base64-encoded inline image or image sequence.
868 %  The method returns a NULL if there is a memory shortage or if the image
869 %  cannot be read.  On failure, a NULL image is returned and exception
870 %  describes the reason for the failure.
871 %
872 %  The format of the ReadInlineImage method is:
873 %
874 %      Image *ReadInlineImage(const ImageInfo *image_info,const char *content,
875 %        ExceptionInfo *exception)
876 %
877 %  A description of each parameter follows:
878 %
879 %    o image_info: the image info.
880 %
881 %    o content: the image encoded in Base64.
882 %
883 %    o exception: return any errors or warnings in this structure.
884 %
885 */
ReadInlineImage(const ImageInfo * image_info,const char * content,ExceptionInfo * exception)886 MagickExport Image *ReadInlineImage(const ImageInfo *image_info,
887   const char *content,ExceptionInfo *exception)
888 {
889   Image
890     *image;
891 
892   ImageInfo
893     *read_info;
894 
895   unsigned char
896     *blob;
897 
898   size_t
899     length;
900 
901   register const char
902     *p;
903 
904   /*
905     Skip over header (e.g. data:image/gif;base64,).
906   */
907   image=NewImageList();
908   for (p=content; (*p != ',') && (*p != '\0'); p++) ;
909   if (*p == '\0')
910     ThrowReaderException(CorruptImageError,"CorruptImage");
911   p++;
912   length=0;
913   blob=Base64Decode(p,&length);
914   if (length == 0)
915     ThrowReaderException(CorruptImageError,"CorruptImage");
916   read_info=CloneImageInfo(image_info);
917   (void) SetImageInfoProgressMonitor(read_info,(MagickProgressMonitor) NULL,
918     (void *) NULL);
919   *read_info->filename='\0';
920   *read_info->magick='\0';
921   image=BlobToImage(read_info,blob,length,exception);
922   blob=(unsigned char *) RelinquishMagickMemory(blob);
923   read_info=DestroyImageInfo(read_info);
924   return(image);
925 }
926 
927 /*
928 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
929 %                                                                             %
930 %                                                                             %
931 %                                                                             %
932 %   W r i t e I m a g e                                                       %
933 %                                                                             %
934 %                                                                             %
935 %                                                                             %
936 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
937 %
938 %  WriteImage() writes an image or an image sequence to a file or file handle.
939 %  If writing to a file is on disk, the name is defined by the filename member
940 %  of the image structure.  WriteImage() returns MagickFalse is there is a
941 %  memory shortage or if the image cannot be written.  Check the exception
942 %  member of image to determine the cause for any failure.
943 %
944 %  The format of the WriteImage method is:
945 %
946 %      MagickBooleanType WriteImage(const ImageInfo *image_info,Image *image,
947 %        ExceptionInfo *exception)
948 %
949 %  A description of each parameter follows:
950 %
951 %    o image_info: the image info.
952 %
953 %    o image: the image.
954 %
955 %    o exception: return any errors or warnings in this structure.
956 %
957 */
WriteImage(const ImageInfo * image_info,Image * image,ExceptionInfo * exception)958 MagickExport MagickBooleanType WriteImage(const ImageInfo *image_info,
959   Image *image,ExceptionInfo *exception)
960 {
961   char
962     filename[MagickPathExtent];
963 
964   const char
965     *option;
966 
967   const DelegateInfo
968     *delegate_info;
969 
970   const MagickInfo
971     *magick_info;
972 
973   ExceptionInfo
974     *sans_exception;
975 
976   ImageInfo
977     *write_info;
978 
979   MagickBooleanType
980     status,
981     temporary;
982 
983   PolicyDomain
984     domain;
985 
986   PolicyRights
987     rights;
988 
989   /*
990     Determine image type from filename prefix or suffix (e.g. image.jpg).
991   */
992   assert(image_info != (ImageInfo *) NULL);
993   assert(image_info->signature == MagickCoreSignature);
994   if (image->debug != MagickFalse)
995     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
996       image_info->filename);
997   assert(image != (Image *) NULL);
998   assert(image->signature == MagickCoreSignature);
999   assert(exception != (ExceptionInfo *) NULL);
1000   sans_exception=AcquireExceptionInfo();
1001   write_info=CloneImageInfo(image_info);
1002   (void) CopyMagickString(write_info->filename,image->filename,
1003     MagickPathExtent);
1004   (void) SetImageInfo(write_info,1,sans_exception);
1005   if (*write_info->magick == '\0')
1006     (void) CopyMagickString(write_info->magick,image->magick,MagickPathExtent);
1007   (void) CopyMagickString(filename,image->filename,MagickPathExtent);
1008   (void) CopyMagickString(image->filename,write_info->filename,
1009     MagickPathExtent);
1010   domain=CoderPolicyDomain;
1011   rights=WritePolicyRights;
1012   if (IsRightsAuthorized(domain,rights,write_info->magick) == MagickFalse)
1013     {
1014       sans_exception=DestroyExceptionInfo(sans_exception);
1015       write_info=DestroyImageInfo(write_info);
1016       errno=EPERM;
1017       ThrowBinaryException(PolicyError,"NotAuthorized",filename);
1018     }
1019   /*
1020     Call appropriate image writer based on image type.
1021   */
1022   magick_info=GetMagickInfo(write_info->magick,sans_exception);
1023   sans_exception=DestroyExceptionInfo(sans_exception);
1024   if (magick_info != (const MagickInfo *) NULL)
1025     {
1026       if (GetMagickEndianSupport(magick_info) == MagickFalse)
1027         image->endian=UndefinedEndian;
1028       else
1029         if ((image_info->endian == UndefinedEndian) &&
1030             (GetMagickRawSupport(magick_info) != MagickFalse))
1031           {
1032             unsigned long
1033               lsb_first;
1034 
1035             lsb_first=1;
1036             image->endian=(*(char *) &lsb_first) == 1 ? LSBEndian : MSBEndian;
1037          }
1038     }
1039   (void) SyncImageProfiles(image);
1040   DisassociateImageStream(image);
1041   option=GetImageOption(image_info,"delegate:bimodal");
1042   if ((IsStringTrue(option) != MagickFalse) &&
1043       (write_info->page == (char *) NULL) &&
1044       (GetPreviousImageInList(image) == (Image *) NULL) &&
1045       (GetNextImageInList(image) == (Image *) NULL) &&
1046       (IsTaintImage(image) == MagickFalse) )
1047     {
1048       delegate_info=GetDelegateInfo(image->magick,write_info->magick,exception);
1049       if ((delegate_info != (const DelegateInfo *) NULL) &&
1050           (GetDelegateMode(delegate_info) == 0) &&
1051           (IsPathAccessible(image->magick_filename) != MagickFalse))
1052         {
1053           /*
1054             Process image with bi-modal delegate.
1055           */
1056           (void) CopyMagickString(image->filename,image->magick_filename,
1057             MagickPathExtent);
1058           status=InvokeDelegate(write_info,image,image->magick,
1059             write_info->magick,exception);
1060           write_info=DestroyImageInfo(write_info);
1061           (void) CopyMagickString(image->filename,filename,MagickPathExtent);
1062           return(status);
1063         }
1064     }
1065   status=MagickFalse;
1066   temporary=MagickFalse;
1067   if ((magick_info != (const MagickInfo *) NULL) &&
1068       (GetMagickSeekableStream(magick_info) != MagickFalse))
1069     {
1070       char
1071         image_filename[MagickPathExtent];
1072 
1073       (void) CopyMagickString(image_filename,image->filename,MagickPathExtent);
1074       status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
1075       (void) CopyMagickString(image->filename, image_filename,MagickPathExtent);
1076       if (status != MagickFalse)
1077         {
1078           if (IsBlobSeekable(image) == MagickFalse)
1079             {
1080               /*
1081                 A seekable stream is required by the encoder.
1082               */
1083               write_info->adjoin=MagickTrue;
1084               (void) CopyMagickString(write_info->filename,image->filename,
1085                 MagickPathExtent);
1086               (void) AcquireUniqueFilename(image->filename);
1087               temporary=MagickTrue;
1088             }
1089           (void) CloseBlob(image);
1090         }
1091     }
1092   if ((magick_info != (const MagickInfo *) NULL) &&
1093       (GetImageEncoder(magick_info) != (EncodeImageHandler *) NULL))
1094     {
1095       /*
1096         Call appropriate image writer based on image type.
1097       */
1098       if (GetMagickEncoderThreadSupport(magick_info) == MagickFalse)
1099         LockSemaphoreInfo(magick_info->semaphore);
1100       status=GetImageEncoder(magick_info)(write_info,image,exception);
1101       if (GetMagickEncoderThreadSupport(magick_info) == MagickFalse)
1102         UnlockSemaphoreInfo(magick_info->semaphore);
1103     }
1104   else
1105     {
1106       delegate_info=GetDelegateInfo((char *) NULL,write_info->magick,exception);
1107       if (delegate_info != (DelegateInfo *) NULL)
1108         {
1109           /*
1110             Process the image with delegate.
1111           */
1112           *write_info->filename='\0';
1113           if (GetDelegateThreadSupport(delegate_info) == MagickFalse)
1114             LockSemaphoreInfo(delegate_info->semaphore);
1115           status=InvokeDelegate(write_info,image,(char *) NULL,
1116             write_info->magick,exception);
1117           if (GetDelegateThreadSupport(delegate_info) == MagickFalse)
1118             UnlockSemaphoreInfo(delegate_info->semaphore);
1119           (void) CopyMagickString(image->filename,filename,MagickPathExtent);
1120         }
1121       else
1122         {
1123           sans_exception=AcquireExceptionInfo();
1124           magick_info=GetMagickInfo(write_info->magick,sans_exception);
1125           sans_exception=DestroyExceptionInfo(sans_exception);
1126           if ((write_info->affirm == MagickFalse) &&
1127               (magick_info == (const MagickInfo *) NULL))
1128             {
1129               (void) CopyMagickString(write_info->magick,image->magick,
1130                 MagickPathExtent);
1131               magick_info=GetMagickInfo(write_info->magick,exception);
1132             }
1133           if ((magick_info == (const MagickInfo *) NULL) ||
1134               (GetImageEncoder(magick_info) == (EncodeImageHandler *) NULL))
1135             {
1136               char
1137                 extension[MagickPathExtent];
1138 
1139               GetPathComponent(image->filename,ExtensionPath,extension);
1140               if (*extension != '\0')
1141                 magick_info=GetMagickInfo(extension,exception);
1142               else
1143                 magick_info=GetMagickInfo(image->magick,exception);
1144               (void) CopyMagickString(image->filename,filename,
1145                 MagickPathExtent);
1146             }
1147           if ((magick_info == (const MagickInfo *) NULL) ||
1148               (GetImageEncoder(magick_info) == (EncodeImageHandler *) NULL))
1149             {
1150               magick_info=GetMagickInfo(image->magick,exception);
1151               if ((magick_info == (const MagickInfo *) NULL) ||
1152                   (GetImageEncoder(magick_info) == (EncodeImageHandler *) NULL))
1153                 (void) ThrowMagickException(exception,GetMagickModule(),
1154                   MissingDelegateError,"NoEncodeDelegateForThisImageFormat",
1155                   "`%s'",write_info->magick);
1156               else
1157                 (void) ThrowMagickException(exception,GetMagickModule(),
1158                   MissingDelegateWarning,"NoEncodeDelegateForThisImageFormat",
1159                   "`%s'",write_info->magick);
1160             }
1161           if ((magick_info != (const MagickInfo *) NULL) &&
1162               (GetImageEncoder(magick_info) != (EncodeImageHandler *) NULL))
1163             {
1164               /*
1165                 Call appropriate image writer based on image type.
1166               */
1167               if (GetMagickEncoderThreadSupport(magick_info) == MagickFalse)
1168                 LockSemaphoreInfo(magick_info->semaphore);
1169               status=GetImageEncoder(magick_info)(write_info,image,exception);
1170               if (GetMagickEncoderThreadSupport(magick_info) == MagickFalse)
1171                 UnlockSemaphoreInfo(magick_info->semaphore);
1172             }
1173         }
1174     }
1175   if (temporary != MagickFalse)
1176     {
1177       /*
1178         Copy temporary image file to permanent.
1179       */
1180       status=OpenBlob(write_info,image,ReadBinaryBlobMode,exception);
1181       if (status != MagickFalse)
1182         {
1183           (void) RelinquishUniqueFileResource(write_info->filename);
1184           status=ImageToFile(image,write_info->filename,exception);
1185         }
1186       (void) CloseBlob(image);
1187       (void) RelinquishUniqueFileResource(image->filename);
1188       (void) CopyMagickString(image->filename,write_info->filename,
1189         MagickPathExtent);
1190     }
1191   if ((LocaleCompare(write_info->magick,"info") != 0) &&
1192       (write_info->verbose != MagickFalse))
1193     (void) IdentifyImage(image,stdout,MagickFalse,exception);
1194   write_info=DestroyImageInfo(write_info);
1195   return(status);
1196 }
1197 
1198 /*
1199 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1200 %                                                                             %
1201 %                                                                             %
1202 %                                                                             %
1203 %   W r i t e I m a g e s                                                     %
1204 %                                                                             %
1205 %                                                                             %
1206 %                                                                             %
1207 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1208 %
1209 %  WriteImages() writes an image sequence into one or more files.  While
1210 %  WriteImage() can write an image sequence, it is limited to writing
1211 %  the sequence into a single file using a format which supports multiple
1212 %  frames.   WriteImages(), however, does not have this limitation, instead it
1213 %  generates multiple output files if necessary (or when requested).  When
1214 %  ImageInfo's adjoin flag is set to MagickFalse, the file name is expected
1215 %  to include a printf-style formatting string for the frame number (e.g.
1216 %  "image%02d.png").
1217 %
1218 %  The format of the WriteImages method is:
1219 %
1220 %      MagickBooleanType WriteImages(const ImageInfo *image_info,Image *images,
1221 %        const char *filename,ExceptionInfo *exception)
1222 %
1223 %  A description of each parameter follows:
1224 %
1225 %    o image_info: the image info.
1226 %
1227 %    o images: the image list.
1228 %
1229 %    o filename: the image filename.
1230 %
1231 %    o exception: return any errors or warnings in this structure.
1232 %
1233 */
WriteImages(const ImageInfo * image_info,Image * images,const char * filename,ExceptionInfo * exception)1234 MagickExport MagickBooleanType WriteImages(const ImageInfo *image_info,
1235   Image *images,const char *filename,ExceptionInfo *exception)
1236 {
1237 #define WriteImageTag  "Write/Image"
1238 
1239   ExceptionInfo
1240     *sans_exception;
1241 
1242   ImageInfo
1243     *write_info;
1244 
1245   MagickBooleanType
1246     proceed;
1247 
1248   MagickOffsetType
1249     progress;
1250 
1251   MagickProgressMonitor
1252     progress_monitor;
1253 
1254   MagickSizeType
1255     number_images;
1256 
1257   MagickStatusType
1258     status;
1259 
1260   register Image
1261     *p;
1262 
1263   assert(image_info != (const ImageInfo *) NULL);
1264   assert(image_info->signature == MagickCoreSignature);
1265   assert(images != (Image *) NULL);
1266   assert(images->signature == MagickCoreSignature);
1267   if (images->debug != MagickFalse)
1268     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",images->filename);
1269   assert(exception != (ExceptionInfo *) NULL);
1270   write_info=CloneImageInfo(image_info);
1271   *write_info->magick='\0';
1272   images=GetFirstImageInList(images);
1273   if (filename != (const char *) NULL)
1274     for (p=images; p != (Image *) NULL; p=GetNextImageInList(p))
1275       (void) CopyMagickString(p->filename,filename,MagickPathExtent);
1276   (void) CopyMagickString(write_info->filename,images->filename,
1277     MagickPathExtent);
1278   sans_exception=AcquireExceptionInfo();
1279   (void) SetImageInfo(write_info,(unsigned int) GetImageListLength(images),
1280     sans_exception);
1281   sans_exception=DestroyExceptionInfo(sans_exception);
1282   if (*write_info->magick == '\0')
1283     (void) CopyMagickString(write_info->magick,images->magick,MagickPathExtent);
1284   p=images;
1285   for ( ; GetNextImageInList(p) != (Image *) NULL; p=GetNextImageInList(p))
1286   {
1287     register Image
1288       *next;
1289 
1290     next=GetNextImageInList(p);
1291     if (next == (Image *) NULL)
1292       break;
1293     if (p->scene >= next->scene)
1294       {
1295         register ssize_t
1296           i;
1297 
1298         /*
1299           Generate consistent scene numbers.
1300         */
1301         i=(ssize_t) images->scene;
1302         for (p=images; p != (Image *) NULL; p=GetNextImageInList(p))
1303           p->scene=(size_t) i++;
1304         break;
1305       }
1306   }
1307   /*
1308     Write images.
1309   */
1310   status=MagickTrue;
1311   progress_monitor=(MagickProgressMonitor) NULL;
1312   progress=0;
1313   number_images=GetImageListLength(images);
1314   for (p=images; p != (Image *) NULL; p=GetNextImageInList(p))
1315   {
1316     if (number_images != 1)
1317       progress_monitor=SetImageProgressMonitor(p,(MagickProgressMonitor) NULL,
1318         p->client_data);
1319     status&=WriteImage(write_info,p,exception);
1320     if (number_images != 1)
1321       (void) SetImageProgressMonitor(p,progress_monitor,p->client_data);
1322     if (write_info->adjoin != MagickFalse)
1323       break;
1324     if (number_images != 1)
1325       {
1326         proceed=SetImageProgress(p,WriteImageTag,progress++,number_images);
1327         if (proceed == MagickFalse)
1328           break;
1329       }
1330   }
1331   write_info=DestroyImageInfo(write_info);
1332   return(status != 0 ? MagickTrue : MagickFalse);
1333 }
1334