blob: bbde89987ff3bad39a7af74d58e3c0fcc4d745a7 [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 %
16% John Cristy %
17% July 1992 %
18% %
19% %
cristy7e41fe82010-12-04 23:12:08 +000020% Copyright 1999-2011 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"
52#include "MagickCore/constitute.h"
53#include "MagickCore/exception.h"
54#include "MagickCore/exception-private.h"
55#include "MagickCore/hashmap.h"
56#include "MagickCore/geometry.h"
57#include "MagickCore/image.h"
58#include "MagickCore/image-private.h"
59#include "MagickCore/list.h"
60#include "MagickCore/magick.h"
61#include "MagickCore/memory_.h"
62#include "MagickCore/module.h"
63#include "MagickCore/monitor.h"
64#include "MagickCore/monitor-private.h"
65#include "MagickCore/option.h"
66#include "MagickCore/pixel.h"
67#include "MagickCore/pixel-accessor.h"
68#include "MagickCore/profile.h"
69#include "MagickCore/property.h"
70#include "MagickCore/quantum-private.h"
71#include "MagickCore/static.h"
72#include "MagickCore/statistic.h"
73#include "MagickCore/string_.h"
74#include "MagickCore/string-private.h"
cristy3ed852e2009-09-05 21:47:34 +000075#if defined(MAGICKCORE_BZLIB_DELEGATE)
76#include "bzlib.h"
77#endif
cristy26377172010-12-20 19:01:58 +000078#if defined(MAGICKCORE_LZMA_DELEGATE)
79#include "lzma.h"
80#endif
81#if defined(MAGICKCORE_ZLIB_DELEGATE)
82#include "zlib.h"
83#endif
cristy3ed852e2009-09-05 21:47:34 +000084
85/*
cristya7a341e2010-12-22 20:28:51 +000086 Define declarations.
87*/
88#if !defined(LZMA_OK)
89#define LZMA_OK 0
90#endif
91
92/*
cristy3ed852e2009-09-05 21:47:34 +000093 Forward declarations.
94*/
95static MagickBooleanType
cristy1e178e72011-08-28 19:44:34 +000096 WriteMIFFImage(const ImageInfo *,Image *,ExceptionInfo *);
cristy3ed852e2009-09-05 21:47:34 +000097
98/*
99%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
100% %
101% %
102% %
103% I s M I F F %
104% %
105% %
106% %
107%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
108%
109% IsMIFF() returns MagickTrue if the image format type, identified by the
110% magick string, is MIFF.
111%
112% The format of the IsMIFF method is:
113%
114% MagickBooleanType IsMIFF(const unsigned char *magick,const size_t length)
115%
116% A description of each parameter follows:
117%
118% o magick: compare image format pattern against these bytes.
119%
120% o length: Specifies the length of the magick string.
121%
122*/
123static MagickBooleanType IsMIFF(const unsigned char *magick,const size_t length)
124{
125 if (length < 14)
126 return(MagickFalse);
127 if (LocaleNCompare((const char *) magick,"id=ImageMagick",14) == 0)
128 return(MagickTrue);
129 return(MagickFalse);
130}
131
132/*
133%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
134% %
135% %
136% %
137% R e a d M I F F I m a g e %
138% %
139% %
140% %
141%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
142%
143% ReadMIFFImage() reads a MIFF image file and returns it. It allocates the
144% memory necessary for the new Image structure and returns a pointer to the
145% new image.
146%
147% The format of the ReadMIFFImage method is:
148%
149% Image *ReadMIFFImage(const ImageInfo *image_info,
150% ExceptionInfo *exception)
151%
152% Decompression code contributed by Kyle Shorter.
153%
154% A description of each parameter follows:
155%
156% o image_info: the image info.
157%
158% o exception: return any errors or warnings in this structure.
159%
160*/
161
162#if defined(MAGICKCORE_BZLIB_DELEGATE)
163static void *AcquireBZIPMemory(void *context,int items,int size)
164{
165 (void) context;
166 return((void *) AcquireQuantumMemory((size_t) items,(size_t) size));
167}
168#endif
169
cristy4b46dba2010-12-20 19:18:20 +0000170#if defined(MAGICKCORE_LZMA_DELEGATE)
171static void *AcquireLZMAMemory(void *context,size_t items,size_t size)
172{
173 (void) context;
174 return((void *) AcquireQuantumMemory((size_t) items,(size_t) size));
175}
176#endif
177
cristy3ed852e2009-09-05 21:47:34 +0000178#if defined(MAGICKCORE_ZLIB_DELEGATE)
179static voidpf AcquireZIPMemory(voidpf context,unsigned int items,
180 unsigned int size)
181{
182 (void) context;
183 return((voidpf) AcquireQuantumMemory(items,size));
184}
185#endif
186
187static inline size_t MagickMax(const size_t x,const size_t y)
188{
189 if (x > y)
190 return(x);
191 return(y);
192}
193
194static inline size_t MagickMin(const size_t x,const size_t y)
195{
196 if (x < y)
197 return(x);
198 return(y);
199}
200
201static void PushRunlengthPacket(Image *image,const unsigned char *pixels,
cristy4c08aed2011-07-01 19:47:50 +0000202 size_t *length,PixelInfo *pixel)
cristy3ed852e2009-09-05 21:47:34 +0000203{
204 const unsigned char
205 *p;
206
207 p=pixels;
208 if (image->storage_class == PseudoClass)
209 {
cristy4c08aed2011-07-01 19:47:50 +0000210 pixel->index=0;
cristy3ed852e2009-09-05 21:47:34 +0000211 switch (image->depth)
212 {
213 case 32:
214 {
cristy4c08aed2011-07-01 19:47:50 +0000215 pixel->index=ConstrainColormapIndex(image,
cristy3ed852e2009-09-05 21:47:34 +0000216 (*p << 24) | (*(p+1) << 16) | (*(p+2) << 8) | *(p+3));
217 p+=4;
218 break;
219 }
220 case 16:
221 {
cristy4c08aed2011-07-01 19:47:50 +0000222 pixel->index=ConstrainColormapIndex(image,(*p << 8) | *(p+1));
cristy3ed852e2009-09-05 21:47:34 +0000223 p+=2;
224 break;
225 }
226 case 8:
227 {
cristy4c08aed2011-07-01 19:47:50 +0000228 pixel->index=ConstrainColormapIndex(image,*p);
cristy3ed852e2009-09-05 21:47:34 +0000229 p++;
230 break;
231 }
232 default:
233 (void) ThrowMagickException(&image->exception,GetMagickModule(),
234 CorruptImageError,"ImageDepthNotSupported","`%s'",image->filename);
235 }
cristy3ed852e2009-09-05 21:47:34 +0000236 switch (image->depth)
237 {
238 case 8:
239 {
240 unsigned char
241 quantum;
242
243 if (image->matte != MagickFalse)
244 {
245 p=PushCharPixel(p,&quantum);
cristy4c08aed2011-07-01 19:47:50 +0000246 pixel->alpha=ScaleCharToQuantum(quantum);
cristy3ed852e2009-09-05 21:47:34 +0000247 }
248 break;
249 }
250 case 16:
251 {
252 unsigned short
253 quantum;
254
255 if (image->matte != MagickFalse)
256 {
257 p=PushShortPixel(MSBEndian,p,&quantum);
cristy4c08aed2011-07-01 19:47:50 +0000258 pixel->alpha=(Quantum) (quantum >> (image->depth-
cristy3ed852e2009-09-05 21:47:34 +0000259 MAGICKCORE_QUANTUM_DEPTH));
260 }
261 break;
262 }
263 case 32:
264 {
cristy4cb162a2010-05-30 03:04:47 +0000265 unsigned int
cristy3ed852e2009-09-05 21:47:34 +0000266 quantum;
267
268 if (image->matte != MagickFalse)
269 {
270 p=PushLongPixel(MSBEndian,p,&quantum);
cristy4c08aed2011-07-01 19:47:50 +0000271 pixel->alpha=(Quantum) (quantum >> (image->depth-
cristy3ed852e2009-09-05 21:47:34 +0000272 MAGICKCORE_QUANTUM_DEPTH));
273 }
274 break;
275 }
276 default:
277 (void) ThrowMagickException(&image->exception,GetMagickModule(),
278 CorruptImageError,"ImageDepthNotSupported","`%s'",image->filename);
279 }
280 *length=(size_t) (*p++)+1;
281 return;
282 }
283 switch (image->depth)
284 {
285 case 8:
286 {
287 unsigned char
288 quantum;
289
290 p=PushCharPixel(p,&quantum);
cristy4c08aed2011-07-01 19:47:50 +0000291 pixel->red=ScaleCharToQuantum(quantum);
cristy3ed852e2009-09-05 21:47:34 +0000292 p=PushCharPixel(p,&quantum);
cristy4c08aed2011-07-01 19:47:50 +0000293 pixel->green=ScaleCharToQuantum(quantum);
cristy3ed852e2009-09-05 21:47:34 +0000294 p=PushCharPixel(p,&quantum);
cristy4c08aed2011-07-01 19:47:50 +0000295 pixel->blue=ScaleCharToQuantum(quantum);
cristy3ed852e2009-09-05 21:47:34 +0000296 if (image->colorspace == CMYKColorspace)
297 {
298 p=PushCharPixel(p,&quantum);
cristy4c08aed2011-07-01 19:47:50 +0000299 pixel->black=ScaleCharToQuantum(quantum);
300 }
301 if (image->matte != MagickFalse)
302 {
303 p=PushCharPixel(p,&quantum);
304 pixel->alpha=ScaleCharToQuantum(quantum);
cristy3ed852e2009-09-05 21:47:34 +0000305 }
306 break;
307 }
308 case 16:
309 {
310 unsigned short
311 quantum;
312
313 p=PushShortPixel(MSBEndian,p,&quantum);
cristy4c08aed2011-07-01 19:47:50 +0000314 pixel->red=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
cristy3ed852e2009-09-05 21:47:34 +0000315 p=PushShortPixel(MSBEndian,p,&quantum);
cristy4c08aed2011-07-01 19:47:50 +0000316 pixel->green=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
cristyfbbafc92011-05-05 01:22:11 +0000317 p=PushShortPixel(MSBEndian,p,&quantum);
cristy4c08aed2011-07-01 19:47:50 +0000318 pixel->blue=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
cristy3ed852e2009-09-05 21:47:34 +0000319 if (image->colorspace == CMYKColorspace)
320 {
321 p=PushShortPixel(MSBEndian,p,&quantum);
cristy4c08aed2011-07-01 19:47:50 +0000322 pixel->black=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
323 }
324 if (image->matte != MagickFalse)
325 {
326 p=PushShortPixel(MSBEndian,p,&quantum);
327 pixel->alpha=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
cristy3ed852e2009-09-05 21:47:34 +0000328 }
329 break;
330 }
331 case 32:
332 {
cristy4cb162a2010-05-30 03:04:47 +0000333 unsigned int
cristy3ed852e2009-09-05 21:47:34 +0000334 quantum;
335
336 p=PushLongPixel(MSBEndian,p,&quantum);
cristy4c08aed2011-07-01 19:47:50 +0000337 pixel->red=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
cristy3ed852e2009-09-05 21:47:34 +0000338 p=PushLongPixel(MSBEndian,p,&quantum);
cristy4c08aed2011-07-01 19:47:50 +0000339 pixel->green=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
cristyfbbafc92011-05-05 01:22:11 +0000340 p=PushLongPixel(MSBEndian,p,&quantum);
cristy4c08aed2011-07-01 19:47:50 +0000341 pixel->blue=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
cristy3ed852e2009-09-05 21:47:34 +0000342 if (image->colorspace == CMYKColorspace)
343 {
344 p=PushLongPixel(MSBEndian,p,&quantum);
cristy4c08aed2011-07-01 19:47:50 +0000345 pixel->black=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
346 }
347 if (image->matte != MagickFalse)
348 {
349 p=PushLongPixel(MSBEndian,p,&quantum);
350 pixel->alpha=quantum >> (image->depth-MAGICKCORE_QUANTUM_DEPTH);
cristy3ed852e2009-09-05 21:47:34 +0000351 }
352 break;
353 }
354 default:
355 (void) ThrowMagickException(&image->exception,GetMagickModule(),
356 CorruptImageError,"ImageDepthNotSupported","`%s'",image->filename);
357 }
358 *length=(size_t) (*p++)+1;
359}
360
cristy4b46dba2010-12-20 19:18:20 +0000361#if defined(MAGICKCORE_BZLIB_DELEGATE)
362static void RelinquishBZIPMemory(void *context,void *memory)
cristy3ed852e2009-09-05 21:47:34 +0000363{
364 (void) context;
365 memory=RelinquishMagickMemory(memory);
366}
367#endif
368
cristy4b46dba2010-12-20 19:18:20 +0000369#if defined(MAGICKCORE_LZMA_DELEGATE)
370static void RelinquishLZMAMemory(void *context,void *memory)
371{
372 (void) context;
373 memory=RelinquishMagickMemory(memory);
374}
375#endif
376
377#if defined(MAGICKCORE_ZLIB_DELEGATE)
378static void RelinquishZIPMemory(voidpf context,voidpf memory)
cristy3ed852e2009-09-05 21:47:34 +0000379{
380 (void) context;
381 memory=RelinquishMagickMemory(memory);
382}
383#endif
384
385static Image *ReadMIFFImage(const ImageInfo *image_info,
386 ExceptionInfo *exception)
387{
388#define BZipMaxExtent(x) ((x)+((x)/100)+600)
cristy26377172010-12-20 19:01:58 +0000389#define LZMAMaxExtent(x) ((x)+((x)/3)+128)
cristy3ed852e2009-09-05 21:47:34 +0000390#define ZipMaxExtent(x) ((x)+(((x)+7) >> 3)+(((x)+63) >> 6)+11)
391
392#if defined(MAGICKCORE_BZLIB_DELEGATE)
393 bz_stream
394 bzip_info;
395#endif
396
397 char
398 id[MaxTextExtent],
399 keyword[MaxTextExtent],
400 *options;
401
402 const unsigned char
403 *p;
404
405 double
406 version;
407
408 GeometryInfo
409 geometry_info;
410
411 Image
412 *image;
413
cristy3ed852e2009-09-05 21:47:34 +0000414 int
415 c,
416 code;
417
cristy26377172010-12-20 19:01:58 +0000418#if defined(MAGICKCORE_LZMA_DELEGATE)
419 lzma_stream
cristy330af6c2010-12-21 14:36:06 +0000420 initialize_lzma = LZMA_STREAM_INIT,
421 lzma_info;
cristy4b46dba2010-12-20 19:18:20 +0000422
423 lzma_allocator
424 allocator;
cristy26377172010-12-20 19:01:58 +0000425#endif
426
cristy3ed852e2009-09-05 21:47:34 +0000427 LinkedListInfo
428 *profiles;
429
cristy3ed852e2009-09-05 21:47:34 +0000430 MagickBooleanType
431 status;
432
cristy4c08aed2011-07-01 19:47:50 +0000433 PixelInfo
434 pixel;
435
cristy3ed852e2009-09-05 21:47:34 +0000436 MagickStatusType
437 flags;
438
cristy3ed852e2009-09-05 21:47:34 +0000439 QuantumFormatType
440 quantum_format;
441
442 QuantumInfo
443 *quantum_info;
444
445 QuantumType
446 quantum_type;
447
cristybb503372010-05-27 20:51:26 +0000448 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000449 i;
450
451 size_t
452 length,
453 packet_size;
454
455 ssize_t
456 count;
457
458 unsigned char
459 *compress_pixels,
460 *pixels;
461
cristybb503372010-05-27 20:51:26 +0000462 size_t
cristy3ed852e2009-09-05 21:47:34 +0000463 colors;
464
cristy26377172010-12-20 19:01:58 +0000465 ssize_t
466 y;
467
cristy3ed852e2009-09-05 21:47:34 +0000468#if defined(MAGICKCORE_ZLIB_DELEGATE)
469 z_stream
470 zip_info;
471#endif
472
473 /*
474 Open image file.
475 */
476 assert(image_info != (const ImageInfo *) NULL);
477 assert(image_info->signature == MagickSignature);
478 if (image_info->debug != MagickFalse)
479 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
480 image_info->filename);
481 assert(exception != (ExceptionInfo *) NULL);
482 assert(exception->signature == MagickSignature);
483 image=AcquireImage(image_info);
484 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
485 if (status == MagickFalse)
486 {
487 image=DestroyImageList(image);
488 return((Image *) NULL);
489 }
490 /*
491 Decode image header; header terminates one character beyond a ':'.
492 */
493 c=ReadBlobByte(image);
494 if (c == EOF)
495 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
496 code=0;
497 *id='\0';
498 (void) ResetMagickMemory(keyword,0,sizeof(keyword));
499 version=0.0;
500 do
501 {
502 /*
503 Decode image header; header terminates one character beyond a ':'.
504 */
505 length=MaxTextExtent;
506 options=AcquireString((char *) NULL);
507 quantum_format=UndefinedQuantumFormat;
508 profiles=(LinkedListInfo *) NULL;
509 colors=0;
510 image->depth=8UL;
511 image->compression=NoCompression;
512 while ((isgraph(c) != MagickFalse) && (c != (int) ':'))
513 {
514 register char
515 *p;
516
517 if (c == (int) '{')
518 {
519 char
520 *comment;
521
522 /*
523 Read comment-- any text between { }.
524 */
525 length=MaxTextExtent;
526 comment=AcquireString((char *) NULL);
527 for (p=comment; comment != (char *) NULL; p++)
528 {
529 c=ReadBlobByte(image);
530 if ((c == EOF) || (c == (int) '}'))
531 break;
532 if ((size_t) (p-comment+1) >= length)
533 {
534 *p='\0';
535 length<<=1;
536 comment=(char *) ResizeQuantumMemory(comment,length+
537 MaxTextExtent,sizeof(*comment));
538 if (comment == (char *) NULL)
539 break;
540 p=comment+strlen(comment);
541 }
542 *p=(char) c;
543 }
544 if (comment == (char *) NULL)
545 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
546 *p='\0';
547 (void) SetImageProperty(image,"comment",comment);
548 comment=DestroyString(comment);
549 c=ReadBlobByte(image);
550 }
551 else
552 if (isalnum(c) != MagickFalse)
553 {
554 /*
555 Get the keyword.
556 */
557 p=keyword;
558 do
559 {
cristy3ed852e2009-09-05 21:47:34 +0000560 if (c == (int) '=')
561 break;
562 if ((size_t) (p-keyword) < (MaxTextExtent-1))
563 *p++=(char) c;
564 c=ReadBlobByte(image);
565 } while (c != EOF);
566 *p='\0';
567 p=options;
cristy93505cf2010-08-10 21:37:49 +0000568 while ((isspace((int) ((unsigned char) c)) != 0) && (c != EOF))
cristy3ed852e2009-09-05 21:47:34 +0000569 c=ReadBlobByte(image);
570 if (c == (int) '=')
571 {
572 /*
573 Get the keyword value.
574 */
575 c=ReadBlobByte(image);
576 while ((c != (int) '}') && (c != EOF))
577 {
578 if ((size_t) (p-options+1) >= length)
579 {
580 *p='\0';
581 length<<=1;
582 options=(char *) ResizeQuantumMemory(options,length+
583 MaxTextExtent,sizeof(*options));
584 if (options == (char *) NULL)
585 break;
586 p=options+strlen(options);
587 }
588 if (options == (char *) NULL)
589 ThrowReaderException(ResourceLimitError,
590 "MemoryAllocationFailed");
591 *p++=(char) c;
592 c=ReadBlobByte(image);
593 if (*options != '{')
594 if (isspace((int) ((unsigned char) c)) != 0)
595 break;
596 }
597 }
598 *p='\0';
599 if (*options == '{')
600 (void) CopyMagickString(options,options+1,MaxTextExtent);
601 /*
602 Assign a value to the specified keyword.
603 */
604 switch (*keyword)
605 {
606 case 'b':
607 case 'B':
608 {
609 if (LocaleCompare(keyword,"background-color") == 0)
610 {
611 (void) QueryColorDatabase(options,&image->background_color,
612 exception);
613 break;
614 }
615 if (LocaleCompare(keyword,"blue-primary") == 0)
616 {
617 flags=ParseGeometry(options,&geometry_info);
618 image->chromaticity.blue_primary.x=geometry_info.rho;
619 image->chromaticity.blue_primary.y=geometry_info.sigma;
620 if ((flags & SigmaValue) == 0)
621 image->chromaticity.blue_primary.y=
622 image->chromaticity.blue_primary.x;
623 break;
624 }
625 if (LocaleCompare(keyword,"border-color") == 0)
626 {
627 (void) QueryColorDatabase(options,&image->border_color,
628 exception);
629 break;
630 }
631 (void) SetImageProperty(image,keyword,options);
632 break;
633 }
634 case 'c':
635 case 'C':
636 {
637 if (LocaleCompare(keyword,"class") == 0)
638 {
cristybb503372010-05-27 20:51:26 +0000639 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000640 storage_class;
641
cristy042ee782011-04-22 18:48:30 +0000642 storage_class=ParseCommandOption(MagickClassOptions,
cristy3ed852e2009-09-05 21:47:34 +0000643 MagickFalse,options);
644 if (storage_class < 0)
645 break;
646 image->storage_class=(ClassType) storage_class;
647 break;
648 }
649 if (LocaleCompare(keyword,"colors") == 0)
650 {
cristye27293e2009-12-18 02:53:20 +0000651 colors=StringToUnsignedLong(options);
cristy3ed852e2009-09-05 21:47:34 +0000652 break;
653 }
654 if (LocaleCompare(keyword,"colorspace") == 0)
655 {
cristybb503372010-05-27 20:51:26 +0000656 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000657 colorspace;
658
cristy042ee782011-04-22 18:48:30 +0000659 colorspace=ParseCommandOption(MagickColorspaceOptions,
cristy3ed852e2009-09-05 21:47:34 +0000660 MagickFalse,options);
661 if (colorspace < 0)
662 break;
663 image->colorspace=(ColorspaceType) colorspace;
664 break;
665 }
666 if (LocaleCompare(keyword,"compression") == 0)
667 {
cristybb503372010-05-27 20:51:26 +0000668 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000669 compression;
670
cristy042ee782011-04-22 18:48:30 +0000671 compression=ParseCommandOption(MagickCompressOptions,
cristy3ed852e2009-09-05 21:47:34 +0000672 MagickFalse,options);
673 if (compression < 0)
674 break;
675 image->compression=(CompressionType) compression;
676 break;
677 }
678 if (LocaleCompare(keyword,"columns") == 0)
679 {
cristye27293e2009-12-18 02:53:20 +0000680 image->columns=StringToUnsignedLong(options);
cristy3ed852e2009-09-05 21:47:34 +0000681 break;
682 }
683 (void) SetImageProperty(image,keyword,options);
684 break;
685 }
686 case 'd':
687 case 'D':
688 {
689 if (LocaleCompare(keyword,"delay") == 0)
690 {
cristye27293e2009-12-18 02:53:20 +0000691 image->delay=StringToUnsignedLong(options);
cristy3ed852e2009-09-05 21:47:34 +0000692 break;
693 }
694 if (LocaleCompare(keyword,"depth") == 0)
695 {
cristye27293e2009-12-18 02:53:20 +0000696 image->depth=StringToUnsignedLong(options);
cristy3ed852e2009-09-05 21:47:34 +0000697 break;
698 }
699 if (LocaleCompare(keyword,"dispose") == 0)
700 {
cristybb503372010-05-27 20:51:26 +0000701 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000702 dispose;
703
cristy042ee782011-04-22 18:48:30 +0000704 dispose=ParseCommandOption(MagickDisposeOptions,MagickFalse,
cristy3ed852e2009-09-05 21:47:34 +0000705 options);
706 if (dispose < 0)
707 break;
708 image->dispose=(DisposeType) dispose;
709 break;
710 }
711 (void) SetImageProperty(image,keyword,options);
712 break;
713 }
714 case 'e':
715 case 'E':
716 {
717 if (LocaleCompare(keyword,"endian") == 0)
718 {
cristybb503372010-05-27 20:51:26 +0000719 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000720 endian;
721
cristy042ee782011-04-22 18:48:30 +0000722 endian=ParseCommandOption(MagickEndianOptions,MagickFalse,
cristy3ed852e2009-09-05 21:47:34 +0000723 options);
724 if (endian < 0)
725 break;
726 image->endian=(EndianType) endian;
727 break;
728 }
729 (void) SetImageProperty(image,keyword,options);
730 break;
731 }
732 case 'g':
733 case 'G':
734 {
735 if (LocaleCompare(keyword,"gamma") == 0)
736 {
cristyc1acd842011-05-19 23:05:47 +0000737 image->gamma=InterpretLocaleValue(options,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000738 break;
739 }
740 if (LocaleCompare(keyword,"gravity") == 0)
741 {
cristybb503372010-05-27 20:51:26 +0000742 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000743 gravity;
744
cristy042ee782011-04-22 18:48:30 +0000745 gravity=ParseCommandOption(MagickGravityOptions,MagickFalse,
cristy3ed852e2009-09-05 21:47:34 +0000746 options);
747 if (gravity < 0)
748 break;
749 image->gravity=(GravityType) gravity;
750 break;
751 }
752 if (LocaleCompare(keyword,"green-primary") == 0)
753 {
754 flags=ParseGeometry(options,&geometry_info);
755 image->chromaticity.green_primary.x=geometry_info.rho;
756 image->chromaticity.green_primary.y=geometry_info.sigma;
757 if ((flags & SigmaValue) == 0)
758 image->chromaticity.green_primary.y=
759 image->chromaticity.green_primary.x;
760 break;
761 }
762 (void) SetImageProperty(image,keyword,options);
763 break;
764 }
765 case 'i':
766 case 'I':
767 {
768 if (LocaleCompare(keyword,"id") == 0)
769 {
770 (void) CopyMagickString(id,options,MaxTextExtent);
771 break;
772 }
773 if (LocaleCompare(keyword,"iterations") == 0)
774 {
cristye27293e2009-12-18 02:53:20 +0000775 image->iterations=StringToUnsignedLong(options);
cristy3ed852e2009-09-05 21:47:34 +0000776 break;
777 }
778 (void) SetImageProperty(image,keyword,options);
779 break;
780 }
781 case 'm':
782 case 'M':
783 {
784 if (LocaleCompare(keyword,"matte") == 0)
785 {
cristybb503372010-05-27 20:51:26 +0000786 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000787 matte;
788
cristy042ee782011-04-22 18:48:30 +0000789 matte=ParseCommandOption(MagickBooleanOptions,MagickFalse,
cristy3ed852e2009-09-05 21:47:34 +0000790 options);
791 if (matte < 0)
792 break;
793 image->matte=(MagickBooleanType) matte;
794 break;
795 }
796 if (LocaleCompare(keyword,"matte-color") == 0)
797 {
798 (void) QueryColorDatabase(options,&image->matte_color,
799 exception);
800 break;
801 }
802 if (LocaleCompare(keyword,"montage") == 0)
803 {
804 (void) CloneString(&image->montage,options);
805 break;
806 }
807 (void) SetImageProperty(image,keyword,options);
808 break;
809 }
810 case 'o':
811 case 'O':
812 {
813 if (LocaleCompare(keyword,"opaque") == 0)
814 {
cristybb503372010-05-27 20:51:26 +0000815 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000816 matte;
817
cristy042ee782011-04-22 18:48:30 +0000818 matte=ParseCommandOption(MagickBooleanOptions,MagickFalse,
cristy3ed852e2009-09-05 21:47:34 +0000819 options);
820 if (matte < 0)
821 break;
822 image->matte=(MagickBooleanType) matte;
823 break;
824 }
825 if (LocaleCompare(keyword,"orientation") == 0)
826 {
cristybb503372010-05-27 20:51:26 +0000827 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000828 orientation;
829
cristy042ee782011-04-22 18:48:30 +0000830 orientation=ParseCommandOption(MagickOrientationOptions,
cristy3ed852e2009-09-05 21:47:34 +0000831 MagickFalse,options);
832 if (orientation < 0)
833 break;
834 image->orientation=(OrientationType) orientation;
835 break;
836 }
837 (void) SetImageProperty(image,keyword,options);
838 break;
839 }
840 case 'p':
841 case 'P':
842 {
843 if (LocaleCompare(keyword,"page") == 0)
844 {
845 char
846 *geometry;
847
848 geometry=GetPageGeometry(options);
849 (void) ParseAbsoluteGeometry(geometry,&image->page);
850 geometry=DestroyString(geometry);
851 break;
852 }
853 if ((LocaleNCompare(keyword,"profile:",8) == 0) ||
854 (LocaleNCompare(keyword,"profile-",8) == 0))
855 {
856 StringInfo
857 *profile;
858
859 if (profiles == (LinkedListInfo *) NULL)
860 profiles=NewLinkedList(0);
861 (void) AppendValueToLinkedList(profiles,
862 AcquireString(keyword+8));
cristy63f9b8e2011-09-01 13:40:50 +0000863 profile=BlobToStringInfo((const void *) NULL,(size_t)
864 StringToLong(options));
cristy49f5e9f2011-09-01 13:44:57 +0000865 if (profile == (StringInfo *) NULL)
cristy63f9b8e2011-09-01 13:40:50 +0000866 ThrowReaderException(ResourceLimitError,
867 "MemoryAllocationFailed");
cristy3ed852e2009-09-05 21:47:34 +0000868 (void) SetImageProfile(image,keyword+8,profile);
869 profile=DestroyStringInfo(profile);
870 break;
871 }
872 (void) SetImageProperty(image,keyword,options);
873 break;
874 }
875 case 'q':
876 case 'Q':
877 {
878 if (LocaleCompare(keyword,"quality") == 0)
879 {
cristye27293e2009-12-18 02:53:20 +0000880 image->quality=StringToUnsignedLong(options);
cristy3ed852e2009-09-05 21:47:34 +0000881 break;
882 }
883 if ((LocaleCompare(keyword,"quantum-format") == 0) ||
884 (LocaleCompare(keyword,"quantum:format") == 0))
885 {
cristybb503372010-05-27 20:51:26 +0000886 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000887 format;
888
cristy042ee782011-04-22 18:48:30 +0000889 format=ParseCommandOption(MagickQuantumFormatOptions,
cristy3ed852e2009-09-05 21:47:34 +0000890 MagickFalse,options);
891 if (format < 0)
892 break;
893 quantum_format=(QuantumFormatType) format;
894 break;
895 }
896 (void) SetImageProperty(image,keyword,options);
897 break;
898 }
899 case 'r':
900 case 'R':
901 {
902 if (LocaleCompare(keyword,"red-primary") == 0)
903 {
904 flags=ParseGeometry(options,&geometry_info);
905 image->chromaticity.red_primary.x=geometry_info.rho;
906 image->chromaticity.red_primary.y=geometry_info.sigma;
907 if ((flags & SigmaValue) == 0)
908 image->chromaticity.red_primary.y=
909 image->chromaticity.red_primary.x;
910 break;
911 }
912 if (LocaleCompare(keyword,"rendering-intent") == 0)
913 {
cristybb503372010-05-27 20:51:26 +0000914 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000915 rendering_intent;
916
cristy042ee782011-04-22 18:48:30 +0000917 rendering_intent=ParseCommandOption(MagickIntentOptions,
cristy3ed852e2009-09-05 21:47:34 +0000918 MagickFalse,options);
919 if (rendering_intent < 0)
920 break;
921 image->rendering_intent=(RenderingIntent) rendering_intent;
922 break;
923 }
924 if (LocaleCompare(keyword,"resolution") == 0)
925 {
926 flags=ParseGeometry(options,&geometry_info);
927 image->x_resolution=geometry_info.rho;
928 image->y_resolution=geometry_info.sigma;
929 if ((flags & SigmaValue) == 0)
930 image->y_resolution=image->x_resolution;
931 break;
932 }
933 if (LocaleCompare(keyword,"rows") == 0)
934 {
cristye27293e2009-12-18 02:53:20 +0000935 image->rows=StringToUnsignedLong(options);
cristy3ed852e2009-09-05 21:47:34 +0000936 break;
937 }
938 (void) SetImageProperty(image,keyword,options);
939 break;
940 }
941 case 's':
942 case 'S':
943 {
944 if (LocaleCompare(keyword,"scene") == 0)
945 {
cristye27293e2009-12-18 02:53:20 +0000946 image->scene=StringToUnsignedLong(options);
cristy3ed852e2009-09-05 21:47:34 +0000947 break;
948 }
949 (void) SetImageProperty(image,keyword,options);
950 break;
951 }
952 case 't':
953 case 'T':
954 {
955 if (LocaleCompare(keyword,"ticks-per-second") == 0)
956 {
cristy15893a42010-11-20 18:57:15 +0000957 image->ticks_per_second=(ssize_t) StringToLong(options);
cristy3ed852e2009-09-05 21:47:34 +0000958 break;
959 }
960 if (LocaleCompare(keyword,"tile-offset") == 0)
961 {
962 char
963 *geometry;
964
965 geometry=GetPageGeometry(options);
966 (void) ParseAbsoluteGeometry(geometry,&image->tile_offset);
967 geometry=DestroyString(geometry);
968 break;
969 }
970 if (LocaleCompare(keyword,"type") == 0)
971 {
cristybb503372010-05-27 20:51:26 +0000972 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000973 type;
974
cristy042ee782011-04-22 18:48:30 +0000975 type=ParseCommandOption(MagickTypeOptions,MagickFalse,
cristy3ed852e2009-09-05 21:47:34 +0000976 options);
977 if (type < 0)
978 break;
979 image->type=(ImageType) type;
980 break;
981 }
982 (void) SetImageProperty(image,keyword,options);
983 break;
984 }
985 case 'u':
986 case 'U':
987 {
988 if (LocaleCompare(keyword,"units") == 0)
989 {
cristybb503372010-05-27 20:51:26 +0000990 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000991 units;
992
cristyfbbafc92011-05-05 01:22:11 +0000993 units=ParseCommandOption(MagickResolutionOptions,
994 MagickFalse,options);
cristy3ed852e2009-09-05 21:47:34 +0000995 if (units < 0)
996 break;
997 image->units=(ResolutionType) units;
998 break;
999 }
1000 (void) SetImageProperty(image,keyword,options);
1001 break;
1002 }
1003 case 'v':
1004 case 'V':
1005 {
1006 if (LocaleCompare(keyword,"version") == 0)
1007 {
cristyc1acd842011-05-19 23:05:47 +00001008 version=InterpretLocaleValue(options,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00001009 break;
1010 }
1011 (void) SetImageProperty(image,keyword,options);
1012 break;
1013 }
1014 case 'w':
1015 case 'W':
1016 {
1017 if (LocaleCompare(keyword,"white-point") == 0)
1018 {
1019 flags=ParseGeometry(options,&geometry_info);
1020 image->chromaticity.white_point.x=geometry_info.rho;
1021 image->chromaticity.white_point.y=geometry_info.rho;
1022 if ((flags & SigmaValue) != 0)
1023 image->chromaticity.white_point.y=
1024 image->chromaticity.white_point.x;
1025 break;
1026 }
1027 (void) SetImageProperty(image,keyword,options);
1028 break;
1029 }
1030 default:
1031 {
1032 (void) SetImageProperty(image,keyword,options);
1033 break;
1034 }
1035 }
1036 }
1037 else
1038 c=ReadBlobByte(image);
1039 while (isspace((int) ((unsigned char) c)) != 0)
1040 c=ReadBlobByte(image);
1041 }
1042 options=DestroyString(options);
1043 (void) ReadBlobByte(image);
1044 /*
1045 Verify that required image information is defined.
1046 */
1047 if ((LocaleCompare(id,"ImageMagick") != 0) ||
1048 (image->storage_class == UndefinedClass) ||
1049 (image->columns == 0) || (image->rows == 0))
1050 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1051 if (image->montage != (char *) NULL)
1052 {
1053 register char
1054 *p;
1055
1056 /*
1057 Image directory.
1058 */
1059 length=MaxTextExtent;
1060 image->directory=AcquireString((char *) NULL);
1061 p=image->directory;
1062 do
1063 {
1064 *p='\0';
1065 if ((strlen(image->directory)+MaxTextExtent) >= length)
1066 {
1067 /*
1068 Allocate more memory for the image directory.
1069 */
1070 length<<=1;
1071 image->directory=(char *) ResizeQuantumMemory(image->directory,
1072 length+MaxTextExtent,sizeof(*image->directory));
1073 if (image->directory == (char *) NULL)
1074 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
1075 p=image->directory+strlen(image->directory);
1076 }
1077 c=ReadBlobByte(image);
1078 *p++=(char) c;
1079 } while (c != (int) '\0');
1080 }
1081 if (profiles != (LinkedListInfo *) NULL)
1082 {
1083 const char
1084 *name;
1085
1086 const StringInfo
1087 *profile;
1088
1089 /*
1090 Read image profiles.
1091 */
1092 ResetLinkedListIterator(profiles);
1093 name=(const char *) GetNextValueInLinkedList(profiles);
1094 while (name != (const char *) NULL)
1095 {
1096 profile=GetImageProfile(image,name);
1097 if (profile != (StringInfo *) NULL)
1098 {
1099 register unsigned char
1100 *p;
1101
1102 p=GetStringInfoDatum(profile);
1103 count=ReadBlob(image,GetStringInfoLength(profile),p);
cristyda16f162011-02-19 23:52:17 +00001104 (void) count;
cristy3ed852e2009-09-05 21:47:34 +00001105 }
1106 name=(const char *) GetNextValueInLinkedList(profiles);
1107 }
1108 profiles=DestroyLinkedList(profiles,RelinquishMagickMemory);
1109 }
1110 image->depth=GetImageQuantumDepth(image,MagickFalse);
1111 if (image->storage_class == PseudoClass)
1112 {
1113 /*
1114 Create image colormap.
1115 */
cristy018f07f2011-09-04 21:15:19 +00001116 status=AcquireImageColormap(image,colors != 0 ? colors : 256,exception);
cristy3ed852e2009-09-05 21:47:34 +00001117 if (status == MagickFalse)
1118 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1119 if (colors != 0)
1120 {
1121 size_t
1122 packet_size;
1123
1124 unsigned char
1125 *colormap;
1126
1127 /*
1128 Read image colormap from file.
1129 */
1130 packet_size=(size_t) (3UL*image->depth/8UL);
1131 colormap=(unsigned char *) AcquireQuantumMemory(image->colors,
1132 packet_size*sizeof(*colormap));
1133 if (colormap == (unsigned char *) NULL)
1134 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1135 count=ReadBlob(image,packet_size*image->colors,colormap);
1136 p=colormap;
1137 switch (image->depth)
1138 {
1139 default:
1140 ThrowReaderException(CorruptImageError,
1141 "ImageDepthNotSupported");
1142 case 8:
1143 {
1144 unsigned char
1145 pixel;
1146
cristybb503372010-05-27 20:51:26 +00001147 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00001148 {
1149 p=PushCharPixel(p,&pixel);
1150 image->colormap[i].red=ScaleCharToQuantum(pixel);
1151 p=PushCharPixel(p,&pixel);
1152 image->colormap[i].green=ScaleCharToQuantum(pixel);
1153 p=PushCharPixel(p,&pixel);
1154 image->colormap[i].blue=ScaleCharToQuantum(pixel);
1155 }
1156 break;
1157 }
1158 case 16:
1159 {
1160 unsigned short
1161 pixel;
1162
cristybb503372010-05-27 20:51:26 +00001163 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00001164 {
1165 p=PushShortPixel(MSBEndian,p,&pixel);
1166 image->colormap[i].red=ScaleShortToQuantum(pixel);
1167 p=PushShortPixel(MSBEndian,p,&pixel);
1168 image->colormap[i].green=ScaleShortToQuantum(pixel);
1169 p=PushShortPixel(MSBEndian,p,&pixel);
1170 image->colormap[i].blue=ScaleShortToQuantum(pixel);
1171 }
1172 break;
1173 }
1174 case 32:
1175 {
cristy4cb162a2010-05-30 03:04:47 +00001176 unsigned int
cristy3ed852e2009-09-05 21:47:34 +00001177 pixel;
1178
cristybb503372010-05-27 20:51:26 +00001179 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00001180 {
1181 p=PushLongPixel(MSBEndian,p,&pixel);
1182 image->colormap[i].red=ScaleLongToQuantum(pixel);
1183 p=PushLongPixel(MSBEndian,p,&pixel);
1184 image->colormap[i].green=ScaleLongToQuantum(pixel);
1185 p=PushLongPixel(MSBEndian,p,&pixel);
1186 image->colormap[i].blue=ScaleLongToQuantum(pixel);
1187 }
1188 break;
1189 }
1190 }
1191 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
1192 }
1193 }
1194 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
1195 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1196 break;
1197 /*
1198 Allocate image pixels.
1199 */
1200 quantum_info=AcquireQuantumInfo(image_info,image);
1201 if (quantum_info == (QuantumInfo *) NULL)
1202 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1203 if (quantum_format != UndefinedQuantumFormat)
1204 {
1205 status=SetQuantumFormat(image,quantum_info,quantum_format);
1206 if (status == MagickFalse)
1207 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1208 }
1209 packet_size=(size_t) (quantum_info->depth/8);
1210 if (image->storage_class == DirectClass)
1211 packet_size=(size_t) (3*quantum_info->depth/8);
cristyc4a6f8a2011-09-21 22:36:11 +00001212 if ((image->type == BilevelType) || (image->type == GrayscaleType) ||
1213 (image->type == GrayscaleMatteType))
cristy0a30e182011-09-01 01:43:15 +00001214 packet_size=quantum_info->depth/8;
cristy3ed852e2009-09-05 21:47:34 +00001215 if (image->matte != MagickFalse)
1216 packet_size+=quantum_info->depth/8;
1217 if (image->colorspace == CMYKColorspace)
1218 packet_size+=quantum_info->depth/8;
1219 if (image->compression == RLECompression)
1220 packet_size++;
cristyc4a6f8a2011-09-21 22:36:11 +00001221printf("%d\n",packet_size);
cristy3ed852e2009-09-05 21:47:34 +00001222 length=image->columns;
cristy26377172010-12-20 19:01:58 +00001223 length=MagickMax(MagickMax(BZipMaxExtent(packet_size*image->columns),
1224 LZMAMaxExtent(packet_size*image->columns)),ZipMaxExtent(packet_size*
1225 image->columns));
cristy3ed852e2009-09-05 21:47:34 +00001226 compress_pixels=(unsigned char *) AcquireQuantumMemory(length,
1227 sizeof(*compress_pixels));
1228 if (compress_pixels == (unsigned char *) NULL)
1229 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1230 /*
1231 Read image pixels.
1232 */
1233 quantum_type=RGBQuantum;
1234 if (image->matte != MagickFalse)
1235 quantum_type=RGBAQuantum;
1236 if (image->colorspace == CMYKColorspace)
1237 {
1238 quantum_type=CMYKQuantum;
1239 if (image->matte != MagickFalse)
1240 quantum_type=CMYKAQuantum;
1241 }
1242 if (image->storage_class == PseudoClass)
1243 {
1244 quantum_type=IndexQuantum;
1245 if (image->matte != MagickFalse)
1246 quantum_type=IndexAlphaQuantum;
1247 }
cristyc4a6f8a2011-09-21 22:36:11 +00001248 if ((image->type == BilevelType) || (image->type == GrayscaleType) ||
1249 (image->type == GrayscaleMatteType))
cristy3ed852e2009-09-05 21:47:34 +00001250 {
1251 quantum_type=GrayQuantum;
1252 if (image->matte != MagickFalse)
1253 quantum_type=GrayAlphaQuantum;
1254 }
cristy330af6c2010-12-21 14:36:06 +00001255 status=MagickTrue;
1256 switch (image->compression)
1257 {
1258#if defined(MAGICKCORE_BZLIB_DELEGATE)
1259 case BZipCompression:
1260 {
1261 (void) ResetMagickMemory(&bzip_info,0,sizeof(bzip_info));
1262 bzip_info.bzalloc=AcquireBZIPMemory;
1263 bzip_info.bzfree=RelinquishBZIPMemory;
1264 bzip_info.opaque=(void *) NULL;
1265 code=BZ2_bzDecompressInit(&bzip_info,(int) image_info->verbose,
1266 MagickFalse);
1267 if (code != BZ_OK)
1268 status=MagickFalse;
cristy330af6c2010-12-21 14:36:06 +00001269 break;
1270 }
1271#endif
1272#if defined(MAGICKCORE_LZMA_DELEGATE)
1273 case LZMACompression:
1274 {
cristy9d72f1a2010-12-21 20:46:59 +00001275 (void) ResetMagickMemory(&allocator,0,sizeof(allocator));
1276 allocator.alloc=AcquireLZMAMemory;
1277 allocator.free=RelinquishLZMAMemory;
cristy330af6c2010-12-21 14:36:06 +00001278 lzma_info=initialize_lzma;
cristy9d72f1a2010-12-21 20:46:59 +00001279 lzma_info.allocator=(&allocator);
cristy330af6c2010-12-21 14:36:06 +00001280 code=lzma_auto_decoder(&lzma_info,-1,0);
1281 if (code != LZMA_OK)
1282 status=MagickFalse;
cristy330af6c2010-12-21 14:36:06 +00001283 break;
1284 }
1285#endif
1286#if defined(MAGICKCORE_ZLIB_DELEGATE)
1287 case LZWCompression:
1288 case ZipCompression:
1289 {
1290 (void) ResetMagickMemory(&zip_info,0,sizeof(zip_info));
1291 zip_info.zalloc=AcquireZIPMemory;
1292 zip_info.zfree=RelinquishZIPMemory;
1293 zip_info.opaque=(voidpf) NULL;
1294 code=inflateInit(&zip_info);
1295 if (code != Z_OK)
1296 status=MagickFalse;
cristy330af6c2010-12-21 14:36:06 +00001297 break;
1298 }
1299#endif
1300 case RLECompression:
1301 {
cristy4c08aed2011-07-01 19:47:50 +00001302 GetPixelInfo(image,&pixel);
cristy330af6c2010-12-21 14:36:06 +00001303 break;
1304 }
1305 default:
1306 break;
1307 }
cristy3ed852e2009-09-05 21:47:34 +00001308 pixels=GetQuantumPixels(quantum_info);
cristy3ed852e2009-09-05 21:47:34 +00001309 length=0;
cristybb503372010-05-27 20:51:26 +00001310 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001311 {
cristybb503372010-05-27 20:51:26 +00001312 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001313 x;
1314
cristy4c08aed2011-07-01 19:47:50 +00001315 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001316 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001317
cristy330af6c2010-12-21 14:36:06 +00001318 if (status == MagickFalse)
1319 break;
cristy3ed852e2009-09-05 21:47:34 +00001320 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00001321 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001322 break;
cristy3ed852e2009-09-05 21:47:34 +00001323 switch (image->compression)
1324 {
cristy3ed852e2009-09-05 21:47:34 +00001325#if defined(MAGICKCORE_BZLIB_DELEGATE)
1326 case BZipCompression:
1327 {
cristy3ed852e2009-09-05 21:47:34 +00001328 bzip_info.next_out=(char *) pixels;
1329 bzip_info.avail_out=(unsigned int) (packet_size*image->columns);
1330 do
1331 {
1332 if (bzip_info.avail_in == 0)
1333 {
1334 bzip_info.next_in=(char *) compress_pixels;
1335 length=(size_t) BZipMaxExtent(packet_size*image->columns);
1336 if (version != 0)
1337 length=(size_t) ReadBlobMSBLong(image);
1338 bzip_info.avail_in=(unsigned int) ReadBlob(image,length,
1339 (unsigned char *) bzip_info.next_in);
1340 }
1341 if (BZ2_bzDecompress(&bzip_info) == BZ_STREAM_END)
1342 break;
1343 } while (bzip_info.avail_out != 0);
cristy26377172010-12-20 19:01:58 +00001344 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1345 quantum_type,pixels,exception);
1346 break;
1347 }
1348#endif
1349#if defined(MAGICKCORE_LZMA_DELEGATE)
1350 case LZMACompression:
1351 {
cristy26377172010-12-20 19:01:58 +00001352 lzma_info.next_out=pixels;
cristy330af6c2010-12-21 14:36:06 +00001353 lzma_info.avail_out=packet_size*image->columns;
cristy26377172010-12-20 19:01:58 +00001354 do
1355 {
1356 if (lzma_info.avail_in == 0)
1357 {
1358 lzma_info.next_in=compress_pixels;
1359 length=(size_t) ReadBlobMSBLong(image);
1360 lzma_info.avail_in=(unsigned int) ReadBlob(image,length,
1361 (unsigned char *) lzma_info.next_in);
1362 }
1363 code=lzma_code(&lzma_info,LZMA_RUN);
cristy330af6c2010-12-21 14:36:06 +00001364 if (code < 0)
1365 {
1366 status=MagickFalse;
1367 break;
1368 }
1369 if (code == LZMA_STREAM_END)
cristy26377172010-12-20 19:01:58 +00001370 break;
1371 } while (lzma_info.avail_out != 0);
cristy26377172010-12-20 19:01:58 +00001372 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1373 quantum_type,pixels,exception);
1374 break;
1375 }
1376#endif
1377#if defined(MAGICKCORE_ZLIB_DELEGATE)
1378 case LZWCompression:
1379 case ZipCompression:
1380 {
cristy26377172010-12-20 19:01:58 +00001381 zip_info.next_out=pixels;
1382 zip_info.avail_out=(uInt) (packet_size*image->columns);
1383 do
1384 {
1385 if (zip_info.avail_in == 0)
1386 {
1387 zip_info.next_in=compress_pixels;
1388 length=(size_t) ZipMaxExtent(packet_size*image->columns);
1389 if (version != 0)
1390 length=(size_t) ReadBlobMSBLong(image);
1391 zip_info.avail_in=(unsigned int) ReadBlob(image,length,
1392 zip_info.next_in);
1393 }
1394 if (inflate(&zip_info,Z_SYNC_FLUSH) == Z_STREAM_END)
1395 break;
1396 } while (zip_info.avail_out != 0);
cristy3ed852e2009-09-05 21:47:34 +00001397 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1398 quantum_type,pixels,exception);
1399 break;
1400 }
1401#endif
1402 case RLECompression:
1403 {
cristybb503372010-05-27 20:51:26 +00001404 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001405 {
1406 if (length == 0)
1407 {
1408 count=ReadBlob(image,packet_size,pixels);
cristy4c08aed2011-07-01 19:47:50 +00001409 PushRunlengthPacket(image,pixels,&length,&pixel);
cristy3ed852e2009-09-05 21:47:34 +00001410 }
1411 length--;
cristy4c08aed2011-07-01 19:47:50 +00001412 if (image->storage_class == PseudoClass)
cristy94b11832011-09-08 19:46:03 +00001413 SetPixelIndex(image,ClampToQuantum(pixel.index),q);
cristy4c08aed2011-07-01 19:47:50 +00001414 else
1415 {
cristy94b11832011-09-08 19:46:03 +00001416 SetPixelRed(image,ClampToQuantum(pixel.red),q);
1417 SetPixelGreen(image,ClampToQuantum(pixel.green),q);
1418 SetPixelBlue(image,ClampToQuantum(pixel.blue),q);
cristy4c08aed2011-07-01 19:47:50 +00001419 if (image->colorspace == CMYKColorspace)
cristy94b11832011-09-08 19:46:03 +00001420 SetPixelBlack(image,ClampToQuantum(pixel.black),q);
cristy4c08aed2011-07-01 19:47:50 +00001421 }
1422 if (image->matte != MagickFalse)
cristy94b11832011-09-08 19:46:03 +00001423 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
cristyed231572011-07-14 02:18:59 +00001424 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001425 }
1426 break;
1427 }
1428 default:
1429 {
1430 count=ReadBlob(image,packet_size*image->columns,pixels);
1431 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1432 quantum_type,pixels,exception);
1433 break;
1434 }
1435 }
1436 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1437 break;
1438 }
1439 SetQuantumImageType(image,quantum_type);
cristy330af6c2010-12-21 14:36:06 +00001440 switch (image->compression)
1441 {
1442#if defined(MAGICKCORE_BZLIB_DELEGATE)
1443 case BZipCompression:
1444 {
1445 if (version == 0)
1446 {
1447 MagickOffsetType
1448 offset;
1449
1450 offset=SeekBlob(image,-((MagickOffsetType)
1451 bzip_info.avail_in),SEEK_CUR);
1452 if (offset < 0)
1453 ThrowReaderException(CorruptImageError,
1454 "ImproperImageHeader");
1455 }
1456 code=BZ2_bzDecompressEnd(&bzip_info);
1457 if (code != BZ_OK)
1458 status=MagickFalse;
1459 break;
1460 }
1461#endif
1462#if defined(MAGICKCORE_LZMA_DELEGATE)
1463 case LZMACompression:
1464 {
cristy3b788a02010-12-27 15:59:54 +00001465 code=lzma_code(&lzma_info,LZMA_FINISH);
1466 if ((code != LZMA_STREAM_END) && (code != LZMA_OK))
cristyb977da52010-12-22 15:55:53 +00001467 status=MagickFalse;
cristy330af6c2010-12-21 14:36:06 +00001468 lzma_end(&lzma_info);
1469 break;
1470 }
1471#endif
1472#if defined(MAGICKCORE_ZLIB_DELEGATE)
1473 case LZWCompression:
1474 case ZipCompression:
1475 {
1476 if (version == 0)
1477 {
1478 MagickOffsetType
1479 offset;
1480
1481 offset=SeekBlob(image,-((MagickOffsetType) zip_info.avail_in),
1482 SEEK_CUR);
1483 if (offset < 0)
1484 ThrowReaderException(CorruptImageError,
1485 "ImproperImageHeader");
1486 }
1487 code=inflateEnd(&zip_info);
1488 if (code != LZMA_OK)
1489 status=MagickFalse;
1490 break;
1491 }
1492#endif
1493 default:
1494 break;
1495 }
cristy3ed852e2009-09-05 21:47:34 +00001496 quantum_info=DestroyQuantumInfo(quantum_info);
1497 compress_pixels=(unsigned char *) RelinquishMagickMemory(compress_pixels);
cristybb503372010-05-27 20:51:26 +00001498 if (((y != (ssize_t) image->rows)) || (status == MagickFalse))
cristy3ed852e2009-09-05 21:47:34 +00001499 {
1500 image=DestroyImageList(image);
1501 return((Image *) NULL);
1502 }
1503 if (EOFBlob(image) != MagickFalse)
1504 {
1505 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
1506 image->filename);
1507 break;
1508 }
1509 /*
1510 Proceed to next image.
1511 */
1512 if (image_info->number_scenes != 0)
1513 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1514 break;
1515 do
1516 {
1517 c=ReadBlobByte(image);
1518 } while ((isgraph(c) == MagickFalse) && (c != EOF));
1519 if (c != EOF)
1520 {
1521 /*
1522 Allocate next image structure.
1523 */
1524 AcquireNextImage(image_info,image);
1525 if (GetNextImageInList(image) == (Image *) NULL)
1526 {
1527 image=DestroyImageList(image);
1528 return((Image *) NULL);
1529 }
1530 image=SyncNextImageInList(image);
1531 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
1532 GetBlobSize(image));
1533 if (status == MagickFalse)
1534 break;
1535 }
1536 } while (c != EOF);
1537 (void) CloseBlob(image);
1538 return(GetFirstImageInList(image));
1539}
1540
1541/*
1542%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1543% %
1544% %
1545% %
1546% R e g i s t e r M I F F I m a g e %
1547% %
1548% %
1549% %
1550%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1551%
1552% RegisterMIFFImage() adds properties for the MIFF image format to the list of
1553% supported formats. The properties include the image format tag, a method to
1554% read and/or write the format, whether the format supports the saving of more
1555% than one frame to the same file or blob, whether the format supports native
1556% in-memory I/O, and a brief description of the format.
1557%
1558% The format of the RegisterMIFFImage method is:
1559%
cristybb503372010-05-27 20:51:26 +00001560% size_t RegisterMIFFImage(void)
cristy3ed852e2009-09-05 21:47:34 +00001561%
1562*/
cristybb503372010-05-27 20:51:26 +00001563ModuleExport size_t RegisterMIFFImage(void)
cristy3ed852e2009-09-05 21:47:34 +00001564{
1565 char
1566 version[MaxTextExtent];
1567
1568 MagickInfo
1569 *entry;
1570
1571 *version='\0';
1572#if defined(MagickImageCoderSignatureText)
1573 (void) CopyMagickString(version,MagickLibVersionText,MaxTextExtent);
1574#if defined(ZLIB_VERSION)
1575 (void) ConcatenateMagickString(version," with Zlib ",MaxTextExtent);
1576 (void) ConcatenateMagickString(version,ZLIB_VERSION,MaxTextExtent);
1577#endif
1578#if defined(MAGICKCORE_BZLIB_DELEGATE)
1579 (void) ConcatenateMagickString(version," and BZlib",MaxTextExtent);
1580#endif
1581#endif
1582 entry=SetMagickInfo("MIFF");
1583 entry->decoder=(DecodeImageHandler *) ReadMIFFImage;
1584 entry->encoder=(EncodeImageHandler *) WriteMIFFImage;
1585 entry->magick=(IsImageFormatHandler *) IsMIFF;
cristyffaf9782011-04-13 19:50:51 +00001586 entry->seekable_stream=MagickTrue;
cristy3ed852e2009-09-05 21:47:34 +00001587 entry->description=ConstantString("Magick Image File Format");
1588 if (*version != '\0')
1589 entry->version=ConstantString(version);
1590 entry->module=ConstantString("MIFF");
1591 (void) RegisterMagickInfo(entry);
1592 return(MagickImageCoderSignature);
1593}
1594
1595/*
1596%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1597% %
1598% %
1599% %
1600% U n r e g i s t e r M I F F I m a g e %
1601% %
1602% %
1603% %
1604%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1605%
1606% UnregisterMIFFImage() removes format registrations made by the MIFF module
1607% from the list of supported formats.
1608%
1609% The format of the UnregisterMIFFImage method is:
1610%
1611% UnregisterMIFFImage(void)
1612%
1613*/
1614ModuleExport void UnregisterMIFFImage(void)
1615{
1616 (void) UnregisterMagickInfo("MIFF");
1617}
1618
1619/*
1620%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1621% %
1622% %
1623% %
1624% W r i t e M I F F I m a g e %
1625% %
1626% %
1627% %
1628%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1629%
1630% WriteMIFFImage() writes a MIFF image to a file.
1631%
1632% The format of the WriteMIFFImage method is:
1633%
1634% MagickBooleanType WriteMIFFImage(const ImageInfo *image_info,
cristy1e178e72011-08-28 19:44:34 +00001635% Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001636%
1637% Compression code contributed by Kyle Shorter.
1638%
1639% A description of each parameter follows:
1640%
1641% o image_info: the image info.
1642%
1643% o image: the image.
1644%
cristy1e178e72011-08-28 19:44:34 +00001645% o exception: return any errors or warnings in this structure.
1646%
cristy3ed852e2009-09-05 21:47:34 +00001647*/
1648
1649static unsigned char *PopRunlengthPacket(Image *image,unsigned char *pixels,
cristy4c08aed2011-07-01 19:47:50 +00001650 size_t length,PixelInfo *pixel)
cristy3ed852e2009-09-05 21:47:34 +00001651{
1652 if (image->storage_class != DirectClass)
1653 {
1654 switch (image->depth)
1655 {
1656 case 32:
1657 {
cristy4c08aed2011-07-01 19:47:50 +00001658 *pixels++=(unsigned char) ((size_t) pixel->index >> 24);
1659 *pixels++=(unsigned char) ((size_t) pixel->index >> 16);
cristy3ed852e2009-09-05 21:47:34 +00001660 }
1661 case 16:
cristy4c08aed2011-07-01 19:47:50 +00001662 *pixels++=(unsigned char) ((size_t) pixel->index >> 8);
cristy3ed852e2009-09-05 21:47:34 +00001663 case 8:
1664 {
cristy4c08aed2011-07-01 19:47:50 +00001665 *pixels++=(unsigned char) pixel->index;
cristy3ed852e2009-09-05 21:47:34 +00001666 break;
1667 }
1668 default:
1669 (void) ThrowMagickException(&image->exception,GetMagickModule(),
1670 CorruptImageError,"ImageDepthNotSupported","`%s'",image->filename);
1671 }
1672 switch (image->depth)
1673 {
1674 case 32:
1675 {
cristy4cb162a2010-05-30 03:04:47 +00001676 unsigned int
cristy3ed852e2009-09-05 21:47:34 +00001677 value;
1678
1679 if (image->matte != MagickFalse)
1680 {
cristy94b11832011-09-08 19:46:03 +00001681 value=ScaleQuantumToLong(ClampToQuantum(pixel->alpha));
cristy3ed852e2009-09-05 21:47:34 +00001682 pixels=PopLongPixel(MSBEndian,value,pixels);
1683 }
1684 break;
1685 }
1686 case 16:
1687 {
1688 unsigned short
1689 value;
1690
1691 if (image->matte != MagickFalse)
1692 {
cristy94b11832011-09-08 19:46:03 +00001693 value=ScaleQuantumToShort(ClampToQuantum(pixel->alpha));
cristy3ed852e2009-09-05 21:47:34 +00001694 pixels=PopShortPixel(MSBEndian,value,pixels);
1695 }
1696 break;
1697 }
1698 case 8:
1699 {
1700 unsigned char
1701 value;
1702
1703 if (image->matte != MagickFalse)
1704 {
cristy94b11832011-09-08 19:46:03 +00001705 value=(unsigned char) ScaleQuantumToChar(ClampToQuantum(
1706 pixel->alpha));
cristy3ed852e2009-09-05 21:47:34 +00001707 pixels=PopCharPixel(value,pixels);
1708 }
1709 break;
1710 }
1711 default:
1712 (void) ThrowMagickException(&image->exception,GetMagickModule(),
1713 CorruptImageError,"ImageDepthNotSupported","`%s'",image->filename);
1714 }
1715 *pixels++=(unsigned char) length;
1716 return(pixels);
1717 }
1718 switch (image->depth)
1719 {
1720 case 32:
1721 {
cristy4cb162a2010-05-30 03:04:47 +00001722 unsigned int
cristy3ed852e2009-09-05 21:47:34 +00001723 value;
1724
cristy94b11832011-09-08 19:46:03 +00001725 value=ScaleQuantumToLong(ClampToQuantum(pixel->red));
cristy3ed852e2009-09-05 21:47:34 +00001726 pixels=PopLongPixel(MSBEndian,value,pixels);
cristy94b11832011-09-08 19:46:03 +00001727 value=ScaleQuantumToLong(ClampToQuantum(pixel->green));
cristy3ed852e2009-09-05 21:47:34 +00001728 pixels=PopLongPixel(MSBEndian,value,pixels);
cristy94b11832011-09-08 19:46:03 +00001729 value=ScaleQuantumToLong(ClampToQuantum(pixel->blue));
cristy3ed852e2009-09-05 21:47:34 +00001730 pixels=PopLongPixel(MSBEndian,value,pixels);
cristy3ed852e2009-09-05 21:47:34 +00001731 if (image->colorspace == CMYKColorspace)
1732 {
cristy94b11832011-09-08 19:46:03 +00001733 value=ScaleQuantumToLong(ClampToQuantum(pixel->black));
cristy4c08aed2011-07-01 19:47:50 +00001734 pixels=PopLongPixel(MSBEndian,value,pixels);
1735 }
1736 if (image->matte != MagickFalse)
1737 {
cristy94b11832011-09-08 19:46:03 +00001738 value=ScaleQuantumToLong(ClampToQuantum(pixel->alpha));
cristy3ed852e2009-09-05 21:47:34 +00001739 pixels=PopLongPixel(MSBEndian,value,pixels);
1740 }
1741 break;
1742 }
1743 case 16:
1744 {
1745 unsigned short
1746 value;
1747
cristy94b11832011-09-08 19:46:03 +00001748 value=ScaleQuantumToShort(ClampToQuantum(pixel->red));
cristy3ed852e2009-09-05 21:47:34 +00001749 pixels=PopShortPixel(MSBEndian,value,pixels);
cristy94b11832011-09-08 19:46:03 +00001750 value=ScaleQuantumToShort(ClampToQuantum(pixel->green));
cristy3ed852e2009-09-05 21:47:34 +00001751 pixels=PopShortPixel(MSBEndian,value,pixels);
cristy94b11832011-09-08 19:46:03 +00001752 value=ScaleQuantumToShort(ClampToQuantum(pixel->blue));
cristy3ed852e2009-09-05 21:47:34 +00001753 pixels=PopShortPixel(MSBEndian,value,pixels);
cristy3ed852e2009-09-05 21:47:34 +00001754 if (image->colorspace == CMYKColorspace)
1755 {
cristy94b11832011-09-08 19:46:03 +00001756 value=ScaleQuantumToShort(ClampToQuantum(pixel->black));
cristy4c08aed2011-07-01 19:47:50 +00001757 pixels=PopShortPixel(MSBEndian,value,pixels);
1758 }
1759 if (image->matte != MagickFalse)
1760 {
cristy94b11832011-09-08 19:46:03 +00001761 value=ScaleQuantumToShort(ClampToQuantum(pixel->alpha));
cristy3ed852e2009-09-05 21:47:34 +00001762 pixels=PopShortPixel(MSBEndian,value,pixels);
1763 }
1764 break;
1765 }
1766 case 8:
1767 {
1768 unsigned char
1769 value;
1770
cristy94b11832011-09-08 19:46:03 +00001771 value=(unsigned char) ScaleQuantumToChar(ClampToQuantum(pixel->red));
cristy3ed852e2009-09-05 21:47:34 +00001772 pixels=PopCharPixel(value,pixels);
cristy94b11832011-09-08 19:46:03 +00001773 value=(unsigned char) ScaleQuantumToChar(ClampToQuantum(pixel->green));
cristy3ed852e2009-09-05 21:47:34 +00001774 pixels=PopCharPixel(value,pixels);
cristy94b11832011-09-08 19:46:03 +00001775 value=(unsigned char) ScaleQuantumToChar(ClampToQuantum(pixel->blue));
cristy3ed852e2009-09-05 21:47:34 +00001776 pixels=PopCharPixel(value,pixels);
cristy3ed852e2009-09-05 21:47:34 +00001777 if (image->colorspace == CMYKColorspace)
1778 {
cristy94b11832011-09-08 19:46:03 +00001779 value=(unsigned char) ScaleQuantumToChar(ClampToQuantum(
1780 pixel->black));
cristy4c08aed2011-07-01 19:47:50 +00001781 pixels=PopCharPixel(value,pixels);
1782 }
1783 if (image->matte != MagickFalse)
1784 {
cristy94b11832011-09-08 19:46:03 +00001785 value=(unsigned char) ScaleQuantumToChar(ClampToQuantum(
1786 pixel->alpha));
cristy3ed852e2009-09-05 21:47:34 +00001787 pixels=PopCharPixel(value,pixels);
1788 }
1789 break;
1790 }
1791 default:
1792 (void) ThrowMagickException(&image->exception,GetMagickModule(),
1793 CorruptImageError,"ImageDepthNotSupported","`%s'",image->filename);
1794 }
1795 *pixels++=(unsigned char) length;
1796 return(pixels);
1797}
1798
1799static MagickBooleanType WriteMIFFImage(const ImageInfo *image_info,
cristy1e178e72011-08-28 19:44:34 +00001800 Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001801{
1802#if defined(MAGICKCORE_BZLIB_DELEGATE)
1803 bz_stream
1804 bzip_info;
1805#endif
1806
1807 char
1808 buffer[MaxTextExtent];
1809
1810 CompressionType
1811 compression;
1812
1813 const char
1814 *property,
1815 *value;
1816
cristy3ed852e2009-09-05 21:47:34 +00001817 int
1818 code;
1819
cristy26377172010-12-20 19:01:58 +00001820#if defined(MAGICKCORE_LZMA_DELEGATE)
cristy4b46dba2010-12-20 19:18:20 +00001821 lzma_allocator
1822 allocator;
1823
cristy26377172010-12-20 19:01:58 +00001824 lzma_stream
cristy330af6c2010-12-21 14:36:06 +00001825 initialize_lzma = LZMA_STREAM_INIT,
1826 lzma_info;
cristy26377172010-12-20 19:01:58 +00001827#endif
cristy3ed852e2009-09-05 21:47:34 +00001828
1829 MagickBooleanType
1830 status;
1831
1832 MagickOffsetType
1833 scene;
1834
cristy4c08aed2011-07-01 19:47:50 +00001835 PixelInfo
1836 pixel,
1837 target;
cristy3ed852e2009-09-05 21:47:34 +00001838
1839 QuantumInfo
1840 *quantum_info;
1841
1842 QuantumType
1843 quantum_type;
1844
cristybb503372010-05-27 20:51:26 +00001845 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001846 i;
1847
1848 size_t
1849 length,
1850 packet_size;
1851
cristy26377172010-12-20 19:01:58 +00001852 ssize_t
1853 y;
1854
cristy3ed852e2009-09-05 21:47:34 +00001855 unsigned char
1856 *compress_pixels,
1857 *pixels,
1858 *q;
1859
1860#if defined(MAGICKCORE_ZLIB_DELEGATE)
1861 z_stream
1862 zip_info;
1863#endif
1864
1865 /*
1866 Open output image file.
1867 */
1868 assert(image_info != (const ImageInfo *) NULL);
1869 assert(image_info->signature == MagickSignature);
1870 assert(image != (Image *) NULL);
1871 assert(image->signature == MagickSignature);
1872 if (image->debug != MagickFalse)
1873 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy3a37efd2011-08-28 20:31:03 +00001874 assert(exception != (ExceptionInfo *) NULL);
1875 assert(exception->signature == MagickSignature);
cristy1e178e72011-08-28 19:44:34 +00001876 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
cristy3ed852e2009-09-05 21:47:34 +00001877 if (status == MagickFalse)
1878 return(status);
1879 code=0;
1880 scene=0;
1881 do
1882 {
1883 /*
1884 Allocate image pixels.
1885 */
1886 image->depth=image->depth <= 8 ? 8UL : image->depth <= 16 ? 16UL :
1887 image->depth <= 32 ? 32UL : 64UL;
1888 quantum_info=AcquireQuantumInfo(image_info,image);
1889 if (quantum_info == (QuantumInfo *) NULL)
1890 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1891 if ((image->storage_class != PseudoClass) && (image->depth >= 32) &&
1892 (quantum_info->format == UndefinedQuantumFormat) &&
cristy1e178e72011-08-28 19:44:34 +00001893 (IsHighDynamicRangeImage(image,exception) != MagickFalse))
cristy3ed852e2009-09-05 21:47:34 +00001894 {
1895 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
1896 if (status == MagickFalse)
1897 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1898 }
1899 if ((image->storage_class == PseudoClass) &&
cristybb503372010-05-27 20:51:26 +00001900 (image->colors > (size_t) (GetQuantumRange(image->depth)+1)))
cristy1e178e72011-08-28 19:44:34 +00001901 (void) SetImageStorageClass(image,DirectClass,exception);
cristy669e5c72011-09-12 12:01:33 +00001902 if (IsImageGray(image,exception) != MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001903 {
cristy1e178e72011-08-28 19:44:34 +00001904 (void) SetImageStorageClass(image,DirectClass,exception);
1905 (void) SetImageColorspace(image,GRAYColorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +00001906 }
1907 compression=image->compression;
1908 if (image_info->compression != UndefinedCompression)
1909 compression=image_info->compression;
1910 switch (compression)
1911 {
1912#if !defined(MAGICKCORE_ZLIB_DELEGATE)
cristy26377172010-12-20 19:01:58 +00001913 case LZMACompression: compression=NoCompression; break;
1914#endif
1915#if !defined(MAGICKCORE_ZLIB_DELEGATE)
cristy3ed852e2009-09-05 21:47:34 +00001916 case LZWCompression:
1917 case ZipCompression: compression=NoCompression; break;
1918#endif
1919#if !defined(MAGICKCORE_BZLIB_DELEGATE)
1920 case BZipCompression: compression=NoCompression; break;
1921#endif
1922 case RLECompression:
1923 {
1924 if (quantum_info->format == FloatingPointQuantumFormat)
1925 compression=NoCompression;
cristy4c08aed2011-07-01 19:47:50 +00001926 GetPixelInfo(image,&target);
cristy3ed852e2009-09-05 21:47:34 +00001927 break;
1928 }
1929 default:
1930 break;
1931 }
1932 packet_size=(size_t) (quantum_info->depth/8);
1933 if (image->storage_class == DirectClass)
1934 packet_size=(size_t) (3*quantum_info->depth/8);
cristy669e5c72011-09-12 12:01:33 +00001935 if (IsImageGray(image,exception) != MagickFalse)
cristy0a30e182011-09-01 01:43:15 +00001936 packet_size=(size_t) (quantum_info->depth/8);
cristy3ed852e2009-09-05 21:47:34 +00001937 if (image->matte != MagickFalse)
1938 packet_size+=quantum_info->depth/8;
1939 if (image->colorspace == CMYKColorspace)
1940 packet_size+=quantum_info->depth/8;
1941 if (compression == RLECompression)
1942 packet_size++;
1943 length=image->columns;
1944 length=MagickMax(BZipMaxExtent(packet_size*image->columns),ZipMaxExtent(
1945 packet_size*image->columns));
1946 if ((compression == BZipCompression) || (compression == ZipCompression))
1947 if (length != (size_t) ((unsigned int) length))
1948 compression=NoCompression;
1949 compress_pixels=(unsigned char *) AcquireQuantumMemory(length,
1950 sizeof(*compress_pixels));
1951 if (compress_pixels == (unsigned char *) NULL)
1952 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1953 /*
1954 Write MIFF header.
1955 */
1956 (void) WriteBlobString(image,"id=ImageMagick version=1.0\n");
cristyb51dff52011-05-19 16:55:47 +00001957 (void) FormatLocaleString(buffer,MaxTextExtent,
cristy042ee782011-04-22 18:48:30 +00001958 "class=%s colors=%.20g matte=%s\n",CommandOptionToMnemonic(
cristye8c25f92010-06-03 00:53:06 +00001959 MagickClassOptions,image->storage_class),(double) image->colors,
cristy042ee782011-04-22 18:48:30 +00001960 CommandOptionToMnemonic(MagickBooleanOptions,(ssize_t) image->matte));
cristy3ed852e2009-09-05 21:47:34 +00001961 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00001962 (void) FormatLocaleString(buffer,MaxTextExtent,"columns=%.20g rows=%.20g "
cristye8c25f92010-06-03 00:53:06 +00001963 "depth=%.20g\n",(double) image->columns,(double) image->rows,(double)
1964 image->depth);
cristy3ed852e2009-09-05 21:47:34 +00001965 (void) WriteBlobString(image,buffer);
cristy5f1c1ff2010-12-23 21:38:06 +00001966 if (image->type != UndefinedType)
cristy3ed852e2009-09-05 21:47:34 +00001967 {
cristyb51dff52011-05-19 16:55:47 +00001968 (void) FormatLocaleString(buffer,MaxTextExtent,"type=%s\n",
cristy042ee782011-04-22 18:48:30 +00001969 CommandOptionToMnemonic(MagickTypeOptions,image->type));
cristy3ed852e2009-09-05 21:47:34 +00001970 (void) WriteBlobString(image,buffer);
1971 }
1972 if (image->colorspace != UndefinedColorspace)
1973 {
cristyb51dff52011-05-19 16:55:47 +00001974 (void) FormatLocaleString(buffer,MaxTextExtent,"colorspace=%s\n",
cristy042ee782011-04-22 18:48:30 +00001975 CommandOptionToMnemonic(MagickColorspaceOptions,image->colorspace));
cristy3ed852e2009-09-05 21:47:34 +00001976 (void) WriteBlobString(image,buffer);
1977 }
1978 if (compression != UndefinedCompression)
1979 {
cristyb51dff52011-05-19 16:55:47 +00001980 (void) FormatLocaleString(buffer,MaxTextExtent,"compression=%s "
cristy042ee782011-04-22 18:48:30 +00001981 "quality=%.20g\n",CommandOptionToMnemonic(MagickCompressOptions,
cristye8c25f92010-06-03 00:53:06 +00001982 compression),(double) image->quality);
cristy3ed852e2009-09-05 21:47:34 +00001983 (void) WriteBlobString(image,buffer);
1984 }
1985 if (image->units != UndefinedResolution)
1986 {
cristyb51dff52011-05-19 16:55:47 +00001987 (void) FormatLocaleString(buffer,MaxTextExtent,"units=%s\n",
cristy042ee782011-04-22 18:48:30 +00001988 CommandOptionToMnemonic(MagickResolutionOptions,image->units));
cristy3ed852e2009-09-05 21:47:34 +00001989 (void) WriteBlobString(image,buffer);
1990 }
1991 if ((image->x_resolution != 0) || (image->y_resolution != 0))
1992 {
cristyb51dff52011-05-19 16:55:47 +00001993 (void) FormatLocaleString(buffer,MaxTextExtent,
cristye7f51092010-01-17 00:39:37 +00001994 "resolution=%gx%g\n",image->x_resolution,image->y_resolution);
cristy3ed852e2009-09-05 21:47:34 +00001995 (void) WriteBlobString(image,buffer);
1996 }
1997 if ((image->page.width != 0) || (image->page.height != 0))
1998 {
cristyb51dff52011-05-19 16:55:47 +00001999 (void) FormatLocaleString(buffer,MaxTextExtent,
cristye8c25f92010-06-03 00:53:06 +00002000 "page=%.20gx%.20g%+.20g%+.20g\n",(double) image->page.width,(double)
2001 image->page.height,(double) image->page.x,(double) image->page.y);
cristy3ed852e2009-09-05 21:47:34 +00002002 (void) WriteBlobString(image,buffer);
2003 }
2004 else
2005 if ((image->page.x != 0) || (image->page.y != 0))
2006 {
cristyb51dff52011-05-19 16:55:47 +00002007 (void) FormatLocaleString(buffer,MaxTextExtent,"page=%+ld%+ld\n",
cristyf2faecf2010-05-28 19:19:36 +00002008 (long) image->page.x,(long) image->page.y);
cristy3ed852e2009-09-05 21:47:34 +00002009 (void) WriteBlobString(image,buffer);
2010 }
2011 if ((image->tile_offset.x != 0) || (image->tile_offset.y != 0))
2012 {
cristyb51dff52011-05-19 16:55:47 +00002013 (void) FormatLocaleString(buffer,MaxTextExtent,"tile-offset=%+ld%+ld\n",
cristyf2faecf2010-05-28 19:19:36 +00002014 (long) image->tile_offset.x,(long) image->tile_offset.y);
cristy3ed852e2009-09-05 21:47:34 +00002015 (void) WriteBlobString(image,buffer);
2016 }
2017 if ((GetNextImageInList(image) != (Image *) NULL) ||
2018 (GetPreviousImageInList(image) != (Image *) NULL))
2019 {
2020 if (image->scene == 0)
cristyb51dff52011-05-19 16:55:47 +00002021 (void) FormatLocaleString(buffer,MaxTextExtent,"iterations=%.20g "
cristye8c25f92010-06-03 00:53:06 +00002022 "delay=%.20g ticks-per-second=%.20g\n",(double) image->iterations,
2023 (double) image->delay,(double) image->ticks_per_second);
cristy3ed852e2009-09-05 21:47:34 +00002024 else
cristyb51dff52011-05-19 16:55:47 +00002025 (void) FormatLocaleString(buffer,MaxTextExtent,"scene=%.20g "
cristye8c25f92010-06-03 00:53:06 +00002026 "iterations=%.20g delay=%.20g ticks-per-second=%.20g\n",(double)
2027 image->scene,(double) image->iterations,(double) image->delay,
2028 (double) image->ticks_per_second);
cristy3ed852e2009-09-05 21:47:34 +00002029 (void) WriteBlobString(image,buffer);
2030 }
2031 else
2032 {
2033 if (image->scene != 0)
2034 {
cristyb51dff52011-05-19 16:55:47 +00002035 (void) FormatLocaleString(buffer,MaxTextExtent,"scene=%.20g\n",
cristye8c25f92010-06-03 00:53:06 +00002036 (double) image->scene);
cristy3ed852e2009-09-05 21:47:34 +00002037 (void) WriteBlobString(image,buffer);
2038 }
2039 if (image->iterations != 0)
2040 {
cristyb51dff52011-05-19 16:55:47 +00002041 (void) FormatLocaleString(buffer,MaxTextExtent,"iterations=%.20g\n",
cristye8c25f92010-06-03 00:53:06 +00002042 (double) image->iterations);
cristy3ed852e2009-09-05 21:47:34 +00002043 (void) WriteBlobString(image,buffer);
2044 }
2045 if (image->delay != 0)
2046 {
cristyb51dff52011-05-19 16:55:47 +00002047 (void) FormatLocaleString(buffer,MaxTextExtent,"delay=%.20g\n",
cristye8c25f92010-06-03 00:53:06 +00002048 (double) image->delay);
cristy3ed852e2009-09-05 21:47:34 +00002049 (void) WriteBlobString(image,buffer);
2050 }
2051 if (image->ticks_per_second != UndefinedTicksPerSecond)
2052 {
cristyb51dff52011-05-19 16:55:47 +00002053 (void) FormatLocaleString(buffer,MaxTextExtent,
cristye8c25f92010-06-03 00:53:06 +00002054 "ticks-per-second=%.20g\n",(double) image->ticks_per_second);
cristy3ed852e2009-09-05 21:47:34 +00002055 (void) WriteBlobString(image,buffer);
2056 }
2057 }
2058 if (image->gravity != UndefinedGravity)
2059 {
cristyb51dff52011-05-19 16:55:47 +00002060 (void) FormatLocaleString(buffer,MaxTextExtent,"gravity=%s\n",
cristy042ee782011-04-22 18:48:30 +00002061 CommandOptionToMnemonic(MagickGravityOptions,image->gravity));
cristy3ed852e2009-09-05 21:47:34 +00002062 (void) WriteBlobString(image,buffer);
2063 }
2064 if (image->dispose != UndefinedDispose)
2065 {
cristyb51dff52011-05-19 16:55:47 +00002066 (void) FormatLocaleString(buffer,MaxTextExtent,"dispose=%s\n",
cristy042ee782011-04-22 18:48:30 +00002067 CommandOptionToMnemonic(MagickDisposeOptions,image->dispose));
cristy3ed852e2009-09-05 21:47:34 +00002068 (void) WriteBlobString(image,buffer);
2069 }
2070 if (image->rendering_intent != UndefinedIntent)
2071 {
cristy0a30e182011-09-01 01:43:15 +00002072 (void) FormatLocaleString(buffer,MaxTextExtent,"rendering-intent=%s\n",
2073 CommandOptionToMnemonic(MagickIntentOptions,image->rendering_intent));
cristy3ed852e2009-09-05 21:47:34 +00002074 (void) WriteBlobString(image,buffer);
2075 }
2076 if (image->gamma != 0.0)
2077 {
cristyb51dff52011-05-19 16:55:47 +00002078 (void) FormatLocaleString(buffer,MaxTextExtent,"gamma=%g\n",
cristy3ed852e2009-09-05 21:47:34 +00002079 image->gamma);
2080 (void) WriteBlobString(image,buffer);
2081 }
2082 if (image->chromaticity.white_point.x != 0.0)
2083 {
2084 /*
2085 Note chomaticity points.
2086 */
cristyb51dff52011-05-19 16:55:47 +00002087 (void) FormatLocaleString(buffer,MaxTextExtent,"red-primary=%g,"
cristye7f51092010-01-17 00:39:37 +00002088 "%g green-primary=%g,%g blue-primary=%g,%g\n",
cristy3ed852e2009-09-05 21:47:34 +00002089 image->chromaticity.red_primary.x,image->chromaticity.red_primary.y,
2090 image->chromaticity.green_primary.x,
2091 image->chromaticity.green_primary.y,
2092 image->chromaticity.blue_primary.x,
2093 image->chromaticity.blue_primary.y);
2094 (void) WriteBlobString(image,buffer);
cristyb51dff52011-05-19 16:55:47 +00002095 (void) FormatLocaleString(buffer,MaxTextExtent,
cristye7f51092010-01-17 00:39:37 +00002096 "white-point=%g,%g\n",image->chromaticity.white_point.x,
cristy8cd5b312010-01-07 01:10:24 +00002097 image->chromaticity.white_point.y);
cristy3ed852e2009-09-05 21:47:34 +00002098 (void) WriteBlobString(image,buffer);
2099 }
2100 if (image->orientation != UndefinedOrientation)
2101 {
cristyb51dff52011-05-19 16:55:47 +00002102 (void) FormatLocaleString(buffer,MaxTextExtent,"orientation=%s\n",
cristy042ee782011-04-22 18:48:30 +00002103 CommandOptionToMnemonic(MagickOrientationOptions,image->orientation));
cristy3ed852e2009-09-05 21:47:34 +00002104 (void) WriteBlobString(image,buffer);
2105 }
2106 if (image->profiles != (void *) NULL)
2107 {
2108 const char
2109 *name;
2110
2111 const StringInfo
2112 *profile;
2113
2114 /*
2115 Write image profiles.
2116 */
2117 ResetImageProfileIterator(image);
2118 name=GetNextImageProfile(image);
2119 while (name != (const char *) NULL)
2120 {
2121 profile=GetImageProfile(image,name);
2122 if (profile != (StringInfo *) NULL)
2123 {
cristyb51dff52011-05-19 16:55:47 +00002124 (void) FormatLocaleString(buffer,MaxTextExtent,
cristye8c25f92010-06-03 00:53:06 +00002125 "profile:%s=%.20g\n",name,(double)
2126 GetStringInfoLength(profile));
cristy3ed852e2009-09-05 21:47:34 +00002127 (void) WriteBlobString(image,buffer);
2128 }
2129 name=GetNextImageProfile(image);
2130 }
2131 }
2132 if (image->montage != (char *) NULL)
2133 {
cristyb51dff52011-05-19 16:55:47 +00002134 (void) FormatLocaleString(buffer,MaxTextExtent,"montage=%s\n",
cristy3ed852e2009-09-05 21:47:34 +00002135 image->montage);
2136 (void) WriteBlobString(image,buffer);
2137 }
2138 if (quantum_info->format == FloatingPointQuantumFormat)
cristy0c2fcca2009-10-16 01:37:20 +00002139 (void) SetImageProperty(image,"quantum:format","floating-point");
cristy3ed852e2009-09-05 21:47:34 +00002140 ResetImagePropertyIterator(image);
2141 property=GetNextImageProperty(image);
2142 while (property != (const char *) NULL)
2143 {
cristyb51dff52011-05-19 16:55:47 +00002144 (void) FormatLocaleString(buffer,MaxTextExtent,"%s=",property);
cristy3ed852e2009-09-05 21:47:34 +00002145 (void) WriteBlobString(image,buffer);
2146 value=GetImageProperty(image,property);
2147 if (value != (const char *) NULL)
2148 {
cristybb503372010-05-27 20:51:26 +00002149 for (i=0; i < (ssize_t) strlen(value); i++)
cristy3ed852e2009-09-05 21:47:34 +00002150 if (isspace((int) ((unsigned char) value[i])) != 0)
2151 break;
cristybb503372010-05-27 20:51:26 +00002152 if (i <= (ssize_t) strlen(value))
cristy3ed852e2009-09-05 21:47:34 +00002153 (void) WriteBlobByte(image,'{');
2154 (void) WriteBlob(image,strlen(value),(const unsigned char *) value);
cristybb503372010-05-27 20:51:26 +00002155 if (i <= (ssize_t) strlen(value))
cristy3ed852e2009-09-05 21:47:34 +00002156 (void) WriteBlobByte(image,'}');
2157 }
2158 (void) WriteBlobByte(image,'\n');
2159 property=GetNextImageProperty(image);
2160 }
2161 (void) WriteBlobString(image,"\f\n:\032");
2162 if (image->montage != (char *) NULL)
2163 {
2164 /*
2165 Write montage tile directory.
2166 */
2167 if (image->directory != (char *) NULL)
2168 (void) WriteBlob(image,strlen(image->directory),(unsigned char *)
2169 image->directory);
2170 (void) WriteBlobByte(image,'\0');
2171 }
2172 if (image->profiles != (void *) NULL)
2173 {
2174 const char
2175 *name;
2176
2177 const StringInfo
2178 *profile;
2179
2180 /*
2181 Generic profile.
2182 */
2183 ResetImageProfileIterator(image);
2184 name=GetNextImageProfile(image);
2185 while (name != (const char *) NULL)
2186 {
2187 profile=GetImageProfile(image,name);
2188 (void) WriteBlob(image,GetStringInfoLength(profile),
2189 GetStringInfoDatum(profile));
2190 name=GetNextImageProfile(image);
2191 }
2192 }
2193 if (image->storage_class == PseudoClass)
2194 {
2195 size_t
2196 packet_size;
2197
2198 unsigned char
2199 *colormap,
2200 *q;
2201
2202 /*
2203 Allocate colormap.
2204 */
2205 packet_size=(size_t) (3*quantum_info->depth/8);
2206 colormap=(unsigned char *) AcquireQuantumMemory(image->colors,
2207 packet_size*sizeof(*colormap));
2208 if (colormap == (unsigned char *) NULL)
2209 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2210 /*
2211 Write colormap to file.
2212 */
2213 q=colormap;
cristybb503372010-05-27 20:51:26 +00002214 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00002215 {
2216 switch (quantum_info->depth)
2217 {
2218 default:
2219 ThrowWriterException(CorruptImageError,"ImageDepthNotSupported");
2220 case 32:
2221 {
cristy4cb162a2010-05-30 03:04:47 +00002222 register unsigned int
cristy3ed852e2009-09-05 21:47:34 +00002223 pixel;
2224
2225 pixel=ScaleQuantumToLong(image->colormap[i].red);
2226 q=PopLongPixel(MSBEndian,pixel,q);
2227 pixel=ScaleQuantumToLong(image->colormap[i].green);
2228 q=PopLongPixel(MSBEndian,pixel,q);
2229 pixel=ScaleQuantumToLong(image->colormap[i].blue);
2230 q=PopLongPixel(MSBEndian,pixel,q);
2231 break;
2232 }
2233 case 16:
2234 {
2235 register unsigned short
2236 pixel;
2237
2238 pixel=ScaleQuantumToShort(image->colormap[i].red);
2239 q=PopShortPixel(MSBEndian,pixel,q);
2240 pixel=ScaleQuantumToShort(image->colormap[i].green);
2241 q=PopShortPixel(MSBEndian,pixel,q);
2242 pixel=ScaleQuantumToShort(image->colormap[i].blue);
2243 q=PopShortPixel(MSBEndian,pixel,q);
2244 break;
2245 }
2246 case 8:
2247 {
2248 register unsigned char
2249 pixel;
2250
2251 pixel=(unsigned char) ScaleQuantumToChar(image->colormap[i].red);
2252 q=PopCharPixel(pixel,q);
2253 pixel=(unsigned char) ScaleQuantumToChar(
2254 image->colormap[i].green);
2255 q=PopCharPixel(pixel,q);
2256 pixel=(unsigned char) ScaleQuantumToChar(image->colormap[i].blue);
2257 q=PopCharPixel(pixel,q);
2258 break;
2259 }
2260 }
2261 }
2262 (void) WriteBlob(image,packet_size*image->colors,colormap);
2263 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
2264 }
2265 /*
2266 Write image pixels to file.
2267 */
cristy330af6c2010-12-21 14:36:06 +00002268 status=MagickTrue;
2269 switch (compression)
2270 {
2271#if defined(MAGICKCORE_BZLIB_DELEGATE)
2272 case BZipCompression:
2273 {
2274 (void) ResetMagickMemory(&bzip_info,0,sizeof(bzip_info));
2275 bzip_info.bzalloc=AcquireBZIPMemory;
2276 bzip_info.bzfree=RelinquishBZIPMemory;
2277 code=BZ2_bzCompressInit(&bzip_info,(int) (image->quality ==
2278 UndefinedCompressionQuality ? 7 : MagickMin(image->quality/10,
2279 9)),(int) image_info->verbose,0);
2280 if (code != BZ_OK)
2281 status=MagickFalse;
2282 break;
2283 }
2284#endif
2285#if defined(MAGICKCORE_LZMA_DELEGATE)
2286 case LZMACompression:
2287 {
cristy330af6c2010-12-21 14:36:06 +00002288 (void) ResetMagickMemory(&allocator,0,sizeof(allocator));
2289 allocator.alloc=AcquireLZMAMemory;
2290 allocator.free=RelinquishLZMAMemory;
cristy9d72f1a2010-12-21 20:46:59 +00002291 lzma_info=initialize_lzma;
cristy330af6c2010-12-21 14:36:06 +00002292 lzma_info.allocator=&allocator;
cristy9d72f1a2010-12-21 20:46:59 +00002293 code=lzma_easy_encoder(&lzma_info,image->quality/10,LZMA_CHECK_SHA256);
2294 if (code != LZMA_OK)
2295 status=MagickTrue;
cristy330af6c2010-12-21 14:36:06 +00002296 break;
2297 }
2298#endif
2299#if defined(MAGICKCORE_ZLIB_DELEGATE)
2300 case LZWCompression:
2301 case ZipCompression:
2302 {
2303 (void) ResetMagickMemory(&zip_info,0,sizeof(zip_info));
2304 zip_info.zalloc=AcquireZIPMemory;
2305 zip_info.zfree=RelinquishZIPMemory;
2306 code=deflateInit(&zip_info,(int) (image->quality ==
2307 UndefinedCompressionQuality ? 7 : MagickMin(image->quality/10,9)));
2308 if (code != Z_OK)
2309 status=MagickFalse;
2310 break;
2311 }
2312#endif
2313 default:
2314 break;
2315 }
cristy1e178e72011-08-28 19:44:34 +00002316 quantum_type=GetQuantumType(image,exception);
cristy3ed852e2009-09-05 21:47:34 +00002317 pixels=GetQuantumPixels(quantum_info);
cristybb503372010-05-27 20:51:26 +00002318 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002319 {
cristy4c08aed2011-07-01 19:47:50 +00002320 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00002321 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00002322
cristybb503372010-05-27 20:51:26 +00002323 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002324 x;
2325
cristy330af6c2010-12-21 14:36:06 +00002326 if (status == MagickFalse)
2327 break;
cristy1e178e72011-08-28 19:44:34 +00002328 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00002329 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002330 break;
cristy3ed852e2009-09-05 21:47:34 +00002331 q=pixels;
2332 switch (compression)
2333 {
cristy3ed852e2009-09-05 21:47:34 +00002334#if defined(MAGICKCORE_BZLIB_DELEGATE)
2335 case BZipCompression:
2336 {
cristy3ed852e2009-09-05 21:47:34 +00002337 bzip_info.next_in=(char *) pixels;
2338 bzip_info.avail_in=(unsigned int) (packet_size*image->columns);
cristy4c08aed2011-07-01 19:47:50 +00002339 (void) ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
cristy1e178e72011-08-28 19:44:34 +00002340 quantum_type,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00002341 do
2342 {
2343 bzip_info.next_out=(char *) compress_pixels;
2344 bzip_info.avail_out=(unsigned int) BZipMaxExtent(packet_size*
2345 image->columns);
2346 code=BZ2_bzCompress(&bzip_info,BZ_FLUSH);
cristy26377172010-12-20 19:01:58 +00002347 if (code != BZ_OK)
2348 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00002349 length=(size_t) (bzip_info.next_out-(char *) compress_pixels);
2350 if (length != 0)
2351 {
2352 (void) WriteBlobMSBLong(image,(unsigned int) length);
2353 (void) WriteBlob(image,length,compress_pixels);
2354 }
2355 } while (bzip_info.avail_in != 0);
cristy26377172010-12-20 19:01:58 +00002356 break;
2357 }
2358#endif
2359#if defined(MAGICKCORE_LZMA_DELEGATE)
2360 case LZMACompression:
2361 {
cristy26377172010-12-20 19:01:58 +00002362 lzma_info.next_in=pixels;
cristy330af6c2010-12-21 14:36:06 +00002363 lzma_info.avail_in=packet_size*image->columns;
cristy4c08aed2011-07-01 19:47:50 +00002364 (void) ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
cristy1e178e72011-08-28 19:44:34 +00002365 quantum_type,pixels,exception);
cristy26377172010-12-20 19:01:58 +00002366 do
2367 {
2368 lzma_info.next_out=compress_pixels;
2369 lzma_info.avail_out=packet_size*image->columns;
2370 code=lzma_code(&lzma_info,LZMA_RUN);
2371 if (code != LZMA_OK)
2372 status=MagickFalse;
2373 length=(size_t) (lzma_info.next_out-compress_pixels);
2374 if (length != 0)
2375 {
2376 (void) WriteBlobMSBLong(image,(unsigned int) length);
2377 (void) WriteBlob(image,length,compress_pixels);
2378 }
2379 } while (lzma_info.avail_in != 0);
cristy26377172010-12-20 19:01:58 +00002380 break;
2381 }
2382#endif
2383#if defined(MAGICKCORE_ZLIB_DELEGATE)
2384 case LZWCompression:
2385 case ZipCompression:
2386 {
cristy26377172010-12-20 19:01:58 +00002387 zip_info.next_in=pixels;
2388 zip_info.avail_in=(uInt) (packet_size*image->columns);
cristy4c08aed2011-07-01 19:47:50 +00002389 (void) ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
cristy1e178e72011-08-28 19:44:34 +00002390 quantum_type,pixels,exception);
cristy26377172010-12-20 19:01:58 +00002391 do
2392 {
2393 zip_info.next_out=compress_pixels;
2394 zip_info.avail_out=(uInt) ZipMaxExtent(packet_size*image->columns);
2395 code=deflate(&zip_info,Z_SYNC_FLUSH);
2396 if (code != Z_OK)
2397 status=MagickFalse;
2398 length=(size_t) (zip_info.next_out-compress_pixels);
2399 if (length != 0)
2400 {
2401 (void) WriteBlobMSBLong(image,(unsigned int) length);
2402 (void) WriteBlob(image,length,compress_pixels);
2403 }
2404 } while (zip_info.avail_in != 0);
cristy3ed852e2009-09-05 21:47:34 +00002405 break;
2406 }
2407#endif
2408 case RLECompression:
2409 {
cristy4c08aed2011-07-01 19:47:50 +00002410 length=0;
2411 SetPixelInfo(image,p,&pixel);
cristyed231572011-07-14 02:18:59 +00002412 p+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00002413 for (x=1; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002414 {
cristy4c08aed2011-07-01 19:47:50 +00002415 SetPixelInfo(image,p,&target);
2416 if ((length < 255) &&
2417 (IsPixelInfoEquivalent(&pixel,&target) != MagickFalse))
cristy3ed852e2009-09-05 21:47:34 +00002418 length++;
2419 else
2420 {
cristy4c08aed2011-07-01 19:47:50 +00002421 q=PopRunlengthPacket(image,q,length,&pixel);
cristy3ed852e2009-09-05 21:47:34 +00002422 length=0;
2423 }
cristy4c08aed2011-07-01 19:47:50 +00002424 SetPixelInfo(image,p,&pixel);
cristyed231572011-07-14 02:18:59 +00002425 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00002426 }
cristy4c08aed2011-07-01 19:47:50 +00002427 q=PopRunlengthPacket(image,q,length,&pixel);
cristy3ed852e2009-09-05 21:47:34 +00002428 (void) WriteBlob(image,(size_t) (q-pixels),pixels);
2429 break;
2430 }
2431 default:
2432 {
cristy4c08aed2011-07-01 19:47:50 +00002433 (void) ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
cristy1e178e72011-08-28 19:44:34 +00002434 quantum_type,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00002435 (void) WriteBlob(image,packet_size*image->columns,pixels);
2436 break;
2437 }
2438 }
cristycee97112010-05-28 00:44:52 +00002439 if (image->previous == (Image *) NULL)
2440 {
2441 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
2442 image->rows);
2443 if (status == MagickFalse)
2444 break;
2445 }
cristy3ed852e2009-09-05 21:47:34 +00002446 }
cristy330af6c2010-12-21 14:36:06 +00002447 switch (compression)
2448 {
2449#if defined(MAGICKCORE_BZLIB_DELEGATE)
2450 case BZipCompression:
2451 {
2452 for ( ; ; )
2453 {
2454 if (status == MagickFalse)
2455 break;
2456 bzip_info.next_out=(char *) compress_pixels;
2457 bzip_info.avail_out=(unsigned int) BZipMaxExtent(packet_size*
2458 image->columns);
2459 code=BZ2_bzCompress(&bzip_info,BZ_FINISH);
cristy330af6c2010-12-21 14:36:06 +00002460 length=(size_t) (bzip_info.next_out-(char *) compress_pixels);
2461 if (length != 0)
2462 {
2463 (void) WriteBlobMSBLong(image,(unsigned int) length);
2464 (void) WriteBlob(image,length,compress_pixels);
2465 }
2466 if (code == BZ_STREAM_END)
2467 break;
2468 }
2469 code=BZ2_bzCompressEnd(&bzip_info);
2470 if (code != BZ_OK)
2471 status=MagickFalse;
2472 break;
2473 }
2474#endif
2475#if defined(MAGICKCORE_LZMA_DELEGATE)
2476 case LZMACompression:
2477 {
2478 for ( ; ; )
2479 {
2480 if (status == MagickFalse)
2481 break;
2482 lzma_info.next_out=compress_pixels;
2483 lzma_info.avail_out=packet_size*image->columns;
2484 code=lzma_code(&lzma_info,LZMA_FINISH);
cristy330af6c2010-12-21 14:36:06 +00002485 length=(size_t) (lzma_info.next_out-compress_pixels);
2486 if (length > 6)
2487 {
2488 (void) WriteBlobMSBLong(image,(unsigned int) length);
2489 (void) WriteBlob(image,length,compress_pixels);
2490 }
2491 if (code == LZMA_STREAM_END)
2492 break;
2493 }
2494 lzma_end(&lzma_info);
2495 break;
2496 }
2497#endif
2498#if defined(MAGICKCORE_ZLIB_DELEGATE)
2499 case LZWCompression:
2500 case ZipCompression:
2501 {
2502 for ( ; ; )
2503 {
2504 if (status == MagickFalse)
2505 break;
2506 zip_info.next_out=compress_pixels;
2507 zip_info.avail_out=(uInt) ZipMaxExtent(packet_size*
2508 image->columns);
2509 code=deflate(&zip_info,Z_FINISH);
cristy330af6c2010-12-21 14:36:06 +00002510 length=(size_t) (zip_info.next_out-compress_pixels);
2511 if (length > 6)
2512 {
2513 (void) WriteBlobMSBLong(image,(unsigned int) length);
2514 (void) WriteBlob(image,length,compress_pixels);
2515 }
2516 if (code == Z_STREAM_END)
2517 break;
2518 }
2519 code=deflateEnd(&zip_info);
2520 if (code != Z_OK)
2521 status=MagickFalse;
2522 break;
2523 }
2524#endif
2525 default:
2526 break;
2527 }
cristy3ed852e2009-09-05 21:47:34 +00002528 quantum_info=DestroyQuantumInfo(quantum_info);
2529 compress_pixels=(unsigned char *) RelinquishMagickMemory(compress_pixels);
2530 if (GetNextImageInList(image) == (Image *) NULL)
2531 break;
2532 image=SyncNextImageInList(image);
2533 status=SetImageProgress(image,SaveImagesTag,scene++,
2534 GetImageListLength(image));
2535 if (status == MagickFalse)
2536 break;
2537 } while (image_info->adjoin != MagickFalse);
2538 (void) CloseBlob(image);
2539 return(status);
2540}