2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write Compuserv Graphics Interchange Format %
20 % Copyright 1999-2011 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/color-private.h"
48 #include "magick/colormap.h"
49 #include "magick/colormap-private.h"
50 #include "magick/colorspace.h"
51 #include "magick/exception.h"
52 #include "magick/exception-private.h"
53 #include "magick/image.h"
54 #include "magick/image-private.h"
55 #include "magick/list.h"
56 #include "magick/profile.h"
57 #include "magick/magick.h"
58 #include "magick/memory_.h"
59 #include "magick/monitor.h"
60 #include "magick/monitor-private.h"
61 #include "magick/option.h"
62 #include "magick/property.h"
63 #include "magick/quantize.h"
64 #include "magick/quantum-private.h"
65 #include "magick/static.h"
66 #include "magick/string_.h"
67 #include "magick/module.h"
72 #define MaximumLZWBits 12
73 #define MaximumLZWCode (1UL << MaximumLZWBits)
78 typedef struct _LZWCodeInfo
91 typedef struct _LZWStack
99 typedef struct _LZWInfo
127 Forward declarations.
130 GetNextLZWCode(LZWInfo *,const size_t);
132 static MagickBooleanType
133 WriteGIFImage(const ImageInfo *,Image *);
136 ReadBlobBlock(Image *,unsigned char *);
139 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
143 % D e c o d e I m a g e %
147 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
149 % DecodeImage uncompresses an image via GIF-coding.
151 % The format of the DecodeImage method is:
153 % MagickBooleanType DecodeImage(Image *image,const ssize_t opacity)
155 % A description of each parameter follows:
157 % o image: the address of a structure of type Image.
159 % o opacity: The colormap index associated with the transparent color.
163 static LZWInfo *RelinquishLZWInfo(LZWInfo *lzw_info)
165 if (lzw_info->table[0] != (size_t *) NULL)
166 lzw_info->table[0]=(size_t *) RelinquishMagickMemory(
168 if (lzw_info->table[1] != (size_t *) NULL)
169 lzw_info->table[1]=(size_t *) RelinquishMagickMemory(
171 if (lzw_info->stack != (LZWStack *) NULL)
173 if (lzw_info->stack->codes != (size_t *) NULL)
174 lzw_info->stack->codes=(size_t *) RelinquishMagickMemory(
175 lzw_info->stack->codes);
176 lzw_info->stack=(LZWStack *) RelinquishMagickMemory(lzw_info->stack);
178 lzw_info=(LZWInfo *) RelinquishMagickMemory(lzw_info);
179 return((LZWInfo *) NULL);
182 static inline void ResetLZWInfo(LZWInfo *lzw_info)
187 lzw_info->bits=lzw_info->data_size+1;
189 lzw_info->maximum_code=one << lzw_info->bits;
190 lzw_info->slot=lzw_info->maximum_data_value+3;
191 lzw_info->genesis=MagickTrue;
194 static LZWInfo *AcquireLZWInfo(Image *image,const size_t data_size)
205 lzw_info=(LZWInfo *) AcquireMagickMemory(sizeof(*lzw_info));
206 if (lzw_info == (LZWInfo *) NULL)
207 return((LZWInfo *) NULL);
208 (void) ResetMagickMemory(lzw_info,0,sizeof(*lzw_info));
209 lzw_info->image=image;
210 lzw_info->data_size=data_size;
212 lzw_info->maximum_data_value=(one << data_size)-1;
213 lzw_info->clear_code=lzw_info->maximum_data_value+1;
214 lzw_info->end_code=lzw_info->maximum_data_value+2;
215 lzw_info->table[0]=(size_t *) AcquireQuantumMemory(MaximumLZWCode,
216 sizeof(*lzw_info->table));
217 lzw_info->table[1]=(size_t *) AcquireQuantumMemory(MaximumLZWCode,
218 sizeof(*lzw_info->table));
219 if ((lzw_info->table[0] == (size_t *) NULL) ||
220 (lzw_info->table[1] == (size_t *) NULL))
222 lzw_info=RelinquishLZWInfo(lzw_info);
223 return((LZWInfo *) NULL);
225 for (i=0; i <= (ssize_t) lzw_info->maximum_data_value; i++)
227 lzw_info->table[0][i]=0;
228 lzw_info->table[1][i]=(size_t) i;
230 ResetLZWInfo(lzw_info);
231 lzw_info->code_info.buffer[0]='\0';
232 lzw_info->code_info.buffer[1]='\0';
233 lzw_info->code_info.count=2;
234 lzw_info->code_info.bit=8*lzw_info->code_info.count;
235 lzw_info->code_info.eof=MagickFalse;
236 lzw_info->genesis=MagickTrue;
237 lzw_info->stack=(LZWStack *) AcquireMagickMemory(sizeof(*lzw_info->stack));
238 if (lzw_info->stack == (LZWStack *) NULL)
240 lzw_info=RelinquishLZWInfo(lzw_info);
241 return((LZWInfo *) NULL);
243 lzw_info->stack->codes=(size_t *) AcquireQuantumMemory(2UL*
244 MaximumLZWCode,sizeof(*lzw_info->stack->codes));
245 if (lzw_info->stack->codes == (size_t *) NULL)
247 lzw_info=RelinquishLZWInfo(lzw_info);
248 return((LZWInfo *) NULL);
250 lzw_info->stack->index=lzw_info->stack->codes;
251 lzw_info->stack->top=lzw_info->stack->codes+2*MaximumLZWCode;
255 static inline int GetNextLZWCode(LZWInfo *lzw_info,const size_t bits)
266 while (((lzw_info->code_info.bit+bits) > (8*lzw_info->code_info.count)) &&
267 (lzw_info->code_info.eof == MagickFalse))
272 lzw_info->code_info.buffer[0]=lzw_info->code_info.buffer[
273 lzw_info->code_info.count-2];
274 lzw_info->code_info.buffer[1]=lzw_info->code_info.buffer[
275 lzw_info->code_info.count-1];
276 lzw_info->code_info.bit-=8*(lzw_info->code_info.count-2);
277 lzw_info->code_info.count=2;
278 count=ReadBlobBlock(lzw_info->image,&lzw_info->code_info.buffer[
279 lzw_info->code_info.count]);
281 lzw_info->code_info.count+=count;
283 lzw_info->code_info.eof=MagickTrue;
285 if ((lzw_info->code_info.bit+bits) > (8*lzw_info->code_info.count))
289 for (i=0; i < (ssize_t) bits; i++)
291 code|=((lzw_info->code_info.buffer[lzw_info->code_info.bit/8] &
292 (one << (lzw_info->code_info.bit % 8))) != 0) << i;
293 lzw_info->code_info.bit++;
298 static inline int PopLZWStack(LZWStack *stack_info)
300 if (stack_info->index <= stack_info->codes)
303 return((int) *stack_info->index);
306 static inline void PushLZWStack(LZWStack *stack_info,const size_t value)
308 if (stack_info->index >= stack_info->top)
310 *stack_info->index=value;
314 static int ReadBlobLZWByte(LZWInfo *lzw_info)
326 if (lzw_info->stack->index != lzw_info->stack->codes)
327 return(PopLZWStack(lzw_info->stack));
328 if (lzw_info->genesis != MagickFalse)
330 lzw_info->genesis=MagickFalse;
333 lzw_info->first_code=(size_t) GetNextLZWCode(lzw_info,
335 lzw_info->last_code=lzw_info->first_code;
336 } while (lzw_info->first_code == lzw_info->clear_code);
337 return((int) lzw_info->first_code);
339 code=GetNextLZWCode(lzw_info,lzw_info->bits);
342 if ((size_t) code == lzw_info->clear_code)
344 ResetLZWInfo(lzw_info);
345 return(ReadBlobLZWByte(lzw_info));
347 if ((size_t) code == lzw_info->end_code)
349 if ((size_t) code < lzw_info->slot)
353 PushLZWStack(lzw_info->stack,lzw_info->first_code);
354 value=lzw_info->last_code;
357 while (value > lzw_info->maximum_data_value)
359 if ((size_t) count > MaximumLZWCode)
362 if ((size_t) value > MaximumLZWCode)
364 PushLZWStack(lzw_info->stack,lzw_info->table[1][value]);
365 value=lzw_info->table[0][value];
367 lzw_info->first_code=lzw_info->table[1][value];
368 PushLZWStack(lzw_info->stack,lzw_info->first_code);
370 if (lzw_info->slot < MaximumLZWCode)
372 lzw_info->table[0][lzw_info->slot]=lzw_info->last_code;
373 lzw_info->table[1][lzw_info->slot]=lzw_info->first_code;
375 if ((lzw_info->slot >= lzw_info->maximum_code) &&
376 (lzw_info->bits < MaximumLZWBits))
379 lzw_info->maximum_code=one << lzw_info->bits;
382 lzw_info->last_code=(size_t) code;
383 return(PopLZWStack(lzw_info->stack));
386 static MagickBooleanType DecodeImage(Image *image,const ssize_t opacity)
411 Allocate decoder tables.
413 assert(image != (Image *) NULL);
414 assert(image->signature == MagickSignature);
415 if (image->debug != MagickFalse)
416 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
417 data_size=(unsigned char) ReadBlobByte(image);
418 if (data_size > MaximumLZWBits)
419 ThrowBinaryException(CorruptImageError,"CorruptImage",image->filename);
420 lzw_info=AcquireLZWInfo(image,data_size);
421 if (lzw_info == (LZWInfo *) NULL)
422 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
424 exception=(&image->exception);
427 for (y=0; y < (ssize_t) image->rows; y++)
438 q=GetAuthenticPixels(image,0,offset,image->columns,1,exception);
439 if (q == (PixelPacket *) NULL)
441 indexes=GetAuthenticIndexQueue(image);
442 for (x=0; x < (ssize_t) image->columns; )
444 c=ReadBlobLZWByte(lzw_info);
447 index=ConstrainColormapIndex(image,(size_t) c);
448 q->red=image->colormap[(ssize_t) index].red;
449 q->green=image->colormap[(ssize_t) index].green;
450 q->blue=image->colormap[(ssize_t) index].blue;
451 q->opacity=(ssize_t) index == opacity ? (Quantum) TransparentOpacity :
452 (Quantum) OpaqueOpacity;
457 if (x < (ssize_t) image->columns)
459 if (image->interlace == NoInterlace)
468 if (offset >= (ssize_t) image->rows)
478 if (offset >= (ssize_t) image->rows)
488 if (offset >= (ssize_t) image->rows)
501 if (SyncAuthenticPixels(image,exception) == MagickFalse)
504 lzw_info=RelinquishLZWInfo(lzw_info);
505 if (y < (ssize_t) image->rows)
506 ThrowBinaryException(CorruptImageError,"CorruptImage",image->filename);
511 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
515 % E n c o d e I m a g e %
519 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
521 % EncodeImage compresses an image via GIF-coding.
523 % The format of the EncodeImage method is:
525 % MagickBooleanType EncodeImage(const ImageInfo *image_info,Image *image,
526 % const size_t data_size)
528 % A description of each parameter follows:
530 % o image_info: the image info.
532 % o image: the address of a structure of type Image.
534 % o data_size: The number of bits in the compressed packet.
537 static MagickBooleanType EncodeImage(const ImageInfo *image_info,Image *image,
538 const size_t data_size)
540 #define MaxCode(number_bits) ((one << (number_bits))-1)
541 #define MaxHashTable 5003
542 #define MaxGIFBits 12UL
543 #define MaxGIFTable (1UL << MaxGIFBits)
544 #define GIFOutputCode(code) \
550 datum|=(code) << bits; \
557 Add a character to current packet. \
559 packet[length++]=(unsigned char) (datum & 0xff); \
562 (void) WriteBlobByte(image,(unsigned char) length); \
563 (void) WriteBlob(image,length,packet); \
569 if (free_code > max_code) \
572 if (number_bits == MaxGIFBits) \
573 max_code=MaxGIFTable; \
575 max_code=MaxCode(number_bits); \
608 end_of_information_code,
616 Allocate encoder tables.
618 assert(image != (Image *) NULL);
620 packet=(unsigned char *) AcquireQuantumMemory(256,sizeof(*packet));
621 hash_code=(short *) AcquireQuantumMemory(MaxHashTable,sizeof(*hash_code));
622 hash_prefix=(short *) AcquireQuantumMemory(MaxHashTable,sizeof(*hash_prefix));
623 hash_suffix=(unsigned char *) AcquireQuantumMemory(MaxHashTable,
624 sizeof(*hash_suffix));
625 if ((packet == (unsigned char *) NULL) || (hash_code == (short *) NULL) ||
626 (hash_prefix == (short *) NULL) ||
627 (hash_suffix == (unsigned char *) NULL))
629 if (packet != (unsigned char *) NULL)
630 packet=(unsigned char *) RelinquishMagickMemory(packet);
631 if (hash_code != (short *) NULL)
632 hash_code=(short *) RelinquishMagickMemory(hash_code);
633 if (hash_prefix != (short *) NULL)
634 hash_prefix=(short *) RelinquishMagickMemory(hash_prefix);
635 if (hash_suffix != (unsigned char *) NULL)
636 hash_suffix=(unsigned char *) RelinquishMagickMemory(hash_suffix);
640 Initialize GIF encoder.
642 number_bits=data_size;
643 max_code=MaxCode(number_bits);
644 clear_code=((short) one << (data_size-1));
645 end_of_information_code=clear_code+1;
646 free_code=clear_code+2;
650 for (i=0; i < MaxHashTable; i++)
652 GIFOutputCode(clear_code);
659 for (y=0; y < (ssize_t) image->rows; y++)
661 register const IndexPacket
664 register const PixelPacket
670 p=GetVirtualPixels(image,0,offset,image->columns,1,&image->exception);
671 if (p == (const PixelPacket *) NULL)
673 indexes=GetVirtualIndexQueue(image);
675 waiting_code=(short) (*indexes);
676 for (x=(ssize_t) (y == 0 ? 1 : 0); x < (ssize_t) image->columns; x++)
681 index=(IndexPacket) ((size_t) indexes[x] & 0xff);
683 k=(ssize_t) (((size_t) index << (MaxGIFBits-8))+waiting_code);
684 if (k >= MaxHashTable)
686 next_pixel=MagickFalse;
688 if (hash_code[k] > 0)
690 if ((hash_prefix[k] == waiting_code) &&
691 (hash_suffix[k] == (unsigned char) index))
693 waiting_code=hash_code[k];
697 displacement=MaxHashTable-k;
703 if (hash_code[k] == 0)
705 if ((hash_prefix[k] == waiting_code) &&
706 (hash_suffix[k] == (unsigned char) index))
708 waiting_code=hash_code[k];
709 next_pixel=MagickTrue;
713 if (next_pixel == MagickTrue)
716 GIFOutputCode((size_t) waiting_code);
717 if (free_code < MaxGIFTable)
719 hash_code[k]=(short) free_code++;
720 hash_prefix[k]=waiting_code;
721 hash_suffix[k]=(unsigned char) index;
726 Fill the hash table with empty entries.
728 for (k=0; k < MaxHashTable; k++)
731 Reset compressor and issue a clear code.
733 free_code=clear_code+2;
734 GIFOutputCode(clear_code);
735 number_bits=data_size;
736 max_code=MaxCode(number_bits);
738 waiting_code=(short) index;
740 if (image_info->interlace == NoInterlace)
749 if (offset >= (ssize_t) image->rows)
759 if (offset >= (ssize_t) image->rows)
769 if (offset >= (ssize_t) image->rows)
784 Flush out the buffered code.
786 GIFOutputCode((size_t) waiting_code);
787 GIFOutputCode(end_of_information_code);
791 Add a character to current packet.
793 packet[length++]=(unsigned char) (datum & 0xff);
796 (void) WriteBlobByte(image,(unsigned char) length);
797 (void) WriteBlob(image,length,packet);
802 Flush accumulated data.
806 (void) WriteBlobByte(image,(unsigned char) length);
807 (void) WriteBlob(image,length,packet);
812 hash_suffix=(unsigned char *) RelinquishMagickMemory(hash_suffix);
813 hash_prefix=(short *) RelinquishMagickMemory(hash_prefix);
814 hash_code=(short *) RelinquishMagickMemory(hash_code);
815 packet=(unsigned char *) RelinquishMagickMemory(packet);
820 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
828 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
830 % IsGIF() returns MagickTrue if the image format type, identified by the
831 % magick string, is GIF.
833 % The format of the IsGIF method is:
835 % MagickBooleanType IsGIF(const unsigned char *magick,const size_t length)
837 % A description of each parameter follows:
839 % o magick: compare image format pattern against these bytes.
841 % o length: Specifies the length of the magick string.
844 static MagickBooleanType IsGIF(const unsigned char *magick,const size_t length)
848 if (LocaleNCompare((char *) magick,"GIF8",4) == 0)
854 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
858 + R e a d B l o b B l o c k %
862 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
864 % ReadBlobBlock() reads data from the image file and returns it. The
865 % amount of data is determined by first reading a count byte. The number
866 % of bytes read is returned.
868 % The format of the ReadBlobBlock method is:
870 % size_t ReadBlobBlock(Image *image,unsigned char *data)
872 % A description of each parameter follows:
874 % o image: the image.
876 % o data: Specifies an area to place the information requested from
880 static ssize_t ReadBlobBlock(Image *image,unsigned char *data)
888 assert(image != (Image *) NULL);
889 assert(image->signature == MagickSignature);
890 assert(data != (unsigned char *) NULL);
891 count=ReadBlob(image,1,&block_count);
894 return(ReadBlob(image,(size_t) block_count,data));
898 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
902 % R e a d G I F I m a g e %
906 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
908 % ReadGIFImage() reads a Compuserve Graphics image file and returns it.
909 % It allocates the memory necessary for the new Image structure and returns a
910 % pointer to the new image.
912 % The format of the ReadGIFImage method is:
914 % Image *ReadGIFImage(const ImageInfo *image_info,ExceptionInfo *exception)
916 % A description of each parameter follows:
918 % o image_info: the image info.
920 % o exception: return any errors or warnings in this structure.
924 static inline size_t MagickMax(const size_t x,const size_t y)
931 static inline size_t MagickMin(const size_t x,const size_t y)
938 static MagickBooleanType PingGIFImage(Image *image)
945 assert(image != (Image *) NULL);
946 assert(image->signature == MagickSignature);
947 if (image->debug != MagickFalse)
948 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
949 if (ReadBlob(image,1,&data_size) != 1)
950 ThrowBinaryException(CorruptImageError,"CorruptImage",image->filename);
951 if (data_size > MaximumLZWBits)
952 ThrowBinaryException(CorruptImageError,"CorruptImage",image->filename);
953 if (ReadBlob(image,1,&length) != 1)
954 ThrowBinaryException(CorruptImageError,"CorruptImage",image->filename);
957 if (ReadBlob(image,length,buffer) != (ssize_t) length)
958 ThrowBinaryException(CorruptImageError,"CorruptImage",image->filename);
959 if (ReadBlob(image,1,&length) != 1)
960 ThrowBinaryException(CorruptImageError,"CorruptImage",image->filename);
965 static Image *ReadGIFImage(const ImageInfo *image_info,ExceptionInfo *exception)
967 #define BitSet(byte,bit) (((byte) & (bit)) == (bit))
968 #define LSBFirstOrder(x,y) (((y) << 8) | (x))
974 number_extensionss=0;
988 register unsigned char
999 header[MaxTextExtent],
1013 assert(image_info != (const ImageInfo *) NULL);
1014 assert(image_info->signature == MagickSignature);
1015 if (image_info->debug != MagickFalse)
1016 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
1017 image_info->filename);
1018 assert(exception != (ExceptionInfo *) NULL);
1019 assert(exception->signature == MagickSignature);
1020 image=AcquireImage(image_info);
1021 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
1022 if (status == MagickFalse)
1024 image=DestroyImageList(image);
1025 return((Image *) NULL);
1028 Determine if this a GIF file.
1030 count=ReadBlob(image,6,magick);
1031 if ((count != 6) || ((LocaleNCompare((char *) magick,"GIF87",5) != 0) &&
1032 (LocaleNCompare((char *) magick,"GIF89",5) != 0)))
1033 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1034 page.width=ReadBlobLSBShort(image);
1035 page.height=ReadBlobLSBShort(image);
1036 flag=(unsigned char) ReadBlobByte(image);
1037 background=(unsigned char) ReadBlobByte(image);
1038 c=(unsigned char) ReadBlobByte(image); /* reserved */
1040 global_colors=one << (((size_t) flag & 0x07)+1);
1041 global_colormap=(unsigned char *) AcquireQuantumMemory((size_t)
1042 MagickMax(global_colors,256),3UL*sizeof(*global_colormap));
1043 if (global_colormap == (unsigned char *) NULL)
1044 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1045 if (BitSet((int) flag,0x80) != 0)
1046 count=ReadBlob(image,(size_t) (3*global_colors),global_colormap);
1054 count=ReadBlob(image,1,&c);
1057 if (c == (unsigned char) ';')
1058 break; /* terminator */
1059 if (c == (unsigned char) '!')
1062 GIF Extension block.
1065 count=ReadBlob(image,1,&c);
1068 global_colormap=(unsigned char *) RelinquishMagickMemory(
1070 ThrowReaderException(CorruptImageError,
1071 "UnableToReadExtensionBlock");
1078 Read graphics control extension.
1080 while (ReadBlobBlock(image,header) != 0) ;
1081 dispose=(size_t) (header[0] >> 2);
1082 delay=(size_t) ((header[2] << 8) | header[1]);
1083 if ((ssize_t) (header[0] & 0x01) == 0x01)
1084 opacity=(ssize_t) header[3];
1093 Read comment extension.
1095 comments=AcquireString((char *) NULL);
1098 count=(ssize_t) ReadBlobBlock(image,header);
1102 (void) ConcatenateString(&comments,(const char *) header);
1104 (void) SetImageProperty(image,"comment",comments);
1105 comments=DestroyString(comments);
1110 /* Read GIF application extension */
1116 Read Netscape Loop extension.
1119 if (ReadBlobBlock(image,header) != 0)
1120 loop=LocaleNCompare((char *) header,"NETSCAPE2.0",11) == 0 ?
1121 MagickTrue : MagickFalse;
1122 if (loop != MagickFalse)
1124 while (ReadBlobBlock(image,header) != 0)
1125 iterations=(size_t) ((header[2] << 8) | header[1]);
1131 name[MaxTextExtent];
1150 Store GIF application extension as a generic profile.
1152 i8bim=LocaleNCompare((char *) header,"MGK8BIM0000",11) == 0 ?
1153 MagickTrue : MagickFalse;
1154 icc=LocaleNCompare((char *) header,"ICCRGBG1012",11) == 0 ?
1155 MagickTrue : MagickFalse;
1156 iptc=LocaleNCompare((char *) header,"MGKIPTC0000",11) == 0 ?
1157 MagickTrue : MagickFalse;
1158 number_extensionss++;
1159 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1160 " Reading GIF application extension");
1161 info=(unsigned char *) AcquireQuantumMemory(255UL,
1163 reserved_length=255;
1164 for (info_length=0; ; )
1166 block_length=(int) ReadBlobBlock(image,&info[info_length]);
1167 if (block_length == 0)
1169 info_length+=block_length;
1170 if (info_length > (reserved_length-255))
1172 reserved_length+=4096;
1173 info=(unsigned char *) ResizeQuantumMemory(info,
1174 (size_t) reserved_length,sizeof(*info));
1177 info=(unsigned char *) ResizeQuantumMemory(info,(size_t)
1178 (info_length+1),sizeof(*info));
1179 profile=AcquireStringInfo((size_t) info_length);
1180 SetStringInfoDatum(profile,(const unsigned char *) info);
1181 if (i8bim == MagickTrue)
1182 (void) CopyMagickString(name,"8bim",sizeof(name));
1183 else if (icc == MagickTrue)
1184 (void) CopyMagickString(name,"icc",sizeof(name));
1185 else if (iptc == MagickTrue)
1186 (void) CopyMagickString(name,"iptc",sizeof(name));
1188 (void) FormatMagickString(name,sizeof(name),"gif:%.11s",
1190 (void) SetImageProfile(image,name,profile);
1191 info=(unsigned char *) RelinquishMagickMemory(info);
1192 profile=DestroyStringInfo(profile);
1193 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1194 " profile name=%s",name);
1200 while (ReadBlobBlock(image,header) != 0) ;
1205 if (c != (unsigned char) ',')
1207 if (image_count != 0)
1210 Allocate next image structure.
1212 AcquireNextImage(image_info,image);
1213 if (GetNextImageInList(image) == (Image *) NULL)
1215 image=DestroyImageList(image);
1216 global_colormap=(unsigned char *) RelinquishMagickMemory(
1218 return((Image *) NULL);
1220 image=SyncNextImageInList(image);
1224 Read image attributes.
1226 image->storage_class=PseudoClass;
1227 image->compression=LZWCompression;
1228 page.x=(ssize_t) ReadBlobLSBShort(image);
1229 page.y=(ssize_t) ReadBlobLSBShort(image);
1230 image->columns=ReadBlobLSBShort(image);
1231 image->rows=ReadBlobLSBShort(image);
1233 flag=(unsigned char) ReadBlobByte(image);
1234 image->interlace=BitSet((int) flag,0x40) != 0 ? GIFInterlace :
1236 image->colors=BitSet((int) flag,0x80) == 0 ? global_colors :
1237 one << ((size_t) (flag & 0x07)+1);
1238 if (opacity >= (ssize_t) image->colors)
1240 image->page.width=page.width;
1241 image->page.height=page.height;
1242 image->page.y=page.y;
1243 image->page.x=page.x;
1245 image->ticks_per_second=100;
1246 image->dispose=(DisposeType) dispose;
1247 image->iterations=iterations;
1248 image->matte=opacity >= 0 ? MagickTrue : MagickFalse;
1252 if ((image->columns == 0) || (image->rows == 0))
1254 global_colormap=(unsigned char *) RelinquishMagickMemory(
1256 ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
1259 Inititialize colormap.
1261 if (AcquireImageColormap(image,image->colors) == MagickFalse)
1263 global_colormap=(unsigned char *) RelinquishMagickMemory(
1265 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1267 if (BitSet((int) flag,0x80) == 0)
1270 Use global colormap.
1273 for (i=0; i < (ssize_t) image->colors; i++)
1275 image->colormap[i].red=ScaleCharToQuantum(*p++);
1276 image->colormap[i].green=ScaleCharToQuantum(*p++);
1277 image->colormap[i].blue=ScaleCharToQuantum(*p++);
1280 image->colormap[i].opacity=(Quantum) TransparentOpacity;
1281 image->transparent_color=image->colormap[opacity];
1284 image->background_color=image->colormap[MagickMin(background,
1293 Read local colormap.
1295 colormap=(unsigned char *) AcquireQuantumMemory(image->colors,
1296 3*sizeof(*colormap));
1297 if (colormap == (unsigned char *) NULL)
1299 global_colormap=(unsigned char *) RelinquishMagickMemory(
1301 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1303 count=ReadBlob(image,(3*image->colors)*sizeof(*colormap),colormap);
1304 if (count != (ssize_t) (3*image->colors))
1306 global_colormap=(unsigned char *) RelinquishMagickMemory(
1308 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
1309 ThrowReaderException(CorruptImageError,
1310 "InsufficientImageDataInFile");
1313 for (i=0; i < (ssize_t) image->colors; i++)
1315 image->colormap[i].red=ScaleCharToQuantum(*p++);
1316 image->colormap[i].green=ScaleCharToQuantum(*p++);
1317 image->colormap[i].blue=ScaleCharToQuantum(*p++);
1319 image->colormap[i].opacity=(Quantum) TransparentOpacity;
1321 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
1323 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
1324 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1329 if (image_info->ping != MagickFalse)
1330 status=PingGIFImage(image);
1332 status=DecodeImage(image,opacity);
1333 if ((image_info->ping == MagickFalse) && (status == MagickFalse))
1335 global_colormap=(unsigned char *) RelinquishMagickMemory(
1337 ThrowReaderException(CorruptImageError,"CorruptImage");
1339 if (image_info->number_scenes != 0)
1340 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1343 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) image->scene-
1345 if (status == MagickFalse)
1348 global_colormap=(unsigned char *) RelinquishMagickMemory(global_colormap);
1349 if ((image->columns == 0) || (image->rows == 0))
1350 ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
1351 (void) CloseBlob(image);
1352 return(GetFirstImageInList(image));
1356 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1360 % R e g i s t e r G I F I m a g e %
1364 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1366 % RegisterGIFImage() adds properties for the GIF image format to
1367 % the list of supported formats. The properties include the image format
1368 % tag, a method to read and/or write the format, whether the format
1369 % supports the saving of more than one frame to the same file or blob,
1370 % whether the format supports native in-memory I/O, and a brief
1371 % description of the format.
1373 % The format of the RegisterGIFImage method is:
1375 % size_t RegisterGIFImage(void)
1378 ModuleExport size_t RegisterGIFImage(void)
1383 entry=SetMagickInfo("GIF");
1384 entry->decoder=(DecodeImageHandler *) ReadGIFImage;
1385 entry->encoder=(EncodeImageHandler *) WriteGIFImage;
1386 entry->magick=(IsImageFormatHandler *) IsGIF;
1387 entry->description=ConstantString("CompuServe graphics interchange format");
1388 entry->module=ConstantString("GIF");
1389 (void) RegisterMagickInfo(entry);
1390 entry=SetMagickInfo("GIF87");
1391 entry->decoder=(DecodeImageHandler *) ReadGIFImage;
1392 entry->encoder=(EncodeImageHandler *) WriteGIFImage;
1393 entry->magick=(IsImageFormatHandler *) IsGIF;
1394 entry->adjoin=MagickFalse;
1395 entry->description=ConstantString("CompuServe graphics interchange format");
1396 entry->version=ConstantString("version 87a");
1397 entry->module=ConstantString("GIF");
1398 (void) RegisterMagickInfo(entry);
1399 return(MagickImageCoderSignature);
1403 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1407 % U n r e g i s t e r G I F I m a g e %
1411 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1413 % UnregisterGIFImage() removes format registrations made by the
1414 % GIF module from the list of supported formats.
1416 % The format of the UnregisterGIFImage method is:
1418 % UnregisterGIFImage(void)
1421 ModuleExport void UnregisterGIFImage(void)
1423 (void) UnregisterMagickInfo("GIF");
1424 (void) UnregisterMagickInfo("GIF87");
1428 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1432 % W r i t e G I F I m a g e %
1436 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1438 % WriteGIFImage() writes an image to a file in the Compuserve Graphics
1441 % The format of the WriteGIFImage method is:
1443 % MagickBooleanType WriteGIFImage(const ImageInfo *image_info,Image *image)
1445 % A description of each parameter follows.
1447 % o image_info: the image info.
1449 % o image: The image.
1452 static MagickBooleanType WriteGIFImage(const ImageInfo *image_info,Image *image)
1482 register unsigned char
1498 Open output image file.
1500 assert(image_info != (const ImageInfo *) NULL);
1501 assert(image_info->signature == MagickSignature);
1502 assert(image != (Image *) NULL);
1503 assert(image->signature == MagickSignature);
1504 if (image->debug != MagickFalse)
1505 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1506 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
1507 if (status == MagickFalse)
1512 global_colormap=(unsigned char *) AcquireQuantumMemory(768UL,
1513 sizeof(*global_colormap));
1514 colormap=(unsigned char *) AcquireQuantumMemory(768UL,sizeof(*colormap));
1515 if ((global_colormap == (unsigned char *) NULL) ||
1516 (colormap == (unsigned char *) NULL))
1517 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1518 for (i=0; i < 768; i++)
1519 colormap[i]=(unsigned char) 0;
1523 write_info=CloneImageInfo(image_info);
1524 if (LocaleCompare(write_info->magick,"GIF87") != 0)
1525 (void) WriteBlob(image,6,(unsigned char *) "GIF89a");
1528 (void) WriteBlob(image,6,(unsigned char *) "GIF87a");
1529 write_info->adjoin=MagickFalse;
1532 Determine image bounding box.
1534 page.width=image->columns;
1535 page.height=image->rows;
1538 if (write_info->adjoin != MagickFalse)
1539 for (next_image=image; next_image != (Image *) NULL; )
1541 page.x=next_image->page.x;
1542 page.y=next_image->page.y;
1543 if ((next_image->page.width+page.x) > page.width)
1544 page.width=next_image->page.width+page.x;
1545 if ((next_image->page.height+page.y) > page.height)
1546 page.height=next_image->page.height+page.y;
1547 next_image=GetNextImageInList(next_image);
1549 page.x=image->page.x;
1550 page.y=image->page.y;
1551 if ((image->page.width != 0) && (image->page.height != 0))
1553 (void) WriteBlobLSBShort(image,(unsigned short) page.width);
1554 (void) WriteBlobLSBShort(image,(unsigned short) page.height);
1556 Write images to file.
1558 interlace=write_info->interlace;
1559 if ((write_info->adjoin != MagickFalse) &&
1560 (GetNextImageInList(image) != (Image *) NULL))
1561 interlace=NoInterlace;
1566 if (image->colorspace != RGBColorspace)
1567 (void) TransformImageColorspace(image,RGBColorspace);
1569 if (IsOpaqueImage(image,&image->exception) != MagickFalse)
1571 if ((image->storage_class == DirectClass) || (image->colors > 256))
1572 (void) SetImageType(image,PaletteType);
1581 Identify transparent colormap index.
1583 if ((image->storage_class == DirectClass) || (image->colors > 256))
1584 (void) SetImageType(image,PaletteBilevelMatteType);
1585 for (i=0; i < (ssize_t) image->colors; i++)
1586 if (image->colormap[i].opacity != OpaqueOpacity)
1593 alpha=(MagickRealType) TransparentOpacity-(MagickRealType)
1594 image->colormap[i].opacity;
1595 beta=(MagickRealType) TransparentOpacity-(MagickRealType)
1596 image->colormap[opacity].opacity;
1602 (void) SetImageType(image,PaletteBilevelMatteType);
1603 for (i=0; i < (ssize_t) image->colors; i++)
1604 if (image->colormap[i].opacity != OpaqueOpacity)
1611 alpha=(Quantum) TransparentOpacity-(MagickRealType)
1612 image->colormap[i].opacity;
1613 beta=(Quantum) TransparentOpacity-(MagickRealType)
1614 image->colormap[opacity].opacity;
1621 image->colormap[opacity].red=image->transparent_color.red;
1622 image->colormap[opacity].green=image->transparent_color.green;
1623 image->colormap[opacity].blue=image->transparent_color.blue;
1626 if ((image->storage_class == DirectClass) || (image->colors > 256))
1627 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1628 for (bits_per_pixel=1; bits_per_pixel < 8; bits_per_pixel++)
1629 if ((one << bits_per_pixel) >= image->colors)
1632 for (i=0; i < (ssize_t) image->colors; i++)
1634 *q++=ScaleQuantumToChar(image->colormap[i].red);
1635 *q++=ScaleQuantumToChar(image->colormap[i].green);
1636 *q++=ScaleQuantumToChar(image->colormap[i].blue);
1638 for ( ; i < (ssize_t) (one << bits_per_pixel); i++)
1640 *q++=(unsigned char) 0x0;
1641 *q++=(unsigned char) 0x0;
1642 *q++=(unsigned char) 0x0;
1644 if ((GetPreviousImageInList(image) == (Image *) NULL) ||
1645 (write_info->adjoin == MagickFalse))
1648 Write global colormap.
1651 c|=(8-1) << 4; /* color resolution */
1652 c|=(bits_per_pixel-1); /* size of global colormap */
1653 (void) WriteBlobByte(image,(unsigned char) c);
1654 for (j=0; j < (ssize_t) image->colors; j++)
1655 if (IsColorEqual(&image->background_color,image->colormap+j))
1657 (void) WriteBlobByte(image,(unsigned char)
1658 (j == (ssize_t) image->colors ? 0 : j)); /* background color */
1659 (void) WriteBlobByte(image,(unsigned char) 0x00); /* reserved */
1660 length=(size_t) (3*(one << bits_per_pixel));
1661 (void) WriteBlob(image,length,colormap);
1662 for (j=0; j < 768; j++)
1663 global_colormap[j]=colormap[j];
1665 if (LocaleCompare(write_info->magick,"GIF87") != 0)
1668 Write graphics control extension.
1670 (void) WriteBlobByte(image,(unsigned char) 0x21);
1671 (void) WriteBlobByte(image,(unsigned char) 0xf9);
1672 (void) WriteBlobByte(image,(unsigned char) 0x04);
1673 c=image->dispose << 2;
1676 (void) WriteBlobByte(image,(unsigned char) c);
1677 delay=(size_t) (100*image->delay/MagickMax((size_t)
1678 image->ticks_per_second,1));
1679 (void) WriteBlobLSBShort(image,(unsigned short) delay);
1680 (void) WriteBlobByte(image,(unsigned char) (opacity >= 0 ? opacity :
1682 (void) WriteBlobByte(image,(unsigned char) 0x00);
1683 if ((LocaleCompare(write_info->magick,"GIF87") != 0) &&
1684 (GetImageProperty(image,"comment") != (const char *) NULL))
1696 Write Comment extension.
1698 (void) WriteBlobByte(image,(unsigned char) 0x21);
1699 (void) WriteBlobByte(image,(unsigned char) 0xfe);
1700 value=GetImageProperty(image,"comment");
1702 while (strlen(p) != 0)
1704 count=MagickMin(strlen(p),255);
1705 (void) WriteBlobByte(image,(unsigned char) count);
1706 for (i=0; i < (ssize_t) count; i++)
1707 (void) WriteBlobByte(image,(unsigned char) *p++);
1709 (void) WriteBlobByte(image,(unsigned char) 0x00);
1711 if ((GetPreviousImageInList(image) == (Image *) NULL) &&
1712 (GetNextImageInList(image) != (Image *) NULL) &&
1713 (image->iterations != 1))
1716 Write Netscape Loop extension.
1718 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1719 " Writing GIF Extension %s","NETSCAPE2.0");
1720 (void) WriteBlobByte(image,(unsigned char) 0x21);
1721 (void) WriteBlobByte(image,(unsigned char) 0xff);
1722 (void) WriteBlobByte(image,(unsigned char) 0x0b);
1723 (void) WriteBlob(image,11,(unsigned char *) "NETSCAPE2.0");
1724 (void) WriteBlobByte(image,(unsigned char) 0x03);
1725 (void) WriteBlobByte(image,(unsigned char) 0x01);
1726 (void) WriteBlobLSBShort(image,(unsigned short) image->iterations);
1727 (void) WriteBlobByte(image,(unsigned char) 0x00);
1729 ResetImageProfileIterator(image);
1738 name=GetNextImageProfile(image);
1739 if (name == (const char *) NULL)
1741 profile=GetImageProfile(image,name);
1742 if (profile != (StringInfo *) NULL)
1744 if ((LocaleCompare(name,"ICC") == 0) ||
1745 (LocaleCompare(name,"ICM") == 0) ||
1746 (LocaleCompare(name,"IPTC") == 0) ||
1747 (LocaleCompare(name,"8BIM") == 0) ||
1748 (LocaleNCompare(name,"gif:",4) == 0))
1759 datum=GetStringInfoDatum(profile);
1760 length=GetStringInfoLength(profile);
1761 (void) WriteBlobByte(image,(unsigned char) 0x21);
1762 (void) WriteBlobByte(image,(unsigned char) 0xff);
1763 (void) WriteBlobByte(image,(unsigned char) 0x0b);
1764 if ((LocaleCompare(name,"ICC") == 0) ||
1765 (LocaleCompare(name,"ICM") == 0))
1768 Write ICC extension.
1770 (void) WriteBlob(image,11,(unsigned char *)"ICCRGBG1012");
1771 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1772 " Writing GIF Extension %s","ICCRGBG1012");
1775 if ((LocaleCompare(name,"IPTC") == 0))
1778 write IPTC extension.
1780 (void) WriteBlob(image,11,(unsigned char *)"MGKIPTC0000");
1781 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1782 " Writing GIF Extension %s","MGKIPTC0000");
1785 if ((LocaleCompare(name,"8BIM") == 0))
1788 Write 8BIM extension>
1790 (void) WriteBlob(image,11,(unsigned char *)
1792 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1793 " Writing GIF Extension %s","MGK8BIM0000");
1798 extension[MaxTextExtent];
1800 /* write generic extension */
1801 (void) CopyMagickString(extension,name+4,
1803 (void) WriteBlob(image,11,(unsigned char *) extension);
1804 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1805 " Writing GIF Extension %s",name);
1808 while ((ssize_t) length > offset)
1813 if ((length-offset) < 255)
1814 block_length=length-offset;
1817 (void) WriteBlobByte(image,(unsigned char) block_length);
1818 (void) WriteBlob(image,(size_t) block_length,datum+offset);
1819 offset+=(ssize_t) block_length;
1821 (void) WriteBlobByte(image,(unsigned char) 0x00);
1826 (void) WriteBlobByte(image,','); /* image separator */
1828 Write the image header.
1830 page.x=image->page.x;
1831 page.y=image->page.y;
1832 if ((image->page.width != 0) && (image->page.height != 0))
1834 (void) WriteBlobLSBShort(image,(unsigned short) (page.x < 0 ? 0 : page.x));
1835 (void) WriteBlobLSBShort(image,(unsigned short) (page.y < 0 ? 0 : page.y));
1836 (void) WriteBlobLSBShort(image,(unsigned short) image->columns);
1837 (void) WriteBlobLSBShort(image,(unsigned short) image->rows);
1839 if (interlace != NoInterlace)
1840 c|=0x40; /* pixel data is interlaced */
1841 for (j=0; j < (ssize_t) (3*image->colors); j++)
1842 if (colormap[j] != global_colormap[j])
1844 if (j == (ssize_t) (3*image->colors))
1845 (void) WriteBlobByte(image,(unsigned char) c);
1849 c|=(bits_per_pixel-1); /* size of local colormap */
1850 (void) WriteBlobByte(image,(unsigned char) c);
1851 length=(size_t) (3*(one << bits_per_pixel));
1852 (void) WriteBlob(image,length,colormap);
1855 Write the image data.
1857 c=(int) MagickMax(bits_per_pixel,2);
1858 (void) WriteBlobByte(image,(unsigned char) c);
1859 status=EncodeImage(write_info,image,(size_t) MagickMax(bits_per_pixel,2)+1);
1860 if (status == MagickFalse)
1862 global_colormap=(unsigned char *) RelinquishMagickMemory(
1864 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
1865 write_info=DestroyImageInfo(write_info);
1866 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1868 (void) WriteBlobByte(image,(unsigned char) 0x00);
1869 if (GetNextImageInList(image) == (Image *) NULL)
1871 image=SyncNextImageInList(image);
1873 status=SetImageProgress(image,SaveImagesTag,scene,
1874 GetImageListLength(image));
1875 if (status == MagickFalse)
1877 } while (write_info->adjoin != MagickFalse);
1878 (void) WriteBlobByte(image,';'); /* terminator */
1879 global_colormap=(unsigned char *) RelinquishMagickMemory(global_colormap);
1880 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
1881 write_info=DestroyImageInfo(write_info);
1882 (void) CloseBlob(image);