MagickCore 7.1.2-37
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 (IsBlobTemporary(image) != MagickFalse)
809 (void) RelinquishUniqueFileResource(read_info->filename);
810 if (exception->severity >= ErrorException)
811 {
812 (void) LogMagickEvent(ExceptionEvent,GetMagickModule(),
813 "Coder (%s) generated an image despite an error (%d), "
814 "notify the developers",image->magick,exception->severity);
815 return(DestroyImageList(image));
816 }
817 if (read_info->ping != MagickFalse)
818 {
819 for (next=image; next != (Image *) NULL; next=GetNextImageInList(next))
820 {
821 if ((image->columns == 0) || (image->rows == 0))
822 {
823 read_info=DestroyImageInfo(read_info);
824 ThrowReaderException(ImageError,"NegativeOrZeroImageSize");
825 }
826 }
827 }
828 if ((postprocess != MagickFalse) &&
829 (IsSceneGeometry(read_info->scenes,MagickFalse) != MagickFalse) &&
830 (GetImageListLength(image) != 1))
831 {
832 Image
833 *clones;
834
835 clones=CloneImages(image,read_info->scenes,exception);
836 image=DestroyImageList(image);
837 if (clones != (Image *) NULL)
838 image=GetFirstImageInList(clones);
839 if (image == (Image *) NULL)
840 {
841 read_info=DestroyImageInfo(read_info);
842 return(image);
843 }
844 }
845 InitializeConstituteInfo(read_info,&constitute_info);
846 for (next=image; next != (Image *) NULL; next=GetNextImageInList(next))
847 {
848 char
849 magick_path[MagickPathExtent],
850 *property;
851
852 const StringInfo
853 *profile;
854
855 next->taint=MagickFalse;
856 GetPathComponent(magick_filename,MagickPath,magick_path);
857 if ((*magick_path == '\0') && (*next->magick == '\0'))
858 (void) CopyMagickString(next->magick,magick,MagickPathExtent);
859 (void) CopyMagickString(next->magick_filename,magick_filename,
860 MagickPathExtent);
861 if (IsBlobTemporary(image) != MagickFalse)
862 (void) CopyMagickString(next->filename,filename,MagickPathExtent);
863 if (next->magick_columns == 0)
864 next->magick_columns=next->columns;
865 if (next->magick_rows == 0)
866 next->magick_rows=next->rows;
867 if (postprocess == MagickFalse)
868 {
869 image=next;
870 continue;
871 }
872 (void) GetImageProperty(next,"exif:*",exception);
873 (void) GetImageProperty(next,"icc:*",exception);
874 (void) GetImageProperty(next,"iptc:*",exception);
875 (void) GetImageProperty(next,"xmp:*",exception);
876 SyncOrientationFromProperties(next,&constitute_info,exception);
877 SyncResolutionFromProperties(next,&constitute_info,exception);
878 if (next->page.width == 0)
879 next->page.width=next->columns;
880 if (next->page.height == 0)
881 next->page.height=next->rows;
882 if (constitute_info.caption != (const char *) NULL)
883 {
884 property=InterpretImageProperties(read_info,next,
885 constitute_info.caption,exception);
886 (void) SetImageProperty(next,"caption",property,exception);
887 property=DestroyString(property);
888 }
889 if (constitute_info.comment != (const char *) NULL)
890 {
891 property=InterpretImageProperties(read_info,next,
892 constitute_info.comment,exception);
893 (void) SetImageProperty(next,"comment",property,exception);
894 property=DestroyString(property);
895 }
896 if (constitute_info.label != (const char *) NULL)
897 {
898 property=InterpretImageProperties(read_info,next,
899 constitute_info.label,exception);
900 (void) SetImageProperty(next,"label",property,exception);
901 property=DestroyString(property);
902 }
903 if (LocaleCompare(next->magick,"TEXT") == 0)
904 (void) ParseAbsoluteGeometry("0x0+0+0",&next->page);
905 if ((read_info->extract != (char *) NULL) &&
906 (read_info->stream == (StreamHandler) NULL))
907 {
908 RectangleInfo
909 geometry;
910
911 MagickStatusType
912 flags;
913
914 SetGeometry(next,&geometry);
915 flags=ParseAbsoluteGeometry(read_info->extract,&geometry);
916 if ((next->columns != geometry.width) ||
917 (next->rows != geometry.height))
918 {
919 if (((flags & XValue) != 0) || ((flags & YValue) != 0))
920 {
921 Image *crop_image = CropImage(next,&geometry,exception);
922 if (crop_image != (Image *) NULL)
923 ReplaceImageInList(&next,crop_image);
924 }
925 else
926 if (((flags & WidthValue) != 0) || ((flags & HeightValue) != 0))
927 {
928 flags=ParseRegionGeometry(next,read_info->extract,&geometry,
929 exception);
930 if ((geometry.width != 0) && (geometry.height != 0))
931 {
932 Image *resize_image = ResizeImage(next,geometry.width,
933 geometry.height,next->filter,exception);
934 if (resize_image != (Image *) NULL)
935 ReplaceImageInList(&next,resize_image);
936 }
937 }
938 }
939 }
940 profile=GetImageProfile(next,"icc");
941 if (profile == (const StringInfo *) NULL)
942 profile=GetImageProfile(next,"icm");
943 profile=GetImageProfile(next,"iptc");
944 if (profile == (const StringInfo *) NULL)
945 profile=GetImageProfile(next,"8bim");
946 if (IsSourceDataEpochSet() == MagickFalse)
947 {
948 char
949 timestamp[MagickTimeExtent];
950
951 (void) FormatMagickTime(next->timestamp,sizeof(timestamp),timestamp);
952 (void) SetImageProperty(next,"date:timestamp",timestamp,exception);
953 (void) FormatMagickTime((time_t) GetBlobProperties(next)->st_mtime,
954 sizeof(timestamp),timestamp);
955 (void) SetImageProperty(next,"date:modify",timestamp,exception);
956 (void) FormatMagickTime((time_t) GetBlobProperties(next)->st_ctime,
957 sizeof(timestamp),timestamp);
958 (void) SetImageProperty(next,"date:create",timestamp,exception);
959 }
960 if (constitute_info.delay_flags != NoValue)
961 {
962 if ((constitute_info.delay_flags & GreaterValue) != 0)
963 {
964 if (next->delay > constitute_info.delay)
965 next->delay=constitute_info.delay;
966 }
967 else
968 if ((constitute_info.delay_flags & LessValue) != 0)
969 {
970 if (next->delay < constitute_info.delay)
971 next->delay=constitute_info.delay;
972 }
973 else
974 next->delay=constitute_info.delay;
975 if ((constitute_info.delay_flags & SigmaValue) != 0)
976 next->ticks_per_second=constitute_info.ticks_per_second;
977 }
978 if (constitute_info.dispose != (const char *) NULL)
979 {
980 ssize_t
981 option_type;
982
983 option_type=ParseCommandOption(MagickDisposeOptions,MagickFalse,
984 constitute_info.dispose);
985 if (option_type >= 0)
986 next->dispose=(DisposeType) option_type;
987 }
988 if (read_info->verbose != MagickFalse)
989 (void) IdentifyImage(next,stderr,MagickFalse,exception);
990 image=next;
991 }
992 read_info=DestroyImageInfo(read_info);
993 if (GetBlobError(image) != MagickFalse)
994 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
995 return(GetFirstImageInList(image));
996}
997
998MagickExport Image *ReadImage(const ImageInfo *image_info,
999 ExceptionInfo *exception)
1000{
1001 return(ReadImageInternal(image_info,MagickTrue,exception));
1002}
1003␌
1004/*
1005%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1006% %
1007% %
1008% %
1009% 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 %
1010% %
1011% %
1012% %
1013%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1014%
1015% ReadImageWithoutPostProcessing() reads an intermediate image with format
1016% and source-dimension bookkeeping, leaving scene selection and final
1017% post-processing to the enclosing ReadImage() call. On failure, a NULL
1018% image is returned and exception describes the reason for the failure.
1019%
1020% The format of the ReadImageWithoutPostProcessing method is:
1021%
1022% Image *ReadImageWithoutPostProcessing(const ImageInfo *image_info,
1023% ExceptionInfo *exception)
1024%
1025% A description of each parameter follows:
1026%
1027% o image_info: Read the image defined by the file or filename members of
1028% this structure.
1029%
1030% o exception: return any errors or warnings in this structure.
1031%
1032*/
1033MagickExport Image *ReadImageWithoutPostProcessing(const ImageInfo *image_info,
1034 ExceptionInfo *exception)
1035{
1036 return(ReadImageInternal(image_info,MagickFalse,exception));
1037}
1038␌
1039/*
1040%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1041% %
1042% %
1043% %
1044% R e a d I m a g e s %
1045% %
1046% %
1047% %
1048%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1049%
1050% ReadImages() reads one or more images and returns them as an image list.
1051%
1052% The format of the ReadImage method is:
1053%
1054% Image *ReadImages(ImageInfo *image_info,const char *filename,
1055% ExceptionInfo *exception)
1056%
1057% A description of each parameter follows:
1058%
1059% o image_info: the image info.
1060%
1061% o filename: the image filename.
1062%
1063% o exception: return any errors or warnings in this structure.
1064%
1065*/
1066MagickExport Image *ReadImages(ImageInfo *image_info,const char *filename,
1067 ExceptionInfo *exception)
1068{
1069 char
1070 read_filename[MagickPathExtent];
1071
1072 Image
1073 *image,
1074 *images;
1075
1076 ImageInfo
1077 *read_info;
1078
1079 /*
1080 Read image list from a file.
1081 */
1082 assert(image_info != (ImageInfo *) NULL);
1083 assert(image_info->signature == MagickCoreSignature);
1084 assert(exception != (ExceptionInfo *) NULL);
1085 if (IsEventLogging() != MagickFalse)
1086 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
1087 image_info->filename);
1088 read_info=CloneImageInfo(image_info);
1089 *read_info->magick='\0';
1090 (void) SetImageOption(read_info,"filename",filename);
1091 (void) CopyMagickString(read_info->filename,filename,MagickPathExtent);
1092 (void) InterpretImageFilename(read_info,(Image *) NULL,filename,
1093 (int) read_info->scene,read_filename,exception);
1094 if (LocaleCompare(read_filename,read_info->filename) != 0)
1095 {
1096 ExceptionInfo
1097 *sans;
1098
1099 ssize_t
1100 extent,
1101 scene;
1102
1103 /*
1104 Images of the form image-%d.png[1-5].
1105 */
1106 sans=AcquireExceptionInfo();
1107 (void) SetImageInfo(read_info,0,sans);
1108 sans=DestroyExceptionInfo(sans);
1109 if (read_info->number_scenes != 0)
1110 {
1111 (void) CopyMagickString(read_filename,read_info->filename,
1112 MagickPathExtent);
1113 images=NewImageList();
1114 extent=(ssize_t) (read_info->scene+read_info->number_scenes);
1115 scene=(ssize_t) read_info->scene;
1116 for ( ; scene < (ssize_t) extent; scene++)
1117 {
1118 (void) InterpretImageFilename(image_info,(Image *) NULL,
1119 read_filename,(int) scene,read_info->filename,exception);
1120 image=ReadImage(read_info,exception);
1121 if (image == (Image *) NULL)
1122 continue;
1123 AppendImageToList(&images,image);
1124 }
1125 read_info=DestroyImageInfo(read_info);
1126 return(images);
1127 }
1128 }
1129 (void) CopyMagickString(read_info->filename,filename,MagickPathExtent);
1130 image=ReadImage(read_info,exception);
1131 read_info=DestroyImageInfo(read_info);
1132 return(image);
1133}
1134␌
1135/*
1136%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1137% %
1138% %
1139% %
1140+ R e a d I n l i n e I m a g e %
1141% %
1142% %
1143% %
1144%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1145%
1146% ReadInlineImage() reads a Base64-encoded inline image or image sequence.
1147% The method returns a NULL if there is a memory shortage or if the image
1148% cannot be read. On failure, a NULL image is returned and exception
1149% describes the reason for the failure.
1150%
1151% The format of the ReadInlineImage method is:
1152%
1153% Image *ReadInlineImage(const ImageInfo *image_info,const char *content,
1154% ExceptionInfo *exception)
1155%
1156% A description of each parameter follows:
1157%
1158% o image_info: the image info.
1159%
1160% o content: the image encoded in Base64.
1161%
1162% o exception: return any errors or warnings in this structure.
1163%
1164*/
1165
1166static MagickBooleanType GetImplicitDataImageType(const char *data_url,
1167 char *magick,ExceptionInfo *exception)
1168{
1169 char
1170 *plus;
1171
1172 const char
1173 *p,
1174 *type,
1175 *slash;
1176
1177 const MagickInfo
1178 *magick_info;
1179
1180 size_t
1181 offset;
1182
1183 if (data_url == (const char *) NULL)
1184 return(MagickFalse);
1185 if (strncmp(data_url,"data:",5) != 0)
1186 return(MagickFalse);
1187 type=data_url+5;
1188 p=type;
1189 while ((*p != '\0') && (*p != ';') && (*p != ','))
1190 p++;
1191 if (p == type)
1192 return(MagickFalse);
1193 slash=(const char *) memchr(type,'/',(size_t) (p-type));
1194 if ((slash == (const char *) NULL) || ((slash+1) >= p))
1195 return(MagickFalse);
1196 offset=(size_t) (p-(slash+1));
1197 if (offset >= MagickPathExtent)
1198 return(MagickFalse);
1199 (void) memcpy(magick,slash+1,offset);
1200 magick[offset]='\0';
1201 plus=strchr(magick,'+');
1202 if (plus != (char *) NULL)
1203 *plus='\0';
1204 if (LocaleNCompare(magick,"x-",2) == 0)
1205 (void) memmove(magick,magick+2,strlen(magick+2)+1);
1206 magick_info=GetMagickInfo(magick,exception);
1207 if ((magick_info == (const MagickInfo *) NULL) ||
1208 (magick_info->format_type == ImplicitFormatType))
1209 return(MagickFalse);
1210 return(MagickTrue);
1211}
1212
1213MagickExport Image *ReadInlineImage(const ImageInfo *image_info,
1214 const char *content,ExceptionInfo *exception)
1215{
1216 const char
1217 *p;
1218
1219 Image
1220 *image = (Image *) NULL;
1221
1222 ImageInfo
1223 *read_info;
1224
1225 size_t
1226 length;
1227
1228 unsigned char
1229 *blob;
1230
1231 /*
1232 Skip over header (e.g. data:image/gif;base64,).
1233 */
1234 image=NewImageList();
1235 for (p=content; (*p != ',') && (*p != '\0'); p++) ;
1236 if (*p == '\0')
1237 ThrowReaderException(CorruptImageError,"CorruptImage");
1238 blob=Base64Decode(++p,&length);
1239 if (length == 0)
1240 {
1241 blob=(unsigned char *) RelinquishMagickMemory(blob);
1242 ThrowReaderException(CorruptImageError,"CorruptImage");
1243 }
1244 read_info=CloneImageInfo(image_info);
1245 (void) SetImageInfoProgressMonitor(read_info,(MagickProgressMonitor) NULL,
1246 (void *) NULL);
1247 *read_info->filename='\0';
1248 if (GetImplicitDataImageType(content,read_info->magick,exception) == MagickFalse)
1249 {
1250 blob=(unsigned char *) RelinquishMagickMemory(blob);
1251 read_info=DestroyImageInfo(read_info);
1252 ThrowReaderException(ImageError,"ImageTypeNotSupported");
1253 }
1254 image=BlobToImage(read_info,blob,length,exception);
1255 blob=(unsigned char *) RelinquishMagickMemory(blob);
1256 read_info=DestroyImageInfo(read_info);
1257 return(image);
1258}
1259␌
1260/*
1261%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1262% %
1263% %
1264% %
1265% W r i t e I m a g e %
1266% %
1267% %
1268% %
1269%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1270%
1271% WriteImage() writes an image or an image sequence to a file or file handle.
1272% If writing to a file is on disk, the name is defined by the filename member
1273% of the image structure. WriteImage() returns MagickFalse is there is a
1274% memory shortage or if the image cannot be written. Check the exception
1275% member of image to determine the cause for any failure.
1276%
1277% The format of the WriteImage method is:
1278%
1279% MagickBooleanType WriteImage(const ImageInfo *image_info,Image *image,
1280% ExceptionInfo *exception)
1281%
1282% A description of each parameter follows:
1283%
1284% o image_info: the image info.
1285%
1286% o image: the image.
1287%
1288% o exception: return any errors or warnings in this structure.
1289%
1290*/
1291MagickExport MagickBooleanType WriteImage(const ImageInfo *image_info,
1292 Image *image,ExceptionInfo *exception)
1293{
1294 char
1295 filename[MagickPathExtent];
1296
1297 const char
1298 *option;
1299
1300 const DelegateInfo
1301 *delegate_info;
1302
1303 const MagickInfo
1304 *magick_info;
1305
1306 EncodeImageHandler
1307 *encoder;
1308
1309 ExceptionInfo
1310 *sans_exception;
1311
1312 ImageInfo
1313 *write_info;
1314
1315 MagickBooleanType
1316 status,
1317 temporary;
1318
1319 /*
1320 Determine image type from filename prefix or suffix (e.g. image.jpg).
1321 */
1322 assert(image_info != (ImageInfo *) NULL);
1323 assert(image_info->signature == MagickCoreSignature);
1324 assert(image != (Image *) NULL);
1325 if (IsEventLogging() != MagickFalse)
1326 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
1327 image_info->filename);
1328 assert(image->signature == MagickCoreSignature);
1329 assert(exception != (ExceptionInfo *) NULL);
1330 sans_exception=AcquireExceptionInfo();
1331 write_info=CloneImageInfo(image_info);
1332 (void) CopyMagickString(write_info->filename,image->filename,
1333 MagickPathExtent);
1334 (void) SetImageInfo(write_info,1,sans_exception);
1335 if (*write_info->magick == '\0')
1336 (void) CopyMagickString(write_info->magick,image->magick,MagickPathExtent);
1337 (void) CopyMagickString(filename,image->filename,MagickPathExtent);
1338 (void) CopyMagickString(image->filename,write_info->filename,
1339 MagickPathExtent);
1340 /*
1341 Call appropriate image writer based on image type.
1342 */
1343 magick_info=GetMagickInfo(write_info->magick,sans_exception);
1344 if (sans_exception->severity == PolicyError)
1345 magick_info=GetMagickInfo(write_info->magick,exception);
1346 sans_exception=DestroyExceptionInfo(sans_exception);
1347 if (magick_info != (const MagickInfo *) NULL)
1348 {
1349 if (GetMagickEndianSupport(magick_info) == MagickFalse)
1350 image->endian=UndefinedEndian;
1351 else
1352 if ((image_info->endian == UndefinedEndian) &&
1353 (GetMagickRawSupport(magick_info) != MagickFalse))
1354 image->endian=GetHostEndian();
1355 }
1356 if ((image->ping != MagickFalse) &&
1357 (SyncImagePixelCache(image,exception) == MagickFalse))
1358 {
1359 write_info=DestroyImageInfo(write_info);
1360 return(MagickFalse);
1361 }
1362 SyncImageProfiles(image);
1363 DisassociateImageStream(image);
1364 option=GetImageOption(image_info,"delegate:bimodal");
1365 if ((IsStringTrue(option) != MagickFalse) &&
1366 (write_info->page == (char *) NULL) &&
1367 (GetPreviousImageInList(image) == (Image *) NULL) &&
1368 (GetNextImageInList(image) == (Image *) NULL) &&
1369 (IsTaintImage(image) == MagickFalse) )
1370 {
1371 delegate_info=GetDelegateInfo(image->magick,write_info->magick,exception);
1372 if ((delegate_info != (const DelegateInfo *) NULL) &&
1373 (GetDelegateMode(delegate_info) == 0) &&
1374 (IsPathAccessible(image->magick_filename) != MagickFalse))
1375 {
1376 /*
1377 Process image with bi-modal delegate.
1378 */
1379 (void) CopyMagickString(image->filename,image->magick_filename,
1380 MagickPathExtent);
1381 status=InvokeDelegate(write_info,image,image->magick,
1382 write_info->magick,exception);
1383 write_info=DestroyImageInfo(write_info);
1384 (void) CopyMagickString(image->filename,filename,MagickPathExtent);
1385 return(status);
1386 }
1387 }
1388 status=MagickFalse;
1389 temporary=MagickFalse;
1390 if ((magick_info != (const MagickInfo *) NULL) &&
1391 (GetMagickEncoderSeekableStream(magick_info) != MagickFalse))
1392 {
1393 char
1394 image_filename[MagickPathExtent];
1395
1396 (void) CopyMagickString(image_filename,image->filename,MagickPathExtent);
1397 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
1398 (void) CopyMagickString(image->filename, image_filename,MagickPathExtent);
1399 if (status != MagickFalse)
1400 {
1401 if (IsBlobSeekable(image) == MagickFalse)
1402 {
1403 /*
1404 A seekable stream is required by the encoder.
1405 */
1406 write_info->adjoin=MagickTrue;
1407 (void) CopyMagickString(write_info->filename,image->filename,
1408 MagickPathExtent);
1409 (void) AcquireUniqueFilename(image->filename);
1410 temporary=MagickTrue;
1411 }
1412 if (CloseBlob(image) == MagickFalse)
1413 status=MagickFalse;
1414 }
1415 }
1416 encoder=GetImageEncoder(magick_info);
1417 if (encoder != (EncodeImageHandler *) NULL)
1418 {
1419 /*
1420 Call appropriate image writer based on image type.
1421 */
1422 if (GetMagickEncoderThreadSupport(magick_info) == MagickFalse)
1423 LockSemaphoreInfo(magick_info->semaphore);
1424 status=IsCoderAuthorized(magick_info->magick_module,magick_info->name,
1425 WritePolicyRights,exception);
1426 if (status != MagickFalse)
1427 status=encoder(write_info,image,exception);
1428 if (GetMagickEncoderThreadSupport(magick_info) == MagickFalse)
1429 UnlockSemaphoreInfo(magick_info->semaphore);
1430 }
1431 else
1432 {
1433 delegate_info=GetDelegateInfo((char *) NULL,write_info->magick,exception);
1434 if (delegate_info != (DelegateInfo *) NULL)
1435 {
1436 /*
1437 Process the image with delegate.
1438 */
1439 *write_info->filename='\0';
1440 if (GetDelegateThreadSupport(delegate_info) == MagickFalse)
1441 LockSemaphoreInfo(delegate_info->semaphore);
1442 status=InvokeDelegate(write_info,image,(char *) NULL,
1443 write_info->magick,exception);
1444 if (GetDelegateThreadSupport(delegate_info) == MagickFalse)
1445 UnlockSemaphoreInfo(delegate_info->semaphore);
1446 (void) CopyMagickString(image->filename,filename,MagickPathExtent);
1447 }
1448 else
1449 {
1450 sans_exception=AcquireExceptionInfo();
1451 magick_info=GetMagickInfo(write_info->magick,sans_exception);
1452 if (sans_exception->severity == PolicyError)
1453 magick_info=GetMagickInfo(write_info->magick,exception);
1454 sans_exception=DestroyExceptionInfo(sans_exception);
1455 if ((write_info->affirm == MagickFalse) &&
1456 (magick_info == (const MagickInfo *) NULL))
1457 {
1458 (void) CopyMagickString(write_info->magick,image->magick,
1459 MagickPathExtent);
1460 magick_info=GetMagickInfo(write_info->magick,exception);
1461 }
1462 encoder=GetImageEncoder(magick_info);
1463 if (encoder == (EncodeImageHandler *) NULL)
1464 {
1465 char
1466 extension[MagickPathExtent];
1467
1468 GetPathComponent(image->filename,ExtensionPath,extension);
1469 if (*extension != '\0')
1470 magick_info=GetMagickInfo(extension,exception);
1471 else
1472 magick_info=GetMagickInfo(image->magick,exception);
1473 (void) CopyMagickString(image->filename,filename,
1474 MagickPathExtent);
1475 encoder=GetImageEncoder(magick_info);
1476 (void) ThrowMagickException(exception,GetMagickModule(),
1477 MissingDelegateWarning,"NoEncodeDelegateForThisImageFormat",
1478 "`%s'",write_info->magick);
1479 }
1480 if (encoder == (EncodeImageHandler *) NULL)
1481 {
1482 magick_info=GetMagickInfo(image->magick,exception);
1483 encoder=GetImageEncoder(magick_info);
1484 if (encoder == (EncodeImageHandler *) NULL)
1485 (void) ThrowMagickException(exception,GetMagickModule(),
1486 MissingDelegateError,"NoEncodeDelegateForThisImageFormat",
1487 "`%s'",write_info->magick);
1488 }
1489 if (encoder != (EncodeImageHandler *) NULL)
1490 {
1491 /*
1492 Call appropriate image writer based on image type.
1493 */
1494 if (GetMagickEncoderThreadSupport(magick_info) == MagickFalse)
1495 LockSemaphoreInfo(magick_info->semaphore);
1496 status=IsCoderAuthorized(magick_info->magick_module,magick_info->name,
1497 WritePolicyRights,exception);
1498 if (status != MagickFalse)
1499 status=encoder(write_info,image,exception);
1500 if (GetMagickEncoderThreadSupport(magick_info) == MagickFalse)
1501 UnlockSemaphoreInfo(magick_info->semaphore);
1502 }
1503 }
1504 }
1505 if (temporary != MagickFalse)
1506 {
1507 /*
1508 Copy temporary image file to permanent.
1509 */
1510 status=OpenBlob(write_info,image,ReadBinaryBlobMode,exception);
1511 if (status != MagickFalse)
1512 {
1513 (void) RelinquishUniqueFileResource(write_info->filename);
1514 status=ImageToFile(image,write_info->filename,exception);
1515 }
1516 if (CloseBlob(image) == MagickFalse)
1517 status=MagickFalse;
1518 (void) RelinquishUniqueFileResource(image->filename);
1519 (void) CopyMagickString(image->filename,write_info->filename,
1520 MagickPathExtent);
1521 }
1522 if ((LocaleCompare(write_info->magick,"info") != 0) &&
1523 (write_info->verbose != MagickFalse))
1524 (void) IdentifyImage(image,stdout,MagickFalse,exception);
1525 write_info=DestroyImageInfo(write_info);
1526 if (GetBlobError(image) != MagickFalse)
1527 ThrowWriterException(FileOpenError,"UnableToWriteFile");
1528 return(status);
1529}
1530␌
1531/*
1532%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1533% %
1534% %
1535% %
1536% W r i t e I m a g e s %
1537% %
1538% %
1539% %
1540%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1541%
1542% WriteImages() writes an image sequence into one or more files. While
1543% WriteImage() can write an image sequence, it is limited to writing
1544% the sequence into a single file using a format which supports multiple
1545% frames. WriteImages(), however, does not have this limitation, instead it
1546% generates multiple output files if necessary (or when requested). When
1547% ImageInfo's adjoin flag is set to MagickFalse, the file name is expected
1548% to include a printf-style formatting string for the frame number (e.g.
1549% "image%02d.png").
1550%
1551% The format of the WriteImages method is:
1552%
1553% MagickBooleanType WriteImages(const ImageInfo *image_info,Image *images,
1554% const char *filename,ExceptionInfo *exception)
1555%
1556% A description of each parameter follows:
1557%
1558% o image_info: the image info.
1559%
1560% o images: the image list.
1561%
1562% o filename: the image filename.
1563%
1564% o exception: return any errors or warnings in this structure.
1565%
1566*/
1567MagickExport MagickBooleanType WriteImages(const ImageInfo *image_info,
1568 Image *images,const char *filename,ExceptionInfo *exception)
1569{
1570#define WriteImageTag "Write/Image"
1571
1572 ExceptionInfo
1573 *sans_exception;
1574
1575 ImageInfo
1576 *write_info;
1577
1578 MagickBooleanType
1579 proceed;
1580
1581 MagickOffsetType
1582 progress;
1583
1584 MagickProgressMonitor
1585 progress_monitor;
1586
1587 MagickSizeType
1588 number_images;
1589
1590 MagickStatusType
1591 status;
1592
1593 Image
1594 *p;
1595
1596 assert(image_info != (const ImageInfo *) NULL);
1597 assert(image_info->signature == MagickCoreSignature);
1598 assert(images != (Image *) NULL);
1599 assert(images->signature == MagickCoreSignature);
1600 if (IsEventLogging() != MagickFalse)
1601 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",images->filename);
1602 assert(exception != (ExceptionInfo *) NULL);
1603 write_info=CloneImageInfo(image_info);
1604 *write_info->magick='\0';
1605 images=GetFirstImageInList(images);
1606 if (images == (Image *) NULL)
1607 return(MagickFalse);
1608 if (filename != (const char *) NULL)
1609 for (p=images; p != (Image *) NULL; p=GetNextImageInList(p))
1610 (void) CopyMagickString(p->filename,filename,MagickPathExtent);
1611 (void) CopyMagickString(write_info->filename,images->filename,
1612 MagickPathExtent);
1613 sans_exception=AcquireExceptionInfo();
1614 (void) SetImageInfo(write_info,(unsigned int) GetImageListLength(images),
1615 sans_exception);
1616 sans_exception=DestroyExceptionInfo(sans_exception);
1617 if (*write_info->magick == '\0')
1618 (void) CopyMagickString(write_info->magick,images->magick,MagickPathExtent);
1619 p=images;
1620 for ( ; GetNextImageInList(p) != (Image *) NULL; p=GetNextImageInList(p))
1621 {
1622 if (p->scene >= GetNextImageInList(p)->scene)
1623 {
1624 ssize_t
1625 i;
1626
1627 /*
1628 Generate consistent scene numbers.
1629 */
1630 i=(ssize_t) images->scene;
1631 for (p=images; p != (Image *) NULL; p=GetNextImageInList(p))
1632 p->scene=(size_t) i++;
1633 break;
1634 }
1635 }
1636 /*
1637 Write images.
1638 */
1639 status=MagickTrue;
1640 progress_monitor=(MagickProgressMonitor) NULL;
1641 progress=0;
1642 number_images=GetImageListLength(images);
1643 for (p=images; p != (Image *) NULL; p=GetNextImageInList(p))
1644 {
1645 if (number_images != 1)
1646 progress_monitor=SetImageProgressMonitor(p,(MagickProgressMonitor) NULL,
1647 p->client_data);
1648 status&=(MagickStatusType) WriteImage(write_info,p,exception);
1649 if (write_info->blob != (void *) NULL)
1650 SyncBlobStream(write_info,p,images);
1651 if (number_images != 1)
1652 (void) SetImageProgressMonitor(p,progress_monitor,p->client_data);
1653 if (write_info->adjoin != MagickFalse)
1654 break;
1655 if (number_images != 1)
1656 {
1657#if defined(MAGICKCORE_OPENMP_SUPPORT)
1658 #pragma omp atomic
1659#endif
1660 progress++;
1661 proceed=SetImageProgress(p,WriteImageTag,progress,number_images);
1662 if (proceed == MagickFalse)
1663 break;
1664 }
1665 }
1666 write_info=DestroyImageInfo(write_info);
1667 return(status != 0 ? MagickTrue : MagickFalse);
1668}