MCPcopy Create free account
hub / github.com/NetHack/NetHack / read_header

Function read_header

win/share/gifread.c:190–287  ·  view source on GitHub ↗

* Read gif header, including colormaps. We expect only one image per * file, so if that image has a local colormap, overwrite the global one. */

Source from the content-addressed store, hash-verified

188 * file, so if that image has a local colormap, overwrite the global one.
189 */
190static void
191read_header(FILE *fd)
192{
193 unsigned char buf[16];
194 unsigned char c;
195 char version[4];
196
197 if (!ReadOK(fd, buf, 6)) {
198 Fprintf(stderr, "error reading magic number\n");
199 exit(EXIT_FAILURE);
200 }
201
202 if (strncmp((genericptr_t) buf, "GIF", 3) != 0) {
203 Fprintf(stderr, "not a GIF file\n");
204 exit(EXIT_FAILURE);
205 }
206
207 (void) strncpy(version, (char *) buf + 3, 3);
208 version[3] = '\0';
209
210 if ((strcmp(version, "87a") != 0) && (strcmp(version, "89a") != 0)) {
211 Fprintf(stderr, "bad version number, not '87a' or '89a'\n");
212 exit(EXIT_FAILURE);
213 }
214
215 if (!ReadOK(fd, buf, 7)) {
216 Fprintf(stderr, "failed to read screen descriptor\n");
217 exit(EXIT_FAILURE);
218 }
219
220 GifScreen.Width = LM_to_uint(buf[0], buf[1]);
221 GifScreen.Height = LM_to_uint(buf[2], buf[3]);
222 GifScreen.Colors = 2 << (buf[4] & 0x07);
223 GifScreen.ColorResolution = (((buf[4] & 0x70) >> 3) + 1);
224 GifScreen.Background = buf[5];
225 GifScreen.AspectRatio = buf[6];
226
227 if (BitSet(buf[4], LOCALCOLORMAP)) { /* Global Colormap */
228 if (!ReadColorMap(fd, GifScreen.Colors)) {
229 Fprintf(stderr, "error reading global colormap\n");
230 exit(EXIT_FAILURE);
231 }
232 }
233
234 if (GifScreen.AspectRatio != 0 && GifScreen.AspectRatio != 49) {
235 Fprintf(stderr, "warning - non-square pixels\n");
236 }
237
238 for (;;) {
239 if (!ReadOK(fd, &c, 1)) {
240 Fprintf(stderr, "EOF / read error on image data\n");
241 exit(EXIT_FAILURE);
242 }
243
244 if (c == ';') { /* GIF terminator */
245 return;
246 }
247

Callers 1

fopen_gif_fileFunction · 0.70

Calls 2

ReadColorMapFunction · 0.85
DoExtensionFunction · 0.85

Tested by

no test coverage detected