-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSDL_gifwrap.c
783 lines (613 loc) · 24 KB
/
SDL_gifwrap.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
#include "SDL_gifwrap.h"
#include "gif_lib.h"
#include <string.h>
#include <stdlib.h>
#define MIN(x,y) ((x) < (y)? (x) : (y))
// Create and initialize a blank GIF image.
DECLSPEC GIF_Image* SDLCALL GIF_CreateImage(void)
{
GIF_Image* result = (GIF_Image*)SDL_malloc(sizeof(GIF_Image));
if(result == NULL)
return NULL;
memset(result, 0, sizeof(GIF_Image));
result->background_color.r = result->background_color.g = result->background_color.b = 0;
result->background_color.a = 255;
return result;
}
static SDL_Surface* gif_create_surface32(Uint32 width, Uint32 height)
{
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
return SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 32, 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF);
#else
return SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 32, 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000);
#endif
}
static Uint32 get_pixel(SDL_Surface *Surface, int x, int y)
{
Uint8* bits;
Uint32 bpp;
if(x < 0 || x >= Surface->w)
return 0; // Best I could do for errors
bpp = Surface->format->BytesPerPixel;
bits = ((Uint8*)Surface->pixels) + y*Surface->pitch + x*bpp;
switch (bpp)
{
case 1:
return *((Uint8*)Surface->pixels + y * Surface->pitch + x);
break;
case 2:
return *((Uint16*)Surface->pixels + y * Surface->pitch/2 + x);
break;
case 3:
{
// Endian-correct, but slower
Uint8 r, g, b;
r = *((bits)+Surface->format->Rshift/8);
g = *((bits)+Surface->format->Gshift/8);
b = *((bits)+Surface->format->Bshift/8);
return SDL_MapRGB(Surface->format, r, g, b);
}
break;
case 4:
return *((Uint32*)Surface->pixels + y * Surface->pitch/4 + x);
break;
}
return 0; // FIXME: Handle errors better
}
static void set_pixel(SDL_Surface* surface, int x, int y, Uint32 color)
{
int bpp = surface->format->BytesPerPixel;
Uint8* bits = ((Uint8 *)surface->pixels) + y*surface->pitch + x*bpp;
/* Set the pixel */
switch(bpp)
{
case 1:
*((Uint8 *)(bits)) = (Uint8)color;
break;
case 2:
*((Uint16 *)(bits)) = (Uint16)color;
break;
case 3: { /* Format/endian independent */
Uint8 r,g,b;
r = (color >> surface->format->Rshift) & 0xFF;
g = (color >> surface->format->Gshift) & 0xFF;
b = (color >> surface->format->Bshift) & 0xFF;
*((bits)+surface->format->Rshift/8) = r;
*((bits)+surface->format->Gshift/8) = g;
*((bits)+surface->format->Bshift/8) = b;
}
break;
case 4:
*((Uint32 *)(bits)) = (Uint32)color;
break;
}
}
// Load a GIF image from a file.
DECLSPEC GIF_Image* SDLCALL GIF_LoadImage(const char* filename)
{
int error;
GifFileType* gif = DGifOpenFileName(filename, &error);
if(gif == NULL)
return NULL;
if(DGifSlurp(gif) == GIF_ERROR)
{
DGifCloseFile(gif, &error);
return NULL;
}
GIF_Image* result = (GIF_Image*)SDL_malloc(sizeof(GIF_Image));
memset(result, 0, sizeof(GIF_Image));
result->width = gif->SWidth;
result->height = gif->SHeight;
// Unused?
//gif->SColorResolution // Number of bits per color channel, roughly.
// Binary 111 (7) -> 8 bits per channel
// Binary 001 (1) -> 2 bits per channel
//gif->AspectByte
//gif->SBackGroundColor
SDL_Palette* global_palette = NULL;
int i;
int j;
if(gif->SColorMap != NULL)
{
SDL_Color* global_colors = (SDL_Color*)SDL_malloc(sizeof(SDL_Color)*gif->SColorMap->ColorCount);
for(i = 0; i < gif->SColorMap->ColorCount; ++i)
{
global_colors[i].r = gif->SColorMap->Colors[i].Red;
global_colors[i].g = gif->SColorMap->Colors[i].Green;
global_colors[i].b = gif->SColorMap->Colors[i].Blue;
global_colors[i].a = 255;
}
global_palette = GIF_CreatePalette(gif->SColorMap->ColorCount, global_colors);
}
for(j = 0; j < gif->ExtensionBlockCount; ++j)
{
if(gif->ExtensionBlocks[j].Function == APPLICATION_EXT_FUNC_CODE)
{
if(gif->ExtensionBlocks[j].ByteCount >= 14)
{
char name[12];
memcpy(name, gif->ExtensionBlocks[j].Bytes, 11);
if(strcmp(name, "NETSCAPE2.0") == 0)
{
result->num_loops = gif->ExtensionBlocks[j].Bytes[12] + gif->ExtensionBlocks[j].Bytes[13]*256;
}
}
}
}
result->num_frames = gif->ImageCount;
result->frames = (GIF_Frame**)SDL_malloc(sizeof(GIF_Frame*)*result->num_frames);
memset(result->frames, 0, sizeof(GIF_Frame*)*result->num_frames);
for(i = 0; i < gif->ImageCount; ++i)
{
SavedImage* img = &gif->SavedImages[i];
GIF_Frame* frame = (GIF_Frame*)SDL_malloc(sizeof(GIF_Frame));
memset(frame, 0, sizeof(GIF_Frame));
result->frames[i] = frame;
// Load basic attributes
frame->width = img->ImageDesc.Width;
frame->height = img->ImageDesc.Height;
frame->left_offset = img->ImageDesc.Left;
frame->top_offset = img->ImageDesc.Top;
// Load palette
if(img->ImageDesc.ColorMap != NULL)
{
SDL_Color* local_colors = (SDL_Color*)SDL_malloc(sizeof(SDL_Color)*img->ImageDesc.ColorMap->ColorCount);
for(j = 0; j < img->ImageDesc.ColorMap->ColorCount; ++j)
{
local_colors[j].r = img->ImageDesc.ColorMap->Colors[j].Red;
local_colors[j].g = img->ImageDesc.ColorMap->Colors[j].Green;
local_colors[j].b = img->ImageDesc.ColorMap->Colors[j].Blue;
local_colors[j].a = 255;
}
frame->local_palette = GIF_CreatePalette(img->ImageDesc.ColorMap->ColorCount, local_colors);
}
SDL_Palette* pal = global_palette;
if(frame->local_palette != NULL)
pal = frame->local_palette;
// Look for graphics extension to get delay and transparency
for(j = 0; j < img->ExtensionBlockCount; ++j)
{
if(img->ExtensionBlocks[j].Function == GRAPHICS_EXT_FUNC_CODE)
{
Uint8 block[4];
memcpy(block, img->ExtensionBlocks[j].Bytes, 4);
// Check for transparency
if(block[0] & 0x01)
frame->use_transparent_index = SDL_TRUE;
frame->transparent_index = block[3];
// Disposal mode
frame->overlay_previous = ((block[0] & 0x08) != 0x08);
// Reconstruct delay
frame->delay = 10*(block[1] + block[2]*256);
// Get transparent color
if(pal != NULL && frame->transparent_index < pal->ncolors)
frame->transparent_color = pal->colors[frame->transparent_index];
}
}
int count = img->ImageDesc.Width * img->ImageDesc.Height;
frame->surface = gif_create_surface32(frame->width, frame->height);
frame->owns_surface = SDL_TRUE;
if(pal != NULL)
{
for(j = 0; j < count; ++j)
{
SDL_Color c = pal->colors[img->RasterBits[j]];
if(frame->transparent_index == img->RasterBits[j])
c.a = 0;
set_pixel(frame->surface, j%frame->width, j/frame->width, SDL_MapRGBA(frame->surface->format, c.r, c.g, c.b, c.a));
}
}
}
DGifCloseFile(gif, &error);
return result;
}
// Load a GIF image from an arbitrary data source.
/*DECLSPEC GIF_Image* SDLCALL GIF_LoadImage_RW(SDL_RWops* rwops, int freerwops)
{
// TODO: Implement this.
return NULL;
}*/
static ColorMapObject* gif_copy_palette(GifFileType* gif, SDL_Palette* palette)
{
if(palette == NULL || palette->ncolors <= 0)
return NULL;
int num_colors = MIN(palette->ncolors, 256);
GifColorType* colors = (GifColorType*)SDL_malloc(sizeof(GifColorType)*num_colors);
SDL_Color* source_colors = palette->colors;
int i;
for(i = 0; i < num_colors; ++i)
{
colors[i].Red = source_colors[i].r;
colors[i].Green = source_colors[i].g;
colors[i].Blue = source_colors[i].b;
}
ColorMapObject* result = GifMakeMapObject(num_colors, colors);
SDL_free(colors);
return result;
}
static Uint8 gif_find_matching_index(SDL_Palette* palette, SDL_Color background_color)
{
if(palette == NULL)
return 0;
int num_colors = MIN(palette->ncolors, 256);
int i;
for(i = 0; i < num_colors; ++i)
{
if(background_color.r == palette->colors[i].r
&& background_color.g == palette->colors[i].g
&& background_color.b == palette->colors[i].b)
return i;
}
return 0;
}
static SDL_bool gif_add_loop_extension(GifFileType* gif, int num_loops)
{
if(EGifPutExtensionLeader(gif, APPLICATION_EXT_FUNC_CODE) == GIF_ERROR)
return SDL_FALSE;
if(EGifPutExtensionBlock(gif, 11, "NETSCAPE2.0") == GIF_ERROR)
return SDL_FALSE;
Uint8 block[3] = {1, num_loops % 256, num_loops / 256}; // Loops bytes are low, high right?
if(EGifPutExtensionBlock(gif, 3, block) == GIF_ERROR)
return SDL_FALSE;
return (EGifPutExtensionTrailer(gif) != GIF_ERROR);
}
static int gif_get_closest_palette_match(Uint8 r, Uint8 g, Uint8 b, SDL_Color* palette, int num_colors)
{
int distance = 1000;
int result = 0;
int index = 0;
int i;
for(i = 0; i < num_colors; ++i)
{
int dist = abs((int)(palette[i].r) - r) + abs((int)(palette[i].g) - g) + abs((int)(palette[i].b) - b);
if(dist < distance)
{
result = index;
distance = dist;
}
++index;
}
return result;
}
static void gif_copy_surface_to_indices(GIF_Frame* frame, SDL_Palette* global_palette)
{
SDL_Surface* surface = frame->surface;
if(surface == NULL)
return;
SDL_Palette* pal = (global_palette != NULL? global_palette : frame->local_palette);
if(pal == NULL)
return;
int n = 0;
Uint8 r,g,b;
int i,j;
for(i = 0; i < surface->w; ++i)
{
for(j = 0; j < surface->h; ++j)
{
Uint32 pixel = get_pixel(surface, i, j);
SDL_GetRGB(pixel, surface->format, &r, &g, &b);
frame->indices[n] = gif_get_closest_palette_match(r, g, b, pal->colors, MIN(pal->ncolors, 256));
++n;
}
}
}
// Save the given image to a file. Returns SDL_FALSE on failure.
DECLSPEC SDL_bool SDLCALL GIF_SaveImage(GIF_Image* image, const char* filename)
{
if(image == NULL || image->num_frames == 0)
return SDL_FALSE;
int error;
// Create the output object
GifFileType* gif = EGifOpenFileName(filename, SDL_FALSE, &error);
if(!gif)
return SDL_FALSE;
// Set up screen description
ColorMapObject* palette = gif_copy_palette(gif, image->global_palette);
if(image->global_palette != NULL && image->use_background_color)
image->background_index = gif_find_matching_index(image->global_palette, image->background_color);
if(EGifPutScreenDesc(gif, image->width, image->height, 8, image->background_index, palette) == GIF_ERROR)
return SDL_FALSE;
if(!gif_add_loop_extension(gif, image->num_loops))
return SDL_FALSE;
int i;
for(i = 0; i < image->num_frames; ++i)
{
GIF_Frame* frame = image->frames[i];
if(image->num_frames > 1 || frame->use_transparent_color || frame->use_transparent_index)
{
if(frame->use_transparent_color)
frame->transparent_index = gif_find_matching_index((frame->local_palette == NULL? image->global_palette : frame->local_palette), frame->transparent_color);
Uint8 block[4] = {0, (frame->delay/10)%256, (frame->delay/10)/256, frame->transparent_index};
if(frame->use_transparent_color || frame->use_transparent_index)
block[0] |= 0x01;
if(!frame->overlay_previous)
block[0] |= 0x08;
if(EGifPutExtension(gif, GRAPHICS_EXT_FUNC_CODE, 4, block) == GIF_ERROR)
return SDL_FALSE;
}
if(frame->surface != NULL)
{
frame->width = frame->surface->w;
frame->height = frame->surface->h;
}
ColorMapObject* palette = gif_copy_palette(gif, frame->local_palette);
if(EGifPutImageDesc(gif, frame->left_offset, frame->top_offset, frame->width, frame->height, SDL_FALSE, palette) == GIF_ERROR)
{
//printf("EGifPutImageDesc error: %d\n", gif->Error);
return SDL_FALSE;
}
if(frame->indices == NULL || frame->surface != NULL)
{
SDL_free(frame->indices);
frame->indices = (Uint8*)SDL_malloc(sizeof(Uint8)*(frame->width*frame->height));
// Copy surface data if it's there
gif_copy_surface_to_indices(frame, image->global_palette);
}
// Write lines of pixel indices
int y;
int j = 0;
for(y = 0; y < frame->height; ++y)
{
if(EGifPutLine(gif, &(frame->indices[j]), frame->width) == GIF_ERROR)
return SDL_FALSE;
j += frame->width;
}
}
if(EGifCloseFile(gif, &error) == GIF_ERROR)
return SDL_FALSE;
return SDL_TRUE;
}
// Save the given image to an arbitrary destination. Returns SDL_FALSE on failure.
/*DECLSPEC SDL_bool SDLCALL GIF_SaveImage_RW(GIF_Image* image, SDL_RWops* rwops, int freerwops)
{
return SDL_FALSE;
}*/
// Free the memory allocated for the given image.
DECLSPEC void SDLCALL GIF_FreeImage(GIF_Image* image)
{
if(image == NULL)
return;
SDL_FreePalette(image->global_palette);
image->global_palette = NULL;
Uint16 i;
for(i = 0; i < image->num_frames; ++i)
GIF_FreeFrame(image->frames[i]);
SDL_free(image->frames);
image->frames = NULL;
for(i = 0; i < image->num_comments; ++i)
SDL_free(image->comments[i]);
SDL_free(image->comments);
image->comments = NULL;
SDL_free(image);
}
// Sets the canvas size.
DECLSPEC void SDLCALL GIF_SetCanvasSize(GIF_Image* image, Uint16 width, Uint16 height)
{
if(image == NULL)
return;
image->width = width;
image->height = height;
}
// Sets the background color.
DECLSPEC void SDLCALL GIF_SetBackgroundColor(GIF_Image* image, SDL_Color background_color)
{
if(image == NULL)
return;
image->use_background_color = SDL_TRUE;
image->background_color = background_color;
}
// Sets the background color index.
DECLSPEC void SDLCALL GIF_SetBackgroundIndex(GIF_Image* image, Uint8 background_color_index)
{
if(image == NULL)
return;
image->use_background_color = SDL_FALSE;
image->background_index = background_color_index;
}
// Sets the number of animation loops. 0 loops means to loop forever.
DECLSPEC void SDLCALL GIF_SetLooping(GIF_Image* image, Uint16 num_loops)
{
if(image == NULL)
return;
image->num_loops = num_loops;
}
// Sets the global palette of the given image. The given palette's ownership is transferred to the GIF image, so it will be freed by GIF_Free().
DECLSPEC void SDLCALL GIF_SetGlobalPalette(GIF_Image* image, SDL_Palette* palette)
{
if(image == NULL)
return;
SDL_FreePalette(image->global_palette);
image->global_palette = palette;
}
// Returns a new palette with a range of colors.
DECLSPEC SDL_Palette* SDLCALL GIF_CreateBasicPalette(void)
{
SDL_Palette* result = SDL_AllocPalette(256);
int i = 0;
for(i = 0; i < 256; ++i)
{
SDL_Color c = {i, i, i, 255};
result->colors[i] = c;
}
return result;
}
// Creates a new palette from the given colors. GIF can only use the first 256 colors. The colors are copied into a new palette object.
DECLSPEC SDL_Palette* SDLCALL GIF_CreatePalette(int ncolors, SDL_Color* colors)
{
SDL_Palette* result = SDL_AllocPalette(ncolors);
int i = 0;
for(i = 0; i < ncolors; ++i)
{
result->colors[i] = colors[i];
}
return result;
}
// Copies a palette.
DECLSPEC SDL_Palette* SDLCALL GIF_CopyPalette(SDL_Palette* palette)
{
SDL_Palette* result;
if(palette == NULL)
return NULL;
result = SDL_AllocPalette(palette->ncolors);
SDL_SetPaletteColors(result, palette->colors, 0, palette->ncolors);
return result;
}
static void gif_grow_image_storage(GIF_Image* image)
{
GIF_Frame** old_frames;
if(image == NULL)
return;
old_frames = image->frames;
image->frame_storage_size += 10;
image->frames = (GIF_Frame**)SDL_malloc(sizeof(GIF_Frame*)*image->frame_storage_size);
if(old_frames == NULL)
memset(image->frames, 0, sizeof(GIF_Frame*)*image->frame_storage_size);
else
{
memcpy(image->frames, old_frames, sizeof(GIF_Frame*)*image->num_frames);
SDL_free(old_frames);
}
}
// Adds the given frame to the end of the given image. The given frame's ownership is transferred to the image, so it will be freed by GIF_FreeImage().
DECLSPEC void SDLCALL GIF_AddFrame(GIF_Image* image, GIF_Frame* frame)
{
if(image == NULL || frame == NULL)
return;
// Do we need to grow the storage?
if(image->frame_storage_size < image->num_frames + 1)
gif_grow_image_storage(image);
image->frames[image->num_frames] = frame;
image->num_frames += 1;
}
// Inserts the given frame before the frame at a specified index of the given image. The given frame's ownership is transferred to the image, so it will be freed by GIF_FreeImage().
DECLSPEC void SDLCALL GIF_InsertFrame(GIF_Image* image, GIF_Frame* frame, Uint16 index)
{
if(image == NULL || frame == NULL || index >= image->num_frames)
return;
// Do we need to grow the storage?
if(image->frame_storage_size < image->num_frames + 1)
gif_grow_image_storage(image);
// Move the rest of the frames up
memmove(&(image->frames[index+1]), &(image->frames[index]), (image->num_frames - index)*sizeof(GIF_Frame*));
image->frames[image->num_frames] = frame;
image->num_frames += 1;
}
// Removes a frame from the given image and returns the frame.
DECLSPEC GIF_Frame* SDLCALL GIF_RemoveFrame(GIF_Image* image, Uint16 index)
{
GIF_Frame* result = NULL;
if(image == NULL || image->num_frames <= index)
return result;
result = image->frames[index];
// Shift down the array
image->num_frames--;
memmove(&(image->frames[index]), &(image->frames[index+1]), (image->num_frames - index)*sizeof(GIF_Frame*));
return result;
}
// Removes a frame from the given image and frees the frame.
DECLSPEC void SDLCALL GIF_DeleteFrame(GIF_Image* image, Uint16 index)
{
if(image == NULL || image->num_frames <= index)
return;
GIF_FreeFrame(image->frames[index]);
// Shift down the array
image->num_frames--;
memmove(&(image->frames[index]), &(image->frames[index+1]), (image->num_frames - index)*sizeof(GIF_Frame*));
}
// Creates a new frame and returns it for further modification. If give_ownership is true, the given surface's ownership is transferred to the GIF frame, so it will be freed by GIF_FreeFrame() or GIF_FreeImage().
DECLSPEC GIF_Frame* SDLCALL GIF_CreateFrame(SDL_Surface* surface, SDL_bool give_ownership)
{
GIF_Frame* result = (GIF_Frame*)SDL_malloc(sizeof(GIF_Frame));
if(result == NULL)
return NULL;
memset(result, 0, sizeof(GIF_Frame));
result->delay = 100;
result->surface = surface;
result->owns_surface = give_ownership;
if(surface != NULL)
{
result->width = surface->w;
result->height = surface->h;
}
return result;
}
// Creates a new frame and returns it for further modification. The given indices are copied.
DECLSPEC GIF_Frame* SDLCALL GIF_CreateFrameIndexed(Uint16 width, Uint16 height, Uint8* indices)
{
GIF_Frame* result = (GIF_Frame*)SDL_malloc(sizeof(GIF_Frame));
if(result == NULL)
return NULL;
memset(result, 0, sizeof(GIF_Frame));
result->delay = 100;
int num_indices = width*height;
if(num_indices > 0 && indices != NULL)
{
result->indices = (Uint8*)SDL_malloc(sizeof(Uint8)*num_indices);
memcpy(result->indices, indices, num_indices);
}
result->width = width;
result->height = height;
return result;
}
// Free the memory allocated for the given frame.
DECLSPEC void SDLCALL GIF_FreeFrame(GIF_Frame* frame)
{
if(frame == NULL)
return;
SDL_FreePalette(frame->local_palette);
frame->local_palette = NULL;
if(frame->owns_surface)
SDL_FreeSurface(frame->surface);
frame->surface = NULL;
SDL_free(frame->indices);
frame->indices = NULL;
SDL_free(frame);
}
// Sets the global palette of the given frame. This will override the global palette for this frame. The given palette's ownership is transferred to the GIF image, so it will be freed by GIF_Free().
DECLSPEC void SDLCALL GIF_SetLocalPalette(GIF_Frame* frame, SDL_Palette* palette)
{
if(frame == NULL)
return;
SDL_FreePalette(frame->local_palette);
frame->local_palette = palette;
}
// Sets the given frame's delay time to the given delay, in milliseconds. GIF only stores delays in hundredths of a second, so the saved image will truncate the given delay.
DECLSPEC void SDLCALL GIF_SetDelay(GIF_Frame* frame, Uint32 delay)
{
if(frame == NULL)
return;
frame->delay = delay;
}
// Enables transparency for the given frame and sets the transparent color.
DECLSPEC void SDLCALL GIF_SetTransparentColor(GIF_Frame* frame, SDL_Color transparent_color)
{
if(frame == NULL)
return;
frame->use_transparent_color = SDL_TRUE;
frame->use_transparent_index = SDL_FALSE;
frame->transparent_color = transparent_color;
}
// Enables transparency for the given frame and sets the transparent color index.
DECLSPEC void SDLCALL GIF_SetTransparentIndex(GIF_Frame* frame, Uint8 color_index)
{
if(frame == NULL)
return;
frame->use_transparent_color = SDL_FALSE;
frame->use_transparent_index = SDL_TRUE;
frame->transparent_index = color_index;
}
// Disables transparency for the given frame.
DECLSPEC void SDLCALL GIF_UnsetTransparency(GIF_Frame* frame)
{
if(frame == NULL)
return;
frame->use_transparent_color = SDL_FALSE;
frame->use_transparent_index = SDL_FALSE;
}
// Sets the disposal method for the colors of the previous frame. SDL_FALSE discards the previous frame colors.
DECLSPEC void SDLCALL GIF_SetDisposal(GIF_Frame* frame, SDL_bool overlay_previous)
{
if(frame == NULL)
return;
frame->overlay_previous = overlay_previous;
}