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