aboutsummaryrefslogtreecommitdiffstats
path: root/src/gif.c
blob: 93a4517a66c4ab1e7529025d7a690ca436519723 (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
#include <stdio.h>
#include <string.h>

#include <assert.h>

#include <gif_lib.h>

#include "meh.h"


unsigned char *loadgif(FILE *f, int *bufwidth, int *bufheight){
	GifFileType *gif;
	SavedImage *img;
	GifColorType *colormap;

	unsigned char *buf;
	if(!(gif = DGifOpenFileHandle(fileno(f)))){
		return NULL;
	}
	if(DGifSlurp(gif) == GIF_ERROR){
		return NULL;
	}
	img = &gif->SavedImages[0];
	*bufwidth = img->ImageDesc.Width;
	*bufheight = img->ImageDesc.Height;
	buf = malloc(img->ImageDesc.Width * img->ImageDesc.Height * 3);

	if(img->ImageDesc.ColorMap)
		colormap = img->ImageDesc.ColorMap->Colors;
	else if(gif->SColorMap)
		colormap = gif->SColorMap->Colors;
	else
		return NULL;

	int i, j = 0;
	for(i = 0; i < img->ImageDesc.Width * img->ImageDesc.Height; i++){
		unsigned char idx = img->RasterBits[i];
		buf[j++] = colormap[idx].Red;
		buf[j++] = colormap[idx].Green;
		buf[j++] = colormap[idx].Blue;
	}
	return buf;
}