MagickCore  6.9.13-43
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/license/ %
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 *module,
422  const char *coder,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  if (IsRightsAuthorized(ModulePolicyDomain,rights,module) == MagickFalse)
432  {
433  errno=EPERM;
434  (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
435  "NotAuthorized","`%s'",module);
436  return(MagickFalse);
437  }
438  return(MagickTrue);
439 }
440 
441 MagickExport Image *ReadImage(const ImageInfo *image_info,
442  ExceptionInfo *exception)
443 {
444  char
445  filename[MaxTextExtent],
446  magick[MaxTextExtent],
447  magick_filename[MaxTextExtent];
448 
449  const char
450  *value;
451 
452  const DelegateInfo
453  *delegate_info;
454 
455  const MagickInfo
456  *magick_info;
457 
459  *sans_exception;
460 
462  geometry_info;
463 
464  Image
465  *image,
466  *next;
467 
468  ImageInfo
469  *read_info;
470 
471  MagickBooleanType
472  status;
473 
474  MagickStatusType
475  flags,
476  thread_support;
477 
478  /*
479  Determine image type from filename prefix or suffix (e.g. image.jpg).
480  */
481  assert(image_info != (ImageInfo *) NULL);
482  assert(image_info->signature == MagickCoreSignature);
483  assert(image_info->filename != (char *) NULL);
484  assert(exception != (ExceptionInfo *) NULL);
485  if (IsEventLogging() != MagickFalse)
486  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
487  image_info->filename);
488  read_info=CloneImageInfo(image_info);
489  (void) CopyMagickString(magick_filename,read_info->filename,MaxTextExtent);
490  (void) SetImageInfo(read_info,0,exception);
491  (void) CopyMagickString(filename,read_info->filename,MaxTextExtent);
492  (void) CopyMagickString(magick,read_info->magick,MaxTextExtent);
493  /*
494  Call appropriate image reader based on image type.
495  */
496  sans_exception=AcquireExceptionInfo();
497  magick_info=GetMagickInfo(read_info->magick,sans_exception);
498  if (sans_exception->severity == PolicyError)
499  magick_info=GetMagickInfo(read_info->magick,exception);
500  sans_exception=DestroyExceptionInfo(sans_exception);
501  if ((magick_info != (const MagickInfo *) NULL) &&
502  (GetMagickRawSupport(magick_info) != MagickFalse))
503  {
504  if (GetMagickEndianSupport(magick_info) == MagickFalse)
505  read_info->endian=UndefinedEndian;
506  else
507  if (image_info->endian == UndefinedEndian)
508  {
509  unsigned long
510  lsb_first;
511 
512  lsb_first=1;
513  read_info->endian=(*(char *) &lsb_first) == 1 ? LSBEndian :
514  MSBEndian;
515  }
516  }
517  if ((magick_info != (const MagickInfo *) NULL) &&
518  (GetMagickSeekableStream(magick_info) != MagickFalse))
519  {
520  MagickBooleanType
521  status;
522 
523  image=AcquireImage(read_info);
524  (void) CopyMagickString(image->filename,read_info->filename,
525  MaxTextExtent);
526  status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
527  if (status == MagickFalse)
528  {
529  read_info=DestroyImageInfo(read_info);
530  image=DestroyImage(image);
531  return((Image *) NULL);
532  }
533  if (IsBlobSeekable(image) == MagickFalse)
534  {
535  /*
536  Coder requires a seekable stream.
537  */
538  *read_info->filename='\0';
539  status=ImageToFile(image,read_info->filename,exception);
540  if (status == MagickFalse)
541  {
542  (void) CloseBlob(image);
543  read_info=DestroyImageInfo(read_info);
544  image=DestroyImage(image);
545  return((Image *) NULL);
546  }
547  read_info->temporary=MagickTrue;
548  }
549  (void) CloseBlob(image);
550  image=DestroyImage(image);
551  }
552  image=NewImageList();
553  if ((magick_info == (const MagickInfo *) NULL) ||
554  (GetImageDecoder(magick_info) == (DecodeImageHandler *) NULL))
555  {
556  delegate_info=GetDelegateInfo(read_info->magick,(char *) NULL,exception);
557  if (delegate_info == (const DelegateInfo *) NULL)
558  {
559  (void) SetImageInfo(read_info,0,exception);
560  (void) CopyMagickString(read_info->filename,filename,MaxTextExtent);
561  magick_info=GetMagickInfo(read_info->magick,exception);
562  }
563  }
564  if ((magick_info != (const MagickInfo *) NULL) &&
565  (GetImageDecoder(magick_info) != (DecodeImageHandler *) NULL))
566  {
567  /*
568  Call appropriate image reader based on image type.
569  */
570  thread_support=GetMagickThreadSupport(magick_info);
571  if ((thread_support & DecoderThreadSupport) == 0)
572  LockSemaphoreInfo(magick_info->semaphore);
573  status=IsCoderAuthorized(magick_info->magick_module,read_info->magick,
574  ReadPolicyRights,exception);
575  image=(Image *) NULL;
576  if (status != MagickFalse)
577  image=GetImageDecoder(magick_info)(read_info,exception);
578  if ((thread_support & DecoderThreadSupport) == 0)
579  UnlockSemaphoreInfo(magick_info->semaphore);
580  }
581  else
582  {
583  MagickBooleanType
584  status;
585 
586  delegate_info=GetDelegateInfo(read_info->magick,(char *) NULL,exception);
587  if (delegate_info == (const DelegateInfo *) NULL)
588  {
589  (void) ThrowMagickException(exception,GetMagickModule(),
590  MissingDelegateError,"NoDecodeDelegateForThisImageFormat","`%s'",
591  read_info->filename);
592  if (read_info->temporary != MagickFalse)
593  (void) RelinquishUniqueFileResource(read_info->filename);
594  read_info=DestroyImageInfo(read_info);
595  return((Image *) NULL);
596  }
597  /*
598  Let our decoding delegate process the image.
599  */
600  image=AcquireImage(read_info);
601  if (image == (Image *) NULL)
602  {
603  read_info=DestroyImageInfo(read_info);
604  return((Image *) NULL);
605  }
606  (void) CopyMagickString(image->filename,read_info->filename,
607  MaxTextExtent);
608  *read_info->filename='\0';
609  if (GetDelegateThreadSupport(delegate_info) == MagickFalse)
610  LockSemaphoreInfo(delegate_info->semaphore);
611  status=InvokeDelegate(read_info,image,read_info->magick,(char *) NULL,
612  exception);
613  if (GetDelegateThreadSupport(delegate_info) == MagickFalse)
614  UnlockSemaphoreInfo(delegate_info->semaphore);
615  image=DestroyImageList(image);
616  read_info->temporary=MagickTrue;
617  if (status != MagickFalse)
618  (void) SetImageInfo(read_info,0,exception);
619  magick_info=GetMagickInfo(read_info->magick,exception);
620  if ((magick_info == (const MagickInfo *) NULL) ||
621  (GetImageDecoder(magick_info) == (DecodeImageHandler *) NULL))
622  {
623  if (IsPathAccessible(read_info->filename) != MagickFalse)
624  (void) ThrowMagickException(exception,GetMagickModule(),
625  MissingDelegateError,"NoDecodeDelegateForThisImageFormat","`%s'",
626  read_info->magick);
627  else
628  ThrowFileException(exception,FileOpenError,"UnableToOpenFile",
629  read_info->filename);
630  read_info=DestroyImageInfo(read_info);
631  return((Image *) NULL);
632  }
633  /*
634  Call appropriate image reader based on image type.
635  */
636  thread_support=GetMagickThreadSupport(magick_info);
637  if ((thread_support & DecoderThreadSupport) == 0)
638  LockSemaphoreInfo(magick_info->semaphore);
639  status=IsCoderAuthorized(magick_info->magick_module,read_info->magick,
640  ReadPolicyRights,exception);
641  image=(Image *) NULL;
642  if (status != MagickFalse)
643  image=(Image *) (GetImageDecoder(magick_info))(read_info,exception);
644  if ((thread_support & DecoderThreadSupport) == 0)
645  UnlockSemaphoreInfo(magick_info->semaphore);
646  }
647  if (read_info->temporary != MagickFalse)
648  {
649  (void) RelinquishUniqueFileResource(read_info->filename);
650  read_info->temporary=MagickFalse;
651  if (image != (Image *) NULL)
652  (void) CopyMagickString(image->filename,filename,MaxTextExtent);
653  }
654  if (image == (Image *) NULL)
655  {
656  read_info=DestroyImageInfo(read_info);
657  return(image);
658  }
659  if (exception->severity >= ErrorException)
660  (void) LogMagickEvent(ExceptionEvent,GetMagickModule(),
661  "Coder (%s) generated an image despite an error (%d), "
662  "notify the developers",image->magick,exception->severity);
663  if (IsBlobTemporary(image) != MagickFalse)
664  (void) RelinquishUniqueFileResource(read_info->filename);
665  if ((IsSceneGeometry(read_info->scenes,MagickFalse) != MagickFalse) &&
666  (GetImageListLength(image) != 1))
667  {
668  Image
669  *clones;
670 
671  clones=CloneImages(image,read_info->scenes,exception);
672  image=DestroyImageList(image);
673  if (clones != (Image *) NULL)
674  image=GetFirstImageInList(clones);
675  if (image == (Image *) NULL)
676  {
677  read_info=DestroyImageInfo(read_info);
678  return(image);
679  }
680  }
681  for (next=image; next != (Image *) NULL; next=GetNextImageInList(next))
682  {
683  char
684  magick_path[MaxTextExtent],
685  *property,
686  timestamp[MaxTextExtent];
687 
688  const char
689  *option;
690 
691  const StringInfo
692  *profile;
693 
694  ssize_t
695  option_type;
696 
697  static const char
698  *source_date_epoch = (const char *) NULL;
699 
700  static MagickBooleanType
701  epoch_initialized = MagickFalse;
702 
703  next->taint=MagickFalse;
704  GetPathComponent(magick_filename,MagickPath,magick_path);
705  if ((*magick_path == '\0') && (*next->magick == '\0'))
706  (void) CopyMagickString(next->magick,magick,MaxTextExtent);
707  (void) CopyMagickString(next->magick_filename,magick_filename,
708  MaxTextExtent);
709  if (IsBlobTemporary(image) != MagickFalse)
710  (void) CopyMagickString(next->filename,filename,MaxTextExtent);
711  if (next->magick_columns == 0)
712  next->magick_columns=next->columns;
713  if (next->magick_rows == 0)
714  next->magick_rows=next->rows;
715  (void) GetImageProperty(next,"exif:*");
716  (void) GetImageProperty(next,"icc:*");
717  (void) GetImageProperty(next,"iptc:*");
718  (void) GetImageProperty(next,"xmp:*");
719  value=GetImageProperty(next,"exif:Orientation");
720  if (value == (char *) NULL)
721  value=GetImageProperty(next,"tiff:Orientation");
722  if (value != (char *) NULL)
723  {
724  next->orientation=(OrientationType) StringToLong(value);
725  (void) DeleteImageProperty(next,"tiff:Orientation");
726  (void) DeleteImageProperty(next,"exif:Orientation");
727  }
728  value=GetImageProperty(next,"exif:XResolution");
729  if (value != (char *) NULL)
730  {
731  geometry_info.rho=next->x_resolution;
732  geometry_info.sigma=1.0;
733  (void) ParseGeometry(value,&geometry_info);
734  if (geometry_info.sigma != 0)
735  next->x_resolution=geometry_info.rho/geometry_info.sigma;
736  if (strchr(value,',') != (char *) NULL)
737  next->x_resolution=geometry_info.rho+geometry_info.sigma/1000.0;
738  (void) DeleteImageProperty(next,"exif:XResolution");
739  }
740  value=GetImageProperty(next,"exif:YResolution");
741  if (value != (char *) NULL)
742  {
743  geometry_info.rho=next->y_resolution;
744  geometry_info.sigma=1.0;
745  (void) ParseGeometry(value,&geometry_info);
746  if (geometry_info.sigma != 0)
747  next->y_resolution=geometry_info.rho/geometry_info.sigma;
748  if (strchr(value,',') != (char *) NULL)
749  next->y_resolution=geometry_info.rho+geometry_info.sigma/1000.0;
750  (void) DeleteImageProperty(next,"exif:YResolution");
751  }
752  value=GetImageProperty(next,"exif:ResolutionUnit");
753  if (value == (char *) NULL)
754  value=GetImageProperty(next,"tiff:ResolutionUnit");
755  if (value != (char *) NULL)
756  {
757  option_type=ParseCommandOption(MagickResolutionOptions,MagickFalse,
758  value);
759  if (option_type >= 0)
760  next->units=(ResolutionType) option_type;
761  (void) DeleteImageProperty(next,"exif:ResolutionUnit");
762  (void) DeleteImageProperty(next,"tiff:ResolutionUnit");
763  }
764  if (next->page.width == 0)
765  next->page.width=next->columns;
766  if (next->page.height == 0)
767  next->page.height=next->rows;
768  option=GetImageOption(read_info,"caption");
769  if (option != (const char *) NULL)
770  {
771  property=InterpretImageProperties(read_info,next,option);
772  (void) SetImageProperty(next,"caption",property);
773  property=DestroyString(property);
774  }
775  option=GetImageOption(read_info,"comment");
776  if (option != (const char *) NULL)
777  {
778  property=InterpretImageProperties(read_info,next,option);
779  (void) SetImageProperty(next,"comment",property);
780  property=DestroyString(property);
781  }
782  option=GetImageOption(read_info,"label");
783  if (option != (const char *) NULL)
784  {
785  property=InterpretImageProperties(read_info,next,option);
786  (void) SetImageProperty(next,"label",property);
787  property=DestroyString(property);
788  }
789  if (LocaleCompare(next->magick,"TEXT") == 0)
790  (void) ParseAbsoluteGeometry("0x0+0+0",&next->page);
791  if ((read_info->extract != (char *) NULL) &&
792  (read_info->stream == (StreamHandler) NULL))
793  {
795  geometry;
796 
797  SetGeometry(next,&geometry);
798  flags=ParseAbsoluteGeometry(read_info->extract,&geometry);
799  if ((next->columns != geometry.width) ||
800  (next->rows != geometry.height))
801  {
802  if (((flags & XValue) != 0) || ((flags & YValue) != 0))
803  {
804  Image
805  *crop_image;
806 
807  crop_image=CropImage(next,&geometry,exception);
808  if (crop_image != (Image *) NULL)
809  ReplaceImageInList(&next,crop_image);
810  }
811  else
812  if (((flags & WidthValue) != 0) || ((flags & HeightValue) != 0))
813  {
814  (void) ParseRegionGeometry(next,read_info->extract,&geometry,
815  exception);
816  if ((geometry.width != 0) && (geometry.height != 0))
817  {
818  Image *resize_image=ResizeImage(next,geometry.width,
819  geometry.height,next->filter,next->blur,exception);
820  if (resize_image != (Image *) NULL)
821  ReplaceImageInList(&next,resize_image);
822  }
823  }
824  }
825  }
826  profile=GetImageProfile(next,"icc");
827  if (profile == (const StringInfo *) NULL)
828  profile=GetImageProfile(next,"icm");
829  if (profile != (const StringInfo *) NULL)
830  {
831  next->color_profile.length=GetStringInfoLength(profile);
832  next->color_profile.info=GetStringInfoDatum(profile);
833  }
834  profile=GetImageProfile(next,"iptc");
835  if (profile == (const StringInfo *) NULL)
836  profile=GetImageProfile(next,"8bim");
837  if (profile != (const StringInfo *) NULL)
838  {
839  next->iptc_profile.length=GetStringInfoLength(profile);
840  next->iptc_profile.info=GetStringInfoDatum(profile);
841  }
842  if (epoch_initialized == MagickFalse)
843  {
844  source_date_epoch=getenv("SOURCE_DATE_EPOCH");
845  epoch_initialized=MagickTrue;
846  }
847  if (source_date_epoch == (const char *) NULL)
848  {
849  (void) FormatMagickTime(image->timestamp,MaxTextExtent,timestamp);
850  (void) SetImageProperty(next,"date:timestamp",timestamp);
851  (void) FormatMagickTime((time_t) GetBlobProperties(next)->st_mtime,
852  MaxTextExtent,timestamp);
853  (void) SetImageProperty(next,"date:modify",timestamp);
854  (void) FormatMagickTime((time_t) GetBlobProperties(next)->st_ctime,
855  MaxTextExtent,timestamp);
856  (void) SetImageProperty(next,"date:create",timestamp);
857  }
858  option=GetImageOption(image_info,"delay");
859  if (option != (const char *) NULL)
860  {
862  geometry_info;
863 
864  flags=ParseGeometry(option,&geometry_info);
865  if ((flags & GreaterValue) != 0)
866  {
867  if (next->delay > (size_t) floor(geometry_info.rho+0.5))
868  next->delay=(size_t) floor(geometry_info.rho+0.5);
869  }
870  else
871  if ((flags & LessValue) != 0)
872  {
873  if (next->delay < (size_t) floor(geometry_info.rho+0.5))
874  next->delay=(size_t) floor(geometry_info.rho+0.5);
875  }
876  else
877  next->delay=(size_t) floor(geometry_info.rho+0.5);
878  if ((flags & SigmaValue) != 0)
879  next->ticks_per_second=CastDoubleToLong(floor(
880  geometry_info.sigma+0.5));
881  }
882  option=GetImageOption(image_info,"dispose");
883  if (option != (const char *) NULL)
884  {
885  option_type=ParseCommandOption(MagickDisposeOptions,MagickFalse,
886  option);
887  if (option_type >= 0)
888  next->dispose=(DisposeType) option_type;
889  }
890  if (read_info->verbose != MagickFalse)
891  (void) IdentifyImage(next,stderr,MagickFalse);
892  image=next;
893  }
894  read_info=DestroyImageInfo(read_info);
895  if (GetBlobError(image) != MagickFalse)
896  ThrowReaderException(CorruptImageError,"UnableToReadImageData");
897  return(GetFirstImageInList(image));
898 }
899 ␌
900 /*
901 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
902 % %
903 % %
904 % %
905 % R e a d I m a g e s %
906 % %
907 % %
908 % %
909 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
910 %
911 % ReadImages() reads one or more images and returns them as an image list.
912 %
913 % The format of the ReadImage method is:
914 %
915 % Image *ReadImages(const ImageInfo *image_info,ExceptionInfo *exception)
916 %
917 % A description of each parameter follows:
918 %
919 % o image_info: the image info.
920 %
921 % o exception: return any errors or warnings in this structure.
922 %
923 */
924 MagickExport Image *ReadImages(const ImageInfo *image_info,
925  ExceptionInfo *exception)
926 {
927  char
928  filename[MaxTextExtent];
929 
930  Image
931  *image,
932  *images;
933 
934  ImageInfo
935  *read_info;
936 
937  /*
938  Read image list from a file.
939  */
940  assert(image_info != (ImageInfo *) NULL);
941  assert(image_info->signature == MagickCoreSignature);
942  assert(exception != (ExceptionInfo *) NULL);
943  if (IsEventLogging() != MagickFalse)
944  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
945  image_info->filename);
946  read_info=CloneImageInfo(image_info);
947  *read_info->magick='\0';
948  (void) InterpretImageFilename(read_info,(Image *) NULL,read_info->filename,
949  (int) read_info->scene,filename);
950  if (LocaleCompare(filename,read_info->filename) != 0)
951  {
953  *sans;
954 
955  ssize_t
956  extent,
957  scene;
958 
959  /*
960  Images of the form image-%d.png[1-5].
961  */
962  sans=AcquireExceptionInfo();
963  (void) SetImageInfo(read_info,0,sans);
964  sans=DestroyExceptionInfo(sans);
965  if (read_info->number_scenes == 0)
966  {
967  read_info=DestroyImageInfo(read_info);
968  return(ReadImage(image_info,exception));
969  }
970  (void) CopyMagickString(filename,read_info->filename,MaxTextExtent);
971  images=NewImageList();
972  extent=(ssize_t) (read_info->scene+read_info->number_scenes);
973  for (scene=(ssize_t) read_info->scene; scene < (ssize_t) extent; scene++)
974  {
975  (void) InterpretImageFilename(image_info,(Image *) NULL,filename,(int)
976  scene,read_info->filename);
977  image=ReadImage(read_info,exception);
978  if (image == (Image *) NULL)
979  continue;
980  AppendImageToList(&images,image);
981  }
982  read_info=DestroyImageInfo(read_info);
983  return(images);
984  }
985  image=ReadImage(read_info,exception);
986  read_info=DestroyImageInfo(read_info);
987  return(image);
988 }
989 ␌
990 /*
991 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
992 % %
993 % %
994 % %
995 + R e a d I n l i n e I m a g e %
996 % %
997 % %
998 % %
999 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1000 %
1001 % ReadInlineImage() reads a Base64-encoded inline image or image sequence.
1002 % The method returns a NULL if there is a memory shortage or if the image
1003 % cannot be read. On failure, a NULL image is returned and exception
1004 % describes the reason for the failure.
1005 %
1006 % The format of the ReadInlineImage method is:
1007 %
1008 % Image *ReadInlineImage(const ImageInfo *image_info,const char *content,
1009 % ExceptionInfo *exception)
1010 %
1011 % A description of each parameter follows:
1012 %
1013 % o image_info: the image info.
1014 %
1015 % o content: the image encoded in Base64.
1016 %
1017 % o exception: return any errors or warnings in this structure.
1018 %
1019 */
1020 MagickExport Image *ReadInlineImage(const ImageInfo *image_info,
1021  const char *content,ExceptionInfo *exception)
1022 {
1023  Image
1024  *image;
1025 
1026  ImageInfo
1027  *read_info;
1028 
1029  unsigned char
1030  *blob;
1031 
1032  size_t
1033  length;
1034 
1035  const char
1036  *p;
1037 
1038  /*
1039  Skip over header (e.g. data:image/gif;base64,).
1040  */
1041  image=NewImageList();
1042  for (p=content; (*p != ',') && (*p != '\0'); p++) ;
1043  if (*p == '\0')
1044  ThrowReaderException(CorruptImageError,"CorruptImage");
1045  blob=Base64Decode(++p,&length);
1046  if (length == 0)
1047  {
1048  blob=(unsigned char *) RelinquishMagickMemory(blob);
1049  ThrowReaderException(CorruptImageError,"CorruptImage");
1050  }
1051  read_info=CloneImageInfo(image_info);
1052  (void) SetImageInfoProgressMonitor(read_info,(MagickProgressMonitor) NULL,
1053  (void *) NULL);
1054  *read_info->filename='\0';
1055  *read_info->magick='\0';
1056  for (p=content; (*p != '/') && (*p != '\0'); p++) ;
1057  if (*p != '\0')
1058  {
1059  char
1060  *q;
1061 
1062  ssize_t
1063  i;
1064 
1065  /*
1066  Extract media type.
1067  */
1068  if (LocaleNCompare(++p,"x-",2) == 0)
1069  p+=(ptrdiff_t) 2;
1070  (void) CopyMagickString(read_info->filename,"data.",MagickPathExtent);
1071  q=read_info->filename+5;
1072  for (i=0; (*p != ';') && (*p != '\0') && (i < (MagickPathExtent-6)); i++)
1073  *q++=(*p++);
1074  *q++='\0';
1075  }
1076  image=BlobToImage(read_info,blob,length,exception);
1077  blob=(unsigned char *) RelinquishMagickMemory(blob);
1078  read_info=DestroyImageInfo(read_info);
1079  return(image);
1080 }
1081 ␌
1082 /*
1083 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1084 % %
1085 % %
1086 % %
1087 % W r i t e I m a g e %
1088 % %
1089 % %
1090 % %
1091 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1092 %
1093 % WriteImage() writes an image or an image sequence to a file or file handle.
1094 % If writing to a file is on disk, the name is defined by the filename member
1095 % of the image structure. WriteImage() returns MagickFalse is there is a
1096 % memory shortage or if the image cannot be written. Check the exception
1097 % member of image to determine the cause for any failure.
1098 %
1099 % The format of the WriteImage method is:
1100 %
1101 % MagickBooleanType WriteImage(const ImageInfo *image_info,Image *image)
1102 %
1103 % A description of each parameter follows:
1104 %
1105 % o image_info: the image info.
1106 %
1107 % o image: the image.
1108 %
1109 */
1110 MagickExport MagickBooleanType WriteImage(const ImageInfo *image_info,
1111  Image *image)
1112 {
1113  char
1114  filename[MaxTextExtent];
1115 
1116  const char
1117  *option;
1118 
1119  const DelegateInfo
1120  *delegate_info;
1121 
1122  const MagickInfo
1123  *magick_info;
1124 
1126  *exception,
1127  *sans_exception;
1128 
1129  ImageInfo
1130  *write_info;
1131 
1132  MagickBooleanType
1133  status,
1134  temporary;
1135 
1136  MagickStatusType
1137  thread_support;
1138 
1139  /*
1140  Determine image type from filename prefix or suffix (e.g. image.jpg).
1141  */
1142  assert(image_info != (ImageInfo *) NULL);
1143  assert(image_info->signature == MagickCoreSignature);
1144  assert(image != (Image *) NULL);
1145  assert(image->signature == MagickCoreSignature);
1146  if (IsEventLogging() != MagickFalse)
1147  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
1148  image_info->filename);
1149  exception=(&image->exception);
1150  sans_exception=AcquireExceptionInfo();
1151  write_info=CloneImageInfo(image_info);
1152  (void) CopyMagickString(write_info->filename,image->filename,MaxTextExtent);
1153  (void) SetImageInfo(write_info,1,sans_exception);
1154  if (*write_info->magick == '\0')
1155  (void) CopyMagickString(write_info->magick,image->magick,MaxTextExtent);
1156  if (LocaleCompare(write_info->magick,"clipmask") == 0)
1157  {
1158  if (image->clip_mask == (Image *) NULL)
1159  {
1160  (void) ThrowMagickException(exception,GetMagickModule(),
1161  OptionError,"NoClipPathDefined","`%s'",image->filename);
1162  write_info=DestroyImageInfo(write_info);
1163  return(MagickFalse);
1164  }
1165  image=image->clip_mask;
1166  (void) SetImageInfo(write_info,1,sans_exception);
1167  }
1168  (void) CopyMagickString(filename,image->filename,MaxTextExtent);
1169  (void) CopyMagickString(image->filename,write_info->filename,MaxTextExtent);
1170  /*
1171  Call appropriate image writer based on image type.
1172  */
1173  magick_info=GetMagickInfo(write_info->magick,sans_exception);
1174  if (sans_exception->severity == PolicyError)
1175  magick_info=GetMagickInfo(write_info->magick,exception);
1176  sans_exception=DestroyExceptionInfo(sans_exception);
1177  if (magick_info != (const MagickInfo *) NULL)
1178  {
1179  if (GetMagickEndianSupport(magick_info) == MagickFalse)
1180  image->endian=UndefinedEndian;
1181  else
1182  if ((image_info->endian == UndefinedEndian) &&
1183  (GetMagickRawSupport(magick_info) != MagickFalse))
1184  {
1185  unsigned long
1186  lsb_first;
1187 
1188  lsb_first=1;
1189  image->endian=(*(char *) &lsb_first) == 1 ? LSBEndian : MSBEndian;
1190  }
1191  }
1192  (void) SyncImageProfiles(image);
1193  DisassociateImageStream(image);
1194  option=GetImageOption(image_info,"delegate:bimodal");
1195  if ((option != (const char *) NULL) &&
1196  (IsMagickTrue(option) != MagickFalse) &&
1197  (write_info->page == (char *) NULL) &&
1198  (GetPreviousImageInList(image) == (Image *) NULL) &&
1199  (GetNextImageInList(image) == (Image *) NULL) &&
1200  (IsTaintImage(image) == MagickFalse))
1201  {
1202  delegate_info=GetDelegateInfo(image->magick,write_info->magick,
1203  exception);
1204  if ((delegate_info != (const DelegateInfo *) NULL) &&
1205  (GetDelegateMode(delegate_info) == 0) &&
1206  (IsPathAccessible(image->magick_filename) != MagickFalse))
1207  {
1208  /*
1209  Process image with bi-modal delegate.
1210  */
1211  (void) CopyMagickString(image->filename,image->magick_filename,
1212  MaxTextExtent);
1213  status=InvokeDelegate(write_info,image,image->magick,
1214  write_info->magick,exception);
1215  write_info=DestroyImageInfo(write_info);
1216  (void) CopyMagickString(image->filename,filename,MaxTextExtent);
1217  return(status);
1218  }
1219  }
1220  status=MagickFalse;
1221  temporary=MagickFalse;
1222  if ((magick_info != (const MagickInfo *) NULL) &&
1223  (GetMagickSeekableStream(magick_info) != MagickFalse))
1224  {
1225  char
1226  filename[MaxTextExtent];
1227 
1228  (void) CopyMagickString(filename,image->filename,MaxTextExtent);
1229  status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
1230  (void) CopyMagickString(image->filename,filename,MaxTextExtent);
1231  if (status != MagickFalse)
1232  {
1233  if (IsBlobSeekable(image) == MagickFalse)
1234  {
1235  /*
1236  A seekable stream is required by the encoder.
1237  */
1238  write_info->adjoin=MagickTrue;
1239  (void) CopyMagickString(write_info->filename,image->filename,
1240  MaxTextExtent);
1241  (void) AcquireUniqueFilename(image->filename);
1242  temporary=MagickTrue;
1243  }
1244  if (CloseBlob(image) == MagickFalse)
1245  status=MagickFalse;
1246  }
1247  }
1248  if ((magick_info != (const MagickInfo *) NULL) &&
1249  (GetImageEncoder(magick_info) != (EncodeImageHandler *) NULL))
1250  {
1251  /*
1252  Call appropriate image writer based on image type.
1253  */
1254  thread_support=GetMagickThreadSupport(magick_info);
1255  if ((thread_support & EncoderThreadSupport) == 0)
1256  LockSemaphoreInfo(magick_info->semaphore);
1257  status=IsCoderAuthorized(magick_info->magick_module,write_info->magick,
1258  WritePolicyRights,exception);
1259  if (status != MagickFalse)
1260  status=GetImageEncoder(magick_info)(write_info,image);
1261  if ((thread_support & EncoderThreadSupport) == 0)
1262  UnlockSemaphoreInfo(magick_info->semaphore);
1263  }
1264  else
1265  {
1266  delegate_info=GetDelegateInfo((char *) NULL,write_info->magick,
1267  exception);
1268  if (delegate_info != (DelegateInfo *) NULL)
1269  {
1270  /*
1271  Process the image with delegate.
1272  */
1273  *write_info->filename='\0';
1274  if (GetDelegateThreadSupport(delegate_info) == MagickFalse)
1275  LockSemaphoreInfo(delegate_info->semaphore);
1276  status=InvokeDelegate(write_info,image,(char *) NULL,
1277  write_info->magick,exception);
1278  if (GetDelegateThreadSupport(delegate_info) == MagickFalse)
1279  UnlockSemaphoreInfo(delegate_info->semaphore);
1280  (void) CopyMagickString(image->filename,filename,MaxTextExtent);
1281  }
1282  else
1283  {
1284  sans_exception=AcquireExceptionInfo();
1285  magick_info=GetMagickInfo(write_info->magick,sans_exception);
1286  if (sans_exception->severity == PolicyError)
1287  magick_info=GetMagickInfo(write_info->magick,exception);
1288  sans_exception=DestroyExceptionInfo(sans_exception);
1289  if ((write_info->affirm == MagickFalse) &&
1290  (magick_info == (const MagickInfo *) NULL))
1291  {
1292  (void) CopyMagickString(write_info->magick,image->magick,
1293  MaxTextExtent);
1294  magick_info=GetMagickInfo(write_info->magick,exception);
1295  }
1296  if ((magick_info == (const MagickInfo *) NULL) ||
1297  (GetImageEncoder(magick_info) == (EncodeImageHandler *) NULL))
1298  {
1299  char
1300  extension[MaxTextExtent];
1301 
1302  GetPathComponent(image->filename,ExtensionPath,extension);
1303  if (*extension != '\0')
1304  magick_info=GetMagickInfo(extension,exception);
1305  else
1306  magick_info=GetMagickInfo(image->magick,exception);
1307  (void) CopyMagickString(image->filename,filename,MaxTextExtent);
1308  }
1309  if ((magick_info == (const MagickInfo *) NULL) ||
1310  (GetImageEncoder(magick_info) == (EncodeImageHandler *) NULL))
1311  {
1312  magick_info=GetMagickInfo(image->magick,exception);
1313  if ((magick_info == (const MagickInfo *) NULL) ||
1314  (GetImageEncoder(magick_info) == (EncodeImageHandler *) NULL))
1315  (void) ThrowMagickException(exception,GetMagickModule(),
1316  MissingDelegateError,"NoEncodeDelegateForThisImageFormat",
1317  "`%s'",write_info->magick);
1318  else
1319  (void) ThrowMagickException(exception,GetMagickModule(),
1320  MissingDelegateWarning,"NoEncodeDelegateForThisImageFormat",
1321  "`%s'",write_info->magick);
1322  }
1323  if ((magick_info != (const MagickInfo *) NULL) &&
1324  (GetImageEncoder(magick_info) != (EncodeImageHandler *) NULL))
1325  {
1326  /*
1327  Call appropriate image writer based on image type.
1328  */
1329  thread_support=GetMagickThreadSupport(magick_info);
1330  if ((thread_support & EncoderThreadSupport) == 0)
1331  LockSemaphoreInfo(magick_info->semaphore);
1332  status=IsCoderAuthorized(magick_info->magick_module,write_info->magick,
1333  WritePolicyRights,exception);
1334  if (status != MagickFalse)
1335  status=GetImageEncoder(magick_info)(write_info,image);
1336  if ((thread_support & EncoderThreadSupport) == 0)
1337  UnlockSemaphoreInfo(magick_info->semaphore);
1338  }
1339  }
1340  }
1341  if (temporary != MagickFalse)
1342  {
1343  /*
1344  Copy temporary image file to permanent.
1345  */
1346  status=OpenBlob(write_info,image,ReadBinaryBlobMode,exception);
1347  if (status != MagickFalse)
1348  {
1349  (void) RelinquishUniqueFileResource(write_info->filename);
1350  status=ImageToFile(image,write_info->filename,exception);
1351  }
1352  if (CloseBlob(image) == MagickFalse)
1353  status=MagickFalse;
1354  (void) RelinquishUniqueFileResource(image->filename);
1355  (void) CopyMagickString(image->filename,write_info->filename,
1356  MaxTextExtent);
1357  }
1358  if ((LocaleCompare(write_info->magick,"info") != 0) &&
1359  (write_info->verbose != MagickFalse))
1360  (void) IdentifyImage(image,stderr,MagickFalse);
1361  write_info=DestroyImageInfo(write_info);
1362  if (GetBlobError(image) != MagickFalse)
1363  ThrowWriterException(FileOpenError,"UnableToWriteFile");
1364  return(status);
1365 }
1366 ␌
1367 /*
1368 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1369 % %
1370 % %
1371 % %
1372 % W r i t e I m a g e s %
1373 % %
1374 % %
1375 % %
1376 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1377 %
1378 % WriteImages() writes an image sequence into one or more files. While
1379 % WriteImage() can write an image sequence, it is limited to writing
1380 % the sequence into a single file using a format which supports multiple
1381 % frames. WriteImages(), however, does not have this limitation, instead it
1382 % generates multiple output files if necessary (or when requested). When
1383 % ImageInfo's adjoin flag is set to MagickFalse, the file name is expected
1384 % to include a printf-style formatting string for the frame number (e.g.
1385 % "image%02d.png").
1386 %
1387 % The format of the WriteImages method is:
1388 %
1389 % MagickBooleanType WriteImages(const ImageInfo *image_info,Image *images,
1390 % const char *filename,ExceptionInfo *exception)
1391 %
1392 % A description of each parameter follows:
1393 %
1394 % o image_info: the image info.
1395 %
1396 % o images: the image list.
1397 %
1398 % o filename: the image filename.
1399 %
1400 % o exception: return any errors or warnings in this structure.
1401 %
1402 */
1403 MagickExport MagickBooleanType WriteImages(const ImageInfo *image_info,
1404  Image *images,const char *filename,ExceptionInfo *exception)
1405 {
1406 #define WriteImageTag "Write/Image"
1407 
1409  *sans_exception;
1410 
1411  ImageInfo
1412  *write_info;
1413 
1414  MagickBooleanType
1415  proceed;
1416 
1417  MagickOffsetType
1418  i;
1419 
1420  MagickProgressMonitor
1421  progress_monitor;
1422 
1423  MagickSizeType
1424  number_images;
1425 
1426  MagickStatusType
1427  status;
1428 
1429  Image
1430  *p;
1431 
1432  assert(image_info != (const ImageInfo *) NULL);
1433  assert(image_info->signature == MagickCoreSignature);
1434  assert(images != (Image *) NULL);
1435  assert(images->signature == MagickCoreSignature);
1436  assert(exception != (ExceptionInfo *) NULL);
1437  if (IsEventLogging() != MagickFalse)
1438  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",images->filename);
1439  write_info=CloneImageInfo(image_info);
1440  *write_info->magick='\0';
1441  images=GetFirstImageInList(images);
1442  if (images == (Image *) NULL)
1443  return(MagickFalse);
1444  if (filename != (const char *) NULL)
1445  for (p=images; p != (Image *) NULL; p=GetNextImageInList(p))
1446  (void) CopyMagickString(p->filename,filename,MaxTextExtent);
1447  (void) CopyMagickString(write_info->filename,images->filename,MaxTextExtent);
1448  sans_exception=AcquireExceptionInfo();
1449  (void) SetImageInfo(write_info,(unsigned int) GetImageListLength(images),
1450  sans_exception);
1451  sans_exception=DestroyExceptionInfo(sans_exception);
1452  if (*write_info->magick == '\0')
1453  (void) CopyMagickString(write_info->magick,images->magick,MaxTextExtent);
1454  p=images;
1455  for ( ; GetNextImageInList(p) != (Image *) NULL; p=GetNextImageInList(p))
1456  {
1457  if (p->scene >= GetNextImageInList(p)->scene)
1458  {
1459  ssize_t
1460  i;
1461 
1462  /*
1463  Generate consistent scene numbers.
1464  */
1465  i=(ssize_t) images->scene;
1466  for (p=images; p != (Image *) NULL; p=GetNextImageInList(p))
1467  p->scene=(size_t) i++;
1468  break;
1469  }
1470  }
1471  /*
1472  Write images.
1473  */
1474  status=MagickTrue;
1475  progress_monitor=(MagickProgressMonitor) NULL;
1476  i=0;
1477  number_images=GetImageListLength(images);
1478  for (p=images; p != (Image *) NULL; p=GetNextImageInList(p))
1479  {
1480  if (number_images != 1)
1481  progress_monitor=SetImageProgressMonitor(p,(MagickProgressMonitor) NULL,
1482  p->client_data);
1483  status&=WriteImage(write_info,p);
1484  GetImageException(p,exception);
1485  if (number_images != 1)
1486  (void) SetImageProgressMonitor(p,progress_monitor,p->client_data);
1487  if (write_info->adjoin != MagickFalse)
1488  break;
1489  if (number_images != 1)
1490  {
1491  proceed=SetImageProgress(p,WriteImageTag,i++,number_images);
1492  if (proceed == MagickFalse)
1493  break;
1494  }
1495  }
1496  write_info=DestroyImageInfo(write_info);
1497  return(status != 0 ? MagickTrue : MagickFalse);
1498 }
Definition: image.h:134