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
) {
166 Object
->ColorCount
= ColorCount
;
167 Object
->BitsPerPixel
= BitSize(ColorCount
);
170 memcpy(Object
->Colors
, ColorMap
, ColorCount
* sizeof(GifColorType
));
177 * Free a color map object
180 FreeMapObject(ColorMapObject
* Object
) {
182 if (Object
!= NULL
) {
183 ungif_free(Object
->Colors
);
186 * When we are willing to break API we need to make this function
187 * FreeMapObject(ColorMapObject **Object)
188 * and do this assignment to NULL here:
195 AddExtensionBlock(Extensions
*New
,
197 const unsigned char ExtData
[]) {
201 if (New
->ExtensionBlocks
== NULL
)
202 New
->ExtensionBlocks
= ungif_alloc(sizeof(ExtensionBlock
));
204 New
->ExtensionBlocks
= ungif_realloc(New
->ExtensionBlocks
,
205 sizeof(ExtensionBlock
) *
206 (New
->ExtensionBlockCount
+ 1));
208 if (New
->ExtensionBlocks
== NULL
)
211 ep
= &New
->ExtensionBlocks
[New
->ExtensionBlockCount
++];
213 ep
->ByteCount
=Len
+ 3;
214 ep
->Bytes
= ungif_alloc(ep
->ByteCount
+ 3);
215 if (ep
->Bytes
== NULL
)
218 /* Extension Header */
220 ep
->Bytes
[1] = New
->Function
;
224 memcpy(ep
->Bytes
+ 3, ExtData
, Len
);
225 ep
->Function
= New
->Function
;
232 AppendExtensionBlock(Extensions
*New
,
234 const unsigned char ExtData
[])
238 if (New
->ExtensionBlocks
== NULL
)
241 ep
= &New
->ExtensionBlocks
[New
->ExtensionBlockCount
- 1];
243 ep
->Bytes
= ungif_realloc(ep
->Bytes
, ep
->ByteCount
+ Len
+ 1);
244 if (ep
->Bytes
== NULL
)
247 ep
->Bytes
[ep
->ByteCount
] = Len
;
250 memcpy(ep
->Bytes
+ ep
->ByteCount
+ 1, ExtData
, Len
);
252 ep
->ByteCount
+= Len
+ 1;
258 FreeExtension(Extensions
*Extensions
)
262 if ((Extensions
== NULL
) || (Extensions
->ExtensionBlocks
== NULL
)) {
265 for (ep
= Extensions
->ExtensionBlocks
;
266 ep
< (Extensions
->ExtensionBlocks
+ Extensions
->ExtensionBlockCount
); ep
++)
267 ungif_free(ep
->Bytes
);
268 ungif_free(Extensions
->ExtensionBlocks
);
269 Extensions
->ExtensionBlocks
= NULL
;
272 /******************************************************************************
273 * Image block allocation functions
274 ******************************************************************************/
277 FreeSavedImages(GifFileType
* GifFile
) {
281 if ((GifFile
== NULL
) || (GifFile
->SavedImages
== NULL
)) {
284 for (sp
= GifFile
->SavedImages
;
285 sp
< GifFile
->SavedImages
+ GifFile
->ImageCount
; sp
++) {
286 if (sp
->ImageDesc
.ColorMap
) {
287 FreeMapObject(sp
->ImageDesc
.ColorMap
);
288 sp
->ImageDesc
.ColorMap
= NULL
;
291 ungif_free(sp
->RasterBits
);
293 if (sp
->Extensions
.ExtensionBlocks
)
294 FreeExtension(&sp
->Extensions
);
296 ungif_free(GifFile
->SavedImages
);
297 GifFile
->SavedImages
=NULL
;
300 /******************************************************************************
301 * This routine should be called before any other DGif calls. Note that
302 * this routine is called automatically from DGif file open routines.
303 *****************************************************************************/
305 DGifGetScreenDesc(GifFileType
* GifFile
) {
307 int i
, BitsPerPixel
, SortFlag
;
310 /* Put the screen descriptor into the file: */
311 if (DGifGetWord(GifFile
, &GifFile
->SWidth
) == GIF_ERROR
||
312 DGifGetWord(GifFile
, &GifFile
->SHeight
) == GIF_ERROR
)
315 if (READ(GifFile
, Buf
, 3) != 3) {
318 GifFile
->SColorResolution
= (((Buf
[0] & 0x70) + 1) >> 4) + 1;
319 SortFlag
= (Buf
[0] & 0x08) != 0;
320 BitsPerPixel
= (Buf
[0] & 0x07) + 1;
321 GifFile
->SBackGroundColor
= Buf
[1];
322 GifFile
->SAspectRatio
= Buf
[2];
323 if (Buf
[0] & 0x80) { /* Do we have global color map? */
325 GifFile
->SColorMap
= MakeMapObject(1 << BitsPerPixel
, NULL
);
326 if (GifFile
->SColorMap
== NULL
) {
330 /* Get the global color map: */
331 GifFile
->SColorMap
->SortFlag
= SortFlag
;
332 for (i
= 0; i
< GifFile
->SColorMap
->ColorCount
; i
++) {
333 if (READ(GifFile
, Buf
, 3) != 3) {
334 FreeMapObject(GifFile
->SColorMap
);
335 GifFile
->SColorMap
= NULL
;
338 GifFile
->SColorMap
->Colors
[i
].Red
= Buf
[0];
339 GifFile
->SColorMap
->Colors
[i
].Green
= Buf
[1];
340 GifFile
->SColorMap
->Colors
[i
].Blue
= Buf
[2];
343 GifFile
->SColorMap
= NULL
;
349 /******************************************************************************
350 * This routine should be called before any attempt to read an image.
351 *****************************************************************************/
353 DGifGetRecordType(GifFileType
* GifFile
,
354 GifRecordType
* Type
) {
358 if (READ(GifFile
, &Buf
, 1) != 1) {
359 /* Wine-specific behavior: Native accepts broken GIF files that have no
360 * terminator, so we match this by treating EOF as a terminator. */
361 *Type
= TERMINATE_RECORD_TYPE
;
367 *Type
= IMAGE_DESC_RECORD_TYPE
;
370 *Type
= EXTENSION_RECORD_TYPE
;
373 *Type
= TERMINATE_RECORD_TYPE
;
376 *Type
= UNDEFINED_RECORD_TYPE
;
383 /******************************************************************************
384 * This routine should be called before any attempt to read an image.
385 * Note it is assumed the Image desc. header (',') has been read.
386 *****************************************************************************/
388 DGifGetImageDesc(GifFileType
* GifFile
) {
390 int i
, BitsPerPixel
, SortFlag
;
392 GifFilePrivateType
*Private
= GifFile
->Private
;
395 if (DGifGetWord(GifFile
, &GifFile
->Image
.Left
) == GIF_ERROR
||
396 DGifGetWord(GifFile
, &GifFile
->Image
.Top
) == GIF_ERROR
||
397 DGifGetWord(GifFile
, &GifFile
->Image
.Width
) == GIF_ERROR
||
398 DGifGetWord(GifFile
, &GifFile
->Image
.Height
) == GIF_ERROR
)
400 if (READ(GifFile
, Buf
, 1) != 1) {
403 BitsPerPixel
= (Buf
[0] & 0x07) + 1;
404 SortFlag
= (Buf
[0] & 0x20) != 0;
405 GifFile
->Image
.Interlace
= (Buf
[0] & 0x40);
406 if (Buf
[0] & 0x80) { /* Does this image have local color map? */
408 FreeMapObject(GifFile
->Image
.ColorMap
);
410 GifFile
->Image
.ColorMap
= MakeMapObject(1 << BitsPerPixel
, NULL
);
411 if (GifFile
->Image
.ColorMap
== NULL
) {
415 /* Get the image local color map: */
416 GifFile
->Image
.ColorMap
->SortFlag
= SortFlag
;
417 for (i
= 0; i
< GifFile
->Image
.ColorMap
->ColorCount
; i
++) {
418 if (READ(GifFile
, Buf
, 3) != 3) {
419 FreeMapObject(GifFile
->Image
.ColorMap
);
420 GifFile
->Image
.ColorMap
= NULL
;
423 GifFile
->Image
.ColorMap
->Colors
[i
].Red
= Buf
[0];
424 GifFile
->Image
.ColorMap
->Colors
[i
].Green
= Buf
[1];
425 GifFile
->Image
.ColorMap
->Colors
[i
].Blue
= Buf
[2];
427 } else if (GifFile
->Image
.ColorMap
) {
428 FreeMapObject(GifFile
->Image
.ColorMap
);
429 GifFile
->Image
.ColorMap
= NULL
;
432 if (GifFile
->SavedImages
) {
433 if ((GifFile
->SavedImages
= ungif_realloc(GifFile
->SavedImages
,
435 (GifFile
->ImageCount
+ 1))) == NULL
) {
439 if ((GifFile
->SavedImages
= ungif_alloc(sizeof(SavedImage
))) == NULL
) {
444 sp
= &GifFile
->SavedImages
[GifFile
->ImageCount
];
445 sp
->ImageDesc
= GifFile
->Image
;
446 if (GifFile
->Image
.ColorMap
!= NULL
) {
447 sp
->ImageDesc
.ColorMap
= MakeMapObject(
448 GifFile
->Image
.ColorMap
->ColorCount
,
449 GifFile
->Image
.ColorMap
->Colors
);
450 if (sp
->ImageDesc
.ColorMap
== NULL
) {
453 sp
->ImageDesc
.ColorMap
->SortFlag
= GifFile
->Image
.ColorMap
->SortFlag
;
455 sp
->RasterBits
= NULL
;
456 sp
->Extensions
.ExtensionBlockCount
= 0;
457 sp
->Extensions
.ExtensionBlocks
= NULL
;
459 GifFile
->ImageCount
++;
461 Private
->PixelCount
= (long)GifFile
->Image
.Width
*
462 (long)GifFile
->Image
.Height
;
464 DGifSetupDecompress(GifFile
); /* Reset decompress algorithm parameters. */
469 /******************************************************************************
470 * Get one full scanned line (Line) of length LineLen from GIF file.
471 *****************************************************************************/
473 DGifGetLine(GifFileType
* GifFile
,
478 GifFilePrivateType
*Private
= GifFile
->Private
;
481 LineLen
= GifFile
->Image
.Width
;
483 if ((Private
->PixelCount
-= LineLen
) > 0xffff0000UL
) {
487 if (DGifDecompressLine(GifFile
, Line
, LineLen
) == GIF_OK
) {
488 if (Private
->PixelCount
== 0) {
489 /* We probably would not be called any more, so lets clean
490 * everything before we return: need to flush out all rest of
491 * image until empty block (size 0) detected. We use GetCodeNext. */
493 if (DGifGetCodeNext(GifFile
, &Dummy
) == GIF_ERROR
)
495 while (Dummy
!= NULL
) ;
502 /******************************************************************************
503 * Get an extension block (see GIF manual) from gif file. This routine only
504 * returns the first data block, and DGifGetExtensionNext should be called
505 * after this one until NULL extension is returned.
506 * The Extension should NOT be freed by the user (not dynamically allocated).
507 * Note it is assumed the Extension desc. header ('!') has been read.
508 *****************************************************************************/
510 DGifGetExtension(GifFileType
* GifFile
,
512 GifByteType
** Extension
) {
516 if (READ(GifFile
, &Buf
, 1) != 1) {
521 return DGifGetExtensionNext(GifFile
, Extension
);
524 /******************************************************************************
525 * Get a following extension block (see GIF manual) from gif file. This
526 * routine should be called until NULL Extension is returned.
527 * The Extension should NOT be freed by the user (not dynamically allocated).
528 *****************************************************************************/
530 DGifGetExtensionNext(GifFileType
* GifFile
,
531 GifByteType
** Extension
) {
534 GifFilePrivateType
*Private
= GifFile
->Private
;
536 if (READ(GifFile
, &Buf
, 1) != 1) {
540 *Extension
= Private
->Buf
; /* Use private unused buffer. */
541 (*Extension
)[0] = Buf
; /* Pascal strings notation (pos. 0 is len.). */
542 if (READ(GifFile
, &((*Extension
)[1]), Buf
) != Buf
) {
551 /******************************************************************************
552 * Get 2 bytes (word) from the given file:
553 *****************************************************************************/
555 DGifGetWord(GifFileType
* GifFile
,
560 if (READ(GifFile
, c
, 2) != 2) {
564 *Word
= (((unsigned int)c
[1]) << 8) + c
[0];
568 /******************************************************************************
569 * Continue to get the image code in compressed form. This routine should be
570 * called until NULL block is returned.
571 * The block should NOT be freed by the user (not dynamically allocated).
572 *****************************************************************************/
574 DGifGetCodeNext(GifFileType
* GifFile
,
575 GifByteType
** CodeBlock
) {
578 GifFilePrivateType
*Private
= GifFile
->Private
;
580 if (READ(GifFile
, &Buf
, 1) != 1) {
585 *CodeBlock
= Private
->Buf
; /* Use private unused buffer. */
586 (*CodeBlock
)[0] = Buf
; /* Pascal strings notation (pos. 0 is len.). */
587 if (READ(GifFile
, &((*CodeBlock
)[1]), Buf
) != Buf
) {
592 Private
->Buf
[0] = 0; /* Make sure the buffer is empty! */
593 Private
->PixelCount
= 0; /* And local info. indicate image read. */
599 /******************************************************************************
600 * Setup the LZ decompression for this image:
601 *****************************************************************************/
603 DGifSetupDecompress(GifFileType
* GifFile
) {
606 GifByteType CodeSize
;
607 GifPrefixType
*Prefix
;
608 GifFilePrivateType
*Private
= GifFile
->Private
;
610 READ(GifFile
, &CodeSize
, 1); /* Read Code size from file. */
611 BitsPerPixel
= CodeSize
;
613 Private
->Buf
[0] = 0; /* Input Buffer empty. */
614 Private
->BitsPerPixel
= BitsPerPixel
;
615 Private
->ClearCode
= (1 << BitsPerPixel
);
616 Private
->EOFCode
= Private
->ClearCode
+ 1;
617 Private
->RunningCode
= Private
->EOFCode
+ 1;
618 Private
->RunningBits
= BitsPerPixel
+ 1; /* Number of bits per code. */
619 Private
->MaxCode1
= 1 << Private
->RunningBits
; /* Max. code + 1. */
620 Private
->StackPtr
= 0; /* No pixels on the pixel stack. */
621 Private
->LastCode
= NO_SUCH_CODE
;
622 Private
->CrntShiftState
= 0; /* No information in CrntShiftDWord. */
623 Private
->CrntShiftDWord
= 0;
625 Prefix
= Private
->Prefix
;
626 for (i
= 0; i
<= LZ_MAX_CODE
; i
++)
627 Prefix
[i
] = NO_SUCH_CODE
;
632 /******************************************************************************
633 * The LZ decompression routine:
634 * This version decompress the given gif file into Line of length LineLen.
635 * This routine can be called few times (one per scan line, for example), in
636 * order the complete the whole image.
637 *****************************************************************************/
639 DGifDecompressLine(GifFileType
* GifFile
,
644 int j
, CrntCode
, EOFCode
, ClearCode
, CrntPrefix
, LastCode
, StackPtr
;
645 GifByteType
*Stack
, *Suffix
;
646 GifPrefixType
*Prefix
;
647 GifFilePrivateType
*Private
= GifFile
->Private
;
649 StackPtr
= Private
->StackPtr
;
650 Prefix
= Private
->Prefix
;
651 Suffix
= Private
->Suffix
;
652 Stack
= Private
->Stack
;
653 EOFCode
= Private
->EOFCode
;
654 ClearCode
= Private
->ClearCode
;
655 LastCode
= Private
->LastCode
;
658 /* Let pop the stack off before continuing to read the gif file: */
659 while (StackPtr
!= 0 && i
< LineLen
)
660 Line
[i
++] = Stack
[--StackPtr
];
663 while (i
< LineLen
) { /* Decode LineLen items. */
664 if (DGifDecompressInput(GifFile
, &CrntCode
) == GIF_ERROR
)
667 if (CrntCode
== EOFCode
) {
668 /* Note, however, that usually we will not be here as we will stop
669 * decoding as soon as we got all the pixel, or EOF code will
670 * not be read at all, and DGifGetLine/Pixel clean everything. */
671 if (i
!= LineLen
- 1 || Private
->PixelCount
!= 0) {
675 } else if (CrntCode
== ClearCode
) {
676 /* We need to start over again: */
677 for (j
= 0; j
<= LZ_MAX_CODE
; j
++)
678 Prefix
[j
] = NO_SUCH_CODE
;
679 Private
->RunningCode
= Private
->EOFCode
+ 1;
680 Private
->RunningBits
= Private
->BitsPerPixel
+ 1;
681 Private
->MaxCode1
= 1 << Private
->RunningBits
;
682 LastCode
= Private
->LastCode
= NO_SUCH_CODE
;
684 /* It's a regular code - if in pixel range simply add it to output
685 * stream, otherwise trace to codes linked list until the prefix
686 * is in pixel range: */
687 if (CrntCode
< ClearCode
) {
688 /* This is simple - its pixel scalar, so add it to output: */
689 Line
[i
++] = CrntCode
;
691 /* It's a code to be traced: trace the linked list
692 * until the prefix is a pixel, while pushing the suffix
693 * pixels on our stack. If we done, pop the stack in reverse
694 * order (that's what stack is good for!) for output. */
695 if (Prefix
[CrntCode
] == NO_SUCH_CODE
) {
696 /* Only allowed if CrntCode is exactly the running code:
697 * In that case CrntCode = XXXCode, CrntCode or the
698 * prefix code is last code and the suffix char is
699 * exactly the prefix of last code! */
700 if (CrntCode
== Private
->RunningCode
- 2) {
701 CrntPrefix
= LastCode
;
702 Suffix
[Private
->RunningCode
- 2] =
703 Stack
[StackPtr
++] = DGifGetPrefixChar(Prefix
,
710 CrntPrefix
= CrntCode
;
712 /* Now (if image is O.K.) we should not get a NO_SUCH_CODE
713 * during the trace. As we might loop forever, in case of
714 * defective image, we count the number of loops we trace
715 * and stop if we got LZ_MAX_CODE. Obviously we cannot
716 * loop more than that. */
718 while (j
++ <= LZ_MAX_CODE
&&
719 CrntPrefix
> ClearCode
&& CrntPrefix
<= LZ_MAX_CODE
) {
720 Stack
[StackPtr
++] = Suffix
[CrntPrefix
];
721 CrntPrefix
= Prefix
[CrntPrefix
];
723 if (j
>= LZ_MAX_CODE
|| CrntPrefix
> LZ_MAX_CODE
) {
726 /* Push the last character on stack: */
727 Stack
[StackPtr
++] = CrntPrefix
;
729 /* Now lets pop all the stack into output: */
730 while (StackPtr
!= 0 && i
< LineLen
)
731 Line
[i
++] = Stack
[--StackPtr
];
733 if (LastCode
!= NO_SUCH_CODE
) {
734 Prefix
[Private
->RunningCode
- 2] = LastCode
;
736 if (CrntCode
== Private
->RunningCode
- 2) {
737 /* Only allowed if CrntCode is exactly the running code:
738 * In that case CrntCode = XXXCode, CrntCode or the
739 * prefix code is last code and the suffix char is
740 * exactly the prefix of last code! */
741 Suffix
[Private
->RunningCode
- 2] =
742 DGifGetPrefixChar(Prefix
, LastCode
, ClearCode
);
744 Suffix
[Private
->RunningCode
- 2] =
745 DGifGetPrefixChar(Prefix
, CrntCode
, ClearCode
);
752 Private
->LastCode
= LastCode
;
753 Private
->StackPtr
= StackPtr
;
758 /******************************************************************************
759 * Routine to trace the Prefixes linked list until we get a prefix which is
760 * not code, but a pixel value (less than ClearCode). Returns that pixel value.
761 * If image is defective, we might loop here forever, so we limit the loops to
762 * the maximum possible if image O.k. - LZ_MAX_CODE times.
763 *****************************************************************************/
765 DGifGetPrefixChar(const GifPrefixType
*Prefix
,
771 while (Code
> ClearCode
&& i
++ <= LZ_MAX_CODE
)
776 /******************************************************************************
777 * The LZ decompression input routine:
778 * This routine is responsible for the decompression of the bit stream from
779 * 8 bits (bytes) packets, into the real codes.
780 * Returns GIF_OK if read successfully.
781 *****************************************************************************/
783 DGifDecompressInput(GifFileType
* GifFile
,
786 GifFilePrivateType
*Private
= GifFile
->Private
;
788 GifByteType NextByte
;
789 static const unsigned short CodeMasks
[] = {
790 0x0000, 0x0001, 0x0003, 0x0007,
791 0x000f, 0x001f, 0x003f, 0x007f,
792 0x00ff, 0x01ff, 0x03ff, 0x07ff,
795 /* The image can't contain more than LZ_BITS per code. */
796 if (Private
->RunningBits
> LZ_BITS
) {
800 while (Private
->CrntShiftState
< Private
->RunningBits
) {
801 /* Needs to get more bytes from input stream for next code: */
802 if (DGifBufferedInput(GifFile
, Private
->Buf
, &NextByte
) == GIF_ERROR
) {
805 Private
->CrntShiftDWord
|=
806 ((unsigned long)NextByte
) << Private
->CrntShiftState
;
807 Private
->CrntShiftState
+= 8;
809 *Code
= Private
->CrntShiftDWord
& CodeMasks
[Private
->RunningBits
];
811 Private
->CrntShiftDWord
>>= Private
->RunningBits
;
812 Private
->CrntShiftState
-= Private
->RunningBits
;
814 /* If code cannot fit into RunningBits bits, must raise its size. Note
815 * however that codes above 4095 are used for special signaling.
816 * If we're using LZ_BITS bits already and we're at the max code, just
817 * keep using the table as it is, don't increment Private->RunningCode.
819 if (Private
->RunningCode
< LZ_MAX_CODE
+ 2 &&
820 ++Private
->RunningCode
> Private
->MaxCode1
&&
821 Private
->RunningBits
< LZ_BITS
) {
822 Private
->MaxCode1
<<= 1;
823 Private
->RunningBits
++;
828 /******************************************************************************
829 * This routines read one gif data block at a time and buffers it internally
830 * so that the decompression routine could access it.
831 * The routine returns the next byte from its internal buffer (or read next
832 * block in if buffer empty) and returns GIF_OK if successful.
833 *****************************************************************************/
835 DGifBufferedInput(GifFileType
* GifFile
,
837 GifByteType
* NextByte
) {
840 /* Needs to read the next buffer - this one is empty: */
841 if (READ(GifFile
, Buf
, 1) != 1) {
844 /* There shouldn't be any empty data blocks here as the LZW spec
845 * says the LZW termination code should come first. Therefore we
846 * shouldn't be inside this routine at that point.
851 if (READ(GifFile
, &Buf
[1], Buf
[0]) != Buf
[0]) {
855 Buf
[1] = 2; /* We use now the second place as last char read! */
858 *NextByte
= Buf
[Buf
[1]++];
865 /******************************************************************************
866 * This routine reads an entire GIF into core, hanging all its state info off
867 * the GifFileType pointer. Call DGifOpenFileName() or DGifOpenFileHandle()
868 * first to initialize I/O. Its inverse is EGifSpew().
869 ******************************************************************************/
871 DGifSlurp(GifFileType
* GifFile
) {
874 GifRecordType RecordType
;
876 GifByteType
*ExtData
;
877 Extensions temp_save
;
879 temp_save
.ExtensionBlocks
= NULL
;
880 temp_save
.ExtensionBlockCount
= 0;
883 if (DGifGetRecordType(GifFile
, &RecordType
) == GIF_ERROR
)
886 switch (RecordType
) {
887 case IMAGE_DESC_RECORD_TYPE
:
888 if (DGifGetImageDesc(GifFile
) == GIF_ERROR
)
891 sp
= &GifFile
->SavedImages
[GifFile
->ImageCount
- 1];
892 ImageSize
= sp
->ImageDesc
.Width
* sp
->ImageDesc
.Height
;
894 sp
->RasterBits
= ungif_alloc(ImageSize
* sizeof(GifPixelType
));
895 if (sp
->RasterBits
== NULL
) {
898 if (DGifGetLine(GifFile
, sp
->RasterBits
, ImageSize
) ==
901 if (temp_save
.ExtensionBlocks
) {
902 sp
->Extensions
.ExtensionBlocks
= temp_save
.ExtensionBlocks
;
903 sp
->Extensions
.ExtensionBlockCount
= temp_save
.ExtensionBlockCount
;
905 temp_save
.ExtensionBlocks
= NULL
;
906 temp_save
.ExtensionBlockCount
= 0;
908 /* FIXME: The following is wrong. It is left in only for
909 * backwards compatibility. Someday it should go away. Use
910 * the sp->ExtensionBlocks->Function variable instead. */
911 sp
->Extensions
.Function
= sp
->Extensions
.ExtensionBlocks
[0].Function
;
915 case EXTENSION_RECORD_TYPE
:
918 Extensions
*Extensions
;
920 if (DGifGetExtension(GifFile
, &Function
, &ExtData
) == GIF_ERROR
)
923 if (GifFile
->ImageCount
|| Function
== GRAPHICS_EXT_FUNC_CODE
)
924 Extensions
= &temp_save
;
926 Extensions
= &GifFile
->Extensions
;
928 Extensions
->Function
= Function
;
930 /* Create an extension block with our data */
931 if (AddExtensionBlock(Extensions
, ExtData
[0], &ExtData
[1]) == GIF_ERROR
)
934 while (ExtData
!= NULL
) {
938 if (DGifGetExtensionNext(GifFile
, &ExtData
) == GIF_ERROR
)
952 if (AppendExtensionBlock(Extensions
, Len
, Data
) == GIF_ERROR
)
958 case TERMINATE_RECORD_TYPE
:
961 default: /* Should be trapped by DGifGetRecordType */
964 } while (RecordType
!= TERMINATE_RECORD_TYPE
);
966 /* Just in case the Gif has an extension block without an associated
967 * image... (Should we save this into a savefile structure with no image
968 * instead? Have to check if the present writing code can handle that as
970 if (temp_save
.ExtensionBlocks
)
971 FreeExtension(&temp_save
);
976 /******************************************************************************
977 * GifFileType constructor with user supplied input function (TVT)
978 *****************************************************************************/
980 DGifOpen(void *userData
,
981 InputFunc readFunc
) {
983 unsigned char Buf
[GIF_STAMP_LEN
+ 1];
984 GifFileType
*GifFile
;
985 GifFilePrivateType
*Private
;
987 GifFile
= ungif_alloc(sizeof(GifFileType
));
988 if (GifFile
== NULL
) {
992 memset(GifFile
, '\0', sizeof(GifFileType
));
994 Private
= ungif_alloc(sizeof(GifFilePrivateType
));
1000 GifFile
->Private
= (void*)Private
;
1002 Private
->Read
= readFunc
; /* TVT */
1003 GifFile
->UserData
= userData
; /* TVT */
1005 /* Lets see if this is a GIF file: */
1006 if (READ(GifFile
, Buf
, GIF_STAMP_LEN
) != GIF_STAMP_LEN
) {
1007 ungif_free(Private
);
1008 ungif_free(GifFile
);
1012 /* The GIF Version number is ignored at this time. Maybe we should do
1013 * something more useful with it. */
1014 Buf
[GIF_STAMP_LEN
] = 0;
1015 if (memcmp(GIF_STAMP
, Buf
, GIF_VERSION_POS
) != 0) {
1016 ungif_free(Private
);
1017 ungif_free(GifFile
);
1021 if (DGifGetScreenDesc(GifFile
) == GIF_ERROR
) {
1022 ungif_free(Private
);
1023 ungif_free(GifFile
);
1030 /******************************************************************************
1031 * This routine should be called last, to close the GIF file.
1032 *****************************************************************************/
1034 DGifCloseFile(GifFileType
* GifFile
) {
1036 GifFilePrivateType
*Private
;
1038 if (GifFile
== NULL
)
1041 Private
= GifFile
->Private
;
1043 if (GifFile
->Image
.ColorMap
) {
1044 FreeMapObject(GifFile
->Image
.ColorMap
);
1045 GifFile
->Image
.ColorMap
= NULL
;
1048 if (GifFile
->SColorMap
) {
1049 FreeMapObject(GifFile
->SColorMap
);
1050 GifFile
->SColorMap
= NULL
;
1053 ungif_free(Private
);
1056 if (GifFile
->SavedImages
) {
1057 FreeSavedImages(GifFile
);
1058 GifFile
->SavedImages
= NULL
;
1061 FreeExtension(&GifFile
->Extensions
);
1063 ungif_free(GifFile
);