annotate src/dgif_lib.c @ 331:c9ae480b1fff r21-0-63

Import from CVS: tag r21-0-63
author cvs
date Mon, 13 Aug 2007 10:49:07 +0200
parents 19dcec799385
children 54f7aa390f4f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
308
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
1 /******************************************************************************
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
2 * "Gif-Lib" - Yet another gif library. *
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
3 * *
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
4 * Written by: Gershon Elber IBM PC Ver 1.1, Aug. 1990 *
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
5 *******************************************************************************
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
6 * The kernel of the GIF Decoding process can be found here. *
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
7 *******************************************************************************
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
8 * History: *
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
9 * 16 Jun 89 - Version 1.0 by Gershon Elber. *
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
10 * 3 Sep 90 - Version 1.1 by Gershon Elber (Support for Gif89, Unique names). *
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
11 * 19 Feb 98 - Version 1.2 by Jareth Hein (Support for user specified I/O) *
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
12 ******************************************************************************/
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
13
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
14 #ifdef __MSDOS__
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
15 #include <io.h>
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
16 #include <alloc.h>
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
17 #include <stdlib.h>
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
18 #include <sys\stat.h>
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
19 #else
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
20 #include <sys/types.h>
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
21 #include <sys/stat.h>
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
22 #endif /* __MSDOS__ */
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
23
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
24 #include <stdio.h>
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
25 #include <stdlib.h>
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
26 #include <string.h>
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
27
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
28 #ifdef HAVE_FCNTL_H
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
29 #include <fcntl.h>
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
30 #endif
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
31
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
32 #include "gifrlib.h"
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
33
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
34 #define PROGRAM_NAME "GIFLIB"
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
35
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
36
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
37 static void DGifGetWord(GifFileType *GifFile, int *Word);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
38 static void DGifSetupDecompress(GifFileType *GifFile);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
39 static void DGifDecompressLine(GifFileType *GifFile, GifPixelType *Line,
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
40 int LineLen);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
41 static int DGifGetPrefixChar(unsigned int *Prefix, int Code, int ClearCode);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
42 static void DGifDecompressInput(GifFileType *GifFile, int *Code);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
43 static void DGifBufferedInput(GifFileType *GifFile, GifByteType *NextByte);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
44
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
45 /******************************************************************************
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
46 * Open a new gif file for read, given by its name. *
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
47 * Returns GifFileType pointer dynamically allocated which serves as the gif *
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
48 * info record. *
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
49 ******************************************************************************/
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
50 void DGifOpenFileName(GifFileType *GifFile, const char *FileName)
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
51 {
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
52 FILE *f;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
53
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
54 if ((f = fopen(FileName,
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
55 #ifdef __MSDOS__
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
56 "rb"
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
57 #else
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
58 "r"
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
59 #endif /* __MSDOS__ */
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
60 )) == NULL)
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
61 GifInternError(GifFile, D_GIF_ERR_OPEN_FAILED);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
62
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
63 GifStdIOInit(GifFile, f, -1);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
64 DGifInitRead(GifFile);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
65 }
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
66
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
67 /******************************************************************************
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
68 * Update a new gif file, given its file handle. *
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
69 * Returns GifFileType pointer dynamically allocated which serves as the gif *
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
70 * info record. *
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
71 ******************************************************************************/
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
72 void DGifOpenFileHandle(GifFileType *GifFile, int FileHandle)
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
73 {
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
74 FILE *f;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
75
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
76 #ifdef __MSDOS__
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
77 setmode(FileHandle, O_BINARY); /* Make sure it is in binary mode. */
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
78 f = fdopen(FileHandle, "rb"); /* Make it into a stream: */
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
79 setvbuf(f, NULL, _IOFBF, GIF_FILE_BUFFER_SIZE);/* And inc. stream buffer.*/
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
80 #else
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
81 f = fdopen(FileHandle, "r"); /* Make it into a stream: */
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
82 #endif /* __MSDOS__ */
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
83
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
84 GifStdIOInit(GifFile, f, -1);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
85 DGifInitRead(GifFile);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
86 }
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
87
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
88 /******************************************************************************
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
89 * Update a new gif file, given its file handle. *
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
90 * Returns GifFileType pointer dynamically allocated which serves as the gif *
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
91 * info record. _GifError is cleared if succesfull. *
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
92 ******************************************************************************/
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
93 void DGifInitRead(GifFileType *GifFile)
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
94 {
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
95 GifByteType Buf[GIF_STAMP_LEN+1];
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
96 GifFilePrivateType *Private;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
97
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
98 if ((Private = (GifFilePrivateType *) malloc(sizeof(GifFilePrivateType)))
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
99 == NULL) {
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
100 GifInternError(GifFile, D_GIF_ERR_NOT_ENOUGH_MEM);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
101 }
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
102 memset(Private, '\0', sizeof(GifFilePrivateType));
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
103 GifFile->Private = (VoidPtr) Private;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
104
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
105 Private->FileState = 0; /* Make sure bit 0 = 0 (File open for read). */
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
106
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
107 /* Lets see if this is a GIF file: */
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
108 GifRead(Buf, GIF_STAMP_LEN, GifFile);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
109
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
110 /* The GIF Version number is ignored at this time. Maybe we should do */
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
111 /* something more useful with it. */
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
112 Buf[GIF_STAMP_LEN] = 0;
321
19dcec799385 Import from CVS: tag r21-0-58
cvs
parents: 308
diff changeset
113 if (strncmp(GIF_STAMP, (const char *) Buf, GIF_VERSION_POS) != 0) {
308
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
114 GifInternError(GifFile, D_GIF_ERR_NOT_GIF_FILE);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
115 }
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
116
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
117 DGifGetScreenDesc(GifFile);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
118 }
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
119
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
120 /******************************************************************************
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
121 * This routine should be called before any other DGif calls. Note that *
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
122 * this routine is called automatically from DGif file open routines. *
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
123 ******************************************************************************/
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
124 void DGifGetScreenDesc(GifFileType *GifFile)
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
125 {
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
126 int i, BitsPerPixel;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
127 GifByteType Buf[3];
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
128 GifFilePrivateType *Private = (GifFilePrivateType*) GifFile->Private;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
129
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
130 if (!IS_READABLE(Private)) {
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
131 /* This file was NOT open for reading: */
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
132 GifInternError(GifFile, D_GIF_ERR_NOT_READABLE);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
133 }
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
134
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
135 /* Put the screen descriptor into the file: */
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
136 DGifGetWord(GifFile, &GifFile->SWidth);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
137 DGifGetWord(GifFile, &GifFile->SHeight);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
138
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
139 GifRead(Buf, 3, GifFile);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
140 GifFile->SColorResolution = (((Buf[0] & 0x70) + 1) >> 4) + 1;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
141 BitsPerPixel = (Buf[0] & 0x07) + 1;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
142 GifFile->SBackGroundColor = Buf[1];
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
143 if (Buf[0] & 0x80) { /* Do we have global color map? */
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
144
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
145 GifFile->SColorMap = MakeMapObject(1 << BitsPerPixel, NULL);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
146
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
147 /* Get the global color map: */
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
148 for (i = 0; i < GifFile->SColorMap->ColorCount; i++) {
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
149 GifRead(Buf, 3, GifFile);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
150 GifFile->SColorMap->Colors[i].Red = Buf[0];
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
151 GifFile->SColorMap->Colors[i].Green = Buf[1];
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
152 GifFile->SColorMap->Colors[i].Blue = Buf[2];
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
153 }
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
154 } else {
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
155 /* We should always have a colormap */
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
156 GifFile->SColorMap = MakeMapObject(2, NULL);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
157 GifFile->SColorMap->Colors[0].Red = 0;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
158 GifFile->SColorMap->Colors[0].Green = 0;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
159 GifFile->SColorMap->Colors[0].Blue = 0;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
160 GifFile->SColorMap->Colors[1].Red = 0xff;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
161 GifFile->SColorMap->Colors[1].Green = 0xff;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
162 GifFile->SColorMap->Colors[1].Blue = 0xff;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
163 }
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
164 }
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
165
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
166 /******************************************************************************
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
167 * This routine should be called before any attemp to read an image. *
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
168 ******************************************************************************/
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
169 void DGifGetRecordType(GifFileType *GifFile, GifRecordType *Type)
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
170 {
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
171 GifByteType Buf;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
172 GifFilePrivateType *Private = (GifFilePrivateType *) GifFile->Private;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
173
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
174 if (!IS_READABLE(Private)) {
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
175 /* This file was NOT open for reading: */
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
176 GifInternError(GifFile, D_GIF_ERR_NOT_READABLE);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
177 }
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
178
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
179 GifRead(&Buf, 1, GifFile);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
180
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
181 switch (Buf) {
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
182 case ',':
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
183 *Type = IMAGE_DESC_RECORD_TYPE;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
184 break;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
185 case '!':
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
186 *Type = EXTENSION_RECORD_TYPE;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
187 break;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
188 case ';':
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
189 *Type = TERMINATE_RECORD_TYPE;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
190 break;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
191 default:
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
192 *Type = UNDEFINED_RECORD_TYPE;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
193 GifInternError(GifFile, D_GIF_ERR_WRONG_RECORD);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
194 }
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
195 }
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
196
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
197 /******************************************************************************
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
198 * This routine should be called before any attemp to read an image. *
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
199 * Note it is assumed the Image desc. header (',') has been read. *
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
200 ******************************************************************************/
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
201 void DGifGetImageDesc(GifFileType *GifFile)
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
202 {
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
203 int i, BitsPerPixel;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
204 GifByteType Buf[3];
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
205 GifFilePrivateType *Private = (GifFilePrivateType *) GifFile->Private;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
206
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
207 if (!IS_READABLE(Private)) {
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
208 /* This file was NOT open for reading: */
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
209 GifInternError(GifFile, D_GIF_ERR_NOT_READABLE);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
210 }
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
211
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
212 DGifGetWord(GifFile, &GifFile->Image.Left);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
213 DGifGetWord(GifFile, &GifFile->Image.Top);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
214 DGifGetWord(GifFile, &GifFile->Image.Width);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
215 DGifGetWord(GifFile, &GifFile->Image.Height);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
216
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
217 GifRead(Buf, 1, GifFile);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
218 BitsPerPixel = (Buf[0] & 0x07) + 1;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
219 GifFile->Image.Interlace = (Buf[0] & 0x40);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
220 if (Buf[0] & 0x80) { /* Does this image have local color map? */
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
221
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
222 if (GifFile->Image.ColorMap && GifFile->SavedImages == NULL)
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
223 FreeMapObject(GifFile->Image.ColorMap);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
224
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
225 GifFile->Image.ColorMap = MakeMapObject(1 << BitsPerPixel, NULL);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
226
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
227 /* Get the image local color map: */
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
228 for (i = 0; i < GifFile->Image.ColorMap->ColorCount; i++) {
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
229 GifRead(Buf, 3, GifFile);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
230 GifFile->Image.ColorMap->Colors[i].Red = Buf[0];
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
231 GifFile->Image.ColorMap->Colors[i].Green = Buf[1];
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
232 GifFile->Image.ColorMap->Colors[i].Blue = Buf[2];
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
233 }
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
234 }
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
235
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
236 if (GifFile->SavedImages) {
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
237 SavedImage *sp;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
238
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
239 if ((GifFile->SavedImages = (SavedImage *)realloc(GifFile->SavedImages,
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
240 sizeof(SavedImage) * (GifFile->ImageCount + 1))) == NULL) {
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
241 GifInternError(GifFile, D_GIF_ERR_NOT_ENOUGH_MEM);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
242 }
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
243
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
244 sp = &GifFile->SavedImages[GifFile->ImageCount];
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
245 memcpy(&sp->ImageDesc, &GifFile->Image, sizeof(GifImageDesc));
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
246 if (GifFile->Image.ColorMap)
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
247 {
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
248 sp->ImageDesc.ColorMap =
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
249 MakeMapObject (GifFile->Image.ColorMap->ColorCount,
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
250 GifFile->Image.ColorMap->Colors);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
251 }
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
252 sp->RasterBits = (GifPixelType *)NULL;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
253 sp->ExtensionBlockCount = 0;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
254 sp->ExtensionBlocks = (ExtensionBlock *)NULL;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
255 }
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
256
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
257 GifFile->ImageCount++;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
258
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
259 Private->PixelCount = (long) GifFile->Image.Width *
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
260 (long) GifFile->Image.Height;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
261
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
262 DGifSetupDecompress(GifFile); /* Reset decompress algorithm parameters. */
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
263 }
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
264
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
265 /******************************************************************************
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
266 * Get one full scanned line (Line) of length LineLen from GIF file. *
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
267 ******************************************************************************/
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
268 void DGifGetLine(GifFileType *GifFile, GifPixelType *Line, int LineLen)
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
269 {
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
270 GifByteType *Dummy;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
271 GifFilePrivateType *Private = (GifFilePrivateType *) GifFile->Private;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
272
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
273 if (!IS_READABLE(Private)) {
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
274 /* This file was NOT open for reading: */
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
275 GifInternError(GifFile, D_GIF_ERR_NOT_READABLE);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
276 }
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
277
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
278 if (!LineLen) LineLen = GifFile->Image.Width;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
279
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
280 #if defined(__MSDOS__) || defined(__GNUC__)
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
281 if ((Private->PixelCount -= LineLen) > 0xffff0000UL)
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
282 #else
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
283 if ((Private->PixelCount -= LineLen) > 0xffff0000)
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
284 #endif /* __MSDOS__ */
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
285 {
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
286 GifInternError(GifFile, D_GIF_ERR_DATA_TOO_BIG);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
287 }
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
288
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
289 DGifDecompressLine(GifFile, Line, LineLen);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
290 if (Private->PixelCount == 0) {
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
291 /* We probably would not be called any more, so lets clean */
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
292 /* everything before we return: need to flush out all rest of */
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
293 /* image until empty block (size 0) detected. We use GetCodeNext.*/
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
294 do
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
295 DGifGetCodeNext(GifFile, &Dummy);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
296 while (Dummy != NULL);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
297 }
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
298 }
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
299
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
300 /******************************************************************************
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
301 * Put one pixel (Pixel) into GIF file. *
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
302 ******************************************************************************/
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
303 void DGifGetPixel(GifFileType *GifFile, GifPixelType Pixel)
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
304 {
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
305 GifByteType *Dummy;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
306 GifFilePrivateType *Private = (GifFilePrivateType *) GifFile->Private;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
307
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
308 if (!IS_READABLE(Private)) {
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
309 /* This file was NOT open for reading: */
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
310 GifInternError(GifFile, D_GIF_ERR_NOT_READABLE);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
311 }
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
312
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
313 #if defined(__MSDOS__) || defined(__GNUC__)
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
314 if (--Private->PixelCount > 0xffff0000UL)
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
315 #else
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
316 if (--Private->PixelCount > 0xffff0000)
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
317 #endif /* __MSDOS__ */
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
318 {
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
319 GifInternError(GifFile, D_GIF_ERR_DATA_TOO_BIG);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
320 }
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
321
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
322 DGifDecompressLine(GifFile, &Pixel, 1);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
323 if (Private->PixelCount == 0) {
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
324 /* We probably would not be called any more, so lets clean */
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
325 /* everything before we return: need to flush out all rest of */
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
326 /* image until empty block (size 0) detected. We use GetCodeNext.*/
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
327 do
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
328 DGifGetCodeNext(GifFile, &Dummy);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
329 while (Dummy != NULL);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
330 }
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
331 }
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
332
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
333 /******************************************************************************
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
334 * Get an extension block (see GIF manual) from gif file. This routine only *
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
335 * returns the first data block, and DGifGetExtensionNext shouldbe called *
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
336 * after this one until NULL extension is returned. *
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
337 * The Extension should NOT be freed by the user (not dynamically allocated).*
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
338 * Note it is assumed the Extension desc. header ('!') has been read. *
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
339 ******************************************************************************/
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
340 void DGifGetExtension(GifFileType *GifFile, int *ExtCode,
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
341 GifByteType **Extension)
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
342 {
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
343 GifByteType Buf;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
344 GifFilePrivateType *Private = (GifFilePrivateType *) GifFile->Private;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
345
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
346 if (!IS_READABLE(Private)) {
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
347 /* This file was NOT open for reading: */
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
348 GifInternError(GifFile, D_GIF_ERR_NOT_READABLE);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
349 }
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
350
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
351 GifRead(&Buf, 1, GifFile);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
352 *ExtCode = Buf;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
353
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
354 DGifGetExtensionNext(GifFile, Extension);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
355 }
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
356
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
357 /******************************************************************************
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
358 * Get a following extension block (see GIF manual) from gif file. This *
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
359 * routine sould be called until NULL Extension is returned. *
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
360 * The Extension should NOT be freed by the user (not dynamically allocated).*
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
361 ******************************************************************************/
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
362 void DGifGetExtensionNext(GifFileType *GifFile, GifByteType **Extension)
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
363 {
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
364 GifByteType Buf;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
365 GifFilePrivateType *Private = (GifFilePrivateType *) GifFile->Private;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
366
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
367 GifRead(&Buf, 1, GifFile);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
368 if (Buf > 0) {
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
369 *Extension = Private->Buf; /* Use private unused buffer. */
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
370 (*Extension)[0] = Buf; /* Pascal strings notation (pos. 0 is len.). */
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
371 GifRead(&((*Extension)[1]), Buf, GifFile);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
372 }
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
373 else
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
374 *Extension = NULL;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
375 }
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
376
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
377 /******************************************************************************
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
378 * This routine should be called second to last, to close the GIF file. *
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
379 ******************************************************************************/
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
380 int DGifCloseFile(GifFileType *GifFile)
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
381 {
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
382 GifFilePrivateType *Private = (GifFilePrivateType *)GifFile->Private;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
383
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
384 if (GifFile == NULL) return -1;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
385
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
386 if (!IS_READABLE(Private))
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
387 {
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
388 /* This file was NOT open for reading: */
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
389 GifInternError(GifFile, D_GIF_ERR_NOT_READABLE);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
390 }
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
391
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
392 if (GifClose (GifFile))
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
393 {
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
394 GifInternError(GifFile, D_GIF_ERR_CLOSE_FAILED);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
395 }
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
396 return 0;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
397 }
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
398
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
399 /******************************************************************************
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
400 * Get 2 bytes (word) from the given file: *
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
401 ******************************************************************************/
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
402 static void DGifGetWord(GifFileType *GifFile, int *Word)
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
403 {
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
404 unsigned char c[2];
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
405
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
406 GifRead(c, 2, GifFile);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
407
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
408 *Word = (((unsigned int) c[1]) << 8) + c[0];
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
409 }
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
410
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
411 /******************************************************************************
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
412 * Get the image code in compressed form. his routine can be called if the *
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
413 * information needed to be piped out as is. Obviously this is much faster *
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
414 * than decoding and encoding again. This routine should be followed by calls *
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
415 * to DGifGetCodeNext, until NULL block is returned. *
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
416 * The block should NOT be freed by the user (not dynamically allocated). *
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
417 ******************************************************************************/
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
418 void DGifGetCode(GifFileType *GifFile, int *CodeSize, GifByteType **CodeBlock)
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
419 {
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
420 GifFilePrivateType *Private = (GifFilePrivateType *) GifFile->Private;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
421
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
422 if (!IS_READABLE(Private)) {
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
423 /* This file was NOT open for reading: */
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
424 GifInternError(GifFile, D_GIF_ERR_NOT_READABLE);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
425 }
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
426
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
427 *CodeSize = Private->BitsPerPixel;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
428
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
429 DGifGetCodeNext(GifFile, CodeBlock);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
430 }
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
431
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
432 /******************************************************************************
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
433 * Continue to get the image code in compressed form. This routine should be *
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
434 * called until NULL block is returned. *
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
435 * The block should NOT be freed by the user (not dynamically allocated). *
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
436 ******************************************************************************/
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
437 void DGifGetCodeNext(GifFileType *GifFile, GifByteType **CodeBlock)
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
438 {
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
439 GifByteType Buf;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
440 GifFilePrivateType *Private = (GifFilePrivateType *) GifFile->Private;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
441
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
442 GifRead(&Buf, 1, GifFile);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
443
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
444 if (Buf > 0) {
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
445 *CodeBlock = Private->Buf; /* Use private unused buffer. */
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
446 (*CodeBlock)[0] = Buf; /* Pascal strings notation (pos. 0 is len.). */
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
447 GifRead(&((*CodeBlock)[1]), Buf, GifFile);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
448 }
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
449 else {
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
450 *CodeBlock = NULL;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
451 Private->Buf[0] = 0; /* Make sure the buffer is empty! */
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
452 Private->PixelCount = 0; /* And local info. indicate image read. */
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
453 }
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
454
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
455 }
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
456
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
457 /******************************************************************************
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
458 * Setup the LZ decompression for this image: *
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
459 ******************************************************************************/
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
460 static void DGifSetupDecompress(GifFileType *GifFile)
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
461 {
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
462 int i, BitsPerPixel;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
463 GifByteType CodeSize;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
464 unsigned int *Prefix;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
465 GifFilePrivateType *Private = (GifFilePrivateType *) GifFile->Private;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
466
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
467 GifRead(&CodeSize, 1, GifFile); /* Read Code size from file. */
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
468 BitsPerPixel = CodeSize;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
469
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
470 Private->Buf[0] = 0; /* Input Buffer empty. */
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
471 Private->BitsPerPixel = BitsPerPixel;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
472 Private->ClearCode = (1 << BitsPerPixel);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
473 Private->EOFCode = Private->ClearCode + 1;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
474 Private->RunningCode = Private->EOFCode + 1;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
475 Private->RunningBits = BitsPerPixel + 1; /* Number of bits per code. */
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
476 Private->MaxCode1 = 1 << Private->RunningBits; /* Max. code + 1. */
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
477 Private->StackPtr = 0; /* No pixels on the pixel stack. */
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
478 Private->LastCode = NO_SUCH_CODE;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
479 Private->CrntShiftState = 0; /* No information in CrntShiftDWord. */
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
480 Private->CrntShiftDWord = 0;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
481
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
482 Prefix = Private->Prefix;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
483 for (i = 0; i <= LZ_MAX_CODE; i++) Prefix[i] = NO_SUCH_CODE;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
484 }
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
485
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
486 /******************************************************************************
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
487 * The LZ decompression routine: *
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
488 * This version decompress the given gif file into Line of length LineLen. *
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
489 * This routine can be called few times (one per scan line, for example), in *
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
490 * order the complete the whole image. *
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
491 ******************************************************************************/
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
492 static void DGifDecompressLine(GifFileType *GifFile, GifPixelType *Line,
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
493 int LineLen)
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
494 {
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
495 int i = 0, j, CrntCode, EOFCode, ClearCode, CrntPrefix, LastCode, StackPtr;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
496 GifByteType *Stack, *Suffix;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
497 unsigned int *Prefix;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
498 GifFilePrivateType *Private = (GifFilePrivateType *) GifFile->Private;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
499
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
500 StackPtr = Private->StackPtr;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
501 Prefix = Private->Prefix;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
502 Suffix = Private->Suffix;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
503 Stack = Private->Stack;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
504 EOFCode = Private->EOFCode;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
505 ClearCode = Private->ClearCode;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
506 LastCode = Private->LastCode;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
507
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
508 CrntPrefix = 0;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
509 if (StackPtr != 0) {
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
510 /* Let pop the stack off before continueing to read the gif file: */
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
511 while (StackPtr != 0 && i < LineLen) Line[i++] = Stack[--StackPtr];
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
512 }
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
513
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
514 while (i < LineLen) { /* Decode LineLen items. */
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
515 DGifDecompressInput(GifFile, &CrntCode);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
516
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
517 if (CrntCode == EOFCode) {
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
518 /* Note however that usually we will not be here as we will stop */
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
519 /* decoding as soon as we got all the pixel, or EOF code will */
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
520 /* not be read at all, and DGifGetLine/Pixel clean everything. */
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
521 if (i != LineLen - 1 || Private->PixelCount != 0) {
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
522 GifInternError(GifFile, D_GIF_ERR_EOF_TOO_SOON);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
523 }
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
524 i++;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
525 }
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
526 else if (CrntCode == ClearCode) {
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
527 /* We need to start over again: */
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
528 for (j = 0; j <= LZ_MAX_CODE; j++) Prefix[j] = NO_SUCH_CODE;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
529 Private->RunningCode = Private->EOFCode + 1;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
530 Private->RunningBits = Private->BitsPerPixel + 1;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
531 Private->MaxCode1 = 1 << Private->RunningBits;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
532 LastCode = Private->LastCode = NO_SUCH_CODE;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
533 }
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
534 else {
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
535 /* Its regular code - if in pixel range simply add it to output */
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
536 /* stream, otherwise trace to codes linked list until the prefix */
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
537 /* is in pixel range: */
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
538 if (CrntCode < ClearCode) {
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
539 /* This is simple - its pixel scalar, so add it to output: */
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
540 Line[i++] = CrntCode;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
541 }
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
542 else {
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
543 /* Its a code to needed to be traced: trace the linked list */
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
544 /* until the prefix is a pixel, while pushing the suffix */
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
545 /* pixels on our stack. If we done, pop the stack in reverse */
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
546 /* (thats what stack is good for!) order to output. */
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
547 if (Prefix[CrntCode] == NO_SUCH_CODE) {
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
548 /* Only allowed if CrntCode is exactly the running code: */
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
549 /* In that case CrntCode = XXXCode, CrntCode or the */
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
550 /* prefix code is last code and the suffix char is */
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
551 /* exactly the prefix of last code! */
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
552 if (CrntCode == Private->RunningCode - 2) {
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
553 CrntPrefix = LastCode;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
554 Suffix[Private->RunningCode - 2] =
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
555 Stack[StackPtr++] = DGifGetPrefixChar(Prefix,
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
556 LastCode, ClearCode);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
557 }
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
558 else {
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
559 GifInternError(GifFile, D_GIF_ERR_IMAGE_DEFECT);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
560 }
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
561 }
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
562 else
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
563 CrntPrefix = CrntCode;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
564
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
565 /* Now (if image is O.K.) we should not get an NO_SUCH_CODE */
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
566 /* During the trace. As we might loop forever, in case of */
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
567 /* defective image, we count the number of loops we trace */
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
568 /* and stop if we got LZ_MAX_CODE. obviously we can not */
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
569 /* loop more than that. */
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
570 j = 0;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
571 while (j++ <= LZ_MAX_CODE &&
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
572 CrntPrefix > ClearCode &&
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
573 CrntPrefix <= LZ_MAX_CODE) {
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
574 Stack[StackPtr++] = Suffix[CrntPrefix];
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
575 CrntPrefix = Prefix[CrntPrefix];
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
576 }
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
577 if (j >= LZ_MAX_CODE || CrntPrefix > LZ_MAX_CODE) {
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
578 GifInternError(GifFile, D_GIF_ERR_IMAGE_DEFECT);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
579 }
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
580 /* Push the last character on stack: */
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
581 Stack[StackPtr++] = CrntPrefix;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
582
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
583 /* Now lets pop all the stack into output: */
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
584 while (StackPtr != 0 && i < LineLen)
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
585 Line[i++] = Stack[--StackPtr];
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
586 }
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
587 if (LastCode != NO_SUCH_CODE) {
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
588 Prefix[Private->RunningCode - 2] = LastCode;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
589
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
590 if (CrntCode == Private->RunningCode - 2) {
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
591 /* Only allowed if CrntCode is exactly the running code: */
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
592 /* In that case CrntCode = XXXCode, CrntCode or the */
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
593 /* prefix code is last code and the suffix char is */
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
594 /* exactly the prefix of last code! */
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
595 Suffix[Private->RunningCode - 2] =
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
596 DGifGetPrefixChar(Prefix, LastCode, ClearCode);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
597 }
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
598 else {
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
599 Suffix[Private->RunningCode - 2] =
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
600 DGifGetPrefixChar(Prefix, CrntCode, ClearCode);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
601 }
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
602 }
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
603 LastCode = CrntCode;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
604 }
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
605 }
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
606
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
607 Private->LastCode = LastCode;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
608 Private->StackPtr = StackPtr;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
609 }
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
610
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
611 /******************************************************************************
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
612 * Routine to trace the Prefixes linked list until we get a prefix which is *
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
613 * not code, but a pixel value (less than ClearCode). Returns that pixel value.*
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
614 * If image is defective, we might loop here forever, so we limit the loops to *
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
615 * the maximum possible if image O.k. - LZ_MAX_CODE times. *
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
616 ******************************************************************************/
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
617 static int DGifGetPrefixChar(unsigned int *Prefix, int Code, int ClearCode)
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
618 {
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
619 int i = 0;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
620
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
621 while (Code > ClearCode && i++ <= LZ_MAX_CODE) Code = Prefix[Code];
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
622 return Code;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
623 }
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
624
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
625 /******************************************************************************
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
626 * Interface for accessing the LZ codes directly. Set Code to the real code *
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
627 * (12bits), or to -1 if EOF code is returned. *
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
628 ******************************************************************************/
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
629 void DGifGetLZCodes(GifFileType *GifFile, int *Code)
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
630 {
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
631 GifByteType *CodeBlock;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
632 GifFilePrivateType *Private = (GifFilePrivateType *) GifFile->Private;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
633
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
634 if (!IS_READABLE(Private)) {
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
635 /* This file was NOT open for reading: */
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
636 GifInternError(GifFile, D_GIF_ERR_NOT_READABLE);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
637 }
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
638
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
639 DGifDecompressInput(GifFile, Code);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
640
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
641 if (*Code == Private->EOFCode) {
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
642 /* Skip rest of codes (hopefully only NULL terminating block): */
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
643 do
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
644 DGifGetCodeNext(GifFile, &CodeBlock);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
645 while (CodeBlock != NULL);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
646
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
647 *Code = -1;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
648 }
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
649 else if (*Code == Private->ClearCode) {
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
650 /* We need to start over again: */
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
651 Private->RunningCode = Private->EOFCode + 1;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
652 Private->RunningBits = Private->BitsPerPixel + 1;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
653 Private->MaxCode1 = 1 << Private->RunningBits;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
654 }
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
655 }
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
656
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
657 /******************************************************************************
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
658 * The LZ decompression input routine: *
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
659 * This routine is responsable for the decompression of the bit stream from *
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
660 * 8 bits (bytes) packets, into the real codes. *
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
661 * Returns GIF_OK if read succesfully. *
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
662 ******************************************************************************/
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
663 static void DGifDecompressInput(GifFileType *GifFile, int *Code)
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
664 {
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
665 GifFilePrivateType *Private = (GifFilePrivateType *) GifFile->Private;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
666 GifByteType NextByte;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
667 static unsigned int CodeMasks[] = {
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
668 0x0000, 0x0001, 0x0003, 0x0007,
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
669 0x000f, 0x001f, 0x003f, 0x007f,
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
670 0x00ff, 0x01ff, 0x03ff, 0x07ff,
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
671 0x0fff
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
672 };
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
673
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
674 while (Private->CrntShiftState < Private->RunningBits) {
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
675 /* Needs to get more bytes from input stream for next code: */
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
676 DGifBufferedInput(GifFile, &NextByte);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
677 Private->CrntShiftDWord |=
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
678 ((unsigned long) NextByte) << Private->CrntShiftState;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
679 Private->CrntShiftState += 8;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
680 }
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
681 *Code = Private->CrntShiftDWord & CodeMasks[Private->RunningBits];
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
682
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
683 Private->CrntShiftDWord >>= Private->RunningBits;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
684 Private->CrntShiftState -= Private->RunningBits;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
685
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
686 /* If code cannt fit into RunningBits bits, must raise its size. Note */
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
687 /* however that codes above 4095 are used for special signaling. */
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
688 if (++Private->RunningCode > Private->MaxCode1 &&
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
689 Private->RunningBits < LZ_BITS) {
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
690 Private->MaxCode1 <<= 1;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
691 Private->RunningBits++;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
692 }
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
693 }
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
694
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
695 /******************************************************************************
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
696 * This routines read one gif data block at a time and buffers it internally *
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
697 * so that the decompression routine could access it. *
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
698 * The routine returns the next byte from its internal buffer (or read next *
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
699 * block in if buffer empty) *
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
700 ******************************************************************************/
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
701 static void DGifBufferedInput(GifFileType *GifFile, GifByteType *NextByte)
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
702 {
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
703 GifFilePrivateType *Private = (GifFilePrivateType *) GifFile->Private;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
704 GifByteType *Buf = Private->Buf;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
705
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
706 if (Buf[0] == 0) {
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
707 /* Needs to read the next buffer - this one is empty: */
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
708 GifRead(Buf, 1, GifFile);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
709 GifRead((Buf + 1), Buf[0], GifFile);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
710 *NextByte = Buf[1];
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
711 Buf[1] = 2; /* We use now the second place as last char read! */
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
712 Buf[0]--;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
713 }
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
714 else {
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
715 *NextByte = Buf[Buf[1]++];
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
716 Buf[0]--;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
717 }
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
718 }
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
719
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
720 /******************************************************************************
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
721 * This routine reads an entire GIF into core, hanging all its state info off *
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
722 * the GifFileType pointer. Call DGifOpenFileName() or DGifOpenFileHandle() *
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
723 * first to initialize I/O. Its inverse is EGifSpew(). *
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
724 ******************************************************************************/
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
725 void DGifSlurp(GifFileType *GifFile)
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
726 {
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
727 int ImageSize;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
728 GifRecordType RecordType;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
729 SavedImage *sp;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
730 GifByteType *ExtData;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
731
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
732 /* Some versions of malloc dislike 0-length requests */
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
733 GifFile->SavedImages = (SavedImage *)malloc(sizeof(SavedImage));
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
734 memset(GifFile->SavedImages, 0, sizeof(SavedImage));
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
735 sp = &GifFile->SavedImages[0];
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
736
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
737 do {
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
738 DGifGetRecordType(GifFile, &RecordType);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
739
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
740 switch (RecordType) {
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
741 case IMAGE_DESC_RECORD_TYPE:
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
742 DGifGetImageDesc(GifFile);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
743
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
744 sp = &GifFile->SavedImages[GifFile->ImageCount-1];
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
745 ImageSize = sp->ImageDesc.Width * sp->ImageDesc.Height;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
746
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
747 sp->RasterBits
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
748 = (GifPixelType*) malloc(ImageSize * sizeof(GifPixelType));
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
749
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
750 DGifGetLine(GifFile, sp->RasterBits, ImageSize);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
751 break;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
752
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
753 case EXTENSION_RECORD_TYPE:
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
754 DGifGetExtension(GifFile,&sp->Function,&ExtData);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
755
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
756 do {
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
757 if (AddExtensionBlock(sp, ExtData[0], ExtData+1) == GIF_ERROR)
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
758 GifInternError(GifFile, D_GIF_ERR_NOT_ENOUGH_MEM);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
759 DGifGetExtensionNext(GifFile, &ExtData);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
760 } while (ExtData != NULL);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
761 break;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
762
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
763 case TERMINATE_RECORD_TYPE:
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
764 break;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
765
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
766 default: /* Should be trapped by DGifGetRecordType */
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
767 break;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
768 }
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
769 } while (RecordType != TERMINATE_RECORD_TYPE);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
770 }
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
771
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
772 /******************************************************************************
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
773 * Extension record functions *
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
774 ******************************************************************************/
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
775
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
776 void MakeExtension(SavedImage *New, int Function)
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
777 {
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
778 New->Function = Function;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
779 /*
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
780 * Someday we might have to deal with multiple extensions.
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
781 */
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
782 }
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
783
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
784 int AddExtensionBlock(SavedImage *New, int Len, GifByteType *data)
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
785 {
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
786 int size;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
787 ExtensionBlock *ep;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
788
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
789 if (New->ExtensionBlocks == NULL)
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
790 New->ExtensionBlocks = (ExtensionBlock *)malloc(sizeof(ExtensionBlock));
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
791 else
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
792 New->ExtensionBlocks =
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
793 (ExtensionBlock *)realloc(New->ExtensionBlocks,
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
794 sizeof(ExtensionBlock) * (New->ExtensionBlockCount + 1));
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
795
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
796 if (New->ExtensionBlocks == NULL)
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
797 return(GIF_ERROR);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
798
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
799 ep = &New->ExtensionBlocks[New->ExtensionBlockCount++];
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
800 ep->ByteCount = Len;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
801 size = Len * sizeof(GifByteType);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
802 ep->Bytes = (GifByteType *)malloc(size);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
803 memcpy(ep->Bytes, data, size);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
804 return(GIF_OK);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
805 }
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
806
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
807 void FreeExtension(SavedImage *Image)
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
808 {
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
809 ExtensionBlock *ep;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
810
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
811 for (ep = Image->ExtensionBlocks;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
812 ep < Image->ExtensionBlocks + Image->ExtensionBlockCount;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
813 ep++)
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
814 (void) free((char *)ep->Bytes);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
815 free((char *)Image->ExtensionBlocks);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
816 Image->ExtensionBlocks = NULL;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
817 }
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
818
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
819 /******************************************************************************
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
820 * Image block allocation functions *
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
821 ******************************************************************************/
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
822 /*
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
823 * Append an image block to the SavedImages array
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
824 */
321
19dcec799385 Import from CVS: tag r21-0-58
cvs
parents: 308
diff changeset
825 SavedImage *
19dcec799385 Import from CVS: tag r21-0-58
cvs
parents: 308
diff changeset
826 MakeSavedImage(GifFileType *GifFile, SavedImage *CopyFrom)
308
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
827 {
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
828 SavedImage *sp;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
829
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
830 if (GifFile->SavedImages == NULL)
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
831 GifFile->SavedImages = (SavedImage *)malloc(sizeof(SavedImage));
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
832 else
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
833 GifFile->SavedImages = (SavedImage *)realloc(GifFile->SavedImages,
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
834 sizeof(SavedImage) * (GifFile->ImageCount+1));
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
835
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
836 if (GifFile->SavedImages == NULL)
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
837 return((SavedImage *)NULL);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
838 else
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
839 {
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
840 sp = &GifFile->SavedImages[GifFile->ImageCount++];
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
841 memset((char *)sp, '\0', sizeof(SavedImage));
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
842
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
843 if (CopyFrom)
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
844 {
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
845 memcpy((char *)sp, CopyFrom, sizeof(SavedImage));
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
846
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
847 /*
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
848 * Make our own allocated copies of the heap fields in the
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
849 * copied record. This guards against potential aliasing
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
850 * problems.
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
851 */
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
852
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
853 /* first, the local color map */
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
854 if (sp->ImageDesc.ColorMap)
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
855 sp->ImageDesc.ColorMap =
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
856 MakeMapObject(CopyFrom->ImageDesc.ColorMap->ColorCount,
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
857 CopyFrom->ImageDesc.ColorMap->Colors);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
858
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
859 /* next, the raster */
321
19dcec799385 Import from CVS: tag r21-0-58
cvs
parents: 308
diff changeset
860 sp->RasterBits = (GifPixelType*)malloc(sizeof(GifPixelType)
308
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
861 * CopyFrom->ImageDesc.Height
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
862 * CopyFrom->ImageDesc.Width);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
863 memcpy(sp->RasterBits,
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
864 CopyFrom->RasterBits,
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
865 sizeof(GifPixelType)
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
866 * CopyFrom->ImageDesc.Height
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
867 * CopyFrom->ImageDesc.Width);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
868
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
869 /* finally, the extension blocks */
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
870 if (sp->ExtensionBlocks)
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
871 {
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
872 sp->ExtensionBlocks
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
873 = (ExtensionBlock*)malloc(sizeof(ExtensionBlock)
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
874 * CopyFrom->ExtensionBlockCount);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
875 memcpy(sp->ExtensionBlocks,
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
876 CopyFrom->ExtensionBlocks,
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
877 sizeof(ExtensionBlock)
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
878 * CopyFrom->ExtensionBlockCount);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
879
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
880 /*
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
881 * For the moment, the actual blocks can take their
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
882 * chances with free(). We'll fix this later.
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
883 */
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
884 }
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
885 }
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
886
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
887 return(sp);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
888 }
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
889 }
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
890
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
891 void FreeSavedImages(GifFileType *GifFile)
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
892 {
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
893 SavedImage *sp;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
894
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
895 for (sp = GifFile->SavedImages;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
896 sp < GifFile->SavedImages + GifFile->ImageCount;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
897 sp++)
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
898 {
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
899 if (sp->ImageDesc.ColorMap)
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
900 FreeMapObject(sp->ImageDesc.ColorMap);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
901
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
902 if (sp->RasterBits)
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
903 free((char *)sp->RasterBits);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
904
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
905 if (sp->ExtensionBlocks)
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
906 FreeExtension(sp);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
907 }
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
908 free((char *) GifFile->SavedImages);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
909 }
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
910
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
911 /******************************************************************************
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
912 * Miscellaneous utility functions *
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
913 ******************************************************************************/
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
914
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
915 int BitSize(int n)
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
916 /* return smallest bitfield size n will fit in */
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
917 {
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
918 register int i;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
919
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
920 for (i = 1; i <= 8; i++)
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
921 if ((1 << i) >= n)
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
922 break;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
923 return(i);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
924 }
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
925
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
926 /******************************************************************************
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
927 * Color map object functions *
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
928 ******************************************************************************/
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
929
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
930 ColorMapObject *MakeMapObject(int ColorCount, GifColorType *ColorMap)
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
931 /*
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
932 * Allocate a color map of given size; initialize with contents of
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
933 * ColorMap if that pointer is non-NULL.
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
934 */
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
935 {
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
936 ColorMapObject *Object;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
937
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
938 if (ColorCount != (1 << BitSize(ColorCount)))
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
939 return((ColorMapObject *)NULL);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
940
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
941 Object = (ColorMapObject *)malloc(sizeof(ColorMapObject));
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
942 if (Object == (ColorMapObject *)NULL)
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
943 return((ColorMapObject *)NULL);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
944
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
945 Object->Colors = (GifColorType *)calloc(ColorCount, sizeof(GifColorType));
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
946 if (Object->Colors == (GifColorType *)NULL)
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
947 return((ColorMapObject *)NULL);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
948
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
949 Object->ColorCount = ColorCount;
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
950 Object->BitsPerPixel = BitSize(ColorCount);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
951
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
952 if (ColorMap)
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
953 memcpy((char *)Object->Colors,
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
954 (char *)ColorMap, ColorCount * sizeof(GifColorType));
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
955
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
956 return(Object);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
957 }
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
958
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
959 void FreeMapObject(ColorMapObject *Object)
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
960 /*
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
961 * Free a color map object
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
962 */
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
963 {
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
964 free(Object->Colors);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
965 free(Object);
33bdb3d4b97f Import from CVS: tag r21-0b52
cvs
parents:
diff changeset
966 }