blob: 1eb8c47804d4c887dc6b58476ae513a5d92925b1 [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
6% GGGG IIIII FFFFF %
7% G I F %
8% G GG I FFF %
9% G G I F %
10% GGG IIIII F %
11% %
12% %
13% Read/Write Compuserv Graphics Interchange Format %
14% %
15% Software Design %
16% John Cristy %
17% July 1992 %
18% %
19% %
20% Copyright 1999-2009 ImageMagick Studio LLC, a non-profit organization %
21% dedicated to making software imaging solutions freely available. %
22% %
23% You may not use this file except in compliance with the License. You may %
24% obtain a copy of the License at %
25% %
26% http://www.imagemagick.org/script/license.php %
27% %
28% Unless required by applicable law or agreed to in writing, software %
29% distributed under the License is distributed on an "AS IS" BASIS, %
30% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31% See the License for the specific language governing permissions and %
32% limitations under the License. %
33% %
34%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35%
36%
37*/
38
39/*
40 Include declarations.
41*/
42#include "magick/studio.h"
43#include "magick/blob.h"
44#include "magick/blob-private.h"
45#include "magick/cache.h"
46#include "magick/color.h"
47#include "magick/color-private.h"
48#include "magick/colorspace.h"
49#include "magick/exception.h"
50#include "magick/exception-private.h"
51#include "magick/image.h"
52#include "magick/image-private.h"
53#include "magick/list.h"
54#include "magick/profile.h"
55#include "magick/magick.h"
56#include "magick/memory_.h"
57#include "magick/monitor.h"
58#include "magick/monitor-private.h"
59#include "magick/option.h"
60#include "magick/property.h"
61#include "magick/quantize.h"
62#include "magick/quantum-private.h"
63#include "magick/static.h"
64#include "magick/string_.h"
65#include "magick/module.h"
66
67/*
68 Define declarations.
69*/
70#define MaximumLZWBits 12
71#define MaximumLZWCode (1UL << MaximumLZWBits)
72
73/*
74 Typdef declarations.
75*/
76typedef struct _LZWCodeInfo
77{
78 unsigned char
79 buffer[280];
80
81 unsigned long
82 count,
83 bit;
84
85 MagickBooleanType
86 eof;
87} LZWCodeInfo;
88
89typedef struct _LZWStack
90{
91 unsigned long
92 *codes,
93 *index,
94 *top;
95} LZWStack;
96
97typedef struct _LZWInfo
98{
99 Image
100 *image;
101
102 LZWStack
103 *stack;
104
105 MagickBooleanType
106 genesis;
107
108 unsigned long
109 data_size,
110 maximum_data_value,
111 clear_code,
112 end_code,
113 bits,
114 first_code,
115 last_code,
116 maximum_code,
117 slot,
118 *table[2];
119
120 LZWCodeInfo
121 code_info;
122} LZWInfo;
123
124/*
125 Forward declarations.
126*/
127static inline int
128 GetNextLZWCode(LZWInfo *,const unsigned long);
129
130static MagickBooleanType
131 WriteGIFImage(const ImageInfo *,Image *);
132
133static ssize_t
134 ReadBlobBlock(Image *,unsigned char *);
135
136/*
137%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
138% %
139% %
140% %
141% D e c o d e I m a g e %
142% %
143% %
144% %
145%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
146%
147% DecodeImage uncompresses an image via GIF-coding.
148%
149% The format of the DecodeImage method is:
150%
151% MagickBooleanType DecodeImage(Image *image,const long opacity)
152%
153% A description of each parameter follows:
154%
155% o image: the address of a structure of type Image.
156%
157% o opacity: The colormap index associated with the transparent color.
158%
159*/
160
161static LZWInfo *RelinquishLZWInfo(LZWInfo *lzw_info)
162{
163 if (lzw_info->table[0] != (unsigned long *) NULL)
164 lzw_info->table[0]=(unsigned long *) RelinquishMagickMemory(
165 lzw_info->table[0]);
166 if (lzw_info->table[1] != (unsigned long *) NULL)
167 lzw_info->table[1]=(unsigned long *) RelinquishMagickMemory(
168 lzw_info->table[1]);
169 if (lzw_info->stack != (LZWStack *) NULL)
170 {
171 if (lzw_info->stack->codes != (unsigned long *) NULL)
172 lzw_info->stack->codes=(unsigned long *) RelinquishMagickMemory(
173 lzw_info->stack->codes);
174 lzw_info->stack=(LZWStack *) RelinquishMagickMemory(lzw_info->stack);
175 }
176 lzw_info=(LZWInfo *) RelinquishMagickMemory(lzw_info);
177 return((LZWInfo *) NULL);
178}
179
180static inline void ResetLZWInfo(LZWInfo *lzw_info)
181{
182 lzw_info->bits=lzw_info->data_size+1;
183 lzw_info->maximum_code=1UL << lzw_info->bits;
184 lzw_info->slot=lzw_info->maximum_data_value+3;
185 lzw_info->genesis=MagickTrue;
186}
187
188static LZWInfo *AcquireLZWInfo(Image *image,const unsigned long data_size)
189{
190 LZWInfo
191 *lzw_info;
192
193 register long
194 i;
195
196 lzw_info=(LZWInfo *) AcquireMagickMemory(sizeof(*lzw_info));
197 if (lzw_info == (LZWInfo *) NULL)
198 return((LZWInfo *) NULL);
199 (void) ResetMagickMemory(lzw_info,0,sizeof(*lzw_info));
200 lzw_info->image=image;
201 lzw_info->data_size=data_size;
202 lzw_info->maximum_data_value=(1UL << data_size)-1;
203 lzw_info->clear_code=lzw_info->maximum_data_value+1;
204 lzw_info->end_code=lzw_info->maximum_data_value+2;
205 lzw_info->table[0]=(unsigned long *) AcquireQuantumMemory(MaximumLZWCode,
206 sizeof(*lzw_info->table));
207 lzw_info->table[1]=(unsigned long *) AcquireQuantumMemory(MaximumLZWCode,
208 sizeof(*lzw_info->table));
209 if ((lzw_info->table[0] == (unsigned long *) NULL) ||
210 (lzw_info->table[1] == (unsigned long *) NULL))
211 {
212 lzw_info=RelinquishLZWInfo(lzw_info);
213 return((LZWInfo *) NULL);
214 }
215 for (i=0; i <= (long) lzw_info->maximum_data_value; i++)
216 {
217 lzw_info->table[0][i]=0;
218 lzw_info->table[1][i]=(unsigned long) i;
219 }
220 ResetLZWInfo(lzw_info);
221 lzw_info->code_info.buffer[0]='\0';
222 lzw_info->code_info.buffer[1]='\0';
223 lzw_info->code_info.count=2;
224 lzw_info->code_info.bit=8*lzw_info->code_info.count;
225 lzw_info->code_info.eof=MagickFalse;
226 lzw_info->genesis=MagickTrue;
227 lzw_info->stack=(LZWStack *) AcquireMagickMemory(sizeof(*lzw_info->stack));
228 if (lzw_info->stack == (LZWStack *) NULL)
229 {
230 lzw_info=RelinquishLZWInfo(lzw_info);
231 return((LZWInfo *) NULL);
232 }
233 lzw_info->stack->codes=(unsigned long *) AcquireQuantumMemory(2UL*
234 MaximumLZWCode,sizeof(*lzw_info->stack->codes));
235 if (lzw_info->stack->codes == (unsigned long *) NULL)
236 {
237 lzw_info=RelinquishLZWInfo(lzw_info);
238 return((LZWInfo *) NULL);
239 }
240 lzw_info->stack->index=lzw_info->stack->codes;
241 lzw_info->stack->top=lzw_info->stack->codes+2*MaximumLZWCode;
242 return(lzw_info);
243}
244
245static inline int GetNextLZWCode(LZWInfo *lzw_info,const unsigned long bits)
246{
247 int
248 code;
249
250 register long
251 i;
252
253 while (((lzw_info->code_info.bit+bits) > (8*lzw_info->code_info.count)) &&
254 (lzw_info->code_info.eof == MagickFalse))
255 {
256 ssize_t
257 count;
258
259 lzw_info->code_info.buffer[0]=lzw_info->code_info.buffer[
260 lzw_info->code_info.count-2];
261 lzw_info->code_info.buffer[1]=lzw_info->code_info.buffer[
262 lzw_info->code_info.count-1];
263 lzw_info->code_info.bit-=8*(lzw_info->code_info.count-2);
264 lzw_info->code_info.count=2;
265 count=ReadBlobBlock(lzw_info->image,&lzw_info->code_info.buffer[
266 lzw_info->code_info.count]);
267 if (count > 0)
268 lzw_info->code_info.count+=count;
269 else
270 lzw_info->code_info.eof=MagickTrue;
271 }
272 if ((lzw_info->code_info.bit+bits) > (8*lzw_info->code_info.count))
273 return(-1);
274 code=0;
275 for (i=0; i < (long) bits; i++)
276 {
277 code|=((lzw_info->code_info.buffer[lzw_info->code_info.bit/8] &
278 (1UL << (lzw_info->code_info.bit % 8))) != 0) << i;
279 lzw_info->code_info.bit++;
280 }
281 return(code);
282}
283
284static inline long PopLZWStack(LZWStack *stack_info)
285{
286 if (stack_info->index <= stack_info->codes)
287 return(-1);
288 stack_info->index--;
289 return((long) *stack_info->index);
290}
291
292static inline void PushLZWStack(LZWStack *stack_info,const unsigned long value)
293{
294 if (stack_info->index >= stack_info->top)
295 return;
296 *stack_info->index=value;
297 stack_info->index++;
298}
299
300static int ReadBlobLZWByte(LZWInfo *lzw_info)
301{
302 int
303 code;
304
305 ssize_t
306 count;
307
308 unsigned long
309 value;
310
311 if (lzw_info->stack->index != lzw_info->stack->codes)
312 return(PopLZWStack(lzw_info->stack));
313 if (lzw_info->genesis != MagickFalse)
314 {
315 lzw_info->genesis=MagickFalse;
316 do
317 {
318 lzw_info->first_code=(unsigned long) GetNextLZWCode(lzw_info,
319 lzw_info->bits);
320 lzw_info->last_code=lzw_info->first_code;
321 } while (lzw_info->first_code == lzw_info->clear_code);
322 return((int) lzw_info->first_code);
323 }
324 code=GetNextLZWCode(lzw_info,lzw_info->bits);
325 if (code < 0)
326 return(code);
327 if ((unsigned long) code == lzw_info->clear_code)
328 {
329 ResetLZWInfo(lzw_info);
330 return(ReadBlobLZWByte(lzw_info));
331 }
332 if ((unsigned long) code == lzw_info->end_code)
333 return(-1);
334 if ((unsigned long) code < lzw_info->slot)
335 value=(unsigned long) code;
336 else
337 {
338 PushLZWStack(lzw_info->stack,lzw_info->first_code);
339 value=lzw_info->last_code;
340 }
341 count=0;
342 while (value > lzw_info->maximum_data_value)
343 {
344 if ((size_t) count > MaximumLZWCode)
345 return(-1);
346 count++;
347 if ((size_t) value > MaximumLZWCode)
348 return(-1);
349 PushLZWStack(lzw_info->stack,lzw_info->table[1][value]);
350 value=lzw_info->table[0][value];
351 }
352 lzw_info->first_code=lzw_info->table[1][value];
353 PushLZWStack(lzw_info->stack,lzw_info->first_code);
354 if (lzw_info->slot < MaximumLZWCode)
355 {
356 lzw_info->table[0][lzw_info->slot]=lzw_info->last_code;
357 lzw_info->table[1][lzw_info->slot]=lzw_info->first_code;
358 lzw_info->slot++;
359 if ((lzw_info->slot >= lzw_info->maximum_code) &&
360 (lzw_info->bits < MaximumLZWBits))
361 {
362 lzw_info->bits++;
363 lzw_info->maximum_code=1UL << lzw_info->bits;
364 }
365 }
366 lzw_info->last_code=(unsigned long) code;
367 return(PopLZWStack(lzw_info->stack));
368}
369
370static MagickBooleanType DecodeImage(Image *image,const long opacity)
371{
372 ExceptionInfo
373 *exception;
374
375 IndexPacket
376 index;
377
378 int
379 c;
380
381 long
382 offset,
383 y;
384
385 LZWInfo
386 *lzw_info;
387
388 unsigned char
389 data_size;
390
391 unsigned long
392 pass;
393
394 /*
395 Allocate decoder tables.
396 */
397 assert(image != (Image *) NULL);
398 assert(image->signature == MagickSignature);
399 if (image->debug != MagickFalse)
400 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
401 data_size=(unsigned char) ReadBlobByte(image);
402 if (data_size > MaximumLZWBits)
403 ThrowBinaryException(CorruptImageError,"CorruptImage",image->filename);
404 lzw_info=AcquireLZWInfo(image,data_size);
405 if (lzw_info == (LZWInfo *) NULL)
406 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
407 image->filename);
408 exception=(&image->exception);
409 pass=0;
410 offset=0;
411 for (y=0; y < (long) image->rows; y++)
412 {
413 register IndexPacket
414 *__restrict indexes;
415
416 register long
417 x;
418
419 register PixelPacket
420 *__restrict q;
421
422 q=GetAuthenticPixels(image,0,offset,image->columns,1,exception);
423 if (q == (PixelPacket *) NULL)
424 break;
425 indexes=GetAuthenticIndexQueue(image);
426 for (x=0; x < (long) image->columns; )
427 {
428 c=ReadBlobLZWByte(lzw_info);
429 if (c < 0)
430 break;
431 index=ConstrainColormapIndex(image,(unsigned long) c);
432 q->red=image->colormap[(long) index].red;
433 q->green=image->colormap[(long) index].green;
434 q->blue=image->colormap[(long) index].blue;
435 q->opacity=(long) index == opacity ? (Quantum) TransparentOpacity :
436 (Quantum) OpaqueOpacity;
437 indexes[x]=index;
438 x++;
439 q++;
440 }
441 if (x < (long) image->columns)
442 break;
443 if (image->interlace == NoInterlace)
444 offset++;
445 else
446 switch (pass)
447 {
448 case 0:
449 default:
450 {
451 offset+=8;
452 if (offset >= (long) image->rows)
453 {
454 pass++;
455 offset=4;
456 }
457 break;
458 }
459 case 1:
460 {
461 offset+=8;
462 if (offset >= (long) image->rows)
463 {
464 pass++;
465 offset=2;
466 }
467 break;
468 }
469 case 2:
470 {
471 offset+=4;
472 if (offset >= (long) image->rows)
473 {
474 pass++;
475 offset=1;
476 }
477 break;
478 }
479 case 3:
480 {
481 offset+=2;
482 break;
483 }
484 }
485 if (SyncAuthenticPixels(image,exception) == MagickFalse)
486 break;
487 }
488 lzw_info=RelinquishLZWInfo(lzw_info);
489 if (y < (long) image->rows)
490 ThrowBinaryException(CorruptImageError,"CorruptImage",image->filename);
491 return(MagickTrue);
492}
493
494/*
495%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
496% %
497% %
498% %
499% E n c o d e I m a g e %
500% %
501% %
502% %
503%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
504%
505% EncodeImage compresses an image via GIF-coding.
506%
507% The format of the EncodeImage method is:
508%
509% MagickBooleanType EncodeImage(const ImageInfo *image_info,Image *image,
510% const unsigned long data_size)
511%
512% A description of each parameter follows:
513%
514% o image_info: the image info.
515%
516% o image: the address of a structure of type Image.
517%
518% o data_size: The number of bits in the compressed packet.
519%
520*/
521static MagickBooleanType EncodeImage(const ImageInfo *image_info,Image *image,
522 const unsigned long data_size)
523{
524#define MaxCode(number_bits) ((1UL << (number_bits))-1)
525#define MaxHashTable 5003
526#define MaxGIFBits 12UL
527#define MaxGIFTable (1UL << MaxGIFBits)
528#define GIFOutputCode(code) \
529{ \
530 /* \
531 Emit a code. \
532 */ \
533 if (bits > 0) \
534 datum|=(code) << bits; \
535 else \
536 datum=code; \
537 bits+=number_bits; \
538 while (bits >= 8) \
539 { \
540 /* \
541 Add a character to current packet. \
542 */ \
543 packet[length++]=(unsigned char) (datum & 0xff); \
544 if (length >= 254) \
545 { \
546 (void) WriteBlobByte(image,(unsigned char) length); \
547 (void) WriteBlob(image,length,packet); \
548 length=0; \
549 } \
550 datum>>=8; \
551 bits-=8; \
552 } \
553 if (free_code > max_code) \
554 { \
555 number_bits++; \
556 if (number_bits == MaxGIFBits) \
557 max_code=MaxGIFTable; \
558 else \
559 max_code=MaxCode(number_bits); \
560 } \
561}
562
563 IndexPacket
564 index;
565
566 long
567 displacement,
568 offset,
569 k,
570 y;
571
572 register long
573 i;
574
575 size_t
576 length;
577
578 short
579 *hash_code,
580 *hash_prefix,
581 waiting_code;
582
583 unsigned char
584 *packet,
585 *hash_suffix;
586
587 unsigned long
588 bits,
589 clear_code,
590 datum,
591 end_of_information_code,
592 free_code,
593 max_code,
594 next_pixel,
595 number_bits,
596 pass;
597
598 /*
599 Allocate encoder tables.
600 */
601 assert(image != (Image *) NULL);
602 packet=(unsigned char *) AcquireQuantumMemory(256,sizeof(*packet));
603 hash_code=(short *) AcquireQuantumMemory(MaxHashTable,sizeof(*hash_code));
604 hash_prefix=(short *) AcquireQuantumMemory(MaxHashTable,sizeof(*hash_prefix));
605 hash_suffix=(unsigned char *) AcquireQuantumMemory(MaxHashTable,
606 sizeof(*hash_suffix));
607 if ((packet == (unsigned char *) NULL) || (hash_code == (short *) NULL) ||
608 (hash_prefix == (short *) NULL) ||
609 (hash_suffix == (unsigned char *) NULL))
610 {
611 if (packet != (unsigned char *) NULL)
612 packet=(unsigned char *) RelinquishMagickMemory(packet);
613 if (hash_code != (short *) NULL)
614 hash_code=(short *) RelinquishMagickMemory(hash_code);
615 if (hash_prefix != (short *) NULL)
616 hash_prefix=(short *) RelinquishMagickMemory(hash_prefix);
617 if (hash_suffix != (unsigned char *) NULL)
618 hash_suffix=(unsigned char *) RelinquishMagickMemory(hash_suffix);
619 return(MagickFalse);
620 }
621 /*
622 Initialize GIF encoder.
623 */
624 number_bits=data_size;
625 max_code=MaxCode(number_bits);
626 clear_code=((short) 1UL << (data_size-1));
627 end_of_information_code=clear_code+1;
628 free_code=clear_code+2;
629 length=0;
630 datum=0;
631 bits=0;
632 for (i=0; i < MaxHashTable; i++)
633 hash_code[i]=0;
634 GIFOutputCode(clear_code);
635 /*
636 Encode pixels.
637 */
638 offset=0;
639 pass=0;
640 waiting_code=0;
641 for (y=0; y < (long) image->rows; y++)
642 {
643 register const IndexPacket
644 *__restrict indexes;
645
646 register const PixelPacket
647 *__restrict p;
648
649 register long
650 x;
651
652 p=GetVirtualPixels(image,0,offset,image->columns,1,&image->exception);
653 if (p == (const PixelPacket *) NULL)
654 break;
655 indexes=GetVirtualIndexQueue(image);
656 if (y == 0)
657 waiting_code=(short) (*indexes);
658 for (x=(y == 0) ? 1 : 0; x < (long) image->columns; x++)
659 {
660 /*
661 Probe hash table.
662 */
663 index=(IndexPacket) ((unsigned long) indexes[x] & 0xff);
664 p++;
665 k=(long) (((unsigned long) index << (MaxGIFBits-8))+waiting_code);
666 if (k >= MaxHashTable)
667 k-=MaxHashTable;
668 next_pixel=MagickFalse;
669 displacement=1;
670 if (hash_code[k] > 0)
671 {
672 if ((hash_prefix[k] == waiting_code) &&
673 (hash_suffix[k] == (unsigned char) index))
674 {
675 waiting_code=hash_code[k];
676 continue;
677 }
678 if (k != 0)
679 displacement=MaxHashTable-k;
680 for ( ; ; )
681 {
682 k-=displacement;
683 if (k < 0)
684 k+=MaxHashTable;
685 if (hash_code[k] == 0)
686 break;
687 if ((hash_prefix[k] == waiting_code) &&
688 (hash_suffix[k] == (unsigned char) index))
689 {
690 waiting_code=hash_code[k];
691 next_pixel=MagickTrue;
692 break;
693 }
694 }
695 if (next_pixel == MagickTrue)
696 continue;
697 }
698 GIFOutputCode((unsigned long) waiting_code);
699 if (free_code < MaxGIFTable)
700 {
701 hash_code[k]=(short) free_code++;
702 hash_prefix[k]=waiting_code;
703 hash_suffix[k]=(unsigned char) index;
704 }
705 else
706 {
707 /*
708 Fill the hash table with empty entries.
709 */
710 for (k=0; k < MaxHashTable; k++)
711 hash_code[k]=0;
712 /*
713 Reset compressor and issue a clear code.
714 */
715 free_code=clear_code+2;
716 GIFOutputCode(clear_code);
717 number_bits=data_size;
718 max_code=MaxCode(number_bits);
719 }
720 waiting_code=(short) index;
721 }
722 if (image_info->interlace == NoInterlace)
723 offset++;
724 else
725 switch (pass)
726 {
727 case 0:
728 default:
729 {
730 offset+=8;
731 if (offset >= (long) image->rows)
732 {
733 pass++;
734 offset=4;
735 }
736 break;
737 }
738 case 1:
739 {
740 offset+=8;
741 if (offset >= (long) image->rows)
742 {
743 pass++;
744 offset=2;
745 }
746 break;
747 }
748 case 2:
749 {
750 offset+=4;
751 if (offset >= (long) image->rows)
752 {
753 pass++;
754 offset=1;
755 }
756 break;
757 }
758 case 3:
759 {
760 offset+=2;
761 break;
762 }
763 }
764 }
765 /*
766 Flush out the buffered code.
767 */
768 GIFOutputCode((unsigned long) waiting_code);
769 GIFOutputCode(end_of_information_code);
770 if (bits > 0)
771 {
772 /*
773 Add a character to current packet.
774 */
775 packet[length++]=(unsigned char) (datum & 0xff);
776 if (length >= 254)
777 {
778 (void) WriteBlobByte(image,(unsigned char) length);
779 (void) WriteBlob(image,length,packet);
780 length=0;
781 }
782 }
783 /*
784 Flush accumulated data.
785 */
786 if (length > 0)
787 {
788 (void) WriteBlobByte(image,(unsigned char) length);
789 (void) WriteBlob(image,length,packet);
790 }
791 /*
792 Free encoder memory.
793 */
794 hash_suffix=(unsigned char *) RelinquishMagickMemory(hash_suffix);
795 hash_prefix=(short *) RelinquishMagickMemory(hash_prefix);
796 hash_code=(short *) RelinquishMagickMemory(hash_code);
797 packet=(unsigned char *) RelinquishMagickMemory(packet);
798 return(MagickTrue);
799}
800
801/*
802%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
803% %
804% %
805% %
806% I s G I F %
807% %
808% %
809% %
810%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
811%
812% IsGIF() returns MagickTrue if the image format type, identified by the
813% magick string, is GIF.
814%
815% The format of the IsGIF method is:
816%
817% MagickBooleanType IsGIF(const unsigned char *magick,const size_t length)
818%
819% A description of each parameter follows:
820%
821% o magick: compare image format pattern against these bytes.
822%
823% o length: Specifies the length of the magick string.
824%
825*/
826static MagickBooleanType IsGIF(const unsigned char *magick,const size_t length)
827{
828 if (length < 4)
829 return(MagickFalse);
830 if (LocaleNCompare((char *) magick,"GIF8",4) == 0)
831 return(MagickTrue);
832 return(MagickFalse);
833}
834
835/*
836%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
837% %
838% %
839% %
840+ R e a d B l o b B l o c k %
841% %
842% %
843% %
844%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
845%
846% ReadBlobBlock() reads data from the image file and returns it. The
847% amount of data is determined by first reading a count byte. The number
848% of bytes read is returned.
849%
850% The format of the ReadBlobBlock method is:
851%
852% size_t ReadBlobBlock(Image *image,unsigned char *data)
853%
854% A description of each parameter follows:
855%
856% o image: the image.
857%
858% o data: Specifies an area to place the information requested from
859% the file.
860%
861*/
862static ssize_t ReadBlobBlock(Image *image,unsigned char *data)
863{
864 ssize_t
865 count;
866
867 unsigned char
868 block_count;
869
870 assert(image != (Image *) NULL);
871 assert(image->signature == MagickSignature);
872 assert(data != (unsigned char *) NULL);
873 count=ReadBlob(image,1,&block_count);
874 if (count != 1)
875 return(0);
876 return(ReadBlob(image,(size_t) block_count,data));
877}
878
879/*
880%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
881% %
882% %
883% %
884% R e a d G I F I m a g e %
885% %
886% %
887% %
888%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
889%
890% ReadGIFImage() reads a Compuserve Graphics image file and returns it.
891% It allocates the memory necessary for the new Image structure and returns a
892% pointer to the new image.
893%
894% The format of the ReadGIFImage method is:
895%
896% Image *ReadGIFImage(const ImageInfo *image_info,ExceptionInfo *exception)
897%
898% A description of each parameter follows:
899%
900% o image_info: the image info.
901%
902% o exception: return any errors or warnings in this structure.
903%
904*/
905
906static inline size_t MagickMax(const size_t x,const size_t y)
907{
908 if (x > y)
909 return(x);
910 return(y);
911}
912
913static inline size_t MagickMin(const size_t x,const size_t y)
914{
915 if (x < y)
916 return(x);
917 return(y);
918}
919
920static MagickBooleanType PingGIFImage(Image *image)
921{
922 unsigned char
923 buffer[256],
924 length,
925 data_size;
926
927 assert(image != (Image *) NULL);
928 assert(image->signature == MagickSignature);
929 if (image->debug != MagickFalse)
930 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
931 if (ReadBlob(image,1,&data_size) != 1)
932 ThrowBinaryException(CorruptImageError,"CorruptImage",image->filename);
933 if (data_size > MaximumLZWBits)
934 ThrowBinaryException(CorruptImageError,"CorruptImage",image->filename);
935 if (ReadBlob(image,1,&length) != 1)
936 ThrowBinaryException(CorruptImageError,"CorruptImage",image->filename);
937 while (length != 0)
938 {
939 if (ReadBlob(image,length,buffer) != (ssize_t) length)
940 ThrowBinaryException(CorruptImageError,"CorruptImage",image->filename);
941 if (ReadBlob(image,1,&length) != 1)
942 ThrowBinaryException(CorruptImageError,"CorruptImage",image->filename);
943 }
944 return(MagickTrue);
945}
946
947static Image *ReadGIFImage(const ImageInfo *image_info,ExceptionInfo *exception)
948{
949#define BitSet(byte,bit) (((byte) & (bit)) == (bit))
950#define LSBFirstOrder(x,y) (((y) << 8) | (x))
951
952 Image
953 *image;
954
955 int
956 number_extensionss=0;
957
958 long
959 opacity;
960
961 MagickBooleanType
962 status;
963
964 RectangleInfo
965 page;
966
967 register long
968 i;
969
970 register unsigned char
971 *p;
972
973 ssize_t
974 count;
975
976 unsigned char
977 background,
978 c,
979 flag,
980 *global_colormap,
981 header[MaxTextExtent],
982 magick[12];
983
984 unsigned long
985 delay,
986 dispose,
987 global_colors,
988 image_count,
989 iterations;
990
991 /*
992 Open image file.
993 */
994 assert(image_info != (const ImageInfo *) NULL);
995 assert(image_info->signature == MagickSignature);
996 if (image_info->debug != MagickFalse)
997 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
998 image_info->filename);
999 assert(exception != (ExceptionInfo *) NULL);
1000 assert(exception->signature == MagickSignature);
1001 image=AcquireImage(image_info);
1002 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
1003 if (status == MagickFalse)
1004 {
1005 image=DestroyImageList(image);
1006 return((Image *) NULL);
1007 }
1008 /*
1009 Determine if this a GIF file.
1010 */
1011 count=ReadBlob(image,6,magick);
1012 if ((count != 6) || ((LocaleNCompare((char *) magick,"GIF87",5) != 0) &&
1013 (LocaleNCompare((char *) magick,"GIF89",5) != 0)))
1014 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1015 page.width=ReadBlobLSBShort(image);
1016 page.height=ReadBlobLSBShort(image);
1017 flag=(unsigned char) ReadBlobByte(image);
1018 background=(unsigned char) ReadBlobByte(image);
1019 c=(unsigned char) ReadBlobByte(image); /* reserved */
1020 global_colors=1UL << (((unsigned long) flag & 0x07)+1);
1021 global_colormap=(unsigned char *) AcquireQuantumMemory((size_t)
1022 MagickMax(global_colors,256),3UL*sizeof(*global_colormap));
1023 if (global_colormap == (unsigned char *) NULL)
1024 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1025 if (BitSet((int) flag,0x80) != 0)
1026 count=ReadBlob(image,(size_t) (3*global_colors),global_colormap);
1027 delay=0;
1028 dispose=0;
1029 iterations=1;
1030 opacity=(-1);
1031 image_count=0;
1032 for ( ; ; )
1033 {
1034 count=ReadBlob(image,1,&c);
1035 if (count != 1)
1036 break;
1037 if (c == (unsigned char) ';')
1038 break; /* terminator */
1039 if (c == (unsigned char) '!')
1040 {
1041 /*
1042 GIF Extension block.
1043 */
1044
1045 count=ReadBlob(image,1,&c);
1046 if (count != 1)
1047 {
1048 global_colormap=(unsigned char *) RelinquishMagickMemory(
1049 global_colormap);
1050 ThrowReaderException(CorruptImageError,
1051 "UnableToReadExtensionBlock");
1052 }
1053 switch (c)
1054 {
1055 case 0xf9:
1056 {
1057 /*
1058 Read graphics control extension.
1059 */
1060 while (ReadBlobBlock(image,header) != 0) ;
1061 dispose=(unsigned long) (header[0] >> 2);
1062 delay=(unsigned long) ((header[2] << 8) | header[1]);
1063 if ((long) (header[0] & 0x01) == 0x01)
1064 opacity=(long) header[3];
1065 break;
1066 }
1067 case 0xfe:
1068 {
1069 char
1070 *comments;
1071
1072 /*
1073 Read comment extension.
1074 */
1075 comments=AcquireString((char *) NULL);
1076 for ( ; ; )
1077 {
1078 count=(ssize_t) ReadBlobBlock(image,header);
1079 if (count == 0)
1080 break;
1081 header[count]='\0';
1082 (void) ConcatenateString(&comments,(const char *) header);
1083 }
1084 (void) SetImageProperty(image,"comment",comments);
1085 comments=DestroyString(comments);
1086 break;
1087 }
1088 case 0xff:
1089 {
1090 /* Read GIF application extension */
1091
1092 MagickBooleanType
1093 loop;
1094
1095 /*
1096 Read Netscape Loop extension.
1097 */
1098 loop=MagickFalse;
1099 if (ReadBlobBlock(image,header) != 0)
1100 loop=LocaleNCompare((char *) header,"NETSCAPE2.0",11) == 0 ?
1101 MagickTrue : MagickFalse;
1102 if (loop != MagickFalse)
1103 {
1104 while (ReadBlobBlock(image,header) != 0)
1105 iterations=(unsigned long) ((header[2] << 8) | header[1]);
1106 break;
1107 }
1108 else
1109 {
1110 char
1111 name[MaxTextExtent];
1112
1113 int
1114 block_length,
1115 info_length,
1116 reserved_length;
1117
1118 MagickBooleanType
1119 i8bim,
1120 icc,
1121 iptc;
1122
1123 StringInfo
1124 *profile;
1125
1126 unsigned char
1127 *info;
1128
1129 /*
1130 Store GIF application extension as a generic profile.
1131 */
1132 i8bim=LocaleNCompare((char *) header,"MGK8BIM0000",11) == 0 ?
1133 MagickTrue : MagickFalse;
1134 icc=LocaleNCompare((char *) header,"ICCRGBG1012",11) == 0 ?
1135 MagickTrue : MagickFalse;
1136 iptc=LocaleNCompare((char *) header,"MGKIPTC0000",11) == 0 ?
1137 MagickTrue : MagickFalse;
1138 number_extensionss++;
1139 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1140 " Reading GIF application extension");
1141 info=(unsigned char *) AcquireQuantumMemory(255UL,
1142 sizeof(*info));
1143 reserved_length=255;
1144 for (info_length=0; ; )
1145 {
1146 block_length=(int) ReadBlobBlock(image,&info[info_length]);
1147 if (block_length == 0)
1148 break;
1149 info_length+=block_length;
1150 if (info_length > (reserved_length-255))
1151 {
1152 reserved_length+=4096;
1153 info=(unsigned char *) ResizeQuantumMemory(info,
1154 (size_t) reserved_length,sizeof(*info));
1155 }
1156 }
1157 info=(unsigned char *) ResizeQuantumMemory(info,(size_t)
1158 (info_length+1),sizeof(*info));
1159 profile=AcquireStringInfo((size_t) info_length);
1160 SetStringInfoDatum(profile,(const unsigned char *) info);
1161 if (i8bim == MagickTrue)
1162 (void) CopyMagickString(name,"8bim",sizeof(name));
1163 else if (icc == MagickTrue)
1164 (void) CopyMagickString(name,"icc",sizeof(name));
1165 else if (iptc == MagickTrue)
1166 (void) CopyMagickString(name,"iptc",sizeof(name));
1167 else
1168 (void) FormatMagickString(name,sizeof(name),"gif:%.11s",
1169 header);
1170 (void) SetImageProfile(image,name,profile);
1171 info=(unsigned char *) RelinquishMagickMemory(info);
1172 profile=DestroyStringInfo(profile);
1173 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1174 " profile name=%s",name);
1175 }
1176 break;
1177 }
1178 default:
1179 {
1180 while (ReadBlobBlock(image,header) != 0) ;
1181 break;
1182 }
1183 }
1184 }
1185 if (c != (unsigned char) ',')
1186 continue;
1187 if (image_count != 0)
1188 {
1189 /*
1190 Allocate next image structure.
1191 */
1192 AcquireNextImage(image_info,image);
1193 if (GetNextImageInList(image) == (Image *) NULL)
1194 {
1195 image=DestroyImageList(image);
1196 global_colormap=(unsigned char *) RelinquishMagickMemory(
1197 global_colormap);
1198 return((Image *) NULL);
1199 }
1200 image=SyncNextImageInList(image);
1201 }
1202 image_count++;
1203 /*
1204 Read image attributes.
1205 */
1206 image->storage_class=PseudoClass;
1207 image->compression=LZWCompression;
1208 page.x=(long) ReadBlobLSBShort(image);
1209 page.y=(long) ReadBlobLSBShort(image);
1210 image->columns=ReadBlobLSBShort(image);
1211 image->rows=ReadBlobLSBShort(image);
1212 image->depth=8;
1213 flag=(unsigned char) ReadBlobByte(image);
1214 image->interlace=BitSet((int) flag,0x40) != 0 ? GIFInterlace :
1215 NoInterlace;
1216 image->colors=BitSet((int) flag,0x80) == 0 ? global_colors :
1217 1UL << ((unsigned long) (flag & 0x07)+1);
1218 if (opacity >= (long) image->colors)
1219 opacity=(-1);
1220 image->page.width=page.width;
1221 image->page.height=page.height;
1222 image->page.y=page.y;
1223 image->page.x=page.x;
1224 image->delay=delay;
1225 image->ticks_per_second=100;
1226 image->dispose=(DisposeType) dispose;
1227 image->iterations=iterations;
1228 image->matte=opacity >= 0 ? MagickTrue : MagickFalse;
1229 delay=0;
1230 dispose=0;
1231 iterations=1;
1232 if ((image->columns == 0) || (image->rows == 0))
1233 {
1234 global_colormap=(unsigned char *) RelinquishMagickMemory(
1235 global_colormap);
1236 ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
1237 }
1238 /*
1239 Inititialize colormap.
1240 */
1241 if (AcquireImageColormap(image,image->colors) == MagickFalse)
1242 {
1243 global_colormap=(unsigned char *) RelinquishMagickMemory(
1244 global_colormap);
1245 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1246 }
1247 if (BitSet((int) flag,0x80) == 0)
1248 {
1249 /*
1250 Use global colormap.
1251 */
1252 p=global_colormap;
1253 for (i=0; i < (long) image->colors; i++)
1254 {
1255 image->colormap[i].red=ScaleCharToQuantum(*p++);
1256 image->colormap[i].green=ScaleCharToQuantum(*p++);
1257 image->colormap[i].blue=ScaleCharToQuantum(*p++);
1258 if (i == opacity)
1259 {
1260 image->colormap[i].opacity=(Quantum) TransparentOpacity;
1261 image->transparent_color=image->colormap[opacity];
1262 }
1263 }
1264 image->background_color=image->colormap[MagickMin(background,
1265 image->colors-1)];
1266 }
1267 else
1268 {
1269 unsigned char
1270 *colormap;
1271
1272 /*
1273 Read local colormap.
1274 */
1275 colormap=(unsigned char *) AcquireQuantumMemory(image->colors,
1276 3*sizeof(*colormap));
1277 if (colormap == (unsigned char *) NULL)
1278 {
1279 global_colormap=(unsigned char *) RelinquishMagickMemory(
1280 global_colormap);
1281 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1282 }
1283 count=ReadBlob(image,(3*image->colors)*sizeof(*colormap),colormap);
1284 if (count != (ssize_t) (3*image->colors))
1285 {
1286 global_colormap=(unsigned char *) RelinquishMagickMemory(
1287 global_colormap);
1288 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
1289 ThrowReaderException(CorruptImageError,
1290 "InsufficientImageDataInFile");
1291 }
1292 p=colormap;
1293 for (i=0; i < (long) image->colors; i++)
1294 {
1295 image->colormap[i].red=ScaleCharToQuantum(*p++);
1296 image->colormap[i].green=ScaleCharToQuantum(*p++);
1297 image->colormap[i].blue=ScaleCharToQuantum(*p++);
1298 if (i == opacity)
1299 image->colormap[i].opacity=(Quantum) TransparentOpacity;
1300 }
1301 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
1302 }
1303 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
1304 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1305 break;
1306 /*
1307 Decode image.
1308 */
1309 if (image_info->ping != MagickFalse)
1310 status=PingGIFImage(image);
1311 else
1312 status=DecodeImage(image,opacity);
1313 if ((image_info->ping == MagickFalse) && (status == MagickFalse))
1314 {
1315 global_colormap=(unsigned char *) RelinquishMagickMemory(
1316 global_colormap);
1317 ThrowReaderException(CorruptImageError,"CorruptImage");
1318 }
1319 if (image_info->number_scenes != 0)
1320 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1321 break;
1322 opacity=(-1);
1323 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) image->scene-
1324 1,image->scene);
1325 if (status == MagickFalse)
1326 break;
1327 }
1328 global_colormap=(unsigned char *) RelinquishMagickMemory(global_colormap);
1329 if ((image->columns == 0) || (image->rows == 0))
1330 ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
1331 (void) CloseBlob(image);
1332 return(GetFirstImageInList(image));
1333}
1334
1335/*
1336%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1337% %
1338% %
1339% %
1340% R e g i s t e r G I F I m a g e %
1341% %
1342% %
1343% %
1344%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1345%
1346% RegisterGIFImage() adds properties for the GIF image format to
1347% the list of supported formats. The properties include the image format
1348% tag, a method to read and/or write the format, whether the format
1349% supports the saving of more than one frame to the same file or blob,
1350% whether the format supports native in-memory I/O, and a brief
1351% description of the format.
1352%
1353% The format of the RegisterGIFImage method is:
1354%
1355% unsigned long RegisterGIFImage(void)
1356%
1357*/
1358ModuleExport unsigned long RegisterGIFImage(void)
1359{
1360 MagickInfo
1361 *entry;
1362
1363 entry=SetMagickInfo("GIF");
1364 entry->decoder=(DecodeImageHandler *) ReadGIFImage;
1365 entry->encoder=(EncodeImageHandler *) WriteGIFImage;
1366 entry->magick=(IsImageFormatHandler *) IsGIF;
1367 entry->description=ConstantString("CompuServe graphics interchange format");
1368 entry->module=ConstantString("GIF");
1369 (void) RegisterMagickInfo(entry);
1370 entry=SetMagickInfo("GIF87");
1371 entry->decoder=(DecodeImageHandler *) ReadGIFImage;
1372 entry->encoder=(EncodeImageHandler *) WriteGIFImage;
1373 entry->magick=(IsImageFormatHandler *) IsGIF;
1374 entry->adjoin=MagickFalse;
1375 entry->description=ConstantString("CompuServe graphics interchange format");
1376 entry->version=ConstantString("version 87a");
1377 entry->module=ConstantString("GIF");
1378 (void) RegisterMagickInfo(entry);
1379 return(MagickImageCoderSignature);
1380}
1381
1382/*
1383%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1384% %
1385% %
1386% %
1387% U n r e g i s t e r G I F I m a g e %
1388% %
1389% %
1390% %
1391%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1392%
1393% UnregisterGIFImage() removes format registrations made by the
1394% GIF module from the list of supported formats.
1395%
1396% The format of the UnregisterGIFImage method is:
1397%
1398% UnregisterGIFImage(void)
1399%
1400*/
1401ModuleExport void UnregisterGIFImage(void)
1402{
1403 (void) UnregisterMagickInfo("GIF");
1404 (void) UnregisterMagickInfo("GIF87");
1405}
1406
1407/*
1408%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1409% %
1410% %
1411% %
1412% W r i t e G I F I m a g e %
1413% %
1414% %
1415% %
1416%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1417%
1418% WriteGIFImage() writes an image to a file in the Compuserve Graphics
1419% image format.
1420%
1421% The format of the WriteGIFImage method is:
1422%
1423% MagickBooleanType WriteGIFImage(const ImageInfo *image_info,Image *image)
1424%
1425% A description of each parameter follows.
1426%
1427% o image_info: the image info.
1428%
1429% o image: The image.
1430%
1431*/
1432static MagickBooleanType WriteGIFImage(const ImageInfo *image_info,Image *image)
1433{
1434 Image
1435 *next_image;
1436
1437 int
1438 c;
1439
1440 long
1441 j,
1442 opacity;
1443
1444 ImageInfo
1445 *write_info;
1446
1447 InterlaceType
1448 interlace;
1449
1450 MagickBooleanType
1451 status;
1452
1453 MagickOffsetType
1454 scene;
1455
1456 RectangleInfo
1457 page;
1458
1459 register long
1460 i;
1461
1462 register unsigned char
1463 *q;
1464
1465 size_t
1466 length;
1467
1468 unsigned char
1469 *colormap,
1470 *global_colormap;
1471
1472 unsigned long
1473 bits_per_pixel,
1474 delay;
1475
1476 /*
1477 Open output image file.
1478 */
1479 assert(image_info != (const ImageInfo *) NULL);
1480 assert(image_info->signature == MagickSignature);
1481 assert(image != (Image *) NULL);
1482 assert(image->signature == MagickSignature);
1483 if (image->debug != MagickFalse)
1484 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1485 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
1486 if (status == MagickFalse)
1487 return(status);
1488 /*
1489 Allocate colormap.
1490 */
1491 global_colormap=(unsigned char *) AcquireQuantumMemory(768UL,
1492 sizeof(*global_colormap));
1493 colormap=(unsigned char *) AcquireQuantumMemory(768UL,sizeof(*colormap));
1494 if ((global_colormap == (unsigned char *) NULL) ||
1495 (colormap == (unsigned char *) NULL))
1496 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1497 for (i=0; i < 768; i++)
1498 colormap[i]=(unsigned char) 0;
1499 /*
1500 Write GIF header.
1501 */
1502 write_info=CloneImageInfo(image_info);
1503 if (LocaleCompare(write_info->magick,"GIF87") != 0)
1504 (void) WriteBlob(image,6,(unsigned char *) "GIF89a");
1505 else
1506 {
1507 (void) WriteBlob(image,6,(unsigned char *) "GIF87a");
1508 write_info->adjoin=MagickFalse;
1509 }
1510 /*
1511 Determine image bounding box.
1512 */
1513 page.width=image->columns;
1514 page.height=image->rows;
1515 page.x=0;
1516 page.y=0;
1517 if (write_info->adjoin != MagickFalse)
1518 for (next_image=image; next_image != (Image *) NULL; )
1519 {
1520 page.x=next_image->page.x;
1521 page.y=next_image->page.y;
1522 if ((next_image->page.width+page.x) > page.width)
1523 page.width=next_image->page.width+page.x;
1524 if ((next_image->page.height+page.y) > page.height)
1525 page.height=next_image->page.height+page.y;
1526 next_image=GetNextImageInList(next_image);
1527 }
1528 page.x=image->page.x;
1529 page.y=image->page.y;
1530 if ((image->page.width != 0) && (image->page.height != 0))
1531 page=image->page;
1532 (void) WriteBlobLSBShort(image,(unsigned short) page.width);
1533 (void) WriteBlobLSBShort(image,(unsigned short) page.height);
1534 /*
1535 Write images to file.
1536 */
1537 interlace=write_info->interlace;
1538 if ((write_info->adjoin != MagickFalse) &&
1539 (GetNextImageInList(image) != (Image *) NULL))
1540 interlace=NoInterlace;
1541 scene=0;
1542 do
1543 {
1544 if (image->colorspace != RGBColorspace)
1545 (void) TransformImageColorspace(image,RGBColorspace);
1546 opacity=(-1);
1547 if (IsOpaqueImage(image,&image->exception) != MagickFalse)
1548 {
1549 if ((image->storage_class == DirectClass) || (image->colors > 256))
1550 (void) SetImageType(image,PaletteType);
1551 }
1552 else
1553 {
1554 MagickRealType
1555 alpha,
1556 beta;
1557
1558 /*
1559 Identify transparent colormap index.
1560 */
1561 if ((image->storage_class == DirectClass) || (image->colors > 256))
1562 (void) SetImageType(image,PaletteBilevelMatteType);
1563 for (i=0; i < (long) image->colors; i++)
1564 if (image->colormap[i].opacity != OpaqueOpacity)
1565 {
1566 if (opacity < 0)
1567 {
1568 opacity=i;
1569 continue;
1570 }
1571 alpha=(MagickRealType) TransparentOpacity-(MagickRealType)
1572 image->colormap[i].opacity;
1573 beta=(MagickRealType) TransparentOpacity-(MagickRealType)
1574 image->colormap[opacity].opacity;
1575 if (alpha < beta)
1576 opacity=i;
1577 }
1578 if (opacity == -1)
1579 {
1580 (void) SetImageType(image,PaletteBilevelMatteType);
1581 for (i=0; i < (long) image->colors; i++)
1582 if (image->colormap[i].opacity != OpaqueOpacity)
1583 {
1584 if (opacity < 0)
1585 {
1586 opacity=i;
1587 continue;
1588 }
1589 alpha=(Quantum) TransparentOpacity-(MagickRealType)
1590 image->colormap[i].opacity;
1591 beta=(Quantum) TransparentOpacity-(MagickRealType)
1592 image->colormap[opacity].opacity;
1593 if (alpha < beta)
1594 opacity=i;
1595 }
1596 }
1597 if (opacity >= 0)
1598 {
1599 image->colormap[opacity].red=image->transparent_color.red;
1600 image->colormap[opacity].green=image->transparent_color.green;
1601 image->colormap[opacity].blue=image->transparent_color.blue;
1602 }
1603 }
1604 if ((image->storage_class == DirectClass) || (image->colors > 256))
1605 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1606 for (bits_per_pixel=1; bits_per_pixel < 8; bits_per_pixel++)
1607 if ((1UL << bits_per_pixel) >= image->colors)
1608 break;
1609 q=colormap;
1610 for (i=0; i < (long) image->colors; i++)
1611 {
1612 *q++=ScaleQuantumToChar(image->colormap[i].red);
1613 *q++=ScaleQuantumToChar(image->colormap[i].green);
1614 *q++=ScaleQuantumToChar(image->colormap[i].blue);
1615 }
1616 for ( ; i < (long) (1UL << bits_per_pixel); i++)
1617 {
1618 *q++=(unsigned char) 0x0;
1619 *q++=(unsigned char) 0x0;
1620 *q++=(unsigned char) 0x0;
1621 }
1622 if ((GetPreviousImageInList(image) == (Image *) NULL) ||
1623 (write_info->adjoin == MagickFalse))
1624 {
1625 /*
1626 Write global colormap.
1627 */
1628 c=0x80;
1629 c|=(8-1) << 4; /* color resolution */
1630 c|=(bits_per_pixel-1); /* size of global colormap */
1631 (void) WriteBlobByte(image,(unsigned char) c);
1632 for (j=0; j < (long) image->colors; j++)
1633 if (IsColorEqual(&image->background_color,image->colormap+j))
1634 break;
1635 (void) WriteBlobByte(image,(unsigned char)
1636 (j == (long) image->colors ? 0 : j)); /* background color */
1637 (void) WriteBlobByte(image,(unsigned char) 0x00); /* reserved */
1638 length=(size_t) (3*(1UL << bits_per_pixel));
1639 (void) WriteBlob(image,length,colormap);
1640 for (j=0; j < 768; j++)
1641 global_colormap[j]=colormap[j];
1642 }
1643 if (LocaleCompare(write_info->magick,"GIF87") != 0)
1644 {
1645 /*
1646 Write graphics control extension.
1647 */
1648 (void) WriteBlobByte(image,(unsigned char) 0x21);
1649 (void) WriteBlobByte(image,(unsigned char) 0xf9);
1650 (void) WriteBlobByte(image,(unsigned char) 0x04);
1651 c=image->dispose << 2;
1652 if (opacity >= 0)
1653 c|=0x01;
1654 (void) WriteBlobByte(image,(unsigned char) c);
1655 delay=(unsigned long) (100*image->delay/MagickMax((size_t)
1656 image->ticks_per_second,1));
1657 (void) WriteBlobLSBShort(image,(unsigned short) delay);
1658 (void) WriteBlobByte(image,(unsigned char) (opacity >= 0 ? opacity :
1659 0));
1660 (void) WriteBlobByte(image,(unsigned char) 0x00);
1661 if ((LocaleCompare(write_info->magick,"GIF87") != 0) &&
1662 (GetImageProperty(image,"comment") != (const char *) NULL))
1663 {
1664 const char
1665 *value;
1666
1667 register const char
1668 *p;
1669
1670 size_t
1671 count;
1672
1673 /*
1674 Write Comment extension.
1675 */
1676 (void) WriteBlobByte(image,(unsigned char) 0x21);
1677 (void) WriteBlobByte(image,(unsigned char) 0xfe);
1678 value=GetImageProperty(image,"comment");
1679 p=value;
1680 while (strlen(p) != 0)
1681 {
1682 count=MagickMin(strlen(p),255);
1683 (void) WriteBlobByte(image,(unsigned char) count);
1684 for (i=0; i < (long) count; i++)
1685 (void) WriteBlobByte(image,(unsigned char) *p++);
1686 }
1687 (void) WriteBlobByte(image,(unsigned char) 0x00);
1688 }
1689 if ((GetPreviousImageInList(image) == (Image *) NULL) &&
1690 (GetNextImageInList(image) != (Image *) NULL) &&
1691 (image->iterations != 1))
1692 {
1693 /*
1694 Write Netscape Loop extension.
1695 */
1696 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1697 " Writing GIF Extension %s","NETSCAPE2.0");
1698 (void) WriteBlobByte(image,(unsigned char) 0x21);
1699 (void) WriteBlobByte(image,(unsigned char) 0xff);
1700 (void) WriteBlobByte(image,(unsigned char) 0x0b);
1701 (void) WriteBlob(image,11,(unsigned char *) "NETSCAPE2.0");
1702 (void) WriteBlobByte(image,(unsigned char) 0x03);
1703 (void) WriteBlobByte(image,(unsigned char) 0x01);
1704 (void) WriteBlobLSBShort(image,(unsigned short) image->iterations);
1705 (void) WriteBlobByte(image,(unsigned char) 0x00);
1706 }
1707 ResetImageProfileIterator(image);
1708 for ( ; ; )
1709 {
1710 char
1711 *name;
1712
1713 const StringInfo
1714 *profile;
1715
1716 name=GetNextImageProfile(image);
1717 if (name == (const char *) NULL)
1718 break;
1719 profile=GetImageProfile(image,name);
1720 if (profile != (StringInfo *) NULL)
1721 {
1722 if ((LocaleCompare(name,"ICC") == 0) ||
1723 (LocaleCompare(name,"ICM") == 0) ||
1724 (LocaleCompare(name,"IPTC") == 0) ||
1725 (LocaleCompare(name,"8BIM") == 0) ||
1726 (LocaleNCompare(name,"gif:",4) == 0))
1727 {
1728 size_t
1729 length;
1730
1731 ssize_t
1732 offset;
1733
1734 unsigned char
1735 *datum;
1736
1737 datum=GetStringInfoDatum(profile);
1738 length=GetStringInfoLength(profile);
1739 (void) WriteBlobByte(image,(unsigned char) 0x21);
1740 (void) WriteBlobByte(image,(unsigned char) 0xff);
1741 (void) WriteBlobByte(image,(unsigned char) 0x0b);
1742 if ((LocaleCompare(name,"ICC") == 0) ||
1743 (LocaleCompare(name,"ICM") == 0))
1744 {
1745 /*
1746 Write ICC extension.
1747 */
1748 (void) WriteBlob(image,11,(unsigned char *)"ICCRGBG1012");
1749 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1750 " Writing GIF Extension %s","ICCRGBG1012");
1751 }
1752 else
1753 if ((LocaleCompare(name,"IPTC") == 0))
1754 {
1755 /*
1756 write IPTC extension.
1757 */
1758 (void) WriteBlob(image,11,(unsigned char *)"MGKIPTC0000");
1759 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1760 " Writing GIF Extension %s","MGKIPTC0000");
1761 }
1762 else
1763 if ((LocaleCompare(name,"8BIM") == 0))
1764 {
1765 /*
1766 Write 8BIM extension>
1767 */
1768 (void) WriteBlob(image,11,(unsigned char *)
1769 "MGK8BIM0000");
1770 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1771 " Writing GIF Extension %s","MGK8BIM0000");
1772 }
1773 else
1774 {
1775 char
1776 extension[MaxTextExtent];
1777
1778 /* write generic extension */
1779 (void) CopyMagickString(extension,name+4,
1780 sizeof(extension));
1781 (void) WriteBlob(image,11,(unsigned char *) extension);
1782 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1783 " Writing GIF Extension %s",name);
1784 }
1785 offset=0;
1786 while ((ssize_t) length > offset)
1787 {
1788 size_t
1789 block_length;
1790
1791 if ((length-offset) < 255)
1792 block_length=length-offset;
1793 else
1794 block_length=255;
1795 (void) WriteBlobByte(image,(unsigned char) block_length);
1796 (void) WriteBlob(image,(size_t) block_length,datum+offset);
1797 offset+=(ssize_t) block_length;
1798 }
1799 (void) WriteBlobByte(image,(unsigned char) 0x00);
1800 }
1801 }
1802 }
1803 }
1804 (void) WriteBlobByte(image,','); /* image separator */
1805 /*
1806 Write the image header.
1807 */
1808 page.x=image->page.x;
1809 page.y=image->page.y;
1810 if ((image->page.width != 0) && (image->page.height != 0))
1811 page=image->page;
1812 (void) WriteBlobLSBShort(image,(unsigned short) (page.x < 0 ? 0 : page.x));
1813 (void) WriteBlobLSBShort(image,(unsigned short) (page.y < 0 ? 0 : page.y));
1814 (void) WriteBlobLSBShort(image,(unsigned short) image->columns);
1815 (void) WriteBlobLSBShort(image,(unsigned short) image->rows);
1816 c=0x00;
1817 if (interlace != NoInterlace)
1818 c|=0x40; /* pixel data is interlaced */
1819 for (j=0; j < (long) (3*image->colors); j++)
1820 if (colormap[j] != global_colormap[j])
1821 break;
1822 if (j == (long) (3*image->colors))
1823 (void) WriteBlobByte(image,(unsigned char) c);
1824 else
1825 {
1826 c|=0x80;
1827 c|=(bits_per_pixel-1); /* size of local colormap */
1828 (void) WriteBlobByte(image,(unsigned char) c);
1829 length=(size_t) (3*(1UL << bits_per_pixel));
1830 (void) WriteBlob(image,length,colormap);
1831 }
1832 /*
1833 Write the image data.
1834 */
1835 c=(int) MagickMax(bits_per_pixel,2);
1836 (void) WriteBlobByte(image,(unsigned char) c);
1837 status=EncodeImage(write_info,image,(unsigned long)
1838 MagickMax(bits_per_pixel,2)+1);
1839 if (status == MagickFalse)
1840 {
1841 global_colormap=(unsigned char *) RelinquishMagickMemory(
1842 global_colormap);
1843 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
1844 write_info=DestroyImageInfo(write_info);
1845 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1846 }
1847 (void) WriteBlobByte(image,(unsigned char) 0x00);
1848 if (GetNextImageInList(image) == (Image *) NULL)
1849 break;
1850 image=SyncNextImageInList(image);
1851 scene++;
1852 status=SetImageProgress(image,SaveImagesTag,scene,
1853 GetImageListLength(image));
1854 if (status == MagickFalse)
1855 break;
1856 } while (write_info->adjoin != MagickFalse);
1857 (void) WriteBlobByte(image,';'); /* terminator */
1858 global_colormap=(unsigned char *) RelinquishMagickMemory(global_colormap);
1859 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
1860 write_info=DestroyImageInfo(write_info);
1861 (void) CloseBlob(image);
1862 return(MagickTrue);
1863}