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