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 *****************************************************************************/
58 static void *ungif_alloc( size_t sz
)
60 return HeapAlloc( GetProcessHeap(), 0, sz
);
63 static void *ungif_calloc( size_t num
, size_t sz
)
65 return HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, num
*sz
);
68 static void *ungif_realloc( void *ptr
, size_t sz
)
70 return HeapReAlloc( GetProcessHeap(), 0, ptr
, sz
);
73 static void ungif_free( void *ptr
)
75 HeapFree( GetProcessHeap(), 0, ptr
);
78 #define LZ_MAX_CODE 4095 /* Biggest code possible in 12 bits. */
81 #define NO_SUCH_CODE 4098 /* Impossible code, to signal empty. */
83 typedef struct GifFilePrivateType
{
84 GifWord BitsPerPixel
, /* Bits per pixel (Codes uses at least this + 1). */
85 ClearCode
, /* The CLEAR LZ code. */
86 EOFCode
, /* The EOF LZ code. */
87 RunningCode
, /* The next code algorithm can generate. */
88 RunningBits
, /* The number of bits required to represent RunningCode. */
89 MaxCode1
, /* 1 bigger than max. possible code, in RunningBits bits. */
90 LastCode
, /* The code before the current code. */
91 CrntCode
, /* Current algorithm code. */
92 StackPtr
, /* For character stack (see below). */
93 CrntShiftState
; /* Number of bits in CrntShiftDWord. */
94 unsigned long CrntShiftDWord
; /* For bytes decomposition into codes. */
95 unsigned long PixelCount
; /* Number of pixels in image. */
96 InputFunc Read
; /* function to read gif input (TVT) */
97 GifByteType Buf
[256]; /* Compressed input is buffered here. */
98 GifByteType Stack
[LZ_MAX_CODE
]; /* Decoded pixels are stacked here. */
99 GifByteType Suffix
[LZ_MAX_CODE
+ 1]; /* So we can trace the codes. */
100 GifPrefixType Prefix
[LZ_MAX_CODE
+ 1];
101 } GifFilePrivateType
;
103 /* avoid extra function call in case we use fread (TVT) */
104 #define READ(_gif,_buf,_len) \
105 ((GifFilePrivateType*)_gif->Private)->Read(_gif,_buf,_len)
107 static int DGifGetWord(GifFileType
*GifFile
, GifWord
*Word
);
108 static int DGifSetupDecompress(GifFileType
*GifFile
);
109 static int DGifDecompressLine(GifFileType
*GifFile
, GifPixelType
*Line
, int LineLen
);
110 static int DGifGetPrefixChar(const GifPrefixType
*Prefix
, int Code
, int ClearCode
);
111 static int DGifDecompressInput(GifFileType
*GifFile
, int *Code
);
112 static int DGifBufferedInput(GifFileType
*GifFile
, GifByteType
*Buf
,
113 GifByteType
*NextByte
);
115 static int DGifGetExtensionNext(GifFileType
* GifFile
, GifByteType
** GifExtension
);
116 static int DGifGetCodeNext(GifFileType
* GifFile
, GifByteType
** GifCodeBlock
);
118 /******************************************************************************
119 * Miscellaneous utility functions
120 *****************************************************************************/
122 /* return smallest bitfield size n will fit in */
128 for (i
= 1; i
<= 8; i
++)
134 /******************************************************************************
135 * Color map object functions
136 *****************************************************************************/
139 * Allocate a color map of given size; initialize with contents of
140 * ColorMap if that pointer is non-NULL.
142 static ColorMapObject
*
143 MakeMapObject(int ColorCount
,
144 const GifColorType
* ColorMap
) {
146 ColorMapObject
*Object
;
148 /*** FIXME: Our ColorCount has to be a power of two. Is it necessary to
149 * make the user know that or should we automatically round up instead? */
150 if (ColorCount
!= (1 << BitSize(ColorCount
))) {
151 return ((ColorMapObject
*) NULL
);
154 Object
= ungif_alloc(sizeof(ColorMapObject
));
155 if (Object
== (ColorMapObject
*) NULL
) {
156 return ((ColorMapObject
*) NULL
);
159 Object
->Colors
= ungif_calloc(ColorCount
, sizeof(GifColorType
));
160 if (Object
->Colors
== (GifColorType
*) NULL
) {
164 Object
->ColorCount
= ColorCount
;
165 Object
->BitsPerPixel
= BitSize(ColorCount
);
168 memcpy(Object
->Colors
, ColorMap
, ColorCount
* sizeof(GifColorType
));
175 * Free a color map object
178 FreeMapObject(ColorMapObject
* Object
) {
180 if (Object
!= NULL
) {
181 ungif_free(Object
->Colors
);
184 * When we are willing to break API we need to make this function
185 * FreeMapObject(ColorMapObject **Object)
186 * and do this assignment to NULL here:
193 AddExtensionBlock(SavedImage
* New
,
195 const unsigned char ExtData
[]) {
199 if (New
->ExtensionBlocks
== NULL
)
200 New
->ExtensionBlocks
= ungif_alloc(sizeof(ExtensionBlock
));
202 New
->ExtensionBlocks
= ungif_realloc(New
->ExtensionBlocks
,
203 sizeof(ExtensionBlock
) *
204 (New
->ExtensionBlockCount
+ 1));
206 if (New
->ExtensionBlocks
== NULL
)
209 ep
= &New
->ExtensionBlocks
[New
->ExtensionBlockCount
++];
212 ep
->Bytes
= ungif_alloc(ep
->ByteCount
);
213 if (ep
->Bytes
== NULL
)
217 memcpy(ep
->Bytes
, ExtData
, Len
);
218 ep
->Function
= New
->Function
;
225 FreeExtension(SavedImage
* Image
)
229 if ((Image
== NULL
) || (Image
->ExtensionBlocks
== NULL
)) {
232 for (ep
= Image
->ExtensionBlocks
;
233 ep
< (Image
->ExtensionBlocks
+ Image
->ExtensionBlockCount
); ep
++)
234 ungif_free(ep
->Bytes
);
235 ungif_free(Image
->ExtensionBlocks
);
236 Image
->ExtensionBlocks
= NULL
;
239 /******************************************************************************
240 * Image block allocation functions
241 ******************************************************************************/
244 FreeSavedImages(GifFileType
* GifFile
) {
248 if ((GifFile
== NULL
) || (GifFile
->SavedImages
== NULL
)) {
251 for (sp
= GifFile
->SavedImages
;
252 sp
< GifFile
->SavedImages
+ GifFile
->ImageCount
; sp
++) {
253 if (sp
->ImageDesc
.ColorMap
) {
254 FreeMapObject(sp
->ImageDesc
.ColorMap
);
255 sp
->ImageDesc
.ColorMap
= NULL
;
258 ungif_free(sp
->RasterBits
);
260 if (sp
->ExtensionBlocks
)
263 ungif_free(GifFile
->SavedImages
);
264 GifFile
->SavedImages
=NULL
;
267 /******************************************************************************
268 * This routine should be called before any other DGif calls. Note that
269 * this routine is called automatically from DGif file open routines.
270 *****************************************************************************/
272 DGifGetScreenDesc(GifFileType
* GifFile
) {
277 /* Put the screen descriptor into the file: */
278 if (DGifGetWord(GifFile
, &GifFile
->SWidth
) == GIF_ERROR
||
279 DGifGetWord(GifFile
, &GifFile
->SHeight
) == GIF_ERROR
)
282 if (READ(GifFile
, Buf
, 3) != 3) {
285 GifFile
->SColorResolution
= (((Buf
[0] & 0x70) + 1) >> 4) + 1;
286 BitsPerPixel
= (Buf
[0] & 0x07) + 1;
287 GifFile
->SBackGroundColor
= Buf
[1];
288 if (Buf
[0] & 0x80) { /* Do we have global color map? */
290 GifFile
->SColorMap
= MakeMapObject(1 << BitsPerPixel
, NULL
);
291 if (GifFile
->SColorMap
== NULL
) {
295 /* Get the global color map: */
296 for (i
= 0; i
< GifFile
->SColorMap
->ColorCount
; i
++) {
297 if (READ(GifFile
, Buf
, 3) != 3) {
298 FreeMapObject(GifFile
->SColorMap
);
299 GifFile
->SColorMap
= NULL
;
302 GifFile
->SColorMap
->Colors
[i
].Red
= Buf
[0];
303 GifFile
->SColorMap
->Colors
[i
].Green
= Buf
[1];
304 GifFile
->SColorMap
->Colors
[i
].Blue
= Buf
[2];
307 GifFile
->SColorMap
= NULL
;
313 /******************************************************************************
314 * This routine should be called before any attempt to read an image.
315 *****************************************************************************/
317 DGifGetRecordType(GifFileType
* GifFile
,
318 GifRecordType
* Type
) {
322 if (READ(GifFile
, &Buf
, 1) != 1) {
328 *Type
= IMAGE_DESC_RECORD_TYPE
;
331 *Type
= EXTENSION_RECORD_TYPE
;
334 *Type
= TERMINATE_RECORD_TYPE
;
337 *Type
= UNDEFINED_RECORD_TYPE
;
344 /******************************************************************************
345 * This routine should be called before any attempt to read an image.
346 * Note it is assumed the Image desc. header (',') has been read.
347 *****************************************************************************/
349 DGifGetImageDesc(GifFileType
* GifFile
) {
353 GifFilePrivateType
*Private
= (GifFilePrivateType
*)GifFile
->Private
;
356 if (DGifGetWord(GifFile
, &GifFile
->Image
.Left
) == GIF_ERROR
||
357 DGifGetWord(GifFile
, &GifFile
->Image
.Top
) == GIF_ERROR
||
358 DGifGetWord(GifFile
, &GifFile
->Image
.Width
) == GIF_ERROR
||
359 DGifGetWord(GifFile
, &GifFile
->Image
.Height
) == GIF_ERROR
)
361 if (READ(GifFile
, Buf
, 1) != 1) {
364 BitsPerPixel
= (Buf
[0] & 0x07) + 1;
365 GifFile
->Image
.Interlace
= (Buf
[0] & 0x40);
366 if (Buf
[0] & 0x80) { /* Does this image have local color map? */
368 /*** FIXME: Why do we check both of these in order to do this?
369 * Why do we have both Image and SavedImages? */
370 if (GifFile
->Image
.ColorMap
&& GifFile
->SavedImages
== NULL
)
371 FreeMapObject(GifFile
->Image
.ColorMap
);
373 GifFile
->Image
.ColorMap
= MakeMapObject(1 << BitsPerPixel
, NULL
);
374 if (GifFile
->Image
.ColorMap
== NULL
) {
378 /* Get the image local color map: */
379 for (i
= 0; i
< GifFile
->Image
.ColorMap
->ColorCount
; i
++) {
380 if (READ(GifFile
, Buf
, 3) != 3) {
381 FreeMapObject(GifFile
->Image
.ColorMap
);
382 GifFile
->Image
.ColorMap
= NULL
;
385 GifFile
->Image
.ColorMap
->Colors
[i
].Red
= Buf
[0];
386 GifFile
->Image
.ColorMap
->Colors
[i
].Green
= Buf
[1];
387 GifFile
->Image
.ColorMap
->Colors
[i
].Blue
= Buf
[2];
389 } else if (GifFile
->Image
.ColorMap
) {
390 FreeMapObject(GifFile
->Image
.ColorMap
);
391 GifFile
->Image
.ColorMap
= NULL
;
394 if (GifFile
->SavedImages
) {
395 if ((GifFile
->SavedImages
= ungif_realloc(GifFile
->SavedImages
,
397 (GifFile
->ImageCount
+ 1))) == NULL
) {
401 if ((GifFile
->SavedImages
= ungif_alloc(sizeof(SavedImage
))) == NULL
) {
406 sp
= &GifFile
->SavedImages
[GifFile
->ImageCount
];
407 memcpy(&sp
->ImageDesc
, &GifFile
->Image
, sizeof(GifImageDesc
));
408 if (GifFile
->Image
.ColorMap
!= NULL
) {
409 sp
->ImageDesc
.ColorMap
= MakeMapObject(
410 GifFile
->Image
.ColorMap
->ColorCount
,
411 GifFile
->Image
.ColorMap
->Colors
);
412 if (sp
->ImageDesc
.ColorMap
== NULL
) {
416 sp
->RasterBits
= (unsigned char *)NULL
;
417 sp
->ExtensionBlockCount
= 0;
418 sp
->ExtensionBlocks
= (ExtensionBlock
*) NULL
;
420 GifFile
->ImageCount
++;
422 Private
->PixelCount
= (long)GifFile
->Image
.Width
*
423 (long)GifFile
->Image
.Height
;
425 DGifSetupDecompress(GifFile
); /* Reset decompress algorithm parameters. */
430 /******************************************************************************
431 * Get one full scanned line (Line) of length LineLen from GIF file.
432 *****************************************************************************/
434 DGifGetLine(GifFileType
* GifFile
,
439 GifFilePrivateType
*Private
= (GifFilePrivateType
*) GifFile
->Private
;
442 LineLen
= GifFile
->Image
.Width
;
444 if ((Private
->PixelCount
-= LineLen
) > 0xffff0000UL
) {
448 if (DGifDecompressLine(GifFile
, Line
, LineLen
) == GIF_OK
) {
449 if (Private
->PixelCount
== 0) {
450 /* We probably would not be called any more, so lets clean
451 * everything before we return: need to flush out all rest of
452 * image until empty block (size 0) detected. We use GetCodeNext. */
454 if (DGifGetCodeNext(GifFile
, &Dummy
) == GIF_ERROR
)
456 while (Dummy
!= NULL
) ;
463 /******************************************************************************
464 * Get an extension block (see GIF manual) from gif file. This routine only
465 * returns the first data block, and DGifGetExtensionNext should be called
466 * after this one until NULL extension is returned.
467 * The Extension should NOT be freed by the user (not dynamically allocated).
468 * Note it is assumed the Extension desc. header ('!') has been read.
469 *****************************************************************************/
471 DGifGetExtension(GifFileType
* GifFile
,
473 GifByteType
** Extension
) {
477 if (READ(GifFile
, &Buf
, 1) != 1) {
482 return DGifGetExtensionNext(GifFile
, Extension
);
485 /******************************************************************************
486 * Get a following extension block (see GIF manual) from gif file. This
487 * routine should be called until NULL Extension is returned.
488 * The Extension should NOT be freed by the user (not dynamically allocated).
489 *****************************************************************************/
491 DGifGetExtensionNext(GifFileType
* GifFile
,
492 GifByteType
** Extension
) {
495 GifFilePrivateType
*Private
= (GifFilePrivateType
*)GifFile
->Private
;
497 if (READ(GifFile
, &Buf
, 1) != 1) {
501 *Extension
= Private
->Buf
; /* Use private unused buffer. */
502 (*Extension
)[0] = Buf
; /* Pascal strings notation (pos. 0 is len.). */
503 if (READ(GifFile
, &((*Extension
)[1]), Buf
) != Buf
) {
512 /******************************************************************************
513 * Get 2 bytes (word) from the given file:
514 *****************************************************************************/
516 DGifGetWord(GifFileType
* GifFile
,
521 if (READ(GifFile
, c
, 2) != 2) {
525 *Word
= (((unsigned int)c
[1]) << 8) + c
[0];
529 /******************************************************************************
530 * Continue to get the image code in compressed form. This routine should be
531 * called until NULL block is returned.
532 * The block should NOT be freed by the user (not dynamically allocated).
533 *****************************************************************************/
535 DGifGetCodeNext(GifFileType
* GifFile
,
536 GifByteType
** CodeBlock
) {
539 GifFilePrivateType
*Private
= (GifFilePrivateType
*)GifFile
->Private
;
541 if (READ(GifFile
, &Buf
, 1) != 1) {
546 *CodeBlock
= Private
->Buf
; /* Use private unused buffer. */
547 (*CodeBlock
)[0] = Buf
; /* Pascal strings notation (pos. 0 is len.). */
548 if (READ(GifFile
, &((*CodeBlock
)[1]), Buf
) != Buf
) {
553 Private
->Buf
[0] = 0; /* Make sure the buffer is empty! */
554 Private
->PixelCount
= 0; /* And local info. indicate image read. */
560 /******************************************************************************
561 * Setup the LZ decompression for this image:
562 *****************************************************************************/
564 DGifSetupDecompress(GifFileType
* GifFile
) {
567 GifByteType CodeSize
;
568 GifPrefixType
*Prefix
;
569 GifFilePrivateType
*Private
= (GifFilePrivateType
*)GifFile
->Private
;
571 READ(GifFile
, &CodeSize
, 1); /* Read Code size from file. */
572 BitsPerPixel
= CodeSize
;
574 Private
->Buf
[0] = 0; /* Input Buffer empty. */
575 Private
->BitsPerPixel
= BitsPerPixel
;
576 Private
->ClearCode
= (1 << BitsPerPixel
);
577 Private
->EOFCode
= Private
->ClearCode
+ 1;
578 Private
->RunningCode
= Private
->EOFCode
+ 1;
579 Private
->RunningBits
= BitsPerPixel
+ 1; /* Number of bits per code. */
580 Private
->MaxCode1
= 1 << Private
->RunningBits
; /* Max. code + 1. */
581 Private
->StackPtr
= 0; /* No pixels on the pixel stack. */
582 Private
->LastCode
= NO_SUCH_CODE
;
583 Private
->CrntShiftState
= 0; /* No information in CrntShiftDWord. */
584 Private
->CrntShiftDWord
= 0;
586 Prefix
= Private
->Prefix
;
587 for (i
= 0; i
<= LZ_MAX_CODE
; i
++)
588 Prefix
[i
] = NO_SUCH_CODE
;
593 /******************************************************************************
594 * The LZ decompression routine:
595 * This version decompress the given gif file into Line of length LineLen.
596 * This routine can be called few times (one per scan line, for example), in
597 * order the complete the whole image.
598 *****************************************************************************/
600 DGifDecompressLine(GifFileType
* GifFile
,
605 int j
, CrntCode
, EOFCode
, ClearCode
, CrntPrefix
, LastCode
, StackPtr
;
606 GifByteType
*Stack
, *Suffix
;
607 GifPrefixType
*Prefix
;
608 GifFilePrivateType
*Private
= (GifFilePrivateType
*) GifFile
->Private
;
610 StackPtr
= Private
->StackPtr
;
611 Prefix
= Private
->Prefix
;
612 Suffix
= Private
->Suffix
;
613 Stack
= Private
->Stack
;
614 EOFCode
= Private
->EOFCode
;
615 ClearCode
= Private
->ClearCode
;
616 LastCode
= Private
->LastCode
;
619 /* Let pop the stack off before continueing to read the gif file: */
620 while (StackPtr
!= 0 && i
< LineLen
)
621 Line
[i
++] = Stack
[--StackPtr
];
624 while (i
< LineLen
) { /* Decode LineLen items. */
625 if (DGifDecompressInput(GifFile
, &CrntCode
) == GIF_ERROR
)
628 if (CrntCode
== EOFCode
) {
629 /* Note however that usually we will not be here as we will stop
630 * decoding as soon as we got all the pixel, or EOF code will
631 * not be read at all, and DGifGetLine/Pixel clean everything. */
632 if (i
!= LineLen
- 1 || Private
->PixelCount
!= 0) {
636 } else if (CrntCode
== ClearCode
) {
637 /* We need to start over again: */
638 for (j
= 0; j
<= LZ_MAX_CODE
; j
++)
639 Prefix
[j
] = NO_SUCH_CODE
;
640 Private
->RunningCode
= Private
->EOFCode
+ 1;
641 Private
->RunningBits
= Private
->BitsPerPixel
+ 1;
642 Private
->MaxCode1
= 1 << Private
->RunningBits
;
643 LastCode
= Private
->LastCode
= NO_SUCH_CODE
;
645 /* It's a regular code - if in pixel range simply add it to output
646 * stream, otherwise trace to codes linked list until the prefix
647 * is in pixel range: */
648 if (CrntCode
< ClearCode
) {
649 /* This is simple - its pixel scalar, so add it to output: */
650 Line
[i
++] = CrntCode
;
652 /* It's a code to be traced: trace the linked list
653 * until the prefix is a pixel, while pushing the suffix
654 * pixels on our stack. If we done, pop the stack in reverse
655 * order (that's what stack is good for!) for output. */
656 if (Prefix
[CrntCode
] == NO_SUCH_CODE
) {
657 /* Only allowed if CrntCode is exactly the running code:
658 * In that case CrntCode = XXXCode, CrntCode or the
659 * prefix code is last code and the suffix char is
660 * exactly the prefix of last code! */
661 if (CrntCode
== Private
->RunningCode
- 2) {
662 CrntPrefix
= LastCode
;
663 Suffix
[Private
->RunningCode
- 2] =
664 Stack
[StackPtr
++] = DGifGetPrefixChar(Prefix
,
671 CrntPrefix
= CrntCode
;
673 /* Now (if image is O.K.) we should not get a NO_SUCH_CODE
674 * during the trace. As we might loop forever, in case of
675 * defective image, we count the number of loops we trace
676 * and stop if we got LZ_MAX_CODE. Obviously we cannot
677 * loop more than that. */
679 while (j
++ <= LZ_MAX_CODE
&&
680 CrntPrefix
> ClearCode
&& CrntPrefix
<= LZ_MAX_CODE
) {
681 Stack
[StackPtr
++] = Suffix
[CrntPrefix
];
682 CrntPrefix
= Prefix
[CrntPrefix
];
684 if (j
>= LZ_MAX_CODE
|| CrntPrefix
> LZ_MAX_CODE
) {
687 /* Push the last character on stack: */
688 Stack
[StackPtr
++] = CrntPrefix
;
690 /* Now lets pop all the stack into output: */
691 while (StackPtr
!= 0 && i
< LineLen
)
692 Line
[i
++] = Stack
[--StackPtr
];
694 if (LastCode
!= NO_SUCH_CODE
) {
695 Prefix
[Private
->RunningCode
- 2] = LastCode
;
697 if (CrntCode
== Private
->RunningCode
- 2) {
698 /* Only allowed if CrntCode is exactly the running code:
699 * In that case CrntCode = XXXCode, CrntCode or the
700 * prefix code is last code and the suffix char is
701 * exactly the prefix of last code! */
702 Suffix
[Private
->RunningCode
- 2] =
703 DGifGetPrefixChar(Prefix
, LastCode
, ClearCode
);
705 Suffix
[Private
->RunningCode
- 2] =
706 DGifGetPrefixChar(Prefix
, CrntCode
, ClearCode
);
713 Private
->LastCode
= LastCode
;
714 Private
->StackPtr
= StackPtr
;
719 /******************************************************************************
720 * Routine to trace the Prefixes linked list until we get a prefix which is
721 * not code, but a pixel value (less than ClearCode). Returns that pixel value.
722 * If image is defective, we might loop here forever, so we limit the loops to
723 * the maximum possible if image O.k. - LZ_MAX_CODE times.
724 *****************************************************************************/
726 DGifGetPrefixChar(const GifPrefixType
*Prefix
,
732 while (Code
> ClearCode
&& i
++ <= LZ_MAX_CODE
)
737 /******************************************************************************
738 * The LZ decompression input routine:
739 * This routine is responsible for the decompression of the bit stream from
740 * 8 bits (bytes) packets, into the real codes.
741 * Returns GIF_OK if read successfully.
742 *****************************************************************************/
744 DGifDecompressInput(GifFileType
* GifFile
,
747 GifFilePrivateType
*Private
= (GifFilePrivateType
*)GifFile
->Private
;
749 GifByteType NextByte
;
750 static const unsigned short CodeMasks
[] = {
751 0x0000, 0x0001, 0x0003, 0x0007,
752 0x000f, 0x001f, 0x003f, 0x007f,
753 0x00ff, 0x01ff, 0x03ff, 0x07ff,
756 /* The image can't contain more than LZ_BITS per code. */
757 if (Private
->RunningBits
> LZ_BITS
) {
761 while (Private
->CrntShiftState
< Private
->RunningBits
) {
762 /* Needs to get more bytes from input stream for next code: */
763 if (DGifBufferedInput(GifFile
, Private
->Buf
, &NextByte
) == GIF_ERROR
) {
766 Private
->CrntShiftDWord
|=
767 ((unsigned long)NextByte
) << Private
->CrntShiftState
;
768 Private
->CrntShiftState
+= 8;
770 *Code
= Private
->CrntShiftDWord
& CodeMasks
[Private
->RunningBits
];
772 Private
->CrntShiftDWord
>>= Private
->RunningBits
;
773 Private
->CrntShiftState
-= Private
->RunningBits
;
775 /* If code cannot fit into RunningBits bits, must raise its size. Note
776 * however that codes above 4095 are used for special signaling.
777 * If we're using LZ_BITS bits already and we're at the max code, just
778 * keep using the table as it is, don't increment Private->RunningCode.
780 if (Private
->RunningCode
< LZ_MAX_CODE
+ 2 &&
781 ++Private
->RunningCode
> Private
->MaxCode1
&&
782 Private
->RunningBits
< LZ_BITS
) {
783 Private
->MaxCode1
<<= 1;
784 Private
->RunningBits
++;
789 /******************************************************************************
790 * This routines read one gif data block at a time and buffers it internally
791 * so that the decompression routine could access it.
792 * The routine returns the next byte from its internal buffer (or read next
793 * block in if buffer empty) and returns GIF_OK if successful.
794 *****************************************************************************/
796 DGifBufferedInput(GifFileType
* GifFile
,
798 GifByteType
* NextByte
) {
801 /* Needs to read the next buffer - this one is empty: */
802 if (READ(GifFile
, Buf
, 1) != 1) {
805 /* There shouldn't be any empty data blocks here as the LZW spec
806 * says the LZW termination code should come first. Therefore we
807 * shouldn't be inside this routine at that point.
812 if (READ(GifFile
, &Buf
[1], Buf
[0]) != Buf
[0]) {
816 Buf
[1] = 2; /* We use now the second place as last char read! */
819 *NextByte
= Buf
[Buf
[1]++];
826 /******************************************************************************
827 * This routine reads an entire GIF into core, hanging all its state info off
828 * the GifFileType pointer. Call DGifOpenFileName() or DGifOpenFileHandle()
829 * first to initialize I/O. Its inverse is EGifSpew().
830 ******************************************************************************/
832 DGifSlurp(GifFileType
* GifFile
) {
835 GifRecordType RecordType
;
837 GifByteType
*ExtData
;
838 SavedImage temp_save
;
840 temp_save
.ExtensionBlocks
= NULL
;
841 temp_save
.ExtensionBlockCount
= 0;
844 if (DGifGetRecordType(GifFile
, &RecordType
) == GIF_ERROR
)
847 switch (RecordType
) {
848 case IMAGE_DESC_RECORD_TYPE
:
849 if (DGifGetImageDesc(GifFile
) == GIF_ERROR
)
852 sp
= &GifFile
->SavedImages
[GifFile
->ImageCount
- 1];
853 ImageSize
= sp
->ImageDesc
.Width
* sp
->ImageDesc
.Height
;
855 sp
->RasterBits
= ungif_alloc(ImageSize
* sizeof(GifPixelType
));
856 if (sp
->RasterBits
== NULL
) {
859 if (DGifGetLine(GifFile
, sp
->RasterBits
, ImageSize
) ==
862 if (temp_save
.ExtensionBlocks
) {
863 sp
->ExtensionBlocks
= temp_save
.ExtensionBlocks
;
864 sp
->ExtensionBlockCount
= temp_save
.ExtensionBlockCount
;
866 temp_save
.ExtensionBlocks
= NULL
;
867 temp_save
.ExtensionBlockCount
= 0;
869 /* FIXME: The following is wrong. It is left in only for
870 * backwards compatibility. Someday it should go away. Use
871 * the sp->ExtensionBlocks->Function variable instead. */
872 sp
->Function
= sp
->ExtensionBlocks
[0].Function
;
876 case EXTENSION_RECORD_TYPE
:
877 if (DGifGetExtension(GifFile
, &temp_save
.Function
, &ExtData
) ==
880 while (ExtData
!= NULL
) {
882 /* Create an extension block with our data */
883 if (AddExtensionBlock(&temp_save
, ExtData
[0], &ExtData
[1])
887 if (DGifGetExtensionNext(GifFile
, &ExtData
) == GIF_ERROR
)
889 temp_save
.Function
= 0;
893 case TERMINATE_RECORD_TYPE
:
896 default: /* Should be trapped by DGifGetRecordType */
899 } while (RecordType
!= TERMINATE_RECORD_TYPE
);
901 /* Just in case the Gif has an extension block without an associated
902 * image... (Should we save this into a savefile structure with no image
903 * instead? Have to check if the present writing code can handle that as
905 if (temp_save
.ExtensionBlocks
)
906 FreeExtension(&temp_save
);
911 /******************************************************************************
912 * GifFileType constructor with user supplied input function (TVT)
913 *****************************************************************************/
915 DGifOpen(void *userData
,
916 InputFunc readFunc
) {
918 unsigned char Buf
[GIF_STAMP_LEN
+ 1];
919 GifFileType
*GifFile
;
920 GifFilePrivateType
*Private
;
922 GifFile
= ungif_alloc(sizeof(GifFileType
));
923 if (GifFile
== NULL
) {
927 memset(GifFile
, '\0', sizeof(GifFileType
));
929 Private
= ungif_alloc(sizeof(GifFilePrivateType
));
935 GifFile
->Private
= (void*)Private
;
937 Private
->Read
= readFunc
; /* TVT */
938 GifFile
->UserData
= userData
; /* TVT */
940 /* Lets see if this is a GIF file: */
941 if (READ(GifFile
, Buf
, GIF_STAMP_LEN
) != GIF_STAMP_LEN
) {
947 /* The GIF Version number is ignored at this time. Maybe we should do
948 * something more useful with it. */
949 Buf
[GIF_STAMP_LEN
] = 0;
950 if (memcmp(GIF_STAMP
, Buf
, GIF_VERSION_POS
) != 0) {
956 if (DGifGetScreenDesc(GifFile
) == GIF_ERROR
) {
965 /******************************************************************************
966 * This routine should be called last, to close the GIF file.
967 *****************************************************************************/
969 DGifCloseFile(GifFileType
* GifFile
) {
971 GifFilePrivateType
*Private
;
976 Private
= (GifFilePrivateType
*) GifFile
->Private
;
978 if (GifFile
->Image
.ColorMap
) {
979 FreeMapObject(GifFile
->Image
.ColorMap
);
980 GifFile
->Image
.ColorMap
= NULL
;
983 if (GifFile
->SColorMap
) {
984 FreeMapObject(GifFile
->SColorMap
);
985 GifFile
->SColorMap
= NULL
;
991 if (GifFile
->SavedImages
) {
992 FreeSavedImages(GifFile
);
993 GifFile
->SavedImages
= NULL
;