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