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