blob: 8da65547f1a19f00d01f1c1395a6d4efa4cfd906 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <assert.h>
#include <gif_lib.h>
#include "meh.h"
struct gif_t{
struct image img;
FILE *f;
GifFileType *gif;
};
static int isgif(FILE *f){
return (getc(f) == 'G' && getc(f) == 'I' && getc(f) == 'F');
}
static struct image *gif_open(FILE *f){
struct gif_t *g;
GifFileType *gif;
rewind(f);
if(!isgif(f))
return NULL;
/* HACK HACK HACK */
rewind(f);
lseek(fileno(f), 0L, SEEK_SET);
if(!(gif = DGifOpenFileHandle(fileno(f)))){
/* HACK AND HOPE */
rewind(f);
lseek(fileno(f), 0L, SEEK_SET);
return NULL;
}
g = malloc(sizeof(struct gif_t));
g->f = f;
g->gif = gif;
g->img.bufwidth = gif->SWidth;
g->img.bufheight = gif->SHeight;
return (struct image *)g;
}
static int gif_read(struct image *img){
unsigned int i, j = 0;
struct gif_t *g = (struct gif_t *)img;
GifColorType *colormap;
SavedImage *s;
if(DGifSlurp(g->gif) == GIF_ERROR){
PrintGifError();
return 1;
}
s = &g->gif->SavedImages[0];
if(s->ImageDesc.ColorMap)
colormap = s->ImageDesc.ColorMap->Colors;
else if(g->gif->SColorMap)
colormap = g->gif->SColorMap->Colors;
else{
PrintGifError();
return 1;
}
for(i = 0; i < img->bufwidth * img->bufheight; i++){
unsigned char idx = s->RasterBits[i];
img->buf[j++] = colormap[idx].Red;
img->buf[j++] = colormap[idx].Green;
img->buf[j++] = colormap[idx].Blue;
}
return 0;
}
void gif_close(struct image *img){
struct gif_t *g = (struct gif_t *)img;
DGifCloseFile(g->gif);
fclose(g->f);
}
struct imageformat giflib = {
gif_open,
gif_read,
gif_close
};
|