blob: c25789f9521643fc4570e4799ad456708f42861a [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
6% M M IIIII FFFFF FFFFF %
7% MM MM I F F %
8% M M M I FFF FFF %
9% M M I F F %
10% M M IIIII F F %
11% %
12% %
13% Read/Write MIFF Image Format %
14% %
15% Software Design %
cristyde984cd2013-12-01 14:49:27 +000016% Cristy %
cristy3ed852e2009-09-05 21:47:34 +000017% July 1992 %
18% %
19% %
Cristy7ce65e72015-12-12 18:03:16 -050020% Copyright 1999-2016 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"
43#include "MagickCore/attribute.h"
44#include "MagickCore/blob.h"
45#include "MagickCore/blob-private.h"
46#include "MagickCore/cache.h"
47#include "MagickCore/color.h"
48#include "MagickCore/color-private.h"
49#include "MagickCore/colormap.h"
50#include "MagickCore/colormap-private.h"
51#include "MagickCore/colorspace.h"
cristy325cfdc2012-04-28 01:00:09 +000052#include "MagickCore/colorspace-private.h"
cristy4c08aed2011-07-01 19:47:50 +000053#include "MagickCore/constitute.h"
54#include "MagickCore/exception.h"
55#include "MagickCore/exception-private.h"
56#include "MagickCore/hashmap.h"
57#include "MagickCore/geometry.h"
58#include "MagickCore/image.h"
59#include "MagickCore/image-private.h"
60#include "MagickCore/list.h"
61#include "MagickCore/magick.h"
62#include "MagickCore/memory_.h"
63#include "MagickCore/module.h"
64#include "MagickCore/monitor.h"
65#include "MagickCore/monitor-private.h"
66#include "MagickCore/option.h"
67#include "MagickCore/pixel.h"
68#include "MagickCore/pixel-accessor.h"
69#include "MagickCore/profile.h"
70#include "MagickCore/property.h"
71#include "MagickCore/quantum-private.h"
72#include "MagickCore/static.h"
73#include "MagickCore/statistic.h"
74#include "MagickCore/string_.h"
75#include "MagickCore/string-private.h"
cristy3ed852e2009-09-05 21:47:34 +000076#if defined(MAGICKCORE_BZLIB_DELEGATE)
77#include "bzlib.h"
78#endif
cristy26377172010-12-20 19:01:58 +000079#if defined(MAGICKCORE_LZMA_DELEGATE)
80#include "lzma.h"
81#endif
82#if defined(MAGICKCORE_ZLIB_DELEGATE)
83#include "zlib.h"
84#endif
cristy3ed852e2009-09-05 21:47:34 +000085
86/*
cristya7a341e2010-12-22 20:28:51 +000087 Define declarations.
88*/
89#if !defined(LZMA_OK)
90#define LZMA_OK 0
91#endif
92
93/*
cristy3ed852e2009-09-05 21:47:34 +000094 Forward declarations.
95*/
96static MagickBooleanType
cristy1e178e72011-08-28 19:44:34 +000097 WriteMIFFImage(const ImageInfo *,Image *,ExceptionInfo *);
cristy3ed852e2009-09-05 21:47:34 +000098
99/*
100%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
101% %
102% %
103% %
104% I s M I F F %
105% %
106% %
107% %
108%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
109%
110% IsMIFF() returns MagickTrue if the image format type, identified by the
111% magick string, is MIFF.
112%
113% The format of the IsMIFF method is:
114%
115% MagickBooleanType IsMIFF(const unsigned char *magick,const size_t length)
116%
117% A description of each parameter follows:
118%
119% o magick: compare image format pattern against these bytes.
120%
121% o length: Specifies the length of the magick string.
122%
123*/
124static MagickBooleanType IsMIFF(const unsigned char *magick,const size_t length)
125{
126 if (length < 14)
127 return(MagickFalse);
128 if (LocaleNCompare((const char *) magick,"id=ImageMagick",14) == 0)
129 return(MagickTrue);
130 return(MagickFalse);
131}
132
133/*
134%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
135% %
136% %
137% %
138% R e a d M I F F I m a g e %
139% %
140% %
141% %
142%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
143%
144% ReadMIFFImage() reads a MIFF image file and returns it. It allocates the
145% memory necessary for the new Image structure and returns a pointer to the
146% new image.
147%
148% The format of the ReadMIFFImage method is:
149%
150% Image *ReadMIFFImage(const ImageInfo *image_info,
151% ExceptionInfo *exception)
152%
153% Decompression code contributed by Kyle Shorter.
154%
155% A description of each parameter follows:
156%
157% o image_info: the image info.
158%
159% o exception: return any errors or warnings in this structure.
160%
161*/
162
163#if defined(MAGICKCORE_BZLIB_DELEGATE)
164static void *AcquireBZIPMemory(void *context,int items,int size)
165{
166 (void) context;
167 return((void *) AcquireQuantumMemory((size_t) items,(size_t) size));
168}
169#endif
170
cristy4b46dba2010-12-20 19:18:20 +0000171#if defined(MAGICKCORE_LZMA_DELEGATE)
172static void *AcquireLZMAMemory(void *context,size_t items,size_t size)
173{
174 (void) context;
175 return((void *) AcquireQuantumMemory((size_t) items,(size_t) size));
176}
177#endif
178
cristy3ed852e2009-09-05 21:47:34 +0000179#if defined(MAGICKCORE_ZLIB_DELEGATE)
180static voidpf AcquireZIPMemory(voidpf context,unsigned int items,
181 unsigned int size)
182{
183 (void) context;
184 return((voidpf) AcquireQuantumMemory(items,size));
185}
186#endif
187
cristy3ed852e2009-09-05 21:47:34 +0000188static void PushRunlengthPacket(Image *image,const unsigned char *pixels,
cristyc82a27b2011-10-21 01:07:16 +0000189 size_t *length,PixelInfo *pixel,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000190{
191 const unsigned char
192 *p;
193
194 p=pixels;
195 if (image->storage_class == PseudoClass)
196 {
cristy4c08aed2011-07-01 19:47:50 +0000197 pixel->index=0;
cristy3ed852e2009-09-05 21:47:34 +0000198 switch (image->depth)
199 {
200 case 32:
201 {
cristy5467fcf2014-05-18 17:20:55 +0000202 pixel->index=ConstrainColormapIndex(image,((size_t) *p << 24) |
203 ((size_t) *(p+1) << 16) | ((size_t) *(p+2) << 8) | (size_t) *(p+3),
204 exception);
cristy3ed852e2009-09-05 21:47:34 +0000205 p+=4;
206 break;
207 }
208 case 16:
209 {
cristyc82a27b2011-10-21 01:07:16 +0000210 pixel->index=ConstrainColormapIndex(image,(*p << 8) | *(p+1),
211 exception);
cristy3ed852e2009-09-05 21:47:34 +0000212 p+=2;
213 break;
214 }
215 case 8:
216 {
cristyc82a27b2011-10-21 01:07:16 +0000217 pixel->index=ConstrainColormapIndex(image,*p,exception);
cristy3ed852e2009-09-05 21:47:34 +0000218 p++;
219 break;
220 }
221 default:
cristyc82a27b2011-10-21 01:07:16 +0000222 (void) ThrowMagickException(exception,GetMagickModule(),
cristy3ed852e2009-09-05 21:47:34 +0000223 CorruptImageError,"ImageDepthNotSupported","`%s'",image->filename);
224 }
cristy3ed852e2009-09-05 21:47:34 +0000225 switch (image->depth)
226 {
227 case 8:
228 {
229 unsigned char
230 quantum;
231
cristy9f36ba72014-12-07 22:55:01 +0000232 if (image->alpha_trait != UndefinedPixelTrait)
cristy3ed852e2009-09-05 21:47:34 +0000233 {
234 p=PushCharPixel(p,&quantum);
cristy4c08aed2011-07-01 19:47:50 +0000235 pixel->alpha=ScaleCharToQuantum(quantum);
cristy3ed852e2009-09-05 21:47:34 +0000236 }
237 break;
238 }
239 case 16:
240 {
241 unsigned short
242 quantum;
243
cristy9f36ba72014-12-07 22:55:01 +0000244 if (image->alpha_trait != UndefinedPixelTrait)
cristy3ed852e2009-09-05 21:47:34 +0000245 {
246 p=PushShortPixel(MSBEndian,p,&quantum);
cristy4c08aed2011-07-01 19:47:50 +0000247 pixel->alpha=(Quantum) (quantum >> (image->depth-
cristy3ed852e2009-09-05 21:47:34 +0000248 MAGICKCORE_QUANTUM_DEPTH));
249 }
250 break;
251 }
252 case 32:
253 {
cristy4cb162a2010-05-30 03:04:47 +0000254 unsigned int
cristy3ed852e2009-09-05 21:47:34 +0000255 quantum;
256
cristy9f36ba72014-12-07 22:55:01 +0000257 if (image->alpha_trait != UndefinedPixelTrait)
cristy3ed852e2009-09-05 21:47:34 +0000258 {
259 p=PushLongPixel(MSBEndian,p,&quantum);
cristy4c08aed2011-07-01 19:47:50 +0000260 pixel->alpha=(Quantum) (quantum >> (image->depth-
cristy3ed852e2009-09-05 21:47:34 +0000261 MAGICKCORE_QUANTUM_DEPTH));
262 }
263 break;
264 }
265 default:
cristyc82a27b2011-10-21 01:07:16 +0000266 (void) ThrowMagickException(exception,GetMagickModule(),
cristy3ed852e2009-09-05 21:47:34 +0000267 CorruptImageError,"ImageDepthNotSupported","`%s'",image->filename);
268 }
269 *length=(size_t) (*p++)+1;
270 return;
271 }
272 switch (image->depth)
273 {
274 case 8:
275 {
276 unsigned char
277 quantum;
278
279 p=PushCharPixel(p,&quantum);
cristy4c08aed2011-07-01 19:47:50 +0000280 pixel->red=ScaleCharToQuantum(quantum);
cristy325cfdc2012-04-28 01:00:09 +0000281 pixel->green=pixel->red;
282 pixel->blue=pixel->red;
283 if (IsGrayColorspace(image->colorspace) == MagickFalse)
284 {
285 p=PushCharPixel(p,&quantum);
286 pixel->green=ScaleCharToQuantum(quantum);
287 p=PushCharPixel(p,&quantum);
288 pixel->blue=ScaleCharToQuantum(quantum);
289 }
cristy3ed852e2009-09-05 21:47:34 +0000290 if (image->colorspace == CMYKColorspace)
291 {
292 p=PushCharPixel(p,&quantum);
cristy4c08aed2011-07-01 19:47:50 +0000293 pixel->black=ScaleCharToQuantum(quantum);
294 }
cristy9f36ba72014-12-07 22:55:01 +0000295 if (image->alpha_trait != UndefinedPixelTrait)
cristy4c08aed2011-07-01 19:47:50 +0000296 {
297 p=PushCharPixel(p,&quantum);
298 pixel->alpha=ScaleCharToQuantum(quantum);
cristy3ed852e2009-09-05 21:47:34 +0000299 }
300 break;
301 }
302 case 16:
303 {
304 unsigned short
305 quantum;
306
307 p=PushShortPixel(MSBEndian,p,&quantum);
cristy4c08aed2011-07-01 19:47:50 +0000308 pixel->red=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
cristy325cfdc2012-04-28 01:00:09 +0000309 pixel->green=pixel->red;
310 pixel->blue=pixel->red;
311 if (IsGrayColorspace(image->colorspace) == MagickFalse)
312 {
313 p=PushShortPixel(MSBEndian,p,&quantum);
314 pixel->green=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
315 p=PushShortPixel(MSBEndian,p,&quantum);
316 pixel->blue=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
317 }
cristy3ed852e2009-09-05 21:47:34 +0000318 if (image->colorspace == CMYKColorspace)
319 {
320 p=PushShortPixel(MSBEndian,p,&quantum);
cristy4c08aed2011-07-01 19:47:50 +0000321 pixel->black=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
322 }
cristy9f36ba72014-12-07 22:55:01 +0000323 if (image->alpha_trait != UndefinedPixelTrait)
cristy4c08aed2011-07-01 19:47:50 +0000324 {
325 p=PushShortPixel(MSBEndian,p,&quantum);
326 pixel->alpha=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
cristy3ed852e2009-09-05 21:47:34 +0000327 }
328 break;
329 }
330 case 32:
331 {
cristy4cb162a2010-05-30 03:04:47 +0000332 unsigned int
cristy3ed852e2009-09-05 21:47:34 +0000333 quantum;
334
335 p=PushLongPixel(MSBEndian,p,&quantum);
cristy4c08aed2011-07-01 19:47:50 +0000336 pixel->red=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
cristy325cfdc2012-04-28 01:00:09 +0000337 pixel->green=pixel->red;
338 pixel->blue=pixel->red;
339 if (IsGrayColorspace(image->colorspace) == MagickFalse)
340 {
341 p=PushLongPixel(MSBEndian,p,&quantum);
342 pixel->green=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
343 p=PushLongPixel(MSBEndian,p,&quantum);
344 pixel->blue=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
345 }
cristy3ed852e2009-09-05 21:47:34 +0000346 if (image->colorspace == CMYKColorspace)
347 {
348 p=PushLongPixel(MSBEndian,p,&quantum);
cristy4c08aed2011-07-01 19:47:50 +0000349 pixel->black=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
350 }
cristy9f36ba72014-12-07 22:55:01 +0000351 if (image->alpha_trait != UndefinedPixelTrait)
cristy4c08aed2011-07-01 19:47:50 +0000352 {
353 p=PushLongPixel(MSBEndian,p,&quantum);
354 pixel->alpha=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
cristy3ed852e2009-09-05 21:47:34 +0000355 }
356 break;
357 }
358 default:
cristyc82a27b2011-10-21 01:07:16 +0000359 (void) ThrowMagickException(exception,GetMagickModule(),CorruptImageError,
360 "ImageDepthNotSupported","`%s'",image->filename);
cristy3ed852e2009-09-05 21:47:34 +0000361 }
362 *length=(size_t) (*p++)+1;
363}
364
cristy4b46dba2010-12-20 19:18:20 +0000365#if defined(MAGICKCORE_BZLIB_DELEGATE)
366static void RelinquishBZIPMemory(void *context,void *memory)
cristy3ed852e2009-09-05 21:47:34 +0000367{
368 (void) context;
369 memory=RelinquishMagickMemory(memory);
370}
371#endif
372
cristy4b46dba2010-12-20 19:18:20 +0000373#if defined(MAGICKCORE_LZMA_DELEGATE)
374static void RelinquishLZMAMemory(void *context,void *memory)
375{
376 (void) context;
377 memory=RelinquishMagickMemory(memory);
378}
379#endif
380
381#if defined(MAGICKCORE_ZLIB_DELEGATE)
382static void RelinquishZIPMemory(voidpf context,voidpf memory)
cristy3ed852e2009-09-05 21:47:34 +0000383{
384 (void) context;
385 memory=RelinquishMagickMemory(memory);
386}
387#endif
388
389static Image *ReadMIFFImage(const ImageInfo *image_info,
390 ExceptionInfo *exception)
391{
392#define BZipMaxExtent(x) ((x)+((x)/100)+600)
cristy26377172010-12-20 19:01:58 +0000393#define LZMAMaxExtent(x) ((x)+((x)/3)+128)
cristy3ed852e2009-09-05 21:47:34 +0000394#define ZipMaxExtent(x) ((x)+(((x)+7) >> 3)+(((x)+63) >> 6)+11)
395
396#if defined(MAGICKCORE_BZLIB_DELEGATE)
397 bz_stream
398 bzip_info;
399#endif
400
401 char
cristy151b66d2015-04-15 10:50:31 +0000402 id[MagickPathExtent],
403 keyword[MagickPathExtent],
cristy3ed852e2009-09-05 21:47:34 +0000404 *options;
405
406 const unsigned char
407 *p;
408
409 double
410 version;
411
412 GeometryInfo
413 geometry_info;
414
415 Image
416 *image;
417
cristy3ed852e2009-09-05 21:47:34 +0000418 int
cristy3a99dcf2011-12-17 01:29:40 +0000419 c;
cristy3ed852e2009-09-05 21:47:34 +0000420
cristyde626a22012-05-13 00:39:00 +0000421 LinkedListInfo
422 *profiles;
423
cristy26377172010-12-20 19:01:58 +0000424#if defined(MAGICKCORE_LZMA_DELEGATE)
425 lzma_stream
cristy330af6c2010-12-21 14:36:06 +0000426 initialize_lzma = LZMA_STREAM_INIT,
427 lzma_info;
cristy4b46dba2010-12-20 19:18:20 +0000428
429 lzma_allocator
430 allocator;
cristy26377172010-12-20 19:01:58 +0000431#endif
432
cristy3ed852e2009-09-05 21:47:34 +0000433 MagickBooleanType
434 status;
435
cristy4c08aed2011-07-01 19:47:50 +0000436 PixelInfo
437 pixel;
438
cristy3ed852e2009-09-05 21:47:34 +0000439 MagickStatusType
440 flags;
441
cristy3ed852e2009-09-05 21:47:34 +0000442 QuantumFormatType
443 quantum_format;
444
445 QuantumInfo
446 *quantum_info;
447
448 QuantumType
449 quantum_type;
450
cristybb503372010-05-27 20:51:26 +0000451 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000452 i;
453
454 size_t
cristy1d3be432015-01-07 11:26:54 +0000455 compress_extent,
cristy3ed852e2009-09-05 21:47:34 +0000456 length,
457 packet_size;
458
459 ssize_t
460 count;
461
462 unsigned char
463 *compress_pixels,
464 *pixels;
465
cristybb503372010-05-27 20:51:26 +0000466 size_t
cristy3ed852e2009-09-05 21:47:34 +0000467 colors;
468
cristy26377172010-12-20 19:01:58 +0000469 ssize_t
470 y;
471
cristy3ed852e2009-09-05 21:47:34 +0000472#if defined(MAGICKCORE_ZLIB_DELEGATE)
473 z_stream
474 zip_info;
475#endif
476
477 /*
478 Open image file.
479 */
480 assert(image_info != (const ImageInfo *) NULL);
cristye1c94d92015-06-28 12:16:33 +0000481 assert(image_info->signature == MagickCoreSignature);
cristy3ed852e2009-09-05 21:47:34 +0000482 if (image_info->debug != MagickFalse)
483 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
484 image_info->filename);
485 assert(exception != (ExceptionInfo *) NULL);
cristye1c94d92015-06-28 12:16:33 +0000486 assert(exception->signature == MagickCoreSignature);
cristy9950d572011-10-01 18:22:35 +0000487 image=AcquireImage(image_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000488 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
489 if (status == MagickFalse)
490 {
491 image=DestroyImageList(image);
492 return((Image *) NULL);
493 }
494 /*
495 Decode image header; header terminates one character beyond a ':'.
496 */
497 c=ReadBlobByte(image);
498 if (c == EOF)
499 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
cristy3ed852e2009-09-05 21:47:34 +0000500 *id='\0';
501 (void) ResetMagickMemory(keyword,0,sizeof(keyword));
502 version=0.0;
cristy51f4b1b2012-02-21 13:56:07 +0000503 (void) version;
cristy3ed852e2009-09-05 21:47:34 +0000504 do
505 {
506 /*
507 Decode image header; header terminates one character beyond a ':'.
508 */
cristy151b66d2015-04-15 10:50:31 +0000509 length=MagickPathExtent;
cristy3ed852e2009-09-05 21:47:34 +0000510 options=AcquireString((char *) NULL);
511 quantum_format=UndefinedQuantumFormat;
512 profiles=(LinkedListInfo *) NULL;
513 colors=0;
514 image->depth=8UL;
515 image->compression=NoCompression;
516 while ((isgraph(c) != MagickFalse) && (c != (int) ':'))
517 {
518 register char
519 *p;
520
521 if (c == (int) '{')
522 {
523 char
524 *comment;
525
526 /*
527 Read comment-- any text between { }.
528 */
cristy151b66d2015-04-15 10:50:31 +0000529 length=MagickPathExtent;
cristy3ed852e2009-09-05 21:47:34 +0000530 comment=AcquireString((char *) NULL);
531 for (p=comment; comment != (char *) NULL; p++)
532 {
533 c=ReadBlobByte(image);
cristyb880ee82012-04-07 15:08:14 +0000534 if (c == (int) '\\')
535 c=ReadBlobByte(image);
536 else
537 if ((c == EOF) || (c == (int) '}'))
538 break;
cristy3ed852e2009-09-05 21:47:34 +0000539 if ((size_t) (p-comment+1) >= length)
540 {
541 *p='\0';
542 length<<=1;
543 comment=(char *) ResizeQuantumMemory(comment,length+
cristy151b66d2015-04-15 10:50:31 +0000544 MagickPathExtent,sizeof(*comment));
cristy3ed852e2009-09-05 21:47:34 +0000545 if (comment == (char *) NULL)
546 break;
547 p=comment+strlen(comment);
548 }
549 *p=(char) c;
550 }
551 if (comment == (char *) NULL)
552 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
553 *p='\0';
cristyd15e6592011-10-15 00:13:06 +0000554 (void) SetImageProperty(image,"comment",comment,exception);
cristy3ed852e2009-09-05 21:47:34 +0000555 comment=DestroyString(comment);
556 c=ReadBlobByte(image);
557 }
558 else
559 if (isalnum(c) != MagickFalse)
560 {
561 /*
562 Get the keyword.
563 */
cristy151b66d2015-04-15 10:50:31 +0000564 length=MagickPathExtent;
cristy3ed852e2009-09-05 21:47:34 +0000565 p=keyword;
566 do
567 {
cristy3ed852e2009-09-05 21:47:34 +0000568 if (c == (int) '=')
569 break;
cristy151b66d2015-04-15 10:50:31 +0000570 if ((size_t) (p-keyword) < (MagickPathExtent-1))
cristy3ed852e2009-09-05 21:47:34 +0000571 *p++=(char) c;
572 c=ReadBlobByte(image);
573 } while (c != EOF);
574 *p='\0';
575 p=options;
cristy93505cf2010-08-10 21:37:49 +0000576 while ((isspace((int) ((unsigned char) c)) != 0) && (c != EOF))
cristy3ed852e2009-09-05 21:47:34 +0000577 c=ReadBlobByte(image);
578 if (c == (int) '=')
579 {
580 /*
581 Get the keyword value.
582 */
583 c=ReadBlobByte(image);
584 while ((c != (int) '}') && (c != EOF))
585 {
586 if ((size_t) (p-options+1) >= length)
587 {
588 *p='\0';
589 length<<=1;
590 options=(char *) ResizeQuantumMemory(options,length+
cristy151b66d2015-04-15 10:50:31 +0000591 MagickPathExtent,sizeof(*options));
cristy3ed852e2009-09-05 21:47:34 +0000592 if (options == (char *) NULL)
593 break;
594 p=options+strlen(options);
595 }
cristy3ed852e2009-09-05 21:47:34 +0000596 *p++=(char) c;
597 c=ReadBlobByte(image);
cristyb880ee82012-04-07 15:08:14 +0000598 if (c == '\\')
599 {
600 c=ReadBlobByte(image);
601 if (c == (int) '}')
602 {
603 *p++=(char) c;
604 c=ReadBlobByte(image);
605 }
606 }
cristy3ed852e2009-09-05 21:47:34 +0000607 if (*options != '{')
608 if (isspace((int) ((unsigned char) c)) != 0)
609 break;
cristy37889762015-01-07 12:20:18 +0000610 }
611 if (options == (char *) NULL)
612 ThrowReaderException(ResourceLimitError,
613 "MemoryAllocationFailed");
614 }
cristy3ed852e2009-09-05 21:47:34 +0000615 *p='\0';
616 if (*options == '{')
cristybfae51d2012-10-05 15:40:35 +0000617 (void) CopyMagickString(options,options+1,strlen(options));
cristy3ed852e2009-09-05 21:47:34 +0000618 /*
619 Assign a value to the specified keyword.
620 */
621 switch (*keyword)
622 {
cristy8a46d822012-08-28 23:32:39 +0000623 case 'a':
624 case 'A':
625 {
626 if (LocaleCompare(keyword,"alpha-trait") == 0)
627 {
628 ssize_t
629 alpha_trait;
630
631 alpha_trait=ParseCommandOption(MagickPixelTraitOptions,
632 MagickFalse,options);
633 if (alpha_trait < 0)
634 break;
635 image->alpha_trait=(PixelTrait) alpha_trait;
636 break;
637 }
638 (void) SetImageProperty(image,keyword,options,exception);
639 break;
640 }
cristy3ed852e2009-09-05 21:47:34 +0000641 case 'b':
642 case 'B':
643 {
644 if (LocaleCompare(keyword,"background-color") == 0)
645 {
cristy9950d572011-10-01 18:22:35 +0000646 (void) QueryColorCompliance(options,AllCompliance,
647 &image->background_color,exception);
cristy3ed852e2009-09-05 21:47:34 +0000648 break;
649 }
650 if (LocaleCompare(keyword,"blue-primary") == 0)
651 {
652 flags=ParseGeometry(options,&geometry_info);
653 image->chromaticity.blue_primary.x=geometry_info.rho;
654 image->chromaticity.blue_primary.y=geometry_info.sigma;
655 if ((flags & SigmaValue) == 0)
656 image->chromaticity.blue_primary.y=
657 image->chromaticity.blue_primary.x;
658 break;
659 }
660 if (LocaleCompare(keyword,"border-color") == 0)
661 {
cristy9950d572011-10-01 18:22:35 +0000662 (void) QueryColorCompliance(options,AllCompliance,
663 &image->border_color,exception);
cristy3ed852e2009-09-05 21:47:34 +0000664 break;
665 }
cristyd15e6592011-10-15 00:13:06 +0000666 (void) SetImageProperty(image,keyword,options,exception);
cristy3ed852e2009-09-05 21:47:34 +0000667 break;
668 }
669 case 'c':
670 case 'C':
671 {
672 if (LocaleCompare(keyword,"class") == 0)
673 {
cristybb503372010-05-27 20:51:26 +0000674 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000675 storage_class;
676
cristy042ee782011-04-22 18:48:30 +0000677 storage_class=ParseCommandOption(MagickClassOptions,
cristy3ed852e2009-09-05 21:47:34 +0000678 MagickFalse,options);
679 if (storage_class < 0)
680 break;
681 image->storage_class=(ClassType) storage_class;
682 break;
683 }
684 if (LocaleCompare(keyword,"colors") == 0)
685 {
cristye27293e2009-12-18 02:53:20 +0000686 colors=StringToUnsignedLong(options);
cristy3ed852e2009-09-05 21:47:34 +0000687 break;
688 }
689 if (LocaleCompare(keyword,"colorspace") == 0)
690 {
cristybb503372010-05-27 20:51:26 +0000691 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000692 colorspace;
693
cristy042ee782011-04-22 18:48:30 +0000694 colorspace=ParseCommandOption(MagickColorspaceOptions,
cristy3ed852e2009-09-05 21:47:34 +0000695 MagickFalse,options);
696 if (colorspace < 0)
697 break;
cristy02a27d52013-03-05 01:06:21 +0000698 image->colorspace=(ColorspaceType) colorspace;
cristy3ed852e2009-09-05 21:47:34 +0000699 break;
700 }
701 if (LocaleCompare(keyword,"compression") == 0)
702 {
cristybb503372010-05-27 20:51:26 +0000703 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000704 compression;
705
cristy042ee782011-04-22 18:48:30 +0000706 compression=ParseCommandOption(MagickCompressOptions,
cristy3ed852e2009-09-05 21:47:34 +0000707 MagickFalse,options);
708 if (compression < 0)
709 break;
710 image->compression=(CompressionType) compression;
711 break;
712 }
713 if (LocaleCompare(keyword,"columns") == 0)
714 {
cristye27293e2009-12-18 02:53:20 +0000715 image->columns=StringToUnsignedLong(options);
cristy3ed852e2009-09-05 21:47:34 +0000716 break;
717 }
cristyd15e6592011-10-15 00:13:06 +0000718 (void) SetImageProperty(image,keyword,options,exception);
cristy3ed852e2009-09-05 21:47:34 +0000719 break;
720 }
721 case 'd':
722 case 'D':
723 {
724 if (LocaleCompare(keyword,"delay") == 0)
725 {
cristye27293e2009-12-18 02:53:20 +0000726 image->delay=StringToUnsignedLong(options);
cristy3ed852e2009-09-05 21:47:34 +0000727 break;
728 }
729 if (LocaleCompare(keyword,"depth") == 0)
730 {
cristye27293e2009-12-18 02:53:20 +0000731 image->depth=StringToUnsignedLong(options);
cristy3ed852e2009-09-05 21:47:34 +0000732 break;
733 }
734 if (LocaleCompare(keyword,"dispose") == 0)
735 {
cristybb503372010-05-27 20:51:26 +0000736 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000737 dispose;
738
cristy042ee782011-04-22 18:48:30 +0000739 dispose=ParseCommandOption(MagickDisposeOptions,MagickFalse,
cristy3ed852e2009-09-05 21:47:34 +0000740 options);
741 if (dispose < 0)
742 break;
743 image->dispose=(DisposeType) dispose;
744 break;
745 }
cristyd15e6592011-10-15 00:13:06 +0000746 (void) SetImageProperty(image,keyword,options,exception);
cristy3ed852e2009-09-05 21:47:34 +0000747 break;
748 }
749 case 'e':
750 case 'E':
751 {
752 if (LocaleCompare(keyword,"endian") == 0)
753 {
cristybb503372010-05-27 20:51:26 +0000754 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000755 endian;
756
cristy042ee782011-04-22 18:48:30 +0000757 endian=ParseCommandOption(MagickEndianOptions,MagickFalse,
cristy3ed852e2009-09-05 21:47:34 +0000758 options);
759 if (endian < 0)
760 break;
761 image->endian=(EndianType) endian;
762 break;
763 }
cristyd15e6592011-10-15 00:13:06 +0000764 (void) SetImageProperty(image,keyword,options,exception);
cristy3ed852e2009-09-05 21:47:34 +0000765 break;
766 }
767 case 'g':
768 case 'G':
769 {
770 if (LocaleCompare(keyword,"gamma") == 0)
771 {
cristydbdd0e32011-11-04 23:29:40 +0000772 image->gamma=StringToDouble(options,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000773 break;
774 }
775 if (LocaleCompare(keyword,"gravity") == 0)
776 {
cristybb503372010-05-27 20:51:26 +0000777 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000778 gravity;
779
cristy042ee782011-04-22 18:48:30 +0000780 gravity=ParseCommandOption(MagickGravityOptions,MagickFalse,
cristy3ed852e2009-09-05 21:47:34 +0000781 options);
782 if (gravity < 0)
783 break;
784 image->gravity=(GravityType) gravity;
785 break;
786 }
787 if (LocaleCompare(keyword,"green-primary") == 0)
788 {
789 flags=ParseGeometry(options,&geometry_info);
790 image->chromaticity.green_primary.x=geometry_info.rho;
791 image->chromaticity.green_primary.y=geometry_info.sigma;
792 if ((flags & SigmaValue) == 0)
793 image->chromaticity.green_primary.y=
794 image->chromaticity.green_primary.x;
795 break;
796 }
cristyd15e6592011-10-15 00:13:06 +0000797 (void) SetImageProperty(image,keyword,options,exception);
cristy3ed852e2009-09-05 21:47:34 +0000798 break;
799 }
800 case 'i':
801 case 'I':
802 {
803 if (LocaleCompare(keyword,"id") == 0)
804 {
cristy151b66d2015-04-15 10:50:31 +0000805 (void) CopyMagickString(id,options,MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +0000806 break;
807 }
808 if (LocaleCompare(keyword,"iterations") == 0)
809 {
cristye27293e2009-12-18 02:53:20 +0000810 image->iterations=StringToUnsignedLong(options);
cristy3ed852e2009-09-05 21:47:34 +0000811 break;
812 }
cristyd15e6592011-10-15 00:13:06 +0000813 (void) SetImageProperty(image,keyword,options,exception);
cristy3ed852e2009-09-05 21:47:34 +0000814 break;
815 }
816 case 'm':
817 case 'M':
818 {
819 if (LocaleCompare(keyword,"matte") == 0)
820 {
cristybb503372010-05-27 20:51:26 +0000821 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000822 matte;
823
cristy042ee782011-04-22 18:48:30 +0000824 matte=ParseCommandOption(MagickBooleanOptions,MagickFalse,
cristy3ed852e2009-09-05 21:47:34 +0000825 options);
826 if (matte < 0)
827 break;
cristy8a46d822012-08-28 23:32:39 +0000828 image->alpha_trait=matte == 0 ? UndefinedPixelTrait :
829 BlendPixelTrait;
cristy3ed852e2009-09-05 21:47:34 +0000830 break;
831 }
832 if (LocaleCompare(keyword,"matte-color") == 0)
833 {
cristy9950d572011-10-01 18:22:35 +0000834 (void) QueryColorCompliance(options,AllCompliance,
835 &image->matte_color,exception);
cristy3ed852e2009-09-05 21:47:34 +0000836 break;
837 }
838 if (LocaleCompare(keyword,"montage") == 0)
839 {
840 (void) CloneString(&image->montage,options);
841 break;
842 }
cristyd15e6592011-10-15 00:13:06 +0000843 (void) SetImageProperty(image,keyword,options,exception);
cristy3ed852e2009-09-05 21:47:34 +0000844 break;
845 }
846 case 'o':
847 case 'O':
848 {
cristy3ed852e2009-09-05 21:47:34 +0000849 if (LocaleCompare(keyword,"orientation") == 0)
850 {
cristybb503372010-05-27 20:51:26 +0000851 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000852 orientation;
853
cristy042ee782011-04-22 18:48:30 +0000854 orientation=ParseCommandOption(MagickOrientationOptions,
cristy3ed852e2009-09-05 21:47:34 +0000855 MagickFalse,options);
856 if (orientation < 0)
857 break;
858 image->orientation=(OrientationType) orientation;
859 break;
860 }
cristyd15e6592011-10-15 00:13:06 +0000861 (void) SetImageProperty(image,keyword,options,exception);
cristy3ed852e2009-09-05 21:47:34 +0000862 break;
863 }
864 case 'p':
865 case 'P':
866 {
867 if (LocaleCompare(keyword,"page") == 0)
868 {
869 char
870 *geometry;
871
872 geometry=GetPageGeometry(options);
873 (void) ParseAbsoluteGeometry(geometry,&image->page);
874 geometry=DestroyString(geometry);
875 break;
876 }
cristy3e4e9f42013-04-06 22:29:40 +0000877 if (LocaleCompare(keyword,"pixel-intensity") == 0)
878 {
879 ssize_t
880 intensity;
881
882 intensity=ParseCommandOption(MagickPixelIntensityOptions,
883 MagickFalse,options);
884 if (intensity < 0)
885 break;
886 image->intensity=(PixelIntensityMethod) intensity;
887 break;
888 }
cristy3ed852e2009-09-05 21:47:34 +0000889 if ((LocaleNCompare(keyword,"profile:",8) == 0) ||
890 (LocaleNCompare(keyword,"profile-",8) == 0))
891 {
892 StringInfo
893 *profile;
894
895 if (profiles == (LinkedListInfo *) NULL)
896 profiles=NewLinkedList(0);
897 (void) AppendValueToLinkedList(profiles,
898 AcquireString(keyword+8));
cristy63f9b8e2011-09-01 13:40:50 +0000899 profile=BlobToStringInfo((const void *) NULL,(size_t)
900 StringToLong(options));
cristy49f5e9f2011-09-01 13:44:57 +0000901 if (profile == (StringInfo *) NULL)
cristy63f9b8e2011-09-01 13:40:50 +0000902 ThrowReaderException(ResourceLimitError,
903 "MemoryAllocationFailed");
cristyd15e6592011-10-15 00:13:06 +0000904 (void) SetImageProfile(image,keyword+8,profile,exception);
cristy3ed852e2009-09-05 21:47:34 +0000905 profile=DestroyStringInfo(profile);
906 break;
907 }
cristyd15e6592011-10-15 00:13:06 +0000908 (void) SetImageProperty(image,keyword,options,exception);
cristy3ed852e2009-09-05 21:47:34 +0000909 break;
910 }
911 case 'q':
912 case 'Q':
913 {
914 if (LocaleCompare(keyword,"quality") == 0)
915 {
cristye27293e2009-12-18 02:53:20 +0000916 image->quality=StringToUnsignedLong(options);
cristy3ed852e2009-09-05 21:47:34 +0000917 break;
918 }
919 if ((LocaleCompare(keyword,"quantum-format") == 0) ||
920 (LocaleCompare(keyword,"quantum:format") == 0))
921 {
cristybb503372010-05-27 20:51:26 +0000922 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000923 format;
924
cristy042ee782011-04-22 18:48:30 +0000925 format=ParseCommandOption(MagickQuantumFormatOptions,
cristy3ed852e2009-09-05 21:47:34 +0000926 MagickFalse,options);
927 if (format < 0)
928 break;
929 quantum_format=(QuantumFormatType) format;
930 break;
931 }
cristyd15e6592011-10-15 00:13:06 +0000932 (void) SetImageProperty(image,keyword,options,exception);
cristy3ed852e2009-09-05 21:47:34 +0000933 break;
934 }
935 case 'r':
936 case 'R':
937 {
938 if (LocaleCompare(keyword,"red-primary") == 0)
939 {
940 flags=ParseGeometry(options,&geometry_info);
941 image->chromaticity.red_primary.x=geometry_info.rho;
942 image->chromaticity.red_primary.y=geometry_info.sigma;
943 if ((flags & SigmaValue) == 0)
944 image->chromaticity.red_primary.y=
945 image->chromaticity.red_primary.x;
946 break;
947 }
948 if (LocaleCompare(keyword,"rendering-intent") == 0)
949 {
cristybb503372010-05-27 20:51:26 +0000950 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000951 rendering_intent;
952
cristy042ee782011-04-22 18:48:30 +0000953 rendering_intent=ParseCommandOption(MagickIntentOptions,
cristy3ed852e2009-09-05 21:47:34 +0000954 MagickFalse,options);
955 if (rendering_intent < 0)
956 break;
957 image->rendering_intent=(RenderingIntent) rendering_intent;
958 break;
959 }
960 if (LocaleCompare(keyword,"resolution") == 0)
961 {
962 flags=ParseGeometry(options,&geometry_info);
cristy2a11bef2011-10-28 18:33:11 +0000963 image->resolution.x=geometry_info.rho;
964 image->resolution.y=geometry_info.sigma;
cristy3ed852e2009-09-05 21:47:34 +0000965 if ((flags & SigmaValue) == 0)
cristy2a11bef2011-10-28 18:33:11 +0000966 image->resolution.y=image->resolution.x;
cristy3ed852e2009-09-05 21:47:34 +0000967 break;
968 }
969 if (LocaleCompare(keyword,"rows") == 0)
970 {
cristye27293e2009-12-18 02:53:20 +0000971 image->rows=StringToUnsignedLong(options);
cristy3ed852e2009-09-05 21:47:34 +0000972 break;
973 }
cristyd15e6592011-10-15 00:13:06 +0000974 (void) SetImageProperty(image,keyword,options,exception);
cristy3ed852e2009-09-05 21:47:34 +0000975 break;
976 }
977 case 's':
978 case 'S':
979 {
980 if (LocaleCompare(keyword,"scene") == 0)
981 {
cristye27293e2009-12-18 02:53:20 +0000982 image->scene=StringToUnsignedLong(options);
cristy3ed852e2009-09-05 21:47:34 +0000983 break;
984 }
cristyd15e6592011-10-15 00:13:06 +0000985 (void) SetImageProperty(image,keyword,options,exception);
cristy3ed852e2009-09-05 21:47:34 +0000986 break;
987 }
988 case 't':
989 case 'T':
990 {
991 if (LocaleCompare(keyword,"ticks-per-second") == 0)
992 {
cristy15893a42010-11-20 18:57:15 +0000993 image->ticks_per_second=(ssize_t) StringToLong(options);
cristy3ed852e2009-09-05 21:47:34 +0000994 break;
995 }
996 if (LocaleCompare(keyword,"tile-offset") == 0)
997 {
998 char
999 *geometry;
1000
1001 geometry=GetPageGeometry(options);
1002 (void) ParseAbsoluteGeometry(geometry,&image->tile_offset);
1003 geometry=DestroyString(geometry);
1004 break;
1005 }
1006 if (LocaleCompare(keyword,"type") == 0)
1007 {
cristybb503372010-05-27 20:51:26 +00001008 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001009 type;
1010
cristy042ee782011-04-22 18:48:30 +00001011 type=ParseCommandOption(MagickTypeOptions,MagickFalse,
cristy3ed852e2009-09-05 21:47:34 +00001012 options);
1013 if (type < 0)
1014 break;
1015 image->type=(ImageType) type;
1016 break;
1017 }
cristyd15e6592011-10-15 00:13:06 +00001018 (void) SetImageProperty(image,keyword,options,exception);
cristy3ed852e2009-09-05 21:47:34 +00001019 break;
1020 }
1021 case 'u':
1022 case 'U':
1023 {
1024 if (LocaleCompare(keyword,"units") == 0)
1025 {
cristybb503372010-05-27 20:51:26 +00001026 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001027 units;
1028
cristyfbbafc92011-05-05 01:22:11 +00001029 units=ParseCommandOption(MagickResolutionOptions,
1030 MagickFalse,options);
cristy3ed852e2009-09-05 21:47:34 +00001031 if (units < 0)
1032 break;
1033 image->units=(ResolutionType) units;
1034 break;
1035 }
cristyd15e6592011-10-15 00:13:06 +00001036 (void) SetImageProperty(image,keyword,options,exception);
cristy3ed852e2009-09-05 21:47:34 +00001037 break;
1038 }
1039 case 'v':
1040 case 'V':
1041 {
1042 if (LocaleCompare(keyword,"version") == 0)
1043 {
cristydbdd0e32011-11-04 23:29:40 +00001044 version=StringToDouble(options,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00001045 break;
1046 }
cristyd15e6592011-10-15 00:13:06 +00001047 (void) SetImageProperty(image,keyword,options,exception);
cristy3ed852e2009-09-05 21:47:34 +00001048 break;
1049 }
1050 case 'w':
1051 case 'W':
1052 {
1053 if (LocaleCompare(keyword,"white-point") == 0)
1054 {
1055 flags=ParseGeometry(options,&geometry_info);
1056 image->chromaticity.white_point.x=geometry_info.rho;
cristy37f88d92012-05-04 11:14:52 +00001057 image->chromaticity.white_point.y=geometry_info.sigma;
cristy74ca1502012-06-21 13:10:47 +00001058 if ((flags & SigmaValue) == 0)
cristy3ed852e2009-09-05 21:47:34 +00001059 image->chromaticity.white_point.y=
1060 image->chromaticity.white_point.x;
1061 break;
1062 }
cristyd15e6592011-10-15 00:13:06 +00001063 (void) SetImageProperty(image,keyword,options,exception);
cristy3ed852e2009-09-05 21:47:34 +00001064 break;
1065 }
1066 default:
1067 {
cristyd15e6592011-10-15 00:13:06 +00001068 (void) SetImageProperty(image,keyword,options,exception);
cristy3ed852e2009-09-05 21:47:34 +00001069 break;
1070 }
1071 }
1072 }
1073 else
1074 c=ReadBlobByte(image);
1075 while (isspace((int) ((unsigned char) c)) != 0)
1076 c=ReadBlobByte(image);
1077 }
1078 options=DestroyString(options);
1079 (void) ReadBlobByte(image);
1080 /*
1081 Verify that required image information is defined.
1082 */
1083 if ((LocaleCompare(id,"ImageMagick") != 0) ||
1084 (image->storage_class == UndefinedClass) ||
1085 (image->columns == 0) || (image->rows == 0))
1086 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1087 if (image->montage != (char *) NULL)
1088 {
1089 register char
1090 *p;
1091
1092 /*
1093 Image directory.
1094 */
cristy151b66d2015-04-15 10:50:31 +00001095 length=MagickPathExtent;
cristy3ed852e2009-09-05 21:47:34 +00001096 image->directory=AcquireString((char *) NULL);
1097 p=image->directory;
1098 do
1099 {
1100 *p='\0';
cristy151b66d2015-04-15 10:50:31 +00001101 if ((strlen(image->directory)+MagickPathExtent) >= length)
cristy3ed852e2009-09-05 21:47:34 +00001102 {
1103 /*
1104 Allocate more memory for the image directory.
1105 */
1106 length<<=1;
1107 image->directory=(char *) ResizeQuantumMemory(image->directory,
cristy151b66d2015-04-15 10:50:31 +00001108 length+MagickPathExtent,sizeof(*image->directory));
cristy3ed852e2009-09-05 21:47:34 +00001109 if (image->directory == (char *) NULL)
1110 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
1111 p=image->directory+strlen(image->directory);
1112 }
1113 c=ReadBlobByte(image);
1114 *p++=(char) c;
1115 } while (c != (int) '\0');
1116 }
1117 if (profiles != (LinkedListInfo *) NULL)
1118 {
1119 const char
1120 *name;
1121
1122 const StringInfo
1123 *profile;
1124
1125 /*
1126 Read image profiles.
1127 */
1128 ResetLinkedListIterator(profiles);
1129 name=(const char *) GetNextValueInLinkedList(profiles);
1130 while (name != (const char *) NULL)
1131 {
1132 profile=GetImageProfile(image,name);
1133 if (profile != (StringInfo *) NULL)
1134 {
1135 register unsigned char
1136 *p;
1137
1138 p=GetStringInfoDatum(profile);
1139 count=ReadBlob(image,GetStringInfoLength(profile),p);
cristyda16f162011-02-19 23:52:17 +00001140 (void) count;
cristy3ed852e2009-09-05 21:47:34 +00001141 }
1142 name=(const char *) GetNextValueInLinkedList(profiles);
1143 }
1144 profiles=DestroyLinkedList(profiles,RelinquishMagickMemory);
1145 }
1146 image->depth=GetImageQuantumDepth(image,MagickFalse);
1147 if (image->storage_class == PseudoClass)
1148 {
1149 /*
1150 Create image colormap.
1151 */
cristy018f07f2011-09-04 21:15:19 +00001152 status=AcquireImageColormap(image,colors != 0 ? colors : 256,exception);
cristy3ed852e2009-09-05 21:47:34 +00001153 if (status == MagickFalse)
1154 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1155 if (colors != 0)
1156 {
1157 size_t
1158 packet_size;
1159
1160 unsigned char
1161 *colormap;
1162
1163 /*
1164 Read image colormap from file.
1165 */
1166 packet_size=(size_t) (3UL*image->depth/8UL);
1167 colormap=(unsigned char *) AcquireQuantumMemory(image->colors,
1168 packet_size*sizeof(*colormap));
1169 if (colormap == (unsigned char *) NULL)
1170 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1171 count=ReadBlob(image,packet_size*image->colors,colormap);
1172 p=colormap;
1173 switch (image->depth)
1174 {
1175 default:
1176 ThrowReaderException(CorruptImageError,
1177 "ImageDepthNotSupported");
1178 case 8:
1179 {
1180 unsigned char
1181 pixel;
1182
cristybb503372010-05-27 20:51:26 +00001183 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00001184 {
1185 p=PushCharPixel(p,&pixel);
1186 image->colormap[i].red=ScaleCharToQuantum(pixel);
1187 p=PushCharPixel(p,&pixel);
1188 image->colormap[i].green=ScaleCharToQuantum(pixel);
1189 p=PushCharPixel(p,&pixel);
1190 image->colormap[i].blue=ScaleCharToQuantum(pixel);
1191 }
1192 break;
1193 }
1194 case 16:
1195 {
1196 unsigned short
1197 pixel;
1198
cristybb503372010-05-27 20:51:26 +00001199 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00001200 {
1201 p=PushShortPixel(MSBEndian,p,&pixel);
1202 image->colormap[i].red=ScaleShortToQuantum(pixel);
1203 p=PushShortPixel(MSBEndian,p,&pixel);
1204 image->colormap[i].green=ScaleShortToQuantum(pixel);
1205 p=PushShortPixel(MSBEndian,p,&pixel);
1206 image->colormap[i].blue=ScaleShortToQuantum(pixel);
1207 }
1208 break;
1209 }
1210 case 32:
1211 {
cristy4cb162a2010-05-30 03:04:47 +00001212 unsigned int
cristy3ed852e2009-09-05 21:47:34 +00001213 pixel;
1214
cristybb503372010-05-27 20:51:26 +00001215 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00001216 {
1217 p=PushLongPixel(MSBEndian,p,&pixel);
1218 image->colormap[i].red=ScaleLongToQuantum(pixel);
1219 p=PushLongPixel(MSBEndian,p,&pixel);
1220 image->colormap[i].green=ScaleLongToQuantum(pixel);
1221 p=PushLongPixel(MSBEndian,p,&pixel);
1222 image->colormap[i].blue=ScaleLongToQuantum(pixel);
1223 }
1224 break;
1225 }
1226 }
1227 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
1228 }
1229 }
1230 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
1231 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1232 break;
cristyacabb842014-12-14 23:36:33 +00001233 status=SetImageExtent(image,image->columns,image->rows,exception);
1234 if (status == MagickFalse)
1235 return(DestroyImageList(image));
cristy3ed852e2009-09-05 21:47:34 +00001236 /*
1237 Allocate image pixels.
1238 */
cristy5f766ef2014-12-14 21:12:47 +00001239 quantum_info=AcquireQuantumInfo(image_info,image);
cristy3ed852e2009-09-05 21:47:34 +00001240 if (quantum_info == (QuantumInfo *) NULL)
1241 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1242 if (quantum_format != UndefinedQuantumFormat)
1243 {
1244 status=SetQuantumFormat(image,quantum_info,quantum_format);
1245 if (status == MagickFalse)
1246 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1247 }
1248 packet_size=(size_t) (quantum_info->depth/8);
1249 if (image->storage_class == DirectClass)
1250 packet_size=(size_t) (3*quantum_info->depth/8);
cristy325cfdc2012-04-28 01:00:09 +00001251 if (IsGrayColorspace(image->colorspace) != MagickFalse)
cristy0a30e182011-09-01 01:43:15 +00001252 packet_size=quantum_info->depth/8;
cristy9f36ba72014-12-07 22:55:01 +00001253 if (image->alpha_trait != UndefinedPixelTrait)
cristy3ed852e2009-09-05 21:47:34 +00001254 packet_size+=quantum_info->depth/8;
1255 if (image->colorspace == CMYKColorspace)
1256 packet_size+=quantum_info->depth/8;
1257 if (image->compression == RLECompression)
1258 packet_size++;
cristy1d3be432015-01-07 11:26:54 +00001259 compress_extent=MagickMax(MagickMax(BZipMaxExtent(packet_size*
1260 image->columns),LZMAMaxExtent(packet_size*image->columns)),
1261 ZipMaxExtent(packet_size*image->columns));
1262 compress_pixels=(unsigned char *) AcquireQuantumMemory(compress_extent,
cristy3ed852e2009-09-05 21:47:34 +00001263 sizeof(*compress_pixels));
1264 if (compress_pixels == (unsigned char *) NULL)
1265 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1266 /*
1267 Read image pixels.
1268 */
1269 quantum_type=RGBQuantum;
cristy9f36ba72014-12-07 22:55:01 +00001270 if (image->alpha_trait != UndefinedPixelTrait)
cristy3ed852e2009-09-05 21:47:34 +00001271 quantum_type=RGBAQuantum;
1272 if (image->colorspace == CMYKColorspace)
1273 {
1274 quantum_type=CMYKQuantum;
cristy9f36ba72014-12-07 22:55:01 +00001275 if (image->alpha_trait != UndefinedPixelTrait)
cristy3ed852e2009-09-05 21:47:34 +00001276 quantum_type=CMYKAQuantum;
1277 }
cristy325cfdc2012-04-28 01:00:09 +00001278 if (IsGrayColorspace(image->colorspace) != MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001279 {
1280 quantum_type=GrayQuantum;
cristy9f36ba72014-12-07 22:55:01 +00001281 if (image->alpha_trait != UndefinedPixelTrait)
cristy3ed852e2009-09-05 21:47:34 +00001282 quantum_type=GrayAlphaQuantum;
1283 }
cristybdbf4b62012-05-13 22:22:59 +00001284 if (image->storage_class == PseudoClass)
1285 {
1286 quantum_type=IndexQuantum;
cristy9f36ba72014-12-07 22:55:01 +00001287 if (image->alpha_trait != UndefinedPixelTrait)
cristybdbf4b62012-05-13 22:22:59 +00001288 quantum_type=IndexAlphaQuantum;
1289 }
cristy330af6c2010-12-21 14:36:06 +00001290 status=MagickTrue;
cristyd9a94d32014-01-11 21:56:26 +00001291 GetPixelInfo(image,&pixel);
cristy16abe0b2014-01-11 21:26:27 +00001292#if defined(MAGICKCORE_BZLIB_DELEGATE)
1293 (void) ResetMagickMemory(&bzip_info,0,sizeof(bzip_info));
1294#endif
1295#if defined(MAGICKCORE_LZMA_DELEGATE)
1296 (void) ResetMagickMemory(&allocator,0,sizeof(allocator));
1297#endif
1298#if defined(MAGICKCORE_ZLIB_DELEGATE)
1299 (void) ResetMagickMemory(&zip_info,0,sizeof(zip_info));
1300#endif
cristy330af6c2010-12-21 14:36:06 +00001301 switch (image->compression)
1302 {
1303#if defined(MAGICKCORE_BZLIB_DELEGATE)
1304 case BZipCompression:
1305 {
cristy3a99dcf2011-12-17 01:29:40 +00001306 int
1307 code;
1308
cristy330af6c2010-12-21 14:36:06 +00001309 bzip_info.bzalloc=AcquireBZIPMemory;
1310 bzip_info.bzfree=RelinquishBZIPMemory;
1311 bzip_info.opaque=(void *) NULL;
1312 code=BZ2_bzDecompressInit(&bzip_info,(int) image_info->verbose,
1313 MagickFalse);
1314 if (code != BZ_OK)
1315 status=MagickFalse;
cristy330af6c2010-12-21 14:36:06 +00001316 break;
1317 }
1318#endif
1319#if defined(MAGICKCORE_LZMA_DELEGATE)
1320 case LZMACompression:
1321 {
cristy3a99dcf2011-12-17 01:29:40 +00001322 int
1323 code;
1324
cristy9d72f1a2010-12-21 20:46:59 +00001325 allocator.alloc=AcquireLZMAMemory;
1326 allocator.free=RelinquishLZMAMemory;
cristy330af6c2010-12-21 14:36:06 +00001327 lzma_info=initialize_lzma;
cristy9d72f1a2010-12-21 20:46:59 +00001328 lzma_info.allocator=(&allocator);
cristy330af6c2010-12-21 14:36:06 +00001329 code=lzma_auto_decoder(&lzma_info,-1,0);
1330 if (code != LZMA_OK)
1331 status=MagickFalse;
cristy330af6c2010-12-21 14:36:06 +00001332 break;
1333 }
1334#endif
1335#if defined(MAGICKCORE_ZLIB_DELEGATE)
1336 case LZWCompression:
1337 case ZipCompression:
1338 {
cristy3a99dcf2011-12-17 01:29:40 +00001339 int
1340 code;
1341
cristy330af6c2010-12-21 14:36:06 +00001342 zip_info.zalloc=AcquireZIPMemory;
1343 zip_info.zfree=RelinquishZIPMemory;
1344 zip_info.opaque=(voidpf) NULL;
1345 code=inflateInit(&zip_info);
1346 if (code != Z_OK)
1347 status=MagickFalse;
cristy330af6c2010-12-21 14:36:06 +00001348 break;
1349 }
1350#endif
1351 case RLECompression:
cristy330af6c2010-12-21 14:36:06 +00001352 break;
cristy330af6c2010-12-21 14:36:06 +00001353 default:
1354 break;
1355 }
cristyb3f97ae2015-05-18 12:29:32 +00001356 pixels=(unsigned char *) GetQuantumPixels(quantum_info);
cristy3ed852e2009-09-05 21:47:34 +00001357 length=0;
cristybb503372010-05-27 20:51:26 +00001358 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001359 {
cristybb503372010-05-27 20:51:26 +00001360 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001361 x;
1362
cristy4c08aed2011-07-01 19:47:50 +00001363 register Quantum
dirk05d2ff72015-11-18 23:13:43 +01001364 *magick_restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001365
cristy330af6c2010-12-21 14:36:06 +00001366 if (status == MagickFalse)
1367 break;
cristy3ed852e2009-09-05 21:47:34 +00001368 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00001369 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001370 break;
cristy3ed852e2009-09-05 21:47:34 +00001371 switch (image->compression)
1372 {
cristy3ed852e2009-09-05 21:47:34 +00001373#if defined(MAGICKCORE_BZLIB_DELEGATE)
1374 case BZipCompression:
1375 {
cristy3ed852e2009-09-05 21:47:34 +00001376 bzip_info.next_out=(char *) pixels;
1377 bzip_info.avail_out=(unsigned int) (packet_size*image->columns);
1378 do
1379 {
cristy89f839d2015-01-25 17:32:32 +00001380 int
1381 code;
1382
cristy3ed852e2009-09-05 21:47:34 +00001383 if (bzip_info.avail_in == 0)
1384 {
1385 bzip_info.next_in=(char *) compress_pixels;
1386 length=(size_t) BZipMaxExtent(packet_size*image->columns);
cristy261ce822012-02-19 22:08:28 +00001387 if (version != 0.0)
cristy3ed852e2009-09-05 21:47:34 +00001388 length=(size_t) ReadBlobMSBLong(image);
cristy1d3be432015-01-07 11:26:54 +00001389 if (length > compress_extent)
cristyfda8bdc2015-07-28 22:50:33 +00001390 {
1391 (void) BZ2_bzDecompressEnd(&bzip_info);
1392 ThrowReaderException(CorruptImageError,
1393 "UnableToReadImageData");
1394 }
cristy3ed852e2009-09-05 21:47:34 +00001395 bzip_info.avail_in=(unsigned int) ReadBlob(image,length,
1396 (unsigned char *) bzip_info.next_in);
1397 }
cristy89f839d2015-01-25 17:32:32 +00001398 code=BZ2_bzDecompress(&bzip_info);
1399 if (code < 0)
1400 {
1401 status=MagickFalse;
1402 break;
1403 }
1404 if (code == BZ_STREAM_END)
cristy3ed852e2009-09-05 21:47:34 +00001405 break;
1406 } while (bzip_info.avail_out != 0);
cristy26377172010-12-20 19:01:58 +00001407 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1408 quantum_type,pixels,exception);
1409 break;
1410 }
1411#endif
1412#if defined(MAGICKCORE_LZMA_DELEGATE)
1413 case LZMACompression:
1414 {
cristy26377172010-12-20 19:01:58 +00001415 lzma_info.next_out=pixels;
cristy330af6c2010-12-21 14:36:06 +00001416 lzma_info.avail_out=packet_size*image->columns;
cristy26377172010-12-20 19:01:58 +00001417 do
1418 {
cristy3a99dcf2011-12-17 01:29:40 +00001419 int
1420 code;
1421
cristy26377172010-12-20 19:01:58 +00001422 if (lzma_info.avail_in == 0)
1423 {
1424 lzma_info.next_in=compress_pixels;
1425 length=(size_t) ReadBlobMSBLong(image);
cristy1d3be432015-01-07 11:26:54 +00001426 if (length > compress_extent)
cristyfda8bdc2015-07-28 22:50:33 +00001427 {
1428 lzma_end(&lzma_info);
1429 ThrowReaderException(CorruptImageError,
1430 "UnableToReadImageData");
1431 }
cristy26377172010-12-20 19:01:58 +00001432 lzma_info.avail_in=(unsigned int) ReadBlob(image,length,
1433 (unsigned char *) lzma_info.next_in);
1434 }
1435 code=lzma_code(&lzma_info,LZMA_RUN);
cristy330af6c2010-12-21 14:36:06 +00001436 if (code < 0)
1437 {
1438 status=MagickFalse;
1439 break;
1440 }
1441 if (code == LZMA_STREAM_END)
cristy26377172010-12-20 19:01:58 +00001442 break;
1443 } while (lzma_info.avail_out != 0);
cristy26377172010-12-20 19:01:58 +00001444 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1445 quantum_type,pixels,exception);
1446 break;
1447 }
1448#endif
1449#if defined(MAGICKCORE_ZLIB_DELEGATE)
1450 case LZWCompression:
1451 case ZipCompression:
1452 {
cristy26377172010-12-20 19:01:58 +00001453 zip_info.next_out=pixels;
1454 zip_info.avail_out=(uInt) (packet_size*image->columns);
1455 do
1456 {
cristy89f839d2015-01-25 17:32:32 +00001457 int
1458 code;
1459
cristy26377172010-12-20 19:01:58 +00001460 if (zip_info.avail_in == 0)
1461 {
1462 zip_info.next_in=compress_pixels;
1463 length=(size_t) ZipMaxExtent(packet_size*image->columns);
cristy261ce822012-02-19 22:08:28 +00001464 if (version != 0.0)
cristy26377172010-12-20 19:01:58 +00001465 length=(size_t) ReadBlobMSBLong(image);
cristy1d3be432015-01-07 11:26:54 +00001466 if (length > compress_extent)
cristyfda8bdc2015-07-28 22:50:33 +00001467 {
1468 (void) inflateEnd(&zip_info);
1469 ThrowReaderException(CorruptImageError,
1470 "UnableToReadImageData");
1471 }
cristy26377172010-12-20 19:01:58 +00001472 zip_info.avail_in=(unsigned int) ReadBlob(image,length,
1473 zip_info.next_in);
1474 }
cristy89f839d2015-01-25 17:32:32 +00001475 code=inflate(&zip_info,Z_SYNC_FLUSH);
1476 if (code < 0)
1477 {
1478 status=MagickFalse;
1479 break;
1480 }
1481 if (code == Z_STREAM_END)
cristy26377172010-12-20 19:01:58 +00001482 break;
1483 } while (zip_info.avail_out != 0);
cristy3ed852e2009-09-05 21:47:34 +00001484 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1485 quantum_type,pixels,exception);
1486 break;
1487 }
1488#endif
1489 case RLECompression:
1490 {
cristybb503372010-05-27 20:51:26 +00001491 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001492 {
1493 if (length == 0)
1494 {
1495 count=ReadBlob(image,packet_size,pixels);
cristyc82a27b2011-10-21 01:07:16 +00001496 PushRunlengthPacket(image,pixels,&length,&pixel,exception);
cristy3ed852e2009-09-05 21:47:34 +00001497 }
1498 length--;
cristy4c08aed2011-07-01 19:47:50 +00001499 if (image->storage_class == PseudoClass)
cristy94b11832011-09-08 19:46:03 +00001500 SetPixelIndex(image,ClampToQuantum(pixel.index),q);
cristy4c08aed2011-07-01 19:47:50 +00001501 else
1502 {
cristy94b11832011-09-08 19:46:03 +00001503 SetPixelRed(image,ClampToQuantum(pixel.red),q);
1504 SetPixelGreen(image,ClampToQuantum(pixel.green),q);
1505 SetPixelBlue(image,ClampToQuantum(pixel.blue),q);
cristy4c08aed2011-07-01 19:47:50 +00001506 if (image->colorspace == CMYKColorspace)
cristy94b11832011-09-08 19:46:03 +00001507 SetPixelBlack(image,ClampToQuantum(pixel.black),q);
cristy4c08aed2011-07-01 19:47:50 +00001508 }
cristy9f36ba72014-12-07 22:55:01 +00001509 if (image->alpha_trait != UndefinedPixelTrait)
cristy94b11832011-09-08 19:46:03 +00001510 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
cristyed231572011-07-14 02:18:59 +00001511 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001512 }
1513 break;
1514 }
1515 default:
1516 {
1517 count=ReadBlob(image,packet_size*image->columns,pixels);
1518 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1519 quantum_type,pixels,exception);
1520 break;
1521 }
1522 }
1523 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1524 break;
1525 }
1526 SetQuantumImageType(image,quantum_type);
cristy330af6c2010-12-21 14:36:06 +00001527 switch (image->compression)
1528 {
1529#if defined(MAGICKCORE_BZLIB_DELEGATE)
1530 case BZipCompression:
1531 {
cristy3a99dcf2011-12-17 01:29:40 +00001532 int
1533 code;
1534
cristy261ce822012-02-19 22:08:28 +00001535 if (version == 0.0)
cristy330af6c2010-12-21 14:36:06 +00001536 {
1537 MagickOffsetType
1538 offset;
1539
1540 offset=SeekBlob(image,-((MagickOffsetType)
1541 bzip_info.avail_in),SEEK_CUR);
1542 if (offset < 0)
cristy3e4e9f42013-04-06 22:29:40 +00001543 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
cristy330af6c2010-12-21 14:36:06 +00001544 }
1545 code=BZ2_bzDecompressEnd(&bzip_info);
1546 if (code != BZ_OK)
1547 status=MagickFalse;
1548 break;
1549 }
1550#endif
1551#if defined(MAGICKCORE_LZMA_DELEGATE)
1552 case LZMACompression:
1553 {
cristy3a99dcf2011-12-17 01:29:40 +00001554 int
1555 code;
1556
cristy3b788a02010-12-27 15:59:54 +00001557 code=lzma_code(&lzma_info,LZMA_FINISH);
1558 if ((code != LZMA_STREAM_END) && (code != LZMA_OK))
cristyb977da52010-12-22 15:55:53 +00001559 status=MagickFalse;
cristy330af6c2010-12-21 14:36:06 +00001560 lzma_end(&lzma_info);
1561 break;
1562 }
1563#endif
1564#if defined(MAGICKCORE_ZLIB_DELEGATE)
1565 case LZWCompression:
1566 case ZipCompression:
1567 {
cristy3a99dcf2011-12-17 01:29:40 +00001568 int
1569 code;
1570
cristy261ce822012-02-19 22:08:28 +00001571 if (version == 0.0)
cristy330af6c2010-12-21 14:36:06 +00001572 {
1573 MagickOffsetType
1574 offset;
1575
1576 offset=SeekBlob(image,-((MagickOffsetType) zip_info.avail_in),
1577 SEEK_CUR);
1578 if (offset < 0)
cristy3e4e9f42013-04-06 22:29:40 +00001579 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
cristy330af6c2010-12-21 14:36:06 +00001580 }
1581 code=inflateEnd(&zip_info);
1582 if (code != LZMA_OK)
1583 status=MagickFalse;
1584 break;
1585 }
1586#endif
1587 default:
1588 break;
1589 }
cristy3ed852e2009-09-05 21:47:34 +00001590 quantum_info=DestroyQuantumInfo(quantum_info);
1591 compress_pixels=(unsigned char *) RelinquishMagickMemory(compress_pixels);
cristybb503372010-05-27 20:51:26 +00001592 if (((y != (ssize_t) image->rows)) || (status == MagickFalse))
cristy3ed852e2009-09-05 21:47:34 +00001593 {
1594 image=DestroyImageList(image);
1595 return((Image *) NULL);
1596 }
1597 if (EOFBlob(image) != MagickFalse)
1598 {
1599 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
1600 image->filename);
1601 break;
1602 }
1603 /*
1604 Proceed to next image.
1605 */
1606 if (image_info->number_scenes != 0)
1607 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1608 break;
1609 do
1610 {
1611 c=ReadBlobByte(image);
1612 } while ((isgraph(c) == MagickFalse) && (c != EOF));
1613 if (c != EOF)
1614 {
1615 /*
1616 Allocate next image structure.
1617 */
cristy9950d572011-10-01 18:22:35 +00001618 AcquireNextImage(image_info,image,exception);
cristy3ed852e2009-09-05 21:47:34 +00001619 if (GetNextImageInList(image) == (Image *) NULL)
1620 {
1621 image=DestroyImageList(image);
1622 return((Image *) NULL);
1623 }
1624 image=SyncNextImageInList(image);
1625 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
1626 GetBlobSize(image));
1627 if (status == MagickFalse)
1628 break;
1629 }
1630 } while (c != EOF);
1631 (void) CloseBlob(image);
1632 return(GetFirstImageInList(image));
1633}
1634
1635/*
1636%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1637% %
1638% %
1639% %
1640% R e g i s t e r M I F F I m a g e %
1641% %
1642% %
1643% %
1644%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1645%
1646% RegisterMIFFImage() adds properties for the MIFF image format to the list of
1647% supported formats. The properties include the image format tag, a method to
1648% read and/or write the format, whether the format supports the saving of more
1649% than one frame to the same file or blob, whether the format supports native
1650% in-memory I/O, and a brief description of the format.
1651%
1652% The format of the RegisterMIFFImage method is:
1653%
cristybb503372010-05-27 20:51:26 +00001654% size_t RegisterMIFFImage(void)
cristy3ed852e2009-09-05 21:47:34 +00001655%
1656*/
cristybb503372010-05-27 20:51:26 +00001657ModuleExport size_t RegisterMIFFImage(void)
cristy3ed852e2009-09-05 21:47:34 +00001658{
1659 char
cristy151b66d2015-04-15 10:50:31 +00001660 version[MagickPathExtent];
cristy3ed852e2009-09-05 21:47:34 +00001661
1662 MagickInfo
1663 *entry;
1664
1665 *version='\0';
1666#if defined(MagickImageCoderSignatureText)
cristy151b66d2015-04-15 10:50:31 +00001667 (void) CopyMagickString(version,MagickLibVersionText,MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +00001668#if defined(ZLIB_VERSION)
cristy151b66d2015-04-15 10:50:31 +00001669 (void) ConcatenateMagickString(version," with Zlib ",MagickPathExtent);
1670 (void) ConcatenateMagickString(version,ZLIB_VERSION,MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +00001671#endif
1672#if defined(MAGICKCORE_BZLIB_DELEGATE)
cristy151b66d2015-04-15 10:50:31 +00001673 (void) ConcatenateMagickString(version," and BZlib",MagickPathExtent);
cristy3ed852e2009-09-05 21:47:34 +00001674#endif
1675#endif
dirk06b627a2015-04-06 18:59:17 +00001676 entry=AcquireMagickInfo("MIFF","MIFF","Magick Image File Format");
cristy3ed852e2009-09-05 21:47:34 +00001677 entry->decoder=(DecodeImageHandler *) ReadMIFFImage;
1678 entry->encoder=(EncodeImageHandler *) WriteMIFFImage;
1679 entry->magick=(IsImageFormatHandler *) IsMIFF;
dirk08e9a112015-02-22 01:51:41 +00001680 entry->flags|=CoderSeekableStreamFlag;
cristy3ed852e2009-09-05 21:47:34 +00001681 if (*version != '\0')
1682 entry->version=ConstantString(version);
cristy3ed852e2009-09-05 21:47:34 +00001683 (void) RegisterMagickInfo(entry);
1684 return(MagickImageCoderSignature);
1685}
1686
1687/*
1688%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1689% %
1690% %
1691% %
1692% U n r e g i s t e r M I F F I m a g e %
1693% %
1694% %
1695% %
1696%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1697%
1698% UnregisterMIFFImage() removes format registrations made by the MIFF module
1699% from the list of supported formats.
1700%
1701% The format of the UnregisterMIFFImage method is:
1702%
1703% UnregisterMIFFImage(void)
1704%
1705*/
1706ModuleExport void UnregisterMIFFImage(void)
1707{
1708 (void) UnregisterMagickInfo("MIFF");
1709}
1710
1711/*
1712%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1713% %
1714% %
1715% %
1716% W r i t e M I F F I m a g e %
1717% %
1718% %
1719% %
1720%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1721%
1722% WriteMIFFImage() writes a MIFF image to a file.
1723%
1724% The format of the WriteMIFFImage method is:
1725%
1726% MagickBooleanType WriteMIFFImage(const ImageInfo *image_info,
cristy1e178e72011-08-28 19:44:34 +00001727% Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001728%
1729% Compression code contributed by Kyle Shorter.
1730%
1731% A description of each parameter follows:
1732%
1733% o image_info: the image info.
1734%
1735% o image: the image.
1736%
cristy1e178e72011-08-28 19:44:34 +00001737% o exception: return any errors or warnings in this structure.
1738%
cristy3ed852e2009-09-05 21:47:34 +00001739*/
1740
1741static unsigned char *PopRunlengthPacket(Image *image,unsigned char *pixels,
cristyc82a27b2011-10-21 01:07:16 +00001742 size_t length,PixelInfo *pixel,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001743{
1744 if (image->storage_class != DirectClass)
1745 {
cristy42c36c12014-04-27 19:57:46 +00001746 unsigned int
1747 value;
1748
1749 value=(unsigned int) ClampToQuantum(pixel->index);
cristy3ed852e2009-09-05 21:47:34 +00001750 switch (image->depth)
1751 {
1752 case 32:
1753 {
cristy42c36c12014-04-27 19:57:46 +00001754 *pixels++=(unsigned char) (value >> 24);
1755 *pixels++=(unsigned char) (value >> 16);
cristy3ed852e2009-09-05 21:47:34 +00001756 }
1757 case 16:
cristy42c36c12014-04-27 19:57:46 +00001758 *pixels++=(unsigned char) (value >> 8);
cristy3ed852e2009-09-05 21:47:34 +00001759 case 8:
1760 {
cristy42c36c12014-04-27 19:57:46 +00001761 *pixels++=(unsigned char) value;
cristy3ed852e2009-09-05 21:47:34 +00001762 break;
1763 }
1764 default:
cristyc82a27b2011-10-21 01:07:16 +00001765 (void) ThrowMagickException(exception,GetMagickModule(),
cristy3ed852e2009-09-05 21:47:34 +00001766 CorruptImageError,"ImageDepthNotSupported","`%s'",image->filename);
1767 }
1768 switch (image->depth)
1769 {
1770 case 32:
1771 {
cristy4cb162a2010-05-30 03:04:47 +00001772 unsigned int
cristy3ed852e2009-09-05 21:47:34 +00001773 value;
1774
cristy9f36ba72014-12-07 22:55:01 +00001775 if (image->alpha_trait != UndefinedPixelTrait)
cristy3ed852e2009-09-05 21:47:34 +00001776 {
cristy94b11832011-09-08 19:46:03 +00001777 value=ScaleQuantumToLong(ClampToQuantum(pixel->alpha));
cristy3ed852e2009-09-05 21:47:34 +00001778 pixels=PopLongPixel(MSBEndian,value,pixels);
1779 }
1780 break;
1781 }
1782 case 16:
1783 {
1784 unsigned short
1785 value;
1786
cristy9f36ba72014-12-07 22:55:01 +00001787 if (image->alpha_trait != UndefinedPixelTrait)
cristy3ed852e2009-09-05 21:47:34 +00001788 {
cristy94b11832011-09-08 19:46:03 +00001789 value=ScaleQuantumToShort(ClampToQuantum(pixel->alpha));
cristy3ed852e2009-09-05 21:47:34 +00001790 pixels=PopShortPixel(MSBEndian,value,pixels);
1791 }
1792 break;
1793 }
1794 case 8:
1795 {
1796 unsigned char
1797 value;
1798
cristy9f36ba72014-12-07 22:55:01 +00001799 if (image->alpha_trait != UndefinedPixelTrait)
cristy3ed852e2009-09-05 21:47:34 +00001800 {
cristy94b11832011-09-08 19:46:03 +00001801 value=(unsigned char) ScaleQuantumToChar(ClampToQuantum(
1802 pixel->alpha));
cristy3ed852e2009-09-05 21:47:34 +00001803 pixels=PopCharPixel(value,pixels);
1804 }
1805 break;
1806 }
1807 default:
cristyc82a27b2011-10-21 01:07:16 +00001808 (void) ThrowMagickException(exception,GetMagickModule(),
cristy3ed852e2009-09-05 21:47:34 +00001809 CorruptImageError,"ImageDepthNotSupported","`%s'",image->filename);
1810 }
1811 *pixels++=(unsigned char) length;
1812 return(pixels);
1813 }
1814 switch (image->depth)
1815 {
1816 case 32:
1817 {
cristy4cb162a2010-05-30 03:04:47 +00001818 unsigned int
cristy3ed852e2009-09-05 21:47:34 +00001819 value;
1820
cristy94b11832011-09-08 19:46:03 +00001821 value=ScaleQuantumToLong(ClampToQuantum(pixel->red));
cristy3ed852e2009-09-05 21:47:34 +00001822 pixels=PopLongPixel(MSBEndian,value,pixels);
cristy325cfdc2012-04-28 01:00:09 +00001823 if (IsGrayColorspace(image->colorspace) == MagickFalse)
1824 {
1825 value=ScaleQuantumToLong(ClampToQuantum(pixel->green));
1826 pixels=PopLongPixel(MSBEndian,value,pixels);
1827 value=ScaleQuantumToLong(ClampToQuantum(pixel->blue));
1828 pixels=PopLongPixel(MSBEndian,value,pixels);
1829 }
cristy3ed852e2009-09-05 21:47:34 +00001830 if (image->colorspace == CMYKColorspace)
1831 {
cristy94b11832011-09-08 19:46:03 +00001832 value=ScaleQuantumToLong(ClampToQuantum(pixel->black));
cristy4c08aed2011-07-01 19:47:50 +00001833 pixels=PopLongPixel(MSBEndian,value,pixels);
1834 }
cristy9f36ba72014-12-07 22:55:01 +00001835 if (image->alpha_trait != UndefinedPixelTrait)
cristy4c08aed2011-07-01 19:47:50 +00001836 {
cristy94b11832011-09-08 19:46:03 +00001837 value=ScaleQuantumToLong(ClampToQuantum(pixel->alpha));
cristy3ed852e2009-09-05 21:47:34 +00001838 pixels=PopLongPixel(MSBEndian,value,pixels);
1839 }
1840 break;
1841 }
1842 case 16:
1843 {
1844 unsigned short
1845 value;
1846
cristy94b11832011-09-08 19:46:03 +00001847 value=ScaleQuantumToShort(ClampToQuantum(pixel->red));
cristy3ed852e2009-09-05 21:47:34 +00001848 pixels=PopShortPixel(MSBEndian,value,pixels);
cristy325cfdc2012-04-28 01:00:09 +00001849 if (IsGrayColorspace(image->colorspace) == MagickFalse)
1850 {
1851 value=ScaleQuantumToShort(ClampToQuantum(pixel->green));
1852 pixels=PopShortPixel(MSBEndian,value,pixels);
1853 value=ScaleQuantumToShort(ClampToQuantum(pixel->blue));
1854 pixels=PopShortPixel(MSBEndian,value,pixels);
1855 }
cristy3ed852e2009-09-05 21:47:34 +00001856 if (image->colorspace == CMYKColorspace)
1857 {
cristy94b11832011-09-08 19:46:03 +00001858 value=ScaleQuantumToShort(ClampToQuantum(pixel->black));
cristy4c08aed2011-07-01 19:47:50 +00001859 pixels=PopShortPixel(MSBEndian,value,pixels);
1860 }
cristy9f36ba72014-12-07 22:55:01 +00001861 if (image->alpha_trait != UndefinedPixelTrait)
cristy4c08aed2011-07-01 19:47:50 +00001862 {
cristy94b11832011-09-08 19:46:03 +00001863 value=ScaleQuantumToShort(ClampToQuantum(pixel->alpha));
cristy3ed852e2009-09-05 21:47:34 +00001864 pixels=PopShortPixel(MSBEndian,value,pixels);
1865 }
1866 break;
1867 }
1868 case 8:
1869 {
1870 unsigned char
1871 value;
1872
cristy94b11832011-09-08 19:46:03 +00001873 value=(unsigned char) ScaleQuantumToChar(ClampToQuantum(pixel->red));
cristy3ed852e2009-09-05 21:47:34 +00001874 pixels=PopCharPixel(value,pixels);
cristy325cfdc2012-04-28 01:00:09 +00001875 if (IsGrayColorspace(image->colorspace) == MagickFalse)
1876 {
1877 value=(unsigned char) ScaleQuantumToChar(ClampToQuantum(
1878 pixel->green));
1879 pixels=PopCharPixel(value,pixels);
1880 value=(unsigned char) ScaleQuantumToChar(ClampToQuantum(pixel->blue));
1881 pixels=PopCharPixel(value,pixels);
1882 }
cristy3ed852e2009-09-05 21:47:34 +00001883 if (image->colorspace == CMYKColorspace)
1884 {
cristy94b11832011-09-08 19:46:03 +00001885 value=(unsigned char) ScaleQuantumToChar(ClampToQuantum(
1886 pixel->black));
cristy4c08aed2011-07-01 19:47:50 +00001887 pixels=PopCharPixel(value,pixels);
1888 }
cristy9f36ba72014-12-07 22:55:01 +00001889 if (image->alpha_trait != UndefinedPixelTrait)
cristy4c08aed2011-07-01 19:47:50 +00001890 {
cristy94b11832011-09-08 19:46:03 +00001891 value=(unsigned char) ScaleQuantumToChar(ClampToQuantum(
1892 pixel->alpha));
cristy3ed852e2009-09-05 21:47:34 +00001893 pixels=PopCharPixel(value,pixels);
1894 }
1895 break;
1896 }
1897 default:
cristyc82a27b2011-10-21 01:07:16 +00001898 (void) ThrowMagickException(exception,GetMagickModule(),CorruptImageError,
1899 "ImageDepthNotSupported","`%s'",image->filename);
cristy3ed852e2009-09-05 21:47:34 +00001900 }
1901 *pixels++=(unsigned char) length;
1902 return(pixels);
1903}
1904
1905static MagickBooleanType WriteMIFFImage(const ImageInfo *image_info,
cristy1e178e72011-08-28 19:44:34 +00001906 Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001907{
1908#if defined(MAGICKCORE_BZLIB_DELEGATE)
1909 bz_stream
1910 bzip_info;
1911#endif
1912
1913 char
cristy151b66d2015-04-15 10:50:31 +00001914 buffer[MagickPathExtent];
cristy3ed852e2009-09-05 21:47:34 +00001915
1916 CompressionType
1917 compression;
1918
1919 const char
1920 *property,
1921 *value;
1922
cristy26377172010-12-20 19:01:58 +00001923#if defined(MAGICKCORE_LZMA_DELEGATE)
cristy4b46dba2010-12-20 19:18:20 +00001924 lzma_allocator
1925 allocator;
1926
cristy26377172010-12-20 19:01:58 +00001927 lzma_stream
cristy330af6c2010-12-21 14:36:06 +00001928 initialize_lzma = LZMA_STREAM_INIT,
1929 lzma_info;
cristy26377172010-12-20 19:01:58 +00001930#endif
cristy3ed852e2009-09-05 21:47:34 +00001931
1932 MagickBooleanType
1933 status;
1934
1935 MagickOffsetType
1936 scene;
1937
cristy4c08aed2011-07-01 19:47:50 +00001938 PixelInfo
1939 pixel,
1940 target;
cristy3ed852e2009-09-05 21:47:34 +00001941
1942 QuantumInfo
1943 *quantum_info;
1944
1945 QuantumType
1946 quantum_type;
1947
cristybb503372010-05-27 20:51:26 +00001948 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001949 i;
1950
1951 size_t
1952 length,
1953 packet_size;
1954
cristy26377172010-12-20 19:01:58 +00001955 ssize_t
1956 y;
1957
cristy3ed852e2009-09-05 21:47:34 +00001958 unsigned char
1959 *compress_pixels,
1960 *pixels,
1961 *q;
1962
1963#if defined(MAGICKCORE_ZLIB_DELEGATE)
1964 z_stream
1965 zip_info;
1966#endif
1967
1968 /*
1969 Open output image file.
1970 */
1971 assert(image_info != (const ImageInfo *) NULL);
cristye1c94d92015-06-28 12:16:33 +00001972 assert(image_info->signature == MagickCoreSignature);
cristy3ed852e2009-09-05 21:47:34 +00001973 assert(image != (Image *) NULL);
cristye1c94d92015-06-28 12:16:33 +00001974 assert(image->signature == MagickCoreSignature);
cristy3ed852e2009-09-05 21:47:34 +00001975 if (image->debug != MagickFalse)
1976 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy3a37efd2011-08-28 20:31:03 +00001977 assert(exception != (ExceptionInfo *) NULL);
cristye1c94d92015-06-28 12:16:33 +00001978 assert(exception->signature == MagickCoreSignature);
cristy1e178e72011-08-28 19:44:34 +00001979 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
cristy3ed852e2009-09-05 21:47:34 +00001980 if (status == MagickFalse)
1981 return(status);
cristy3ed852e2009-09-05 21:47:34 +00001982 scene=0;
1983 do
1984 {
1985 /*
1986 Allocate image pixels.
1987 */
cristyd7f03932013-04-07 01:19:28 +00001988 if ((image->storage_class == PseudoClass) &&
1989 (image->colors > (size_t) (GetQuantumRange(image->depth)+1)))
1990 (void) SetImageStorageClass(image,DirectClass,exception);
cristyca5fbf32013-03-30 00:04:11 +00001991 image->depth=image->depth <= 8 ? 8UL : image->depth <= 16 ? 16UL :
1992 image->depth <= 32 ? 32UL : 64UL;
cristy5f766ef2014-12-14 21:12:47 +00001993 quantum_info=AcquireQuantumInfo(image_info,image);
cristy3ed852e2009-09-05 21:47:34 +00001994 if (quantum_info == (QuantumInfo *) NULL)
1995 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1996 if ((image->storage_class != PseudoClass) && (image->depth >= 32) &&
1997 (quantum_info->format == UndefinedQuantumFormat) &&
cristy1e178e72011-08-28 19:44:34 +00001998 (IsHighDynamicRangeImage(image,exception) != MagickFalse))
cristy3ed852e2009-09-05 21:47:34 +00001999 {
2000 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
2001 if (status == MagickFalse)
2002 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2003 }
dirk29dd80e2013-10-31 23:11:11 +00002004 compression=UndefinedCompression;
cristy3ed852e2009-09-05 21:47:34 +00002005 if (image_info->compression != UndefinedCompression)
2006 compression=image_info->compression;
2007 switch (compression)
2008 {
cristy49f95c22013-05-09 11:32:05 +00002009#if !defined(MAGICKCORE_LZMA_DELEGATE)
cristy26377172010-12-20 19:01:58 +00002010 case LZMACompression: compression=NoCompression; break;
2011#endif
2012#if !defined(MAGICKCORE_ZLIB_DELEGATE)
cristy3ed852e2009-09-05 21:47:34 +00002013 case LZWCompression:
2014 case ZipCompression: compression=NoCompression; break;
2015#endif
2016#if !defined(MAGICKCORE_BZLIB_DELEGATE)
2017 case BZipCompression: compression=NoCompression; break;
2018#endif
2019 case RLECompression:
2020 {
2021 if (quantum_info->format == FloatingPointQuantumFormat)
2022 compression=NoCompression;
cristy4c08aed2011-07-01 19:47:50 +00002023 GetPixelInfo(image,&target);
cristy3ed852e2009-09-05 21:47:34 +00002024 break;
2025 }
2026 default:
2027 break;
2028 }
2029 packet_size=(size_t) (quantum_info->depth/8);
2030 if (image->storage_class == DirectClass)
2031 packet_size=(size_t) (3*quantum_info->depth/8);
cristy325cfdc2012-04-28 01:00:09 +00002032 if (IsGrayColorspace(image->colorspace) != MagickFalse)
cristy0a30e182011-09-01 01:43:15 +00002033 packet_size=(size_t) (quantum_info->depth/8);
cristy9f36ba72014-12-07 22:55:01 +00002034 if (image->alpha_trait != UndefinedPixelTrait)
cristy3ed852e2009-09-05 21:47:34 +00002035 packet_size+=quantum_info->depth/8;
2036 if (image->colorspace == CMYKColorspace)
2037 packet_size+=quantum_info->depth/8;
2038 if (compression == RLECompression)
2039 packet_size++;
cristy3ed852e2009-09-05 21:47:34 +00002040 length=MagickMax(BZipMaxExtent(packet_size*image->columns),ZipMaxExtent(
2041 packet_size*image->columns));
2042 if ((compression == BZipCompression) || (compression == ZipCompression))
2043 if (length != (size_t) ((unsigned int) length))
2044 compression=NoCompression;
2045 compress_pixels=(unsigned char *) AcquireQuantumMemory(length,
2046 sizeof(*compress_pixels));
2047 if (compress_pixels == (unsigned char *) NULL)
2048 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2049 /*
2050 Write MIFF header.
2051 */
Cristy26645012015-08-23 20:24:30 -04002052 (void) WriteBlobString(image,"id=ImageMagick version=1.0\n");
cristy151b66d2015-04-15 10:50:31 +00002053 (void) FormatLocaleString(buffer,MagickPathExtent,
cristy8a46d822012-08-28 23:32:39 +00002054 "class=%s colors=%.20g alpha-trait=%s\n",CommandOptionToMnemonic(
cristye8c25f92010-06-03 00:53:06 +00002055 MagickClassOptions,image->storage_class),(double) image->colors,
cristy8a46d822012-08-28 23:32:39 +00002056 CommandOptionToMnemonic(MagickPixelTraitOptions,(ssize_t)
cristy9f36ba72014-12-07 22:55:01 +00002057 image->alpha_trait));
cristy3ed852e2009-09-05 21:47:34 +00002058 (void) WriteBlobString(image,buffer);
Cristy4cb7acb2015-08-30 09:56:13 -04002059 if (image->alpha_trait != UndefinedPixelTrait)
2060 (void) WriteBlobString(image,"matte=True\n");
Cristy6ffac4f2015-08-23 10:55:57 -04002061 (void) FormatLocaleString(buffer,MagickPathExtent,
2062 "columns=%.20g rows=%.20g depth=%.20g\n",(double) image->columns,
2063 (double) image->rows,(double) image->depth);
cristy3ed852e2009-09-05 21:47:34 +00002064 (void) WriteBlobString(image,buffer);
cristy5f1c1ff2010-12-23 21:38:06 +00002065 if (image->type != UndefinedType)
cristy3ed852e2009-09-05 21:47:34 +00002066 {
cristy151b66d2015-04-15 10:50:31 +00002067 (void) FormatLocaleString(buffer,MagickPathExtent,"type=%s\n",
cristy042ee782011-04-22 18:48:30 +00002068 CommandOptionToMnemonic(MagickTypeOptions,image->type));
cristy3ed852e2009-09-05 21:47:34 +00002069 (void) WriteBlobString(image,buffer);
2070 }
2071 if (image->colorspace != UndefinedColorspace)
2072 {
cristy151b66d2015-04-15 10:50:31 +00002073 (void) FormatLocaleString(buffer,MagickPathExtent,"colorspace=%s\n",
cristy042ee782011-04-22 18:48:30 +00002074 CommandOptionToMnemonic(MagickColorspaceOptions,image->colorspace));
cristy3ed852e2009-09-05 21:47:34 +00002075 (void) WriteBlobString(image,buffer);
2076 }
cristy3e4e9f42013-04-06 22:29:40 +00002077 if (image->intensity != UndefinedPixelIntensityMethod)
2078 {
Cristy6ffac4f2015-08-23 10:55:57 -04002079 (void) FormatLocaleString(buffer,MagickPathExtent,
2080 "pixel-intensity=%s\n",CommandOptionToMnemonic(
2081 MagickPixelIntensityOptions,image->intensity));
cristy3e4e9f42013-04-06 22:29:40 +00002082 (void) WriteBlobString(image,buffer);
2083 }
2084 if (image->endian != UndefinedEndian)
2085 {
cristy151b66d2015-04-15 10:50:31 +00002086 (void) FormatLocaleString(buffer,MagickPathExtent,"endian=%s\n",
cristy3e4e9f42013-04-06 22:29:40 +00002087 CommandOptionToMnemonic(MagickEndianOptions,image->endian));
2088 (void) WriteBlobString(image,buffer);
2089 }
cristy3ed852e2009-09-05 21:47:34 +00002090 if (compression != UndefinedCompression)
2091 {
cristy151b66d2015-04-15 10:50:31 +00002092 (void) FormatLocaleString(buffer,MagickPathExtent,"compression=%s "
cristy042ee782011-04-22 18:48:30 +00002093 "quality=%.20g\n",CommandOptionToMnemonic(MagickCompressOptions,
cristye8c25f92010-06-03 00:53:06 +00002094 compression),(double) image->quality);
cristy3ed852e2009-09-05 21:47:34 +00002095 (void) WriteBlobString(image,buffer);
2096 }
2097 if (image->units != UndefinedResolution)
2098 {
cristy151b66d2015-04-15 10:50:31 +00002099 (void) FormatLocaleString(buffer,MagickPathExtent,"units=%s\n",
cristy042ee782011-04-22 18:48:30 +00002100 CommandOptionToMnemonic(MagickResolutionOptions,image->units));
cristy3ed852e2009-09-05 21:47:34 +00002101 (void) WriteBlobString(image,buffer);
2102 }
cristy2a11bef2011-10-28 18:33:11 +00002103 if ((image->resolution.x != 0) || (image->resolution.y != 0))
cristy3ed852e2009-09-05 21:47:34 +00002104 {
cristy151b66d2015-04-15 10:50:31 +00002105 (void) FormatLocaleString(buffer,MagickPathExtent,
cristy2a11bef2011-10-28 18:33:11 +00002106 "resolution=%gx%g\n",image->resolution.x,image->resolution.y);
cristy3ed852e2009-09-05 21:47:34 +00002107 (void) WriteBlobString(image,buffer);
2108 }
2109 if ((image->page.width != 0) || (image->page.height != 0))
2110 {
cristy151b66d2015-04-15 10:50:31 +00002111 (void) FormatLocaleString(buffer,MagickPathExtent,
cristye8c25f92010-06-03 00:53:06 +00002112 "page=%.20gx%.20g%+.20g%+.20g\n",(double) image->page.width,(double)
2113 image->page.height,(double) image->page.x,(double) image->page.y);
cristy3ed852e2009-09-05 21:47:34 +00002114 (void) WriteBlobString(image,buffer);
2115 }
2116 else
2117 if ((image->page.x != 0) || (image->page.y != 0))
2118 {
cristy151b66d2015-04-15 10:50:31 +00002119 (void) FormatLocaleString(buffer,MagickPathExtent,"page=%+ld%+ld\n",
cristyf2faecf2010-05-28 19:19:36 +00002120 (long) image->page.x,(long) image->page.y);
cristy3ed852e2009-09-05 21:47:34 +00002121 (void) WriteBlobString(image,buffer);
2122 }
2123 if ((image->tile_offset.x != 0) || (image->tile_offset.y != 0))
2124 {
Cristy6ffac4f2015-08-23 10:55:57 -04002125 (void) FormatLocaleString(buffer,MagickPathExtent,
2126 "tile-offset=%+ld%+ld\n",(long) image->tile_offset.x,(long)
2127 image->tile_offset.y);
cristy3ed852e2009-09-05 21:47:34 +00002128 (void) WriteBlobString(image,buffer);
2129 }
2130 if ((GetNextImageInList(image) != (Image *) NULL) ||
2131 (GetPreviousImageInList(image) != (Image *) NULL))
2132 {
2133 if (image->scene == 0)
cristy151b66d2015-04-15 10:50:31 +00002134 (void) FormatLocaleString(buffer,MagickPathExtent,"iterations=%.20g "
cristye8c25f92010-06-03 00:53:06 +00002135 "delay=%.20g ticks-per-second=%.20g\n",(double) image->iterations,
2136 (double) image->delay,(double) image->ticks_per_second);
cristy3ed852e2009-09-05 21:47:34 +00002137 else
cristy151b66d2015-04-15 10:50:31 +00002138 (void) FormatLocaleString(buffer,MagickPathExtent,"scene=%.20g "
cristye8c25f92010-06-03 00:53:06 +00002139 "iterations=%.20g delay=%.20g ticks-per-second=%.20g\n",(double)
2140 image->scene,(double) image->iterations,(double) image->delay,
2141 (double) image->ticks_per_second);
cristy3ed852e2009-09-05 21:47:34 +00002142 (void) WriteBlobString(image,buffer);
2143 }
2144 else
2145 {
2146 if (image->scene != 0)
2147 {
cristy151b66d2015-04-15 10:50:31 +00002148 (void) FormatLocaleString(buffer,MagickPathExtent,"scene=%.20g\n",
cristye8c25f92010-06-03 00:53:06 +00002149 (double) image->scene);
cristy3ed852e2009-09-05 21:47:34 +00002150 (void) WriteBlobString(image,buffer);
2151 }
2152 if (image->iterations != 0)
2153 {
Cristy6ffac4f2015-08-23 10:55:57 -04002154 (void) FormatLocaleString(buffer,MagickPathExtent,
2155 "iterations=%.20g\n",(double) image->iterations);
cristy3ed852e2009-09-05 21:47:34 +00002156 (void) WriteBlobString(image,buffer);
2157 }
2158 if (image->delay != 0)
2159 {
cristy151b66d2015-04-15 10:50:31 +00002160 (void) FormatLocaleString(buffer,MagickPathExtent,"delay=%.20g\n",
cristye8c25f92010-06-03 00:53:06 +00002161 (double) image->delay);
cristy3ed852e2009-09-05 21:47:34 +00002162 (void) WriteBlobString(image,buffer);
2163 }
2164 if (image->ticks_per_second != UndefinedTicksPerSecond)
2165 {
cristy151b66d2015-04-15 10:50:31 +00002166 (void) FormatLocaleString(buffer,MagickPathExtent,
cristye8c25f92010-06-03 00:53:06 +00002167 "ticks-per-second=%.20g\n",(double) image->ticks_per_second);
cristy3ed852e2009-09-05 21:47:34 +00002168 (void) WriteBlobString(image,buffer);
2169 }
2170 }
2171 if (image->gravity != UndefinedGravity)
2172 {
cristy151b66d2015-04-15 10:50:31 +00002173 (void) FormatLocaleString(buffer,MagickPathExtent,"gravity=%s\n",
cristy042ee782011-04-22 18:48:30 +00002174 CommandOptionToMnemonic(MagickGravityOptions,image->gravity));
cristy3ed852e2009-09-05 21:47:34 +00002175 (void) WriteBlobString(image,buffer);
2176 }
2177 if (image->dispose != UndefinedDispose)
2178 {
cristy151b66d2015-04-15 10:50:31 +00002179 (void) FormatLocaleString(buffer,MagickPathExtent,"dispose=%s\n",
cristy042ee782011-04-22 18:48:30 +00002180 CommandOptionToMnemonic(MagickDisposeOptions,image->dispose));
cristy3ed852e2009-09-05 21:47:34 +00002181 (void) WriteBlobString(image,buffer);
2182 }
2183 if (image->rendering_intent != UndefinedIntent)
2184 {
Cristy6ffac4f2015-08-23 10:55:57 -04002185 (void) FormatLocaleString(buffer,MagickPathExtent,
2186 "rendering-intent=%s\n",CommandOptionToMnemonic(MagickIntentOptions,
2187 image->rendering_intent));
cristy3ed852e2009-09-05 21:47:34 +00002188 (void) WriteBlobString(image,buffer);
2189 }
2190 if (image->gamma != 0.0)
2191 {
cristy151b66d2015-04-15 10:50:31 +00002192 (void) FormatLocaleString(buffer,MagickPathExtent,"gamma=%g\n",
cristy3ed852e2009-09-05 21:47:34 +00002193 image->gamma);
2194 (void) WriteBlobString(image,buffer);
2195 }
2196 if (image->chromaticity.white_point.x != 0.0)
2197 {
2198 /*
2199 Note chomaticity points.
2200 */
cristy151b66d2015-04-15 10:50:31 +00002201 (void) FormatLocaleString(buffer,MagickPathExtent,"red-primary=%g,"
cristye7f51092010-01-17 00:39:37 +00002202 "%g green-primary=%g,%g blue-primary=%g,%g\n",
cristy3ed852e2009-09-05 21:47:34 +00002203 image->chromaticity.red_primary.x,image->chromaticity.red_primary.y,
2204 image->chromaticity.green_primary.x,
2205 image->chromaticity.green_primary.y,
2206 image->chromaticity.blue_primary.x,
2207 image->chromaticity.blue_primary.y);
2208 (void) WriteBlobString(image,buffer);
cristy151b66d2015-04-15 10:50:31 +00002209 (void) FormatLocaleString(buffer,MagickPathExtent,
cristye7f51092010-01-17 00:39:37 +00002210 "white-point=%g,%g\n",image->chromaticity.white_point.x,
cristy8cd5b312010-01-07 01:10:24 +00002211 image->chromaticity.white_point.y);
cristy3ed852e2009-09-05 21:47:34 +00002212 (void) WriteBlobString(image,buffer);
2213 }
2214 if (image->orientation != UndefinedOrientation)
2215 {
cristy151b66d2015-04-15 10:50:31 +00002216 (void) FormatLocaleString(buffer,MagickPathExtent,"orientation=%s\n",
cristy042ee782011-04-22 18:48:30 +00002217 CommandOptionToMnemonic(MagickOrientationOptions,image->orientation));
cristy3ed852e2009-09-05 21:47:34 +00002218 (void) WriteBlobString(image,buffer);
2219 }
2220 if (image->profiles != (void *) NULL)
2221 {
2222 const char
2223 *name;
2224
2225 const StringInfo
2226 *profile;
2227
2228 /*
2229 Write image profiles.
2230 */
2231 ResetImageProfileIterator(image);
2232 name=GetNextImageProfile(image);
2233 while (name != (const char *) NULL)
2234 {
2235 profile=GetImageProfile(image,name);
2236 if (profile != (StringInfo *) NULL)
2237 {
cristy151b66d2015-04-15 10:50:31 +00002238 (void) FormatLocaleString(buffer,MagickPathExtent,
cristye8c25f92010-06-03 00:53:06 +00002239 "profile:%s=%.20g\n",name,(double)
2240 GetStringInfoLength(profile));
cristy3ed852e2009-09-05 21:47:34 +00002241 (void) WriteBlobString(image,buffer);
2242 }
2243 name=GetNextImageProfile(image);
2244 }
2245 }
2246 if (image->montage != (char *) NULL)
2247 {
cristy151b66d2015-04-15 10:50:31 +00002248 (void) FormatLocaleString(buffer,MagickPathExtent,"montage=%s\n",
cristy3ed852e2009-09-05 21:47:34 +00002249 image->montage);
2250 (void) WriteBlobString(image,buffer);
2251 }
2252 if (quantum_info->format == FloatingPointQuantumFormat)
cristyd15e6592011-10-15 00:13:06 +00002253 (void) SetImageProperty(image,"quantum:format","floating-point",
2254 exception);
cristy3ed852e2009-09-05 21:47:34 +00002255 ResetImagePropertyIterator(image);
2256 property=GetNextImageProperty(image);
2257 while (property != (const char *) NULL)
2258 {
cristy151b66d2015-04-15 10:50:31 +00002259 (void) FormatLocaleString(buffer,MagickPathExtent,"%s=",property);
cristy3ed852e2009-09-05 21:47:34 +00002260 (void) WriteBlobString(image,buffer);
cristyd15e6592011-10-15 00:13:06 +00002261 value=GetImageProperty(image,property,exception);
cristy3ed852e2009-09-05 21:47:34 +00002262 if (value != (const char *) NULL)
2263 {
cristyd76ed5d2012-05-20 16:15:57 +00002264 size_t
2265 length;
2266
cristy0e75d752012-05-20 16:10:32 +00002267 length=strlen(value);
2268 for (i=0; i < (ssize_t) length; i++)
cristy3ed852e2009-09-05 21:47:34 +00002269 if (isspace((int) ((unsigned char) value[i])) != 0)
2270 break;
cristy04e917f2013-08-17 00:54:17 +00002271 if ((i == (ssize_t) length) && (i != 0))
cristy0e75d752012-05-20 16:10:32 +00002272 (void) WriteBlob(image,length,(const unsigned char *) value);
cristyb880ee82012-04-07 15:08:14 +00002273 else
2274 {
2275 (void) WriteBlobByte(image,'{');
cristy3d136bd2012-05-04 12:16:20 +00002276 if (strchr(value,'}') == (char *) NULL)
cristy0e75d752012-05-20 16:10:32 +00002277 (void) WriteBlob(image,length,(const unsigned char *) value);
cristy3d136bd2012-05-04 12:16:20 +00002278 else
cristy0e75d752012-05-20 16:10:32 +00002279 for (i=0; i < (ssize_t) length; i++)
cristy3d136bd2012-05-04 12:16:20 +00002280 {
2281 if (value[i] == (int) '}')
2282 (void) WriteBlobByte(image,'\\');
2283 (void) WriteBlobByte(image,value[i]);
2284 }
cristyb880ee82012-04-07 15:08:14 +00002285 (void) WriteBlobByte(image,'}');
2286 }
cristy3ed852e2009-09-05 21:47:34 +00002287 }
2288 (void) WriteBlobByte(image,'\n');
2289 property=GetNextImageProperty(image);
2290 }
2291 (void) WriteBlobString(image,"\f\n:\032");
2292 if (image->montage != (char *) NULL)
2293 {
2294 /*
2295 Write montage tile directory.
2296 */
2297 if (image->directory != (char *) NULL)
2298 (void) WriteBlob(image,strlen(image->directory),(unsigned char *)
2299 image->directory);
2300 (void) WriteBlobByte(image,'\0');
2301 }
2302 if (image->profiles != (void *) NULL)
2303 {
2304 const char
2305 *name;
2306
2307 const StringInfo
2308 *profile;
2309
2310 /*
2311 Generic profile.
2312 */
2313 ResetImageProfileIterator(image);
2314 name=GetNextImageProfile(image);
2315 while (name != (const char *) NULL)
2316 {
2317 profile=GetImageProfile(image,name);
2318 (void) WriteBlob(image,GetStringInfoLength(profile),
2319 GetStringInfoDatum(profile));
2320 name=GetNextImageProfile(image);
2321 }
2322 }
2323 if (image->storage_class == PseudoClass)
2324 {
2325 size_t
2326 packet_size;
2327
2328 unsigned char
2329 *colormap,
2330 *q;
2331
2332 /*
2333 Allocate colormap.
2334 */
2335 packet_size=(size_t) (3*quantum_info->depth/8);
2336 colormap=(unsigned char *) AcquireQuantumMemory(image->colors,
2337 packet_size*sizeof(*colormap));
2338 if (colormap == (unsigned char *) NULL)
2339 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2340 /*
2341 Write colormap to file.
2342 */
2343 q=colormap;
cristybb503372010-05-27 20:51:26 +00002344 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00002345 {
2346 switch (quantum_info->depth)
2347 {
2348 default:
2349 ThrowWriterException(CorruptImageError,"ImageDepthNotSupported");
2350 case 32:
2351 {
cristy4cb162a2010-05-30 03:04:47 +00002352 register unsigned int
cristy3ed852e2009-09-05 21:47:34 +00002353 pixel;
2354
2355 pixel=ScaleQuantumToLong(image->colormap[i].red);
2356 q=PopLongPixel(MSBEndian,pixel,q);
2357 pixel=ScaleQuantumToLong(image->colormap[i].green);
2358 q=PopLongPixel(MSBEndian,pixel,q);
2359 pixel=ScaleQuantumToLong(image->colormap[i].blue);
2360 q=PopLongPixel(MSBEndian,pixel,q);
2361 break;
2362 }
2363 case 16:
2364 {
2365 register unsigned short
2366 pixel;
2367
2368 pixel=ScaleQuantumToShort(image->colormap[i].red);
2369 q=PopShortPixel(MSBEndian,pixel,q);
2370 pixel=ScaleQuantumToShort(image->colormap[i].green);
2371 q=PopShortPixel(MSBEndian,pixel,q);
2372 pixel=ScaleQuantumToShort(image->colormap[i].blue);
2373 q=PopShortPixel(MSBEndian,pixel,q);
2374 break;
2375 }
2376 case 8:
2377 {
2378 register unsigned char
2379 pixel;
2380
2381 pixel=(unsigned char) ScaleQuantumToChar(image->colormap[i].red);
2382 q=PopCharPixel(pixel,q);
2383 pixel=(unsigned char) ScaleQuantumToChar(
2384 image->colormap[i].green);
2385 q=PopCharPixel(pixel,q);
2386 pixel=(unsigned char) ScaleQuantumToChar(image->colormap[i].blue);
2387 q=PopCharPixel(pixel,q);
2388 break;
2389 }
2390 }
2391 }
2392 (void) WriteBlob(image,packet_size*image->colors,colormap);
2393 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
2394 }
2395 /*
2396 Write image pixels to file.
2397 */
cristy330af6c2010-12-21 14:36:06 +00002398 status=MagickTrue;
2399 switch (compression)
2400 {
2401#if defined(MAGICKCORE_BZLIB_DELEGATE)
2402 case BZipCompression:
2403 {
cristy3a99dcf2011-12-17 01:29:40 +00002404 int
2405 code;
2406
cristy330af6c2010-12-21 14:36:06 +00002407 (void) ResetMagickMemory(&bzip_info,0,sizeof(bzip_info));
2408 bzip_info.bzalloc=AcquireBZIPMemory;
2409 bzip_info.bzfree=RelinquishBZIPMemory;
2410 code=BZ2_bzCompressInit(&bzip_info,(int) (image->quality ==
Cristy6ffac4f2015-08-23 10:55:57 -04002411 UndefinedCompressionQuality ? 7 : MagickMin(image->quality/10,9)),
2412 (int) image_info->verbose,0);
cristy330af6c2010-12-21 14:36:06 +00002413 if (code != BZ_OK)
2414 status=MagickFalse;
2415 break;
2416 }
2417#endif
2418#if defined(MAGICKCORE_LZMA_DELEGATE)
2419 case LZMACompression:
2420 {
cristy3a99dcf2011-12-17 01:29:40 +00002421 int
2422 code;
2423
cristy330af6c2010-12-21 14:36:06 +00002424 (void) ResetMagickMemory(&allocator,0,sizeof(allocator));
2425 allocator.alloc=AcquireLZMAMemory;
2426 allocator.free=RelinquishLZMAMemory;
cristy9d72f1a2010-12-21 20:46:59 +00002427 lzma_info=initialize_lzma;
cristy330af6c2010-12-21 14:36:06 +00002428 lzma_info.allocator=&allocator;
cristy9d72f1a2010-12-21 20:46:59 +00002429 code=lzma_easy_encoder(&lzma_info,image->quality/10,LZMA_CHECK_SHA256);
2430 if (code != LZMA_OK)
2431 status=MagickTrue;
cristy330af6c2010-12-21 14:36:06 +00002432 break;
2433 }
2434#endif
2435#if defined(MAGICKCORE_ZLIB_DELEGATE)
2436 case LZWCompression:
2437 case ZipCompression:
2438 {
cristy3a99dcf2011-12-17 01:29:40 +00002439 int
2440 code;
2441
cristy330af6c2010-12-21 14:36:06 +00002442 (void) ResetMagickMemory(&zip_info,0,sizeof(zip_info));
2443 zip_info.zalloc=AcquireZIPMemory;
2444 zip_info.zfree=RelinquishZIPMemory;
2445 code=deflateInit(&zip_info,(int) (image->quality ==
2446 UndefinedCompressionQuality ? 7 : MagickMin(image->quality/10,9)));
2447 if (code != Z_OK)
2448 status=MagickFalse;
2449 break;
2450 }
2451#endif
2452 default:
2453 break;
2454 }
cristy1e178e72011-08-28 19:44:34 +00002455 quantum_type=GetQuantumType(image,exception);
cristyb3f97ae2015-05-18 12:29:32 +00002456 pixels=(unsigned char *) GetQuantumPixels(quantum_info);
cristybb503372010-05-27 20:51:26 +00002457 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002458 {
cristy4c08aed2011-07-01 19:47:50 +00002459 register const Quantum
dirk05d2ff72015-11-18 23:13:43 +01002460 *magick_restrict p;
cristy3ed852e2009-09-05 21:47:34 +00002461
cristybb503372010-05-27 20:51:26 +00002462 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002463 x;
2464
cristy330af6c2010-12-21 14:36:06 +00002465 if (status == MagickFalse)
2466 break;
cristy1e178e72011-08-28 19:44:34 +00002467 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00002468 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002469 break;
cristy3ed852e2009-09-05 21:47:34 +00002470 q=pixels;
2471 switch (compression)
2472 {
cristy3ed852e2009-09-05 21:47:34 +00002473#if defined(MAGICKCORE_BZLIB_DELEGATE)
2474 case BZipCompression:
2475 {
cristy3ed852e2009-09-05 21:47:34 +00002476 bzip_info.next_in=(char *) pixels;
2477 bzip_info.avail_in=(unsigned int) (packet_size*image->columns);
cristydeec0c22013-04-18 14:46:41 +00002478 (void) ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2479 quantum_type,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00002480 do
2481 {
cristy3a99dcf2011-12-17 01:29:40 +00002482 int
2483 code;
2484
cristy3ed852e2009-09-05 21:47:34 +00002485 bzip_info.next_out=(char *) compress_pixels;
2486 bzip_info.avail_out=(unsigned int) BZipMaxExtent(packet_size*
2487 image->columns);
2488 code=BZ2_bzCompress(&bzip_info,BZ_FLUSH);
cristy26377172010-12-20 19:01:58 +00002489 if (code != BZ_OK)
2490 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00002491 length=(size_t) (bzip_info.next_out-(char *) compress_pixels);
2492 if (length != 0)
2493 {
2494 (void) WriteBlobMSBLong(image,(unsigned int) length);
2495 (void) WriteBlob(image,length,compress_pixels);
2496 }
2497 } while (bzip_info.avail_in != 0);
cristy26377172010-12-20 19:01:58 +00002498 break;
2499 }
2500#endif
2501#if defined(MAGICKCORE_LZMA_DELEGATE)
2502 case LZMACompression:
2503 {
cristy26377172010-12-20 19:01:58 +00002504 lzma_info.next_in=pixels;
cristy330af6c2010-12-21 14:36:06 +00002505 lzma_info.avail_in=packet_size*image->columns;
cristydeec0c22013-04-18 14:46:41 +00002506 (void) ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2507 quantum_type,pixels,exception);
cristy26377172010-12-20 19:01:58 +00002508 do
2509 {
cristy3a99dcf2011-12-17 01:29:40 +00002510 int
2511 code;
2512
cristy26377172010-12-20 19:01:58 +00002513 lzma_info.next_out=compress_pixels;
2514 lzma_info.avail_out=packet_size*image->columns;
2515 code=lzma_code(&lzma_info,LZMA_RUN);
2516 if (code != LZMA_OK)
2517 status=MagickFalse;
2518 length=(size_t) (lzma_info.next_out-compress_pixels);
2519 if (length != 0)
2520 {
2521 (void) WriteBlobMSBLong(image,(unsigned int) length);
2522 (void) WriteBlob(image,length,compress_pixels);
2523 }
2524 } while (lzma_info.avail_in != 0);
cristy26377172010-12-20 19:01:58 +00002525 break;
2526 }
2527#endif
2528#if defined(MAGICKCORE_ZLIB_DELEGATE)
2529 case LZWCompression:
2530 case ZipCompression:
2531 {
cristy26377172010-12-20 19:01:58 +00002532 zip_info.next_in=pixels;
2533 zip_info.avail_in=(uInt) (packet_size*image->columns);
cristydeec0c22013-04-18 14:46:41 +00002534 (void) ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2535 quantum_type,pixels,exception);
cristy26377172010-12-20 19:01:58 +00002536 do
2537 {
cristy3a99dcf2011-12-17 01:29:40 +00002538 int
2539 code;
2540
cristy26377172010-12-20 19:01:58 +00002541 zip_info.next_out=compress_pixels;
2542 zip_info.avail_out=(uInt) ZipMaxExtent(packet_size*image->columns);
2543 code=deflate(&zip_info,Z_SYNC_FLUSH);
2544 if (code != Z_OK)
2545 status=MagickFalse;
2546 length=(size_t) (zip_info.next_out-compress_pixels);
2547 if (length != 0)
2548 {
2549 (void) WriteBlobMSBLong(image,(unsigned int) length);
2550 (void) WriteBlob(image,length,compress_pixels);
2551 }
2552 } while (zip_info.avail_in != 0);
cristy3ed852e2009-09-05 21:47:34 +00002553 break;
2554 }
2555#endif
2556 case RLECompression:
2557 {
cristy4c08aed2011-07-01 19:47:50 +00002558 length=0;
cristy803640d2011-11-17 02:11:32 +00002559 GetPixelInfoPixel(image,p,&pixel);
cristyed231572011-07-14 02:18:59 +00002560 p+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00002561 for (x=1; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002562 {
cristy803640d2011-11-17 02:11:32 +00002563 GetPixelInfoPixel(image,p,&target);
cristy4c08aed2011-07-01 19:47:50 +00002564 if ((length < 255) &&
2565 (IsPixelInfoEquivalent(&pixel,&target) != MagickFalse))
cristy3ed852e2009-09-05 21:47:34 +00002566 length++;
2567 else
2568 {
cristyc82a27b2011-10-21 01:07:16 +00002569 q=PopRunlengthPacket(image,q,length,&pixel,exception);
cristy3ed852e2009-09-05 21:47:34 +00002570 length=0;
2571 }
cristy803640d2011-11-17 02:11:32 +00002572 GetPixelInfoPixel(image,p,&pixel);
cristyed231572011-07-14 02:18:59 +00002573 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00002574 }
cristyc82a27b2011-10-21 01:07:16 +00002575 q=PopRunlengthPacket(image,q,length,&pixel,exception);
cristy3ed852e2009-09-05 21:47:34 +00002576 (void) WriteBlob(image,(size_t) (q-pixels),pixels);
2577 break;
2578 }
2579 default:
2580 {
cristydeec0c22013-04-18 14:46:41 +00002581 (void) ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2582 quantum_type,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00002583 (void) WriteBlob(image,packet_size*image->columns,pixels);
2584 break;
2585 }
2586 }
cristycee97112010-05-28 00:44:52 +00002587 if (image->previous == (Image *) NULL)
2588 {
2589 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2590 image->rows);
2591 if (status == MagickFalse)
2592 break;
2593 }
cristy3ed852e2009-09-05 21:47:34 +00002594 }
cristy330af6c2010-12-21 14:36:06 +00002595 switch (compression)
2596 {
2597#if defined(MAGICKCORE_BZLIB_DELEGATE)
2598 case BZipCompression:
2599 {
cristy3a99dcf2011-12-17 01:29:40 +00002600 int
2601 code;
2602
cristy330af6c2010-12-21 14:36:06 +00002603 for ( ; ; )
2604 {
2605 if (status == MagickFalse)
2606 break;
2607 bzip_info.next_out=(char *) compress_pixels;
2608 bzip_info.avail_out=(unsigned int) BZipMaxExtent(packet_size*
2609 image->columns);
2610 code=BZ2_bzCompress(&bzip_info,BZ_FINISH);
cristy330af6c2010-12-21 14:36:06 +00002611 length=(size_t) (bzip_info.next_out-(char *) compress_pixels);
2612 if (length != 0)
2613 {
2614 (void) WriteBlobMSBLong(image,(unsigned int) length);
2615 (void) WriteBlob(image,length,compress_pixels);
2616 }
2617 if (code == BZ_STREAM_END)
2618 break;
2619 }
2620 code=BZ2_bzCompressEnd(&bzip_info);
2621 if (code != BZ_OK)
2622 status=MagickFalse;
2623 break;
2624 }
2625#endif
2626#if defined(MAGICKCORE_LZMA_DELEGATE)
2627 case LZMACompression:
2628 {
cristy3a99dcf2011-12-17 01:29:40 +00002629 int
2630 code;
2631
cristy330af6c2010-12-21 14:36:06 +00002632 for ( ; ; )
2633 {
2634 if (status == MagickFalse)
2635 break;
2636 lzma_info.next_out=compress_pixels;
2637 lzma_info.avail_out=packet_size*image->columns;
2638 code=lzma_code(&lzma_info,LZMA_FINISH);
cristy330af6c2010-12-21 14:36:06 +00002639 length=(size_t) (lzma_info.next_out-compress_pixels);
2640 if (length > 6)
2641 {
2642 (void) WriteBlobMSBLong(image,(unsigned int) length);
2643 (void) WriteBlob(image,length,compress_pixels);
2644 }
2645 if (code == LZMA_STREAM_END)
2646 break;
2647 }
2648 lzma_end(&lzma_info);
2649 break;
2650 }
2651#endif
2652#if defined(MAGICKCORE_ZLIB_DELEGATE)
2653 case LZWCompression:
2654 case ZipCompression:
2655 {
cristy3a99dcf2011-12-17 01:29:40 +00002656 int
2657 code;
2658
cristy330af6c2010-12-21 14:36:06 +00002659 for ( ; ; )
2660 {
2661 if (status == MagickFalse)
2662 break;
2663 zip_info.next_out=compress_pixels;
cristyc3ca81b2011-10-17 18:05:54 +00002664 zip_info.avail_out=(uInt) ZipMaxExtent(packet_size*image->columns);
cristy330af6c2010-12-21 14:36:06 +00002665 code=deflate(&zip_info,Z_FINISH);
cristy330af6c2010-12-21 14:36:06 +00002666 length=(size_t) (zip_info.next_out-compress_pixels);
2667 if (length > 6)
2668 {
2669 (void) WriteBlobMSBLong(image,(unsigned int) length);
2670 (void) WriteBlob(image,length,compress_pixels);
2671 }
2672 if (code == Z_STREAM_END)
2673 break;
2674 }
2675 code=deflateEnd(&zip_info);
2676 if (code != Z_OK)
2677 status=MagickFalse;
2678 break;
2679 }
2680#endif
2681 default:
2682 break;
2683 }
cristy3ed852e2009-09-05 21:47:34 +00002684 quantum_info=DestroyQuantumInfo(quantum_info);
2685 compress_pixels=(unsigned char *) RelinquishMagickMemory(compress_pixels);
2686 if (GetNextImageInList(image) == (Image *) NULL)
2687 break;
2688 image=SyncNextImageInList(image);
Cristy6ffac4f2015-08-23 10:55:57 -04002689 status=SetImageProgress(image,SaveImagesTag,scene++,GetImageListLength(
2690 image));
cristy3ed852e2009-09-05 21:47:34 +00002691 if (status == MagickFalse)
2692 break;
2693 } while (image_info->adjoin != MagickFalse);
2694 (void) CloseBlob(image);
2695 return(status);
2696}