annotate src/dgif_lib.c @ 20:859a2309aef8 r19-15b93

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