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