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 SetIndexPixelComponent(indexes+x,index);
449 SetRGBOPixelComponents(q,image->colormap+(ssize_t) index);
450 SetOpacityPixelComponent(q,(ssize_t) index == opacity ?
451 TransparentOpacity : OpaqueOpacity);
455 if (x < (ssize_t) image->columns)
457 if (image->interlace == NoInterlace)
466 if (offset >= (ssize_t) image->rows)
476 if (offset >= (ssize_t) image->rows)
486 if (offset >= (ssize_t) image->rows)
499 if (SyncAuthenticPixels(image,exception) == MagickFalse)
502 lzw_info=RelinquishLZWInfo(lzw_info);
503 if (y < (ssize_t) image->rows)
504 ThrowBinaryException(CorruptImageError,"CorruptImage",image->filename);
509 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
513 % E n c o d e I m a g e %
517 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
519 % EncodeImage compresses an image via GIF-coding.
521 % The format of the EncodeImage method is:
523 % MagickBooleanType EncodeImage(const ImageInfo *image_info,Image *image,
524 % const size_t data_size)
526 % A description of each parameter follows:
528 % o image_info: the image info.
530 % o image: the address of a structure of type Image.
532 % o data_size: The number of bits in the compressed packet.
535 static MagickBooleanType EncodeImage(const ImageInfo *image_info,Image *image,
536 const size_t data_size)
538 #define MaxCode(number_bits) ((one << (number_bits))-1)
539 #define MaxHashTable 5003
540 #define MaxGIFBits 12UL
541 #define MaxGIFTable (1UL << MaxGIFBits)
542 #define GIFOutputCode(code) \
548 datum|=(code) << bits; \
555 Add a character to current packet. \
557 packet[length++]=(unsigned char) (datum & 0xff); \
560 (void) WriteBlobByte(image,(unsigned char) length); \
561 (void) WriteBlob(image,length,packet); \
567 if (free_code > max_code) \
570 if (number_bits == MaxGIFBits) \
571 max_code=MaxGIFTable; \
573 max_code=MaxCode(number_bits); \
592 end_of_information_code,
612 Allocate encoder tables.
614 assert(image != (Image *) NULL);
616 packet=(unsigned char *) AcquireQuantumMemory(256,sizeof(*packet));
617 hash_code=(short *) AcquireQuantumMemory(MaxHashTable,sizeof(*hash_code));
618 hash_prefix=(short *) AcquireQuantumMemory(MaxHashTable,sizeof(*hash_prefix));
619 hash_suffix=(unsigned char *) AcquireQuantumMemory(MaxHashTable,
620 sizeof(*hash_suffix));
621 if ((packet == (unsigned char *) NULL) || (hash_code == (short *) NULL) ||
622 (hash_prefix == (short *) NULL) ||
623 (hash_suffix == (unsigned char *) NULL))
625 if (packet != (unsigned char *) NULL)
626 packet=(unsigned char *) RelinquishMagickMemory(packet);
627 if (hash_code != (short *) NULL)
628 hash_code=(short *) RelinquishMagickMemory(hash_code);
629 if (hash_prefix != (short *) NULL)
630 hash_prefix=(short *) RelinquishMagickMemory(hash_prefix);
631 if (hash_suffix != (unsigned char *) NULL)
632 hash_suffix=(unsigned char *) RelinquishMagickMemory(hash_suffix);
636 Initialize GIF encoder.
638 number_bits=data_size;
639 max_code=MaxCode(number_bits);
640 clear_code=((short) one << (data_size-1));
641 end_of_information_code=clear_code+1;
642 free_code=clear_code+2;
646 for (i=0; i < MaxHashTable; i++)
648 GIFOutputCode(clear_code);
655 for (y=0; y < (ssize_t) image->rows; y++)
657 register const IndexPacket
660 register const PixelPacket
666 p=GetVirtualPixels(image,0,offset,image->columns,1,&image->exception);
667 if (p == (const PixelPacket *) NULL)
669 indexes=GetVirtualIndexQueue(image);
671 waiting_code=(short) (*indexes);
672 for (x=(ssize_t) (y == 0 ? 1 : 0); x < (ssize_t) image->columns; x++)
677 index=(IndexPacket) ((size_t) GetIndexPixelComponent(indexes+x) & 0xff);
679 k=(ssize_t) (((size_t) index << (MaxGIFBits-8))+waiting_code);
680 if (k >= MaxHashTable)
682 next_pixel=MagickFalse;
684 if (hash_code[k] > 0)
686 if ((hash_prefix[k] == waiting_code) &&
687 (hash_suffix[k] == (unsigned char) index))
689 waiting_code=hash_code[k];
693 displacement=MaxHashTable-k;
699 if (hash_code[k] == 0)
701 if ((hash_prefix[k] == waiting_code) &&
702 (hash_suffix[k] == (unsigned char) index))
704 waiting_code=hash_code[k];
705 next_pixel=MagickTrue;
709 if (next_pixel == MagickTrue)
712 GIFOutputCode((size_t) waiting_code);
713 if (free_code < MaxGIFTable)
715 hash_code[k]=(short) free_code++;
716 hash_prefix[k]=waiting_code;
717 hash_suffix[k]=(unsigned char) index;
722 Fill the hash table with empty entries.
724 for (k=0; k < MaxHashTable; k++)
727 Reset compressor and issue a clear code.
729 free_code=clear_code+2;
730 GIFOutputCode(clear_code);
731 number_bits=data_size;
732 max_code=MaxCode(number_bits);
734 waiting_code=(short) index;
736 if (image_info->interlace == NoInterlace)
745 if (offset >= (ssize_t) image->rows)
755 if (offset >= (ssize_t) image->rows)
765 if (offset >= (ssize_t) image->rows)
780 Flush out the buffered code.
782 GIFOutputCode((size_t) waiting_code);
783 GIFOutputCode(end_of_information_code);
787 Add a character to current packet.
789 packet[length++]=(unsigned char) (datum & 0xff);
792 (void) WriteBlobByte(image,(unsigned char) length);
793 (void) WriteBlob(image,length,packet);
798 Flush accumulated data.
802 (void) WriteBlobByte(image,(unsigned char) length);
803 (void) WriteBlob(image,length,packet);
808 hash_suffix=(unsigned char *) RelinquishMagickMemory(hash_suffix);
809 hash_prefix=(short *) RelinquishMagickMemory(hash_prefix);
810 hash_code=(short *) RelinquishMagickMemory(hash_code);
811 packet=(unsigned char *) RelinquishMagickMemory(packet);
816 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
824 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
826 % IsGIF() returns MagickTrue if the image format type, identified by the
827 % magick string, is GIF.
829 % The format of the IsGIF method is:
831 % MagickBooleanType IsGIF(const unsigned char *magick,const size_t length)
833 % A description of each parameter follows:
835 % o magick: compare image format pattern against these bytes.
837 % o length: Specifies the length of the magick string.
840 static MagickBooleanType IsGIF(const unsigned char *magick,const size_t length)
844 if (LocaleNCompare((char *) magick,"GIF8",4) == 0)
850 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
854 + R e a d B l o b B l o c k %
858 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
860 % ReadBlobBlock() reads data from the image file and returns it. The
861 % amount of data is determined by first reading a count byte. The number
862 % of bytes read is returned.
864 % The format of the ReadBlobBlock method is:
866 % size_t ReadBlobBlock(Image *image,unsigned char *data)
868 % A description of each parameter follows:
870 % o image: the image.
872 % o data: Specifies an area to place the information requested from
876 static ssize_t ReadBlobBlock(Image *image,unsigned char *data)
884 assert(image != (Image *) NULL);
885 assert(image->signature == MagickSignature);
886 assert(data != (unsigned char *) NULL);
887 count=ReadBlob(image,1,&block_count);
890 return(ReadBlob(image,(size_t) block_count,data));
894 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
898 % R e a d G I F I m a g e %
902 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
904 % ReadGIFImage() reads a Compuserve Graphics image file and returns it.
905 % It allocates the memory necessary for the new Image structure and returns a
906 % pointer to the new image.
908 % The format of the ReadGIFImage method is:
910 % Image *ReadGIFImage(const ImageInfo *image_info,ExceptionInfo *exception)
912 % A description of each parameter follows:
914 % o image_info: the image info.
916 % o exception: return any errors or warnings in this structure.
920 static inline size_t MagickMax(const size_t x,const size_t y)
927 static inline size_t MagickMin(const size_t x,const size_t y)
934 static MagickBooleanType PingGIFImage(Image *image)
941 assert(image != (Image *) NULL);
942 assert(image->signature == MagickSignature);
943 if (image->debug != MagickFalse)
944 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
945 if (ReadBlob(image,1,&data_size) != 1)
946 ThrowBinaryException(CorruptImageError,"CorruptImage",image->filename);
947 if (data_size > MaximumLZWBits)
948 ThrowBinaryException(CorruptImageError,"CorruptImage",image->filename);
949 if (ReadBlob(image,1,&length) != 1)
950 ThrowBinaryException(CorruptImageError,"CorruptImage",image->filename);
953 if (ReadBlob(image,length,buffer) != (ssize_t) length)
954 ThrowBinaryException(CorruptImageError,"CorruptImage",image->filename);
955 if (ReadBlob(image,1,&length) != 1)
956 ThrowBinaryException(CorruptImageError,"CorruptImage",image->filename);
961 static Image *ReadGIFImage(const ImageInfo *image_info,ExceptionInfo *exception)
963 #define BitSet(byte,bit) (((byte) & (bit)) == (bit))
964 #define LSBFirstOrder(x,y) (((y) << 8) | (x))
970 number_extensionss=0;
981 register unsigned char
1001 header[MaxTextExtent],
1007 assert(image_info != (const ImageInfo *) NULL);
1008 assert(image_info->signature == MagickSignature);
1009 if (image_info->debug != MagickFalse)
1010 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
1011 image_info->filename);
1012 assert(exception != (ExceptionInfo *) NULL);
1013 assert(exception->signature == MagickSignature);
1014 image=AcquireImage(image_info);
1015 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
1016 if (status == MagickFalse)
1018 image=DestroyImageList(image);
1019 return((Image *) NULL);
1022 Determine if this a GIF file.
1024 count=ReadBlob(image,6,magick);
1025 if ((count != 6) || ((LocaleNCompare((char *) magick,"GIF87",5) != 0) &&
1026 (LocaleNCompare((char *) magick,"GIF89",5) != 0)))
1027 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1028 page.width=ReadBlobLSBShort(image);
1029 page.height=ReadBlobLSBShort(image);
1030 flag=(unsigned char) ReadBlobByte(image);
1031 background=(unsigned char) ReadBlobByte(image);
1032 c=(unsigned char) ReadBlobByte(image); /* reserved */
1034 global_colors=one << (((size_t) flag & 0x07)+1);
1035 global_colormap=(unsigned char *) AcquireQuantumMemory((size_t)
1036 MagickMax(global_colors,256),3UL*sizeof(*global_colormap));
1037 if (global_colormap == (unsigned char *) NULL)
1038 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1039 if (BitSet((int) flag,0x80) != 0)
1040 count=ReadBlob(image,(size_t) (3*global_colors),global_colormap);
1048 count=ReadBlob(image,1,&c);
1051 if (c == (unsigned char) ';')
1052 break; /* terminator */
1053 if (c == (unsigned char) '!')
1056 GIF Extension block.
1059 count=ReadBlob(image,1,&c);
1062 global_colormap=(unsigned char *) RelinquishMagickMemory(
1064 ThrowReaderException(CorruptImageError,
1065 "UnableToReadExtensionBlock");
1072 Read graphics control extension.
1074 while (ReadBlobBlock(image,header) != 0) ;
1075 dispose=(size_t) (header[0] >> 2);
1076 delay=(size_t) ((header[2] << 8) | header[1]);
1077 if ((ssize_t) (header[0] & 0x01) == 0x01)
1078 opacity=(ssize_t) header[3];
1087 Read comment extension.
1089 comments=AcquireString((char *) NULL);
1092 count=(ssize_t) ReadBlobBlock(image,header);
1096 (void) ConcatenateString(&comments,(const char *) header);
1098 (void) SetImageProperty(image,"comment",comments);
1099 comments=DestroyString(comments);
1104 /* Read GIF application extension */
1110 Read Netscape Loop extension.
1113 if (ReadBlobBlock(image,header) != 0)
1114 loop=LocaleNCompare((char *) header,"NETSCAPE2.0",11) == 0 ?
1115 MagickTrue : MagickFalse;
1116 if (loop != MagickFalse)
1118 while (ReadBlobBlock(image,header) != 0)
1119 iterations=(size_t) ((header[2] << 8) | header[1]);
1125 name[MaxTextExtent];
1144 Store GIF application extension as a generic profile.
1146 i8bim=LocaleNCompare((char *) header,"MGK8BIM0000",11) == 0 ?
1147 MagickTrue : MagickFalse;
1148 icc=LocaleNCompare((char *) header,"ICCRGBG1012",11) == 0 ?
1149 MagickTrue : MagickFalse;
1150 iptc=LocaleNCompare((char *) header,"MGKIPTC0000",11) == 0 ?
1151 MagickTrue : MagickFalse;
1152 number_extensionss++;
1153 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1154 " Reading GIF application extension");
1155 info=(unsigned char *) AcquireQuantumMemory(255UL,
1157 reserved_length=255;
1158 for (info_length=0; ; )
1160 block_length=(int) ReadBlobBlock(image,&info[info_length]);
1161 if (block_length == 0)
1163 info_length+=block_length;
1164 if (info_length > (reserved_length-255))
1166 reserved_length+=4096;
1167 info=(unsigned char *) ResizeQuantumMemory(info,
1168 (size_t) reserved_length,sizeof(*info));
1171 info=(unsigned char *) ResizeQuantumMemory(info,(size_t)
1172 (info_length+1),sizeof(*info));
1173 profile=AcquireStringInfo((size_t) info_length);
1174 SetStringInfoDatum(profile,(const unsigned char *) info);
1175 if (i8bim == MagickTrue)
1176 (void) CopyMagickString(name,"8bim",sizeof(name));
1177 else if (icc == MagickTrue)
1178 (void) CopyMagickString(name,"icc",sizeof(name));
1179 else if (iptc == MagickTrue)
1180 (void) CopyMagickString(name,"iptc",sizeof(name));
1182 (void) FormatLocaleString(name,sizeof(name),"gif:%.11s",
1184 (void) SetImageProfile(image,name,profile);
1185 info=(unsigned char *) RelinquishMagickMemory(info);
1186 profile=DestroyStringInfo(profile);
1187 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1188 " profile name=%s",name);
1194 while (ReadBlobBlock(image,header) != 0) ;
1199 if (c != (unsigned char) ',')
1201 if (image_count != 0)
1204 Allocate next image structure.
1206 AcquireNextImage(image_info,image);
1207 if (GetNextImageInList(image) == (Image *) NULL)
1209 image=DestroyImageList(image);
1210 global_colormap=(unsigned char *) RelinquishMagickMemory(
1212 return((Image *) NULL);
1214 image=SyncNextImageInList(image);
1218 Read image attributes.
1220 image->storage_class=PseudoClass;
1221 image->compression=LZWCompression;
1222 page.x=(ssize_t) ReadBlobLSBShort(image);
1223 page.y=(ssize_t) ReadBlobLSBShort(image);
1224 image->columns=ReadBlobLSBShort(image);
1225 image->rows=ReadBlobLSBShort(image);
1227 flag=(unsigned char) ReadBlobByte(image);
1228 image->interlace=BitSet((int) flag,0x40) != 0 ? GIFInterlace :
1230 image->colors=BitSet((int) flag,0x80) == 0 ? global_colors :
1231 one << ((size_t) (flag & 0x07)+1);
1232 if (opacity >= (ssize_t) image->colors)
1234 image->page.width=page.width;
1235 image->page.height=page.height;
1236 image->page.y=page.y;
1237 image->page.x=page.x;
1239 image->ticks_per_second=100;
1240 image->dispose=(DisposeType) dispose;
1241 image->iterations=iterations;
1242 image->matte=opacity >= 0 ? MagickTrue : MagickFalse;
1246 if ((image->columns == 0) || (image->rows == 0))
1248 global_colormap=(unsigned char *) RelinquishMagickMemory(
1250 ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
1253 Inititialize colormap.
1255 if (AcquireImageColormap(image,image->colors) == MagickFalse)
1257 global_colormap=(unsigned char *) RelinquishMagickMemory(
1259 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1261 if (BitSet((int) flag,0x80) == 0)
1264 Use global colormap.
1267 for (i=0; i < (ssize_t) image->colors; i++)
1269 image->colormap[i].red=ScaleCharToQuantum(*p++);
1270 image->colormap[i].green=ScaleCharToQuantum(*p++);
1271 image->colormap[i].blue=ScaleCharToQuantum(*p++);
1274 image->colormap[i].opacity=(Quantum) TransparentOpacity;
1275 image->transparent_color=image->colormap[opacity];
1278 image->background_color=image->colormap[MagickMin(background,
1287 Read local colormap.
1289 colormap=(unsigned char *) AcquireQuantumMemory(image->colors,
1290 3*sizeof(*colormap));
1291 if (colormap == (unsigned char *) NULL)
1293 global_colormap=(unsigned char *) RelinquishMagickMemory(
1295 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1297 count=ReadBlob(image,(3*image->colors)*sizeof(*colormap),colormap);
1298 if (count != (ssize_t) (3*image->colors))
1300 global_colormap=(unsigned char *) RelinquishMagickMemory(
1302 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
1303 ThrowReaderException(CorruptImageError,
1304 "InsufficientImageDataInFile");
1307 for (i=0; i < (ssize_t) image->colors; i++)
1309 image->colormap[i].red=ScaleCharToQuantum(*p++);
1310 image->colormap[i].green=ScaleCharToQuantum(*p++);
1311 image->colormap[i].blue=ScaleCharToQuantum(*p++);
1313 image->colormap[i].opacity=(Quantum) TransparentOpacity;
1315 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
1317 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
1318 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1323 if (image_info->ping != MagickFalse)
1324 status=PingGIFImage(image);
1326 status=DecodeImage(image,opacity);
1327 if ((image_info->ping == MagickFalse) && (status == MagickFalse))
1329 global_colormap=(unsigned char *) RelinquishMagickMemory(
1331 ThrowReaderException(CorruptImageError,"CorruptImage");
1333 if (image_info->number_scenes != 0)
1334 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1337 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) image->scene-
1339 if (status == MagickFalse)
1342 global_colormap=(unsigned char *) RelinquishMagickMemory(global_colormap);
1343 if ((image->columns == 0) || (image->rows == 0))
1344 ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
1345 (void) CloseBlob(image);
1346 return(GetFirstImageInList(image));
1350 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1354 % R e g i s t e r G I F I m a g e %
1358 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1360 % RegisterGIFImage() adds properties for the GIF image format to
1361 % the list of supported formats. The properties include the image format
1362 % tag, a method to read and/or write the format, whether the format
1363 % supports the saving of more than one frame to the same file or blob,
1364 % whether the format supports native in-memory I/O, and a brief
1365 % description of the format.
1367 % The format of the RegisterGIFImage method is:
1369 % size_t RegisterGIFImage(void)
1372 ModuleExport size_t RegisterGIFImage(void)
1377 entry=SetMagickInfo("GIF");
1378 entry->decoder=(DecodeImageHandler *) ReadGIFImage;
1379 entry->encoder=(EncodeImageHandler *) WriteGIFImage;
1380 entry->magick=(IsImageFormatHandler *) IsGIF;
1381 entry->description=ConstantString("CompuServe graphics interchange format");
1382 entry->module=ConstantString("GIF");
1383 (void) RegisterMagickInfo(entry);
1384 entry=SetMagickInfo("GIF87");
1385 entry->decoder=(DecodeImageHandler *) ReadGIFImage;
1386 entry->encoder=(EncodeImageHandler *) WriteGIFImage;
1387 entry->magick=(IsImageFormatHandler *) IsGIF;
1388 entry->adjoin=MagickFalse;
1389 entry->description=ConstantString("CompuServe graphics interchange format");
1390 entry->version=ConstantString("version 87a");
1391 entry->module=ConstantString("GIF");
1392 (void) RegisterMagickInfo(entry);
1393 return(MagickImageCoderSignature);
1397 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1401 % U n r e g i s t e r G I F I m a g e %
1405 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1407 % UnregisterGIFImage() removes format registrations made by the
1408 % GIF module from the list of supported formats.
1410 % The format of the UnregisterGIFImage method is:
1412 % UnregisterGIFImage(void)
1415 ModuleExport void UnregisterGIFImage(void)
1417 (void) UnregisterMagickInfo("GIF");
1418 (void) UnregisterMagickInfo("GIF87");
1422 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1426 % W r i t e G I F I m a g e %
1430 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1432 % WriteGIFImage() writes an image to a file in the Compuserve Graphics
1435 % The format of the WriteGIFImage method is:
1437 % MagickBooleanType WriteGIFImage(const ImageInfo *image_info,Image *image)
1439 % A description of each parameter follows.
1441 % o image_info: the image info.
1443 % o image: The image.
1446 static MagickBooleanType WriteGIFImage(const ImageInfo *image_info,Image *image)
1472 register unsigned char
1490 Open output image file.
1492 assert(image_info != (const ImageInfo *) NULL);
1493 assert(image_info->signature == MagickSignature);
1494 assert(image != (Image *) NULL);
1495 assert(image->signature == MagickSignature);
1496 if (image->debug != MagickFalse)
1497 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1498 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
1499 if (status == MagickFalse)
1504 global_colormap=(unsigned char *) AcquireQuantumMemory(768UL,
1505 sizeof(*global_colormap));
1506 colormap=(unsigned char *) AcquireQuantumMemory(768UL,sizeof(*colormap));
1507 if ((global_colormap == (unsigned char *) NULL) ||
1508 (colormap == (unsigned char *) NULL))
1509 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1510 for (i=0; i < 768; i++)
1511 colormap[i]=(unsigned char) 0;
1515 write_info=CloneImageInfo(image_info);
1516 if (LocaleCompare(write_info->magick,"GIF87") != 0)
1517 (void) WriteBlob(image,6,(unsigned char *) "GIF89a");
1520 (void) WriteBlob(image,6,(unsigned char *) "GIF87a");
1521 write_info->adjoin=MagickFalse;
1524 Determine image bounding box.
1526 page.width=image->columns;
1527 page.height=image->rows;
1530 if (write_info->adjoin != MagickFalse)
1531 for (next_image=image; next_image != (Image *) NULL; )
1533 page.x=next_image->page.x;
1534 page.y=next_image->page.y;
1535 if ((next_image->page.width+page.x) > page.width)
1536 page.width=next_image->page.width+page.x;
1537 if ((next_image->page.height+page.y) > page.height)
1538 page.height=next_image->page.height+page.y;
1539 next_image=GetNextImageInList(next_image);
1541 page.x=image->page.x;
1542 page.y=image->page.y;
1543 if ((image->page.width != 0) && (image->page.height != 0))
1545 (void) WriteBlobLSBShort(image,(unsigned short) page.width);
1546 (void) WriteBlobLSBShort(image,(unsigned short) page.height);
1548 Write images to file.
1550 interlace=write_info->interlace;
1551 if ((write_info->adjoin != MagickFalse) &&
1552 (GetNextImageInList(image) != (Image *) NULL))
1553 interlace=NoInterlace;
1558 if (image->colorspace != RGBColorspace)
1559 (void) TransformImageColorspace(image,RGBColorspace);
1561 if (IsOpaqueImage(image,&image->exception) != MagickFalse)
1563 if ((image->storage_class == DirectClass) || (image->colors > 256))
1564 (void) SetImageType(image,PaletteType);
1573 Identify transparent colormap index.
1575 if ((image->storage_class == DirectClass) || (image->colors > 256))
1576 (void) SetImageType(image,PaletteBilevelMatteType);
1577 for (i=0; i < (ssize_t) image->colors; i++)
1578 if (image->colormap[i].opacity != OpaqueOpacity)
1585 alpha=(MagickRealType) TransparentOpacity-(MagickRealType)
1586 image->colormap[i].opacity;
1587 beta=(MagickRealType) TransparentOpacity-(MagickRealType)
1588 image->colormap[opacity].opacity;
1594 (void) SetImageType(image,PaletteBilevelMatteType);
1595 for (i=0; i < (ssize_t) image->colors; i++)
1596 if (image->colormap[i].opacity != OpaqueOpacity)
1603 alpha=(Quantum) TransparentOpacity-(MagickRealType)
1604 image->colormap[i].opacity;
1605 beta=(Quantum) TransparentOpacity-(MagickRealType)
1606 image->colormap[opacity].opacity;
1613 image->colormap[opacity].red=image->transparent_color.red;
1614 image->colormap[opacity].green=image->transparent_color.green;
1615 image->colormap[opacity].blue=image->transparent_color.blue;
1618 if ((image->storage_class == DirectClass) || (image->colors > 256))
1619 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1620 for (bits_per_pixel=1; bits_per_pixel < 8; bits_per_pixel++)
1621 if ((one << bits_per_pixel) >= image->colors)
1624 for (i=0; i < (ssize_t) image->colors; i++)
1626 *q++=ScaleQuantumToChar(image->colormap[i].red);
1627 *q++=ScaleQuantumToChar(image->colormap[i].green);
1628 *q++=ScaleQuantumToChar(image->colormap[i].blue);
1630 for ( ; i < (ssize_t) (one << bits_per_pixel); i++)
1632 *q++=(unsigned char) 0x0;
1633 *q++=(unsigned char) 0x0;
1634 *q++=(unsigned char) 0x0;
1636 if ((GetPreviousImageInList(image) == (Image *) NULL) ||
1637 (write_info->adjoin == MagickFalse))
1640 Write global colormap.
1643 c|=(8-1) << 4; /* color resolution */
1644 c|=(bits_per_pixel-1); /* size of global colormap */
1645 (void) WriteBlobByte(image,(unsigned char) c);
1646 for (j=0; j < (ssize_t) image->colors; j++)
1647 if (IsColorEqual(&image->background_color,image->colormap+j))
1649 (void) WriteBlobByte(image,(unsigned char)
1650 (j == (ssize_t) image->colors ? 0 : j)); /* background color */
1651 (void) WriteBlobByte(image,(unsigned char) 0x00); /* reserved */
1652 length=(size_t) (3*(one << bits_per_pixel));
1653 (void) WriteBlob(image,length,colormap);
1654 for (j=0; j < 768; j++)
1655 global_colormap[j]=colormap[j];
1657 if (LocaleCompare(write_info->magick,"GIF87") != 0)
1660 Write graphics control extension.
1662 (void) WriteBlobByte(image,(unsigned char) 0x21);
1663 (void) WriteBlobByte(image,(unsigned char) 0xf9);
1664 (void) WriteBlobByte(image,(unsigned char) 0x04);
1665 c=image->dispose << 2;
1668 (void) WriteBlobByte(image,(unsigned char) c);
1669 delay=(size_t) (100*image->delay/MagickMax((size_t)
1670 image->ticks_per_second,1));
1671 (void) WriteBlobLSBShort(image,(unsigned short) delay);
1672 (void) WriteBlobByte(image,(unsigned char) (opacity >= 0 ? opacity :
1674 (void) WriteBlobByte(image,(unsigned char) 0x00);
1675 if ((LocaleCompare(write_info->magick,"GIF87") != 0) &&
1676 (GetImageProperty(image,"comment") != (const char *) NULL))
1688 Write Comment extension.
1690 (void) WriteBlobByte(image,(unsigned char) 0x21);
1691 (void) WriteBlobByte(image,(unsigned char) 0xfe);
1692 value=GetImageProperty(image,"comment");
1694 while (strlen(p) != 0)
1696 count=MagickMin(strlen(p),255);
1697 (void) WriteBlobByte(image,(unsigned char) count);
1698 for (i=0; i < (ssize_t) count; i++)
1699 (void) WriteBlobByte(image,(unsigned char) *p++);
1701 (void) WriteBlobByte(image,(unsigned char) 0x00);
1703 if ((GetPreviousImageInList(image) == (Image *) NULL) &&
1704 (GetNextImageInList(image) != (Image *) NULL) &&
1705 (image->iterations != 1))
1708 Write Netscape Loop extension.
1710 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1711 " Writing GIF Extension %s","NETSCAPE2.0");
1712 (void) WriteBlobByte(image,(unsigned char) 0x21);
1713 (void) WriteBlobByte(image,(unsigned char) 0xff);
1714 (void) WriteBlobByte(image,(unsigned char) 0x0b);
1715 (void) WriteBlob(image,11,(unsigned char *) "NETSCAPE2.0");
1716 (void) WriteBlobByte(image,(unsigned char) 0x03);
1717 (void) WriteBlobByte(image,(unsigned char) 0x01);
1718 (void) WriteBlobLSBShort(image,(unsigned short) image->iterations);
1719 (void) WriteBlobByte(image,(unsigned char) 0x00);
1721 ResetImageProfileIterator(image);
1730 name=GetNextImageProfile(image);
1731 if (name == (const char *) NULL)
1733 profile=GetImageProfile(image,name);
1734 if (profile != (StringInfo *) NULL)
1736 if ((LocaleCompare(name,"ICC") == 0) ||
1737 (LocaleCompare(name,"ICM") == 0) ||
1738 (LocaleCompare(name,"IPTC") == 0) ||
1739 (LocaleCompare(name,"8BIM") == 0) ||
1740 (LocaleNCompare(name,"gif:",4) == 0))
1751 datum=GetStringInfoDatum(profile);
1752 length=GetStringInfoLength(profile);
1753 (void) WriteBlobByte(image,(unsigned char) 0x21);
1754 (void) WriteBlobByte(image,(unsigned char) 0xff);
1755 (void) WriteBlobByte(image,(unsigned char) 0x0b);
1756 if ((LocaleCompare(name,"ICC") == 0) ||
1757 (LocaleCompare(name,"ICM") == 0))
1760 Write ICC extension.
1762 (void) WriteBlob(image,11,(unsigned char *)"ICCRGBG1012");
1763 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1764 " Writing GIF Extension %s","ICCRGBG1012");
1767 if ((LocaleCompare(name,"IPTC") == 0))
1770 write IPTC extension.
1772 (void) WriteBlob(image,11,(unsigned char *)"MGKIPTC0000");
1773 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1774 " Writing GIF Extension %s","MGKIPTC0000");
1777 if ((LocaleCompare(name,"8BIM") == 0))
1780 Write 8BIM extension>
1782 (void) WriteBlob(image,11,(unsigned char *)
1784 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1785 " Writing GIF Extension %s","MGK8BIM0000");
1790 extension[MaxTextExtent];
1792 /* write generic extension */
1793 (void) CopyMagickString(extension,name+4,
1795 (void) WriteBlob(image,11,(unsigned char *) extension);
1796 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1797 " Writing GIF Extension %s",name);
1800 while ((ssize_t) length > offset)
1805 if ((length-offset) < 255)
1806 block_length=length-offset;
1809 (void) WriteBlobByte(image,(unsigned char) block_length);
1810 (void) WriteBlob(image,(size_t) block_length,datum+offset);
1811 offset+=(ssize_t) block_length;
1813 (void) WriteBlobByte(image,(unsigned char) 0x00);
1818 (void) WriteBlobByte(image,','); /* image separator */
1820 Write the image header.
1822 page.x=image->page.x;
1823 page.y=image->page.y;
1824 if ((image->page.width != 0) && (image->page.height != 0))
1826 (void) WriteBlobLSBShort(image,(unsigned short) (page.x < 0 ? 0 : page.x));
1827 (void) WriteBlobLSBShort(image,(unsigned short) (page.y < 0 ? 0 : page.y));
1828 (void) WriteBlobLSBShort(image,(unsigned short) image->columns);
1829 (void) WriteBlobLSBShort(image,(unsigned short) image->rows);
1831 if (interlace != NoInterlace)
1832 c|=0x40; /* pixel data is interlaced */
1833 for (j=0; j < (ssize_t) (3*image->colors); j++)
1834 if (colormap[j] != global_colormap[j])
1836 if (j == (ssize_t) (3*image->colors))
1837 (void) WriteBlobByte(image,(unsigned char) c);
1841 c|=(bits_per_pixel-1); /* size of local colormap */
1842 (void) WriteBlobByte(image,(unsigned char) c);
1843 length=(size_t) (3*(one << bits_per_pixel));
1844 (void) WriteBlob(image,length,colormap);
1847 Write the image data.
1849 c=(int) MagickMax(bits_per_pixel,2);
1850 (void) WriteBlobByte(image,(unsigned char) c);
1851 status=EncodeImage(write_info,image,(size_t) MagickMax(bits_per_pixel,2)+1);
1852 if (status == MagickFalse)
1854 global_colormap=(unsigned char *) RelinquishMagickMemory(
1856 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
1857 write_info=DestroyImageInfo(write_info);
1858 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1860 (void) WriteBlobByte(image,(unsigned char) 0x00);
1861 if (GetNextImageInList(image) == (Image *) NULL)
1863 image=SyncNextImageInList(image);
1865 status=SetImageProgress(image,SaveImagesTag,scene,
1866 GetImageListLength(image));
1867 if (status == MagickFalse)
1869 } while (write_info->adjoin != MagickFalse);
1870 (void) WriteBlobByte(image,';'); /* terminator */
1871 global_colormap=(unsigned char *) RelinquishMagickMemory(global_colormap);
1872 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
1873 write_info=DestroyImageInfo(write_info);
1874 (void) CloseBlob(image);