2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write Compuserv Graphics Interchange Format %
20 % Copyright 1999-2010 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
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. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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/colormap-private.h"
48 #include "magick/color-private.h"
49 #include "magick/colorspace.h"
50 #include "magick/exception.h"
51 #include "magick/exception-private.h"
52 #include "magick/image.h"
53 #include "magick/image-private.h"
54 #include "magick/list.h"
55 #include "magick/profile.h"
56 #include "magick/magick.h"
57 #include "magick/memory_.h"
58 #include "magick/monitor.h"
59 #include "magick/monitor-private.h"
60 #include "magick/option.h"
61 #include "magick/property.h"
62 #include "magick/quantize.h"
63 #include "magick/quantum-private.h"
64 #include "magick/static.h"
65 #include "magick/string_.h"
66 #include "magick/module.h"
71 #define MaximumLZWBits 12
72 #define MaximumLZWCode (1UL << MaximumLZWBits)
77 typedef struct _LZWCodeInfo
90 typedef struct _LZWStack
98 typedef struct _LZWInfo
126 Forward declarations.
129 GetNextLZWCode(LZWInfo *,const unsigned long);
131 static MagickBooleanType
132 WriteGIFImage(const ImageInfo *,Image *);
135 ReadBlobBlock(Image *,unsigned char *);
138 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
142 % D e c o d e I m a g e %
146 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
148 % DecodeImage uncompresses an image via GIF-coding.
150 % The format of the DecodeImage method is:
152 % MagickBooleanType DecodeImage(Image *image,const long opacity)
154 % A description of each parameter follows:
156 % o image: the address of a structure of type Image.
158 % o opacity: The colormap index associated with the transparent color.
162 static LZWInfo *RelinquishLZWInfo(LZWInfo *lzw_info)
164 if (lzw_info->table[0] != (unsigned long *) NULL)
165 lzw_info->table[0]=(unsigned long *) RelinquishMagickMemory(
167 if (lzw_info->table[1] != (unsigned long *) NULL)
168 lzw_info->table[1]=(unsigned long *) RelinquishMagickMemory(
170 if (lzw_info->stack != (LZWStack *) NULL)
172 if (lzw_info->stack->codes != (unsigned long *) NULL)
173 lzw_info->stack->codes=(unsigned long *) RelinquishMagickMemory(
174 lzw_info->stack->codes);
175 lzw_info->stack=(LZWStack *) RelinquishMagickMemory(lzw_info->stack);
177 lzw_info=(LZWInfo *) RelinquishMagickMemory(lzw_info);
178 return((LZWInfo *) NULL);
181 static inline void ResetLZWInfo(LZWInfo *lzw_info)
183 lzw_info->bits=lzw_info->data_size+1;
184 lzw_info->maximum_code=1UL << lzw_info->bits;
185 lzw_info->slot=lzw_info->maximum_data_value+3;
186 lzw_info->genesis=MagickTrue;
189 static LZWInfo *AcquireLZWInfo(Image *image,const unsigned long data_size)
197 lzw_info=(LZWInfo *) AcquireAlignedMemory(1,sizeof(*lzw_info));
198 if (lzw_info == (LZWInfo *) NULL)
199 return((LZWInfo *) NULL);
200 (void) ResetMagickMemory(lzw_info,0,sizeof(*lzw_info));
201 lzw_info->image=image;
202 lzw_info->data_size=data_size;
203 lzw_info->maximum_data_value=(1UL << data_size)-1;
204 lzw_info->clear_code=lzw_info->maximum_data_value+1;
205 lzw_info->end_code=lzw_info->maximum_data_value+2;
206 lzw_info->table[0]=(unsigned long *) AcquireQuantumMemory(MaximumLZWCode,
207 sizeof(*lzw_info->table));
208 lzw_info->table[1]=(unsigned long *) AcquireQuantumMemory(MaximumLZWCode,
209 sizeof(*lzw_info->table));
210 if ((lzw_info->table[0] == (unsigned long *) NULL) ||
211 (lzw_info->table[1] == (unsigned long *) NULL))
213 lzw_info=RelinquishLZWInfo(lzw_info);
214 return((LZWInfo *) NULL);
216 for (i=0; i <= (long) lzw_info->maximum_data_value; i++)
218 lzw_info->table[0][i]=0;
219 lzw_info->table[1][i]=(unsigned long) i;
221 ResetLZWInfo(lzw_info);
222 lzw_info->code_info.buffer[0]='\0';
223 lzw_info->code_info.buffer[1]='\0';
224 lzw_info->code_info.count=2;
225 lzw_info->code_info.bit=8*lzw_info->code_info.count;
226 lzw_info->code_info.eof=MagickFalse;
227 lzw_info->genesis=MagickTrue;
228 lzw_info->stack=(LZWStack *) AcquireAlignedMemory(1,sizeof(*lzw_info->stack));
229 if (lzw_info->stack == (LZWStack *) NULL)
231 lzw_info=RelinquishLZWInfo(lzw_info);
232 return((LZWInfo *) NULL);
234 lzw_info->stack->codes=(unsigned long *) AcquireQuantumMemory(2UL*
235 MaximumLZWCode,sizeof(*lzw_info->stack->codes));
236 if (lzw_info->stack->codes == (unsigned long *) NULL)
238 lzw_info=RelinquishLZWInfo(lzw_info);
239 return((LZWInfo *) NULL);
241 lzw_info->stack->index=lzw_info->stack->codes;
242 lzw_info->stack->top=lzw_info->stack->codes+2*MaximumLZWCode;
246 static inline int GetNextLZWCode(LZWInfo *lzw_info,const unsigned long bits)
254 while (((lzw_info->code_info.bit+bits) > (8*lzw_info->code_info.count)) &&
255 (lzw_info->code_info.eof == MagickFalse))
260 lzw_info->code_info.buffer[0]=lzw_info->code_info.buffer[
261 lzw_info->code_info.count-2];
262 lzw_info->code_info.buffer[1]=lzw_info->code_info.buffer[
263 lzw_info->code_info.count-1];
264 lzw_info->code_info.bit-=8*(lzw_info->code_info.count-2);
265 lzw_info->code_info.count=2;
266 count=ReadBlobBlock(lzw_info->image,&lzw_info->code_info.buffer[
267 lzw_info->code_info.count]);
269 lzw_info->code_info.count+=count;
271 lzw_info->code_info.eof=MagickTrue;
273 if ((lzw_info->code_info.bit+bits) > (8*lzw_info->code_info.count))
276 for (i=0; i < (long) bits; i++)
278 code|=((lzw_info->code_info.buffer[lzw_info->code_info.bit/8] &
279 (1UL << (lzw_info->code_info.bit % 8))) != 0) << i;
280 lzw_info->code_info.bit++;
285 static inline long PopLZWStack(LZWStack *stack_info)
287 if (stack_info->index <= stack_info->codes)
290 return((long) *stack_info->index);
293 static inline void PushLZWStack(LZWStack *stack_info,const unsigned long value)
295 if (stack_info->index >= stack_info->top)
297 *stack_info->index=value;
301 static int ReadBlobLZWByte(LZWInfo *lzw_info)
312 if (lzw_info->stack->index != lzw_info->stack->codes)
313 return(PopLZWStack(lzw_info->stack));
314 if (lzw_info->genesis != MagickFalse)
316 lzw_info->genesis=MagickFalse;
319 lzw_info->first_code=(unsigned long) GetNextLZWCode(lzw_info,
321 lzw_info->last_code=lzw_info->first_code;
322 } while (lzw_info->first_code == lzw_info->clear_code);
323 return((int) lzw_info->first_code);
325 code=GetNextLZWCode(lzw_info,lzw_info->bits);
328 if ((unsigned long) code == lzw_info->clear_code)
330 ResetLZWInfo(lzw_info);
331 return(ReadBlobLZWByte(lzw_info));
333 if ((unsigned long) code == lzw_info->end_code)
335 if ((unsigned long) code < lzw_info->slot)
336 value=(unsigned long) code;
339 PushLZWStack(lzw_info->stack,lzw_info->first_code);
340 value=lzw_info->last_code;
343 while (value > lzw_info->maximum_data_value)
345 if ((size_t) count > MaximumLZWCode)
348 if ((size_t) value > MaximumLZWCode)
350 PushLZWStack(lzw_info->stack,lzw_info->table[1][value]);
351 value=lzw_info->table[0][value];
353 lzw_info->first_code=lzw_info->table[1][value];
354 PushLZWStack(lzw_info->stack,lzw_info->first_code);
355 if (lzw_info->slot < MaximumLZWCode)
357 lzw_info->table[0][lzw_info->slot]=lzw_info->last_code;
358 lzw_info->table[1][lzw_info->slot]=lzw_info->first_code;
360 if ((lzw_info->slot >= lzw_info->maximum_code) &&
361 (lzw_info->bits < MaximumLZWBits))
364 lzw_info->maximum_code=1UL << lzw_info->bits;
367 lzw_info->last_code=(unsigned long) code;
368 return(PopLZWStack(lzw_info->stack));
371 static MagickBooleanType DecodeImage(Image *image,const long opacity)
396 Allocate decoder tables.
398 assert(image != (Image *) NULL);
399 assert(image->signature == MagickSignature);
400 if (image->debug != MagickFalse)
401 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
402 data_size=(unsigned char) ReadBlobByte(image);
403 if (data_size > MaximumLZWBits)
404 ThrowBinaryException(CorruptImageError,"CorruptImage",image->filename);
405 lzw_info=AcquireLZWInfo(image,data_size);
406 if (lzw_info == (LZWInfo *) NULL)
407 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
409 exception=(&image->exception);
412 for (y=0; y < (long) image->rows; y++)
423 q=GetAuthenticPixels(image,0,offset,image->columns,1,exception);
424 if (q == (PixelPacket *) NULL)
426 indexes=GetAuthenticIndexQueue(image);
427 for (x=0; x < (long) image->columns; )
429 c=ReadBlobLZWByte(lzw_info);
432 index=ConstrainColormapIndex(image,(unsigned long) c);
433 q->red=image->colormap[(long) index].red;
434 q->green=image->colormap[(long) index].green;
435 q->blue=image->colormap[(long) index].blue;
436 q->opacity=(long) index == opacity ? (Quantum) TransparentOpacity :
437 (Quantum) OpaqueOpacity;
442 if (x < (long) image->columns)
444 if (image->interlace == NoInterlace)
453 if (offset >= (long) image->rows)
463 if (offset >= (long) image->rows)
473 if (offset >= (long) image->rows)
486 if (SyncAuthenticPixels(image,exception) == MagickFalse)
489 lzw_info=RelinquishLZWInfo(lzw_info);
490 if (y < (long) image->rows)
491 ThrowBinaryException(CorruptImageError,"CorruptImage",image->filename);
496 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
500 % E n c o d e I m a g e %
504 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
506 % EncodeImage compresses an image via GIF-coding.
508 % The format of the EncodeImage method is:
510 % MagickBooleanType EncodeImage(const ImageInfo *image_info,Image *image,
511 % const unsigned long data_size)
513 % A description of each parameter follows:
515 % o image_info: the image info.
517 % o image: the address of a structure of type Image.
519 % o data_size: The number of bits in the compressed packet.
522 static MagickBooleanType EncodeImage(const ImageInfo *image_info,Image *image,
523 const unsigned long data_size)
525 #define MaxCode(number_bits) ((1UL << (number_bits))-1)
526 #define MaxHashTable 5003
527 #define MaxGIFBits 12UL
528 #define MaxGIFTable (1UL << MaxGIFBits)
529 #define GIFOutputCode(code) \
535 datum|=(code) << bits; \
542 Add a character to current packet. \
544 packet[length++]=(unsigned char) (datum & 0xff); \
547 (void) WriteBlobByte(image,(unsigned char) length); \
548 (void) WriteBlob(image,length,packet); \
554 if (free_code > max_code) \
557 if (number_bits == MaxGIFBits) \
558 max_code=MaxGIFTable; \
560 max_code=MaxCode(number_bits); \
592 end_of_information_code,
600 Allocate encoder tables.
602 assert(image != (Image *) NULL);
603 packet=(unsigned char *) AcquireQuantumMemory(256,sizeof(*packet));
604 hash_code=(short *) AcquireQuantumMemory(MaxHashTable,sizeof(*hash_code));
605 hash_prefix=(short *) AcquireQuantumMemory(MaxHashTable,sizeof(*hash_prefix));
606 hash_suffix=(unsigned char *) AcquireQuantumMemory(MaxHashTable,
607 sizeof(*hash_suffix));
608 if ((packet == (unsigned char *) NULL) || (hash_code == (short *) NULL) ||
609 (hash_prefix == (short *) NULL) ||
610 (hash_suffix == (unsigned char *) NULL))
612 if (packet != (unsigned char *) NULL)
613 packet=(unsigned char *) RelinquishMagickMemory(packet);
614 if (hash_code != (short *) NULL)
615 hash_code=(short *) RelinquishMagickMemory(hash_code);
616 if (hash_prefix != (short *) NULL)
617 hash_prefix=(short *) RelinquishMagickMemory(hash_prefix);
618 if (hash_suffix != (unsigned char *) NULL)
619 hash_suffix=(unsigned char *) RelinquishMagickMemory(hash_suffix);
623 Initialize GIF encoder.
625 number_bits=data_size;
626 max_code=MaxCode(number_bits);
627 clear_code=((short) 1UL << (data_size-1));
628 end_of_information_code=clear_code+1;
629 free_code=clear_code+2;
633 for (i=0; i < MaxHashTable; i++)
635 GIFOutputCode(clear_code);
642 for (y=0; y < (long) image->rows; y++)
644 register const IndexPacket
647 register const PixelPacket
653 p=GetVirtualPixels(image,0,offset,image->columns,1,&image->exception);
654 if (p == (const PixelPacket *) NULL)
656 indexes=GetVirtualIndexQueue(image);
658 waiting_code=(short) (*indexes);
659 for (x=(y == 0) ? 1 : 0; x < (long) image->columns; x++)
664 index=(IndexPacket) ((unsigned long) indexes[x] & 0xff);
666 k=(long) (((unsigned long) index << (MaxGIFBits-8))+waiting_code);
667 if (k >= MaxHashTable)
669 next_pixel=MagickFalse;
671 if (hash_code[k] > 0)
673 if ((hash_prefix[k] == waiting_code) &&
674 (hash_suffix[k] == (unsigned char) index))
676 waiting_code=hash_code[k];
680 displacement=MaxHashTable-k;
686 if (hash_code[k] == 0)
688 if ((hash_prefix[k] == waiting_code) &&
689 (hash_suffix[k] == (unsigned char) index))
691 waiting_code=hash_code[k];
692 next_pixel=MagickTrue;
696 if (next_pixel == MagickTrue)
699 GIFOutputCode((unsigned long) waiting_code);
700 if (free_code < MaxGIFTable)
702 hash_code[k]=(short) free_code++;
703 hash_prefix[k]=waiting_code;
704 hash_suffix[k]=(unsigned char) index;
709 Fill the hash table with empty entries.
711 for (k=0; k < MaxHashTable; k++)
714 Reset compressor and issue a clear code.
716 free_code=clear_code+2;
717 GIFOutputCode(clear_code);
718 number_bits=data_size;
719 max_code=MaxCode(number_bits);
721 waiting_code=(short) index;
723 if (image_info->interlace == NoInterlace)
732 if (offset >= (long) image->rows)
742 if (offset >= (long) image->rows)
752 if (offset >= (long) image->rows)
767 Flush out the buffered code.
769 GIFOutputCode((unsigned long) waiting_code);
770 GIFOutputCode(end_of_information_code);
774 Add a character to current packet.
776 packet[length++]=(unsigned char) (datum & 0xff);
779 (void) WriteBlobByte(image,(unsigned char) length);
780 (void) WriteBlob(image,length,packet);
785 Flush accumulated data.
789 (void) WriteBlobByte(image,(unsigned char) length);
790 (void) WriteBlob(image,length,packet);
795 hash_suffix=(unsigned char *) RelinquishMagickMemory(hash_suffix);
796 hash_prefix=(short *) RelinquishMagickMemory(hash_prefix);
797 hash_code=(short *) RelinquishMagickMemory(hash_code);
798 packet=(unsigned char *) RelinquishMagickMemory(packet);
803 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
811 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
813 % IsGIF() returns MagickTrue if the image format type, identified by the
814 % magick string, is GIF.
816 % The format of the IsGIF method is:
818 % MagickBooleanType IsGIF(const unsigned char *magick,const size_t length)
820 % A description of each parameter follows:
822 % o magick: compare image format pattern against these bytes.
824 % o length: Specifies the length of the magick string.
827 static MagickBooleanType IsGIF(const unsigned char *magick,const size_t length)
831 if (LocaleNCompare((char *) magick,"GIF8",4) == 0)
837 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
841 + R e a d B l o b B l o c k %
845 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
847 % ReadBlobBlock() reads data from the image file and returns it. The
848 % amount of data is determined by first reading a count byte. The number
849 % of bytes read is returned.
851 % The format of the ReadBlobBlock method is:
853 % size_t ReadBlobBlock(Image *image,unsigned char *data)
855 % A description of each parameter follows:
857 % o image: the image.
859 % o data: Specifies an area to place the information requested from
863 static ssize_t ReadBlobBlock(Image *image,unsigned char *data)
871 assert(image != (Image *) NULL);
872 assert(image->signature == MagickSignature);
873 assert(data != (unsigned char *) NULL);
874 count=ReadBlob(image,1,&block_count);
877 return(ReadBlob(image,(size_t) block_count,data));
881 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
885 % R e a d G I F I m a g e %
889 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
891 % ReadGIFImage() reads a Compuserve Graphics image file and returns it.
892 % It allocates the memory necessary for the new Image structure and returns a
893 % pointer to the new image.
895 % The format of the ReadGIFImage method is:
897 % Image *ReadGIFImage(const ImageInfo *image_info,ExceptionInfo *exception)
899 % A description of each parameter follows:
901 % o image_info: the image info.
903 % o exception: return any errors or warnings in this structure.
907 static inline size_t MagickMax(const size_t x,const size_t y)
914 static inline size_t MagickMin(const size_t x,const size_t y)
921 static MagickBooleanType PingGIFImage(Image *image)
928 assert(image != (Image *) NULL);
929 assert(image->signature == MagickSignature);
930 if (image->debug != MagickFalse)
931 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
932 if (ReadBlob(image,1,&data_size) != 1)
933 ThrowBinaryException(CorruptImageError,"CorruptImage",image->filename);
934 if (data_size > MaximumLZWBits)
935 ThrowBinaryException(CorruptImageError,"CorruptImage",image->filename);
936 if (ReadBlob(image,1,&length) != 1)
937 ThrowBinaryException(CorruptImageError,"CorruptImage",image->filename);
940 if (ReadBlob(image,length,buffer) != (ssize_t) length)
941 ThrowBinaryException(CorruptImageError,"CorruptImage",image->filename);
942 if (ReadBlob(image,1,&length) != 1)
943 ThrowBinaryException(CorruptImageError,"CorruptImage",image->filename);
948 static Image *ReadGIFImage(const ImageInfo *image_info,ExceptionInfo *exception)
950 #define BitSet(byte,bit) (((byte) & (bit)) == (bit))
951 #define LSBFirstOrder(x,y) (((y) << 8) | (x))
957 number_extensionss=0;
971 register unsigned char
982 header[MaxTextExtent],
995 assert(image_info != (const ImageInfo *) NULL);
996 assert(image_info->signature == MagickSignature);
997 if (image_info->debug != MagickFalse)
998 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
999 image_info->filename);
1000 assert(exception != (ExceptionInfo *) NULL);
1001 assert(exception->signature == MagickSignature);
1002 image=AcquireImage(image_info);
1003 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
1004 if (status == MagickFalse)
1006 image=DestroyImageList(image);
1007 return((Image *) NULL);
1010 Determine if this a GIF file.
1012 count=ReadBlob(image,6,magick);
1013 if ((count != 6) || ((LocaleNCompare((char *) magick,"GIF87",5) != 0) &&
1014 (LocaleNCompare((char *) magick,"GIF89",5) != 0)))
1015 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1016 page.width=ReadBlobLSBShort(image);
1017 page.height=ReadBlobLSBShort(image);
1018 flag=(unsigned char) ReadBlobByte(image);
1019 background=(unsigned char) ReadBlobByte(image);
1020 c=(unsigned char) ReadBlobByte(image); /* reserved */
1021 global_colors=1UL << (((unsigned long) flag & 0x07)+1);
1022 global_colormap=(unsigned char *) AcquireQuantumMemory((size_t)
1023 MagickMax(global_colors,256),3UL*sizeof(*global_colormap));
1024 if (global_colormap == (unsigned char *) NULL)
1025 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1026 if (BitSet((int) flag,0x80) != 0)
1027 count=ReadBlob(image,(size_t) (3*global_colors),global_colormap);
1035 count=ReadBlob(image,1,&c);
1038 if (c == (unsigned char) ';')
1039 break; /* terminator */
1040 if (c == (unsigned char) '!')
1043 GIF Extension block.
1046 count=ReadBlob(image,1,&c);
1049 global_colormap=(unsigned char *) RelinquishMagickMemory(
1051 ThrowReaderException(CorruptImageError,
1052 "UnableToReadExtensionBlock");
1059 Read graphics control extension.
1061 while (ReadBlobBlock(image,header) != 0) ;
1062 dispose=(unsigned long) (header[0] >> 2);
1063 delay=(unsigned long) ((header[2] << 8) | header[1]);
1064 if ((long) (header[0] & 0x01) == 0x01)
1065 opacity=(long) header[3];
1074 Read comment extension.
1076 comments=AcquireString((char *) NULL);
1079 count=(ssize_t) ReadBlobBlock(image,header);
1083 (void) ConcatenateString(&comments,(const char *) header);
1085 (void) SetImageProperty(image,"comment",comments);
1086 comments=DestroyString(comments);
1091 /* Read GIF application extension */
1097 Read Netscape Loop extension.
1100 if (ReadBlobBlock(image,header) != 0)
1101 loop=LocaleNCompare((char *) header,"NETSCAPE2.0",11) == 0 ?
1102 MagickTrue : MagickFalse;
1103 if (loop != MagickFalse)
1105 while (ReadBlobBlock(image,header) != 0)
1106 iterations=(unsigned long) ((header[2] << 8) | header[1]);
1112 name[MaxTextExtent];
1131 Store GIF application extension as a generic profile.
1133 i8bim=LocaleNCompare((char *) header,"MGK8BIM0000",11) == 0 ?
1134 MagickTrue : MagickFalse;
1135 icc=LocaleNCompare((char *) header,"ICCRGBG1012",11) == 0 ?
1136 MagickTrue : MagickFalse;
1137 iptc=LocaleNCompare((char *) header,"MGKIPTC0000",11) == 0 ?
1138 MagickTrue : MagickFalse;
1139 number_extensionss++;
1140 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1141 " Reading GIF application extension");
1142 info=(unsigned char *) AcquireQuantumMemory(255UL,
1144 reserved_length=255;
1145 for (info_length=0; ; )
1147 block_length=(int) ReadBlobBlock(image,&info[info_length]);
1148 if (block_length == 0)
1150 info_length+=block_length;
1151 if (info_length > (reserved_length-255))
1153 reserved_length+=4096;
1154 info=(unsigned char *) ResizeQuantumMemory(info,
1155 (size_t) reserved_length,sizeof(*info));
1158 info=(unsigned char *) ResizeQuantumMemory(info,(size_t)
1159 (info_length+1),sizeof(*info));
1160 profile=AcquireStringInfo((size_t) info_length);
1161 SetStringInfoDatum(profile,(const unsigned char *) info);
1162 if (i8bim == MagickTrue)
1163 (void) CopyMagickString(name,"8bim",sizeof(name));
1164 else if (icc == MagickTrue)
1165 (void) CopyMagickString(name,"icc",sizeof(name));
1166 else if (iptc == MagickTrue)
1167 (void) CopyMagickString(name,"iptc",sizeof(name));
1169 (void) FormatMagickString(name,sizeof(name),"gif:%.11s",
1171 (void) SetImageProfile(image,name,profile);
1172 info=(unsigned char *) RelinquishMagickMemory(info);
1173 profile=DestroyStringInfo(profile);
1174 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1175 " profile name=%s",name);
1181 while (ReadBlobBlock(image,header) != 0) ;
1186 if (c != (unsigned char) ',')
1188 if (image_count != 0)
1191 Allocate next image structure.
1193 AcquireNextImage(image_info,image);
1194 if (GetNextImageInList(image) == (Image *) NULL)
1196 image=DestroyImageList(image);
1197 global_colormap=(unsigned char *) RelinquishMagickMemory(
1199 return((Image *) NULL);
1201 image=SyncNextImageInList(image);
1205 Read image attributes.
1207 image->storage_class=PseudoClass;
1208 image->compression=LZWCompression;
1209 page.x=(long) ReadBlobLSBShort(image);
1210 page.y=(long) ReadBlobLSBShort(image);
1211 image->columns=ReadBlobLSBShort(image);
1212 image->rows=ReadBlobLSBShort(image);
1214 flag=(unsigned char) ReadBlobByte(image);
1215 image->interlace=BitSet((int) flag,0x40) != 0 ? GIFInterlace :
1217 image->colors=BitSet((int) flag,0x80) == 0 ? global_colors :
1218 1UL << ((unsigned long) (flag & 0x07)+1);
1219 if (opacity >= (long) image->colors)
1221 image->page.width=page.width;
1222 image->page.height=page.height;
1223 image->page.y=page.y;
1224 image->page.x=page.x;
1226 image->ticks_per_second=100;
1227 image->dispose=(DisposeType) dispose;
1228 image->iterations=iterations;
1229 image->matte=opacity >= 0 ? MagickTrue : MagickFalse;
1233 if ((image->columns == 0) || (image->rows == 0))
1235 global_colormap=(unsigned char *) RelinquishMagickMemory(
1237 ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
1240 Inititialize colormap.
1242 if (AcquireImageColormap(image,image->colors) == MagickFalse)
1244 global_colormap=(unsigned char *) RelinquishMagickMemory(
1246 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1248 if (BitSet((int) flag,0x80) == 0)
1251 Use global colormap.
1254 for (i=0; i < (long) image->colors; i++)
1256 image->colormap[i].red=ScaleCharToQuantum(*p++);
1257 image->colormap[i].green=ScaleCharToQuantum(*p++);
1258 image->colormap[i].blue=ScaleCharToQuantum(*p++);
1261 image->colormap[i].opacity=(Quantum) TransparentOpacity;
1262 image->transparent_color=image->colormap[opacity];
1265 image->background_color=image->colormap[MagickMin(background,
1274 Read local colormap.
1276 colormap=(unsigned char *) AcquireQuantumMemory(image->colors,
1277 3*sizeof(*colormap));
1278 if (colormap == (unsigned char *) NULL)
1280 global_colormap=(unsigned char *) RelinquishMagickMemory(
1282 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1284 count=ReadBlob(image,(3*image->colors)*sizeof(*colormap),colormap);
1285 if (count != (ssize_t) (3*image->colors))
1287 global_colormap=(unsigned char *) RelinquishMagickMemory(
1289 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
1290 ThrowReaderException(CorruptImageError,
1291 "InsufficientImageDataInFile");
1294 for (i=0; i < (long) image->colors; i++)
1296 image->colormap[i].red=ScaleCharToQuantum(*p++);
1297 image->colormap[i].green=ScaleCharToQuantum(*p++);
1298 image->colormap[i].blue=ScaleCharToQuantum(*p++);
1300 image->colormap[i].opacity=(Quantum) TransparentOpacity;
1302 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
1304 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
1305 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1310 if (image_info->ping != MagickFalse)
1311 status=PingGIFImage(image);
1313 status=DecodeImage(image,opacity);
1314 if ((image_info->ping == MagickFalse) && (status == MagickFalse))
1316 global_colormap=(unsigned char *) RelinquishMagickMemory(
1318 ThrowReaderException(CorruptImageError,"CorruptImage");
1320 if (image_info->number_scenes != 0)
1321 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1324 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) image->scene-
1326 if (status == MagickFalse)
1329 global_colormap=(unsigned char *) RelinquishMagickMemory(global_colormap);
1330 if ((image->columns == 0) || (image->rows == 0))
1331 ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
1332 (void) CloseBlob(image);
1333 return(GetFirstImageInList(image));
1337 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1341 % R e g i s t e r G I F I m a g e %
1345 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1347 % RegisterGIFImage() adds properties for the GIF image format to
1348 % the list of supported formats. The properties include the image format
1349 % tag, a method to read and/or write the format, whether the format
1350 % supports the saving of more than one frame to the same file or blob,
1351 % whether the format supports native in-memory I/O, and a brief
1352 % description of the format.
1354 % The format of the RegisterGIFImage method is:
1356 % unsigned long RegisterGIFImage(void)
1359 ModuleExport unsigned long RegisterGIFImage(void)
1364 entry=SetMagickInfo("GIF");
1365 entry->decoder=(DecodeImageHandler *) ReadGIFImage;
1366 entry->encoder=(EncodeImageHandler *) WriteGIFImage;
1367 entry->magick=(IsImageFormatHandler *) IsGIF;
1368 entry->description=ConstantString("CompuServe graphics interchange format");
1369 entry->module=ConstantString("GIF");
1370 (void) RegisterMagickInfo(entry);
1371 entry=SetMagickInfo("GIF87");
1372 entry->decoder=(DecodeImageHandler *) ReadGIFImage;
1373 entry->encoder=(EncodeImageHandler *) WriteGIFImage;
1374 entry->magick=(IsImageFormatHandler *) IsGIF;
1375 entry->adjoin=MagickFalse;
1376 entry->description=ConstantString("CompuServe graphics interchange format");
1377 entry->version=ConstantString("version 87a");
1378 entry->module=ConstantString("GIF");
1379 (void) RegisterMagickInfo(entry);
1380 return(MagickImageCoderSignature);
1384 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1388 % U n r e g i s t e r G I F I m a g e %
1392 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1394 % UnregisterGIFImage() removes format registrations made by the
1395 % GIF module from the list of supported formats.
1397 % The format of the UnregisterGIFImage method is:
1399 % UnregisterGIFImage(void)
1402 ModuleExport void UnregisterGIFImage(void)
1404 (void) UnregisterMagickInfo("GIF");
1405 (void) UnregisterMagickInfo("GIF87");
1409 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1413 % W r i t e G I F I m a g e %
1417 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1419 % WriteGIFImage() writes an image to a file in the Compuserve Graphics
1422 % The format of the WriteGIFImage method is:
1424 % MagickBooleanType WriteGIFImage(const ImageInfo *image_info,Image *image)
1426 % A description of each parameter follows.
1428 % o image_info: the image info.
1430 % o image: The image.
1433 static MagickBooleanType WriteGIFImage(const ImageInfo *image_info,Image *image)
1463 register unsigned char
1478 Open output image file.
1480 assert(image_info != (const ImageInfo *) NULL);
1481 assert(image_info->signature == MagickSignature);
1482 assert(image != (Image *) NULL);
1483 assert(image->signature == MagickSignature);
1484 if (image->debug != MagickFalse)
1485 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1486 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
1487 if (status == MagickFalse)
1492 global_colormap=(unsigned char *) AcquireQuantumMemory(768UL,
1493 sizeof(*global_colormap));
1494 colormap=(unsigned char *) AcquireQuantumMemory(768UL,sizeof(*colormap));
1495 if ((global_colormap == (unsigned char *) NULL) ||
1496 (colormap == (unsigned char *) NULL))
1497 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1498 for (i=0; i < 768; i++)
1499 colormap[i]=(unsigned char) 0;
1503 write_info=CloneImageInfo(image_info);
1504 if (LocaleCompare(write_info->magick,"GIF87") != 0)
1505 (void) WriteBlob(image,6,(unsigned char *) "GIF89a");
1508 (void) WriteBlob(image,6,(unsigned char *) "GIF87a");
1509 write_info->adjoin=MagickFalse;
1512 Determine image bounding box.
1514 page.width=image->columns;
1515 page.height=image->rows;
1518 if (write_info->adjoin != MagickFalse)
1519 for (next_image=image; next_image != (Image *) NULL; )
1521 page.x=next_image->page.x;
1522 page.y=next_image->page.y;
1523 if ((next_image->page.width+page.x) > page.width)
1524 page.width=next_image->page.width+page.x;
1525 if ((next_image->page.height+page.y) > page.height)
1526 page.height=next_image->page.height+page.y;
1527 next_image=GetNextImageInList(next_image);
1529 page.x=image->page.x;
1530 page.y=image->page.y;
1531 if ((image->page.width != 0) && (image->page.height != 0))
1533 (void) WriteBlobLSBShort(image,(unsigned short) page.width);
1534 (void) WriteBlobLSBShort(image,(unsigned short) page.height);
1536 Write images to file.
1538 interlace=write_info->interlace;
1539 if ((write_info->adjoin != MagickFalse) &&
1540 (GetNextImageInList(image) != (Image *) NULL))
1541 interlace=NoInterlace;
1545 if (image->colorspace != RGBColorspace)
1546 (void) TransformImageColorspace(image,RGBColorspace);
1548 if (IsOpaqueImage(image,&image->exception) != MagickFalse)
1550 if ((image->storage_class == DirectClass) || (image->colors > 256))
1551 (void) SetImageType(image,PaletteType);
1560 Identify transparent colormap index.
1562 if ((image->storage_class == DirectClass) || (image->colors > 256))
1563 (void) SetImageType(image,PaletteBilevelMatteType);
1564 for (i=0; i < (long) image->colors; i++)
1565 if (image->colormap[i].opacity != OpaqueOpacity)
1572 alpha=(MagickRealType) TransparentOpacity-(MagickRealType)
1573 image->colormap[i].opacity;
1574 beta=(MagickRealType) TransparentOpacity-(MagickRealType)
1575 image->colormap[opacity].opacity;
1581 (void) SetImageType(image,PaletteBilevelMatteType);
1582 for (i=0; i < (long) image->colors; i++)
1583 if (image->colormap[i].opacity != OpaqueOpacity)
1590 alpha=(Quantum) TransparentOpacity-(MagickRealType)
1591 image->colormap[i].opacity;
1592 beta=(Quantum) TransparentOpacity-(MagickRealType)
1593 image->colormap[opacity].opacity;
1600 image->colormap[opacity].red=image->transparent_color.red;
1601 image->colormap[opacity].green=image->transparent_color.green;
1602 image->colormap[opacity].blue=image->transparent_color.blue;
1605 if ((image->storage_class == DirectClass) || (image->colors > 256))
1606 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1607 for (bits_per_pixel=1; bits_per_pixel < 8; bits_per_pixel++)
1608 if ((1UL << bits_per_pixel) >= image->colors)
1611 for (i=0; i < (long) image->colors; i++)
1613 *q++=ScaleQuantumToChar(image->colormap[i].red);
1614 *q++=ScaleQuantumToChar(image->colormap[i].green);
1615 *q++=ScaleQuantumToChar(image->colormap[i].blue);
1617 for ( ; i < (long) (1UL << bits_per_pixel); i++)
1619 *q++=(unsigned char) 0x0;
1620 *q++=(unsigned char) 0x0;
1621 *q++=(unsigned char) 0x0;
1623 if ((GetPreviousImageInList(image) == (Image *) NULL) ||
1624 (write_info->adjoin == MagickFalse))
1627 Write global colormap.
1630 c|=(8-1) << 4; /* color resolution */
1631 c|=(bits_per_pixel-1); /* size of global colormap */
1632 (void) WriteBlobByte(image,(unsigned char) c);
1633 for (j=0; j < (long) image->colors; j++)
1634 if (IsColorEqual(&image->background_color,image->colormap+j))
1636 (void) WriteBlobByte(image,(unsigned char)
1637 (j == (long) image->colors ? 0 : j)); /* background color */
1638 (void) WriteBlobByte(image,(unsigned char) 0x00); /* reserved */
1639 length=(size_t) (3*(1UL << bits_per_pixel));
1640 (void) WriteBlob(image,length,colormap);
1641 for (j=0; j < 768; j++)
1642 global_colormap[j]=colormap[j];
1644 if (LocaleCompare(write_info->magick,"GIF87") != 0)
1647 Write graphics control extension.
1649 (void) WriteBlobByte(image,(unsigned char) 0x21);
1650 (void) WriteBlobByte(image,(unsigned char) 0xf9);
1651 (void) WriteBlobByte(image,(unsigned char) 0x04);
1652 c=image->dispose << 2;
1655 (void) WriteBlobByte(image,(unsigned char) c);
1656 delay=(unsigned long) (100*image->delay/MagickMax((size_t)
1657 image->ticks_per_second,1));
1658 (void) WriteBlobLSBShort(image,(unsigned short) delay);
1659 (void) WriteBlobByte(image,(unsigned char) (opacity >= 0 ? opacity :
1661 (void) WriteBlobByte(image,(unsigned char) 0x00);
1662 if ((LocaleCompare(write_info->magick,"GIF87") != 0) &&
1663 (GetImageProperty(image,"comment") != (const char *) NULL))
1675 Write Comment extension.
1677 (void) WriteBlobByte(image,(unsigned char) 0x21);
1678 (void) WriteBlobByte(image,(unsigned char) 0xfe);
1679 value=GetImageProperty(image,"comment");
1681 while (strlen(p) != 0)
1683 count=MagickMin(strlen(p),255);
1684 (void) WriteBlobByte(image,(unsigned char) count);
1685 for (i=0; i < (long) count; i++)
1686 (void) WriteBlobByte(image,(unsigned char) *p++);
1688 (void) WriteBlobByte(image,(unsigned char) 0x00);
1690 if ((GetPreviousImageInList(image) == (Image *) NULL) &&
1691 (GetNextImageInList(image) != (Image *) NULL) &&
1692 (image->iterations != 1))
1695 Write Netscape Loop extension.
1697 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1698 " Writing GIF Extension %s","NETSCAPE2.0");
1699 (void) WriteBlobByte(image,(unsigned char) 0x21);
1700 (void) WriteBlobByte(image,(unsigned char) 0xff);
1701 (void) WriteBlobByte(image,(unsigned char) 0x0b);
1702 (void) WriteBlob(image,11,(unsigned char *) "NETSCAPE2.0");
1703 (void) WriteBlobByte(image,(unsigned char) 0x03);
1704 (void) WriteBlobByte(image,(unsigned char) 0x01);
1705 (void) WriteBlobLSBShort(image,(unsigned short) image->iterations);
1706 (void) WriteBlobByte(image,(unsigned char) 0x00);
1708 ResetImageProfileIterator(image);
1717 name=GetNextImageProfile(image);
1718 if (name == (const char *) NULL)
1720 profile=GetImageProfile(image,name);
1721 if (profile != (StringInfo *) NULL)
1723 if ((LocaleCompare(name,"ICC") == 0) ||
1724 (LocaleCompare(name,"ICM") == 0) ||
1725 (LocaleCompare(name,"IPTC") == 0) ||
1726 (LocaleCompare(name,"8BIM") == 0) ||
1727 (LocaleNCompare(name,"gif:",4) == 0))
1738 datum=GetStringInfoDatum(profile);
1739 length=GetStringInfoLength(profile);
1740 (void) WriteBlobByte(image,(unsigned char) 0x21);
1741 (void) WriteBlobByte(image,(unsigned char) 0xff);
1742 (void) WriteBlobByte(image,(unsigned char) 0x0b);
1743 if ((LocaleCompare(name,"ICC") == 0) ||
1744 (LocaleCompare(name,"ICM") == 0))
1747 Write ICC extension.
1749 (void) WriteBlob(image,11,(unsigned char *)"ICCRGBG1012");
1750 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1751 " Writing GIF Extension %s","ICCRGBG1012");
1754 if ((LocaleCompare(name,"IPTC") == 0))
1757 write IPTC extension.
1759 (void) WriteBlob(image,11,(unsigned char *)"MGKIPTC0000");
1760 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1761 " Writing GIF Extension %s","MGKIPTC0000");
1764 if ((LocaleCompare(name,"8BIM") == 0))
1767 Write 8BIM extension>
1769 (void) WriteBlob(image,11,(unsigned char *)
1771 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1772 " Writing GIF Extension %s","MGK8BIM0000");
1777 extension[MaxTextExtent];
1779 /* write generic extension */
1780 (void) CopyMagickString(extension,name+4,
1782 (void) WriteBlob(image,11,(unsigned char *) extension);
1783 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1784 " Writing GIF Extension %s",name);
1787 while ((ssize_t) length > offset)
1792 if ((length-offset) < 255)
1793 block_length=length-offset;
1796 (void) WriteBlobByte(image,(unsigned char) block_length);
1797 (void) WriteBlob(image,(size_t) block_length,datum+offset);
1798 offset+=(ssize_t) block_length;
1800 (void) WriteBlobByte(image,(unsigned char) 0x00);
1805 (void) WriteBlobByte(image,','); /* image separator */
1807 Write the image header.
1809 page.x=image->page.x;
1810 page.y=image->page.y;
1811 if ((image->page.width != 0) && (image->page.height != 0))
1813 (void) WriteBlobLSBShort(image,(unsigned short) (page.x < 0 ? 0 : page.x));
1814 (void) WriteBlobLSBShort(image,(unsigned short) (page.y < 0 ? 0 : page.y));
1815 (void) WriteBlobLSBShort(image,(unsigned short) image->columns);
1816 (void) WriteBlobLSBShort(image,(unsigned short) image->rows);
1818 if (interlace != NoInterlace)
1819 c|=0x40; /* pixel data is interlaced */
1820 for (j=0; j < (long) (3*image->colors); j++)
1821 if (colormap[j] != global_colormap[j])
1823 if (j == (long) (3*image->colors))
1824 (void) WriteBlobByte(image,(unsigned char) c);
1828 c|=(bits_per_pixel-1); /* size of local colormap */
1829 (void) WriteBlobByte(image,(unsigned char) c);
1830 length=(size_t) (3*(1UL << bits_per_pixel));
1831 (void) WriteBlob(image,length,colormap);
1834 Write the image data.
1836 c=(int) MagickMax(bits_per_pixel,2);
1837 (void) WriteBlobByte(image,(unsigned char) c);
1838 status=EncodeImage(write_info,image,(unsigned long)
1839 MagickMax(bits_per_pixel,2)+1);
1840 if (status == MagickFalse)
1842 global_colormap=(unsigned char *) RelinquishMagickMemory(
1844 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
1845 write_info=DestroyImageInfo(write_info);
1846 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1848 (void) WriteBlobByte(image,(unsigned char) 0x00);
1849 if (GetNextImageInList(image) == (Image *) NULL)
1851 image=SyncNextImageInList(image);
1853 status=SetImageProgress(image,SaveImagesTag,scene,
1854 GetImageListLength(image));
1855 if (status == MagickFalse)
1857 } while (write_info->adjoin != MagickFalse);
1858 (void) WriteBlobByte(image,';'); /* terminator */
1859 global_colormap=(unsigned char *) RelinquishMagickMemory(global_colormap);
1860 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
1861 write_info=DestroyImageInfo(write_info);
1862 (void) CloseBlob(image);