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