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