-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathngiflibSDL.c
239 lines (233 loc) · 6.94 KB
/
ngiflibSDL.c
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#include <stdio.h>
#include "ngiflib.h"
#include "ngiflibSDL.h"
/* define NGIFLIBSDL_LOG to log to stdout */
/* SDL_LoadGIF a le meme comportement que SDL_LoadBMP */
SDL_Surface * SDL_LoadGIF(const char * file)
{
/* MODE WRAPPER A DEUX BALLES */
SDL_Surface * surface;
struct ngiflib_gif * gif;
FILE *fgif;
int err,i;
u8 * pdst, * psrc;
u8 * p = NULL;
#ifdef NGIFLIB_NO_FILE
u8 * buffer;
long filesize;
#endif /* NGIFLIB_NO_FILE */
fgif = fopen(file, "rb");
if(fgif==NULL)
return NULL;
gif = (struct ngiflib_gif *)ngiflib_malloc(sizeof(struct ngiflib_gif));
#ifdef EXTRA_MALLOC_CHECK
if(gif == NULL) {
return NULL;
}
#endif /* EXTRA_MALLOC_CHECK */
ngiflib_memset(gif, 0, sizeof(struct ngiflib_gif));
#ifdef NGIFLIB_NO_FILE
fseek(fgif, 0, SEEK_END);
filesize = ftell(fgif);
if (filesize < 0) {
GifDestroy(gif);
return NULL;
}
fseek(fgif, 0, SEEK_SET);
buffer = malloc(filesize);
if(buffer == NULL) {
GifDestroy(gif);
return NULL;
}
fread(buffer, 1, filesize, fgif);
gif->input.buffer.bytes = buffer;
gif->input.buffer.count = (unsigned long)filesize;
gif->mode = NGIFLIB_MODE_FROM_MEM | NGIFLIB_MODE_INDEXED;
#else /* NGIFLIB_NO_FILE */
gif->input.file = fgif;
/*gif->mode = NGIFLIB_MODE_FROM_FILE | NGIFLIB_MODE_TRUE_COLOR; */
gif->mode = NGIFLIB_MODE_FROM_FILE | NGIFLIB_MODE_INDEXED;
#ifdef NGIFLIBSDL_LOG
gif->log = stdout;
#endif /* NGIFLIBSDL_LOG */
#endif /* NGIFLIB_NO_FILE */
err = LoadGif(gif);
fclose(fgif);
#ifdef NGIFLIB_NO_FILE
free(buffer);
#endif /* NGIFLIB_NO_FILE */
if(err!=1)
{
GifDestroy(gif);
return NULL;
}
p = gif->frbuff.p8;
/*
surface = SDL_CreateRGBSurfaceFrom(p, gif->width, gif->height,
32, gif->width << 2,
0x00ff0000,
0x0000ff00,
0x000000ff,
0xff000000);
*/
surface = SDL_CreateRGBSurface(SDL_SWSURFACE | SDL_SRCCOLORKEY,
gif->width, gif->height, 8,
0,0,0,0);
if (surface == NULL) {
fprintf(stderr, "SDL_CreateRGBSurface(..., %d, %d, 8, ...) failed : %s\n", gif->width, gif->height, SDL_GetError());
GifDestroy(gif);
return NULL;
}
SDL_LockSurface(surface);
if(gif->cur_img->gce.transparent_flag)
{
SDL_SetColorKey(surface, SDL_SRCCOLORKEY, gif->cur_img->gce.transparent_color);
}
for(i=0; i<gif->ncolors; i++)
{
surface->format->palette->colors[i].r = gif->palette[i].r;
surface->format->palette->colors[i].g = gif->palette[i].g;
surface->format->palette->colors[i].b = gif->palette[i].b;
}
psrc = p; pdst = surface->pixels;
for(i=0; i<gif->height; i++)
{
ngiflib_memcpy(pdst, psrc, gif->width);
pdst += surface->pitch;
psrc += gif->width;
}
SDL_UnlockSurface(surface);
GifDestroy(gif);
return surface;
}
struct ngiflibSDL_animation * SDL_LoadAnimatedGif(const char * file)
{
SDL_Surface * surface;
struct ngiflib_gif * gif;
FILE *fgif;
int err,i;
u8 * pdst, * psrc;
u8 * p = NULL;
#ifdef NGIFLIB_NO_FILE
u8 * buffer;
long filesize;
#endif /* NGIFLIB_NO_FILE */
int image_count = 0;
int image_count_max = 50;
struct ngiflibSDL_animation * animation = NULL;
struct ngiflib_rgb * current_palette = NULL;
int current_palette_size = 0;
fgif = fopen(file, "rb");
if(fgif==NULL)
return NULL;
gif = (struct ngiflib_gif *)ngiflib_malloc(sizeof(struct ngiflib_gif));
#ifdef EXTRA_MALLOC_CHECK
if(gif == NULL) {
return NULL;
}
#endif /* EXTRA_MALLOC_CHECK */
ngiflib_memset(gif, 0, sizeof(struct ngiflib_gif));
#ifdef NGIFLIB_NO_FILE
fseek(fgif, 0, SEEK_END);
filesize = ftell(fgif);
fseek(fgif, 0, SEEK_SET);
buffer = malloc(filesize);
if(buffer == NULL) {
GifDestroy(gif);
return NULL;
}
fread(buffer, 1, filesize, fgif);
gif->input.buffer.bytes = buffer;
gif->input.buffer.count = (unsigned long)filesize;
gif->mode = NGIFLIB_MODE_FROM_MEM | NGIFLIB_MODE_INDEXED;
#else /* NGIFLIB_NO_FILE */
gif->input.file = fgif;
/*gif->mode = NGIFLIB_MODE_FROM_FILE | NGIFLIB_MODE_TRUE_COLOR; */
gif->mode = NGIFLIB_MODE_FROM_FILE | NGIFLIB_MODE_INDEXED;
#ifdef NGIFLIBSDL_LOG
gif->log = stdout;
#endif /* NGIFLIBSDL_LOG */
#endif /* NGIFLIB_NO_FILE */
while((err = LoadGif(gif)) == 1) {
if(animation == NULL) {
animation = ngiflib_malloc(sizeof(struct ngiflibSDL_animation) + image_count_max*sizeof(struct ngiflibSDL_image));
if(animation == NULL)
return NULL;
} else if(image_count >= image_count_max) {
image_count_max += 50;
struct ngiflibSDL_animation * tmp;
tmp = realloc(animation, sizeof(struct ngiflibSDL_animation) + image_count_max*sizeof(struct ngiflibSDL_image));
if(tmp == NULL) {
fprintf(stderr, "realloc() failed, cannot decode more images\n");
break;
}
animation = tmp;
}
p = gif->frbuff.p8;
/*
surface = SDL_CreateRGBSurfaceFrom(p, gif->width, gif->height,
32, gif->width << 2,
0x00ff0000,
0x0000ff00,
0x000000ff,
0xff000000);
*/
surface = SDL_CreateRGBSurface(SDL_SWSURFACE | SDL_SRCCOLORKEY,
gif->width, gif->height, 8,
0,0,0,0);
if (surface == NULL) {
fprintf(stderr, "SDL_CreateRGBSurface(..., %d, %d, 8, ...) failed : %s\n", gif->width, gif->height, SDL_GetError());
continue;
}
SDL_LockSurface(surface);
/*
if(gif->transparent_flag) {
SDL_SetColorKey(surface, SDL_SRCCOLORKEY, gif->transparent_color);
}
*/
if(gif->palette != gif->cur_img->palette) {
current_palette = gif->cur_img->palette;
current_palette_size = (1 << gif->cur_img->localpalbits);
} else if(current_palette == NULL) {
current_palette = gif->palette;
current_palette_size = gif->ncolors;
}
if (current_palette != NULL && gif->palette != NULL) {
for(i = 0; i < current_palette_size; i++) {
surface->format->palette->colors[i].r = current_palette[i].r;
surface->format->palette->colors[i].g = current_palette[i].g;
surface->format->palette->colors[i].b = current_palette[i].b;
//printf("#%02x%02x%02x ", current_palette[i].r, current_palette[i].g, current_palette[i].b);
}
for(; i < gif->ncolors; i++) {
surface->format->palette->colors[i].r = gif->palette[i].r;
surface->format->palette->colors[i].g = gif->palette[i].g;
surface->format->palette->colors[i].b = gif->palette[i].b;
//printf("#%02x%02x%02x ", gif->palette[i].r, gif->palette[i].g, gif->palette[i].b);
}
//printf("\n");
} else {
fprintf(stderr, "no palette in GIF\n");
}
psrc = p; pdst = surface->pixels;
for(i=0; i<gif->height; i++) {
ngiflib_memcpy(pdst, psrc, gif->width);
pdst += surface->pitch;
psrc += gif->width;
}
SDL_UnlockSurface(surface);
animation->images[image_count].delay_time = -1;
if(gif->cur_img->gce.gce_present) {
animation->images[image_count].delay_time = gif->cur_img->gce.delay_time;
}
animation->images[image_count].surface = surface;
image_count++;
}
fclose(fgif);
#ifdef NGIFLIB_NO_FILE
free(buffer);
#endif /* NGIFLIB_NO_FILE */
GifDestroy(gif);
if(animation) animation->image_count = image_count;
return animation;
}