MagickCore 7.1.2
Convert, Edit, Or Compose Bitmap Images
Loading...
Searching...
No Matches
constitute.c
1/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
6% CCCC OOO N N SSSSS TTTTT IIIII TTTTT U U TTTTT EEEEE %
7% C O O NN N SS T I T U U T E %
8% C O O N N N ESSS T I T U U T EEE %
9% C O O N NN SS T I T U U T E %
10% CCCC OOO N N SSSSS T IIIII T UUU T EEEEE %
11% %
12% %
13% MagickCore Methods to Constitute an Image %
14% %
15% Software Design %
16% Cristy %
17% October 1998 %
18% %
19% %
20% Copyright @ 1999 ImageMagick Studio LLC, a non-profit organization %
21% dedicated to making software imaging solutions freely available. %
22% %
23% You may not use this file except in compliance with the License. You may %
24% obtain a copy of the License at %
25% %
26% https://imagemagick.org/script/license.php %
27% %
28% Unless required by applicable law or agreed to in writing, software %
29% distributed under the License is distributed on an "AS IS" BASIS, %
30% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31% See the License for the specific language governing permissions and %
32% limitations under the License. %
33% %
34%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35%
36%
37*/
38
39/*
40 Include declarations.
41*/
42#include "MagickCore/studio.h"
43#include "MagickCore/attribute.h"
44#include "MagickCore/blob.h"
45#include "MagickCore/blob-private.h"
46#include "MagickCore/exception.h"
47#include "MagickCore/exception-private.h"
48#include "MagickCore/cache.h"
49#include "MagickCore/client.h"
50#include "MagickCore/coder-private.h"
51#include "MagickCore/colorspace-private.h"
52#include "MagickCore/constitute.h"
53#include "MagickCore/constitute-private.h"
54#include "MagickCore/delegate.h"
55#include "MagickCore/geometry.h"
56#include "MagickCore/identify.h"
57#include "MagickCore/image-private.h"
58#include "MagickCore/list.h"
59#include "MagickCore/magick.h"
60#include "MagickCore/memory_.h"
61#include "MagickCore/monitor.h"
62#include "MagickCore/monitor-private.h"
63#include "MagickCore/option.h"
64#include "MagickCore/pixel.h"
65#include "MagickCore/pixel-accessor.h"
66#include "MagickCore/policy.h"
67#include "MagickCore/profile.h"
68#include "MagickCore/profile-private.h"
69#include "MagickCore/property.h"
70#include "MagickCore/quantum.h"
71#include "MagickCore/resize.h"
72#include "MagickCore/resource_.h"
73#include "MagickCore/semaphore.h"
74#include "MagickCore/statistic.h"
75#include "MagickCore/stream.h"
76#include "MagickCore/string_.h"
77#include "MagickCore/string-private.h"
78#include "MagickCore/timer.h"
79#include "MagickCore/timer-private.h"
80#include "MagickCore/token.h"
81#include "MagickCore/transform.h"
82#include "MagickCore/utility.h"
83#include "MagickCore/utility-private.h"
84
85/*
86 Define declarations.
87*/
88#define MaxReadRecursionDepth 100
89
90/*
91 Typedef declarations.
92*/
93typedef struct _ConstituteInfo
94{
95 const char
96 *caption,
97 *comment,
98 *dispose,
99 *label;
100
101 MagickBooleanType
102 sync_from_exif,
103 sync_from_tiff;
104
105 MagickStatusType
106 delay_flags;
107
108 size_t
109 delay;
110
111 ssize_t
112 ticks_per_second;
113} ConstituteInfo;
114
115/*
116%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
117% %
118% %
119% %
120% C o n s t i t u t e I m a g e %
121% %
122% %
123% %
124%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
125%
126% ConstituteImage() returns an image from the pixel data you supply.
127% The pixel data must be in scanline order top-to-bottom. The data can be
128% char, short int, int, float, or double. Float and double require the
129% pixels to be normalized [0..1], otherwise [0..QuantumRange]. For example, to
130% create a 640x480 image from unsigned red-green-blue character data, use:
131%
132% image = ConstituteImage(640,480,"RGB",CharPixel,pixels,&exception);
133%
134% The format of the ConstituteImage method is:
135%
136% Image *ConstituteImage(const size_t columns,const size_t rows,
137% const char *map,const StorageType storage,const void *pixels,
138% ExceptionInfo *exception)
139%
140% A description of each parameter follows:
141%
142% o columns: width in pixels of the image.
143%
144% o rows: height in pixels of the image.
145%
146% o map: This string reflects the expected ordering of the pixel array.
147% It can be any combination or order of R = red, G = green, B = blue,
148% A = alpha (0 is transparent), O = opacity (0 is opaque), C = cyan,
149% Y = yellow, M = magenta, K = black, I = intensity (for grayscale),
150% P = pad.
151%
152% o storage: Define the data type of the pixels. Float and double types are
153% expected to be normalized [0..1] otherwise [0..QuantumRange]. Choose
154% from these types: CharPixel, DoublePixel, FloatPixel, IntegerPixel,
155% LongPixel, QuantumPixel, or ShortPixel.
156%
157% o pixels: This array of values contain the pixel components as defined by
158% map and type. You must preallocate this array where the expected
159% length varies depending on the values of width, height, map, and type.
160%
161% o exception: return any errors or warnings in this structure.
162%
163*/
164MagickExport Image *ConstituteImage(const size_t columns,const size_t rows,
165 const char *map,const StorageType storage,const void *pixels,
166 ExceptionInfo *exception)
167{
168 Image
169 *image;
170
171 MagickBooleanType
172 status;
173
174 ssize_t
175 i;
176
177 size_t
178 length;
179
180 /*
181 Allocate image structure.
182 */
183 assert(map != (const char *) NULL);
184 assert(pixels != (void *) NULL);
185 assert(exception != (ExceptionInfo *) NULL);
186 assert(exception->signature == MagickCoreSignature);
187 if (IsEventLogging() != MagickFalse)
188 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",map);
189 image=AcquireImage((ImageInfo *) NULL,exception);
190 if (image == (Image *) NULL)
191 return((Image *) NULL);
192 switch (storage)
193 {
194 case CharPixel: image->depth=8*sizeof(unsigned char); break;
195 case DoublePixel: image->depth=8*sizeof(double); break;
196 case FloatPixel: image->depth=8*sizeof(float); break;
197 case LongPixel: image->depth=8*sizeof(unsigned long); break;
198 case LongLongPixel: image->depth=8*sizeof(MagickSizeType); break;
199 case ShortPixel: image->depth=8*sizeof(unsigned short); break;
200 default: break;
201 }
202 length=strlen(map);
203 for (i=0; i < (ssize_t) length; i++)
204 {
205 switch (map[i])
206 {
207 case 'a':
208 case 'A':
209 case 'O':
210 case 'o':
211 {
212 image->alpha_trait=BlendPixelTrait;
213 break;
214 }
215 case 'C':
216 case 'c':
217 case 'm':
218 case 'M':
219 case 'Y':
220 case 'y':
221 case 'K':
222 case 'k':
223 {
224 image->colorspace=CMYKColorspace;
225 break;
226 }
227 case 'I':
228 case 'i':
229 {
230 image->colorspace=GRAYColorspace;
231 break;
232 }
233 default:
234 {
235 if (length == 1)
236 image->colorspace=GRAYColorspace;
237 break;
238 }
239 }
240 }
241 status=SetImageExtent(image,columns,rows,exception);
242 if (status == MagickFalse)
243 return(DestroyImageList(image));
244 status=ResetImagePixels(image,exception);
245 if (status == MagickFalse)
246 return(DestroyImageList(image));
247 status=ImportImagePixels(image,0,0,columns,rows,map,storage,pixels,exception);
248 if (status == MagickFalse)
249 image=DestroyImage(image);
250 return(image);
251}
252
253/*
254%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
255% %
256% %
257% %
258% P i n g I m a g e %
259% %
260% %
261% %
262%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
263%
264% PingImage() returns all the properties of an image or image sequence
265% except for the pixels. It is much faster and consumes far less memory
266% than ReadImage(). On failure, a NULL image is returned and exception
267% describes the reason for the failure.
268%
269% The format of the PingImage method is:
270%
271% Image *PingImage(const ImageInfo *image_info,ExceptionInfo *exception)
272%
273% A description of each parameter follows:
274%
275% o image_info: Ping the image defined by the file or filename members of
276% this structure.
277%
278% o exception: return any errors or warnings in this structure.
279%
280*/
281
282#if defined(__cplusplus) || defined(c_plusplus)
283extern "C" {
284#endif
285
286static size_t PingStream(const Image *magick_unused(image),
287 const void *magick_unused(pixels),const size_t columns)
288{
289 magick_unreferenced(image);
290 magick_unreferenced(pixels);
291 return(columns);
292}
293
294#if defined(__cplusplus) || defined(c_plusplus)
295}
296#endif
297
298MagickExport Image *PingImage(const ImageInfo *image_info,
299 ExceptionInfo *exception)
300{
301 Image
302 *image;
303
304 ImageInfo
305 *ping_info;
306
307 assert(image_info != (ImageInfo *) NULL);
308 assert(image_info->signature == MagickCoreSignature);
309 assert(exception != (ExceptionInfo *) NULL);
310 if (IsEventLogging() != MagickFalse)
311 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
312 image_info->filename);
313 ping_info=CloneImageInfo(image_info);
314 ping_info->ping=MagickTrue;
315 image=ReadStream(ping_info,&PingStream,exception);
316 if (image != (Image *) NULL)
317 {
318 ResetTimer(&image->timer);
319 if (ping_info->verbose != MagickFalse)
320 (void) IdentifyImage(image,stdout,MagickFalse,exception);
321 }
322 ping_info=DestroyImageInfo(ping_info);
323 return(image);
324}
325
326/*
327%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
328% %
329% %
330% %
331% P i n g I m a g e s %
332% %
333% %
334% %
335%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
336%
337% PingImages() pings one or more images and returns them as an image list.
338%
339% The format of the PingImage method is:
340%
341% Image *PingImages(ImageInfo *image_info,const char *filename,
342% ExceptionInfo *exception)
343%
344% A description of each parameter follows:
345%
346% o image_info: the image info.
347%
348% o filename: the image filename.
349%
350% o exception: return any errors or warnings in this structure.
351%
352*/
353MagickExport Image *PingImages(ImageInfo *image_info,const char *filename,
354 ExceptionInfo *exception)
355{
356 char
357 ping_filename[MagickPathExtent];
358
359 Image
360 *image,
361 *images;
362
363 ImageInfo
364 *read_info;
365
366 /*
367 Ping image list from a file.
368 */
369 assert(image_info != (ImageInfo *) NULL);
370 assert(image_info->signature == MagickCoreSignature);
371 assert(exception != (ExceptionInfo *) NULL);
372 if (IsEventLogging() != MagickFalse)
373 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
374 image_info->filename);
375 (void) SetImageOption(image_info,"filename",filename);
376 (void) CopyMagickString(image_info->filename,filename,MagickPathExtent);
377 (void) InterpretImageFilename(image_info,(Image *) NULL,image_info->filename,
378 (int) image_info->scene,ping_filename,exception);
379 if (LocaleCompare(ping_filename,image_info->filename) != 0)
380 {
381 ExceptionInfo
382 *sans;
383
384 ssize_t
385 extent,
386 scene;
387
388 /*
389 Images of the form image-%d.png[1-5].
390 */
391 read_info=CloneImageInfo(image_info);
392 sans=AcquireExceptionInfo();
393 (void) SetImageInfo(read_info,0,sans);
394 sans=DestroyExceptionInfo(sans);
395 if (read_info->number_scenes == 0)
396 {
397 read_info=DestroyImageInfo(read_info);
398 return(PingImage(image_info,exception));
399 }
400 (void) CopyMagickString(ping_filename,read_info->filename,
401 MagickPathExtent);
402 images=NewImageList();
403 extent=(ssize_t) (read_info->scene+read_info->number_scenes);
404 for (scene=(ssize_t) read_info->scene; scene < (ssize_t) extent; scene++)
405 {
406 (void) InterpretImageFilename(image_info,(Image *) NULL,ping_filename,
407 (int) scene,read_info->filename,exception);
408 image=PingImage(read_info,exception);
409 if (image == (Image *) NULL)
410 continue;
411 AppendImageToList(&images,image);
412 }
413 read_info=DestroyImageInfo(read_info);
414 return(images);
415 }
416 return(PingImage(image_info,exception));
417}
418
419/*
420%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
421% %
422% %
423% %
424% R e a d I m a g e %
425% %
426% %
427% %
428%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
429%
430% ReadImage() reads an image or image sequence from a file or file handle.
431% The method returns a NULL if there is a memory shortage or if the image
432% cannot be read. On failure, a NULL image is returned and exception
433% describes the reason for the failure.
434%
435% The format of the ReadImage method is:
436%
437% Image *ReadImage(const ImageInfo *image_info,ExceptionInfo *exception)
438%
439% A description of each parameter follows:
440%
441% o image_info: Read the image defined by the file or filename members of
442% this structure.
443%
444% o exception: return any errors or warnings in this structure.
445%
446*/
447
448static MagickBooleanType IsCoderAuthorized(const char *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 {
482 GeometryInfo
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=(size_t) floor(geometry_info.rho+0.5);
489 if ((constitute_info->delay_flags & SigmaValue) != 0)
490 constitute_info->ticks_per_second=CastDoubleToSsizeT(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 {
565 GeometryInfo
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
615 ConstituteInfo
616 constitute_info;
617
618 const DelegateInfo
619 *delegate_info;
620
621 const MagickInfo
622 *magick_info;
623
624 DecodeImageHandler
625 *decoder;
626
627 ExceptionInfo
628 *sans_exception;
629
630 Image
631 *image,
632 *next;
633
634 ImageInfo
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 image=DestroyImageList(image);
828 if (clones != (Image *) NULL)
829 image=GetFirstImageInList(clones);
830 if (image == (Image *) NULL)
831 {
832 read_info=DestroyImageInfo(read_info);
833 return(image);
834 }
835 }
836 InitializeConstituteInfo(read_info,&constitute_info);
837 for (next=image; next != (Image *) NULL; next=GetNextImageInList(next))
838 {
839 char
840 magick_path[MagickPathExtent],
841 *property;
842
843 const StringInfo
844 *profile;
845
846 next->taint=MagickFalse;
847 GetPathComponent(magick_filename,MagickPath,magick_path);
848 if ((*magick_path == '\0') && (*next->magick == '\0'))
849 (void) CopyMagickString(next->magick,magick,MagickPathExtent);
850 (void) CopyMagickString(next->magick_filename,magick_filename,
851 MagickPathExtent);
852 if (IsBlobTemporary(image) != MagickFalse)
853 (void) CopyMagickString(next->filename,filename,MagickPathExtent);
854 if (next->magick_columns == 0)
855 next->magick_columns=next->columns;
856 if (next->magick_rows == 0)
857 next->magick_rows=next->rows;
858 (void) GetImageProperty(next,"exif:*",exception);
859 (void) GetImageProperty(next,"icc:*",exception);
860 (void) GetImageProperty(next,"iptc:*",exception);
861 (void) GetImageProperty(next,"xmp:*",exception);
862 SyncOrientationFromProperties(next,&constitute_info,exception);
863 SyncResolutionFromProperties(next,&constitute_info,exception);
864 if (next->page.width == 0)
865 next->page.width=next->columns;
866 if (next->page.height == 0)
867 next->page.height=next->rows;
868 if (constitute_info.caption != (const char *) NULL)
869 {
870 property=InterpretImageProperties(read_info,next,
871 constitute_info.caption,exception);
872 (void) SetImageProperty(next,"caption",property,exception);
873 property=DestroyString(property);
874 }
875 if (constitute_info.comment != (const char *) NULL)
876 {
877 property=InterpretImageProperties(read_info,next,
878 constitute_info.comment,exception);
879 (void) SetImageProperty(next,"comment",property,exception);
880 property=DestroyString(property);
881 }
882 if (constitute_info.label != (const char *) NULL)
883 {
884 property=InterpretImageProperties(read_info,next,
885 constitute_info.label,exception);
886 (void) SetImageProperty(next,"label",property,exception);
887 property=DestroyString(property);
888 }
889 if (LocaleCompare(next->magick,"TEXT") == 0)
890 (void) ParseAbsoluteGeometry("0x0+0+0",&next->page);
891 if ((read_info->extract != (char *) NULL) &&
892 (read_info->stream == (StreamHandler) NULL))
893 {
894 RectangleInfo
895 geometry;
896
897 MagickStatusType
898 flags;
899
900 SetGeometry(next,&geometry);
901 flags=ParseAbsoluteGeometry(read_info->extract,&geometry);
902 if ((next->columns != geometry.width) ||
903 (next->rows != geometry.height))
904 {
905 if (((flags & XValue) != 0) || ((flags & YValue) != 0))
906 {
907 Image
908 *crop_image;
909
910 crop_image=CropImage(next,&geometry,exception);
911 if (crop_image != (Image *) NULL)
912 ReplaceImageInList(&next,crop_image);
913 }
914 else
915 if (((flags & WidthValue) != 0) || ((flags & HeightValue) != 0))
916 {
917 flags=ParseRegionGeometry(next,read_info->extract,&geometry,
918 exception);
919 if ((geometry.width != 0) && (geometry.height != 0))
920 {
921 Image *resize_image = ResizeImage(next,geometry.width,
922 geometry.height,next->filter,exception);
923 if (resize_image != (Image *) NULL)
924 ReplaceImageInList(&next,resize_image);
925 }
926 }
927 }
928 }
929 profile=GetImageProfile(next,"icc");
930 if (profile == (const StringInfo *) NULL)
931 profile=GetImageProfile(next,"icm");
932 profile=GetImageProfile(next,"iptc");
933 if (profile == (const StringInfo *) NULL)
934 profile=GetImageProfile(next,"8bim");
935 if (IsSourceDataEpochSet() == MagickFalse)
936 {
937 char
938 timestamp[MagickTimeExtent];
939
940 (void) FormatMagickTime(next->timestamp,sizeof(timestamp),timestamp);
941 (void) SetImageProperty(next,"date:timestamp",timestamp,exception);
942 (void) FormatMagickTime((time_t) GetBlobProperties(next)->st_mtime,
943 sizeof(timestamp),timestamp);
944 (void) SetImageProperty(next,"date:modify",timestamp,exception);
945 (void) FormatMagickTime((time_t) GetBlobProperties(next)->st_ctime,
946 sizeof(timestamp),timestamp);
947 (void) SetImageProperty(next,"date:create",timestamp,exception);
948 }
949 if (constitute_info.delay_flags != NoValue)
950 {
951 if ((constitute_info.delay_flags & GreaterValue) != 0)
952 {
953 if (next->delay > constitute_info.delay)
954 next->delay=constitute_info.delay;
955 }
956 else
957 if ((constitute_info.delay_flags & LessValue) != 0)
958 {
959 if (next->delay < constitute_info.delay)
960 next->delay=constitute_info.delay;
961 }
962 else
963 next->delay=constitute_info.delay;
964 if ((constitute_info.delay_flags & SigmaValue) != 0)
965 next->ticks_per_second=constitute_info.ticks_per_second;
966 }
967 if (constitute_info.dispose != (const char *) NULL)
968 {
969 ssize_t
970 option_type;
971
972 option_type=ParseCommandOption(MagickDisposeOptions,MagickFalse,
973 constitute_info.dispose);
974 if (option_type >= 0)
975 next->dispose=(DisposeType) option_type;
976 }
977 if (read_info->verbose != MagickFalse)
978 (void) IdentifyImage(next,stderr,MagickFalse,exception);
979 image=next;
980 }
981 read_info=DestroyImageInfo(read_info);
982 if (GetBlobError(image) != MagickFalse)
983 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
984 return(GetFirstImageInList(image));
985}
986
987/*
988%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
989% %
990% %
991% %
992% R e a d I m a g e s %
993% %
994% %
995% %
996%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
997%
998% ReadImages() reads one or more images and returns them as an image list.
999%
1000% The format of the ReadImage method is:
1001%
1002% Image *ReadImages(ImageInfo *image_info,const char *filename,
1003% ExceptionInfo *exception)
1004%
1005% A description of each parameter follows:
1006%
1007% o image_info: the image info.
1008%
1009% o filename: the image filename.
1010%
1011% o exception: return any errors or warnings in this structure.
1012%
1013*/
1014MagickExport Image *ReadImages(ImageInfo *image_info,const char *filename,
1015 ExceptionInfo *exception)
1016{
1017 char
1018 read_filename[MagickPathExtent];
1019
1020 Image
1021 *image,
1022 *images;
1023
1024 ImageInfo
1025 *read_info;
1026
1027 /*
1028 Read image list from a file.
1029 */
1030 assert(image_info != (ImageInfo *) NULL);
1031 assert(image_info->signature == MagickCoreSignature);
1032 assert(exception != (ExceptionInfo *) NULL);
1033 if (IsEventLogging() != MagickFalse)
1034 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
1035 image_info->filename);
1036 read_info=CloneImageInfo(image_info);
1037 *read_info->magick='\0';
1038 (void) SetImageOption(read_info,"filename",filename);
1039 (void) CopyMagickString(read_info->filename,filename,MagickPathExtent);
1040 (void) InterpretImageFilename(read_info,(Image *) NULL,filename,
1041 (int) read_info->scene,read_filename,exception);
1042 if (LocaleCompare(read_filename,read_info->filename) != 0)
1043 {
1044 ExceptionInfo
1045 *sans;
1046
1047 ssize_t
1048 extent,
1049 scene;
1050
1051 /*
1052 Images of the form image-%d.png[1-5].
1053 */
1054 sans=AcquireExceptionInfo();
1055 (void) SetImageInfo(read_info,0,sans);
1056 sans=DestroyExceptionInfo(sans);
1057 if (read_info->number_scenes != 0)
1058 {
1059 (void) CopyMagickString(read_filename,read_info->filename,
1060 MagickPathExtent);
1061 images=NewImageList();
1062 extent=(ssize_t) (read_info->scene+read_info->number_scenes);
1063 scene=(ssize_t) read_info->scene;
1064 for ( ; scene < (ssize_t) extent; scene++)
1065 {
1066 (void) InterpretImageFilename(image_info,(Image *) NULL,
1067 read_filename,(int) scene,read_info->filename,exception);
1068 image=ReadImage(read_info,exception);
1069 if (image == (Image *) NULL)
1070 continue;
1071 AppendImageToList(&images,image);
1072 }
1073 read_info=DestroyImageInfo(read_info);
1074 return(images);
1075 }
1076 }
1077 (void) CopyMagickString(read_info->filename,filename,MagickPathExtent);
1078 image=ReadImage(read_info,exception);
1079 read_info=DestroyImageInfo(read_info);
1080 return(image);
1081}
1082
1083/*
1084%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1085% %
1086% %
1087% %
1088+ R e a d I n l i n e I m a g e %
1089% %
1090% %
1091% %
1092%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1093%
1094% ReadInlineImage() reads a Base64-encoded inline image or image sequence.
1095% The method returns a NULL if there is a memory shortage or if the image
1096% cannot be read. On failure, a NULL image is returned and exception
1097% describes the reason for the failure.
1098%
1099% The format of the ReadInlineImage method is:
1100%
1101% Image *ReadInlineImage(const ImageInfo *image_info,const char *content,
1102% ExceptionInfo *exception)
1103%
1104% A description of each parameter follows:
1105%
1106% o image_info: the image info.
1107%
1108% o content: the image encoded in Base64.
1109%
1110% o exception: return any errors or warnings in this structure.
1111%
1112*/
1113MagickExport Image *ReadInlineImage(const ImageInfo *image_info,
1114 const char *content,ExceptionInfo *exception)
1115{
1116 Image
1117 *image;
1118
1119 ImageInfo
1120 *read_info;
1121
1122 unsigned char
1123 *blob;
1124
1125 size_t
1126 length;
1127
1128 const char
1129 *p;
1130
1131 /*
1132 Skip over header (e.g. data:image/gif;base64,).
1133 */
1134 image=NewImageList();
1135 for (p=content; (*p != ',') && (*p != '\0'); p++) ;
1136 if (*p == '\0')
1137 ThrowReaderException(CorruptImageError,"CorruptImage");
1138 blob=Base64Decode(++p,&length);
1139 if (length == 0)
1140 {
1141 blob=(unsigned char *) RelinquishMagickMemory(blob);
1142 ThrowReaderException(CorruptImageError,"CorruptImage");
1143 }
1144 read_info=CloneImageInfo(image_info);
1145 (void) SetImageInfoProgressMonitor(read_info,(MagickProgressMonitor) NULL,
1146 (void *) NULL);
1147 *read_info->filename='\0';
1148 *read_info->magick='\0';
1149 for (p=content; (*p != '/') && (*p != '\0'); p++) ;
1150 if (*p != '\0')
1151 {
1152 char
1153 *q;
1154
1155 ssize_t
1156 i;
1157
1158 /*
1159 Extract media type.
1160 */
1161 if (LocaleNCompare(++p,"x-",2) == 0)
1162 p+=(ptrdiff_t) 2;
1163 (void) CopyMagickString(read_info->filename,"data.",MagickPathExtent);
1164 q=read_info->filename+5;
1165 for (i=0; (*p != ';') && (*p != '\0') && (i < (MagickPathExtent-6)); i++)
1166 *q++=(*p++);
1167 *q++='\0';
1168 }
1169 image=BlobToImage(read_info,blob,length,exception);
1170 blob=(unsigned char *) RelinquishMagickMemory(blob);
1171 read_info=DestroyImageInfo(read_info);
1172 return(image);
1173}
1174
1175/*
1176%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1177% %
1178% %
1179% %
1180% W r i t e I m a g e %
1181% %
1182% %
1183% %
1184%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1185%
1186% WriteImage() writes an image or an image sequence to a file or file handle.
1187% If writing to a file is on disk, the name is defined by the filename member
1188% of the image structure. WriteImage() returns MagickFalse is there is a
1189% memory shortage or if the image cannot be written. Check the exception
1190% member of image to determine the cause for any failure.
1191%
1192% The format of the WriteImage method is:
1193%
1194% MagickBooleanType WriteImage(const ImageInfo *image_info,Image *image,
1195% ExceptionInfo *exception)
1196%
1197% A description of each parameter follows:
1198%
1199% o image_info: the image info.
1200%
1201% o image: the image.
1202%
1203% o exception: return any errors or warnings in this structure.
1204%
1205*/
1206MagickExport MagickBooleanType WriteImage(const ImageInfo *image_info,
1207 Image *image,ExceptionInfo *exception)
1208{
1209 char
1210 filename[MagickPathExtent];
1211
1212 const char
1213 *option;
1214
1215 const DelegateInfo
1216 *delegate_info;
1217
1218 const MagickInfo
1219 *magick_info;
1220
1221 EncodeImageHandler
1222 *encoder;
1223
1224 ExceptionInfo
1225 *sans_exception;
1226
1227 ImageInfo
1228 *write_info;
1229
1230 MagickBooleanType
1231 status,
1232 temporary;
1233
1234 /*
1235 Determine image type from filename prefix or suffix (e.g. image.jpg).
1236 */
1237 assert(image_info != (ImageInfo *) NULL);
1238 assert(image_info->signature == MagickCoreSignature);
1239 assert(image != (Image *) NULL);
1240 if (IsEventLogging() != MagickFalse)
1241 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
1242 image_info->filename);
1243 assert(image->signature == MagickCoreSignature);
1244 assert(exception != (ExceptionInfo *) NULL);
1245 sans_exception=AcquireExceptionInfo();
1246 write_info=CloneImageInfo(image_info);
1247 (void) CopyMagickString(write_info->filename,image->filename,
1248 MagickPathExtent);
1249 (void) SetImageInfo(write_info,1,sans_exception);
1250 if (*write_info->magick == '\0')
1251 (void) CopyMagickString(write_info->magick,image->magick,MagickPathExtent);
1252 (void) CopyMagickString(filename,image->filename,MagickPathExtent);
1253 (void) CopyMagickString(image->filename,write_info->filename,
1254 MagickPathExtent);
1255 /*
1256 Call appropriate image writer based on image type.
1257 */
1258 magick_info=GetMagickInfo(write_info->magick,sans_exception);
1259 if (sans_exception->severity == PolicyError)
1260 magick_info=GetMagickInfo(write_info->magick,exception);
1261 sans_exception=DestroyExceptionInfo(sans_exception);
1262 if (magick_info != (const MagickInfo *) NULL)
1263 {
1264 if (GetMagickEndianSupport(magick_info) == MagickFalse)
1265 image->endian=UndefinedEndian;
1266 else
1267 if ((image_info->endian == UndefinedEndian) &&
1268 (GetMagickRawSupport(magick_info) != MagickFalse))
1269 {
1270 unsigned long
1271 lsb_first;
1272
1273 lsb_first=1;
1274 image->endian=(*(char *) &lsb_first) == 1 ? LSBEndian : MSBEndian;
1275 }
1276 }
1277 SyncImageProfiles(image);
1278 DisassociateImageStream(image);
1279 option=GetImageOption(image_info,"delegate:bimodal");
1280 if ((IsStringTrue(option) != MagickFalse) &&
1281 (write_info->page == (char *) NULL) &&
1282 (GetPreviousImageInList(image) == (Image *) NULL) &&
1283 (GetNextImageInList(image) == (Image *) NULL) &&
1284 (IsTaintImage(image) == MagickFalse) )
1285 {
1286 delegate_info=GetDelegateInfo(image->magick,write_info->magick,exception);
1287 if ((delegate_info != (const DelegateInfo *) NULL) &&
1288 (GetDelegateMode(delegate_info) == 0) &&
1289 (IsPathAccessible(image->magick_filename) != MagickFalse))
1290 {
1291 /*
1292 Process image with bi-modal delegate.
1293 */
1294 (void) CopyMagickString(image->filename,image->magick_filename,
1295 MagickPathExtent);
1296 status=InvokeDelegate(write_info,image,image->magick,
1297 write_info->magick,exception);
1298 write_info=DestroyImageInfo(write_info);
1299 (void) CopyMagickString(image->filename,filename,MagickPathExtent);
1300 return(status);
1301 }
1302 }
1303 status=MagickFalse;
1304 temporary=MagickFalse;
1305 if ((magick_info != (const MagickInfo *) NULL) &&
1306 (GetMagickEncoderSeekableStream(magick_info) != MagickFalse))
1307 {
1308 char
1309 image_filename[MagickPathExtent];
1310
1311 (void) CopyMagickString(image_filename,image->filename,MagickPathExtent);
1312 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
1313 (void) CopyMagickString(image->filename, image_filename,MagickPathExtent);
1314 if (status != MagickFalse)
1315 {
1316 if (IsBlobSeekable(image) == MagickFalse)
1317 {
1318 /*
1319 A seekable stream is required by the encoder.
1320 */
1321 write_info->adjoin=MagickTrue;
1322 (void) CopyMagickString(write_info->filename,image->filename,
1323 MagickPathExtent);
1324 (void) AcquireUniqueFilename(image->filename);
1325 temporary=MagickTrue;
1326 }
1327 if (CloseBlob(image) == MagickFalse)
1328 status=MagickFalse;
1329 }
1330 }
1331 encoder=GetImageEncoder(magick_info);
1332 if (encoder != (EncodeImageHandler *) NULL)
1333 {
1334 /*
1335 Call appropriate image writer based on image type.
1336 */
1337 if ((magick_info != (const MagickInfo *) NULL) &&
1338 (GetMagickEncoderThreadSupport(magick_info) == MagickFalse))
1339 LockSemaphoreInfo(magick_info->semaphore);
1340 status=IsCoderAuthorized(write_info->magick,WritePolicyRights,exception);
1341 if (status != MagickFalse)
1342 status=encoder(write_info,image,exception);
1343 if ((magick_info != (const MagickInfo *) NULL) &&
1344 (GetMagickEncoderThreadSupport(magick_info) == MagickFalse))
1345 UnlockSemaphoreInfo(magick_info->semaphore);
1346 }
1347 else
1348 {
1349 delegate_info=GetDelegateInfo((char *) NULL,write_info->magick,exception);
1350 if (delegate_info != (DelegateInfo *) NULL)
1351 {
1352 /*
1353 Process the image with delegate.
1354 */
1355 *write_info->filename='\0';
1356 if (GetDelegateThreadSupport(delegate_info) == MagickFalse)
1357 LockSemaphoreInfo(delegate_info->semaphore);
1358 status=InvokeDelegate(write_info,image,(char *) NULL,
1359 write_info->magick,exception);
1360 if (GetDelegateThreadSupport(delegate_info) == MagickFalse)
1361 UnlockSemaphoreInfo(delegate_info->semaphore);
1362 (void) CopyMagickString(image->filename,filename,MagickPathExtent);
1363 }
1364 else
1365 {
1366 sans_exception=AcquireExceptionInfo();
1367 magick_info=GetMagickInfo(write_info->magick,sans_exception);
1368 if (sans_exception->severity == PolicyError)
1369 magick_info=GetMagickInfo(write_info->magick,exception);
1370 sans_exception=DestroyExceptionInfo(sans_exception);
1371 if ((write_info->affirm == MagickFalse) &&
1372 (magick_info == (const MagickInfo *) NULL))
1373 {
1374 (void) CopyMagickString(write_info->magick,image->magick,
1375 MagickPathExtent);
1376 magick_info=GetMagickInfo(write_info->magick,exception);
1377 }
1378 encoder=GetImageEncoder(magick_info);
1379 if (encoder == (EncodeImageHandler *) NULL)
1380 {
1381 char
1382 extension[MagickPathExtent];
1383
1384 GetPathComponent(image->filename,ExtensionPath,extension);
1385 if (*extension != '\0')
1386 magick_info=GetMagickInfo(extension,exception);
1387 else
1388 magick_info=GetMagickInfo(image->magick,exception);
1389 (void) CopyMagickString(image->filename,filename,
1390 MagickPathExtent);
1391 encoder=GetImageEncoder(magick_info);
1392 (void) ThrowMagickException(exception,GetMagickModule(),
1393 MissingDelegateWarning,"NoEncodeDelegateForThisImageFormat",
1394 "`%s'",write_info->magick);
1395 }
1396 if (encoder == (EncodeImageHandler *) NULL)
1397 {
1398 magick_info=GetMagickInfo(image->magick,exception);
1399 encoder=GetImageEncoder(magick_info);
1400 if (encoder == (EncodeImageHandler *) NULL)
1401 (void) ThrowMagickException(exception,GetMagickModule(),
1402 MissingDelegateError,"NoEncodeDelegateForThisImageFormat",
1403 "`%s'",write_info->magick);
1404 }
1405 if (encoder != (EncodeImageHandler *) NULL)
1406 {
1407 /*
1408 Call appropriate image writer based on image type.
1409 */
1410 if (GetMagickEncoderThreadSupport(magick_info) == MagickFalse)
1411 LockSemaphoreInfo(magick_info->semaphore);
1412 status=IsCoderAuthorized(write_info->magick,WritePolicyRights,
1413 exception);
1414 if (status != MagickFalse)
1415 status=encoder(write_info,image,exception);
1416 if (GetMagickEncoderThreadSupport(magick_info) == MagickFalse)
1417 UnlockSemaphoreInfo(magick_info->semaphore);
1418 }
1419 }
1420 }
1421 if (temporary != MagickFalse)
1422 {
1423 /*
1424 Copy temporary image file to permanent.
1425 */
1426 status=OpenBlob(write_info,image,ReadBinaryBlobMode,exception);
1427 if (status != MagickFalse)
1428 {
1429 (void) RelinquishUniqueFileResource(write_info->filename);
1430 status=ImageToFile(image,write_info->filename,exception);
1431 }
1432 if (CloseBlob(image) == MagickFalse)
1433 status=MagickFalse;
1434 (void) RelinquishUniqueFileResource(image->filename);
1435 (void) CopyMagickString(image->filename,write_info->filename,
1436 MagickPathExtent);
1437 }
1438 if ((LocaleCompare(write_info->magick,"info") != 0) &&
1439 (write_info->verbose != MagickFalse))
1440 (void) IdentifyImage(image,stdout,MagickFalse,exception);
1441 write_info=DestroyImageInfo(write_info);
1442 if (GetBlobError(image) != MagickFalse)
1443 ThrowWriterException(FileOpenError,"UnableToWriteFile");
1444 return(status);
1445}
1446
1447/*
1448%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1449% %
1450% %
1451% %
1452% W r i t e I m a g e s %
1453% %
1454% %
1455% %
1456%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1457%
1458% WriteImages() writes an image sequence into one or more files. While
1459% WriteImage() can write an image sequence, it is limited to writing
1460% the sequence into a single file using a format which supports multiple
1461% frames. WriteImages(), however, does not have this limitation, instead it
1462% generates multiple output files if necessary (or when requested). When
1463% ImageInfo's adjoin flag is set to MagickFalse, the file name is expected
1464% to include a printf-style formatting string for the frame number (e.g.
1465% "image%02d.png").
1466%
1467% The format of the WriteImages method is:
1468%
1469% MagickBooleanType WriteImages(const ImageInfo *image_info,Image *images,
1470% const char *filename,ExceptionInfo *exception)
1471%
1472% A description of each parameter follows:
1473%
1474% o image_info: the image info.
1475%
1476% o images: the image list.
1477%
1478% o filename: the image filename.
1479%
1480% o exception: return any errors or warnings in this structure.
1481%
1482*/
1483MagickExport MagickBooleanType WriteImages(const ImageInfo *image_info,
1484 Image *images,const char *filename,ExceptionInfo *exception)
1485{
1486#define WriteImageTag "Write/Image"
1487
1488 ExceptionInfo
1489 *sans_exception;
1490
1491 ImageInfo
1492 *write_info;
1493
1494 MagickBooleanType
1495 proceed;
1496
1497 MagickOffsetType
1498 progress;
1499
1500 MagickProgressMonitor
1501 progress_monitor;
1502
1503 MagickSizeType
1504 number_images;
1505
1506 MagickStatusType
1507 status;
1508
1509 Image
1510 *p;
1511
1512 assert(image_info != (const ImageInfo *) NULL);
1513 assert(image_info->signature == MagickCoreSignature);
1514 assert(images != (Image *) NULL);
1515 assert(images->signature == MagickCoreSignature);
1516 if (IsEventLogging() != MagickFalse)
1517 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",images->filename);
1518 assert(exception != (ExceptionInfo *) NULL);
1519 write_info=CloneImageInfo(image_info);
1520 *write_info->magick='\0';
1521 images=GetFirstImageInList(images);
1522 if (images == (Image *) NULL)
1523 return(MagickFalse);
1524 if (filename != (const char *) NULL)
1525 for (p=images; p != (Image *) NULL; p=GetNextImageInList(p))
1526 (void) CopyMagickString(p->filename,filename,MagickPathExtent);
1527 (void) CopyMagickString(write_info->filename,images->filename,
1528 MagickPathExtent);
1529 sans_exception=AcquireExceptionInfo();
1530 (void) SetImageInfo(write_info,(unsigned int) GetImageListLength(images),
1531 sans_exception);
1532 sans_exception=DestroyExceptionInfo(sans_exception);
1533 if (*write_info->magick == '\0')
1534 (void) CopyMagickString(write_info->magick,images->magick,MagickPathExtent);
1535 p=images;
1536 for ( ; GetNextImageInList(p) != (Image *) NULL; p=GetNextImageInList(p))
1537 {
1538 if (p->scene >= GetNextImageInList(p)->scene)
1539 {
1540 ssize_t
1541 i;
1542
1543 /*
1544 Generate consistent scene numbers.
1545 */
1546 i=(ssize_t) images->scene;
1547 for (p=images; p != (Image *) NULL; p=GetNextImageInList(p))
1548 p->scene=(size_t) i++;
1549 break;
1550 }
1551 }
1552 /*
1553 Write images.
1554 */
1555 status=MagickTrue;
1556 progress_monitor=(MagickProgressMonitor) NULL;
1557 progress=0;
1558 number_images=GetImageListLength(images);
1559 for (p=images; p != (Image *) NULL; p=GetNextImageInList(p))
1560 {
1561 if (number_images != 1)
1562 progress_monitor=SetImageProgressMonitor(p,(MagickProgressMonitor) NULL,
1563 p->client_data);
1564 status&=(MagickStatusType) WriteImage(write_info,p,exception);
1565 if (number_images != 1)
1566 (void) SetImageProgressMonitor(p,progress_monitor,p->client_data);
1567 if (write_info->adjoin != MagickFalse)
1568 break;
1569 if (number_images != 1)
1570 {
1571#if defined(MAGICKCORE_OPENMP_SUPPORT)
1572 #pragma omp atomic
1573#endif
1574 progress++;
1575 proceed=SetImageProgress(p,WriteImageTag,progress,number_images);
1576 if (proceed == MagickFalse)
1577 break;
1578 }
1579 }
1580 write_info=DestroyImageInfo(write_info);
1581 return(status != 0 ? MagickTrue : MagickFalse);
1582}