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 *****************************************************************************/
57 #include "wine/debug.h"
59 WINE_DEFAULT_DEBUG_CHANNEL(wincodecs
);
61 static void *ungif_alloc( size_t sz
)
63 return HeapAlloc( GetProcessHeap(), 0, sz
);
66 static void *ungif_calloc( size_t num
, size_t sz
)
68 return HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, num
*sz
);
71 static void *ungif_realloc( void *ptr
, size_t sz
)
73 return HeapReAlloc( GetProcessHeap(), 0, ptr
, sz
);
76 static void ungif_free( void *ptr
)
78 HeapFree( GetProcessHeap(), 0, ptr
);
81 #define LZ_MAX_CODE 4095 /* Biggest code possible in 12 bits. */
84 #define NO_SUCH_CODE 4098 /* Impossible code, to signal empty. */
86 typedef struct GifFilePrivateType
{
87 GifWord BitsPerPixel
, /* Bits per pixel (Codes uses at least this + 1). */
88 ClearCode
, /* The CLEAR LZ code. */
89 EOFCode
, /* The EOF LZ code. */
90 RunningCode
, /* The next code algorithm can generate. */
91 RunningBits
, /* The number of bits required to represent RunningCode. */
92 MaxCode1
, /* 1 bigger than max. possible code, in RunningBits bits. */
93 LastCode
, /* The code before the current code. */
94 CrntCode
, /* Current algorithm code. */
95 StackPtr
, /* For character stack (see below). */
96 CrntShiftState
; /* Number of bits in CrntShiftDWord. */
97 unsigned long CrntShiftDWord
; /* For bytes decomposition into codes. */
98 unsigned long PixelCount
; /* Number of pixels in image. */
99 InputFunc Read
; /* function to read gif input (TVT) */
100 GifByteType Buf
[256]; /* Compressed input is buffered here. */
101 GifByteType Stack
[LZ_MAX_CODE
]; /* Decoded pixels are stacked here. */
102 GifByteType Suffix
[LZ_MAX_CODE
+ 1]; /* So we can trace the codes. */
103 GifPrefixType Prefix
[LZ_MAX_CODE
+ 1];
104 } GifFilePrivateType
;
106 /* avoid extra function call in case we use fread (TVT) */
107 #define READ(_gif,_buf,_len) \
108 ((GifFilePrivateType*)_gif->Private)->Read(_gif,_buf,_len)
110 static int DGifGetWord(GifFileType
*GifFile
, GifWord
*Word
);
111 static int DGifSetupDecompress(GifFileType
*GifFile
);
112 static int DGifDecompressLine(GifFileType
*GifFile
, GifPixelType
*Line
, int LineLen
);
113 static int DGifGetPrefixChar(const GifPrefixType
*Prefix
, int Code
, int ClearCode
);
114 static int DGifDecompressInput(GifFileType
*GifFile
, int *Code
);
115 static int DGifBufferedInput(GifFileType
*GifFile
, GifByteType
*Buf
,
116 GifByteType
*NextByte
);
118 static int DGifGetExtensionNext(GifFileType
* GifFile
, GifByteType
** GifExtension
);
119 static int DGifGetCodeNext(GifFileType
* GifFile
, GifByteType
** GifCodeBlock
);
121 /******************************************************************************
122 * Miscellaneous utility functions
123 *****************************************************************************/
125 /* return smallest bitfield size n will fit in */
131 for (i
= 1; i
<= 8; i
++)
137 /******************************************************************************
138 * Color map object functions
139 *****************************************************************************/
142 * Allocate a color map of given size; initialize with contents of
143 * ColorMap if that pointer is non-NULL.
145 static ColorMapObject
*
146 MakeMapObject(int ColorCount
,
147 const GifColorType
* ColorMap
) {
149 ColorMapObject
*Object
;
151 /*** FIXME: Our ColorCount has to be a power of two. Is it necessary to
152 * make the user know that or should we automatically round up instead? */
153 if (ColorCount
!= (1 << BitSize(ColorCount
))) {
157 Object
= ungif_alloc(sizeof(ColorMapObject
));
158 if (Object
== NULL
) {
162 Object
->Colors
= ungif_calloc(ColorCount
, sizeof(GifColorType
));
163 if (Object
->Colors
== NULL
) {
168 Object
->ColorCount
= ColorCount
;
169 Object
->BitsPerPixel
= BitSize(ColorCount
);
172 memcpy(Object
->Colors
, ColorMap
, ColorCount
* sizeof(GifColorType
));
179 * Free a color map object
182 FreeMapObject(ColorMapObject
* Object
) {
184 if (Object
!= NULL
) {
185 ungif_free(Object
->Colors
);
188 * When we are willing to break API we need to make this function
189 * FreeMapObject(ColorMapObject **Object)
190 * and do this assignment to NULL here:
197 AddExtensionBlock(Extensions
*New
,
199 const unsigned char ExtData
[]) {
203 if (New
->ExtensionBlocks
== NULL
)
204 New
->ExtensionBlocks
= ungif_alloc(sizeof(ExtensionBlock
));
206 New
->ExtensionBlocks
= ungif_realloc(New
->ExtensionBlocks
,
207 sizeof(ExtensionBlock
) *
208 (New
->ExtensionBlockCount
+ 1));
210 if (New
->ExtensionBlocks
== NULL
)
213 ep
= &New
->ExtensionBlocks
[New
->ExtensionBlockCount
++];
215 ep
->ByteCount
=Len
+ 3;
216 ep
->Bytes
= ungif_alloc(ep
->ByteCount
+ 3);
217 if (ep
->Bytes
== NULL
)
220 /* Extension Header */
222 ep
->Bytes
[1] = New
->Function
;
226 memcpy(ep
->Bytes
+ 3, ExtData
, Len
);
227 ep
->Function
= New
->Function
;
234 AppendExtensionBlock(Extensions
*New
,
236 const unsigned char ExtData
[])
240 if (New
->ExtensionBlocks
== NULL
)
243 ep
= &New
->ExtensionBlocks
[New
->ExtensionBlockCount
- 1];
245 ep
->Bytes
= ungif_realloc(ep
->Bytes
, ep
->ByteCount
+ Len
+ 1);
246 if (ep
->Bytes
== NULL
)
249 ep
->Bytes
[ep
->ByteCount
] = Len
;
252 memcpy(ep
->Bytes
+ ep
->ByteCount
+ 1, ExtData
, Len
);
254 ep
->ByteCount
+= Len
+ 1;
260 FreeExtension(Extensions
*Extensions
)
264 if ((Extensions
== NULL
) || (Extensions
->ExtensionBlocks
== NULL
)) {
267 for (ep
= Extensions
->ExtensionBlocks
;
268 ep
< (Extensions
->ExtensionBlocks
+ Extensions
->ExtensionBlockCount
); ep
++)
269 ungif_free(ep
->Bytes
);
270 ungif_free(Extensions
->ExtensionBlocks
);
271 Extensions
->ExtensionBlocks
= NULL
;
274 /******************************************************************************
275 * Image block allocation functions
276 ******************************************************************************/
279 FreeSavedImages(GifFileType
* GifFile
) {
283 if ((GifFile
== NULL
) || (GifFile
->SavedImages
== NULL
)) {
286 for (sp
= GifFile
->SavedImages
;
287 sp
< GifFile
->SavedImages
+ GifFile
->ImageCount
; sp
++) {
288 if (sp
->ImageDesc
.ColorMap
) {
289 FreeMapObject(sp
->ImageDesc
.ColorMap
);
290 sp
->ImageDesc
.ColorMap
= NULL
;
293 ungif_free(sp
->RasterBits
);
295 if (sp
->Extensions
.ExtensionBlocks
)
296 FreeExtension(&sp
->Extensions
);
298 ungif_free(GifFile
->SavedImages
);
299 GifFile
->SavedImages
=NULL
;
302 /******************************************************************************
303 * This routine should be called before any other DGif calls. Note that
304 * this routine is called automatically from DGif file open routines.
305 *****************************************************************************/
307 DGifGetScreenDesc(GifFileType
* GifFile
) {
309 int i
, BitsPerPixel
, SortFlag
;
312 /* Put the screen descriptor into the file: */
313 if (DGifGetWord(GifFile
, &GifFile
->SWidth
) == GIF_ERROR
||
314 DGifGetWord(GifFile
, &GifFile
->SHeight
) == GIF_ERROR
)
317 if (READ(GifFile
, Buf
, 3) != 3) {
320 GifFile
->SColorResolution
= (((Buf
[0] & 0x70) + 1) >> 4) + 1;
321 SortFlag
= (Buf
[0] & 0x08) != 0;
322 BitsPerPixel
= (Buf
[0] & 0x07) + 1;
323 GifFile
->SBackGroundColor
= Buf
[1];
324 GifFile
->SAspectRatio
= Buf
[2];
325 if (Buf
[0] & 0x80) { /* Do we have global color map? */
327 GifFile
->SColorMap
= MakeMapObject(1 << BitsPerPixel
, NULL
);
328 if (GifFile
->SColorMap
== NULL
) {
332 /* Get the global color map: */
333 GifFile
->SColorMap
->SortFlag
= SortFlag
;
334 for (i
= 0; i
< GifFile
->SColorMap
->ColorCount
; i
++) {
335 if (READ(GifFile
, Buf
, 3) != 3) {
336 FreeMapObject(GifFile
->SColorMap
);
337 GifFile
->SColorMap
= NULL
;
340 GifFile
->SColorMap
->Colors
[i
].Red
= Buf
[0];
341 GifFile
->SColorMap
->Colors
[i
].Green
= Buf
[1];
342 GifFile
->SColorMap
->Colors
[i
].Blue
= Buf
[2];
345 GifFile
->SColorMap
= NULL
;
351 /******************************************************************************
352 * This routine should be called before any attempt to read an image.
353 *****************************************************************************/
355 DGifGetRecordType(GifFileType
* GifFile
,
356 GifRecordType
* Type
) {
360 if (READ(GifFile
, &Buf
, 1) != 1) {
361 /* Wine-specific behavior: Native accepts broken GIF files that have no
362 * terminator, so we match this by treating EOF as a terminator. */
363 *Type
= TERMINATE_RECORD_TYPE
;
369 *Type
= IMAGE_DESC_RECORD_TYPE
;
372 *Type
= EXTENSION_RECORD_TYPE
;
375 *Type
= TERMINATE_RECORD_TYPE
;
378 *Type
= UNDEFINED_RECORD_TYPE
;
385 /******************************************************************************
386 * This routine should be called before any attempt to read an image.
387 * Note it is assumed the Image desc. header (',') has been read.
388 *****************************************************************************/
390 DGifGetImageDesc(GifFileType
* GifFile
) {
392 int i
, BitsPerPixel
, SortFlag
;
394 GifFilePrivateType
*Private
= GifFile
->Private
;
397 if (DGifGetWord(GifFile
, &GifFile
->Image
.Left
) == GIF_ERROR
||
398 DGifGetWord(GifFile
, &GifFile
->Image
.Top
) == GIF_ERROR
||
399 DGifGetWord(GifFile
, &GifFile
->Image
.Width
) == GIF_ERROR
||
400 DGifGetWord(GifFile
, &GifFile
->Image
.Height
) == GIF_ERROR
)
402 if (READ(GifFile
, Buf
, 1) != 1) {
405 BitsPerPixel
= (Buf
[0] & 0x07) + 1;
406 SortFlag
= (Buf
[0] & 0x20) != 0;
407 GifFile
->Image
.Interlace
= (Buf
[0] & 0x40);
408 if (Buf
[0] & 0x80) { /* Does this image have local color map? */
410 FreeMapObject(GifFile
->Image
.ColorMap
);
412 GifFile
->Image
.ColorMap
= MakeMapObject(1 << BitsPerPixel
, NULL
);
413 if (GifFile
->Image
.ColorMap
== NULL
) {
417 /* Get the image local color map: */
418 GifFile
->Image
.ColorMap
->SortFlag
= SortFlag
;
419 for (i
= 0; i
< GifFile
->Image
.ColorMap
->ColorCount
; i
++) {
420 if (READ(GifFile
, Buf
, 3) != 3) {
421 FreeMapObject(GifFile
->Image
.ColorMap
);
422 GifFile
->Image
.ColorMap
= NULL
;
425 GifFile
->Image
.ColorMap
->Colors
[i
].Red
= Buf
[0];
426 GifFile
->Image
.ColorMap
->Colors
[i
].Green
= Buf
[1];
427 GifFile
->Image
.ColorMap
->Colors
[i
].Blue
= Buf
[2];
429 } else if (GifFile
->Image
.ColorMap
) {
430 FreeMapObject(GifFile
->Image
.ColorMap
);
431 GifFile
->Image
.ColorMap
= NULL
;
434 if (GifFile
->SavedImages
) {
435 if ((GifFile
->SavedImages
= ungif_realloc(GifFile
->SavedImages
,
437 (GifFile
->ImageCount
+ 1))) == NULL
) {
441 if ((GifFile
->SavedImages
= ungif_alloc(sizeof(SavedImage
))) == NULL
) {
446 sp
= &GifFile
->SavedImages
[GifFile
->ImageCount
];
447 sp
->ImageDesc
= GifFile
->Image
;
448 if (GifFile
->Image
.ColorMap
!= NULL
) {
449 sp
->ImageDesc
.ColorMap
= MakeMapObject(
450 GifFile
->Image
.ColorMap
->ColorCount
,
451 GifFile
->Image
.ColorMap
->Colors
);
452 if (sp
->ImageDesc
.ColorMap
== NULL
) {
455 sp
->ImageDesc
.ColorMap
->SortFlag
= GifFile
->Image
.ColorMap
->SortFlag
;
457 sp
->RasterBits
= NULL
;
458 sp
->Extensions
.ExtensionBlockCount
= 0;
459 sp
->Extensions
.ExtensionBlocks
= NULL
;
461 GifFile
->ImageCount
++;
463 Private
->PixelCount
= GifFile
->Image
.Width
* GifFile
->Image
.Height
;
465 DGifSetupDecompress(GifFile
); /* Reset decompress algorithm parameters. */
470 /******************************************************************************
471 * Get one full scanned line (Line) of length LineLen from GIF file.
472 *****************************************************************************/
474 DGifGetLine(GifFileType
* GifFile
,
479 GifFilePrivateType
*Private
= GifFile
->Private
;
482 LineLen
= GifFile
->Image
.Width
;
484 if ((Private
->PixelCount
-= LineLen
) > 0xffff0000UL
) {
488 if (DGifDecompressLine(GifFile
, Line
, LineLen
) == GIF_OK
) {
489 if (Private
->PixelCount
== 0) {
490 /* We probably would not be called any more, so lets clean
491 * everything before we return: need to flush out all rest of
492 * image until empty block (size 0) detected. We use GetCodeNext. */
494 if (DGifGetCodeNext(GifFile
, &Dummy
) == GIF_ERROR
)
496 WARN("GIF is not properly terminated\n");
499 while (Dummy
!= NULL
) ;
506 /******************************************************************************
507 * Get an extension block (see GIF manual) from gif file. This routine only
508 * returns the first data block, and DGifGetExtensionNext should be called
509 * after this one until NULL extension is returned.
510 * The Extension should NOT be freed by the user (not dynamically allocated).
511 * Note it is assumed the Extension desc. header ('!') has been read.
512 *****************************************************************************/
514 DGifGetExtension(GifFileType
* GifFile
,
516 GifByteType
** Extension
) {
520 if (READ(GifFile
, &Buf
, 1) != 1) {
525 return DGifGetExtensionNext(GifFile
, Extension
);
528 /******************************************************************************
529 * Get a following extension block (see GIF manual) from gif file. This
530 * routine should be called until NULL Extension is returned.
531 * The Extension should NOT be freed by the user (not dynamically allocated).
532 *****************************************************************************/
534 DGifGetExtensionNext(GifFileType
* GifFile
,
535 GifByteType
** Extension
) {
538 GifFilePrivateType
*Private
= GifFile
->Private
;
540 if (READ(GifFile
, &Buf
, 1) != 1) {
544 *Extension
= Private
->Buf
; /* Use private unused buffer. */
545 (*Extension
)[0] = Buf
; /* Pascal strings notation (pos. 0 is len.). */
546 if (READ(GifFile
, &((*Extension
)[1]), Buf
) != Buf
) {
555 /******************************************************************************
556 * Get 2 bytes (word) from the given file:
557 *****************************************************************************/
559 DGifGetWord(GifFileType
* GifFile
,
564 if (READ(GifFile
, c
, 2) != 2) {
568 *Word
= (((unsigned int)c
[1]) << 8) + c
[0];
572 /******************************************************************************
573 * Continue to get the image code in compressed form. This routine should be
574 * called until NULL block is returned.
575 * The block should NOT be freed by the user (not dynamically allocated).
576 *****************************************************************************/
578 DGifGetCodeNext(GifFileType
* GifFile
,
579 GifByteType
** CodeBlock
) {
582 GifFilePrivateType
*Private
= GifFile
->Private
;
584 if (READ(GifFile
, &Buf
, 1) != 1) {
589 *CodeBlock
= Private
->Buf
; /* Use private unused buffer. */
590 (*CodeBlock
)[0] = Buf
; /* Pascal strings notation (pos. 0 is len.). */
591 if (READ(GifFile
, &((*CodeBlock
)[1]), Buf
) != Buf
) {
596 Private
->Buf
[0] = 0; /* Make sure the buffer is empty! */
597 Private
->PixelCount
= 0; /* And local info. indicate image read. */
603 /******************************************************************************
604 * Setup the LZ decompression for this image:
605 *****************************************************************************/
607 DGifSetupDecompress(GifFileType
* GifFile
) {
610 GifByteType CodeSize
;
611 GifPrefixType
*Prefix
;
612 GifFilePrivateType
*Private
= GifFile
->Private
;
614 READ(GifFile
, &CodeSize
, 1); /* Read Code size from file. */
615 BitsPerPixel
= CodeSize
;
617 Private
->Buf
[0] = 0; /* Input Buffer empty. */
618 Private
->BitsPerPixel
= BitsPerPixel
;
619 Private
->ClearCode
= (1 << BitsPerPixel
);
620 Private
->EOFCode
= Private
->ClearCode
+ 1;
621 Private
->RunningCode
= Private
->EOFCode
+ 1;
622 Private
->RunningBits
= BitsPerPixel
+ 1; /* Number of bits per code. */
623 Private
->MaxCode1
= 1 << Private
->RunningBits
; /* Max. code + 1. */
624 Private
->StackPtr
= 0; /* No pixels on the pixel stack. */
625 Private
->LastCode
= NO_SUCH_CODE
;
626 Private
->CrntShiftState
= 0; /* No information in CrntShiftDWord. */
627 Private
->CrntShiftDWord
= 0;
629 Prefix
= Private
->Prefix
;
630 for (i
= 0; i
<= LZ_MAX_CODE
; i
++)
631 Prefix
[i
] = NO_SUCH_CODE
;
636 /******************************************************************************
637 * The LZ decompression routine:
638 * This version decompress the given gif file into Line of length LineLen.
639 * This routine can be called few times (one per scan line, for example), in
640 * order the complete the whole image.
641 *****************************************************************************/
643 DGifDecompressLine(GifFileType
* GifFile
,
648 int j
, CrntCode
, EOFCode
, ClearCode
, CrntPrefix
, LastCode
, StackPtr
;
649 GifByteType
*Stack
, *Suffix
;
650 GifPrefixType
*Prefix
;
651 GifFilePrivateType
*Private
= GifFile
->Private
;
653 StackPtr
= Private
->StackPtr
;
654 Prefix
= Private
->Prefix
;
655 Suffix
= Private
->Suffix
;
656 Stack
= Private
->Stack
;
657 EOFCode
= Private
->EOFCode
;
658 ClearCode
= Private
->ClearCode
;
659 LastCode
= Private
->LastCode
;
662 /* Let pop the stack off before continuing to read the gif file: */
663 while (StackPtr
!= 0 && i
< LineLen
)
664 Line
[i
++] = Stack
[--StackPtr
];
667 while (i
< LineLen
) { /* Decode LineLen items. */
668 if (DGifDecompressInput(GifFile
, &CrntCode
) == GIF_ERROR
)
671 if (CrntCode
== EOFCode
) {
672 /* Note, however, that usually we will not be here as we will stop
673 * decoding as soon as we got all the pixel, or EOF code will
674 * not be read at all, and DGifGetLine/Pixel clean everything. */
675 if (i
!= LineLen
- 1 || Private
->PixelCount
!= 0) {
679 } else if (CrntCode
== ClearCode
) {
680 /* We need to start over again: */
681 for (j
= 0; j
<= LZ_MAX_CODE
; j
++)
682 Prefix
[j
] = NO_SUCH_CODE
;
683 Private
->RunningCode
= Private
->EOFCode
+ 1;
684 Private
->RunningBits
= Private
->BitsPerPixel
+ 1;
685 Private
->MaxCode1
= 1 << Private
->RunningBits
;
686 LastCode
= Private
->LastCode
= NO_SUCH_CODE
;
688 /* It's a regular code - if in pixel range simply add it to output
689 * stream, otherwise trace to codes linked list until the prefix
690 * is in pixel range: */
691 if (CrntCode
< ClearCode
) {
692 /* This is simple - its pixel scalar, so add it to output: */
693 Line
[i
++] = CrntCode
;
695 /* It's a code to be traced: trace the linked list
696 * until the prefix is a pixel, while pushing the suffix
697 * pixels on our stack. If we done, pop the stack in reverse
698 * order (that's what stack is good for!) for output. */
699 if (Prefix
[CrntCode
] == NO_SUCH_CODE
) {
700 /* Only allowed if CrntCode is exactly the running code:
701 * In that case CrntCode = XXXCode, CrntCode or the
702 * prefix code is last code and the suffix char is
703 * exactly the prefix of last code! */
704 if (CrntCode
== Private
->RunningCode
- 2) {
705 CrntPrefix
= LastCode
;
706 Suffix
[Private
->RunningCode
- 2] =
707 Stack
[StackPtr
++] = DGifGetPrefixChar(Prefix
,
714 CrntPrefix
= CrntCode
;
716 /* Now (if image is O.K.) we should not get a NO_SUCH_CODE
717 * during the trace. As we might loop forever, in case of
718 * defective image, we count the number of loops we trace
719 * and stop if we got LZ_MAX_CODE. Obviously we cannot
720 * loop more than that. */
722 while (j
++ <= LZ_MAX_CODE
&&
723 CrntPrefix
> ClearCode
&& CrntPrefix
<= LZ_MAX_CODE
) {
724 Stack
[StackPtr
++] = Suffix
[CrntPrefix
];
725 CrntPrefix
= Prefix
[CrntPrefix
];
727 if (j
>= LZ_MAX_CODE
|| CrntPrefix
> LZ_MAX_CODE
) {
730 /* Push the last character on stack: */
731 Stack
[StackPtr
++] = CrntPrefix
;
733 /* Now lets pop all the stack into output: */
734 while (StackPtr
!= 0 && i
< LineLen
)
735 Line
[i
++] = Stack
[--StackPtr
];
737 if (LastCode
!= NO_SUCH_CODE
) {
738 Prefix
[Private
->RunningCode
- 2] = LastCode
;
740 if (CrntCode
== Private
->RunningCode
- 2) {
741 /* Only allowed if CrntCode is exactly the running code:
742 * In that case CrntCode = XXXCode, CrntCode or the
743 * prefix code is last code and the suffix char is
744 * exactly the prefix of last code! */
745 Suffix
[Private
->RunningCode
- 2] =
746 DGifGetPrefixChar(Prefix
, LastCode
, ClearCode
);
748 Suffix
[Private
->RunningCode
- 2] =
749 DGifGetPrefixChar(Prefix
, CrntCode
, ClearCode
);
756 Private
->LastCode
= LastCode
;
757 Private
->StackPtr
= StackPtr
;
762 /******************************************************************************
763 * Routine to trace the Prefixes linked list until we get a prefix which is
764 * not code, but a pixel value (less than ClearCode). Returns that pixel value.
765 * If image is defective, we might loop here forever, so we limit the loops to
766 * the maximum possible if image O.k. - LZ_MAX_CODE times.
767 *****************************************************************************/
769 DGifGetPrefixChar(const GifPrefixType
*Prefix
,
775 while (Code
> ClearCode
&& i
++ <= LZ_MAX_CODE
)
780 /******************************************************************************
781 * The LZ decompression input routine:
782 * This routine is responsible for the decompression of the bit stream from
783 * 8 bits (bytes) packets, into the real codes.
784 * Returns GIF_OK if read successfully.
785 *****************************************************************************/
787 DGifDecompressInput(GifFileType
* GifFile
,
790 GifFilePrivateType
*Private
= GifFile
->Private
;
792 GifByteType NextByte
;
793 static const unsigned short CodeMasks
[] = {
794 0x0000, 0x0001, 0x0003, 0x0007,
795 0x000f, 0x001f, 0x003f, 0x007f,
796 0x00ff, 0x01ff, 0x03ff, 0x07ff,
799 /* The image can't contain more than LZ_BITS per code. */
800 if (Private
->RunningBits
> LZ_BITS
) {
804 while (Private
->CrntShiftState
< Private
->RunningBits
) {
805 /* Needs to get more bytes from input stream for next code: */
806 if (DGifBufferedInput(GifFile
, Private
->Buf
, &NextByte
) == GIF_ERROR
) {
809 Private
->CrntShiftDWord
|=
810 ((unsigned long)NextByte
) << Private
->CrntShiftState
;
811 Private
->CrntShiftState
+= 8;
813 *Code
= Private
->CrntShiftDWord
& CodeMasks
[Private
->RunningBits
];
815 Private
->CrntShiftDWord
>>= Private
->RunningBits
;
816 Private
->CrntShiftState
-= Private
->RunningBits
;
818 /* If code cannot fit into RunningBits bits, must raise its size. Note
819 * however that codes above 4095 are used for special signaling.
820 * If we're using LZ_BITS bits already and we're at the max code, just
821 * keep using the table as it is, don't increment Private->RunningCode.
823 if (Private
->RunningCode
< LZ_MAX_CODE
+ 2 &&
824 ++Private
->RunningCode
> Private
->MaxCode1
&&
825 Private
->RunningBits
< LZ_BITS
) {
826 Private
->MaxCode1
<<= 1;
827 Private
->RunningBits
++;
832 /******************************************************************************
833 * This routines read one gif data block at a time and buffers it internally
834 * so that the decompression routine could access it.
835 * The routine returns the next byte from its internal buffer (or read next
836 * block in if buffer empty) and returns GIF_OK if successful.
837 *****************************************************************************/
839 DGifBufferedInput(GifFileType
* GifFile
,
841 GifByteType
* NextByte
) {
844 /* Needs to read the next buffer - this one is empty: */
845 if (READ(GifFile
, Buf
, 1) != 1) {
848 /* There shouldn't be any empty data blocks here as the LZW spec
849 * says the LZW termination code should come first. Therefore we
850 * shouldn't be inside this routine at that point.
855 if (READ(GifFile
, &Buf
[1], Buf
[0]) != Buf
[0]) {
859 Buf
[1] = 2; /* We use now the second place as last char read! */
862 *NextByte
= Buf
[Buf
[1]++];
869 /******************************************************************************
870 * This routine reads an entire GIF into core, hanging all its state info off
871 * the GifFileType pointer. Call DGifOpenFileName() or DGifOpenFileHandle()
872 * first to initialize I/O. Its inverse is EGifSpew().
873 ******************************************************************************/
875 DGifSlurp(GifFileType
* GifFile
) {
878 GifRecordType RecordType
;
880 GifByteType
*ExtData
;
881 Extensions temp_save
;
883 temp_save
.ExtensionBlocks
= NULL
;
884 temp_save
.ExtensionBlockCount
= 0;
887 if (DGifGetRecordType(GifFile
, &RecordType
) == GIF_ERROR
)
890 switch (RecordType
) {
891 case IMAGE_DESC_RECORD_TYPE
:
892 if (DGifGetImageDesc(GifFile
) == GIF_ERROR
)
895 sp
= &GifFile
->SavedImages
[GifFile
->ImageCount
- 1];
896 ImageSize
= sp
->ImageDesc
.Width
* sp
->ImageDesc
.Height
;
898 sp
->RasterBits
= ungif_alloc(ImageSize
* sizeof(GifPixelType
));
899 if (sp
->RasterBits
== NULL
) {
902 if (DGifGetLine(GifFile
, sp
->RasterBits
, ImageSize
) ==
905 if (temp_save
.ExtensionBlocks
) {
906 sp
->Extensions
.ExtensionBlocks
= temp_save
.ExtensionBlocks
;
907 sp
->Extensions
.ExtensionBlockCount
= temp_save
.ExtensionBlockCount
;
909 temp_save
.ExtensionBlocks
= NULL
;
910 temp_save
.ExtensionBlockCount
= 0;
912 /* FIXME: The following is wrong. It is left in only for
913 * backwards compatibility. Someday it should go away. Use
914 * the sp->ExtensionBlocks->Function variable instead. */
915 sp
->Extensions
.Function
= sp
->Extensions
.ExtensionBlocks
[0].Function
;
919 case EXTENSION_RECORD_TYPE
:
922 Extensions
*Extensions
;
924 if (DGifGetExtension(GifFile
, &Function
, &ExtData
) == GIF_ERROR
)
927 if (GifFile
->ImageCount
|| Function
== GRAPHICS_EXT_FUNC_CODE
)
928 Extensions
= &temp_save
;
930 Extensions
= &GifFile
->Extensions
;
932 Extensions
->Function
= Function
;
936 /* Create an extension block with our data */
937 if (AddExtensionBlock(Extensions
, ExtData
[0], &ExtData
[1]) == GIF_ERROR
)
940 else /* Empty extension block */
942 if (AddExtensionBlock(Extensions
, 0, NULL
) == GIF_ERROR
)
946 while (ExtData
!= NULL
) {
950 if (DGifGetExtensionNext(GifFile
, &ExtData
) == GIF_ERROR
)
964 if (AppendExtensionBlock(Extensions
, Len
, Data
) == GIF_ERROR
)
970 case TERMINATE_RECORD_TYPE
:
973 default: /* Should be trapped by DGifGetRecordType */
976 } while (RecordType
!= TERMINATE_RECORD_TYPE
);
978 /* Just in case the Gif has an extension block without an associated
979 * image... (Should we save this into a savefile structure with no image
980 * instead? Have to check if the present writing code can handle that as
982 if (temp_save
.ExtensionBlocks
)
983 FreeExtension(&temp_save
);
988 /******************************************************************************
989 * GifFileType constructor with user supplied input function (TVT)
990 *****************************************************************************/
992 DGifOpen(void *userData
,
993 InputFunc readFunc
) {
995 unsigned char Buf
[GIF_STAMP_LEN
+ 1];
996 GifFileType
*GifFile
;
997 GifFilePrivateType
*Private
;
999 GifFile
= ungif_alloc(sizeof(GifFileType
));
1000 if (GifFile
== NULL
) {
1004 memset(GifFile
, '\0', sizeof(GifFileType
));
1006 Private
= ungif_alloc(sizeof(GifFilePrivateType
));
1008 ungif_free(GifFile
);
1012 GifFile
->Private
= (void*)Private
;
1014 Private
->Read
= readFunc
; /* TVT */
1015 GifFile
->UserData
= userData
; /* TVT */
1017 /* Lets see if this is a GIF file: */
1018 if (READ(GifFile
, Buf
, GIF_STAMP_LEN
) != GIF_STAMP_LEN
) {
1019 ungif_free(Private
);
1020 ungif_free(GifFile
);
1024 /* The GIF Version number is ignored at this time. Maybe we should do
1025 * something more useful with it. */
1026 Buf
[GIF_STAMP_LEN
] = 0;
1027 if (memcmp(GIF_STAMP
, Buf
, GIF_VERSION_POS
) != 0) {
1028 ungif_free(Private
);
1029 ungif_free(GifFile
);
1033 if (DGifGetScreenDesc(GifFile
) == GIF_ERROR
) {
1034 ungif_free(Private
);
1035 ungif_free(GifFile
);
1042 /******************************************************************************
1043 * This routine should be called last, to close the GIF file.
1044 *****************************************************************************/
1046 DGifCloseFile(GifFileType
* GifFile
) {
1048 GifFilePrivateType
*Private
;
1050 if (GifFile
== NULL
)
1053 Private
= GifFile
->Private
;
1055 if (GifFile
->Image
.ColorMap
) {
1056 FreeMapObject(GifFile
->Image
.ColorMap
);
1057 GifFile
->Image
.ColorMap
= NULL
;
1060 if (GifFile
->SColorMap
) {
1061 FreeMapObject(GifFile
->SColorMap
);
1062 GifFile
->SColorMap
= NULL
;
1065 ungif_free(Private
);
1068 if (GifFile
->SavedImages
) {
1069 FreeSavedImages(GifFile
);
1070 GifFile
->SavedImages
= NULL
;
1073 FreeExtension(&GifFile
->Extensions
);
1075 ungif_free(GifFile
);