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