-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers_gif.c
176 lines (139 loc) · 3.54 KB
/
helpers_gif.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
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <emscripten.h>
#include <emscripten/html5.h>
#include "nsgif.h"
typedef struct
{
nsgif_t *gif;
int frame_count;
int frame_index;
nsgif_bitmap_t *frame_bitmap;
uint8_t data[];
} gif_reader_t;
#define XCALL(Reader, Fn, ...) \
({ nsgif_error err__ = nsgif_##Fn((Reader)->gif, ## __VA_ARGS__); \
NSGIF_OK == err__ ? 0 : (emscripten_throw_string(nsgif_strerror(err__)), 1); })
#define INFO(Reader, Field) nsgif_get_info((Reader)->gif)->Field
static void *_bitmap_create(int width, int height)
{
return calloc(width * height, 4);
}
static void _bitmap_destroy(void *bitmap)
{
free(bitmap);
}
static unsigned char *_bitmap_get_buffer(void *bitmap)
{
return bitmap;
}
static const nsgif_bitmap_cb_vt _bitmap_callbacks =
{
_bitmap_create,
_bitmap_destroy,
_bitmap_get_buffer,
NULL,
NULL,
NULL,
NULL,
};
EMSCRIPTEN_KEEPALIVE
gif_reader_t *gif_reader_create(uint8_t *data, size_t data_size)
{
const char *err_str;
gif_reader_t *reader;
nsgif_error err;
err_str = NULL;
reader = malloc(sizeof (gif_reader_t) + data_size);
if (NULL == reader)
{
err_str = "reader allocation failed";
goto err_1;
}
err = nsgif_create(&_bitmap_callbacks, NSGIF_BITMAP_FMT_R8G8B8A8, &reader->gif);
if (NSGIF_OK != err)
goto err_2;
memcpy(reader->data, data, data_size);
err = nsgif_data_scan(reader->gif, data_size, data);
if (NSGIF_OK != err && NSGIF_ERR_END_OF_DATA != err)
goto err_3;
reader->frame_count = (int)INFO(reader, frame_count);
assert(reader->frame_count >= 0);
if (!reader->frame_count)
{
err_str = "gif has no frame";
goto err_3;
}
reader->frame_index = -1;
reader->frame_bitmap = NULL;
return reader;
err_3:
nsgif_destroy(reader->gif);
err_2:
free(reader);
err_1:
emscripten_throw_string(err_str == NULL ? err_str : nsgif_strerror(err));
return NULL;
}
EMSCRIPTEN_KEEPALIVE
void gif_reader_destroy(gif_reader_t *reader)
{
nsgif_destroy(reader->gif);
free(reader);
}
EMSCRIPTEN_KEEPALIVE
uint32_t gif_reader_canvas_width(const gif_reader_t *reader)
{
return INFO(reader, width);
}
EMSCRIPTEN_KEEPALIVE
uint32_t gif_reader_canvas_height(const gif_reader_t *reader)
{
return INFO(reader, height);
}
EMSCRIPTEN_KEEPALIVE
uint32_t gif_reader_frame_count(const gif_reader_t *reader)
{
return reader->frame_count;
}
EMSCRIPTEN_KEEPALIVE
uint32_t gif_reader_loop_count(const gif_reader_t *reader)
{
return INFO(reader, loop_max);
}
EMSCRIPTEN_KEEPALIVE
void gif_reader_rewind(gif_reader_t *reader)
{
if (XCALL(reader, reset))
return;
reader->frame_index = -1;
}
EMSCRIPTEN_KEEPALIVE
void gif_reader_decode_next_frame(gif_reader_t *reader)
{
if (++reader->frame_index == reader->frame_count)
{
if (XCALL(reader, reset))
return;
reader->frame_index = 0;
}
XCALL(reader, frame_decode, reader->frame_index, &reader->frame_bitmap);
}
EMSCRIPTEN_KEEPALIVE
int gif_reader_frame_index(const gif_reader_t *reader)
{
return reader->frame_index;
}
EMSCRIPTEN_KEEPALIVE
int gif_reader_frame_duration(const gif_reader_t *reader)
{
uint32_t delay;
delay = nsgif_get_frame_info(reader->gif, reader->frame_index)->delay;
return delay > INT32_MAX ? INT32_MAX : delay * 10;
}
EMSCRIPTEN_KEEPALIVE
const uint8_t *gif_reader_frame_rgba(const gif_reader_t *reader)
{
return reader->frame_bitmap;
}