/
rwgif.c
172 lines (143 loc) · 4.7 KB
/
rwgif.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
/* -*- c -*- */
/*
* rwgif.c
*
* rwimg
*
* Copyright (C) 2006 Xavier Martin
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <gif_lib.h>
#include <string.h>
#if GIFLIB_MAJOR >= 5
#define GIF_OPEN_FILE_NAME(filename) DGifOpenFileName(filename, NULL)
#else
#define GIF_OPEN_FILE_NAME(filename) DGifOpenFileName(filename)
#endif
// From https://github.com/Automattic/node-canvas/issues/440 (2017-05-15)
#if GIFLIB_MAJOR > 5 || GIFLIB_MAJOR == 5 && GIFLIB_MINOR >= 1
#define GIF_CLOSE_FILE(gif) DGifCloseFile(gif, NULL)
#else
#define GIF_CLOSE_FILE(gif) DGifCloseFile(gif)
#endif
typedef struct
{
GifFileType *file;
int width;
int height;
unsigned char *rgb;
} gif_data_t;
void*
open_gif_file (const char *filename, int *width, int *height)
{
int interlace_offset[] = { 0, 4, 2, 1 };
int interlace_jump[] = { 8, 8, 4, 2 };
GifColorType *colormap;
GifRecordType record_type;
GifRowType *buffer = NULL;
int i, j;
int color_index;
unsigned char *ptr = NULL;
gif_data_t *data = (gif_data_t*)malloc(sizeof(gif_data_t));
assert(data != 0);
data->file = GIF_OPEN_FILE_NAME(filename);
assert(data->file !=0);
do
{
assert(DGifGetRecordType(data->file, &record_type) != GIF_ERROR) ;
switch (record_type)
{
case IMAGE_DESC_RECORD_TYPE:
assert(DGifGetImageDesc(data->file) != GIF_ERROR);
*width = data->file->Image.Width;
*height = data->file->Image.Height;
data->width = *width;
data->height = *height;
buffer = malloc(*height * sizeof(GifRowType *));
assert(buffer != NULL);
for (i = 0; i < *height; i++)
{
buffer[i] = malloc(*width * sizeof(GifPixelType));
assert(buffer[i] != NULL);
}
if (data->file->Image.Interlace)
{
for (i = 0; i < 4; i++)
for (j = interlace_offset[i]; j < *height;
j += interlace_jump[i])
DGifGetLine(data->file, buffer[j], *width);
}
else
{
for (i = 0; i < *height; i++)
DGifGetLine(data->file, buffer[i], *width);
}
break;
case EXTENSION_RECORD_TYPE:
{
/* Skip extension blocks */
int ext_code;
GifByteType *ext;
assert(DGifGetExtension(data->file, &ext_code, &ext) != GIF_ERROR);
while (ext != NULL)
{
assert(DGifGetExtensionNext(data->file, &ext) != GIF_ERROR);
}
}
break;
case TERMINATE_RECORD_TYPE:
break;
default:
fprintf(stderr, "unknown record type in GIF file\n");
break;
}
} while (record_type != TERMINATE_RECORD_TYPE);
colormap = (data->file->Image.ColorMap ? data->file->Image.ColorMap->Colors
: data->file->SColorMap->Colors);
data->rgb = (unsigned char*)malloc( (data->width* data->height * 3) );
assert(data->rgb != NULL);
ptr = data->rgb;
for (j = 0; j < *height; j++)
{
for (i = 0; i < *width; i++)
{
color_index = (int) buffer[j][i];
*ptr++ = (unsigned char) colormap[color_index].Red;
*ptr++ = (unsigned char) colormap[color_index].Green;
*ptr++ = (unsigned char) colormap[color_index].Blue;
}
free(buffer[j]);
}
free(buffer);
assert(GIF_CLOSE_FILE(data->file) == GIF_OK);
return data;
}
void
gif_read_lines (void *_data, unsigned char *lines, int num_lines)
{
gif_data_t *data = (gif_data_t*)_data;
memcpy(lines, data->rgb, data->width * num_lines * 3);
}
void
gif_free_data (void *_data)
{
gif_data_t *data = (gif_data_t*)_data;
free(data->rgb);
free(data);
}