blob: 5cc46b605c9d879313f818e30e73f1bba3cec7f5 [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
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 Consitute an Image %
14% %
15% Software Design %
16% John Cristy %
17% October 1998 %
18% %
19% %
cristy16af1cb2009-12-11 21:38:29 +000020% Copyright 1999-2010 ImageMagick Studio LLC, a non-profit organization %
cristy3ed852e2009-09-05 21:47:34 +000021% 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% http://www.imagemagick.org/script/license.php %
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 "magick/studio.h"
43#include "magick/blob.h"
44#include "magick/blob-private.h"
45#include "magick/exception.h"
46#include "magick/exception-private.h"
47#include "magick/cache.h"
48#include "magick/client.h"
49#include "magick/constitute.h"
50#include "magick/delegate.h"
51#include "magick/geometry.h"
52#include "magick/identify.h"
53#include "magick/image-private.h"
54#include "magick/list.h"
55#include "magick/magick.h"
56#include "magick/memory_.h"
57#include "magick/monitor.h"
cristy36507e62010-02-11 00:26:48 +000058#include "magick/monitor-private.h"
cristy3ed852e2009-09-05 21:47:34 +000059#include "magick/option.h"
60#include "magick/pixel.h"
61#include "magick/policy.h"
62#include "magick/profile.h"
63#include "magick/property.h"
64#include "magick/quantum.h"
65#include "magick/resize.h"
66#include "magick/resource_.h"
67#include "magick/semaphore.h"
68#include "magick/statistic.h"
69#include "magick/stream.h"
70#include "magick/string_.h"
cristyf2f27272009-12-17 14:48:46 +000071#include "magick/string-private.h"
cristy3ed852e2009-09-05 21:47:34 +000072#include "magick/timer.h"
73#include "magick/transform.h"
74#include "magick/utility.h"
75
76static SemaphoreInfo
77 *constitute_semaphore = (SemaphoreInfo *) NULL;
78
79/*
80%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
81% %
82% %
83% %
cristyf34a1452009-10-24 22:29:27 +000084+ C o n s t i t u t e C o m p o n e n t G e n e s i s %
85% %
86% %
87% %
88%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
89%
90% ConstituteComponentGenesis() instantiates the constitute component.
91%
92% The format of the ConstituteComponentGenesis method is:
93%
94% MagickBooleanType ConstituteComponentGenesis(void)
95%
96*/
97MagickExport MagickBooleanType ConstituteComponentGenesis(void)
98{
cristy165b6092009-10-26 13:52:10 +000099 AcquireSemaphoreInfo(&constitute_semaphore);
cristyf34a1452009-10-24 22:29:27 +0000100 return(MagickTrue);
101}
102
103/*
104%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
105% %
106% %
107% %
108+ C o n s t i t u t e C o m p o n e n t T e r m i n u s %
109% %
110% %
111% %
112%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
113%
114% ConstituteComponentTerminus() destroys the constitute component.
115%
116% The format of the ConstituteComponentTerminus method is:
117%
118% ConstituteComponentTerminus(void)
119%
120*/
121MagickExport void ConstituteComponentTerminus(void)
122{
cristy18b17442009-10-25 18:36:48 +0000123 if (constitute_semaphore == (SemaphoreInfo *) NULL)
124 AcquireSemaphoreInfo(&constitute_semaphore);
125 DestroySemaphoreInfo(&constitute_semaphore);
cristyf34a1452009-10-24 22:29:27 +0000126}
127
128/*
129%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
130% %
131% %
132% %
cristy3ed852e2009-09-05 21:47:34 +0000133% C o n s t i t u t e I m a g e %
134% %
135% %
136% %
137%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
138%
139% ConstituteImage() returns an image from the pixel data you supply.
140% The pixel data must be in scanline order top-to-bottom. The data can be
141% char, short int, int, float, or double. Float and double require the
142% pixels to be normalized [0..1], otherwise [0..QuantumRange]. For example, to
143% create a 640x480 image from unsigned red-green-blue character data, use:
144%
145% image = ConstituteImage(640,480,"RGB",CharPixel,pixels,&exception);
146%
147% The format of the ConstituteImage method is:
148%
cristy0758f9e2010-05-28 01:44:10 +0000149% Image *ConstituteImage(const size_t columns,const size_t rows,
150% const char *map,const StorageType storage,const void *pixels,
151% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000152%
153% A description of each parameter follows:
154%
155% o columns: width in pixels of the image.
156%
157% o rows: height in pixels of the image.
158%
159% o map: This string reflects the expected ordering of the pixel array.
160% It can be any combination or order of R = red, G = green, B = blue,
161% A = alpha (0 is transparent), O = opacity (0 is opaque), C = cyan,
162% Y = yellow, M = magenta, K = black, I = intensity (for grayscale),
163% P = pad.
164%
165% o storage: Define the data type of the pixels. Float and double types are
166% expected to be normalized [0..1] otherwise [0..QuantumRange]. Choose
167% from these types: CharPixel, DoublePixel, FloatPixel, IntegerPixel,
168% LongPixel, QuantumPixel, or ShortPixel.
169%
170% o pixels: This array of values contain the pixel components as defined by
171% map and type. You must preallocate this array where the expected
172% length varies depending on the values of width, height, map, and type.
173%
174% o exception: return any errors or warnings in this structure.
175%
176*/
cristybb503372010-05-27 20:51:26 +0000177MagickExport Image *ConstituteImage(const size_t columns,
178 const size_t rows,const char *map,const StorageType storage,
cristy3ed852e2009-09-05 21:47:34 +0000179 const void *pixels,ExceptionInfo *exception)
180{
181 Image
182 *image;
183
184 MagickBooleanType
185 status;
186
187 /*
188 Allocate image structure.
189 */
190 assert(map != (const char *) NULL);
191 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",map);
192 assert(pixels != (void *) NULL);
193 assert(exception != (ExceptionInfo *) NULL);
194 assert(exception->signature == MagickSignature);
195 image=AcquireImage((ImageInfo *) NULL);
196 if (image == (Image *) NULL)
197 return((Image *) NULL);
198 if ((columns == 0) || (rows == 0))
199 ThrowImageException(OptionError,"NonZeroWidthAndHeightRequired");
200 image->columns=columns;
201 image->rows=rows;
202 (void) SetImageBackgroundColor(image);
203 status=ImportImagePixels(image,0,0,columns,rows,map,storage,pixels);
204 if (status == MagickFalse)
205 {
206 InheritException(exception,&image->exception);
207 image=DestroyImage(image);
208 }
209 return(image);
210}
211
212/*
213%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
214% %
215% %
216% %
cristy3ed852e2009-09-05 21:47:34 +0000217% P i n g I m a g e %
218% %
219% %
220% %
221%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
222%
223% PingImage() returns all the properties of an image or image sequence
224% except for the pixels. It is much faster and consumes far less memory
225% than ReadImage(). On failure, a NULL image is returned and exception
226% describes the reason for the failure.
227%
228% The format of the PingImage method is:
229%
230% Image *PingImage(const ImageInfo *image_info,ExceptionInfo *exception)
231%
232% A description of each parameter follows:
233%
234% o image_info: Ping the image defined by the file or filename members of
235% this structure.
236%
237% o exception: return any errors or warnings in this structure.
238%
239*/
240
241#if defined(__cplusplus) || defined(c_plusplus)
242extern "C" {
243#endif
244
245static size_t PingStream(const Image *magick_unused(image),
246 const void *magick_unused(pixels),const size_t columns)
247{
248 return(columns);
249}
250
251#if defined(__cplusplus) || defined(c_plusplus)
252}
253#endif
254
255MagickExport Image *PingImage(const ImageInfo *image_info,
256 ExceptionInfo *exception)
257{
258 Image
259 *image;
260
261 ImageInfo
262 *ping_info;
263
264 assert(image_info != (ImageInfo *) NULL);
265 assert(image_info->signature == MagickSignature);
266 if (image_info->debug != MagickFalse)
267 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
268 image_info->filename);
269 assert(exception != (ExceptionInfo *) NULL);
270 ping_info=CloneImageInfo(image_info);
271 ping_info->ping=MagickTrue;
272 image=ReadStream(ping_info,&PingStream,exception);
273 if (image != (Image *) NULL)
274 {
275 ResetTimer(&image->timer);
276 if (ping_info->verbose != MagickFalse)
277 (void) IdentifyImage(image,stdout,MagickFalse);
278 }
279 ping_info=DestroyImageInfo(ping_info);
280 return(image);
281}
282
283/*
284%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
285% %
286% %
287% %
288% P i n g I m a g e s %
289% %
290% %
291% %
292%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
293%
294% PingImages() pings one or more images and returns them as an image list.
295%
296% The format of the PingImage method is:
297%
298% Image *PingImages(const ImageInfo *image_info,ExceptionInfo *exception)
299%
300% A description of each parameter follows:
301%
302% o image_info: the image info.
303%
304% o exception: return any errors or warnings in this structure.
305%
306*/
307MagickExport Image *PingImages(const ImageInfo *image_info,
308 ExceptionInfo *exception)
309{
310 char
311 filename[MaxTextExtent];
312
313 Image
314 *image,
315 *images;
316
317 ImageInfo
318 *read_info;
319
320 /*
321 Ping image list from a file.
322 */
323 assert(image_info != (ImageInfo *) NULL);
324 assert(image_info->signature == MagickSignature);
325 if (image_info->debug != MagickFalse)
326 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
327 image_info->filename);
328 assert(exception != (ExceptionInfo *) NULL);
329 (void) InterpretImageFilename(image_info,(Image *) NULL,image_info->filename,
330 (int) image_info->scene,filename);
331 if (LocaleCompare(filename,image_info->filename) != 0)
332 {
333 ExceptionInfo
334 *sans;
335
cristybb503372010-05-27 20:51:26 +0000336 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000337 extent,
338 scene;
339
340 /*
341 Images of the form image-%d.png[1-5].
342 */
343 read_info=CloneImageInfo(image_info);
344 sans=AcquireExceptionInfo();
cristyd965a422010-03-03 17:47:35 +0000345 (void) SetImageInfo(read_info,0,sans);
cristy3ed852e2009-09-05 21:47:34 +0000346 sans=DestroyExceptionInfo(sans);
347 (void) CopyMagickString(filename,read_info->filename,MaxTextExtent);
348 images=NewImageList();
cristybb503372010-05-27 20:51:26 +0000349 extent=(ssize_t) (read_info->scene+read_info->number_scenes);
350 for (scene=(ssize_t) read_info->scene; scene < (ssize_t) extent; scene++)
cristy3ed852e2009-09-05 21:47:34 +0000351 {
352 (void) InterpretImageFilename(image_info,(Image *) NULL,filename,(int)
353 scene,read_info->filename);
354 image=PingImage(read_info,exception);
355 if (image == (Image *) NULL)
356 continue;
357 AppendImageToList(&images,image);
358 }
359 read_info=DestroyImageInfo(read_info);
360 return(images);
361 }
362 return(PingImage(image_info,exception));
363}
364
365/*
366%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
367% %
368% %
369% %
370% R e a d I m a g e %
371% %
372% %
373% %
374%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
375%
376% ReadImage() reads an image or image sequence from a file or file handle.
377% The method returns a NULL if there is a memory shortage or if the image
378% cannot be read. On failure, a NULL image is returned and exception
379% describes the reason for the failure.
380%
381% The format of the ReadImage method is:
382%
383% Image *ReadImage(const ImageInfo *image_info,ExceptionInfo *exception)
384%
385% A description of each parameter follows:
386%
387% o image_info: Read the image defined by the file or filename members of
388% this structure.
389%
390% o exception: return any errors or warnings in this structure.
391%
392*/
393MagickExport Image *ReadImage(const ImageInfo *image_info,
394 ExceptionInfo *exception)
395{
396 char
397 filename[MaxTextExtent],
398 magick[MaxTextExtent],
399 magick_filename[MaxTextExtent];
400
401 const char
402 *value;
403
404 const DelegateInfo
405 *delegate_info;
406
407 const MagickInfo
408 *magick_info;
409
410 ExceptionInfo
411 *sans_exception;
412
413 GeometryInfo
414 geometry_info;
415
416 Image
417 *image,
418 *next;
419
420 ImageInfo
421 *read_info;
422
423 MagickStatusType
424 flags,
425 thread_support;
426
427 PolicyDomain
428 domain;
429
430 PolicyRights
431 rights;
432
433 /*
434 Determine image type from filename prefix or suffix (e.g. image.jpg).
435 */
436 assert(image_info != (ImageInfo *) NULL);
437 assert(image_info->signature == MagickSignature);
438 assert(image_info->filename != (char *) NULL);
439 if (image_info->debug != MagickFalse)
440 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
441 image_info->filename);
442 assert(exception != (ExceptionInfo *) NULL);
443 read_info=CloneImageInfo(image_info);
444 (void) CopyMagickString(magick_filename,read_info->filename,MaxTextExtent);
cristyd965a422010-03-03 17:47:35 +0000445 (void) SetImageInfo(read_info,0,exception);
cristy3ed852e2009-09-05 21:47:34 +0000446 (void) CopyMagickString(filename,read_info->filename,MaxTextExtent);
447 (void) CopyMagickString(magick,read_info->magick,MaxTextExtent);
448 domain=CoderPolicyDomain;
449 rights=ReadPolicyRights;
450 if (IsRightsAuthorized(domain,rights,read_info->magick) == MagickFalse)
451 {
cristya9197f62010-01-12 02:23:34 +0000452 errno=EPERM;
cristy3ed852e2009-09-05 21:47:34 +0000453 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
454 "NotAuthorized","`%s'",read_info->filename);
455 return((Image *) NULL);
456 }
457 /*
458 Call appropriate image reader based on image type.
459 */
460 sans_exception=AcquireExceptionInfo();
461 magick_info=GetMagickInfo(read_info->magick,sans_exception);
462 sans_exception=DestroyExceptionInfo(sans_exception);
463 if (magick_info != (const MagickInfo *) NULL)
464 {
465 if (GetMagickEndianSupport(magick_info) == MagickFalse)
466 read_info->endian=UndefinedEndian;
467 else
468 if ((image_info->endian == UndefinedEndian) &&
469 (GetMagickRawSupport(magick_info) != MagickFalse))
470 {
cristybb503372010-05-27 20:51:26 +0000471 size_t
cristy3ed852e2009-09-05 21:47:34 +0000472 lsb_first;
473
474 lsb_first=1;
475 read_info->endian=(*(char *) &lsb_first) == 1 ? LSBEndian :
476 MSBEndian;
477 }
478 }
479 if ((magick_info != (const MagickInfo *) NULL) &&
480 (GetMagickSeekableStream(magick_info) != MagickFalse))
481 {
482 MagickBooleanType
483 status;
484
485 image=AcquireImage(read_info);
486 (void) CopyMagickString(image->filename,read_info->filename,
487 MaxTextExtent);
488 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
489 if (status == MagickFalse)
490 {
491 read_info=DestroyImageInfo(read_info);
492 image=DestroyImage(image);
493 return((Image *) NULL);
494 }
495 if (IsBlobSeekable(image) == MagickFalse)
496 {
497 /*
498 Coder requires a seekable stream.
499 */
500 *read_info->filename='\0';
501 status=ImageToFile(image,read_info->filename,exception);
502 if (status == MagickFalse)
503 {
504 (void) CloseBlob(image);
505 read_info=DestroyImageInfo(read_info);
506 image=DestroyImage(image);
507 return((Image *) NULL);
508 }
509 read_info->temporary=MagickTrue;
510 }
511 (void) CloseBlob(image);
512 image=DestroyImage(image);
513 }
514 image=NewImageList();
cristy4e1dff62009-10-25 20:36:03 +0000515 if (constitute_semaphore == (SemaphoreInfo *) NULL)
516 AcquireSemaphoreInfo(&constitute_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000517 if ((magick_info != (const MagickInfo *) NULL) &&
518 (GetImageDecoder(magick_info) != (DecodeImageHandler *) NULL))
519 {
520 thread_support=GetMagickThreadSupport(magick_info);
521 if ((thread_support & DecoderThreadSupport) == 0)
cristyf84a1932010-01-03 18:00:18 +0000522 LockSemaphoreInfo(constitute_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000523 image=GetImageDecoder(magick_info)(read_info,exception);
524 if ((thread_support & DecoderThreadSupport) == 0)
cristyf84a1932010-01-03 18:00:18 +0000525 UnlockSemaphoreInfo(constitute_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000526 }
527 else
528 {
529 delegate_info=GetDelegateInfo(read_info->magick,(char *) NULL,exception);
530 if (delegate_info == (const DelegateInfo *) NULL)
531 {
cristy3a7b2b82010-04-28 22:14:19 +0000532 (void) ThrowMagickException(exception,GetMagickModule(),
533 MissingDelegateError,"NoDecodeDelegateForThisImageFormat","`%s'",
534 read_info->filename);
cristy3ed852e2009-09-05 21:47:34 +0000535 if (read_info->temporary != MagickFalse)
536 (void) RelinquishUniqueFileResource(read_info->filename);
537 read_info=DestroyImageInfo(read_info);
538 return((Image *) NULL);
539 }
540 /*
541 Let our decoding delegate process the image.
542 */
543 image=AcquireImage(read_info);
544 if (image == (Image *) NULL)
545 {
546 read_info=DestroyImageInfo(read_info);
547 return((Image *) NULL);
548 }
549 (void) CopyMagickString(image->filename,read_info->filename,
550 MaxTextExtent);
551 *read_info->filename='\0';
552 if (GetDelegateThreadSupport(delegate_info) == MagickFalse)
cristyf84a1932010-01-03 18:00:18 +0000553 LockSemaphoreInfo(constitute_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000554 (void) InvokeDelegate(read_info,image,read_info->magick,(char *) NULL,
555 exception);
556 if (GetDelegateThreadSupport(delegate_info) == MagickFalse)
cristyf84a1932010-01-03 18:00:18 +0000557 UnlockSemaphoreInfo(constitute_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000558 image=DestroyImageList(image);
559 read_info->temporary=MagickTrue;
cristyd965a422010-03-03 17:47:35 +0000560 (void) SetImageInfo(read_info,0,exception);
cristy3ed852e2009-09-05 21:47:34 +0000561 magick_info=GetMagickInfo(read_info->magick,exception);
562 if ((magick_info == (const MagickInfo *) NULL) ||
563 (GetImageDecoder(magick_info) == (DecodeImageHandler *) NULL))
564 {
565 if (IsPathAccessible(read_info->filename) != MagickFalse)
566 (void) ThrowMagickException(exception,GetMagickModule(),
567 MissingDelegateError,"NoDecodeDelegateForThisImageFormat","`%s'",
568 read_info->filename);
569 else
570 ThrowFileException(exception,FileOpenError,"UnableToOpenFile",
571 read_info->filename);
572 read_info=DestroyImageInfo(read_info);
573 return((Image *) NULL);
574 }
575 thread_support=GetMagickThreadSupport(magick_info);
576 if ((thread_support & DecoderThreadSupport) == 0)
cristyf84a1932010-01-03 18:00:18 +0000577 LockSemaphoreInfo(constitute_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000578 image=(Image *) (GetImageDecoder(magick_info))(read_info,exception);
579 if ((thread_support & DecoderThreadSupport) == 0)
cristyf84a1932010-01-03 18:00:18 +0000580 UnlockSemaphoreInfo(constitute_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000581 }
582 if (read_info->temporary != MagickFalse)
583 {
584 (void) RelinquishUniqueFileResource(read_info->filename);
585 read_info->temporary=MagickFalse;
586 if (image != (Image *) NULL)
587 (void) CopyMagickString(image->filename,filename,MaxTextExtent);
588 }
589 if (image == (Image *) NULL)
590 {
591 read_info=DestroyImageInfo(read_info);
592 return(image);
593 }
594 if (exception->severity >= ErrorException)
595 (void) LogMagickEvent(ExceptionEvent,GetMagickModule(),
596 "Coder (%s) generated an image despite an error (%d), "
597 "notify the developers",image->magick,exception->severity);
598 if (IsBlobTemporary(image) != MagickFalse)
599 (void) RelinquishUniqueFileResource(read_info->filename);
600 if ((GetNextImageInList(image) != (Image *) NULL) &&
601 (IsSceneGeometry(read_info->scenes,MagickFalse) != MagickFalse))
602 {
603 Image
604 *clones;
605
606 clones=CloneImages(image,read_info->scenes,exception);
607 if (clones == (Image *) NULL)
608 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
609 "SubimageSpecificationReturnsNoImages","`%s'",read_info->filename);
610 else
611 {
612 image=DestroyImageList(image);
613 image=GetFirstImageInList(clones);
614 }
615 }
616 if (GetBlobError(image) != MagickFalse)
617 {
618 ThrowFileException(exception,FileOpenError,
619 "AnErrorHasOccurredReadingFromFile",read_info->filename);
620 image=DestroyImageList(image);
621 read_info=DestroyImageInfo(read_info);
622 return((Image *) NULL);
623 }
624 for (next=image; next != (Image *) NULL; next=GetNextImageInList(next))
625 {
626 char
cristy7734b602010-09-13 18:32:30 +0000627 *property,
cristy3ed852e2009-09-05 21:47:34 +0000628 timestamp[MaxTextExtent];
629
cristy05e38ec2010-08-20 15:20:12 +0000630 const char
631 *option;
632
cristy3ed852e2009-09-05 21:47:34 +0000633 const StringInfo
634 *profile;
635
636 next->taint=MagickFalse;
637 if (next->magick_columns == 0)
638 next->magick_columns=next->columns;
639 if (next->magick_rows == 0)
640 next->magick_rows=next->rows;
641 if ((LocaleCompare(magick,"HTTP") != 0) &&
642 (LocaleCompare(magick,"FTP") != 0))
643 (void) CopyMagickString(next->magick,magick,MaxTextExtent);
644 (void) CopyMagickString(next->magick_filename,magick_filename,
645 MaxTextExtent);
646 if (IsBlobTemporary(image) != MagickFalse)
647 (void) CopyMagickString(next->filename,filename,MaxTextExtent);
648 value=GetImageProperty(next,"tiff:Orientation");
649 if (value == (char *) NULL)
650 value=GetImageProperty(next,"exif:Orientation");
651 if (value != (char *) NULL)
652 {
cristyf2f27272009-12-17 14:48:46 +0000653 next->orientation=(OrientationType) StringToLong(value);
cristy3ed852e2009-09-05 21:47:34 +0000654 (void) DeleteImageProperty(next,"tiff:Orientation");
655 (void) DeleteImageProperty(next,"exif:Orientation");
656 }
cristy9a98aa42010-05-14 16:07:28 +0000657 value=GetImageProperty(next,"exif:XResolution");
cristy3ed852e2009-09-05 21:47:34 +0000658 if (value != (char *) NULL)
659 {
660 geometry_info.rho=next->x_resolution;
661 geometry_info.sigma=1.0;
662 flags=ParseGeometry(value,&geometry_info);
663 if (geometry_info.sigma != 0)
664 next->x_resolution=geometry_info.rho/geometry_info.sigma;
665 (void) DeleteImageProperty(next,"exif:XResolution");
cristy3ed852e2009-09-05 21:47:34 +0000666 }
cristy9a98aa42010-05-14 16:07:28 +0000667 value=GetImageProperty(next,"exif:YResolution");
cristy3ed852e2009-09-05 21:47:34 +0000668 if (value != (char *) NULL)
669 {
670 geometry_info.rho=next->y_resolution;
671 geometry_info.sigma=1.0;
672 flags=ParseGeometry(value,&geometry_info);
673 if (geometry_info.sigma != 0)
674 next->y_resolution=geometry_info.rho/geometry_info.sigma;
675 (void) DeleteImageProperty(next,"exif:YResolution");
cristy3ed852e2009-09-05 21:47:34 +0000676 }
677 value=GetImageProperty(next,"tiff:ResolutionUnit");
678 if (value == (char *) NULL)
679 value=GetImageProperty(next,"exif:ResolutionUnit");
680 if (value != (char *) NULL)
681 {
cristyf2f27272009-12-17 14:48:46 +0000682 next->units=(ResolutionType) (StringToLong(value)-1);
cristy3ed852e2009-09-05 21:47:34 +0000683 (void) DeleteImageProperty(next,"exif:ResolutionUnit");
684 (void) DeleteImageProperty(next,"tiff:ResolutionUnit");
685 }
686 if (next->page.width == 0)
687 next->page.width=next->columns;
688 if (next->page.height == 0)
689 next->page.height=next->rows;
cristy7734b602010-09-13 18:32:30 +0000690 option=GetImageOption(read_info,"caption");
691 if (option != (const char *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000692 {
cristy7734b602010-09-13 18:32:30 +0000693 property=InterpretImageProperties(read_info,next,option);
694 (void) SetImageProperty(next,"caption",property);
695 property=DestroyString(property);
696 }
697 option=GetImageOption(read_info,"comment");
698 if (option != (const char *) NULL)
699 {
700 property=InterpretImageProperties(read_info,next,option);
701 (void) SetImageProperty(next,"comment",property);
702 property=DestroyString(property);
703 }
704 option=GetImageOption(read_info,"label");
705 if (option != (const char *) NULL)
706 {
707 property=InterpretImageProperties(read_info,next,option);
708 (void) SetImageProperty(next,"label",property);
709 property=DestroyString(property);
cristy3ed852e2009-09-05 21:47:34 +0000710 }
711 if (LocaleCompare(next->magick,"TEXT") == 0)
712 (void) ParseAbsoluteGeometry("0x0+0+0",&next->page);
713 if ((read_info->extract != (char *) NULL) &&
714 (read_info->stream == (StreamHandler) NULL))
715 {
716 RectangleInfo
717 geometry;
718
719 flags=ParseAbsoluteGeometry(read_info->extract,&geometry);
720 if ((next->columns != geometry.width) ||
721 (next->rows != geometry.height))
722 {
723 if (((flags & XValue) != 0) || ((flags & YValue) != 0))
724 {
725 Image
726 *crop_image;
727
728 crop_image=CropImage(next,&geometry,exception);
729 if (crop_image != (Image *) NULL)
730 ReplaceImageInList(&next,crop_image);
731 }
732 else
733 if (((flags & WidthValue) != 0) || ((flags & HeightValue) != 0))
734 {
735 Image
736 *size_image;
737
738 flags=ParseRegionGeometry(next,read_info->extract,&geometry,
739 exception);
740 size_image=ResizeImage(next,geometry.width,geometry.height,
741 next->filter,next->blur,exception);
742 if (size_image != (Image *) NULL)
743 ReplaceImageInList(&next,size_image);
744 }
745 }
746 }
747 profile=GetImageProfile(next,"icc");
748 if (profile == (const StringInfo *) NULL)
749 profile=GetImageProfile(next,"icm");
750 if (profile != (const StringInfo *) NULL)
751 {
752 next->color_profile.length=GetStringInfoLength(profile);
753 next->color_profile.info=GetStringInfoDatum(profile);
754 }
755 profile=GetImageProfile(next,"iptc");
756 if (profile == (const StringInfo *) NULL)
757 profile=GetImageProfile(next,"8bim");
758 if (profile != (const StringInfo *) NULL)
759 {
760 next->iptc_profile.length=GetStringInfoLength(profile);
761 next->iptc_profile.info=GetStringInfoDatum(profile);
762 }
763 (void) FormatMagickTime(GetBlobProperties(next)->st_mtime,MaxTextExtent,
764 timestamp);
765 (void) SetImageProperty(next,"date:modify",timestamp);
766 (void) FormatMagickTime(GetBlobProperties(next)->st_ctime,MaxTextExtent,
767 timestamp);
768 (void) SetImageProperty(next,"date:create",timestamp);
cristy74f82a62010-08-19 22:24:53 +0000769 option=GetImageOption(image_info,"delay");
770 if (option != (const char *) NULL)
771 {
772 GeometryInfo
773 geometry_info;
774
775 flags=ParseGeometry(option,&geometry_info);
776 if ((flags & GreaterValue) != 0)
777 {
cristy7734b602010-09-13 18:32:30 +0000778 if (next->delay > (size_t) floor(geometry_info.rho+0.5))
779 next->delay=(size_t) floor(geometry_info.rho+0.5);
cristy74f82a62010-08-19 22:24:53 +0000780 }
781 else
782 if ((flags & LessValue) != 0)
783 {
cristy7734b602010-09-13 18:32:30 +0000784 if (next->delay < (size_t) floor(geometry_info.rho+0.5))
785 next->ticks_per_second=(ssize_t) floor(geometry_info.sigma+0.5);
cristy74f82a62010-08-19 22:24:53 +0000786 }
787 else
cristy7734b602010-09-13 18:32:30 +0000788 next->delay=(size_t) floor(geometry_info.rho+0.5);
cristy74f82a62010-08-19 22:24:53 +0000789 if ((flags & SigmaValue) != 0)
cristy7734b602010-09-13 18:32:30 +0000790 next->ticks_per_second=(ssize_t) floor(geometry_info.sigma+0.5);
cristy74f82a62010-08-19 22:24:53 +0000791 }
792 option=GetImageOption(image_info,"dispose");
793 if (option != (const char *) NULL)
cristy7734b602010-09-13 18:32:30 +0000794 next->dispose=(DisposeType) ParseMagickOption(MagickDisposeOptions,
cristy74f82a62010-08-19 22:24:53 +0000795 MagickFalse,option);
cristy3ed852e2009-09-05 21:47:34 +0000796 if (read_info->verbose != MagickFalse)
cristy54439632010-07-15 00:43:34 +0000797 (void) IdentifyImage(next,stderr,MagickFalse);
cristy3ed852e2009-09-05 21:47:34 +0000798 image=next;
799 }
800 read_info=DestroyImageInfo(read_info);
801 return(GetFirstImageInList(image));
802}
803
804/*
805%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
806% %
807% %
808% %
809% R e a d I m a g e s %
810% %
811% %
812% %
813%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
814%
815% ReadImages() reads one or more images and returns them as an image list.
816%
817% The format of the ReadImage method is:
818%
819% Image *ReadImages(const ImageInfo *image_info,ExceptionInfo *exception)
820%
821% A description of each parameter follows:
822%
823% o image_info: the image info.
824%
825% o exception: return any errors or warnings in this structure.
826%
827*/
828MagickExport Image *ReadImages(const ImageInfo *image_info,
829 ExceptionInfo *exception)
830{
831 char
832 filename[MaxTextExtent];
833
834 Image
835 *image,
836 *images;
837
838 ImageInfo
839 *read_info;
840
841 /*
842 Read image list from a file.
843 */
844 assert(image_info != (ImageInfo *) NULL);
845 assert(image_info->signature == MagickSignature);
846 if (image_info->debug != MagickFalse)
847 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
848 image_info->filename);
849 assert(exception != (ExceptionInfo *) NULL);
850 (void) InterpretImageFilename(image_info,(Image *) NULL,image_info->filename,
851 (int) image_info->scene,filename);
852 if (LocaleCompare(filename,image_info->filename) != 0)
853 {
854 ExceptionInfo
855 *sans;
856
cristybb503372010-05-27 20:51:26 +0000857 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000858 extent,
859 scene;
860
861 /*
862 Images of the form image-%d.png[1-5].
863 */
864 read_info=CloneImageInfo(image_info);
865 sans=AcquireExceptionInfo();
cristyd965a422010-03-03 17:47:35 +0000866 (void) SetImageInfo(read_info,0,sans);
cristy3ed852e2009-09-05 21:47:34 +0000867 sans=DestroyExceptionInfo(sans);
868 (void) CopyMagickString(filename,read_info->filename,MaxTextExtent);
869 images=NewImageList();
cristybb503372010-05-27 20:51:26 +0000870 extent=(ssize_t) (read_info->scene+read_info->number_scenes);
871 for (scene=(ssize_t) read_info->scene; scene < (ssize_t) extent; scene++)
cristy3ed852e2009-09-05 21:47:34 +0000872 {
873 (void) InterpretImageFilename(image_info,(Image *) NULL,filename,(int)
874 scene,read_info->filename);
875 image=ReadImage(read_info,exception);
876 if (image == (Image *) NULL)
877 continue;
878 AppendImageToList(&images,image);
879 }
880 read_info=DestroyImageInfo(read_info);
881 return(images);
882 }
883 return(ReadImage(image_info,exception));
884}
885
886/*
887%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
888% %
889% %
890% %
891+ R e a d I n l i n e I m a g e %
892% %
893% %
894% %
895%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
896%
897% ReadInlineImage() reads a Base64-encoded inline image or image sequence.
898% The method returns a NULL if there is a memory shortage or if the image
899% cannot be read. On failure, a NULL image is returned and exception
900% describes the reason for the failure.
901%
902% The format of the ReadInlineImage method is:
903%
904% Image *ReadInlineImage(const ImageInfo *image_info,const char *content,
905% ExceptionInfo *exception)
906%
907% A description of each parameter follows:
908%
909% o image_info: the image info.
910%
911% o content: the image encoded in Base64.
912%
913% o exception: return any errors or warnings in this structure.
914%
915*/
916MagickExport Image *ReadInlineImage(const ImageInfo *image_info,
917 const char *content,ExceptionInfo *exception)
918{
919 Image
920 *image;
921
922 ImageInfo
923 *read_info;
924
925 unsigned char
926 *blob;
927
928 size_t
929 length;
930
931 register const char
932 *p;
933
cristyd5562a92010-06-05 13:43:49 +0000934 /*
935 Skip over header (e.g. data:image/gif;base64,).
936 */
cristy3ed852e2009-09-05 21:47:34 +0000937 image=NewImageList();
938 for (p=content; (*p != ',') && (*p != '\0'); p++) ;
939 if (*p == '\0')
940 ThrowReaderException(CorruptImageError,"CorruptImage");
941 p++;
942 length=0;
943 blob=Base64Decode(p,&length);
944 if (length == 0)
945 ThrowReaderException(CorruptImageError,"CorruptImage");
946 read_info=CloneImageInfo(image_info);
947 (void) SetImageInfoProgressMonitor(read_info,(MagickProgressMonitor) NULL,
948 (void *) NULL);
cristycc1fc8a2010-06-05 13:35:05 +0000949 *read_info->filename='\0';
950 *read_info->magick='\0';
cristy3ed852e2009-09-05 21:47:34 +0000951 image=BlobToImage(read_info,blob,length,exception);
952 blob=(unsigned char *) RelinquishMagickMemory(blob);
953 read_info=DestroyImageInfo(read_info);
954 return(image);
955}
956
957/*
958%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
959% %
960% %
961% %
962% W r i t e I m a g e %
963% %
964% %
965% %
966%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
967%
968% WriteImage() writes an image or an image sequence to a file or filehandle.
969% If writing to a file on disk, the name is defined by the filename member of
970% the image structure. Write() returns MagickFalse is these is a memory
971% shortage or if the image cannot be written. Check the exception member of
972% image to determine the cause for any failure.
973%
974% The format of the WriteImage method is:
975%
976% MagickBooleanType WriteImage(const ImageInfo *image_info,Image *image)
977%
978% A description of each parameter follows:
979%
980% o image_info: the image info.
981%
982% o image: the image.
983%
984*/
985MagickExport MagickBooleanType WriteImage(const ImageInfo *image_info,
986 Image *image)
987{
988 char
989 filename[MaxTextExtent];
990
cristy4c491342009-11-02 01:46:10 +0000991 const char
992 *option;
993
cristy3ed852e2009-09-05 21:47:34 +0000994 const DelegateInfo
995 *delegate_info;
996
997 const MagickInfo
998 *magick_info;
999
1000 ExceptionInfo
1001 *sans_exception;
1002
1003 ImageInfo
1004 *write_info;
1005
1006 MagickBooleanType
1007 status,
1008 temporary;
1009
1010 MagickStatusType
1011 thread_support;
1012
1013 PolicyDomain
1014 domain;
1015
1016 PolicyRights
1017 rights;
1018
1019 /*
1020 Determine image type from filename prefix or suffix (e.g. image.jpg).
1021 */
1022 assert(image_info != (ImageInfo *) NULL);
1023 assert(image_info->signature == MagickSignature);
1024 if (image->debug != MagickFalse)
1025 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
1026 image_info->filename);
1027 assert(image != (Image *) NULL);
1028 assert(image->signature == MagickSignature);
1029 sans_exception=AcquireExceptionInfo();
1030 write_info=CloneImageInfo(image_info);
1031 (void) CopyMagickString(write_info->filename,image->filename,MaxTextExtent);
1032 if (*write_info->magick == '\0')
1033 (void) CopyMagickString(write_info->magick,image->magick,MaxTextExtent);
cristyd965a422010-03-03 17:47:35 +00001034 (void) SetImageInfo(write_info,1,sans_exception);
cristy3ed852e2009-09-05 21:47:34 +00001035 if (LocaleCompare(write_info->magick,"clipmask") == 0)
1036 {
1037 if (image->clip_mask == (Image *) NULL)
1038 {
1039 (void) ThrowMagickException(&image->exception,GetMagickModule(),
1040 OptionError,"NoClipPathDefined","`%s'",image->filename);
1041 return(MagickFalse);
1042 }
1043 image=image->clip_mask;
cristyd965a422010-03-03 17:47:35 +00001044 (void) SetImageInfo(write_info,1,sans_exception);
cristy3ed852e2009-09-05 21:47:34 +00001045 }
1046 (void) CopyMagickString(filename,image->filename,MaxTextExtent);
1047 (void) CopyMagickString(image->filename,write_info->filename,MaxTextExtent);
1048 domain=CoderPolicyDomain;
1049 rights=WritePolicyRights;
1050 if (IsRightsAuthorized(domain,rights,write_info->magick) == MagickFalse)
1051 {
1052 sans_exception=DestroyExceptionInfo(sans_exception);
cristya9197f62010-01-12 02:23:34 +00001053 errno=EPERM;
cristy3ed852e2009-09-05 21:47:34 +00001054 ThrowBinaryException(PolicyError,"NotAuthorized",filename);
1055 }
1056 magick_info=GetMagickInfo(write_info->magick,sans_exception);
1057 sans_exception=DestroyExceptionInfo(sans_exception);
1058 if (magick_info != (const MagickInfo *) NULL)
1059 {
1060 if (GetMagickEndianSupport(magick_info) == MagickFalse)
1061 image->endian=UndefinedEndian;
1062 else
1063 if ((image_info->endian == UndefinedEndian) &&
1064 (GetMagickRawSupport(magick_info) != MagickFalse))
1065 {
cristybb503372010-05-27 20:51:26 +00001066 size_t
cristy3ed852e2009-09-05 21:47:34 +00001067 lsb_first;
1068
1069 lsb_first=1;
1070 image->endian=(*(char *) &lsb_first) == 1 ? LSBEndian : MSBEndian;
1071 }
1072 }
1073 (void) SyncImageProfiles(image);
cristy4c491342009-11-02 01:46:10 +00001074 option=GetImageOption(image_info,"delegate:bimodal");
1075 if ((option != (const char *) NULL) &&
1076 (IsMagickTrue(option) != MagickFalse) &&
1077 (write_info->page == (char *) NULL) &&
cristy3ed852e2009-09-05 21:47:34 +00001078 (GetPreviousImageInList(image) == (Image *) NULL) &&
1079 (GetNextImageInList(image) == (Image *) NULL) &&
1080 (IsTaintImage(image) == MagickFalse))
1081 {
1082 delegate_info=GetDelegateInfo(image->magick,write_info->magick,
1083 &image->exception);
1084 if ((delegate_info != (const DelegateInfo *) NULL) &&
1085 (GetDelegateMode(delegate_info) == 0) &&
1086 (IsPathAccessible(image->magick_filename) != MagickFalse))
1087 {
1088 /*
1089 Process image with bi-modal delegate.
1090 */
1091 (void) CopyMagickString(image->filename,image->magick_filename,
1092 MaxTextExtent);
1093 status=InvokeDelegate(write_info,image,image->magick,
1094 write_info->magick,&image->exception);
1095 write_info=DestroyImageInfo(write_info);
1096 (void) CopyMagickString(image->filename,filename,MaxTextExtent);
1097 return(status);
1098 }
1099 }
1100 status=MagickFalse;
1101 temporary=MagickFalse;
1102 if ((magick_info != (const MagickInfo *) NULL) &&
1103 (GetMagickSeekableStream(magick_info) != MagickFalse))
1104 {
1105 char
1106 filename[MaxTextExtent];
1107
1108 (void) CopyMagickString(filename,image->filename,MaxTextExtent);
1109 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
1110 (void) CopyMagickString(image->filename,filename,MaxTextExtent);
1111 if (status != MagickFalse)
1112 {
cristye92a6fd2010-02-04 01:36:45 +00001113 if (IsBlobSeekable(image) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001114 {
1115 /*
1116 A seekable stream is required by the encoder.
1117 */
cristy54439632010-07-15 00:43:34 +00001118 write_info->adjoin=MagickTrue;
cristy3ed852e2009-09-05 21:47:34 +00001119 (void) CopyMagickString(write_info->filename,image->filename,
1120 MaxTextExtent);
1121 (void) AcquireUniqueFilename(image->filename);
1122 temporary=MagickTrue;
1123 }
1124 (void) CloseBlob(image);
1125 }
1126 }
cristy4e1dff62009-10-25 20:36:03 +00001127 if (constitute_semaphore == (SemaphoreInfo *) NULL)
1128 AcquireSemaphoreInfo(&constitute_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001129 if ((magick_info != (const MagickInfo *) NULL) &&
1130 (GetImageEncoder(magick_info) != (EncodeImageHandler *) NULL))
1131 {
1132 /*
1133 Call appropriate image writer based on image type.
1134 */
1135 thread_support=GetMagickThreadSupport(magick_info);
1136 if ((thread_support & EncoderThreadSupport) == 0)
cristyf84a1932010-01-03 18:00:18 +00001137 LockSemaphoreInfo(constitute_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001138 status=GetImageEncoder(magick_info)(write_info,image);
1139 if ((thread_support & EncoderThreadSupport) == 0)
cristyf84a1932010-01-03 18:00:18 +00001140 UnlockSemaphoreInfo(constitute_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001141 }
1142 else
1143 {
1144 delegate_info=GetDelegateInfo((char *) NULL,write_info->magick,
1145 &image->exception);
1146 if (delegate_info != (DelegateInfo *) NULL)
1147 {
1148 /*
1149 Process the image with delegate.
1150 */
1151 *write_info->filename='\0';
1152 if (GetDelegateThreadSupport(delegate_info) == MagickFalse)
cristyf84a1932010-01-03 18:00:18 +00001153 LockSemaphoreInfo(constitute_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001154 status=InvokeDelegate(write_info,image,(char *) NULL,
1155 write_info->magick,&image->exception);
1156 if (GetDelegateThreadSupport(delegate_info) == MagickFalse)
cristyf84a1932010-01-03 18:00:18 +00001157 UnlockSemaphoreInfo(constitute_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001158 (void) CopyMagickString(image->filename,filename,MaxTextExtent);
1159 }
1160 else
1161 {
1162 sans_exception=AcquireExceptionInfo();
1163 magick_info=GetMagickInfo(write_info->magick,sans_exception);
1164 sans_exception=DestroyExceptionInfo(sans_exception);
1165 if ((write_info->affirm == MagickFalse) &&
1166 (magick_info == (const MagickInfo *) NULL))
1167 {
1168 (void) CopyMagickString(write_info->magick,image->magick,
1169 MaxTextExtent);
1170 magick_info=GetMagickInfo(write_info->magick,&image->exception);
1171 }
1172 if ((magick_info == (const MagickInfo *) NULL) ||
1173 (GetImageEncoder(magick_info) == (EncodeImageHandler *) NULL))
cristy1a8fced2010-03-15 18:05:43 +00001174 {
cristy924c8722010-03-15 17:50:24 +00001175 magick_info=GetMagickInfo(image->magick,&image->exception);
cristy1a8fced2010-03-15 18:05:43 +00001176 (void) CopyMagickString(image->filename,filename,MaxTextExtent);
cristy924c8722010-03-15 17:50:24 +00001177 }
cristy1a8fced2010-03-15 18:05:43 +00001178 if ((magick_info == (const MagickInfo *) NULL) ||
1179 (GetImageEncoder(magick_info) == (EncodeImageHandler *) NULL))
1180 (void) ThrowMagickException(&image->exception,GetMagickModule(),
1181 MissingDelegateError,"NoEncodeDelegateForThisImageFormat","`%s'",
1182 image->filename);
cristy3ed852e2009-09-05 21:47:34 +00001183 else
1184 {
1185 /*
1186 Call appropriate image writer based on image type.
1187 */
1188 thread_support=GetMagickThreadSupport(magick_info);
1189 if ((thread_support & EncoderThreadSupport) == 0)
cristyf84a1932010-01-03 18:00:18 +00001190 LockSemaphoreInfo(constitute_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001191 status=GetImageEncoder(magick_info)(write_info,image);
1192 if ((thread_support & EncoderThreadSupport) == 0)
cristyf84a1932010-01-03 18:00:18 +00001193 UnlockSemaphoreInfo(constitute_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001194 }
1195 }
1196 }
1197 if (GetBlobError(image) != MagickFalse)
1198 ThrowFileException(&image->exception,FileOpenError,
1199 "AnErrorHasOccurredWritingToFile",image->filename);
1200 if (temporary == MagickTrue)
1201 {
1202 /*
1203 Copy temporary image file to permanent.
1204 */
1205 status=OpenBlob(write_info,image,ReadBinaryBlobMode,&image->exception);
1206 if (status != MagickFalse)
1207 status=ImageToFile(image,write_info->filename,&image->exception);
cristya8902942010-07-30 00:49:52 +00001208 (void) CloseBlob(image);
cristy3ed852e2009-09-05 21:47:34 +00001209 (void) RelinquishUniqueFileResource(image->filename);
1210 (void) CopyMagickString(image->filename,write_info->filename,
1211 MaxTextExtent);
cristy3ed852e2009-09-05 21:47:34 +00001212 }
1213 if ((LocaleCompare(write_info->magick,"info") != 0) &&
1214 (write_info->verbose != MagickFalse))
1215 (void) IdentifyImage(image,stdout,MagickFalse);
1216 write_info=DestroyImageInfo(write_info);
1217 return(status);
1218}
1219
1220/*
1221%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1222% %
1223% %
1224% %
1225% W r i t e I m a g e s %
1226% %
1227% %
1228% %
1229%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1230%
1231% WriteImages() writes an image sequence.
1232%
1233% The format of the WriteImages method is:
1234%
1235% MagickBooleanType WriteImages(const ImageInfo *image_info,Image *images,
1236% const char *filename,ExceptionInfo *exception)
1237%
1238% A description of each parameter follows:
1239%
1240% o image_info: the image info.
1241%
1242% o images: the image list.
1243%
1244% o filename: the image filename.
1245%
1246% o exception: return any errors or warnings in this structure.
1247%
1248*/
1249MagickExport MagickBooleanType WriteImages(const ImageInfo *image_info,
1250 Image *images,const char *filename,ExceptionInfo *exception)
1251{
cristy36507e62010-02-11 00:26:48 +00001252#define WriteImageTag "Write/Image"
1253
cristy3ed852e2009-09-05 21:47:34 +00001254 BlobInfo
1255 *blob;
1256
1257 ExceptionInfo
1258 *sans_exception;
1259
1260 ImageInfo
1261 *write_info;
1262
cristy36507e62010-02-11 00:26:48 +00001263 MagickBooleanType
1264 proceed;
1265
1266 MagickOffsetType
1267 i;
1268
1269 MagickProgressMonitor
1270 progress_monitor;
1271
1272 MagickSizeType
1273 number_images;
1274
cristy3ed852e2009-09-05 21:47:34 +00001275 MagickStatusType
1276 status;
1277
1278 register Image
1279 *p;
1280
1281 assert(image_info != (const ImageInfo *) NULL);
1282 assert(image_info->signature == MagickSignature);
1283 assert(images != (Image *) NULL);
1284 assert(images->signature == MagickSignature);
1285 if (images->debug != MagickFalse)
1286 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",images->filename);
1287 assert(exception != (ExceptionInfo *) NULL);
1288 write_info=CloneImageInfo(image_info);
1289 images=GetFirstImageInList(images);
1290 blob=CloneBlobInfo(images->blob); /* thread specific I/O handler */
1291 DestroyBlob(images);
1292 images->blob=blob;
1293 if (filename != (const char *) NULL)
1294 for (p=images; p != (Image *) NULL; p=GetNextImageInList(p))
1295 (void) CopyMagickString(p->filename,filename,MaxTextExtent);
1296 (void) CopyMagickString(write_info->filename,images->filename,MaxTextExtent);
1297 if (*write_info->magick == '\0')
1298 (void) CopyMagickString(write_info->magick,images->magick,MaxTextExtent);
1299 sans_exception=AcquireExceptionInfo();
cristyd965a422010-03-03 17:47:35 +00001300 (void) SetImageInfo(write_info,(unsigned int) GetImageListLength(images),
1301 sans_exception);
cristy3ed852e2009-09-05 21:47:34 +00001302 sans_exception=DestroyExceptionInfo(sans_exception);
1303 p=images;
1304 for ( ; GetNextImageInList(p) != (Image *) NULL; p=GetNextImageInList(p))
1305 if (p->scene >= GetNextImageInList(p)->scene)
1306 {
cristybb503372010-05-27 20:51:26 +00001307 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001308 i;
1309
1310 /*
1311 Generate consistent scene numbers.
1312 */
cristybb503372010-05-27 20:51:26 +00001313 i=(ssize_t) images->scene;
cristy3ed852e2009-09-05 21:47:34 +00001314 for (p=images; p != (Image *) NULL; p=GetNextImageInList(p))
cristybb503372010-05-27 20:51:26 +00001315 p->scene=(size_t) i++;
cristy3ed852e2009-09-05 21:47:34 +00001316 break;
1317 }
1318 /*
1319 Write images.
1320 */
1321 status=MagickTrue;
cristy466802f2010-02-24 14:18:37 +00001322 progress_monitor=(MagickProgressMonitor) NULL;
cristy36507e62010-02-11 00:26:48 +00001323 i=0;
1324 number_images=GetImageListLength(images);
cristy3ed852e2009-09-05 21:47:34 +00001325 for (p=images; p != (Image *) NULL; p=GetNextImageInList(p))
1326 {
cristy8b27a6d2010-02-14 03:31:15 +00001327 if (number_images != 1)
1328 progress_monitor=SetImageProgressMonitor(p,(MagickProgressMonitor) NULL,
1329 p->client_data);
cristy3ed852e2009-09-05 21:47:34 +00001330 status&=WriteImage(write_info,p);
1331 GetImageException(p,exception);
cristy8b27a6d2010-02-14 03:31:15 +00001332 if (number_images != 1)
1333 (void) SetImageProgressMonitor(p,progress_monitor,p->client_data);
cristy3ed852e2009-09-05 21:47:34 +00001334 if (write_info->adjoin != MagickFalse)
1335 break;
cristy8b27a6d2010-02-14 03:31:15 +00001336 if (number_images != 1)
1337 {
1338 proceed=SetImageProgress(p,WriteImageTag,i++,number_images);
1339 if (proceed == MagickFalse)
1340 break;
1341 }
cristy3ed852e2009-09-05 21:47:34 +00001342 }
1343 write_info=DestroyImageInfo(write_info);
1344 return(status != 0 ? MagickTrue : MagickFalse);
1345}