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