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