2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write Compuserv Graphics Interchange Format %
20 % Copyright 1999-2013 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 "MagickCore/studio.h"
43 #include "MagickCore/attribute.h"
44 #include "MagickCore/blob.h"
45 #include "MagickCore/blob-private.h"
46 #include "MagickCore/cache.h"
47 #include "MagickCore/color.h"
48 #include "MagickCore/color-private.h"
49 #include "MagickCore/colormap.h"
50 #include "MagickCore/colormap-private.h"
51 #include "MagickCore/colorspace.h"
52 #include "MagickCore/colorspace-private.h"
53 #include "MagickCore/exception.h"
54 #include "MagickCore/exception-private.h"
55 #include "MagickCore/image.h"
56 #include "MagickCore/image-private.h"
57 #include "MagickCore/list.h"
58 #include "MagickCore/profile.h"
59 #include "MagickCore/magick.h"
60 #include "MagickCore/memory_.h"
61 #include "MagickCore/monitor.h"
62 #include "MagickCore/monitor-private.h"
63 #include "MagickCore/option.h"
64 #include "MagickCore/pixel.h"
65 #include "MagickCore/pixel-accessor.h"
66 #include "MagickCore/property.h"
67 #include "MagickCore/quantize.h"
68 #include "MagickCore/quantum-private.h"
69 #include "MagickCore/static.h"
70 #include "MagickCore/string_.h"
71 #include "MagickCore/string-private.h"
72 #include "MagickCore/module.h"
77 #define MaximumLZWBits 12
78 #define MaximumLZWCode (1UL << MaximumLZWBits)
83 typedef struct _LZWCodeInfo
96 typedef struct _LZWStack
104 typedef struct _LZWInfo
132 Forward declarations.
135 GetNextLZWCode(LZWInfo *,const size_t);
137 static MagickBooleanType
138 WriteGIFImage(const ImageInfo *,Image *,ExceptionInfo *);
141 ReadBlobBlock(Image *,unsigned char *);
144 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
148 % D e c o d e I m a g e %
152 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
154 % DecodeImage uncompresses an image via GIF-coding.
156 % The format of the DecodeImage method is:
158 % MagickBooleanType DecodeImage(Image *image,const ssize_t opacity)
160 % A description of each parameter follows:
162 % o image: the address of a structure of type Image.
164 % o opacity: The colormap index associated with the transparent color.
168 static LZWInfo *RelinquishLZWInfo(LZWInfo *lzw_info)
170 if (lzw_info->table[0] != (size_t *) NULL)
171 lzw_info->table[0]=(size_t *) RelinquishMagickMemory(
173 if (lzw_info->table[1] != (size_t *) NULL)
174 lzw_info->table[1]=(size_t *) RelinquishMagickMemory(
176 if (lzw_info->stack != (LZWStack *) NULL)
178 if (lzw_info->stack->codes != (size_t *) NULL)
179 lzw_info->stack->codes=(size_t *) RelinquishMagickMemory(
180 lzw_info->stack->codes);
181 lzw_info->stack=(LZWStack *) RelinquishMagickMemory(lzw_info->stack);
183 lzw_info=(LZWInfo *) RelinquishMagickMemory(lzw_info);
184 return((LZWInfo *) NULL);
187 static inline void ResetLZWInfo(LZWInfo *lzw_info)
192 lzw_info->bits=lzw_info->data_size+1;
194 lzw_info->maximum_code=one << lzw_info->bits;
195 lzw_info->slot=lzw_info->maximum_data_value+3;
196 lzw_info->genesis=MagickTrue;
199 static LZWInfo *AcquireLZWInfo(Image *image,const size_t data_size)
210 lzw_info=(LZWInfo *) AcquireMagickMemory(sizeof(*lzw_info));
211 if (lzw_info == (LZWInfo *) NULL)
212 return((LZWInfo *) NULL);
213 (void) ResetMagickMemory(lzw_info,0,sizeof(*lzw_info));
214 lzw_info->image=image;
215 lzw_info->data_size=data_size;
217 lzw_info->maximum_data_value=(one << data_size)-1;
218 lzw_info->clear_code=lzw_info->maximum_data_value+1;
219 lzw_info->end_code=lzw_info->maximum_data_value+2;
220 lzw_info->table[0]=(size_t *) AcquireQuantumMemory(MaximumLZWCode,
221 sizeof(*lzw_info->table));
222 lzw_info->table[1]=(size_t *) AcquireQuantumMemory(MaximumLZWCode,
223 sizeof(*lzw_info->table));
224 if ((lzw_info->table[0] == (size_t *) NULL) ||
225 (lzw_info->table[1] == (size_t *) NULL))
227 lzw_info=RelinquishLZWInfo(lzw_info);
228 return((LZWInfo *) NULL);
230 for (i=0; i <= (ssize_t) lzw_info->maximum_data_value; i++)
232 lzw_info->table[0][i]=0;
233 lzw_info->table[1][i]=(size_t) i;
235 ResetLZWInfo(lzw_info);
236 lzw_info->code_info.buffer[0]='\0';
237 lzw_info->code_info.buffer[1]='\0';
238 lzw_info->code_info.count=2;
239 lzw_info->code_info.bit=8*lzw_info->code_info.count;
240 lzw_info->code_info.eof=MagickFalse;
241 lzw_info->genesis=MagickTrue;
242 lzw_info->stack=(LZWStack *) AcquireMagickMemory(sizeof(*lzw_info->stack));
243 if (lzw_info->stack == (LZWStack *) NULL)
245 lzw_info=RelinquishLZWInfo(lzw_info);
246 return((LZWInfo *) NULL);
248 lzw_info->stack->codes=(size_t *) AcquireQuantumMemory(2UL*
249 MaximumLZWCode,sizeof(*lzw_info->stack->codes));
250 if (lzw_info->stack->codes == (size_t *) NULL)
252 lzw_info=RelinquishLZWInfo(lzw_info);
253 return((LZWInfo *) NULL);
255 lzw_info->stack->index=lzw_info->stack->codes;
256 lzw_info->stack->top=lzw_info->stack->codes+2*MaximumLZWCode;
260 static inline int GetNextLZWCode(LZWInfo *lzw_info,const size_t bits)
271 while (((lzw_info->code_info.bit+bits) > (8*lzw_info->code_info.count)) &&
272 (lzw_info->code_info.eof == MagickFalse))
277 lzw_info->code_info.buffer[0]=lzw_info->code_info.buffer[
278 lzw_info->code_info.count-2];
279 lzw_info->code_info.buffer[1]=lzw_info->code_info.buffer[
280 lzw_info->code_info.count-1];
281 lzw_info->code_info.bit-=8*(lzw_info->code_info.count-2);
282 lzw_info->code_info.count=2;
283 count=ReadBlobBlock(lzw_info->image,&lzw_info->code_info.buffer[
284 lzw_info->code_info.count]);
286 lzw_info->code_info.count+=count;
288 lzw_info->code_info.eof=MagickTrue;
290 if ((lzw_info->code_info.bit+bits) > (8*lzw_info->code_info.count))
294 for (i=0; i < (ssize_t) bits; i++)
296 code|=((lzw_info->code_info.buffer[lzw_info->code_info.bit/8] &
297 (one << (lzw_info->code_info.bit % 8))) != 0) << i;
298 lzw_info->code_info.bit++;
303 static inline int PopLZWStack(LZWStack *stack_info)
305 if (stack_info->index <= stack_info->codes)
308 return((int) *stack_info->index);
311 static inline void PushLZWStack(LZWStack *stack_info,const size_t value)
313 if (stack_info->index >= stack_info->top)
315 *stack_info->index=value;
319 static int ReadBlobLZWByte(LZWInfo *lzw_info)
331 if (lzw_info->stack->index != lzw_info->stack->codes)
332 return(PopLZWStack(lzw_info->stack));
333 if (lzw_info->genesis != MagickFalse)
335 lzw_info->genesis=MagickFalse;
338 lzw_info->first_code=(size_t) GetNextLZWCode(lzw_info,lzw_info->bits);
339 lzw_info->last_code=lzw_info->first_code;
340 } while (lzw_info->first_code == lzw_info->clear_code);
341 return((int) lzw_info->first_code);
343 code=GetNextLZWCode(lzw_info,lzw_info->bits);
346 if ((size_t) code == lzw_info->clear_code)
348 ResetLZWInfo(lzw_info);
349 return(ReadBlobLZWByte(lzw_info));
351 if ((size_t) code == lzw_info->end_code)
353 if ((size_t) code < lzw_info->slot)
357 PushLZWStack(lzw_info->stack,lzw_info->first_code);
358 value=lzw_info->last_code;
361 while (value > lzw_info->maximum_data_value)
363 if ((size_t) count > MaximumLZWCode)
366 if ((size_t) value > MaximumLZWCode)
368 PushLZWStack(lzw_info->stack,lzw_info->table[1][value]);
369 value=lzw_info->table[0][value];
371 lzw_info->first_code=lzw_info->table[1][value];
372 PushLZWStack(lzw_info->stack,lzw_info->first_code);
374 if (lzw_info->slot < MaximumLZWCode)
376 lzw_info->table[0][lzw_info->slot]=lzw_info->last_code;
377 lzw_info->table[1][lzw_info->slot]=lzw_info->first_code;
379 if ((lzw_info->slot >= lzw_info->maximum_code) &&
380 (lzw_info->bits < MaximumLZWBits))
383 lzw_info->maximum_code=one << lzw_info->bits;
386 lzw_info->last_code=(size_t) code;
387 return(PopLZWStack(lzw_info->stack));
390 static MagickBooleanType DecodeImage(Image *image,const ssize_t opacity,
391 ExceptionInfo *exception)
413 Allocate decoder tables.
415 assert(image != (Image *) NULL);
416 assert(image->signature == MagickSignature);
417 if (image->debug != MagickFalse)
418 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
419 data_size=(unsigned char) ReadBlobByte(image);
420 if (data_size > MaximumLZWBits)
421 ThrowBinaryException(CorruptImageError,"CorruptImage",image->filename);
422 lzw_info=AcquireLZWInfo(image,data_size);
423 if (lzw_info == (LZWInfo *) NULL)
424 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
428 for (y=0; y < (ssize_t) image->rows; y++)
436 q=QueueAuthenticPixels(image,0,offset,image->columns,1,exception);
437 if (q == (Quantum *) NULL)
439 for (x=0; x < (ssize_t) image->columns; )
441 c=ReadBlobLZWByte(lzw_info);
444 index=ConstrainColormapIndex(image,(size_t) c,exception);
445 SetPixelIndex(image,index,q);
446 SetPixelInfoPixel(image,image->colormap+(ssize_t) index,q);
447 SetPixelAlpha(image,(ssize_t) index == opacity ? TransparentAlpha :
450 q+=GetPixelChannels(image);
452 if (SyncAuthenticPixels(image,exception) == MagickFalse)
454 if (x < (ssize_t) image->columns)
456 if (image->interlace == NoInterlace)
484 if ((pass == 0) && (offset >= (ssize_t) image->rows))
489 if ((pass == 1) && (offset >= (ssize_t) image->rows))
494 if ((pass == 2) && (offset >= (ssize_t) image->rows))
501 lzw_info=RelinquishLZWInfo(lzw_info);
502 if (y < (ssize_t) image->rows)
503 ThrowBinaryException(CorruptImageError,"CorruptImage",image->filename);
508 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
512 % E n c o d e I m a g e %
516 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
518 % EncodeImage compresses an image via GIF-coding.
520 % The format of the EncodeImage method is:
522 % MagickBooleanType EncodeImage(const ImageInfo *image_info,Image *image,
523 % const size_t data_size)
525 % A description of each parameter follows:
527 % o image_info: the image info.
529 % o image: the address of a structure of type Image.
531 % o data_size: The number of bits in the compressed packet.
534 static MagickBooleanType EncodeImage(const ImageInfo *image_info,Image *image,
535 const size_t data_size,ExceptionInfo *exception)
537 #define MaxCode(number_bits) ((one << (number_bits))-1)
538 #define MaxHashTable 5003
539 #define MaxGIFBits 12UL
540 #define MaxGIFTable (1UL << MaxGIFBits)
541 #define GIFOutputCode(code) \
547 datum|=(code) << bits; \
554 Add a character to current packet. \
556 packet[length++]=(unsigned char) (datum & 0xff); \
559 (void) WriteBlobByte(image,(unsigned char) length); \
560 (void) WriteBlob(image,length,packet); \
566 if (free_code > max_code) \
569 if (number_bits == MaxGIFBits) \
570 max_code=MaxGIFTable; \
572 max_code=MaxCode(number_bits); \
591 end_of_information_code,
611 Allocate encoder tables.
613 assert(image != (Image *) NULL);
615 packet=(unsigned char *) AcquireQuantumMemory(256,sizeof(*packet));
616 hash_code=(short *) AcquireQuantumMemory(MaxHashTable,sizeof(*hash_code));
617 hash_prefix=(short *) AcquireQuantumMemory(MaxHashTable,sizeof(*hash_prefix));
618 hash_suffix=(unsigned char *) AcquireQuantumMemory(MaxHashTable,
619 sizeof(*hash_suffix));
620 if ((packet == (unsigned char *) NULL) || (hash_code == (short *) NULL) ||
621 (hash_prefix == (short *) NULL) ||
622 (hash_suffix == (unsigned char *) NULL))
624 if (packet != (unsigned char *) NULL)
625 packet=(unsigned char *) RelinquishMagickMemory(packet);
626 if (hash_code != (short *) NULL)
627 hash_code=(short *) RelinquishMagickMemory(hash_code);
628 if (hash_prefix != (short *) NULL)
629 hash_prefix=(short *) RelinquishMagickMemory(hash_prefix);
630 if (hash_suffix != (unsigned char *) NULL)
631 hash_suffix=(unsigned char *) RelinquishMagickMemory(hash_suffix);
635 Initialize GIF encoder.
637 number_bits=data_size;
638 max_code=MaxCode(number_bits);
639 clear_code=((short) one << (data_size-1));
640 end_of_information_code=clear_code+1;
641 free_code=clear_code+2;
645 for (i=0; i < MaxHashTable; i++)
647 GIFOutputCode(clear_code);
654 for (y=0; y < (ssize_t) image->rows; y++)
656 register const Quantum
662 p=GetVirtualPixels(image,0,offset,image->columns,1,exception);
663 if (p == (const Quantum *) NULL)
667 waiting_code=(short) GetPixelIndex(image,p);
668 p+=GetPixelChannels(image);
670 for (x=(ssize_t) (y == 0 ? 1 : 0); x < (ssize_t) image->columns; x++)
675 index=(Quantum) ((size_t) GetPixelIndex(image,p) & 0xff);
676 p+=GetPixelChannels(image);
677 k=(ssize_t) (((size_t) index << (MaxGIFBits-8))+waiting_code);
678 if (k >= MaxHashTable)
680 next_pixel=MagickFalse;
682 if (hash_code[k] > 0)
684 if ((hash_prefix[k] == waiting_code) &&
685 (hash_suffix[k] == (unsigned char) index))
687 waiting_code=hash_code[k];
691 displacement=MaxHashTable-k;
697 if (hash_code[k] == 0)
699 if ((hash_prefix[k] == waiting_code) &&
700 (hash_suffix[k] == (unsigned char) index))
702 waiting_code=hash_code[k];
703 next_pixel=MagickTrue;
707 if (next_pixel == MagickTrue)
710 GIFOutputCode((size_t) waiting_code);
711 if (free_code < MaxGIFTable)
713 hash_code[k]=(short) free_code++;
714 hash_prefix[k]=waiting_code;
715 hash_suffix[k]=(unsigned char) index;
720 Fill the hash table with empty entries.
722 for (k=0; k < MaxHashTable; k++)
725 Reset compressor and issue a clear code.
727 free_code=clear_code+2;
728 GIFOutputCode(clear_code);
729 number_bits=data_size;
730 max_code=MaxCode(number_bits);
732 waiting_code=(short) index;
734 if (image_info->interlace == NoInterlace)
743 if (offset >= (ssize_t) image->rows)
753 if (offset >= (ssize_t) image->rows)
763 if (offset >= (ssize_t) image->rows)
778 Flush out the buffered code.
780 GIFOutputCode((size_t) waiting_code);
781 GIFOutputCode(end_of_information_code);
785 Add a character to current packet.
787 packet[length++]=(unsigned char) (datum & 0xff);
790 (void) WriteBlobByte(image,(unsigned char) length);
791 (void) WriteBlob(image,length,packet);
796 Flush accumulated data.
800 (void) WriteBlobByte(image,(unsigned char) length);
801 (void) WriteBlob(image,length,packet);
806 hash_suffix=(unsigned char *) RelinquishMagickMemory(hash_suffix);
807 hash_prefix=(short *) RelinquishMagickMemory(hash_prefix);
808 hash_code=(short *) RelinquishMagickMemory(hash_code);
809 packet=(unsigned char *) RelinquishMagickMemory(packet);
814 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
822 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
824 % IsGIF() returns MagickTrue if the image format type, identified by the
825 % magick string, is GIF.
827 % The format of the IsGIF method is:
829 % MagickBooleanType IsGIF(const unsigned char *magick,const size_t length)
831 % A description of each parameter follows:
833 % o magick: compare image format pattern against these bytes.
835 % o length: Specifies the length of the magick string.
838 static MagickBooleanType IsGIF(const unsigned char *magick,const size_t length)
842 if (LocaleNCompare((char *) magick,"GIF8",4) == 0)
848 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
852 + R e a d B l o b B l o c k %
856 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
858 % ReadBlobBlock() reads data from the image file and returns it. The
859 % amount of data is determined by first reading a count byte. The number
860 % of bytes read is returned.
862 % The format of the ReadBlobBlock method is:
864 % size_t ReadBlobBlock(Image *image,unsigned char *data)
866 % A description of each parameter follows:
868 % o image: the image.
870 % o data: Specifies an area to place the information requested from
874 static ssize_t ReadBlobBlock(Image *image,unsigned char *data)
882 assert(image != (Image *) NULL);
883 assert(image->signature == MagickSignature);
884 assert(data != (unsigned char *) NULL);
885 count=ReadBlob(image,1,&block_count);
888 count=ReadBlob(image,(size_t) block_count,data);
889 if (count != (ssize_t) block_count)
895 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
899 % R e a d G I F I m a g e %
903 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
905 % ReadGIFImage() reads a Compuserve Graphics image file and returns it.
906 % It allocates the memory necessary for the new Image structure and returns a
907 % pointer to the new image.
909 % The format of the ReadGIFImage method is:
911 % Image *ReadGIFImage(const ImageInfo *image_info,ExceptionInfo *exception)
913 % A description of each parameter follows:
915 % o image_info: the image info.
917 % o exception: return any errors or warnings in this structure.
921 static inline size_t MagickMax(const size_t x,const size_t y)
928 static inline size_t MagickMin(const size_t x,const size_t y)
935 static MagickBooleanType PingGIFImage(Image *image,ExceptionInfo *exception)
942 assert(image != (Image *) NULL);
943 assert(image->signature == MagickSignature);
944 if (image->debug != MagickFalse)
945 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
946 if (ReadBlob(image,1,&data_size) != 1)
947 ThrowBinaryException(CorruptImageError,"CorruptImage",image->filename);
948 if (data_size > MaximumLZWBits)
949 ThrowBinaryException(CorruptImageError,"CorruptImage",image->filename);
950 if (ReadBlob(image,1,&length) != 1)
951 ThrowBinaryException(CorruptImageError,"CorruptImage",image->filename);
954 if (ReadBlob(image,length,buffer) != (ssize_t) length)
955 ThrowBinaryException(CorruptImageError,"CorruptImage",image->filename);
956 if (ReadBlob(image,1,&length) != 1)
957 ThrowBinaryException(CorruptImageError,"CorruptImage",image->filename);
962 static Image *ReadGIFImage(const ImageInfo *image_info,ExceptionInfo *exception)
964 #define BitSet(byte,bit) (((byte) & (bit)) == (bit))
965 #define LSBFirstOrder(x,y) (((y) << 8) | (x))
972 number_extensionss=0;
983 register unsigned char
1003 header[MaxTextExtent],
1009 assert(image_info != (const ImageInfo *) NULL);
1010 assert(image_info->signature == MagickSignature);
1011 if (image_info->debug != MagickFalse)
1012 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
1013 image_info->filename);
1014 assert(exception != (ExceptionInfo *) NULL);
1015 assert(exception->signature == MagickSignature);
1016 image=AcquireImage(image_info,exception);
1017 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
1018 if (status == MagickFalse)
1020 image=DestroyImageList(image);
1021 return((Image *) NULL);
1024 Determine if this a GIF file.
1026 count=ReadBlob(image,6,magick);
1027 if ((count != 6) || ((LocaleNCompare((char *) magick,"GIF87",5) != 0) &&
1028 (LocaleNCompare((char *) magick,"GIF89",5) != 0)))
1029 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1030 page.width=ReadBlobLSBShort(image);
1031 page.height=ReadBlobLSBShort(image);
1032 flag=(unsigned char) ReadBlobByte(image);
1033 background=(unsigned char) ReadBlobByte(image);
1034 c=(unsigned char) ReadBlobByte(image); /* reserved */
1036 global_colors=one << (((size_t) flag & 0x07)+1);
1037 global_colormap=(unsigned char *) AcquireQuantumMemory((size_t)
1038 MagickMax(global_colors,256),3UL*sizeof(*global_colormap));
1039 if (global_colormap == (unsigned char *) NULL)
1040 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1041 if (BitSet((int) flag,0x80) != 0)
1042 count=ReadBlob(image,(size_t) (3*global_colors),global_colormap);
1048 meta_image=AcquireImage(image_info,exception); /* metadata container */
1051 count=ReadBlob(image,1,&c);
1054 if (c == (unsigned char) ';')
1055 break; /* terminator */
1056 if (c == (unsigned char) '!')
1059 GIF Extension block.
1061 count=ReadBlob(image,1,&c);
1064 global_colormap=(unsigned char *) RelinquishMagickMemory(
1066 ThrowReaderException(CorruptImageError,
1067 "UnableToReadExtensionBlock");
1074 Read graphics control extension.
1076 while (ReadBlobBlock(image,header) != 0) ;
1077 dispose=(size_t) (header[0] >> 2);
1078 delay=(size_t) ((header[2] << 8) | header[1]);
1079 if ((ssize_t) (header[0] & 0x01) == 0x01)
1080 opacity=(ssize_t) header[3];
1092 Read comment extension.
1094 comments=AcquireString((char *) NULL);
1095 for (length=0; ; length+=count)
1097 count=(ssize_t) ReadBlobBlock(image,header);
1101 (void) ConcatenateString(&comments,(const char *) header);
1103 (void) SetImageProperty(meta_image,"comment",comments,exception);
1104 comments=DestroyString(comments);
1113 Read Netscape Loop extension.
1116 if (ReadBlobBlock(image,header) != 0)
1117 loop=LocaleNCompare((char *) header,"NETSCAPE2.0",11) == 0 ?
1118 MagickTrue : MagickFalse;
1119 if (loop != MagickFalse)
1121 while (ReadBlobBlock(image,header) != 0)
1122 iterations=(size_t) ((header[2] << 8) | header[1]);
1128 name[MaxTextExtent];
1148 Store GIF application extension as a generic profile.
1150 icc=LocaleNCompare((char *) header,"ICCRGBG1012",11) == 0 ?
1151 MagickTrue : MagickFalse;
1152 magick=LocaleNCompare((char *) header,"ImageMagick",11) == 0 ?
1153 MagickTrue : MagickFalse;
1154 i8bim=LocaleNCompare((char *) header,"MGK8BIM0000",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 if (info == (unsigned char *) NULL)
1164 ThrowReaderException(ResourceLimitError,
1165 "MemoryAllocationFailed");
1166 reserved_length=255;
1167 for (info_length=0; ; )
1169 block_length=(int) ReadBlobBlock(image,&info[info_length]);
1170 if (block_length == 0)
1172 info_length+=block_length;
1173 if (info_length > (reserved_length-255))
1175 reserved_length+=4096;
1176 info=(unsigned char *) ResizeQuantumMemory(info,
1177 (size_t) reserved_length,sizeof(*info));
1178 if (info == (unsigned char *) NULL)
1179 ThrowReaderException(ResourceLimitError,
1180 "MemoryAllocationFailed");
1183 profile=BlobToStringInfo(info,(size_t) info_length);
1184 if (profile == (StringInfo *) NULL)
1185 ThrowReaderException(ResourceLimitError,
1186 "MemoryAllocationFailed");
1187 if (i8bim == MagickTrue)
1188 (void) CopyMagickString(name,"8bim",sizeof(name));
1189 else if (icc == MagickTrue)
1190 (void) CopyMagickString(name,"icc",sizeof(name));
1191 else if (iptc == MagickTrue)
1192 (void) CopyMagickString(name,"iptc",sizeof(name));
1193 else if (magick == MagickTrue)
1195 (void) CopyMagickString(name,"magick",sizeof(name));
1196 image->gamma=StringToDouble((char *) info+6,(char **) NULL);
1199 (void) FormatLocaleString(name,sizeof(name),"gif:%.11s",
1201 info=(unsigned char *) RelinquishMagickMemory(info);
1202 if (magick == MagickFalse)
1203 (void) SetImageProfile(meta_image,name,profile,exception);
1204 profile=DestroyStringInfo(profile);
1205 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1206 " profile name=%s",name);
1212 while (ReadBlobBlock(image,header) != 0) ;
1217 if (c != (unsigned char) ',')
1219 if (image_count != 0)
1222 Allocate next image structure.
1224 AcquireNextImage(image_info,image,exception);
1225 if (GetNextImageInList(image) == (Image *) NULL)
1227 image=DestroyImageList(image);
1228 global_colormap=(unsigned char *) RelinquishMagickMemory(
1230 return((Image *) NULL);
1232 image=SyncNextImageInList(image);
1236 Read image attributes.
1238 CloneImageProperties(image,meta_image);
1239 DestroyImageProperties(meta_image);
1240 CloneImageProfiles(image,meta_image);
1241 DestroyImageProfiles(meta_image);
1242 image->storage_class=PseudoClass;
1243 image->compression=LZWCompression;
1244 page.x=(ssize_t) ReadBlobLSBShort(image);
1245 page.y=(ssize_t) ReadBlobLSBShort(image);
1246 image->columns=ReadBlobLSBShort(image);
1247 image->rows=ReadBlobLSBShort(image);
1249 flag=(unsigned char) ReadBlobByte(image);
1250 image->interlace=BitSet((int) flag,0x40) != 0 ? GIFInterlace : NoInterlace;
1251 image->colors=BitSet((int) flag,0x80) == 0 ? global_colors : one <<
1252 ((size_t) (flag & 0x07)+1);
1253 if (opacity >= (ssize_t) image->colors)
1255 image->page.width=page.width;
1256 image->page.height=page.height;
1257 image->page.y=page.y;
1258 image->page.x=page.x;
1260 image->ticks_per_second=100;
1261 image->dispose=(DisposeType) dispose;
1262 image->iterations=iterations;
1263 image->alpha_trait=opacity >= 0 ? BlendPixelTrait : UndefinedPixelTrait;
1267 if ((image->columns == 0) || (image->rows == 0))
1269 global_colormap=(unsigned char *) RelinquishMagickMemory(
1271 ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
1274 Inititialize colormap.
1276 if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
1278 global_colormap=(unsigned char *) RelinquishMagickMemory(
1280 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1282 if (BitSet((int) flag,0x80) == 0)
1285 Use global colormap.
1288 for (i=0; i < (ssize_t) image->colors; i++)
1290 image->colormap[i].red=(double) ScaleCharToQuantum(*p++);
1291 image->colormap[i].green=(double) ScaleCharToQuantum(*p++);
1292 image->colormap[i].blue=(double) ScaleCharToQuantum(*p++);
1295 image->colormap[i].alpha=(double) TransparentAlpha;
1296 image->transparent_color=image->colormap[opacity];
1299 image->background_color=image->colormap[MagickMin(background,
1308 Read local colormap.
1310 colormap=(unsigned char *) AcquireQuantumMemory(image->colors,3*
1312 if (colormap == (unsigned char *) NULL)
1314 global_colormap=(unsigned char *) RelinquishMagickMemory(
1316 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1318 count=ReadBlob(image,(3*image->colors)*sizeof(*colormap),colormap);
1319 if (count != (ssize_t) (3*image->colors))
1321 global_colormap=(unsigned char *) RelinquishMagickMemory(
1323 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
1324 ThrowReaderException(CorruptImageError,
1325 "InsufficientImageDataInFile");
1328 for (i=0; i < (ssize_t) image->colors; i++)
1330 image->colormap[i].red=(double) ScaleCharToQuantum(*p++);
1331 image->colormap[i].green=(double) ScaleCharToQuantum(*p++);
1332 image->colormap[i].blue=(double) ScaleCharToQuantum(*p++);
1334 image->colormap[i].alpha=(double) TransparentAlpha;
1336 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
1338 if (image->gamma == 1.0)
1340 for (i=0; i < (ssize_t) image->colors; i++)
1341 if (IsPixelInfoGray(image->colormap+i) == MagickFalse)
1343 (void) SetImageColorspace(image,i == (ssize_t) image->colors ?
1344 GRAYColorspace : RGBColorspace,exception);
1346 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
1347 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1352 if (image_info->ping != MagickFalse)
1353 status=PingGIFImage(image,exception);
1355 status=DecodeImage(image,opacity,exception);
1356 if ((image_info->ping == MagickFalse) && (status == MagickFalse))
1358 global_colormap=(unsigned char *) RelinquishMagickMemory(
1360 ThrowReaderException(CorruptImageError,"CorruptImage");
1362 if (image_info->number_scenes != 0)
1363 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1366 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) image->scene-
1368 if (status == MagickFalse)
1371 meta_image=DestroyImage(meta_image);
1372 global_colormap=(unsigned char *) RelinquishMagickMemory(global_colormap);
1373 if ((image->columns == 0) || (image->rows == 0))
1374 ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
1375 (void) CloseBlob(image);
1376 return(GetFirstImageInList(image));
1380 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1384 % R e g i s t e r G I F I m a g e %
1388 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1390 % RegisterGIFImage() adds properties for the GIF image format to
1391 % the list of supported formats. The properties include the image format
1392 % tag, a method to read and/or write the format, whether the format
1393 % supports the saving of more than one frame to the same file or blob,
1394 % whether the format supports native in-memory I/O, and a brief
1395 % description of the format.
1397 % The format of the RegisterGIFImage method is:
1399 % size_t RegisterGIFImage(void)
1402 ModuleExport size_t RegisterGIFImage(void)
1407 entry=SetMagickInfo("GIF");
1408 entry->decoder=(DecodeImageHandler *) ReadGIFImage;
1409 entry->encoder=(EncodeImageHandler *) WriteGIFImage;
1410 entry->magick=(IsImageFormatHandler *) IsGIF;
1411 entry->description=ConstantString("CompuServe graphics interchange format");
1412 entry->mime_type=ConstantString("image/gif");
1413 entry->module=ConstantString("GIF");
1414 (void) RegisterMagickInfo(entry);
1415 entry=SetMagickInfo("GIF87");
1416 entry->decoder=(DecodeImageHandler *) ReadGIFImage;
1417 entry->encoder=(EncodeImageHandler *) WriteGIFImage;
1418 entry->magick=(IsImageFormatHandler *) IsGIF;
1419 entry->adjoin=MagickFalse;
1420 entry->description=ConstantString("CompuServe graphics interchange format");
1421 entry->version=ConstantString("version 87a");
1422 entry->mime_type=ConstantString("image/gif");
1423 entry->module=ConstantString("GIF");
1424 (void) RegisterMagickInfo(entry);
1425 return(MagickImageCoderSignature);
1429 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1433 % U n r e g i s t e r G I F I m a g e %
1437 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1439 % UnregisterGIFImage() removes format registrations made by the
1440 % GIF module from the list of supported formats.
1442 % The format of the UnregisterGIFImage method is:
1444 % UnregisterGIFImage(void)
1447 ModuleExport void UnregisterGIFImage(void)
1449 (void) UnregisterMagickInfo("GIF");
1450 (void) UnregisterMagickInfo("GIF87");
1454 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1458 % W r i t e G I F I m a g e %
1462 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1464 % WriteGIFImage() writes an image to a file in the Compuserve Graphics
1467 % The format of the WriteGIFImage method is:
1469 % MagickBooleanType WriteGIFImage(const ImageInfo *image_info,
1470 % Image *image,ExceptionInfo *exception)
1472 % A description of each parameter follows.
1474 % o image_info: the image info.
1476 % o image: The image.
1478 % o exception: return any errors or warnings in this structure.
1481 static MagickBooleanType WriteGIFImage(const ImageInfo *image_info,Image *image,
1482 ExceptionInfo *exception)
1505 register unsigned char
1523 Open output image file.
1525 assert(image_info != (const ImageInfo *) NULL);
1526 assert(image_info->signature == MagickSignature);
1527 assert(image != (Image *) NULL);
1528 assert(image->signature == MagickSignature);
1529 if (image->debug != MagickFalse)
1530 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1531 assert(exception != (ExceptionInfo *) NULL);
1532 assert(exception->signature == MagickSignature);
1533 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
1534 if (status == MagickFalse)
1539 global_colormap=(unsigned char *) AcquireQuantumMemory(768UL,
1540 sizeof(*global_colormap));
1541 colormap=(unsigned char *) AcquireQuantumMemory(768UL,sizeof(*colormap));
1542 if ((global_colormap == (unsigned char *) NULL) ||
1543 (colormap == (unsigned char *) NULL))
1544 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1545 for (i=0; i < 768; i++)
1546 colormap[i]=(unsigned char) 0;
1550 write_info=CloneImageInfo(image_info);
1551 if (LocaleCompare(write_info->magick,"GIF87") != 0)
1552 (void) WriteBlob(image,6,(unsigned char *) "GIF89a");
1555 (void) WriteBlob(image,6,(unsigned char *) "GIF87a");
1556 write_info->adjoin=MagickFalse;
1559 Determine image bounding box.
1561 page.width=image->columns;
1562 if (image->page.width > page.width)
1563 page.width=image->page.width;
1564 page.height=image->rows;
1565 if (image->page.height > page.height)
1566 page.height=image->page.height;
1567 page.x=image->page.x;
1568 page.y=image->page.y;
1569 (void) WriteBlobLSBShort(image,(unsigned short) page.width);
1570 (void) WriteBlobLSBShort(image,(unsigned short) page.height);
1572 Write images to file.
1574 interlace=write_info->interlace;
1575 if ((write_info->adjoin != MagickFalse) &&
1576 (GetNextImageInList(image) != (Image *) NULL))
1577 interlace=NoInterlace;
1582 if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)
1583 (void) TransformImageColorspace(image,sRGBColorspace,exception);
1585 if (IsImageOpaque(image,exception) != MagickFalse)
1587 if ((image->storage_class == DirectClass) || (image->colors > 256))
1588 (void) SetImageType(image,PaletteType,exception);
1597 Identify transparent colormap index.
1599 if ((image->storage_class == DirectClass) || (image->colors > 256))
1600 (void) SetImageType(image,PaletteBilevelMatteType,exception);
1601 for (i=0; i < (ssize_t) image->colors; i++)
1602 if (image->colormap[i].alpha != OpaqueAlpha)
1609 alpha=(double) TransparentAlpha-(double)
1610 image->colormap[i].alpha;
1611 beta=(double) TransparentAlpha-(double)
1612 image->colormap[opacity].alpha;
1618 (void) SetImageType(image,PaletteBilevelMatteType,exception);
1619 for (i=0; i < (ssize_t) image->colors; i++)
1620 if (image->colormap[i].alpha != OpaqueAlpha)
1627 alpha=(Quantum) TransparentAlpha-(double)
1628 image->colormap[i].alpha;
1629 beta=(Quantum) TransparentAlpha-(double)
1630 image->colormap[opacity].alpha;
1637 image->colormap[opacity].red=image->transparent_color.red;
1638 image->colormap[opacity].green=image->transparent_color.green;
1639 image->colormap[opacity].blue=image->transparent_color.blue;
1642 if ((image->storage_class == DirectClass) || (image->colors > 256))
1643 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1644 for (bits_per_pixel=1; bits_per_pixel < 8; bits_per_pixel++)
1645 if ((one << bits_per_pixel) >= image->colors)
1648 for (i=0; i < (ssize_t) image->colors; i++)
1650 *q++=ScaleQuantumToChar(ClampToQuantum(image->colormap[i].red));
1651 *q++=ScaleQuantumToChar(ClampToQuantum(image->colormap[i].green));
1652 *q++=ScaleQuantumToChar(ClampToQuantum(image->colormap[i].blue));
1654 for ( ; i < (ssize_t) (one << bits_per_pixel); i++)
1656 *q++=(unsigned char) 0x0;
1657 *q++=(unsigned char) 0x0;
1658 *q++=(unsigned char) 0x0;
1660 if ((GetPreviousImageInList(image) == (Image *) NULL) ||
1661 (write_info->adjoin == MagickFalse))
1664 Write global colormap.
1667 c|=(8-1) << 4; /* color resolution */
1668 c|=(bits_per_pixel-1); /* size of global colormap */
1669 (void) WriteBlobByte(image,(unsigned char) c);
1670 for (j=0; j < (ssize_t) image->colors; j++)
1671 if (IsPixelInfoEquivalent(&image->background_color,image->colormap+j))
1673 (void) WriteBlobByte(image,(unsigned char)
1674 (j == (ssize_t) image->colors ? 0 : j)); /* background color */
1675 (void) WriteBlobByte(image,(unsigned char) 0x00); /* reserved */
1676 length=(size_t) (3*(one << bits_per_pixel));
1677 (void) WriteBlob(image,length,colormap);
1678 for (j=0; j < 768; j++)
1679 global_colormap[j]=colormap[j];
1681 if (LocaleCompare(write_info->magick,"GIF87") != 0)
1684 Write graphics control extension.
1686 (void) WriteBlobByte(image,(unsigned char) 0x21);
1687 (void) WriteBlobByte(image,(unsigned char) 0xf9);
1688 (void) WriteBlobByte(image,(unsigned char) 0x04);
1689 c=image->dispose << 2;
1692 (void) WriteBlobByte(image,(unsigned char) c);
1693 delay=(size_t) (100*image->delay/MagickMax((size_t)
1694 image->ticks_per_second,1));
1695 (void) WriteBlobLSBShort(image,(unsigned short) delay);
1696 (void) WriteBlobByte(image,(unsigned char) (opacity >= 0 ? opacity :
1698 (void) WriteBlobByte(image,(unsigned char) 0x00);
1699 if ((LocaleCompare(write_info->magick,"GIF87") != 0) &&
1700 (GetImageProperty(image,"comment",exception) != (const char *) NULL))
1712 Write comment extension.
1714 (void) WriteBlobByte(image,(unsigned char) 0x21);
1715 (void) WriteBlobByte(image,(unsigned char) 0xfe);
1716 value=GetImageProperty(image,"comment",exception);
1717 for (p=value; *p != '\0'; )
1719 count=MagickMin(strlen(p),255);
1720 (void) WriteBlobByte(image,(unsigned char) count);
1721 for (i=0; i < (ssize_t) count; i++)
1722 (void) WriteBlobByte(image,(unsigned char) *p++);
1724 (void) WriteBlobByte(image,(unsigned char) 0x00);
1726 if ((GetPreviousImageInList(image) == (Image *) NULL) &&
1727 (GetNextImageInList(image) != (Image *) NULL) &&
1728 (image->iterations != 1))
1731 Write Netscape Loop extension.
1733 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1734 " Writing GIF Extension %s","NETSCAPE2.0");
1735 (void) WriteBlobByte(image,(unsigned char) 0x21);
1736 (void) WriteBlobByte(image,(unsigned char) 0xff);
1737 (void) WriteBlobByte(image,(unsigned char) 0x0b);
1738 (void) WriteBlob(image,11,(unsigned char *) "NETSCAPE2.0");
1739 (void) WriteBlobByte(image,(unsigned char) 0x03);
1740 (void) WriteBlobByte(image,(unsigned char) 0x01);
1741 (void) WriteBlobLSBShort(image,(unsigned short) image->iterations);
1742 (void) WriteBlobByte(image,(unsigned char) 0x00);
1744 if ((image->gamma != 1.0f/2.2f))
1747 attributes[MaxTextExtent];
1753 Write ImageMagick extension.
1755 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1756 " Writing GIF Extension %s","ImageMagick");
1757 (void) WriteBlobByte(image,(unsigned char) 0x21);
1758 (void) WriteBlobByte(image,(unsigned char) 0xff);
1759 (void) WriteBlobByte(image,(unsigned char) 0x0b);
1760 (void) WriteBlob(image,11,(unsigned char *) "ImageMagick");
1761 length=FormatLocaleString(attributes,MaxTextExtent,"gamma=%g",
1763 (void) WriteBlobByte(image,(unsigned char) length);
1764 (void) WriteBlob(image,length,(unsigned char *) attributes);
1765 (void) WriteBlobByte(image,(unsigned char) 0x00);
1767 ResetImageProfileIterator(image);
1776 name=GetNextImageProfile(image);
1777 if (name == (const char *) NULL)
1779 profile=GetImageProfile(image,name);
1780 if (profile != (StringInfo *) NULL)
1782 if ((LocaleCompare(name,"ICC") == 0) ||
1783 (LocaleCompare(name,"ICM") == 0) ||
1784 (LocaleCompare(name,"IPTC") == 0) ||
1785 (LocaleCompare(name,"8BIM") == 0) ||
1786 (LocaleNCompare(name,"gif:",4) == 0))
1797 datum=GetStringInfoDatum(profile);
1798 length=GetStringInfoLength(profile);
1799 (void) WriteBlobByte(image,(unsigned char) 0x21);
1800 (void) WriteBlobByte(image,(unsigned char) 0xff);
1801 (void) WriteBlobByte(image,(unsigned char) 0x0b);
1802 if ((LocaleCompare(name,"ICC") == 0) ||
1803 (LocaleCompare(name,"ICM") == 0))
1806 Write ICC extension.
1808 (void) WriteBlob(image,11,(unsigned char *) "ICCRGBG1012");
1809 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1810 " Writing GIF Extension %s","ICCRGBG1012");
1813 if ((LocaleCompare(name,"IPTC") == 0))
1816 Write IPTC extension.
1818 (void) WriteBlob(image,11,(unsigned char *) "MGKIPTC0000");
1819 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1820 " Writing GIF Extension %s","MGKIPTC0000");
1823 if ((LocaleCompare(name,"8BIM") == 0))
1826 Write 8BIM extension.
1828 (void) WriteBlob(image,11,(unsigned char *)
1830 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1831 " Writing GIF Extension %s","MGK8BIM0000");
1836 extension[MaxTextExtent];
1839 Write generic extension.
1841 (void) CopyMagickString(extension,name+4,
1843 (void) WriteBlob(image,11,(unsigned char *) extension);
1844 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1845 " Writing GIF Extension %s",name);
1848 while ((ssize_t) length > offset)
1853 if ((length-offset) < 255)
1854 block_length=length-offset;
1857 (void) WriteBlobByte(image,(unsigned char) block_length);
1858 (void) WriteBlob(image,(size_t) block_length,datum+offset);
1859 offset+=(ssize_t) block_length;
1861 (void) WriteBlobByte(image,(unsigned char) 0x00);
1866 (void) WriteBlobByte(image,','); /* image separator */
1868 Write the image header.
1870 page.x=image->page.x;
1871 page.y=image->page.y;
1872 if ((image->page.width != 0) && (image->page.height != 0))
1874 (void) WriteBlobLSBShort(image,(unsigned short) (page.x < 0 ? 0 : page.x));
1875 (void) WriteBlobLSBShort(image,(unsigned short) (page.y < 0 ? 0 : page.y));
1876 (void) WriteBlobLSBShort(image,(unsigned short) image->columns);
1877 (void) WriteBlobLSBShort(image,(unsigned short) image->rows);
1879 if (interlace != NoInterlace)
1880 c|=0x40; /* pixel data is interlaced */
1881 for (j=0; j < (ssize_t) (3*image->colors); j++)
1882 if (colormap[j] != global_colormap[j])
1884 if (j == (ssize_t) (3*image->colors))
1885 (void) WriteBlobByte(image,(unsigned char) c);
1889 c|=(bits_per_pixel-1); /* size of local colormap */
1890 (void) WriteBlobByte(image,(unsigned char) c);
1891 length=(size_t) (3*(one << bits_per_pixel));
1892 (void) WriteBlob(image,length,colormap);
1895 Write the image data.
1897 c=(int) MagickMax(bits_per_pixel,2);
1898 (void) WriteBlobByte(image,(unsigned char) c);
1899 status=EncodeImage(write_info,image,(size_t) MagickMax(bits_per_pixel,2)+1,
1901 if (status == MagickFalse)
1903 global_colormap=(unsigned char *) RelinquishMagickMemory(
1905 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
1906 write_info=DestroyImageInfo(write_info);
1907 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1909 (void) WriteBlobByte(image,(unsigned char) 0x00);
1910 if (GetNextImageInList(image) == (Image *) NULL)
1912 image=SyncNextImageInList(image);
1914 status=SetImageProgress(image,SaveImagesTag,scene,
1915 GetImageListLength(image));
1916 if (status == MagickFalse)
1918 } while (write_info->adjoin != MagickFalse);
1919 (void) WriteBlobByte(image,';'); /* terminator */
1920 global_colormap=(unsigned char *) RelinquishMagickMemory(global_colormap);
1921 colormap=(unsigned char *) RelinquishMagickMemory(colormap);
1922 write_info=DestroyImageInfo(write_info);
1923 (void) CloseBlob(image);