MagickCore 7.1.2-26
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 ThrowPolicyException(coder,MagickFalse);
456 if (IsRightsAuthorized(ModulePolicyDomain,rights,module) == MagickFalse)
457 ThrowPolicyException(module,MagickFalse);
458 return(MagickTrue);
459}
460
461static void InitializeConstituteInfo(const ImageInfo *image_info,
462 ConstituteInfo *constitute_info)
463{
464 const char
465 *option;
466
467 memset(constitute_info,0,sizeof(*constitute_info));
468 constitute_info->sync_from_exif=MagickTrue;
469 constitute_info->sync_from_tiff=MagickTrue;
470 option=GetImageOption(image_info,"exif:sync-image");
471 if (IsStringFalse(option) != MagickFalse)
472 constitute_info->sync_from_exif=MagickFalse;
473 option=GetImageOption(image_info,"tiff:sync-image");
474 if (IsStringFalse(option) != MagickFalse)
475 constitute_info->sync_from_tiff=MagickFalse;
476 constitute_info->caption=GetImageOption(image_info,"caption");
477 constitute_info->comment=GetImageOption(image_info,"comment");
478 constitute_info->label=GetImageOption(image_info,"label");
479 option=GetImageOption(image_info,"delay");
480 if (option != (const char *) NULL)
481 {
482 GeometryInfo
483 geometry_info;
484
485 constitute_info->delay_flags=ParseGeometry(option,&geometry_info);
486 if (constitute_info->delay_flags != NoValue)
487 {
488 constitute_info->delay=(size_t) floor(geometry_info.rho+0.5);
489 if ((constitute_info->delay_flags & SigmaValue) != 0)
490 constitute_info->ticks_per_second=CastDoubleToSsizeT(floor(
491 geometry_info.sigma+0.5));
492 }
493 }
494}
495
496static void SyncOrientationFromProperties(Image *image,
497 ConstituteInfo *constitute_info,ExceptionInfo *exception)
498{
499 const char
500 *orientation;
501
502 orientation=(const char *) NULL;
503 if (constitute_info->sync_from_exif != MagickFalse)
504 {
505 orientation=GetImageProperty(image,"exif:Orientation",exception);
506 if (orientation != (const char *) NULL)
507 {
508 image->orientation=(OrientationType) StringToLong(orientation);
509 (void) DeleteImageProperty(image,"exif:Orientation");
510 }
511 }
512 if ((orientation == (const char *) NULL) &&
513 (constitute_info->sync_from_tiff != MagickFalse))
514 {
515 orientation=GetImageProperty(image,"tiff:Orientation",exception);
516 if (orientation != (const char *) NULL)
517 {
518 image->orientation=(OrientationType) StringToLong(orientation);
519 (void) DeleteImageProperty(image,"tiff:Orientation");
520 }
521 }
522}
523
524static void SyncResolutionFromProperties(Image *image,
525 ConstituteInfo *constitute_info, ExceptionInfo *exception)
526{
527 const char
528 *resolution_x,
529 *resolution_y,
530 *resolution_units;
531
532 MagickBooleanType
533 used_tiff;
534
535 resolution_x=(const char *) NULL;
536 resolution_y=(const char *) NULL;
537 resolution_units=(const char *) NULL;
538 used_tiff=MagickFalse;
539 if (constitute_info->sync_from_exif != MagickFalse)
540 {
541 resolution_x=GetImageProperty(image,"exif:XResolution",exception);
542 resolution_y=GetImageProperty(image,"exif:YResolution",exception);
543 if ((resolution_x != (const char *) NULL) &&
544 (resolution_y != (const char *) NULL))
545 resolution_units=GetImageProperty(image,"exif:ResolutionUnit",
546 exception);
547 }
548 if ((resolution_x == (const char *) NULL) &&
549 (resolution_y == (const char *) NULL) &&
550 (constitute_info->sync_from_tiff != MagickFalse))
551 {
552 resolution_x=GetImageProperty(image,"tiff:XResolution",exception);
553 resolution_y=GetImageProperty(image,"tiff:YResolution",exception);
554 if ((resolution_x != (const char *) NULL) &&
555 (resolution_y != (const char *) NULL))
556 {
557 used_tiff=MagickTrue;
558 resolution_units=GetImageProperty(image,"tiff:ResolutionUnit",
559 exception);
560 }
561 }
562 if ((resolution_x != (const char *) NULL) &&
563 (resolution_y != (const char *) NULL))
564 {
565 GeometryInfo
566 geometry_info;
567
568 ssize_t
569 option_type;
570
571 geometry_info.rho=image->resolution.x;
572 geometry_info.sigma=1.0;
573 (void) ParseGeometry(resolution_x,&geometry_info);
574 if (geometry_info.sigma != 0)
575 image->resolution.x=geometry_info.rho/geometry_info.sigma;
576 if (strchr(resolution_x,',') != (char *) NULL)
577 image->resolution.x=geometry_info.rho+geometry_info.sigma/1000.0;
578 geometry_info.rho=image->resolution.y;
579 geometry_info.sigma=1.0;
580 (void) ParseGeometry(resolution_y,&geometry_info);
581 if (geometry_info.sigma != 0)
582 image->resolution.y=geometry_info.rho/geometry_info.sigma;
583 if (strchr(resolution_y,',') != (char *) NULL)
584 image->resolution.y=geometry_info.rho+geometry_info.sigma/1000.0;
585 if (resolution_units != (char *) NULL)
586 {
587 option_type=ParseCommandOption(MagickResolutionOptions,MagickFalse,
588 resolution_units);
589 if (option_type >= 0)
590 image->units=(ResolutionType) option_type;
591 }
592 if (used_tiff == MagickFalse)
593 {
594 (void) DeleteImageProperty(image,"exif:XResolution");
595 (void) DeleteImageProperty(image,"exif:YResolution");
596 (void) DeleteImageProperty(image,"exif:ResolutionUnit");
597 }
598 else
599 {
600 (void) DeleteImageProperty(image,"tiff:XResolution");
601 (void) DeleteImageProperty(image,"tiff:YResolution");
602 (void) DeleteImageProperty(image,"tiff:ResolutionUnit");
603 }
604 }
605}
606
607MagickExport Image *ReadImage(const ImageInfo *image_info,
608 ExceptionInfo *exception)
609{
610 char
611 filename[MagickPathExtent],
612 magick[MagickPathExtent],
613 magick_filename[MagickPathExtent];
614
615 ConstituteInfo
616 constitute_info;
617
618 const DelegateInfo
619 *delegate_info;
620
621 const MagickInfo
622 *magick_info;
623
624 DecodeImageHandler
625 *decoder;
626
627 ExceptionInfo
628 *sans_exception;
629
630 Image
631 *image,
632 *next;
633
634 ImageInfo
635 *read_info;
636
637 MagickBooleanType
638 status;
639
640 /*
641 Determine image type from filename prefix or suffix (e.g. image.jpg).
642 */
643 assert(image_info != (ImageInfo *) NULL);
644 assert(image_info->signature == MagickCoreSignature);
645 assert(image_info->filename != (char *) NULL);
646 if (IsEventLogging() != MagickFalse)
647 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
648 image_info->filename);
649 assert(exception != (ExceptionInfo *) NULL);
650 read_info=CloneImageInfo(image_info);
651 (void) CopyMagickString(magick_filename,read_info->filename,MagickPathExtent);
652 (void) SetImageInfo(read_info,0,exception);
653 (void) CopyMagickString(filename,read_info->filename,MagickPathExtent);
654 (void) CopyMagickString(magick,read_info->magick,MagickPathExtent);
655 /*
656 Call appropriate image reader based on image type.
657 */
658 sans_exception=AcquireExceptionInfo();
659 magick_info=GetMagickInfo(read_info->magick,sans_exception);
660 if (sans_exception->severity == PolicyError)
661 InheritException(exception,sans_exception);
662 sans_exception=DestroyExceptionInfo(sans_exception);
663 if (magick_info != (const MagickInfo *) NULL)
664 {
665 if (GetMagickEndianSupport(magick_info) == MagickFalse)
666 read_info->endian=UndefinedEndian;
667 else
668 if ((image_info->endian == UndefinedEndian) &&
669 (GetMagickRawSupport(magick_info) != MagickFalse))
670 {
671 unsigned long
672 lsb_first;
673
674 lsb_first=1;
675 read_info->endian=(*(char *) &lsb_first) == 1 ? LSBEndian :
676 MSBEndian;
677 }
678 }
679 if ((magick_info != (const MagickInfo *) NULL) &&
680 (GetMagickDecoderSeekableStream(magick_info) != MagickFalse))
681 {
682 image=AcquireImage(read_info,exception);
683 (void) CopyMagickString(image->filename,read_info->filename,
684 MagickPathExtent);
685 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
686 if (status == MagickFalse)
687 {
688 read_info=DestroyImageInfo(read_info);
689 image=DestroyImage(image);
690 return((Image *) NULL);
691 }
692 if (IsBlobSeekable(image) == MagickFalse)
693 {
694 /*
695 Coder requires a seekable stream.
696 */
697 *read_info->filename='\0';
698 status=ImageToFile(image,read_info->filename,exception);
699 if (status == MagickFalse)
700 {
701 (void) CloseBlob(image);
702 read_info=DestroyImageInfo(read_info);
703 image=DestroyImage(image);
704 return((Image *) NULL);
705 }
706 read_info->temporary=MagickTrue;
707 }
708 (void) CloseBlob(image);
709 image=DestroyImage(image);
710 }
711 image=NewImageList();
712 decoder=GetImageDecoder(magick_info);
713 if (decoder == (DecodeImageHandler *) NULL)
714 {
715 delegate_info=GetDelegateInfo(read_info->magick,(char *) NULL,exception);
716 if (delegate_info == (const DelegateInfo *) NULL)
717 {
718 (void) SetImageInfo(read_info,0,exception);
719 (void) CopyMagickString(read_info->filename,filename,
720 MagickPathExtent);
721 magick_info=GetMagickInfo(read_info->magick,exception);
722 decoder=GetImageDecoder(magick_info);
723 }
724 }
725 if (decoder != (DecodeImageHandler *) NULL)
726 {
727 /*
728 Call appropriate image reader based on image type.
729 */
730 if (GetMagickDecoderThreadSupport(magick_info) == MagickFalse)
731 LockSemaphoreInfo(magick_info->semaphore);
732 status=IsCoderAuthorized(magick_info->magick_module,read_info->magick,
733 ReadPolicyRights,exception);
734 image=(Image *) NULL;
735 if (status != MagickFalse)
736 image=decoder(read_info,exception);
737 if (GetMagickDecoderThreadSupport(magick_info) == MagickFalse)
738 UnlockSemaphoreInfo(magick_info->semaphore);
739 }
740 else
741 {
742 delegate_info=GetDelegateInfo(read_info->magick,(char *) NULL,exception);
743 if (delegate_info == (const DelegateInfo *) NULL)
744 {
745 (void) ThrowMagickException(exception,GetMagickModule(),
746 MissingDelegateError,"NoDecodeDelegateForThisImageFormat","`%s'",
747 read_info->filename);
748 if (read_info->temporary != MagickFalse)
749 (void) RelinquishUniqueFileResource(read_info->filename);
750 read_info=DestroyImageInfo(read_info);
751 return((Image *) NULL);
752 }
753 /*
754 Let our decoding delegate process the image.
755 */
756 image=AcquireImage(read_info,exception);
757 if (image == (Image *) NULL)
758 {
759 read_info=DestroyImageInfo(read_info);
760 return((Image *) NULL);
761 }
762 (void) CopyMagickString(image->filename,read_info->filename,
763 MagickPathExtent);
764 *read_info->filename='\0';
765 if (GetDelegateThreadSupport(delegate_info) == MagickFalse)
766 LockSemaphoreInfo(delegate_info->semaphore);
767 status=InvokeDelegate(read_info,image,read_info->magick,(char *) NULL,
768 exception);
769 if (GetDelegateThreadSupport(delegate_info) == MagickFalse)
770 UnlockSemaphoreInfo(delegate_info->semaphore);
771 image=DestroyImageList(image);
772 read_info->temporary=MagickTrue;
773 if (status != MagickFalse)
774 (void) SetImageInfo(read_info,0,exception);
775 magick_info=GetMagickInfo(read_info->magick,exception);
776 decoder=GetImageDecoder(magick_info);
777 if (decoder == (DecodeImageHandler *) NULL)
778 {
779 if (IsPathAccessible(read_info->filename) != MagickFalse)
780 (void) ThrowMagickException(exception,GetMagickModule(),
781 MissingDelegateError,"NoDecodeDelegateForThisImageFormat","`%s'",
782 read_info->magick);
783 else
784 ThrowFileException(exception,FileOpenError,"UnableToOpenFile",
785 read_info->filename);
786 read_info=DestroyImageInfo(read_info);
787 return((Image *) NULL);
788 }
789 /*
790 Call appropriate image reader based on image type.
791 */
792 if (GetMagickDecoderThreadSupport(magick_info) == MagickFalse)
793 LockSemaphoreInfo(magick_info->semaphore);
794 status=IsCoderAuthorized(magick_info->magick_module,read_info->magick,
795 ReadPolicyRights,exception);
796 image=(Image *) NULL;
797 if (status != MagickFalse)
798 image=(decoder)(read_info,exception);
799 if (GetMagickDecoderThreadSupport(magick_info) == MagickFalse)
800 UnlockSemaphoreInfo(magick_info->semaphore);
801 }
802 if (read_info->temporary != MagickFalse)
803 {
804 (void) RelinquishUniqueFileResource(read_info->filename);
805 read_info->temporary=MagickFalse;
806 if (image != (Image *) NULL)
807 (void) CopyMagickString(image->filename,filename,MagickPathExtent);
808 }
809 if (image == (Image *) NULL)
810 {
811 read_info=DestroyImageInfo(read_info);
812 return(image);
813 }
814 if (exception->severity >= ErrorException)
815 (void) LogMagickEvent(ExceptionEvent,GetMagickModule(),
816 "Coder (%s) generated an image despite an error (%d), "
817 "notify the developers",image->magick,exception->severity);
818 if (IsBlobTemporary(image) != MagickFalse)
819 (void) RelinquishUniqueFileResource(read_info->filename);
820 if (read_info->ping != MagickFalse)
821 {
822 for (next=image; next != (Image *) NULL; next=GetNextImageInList(next))
823 {
824 if ((image->columns == 0) || (image->rows == 0))
825 {
826 read_info=DestroyImageInfo(read_info);
827 ThrowReaderException(ImageError,"NegativeOrZeroImageSize");
828 }
829 }
830 }
831 if ((IsSceneGeometry(read_info->scenes,MagickFalse) != MagickFalse) &&
832 (GetImageListLength(image) != 1))
833 {
834 Image
835 *clones;
836
837 clones=CloneImages(image,read_info->scenes,exception);
838 image=DestroyImageList(image);
839 if (clones != (Image *) NULL)
840 image=GetFirstImageInList(clones);
841 if (image == (Image *) NULL)
842 {
843 read_info=DestroyImageInfo(read_info);
844 return(image);
845 }
846 }
847 InitializeConstituteInfo(read_info,&constitute_info);
848 for (next=image; next != (Image *) NULL; next=GetNextImageInList(next))
849 {
850 char
851 magick_path[MagickPathExtent],
852 *property;
853
854 const StringInfo
855 *profile;
856
857 next->taint=MagickFalse;
858 GetPathComponent(magick_filename,MagickPath,magick_path);
859 if ((*magick_path == '\0') && (*next->magick == '\0'))
860 (void) CopyMagickString(next->magick,magick,MagickPathExtent);
861 (void) CopyMagickString(next->magick_filename,magick_filename,
862 MagickPathExtent);
863 if (IsBlobTemporary(image) != MagickFalse)
864 (void) CopyMagickString(next->filename,filename,MagickPathExtent);
865 if (next->magick_columns == 0)
866 next->magick_columns=next->columns;
867 if (next->magick_rows == 0)
868 next->magick_rows=next->rows;
869 (void) GetImageProperty(next,"exif:*",exception);
870 (void) GetImageProperty(next,"icc:*",exception);
871 (void) GetImageProperty(next,"iptc:*",exception);
872 (void) GetImageProperty(next,"xmp:*",exception);
873 SyncOrientationFromProperties(next,&constitute_info,exception);
874 SyncResolutionFromProperties(next,&constitute_info,exception);
875 if (next->page.width == 0)
876 next->page.width=next->columns;
877 if (next->page.height == 0)
878 next->page.height=next->rows;
879 if (constitute_info.caption != (const char *) NULL)
880 {
881 property=InterpretImageProperties(read_info,next,
882 constitute_info.caption,exception);
883 (void) SetImageProperty(next,"caption",property,exception);
884 property=DestroyString(property);
885 }
886 if (constitute_info.comment != (const char *) NULL)
887 {
888 property=InterpretImageProperties(read_info,next,
889 constitute_info.comment,exception);
890 (void) SetImageProperty(next,"comment",property,exception);
891 property=DestroyString(property);
892 }
893 if (constitute_info.label != (const char *) NULL)
894 {
895 property=InterpretImageProperties(read_info,next,
896 constitute_info.label,exception);
897 (void) SetImageProperty(next,"label",property,exception);
898 property=DestroyString(property);
899 }
900 if (LocaleCompare(next->magick,"TEXT") == 0)
901 (void) ParseAbsoluteGeometry("0x0+0+0",&next->page);
902 if ((read_info->extract != (char *) NULL) &&
903 (read_info->stream == (StreamHandler) NULL))
904 {
905 RectangleInfo
906 geometry;
907
908 MagickStatusType
909 flags;
910
911 SetGeometry(next,&geometry);
912 flags=ParseAbsoluteGeometry(read_info->extract,&geometry);
913 if ((next->columns != geometry.width) ||
914 (next->rows != geometry.height))
915 {
916 if (((flags & XValue) != 0) || ((flags & YValue) != 0))
917 {
918 Image *crop_image = CropImage(next,&geometry,exception);
919 if (crop_image != (Image *) NULL)
920 ReplaceImageInList(&next,crop_image);
921 }
922 else
923 if (((flags & WidthValue) != 0) || ((flags & HeightValue) != 0))
924 {
925 flags=ParseRegionGeometry(next,read_info->extract,&geometry,
926 exception);
927 if ((geometry.width != 0) && (geometry.height != 0))
928 {
929 Image *resize_image = ResizeImage(next,geometry.width,
930 geometry.height,next->filter,exception);
931 if (resize_image != (Image *) NULL)
932 ReplaceImageInList(&next,resize_image);
933 }
934 }
935 }
936 }
937 profile=GetImageProfile(next,"icc");
938 if (profile == (const StringInfo *) NULL)
939 profile=GetImageProfile(next,"icm");
940 profile=GetImageProfile(next,"iptc");
941 if (profile == (const StringInfo *) NULL)
942 profile=GetImageProfile(next,"8bim");
943 if (IsSourceDataEpochSet() == MagickFalse)
944 {
945 char
946 timestamp[MagickTimeExtent];
947
948 (void) FormatMagickTime(next->timestamp,sizeof(timestamp),timestamp);
949 (void) SetImageProperty(next,"date:timestamp",timestamp,exception);
950 (void) FormatMagickTime((time_t) GetBlobProperties(next)->st_mtime,
951 sizeof(timestamp),timestamp);
952 (void) SetImageProperty(next,"date:modify",timestamp,exception);
953 (void) FormatMagickTime((time_t) GetBlobProperties(next)->st_ctime,
954 sizeof(timestamp),timestamp);
955 (void) SetImageProperty(next,"date:create",timestamp,exception);
956 }
957 if (constitute_info.delay_flags != NoValue)
958 {
959 if ((constitute_info.delay_flags & GreaterValue) != 0)
960 {
961 if (next->delay > constitute_info.delay)
962 next->delay=constitute_info.delay;
963 }
964 else
965 if ((constitute_info.delay_flags & LessValue) != 0)
966 {
967 if (next->delay < constitute_info.delay)
968 next->delay=constitute_info.delay;
969 }
970 else
971 next->delay=constitute_info.delay;
972 if ((constitute_info.delay_flags & SigmaValue) != 0)
973 next->ticks_per_second=constitute_info.ticks_per_second;
974 }
975 if (constitute_info.dispose != (const char *) NULL)
976 {
977 ssize_t
978 option_type;
979
980 option_type=ParseCommandOption(MagickDisposeOptions,MagickFalse,
981 constitute_info.dispose);
982 if (option_type >= 0)
983 next->dispose=(DisposeType) option_type;
984 }
985 if (read_info->verbose != MagickFalse)
986 (void) IdentifyImage(next,stderr,MagickFalse,exception);
987 image=next;
988 }
989 read_info=DestroyImageInfo(read_info);
990 if (GetBlobError(image) != MagickFalse)
991 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
992 return(GetFirstImageInList(image));
993}
994
995/*
996%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
997% %
998% %
999% %
1000% R e a d I m a g e s %
1001% %
1002% %
1003% %
1004%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1005%
1006% ReadImages() reads one or more images and returns them as an image list.
1007%
1008% The format of the ReadImage method is:
1009%
1010% Image *ReadImages(ImageInfo *image_info,const char *filename,
1011% ExceptionInfo *exception)
1012%
1013% A description of each parameter follows:
1014%
1015% o image_info: the image info.
1016%
1017% o filename: the image filename.
1018%
1019% o exception: return any errors or warnings in this structure.
1020%
1021*/
1022MagickExport Image *ReadImages(ImageInfo *image_info,const char *filename,
1023 ExceptionInfo *exception)
1024{
1025 char
1026 read_filename[MagickPathExtent];
1027
1028 Image
1029 *image,
1030 *images;
1031
1032 ImageInfo
1033 *read_info;
1034
1035 /*
1036 Read image list from a file.
1037 */
1038 assert(image_info != (ImageInfo *) NULL);
1039 assert(image_info->signature == MagickCoreSignature);
1040 assert(exception != (ExceptionInfo *) NULL);
1041 if (IsEventLogging() != MagickFalse)
1042 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
1043 image_info->filename);
1044 read_info=CloneImageInfo(image_info);
1045 *read_info->magick='\0';
1046 (void) SetImageOption(read_info,"filename",filename);
1047 (void) CopyMagickString(read_info->filename,filename,MagickPathExtent);
1048 (void) InterpretImageFilename(read_info,(Image *) NULL,filename,
1049 (int) read_info->scene,read_filename,exception);
1050 if (LocaleCompare(read_filename,read_info->filename) != 0)
1051 {
1052 ExceptionInfo
1053 *sans;
1054
1055 ssize_t
1056 extent,
1057 scene;
1058
1059 /*
1060 Images of the form image-%d.png[1-5].
1061 */
1062 sans=AcquireExceptionInfo();
1063 (void) SetImageInfo(read_info,0,sans);
1064 sans=DestroyExceptionInfo(sans);
1065 if (read_info->number_scenes != 0)
1066 {
1067 (void) CopyMagickString(read_filename,read_info->filename,
1068 MagickPathExtent);
1069 images=NewImageList();
1070 extent=(ssize_t) (read_info->scene+read_info->number_scenes);
1071 scene=(ssize_t) read_info->scene;
1072 for ( ; scene < (ssize_t) extent; scene++)
1073 {
1074 (void) InterpretImageFilename(image_info,(Image *) NULL,
1075 read_filename,(int) scene,read_info->filename,exception);
1076 image=ReadImage(read_info,exception);
1077 if (image == (Image *) NULL)
1078 continue;
1079 AppendImageToList(&images,image);
1080 }
1081 read_info=DestroyImageInfo(read_info);
1082 return(images);
1083 }
1084 }
1085 (void) CopyMagickString(read_info->filename,filename,MagickPathExtent);
1086 image=ReadImage(read_info,exception);
1087 read_info=DestroyImageInfo(read_info);
1088 return(image);
1089}
1090
1091/*
1092%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1093% %
1094% %
1095% %
1096+ R e a d I n l i n e I m a g e %
1097% %
1098% %
1099% %
1100%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1101%
1102% ReadInlineImage() reads a Base64-encoded inline image or image sequence.
1103% The method returns a NULL if there is a memory shortage or if the image
1104% cannot be read. On failure, a NULL image is returned and exception
1105% describes the reason for the failure.
1106%
1107% The format of the ReadInlineImage method is:
1108%
1109% Image *ReadInlineImage(const ImageInfo *image_info,const char *content,
1110% ExceptionInfo *exception)
1111%
1112% A description of each parameter follows:
1113%
1114% o image_info: the image info.
1115%
1116% o content: the image encoded in Base64.
1117%
1118% o exception: return any errors or warnings in this structure.
1119%
1120*/
1121MagickExport Image *ReadInlineImage(const ImageInfo *image_info,
1122 const char *content,ExceptionInfo *exception)
1123{
1124 Image
1125 *image;
1126
1127 ImageInfo
1128 *read_info;
1129
1130 unsigned char
1131 *blob;
1132
1133 size_t
1134 length;
1135
1136 const char
1137 *p;
1138
1139 /*
1140 Skip over header (e.g. data:image/gif;base64,).
1141 */
1142 image=NewImageList();
1143 for (p=content; (*p != ',') && (*p != '\0'); p++) ;
1144 if (*p == '\0')
1145 ThrowReaderException(CorruptImageError,"CorruptImage");
1146 blob=Base64Decode(++p,&length);
1147 if (length == 0)
1148 {
1149 blob=(unsigned char *) RelinquishMagickMemory(blob);
1150 ThrowReaderException(CorruptImageError,"CorruptImage");
1151 }
1152 read_info=CloneImageInfo(image_info);
1153 (void) SetImageInfoProgressMonitor(read_info,(MagickProgressMonitor) NULL,
1154 (void *) NULL);
1155 *read_info->filename='\0';
1156 *read_info->magick='\0';
1157 for (p=content; (*p != '/') && (*p != '\0'); p++) ;
1158 if (*p != '\0')
1159 {
1160 char
1161 *q;
1162
1163 ssize_t
1164 i;
1165
1166 /*
1167 Extract media type.
1168 */
1169 if (LocaleNCompare(++p,"x-",2) == 0)
1170 p+=(ptrdiff_t) 2;
1171 (void) CopyMagickString(read_info->filename,"data.",MagickPathExtent);
1172 q=read_info->filename+5;
1173 for (i=0; (*p != ';') && (*p != '\0') && (i < (MagickPathExtent-6)); i++)
1174 *q++=(*p++);
1175 *q++='\0';
1176 }
1177 image=BlobToImage(read_info,blob,length,exception);
1178 blob=(unsigned char *) RelinquishMagickMemory(blob);
1179 read_info=DestroyImageInfo(read_info);
1180 return(image);
1181}
1182
1183/*
1184%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1185% %
1186% %
1187% %
1188% W r i t e I m a g e %
1189% %
1190% %
1191% %
1192%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1193%
1194% WriteImage() writes an image or an image sequence to a file or file handle.
1195% If writing to a file is on disk, the name is defined by the filename member
1196% of the image structure. WriteImage() returns MagickFalse is there is a
1197% memory shortage or if the image cannot be written. Check the exception
1198% member of image to determine the cause for any failure.
1199%
1200% The format of the WriteImage method is:
1201%
1202% MagickBooleanType WriteImage(const ImageInfo *image_info,Image *image,
1203% ExceptionInfo *exception)
1204%
1205% A description of each parameter follows:
1206%
1207% o image_info: the image info.
1208%
1209% o image: the image.
1210%
1211% o exception: return any errors or warnings in this structure.
1212%
1213*/
1214MagickExport MagickBooleanType WriteImage(const ImageInfo *image_info,
1215 Image *image,ExceptionInfo *exception)
1216{
1217 char
1218 filename[MagickPathExtent];
1219
1220 const char
1221 *option;
1222
1223 const DelegateInfo
1224 *delegate_info;
1225
1226 const MagickInfo
1227 *magick_info;
1228
1229 EncodeImageHandler
1230 *encoder;
1231
1232 ExceptionInfo
1233 *sans_exception;
1234
1235 ImageInfo
1236 *write_info;
1237
1238 MagickBooleanType
1239 status,
1240 temporary;
1241
1242 /*
1243 Determine image type from filename prefix or suffix (e.g. image.jpg).
1244 */
1245 assert(image_info != (ImageInfo *) NULL);
1246 assert(image_info->signature == MagickCoreSignature);
1247 assert(image != (Image *) NULL);
1248 if (IsEventLogging() != MagickFalse)
1249 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
1250 image_info->filename);
1251 assert(image->signature == MagickCoreSignature);
1252 assert(exception != (ExceptionInfo *) NULL);
1253 sans_exception=AcquireExceptionInfo();
1254 write_info=CloneImageInfo(image_info);
1255 (void) CopyMagickString(write_info->filename,image->filename,
1256 MagickPathExtent);
1257 (void) SetImageInfo(write_info,1,sans_exception);
1258 if (*write_info->magick == '\0')
1259 (void) CopyMagickString(write_info->magick,image->magick,MagickPathExtent);
1260 (void) CopyMagickString(filename,image->filename,MagickPathExtent);
1261 (void) CopyMagickString(image->filename,write_info->filename,
1262 MagickPathExtent);
1263 /*
1264 Call appropriate image writer based on image type.
1265 */
1266 magick_info=GetMagickInfo(write_info->magick,sans_exception);
1267 if (sans_exception->severity == PolicyError)
1268 magick_info=GetMagickInfo(write_info->magick,exception);
1269 sans_exception=DestroyExceptionInfo(sans_exception);
1270 if (magick_info != (const MagickInfo *) NULL)
1271 {
1272 if (GetMagickEndianSupport(magick_info) == MagickFalse)
1273 image->endian=UndefinedEndian;
1274 else
1275 if ((image_info->endian == UndefinedEndian) &&
1276 (GetMagickRawSupport(magick_info) != MagickFalse))
1277 {
1278 unsigned long
1279 lsb_first;
1280
1281 lsb_first=1;
1282 image->endian=(*(char *) &lsb_first) == 1 ? LSBEndian : MSBEndian;
1283 }
1284 }
1285 if ((image->ping != MagickFalse) &&
1286 (SyncImagePixelCache(image,exception) == MagickFalse))
1287 {
1288 write_info=DestroyImageInfo(write_info);
1289 return(MagickFalse);
1290 }
1291 SyncImageProfiles(image);
1292 DisassociateImageStream(image);
1293 option=GetImageOption(image_info,"delegate:bimodal");
1294 if ((IsStringTrue(option) != MagickFalse) &&
1295 (write_info->page == (char *) NULL) &&
1296 (GetPreviousImageInList(image) == (Image *) NULL) &&
1297 (GetNextImageInList(image) == (Image *) NULL) &&
1298 (IsTaintImage(image) == MagickFalse) )
1299 {
1300 delegate_info=GetDelegateInfo(image->magick,write_info->magick,exception);
1301 if ((delegate_info != (const DelegateInfo *) NULL) &&
1302 (GetDelegateMode(delegate_info) == 0) &&
1303 (IsPathAccessible(image->magick_filename) != MagickFalse))
1304 {
1305 /*
1306 Process image with bi-modal delegate.
1307 */
1308 (void) CopyMagickString(image->filename,image->magick_filename,
1309 MagickPathExtent);
1310 status=InvokeDelegate(write_info,image,image->magick,
1311 write_info->magick,exception);
1312 write_info=DestroyImageInfo(write_info);
1313 (void) CopyMagickString(image->filename,filename,MagickPathExtent);
1314 return(status);
1315 }
1316 }
1317 status=MagickFalse;
1318 temporary=MagickFalse;
1319 if ((magick_info != (const MagickInfo *) NULL) &&
1320 (GetMagickEncoderSeekableStream(magick_info) != MagickFalse))
1321 {
1322 char
1323 image_filename[MagickPathExtent];
1324
1325 (void) CopyMagickString(image_filename,image->filename,MagickPathExtent);
1326 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
1327 (void) CopyMagickString(image->filename, image_filename,MagickPathExtent);
1328 if (status != MagickFalse)
1329 {
1330 if (IsBlobSeekable(image) == MagickFalse)
1331 {
1332 /*
1333 A seekable stream is required by the encoder.
1334 */
1335 write_info->adjoin=MagickTrue;
1336 (void) CopyMagickString(write_info->filename,image->filename,
1337 MagickPathExtent);
1338 (void) AcquireUniqueFilename(image->filename);
1339 temporary=MagickTrue;
1340 }
1341 if (CloseBlob(image) == MagickFalse)
1342 status=MagickFalse;
1343 }
1344 }
1345 encoder=GetImageEncoder(magick_info);
1346 if (encoder != (EncodeImageHandler *) NULL)
1347 {
1348 /*
1349 Call appropriate image writer based on image type.
1350 */
1351 if (GetMagickEncoderThreadSupport(magick_info) == MagickFalse)
1352 LockSemaphoreInfo(magick_info->semaphore);
1353 status=IsCoderAuthorized(magick_info->magick_module,write_info->magick,
1354 WritePolicyRights,exception);
1355 if (status != MagickFalse)
1356 status=encoder(write_info,image,exception);
1357 if (GetMagickEncoderThreadSupport(magick_info) == MagickFalse)
1358 UnlockSemaphoreInfo(magick_info->semaphore);
1359 }
1360 else
1361 {
1362 delegate_info=GetDelegateInfo((char *) NULL,write_info->magick,exception);
1363 if (delegate_info != (DelegateInfo *) NULL)
1364 {
1365 /*
1366 Process the image with delegate.
1367 */
1368 *write_info->filename='\0';
1369 if (GetDelegateThreadSupport(delegate_info) == MagickFalse)
1370 LockSemaphoreInfo(delegate_info->semaphore);
1371 status=InvokeDelegate(write_info,image,(char *) NULL,
1372 write_info->magick,exception);
1373 if (GetDelegateThreadSupport(delegate_info) == MagickFalse)
1374 UnlockSemaphoreInfo(delegate_info->semaphore);
1375 (void) CopyMagickString(image->filename,filename,MagickPathExtent);
1376 }
1377 else
1378 {
1379 sans_exception=AcquireExceptionInfo();
1380 magick_info=GetMagickInfo(write_info->magick,sans_exception);
1381 if (sans_exception->severity == PolicyError)
1382 magick_info=GetMagickInfo(write_info->magick,exception);
1383 sans_exception=DestroyExceptionInfo(sans_exception);
1384 if ((write_info->affirm == MagickFalse) &&
1385 (magick_info == (const MagickInfo *) NULL))
1386 {
1387 (void) CopyMagickString(write_info->magick,image->magick,
1388 MagickPathExtent);
1389 magick_info=GetMagickInfo(write_info->magick,exception);
1390 }
1391 encoder=GetImageEncoder(magick_info);
1392 if (encoder == (EncodeImageHandler *) NULL)
1393 {
1394 char
1395 extension[MagickPathExtent];
1396
1397 GetPathComponent(image->filename,ExtensionPath,extension);
1398 if (*extension != '\0')
1399 magick_info=GetMagickInfo(extension,exception);
1400 else
1401 magick_info=GetMagickInfo(image->magick,exception);
1402 (void) CopyMagickString(image->filename,filename,
1403 MagickPathExtent);
1404 encoder=GetImageEncoder(magick_info);
1405 (void) ThrowMagickException(exception,GetMagickModule(),
1406 MissingDelegateWarning,"NoEncodeDelegateForThisImageFormat",
1407 "`%s'",write_info->magick);
1408 }
1409 if (encoder == (EncodeImageHandler *) NULL)
1410 {
1411 magick_info=GetMagickInfo(image->magick,exception);
1412 encoder=GetImageEncoder(magick_info);
1413 if (encoder == (EncodeImageHandler *) NULL)
1414 (void) ThrowMagickException(exception,GetMagickModule(),
1415 MissingDelegateError,"NoEncodeDelegateForThisImageFormat",
1416 "`%s'",write_info->magick);
1417 }
1418 if (encoder != (EncodeImageHandler *) NULL)
1419 {
1420 /*
1421 Call appropriate image writer based on image type.
1422 */
1423 if (GetMagickEncoderThreadSupport(magick_info) == MagickFalse)
1424 LockSemaphoreInfo(magick_info->semaphore);
1425 status=IsCoderAuthorized(magick_info->magick_module,write_info->magick,
1426 WritePolicyRights,exception);
1427 if (status != MagickFalse)
1428 status=encoder(write_info,image,exception);
1429 if (GetMagickEncoderThreadSupport(magick_info) == MagickFalse)
1430 UnlockSemaphoreInfo(magick_info->semaphore);
1431 }
1432 }
1433 }
1434 if (temporary != MagickFalse)
1435 {
1436 /*
1437 Copy temporary image file to permanent.
1438 */
1439 status=OpenBlob(write_info,image,ReadBinaryBlobMode,exception);
1440 if (status != MagickFalse)
1441 {
1442 (void) RelinquishUniqueFileResource(write_info->filename);
1443 status=ImageToFile(image,write_info->filename,exception);
1444 }
1445 if (CloseBlob(image) == MagickFalse)
1446 status=MagickFalse;
1447 (void) RelinquishUniqueFileResource(image->filename);
1448 (void) CopyMagickString(image->filename,write_info->filename,
1449 MagickPathExtent);
1450 }
1451 if ((LocaleCompare(write_info->magick,"info") != 0) &&
1452 (write_info->verbose != MagickFalse))
1453 (void) IdentifyImage(image,stdout,MagickFalse,exception);
1454 write_info=DestroyImageInfo(write_info);
1455 if (GetBlobError(image) != MagickFalse)
1456 ThrowWriterException(FileOpenError,"UnableToWriteFile");
1457 return(status);
1458}
1459
1460/*
1461%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1462% %
1463% %
1464% %
1465% W r i t e I m a g e s %
1466% %
1467% %
1468% %
1469%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1470%
1471% WriteImages() writes an image sequence into one or more files. While
1472% WriteImage() can write an image sequence, it is limited to writing
1473% the sequence into a single file using a format which supports multiple
1474% frames. WriteImages(), however, does not have this limitation, instead it
1475% generates multiple output files if necessary (or when requested). When
1476% ImageInfo's adjoin flag is set to MagickFalse, the file name is expected
1477% to include a printf-style formatting string for the frame number (e.g.
1478% "image%02d.png").
1479%
1480% The format of the WriteImages method is:
1481%
1482% MagickBooleanType WriteImages(const ImageInfo *image_info,Image *images,
1483% const char *filename,ExceptionInfo *exception)
1484%
1485% A description of each parameter follows:
1486%
1487% o image_info: the image info.
1488%
1489% o images: the image list.
1490%
1491% o filename: the image filename.
1492%
1493% o exception: return any errors or warnings in this structure.
1494%
1495*/
1496MagickExport MagickBooleanType WriteImages(const ImageInfo *image_info,
1497 Image *images,const char *filename,ExceptionInfo *exception)
1498{
1499#define WriteImageTag "Write/Image"
1500
1501 ExceptionInfo
1502 *sans_exception;
1503
1504 ImageInfo
1505 *write_info;
1506
1507 MagickBooleanType
1508 proceed;
1509
1510 MagickOffsetType
1511 progress;
1512
1513 MagickProgressMonitor
1514 progress_monitor;
1515
1516 MagickSizeType
1517 number_images;
1518
1519 MagickStatusType
1520 status;
1521
1522 Image
1523 *p;
1524
1525 assert(image_info != (const ImageInfo *) NULL);
1526 assert(image_info->signature == MagickCoreSignature);
1527 assert(images != (Image *) NULL);
1528 assert(images->signature == MagickCoreSignature);
1529 if (IsEventLogging() != MagickFalse)
1530 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",images->filename);
1531 assert(exception != (ExceptionInfo *) NULL);
1532 write_info=CloneImageInfo(image_info);
1533 *write_info->magick='\0';
1534 images=GetFirstImageInList(images);
1535 if (images == (Image *) NULL)
1536 return(MagickFalse);
1537 if (filename != (const char *) NULL)
1538 for (p=images; p != (Image *) NULL; p=GetNextImageInList(p))
1539 (void) CopyMagickString(p->filename,filename,MagickPathExtent);
1540 (void) CopyMagickString(write_info->filename,images->filename,
1541 MagickPathExtent);
1542 sans_exception=AcquireExceptionInfo();
1543 (void) SetImageInfo(write_info,(unsigned int) GetImageListLength(images),
1544 sans_exception);
1545 sans_exception=DestroyExceptionInfo(sans_exception);
1546 if (*write_info->magick == '\0')
1547 (void) CopyMagickString(write_info->magick,images->magick,MagickPathExtent);
1548 p=images;
1549 for ( ; GetNextImageInList(p) != (Image *) NULL; p=GetNextImageInList(p))
1550 {
1551 if (p->scene >= GetNextImageInList(p)->scene)
1552 {
1553 ssize_t
1554 i;
1555
1556 /*
1557 Generate consistent scene numbers.
1558 */
1559 i=(ssize_t) images->scene;
1560 for (p=images; p != (Image *) NULL; p=GetNextImageInList(p))
1561 p->scene=(size_t) i++;
1562 break;
1563 }
1564 }
1565 /*
1566 Write images.
1567 */
1568 status=MagickTrue;
1569 progress_monitor=(MagickProgressMonitor) NULL;
1570 progress=0;
1571 number_images=GetImageListLength(images);
1572 for (p=images; p != (Image *) NULL; p=GetNextImageInList(p))
1573 {
1574 if (number_images != 1)
1575 progress_monitor=SetImageProgressMonitor(p,(MagickProgressMonitor) NULL,
1576 p->client_data);
1577 status&=(MagickStatusType) WriteImage(write_info,p,exception);
1578 if (number_images != 1)
1579 (void) SetImageProgressMonitor(p,progress_monitor,p->client_data);
1580 if (write_info->adjoin != MagickFalse)
1581 break;
1582 if (number_images != 1)
1583 {
1584#if defined(MAGICKCORE_OPENMP_SUPPORT)
1585 #pragma omp atomic
1586#endif
1587 progress++;
1588 proceed=SetImageProgress(p,WriteImageTag,progress,number_images);
1589 if (proceed == MagickFalse)
1590 break;
1591 }
1592 }
1593 write_info=DestroyImageInfo(write_info);
1594 return(status != 0 ? MagickTrue : MagickFalse);
1595}