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