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/license/ %
27% %
28% Unless required by applicable law or agreed to in writing, software %
29% distributed under the License is distributed on an "AS IS" BASIS, %
30% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31% See the License for the specific language governing permissions and %
32% limitations under the License. %
33% %
34%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35%
36%
37*/
38
39/*
40 Include declarations.
41*/
42#include "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 *crop_image = CropImage(next,&geometry,exception);
915 if (crop_image != (Image *) NULL)
916 ReplaceImageInList(&next,crop_image);
917 }
918 else
919 if (((flags & WidthValue) != 0) || ((flags & HeightValue) != 0))
920 {
921 flags=ParseRegionGeometry(next,read_info->extract,&geometry,
922 exception);
923 if ((geometry.width != 0) && (geometry.height != 0))
924 {
925 Image *resize_image = ResizeImage(next,geometry.width,
926 geometry.height,next->filter,exception);
927 if (resize_image != (Image *) NULL)
928 ReplaceImageInList(&next,resize_image);
929 }
930 }
931 }
932 }
933 profile=GetImageProfile(next,"icc");
934 if (profile == (const StringInfo *) NULL)
935 profile=GetImageProfile(next,"icm");
936 profile=GetImageProfile(next,"iptc");
937 if (profile == (const StringInfo *) NULL)
938 profile=GetImageProfile(next,"8bim");
939 if (IsSourceDataEpochSet() == MagickFalse)
940 {
941 char
942 timestamp[MagickTimeExtent];
943
944 (void) FormatMagickTime(next->timestamp,sizeof(timestamp),timestamp);
945 (void) SetImageProperty(next,"date:timestamp",timestamp,exception);
946 (void) FormatMagickTime((time_t) GetBlobProperties(next)->st_mtime,
947 sizeof(timestamp),timestamp);
948 (void) SetImageProperty(next,"date:modify",timestamp,exception);
949 (void) FormatMagickTime((time_t) GetBlobProperties(next)->st_ctime,
950 sizeof(timestamp),timestamp);
951 (void) SetImageProperty(next,"date:create",timestamp,exception);
952 }
953 if (constitute_info.delay_flags != NoValue)
954 {
955 if ((constitute_info.delay_flags & GreaterValue) != 0)
956 {
957 if (next->delay > constitute_info.delay)
958 next->delay=constitute_info.delay;
959 }
960 else
961 if ((constitute_info.delay_flags & LessValue) != 0)
962 {
963 if (next->delay < constitute_info.delay)
964 next->delay=constitute_info.delay;
965 }
966 else
967 next->delay=constitute_info.delay;
968 if ((constitute_info.delay_flags & SigmaValue) != 0)
969 next->ticks_per_second=constitute_info.ticks_per_second;
970 }
971 if (constitute_info.dispose != (const char *) NULL)
972 {
973 ssize_t
974 option_type;
975
976 option_type=ParseCommandOption(MagickDisposeOptions,MagickFalse,
977 constitute_info.dispose);
978 if (option_type >= 0)
979 next->dispose=(DisposeType) option_type;
980 }
981 if (read_info->verbose != MagickFalse)
982 (void) IdentifyImage(next,stderr,MagickFalse,exception);
983 image=next;
984 }
985 read_info=DestroyImageInfo(read_info);
986 if (GetBlobError(image) != MagickFalse)
987 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
988 return(GetFirstImageInList(image));
989}
990
991/*
992%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
993% %
994% %
995% %
996% R e a d I m a g e s %
997% %
998% %
999% %
1000%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1001%
1002% ReadImages() reads one or more images and returns them as an image list.
1003%
1004% The format of the ReadImage method is:
1005%
1006% Image *ReadImages(ImageInfo *image_info,const char *filename,
1007% ExceptionInfo *exception)
1008%
1009% A description of each parameter follows:
1010%
1011% o image_info: the image info.
1012%
1013% o filename: the image filename.
1014%
1015% o exception: return any errors or warnings in this structure.
1016%
1017*/
1018MagickExport Image *ReadImages(ImageInfo *image_info,const char *filename,
1019 ExceptionInfo *exception)
1020{
1021 char
1022 read_filename[MagickPathExtent];
1023
1024 Image
1025 *image,
1026 *images;
1027
1028 ImageInfo
1029 *read_info;
1030
1031 /*
1032 Read image list from a file.
1033 */
1034 assert(image_info != (ImageInfo *) NULL);
1035 assert(image_info->signature == MagickCoreSignature);
1036 assert(exception != (ExceptionInfo *) NULL);
1037 if (IsEventLogging() != MagickFalse)
1038 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
1039 image_info->filename);
1040 read_info=CloneImageInfo(image_info);
1041 *read_info->magick='\0';
1042 (void) SetImageOption(read_info,"filename",filename);
1043 (void) CopyMagickString(read_info->filename,filename,MagickPathExtent);
1044 (void) InterpretImageFilename(read_info,(Image *) NULL,filename,
1045 (int) read_info->scene,read_filename,exception);
1046 if (LocaleCompare(read_filename,read_info->filename) != 0)
1047 {
1048 ExceptionInfo
1049 *sans;
1050
1051 ssize_t
1052 extent,
1053 scene;
1054
1055 /*
1056 Images of the form image-%d.png[1-5].
1057 */
1058 sans=AcquireExceptionInfo();
1059 (void) SetImageInfo(read_info,0,sans);
1060 sans=DestroyExceptionInfo(sans);
1061 if (read_info->number_scenes != 0)
1062 {
1063 (void) CopyMagickString(read_filename,read_info->filename,
1064 MagickPathExtent);
1065 images=NewImageList();
1066 extent=(ssize_t) (read_info->scene+read_info->number_scenes);
1067 scene=(ssize_t) read_info->scene;
1068 for ( ; scene < (ssize_t) extent; scene++)
1069 {
1070 (void) InterpretImageFilename(image_info,(Image *) NULL,
1071 read_filename,(int) scene,read_info->filename,exception);
1072 image=ReadImage(read_info,exception);
1073 if (image == (Image *) NULL)
1074 continue;
1075 AppendImageToList(&images,image);
1076 }
1077 read_info=DestroyImageInfo(read_info);
1078 return(images);
1079 }
1080 }
1081 (void) CopyMagickString(read_info->filename,filename,MagickPathExtent);
1082 image=ReadImage(read_info,exception);
1083 read_info=DestroyImageInfo(read_info);
1084 return(image);
1085}
1086
1087/*
1088%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1089% %
1090% %
1091% %
1092+ R e a d I n l i n e I m a g e %
1093% %
1094% %
1095% %
1096%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1097%
1098% ReadInlineImage() reads a Base64-encoded inline image or image sequence.
1099% The method returns a NULL if there is a memory shortage or if the image
1100% cannot be read. On failure, a NULL image is returned and exception
1101% describes the reason for the failure.
1102%
1103% The format of the ReadInlineImage method is:
1104%
1105% Image *ReadInlineImage(const ImageInfo *image_info,const char *content,
1106% ExceptionInfo *exception)
1107%
1108% A description of each parameter follows:
1109%
1110% o image_info: the image info.
1111%
1112% o content: the image encoded in Base64.
1113%
1114% o exception: return any errors or warnings in this structure.
1115%
1116*/
1117MagickExport Image *ReadInlineImage(const ImageInfo *image_info,
1118 const char *content,ExceptionInfo *exception)
1119{
1120 Image
1121 *image;
1122
1123 ImageInfo
1124 *read_info;
1125
1126 unsigned char
1127 *blob;
1128
1129 size_t
1130 length;
1131
1132 const char
1133 *p;
1134
1135 /*
1136 Skip over header (e.g. data:image/gif;base64,).
1137 */
1138 image=NewImageList();
1139 for (p=content; (*p != ',') && (*p != '\0'); p++) ;
1140 if (*p == '\0')
1141 ThrowReaderException(CorruptImageError,"CorruptImage");
1142 blob=Base64Decode(++p,&length);
1143 if (length == 0)
1144 {
1145 blob=(unsigned char *) RelinquishMagickMemory(blob);
1146 ThrowReaderException(CorruptImageError,"CorruptImage");
1147 }
1148 read_info=CloneImageInfo(image_info);
1149 (void) SetImageInfoProgressMonitor(read_info,(MagickProgressMonitor) NULL,
1150 (void *) NULL);
1151 *read_info->filename='\0';
1152 *read_info->magick='\0';
1153 for (p=content; (*p != '/') && (*p != '\0'); p++) ;
1154 if (*p != '\0')
1155 {
1156 char
1157 *q;
1158
1159 ssize_t
1160 i;
1161
1162 /*
1163 Extract media type.
1164 */
1165 if (LocaleNCompare(++p,"x-",2) == 0)
1166 p+=(ptrdiff_t) 2;
1167 (void) CopyMagickString(read_info->filename,"data.",MagickPathExtent);
1168 q=read_info->filename+5;
1169 for (i=0; (*p != ';') && (*p != '\0') && (i < (MagickPathExtent-6)); i++)
1170 *q++=(*p++);
1171 *q++='\0';
1172 }
1173 image=BlobToImage(read_info,blob,length,exception);
1174 blob=(unsigned char *) RelinquishMagickMemory(blob);
1175 read_info=DestroyImageInfo(read_info);
1176 return(image);
1177}
1178
1179/*
1180%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1181% %
1182% %
1183% %
1184% W r i t e I m a g e %
1185% %
1186% %
1187% %
1188%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1189%
1190% WriteImage() writes an image or an image sequence to a file or file handle.
1191% If writing to a file is on disk, the name is defined by the filename member
1192% of the image structure. WriteImage() returns MagickFalse is there is a
1193% memory shortage or if the image cannot be written. Check the exception
1194% member of image to determine the cause for any failure.
1195%
1196% The format of the WriteImage method is:
1197%
1198% MagickBooleanType WriteImage(const ImageInfo *image_info,Image *image,
1199% ExceptionInfo *exception)
1200%
1201% A description of each parameter follows:
1202%
1203% o image_info: the image info.
1204%
1205% o image: the image.
1206%
1207% o exception: return any errors or warnings in this structure.
1208%
1209*/
1210MagickExport MagickBooleanType WriteImage(const ImageInfo *image_info,
1211 Image *image,ExceptionInfo *exception)
1212{
1213 char
1214 filename[MagickPathExtent];
1215
1216 const char
1217 *option;
1218
1219 const DelegateInfo
1220 *delegate_info;
1221
1222 const MagickInfo
1223 *magick_info;
1224
1225 EncodeImageHandler
1226 *encoder;
1227
1228 ExceptionInfo
1229 *sans_exception;
1230
1231 ImageInfo
1232 *write_info;
1233
1234 MagickBooleanType
1235 status,
1236 temporary;
1237
1238 /*
1239 Determine image type from filename prefix or suffix (e.g. image.jpg).
1240 */
1241 assert(image_info != (ImageInfo *) NULL);
1242 assert(image_info->signature == MagickCoreSignature);
1243 assert(image != (Image *) NULL);
1244 if (IsEventLogging() != MagickFalse)
1245 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
1246 image_info->filename);
1247 assert(image->signature == MagickCoreSignature);
1248 assert(exception != (ExceptionInfo *) NULL);
1249 sans_exception=AcquireExceptionInfo();
1250 write_info=CloneImageInfo(image_info);
1251 (void) CopyMagickString(write_info->filename,image->filename,
1252 MagickPathExtent);
1253 (void) SetImageInfo(write_info,1,sans_exception);
1254 if (*write_info->magick == '\0')
1255 (void) CopyMagickString(write_info->magick,image->magick,MagickPathExtent);
1256 (void) CopyMagickString(filename,image->filename,MagickPathExtent);
1257 (void) CopyMagickString(image->filename,write_info->filename,
1258 MagickPathExtent);
1259 /*
1260 Call appropriate image writer based on image type.
1261 */
1262 magick_info=GetMagickInfo(write_info->magick,sans_exception);
1263 if (sans_exception->severity == PolicyError)
1264 magick_info=GetMagickInfo(write_info->magick,exception);
1265 sans_exception=DestroyExceptionInfo(sans_exception);
1266 if (magick_info != (const MagickInfo *) NULL)
1267 {
1268 if (GetMagickEndianSupport(magick_info) == MagickFalse)
1269 image->endian=UndefinedEndian;
1270 else
1271 if ((image_info->endian == UndefinedEndian) &&
1272 (GetMagickRawSupport(magick_info) != MagickFalse))
1273 {
1274 unsigned long
1275 lsb_first;
1276
1277 lsb_first=1;
1278 image->endian=(*(char *) &lsb_first) == 1 ? LSBEndian : MSBEndian;
1279 }
1280 }
1281 SyncImageProfiles(image);
1282 DisassociateImageStream(image);
1283 option=GetImageOption(image_info,"delegate:bimodal");
1284 if ((IsStringTrue(option) != MagickFalse) &&
1285 (write_info->page == (char *) NULL) &&
1286 (GetPreviousImageInList(image) == (Image *) NULL) &&
1287 (GetNextImageInList(image) == (Image *) NULL) &&
1288 (IsTaintImage(image) == MagickFalse) )
1289 {
1290 delegate_info=GetDelegateInfo(image->magick,write_info->magick,exception);
1291 if ((delegate_info != (const DelegateInfo *) NULL) &&
1292 (GetDelegateMode(delegate_info) == 0) &&
1293 (IsPathAccessible(image->magick_filename) != MagickFalse))
1294 {
1295 /*
1296 Process image with bi-modal delegate.
1297 */
1298 (void) CopyMagickString(image->filename,image->magick_filename,
1299 MagickPathExtent);
1300 status=InvokeDelegate(write_info,image,image->magick,
1301 write_info->magick,exception);
1302 write_info=DestroyImageInfo(write_info);
1303 (void) CopyMagickString(image->filename,filename,MagickPathExtent);
1304 return(status);
1305 }
1306 }
1307 status=MagickFalse;
1308 temporary=MagickFalse;
1309 if ((magick_info != (const MagickInfo *) NULL) &&
1310 (GetMagickEncoderSeekableStream(magick_info) != MagickFalse))
1311 {
1312 char
1313 image_filename[MagickPathExtent];
1314
1315 (void) CopyMagickString(image_filename,image->filename,MagickPathExtent);
1316 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
1317 (void) CopyMagickString(image->filename, image_filename,MagickPathExtent);
1318 if (status != MagickFalse)
1319 {
1320 if (IsBlobSeekable(image) == MagickFalse)
1321 {
1322 /*
1323 A seekable stream is required by the encoder.
1324 */
1325 write_info->adjoin=MagickTrue;
1326 (void) CopyMagickString(write_info->filename,image->filename,
1327 MagickPathExtent);
1328 (void) AcquireUniqueFilename(image->filename);
1329 temporary=MagickTrue;
1330 }
1331 if (CloseBlob(image) == MagickFalse)
1332 status=MagickFalse;
1333 }
1334 }
1335 encoder=GetImageEncoder(magick_info);
1336 if (encoder != (EncodeImageHandler *) NULL)
1337 {
1338 /*
1339 Call appropriate image writer based on image type.
1340 */
1341 if (GetMagickEncoderThreadSupport(magick_info) == MagickFalse)
1342 LockSemaphoreInfo(magick_info->semaphore);
1343 status=IsCoderAuthorized(magick_info->magick_module,write_info->magick,
1344 WritePolicyRights,exception);
1345 if (status != MagickFalse)
1346 status=encoder(write_info,image,exception);
1347 if (GetMagickEncoderThreadSupport(magick_info) == MagickFalse)
1348 UnlockSemaphoreInfo(magick_info->semaphore);
1349 }
1350 else
1351 {
1352 delegate_info=GetDelegateInfo((char *) NULL,write_info->magick,exception);
1353 if (delegate_info != (DelegateInfo *) NULL)
1354 {
1355 /*
1356 Process the image with delegate.
1357 */
1358 *write_info->filename='\0';
1359 if (GetDelegateThreadSupport(delegate_info) == MagickFalse)
1360 LockSemaphoreInfo(delegate_info->semaphore);
1361 status=InvokeDelegate(write_info,image,(char *) NULL,
1362 write_info->magick,exception);
1363 if (GetDelegateThreadSupport(delegate_info) == MagickFalse)
1364 UnlockSemaphoreInfo(delegate_info->semaphore);
1365 (void) CopyMagickString(image->filename,filename,MagickPathExtent);
1366 }
1367 else
1368 {
1369 sans_exception=AcquireExceptionInfo();
1370 magick_info=GetMagickInfo(write_info->magick,sans_exception);
1371 if (sans_exception->severity == PolicyError)
1372 magick_info=GetMagickInfo(write_info->magick,exception);
1373 sans_exception=DestroyExceptionInfo(sans_exception);
1374 if ((write_info->affirm == MagickFalse) &&
1375 (magick_info == (const MagickInfo *) NULL))
1376 {
1377 (void) CopyMagickString(write_info->magick,image->magick,
1378 MagickPathExtent);
1379 magick_info=GetMagickInfo(write_info->magick,exception);
1380 }
1381 encoder=GetImageEncoder(magick_info);
1382 if (encoder == (EncodeImageHandler *) NULL)
1383 {
1384 char
1385 extension[MagickPathExtent];
1386
1387 GetPathComponent(image->filename,ExtensionPath,extension);
1388 if (*extension != '\0')
1389 magick_info=GetMagickInfo(extension,exception);
1390 else
1391 magick_info=GetMagickInfo(image->magick,exception);
1392 (void) CopyMagickString(image->filename,filename,
1393 MagickPathExtent);
1394 encoder=GetImageEncoder(magick_info);
1395 (void) ThrowMagickException(exception,GetMagickModule(),
1396 MissingDelegateWarning,"NoEncodeDelegateForThisImageFormat",
1397 "`%s'",write_info->magick);
1398 }
1399 if (encoder == (EncodeImageHandler *) NULL)
1400 {
1401 magick_info=GetMagickInfo(image->magick,exception);
1402 encoder=GetImageEncoder(magick_info);
1403 if (encoder == (EncodeImageHandler *) NULL)
1404 (void) ThrowMagickException(exception,GetMagickModule(),
1405 MissingDelegateError,"NoEncodeDelegateForThisImageFormat",
1406 "`%s'",write_info->magick);
1407 }
1408 if (encoder != (EncodeImageHandler *) NULL)
1409 {
1410 /*
1411 Call appropriate image writer based on image type.
1412 */
1413 if (GetMagickEncoderThreadSupport(magick_info) == MagickFalse)
1414 LockSemaphoreInfo(magick_info->semaphore);
1415 status=IsCoderAuthorized(magick_info->magick_module,write_info->magick,
1416 WritePolicyRights,exception);
1417 if (status != MagickFalse)
1418 status=encoder(write_info,image,exception);
1419 if (GetMagickEncoderThreadSupport(magick_info) == MagickFalse)
1420 UnlockSemaphoreInfo(magick_info->semaphore);
1421 }
1422 }
1423 }
1424 if (temporary != MagickFalse)
1425 {
1426 /*
1427 Copy temporary image file to permanent.
1428 */
1429 status=OpenBlob(write_info,image,ReadBinaryBlobMode,exception);
1430 if (status != MagickFalse)
1431 {
1432 (void) RelinquishUniqueFileResource(write_info->filename);
1433 status=ImageToFile(image,write_info->filename,exception);
1434 }
1435 if (CloseBlob(image) == MagickFalse)
1436 status=MagickFalse;
1437 (void) RelinquishUniqueFileResource(image->filename);
1438 (void) CopyMagickString(image->filename,write_info->filename,
1439 MagickPathExtent);
1440 }
1441 if ((LocaleCompare(write_info->magick,"info") != 0) &&
1442 (write_info->verbose != MagickFalse))
1443 (void) IdentifyImage(image,stdout,MagickFalse,exception);
1444 write_info=DestroyImageInfo(write_info);
1445 if (GetBlobError(image) != MagickFalse)
1446 ThrowWriterException(FileOpenError,"UnableToWriteFile");
1447 return(status);
1448}
1449
1450/*
1451%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1452% %
1453% %
1454% %
1455% W r i t e I m a g e s %
1456% %
1457% %
1458% %
1459%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1460%
1461% WriteImages() writes an image sequence into one or more files. While
1462% WriteImage() can write an image sequence, it is limited to writing
1463% the sequence into a single file using a format which supports multiple
1464% frames. WriteImages(), however, does not have this limitation, instead it
1465% generates multiple output files if necessary (or when requested). When
1466% ImageInfo's adjoin flag is set to MagickFalse, the file name is expected
1467% to include a printf-style formatting string for the frame number (e.g.
1468% "image%02d.png").
1469%
1470% The format of the WriteImages method is:
1471%
1472% MagickBooleanType WriteImages(const ImageInfo *image_info,Image *images,
1473% const char *filename,ExceptionInfo *exception)
1474%
1475% A description of each parameter follows:
1476%
1477% o image_info: the image info.
1478%
1479% o images: the image list.
1480%
1481% o filename: the image filename.
1482%
1483% o exception: return any errors or warnings in this structure.
1484%
1485*/
1486MagickExport MagickBooleanType WriteImages(const ImageInfo *image_info,
1487 Image *images,const char *filename,ExceptionInfo *exception)
1488{
1489#define WriteImageTag "Write/Image"
1490
1491 ExceptionInfo
1492 *sans_exception;
1493
1494 ImageInfo
1495 *write_info;
1496
1497 MagickBooleanType
1498 proceed;
1499
1500 MagickOffsetType
1501 progress;
1502
1503 MagickProgressMonitor
1504 progress_monitor;
1505
1506 MagickSizeType
1507 number_images;
1508
1509 MagickStatusType
1510 status;
1511
1512 Image
1513 *p;
1514
1515 assert(image_info != (const ImageInfo *) NULL);
1516 assert(image_info->signature == MagickCoreSignature);
1517 assert(images != (Image *) NULL);
1518 assert(images->signature == MagickCoreSignature);
1519 if (IsEventLogging() != MagickFalse)
1520 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",images->filename);
1521 assert(exception != (ExceptionInfo *) NULL);
1522 write_info=CloneImageInfo(image_info);
1523 *write_info->magick='\0';
1524 images=GetFirstImageInList(images);
1525 if (images == (Image *) NULL)
1526 return(MagickFalse);
1527 if (filename != (const char *) NULL)
1528 for (p=images; p != (Image *) NULL; p=GetNextImageInList(p))
1529 (void) CopyMagickString(p->filename,filename,MagickPathExtent);
1530 (void) CopyMagickString(write_info->filename,images->filename,
1531 MagickPathExtent);
1532 sans_exception=AcquireExceptionInfo();
1533 (void) SetImageInfo(write_info,(unsigned int) GetImageListLength(images),
1534 sans_exception);
1535 sans_exception=DestroyExceptionInfo(sans_exception);
1536 if (*write_info->magick == '\0')
1537 (void) CopyMagickString(write_info->magick,images->magick,MagickPathExtent);
1538 p=images;
1539 for ( ; GetNextImageInList(p) != (Image *) NULL; p=GetNextImageInList(p))
1540 {
1541 if (p->scene >= GetNextImageInList(p)->scene)
1542 {
1543 ssize_t
1544 i;
1545
1546 /*
1547 Generate consistent scene numbers.
1548 */
1549 i=(ssize_t) images->scene;
1550 for (p=images; p != (Image *) NULL; p=GetNextImageInList(p))
1551 p->scene=(size_t) i++;
1552 break;
1553 }
1554 }
1555 /*
1556 Write images.
1557 */
1558 status=MagickTrue;
1559 progress_monitor=(MagickProgressMonitor) NULL;
1560 progress=0;
1561 number_images=GetImageListLength(images);
1562 for (p=images; p != (Image *) NULL; p=GetNextImageInList(p))
1563 {
1564 if (number_images != 1)
1565 progress_monitor=SetImageProgressMonitor(p,(MagickProgressMonitor) NULL,
1566 p->client_data);
1567 status&=(MagickStatusType) WriteImage(write_info,p,exception);
1568 if (number_images != 1)
1569 (void) SetImageProgressMonitor(p,progress_monitor,p->client_data);
1570 if (write_info->adjoin != MagickFalse)
1571 break;
1572 if (number_images != 1)
1573 {
1574#if defined(MAGICKCORE_OPENMP_SUPPORT)
1575 #pragma omp atomic
1576#endif
1577 progress++;
1578 proceed=SetImageProgress(p,WriteImageTag,progress,number_images);
1579 if (proceed == MagickFalse)
1580 break;
1581 }
1582 }
1583 write_info=DestroyImageInfo(write_info);
1584 return(status != 0 ? MagickTrue : MagickFalse);
1585}