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)
24 if ((GifFile = (GifFileType *) malloc(sizeof(GifFileType))) == NULL)
26 memset(GifFile, '\0', sizeof(GifFileType));
27 if ((GifIO = (GifIODataType *) malloc(sizeof(GifIODataType))) == NULL) {
28 free((char *) GifFile);
31 memset(GifIO, '\0', sizeof(GifIODataType));
32 GifFile->GifIO = GifIO;
36 void GifFree(GifFileType *GifFile)
38 GifFilePrivateType *Private;
40 if (GifFile == NULL) return;
42 Private = (GifFilePrivateType *) GifFile->Private;
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);
59 /****************************************************************************
60 * Install the specified ReadFunction into the GifFile specified. *
61 ****************************************************************************/
62 void GifSetReadFunc(GifFileType *GifFile, Gif_rw_func ReadFunc, VoidPtr data)
64 GifIODataType *GifIO = (GifIODataType *)GifFile->GifIO;
65 GifIO->ReadFunc = ReadFunc;
66 GifIO->ReadFunc_data = data;
69 /****************************************************************************
70 * Install the specified WriteFunction into the GifFile specified. *
71 ****************************************************************************/
72 void GifSetWriteFunc(GifFileType *GifFile, Gif_rw_func WriteFunc, VoidPtr data)
74 GifIODataType *GifIO = (GifIODataType *)GifFile->GifIO;
75 GifIO->WriteFunc = WriteFunc;
76 GifIO->WriteFunc_data = data;
79 /****************************************************************************
80 * Install the specified CloseFunction into the GifFile specified. *
81 ****************************************************************************/
82 void GifSetCloseFunc(GifFileType *GifFile, Gif_close_func CloseFunc, VoidPtr data)
84 GifIODataType *GifIO = (GifIODataType *)GifFile->GifIO;
85 GifIO->CloseFunc = CloseFunc;
86 GifIO->CloseFunc_data = data;
89 /****************************************************************************
90 * Install the standard IO funcs into the GifFile, including the FILE info *
91 ****************************************************************************/
92 void GifStdIOInit(GifFileType *GifFile, FILE *file, int filehandle)
94 GifStdIODataType *IOData;
96 if ((IOData = (GifStdIODataType*)malloc(sizeof(GifStdIODataType))) == NULL)
97 GifInternError(GifFile, GIF_ERR_NOT_ENOUGH_MEM);
99 IOData->FileHandle = filehandle;
100 GifSetReadFunc(GifFile, GifStdRead, IOData);
101 GifSetWriteFunc(GifFile, GifStdWrite, IOData);
102 GifSetCloseFunc(GifFile, GifStdFileClose, IOData);
105 size_t GifStdRead(GifByteType *buf, size_t size, VoidPtr method_data)
107 GifStdIODataType *IOtype = (GifStdIODataType*)method_data;
108 return (fread(buf, 1, size, IOtype->File));
111 size_t GifStdWrite(GifByteType *buf, size_t size, VoidPtr method_data)
113 GifStdIODataType *IOtype = (GifStdIODataType*)method_data;
114 return (fwrite(buf, 1, size, IOtype->File));
117 int GifStdFileClose(VoidPtr method_data)
120 GifStdIODataType *IOtype = (GifStdIODataType*)method_data;
121 ret = fclose(IOtype->File);
122 if (ret == 0 && IOtype->FileHandle != -1)
123 ret = close(IOtype->FileHandle);
127 void GifRead(GifByteType *buf, size_t size, GifFileType *GifFile)
129 GifIODataType *GifIO = (GifIODataType*)GifFile->GifIO;
130 if ((*(GifIO->ReadFunc))(buf, size, GifIO->ReadFunc_data) != size)
131 GifError(GifFile, "Read error!");
134 void GifWrite(GifByteType *buf, size_t size, GifFileType *GifFile)
136 GifIODataType *GifIO = (GifIODataType*)GifFile->GifIO;
137 if ((*(GifIO->WriteFunc))(buf, size, GifIO->WriteFunc_data) != size)
138 GifError(GifFile, "Write error!");
141 int GifClose(GifFileType *GifFile)
143 GifIODataType *GifIO = (GifIODataType*)GifFile->GifIO;
144 return ((*(GifIO->CloseFunc))(GifIO->CloseFunc_data));
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 */
164 const char *GetGifError(int error);
166 /*****************************************************************************
167 * Get the last GIF error in human-readable form. *
168 *****************************************************************************/
169 const char *GetGifError(int error)
174 case D_GIF_ERR_OPEN_FAILED:
175 Err = GifErrorString[0];
177 case D_GIF_ERR_READ_FAILED:
178 Err = GifErrorString[1];
180 case D_GIF_ERR_NOT_GIF_FILE:
181 Err = GifErrorString[2];
183 case D_GIF_ERR_NO_SCRN_DSCR:
184 Err = GifErrorString[3];
186 case D_GIF_ERR_NO_IMAG_DSCR:
187 Err = GifErrorString[4];
189 case D_GIF_ERR_NO_COLOR_MAP:
190 Err = GifErrorString[5];
192 case D_GIF_ERR_WRONG_RECORD:
193 Err = GifErrorString[6];
195 case D_GIF_ERR_DATA_TOO_BIG:
196 Err = GifErrorString[7];
198 case D_GIF_ERR_NOT_ENOUGH_MEM:
199 Err = GifErrorString[8];
201 case D_GIF_ERR_CLOSE_FAILED:
202 Err = GifErrorString[9];
204 case D_GIF_ERR_NOT_READABLE:
205 Err = GifErrorString[10];
207 case D_GIF_ERR_IMAGE_DEFECT:
208 Err = GifErrorString[11];
210 case D_GIF_ERR_EOF_TOO_SOON:
211 Err = GifErrorString[12];
214 Err = GifErrorString[13];
220 /******************************
221 * These are called internally *
222 ******************************/
223 void GifError(GifFileType *GifFile, const char *err_str)
225 GifIODataType *GifIO = (GifIODataType*)GifFile->GifIO;
226 if (GifIO->ErrorFunc)
227 (*(GifIO->ErrorFunc))(err_str, GifIO->ErrorFunc_data);
229 fprintf(stderr, "GIF FATAL ERROR: %s", err_str);
233 void GifWarning(GifFileType *GifFile, const char *err_str)
235 GifIODataType *GifIO = (GifIODataType*)GifFile->GifIO;
236 if (GifIO->WarningFunc)
237 (*(GifIO->WarningFunc))(err_str, GifIO->WarningFunc_data);
240 void GifInternError(GifFileType *GifFile, int error_num)
242 const char *ErrStr = GetGifError(error_num);
243 GifError(GifFile, ErrStr);
246 void GifInternWarning(GifFileType *GifFile, int error_num)
248 const char *ErrStr = GetGifError(error_num);
249 GifWarning(GifFile, ErrStr);
252 void GifSetErrorFunc(GifFileType *GifFile, Gif_error_func ErrorFunc, VoidPtr data)
254 GifIODataType *GifIO = (GifIODataType *)GifFile->GifIO;
255 GifIO->ErrorFunc = ErrorFunc;
256 GifIO->ErrorFunc_data = data;
259 void GifSetWarningFunc(GifFileType *GifFile, Gif_error_func WarningFunc, VoidPtr data)
261 GifIODataType *GifIO = (GifIODataType *)GifFile->GifIO;
262 GifIO->WarningFunc = WarningFunc;
263 GifIO->WarningFunc_data = data;