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 *****************************************************************************/
59 static void *ungif_alloc( size_t sz
)
61 return HeapAlloc( GetProcessHeap(), 0, sz
);
64 static void *ungif_calloc( size_t num
, size_t sz
)
66 return HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, num
*sz
);
69 static void *ungif_realloc( void *ptr
, size_t sz
)
71 return HeapReAlloc( GetProcessHeap(), 0, ptr
, sz
);
74 static void ungif_free( void *ptr
)
76 HeapFree( GetProcessHeap(), 0, ptr
);
79 #define LZ_MAX_CODE 4095 /* Biggest code possible in 12 bits. */
82 #define NO_SUCH_CODE 4098 /* Impossible code, to signal empty. */
84 typedef struct GifFilePrivateType
{
85 GifWord BitsPerPixel
, /* Bits per pixel (Codes uses at least this + 1). */
86 ClearCode
, /* The CLEAR LZ code. */
87 EOFCode
, /* The EOF LZ code. */
88 RunningCode
, /* The next code algorithm can generate. */
89 RunningBits
, /* The number of bits required to represent RunningCode. */
90 MaxCode1
, /* 1 bigger than max. possible code, in RunningBits bits. */
91 LastCode
, /* The code before the current code. */
92 CrntCode
, /* Current algorithm code. */
93 StackPtr
, /* For character stack (see below). */
94 CrntShiftState
; /* Number of bits in CrntShiftDWord. */
95 unsigned long CrntShiftDWord
; /* For bytes decomposition into codes. */
96 unsigned long PixelCount
; /* Number of pixels in image. */
97 InputFunc Read
; /* function to read gif input (TVT) */
98 GifByteType Buf
[256]; /* Compressed input is buffered here. */
99 GifByteType Stack
[LZ_MAX_CODE
]; /* Decoded pixels are stacked here. */
100 GifByteType Suffix
[LZ_MAX_CODE
+ 1]; /* So we can trace the codes. */
101 GifPrefixType Prefix
[LZ_MAX_CODE
+ 1];
102 } GifFilePrivateType
;
104 /* avoid extra function call in case we use fread (TVT) */
105 #define READ(_gif,_buf,_len) \
106 ((GifFilePrivateType*)_gif->Private)->Read(_gif,_buf,_len)
108 static int DGifGetWord(GifFileType
*GifFile
, GifWord
*Word
);
109 static int DGifSetupDecompress(GifFileType
*GifFile
);
110 static int DGifDecompressLine(GifFileType
*GifFile
, GifPixelType
*Line
, int LineLen
);
111 static int DGifGetPrefixChar(const GifPrefixType
*Prefix
, int Code
, int ClearCode
);
112 static int DGifDecompressInput(GifFileType
*GifFile
, int *Code
);
113 static int DGifBufferedInput(GifFileType
*GifFile
, GifByteType
*Buf
,
114 GifByteType
*NextByte
);
116 static int DGifGetExtensionNext(GifFileType
* GifFile
, GifByteType
** GifExtension
);
117 static int DGifGetCodeNext(GifFileType
* GifFile
, GifByteType
** GifCodeBlock
);
119 /******************************************************************************
120 * Miscellaneous utility functions
121 *****************************************************************************/
123 /* return smallest bitfield size n will fit in */
129 for (i
= 1; i
<= 8; i
++)
135 /******************************************************************************
136 * Color map object functions
137 *****************************************************************************/
140 * Allocate a color map of given size; initialize with contents of
141 * ColorMap if that pointer is non-NULL.
143 static ColorMapObject
*
144 MakeMapObject(int ColorCount
,
145 const GifColorType
* ColorMap
) {
147 ColorMapObject
*Object
;
149 /*** FIXME: Our ColorCount has to be a power of two. Is it necessary to
150 * make the user know that or should we automatically round up instead? */
151 if (ColorCount
!= (1 << BitSize(ColorCount
))) {
155 Object
= ungif_alloc(sizeof(ColorMapObject
));
156 if (Object
== NULL
) {
160 Object
->Colors
= ungif_calloc(ColorCount
, sizeof(GifColorType
));
161 if (Object
->Colors
== NULL
) {
165 Object
->ColorCount
= ColorCount
;
166 Object
->BitsPerPixel
= BitSize(ColorCount
);
169 memcpy(Object
->Colors
, ColorMap
, ColorCount
* sizeof(GifColorType
));
176 * Free a color map object
179 FreeMapObject(ColorMapObject
* Object
) {
181 if (Object
!= NULL
) {
182 ungif_free(Object
->Colors
);
185 * When we are willing to break API we need to make this function
186 * FreeMapObject(ColorMapObject **Object)
187 * and do this assignment to NULL here:
194 AddExtensionBlock(SavedImage
* New
,
196 const unsigned char ExtData
[]) {
200 if (New
->ExtensionBlocks
== NULL
)
201 New
->ExtensionBlocks
= ungif_alloc(sizeof(ExtensionBlock
));
203 New
->ExtensionBlocks
= ungif_realloc(New
->ExtensionBlocks
,
204 sizeof(ExtensionBlock
) *
205 (New
->ExtensionBlockCount
+ 1));
207 if (New
->ExtensionBlocks
== NULL
)
210 ep
= &New
->ExtensionBlocks
[New
->ExtensionBlockCount
++];
213 ep
->Bytes
= ungif_alloc(ep
->ByteCount
);
214 if (ep
->Bytes
== NULL
)
218 memcpy(ep
->Bytes
, ExtData
, Len
);
219 ep
->Function
= New
->Function
;
226 FreeExtension(SavedImage
* Image
)
230 if ((Image
== NULL
) || (Image
->ExtensionBlocks
== NULL
)) {
233 for (ep
= Image
->ExtensionBlocks
;
234 ep
< (Image
->ExtensionBlocks
+ Image
->ExtensionBlockCount
); ep
++)
235 ungif_free(ep
->Bytes
);
236 ungif_free(Image
->ExtensionBlocks
);
237 Image
->ExtensionBlocks
= NULL
;
240 /******************************************************************************
241 * Image block allocation functions
242 ******************************************************************************/
245 FreeSavedImages(GifFileType
* GifFile
) {
249 if ((GifFile
== NULL
) || (GifFile
->SavedImages
== NULL
)) {
252 for (sp
= GifFile
->SavedImages
;
253 sp
< GifFile
->SavedImages
+ GifFile
->ImageCount
; sp
++) {
254 if (sp
->ImageDesc
.ColorMap
) {
255 FreeMapObject(sp
->ImageDesc
.ColorMap
);
256 sp
->ImageDesc
.ColorMap
= NULL
;
259 ungif_free(sp
->RasterBits
);
261 if (sp
->ExtensionBlocks
)
264 ungif_free(GifFile
->SavedImages
);
265 GifFile
->SavedImages
=NULL
;
268 /******************************************************************************
269 * This routine should be called before any other DGif calls. Note that
270 * this routine is called automatically from DGif file open routines.
271 *****************************************************************************/
273 DGifGetScreenDesc(GifFileType
* GifFile
) {
278 /* Put the screen descriptor into the file: */
279 if (DGifGetWord(GifFile
, &GifFile
->SWidth
) == GIF_ERROR
||
280 DGifGetWord(GifFile
, &GifFile
->SHeight
) == GIF_ERROR
)
283 if (READ(GifFile
, Buf
, 3) != 3) {
286 GifFile
->SColorResolution
= (((Buf
[0] & 0x70) + 1) >> 4) + 1;
287 BitsPerPixel
= (Buf
[0] & 0x07) + 1;
288 GifFile
->SBackGroundColor
= Buf
[1];
289 GifFile
->SAspectRatio
= Buf
[2];
290 if (Buf
[0] & 0x80) { /* Do we have global color map? */
292 GifFile
->SColorMap
= MakeMapObject(1 << BitsPerPixel
, NULL
);
293 if (GifFile
->SColorMap
== NULL
) {
297 /* Get the global color map: */
298 for (i
= 0; i
< GifFile
->SColorMap
->ColorCount
; i
++) {
299 if (READ(GifFile
, Buf
, 3) != 3) {
300 FreeMapObject(GifFile
->SColorMap
);
301 GifFile
->SColorMap
= NULL
;
304 GifFile
->SColorMap
->Colors
[i
].Red
= Buf
[0];
305 GifFile
->SColorMap
->Colors
[i
].Green
= Buf
[1];
306 GifFile
->SColorMap
->Colors
[i
].Blue
= Buf
[2];
309 GifFile
->SColorMap
= NULL
;
315 /******************************************************************************
316 * This routine should be called before any attempt to read an image.
317 *****************************************************************************/
319 DGifGetRecordType(GifFileType
* GifFile
,
320 GifRecordType
* Type
) {
324 if (READ(GifFile
, &Buf
, 1) != 1) {
325 /* Wine-specific behavior: Native accepts broken GIF files that have no
326 * terminator, so we match this by treating EOF as a terminator. */
327 *Type
= TERMINATE_RECORD_TYPE
;
333 *Type
= IMAGE_DESC_RECORD_TYPE
;
336 *Type
= EXTENSION_RECORD_TYPE
;
339 *Type
= TERMINATE_RECORD_TYPE
;
342 *Type
= UNDEFINED_RECORD_TYPE
;
349 /******************************************************************************
350 * This routine should be called before any attempt to read an image.
351 * Note it is assumed the Image desc. header (',') has been read.
352 *****************************************************************************/
354 DGifGetImageDesc(GifFileType
* GifFile
) {
358 GifFilePrivateType
*Private
= GifFile
->Private
;
361 if (DGifGetWord(GifFile
, &GifFile
->Image
.Left
) == GIF_ERROR
||
362 DGifGetWord(GifFile
, &GifFile
->Image
.Top
) == GIF_ERROR
||
363 DGifGetWord(GifFile
, &GifFile
->Image
.Width
) == GIF_ERROR
||
364 DGifGetWord(GifFile
, &GifFile
->Image
.Height
) == GIF_ERROR
)
366 if (READ(GifFile
, Buf
, 1) != 1) {
369 BitsPerPixel
= (Buf
[0] & 0x07) + 1;
370 GifFile
->Image
.Interlace
= (Buf
[0] & 0x40);
371 if (Buf
[0] & 0x80) { /* Does this image have local color map? */
373 /*** FIXME: Why do we check both of these in order to do this?
374 * Why do we have both Image and SavedImages? */
375 if (GifFile
->Image
.ColorMap
&& GifFile
->SavedImages
== NULL
)
376 FreeMapObject(GifFile
->Image
.ColorMap
);
378 GifFile
->Image
.ColorMap
= MakeMapObject(1 << BitsPerPixel
, NULL
);
379 if (GifFile
->Image
.ColorMap
== NULL
) {
383 /* Get the image local color map: */
384 for (i
= 0; i
< GifFile
->Image
.ColorMap
->ColorCount
; i
++) {
385 if (READ(GifFile
, Buf
, 3) != 3) {
386 FreeMapObject(GifFile
->Image
.ColorMap
);
387 GifFile
->Image
.ColorMap
= NULL
;
390 GifFile
->Image
.ColorMap
->Colors
[i
].Red
= Buf
[0];
391 GifFile
->Image
.ColorMap
->Colors
[i
].Green
= Buf
[1];
392 GifFile
->Image
.ColorMap
->Colors
[i
].Blue
= Buf
[2];
394 } else if (GifFile
->Image
.ColorMap
) {
395 FreeMapObject(GifFile
->Image
.ColorMap
);
396 GifFile
->Image
.ColorMap
= NULL
;
399 if (GifFile
->SavedImages
) {
400 if ((GifFile
->SavedImages
= ungif_realloc(GifFile
->SavedImages
,
402 (GifFile
->ImageCount
+ 1))) == NULL
) {
406 if ((GifFile
->SavedImages
= ungif_alloc(sizeof(SavedImage
))) == NULL
) {
411 sp
= &GifFile
->SavedImages
[GifFile
->ImageCount
];
412 sp
->ImageDesc
= GifFile
->Image
;
413 if (GifFile
->Image
.ColorMap
!= NULL
) {
414 sp
->ImageDesc
.ColorMap
= MakeMapObject(
415 GifFile
->Image
.ColorMap
->ColorCount
,
416 GifFile
->Image
.ColorMap
->Colors
);
417 if (sp
->ImageDesc
.ColorMap
== NULL
) {
421 sp
->RasterBits
= NULL
;
422 sp
->ExtensionBlockCount
= 0;
423 sp
->ExtensionBlocks
= NULL
;
425 GifFile
->ImageCount
++;
427 Private
->PixelCount
= (long)GifFile
->Image
.Width
*
428 (long)GifFile
->Image
.Height
;
430 DGifSetupDecompress(GifFile
); /* Reset decompress algorithm parameters. */
435 /******************************************************************************
436 * Get one full scanned line (Line) of length LineLen from GIF file.
437 *****************************************************************************/
439 DGifGetLine(GifFileType
* GifFile
,
444 GifFilePrivateType
*Private
= GifFile
->Private
;
447 LineLen
= GifFile
->Image
.Width
;
449 if ((Private
->PixelCount
-= LineLen
) > 0xffff0000UL
) {
453 if (DGifDecompressLine(GifFile
, Line
, LineLen
) == GIF_OK
) {
454 if (Private
->PixelCount
== 0) {
455 /* We probably would not be called any more, so lets clean
456 * everything before we return: need to flush out all rest of
457 * image until empty block (size 0) detected. We use GetCodeNext. */
459 if (DGifGetCodeNext(GifFile
, &Dummy
) == GIF_ERROR
)
461 while (Dummy
!= NULL
) ;
468 /******************************************************************************
469 * Get an extension block (see GIF manual) from gif file. This routine only
470 * returns the first data block, and DGifGetExtensionNext should be called
471 * after this one until NULL extension is returned.
472 * The Extension should NOT be freed by the user (not dynamically allocated).
473 * Note it is assumed the Extension desc. header ('!') has been read.
474 *****************************************************************************/
476 DGifGetExtension(GifFileType
* GifFile
,
478 GifByteType
** Extension
) {
482 if (READ(GifFile
, &Buf
, 1) != 1) {
487 return DGifGetExtensionNext(GifFile
, Extension
);
490 /******************************************************************************
491 * Get a following extension block (see GIF manual) from gif file. This
492 * routine should be called until NULL Extension is returned.
493 * The Extension should NOT be freed by the user (not dynamically allocated).
494 *****************************************************************************/
496 DGifGetExtensionNext(GifFileType
* GifFile
,
497 GifByteType
** Extension
) {
500 GifFilePrivateType
*Private
= GifFile
->Private
;
502 if (READ(GifFile
, &Buf
, 1) != 1) {
506 *Extension
= Private
->Buf
; /* Use private unused buffer. */
507 (*Extension
)[0] = Buf
; /* Pascal strings notation (pos. 0 is len.). */
508 if (READ(GifFile
, &((*Extension
)[1]), Buf
) != Buf
) {
517 /******************************************************************************
518 * Get 2 bytes (word) from the given file:
519 *****************************************************************************/
521 DGifGetWord(GifFileType
* GifFile
,
526 if (READ(GifFile
, c
, 2) != 2) {
530 *Word
= (((unsigned int)c
[1]) << 8) + c
[0];
534 /******************************************************************************
535 * Continue to get the image code in compressed form. This routine should be
536 * called until NULL block is returned.
537 * The block should NOT be freed by the user (not dynamically allocated).
538 *****************************************************************************/
540 DGifGetCodeNext(GifFileType
* GifFile
,
541 GifByteType
** CodeBlock
) {
544 GifFilePrivateType
*Private
= GifFile
->Private
;
546 if (READ(GifFile
, &Buf
, 1) != 1) {
551 *CodeBlock
= Private
->Buf
; /* Use private unused buffer. */
552 (*CodeBlock
)[0] = Buf
; /* Pascal strings notation (pos. 0 is len.). */
553 if (READ(GifFile
, &((*CodeBlock
)[1]), Buf
) != Buf
) {
558 Private
->Buf
[0] = 0; /* Make sure the buffer is empty! */
559 Private
->PixelCount
= 0; /* And local info. indicate image read. */
565 /******************************************************************************
566 * Setup the LZ decompression for this image:
567 *****************************************************************************/
569 DGifSetupDecompress(GifFileType
* GifFile
) {
572 GifByteType CodeSize
;
573 GifPrefixType
*Prefix
;
574 GifFilePrivateType
*Private
= GifFile
->Private
;
576 READ(GifFile
, &CodeSize
, 1); /* Read Code size from file. */
577 BitsPerPixel
= CodeSize
;
579 Private
->Buf
[0] = 0; /* Input Buffer empty. */
580 Private
->BitsPerPixel
= BitsPerPixel
;
581 Private
->ClearCode
= (1 << BitsPerPixel
);
582 Private
->EOFCode
= Private
->ClearCode
+ 1;
583 Private
->RunningCode
= Private
->EOFCode
+ 1;
584 Private
->RunningBits
= BitsPerPixel
+ 1; /* Number of bits per code. */
585 Private
->MaxCode1
= 1 << Private
->RunningBits
; /* Max. code + 1. */
586 Private
->StackPtr
= 0; /* No pixels on the pixel stack. */
587 Private
->LastCode
= NO_SUCH_CODE
;
588 Private
->CrntShiftState
= 0; /* No information in CrntShiftDWord. */
589 Private
->CrntShiftDWord
= 0;
591 Prefix
= Private
->Prefix
;
592 for (i
= 0; i
<= LZ_MAX_CODE
; i
++)
593 Prefix
[i
] = NO_SUCH_CODE
;
598 /******************************************************************************
599 * The LZ decompression routine:
600 * This version decompress the given gif file into Line of length LineLen.
601 * This routine can be called few times (one per scan line, for example), in
602 * order the complete the whole image.
603 *****************************************************************************/
605 DGifDecompressLine(GifFileType
* GifFile
,
610 int j
, CrntCode
, EOFCode
, ClearCode
, CrntPrefix
, LastCode
, StackPtr
;
611 GifByteType
*Stack
, *Suffix
;
612 GifPrefixType
*Prefix
;
613 GifFilePrivateType
*Private
= GifFile
->Private
;
615 StackPtr
= Private
->StackPtr
;
616 Prefix
= Private
->Prefix
;
617 Suffix
= Private
->Suffix
;
618 Stack
= Private
->Stack
;
619 EOFCode
= Private
->EOFCode
;
620 ClearCode
= Private
->ClearCode
;
621 LastCode
= Private
->LastCode
;
624 /* Let pop the stack off before continuing to read the gif file: */
625 while (StackPtr
!= 0 && i
< LineLen
)
626 Line
[i
++] = Stack
[--StackPtr
];
629 while (i
< LineLen
) { /* Decode LineLen items. */
630 if (DGifDecompressInput(GifFile
, &CrntCode
) == GIF_ERROR
)
633 if (CrntCode
== EOFCode
) {
634 /* Note, however, that usually we will not be here as we will stop
635 * decoding as soon as we got all the pixel, or EOF code will
636 * not be read at all, and DGifGetLine/Pixel clean everything. */
637 if (i
!= LineLen
- 1 || Private
->PixelCount
!= 0) {
641 } else if (CrntCode
== ClearCode
) {
642 /* We need to start over again: */
643 for (j
= 0; j
<= LZ_MAX_CODE
; j
++)
644 Prefix
[j
] = NO_SUCH_CODE
;
645 Private
->RunningCode
= Private
->EOFCode
+ 1;
646 Private
->RunningBits
= Private
->BitsPerPixel
+ 1;
647 Private
->MaxCode1
= 1 << Private
->RunningBits
;
648 LastCode
= Private
->LastCode
= NO_SUCH_CODE
;
650 /* It's a regular code - if in pixel range simply add it to output
651 * stream, otherwise trace to codes linked list until the prefix
652 * is in pixel range: */
653 if (CrntCode
< ClearCode
) {
654 /* This is simple - its pixel scalar, so add it to output: */
655 Line
[i
++] = CrntCode
;
657 /* It's a code to be traced: trace the linked list
658 * until the prefix is a pixel, while pushing the suffix
659 * pixels on our stack. If we done, pop the stack in reverse
660 * order (that's what stack is good for!) for output. */
661 if (Prefix
[CrntCode
] == NO_SUCH_CODE
) {
662 /* Only allowed if CrntCode is exactly the running code:
663 * In that case CrntCode = XXXCode, CrntCode or the
664 * prefix code is last code and the suffix char is
665 * exactly the prefix of last code! */
666 if (CrntCode
== Private
->RunningCode
- 2) {
667 CrntPrefix
= LastCode
;
668 Suffix
[Private
->RunningCode
- 2] =
669 Stack
[StackPtr
++] = DGifGetPrefixChar(Prefix
,
676 CrntPrefix
= CrntCode
;
678 /* Now (if image is O.K.) we should not get a NO_SUCH_CODE
679 * during the trace. As we might loop forever, in case of
680 * defective image, we count the number of loops we trace
681 * and stop if we got LZ_MAX_CODE. Obviously we cannot
682 * loop more than that. */
684 while (j
++ <= LZ_MAX_CODE
&&
685 CrntPrefix
> ClearCode
&& CrntPrefix
<= LZ_MAX_CODE
) {
686 Stack
[StackPtr
++] = Suffix
[CrntPrefix
];
687 CrntPrefix
= Prefix
[CrntPrefix
];
689 if (j
>= LZ_MAX_CODE
|| CrntPrefix
> LZ_MAX_CODE
) {
692 /* Push the last character on stack: */
693 Stack
[StackPtr
++] = CrntPrefix
;
695 /* Now lets pop all the stack into output: */
696 while (StackPtr
!= 0 && i
< LineLen
)
697 Line
[i
++] = Stack
[--StackPtr
];
699 if (LastCode
!= NO_SUCH_CODE
) {
700 Prefix
[Private
->RunningCode
- 2] = LastCode
;
702 if (CrntCode
== Private
->RunningCode
- 2) {
703 /* Only allowed if CrntCode is exactly the running code:
704 * In that case CrntCode = XXXCode, CrntCode or the
705 * prefix code is last code and the suffix char is
706 * exactly the prefix of last code! */
707 Suffix
[Private
->RunningCode
- 2] =
708 DGifGetPrefixChar(Prefix
, LastCode
, ClearCode
);
710 Suffix
[Private
->RunningCode
- 2] =
711 DGifGetPrefixChar(Prefix
, CrntCode
, ClearCode
);
718 Private
->LastCode
= LastCode
;
719 Private
->StackPtr
= StackPtr
;
724 /******************************************************************************
725 * Routine to trace the Prefixes linked list until we get a prefix which is
726 * not code, but a pixel value (less than ClearCode). Returns that pixel value.
727 * If image is defective, we might loop here forever, so we limit the loops to
728 * the maximum possible if image O.k. - LZ_MAX_CODE times.
729 *****************************************************************************/
731 DGifGetPrefixChar(const GifPrefixType
*Prefix
,
737 while (Code
> ClearCode
&& i
++ <= LZ_MAX_CODE
)
742 /******************************************************************************
743 * The LZ decompression input routine:
744 * This routine is responsible for the decompression of the bit stream from
745 * 8 bits (bytes) packets, into the real codes.
746 * Returns GIF_OK if read successfully.
747 *****************************************************************************/
749 DGifDecompressInput(GifFileType
* GifFile
,
752 GifFilePrivateType
*Private
= GifFile
->Private
;
754 GifByteType NextByte
;
755 static const unsigned short CodeMasks
[] = {
756 0x0000, 0x0001, 0x0003, 0x0007,
757 0x000f, 0x001f, 0x003f, 0x007f,
758 0x00ff, 0x01ff, 0x03ff, 0x07ff,
761 /* The image can't contain more than LZ_BITS per code. */
762 if (Private
->RunningBits
> LZ_BITS
) {
766 while (Private
->CrntShiftState
< Private
->RunningBits
) {
767 /* Needs to get more bytes from input stream for next code: */
768 if (DGifBufferedInput(GifFile
, Private
->Buf
, &NextByte
) == GIF_ERROR
) {
771 Private
->CrntShiftDWord
|=
772 ((unsigned long)NextByte
) << Private
->CrntShiftState
;
773 Private
->CrntShiftState
+= 8;
775 *Code
= Private
->CrntShiftDWord
& CodeMasks
[Private
->RunningBits
];
777 Private
->CrntShiftDWord
>>= Private
->RunningBits
;
778 Private
->CrntShiftState
-= Private
->RunningBits
;
780 /* If code cannot fit into RunningBits bits, must raise its size. Note
781 * however that codes above 4095 are used for special signaling.
782 * If we're using LZ_BITS bits already and we're at the max code, just
783 * keep using the table as it is, don't increment Private->RunningCode.
785 if (Private
->RunningCode
< LZ_MAX_CODE
+ 2 &&
786 ++Private
->RunningCode
> Private
->MaxCode1
&&
787 Private
->RunningBits
< LZ_BITS
) {
788 Private
->MaxCode1
<<= 1;
789 Private
->RunningBits
++;
794 /******************************************************************************
795 * This routines read one gif data block at a time and buffers it internally
796 * so that the decompression routine could access it.
797 * The routine returns the next byte from its internal buffer (or read next
798 * block in if buffer empty) and returns GIF_OK if successful.
799 *****************************************************************************/
801 DGifBufferedInput(GifFileType
* GifFile
,
803 GifByteType
* NextByte
) {
806 /* Needs to read the next buffer - this one is empty: */
807 if (READ(GifFile
, Buf
, 1) != 1) {
810 /* There shouldn't be any empty data blocks here as the LZW spec
811 * says the LZW termination code should come first. Therefore we
812 * shouldn't be inside this routine at that point.
817 if (READ(GifFile
, &Buf
[1], Buf
[0]) != Buf
[0]) {
821 Buf
[1] = 2; /* We use now the second place as last char read! */
824 *NextByte
= Buf
[Buf
[1]++];
831 /******************************************************************************
832 * This routine reads an entire GIF into core, hanging all its state info off
833 * the GifFileType pointer. Call DGifOpenFileName() or DGifOpenFileHandle()
834 * first to initialize I/O. Its inverse is EGifSpew().
835 ******************************************************************************/
837 DGifSlurp(GifFileType
* GifFile
) {
840 GifRecordType RecordType
;
842 GifByteType
*ExtData
;
843 SavedImage temp_save
;
845 temp_save
.ExtensionBlocks
= NULL
;
846 temp_save
.ExtensionBlockCount
= 0;
849 if (DGifGetRecordType(GifFile
, &RecordType
) == GIF_ERROR
)
852 switch (RecordType
) {
853 case IMAGE_DESC_RECORD_TYPE
:
854 if (DGifGetImageDesc(GifFile
) == GIF_ERROR
)
857 sp
= &GifFile
->SavedImages
[GifFile
->ImageCount
- 1];
858 ImageSize
= sp
->ImageDesc
.Width
* sp
->ImageDesc
.Height
;
860 sp
->RasterBits
= ungif_alloc(ImageSize
* sizeof(GifPixelType
));
861 if (sp
->RasterBits
== NULL
) {
864 if (DGifGetLine(GifFile
, sp
->RasterBits
, ImageSize
) ==
867 if (temp_save
.ExtensionBlocks
) {
868 sp
->ExtensionBlocks
= temp_save
.ExtensionBlocks
;
869 sp
->ExtensionBlockCount
= temp_save
.ExtensionBlockCount
;
871 temp_save
.ExtensionBlocks
= NULL
;
872 temp_save
.ExtensionBlockCount
= 0;
874 /* FIXME: The following is wrong. It is left in only for
875 * backwards compatibility. Someday it should go away. Use
876 * the sp->ExtensionBlocks->Function variable instead. */
877 sp
->Function
= sp
->ExtensionBlocks
[0].Function
;
881 case EXTENSION_RECORD_TYPE
:
882 if (DGifGetExtension(GifFile
, &temp_save
.Function
, &ExtData
) ==
885 while (ExtData
!= NULL
) {
887 /* Create an extension block with our data */
888 if (AddExtensionBlock(&temp_save
, ExtData
[0], &ExtData
[1])
892 if (DGifGetExtensionNext(GifFile
, &ExtData
) == GIF_ERROR
)
894 temp_save
.Function
= 0;
898 case TERMINATE_RECORD_TYPE
:
901 default: /* Should be trapped by DGifGetRecordType */
904 } while (RecordType
!= TERMINATE_RECORD_TYPE
);
906 /* Just in case the Gif has an extension block without an associated
907 * image... (Should we save this into a savefile structure with no image
908 * instead? Have to check if the present writing code can handle that as
910 if (temp_save
.ExtensionBlocks
)
911 FreeExtension(&temp_save
);
916 /******************************************************************************
917 * GifFileType constructor with user supplied input function (TVT)
918 *****************************************************************************/
920 DGifOpen(void *userData
,
921 InputFunc readFunc
) {
923 unsigned char Buf
[GIF_STAMP_LEN
+ 1];
924 GifFileType
*GifFile
;
925 GifFilePrivateType
*Private
;
927 GifFile
= ungif_alloc(sizeof(GifFileType
));
928 if (GifFile
== NULL
) {
932 memset(GifFile
, '\0', sizeof(GifFileType
));
934 Private
= ungif_alloc(sizeof(GifFilePrivateType
));
940 GifFile
->Private
= (void*)Private
;
942 Private
->Read
= readFunc
; /* TVT */
943 GifFile
->UserData
= userData
; /* TVT */
945 /* Lets see if this is a GIF file: */
946 if (READ(GifFile
, Buf
, GIF_STAMP_LEN
) != GIF_STAMP_LEN
) {
952 /* The GIF Version number is ignored at this time. Maybe we should do
953 * something more useful with it. */
954 Buf
[GIF_STAMP_LEN
] = 0;
955 if (memcmp(GIF_STAMP
, Buf
, GIF_VERSION_POS
) != 0) {
961 if (DGifGetScreenDesc(GifFile
) == GIF_ERROR
) {
970 /******************************************************************************
971 * This routine should be called last, to close the GIF file.
972 *****************************************************************************/
974 DGifCloseFile(GifFileType
* GifFile
) {
976 GifFilePrivateType
*Private
;
981 Private
= GifFile
->Private
;
983 if (GifFile
->Image
.ColorMap
) {
984 FreeMapObject(GifFile
->Image
.ColorMap
);
985 GifFile
->Image
.ColorMap
= NULL
;
988 if (GifFile
->SColorMap
) {
989 FreeMapObject(GifFile
->SColorMap
);
990 GifFile
->SColorMap
= NULL
;
996 if (GifFile
->SavedImages
) {
997 FreeSavedImages(GifFile
);
998 GifFile
->SavedImages
= NULL
;
1001 ungif_free(GifFile
);