442
|
1 #include <config.h>
|
|
2
|
428
|
3 #include <stdio.h>
|
|
4 #include <stdlib.h>
|
|
5 #include <string.h>
|
442
|
6 #ifdef HAVE_UNISTD_H
|
428
|
7 #include <unistd.h>
|
442
|
8 #endif
|
428
|
9 #include "gifrlib.h"
|
|
10 #include "sysfile.h"
|
|
11
|
|
12 /******************************************************************************
|
|
13 * Set up the GifFileType structure for use. This must be called first in any *
|
|
14 * client program. Then, if custom IO or Error functions are desired, call *
|
|
15 * GifSetIOFunc/GifSetErrorFunc, then call EGifInitWrite. Else call *
|
|
16 * EGifOpenFileName or EGifOpenFileHandle for standard IO functions. *
|
|
17 * If setup fails, a NULL pointer is returned. *
|
|
18 ******************************************************************************/
|
|
19 GifFileType *GifSetup(void)
|
|
20 {
|
|
21 GifIODataType *GifIO;
|
|
22 GifFileType *GifFile;
|
|
23
|
|
24 if ((GifFile = (GifFileType *) malloc(sizeof(GifFileType))) == NULL)
|
|
25 return NULL;
|
|
26 memset(GifFile, '\0', sizeof(GifFileType));
|
|
27 if ((GifIO = (GifIODataType *) malloc(sizeof(GifIODataType))) == NULL) {
|
|
28 free((char *) GifFile);
|
|
29 return NULL;
|
|
30 }
|
|
31 memset(GifIO, '\0', sizeof(GifIODataType));
|
|
32 GifFile->GifIO = GifIO;
|
|
33 return GifFile;
|
|
34 }
|
|
35
|
|
36 void GifFree(GifFileType *GifFile)
|
|
37 {
|
|
38 GifFilePrivateType *Private;
|
|
39
|
|
40 if (GifFile == NULL) return;
|
|
41
|
|
42 Private = (GifFilePrivateType *) GifFile->Private;
|
|
43
|
|
44 if (GifFile->SavedImages)
|
|
45 FreeSavedImages(GifFile);
|
|
46 if (GifFile->Image.ColorMap)
|
|
47 FreeMapObject(GifFile->Image.ColorMap);
|
|
48 if (GifFile->SColorMap)
|
|
49 FreeMapObject(GifFile->SColorMap);
|
|
50 if (Private)
|
|
51 {
|
|
52 free(Private);
|
|
53 }
|
|
54 if (GifFile->GifIO)
|
|
55 free(GifFile->GifIO);
|
|
56 free(GifFile);
|
|
57 }
|
|
58
|
|
59 /****************************************************************************
|
|
60 * Install the specified ReadFunction into the GifFile specified. *
|
|
61 ****************************************************************************/
|
|
62 void GifSetReadFunc(GifFileType *GifFile, Gif_rw_func ReadFunc, VoidPtr data)
|
|
63 {
|
|
64 GifIODataType *GifIO = (GifIODataType *)GifFile->GifIO;
|
|
65 GifIO->ReadFunc = ReadFunc;
|
|
66 GifIO->ReadFunc_data = data;
|
|
67 }
|
|
68
|
|
69 /****************************************************************************
|
|
70 * Install the specified WriteFunction into the GifFile specified. *
|
|
71 ****************************************************************************/
|
|
72 void GifSetWriteFunc(GifFileType *GifFile, Gif_rw_func WriteFunc, VoidPtr data)
|
|
73 {
|
|
74 GifIODataType *GifIO = (GifIODataType *)GifFile->GifIO;
|
|
75 GifIO->WriteFunc = WriteFunc;
|
|
76 GifIO->WriteFunc_data = data;
|
|
77 }
|
|
78
|
|
79 /****************************************************************************
|
|
80 * Install the specified CloseFunction into the GifFile specified. *
|
|
81 ****************************************************************************/
|
|
82 void GifSetCloseFunc(GifFileType *GifFile, Gif_close_func CloseFunc, VoidPtr data)
|
|
83 {
|
|
84 GifIODataType *GifIO = (GifIODataType *)GifFile->GifIO;
|
|
85 GifIO->CloseFunc = CloseFunc;
|
|
86 GifIO->CloseFunc_data = data;
|
|
87 }
|
|
88
|
|
89 /****************************************************************************
|
|
90 * Install the standard IO funcs into the GifFile, including the FILE info *
|
|
91 ****************************************************************************/
|
|
92 void GifStdIOInit(GifFileType *GifFile, FILE *file, int filehandle)
|
|
93 {
|
|
94 GifStdIODataType *IOData;
|
|
95
|
|
96 if ((IOData = (GifStdIODataType*)malloc(sizeof(GifStdIODataType))) == NULL)
|
|
97 GifInternError(GifFile, GIF_ERR_NOT_ENOUGH_MEM);
|
|
98 IOData->File = file;
|
|
99 IOData->FileHandle = filehandle;
|
|
100 GifSetReadFunc(GifFile, GifStdRead, IOData);
|
|
101 GifSetWriteFunc(GifFile, GifStdWrite, IOData);
|
|
102 GifSetCloseFunc(GifFile, GifStdFileClose, IOData);
|
|
103 }
|
|
104
|
|
105 size_t GifStdRead(GifByteType *buf, size_t size, VoidPtr method_data)
|
|
106 {
|
|
107 GifStdIODataType *IOtype = (GifStdIODataType*)method_data;
|
|
108 return (fread(buf, 1, size, IOtype->File));
|
|
109 }
|
|
110
|
|
111 size_t GifStdWrite(GifByteType *buf, size_t size, VoidPtr method_data)
|
|
112 {
|
|
113 GifStdIODataType *IOtype = (GifStdIODataType*)method_data;
|
|
114 return (fwrite(buf, 1, size, IOtype->File));
|
|
115 }
|
|
116
|
|
117 int GifStdFileClose(VoidPtr method_data)
|
|
118 {
|
|
119 int ret;
|
|
120 GifStdIODataType *IOtype = (GifStdIODataType*)method_data;
|
|
121 ret = fclose(IOtype->File);
|
|
122 if (ret == 0 && IOtype->FileHandle != -1)
|
|
123 ret = close(IOtype->FileHandle);
|
|
124 return ret;
|
|
125 }
|
|
126
|
|
127 void GifRead(GifByteType *buf, size_t size, GifFileType *GifFile)
|
|
128 {
|
|
129 GifIODataType *GifIO = (GifIODataType*)GifFile->GifIO;
|
|
130 if ((*(GifIO->ReadFunc))(buf, size, GifIO->ReadFunc_data) != size)
|
|
131 GifError(GifFile, "Read error!");
|
|
132 }
|
|
133
|
|
134 void GifWrite(GifByteType *buf, size_t size, GifFileType *GifFile)
|
|
135 {
|
|
136 GifIODataType *GifIO = (GifIODataType*)GifFile->GifIO;
|
|
137 if ((*(GifIO->WriteFunc))(buf, size, GifIO->WriteFunc_data) != size)
|
|
138 GifError(GifFile, "Write error!");
|
|
139 }
|
|
140
|
|
141 int GifClose(GifFileType *GifFile)
|
|
142 {
|
|
143 GifIODataType *GifIO = (GifIODataType*)GifFile->GifIO;
|
|
144 return ((*(GifIO->CloseFunc))(GifIO->CloseFunc_data));
|
|
145 }
|
|
146
|
|
147 static char *GifErrorString[14] = {
|
|
148 "Failed to open given file", /* D_GIF_ERR_OPEN_FAILED */
|
|
149 "Failed to read from given file", /* D_GIF_ERR_READ_FAILED */
|
|
150 "Given file is NOT a GIF file", /* D_GIF_ERR_NOT_GIF_FILE */
|
|
151 "No Screen Descriptor detected", /* D_GIF_ERR_NO_SCRN_DSCR */
|
|
152 "No Image Descriptor detected", /* D_GIF_ERR_NO_IMAG_DSCR */
|
|
153 "No global or local color map", /* D_GIF_ERR_NO_COLOR_MAP */
|
|
154 "Wrong record type detected", /* D_GIF_ERR_WRONG_RECORD */
|
|
155 "#Pixels bigger than Width * Height", /* D_GIF_ERR_DATA_TOO_BIG */
|
|
156 "Fail to allocate required memory", /* D_GIF_ERR_NOT_ENOUGH_MEM */
|
|
157 "Failed to close given file", /* D_GIF_ERR_CLOSE_FAILED */
|
|
158 "Given file was not opened for read", /* D_GIF_ERR_CLOSE_FAILED */
|
|
159 "Image is defective, decoding aborted", /* D_GIF_ERR_IMAGE_DEFECT */
|
|
160 "Image EOF detected before image complete", /* D_GIF_ERR_EOF_TOO_SOON */
|
|
161 "Undefined error!",
|
|
162 };
|
|
163
|
|
164 const char *GetGifError(int error);
|
|
165
|
|
166 /*****************************************************************************
|
|
167 * Get the last GIF error in human-readable form. *
|
|
168 *****************************************************************************/
|
|
169 const char *GetGifError(int error)
|
|
170 {
|
|
171 char *Err;
|
|
172
|
|
173 switch(error) {
|
|
174 case D_GIF_ERR_OPEN_FAILED:
|
|
175 Err = GifErrorString[0];
|
|
176 break;
|
|
177 case D_GIF_ERR_READ_FAILED:
|
|
178 Err = GifErrorString[1];
|
|
179 break;
|
|
180 case D_GIF_ERR_NOT_GIF_FILE:
|
|
181 Err = GifErrorString[2];
|
|
182 break;
|
|
183 case D_GIF_ERR_NO_SCRN_DSCR:
|
|
184 Err = GifErrorString[3];
|
|
185 break;
|
|
186 case D_GIF_ERR_NO_IMAG_DSCR:
|
|
187 Err = GifErrorString[4];
|
|
188 break;
|
|
189 case D_GIF_ERR_NO_COLOR_MAP:
|
|
190 Err = GifErrorString[5];
|
|
191 break;
|
|
192 case D_GIF_ERR_WRONG_RECORD:
|
|
193 Err = GifErrorString[6];
|
|
194 break;
|
|
195 case D_GIF_ERR_DATA_TOO_BIG:
|
|
196 Err = GifErrorString[7];
|
|
197 break;
|
|
198 case D_GIF_ERR_NOT_ENOUGH_MEM:
|
|
199 Err = GifErrorString[8];
|
|
200 break;
|
|
201 case D_GIF_ERR_CLOSE_FAILED:
|
|
202 Err = GifErrorString[9];
|
|
203 break;
|
|
204 case D_GIF_ERR_NOT_READABLE:
|
|
205 Err = GifErrorString[10];
|
|
206 break;
|
|
207 case D_GIF_ERR_IMAGE_DEFECT:
|
|
208 Err = GifErrorString[11];
|
|
209 break;
|
|
210 case D_GIF_ERR_EOF_TOO_SOON:
|
|
211 Err = GifErrorString[12];
|
|
212 break;
|
|
213 default:
|
|
214 Err = GifErrorString[13];
|
|
215 break;
|
|
216 }
|
|
217 return Err;
|
|
218 }
|
|
219
|
|
220 /******************************
|
|
221 * These are called internally *
|
|
222 ******************************/
|
|
223 void GifError(GifFileType *GifFile, const char *err_str)
|
|
224 {
|
|
225 GifIODataType *GifIO = (GifIODataType*)GifFile->GifIO;
|
|
226 if (GifIO->ErrorFunc)
|
|
227 (*(GifIO->ErrorFunc))(err_str, GifIO->ErrorFunc_data);
|
|
228 else
|
|
229 fprintf(stderr, "GIF FATAL ERROR: %s", err_str);
|
|
230 exit(-10);
|
|
231 }
|
|
232
|
|
233 void GifWarning(GifFileType *GifFile, const char *err_str)
|
|
234 {
|
|
235 GifIODataType *GifIO = (GifIODataType*)GifFile->GifIO;
|
|
236 if (GifIO->WarningFunc)
|
|
237 (*(GifIO->WarningFunc))(err_str, GifIO->WarningFunc_data);
|
|
238 }
|
|
239
|
|
240 void GifInternError(GifFileType *GifFile, int error_num)
|
|
241 {
|
|
242 const char *ErrStr = GetGifError(error_num);
|
|
243 GifError(GifFile, ErrStr);
|
|
244 }
|
|
245
|
|
246 void GifInternWarning(GifFileType *GifFile, int error_num)
|
|
247 {
|
|
248 const char *ErrStr = GetGifError(error_num);
|
|
249 GifWarning(GifFile, ErrStr);
|
|
250 }
|
|
251
|
|
252 void GifSetErrorFunc(GifFileType *GifFile, Gif_error_func ErrorFunc, VoidPtr data)
|
|
253 {
|
|
254 GifIODataType *GifIO = (GifIODataType *)GifFile->GifIO;
|
|
255 GifIO->ErrorFunc = ErrorFunc;
|
|
256 GifIO->ErrorFunc_data = data;
|
|
257 }
|
|
258
|
|
259 void GifSetWarningFunc(GifFileType *GifFile, Gif_error_func WarningFunc, VoidPtr data)
|
|
260 {
|
|
261 GifIODataType *GifIO = (GifIODataType *)GifFile->GifIO;
|
|
262 GifIO->WarningFunc = WarningFunc;
|
|
263 GifIO->WarningFunc_data = data;
|
|
264 }
|