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