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