]> 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       delegate_info=GetDelegateInfo(read_info->magick,(char *) NULL,exception);
528       if (delegate_info == (const DelegateInfo *) NULL)
529         {
530           (void) SetImageInfo(read_info,0,exception);
531           (void) CopyMagickString(read_info->filename,filename,MaxTextExtent);
532           magick_info=GetMagickInfo(read_info->magick,exception);
533         }
534     }
535   if ((magick_info != (const MagickInfo *) NULL) &&
536       (GetImageDecoder(magick_info) != (DecodeImageHandler *) NULL))
537     {
538       thread_support=GetMagickThreadSupport(magick_info);
539       if ((thread_support & DecoderThreadSupport) == 0)
540         LockSemaphoreInfo(constitute_semaphore);
541       image=GetImageDecoder(magick_info)(read_info,exception);
542       if ((thread_support & DecoderThreadSupport) == 0)
543         UnlockSemaphoreInfo(constitute_semaphore);
544     }
545   else
546     {
547       delegate_info=GetDelegateInfo(read_info->magick,(char *) NULL,exception);
548       if (delegate_info == (const DelegateInfo *) NULL)
549         {
550           (void) ThrowMagickException(exception,GetMagickModule(),
551             MissingDelegateError,"NoDecodeDelegateForThisImageFormat","'%s'",
552             read_info->filename);
553           if (read_info->temporary != MagickFalse)
554             (void) RelinquishUniqueFileResource(read_info->filename);
555           read_info=DestroyImageInfo(read_info);
556           return((Image *) NULL);
557         }
558       /*
559         Let our decoding delegate process the image.
560       */
561       image=AcquireImage(read_info,exception);
562       if (image == (Image *) NULL)
563         {
564           read_info=DestroyImageInfo(read_info);
565           return((Image *) NULL);
566         }
567       (void) CopyMagickString(image->filename,read_info->filename,
568         MaxTextExtent);
569       *read_info->filename='\0';
570       if (GetDelegateThreadSupport(delegate_info) == MagickFalse)
571         LockSemaphoreInfo(constitute_semaphore);
572       (void) InvokeDelegate(read_info,image,read_info->magick,(char *) NULL,
573         exception);
574       if (GetDelegateThreadSupport(delegate_info) == MagickFalse)
575         UnlockSemaphoreInfo(constitute_semaphore);
576       image=DestroyImageList(image);
577       read_info->temporary=MagickTrue;
578       (void) SetImageInfo(read_info,0,exception);
579       magick_info=GetMagickInfo(read_info->magick,exception);
580       if ((magick_info == (const MagickInfo *) NULL) ||
581           (GetImageDecoder(magick_info) == (DecodeImageHandler *) NULL))
582         {
583           if (IsPathAccessible(read_info->filename) != MagickFalse)
584             (void) ThrowMagickException(exception,GetMagickModule(),
585               MissingDelegateError,"NoDecodeDelegateForThisImageFormat","'%s'",
586               read_info->filename);
587           else
588             ThrowFileException(exception,FileOpenError,"UnableToOpenFile",
589               read_info->filename);
590           read_info=DestroyImageInfo(read_info);
591           return((Image *) NULL);
592         }
593       thread_support=GetMagickThreadSupport(magick_info);
594       if ((thread_support & DecoderThreadSupport) == 0)
595         LockSemaphoreInfo(constitute_semaphore);
596       image=(Image *) (GetImageDecoder(magick_info))(read_info,exception);
597       if ((thread_support & DecoderThreadSupport) == 0)
598         UnlockSemaphoreInfo(constitute_semaphore);
599     }
600   if (read_info->temporary != MagickFalse)
601     {
602       (void) RelinquishUniqueFileResource(read_info->filename);
603       read_info->temporary=MagickFalse;
604       if (image != (Image *) NULL)
605         (void) CopyMagickString(image->filename,filename,MaxTextExtent);
606     }
607   if (image == (Image *) NULL)
608     {
609       read_info=DestroyImageInfo(read_info);
610       return(image);
611     }
612   if (exception->severity >= ErrorException)
613     (void) LogMagickEvent(ExceptionEvent,GetMagickModule(),
614       "Coder (%s) generated an image despite an error (%d), "
615       "notify the developers",image->magick,exception->severity);
616   if (IsBlobTemporary(image) != MagickFalse)
617     (void) RelinquishUniqueFileResource(read_info->filename);
618   if ((GetNextImageInList(image) != (Image *) NULL) &&
619       (IsSceneGeometry(read_info->scenes,MagickFalse) != MagickFalse))
620     {
621       Image
622         *clones;
623
624       clones=CloneImages(image,read_info->scenes,exception);
625       if (clones == (Image *) NULL)
626         (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
627           "SubimageSpecificationReturnsNoImages","'%s'",read_info->filename);
628       else
629         {
630           image=DestroyImageList(image);
631           image=GetFirstImageInList(clones);
632         }
633     }
634   if (GetBlobError(image) != MagickFalse)
635     {
636       ThrowFileException(exception,FileOpenError,
637         "AnErrorHasOccurredReadingFromFile",read_info->filename);
638       image=DestroyImageList(image);
639       read_info=DestroyImageInfo(read_info);
640       return((Image *) NULL);
641     }
642   for (next=image; next != (Image *) NULL; next=GetNextImageInList(next))
643   {
644     char
645       magick_path[MaxTextExtent],
646       *property,
647       timestamp[MaxTextExtent];
648
649     const char
650       *option;
651
652     const StringInfo
653       *profile;
654
655     next->taint=MagickFalse;
656     GetPathComponent(magick_filename,MagickPath,magick_path);
657     if (*magick_path == '\0')
658       (void) CopyMagickString(next->magick,magick,MaxTextExtent);
659     (void) CopyMagickString(next->magick_filename,magick_filename,
660       MaxTextExtent);
661     if (IsBlobTemporary(image) != MagickFalse)
662       (void) CopyMagickString(next->filename,filename,MaxTextExtent);
663     if (next->magick_columns == 0)
664       next->magick_columns=next->columns;
665     if (next->magick_rows == 0)
666       next->magick_rows=next->rows;
667     value=GetImageProperty(next,"tiff:Orientation",exception);
668     if (value == (char *) NULL)
669       value=GetImageProperty(next,"exif:Orientation",exception);
670     if (value != (char *) NULL)
671       {
672         next->orientation=(OrientationType) StringToLong(value);
673         (void) DeleteImageProperty(next,"tiff:Orientation");
674         (void) DeleteImageProperty(next,"exif:Orientation");
675       }
676     value=GetImageProperty(next,"exif:XResolution",exception);
677     if (value != (char *) NULL)
678       {
679         geometry_info.rho=next->resolution.x;
680         geometry_info.sigma=1.0;
681         flags=ParseGeometry(value,&geometry_info);
682         if (geometry_info.sigma != 0)
683           next->resolution.x=geometry_info.rho/geometry_info.sigma;
684         (void) DeleteImageProperty(next,"exif:XResolution");
685       }
686     value=GetImageProperty(next,"exif:YResolution",exception);
687     if (value != (char *) NULL)
688       {
689         geometry_info.rho=next->resolution.y;
690         geometry_info.sigma=1.0;
691         flags=ParseGeometry(value,&geometry_info);
692         if (geometry_info.sigma != 0)
693           next->resolution.y=geometry_info.rho/geometry_info.sigma;
694         (void) DeleteImageProperty(next,"exif:YResolution");
695       }
696     value=GetImageProperty(next,"tiff:ResolutionUnit",exception);
697     if (value == (char *) NULL)
698       value=GetImageProperty(next,"exif:ResolutionUnit",exception);
699     if (value != (char *) NULL)
700       {
701         next->units=(ResolutionType) (StringToLong(value)-1);
702         (void) DeleteImageProperty(next,"exif:ResolutionUnit");
703         (void) DeleteImageProperty(next,"tiff:ResolutionUnit");
704       }
705     if (next->page.width == 0)
706       next->page.width=next->columns;
707     if (next->page.height == 0)
708       next->page.height=next->rows;
709     option=GetImageOption(read_info,"caption");
710     if (option != (const char *) NULL)
711       {
712         property=InterpretImageProperties(read_info,next,option,exception);
713         (void) SetImageProperty(next,"caption",property,exception);
714         property=DestroyString(property);
715       }
716     option=GetImageOption(read_info,"comment");
717     if (option != (const char *) NULL)
718       {
719         property=InterpretImageProperties(read_info,next,option,exception);
720         (void) SetImageProperty(next,"comment",property,exception);
721         property=DestroyString(property);
722       }
723     option=GetImageOption(read_info,"label");
724     if (option != (const char *) NULL)
725       {
726         property=InterpretImageProperties(read_info,next,option,exception);
727         (void) SetImageProperty(next,"label",property,exception);
728         property=DestroyString(property);
729       }
730     if (LocaleCompare(next->magick,"TEXT") == 0)
731       (void) ParseAbsoluteGeometry("0x0+0+0",&next->page);
732     if ((read_info->extract != (char *) NULL) &&
733         (read_info->stream == (StreamHandler) NULL))
734       {
735         RectangleInfo
736           geometry;
737
738         flags=ParseAbsoluteGeometry(read_info->extract,&geometry);
739         if ((next->columns != geometry.width) ||
740             (next->rows != geometry.height))
741           {
742             if (((flags & XValue) != 0) || ((flags & YValue) != 0))
743               {
744                 Image
745                   *crop_image;
746
747                 crop_image=CropImage(next,&geometry,exception);
748                 if (crop_image != (Image *) NULL)
749                   ReplaceImageInList(&next,crop_image);
750               }
751             else
752               if (((flags & WidthValue) != 0) || ((flags & HeightValue) != 0))
753                 {
754                   Image
755                     *size_image;
756
757                   flags=ParseRegionGeometry(next,read_info->extract,&geometry,
758                     exception);
759                   size_image=ResizeImage(next,geometry.width,geometry.height,
760                     next->filter,exception);
761                   if (size_image != (Image *) NULL)
762                     ReplaceImageInList(&next,size_image);
763                 }
764           }
765       }
766     profile=GetImageProfile(next,"icc");
767     if (profile == (const StringInfo *) NULL)
768       profile=GetImageProfile(next,"icm");
769     profile=GetImageProfile(next,"iptc");
770     if (profile == (const StringInfo *) NULL)
771       profile=GetImageProfile(next,"8bim");
772     (void) FormatMagickTime(GetBlobProperties(next)->st_mtime,MaxTextExtent,
773       timestamp);
774     (void) SetImageProperty(next,"date:modify",timestamp,exception);
775     (void) FormatMagickTime(GetBlobProperties(next)->st_ctime,MaxTextExtent,
776       timestamp);
777     (void) SetImageProperty(next,"date:create",timestamp,exception);
778     option=GetImageOption(image_info,"delay");
779     if (option != (const char *) NULL)
780       {
781         GeometryInfo
782           geometry_info;
783
784         flags=ParseGeometry(option,&geometry_info);
785         if ((flags & GreaterValue) != 0)
786           {
787             if (next->delay > (size_t) floor(geometry_info.rho+0.5))
788               next->delay=(size_t) floor(geometry_info.rho+0.5);
789           }
790         else
791           if ((flags & LessValue) != 0)
792             {
793               if (next->delay < (size_t) floor(geometry_info.rho+0.5))
794                 next->ticks_per_second=(ssize_t) floor(geometry_info.sigma+0.5);
795             }
796           else
797             next->delay=(size_t) floor(geometry_info.rho+0.5);
798         if ((flags & SigmaValue) != 0)
799           next->ticks_per_second=(ssize_t) floor(geometry_info.sigma+0.5);
800       }
801     option=GetImageOption(image_info,"dispose");
802     if (option != (const char *) NULL)
803       next->dispose=(DisposeType) ParseCommandOption(MagickDisposeOptions,
804         MagickFalse,option);
805     if (read_info->verbose != MagickFalse)
806       (void) IdentifyImage(next,stderr,MagickFalse,exception);
807     image=next;
808   }
809   read_info=DestroyImageInfo(read_info);
810   return(GetFirstImageInList(image));
811 }
812 \f
813 /*
814 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
815 %                                                                             %
816 %                                                                             %
817 %                                                                             %
818 %   R e a d I m a g e s                                                       %
819 %                                                                             %
820 %                                                                             %
821 %                                                                             %
822 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
823 %
824 %  ReadImages() reads one or more images and returns them as an image list.
825 %
826 %  The format of the ReadImage method is:
827 %
828 %      Image *ReadImages(ImageInfo *image_info,const char *filename,
829 %        ExceptionInfo *exception)
830 %
831 %  A description of each parameter follows:
832 %
833 %    o image_info: the image info.
834 %
835 %    o filename: the image filename.
836 %
837 %    o exception: return any errors or warnings in this structure.
838 %
839 */
840 MagickExport Image *ReadImages(ImageInfo *image_info,const char *filename,
841   ExceptionInfo *exception)
842 {
843   char
844     read_filename[MaxTextExtent];
845
846   Image
847     *image,
848     *images;
849
850   ImageInfo
851     *read_info;
852
853   /*
854     Read image list from a file.
855   */
856   assert(image_info != (ImageInfo *) NULL);
857   assert(image_info->signature == MagickSignature);
858   if (image_info->debug != MagickFalse)
859     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
860       image_info->filename);
861   assert(exception != (ExceptionInfo *) NULL);
862   (void) SetImageOption(image_info,"filename",filename);
863   (void) CopyMagickString(image_info->filename,filename,MaxTextExtent);
864   (void) InterpretImageFilename(image_info,(Image *) NULL,filename,
865     (int) image_info->scene,read_filename,exception);
866   if (LocaleCompare(read_filename,image_info->filename) != 0)
867     {
868       ExceptionInfo
869         *sans;
870
871       ssize_t
872         extent,
873         scene;
874
875       /*
876         Images of the form image-%d.png[1-5].
877       */
878       read_info=CloneImageInfo(image_info);
879       sans=AcquireExceptionInfo();
880       (void) SetImageInfo(read_info,0,sans);
881       sans=DestroyExceptionInfo(sans);
882       if (read_info->number_scenes == 0)
883         {
884           read_info=DestroyImageInfo(read_info);
885           return(ReadImage(image_info,exception));
886         }
887       (void) CopyMagickString(read_filename,read_info->filename,MaxTextExtent);
888       images=NewImageList();
889       extent=(ssize_t) (read_info->scene+read_info->number_scenes);
890       for (scene=(ssize_t) read_info->scene; scene < (ssize_t) extent; scene++)
891       {
892         (void) InterpretImageFilename(image_info,(Image *) NULL,read_filename,
893           (int) scene,read_info->filename,exception);
894         image=ReadImage(read_info,exception);
895         if (image == (Image *) NULL)
896           continue;
897         AppendImageToList(&images,image);
898       }
899       read_info=DestroyImageInfo(read_info);
900       return(images);
901     }
902   return(ReadImage(image_info,exception));
903 }
904 \f
905 /*
906 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
907 %                                                                             %
908 %                                                                             %
909 %                                                                             %
910 +   R e a d I n l i n e I m a g e                                             %
911 %                                                                             %
912 %                                                                             %
913 %                                                                             %
914 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
915 %
916 %  ReadInlineImage() reads a Base64-encoded inline image or image sequence.
917 %  The method returns a NULL if there is a memory shortage or if the image
918 %  cannot be read.  On failure, a NULL image is returned and exception
919 %  describes the reason for the failure.
920 %
921 %  The format of the ReadInlineImage method is:
922 %
923 %      Image *ReadInlineImage(const ImageInfo *image_info,const char *content,
924 %        ExceptionInfo *exception)
925 %
926 %  A description of each parameter follows:
927 %
928 %    o image_info: the image info.
929 %
930 %    o content: the image encoded in Base64.
931 %
932 %    o exception: return any errors or warnings in this structure.
933 %
934 */
935 MagickExport Image *ReadInlineImage(const ImageInfo *image_info,
936   const char *content,ExceptionInfo *exception)
937 {
938   Image
939     *image;
940
941   ImageInfo
942     *read_info;
943
944   unsigned char
945     *blob;
946
947   size_t
948     length;
949
950   register const char
951     *p;
952
953   /*
954     Skip over header (e.g. data:image/gif;base64,).
955   */
956   image=NewImageList();
957   for (p=content; (*p != ',') && (*p != '\0'); p++) ;
958   if (*p == '\0')
959     ThrowReaderException(CorruptImageError,"CorruptImage");
960   p++;
961   length=0;
962   blob=Base64Decode(p,&length);
963   if (length == 0)
964     ThrowReaderException(CorruptImageError,"CorruptImage");
965   read_info=CloneImageInfo(image_info);
966   (void) SetImageInfoProgressMonitor(read_info,(MagickProgressMonitor) NULL,
967     (void *) NULL);
968   *read_info->filename='\0';
969   *read_info->magick='\0';
970   image=BlobToImage(read_info,blob,length,exception);
971   blob=(unsigned char *) RelinquishMagickMemory(blob);
972   read_info=DestroyImageInfo(read_info);
973   return(image);
974 }
975 \f
976 /*
977 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
978 %                                                                             %
979 %                                                                             %
980 %                                                                             %
981 %   W r i t e I m a g e                                                       %
982 %                                                                             %
983 %                                                                             %
984 %                                                                             %
985 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
986 %
987 %  WriteImage() writes an image or an image sequence to a file or file handle.
988 %  If writing to a file is on disk, the name is defined by the filename member
989 %  of the image structure.  WriteImage() returns MagickFalse is there is a
990 %  memory shortage or if the image cannot be written.  Check the exception
991 %  member of image to determine the cause for any failure.
992 %
993 %  The format of the WriteImage method is:
994 %
995 %      MagickBooleanType WriteImage(const ImageInfo *image_info,Image *image,
996 %        ExceptionInfo *exception)
997 %
998 %  A description of each parameter follows:
999 %
1000 %    o image_info: the image info.
1001 %
1002 %    o image: the image.
1003 %
1004 %    o exception: return any errors or warnings in this structure.
1005 %
1006 */
1007 MagickExport MagickBooleanType WriteImage(const ImageInfo *image_info,
1008   Image *image,ExceptionInfo *exception)
1009 {
1010   char
1011     filename[MaxTextExtent];
1012
1013   const char
1014     *option;
1015
1016   const DelegateInfo
1017     *delegate_info;
1018
1019   const MagickInfo
1020     *magick_info;
1021
1022   ExceptionInfo
1023     *sans_exception;
1024
1025   ImageInfo
1026     *write_info;
1027
1028   MagickBooleanType
1029     status,
1030     temporary;
1031
1032   MagickStatusType
1033     thread_support;
1034
1035   PolicyDomain
1036     domain;
1037
1038   PolicyRights
1039     rights;
1040
1041   /*
1042     Determine image type from filename prefix or suffix (e.g. image.jpg).
1043   */
1044   assert(image_info != (ImageInfo *) NULL);
1045   assert(image_info->signature == MagickSignature);
1046   if (image->debug != MagickFalse)
1047     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
1048       image_info->filename);
1049   assert(image != (Image *) NULL);
1050   assert(image->signature == MagickSignature);
1051   assert(exception != (ExceptionInfo *) NULL);
1052   sans_exception=AcquireExceptionInfo();
1053   write_info=CloneImageInfo(image_info);
1054   (void) CopyMagickString(write_info->filename,image->filename,MaxTextExtent);
1055   if (*write_info->magick == '\0')
1056     (void) CopyMagickString(write_info->magick,image->magick,MaxTextExtent);
1057   (void) SetImageInfo(write_info,1,sans_exception);
1058   (void) CopyMagickString(filename,image->filename,MaxTextExtent);
1059   (void) CopyMagickString(image->filename,write_info->filename,MaxTextExtent);
1060   domain=CoderPolicyDomain;
1061   rights=WritePolicyRights;
1062   if (IsRightsAuthorized(domain,rights,write_info->magick) == MagickFalse)
1063     {
1064       sans_exception=DestroyExceptionInfo(sans_exception);
1065       errno=EPERM;
1066       ThrowBinaryException(PolicyError,"NotAuthorized",filename);
1067     }
1068   magick_info=GetMagickInfo(write_info->magick,sans_exception);
1069   sans_exception=DestroyExceptionInfo(sans_exception);
1070   if (magick_info != (const MagickInfo *) NULL)
1071     {
1072       if (GetMagickEndianSupport(magick_info) == MagickFalse)
1073         image->endian=UndefinedEndian;
1074       else
1075         if ((image_info->endian == UndefinedEndian) &&
1076             (GetMagickRawSupport(magick_info) != MagickFalse))
1077           {
1078             size_t
1079               lsb_first;
1080
1081             lsb_first=1;
1082             image->endian=(*(char *) &lsb_first) == 1 ? LSBEndian : MSBEndian;
1083          }
1084     }
1085   (void) SyncImageProfiles(image);
1086   option=GetImageOption(image_info,"delegate:bimodal");
1087   if ((IfMagickTrue(IsStringTrue(option))) &&
1088       (write_info->page == (char *) NULL) &&
1089       (GetPreviousImageInList(image) == (Image *) NULL) &&
1090       (GetNextImageInList(image) == (Image *) NULL) &&
1091       (IfMagickFalse(IsTaintImage(image))) )
1092     {
1093       delegate_info=GetDelegateInfo(image->magick,write_info->magick,exception);
1094       if ((delegate_info != (const DelegateInfo *) NULL) &&
1095           (GetDelegateMode(delegate_info) == 0) &&
1096           (IsPathAccessible(image->magick_filename) != MagickFalse))
1097         {
1098           /*
1099             Process image with bi-modal delegate.
1100           */
1101           (void) CopyMagickString(image->filename,image->magick_filename,
1102             MaxTextExtent);
1103           status=InvokeDelegate(write_info,image,image->magick,
1104             write_info->magick,exception);
1105           write_info=DestroyImageInfo(write_info);
1106           (void) CopyMagickString(image->filename,filename,MaxTextExtent);
1107           return(status);
1108         }
1109     }
1110   status=MagickFalse;
1111   temporary=MagickFalse;
1112   if ((magick_info != (const MagickInfo *) NULL) &&
1113       (GetMagickSeekableStream(magick_info) != MagickFalse))
1114     {
1115       char
1116         filename[MaxTextExtent];
1117
1118       (void) CopyMagickString(filename,image->filename,MaxTextExtent);
1119       status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
1120       (void) CopyMagickString(image->filename,filename,MaxTextExtent);
1121       if (status != MagickFalse)
1122         {
1123           if (IsBlobSeekable(image) == MagickFalse)
1124             {
1125               /*
1126                 A seekable stream is required by the encoder.
1127               */
1128               write_info->adjoin=MagickTrue;
1129               (void) CopyMagickString(write_info->filename,image->filename,
1130                 MaxTextExtent);
1131               (void) AcquireUniqueFilename(image->filename);
1132               temporary=MagickTrue;
1133             }
1134           (void) CloseBlob(image);
1135         }
1136     }
1137   if (constitute_semaphore == (SemaphoreInfo *) NULL)
1138     AcquireSemaphoreInfo(&constitute_semaphore);
1139   if ((magick_info != (const MagickInfo *) NULL) &&
1140       (GetImageEncoder(magick_info) != (EncodeImageHandler *) NULL))
1141     {
1142       /*
1143         Call appropriate image writer based on image type.
1144       */
1145       thread_support=GetMagickThreadSupport(magick_info);
1146       if ((thread_support & EncoderThreadSupport) == 0)
1147         LockSemaphoreInfo(constitute_semaphore);
1148       status=GetImageEncoder(magick_info)(write_info,image,exception);
1149       if ((thread_support & EncoderThreadSupport) == 0)
1150         UnlockSemaphoreInfo(constitute_semaphore);
1151     }
1152   else
1153     {
1154       delegate_info=GetDelegateInfo((char *) NULL,write_info->magick,exception);
1155       if (delegate_info != (DelegateInfo *) NULL)
1156         {
1157           /*
1158             Process the image with delegate.
1159           */
1160           *write_info->filename='\0';
1161           if (GetDelegateThreadSupport(delegate_info) == MagickFalse)
1162             LockSemaphoreInfo(constitute_semaphore);
1163           status=InvokeDelegate(write_info,image,(char *) NULL,
1164             write_info->magick,exception);
1165           if (GetDelegateThreadSupport(delegate_info) == MagickFalse)
1166             UnlockSemaphoreInfo(constitute_semaphore);
1167           (void) CopyMagickString(image->filename,filename,MaxTextExtent);
1168         }
1169       else
1170         {
1171           sans_exception=AcquireExceptionInfo();
1172           magick_info=GetMagickInfo(write_info->magick,sans_exception);
1173           sans_exception=DestroyExceptionInfo(sans_exception);
1174           if ((write_info->affirm == MagickFalse) &&
1175               (magick_info == (const MagickInfo *) NULL))
1176             {
1177               (void) CopyMagickString(write_info->magick,image->magick,
1178                 MaxTextExtent);
1179               magick_info=GetMagickInfo(write_info->magick,exception);
1180             }
1181           if ((magick_info == (const MagickInfo *) NULL) ||
1182               (GetImageEncoder(magick_info) == (EncodeImageHandler *) NULL))
1183             {
1184               char
1185                 extension[MaxTextExtent];
1186
1187               GetPathComponent(image->filename,ExtensionPath,extension);
1188               if (*extension != '\0')
1189                 magick_info=GetMagickInfo(extension,exception);
1190               else
1191                 magick_info=GetMagickInfo(image->magick,exception);
1192               (void) CopyMagickString(image->filename,filename,MaxTextExtent);
1193             }
1194           if ((magick_info == (const MagickInfo *) NULL) ||
1195               (GetImageEncoder(magick_info) == (EncodeImageHandler *) NULL))
1196             {
1197               magick_info=GetMagickInfo(image->magick,exception);
1198               if ((magick_info == (const MagickInfo *) NULL) ||
1199                   (GetImageEncoder(magick_info) == (EncodeImageHandler *) NULL))
1200                 (void) ThrowMagickException(exception,GetMagickModule(),
1201                   MissingDelegateError,"NoEncodeDelegateForThisImageFormat",
1202                   "'%s'",image->filename);
1203               else
1204                 (void) ThrowMagickException(exception,GetMagickModule(),
1205                   MissingDelegateWarning,"NoEncodeDelegateForThisImageFormat",
1206                   "'%s'",image->filename);
1207             }
1208           if ((magick_info != (const MagickInfo *) NULL) &&
1209               (GetImageEncoder(magick_info) != (EncodeImageHandler *) NULL))
1210             {
1211               /*
1212                 Call appropriate image writer based on image type.
1213               */
1214               thread_support=GetMagickThreadSupport(magick_info);
1215               if ((thread_support & EncoderThreadSupport) == 0)
1216                 LockSemaphoreInfo(constitute_semaphore);
1217               status=GetImageEncoder(magick_info)(write_info,image,exception);
1218               if ((thread_support & EncoderThreadSupport) == 0)
1219                 UnlockSemaphoreInfo(constitute_semaphore);
1220             }
1221         }
1222     }
1223   if (GetBlobError(image) != MagickFalse)
1224     ThrowFileException(exception,FileOpenError,
1225       "AnErrorHasOccurredWritingToFile",image->filename);
1226   if (temporary == MagickTrue)
1227     {
1228       /*
1229         Copy temporary image file to permanent.
1230       */
1231       status=OpenBlob(write_info,image,ReadBinaryBlobMode,exception);
1232       if (status != MagickFalse)
1233         {
1234           (void) RelinquishUniqueFileResource(write_info->filename);
1235           status=ImageToFile(image,write_info->filename,exception);
1236         }
1237       (void) CloseBlob(image);
1238       (void) RelinquishUniqueFileResource(image->filename);
1239       (void) CopyMagickString(image->filename,write_info->filename,
1240         MaxTextExtent);
1241     }
1242   if ((LocaleCompare(write_info->magick,"info") != 0) &&
1243       (write_info->verbose != MagickFalse))
1244     (void) IdentifyImage(image,stdout,MagickFalse,exception);
1245   write_info=DestroyImageInfo(write_info);
1246   return(status);
1247 }
1248 \f
1249 /*
1250 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1251 %                                                                             %
1252 %                                                                             %
1253 %                                                                             %
1254 %   W r i t e I m a g e s                                                     %
1255 %                                                                             %
1256 %                                                                             %
1257 %                                                                             %
1258 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1259 %
1260 %  WriteImages() writes an image sequence into one or more files.  While
1261 %  WriteImage() can write an image sequence, it is limited to writing
1262 %  the sequence into a single file using a format which supports multiple
1263 %  frames.   WriteImages(), however, does not have this limitation, instead it
1264 %  generates multiple output files if necessary (or when requested).  When
1265 %  ImageInfo's adjoin flag is set to MagickFalse, the file name is expected
1266 %  to include a printf-style formatting string for the frame number (e.g.
1267 %  "image%02d.png").
1268 %
1269 %  The format of the WriteImages method is:
1270 %
1271 %      MagickBooleanType WriteImages(const ImageInfo *image_info,Image *images,
1272 %        const char *filename,ExceptionInfo *exception)
1273 %
1274 %  A description of each parameter follows:
1275 %
1276 %    o image_info: the image info.
1277 %
1278 %    o images: the image list.
1279 %
1280 %    o filename: the image filename.
1281 %
1282 %    o exception: return any errors or warnings in this structure.
1283 %
1284 */
1285 MagickExport MagickBooleanType WriteImages(const ImageInfo *image_info,
1286   Image *images,const char *filename,ExceptionInfo *exception)
1287 {
1288 #define WriteImageTag  "Write/Image"
1289
1290   BlobInfo
1291     *blob;
1292
1293   ExceptionInfo
1294     *sans_exception;
1295
1296   ImageInfo
1297     *write_info;
1298
1299   MagickBooleanType
1300     proceed;
1301
1302   MagickOffsetType
1303     i;
1304
1305   MagickProgressMonitor
1306     progress_monitor;
1307
1308   MagickSizeType
1309     number_images;
1310
1311   MagickStatusType
1312     status;
1313
1314   register Image
1315     *p;
1316
1317   assert(image_info != (const ImageInfo *) NULL);
1318   assert(image_info->signature == MagickSignature);
1319   assert(images != (Image *) NULL);
1320   assert(images->signature == MagickSignature);
1321   if (images->debug != MagickFalse)
1322     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",images->filename);
1323   assert(exception != (ExceptionInfo *) NULL);
1324   write_info=CloneImageInfo(image_info);
1325   images=GetFirstImageInList(images);
1326   blob=CloneBlobInfo(images->blob);  /* thread specific I/O handler */
1327   DestroyBlob(images);
1328   images->blob=blob;
1329   if (filename != (const char *) NULL)
1330     for (p=images; p != (Image *) NULL; p=GetNextImageInList(p))
1331       (void) CopyMagickString(p->filename,filename,MaxTextExtent);
1332   (void) CopyMagickString(write_info->filename,images->filename,MaxTextExtent);
1333   if (*write_info->magick == '\0')
1334     (void) CopyMagickString(write_info->magick,images->magick,MaxTextExtent);
1335   sans_exception=AcquireExceptionInfo();
1336   (void) SetImageInfo(write_info,(unsigned int) GetImageListLength(images),
1337     sans_exception);
1338   sans_exception=DestroyExceptionInfo(sans_exception);
1339   p=images;
1340   for ( ; GetNextImageInList(p) != (Image *) NULL; p=GetNextImageInList(p))
1341     if (p->scene >= GetNextImageInList(p)->scene)
1342       {
1343         register ssize_t
1344           i;
1345
1346         /*
1347           Generate consistent scene numbers.
1348         */
1349         i=(ssize_t) images->scene;
1350         for (p=images; p != (Image *) NULL; p=GetNextImageInList(p))
1351           p->scene=(size_t) i++;
1352         break;
1353       }
1354   /*
1355     Write images.
1356   */
1357   status=MagickTrue;
1358   progress_monitor=(MagickProgressMonitor) NULL;
1359   i=0;
1360   number_images=GetImageListLength(images);
1361   for (p=images; p != (Image *) NULL; p=GetNextImageInList(p))
1362   {
1363     if (number_images != 1)
1364       progress_monitor=SetImageProgressMonitor(p,(MagickProgressMonitor) NULL,
1365         p->client_data);
1366     status&=WriteImage(write_info,p,exception);
1367     if (number_images != 1)
1368       (void) SetImageProgressMonitor(p,progress_monitor,p->client_data);
1369     if (write_info->adjoin != MagickFalse)
1370       break;
1371     if (number_images != 1)
1372       {
1373         proceed=SetImageProgress(p,WriteImageTag,i++,number_images);
1374         if (proceed == MagickFalse)
1375           break;
1376       }
1377   }
1378   write_info=DestroyImageInfo(write_info);
1379   return(status != 0 ? MagickTrue : MagickFalse);
1380 }