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