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