2 * Gif extracting routines - derived from libungif
4 * Portions Copyright 2006 Mike McCormack
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 * Original copyright notice:
24 * The GIFLIB distribution is Copyright (c) 1997 Eric S. Raymond
26 * Permission is hereby granted, free of charge, to any person obtaining a copy
27 * of this software and associated documentation files (the "Software"), to deal
28 * in the Software without restriction, including without limitation the rights
29 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
30 * copies of the Software, and to permit persons to whom the Software is
31 * furnished to do so, subject to the following conditions:
33 * The above copyright notice and this permission notice shall be included in
34 * all copies or substantial portions of the Software.
38 /******************************************************************************
39 * "Gif-Lib" - Yet another gif library.
41 * Written by: Gershon Elber IBM PC Ver 1.1, Aug. 1990
42 ******************************************************************************
43 * The kernel of the GIF Decoding process can be found here.
44 ******************************************************************************
46 * 16 Jun 89 - Version 1.0 by Gershon Elber.
47 * 3 Sep 90 - Version 1.1 by Gershon Elber (Support for Gif89, Unique names).
48 *****************************************************************************/
54 #define LZ_MAX_CODE 4095 /* Biggest code possible in 12 bits. */
57 #define NO_SUCH_CODE 4098 /* Impossible code, to signal empty. */
59 typedef struct GifFilePrivateType {
60 GifWord BitsPerPixel, /* Bits per pixel (Codes uses at least this + 1). */
61 ClearCode, /* The CLEAR LZ code. */
62 EOFCode, /* The EOF LZ code. */
63 RunningCode, /* The next code algorithm can generate. */
64 RunningBits, /* The number of bits required to represent RunningCode. */
65 MaxCode1, /* 1 bigger than max. possible code, in RunningBits bits. */
66 LastCode, /* The code before the current code. */
67 CrntCode, /* Current algorithm code. */
68 StackPtr, /* For character stack (see below). */
69 CrntShiftState; /* Number of bits in CrntShiftDWord. */
70 unsigned long CrntShiftDWord; /* For bytes decomposition into codes. */
71 unsigned long PixelCount; /* Number of pixels in image. */
72 InputFunc Read; /* function to read gif input (TVT) */
73 GifByteType Buf[256]; /* Compressed input is buffered here. */
74 GifByteType Stack[LZ_MAX_CODE]; /* Decoded pixels are stacked here. */
75 GifByteType Suffix[LZ_MAX_CODE + 1]; /* So we can trace the codes. */
76 GifPrefixType Prefix[LZ_MAX_CODE + 1];
79 /* avoid extra function call in case we use fread (TVT) */
80 #define READ(_gif,_buf,_len) \
81 ((GifFilePrivateType*)_gif->Private)->Read(_gif,_buf,_len)
83 static int DGifGetWord(GifFileType *GifFile, GifWord *Word);
84 static int DGifSetupDecompress(GifFileType *GifFile);
85 static int DGifDecompressLine(GifFileType *GifFile, GifPixelType *Line, int LineLen);
86 static int DGifGetPrefixChar(GifPrefixType *Prefix, int Code, int ClearCode);
87 static int DGifDecompressInput(GifFileType *GifFile, int *Code);
88 static int DGifBufferedInput(GifFileType *GifFile, GifByteType *Buf,
89 GifByteType *NextByte);
91 static int DGifGetExtensionNext(GifFileType * GifFile, GifByteType ** GifExtension);
92 static int DGifGetCodeNext(GifFileType * GifFile, GifByteType ** GifCodeBlock);
94 /******************************************************************************
95 * Miscellaneous utility functions
96 *****************************************************************************/
98 /* return smallest bitfield size n will fit in */
104 for (i = 1; i <= 8; i++)
110 /******************************************************************************
111 * Color map object functions
112 *****************************************************************************/
115 * Allocate a color map of given size; initialize with contents of
116 * ColorMap if that pointer is non-NULL.
118 static ColorMapObject *
119 MakeMapObject(int ColorCount,
120 const GifColorType * ColorMap) {
122 ColorMapObject *Object;
124 /*** FIXME: Our ColorCount has to be a power of two. Is it necessary to
125 * make the user know that or should we automatically round up instead? */
126 if (ColorCount != (1 << BitSize(ColorCount))) {
127 return ((ColorMapObject *) NULL);
130 Object = malloc(sizeof(ColorMapObject));
131 if (Object == (ColorMapObject *) NULL) {
132 return ((ColorMapObject *) NULL);
135 Object->Colors = (GifColorType *)calloc(ColorCount, sizeof(GifColorType));
136 if (Object->Colors == (GifColorType *) NULL) {
137 return ((ColorMapObject *) NULL);
140 Object->ColorCount = ColorCount;
141 Object->BitsPerPixel = BitSize(ColorCount);
144 memcpy(Object->Colors, ColorMap, ColorCount * sizeof(GifColorType));
151 * Free a color map object
154 FreeMapObject(ColorMapObject * Object) {
156 if (Object != NULL) {
157 free(Object->Colors);
160 * When we are willing to break API we need to make this function
161 * FreeMapObject(ColorMapObject **Object)
162 * and do this assignment to NULL here:
169 AddExtensionBlock(SavedImage * New,
171 unsigned char ExtData[]) {
175 if (New->ExtensionBlocks == NULL)
176 New->ExtensionBlocks = malloc(sizeof(ExtensionBlock));
178 New->ExtensionBlocks = realloc(New->ExtensionBlocks,
179 sizeof(ExtensionBlock) *
180 (New->ExtensionBlockCount + 1));
182 if (New->ExtensionBlocks == NULL)
185 ep = &New->ExtensionBlocks[New->ExtensionBlockCount++];
188 ep->Bytes = malloc(ep->ByteCount);
189 if (ep->Bytes == NULL)
193 memcpy(ep->Bytes, ExtData, Len);
194 ep->Function = New->Function;
201 FreeExtension(SavedImage * Image)
205 if ((Image == NULL) || (Image->ExtensionBlocks == NULL)) {
208 for (ep = Image->ExtensionBlocks;
209 ep < (Image->ExtensionBlocks + Image->ExtensionBlockCount); ep++)
211 free(Image->ExtensionBlocks);
212 Image->ExtensionBlocks = NULL;
215 /******************************************************************************
216 * Image block allocation functions
217 ******************************************************************************/
220 FreeSavedImages(GifFileType * GifFile) {
224 if ((GifFile == NULL) || (GifFile->SavedImages == NULL)) {
227 for (sp = GifFile->SavedImages;
228 sp < GifFile->SavedImages + GifFile->ImageCount; sp++) {
229 if (sp->ImageDesc.ColorMap) {
230 FreeMapObject(sp->ImageDesc.ColorMap);
231 sp->ImageDesc.ColorMap = NULL;
234 free(sp->RasterBits);
236 if (sp->ExtensionBlocks)
239 free(GifFile->SavedImages);
240 GifFile->SavedImages=NULL;
243 /******************************************************************************
244 * This routine should be called before any other DGif calls. Note that
245 * this routine is called automatically from DGif file open routines.
246 *****************************************************************************/
248 DGifGetScreenDesc(GifFileType * GifFile) {
253 /* Put the screen descriptor into the file: */
254 if (DGifGetWord(GifFile, &GifFile->SWidth) == GIF_ERROR ||
255 DGifGetWord(GifFile, &GifFile->SHeight) == GIF_ERROR)
258 if (READ(GifFile, Buf, 3) != 3) {
261 GifFile->SColorResolution = (((Buf[0] & 0x70) + 1) >> 4) + 1;
262 BitsPerPixel = (Buf[0] & 0x07) + 1;
263 GifFile->SBackGroundColor = Buf[1];
264 if (Buf[0] & 0x80) { /* Do we have global color map? */
266 GifFile->SColorMap = MakeMapObject(1 << BitsPerPixel, NULL);
267 if (GifFile->SColorMap == NULL) {
271 /* Get the global color map: */
272 for (i = 0; i < GifFile->SColorMap->ColorCount; i++) {
273 if (READ(GifFile, Buf, 3) != 3) {
274 FreeMapObject(GifFile->SColorMap);
275 GifFile->SColorMap = NULL;
278 GifFile->SColorMap->Colors[i].Red = Buf[0];
279 GifFile->SColorMap->Colors[i].Green = Buf[1];
280 GifFile->SColorMap->Colors[i].Blue = Buf[2];
283 GifFile->SColorMap = NULL;
289 /******************************************************************************
290 * This routine should be called before any attempt to read an image.
291 *****************************************************************************/
293 DGifGetRecordType(GifFileType * GifFile,
294 GifRecordType * Type) {
298 if (READ(GifFile, &Buf, 1) != 1) {
304 *Type = IMAGE_DESC_RECORD_TYPE;
307 *Type = EXTENSION_RECORD_TYPE;
310 *Type = TERMINATE_RECORD_TYPE;
313 *Type = UNDEFINED_RECORD_TYPE;
320 /******************************************************************************
321 * This routine should be called before any attempt to read an image.
322 * Note it is assumed the Image desc. header (',') has been read.
323 *****************************************************************************/
325 DGifGetImageDesc(GifFileType * GifFile) {
329 GifFilePrivateType *Private = (GifFilePrivateType *)GifFile->Private;
332 if (DGifGetWord(GifFile, &GifFile->Image.Left) == GIF_ERROR ||
333 DGifGetWord(GifFile, &GifFile->Image.Top) == GIF_ERROR ||
334 DGifGetWord(GifFile, &GifFile->Image.Width) == GIF_ERROR ||
335 DGifGetWord(GifFile, &GifFile->Image.Height) == GIF_ERROR)
337 if (READ(GifFile, Buf, 1) != 1) {
340 BitsPerPixel = (Buf[0] & 0x07) + 1;
341 GifFile->Image.Interlace = (Buf[0] & 0x40);
342 if (Buf[0] & 0x80) { /* Does this image have local color map? */
344 /*** FIXME: Why do we check both of these in order to do this?
345 * Why do we have both Image and SavedImages? */
346 if (GifFile->Image.ColorMap && GifFile->SavedImages == NULL)
347 FreeMapObject(GifFile->Image.ColorMap);
349 GifFile->Image.ColorMap = MakeMapObject(1 << BitsPerPixel, NULL);
350 if (GifFile->Image.ColorMap == NULL) {
354 /* Get the image local color map: */
355 for (i = 0; i < GifFile->Image.ColorMap->ColorCount; i++) {
356 if (READ(GifFile, Buf, 3) != 3) {
357 FreeMapObject(GifFile->Image.ColorMap);
358 GifFile->Image.ColorMap = NULL;
361 GifFile->Image.ColorMap->Colors[i].Red = Buf[0];
362 GifFile->Image.ColorMap->Colors[i].Green = Buf[1];
363 GifFile->Image.ColorMap->Colors[i].Blue = Buf[2];
365 } else if (GifFile->Image.ColorMap) {
366 FreeMapObject(GifFile->Image.ColorMap);
367 GifFile->Image.ColorMap = NULL;
370 if (GifFile->SavedImages) {
371 if ((GifFile->SavedImages = realloc(GifFile->SavedImages,
373 (GifFile->ImageCount + 1))) == NULL) {
377 if ((GifFile->SavedImages = malloc(sizeof(SavedImage))) == NULL) {
382 sp = &GifFile->SavedImages[GifFile->ImageCount];
383 memcpy(&sp->ImageDesc, &GifFile->Image, sizeof(GifImageDesc));
384 if (GifFile->Image.ColorMap != NULL) {
385 sp->ImageDesc.ColorMap = MakeMapObject(
386 GifFile->Image.ColorMap->ColorCount,
387 GifFile->Image.ColorMap->Colors);
388 if (sp->ImageDesc.ColorMap == NULL) {
392 sp->RasterBits = (unsigned char *)NULL;
393 sp->ExtensionBlockCount = 0;
394 sp->ExtensionBlocks = (ExtensionBlock *) NULL;
396 GifFile->ImageCount++;
398 Private->PixelCount = (long)GifFile->Image.Width *
399 (long)GifFile->Image.Height;
401 DGifSetupDecompress(GifFile); /* Reset decompress algorithm parameters. */
406 /******************************************************************************
407 * Get one full scanned line (Line) of length LineLen from GIF file.
408 *****************************************************************************/
410 DGifGetLine(GifFileType * GifFile,
415 GifFilePrivateType *Private = (GifFilePrivateType *) GifFile->Private;
418 LineLen = GifFile->Image.Width;
420 if ((Private->PixelCount -= LineLen) > 0xffff0000UL) {
424 if (DGifDecompressLine(GifFile, Line, LineLen) == GIF_OK) {
425 if (Private->PixelCount == 0) {
426 /* We probably would not be called any more, so lets clean
427 * everything before we return: need to flush out all rest of
428 * image until empty block (size 0) detected. We use GetCodeNext. */
430 if (DGifGetCodeNext(GifFile, &Dummy) == GIF_ERROR)
432 while (Dummy != NULL) ;
439 /******************************************************************************
440 * Get an extension block (see GIF manual) from gif file. This routine only
441 * returns the first data block, and DGifGetExtensionNext should be called
442 * after this one until NULL extension is returned.
443 * The Extension should NOT be freed by the user (not dynamically allocated).
444 * Note it is assumed the Extension desc. header ('!') has been read.
445 *****************************************************************************/
447 DGifGetExtension(GifFileType * GifFile,
449 GifByteType ** Extension) {
453 if (READ(GifFile, &Buf, 1) != 1) {
458 return DGifGetExtensionNext(GifFile, Extension);
461 /******************************************************************************
462 * Get a following extension block (see GIF manual) from gif file. This
463 * routine should be called until NULL Extension is returned.
464 * The Extension should NOT be freed by the user (not dynamically allocated).
465 *****************************************************************************/
467 DGifGetExtensionNext(GifFileType * GifFile,
468 GifByteType ** Extension) {
471 GifFilePrivateType *Private = (GifFilePrivateType *)GifFile->Private;
473 if (READ(GifFile, &Buf, 1) != 1) {
477 *Extension = Private->Buf; /* Use private unused buffer. */
478 (*Extension)[0] = Buf; /* Pascal strings notation (pos. 0 is len.). */
479 if (READ(GifFile, &((*Extension)[1]), Buf) != Buf) {
488 /******************************************************************************
489 * Get 2 bytes (word) from the given file:
490 *****************************************************************************/
492 DGifGetWord(GifFileType * GifFile,
497 if (READ(GifFile, c, 2) != 2) {
501 *Word = (((unsigned int)c[1]) << 8) + c[0];
505 /******************************************************************************
506 * Continue to get the image code in compressed form. This routine should be
507 * called until NULL block is returned.
508 * The block should NOT be freed by the user (not dynamically allocated).
509 *****************************************************************************/
511 DGifGetCodeNext(GifFileType * GifFile,
512 GifByteType ** CodeBlock) {
515 GifFilePrivateType *Private = (GifFilePrivateType *)GifFile->Private;
517 if (READ(GifFile, &Buf, 1) != 1) {
522 *CodeBlock = Private->Buf; /* Use private unused buffer. */
523 (*CodeBlock)[0] = Buf; /* Pascal strings notation (pos. 0 is len.). */
524 if (READ(GifFile, &((*CodeBlock)[1]), Buf) != Buf) {
529 Private->Buf[0] = 0; /* Make sure the buffer is empty! */
530 Private->PixelCount = 0; /* And local info. indicate image read. */
536 /******************************************************************************
537 * Setup the LZ decompression for this image:
538 *****************************************************************************/
540 DGifSetupDecompress(GifFileType * GifFile) {
543 GifByteType CodeSize;
544 GifPrefixType *Prefix;
545 GifFilePrivateType *Private = (GifFilePrivateType *)GifFile->Private;
547 READ(GifFile, &CodeSize, 1); /* Read Code size from file. */
548 BitsPerPixel = CodeSize;
550 Private->Buf[0] = 0; /* Input Buffer empty. */
551 Private->BitsPerPixel = BitsPerPixel;
552 Private->ClearCode = (1 << BitsPerPixel);
553 Private->EOFCode = Private->ClearCode + 1;
554 Private->RunningCode = Private->EOFCode + 1;
555 Private->RunningBits = BitsPerPixel + 1; /* Number of bits per code. */
556 Private->MaxCode1 = 1 << Private->RunningBits; /* Max. code + 1. */
557 Private->StackPtr = 0; /* No pixels on the pixel stack. */
558 Private->LastCode = NO_SUCH_CODE;
559 Private->CrntShiftState = 0; /* No information in CrntShiftDWord. */
560 Private->CrntShiftDWord = 0;
562 Prefix = Private->Prefix;
563 for (i = 0; i <= LZ_MAX_CODE; i++)
564 Prefix[i] = NO_SUCH_CODE;
569 /******************************************************************************
570 * The LZ decompression routine:
571 * This version decompress the given gif file into Line of length LineLen.
572 * This routine can be called few times (one per scan line, for example), in
573 * order the complete the whole image.
574 *****************************************************************************/
576 DGifDecompressLine(GifFileType * GifFile,
581 int j, CrntCode, EOFCode, ClearCode, CrntPrefix, LastCode, StackPtr;
582 GifByteType *Stack, *Suffix;
583 GifPrefixType *Prefix;
584 GifFilePrivateType *Private = (GifFilePrivateType *) GifFile->Private;
586 StackPtr = Private->StackPtr;
587 Prefix = Private->Prefix;
588 Suffix = Private->Suffix;
589 Stack = Private->Stack;
590 EOFCode = Private->EOFCode;
591 ClearCode = Private->ClearCode;
592 LastCode = Private->LastCode;
595 /* Let pop the stack off before continueing to read the gif file: */
596 while (StackPtr != 0 && i < LineLen)
597 Line[i++] = Stack[--StackPtr];
600 while (i < LineLen) { /* Decode LineLen items. */
601 if (DGifDecompressInput(GifFile, &CrntCode) == GIF_ERROR)
604 if (CrntCode == EOFCode) {
605 /* Note however that usually we will not be here as we will stop
606 * decoding as soon as we got all the pixel, or EOF code will
607 * not be read at all, and DGifGetLine/Pixel clean everything. */
608 if (i != LineLen - 1 || Private->PixelCount != 0) {
612 } else if (CrntCode == ClearCode) {
613 /* We need to start over again: */
614 for (j = 0; j <= LZ_MAX_CODE; j++)
615 Prefix[j] = NO_SUCH_CODE;
616 Private->RunningCode = Private->EOFCode + 1;
617 Private->RunningBits = Private->BitsPerPixel + 1;
618 Private->MaxCode1 = 1 << Private->RunningBits;
619 LastCode = Private->LastCode = NO_SUCH_CODE;
621 /* Its regular code - if in pixel range simply add it to output
622 * stream, otherwise trace to codes linked list until the prefix
623 * is in pixel range: */
624 if (CrntCode < ClearCode) {
625 /* This is simple - its pixel scalar, so add it to output: */
626 Line[i++] = CrntCode;
628 /* Its a code to needed to be traced: trace the linked list
629 * until the prefix is a pixel, while pushing the suffix
630 * pixels on our stack. If we done, pop the stack in reverse
631 * (thats what stack is good for!) order to output. */
632 if (Prefix[CrntCode] == NO_SUCH_CODE) {
633 /* Only allowed if CrntCode is exactly the running code:
634 * In that case CrntCode = XXXCode, CrntCode or the
635 * prefix code is last code and the suffix char is
636 * exactly the prefix of last code! */
637 if (CrntCode == Private->RunningCode - 2) {
638 CrntPrefix = LastCode;
639 Suffix[Private->RunningCode - 2] =
640 Stack[StackPtr++] = DGifGetPrefixChar(Prefix,
647 CrntPrefix = CrntCode;
649 /* Now (if image is O.K.) we should not get a NO_SUCH_CODE
650 * during the trace. As we might loop forever, in case of
651 * defective image, we count the number of loops we trace
652 * and stop if we got LZ_MAX_CODE. Obviously we cannot
653 * loop more than that. */
655 while (j++ <= LZ_MAX_CODE &&
656 CrntPrefix > ClearCode && CrntPrefix <= LZ_MAX_CODE) {
657 Stack[StackPtr++] = Suffix[CrntPrefix];
658 CrntPrefix = Prefix[CrntPrefix];
660 if (j >= LZ_MAX_CODE || CrntPrefix > LZ_MAX_CODE) {
663 /* Push the last character on stack: */
664 Stack[StackPtr++] = CrntPrefix;
666 /* Now lets pop all the stack into output: */
667 while (StackPtr != 0 && i < LineLen)
668 Line[i++] = Stack[--StackPtr];
670 if (LastCode != NO_SUCH_CODE) {
671 Prefix[Private->RunningCode - 2] = LastCode;
673 if (CrntCode == Private->RunningCode - 2) {
674 /* Only allowed if CrntCode is exactly the running code:
675 * In that case CrntCode = XXXCode, CrntCode or the
676 * prefix code is last code and the suffix char is
677 * exactly the prefix of last code! */
678 Suffix[Private->RunningCode - 2] =
679 DGifGetPrefixChar(Prefix, LastCode, ClearCode);
681 Suffix[Private->RunningCode - 2] =
682 DGifGetPrefixChar(Prefix, CrntCode, ClearCode);
689 Private->LastCode = LastCode;
690 Private->StackPtr = StackPtr;
695 /******************************************************************************
696 * Routine to trace the Prefixes linked list until we get a prefix which is
697 * not code, but a pixel value (less than ClearCode). Returns that pixel value.
698 * If image is defective, we might loop here forever, so we limit the loops to
699 * the maximum possible if image O.k. - LZ_MAX_CODE times.
700 *****************************************************************************/
702 DGifGetPrefixChar(GifPrefixType *Prefix,
708 while (Code > ClearCode && i++ <= LZ_MAX_CODE)
713 /******************************************************************************
714 * The LZ decompression input routine:
715 * This routine is responsible for the decompression of the bit stream from
716 * 8 bits (bytes) packets, into the real codes.
717 * Returns GIF_OK if read successfully.
718 *****************************************************************************/
720 DGifDecompressInput(GifFileType * GifFile,
723 GifFilePrivateType *Private = (GifFilePrivateType *)GifFile->Private;
725 GifByteType NextByte;
726 static unsigned short CodeMasks[] = {
727 0x0000, 0x0001, 0x0003, 0x0007,
728 0x000f, 0x001f, 0x003f, 0x007f,
729 0x00ff, 0x01ff, 0x03ff, 0x07ff,
732 /* The image can't contain more than LZ_BITS per code. */
733 if (Private->RunningBits > LZ_BITS) {
737 while (Private->CrntShiftState < Private->RunningBits) {
738 /* Needs to get more bytes from input stream for next code: */
739 if (DGifBufferedInput(GifFile, Private->Buf, &NextByte) == GIF_ERROR) {
742 Private->CrntShiftDWord |=
743 ((unsigned long)NextByte) << Private->CrntShiftState;
744 Private->CrntShiftState += 8;
746 *Code = Private->CrntShiftDWord & CodeMasks[Private->RunningBits];
748 Private->CrntShiftDWord >>= Private->RunningBits;
749 Private->CrntShiftState -= Private->RunningBits;
751 /* If code cannot fit into RunningBits bits, must raise its size. Note
752 * however that codes above 4095 are used for special signaling.
753 * If we're using LZ_BITS bits already and we're at the max code, just
754 * keep using the table as it is, don't increment Private->RunningCode.
756 if (Private->RunningCode < LZ_MAX_CODE + 2 &&
757 ++Private->RunningCode > Private->MaxCode1 &&
758 Private->RunningBits < LZ_BITS) {
759 Private->MaxCode1 <<= 1;
760 Private->RunningBits++;
765 /******************************************************************************
766 * This routines read one gif data block at a time and buffers it internally
767 * so that the decompression routine could access it.
768 * The routine returns the next byte from its internal buffer (or read next
769 * block in if buffer empty) and returns GIF_OK if successful.
770 *****************************************************************************/
772 DGifBufferedInput(GifFileType * GifFile,
774 GifByteType * NextByte) {
777 /* Needs to read the next buffer - this one is empty: */
778 if (READ(GifFile, Buf, 1) != 1) {
781 /* There shouldn't be any empty data blocks here as the LZW spec
782 * says the LZW termination code should come first. Therefore we
783 * shouldn't be inside this routine at that point.
788 if (READ(GifFile, &Buf[1], Buf[0]) != Buf[0]) {
792 Buf[1] = 2; /* We use now the second place as last char read! */
795 *NextByte = Buf[Buf[1]++];
802 /******************************************************************************
803 * This routine reads an entire GIF into core, hanging all its state info off
804 * the GifFileType pointer. Call DGifOpenFileName() or DGifOpenFileHandle()
805 * first to initialize I/O. Its inverse is EGifSpew().
806 ******************************************************************************/
808 DGifSlurp(GifFileType * GifFile) {
811 GifRecordType RecordType;
813 GifByteType *ExtData;
814 SavedImage temp_save;
816 temp_save.ExtensionBlocks = NULL;
817 temp_save.ExtensionBlockCount = 0;
820 if (DGifGetRecordType(GifFile, &RecordType) == GIF_ERROR)
823 switch (RecordType) {
824 case IMAGE_DESC_RECORD_TYPE:
825 if (DGifGetImageDesc(GifFile) == GIF_ERROR)
828 sp = &GifFile->SavedImages[GifFile->ImageCount - 1];
829 ImageSize = sp->ImageDesc.Width * sp->ImageDesc.Height;
831 sp->RasterBits = malloc(ImageSize * sizeof(GifPixelType));
832 if (sp->RasterBits == NULL) {
835 if (DGifGetLine(GifFile, sp->RasterBits, ImageSize) ==
838 if (temp_save.ExtensionBlocks) {
839 sp->ExtensionBlocks = temp_save.ExtensionBlocks;
840 sp->ExtensionBlockCount = temp_save.ExtensionBlockCount;
842 temp_save.ExtensionBlocks = NULL;
843 temp_save.ExtensionBlockCount = 0;
845 /* FIXME: The following is wrong. It is left in only for
846 * backwards compatibility. Someday it should go away. Use
847 * the sp->ExtensionBlocks->Function variable instead. */
848 sp->Function = sp->ExtensionBlocks[0].Function;
852 case EXTENSION_RECORD_TYPE:
853 if (DGifGetExtension(GifFile, &temp_save.Function, &ExtData) ==
856 while (ExtData != NULL) {
858 /* Create an extension block with our data */
859 if (AddExtensionBlock(&temp_save, ExtData[0], &ExtData[1])
863 if (DGifGetExtensionNext(GifFile, &ExtData) == GIF_ERROR)
865 temp_save.Function = 0;
869 case TERMINATE_RECORD_TYPE:
872 default: /* Should be trapped by DGifGetRecordType */
875 } while (RecordType != TERMINATE_RECORD_TYPE);
877 /* Just in case the Gif has an extension block without an associated
878 * image... (Should we save this into a savefile structure with no image
879 * instead? Have to check if the present writing code can handle that as
881 if (temp_save.ExtensionBlocks)
882 FreeExtension(&temp_save);
887 /******************************************************************************
888 * GifFileType constructor with user supplied input function (TVT)
889 *****************************************************************************/
891 DGifOpen(void *userData,
892 InputFunc readFunc) {
894 unsigned char Buf[GIF_STAMP_LEN + 1];
895 GifFileType *GifFile;
896 GifFilePrivateType *Private;
898 GifFile = malloc(sizeof(GifFileType));
899 if (GifFile == NULL) {
903 memset(GifFile, '\0', sizeof(GifFileType));
905 Private = malloc(sizeof(GifFilePrivateType));
911 GifFile->Private = (void*)Private;
913 Private->Read = readFunc; /* TVT */
914 GifFile->UserData = userData; /* TVT */
916 /* Lets see if this is a GIF file: */
917 if (READ(GifFile, Buf, GIF_STAMP_LEN) != GIF_STAMP_LEN) {
923 /* The GIF Version number is ignored at this time. Maybe we should do
924 * something more useful with it. */
925 Buf[GIF_STAMP_LEN] = 0;
926 if (memcmp(GIF_STAMP, Buf, GIF_VERSION_POS) != 0) {
932 if (DGifGetScreenDesc(GifFile) == GIF_ERROR) {
941 /******************************************************************************
942 * This routine should be called last, to close the GIF file.
943 *****************************************************************************/
945 DGifCloseFile(GifFileType * GifFile) {
947 GifFilePrivateType *Private;
952 Private = (GifFilePrivateType *) GifFile->Private;
954 if (GifFile->Image.ColorMap) {
955 FreeMapObject(GifFile->Image.ColorMap);
956 GifFile->Image.ColorMap = NULL;
959 if (GifFile->SColorMap) {
960 FreeMapObject(GifFile->SColorMap);
961 GifFile->SColorMap = NULL;
967 if (GifFile->SavedImages) {
968 FreeSavedImages(GifFile);
969 GifFile->SavedImages = NULL;