blob: 555be54c76db741af6dc2006f2ef224a5f9ba6ea [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 %
cristyde984cd2013-12-01 14:49:27 +000016% Cristy %
cristy3ed852e2009-09-05 21:47:34 +000017% October 1998 %
18% %
19% %
cristyb56bb242014-11-25 17:12:48 +000020% Copyright 1999-2015 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*/
cristy4c08aed2011-07-01 19:47:50 +000042#include "MagickCore/studio.h"
cristy4c271d82013-05-09 12:31:08 +000043#include "MagickCore/attribute.h"
cristy4c08aed2011-07-01 19:47:50 +000044#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/client.h"
cristyc669ac62013-05-09 12:28:16 +000050#include "MagickCore/colorspace-private.h"
cristy4c08aed2011-07-01 19:47:50 +000051#include "MagickCore/constitute.h"
cristy5ff4eaf2011-09-03 01:38:02 +000052#include "MagickCore/constitute-private.h"
cristy4c08aed2011-07-01 19:47:50 +000053#include "MagickCore/delegate.h"
54#include "MagickCore/geometry.h"
55#include "MagickCore/identify.h"
56#include "MagickCore/image-private.h"
57#include "MagickCore/list.h"
58#include "MagickCore/magick.h"
59#include "MagickCore/memory_.h"
60#include "MagickCore/monitor.h"
61#include "MagickCore/monitor-private.h"
62#include "MagickCore/option.h"
63#include "MagickCore/pixel.h"
64#include "MagickCore/pixel-accessor.h"
65#include "MagickCore/policy.h"
66#include "MagickCore/profile.h"
cristy7832dc22011-09-05 01:21:53 +000067#include "MagickCore/profile-private.h"
cristy4c08aed2011-07-01 19:47:50 +000068#include "MagickCore/property.h"
69#include "MagickCore/quantum.h"
70#include "MagickCore/resize.h"
71#include "MagickCore/resource_.h"
72#include "MagickCore/semaphore.h"
73#include "MagickCore/statistic.h"
74#include "MagickCore/stream.h"
75#include "MagickCore/string_.h"
76#include "MagickCore/string-private.h"
77#include "MagickCore/timer.h"
cristy63a81872012-03-22 15:52:52 +000078#include "MagickCore/token.h"
cristy4c08aed2011-07-01 19:47:50 +000079#include "MagickCore/transform.h"
80#include "MagickCore/utility.h"
cristyd1dd6e42011-09-04 01:46:08 +000081#include "MagickCore/utility-private.h"
cristy3ed852e2009-09-05 21:47:34 +000082
cristyf34a1452009-10-24 22:29:27 +000083/*
84%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
85% %
86% %
87% %
cristy3ed852e2009-09-05 21:47:34 +000088% C o n s t i t u t e I m a g e %
89% %
90% %
91% %
92%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
93%
94% ConstituteImage() returns an image from the pixel data you supply.
95% The pixel data must be in scanline order top-to-bottom. The data can be
96% char, short int, int, float, or double. Float and double require the
97% pixels to be normalized [0..1], otherwise [0..QuantumRange]. For example, to
98% create a 640x480 image from unsigned red-green-blue character data, use:
99%
100% image = ConstituteImage(640,480,"RGB",CharPixel,pixels,&exception);
101%
102% The format of the ConstituteImage method is:
103%
cristy0758f9e2010-05-28 01:44:10 +0000104% Image *ConstituteImage(const size_t columns,const size_t rows,
105% const char *map,const StorageType storage,const void *pixels,
106% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000107%
108% A description of each parameter follows:
109%
110% o columns: width in pixels of the image.
111%
112% o rows: height in pixels of the image.
113%
114% o map: This string reflects the expected ordering of the pixel array.
115% It can be any combination or order of R = red, G = green, B = blue,
116% A = alpha (0 is transparent), O = opacity (0 is opaque), C = cyan,
117% Y = yellow, M = magenta, K = black, I = intensity (for grayscale),
118% P = pad.
119%
120% o storage: Define the data type of the pixels. Float and double types are
121% expected to be normalized [0..1] otherwise [0..QuantumRange]. Choose
122% from these types: CharPixel, DoublePixel, FloatPixel, IntegerPixel,
123% LongPixel, QuantumPixel, or ShortPixel.
124%
125% o pixels: This array of values contain the pixel components as defined by
126% map and type. You must preallocate this array where the expected
127% length varies depending on the values of width, height, map, and type.
128%
129% o exception: return any errors or warnings in this structure.
130%
131*/
cristy7076bbb2012-07-05 15:02:27 +0000132MagickExport Image *ConstituteImage(const size_t columns,const size_t rows,
133 const char *map,const StorageType storage,const void *pixels,
134 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000135{
136 Image
137 *image;
138
139 MagickBooleanType
140 status;
141
142 /*
143 Allocate image structure.
144 */
145 assert(map != (const char *) NULL);
146 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",map);
147 assert(pixels != (void *) NULL);
148 assert(exception != (ExceptionInfo *) NULL);
cristye1c94d92015-06-28 12:16:33 +0000149 assert(exception->signature == MagickCoreSignature);
cristy9950d572011-10-01 18:22:35 +0000150 image=AcquireImage((ImageInfo *) NULL,exception);
cristy3ed852e2009-09-05 21:47:34 +0000151 if (image == (Image *) NULL)
152 return((Image *) NULL);
153 if ((columns == 0) || (rows == 0))
154 ThrowImageException(OptionError,"NonZeroWidthAndHeightRequired");
155 image->columns=columns;
156 image->rows=rows;
cristyea1a8aa2011-10-20 13:24:06 +0000157 (void) SetImageBackgroundColor(image,exception);
cristy018f07f2011-09-04 21:15:19 +0000158 status=ImportImagePixels(image,0,0,columns,rows,map,storage,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +0000159 if (status == MagickFalse)
cristy018f07f2011-09-04 21:15:19 +0000160 image=DestroyImage(image);
cristy3ed852e2009-09-05 21:47:34 +0000161 return(image);
162}
163
164/*
165%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
166% %
167% %
168% %
cristy3ed852e2009-09-05 21:47:34 +0000169% P i n g I m a g e %
170% %
171% %
172% %
173%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
174%
175% PingImage() returns all the properties of an image or image sequence
176% except for the pixels. It is much faster and consumes far less memory
177% than ReadImage(). On failure, a NULL image is returned and exception
178% describes the reason for the failure.
179%
180% The format of the PingImage method is:
181%
182% Image *PingImage(const ImageInfo *image_info,ExceptionInfo *exception)
183%
184% A description of each parameter follows:
185%
186% o image_info: Ping the image defined by the file or filename members of
187% this structure.
188%
189% o exception: return any errors or warnings in this structure.
190%
191*/
192
193#if defined(__cplusplus) || defined(c_plusplus)
194extern "C" {
195#endif
196
197static size_t PingStream(const Image *magick_unused(image),
198 const void *magick_unused(pixels),const size_t columns)
199{
200 return(columns);
201}
202
203#if defined(__cplusplus) || defined(c_plusplus)
204}
205#endif
206
207MagickExport Image *PingImage(const ImageInfo *image_info,
208 ExceptionInfo *exception)
209{
210 Image
211 *image;
212
213 ImageInfo
214 *ping_info;
215
216 assert(image_info != (ImageInfo *) NULL);
cristye1c94d92015-06-28 12:16:33 +0000217 assert(image_info->signature == MagickCoreSignature);
cristy3ed852e2009-09-05 21:47:34 +0000218 if (image_info->debug != MagickFalse)
219 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
220 image_info->filename);
221 assert(exception != (ExceptionInfo *) NULL);
222 ping_info=CloneImageInfo(image_info);
223 ping_info->ping=MagickTrue;
224 image=ReadStream(ping_info,&PingStream,exception);
225 if (image != (Image *) NULL)
226 {
227 ResetTimer(&image->timer);
228 if (ping_info->verbose != MagickFalse)
cristya4037272011-08-28 15:11:39 +0000229 (void) IdentifyImage(image,stdout,MagickFalse,exception);
cristy3ed852e2009-09-05 21:47:34 +0000230 }
231 ping_info=DestroyImageInfo(ping_info);
232 return(image);
233}
234
235/*
236%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
237% %
238% %
239% %
240% P i n g I m a g e s %
241% %
242% %
243% %
244%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
245%
246% PingImages() pings one or more images and returns them as an image list.
247%
248% The format of the PingImage method is:
249%
cristy1b58f252012-03-01 01:41:41 +0000250% Image *PingImages(ImageInfo *image_info,const char *filename,
251% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000252%
253% A description of each parameter follows:
254%
255% o image_info: the image info.
256%
cristy1b58f252012-03-01 01:41:41 +0000257% o filename: the image filename.
258%
cristy3ed852e2009-09-05 21:47:34 +0000259% o exception: return any errors or warnings in this structure.
260%
261*/
cristy1b58f252012-03-01 01:41:41 +0000262MagickExport Image *PingImages(ImageInfo *image_info,const char *filename,
cristy3ed852e2009-09-05 21:47:34 +0000263 ExceptionInfo *exception)
264{
265 char
cristy151b66d2015-04-15 10:50:31 +0000266 ping_filename[MagickPathExtent];
cristy3ed852e2009-09-05 21:47:34 +0000267
268 Image
269 *image,
270 *images;
271
272 ImageInfo
273 *read_info;
274
275 /*
276 Ping image list from a file.
277 */
278 assert(image_info != (ImageInfo *) NULL);
cristye1c94d92015-06-28 12:16:33 +0000279 assert(image_info->signature == MagickCoreSignature);
cristy3ed852e2009-09-05 21:47:34 +0000280 if (image_info->debug != MagickFalse)
281 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
282 image_info->filename);
283 assert(exception != (ExceptionInfo *) NULL);
cristy1b58f252012-03-01 01:41:41 +0000284 (void) SetImageOption(image_info,"filename",filename);
cristy151b66d2015-04-15 10:50:31 +0000285 (void) CopyMagickString(image_info->filename,filename,MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +0000286 (void) InterpretImageFilename(image_info,(Image *) NULL,image_info->filename,
cristy1b58f252012-03-01 01:41:41 +0000287 (int) image_info->scene,ping_filename,exception);
288 if (LocaleCompare(ping_filename,image_info->filename) != 0)
cristy3ed852e2009-09-05 21:47:34 +0000289 {
290 ExceptionInfo
291 *sans;
292
cristybb503372010-05-27 20:51:26 +0000293 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000294 extent,
295 scene;
296
297 /*
298 Images of the form image-%d.png[1-5].
299 */
300 read_info=CloneImageInfo(image_info);
301 sans=AcquireExceptionInfo();
cristyd965a422010-03-03 17:47:35 +0000302 (void) SetImageInfo(read_info,0,sans);
cristy3ed852e2009-09-05 21:47:34 +0000303 sans=DestroyExceptionInfo(sans);
cristy0a33a9c2012-09-05 11:17:34 +0000304 if (read_info->number_scenes == 0)
305 {
306 read_info=DestroyImageInfo(read_info);
307 return(PingImage(image_info,exception));
308 }
cristy151b66d2015-04-15 10:50:31 +0000309 (void) CopyMagickString(ping_filename,read_info->filename,MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +0000310 images=NewImageList();
cristybb503372010-05-27 20:51:26 +0000311 extent=(ssize_t) (read_info->scene+read_info->number_scenes);
312 for (scene=(ssize_t) read_info->scene; scene < (ssize_t) extent; scene++)
cristy3ed852e2009-09-05 21:47:34 +0000313 {
cristy1b58f252012-03-01 01:41:41 +0000314 (void) InterpretImageFilename(image_info,(Image *) NULL,ping_filename,
315 (int) scene,read_info->filename,exception);
cristy3ed852e2009-09-05 21:47:34 +0000316 image=PingImage(read_info,exception);
317 if (image == (Image *) NULL)
318 continue;
319 AppendImageToList(&images,image);
320 }
321 read_info=DestroyImageInfo(read_info);
322 return(images);
323 }
324 return(PingImage(image_info,exception));
325}
326
327/*
328%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
329% %
330% %
331% %
332% R e a d I m a g e %
333% %
334% %
335% %
336%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
337%
338% ReadImage() reads an image or image sequence from a file or file handle.
339% The method returns a NULL if there is a memory shortage or if the image
340% cannot be read. On failure, a NULL image is returned and exception
341% describes the reason for the failure.
342%
343% The format of the ReadImage method is:
344%
345% Image *ReadImage(const ImageInfo *image_info,ExceptionInfo *exception)
346%
347% A description of each parameter follows:
348%
349% o image_info: Read the image defined by the file or filename members of
350% this structure.
351%
352% o exception: return any errors or warnings in this structure.
353%
354*/
355MagickExport Image *ReadImage(const ImageInfo *image_info,
356 ExceptionInfo *exception)
357{
358 char
cristy151b66d2015-04-15 10:50:31 +0000359 filename[MagickPathExtent],
360 magick[MagickPathExtent],
361 magick_filename[MagickPathExtent];
cristy3ed852e2009-09-05 21:47:34 +0000362
363 const char
364 *value;
365
366 const DelegateInfo
367 *delegate_info;
368
369 const MagickInfo
370 *magick_info;
371
372 ExceptionInfo
373 *sans_exception;
374
375 GeometryInfo
376 geometry_info;
377
378 Image
379 *image,
380 *next;
381
382 ImageInfo
383 *read_info;
384
385 MagickStatusType
dirk24ae22e2015-02-22 00:17:42 +0000386 flags;
cristy3ed852e2009-09-05 21:47:34 +0000387
388 PolicyDomain
389 domain;
390
391 PolicyRights
392 rights;
393
394 /*
395 Determine image type from filename prefix or suffix (e.g. image.jpg).
396 */
397 assert(image_info != (ImageInfo *) NULL);
cristye1c94d92015-06-28 12:16:33 +0000398 assert(image_info->signature == MagickCoreSignature);
cristy3ed852e2009-09-05 21:47:34 +0000399 assert(image_info->filename != (char *) NULL);
400 if (image_info->debug != MagickFalse)
401 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
402 image_info->filename);
403 assert(exception != (ExceptionInfo *) NULL);
404 read_info=CloneImageInfo(image_info);
cristy151b66d2015-04-15 10:50:31 +0000405 (void) CopyMagickString(magick_filename,read_info->filename,MagickPathExtent);
cristyd965a422010-03-03 17:47:35 +0000406 (void) SetImageInfo(read_info,0,exception);
cristy151b66d2015-04-15 10:50:31 +0000407 (void) CopyMagickString(filename,read_info->filename,MagickPathExtent);
408 (void) CopyMagickString(magick,read_info->magick,MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +0000409 domain=CoderPolicyDomain;
410 rights=ReadPolicyRights;
411 if (IsRightsAuthorized(domain,rights,read_info->magick) == MagickFalse)
412 {
cristya9197f62010-01-12 02:23:34 +0000413 errno=EPERM;
cristy3ed852e2009-09-05 21:47:34 +0000414 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
cristyefe601c2013-01-05 17:51:12 +0000415 "NotAuthorized","`%s'",read_info->filename);
cristy67ce5822012-09-02 23:52:13 +0000416 read_info=DestroyImageInfo(read_info);
cristy3ed852e2009-09-05 21:47:34 +0000417 return((Image *) NULL);
418 }
419 /*
420 Call appropriate image reader based on image type.
421 */
422 sans_exception=AcquireExceptionInfo();
423 magick_info=GetMagickInfo(read_info->magick,sans_exception);
424 sans_exception=DestroyExceptionInfo(sans_exception);
425 if (magick_info != (const MagickInfo *) NULL)
426 {
427 if (GetMagickEndianSupport(magick_info) == MagickFalse)
428 read_info->endian=UndefinedEndian;
429 else
430 if ((image_info->endian == UndefinedEndian) &&
431 (GetMagickRawSupport(magick_info) != MagickFalse))
432 {
cristybb503372010-05-27 20:51:26 +0000433 size_t
cristy3ed852e2009-09-05 21:47:34 +0000434 lsb_first;
435
436 lsb_first=1;
437 read_info->endian=(*(char *) &lsb_first) == 1 ? LSBEndian :
438 MSBEndian;
439 }
440 }
441 if ((magick_info != (const MagickInfo *) NULL) &&
442 (GetMagickSeekableStream(magick_info) != MagickFalse))
443 {
444 MagickBooleanType
445 status;
446
cristy9950d572011-10-01 18:22:35 +0000447 image=AcquireImage(read_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000448 (void) CopyMagickString(image->filename,read_info->filename,
cristy151b66d2015-04-15 10:50:31 +0000449 MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +0000450 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
451 if (status == MagickFalse)
452 {
453 read_info=DestroyImageInfo(read_info);
454 image=DestroyImage(image);
455 return((Image *) NULL);
456 }
457 if (IsBlobSeekable(image) == MagickFalse)
458 {
459 /*
460 Coder requires a seekable stream.
461 */
462 *read_info->filename='\0';
463 status=ImageToFile(image,read_info->filename,exception);
464 if (status == MagickFalse)
465 {
466 (void) CloseBlob(image);
467 read_info=DestroyImageInfo(read_info);
468 image=DestroyImage(image);
469 return((Image *) NULL);
470 }
471 read_info->temporary=MagickTrue;
472 }
473 (void) CloseBlob(image);
474 image=DestroyImage(image);
475 }
476 image=NewImageList();
cristy95fc4ef2012-04-21 18:58:21 +0000477 if ((magick_info == (const MagickInfo *) NULL) ||
478 (GetImageDecoder(magick_info) == (DecodeImageHandler *) NULL))
479 {
480 delegate_info=GetDelegateInfo(read_info->magick,(char *) NULL,exception);
481 if (delegate_info == (const DelegateInfo *) NULL)
482 {
483 (void) SetImageInfo(read_info,0,exception);
cristy151b66d2015-04-15 10:50:31 +0000484 (void) CopyMagickString(read_info->filename,filename,MagickPathExtent);
cristy95fc4ef2012-04-21 18:58:21 +0000485 magick_info=GetMagickInfo(read_info->magick,exception);
486 }
487 }
cristy3ed852e2009-09-05 21:47:34 +0000488 if ((magick_info != (const MagickInfo *) NULL) &&
489 (GetImageDecoder(magick_info) != (DecodeImageHandler *) NULL))
490 {
dirk24ae22e2015-02-22 00:17:42 +0000491 if (GetMagickDecoderThreadSupport(magick_info) == MagickFalse)
cristy191ba5c2014-03-16 21:26:40 +0000492 LockSemaphoreInfo(magick_info->semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000493 image=GetImageDecoder(magick_info)(read_info,exception);
dirk24ae22e2015-02-22 00:17:42 +0000494 if (GetMagickDecoderThreadSupport(magick_info) == MagickFalse)
cristy191ba5c2014-03-16 21:26:40 +0000495 UnlockSemaphoreInfo(magick_info->semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000496 }
497 else
498 {
499 delegate_info=GetDelegateInfo(read_info->magick,(char *) NULL,exception);
500 if (delegate_info == (const DelegateInfo *) NULL)
501 {
cristy3a7b2b82010-04-28 22:14:19 +0000502 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +0000503 MissingDelegateError,"NoDecodeDelegateForThisImageFormat","`%s'",
dirkea8856e2014-04-09 16:44:40 +0000504 read_info->magick);
cristy3ed852e2009-09-05 21:47:34 +0000505 if (read_info->temporary != MagickFalse)
506 (void) RelinquishUniqueFileResource(read_info->filename);
507 read_info=DestroyImageInfo(read_info);
508 return((Image *) NULL);
509 }
510 /*
511 Let our decoding delegate process the image.
512 */
cristy9950d572011-10-01 18:22:35 +0000513 image=AcquireImage(read_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000514 if (image == (Image *) NULL)
515 {
516 read_info=DestroyImageInfo(read_info);
517 return((Image *) NULL);
518 }
519 (void) CopyMagickString(image->filename,read_info->filename,
cristy151b66d2015-04-15 10:50:31 +0000520 MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +0000521 *read_info->filename='\0';
522 if (GetDelegateThreadSupport(delegate_info) == MagickFalse)
cristy191ba5c2014-03-16 21:26:40 +0000523 LockSemaphoreInfo(delegate_info->semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000524 (void) InvokeDelegate(read_info,image,read_info->magick,(char *) NULL,
525 exception);
526 if (GetDelegateThreadSupport(delegate_info) == MagickFalse)
cristy191ba5c2014-03-16 21:26:40 +0000527 UnlockSemaphoreInfo(delegate_info->semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000528 image=DestroyImageList(image);
529 read_info->temporary=MagickTrue;
cristyd965a422010-03-03 17:47:35 +0000530 (void) SetImageInfo(read_info,0,exception);
cristy3ed852e2009-09-05 21:47:34 +0000531 magick_info=GetMagickInfo(read_info->magick,exception);
532 if ((magick_info == (const MagickInfo *) NULL) ||
533 (GetImageDecoder(magick_info) == (DecodeImageHandler *) NULL))
534 {
535 if (IsPathAccessible(read_info->filename) != MagickFalse)
536 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +0000537 MissingDelegateError,"NoDecodeDelegateForThisImageFormat","`%s'",
dirkea8856e2014-04-09 16:44:40 +0000538 read_info->magick);
cristy3ed852e2009-09-05 21:47:34 +0000539 else
540 ThrowFileException(exception,FileOpenError,"UnableToOpenFile",
541 read_info->filename);
542 read_info=DestroyImageInfo(read_info);
543 return((Image *) NULL);
544 }
dirk24ae22e2015-02-22 00:17:42 +0000545 if (GetMagickDecoderThreadSupport(magick_info) == MagickFalse)
cristy191ba5c2014-03-16 21:26:40 +0000546 LockSemaphoreInfo(magick_info->semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000547 image=(Image *) (GetImageDecoder(magick_info))(read_info,exception);
dirk24ae22e2015-02-22 00:17:42 +0000548 if (GetMagickDecoderThreadSupport(magick_info) == MagickFalse)
cristy191ba5c2014-03-16 21:26:40 +0000549 UnlockSemaphoreInfo(magick_info->semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000550 }
551 if (read_info->temporary != MagickFalse)
552 {
553 (void) RelinquishUniqueFileResource(read_info->filename);
554 read_info->temporary=MagickFalse;
555 if (image != (Image *) NULL)
cristy151b66d2015-04-15 10:50:31 +0000556 (void) CopyMagickString(image->filename,filename,MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +0000557 }
558 if (image == (Image *) NULL)
559 {
560 read_info=DestroyImageInfo(read_info);
561 return(image);
562 }
563 if (exception->severity >= ErrorException)
564 (void) LogMagickEvent(ExceptionEvent,GetMagickModule(),
565 "Coder (%s) generated an image despite an error (%d), "
566 "notify the developers",image->magick,exception->severity);
567 if (IsBlobTemporary(image) != MagickFalse)
568 (void) RelinquishUniqueFileResource(read_info->filename);
569 if ((GetNextImageInList(image) != (Image *) NULL) &&
570 (IsSceneGeometry(read_info->scenes,MagickFalse) != MagickFalse))
571 {
572 Image
573 *clones;
574
575 clones=CloneImages(image,read_info->scenes,exception);
576 if (clones == (Image *) NULL)
577 (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
cristyefe601c2013-01-05 17:51:12 +0000578 "SubimageSpecificationReturnsNoImages","`%s'",read_info->filename);
cristy3ed852e2009-09-05 21:47:34 +0000579 else
580 {
581 image=DestroyImageList(image);
582 image=GetFirstImageInList(clones);
583 }
584 }
cristy3ed852e2009-09-05 21:47:34 +0000585 for (next=image; next != (Image *) NULL; next=GetNextImageInList(next))
586 {
587 char
cristy151b66d2015-04-15 10:50:31 +0000588 magick_path[MagickPathExtent],
cristy7734b602010-09-13 18:32:30 +0000589 *property,
cristy151b66d2015-04-15 10:50:31 +0000590 timestamp[MagickPathExtent];
cristy3ed852e2009-09-05 21:47:34 +0000591
cristy05e38ec2010-08-20 15:20:12 +0000592 const char
593 *option;
594
cristy3ed852e2009-09-05 21:47:34 +0000595 const StringInfo
596 *profile;
597
598 next->taint=MagickFalse;
cristyad126fb2011-03-12 14:59:23 +0000599 GetPathComponent(magick_filename,MagickPath,magick_path);
dirk5816af42014-09-14 08:53:21 +0000600 if (*magick_path == '\0' && *next->magick == '\0')
cristy151b66d2015-04-15 10:50:31 +0000601 (void) CopyMagickString(next->magick,magick,MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +0000602 (void) CopyMagickString(next->magick_filename,magick_filename,
cristy151b66d2015-04-15 10:50:31 +0000603 MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +0000604 if (IsBlobTemporary(image) != MagickFalse)
cristy151b66d2015-04-15 10:50:31 +0000605 (void) CopyMagickString(next->filename,filename,MagickPathExtent);
cristyad126fb2011-03-12 14:59:23 +0000606 if (next->magick_columns == 0)
607 next->magick_columns=next->columns;
608 if (next->magick_rows == 0)
609 next->magick_rows=next->rows;
cristyd15e6592011-10-15 00:13:06 +0000610 value=GetImageProperty(next,"tiff:Orientation",exception);
cristy3ed852e2009-09-05 21:47:34 +0000611 if (value == (char *) NULL)
cristyd15e6592011-10-15 00:13:06 +0000612 value=GetImageProperty(next,"exif:Orientation",exception);
cristy3ed852e2009-09-05 21:47:34 +0000613 if (value != (char *) NULL)
614 {
cristyf2f27272009-12-17 14:48:46 +0000615 next->orientation=(OrientationType) StringToLong(value);
cristy3ed852e2009-09-05 21:47:34 +0000616 (void) DeleteImageProperty(next,"tiff:Orientation");
617 (void) DeleteImageProperty(next,"exif:Orientation");
618 }
cristyd15e6592011-10-15 00:13:06 +0000619 value=GetImageProperty(next,"exif:XResolution",exception);
cristy3ed852e2009-09-05 21:47:34 +0000620 if (value != (char *) NULL)
621 {
cristy2a11bef2011-10-28 18:33:11 +0000622 geometry_info.rho=next->resolution.x;
cristy3ed852e2009-09-05 21:47:34 +0000623 geometry_info.sigma=1.0;
624 flags=ParseGeometry(value,&geometry_info);
625 if (geometry_info.sigma != 0)
cristy2a11bef2011-10-28 18:33:11 +0000626 next->resolution.x=geometry_info.rho/geometry_info.sigma;
cristy3ed852e2009-09-05 21:47:34 +0000627 (void) DeleteImageProperty(next,"exif:XResolution");
cristy3ed852e2009-09-05 21:47:34 +0000628 }
cristyd15e6592011-10-15 00:13:06 +0000629 value=GetImageProperty(next,"exif:YResolution",exception);
cristy3ed852e2009-09-05 21:47:34 +0000630 if (value != (char *) NULL)
631 {
cristy2a11bef2011-10-28 18:33:11 +0000632 geometry_info.rho=next->resolution.y;
cristy3ed852e2009-09-05 21:47:34 +0000633 geometry_info.sigma=1.0;
634 flags=ParseGeometry(value,&geometry_info);
635 if (geometry_info.sigma != 0)
cristy2a11bef2011-10-28 18:33:11 +0000636 next->resolution.y=geometry_info.rho/geometry_info.sigma;
cristy3ed852e2009-09-05 21:47:34 +0000637 (void) DeleteImageProperty(next,"exif:YResolution");
cristy3ed852e2009-09-05 21:47:34 +0000638 }
cristyd15e6592011-10-15 00:13:06 +0000639 value=GetImageProperty(next,"tiff:ResolutionUnit",exception);
cristy3ed852e2009-09-05 21:47:34 +0000640 if (value == (char *) NULL)
cristyd15e6592011-10-15 00:13:06 +0000641 value=GetImageProperty(next,"exif:ResolutionUnit",exception);
cristy3ed852e2009-09-05 21:47:34 +0000642 if (value != (char *) NULL)
643 {
cristyf2f27272009-12-17 14:48:46 +0000644 next->units=(ResolutionType) (StringToLong(value)-1);
cristy3ed852e2009-09-05 21:47:34 +0000645 (void) DeleteImageProperty(next,"exif:ResolutionUnit");
646 (void) DeleteImageProperty(next,"tiff:ResolutionUnit");
647 }
648 if (next->page.width == 0)
649 next->page.width=next->columns;
650 if (next->page.height == 0)
651 next->page.height=next->rows;
cristy7734b602010-09-13 18:32:30 +0000652 option=GetImageOption(read_info,"caption");
653 if (option != (const char *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000654 {
cristy018f07f2011-09-04 21:15:19 +0000655 property=InterpretImageProperties(read_info,next,option,exception);
cristyd15e6592011-10-15 00:13:06 +0000656 (void) SetImageProperty(next,"caption",property,exception);
cristy7734b602010-09-13 18:32:30 +0000657 property=DestroyString(property);
658 }
659 option=GetImageOption(read_info,"comment");
660 if (option != (const char *) NULL)
661 {
cristy018f07f2011-09-04 21:15:19 +0000662 property=InterpretImageProperties(read_info,next,option,exception);
cristyd15e6592011-10-15 00:13:06 +0000663 (void) SetImageProperty(next,"comment",property,exception);
cristy7734b602010-09-13 18:32:30 +0000664 property=DestroyString(property);
665 }
666 option=GetImageOption(read_info,"label");
667 if (option != (const char *) NULL)
668 {
cristy018f07f2011-09-04 21:15:19 +0000669 property=InterpretImageProperties(read_info,next,option,exception);
cristyd15e6592011-10-15 00:13:06 +0000670 (void) SetImageProperty(next,"label",property,exception);
cristy7734b602010-09-13 18:32:30 +0000671 property=DestroyString(property);
cristy3ed852e2009-09-05 21:47:34 +0000672 }
673 if (LocaleCompare(next->magick,"TEXT") == 0)
674 (void) ParseAbsoluteGeometry("0x0+0+0",&next->page);
675 if ((read_info->extract != (char *) NULL) &&
676 (read_info->stream == (StreamHandler) NULL))
677 {
678 RectangleInfo
679 geometry;
680
681 flags=ParseAbsoluteGeometry(read_info->extract,&geometry);
682 if ((next->columns != geometry.width) ||
683 (next->rows != geometry.height))
684 {
685 if (((flags & XValue) != 0) || ((flags & YValue) != 0))
686 {
687 Image
688 *crop_image;
689
690 crop_image=CropImage(next,&geometry,exception);
691 if (crop_image != (Image *) NULL)
692 ReplaceImageInList(&next,crop_image);
693 }
694 else
695 if (((flags & WidthValue) != 0) || ((flags & HeightValue) != 0))
696 {
697 Image
698 *size_image;
699
700 flags=ParseRegionGeometry(next,read_info->extract,&geometry,
701 exception);
702 size_image=ResizeImage(next,geometry.width,geometry.height,
cristyaa2c16c2012-03-25 22:21:35 +0000703 next->filter,exception);
cristy3ed852e2009-09-05 21:47:34 +0000704 if (size_image != (Image *) NULL)
705 ReplaceImageInList(&next,size_image);
706 }
707 }
708 }
709 profile=GetImageProfile(next,"icc");
710 if (profile == (const StringInfo *) NULL)
711 profile=GetImageProfile(next,"icm");
cristy3ed852e2009-09-05 21:47:34 +0000712 profile=GetImageProfile(next,"iptc");
713 if (profile == (const StringInfo *) NULL)
714 profile=GetImageProfile(next,"8bim");
cristy151b66d2015-04-15 10:50:31 +0000715 (void) FormatMagickTime(GetBlobProperties(next)->st_mtime,MagickPathExtent,
cristy3ed852e2009-09-05 21:47:34 +0000716 timestamp);
cristyd15e6592011-10-15 00:13:06 +0000717 (void) SetImageProperty(next,"date:modify",timestamp,exception);
cristy151b66d2015-04-15 10:50:31 +0000718 (void) FormatMagickTime(GetBlobProperties(next)->st_ctime,MagickPathExtent,
cristy3ed852e2009-09-05 21:47:34 +0000719 timestamp);
cristyd15e6592011-10-15 00:13:06 +0000720 (void) SetImageProperty(next,"date:create",timestamp,exception);
cristy74f82a62010-08-19 22:24:53 +0000721 option=GetImageOption(image_info,"delay");
722 if (option != (const char *) NULL)
723 {
724 GeometryInfo
725 geometry_info;
726
727 flags=ParseGeometry(option,&geometry_info);
728 if ((flags & GreaterValue) != 0)
729 {
cristy7734b602010-09-13 18:32:30 +0000730 if (next->delay > (size_t) floor(geometry_info.rho+0.5))
731 next->delay=(size_t) floor(geometry_info.rho+0.5);
cristy74f82a62010-08-19 22:24:53 +0000732 }
733 else
734 if ((flags & LessValue) != 0)
735 {
cristy7734b602010-09-13 18:32:30 +0000736 if (next->delay < (size_t) floor(geometry_info.rho+0.5))
737 next->ticks_per_second=(ssize_t) floor(geometry_info.sigma+0.5);
cristy74f82a62010-08-19 22:24:53 +0000738 }
739 else
cristy7734b602010-09-13 18:32:30 +0000740 next->delay=(size_t) floor(geometry_info.rho+0.5);
cristy74f82a62010-08-19 22:24:53 +0000741 if ((flags & SigmaValue) != 0)
cristy7734b602010-09-13 18:32:30 +0000742 next->ticks_per_second=(ssize_t) floor(geometry_info.sigma+0.5);
cristy74f82a62010-08-19 22:24:53 +0000743 }
744 option=GetImageOption(image_info,"dispose");
745 if (option != (const char *) NULL)
cristy042ee782011-04-22 18:48:30 +0000746 next->dispose=(DisposeType) ParseCommandOption(MagickDisposeOptions,
cristy74f82a62010-08-19 22:24:53 +0000747 MagickFalse,option);
cristy3ed852e2009-09-05 21:47:34 +0000748 if (read_info->verbose != MagickFalse)
cristya4037272011-08-28 15:11:39 +0000749 (void) IdentifyImage(next,stderr,MagickFalse,exception);
cristy3ed852e2009-09-05 21:47:34 +0000750 image=next;
751 }
752 read_info=DestroyImageInfo(read_info);
753 return(GetFirstImageInList(image));
754}
755
756/*
757%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
758% %
759% %
760% %
761% R e a d I m a g e s %
762% %
763% %
764% %
765%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
766%
767% ReadImages() reads one or more images and returns them as an image list.
768%
769% The format of the ReadImage method is:
770%
cristy1b58f252012-03-01 01:41:41 +0000771% Image *ReadImages(ImageInfo *image_info,const char *filename,
772% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000773%
774% A description of each parameter follows:
775%
776% o image_info: the image info.
777%
cristy1b58f252012-03-01 01:41:41 +0000778% o filename: the image filename.
779%
cristy3ed852e2009-09-05 21:47:34 +0000780% o exception: return any errors or warnings in this structure.
781%
782*/
cristy1b58f252012-03-01 01:41:41 +0000783MagickExport Image *ReadImages(ImageInfo *image_info,const char *filename,
cristy3ed852e2009-09-05 21:47:34 +0000784 ExceptionInfo *exception)
785{
786 char
cristy151b66d2015-04-15 10:50:31 +0000787 read_filename[MagickPathExtent];
cristy3ed852e2009-09-05 21:47:34 +0000788
789 Image
790 *image,
791 *images;
792
793 ImageInfo
794 *read_info;
795
796 /*
797 Read image list from a file.
798 */
799 assert(image_info != (ImageInfo *) NULL);
cristye1c94d92015-06-28 12:16:33 +0000800 assert(image_info->signature == MagickCoreSignature);
cristy3ed852e2009-09-05 21:47:34 +0000801 if (image_info->debug != MagickFalse)
802 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
803 image_info->filename);
804 assert(exception != (ExceptionInfo *) NULL);
cristy5ea47602015-01-09 18:24:17 +0000805 read_info=CloneImageInfo(image_info);
806 *read_info->magick='\0';
807 (void) SetImageOption(read_info,"filename",filename);
cristy151b66d2015-04-15 10:50:31 +0000808 (void) CopyMagickString(read_info->filename,filename,MagickPathExtent);
cristy5ea47602015-01-09 18:24:17 +0000809 (void) InterpretImageFilename(read_info,(Image *) NULL,filename,
810 (int) read_info->scene,read_filename,exception);
811 if (LocaleCompare(read_filename,read_info->filename) != 0)
cristy3ed852e2009-09-05 21:47:34 +0000812 {
813 ExceptionInfo
814 *sans;
815
cristybb503372010-05-27 20:51:26 +0000816 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000817 extent,
818 scene;
819
820 /*
821 Images of the form image-%d.png[1-5].
822 */
cristy3ed852e2009-09-05 21:47:34 +0000823 sans=AcquireExceptionInfo();
cristyd965a422010-03-03 17:47:35 +0000824 (void) SetImageInfo(read_info,0,sans);
cristy3ed852e2009-09-05 21:47:34 +0000825 sans=DestroyExceptionInfo(sans);
cristyf95ba5f2011-07-02 00:35:48 +0000826 if (read_info->number_scenes == 0)
827 {
828 read_info=DestroyImageInfo(read_info);
829 return(ReadImage(image_info,exception));
830 }
cristy151b66d2015-04-15 10:50:31 +0000831 (void) CopyMagickString(read_filename,read_info->filename,MagickPathExtent);
cristyeb883912011-07-02 00:38:10 +0000832 images=NewImageList();
cristybb503372010-05-27 20:51:26 +0000833 extent=(ssize_t) (read_info->scene+read_info->number_scenes);
834 for (scene=(ssize_t) read_info->scene; scene < (ssize_t) extent; scene++)
cristy3ed852e2009-09-05 21:47:34 +0000835 {
cristy1b58f252012-03-01 01:41:41 +0000836 (void) InterpretImageFilename(image_info,(Image *) NULL,read_filename,
837 (int) scene,read_info->filename,exception);
cristy3ed852e2009-09-05 21:47:34 +0000838 image=ReadImage(read_info,exception);
839 if (image == (Image *) NULL)
840 continue;
841 AppendImageToList(&images,image);
842 }
843 read_info=DestroyImageInfo(read_info);
844 return(images);
845 }
cristy5ea47602015-01-09 18:24:17 +0000846 image=ReadImage(read_info,exception);
847 read_info=DestroyImageInfo(read_info);
848 return(image);
cristy3ed852e2009-09-05 21:47:34 +0000849}
850
851/*
852%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
853% %
854% %
855% %
856+ R e a d I n l i n e I m a g e %
857% %
858% %
859% %
860%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
861%
862% ReadInlineImage() reads a Base64-encoded inline image or image sequence.
863% The method returns a NULL if there is a memory shortage or if the image
864% cannot be read. On failure, a NULL image is returned and exception
865% describes the reason for the failure.
866%
867% The format of the ReadInlineImage method is:
868%
869% Image *ReadInlineImage(const ImageInfo *image_info,const char *content,
870% ExceptionInfo *exception)
871%
872% A description of each parameter follows:
873%
874% o image_info: the image info.
875%
876% o content: the image encoded in Base64.
877%
878% o exception: return any errors or warnings in this structure.
879%
880*/
881MagickExport Image *ReadInlineImage(const ImageInfo *image_info,
882 const char *content,ExceptionInfo *exception)
883{
884 Image
885 *image;
886
887 ImageInfo
888 *read_info;
889
890 unsigned char
891 *blob;
892
893 size_t
894 length;
895
896 register const char
897 *p;
898
cristyd5562a92010-06-05 13:43:49 +0000899 /*
900 Skip over header (e.g. data:image/gif;base64,).
901 */
cristy3ed852e2009-09-05 21:47:34 +0000902 image=NewImageList();
903 for (p=content; (*p != ',') && (*p != '\0'); p++) ;
904 if (*p == '\0')
905 ThrowReaderException(CorruptImageError,"CorruptImage");
906 p++;
907 length=0;
908 blob=Base64Decode(p,&length);
909 if (length == 0)
910 ThrowReaderException(CorruptImageError,"CorruptImage");
911 read_info=CloneImageInfo(image_info);
912 (void) SetImageInfoProgressMonitor(read_info,(MagickProgressMonitor) NULL,
913 (void *) NULL);
cristycc1fc8a2010-06-05 13:35:05 +0000914 *read_info->filename='\0';
915 *read_info->magick='\0';
cristy3ed852e2009-09-05 21:47:34 +0000916 image=BlobToImage(read_info,blob,length,exception);
917 blob=(unsigned char *) RelinquishMagickMemory(blob);
918 read_info=DestroyImageInfo(read_info);
919 return(image);
920}
921
922/*
923%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
924% %
925% %
926% %
927% W r i t e I m a g e %
928% %
929% %
930% %
931%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
932%
cristy96581e82011-02-01 01:10:11 +0000933% WriteImage() writes an image or an image sequence to a file or file handle.
934% If writing to a file is on disk, the name is defined by the filename member
935% of the image structure. WriteImage() returns MagickFalse is there is a
936% memory shortage or if the image cannot be written. Check the exception
937% member of image to determine the cause for any failure.
cristy3ed852e2009-09-05 21:47:34 +0000938%
939% The format of the WriteImage method is:
940%
cristy6f9e0d32011-08-28 16:32:09 +0000941% MagickBooleanType WriteImage(const ImageInfo *image_info,Image *image,
942% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000943%
944% A description of each parameter follows:
945%
946% o image_info: the image info.
947%
948% o image: the image.
949%
cristy6f9e0d32011-08-28 16:32:09 +0000950% o exception: return any errors or warnings in this structure.
951%
cristy3ed852e2009-09-05 21:47:34 +0000952*/
953MagickExport MagickBooleanType WriteImage(const ImageInfo *image_info,
cristy6f9e0d32011-08-28 16:32:09 +0000954 Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000955{
956 char
cristy151b66d2015-04-15 10:50:31 +0000957 filename[MagickPathExtent];
cristy3ed852e2009-09-05 21:47:34 +0000958
cristy4c491342009-11-02 01:46:10 +0000959 const char
960 *option;
961
cristy3ed852e2009-09-05 21:47:34 +0000962 const DelegateInfo
963 *delegate_info;
964
965 const MagickInfo
966 *magick_info;
967
968 ExceptionInfo
969 *sans_exception;
970
971 ImageInfo
972 *write_info;
973
974 MagickBooleanType
975 status,
976 temporary;
977
cristy3ed852e2009-09-05 21:47:34 +0000978 PolicyDomain
979 domain;
980
981 PolicyRights
982 rights;
983
984 /*
985 Determine image type from filename prefix or suffix (e.g. image.jpg).
986 */
987 assert(image_info != (ImageInfo *) NULL);
cristye1c94d92015-06-28 12:16:33 +0000988 assert(image_info->signature == MagickCoreSignature);
cristy3ed852e2009-09-05 21:47:34 +0000989 if (image->debug != MagickFalse)
990 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
991 image_info->filename);
992 assert(image != (Image *) NULL);
cristye1c94d92015-06-28 12:16:33 +0000993 assert(image->signature == MagickCoreSignature);
cristy6f9e0d32011-08-28 16:32:09 +0000994 assert(exception != (ExceptionInfo *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000995 sans_exception=AcquireExceptionInfo();
996 write_info=CloneImageInfo(image_info);
cristy151b66d2015-04-15 10:50:31 +0000997 (void) CopyMagickString(write_info->filename,image->filename,MagickPathExtent);
dirk39930a02014-12-27 11:28:17 +0000998 (void) SetImageInfo(write_info,1,sans_exception);
cristy3ed852e2009-09-05 21:47:34 +0000999 if (*write_info->magick == '\0')
cristy151b66d2015-04-15 10:50:31 +00001000 (void) CopyMagickString(write_info->magick,image->magick,MagickPathExtent);
1001 (void) CopyMagickString(filename,image->filename,MagickPathExtent);
1002 (void) CopyMagickString(image->filename,write_info->filename,MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +00001003 domain=CoderPolicyDomain;
1004 rights=WritePolicyRights;
1005 if (IsRightsAuthorized(domain,rights,write_info->magick) == MagickFalse)
1006 {
1007 sans_exception=DestroyExceptionInfo(sans_exception);
cristyc12aee62012-09-02 23:54:31 +00001008 write_info=DestroyImageInfo(write_info);
cristya9197f62010-01-12 02:23:34 +00001009 errno=EPERM;
cristy3ed852e2009-09-05 21:47:34 +00001010 ThrowBinaryException(PolicyError,"NotAuthorized",filename);
1011 }
cristya96b2162012-07-20 13:27:40 +00001012 /*
1013 Call appropriate image reader based on image type.
1014 */
cristy3ed852e2009-09-05 21:47:34 +00001015 magick_info=GetMagickInfo(write_info->magick,sans_exception);
1016 sans_exception=DestroyExceptionInfo(sans_exception);
1017 if (magick_info != (const MagickInfo *) NULL)
1018 {
1019 if (GetMagickEndianSupport(magick_info) == MagickFalse)
1020 image->endian=UndefinedEndian;
1021 else
1022 if ((image_info->endian == UndefinedEndian) &&
1023 (GetMagickRawSupport(magick_info) != MagickFalse))
1024 {
cristybb503372010-05-27 20:51:26 +00001025 size_t
cristy3ed852e2009-09-05 21:47:34 +00001026 lsb_first;
1027
1028 lsb_first=1;
1029 image->endian=(*(char *) &lsb_first) == 1 ? LSBEndian : MSBEndian;
1030 }
1031 }
1032 (void) SyncImageProfiles(image);
cristyb83897f2014-09-20 13:25:25 +00001033 DisassociateImageStream(image);
cristy4c491342009-11-02 01:46:10 +00001034 option=GetImageOption(image_info,"delegate:bimodal");
anthony7bcfe7f2012-03-30 14:01:22 +00001035 if ((IfMagickTrue(IsStringTrue(option))) &&
cristy4c491342009-11-02 01:46:10 +00001036 (write_info->page == (char *) NULL) &&
cristy3ed852e2009-09-05 21:47:34 +00001037 (GetPreviousImageInList(image) == (Image *) NULL) &&
1038 (GetNextImageInList(image) == (Image *) NULL) &&
anthony7bcfe7f2012-03-30 14:01:22 +00001039 (IfMagickFalse(IsTaintImage(image))) )
cristy3ed852e2009-09-05 21:47:34 +00001040 {
cristy6f9e0d32011-08-28 16:32:09 +00001041 delegate_info=GetDelegateInfo(image->magick,write_info->magick,exception);
cristy3ed852e2009-09-05 21:47:34 +00001042 if ((delegate_info != (const DelegateInfo *) NULL) &&
1043 (GetDelegateMode(delegate_info) == 0) &&
1044 (IsPathAccessible(image->magick_filename) != MagickFalse))
1045 {
1046 /*
1047 Process image with bi-modal delegate.
1048 */
1049 (void) CopyMagickString(image->filename,image->magick_filename,
cristy151b66d2015-04-15 10:50:31 +00001050 MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +00001051 status=InvokeDelegate(write_info,image,image->magick,
cristy6f9e0d32011-08-28 16:32:09 +00001052 write_info->magick,exception);
cristy3ed852e2009-09-05 21:47:34 +00001053 write_info=DestroyImageInfo(write_info);
cristy151b66d2015-04-15 10:50:31 +00001054 (void) CopyMagickString(image->filename,filename,MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +00001055 return(status);
1056 }
1057 }
1058 status=MagickFalse;
1059 temporary=MagickFalse;
1060 if ((magick_info != (const MagickInfo *) NULL) &&
1061 (GetMagickSeekableStream(magick_info) != MagickFalse))
1062 {
1063 char
cristy151b66d2015-04-15 10:50:31 +00001064 filename[MagickPathExtent];
cristy3ed852e2009-09-05 21:47:34 +00001065
cristy151b66d2015-04-15 10:50:31 +00001066 (void) CopyMagickString(filename,image->filename,MagickPathExtent);
cristy6f9e0d32011-08-28 16:32:09 +00001067 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
cristy151b66d2015-04-15 10:50:31 +00001068 (void) CopyMagickString(image->filename,filename,MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +00001069 if (status != MagickFalse)
1070 {
cristye92a6fd2010-02-04 01:36:45 +00001071 if (IsBlobSeekable(image) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001072 {
1073 /*
1074 A seekable stream is required by the encoder.
1075 */
cristy54439632010-07-15 00:43:34 +00001076 write_info->adjoin=MagickTrue;
cristy3ed852e2009-09-05 21:47:34 +00001077 (void) CopyMagickString(write_info->filename,image->filename,
cristy151b66d2015-04-15 10:50:31 +00001078 MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +00001079 (void) AcquireUniqueFilename(image->filename);
1080 temporary=MagickTrue;
1081 }
1082 (void) CloseBlob(image);
1083 }
1084 }
1085 if ((magick_info != (const MagickInfo *) NULL) &&
1086 (GetImageEncoder(magick_info) != (EncodeImageHandler *) NULL))
1087 {
1088 /*
1089 Call appropriate image writer based on image type.
1090 */
dirk24ae22e2015-02-22 00:17:42 +00001091 if (GetMagickEncoderThreadSupport(magick_info) == MagickFalse)
cristy191ba5c2014-03-16 21:26:40 +00001092 LockSemaphoreInfo(magick_info->semaphore);
cristy1e178e72011-08-28 19:44:34 +00001093 status=GetImageEncoder(magick_info)(write_info,image,exception);
dirk24ae22e2015-02-22 00:17:42 +00001094 if (GetMagickEncoderThreadSupport(magick_info) == MagickFalse)
cristy191ba5c2014-03-16 21:26:40 +00001095 UnlockSemaphoreInfo(magick_info->semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001096 }
1097 else
1098 {
cristy6f9e0d32011-08-28 16:32:09 +00001099 delegate_info=GetDelegateInfo((char *) NULL,write_info->magick,exception);
cristy3ed852e2009-09-05 21:47:34 +00001100 if (delegate_info != (DelegateInfo *) NULL)
1101 {
1102 /*
1103 Process the image with delegate.
1104 */
1105 *write_info->filename='\0';
1106 if (GetDelegateThreadSupport(delegate_info) == MagickFalse)
cristy191ba5c2014-03-16 21:26:40 +00001107 LockSemaphoreInfo(delegate_info->semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001108 status=InvokeDelegate(write_info,image,(char *) NULL,
cristy6f9e0d32011-08-28 16:32:09 +00001109 write_info->magick,exception);
cristy3ed852e2009-09-05 21:47:34 +00001110 if (GetDelegateThreadSupport(delegate_info) == MagickFalse)
cristy191ba5c2014-03-16 21:26:40 +00001111 UnlockSemaphoreInfo(delegate_info->semaphore);
cristy151b66d2015-04-15 10:50:31 +00001112 (void) CopyMagickString(image->filename,filename,MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +00001113 }
1114 else
1115 {
1116 sans_exception=AcquireExceptionInfo();
1117 magick_info=GetMagickInfo(write_info->magick,sans_exception);
1118 sans_exception=DestroyExceptionInfo(sans_exception);
1119 if ((write_info->affirm == MagickFalse) &&
1120 (magick_info == (const MagickInfo *) NULL))
1121 {
1122 (void) CopyMagickString(write_info->magick,image->magick,
cristy151b66d2015-04-15 10:50:31 +00001123 MagickPathExtent);
cristy6f9e0d32011-08-28 16:32:09 +00001124 magick_info=GetMagickInfo(write_info->magick,exception);
cristy3ed852e2009-09-05 21:47:34 +00001125 }
1126 if ((magick_info == (const MagickInfo *) NULL) ||
1127 (GetImageEncoder(magick_info) == (EncodeImageHandler *) NULL))
cristy1a8fced2010-03-15 18:05:43 +00001128 {
cristy91c09252011-02-14 00:00:33 +00001129 char
cristy151b66d2015-04-15 10:50:31 +00001130 extension[MagickPathExtent];
cristy91c09252011-02-14 00:00:33 +00001131
1132 GetPathComponent(image->filename,ExtensionPath,extension);
1133 if (*extension != '\0')
cristy6f9e0d32011-08-28 16:32:09 +00001134 magick_info=GetMagickInfo(extension,exception);
cristy91c09252011-02-14 00:00:33 +00001135 else
cristy6f9e0d32011-08-28 16:32:09 +00001136 magick_info=GetMagickInfo(image->magick,exception);
cristy151b66d2015-04-15 10:50:31 +00001137 (void) CopyMagickString(image->filename,filename,MagickPathExtent);
cristy924c8722010-03-15 17:50:24 +00001138 }
cristy1a8fced2010-03-15 18:05:43 +00001139 if ((magick_info == (const MagickInfo *) NULL) ||
1140 (GetImageEncoder(magick_info) == (EncodeImageHandler *) NULL))
cristy95fc4ef2012-04-21 18:58:21 +00001141 {
1142 magick_info=GetMagickInfo(image->magick,exception);
1143 if ((magick_info == (const MagickInfo *) NULL) ||
1144 (GetImageEncoder(magick_info) == (EncodeImageHandler *) NULL))
1145 (void) ThrowMagickException(exception,GetMagickModule(),
1146 MissingDelegateError,"NoEncodeDelegateForThisImageFormat",
dirkea8856e2014-04-09 16:44:40 +00001147 "`%s'",write_info->magick);
cristy95fc4ef2012-04-21 18:58:21 +00001148 else
1149 (void) ThrowMagickException(exception,GetMagickModule(),
1150 MissingDelegateWarning,"NoEncodeDelegateForThisImageFormat",
dirkea8856e2014-04-09 16:44:40 +00001151 "`%s'",write_info->magick);
cristy95fc4ef2012-04-21 18:58:21 +00001152 }
1153 if ((magick_info != (const MagickInfo *) NULL) &&
1154 (GetImageEncoder(magick_info) != (EncodeImageHandler *) NULL))
cristy3ed852e2009-09-05 21:47:34 +00001155 {
1156 /*
1157 Call appropriate image writer based on image type.
1158 */
dirk24ae22e2015-02-22 00:17:42 +00001159 if (GetMagickEncoderThreadSupport(magick_info) == MagickFalse)
cristy191ba5c2014-03-16 21:26:40 +00001160 LockSemaphoreInfo(magick_info->semaphore);
cristy1e178e72011-08-28 19:44:34 +00001161 status=GetImageEncoder(magick_info)(write_info,image,exception);
dirk24ae22e2015-02-22 00:17:42 +00001162 if (GetMagickEncoderThreadSupport(magick_info) == MagickFalse)
cristy191ba5c2014-03-16 21:26:40 +00001163 UnlockSemaphoreInfo(magick_info->semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001164 }
1165 }
1166 }
cristycd8b3312013-12-22 01:51:11 +00001167 if (temporary != MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001168 {
1169 /*
1170 Copy temporary image file to permanent.
1171 */
cristy6f9e0d32011-08-28 16:32:09 +00001172 status=OpenBlob(write_info,image,ReadBinaryBlobMode,exception);
cristy3ed852e2009-09-05 21:47:34 +00001173 if (status != MagickFalse)
cristycd20d2b2012-01-31 16:02:29 +00001174 {
1175 (void) RelinquishUniqueFileResource(write_info->filename);
1176 status=ImageToFile(image,write_info->filename,exception);
1177 }
cristya8902942010-07-30 00:49:52 +00001178 (void) CloseBlob(image);
cristy3ed852e2009-09-05 21:47:34 +00001179 (void) RelinquishUniqueFileResource(image->filename);
1180 (void) CopyMagickString(image->filename,write_info->filename,
cristy151b66d2015-04-15 10:50:31 +00001181 MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +00001182 }
1183 if ((LocaleCompare(write_info->magick,"info") != 0) &&
1184 (write_info->verbose != MagickFalse))
cristy6f9e0d32011-08-28 16:32:09 +00001185 (void) IdentifyImage(image,stdout,MagickFalse,exception);
cristy3ed852e2009-09-05 21:47:34 +00001186 write_info=DestroyImageInfo(write_info);
1187 return(status);
1188}
1189
1190/*
1191%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1192% %
1193% %
1194% %
1195% W r i t e I m a g e s %
1196% %
1197% %
1198% %
1199%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1200%
cristy96581e82011-02-01 01:10:11 +00001201% WriteImages() writes an image sequence into one or more files. While
1202% WriteImage() can write an image sequence, it is limited to writing
1203% the sequence into a single file using a format which supports multiple
1204% frames. WriteImages(), however, does not have this limitation, instead it
1205% generates multiple output files if necessary (or when requested). When
1206% ImageInfo's adjoin flag is set to MagickFalse, the file name is expected
1207% to include a printf-style formatting string for the frame number (e.g.
1208% "image%02d.png").
cristy3ed852e2009-09-05 21:47:34 +00001209%
1210% The format of the WriteImages method is:
1211%
1212% MagickBooleanType WriteImages(const ImageInfo *image_info,Image *images,
1213% const char *filename,ExceptionInfo *exception)
1214%
1215% A description of each parameter follows:
1216%
1217% o image_info: the image info.
1218%
1219% o images: the image list.
1220%
1221% o filename: the image filename.
1222%
1223% o exception: return any errors or warnings in this structure.
1224%
1225*/
1226MagickExport MagickBooleanType WriteImages(const ImageInfo *image_info,
1227 Image *images,const char *filename,ExceptionInfo *exception)
1228{
cristy36507e62010-02-11 00:26:48 +00001229#define WriteImageTag "Write/Image"
1230
cristy3ed852e2009-09-05 21:47:34 +00001231 ExceptionInfo
1232 *sans_exception;
1233
1234 ImageInfo
1235 *write_info;
1236
cristy36507e62010-02-11 00:26:48 +00001237 MagickBooleanType
1238 proceed;
1239
1240 MagickOffsetType
1241 i;
1242
1243 MagickProgressMonitor
1244 progress_monitor;
1245
1246 MagickSizeType
1247 number_images;
1248
cristy3ed852e2009-09-05 21:47:34 +00001249 MagickStatusType
1250 status;
1251
1252 register Image
1253 *p;
1254
1255 assert(image_info != (const ImageInfo *) NULL);
cristye1c94d92015-06-28 12:16:33 +00001256 assert(image_info->signature == MagickCoreSignature);
cristy3ed852e2009-09-05 21:47:34 +00001257 assert(images != (Image *) NULL);
cristye1c94d92015-06-28 12:16:33 +00001258 assert(images->signature == MagickCoreSignature);
cristy3ed852e2009-09-05 21:47:34 +00001259 if (images->debug != MagickFalse)
1260 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",images->filename);
1261 assert(exception != (ExceptionInfo *) NULL);
1262 write_info=CloneImageInfo(image_info);
cristy1d52c602015-02-25 12:19:00 +00001263 *write_info->magick='\0';
cristy3ed852e2009-09-05 21:47:34 +00001264 images=GetFirstImageInList(images);
cristy3ed852e2009-09-05 21:47:34 +00001265 if (filename != (const char *) NULL)
1266 for (p=images; p != (Image *) NULL; p=GetNextImageInList(p))
cristy151b66d2015-04-15 10:50:31 +00001267 (void) CopyMagickString(p->filename,filename,MagickPathExtent);
1268 (void) CopyMagickString(write_info->filename,images->filename,MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +00001269 sans_exception=AcquireExceptionInfo();
cristyd965a422010-03-03 17:47:35 +00001270 (void) SetImageInfo(write_info,(unsigned int) GetImageListLength(images),
1271 sans_exception);
cristy3ed852e2009-09-05 21:47:34 +00001272 sans_exception=DestroyExceptionInfo(sans_exception);
dirk39930a02014-12-27 11:28:17 +00001273 if (*write_info->magick == '\0')
cristy151b66d2015-04-15 10:50:31 +00001274 (void) CopyMagickString(write_info->magick,images->magick,MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +00001275 p=images;
1276 for ( ; GetNextImageInList(p) != (Image *) NULL; p=GetNextImageInList(p))
1277 if (p->scene >= GetNextImageInList(p)->scene)
1278 {
cristybb503372010-05-27 20:51:26 +00001279 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001280 i;
1281
1282 /*
1283 Generate consistent scene numbers.
1284 */
cristybb503372010-05-27 20:51:26 +00001285 i=(ssize_t) images->scene;
cristy3ed852e2009-09-05 21:47:34 +00001286 for (p=images; p != (Image *) NULL; p=GetNextImageInList(p))
cristybb503372010-05-27 20:51:26 +00001287 p->scene=(size_t) i++;
cristy3ed852e2009-09-05 21:47:34 +00001288 break;
1289 }
1290 /*
1291 Write images.
1292 */
1293 status=MagickTrue;
cristy466802f2010-02-24 14:18:37 +00001294 progress_monitor=(MagickProgressMonitor) NULL;
cristy36507e62010-02-11 00:26:48 +00001295 i=0;
1296 number_images=GetImageListLength(images);
cristy3ed852e2009-09-05 21:47:34 +00001297 for (p=images; p != (Image *) NULL; p=GetNextImageInList(p))
1298 {
cristy8b27a6d2010-02-14 03:31:15 +00001299 if (number_images != 1)
1300 progress_monitor=SetImageProgressMonitor(p,(MagickProgressMonitor) NULL,
1301 p->client_data);
cristy6f9e0d32011-08-28 16:32:09 +00001302 status&=WriteImage(write_info,p,exception);
cristy8b27a6d2010-02-14 03:31:15 +00001303 if (number_images != 1)
1304 (void) SetImageProgressMonitor(p,progress_monitor,p->client_data);
cristy3ed852e2009-09-05 21:47:34 +00001305 if (write_info->adjoin != MagickFalse)
1306 break;
cristy8b27a6d2010-02-14 03:31:15 +00001307 if (number_images != 1)
1308 {
1309 proceed=SetImageProgress(p,WriteImageTag,i++,number_images);
1310 if (proceed == MagickFalse)
1311 break;
1312 }
cristy3ed852e2009-09-05 21:47:34 +00001313 }
1314 write_info=DestroyImageInfo(write_info);
1315 return(status != 0 ? MagickTrue : MagickFalse);
1316}