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