-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgif.cpp
More file actions
421 lines (378 loc) · 15.9 KB
/
gif.cpp
File metadata and controls
421 lines (378 loc) · 15.9 KB
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
/* Copyright (c) David Cunningham and the Grit Game Engine project 2015
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <cstdlib>
#include <cstdint>
#include <memory>
extern "C" {
#include <gif_lib.h>
}
#include <io_util.h>
#include <console.h>
#include <exception.h>
#include "gif.h"
#include "image.h"
float clamp (float x)
{
if (x < 0) return 0;
if (x > 1) return 1;
return x;
}
struct ColorMapPtr {
ColorMapObject * const palette;
ColorMapPtr (unsigned sz)
: palette(GifMakeMapObject(sz, nullptr))
{
if (palette == nullptr)
EXCEPT << "Libgif unable to make palette" << ENDL;
}
~ColorMapPtr (void)
{
GifFreeMapObject(palette);
}
};
struct ScopedFile {
GifFileType * const file;
const std::string filename;
int openErr;
ScopedFile (GifFileType *file, const std::string filename)
: file(file), filename(filename)
{ }
NORETURN1 std::string throwErr (const std::string &msg) NORETURN2
{
EXCEPT << filename << ": " << GifErrorString(file->Error) << " while " << msg << ENDL;
}
};
struct ScopedLoadFile : public ScopedFile {
ScopedLoadFile (const std::string &filename)
: ScopedFile(DGifOpenFileName(filename.c_str(), &openErr), filename)
{
if (file == nullptr) {
EXCEPT << "Libgif error while opening file: "
<< filename << " (" << GifErrorString(openErr) << ")" << ENDL;
}
}
~ScopedLoadFile (void)
{
if (file != nullptr) {
if (DGifCloseFile(file, &openErr) == GIF_ERROR) {
CERR << "Libgif error while closing file: "
<< filename << " (" << GifErrorString(openErr) << ")" << std::endl;
}
}
}
};
struct ScopedSaveFile : public ScopedFile {
ScopedSaveFile (const std::string &filename)
: ScopedFile(EGifOpenFileName(filename.c_str(), false, &openErr), filename)
{
if (file == nullptr) {
EXCEPT << "Libgif error while opening file: "
<< filename << " (" << GifErrorString(openErr) << ")" << ENDL;
}
}
~ScopedSaveFile (void)
{
if (file != nullptr) {
if (EGifCloseFile(file, &openErr) == GIF_ERROR) {
CERR << "Libgif error while closing file: "
<< filename << " (" << GifErrorString(openErr) << ")" << std::endl;
}
}
}
};
void gif_save (const std::string &filename, const GifFile &content)
{
const std::string msg = "While writing \"" + filename + "\": ";
const auto &frames = content.frames;
// Sanity checks
if (content.loops > 65536)
EXCEPT << msg << "Too many loops: " << content.loops << ENDL;
if (frames.size() < 1)
EXCEPT << msg << "Cannot write zero frames" << ENDL;
uimglen_t w = frames[0].image->width;
uimglen_t h = frames[0].image->height;
for (unsigned i=0 ; i<frames.size() ; ++i) {
if (frames[i].image->width != w || frames[i].image->height != h)
EXCEPT << msg << "frame " << i << " has wrong dimensions" << ENDL;
if (frames[i].image->colourChannels() != 3)
EXCEPT << msg << "Frame " << i << " does not have 3 channels" << ENDL;
}
ScopedSaveFile f(filename);
EGifSetGifVersion(f.file, true);
if (EGifPutScreenDesc(f.file, w, h, 8, 0, nullptr) == GIF_ERROR)
f.throwErr("writing screen desc");
if (content.loops != 1) {
unsigned short reps = content.loops;
if (reps > 0) reps--;
GifByteType nab[] = { 'N', 'E', 'T', 'S', 'C', 'A', 'P', 'E', '2', '.', '0' };
if (EGifPutExtensionLeader(f.file, 255) == GIF_ERROR)
f.throwErr("writing NAB leader");
if (EGifPutExtensionBlock(f.file, sizeof nab, nab) == GIF_ERROR)
f.throwErr("writing NAB block1");
GifByteType loops[] = {
1,
GifByteType(reps & 0xff),
GifByteType(reps >> 8)
};
if (EGifPutExtensionBlock(f.file, sizeof loops, loops) == GIF_ERROR)
f.throwErr("writing NAB block2");
if (EGifPutExtensionTrailer(f.file) == GIF_ERROR)
f.throwErr("writing NAB trailer");
}
std::vector<GifByteType> rbuf(w * h);
std::vector<GifByteType> gbuf(w * h);
std::vector<GifByteType> bbuf(w * h);
std::vector<bool> abuf(w * h);
std::vector<GifByteType> obuf(w * h);
for (const auto &frame : frames) {
ColorMapPtr palette(256);
GifColorType *pcols = palette.palette->Colors;
int psize;
if (frame.image->hasAlpha()) {
const auto &img = *static_cast<const Image<3,1>*>(frame.image);
psize = 255;
for (uimglen_t y=0 ; y<h ; ++y) {
for (uimglen_t x=0 ; x<w ; ++x) {
const auto &pixel = img.pixel(x, h-y-1);
if (pixel[3] < 0.5) {
rbuf[y * w + x] = 0;
gbuf[y * w + x] = 0;
bbuf[y * w + x] = 0;
abuf[y * w + x] = false;
} else {
rbuf[y * w + x] = clamp(pixel[0]) * 255 + 0.5;
gbuf[y * w + x] = clamp(pixel[1]) * 255 + 0.5;
bbuf[y * w + x] = clamp(pixel[2]) * 255 + 0.5;
abuf[y * w + x] = true;
}
}
}
} else {
psize = 256;
const auto &img = *static_cast<const Image<3,0>*>(frame.image);
for (uimglen_t y=0 ; y<h ; ++y) {
for (uimglen_t x=0 ; x<w ; ++x) {
const auto &pixel = img.pixel(x, h-y-1);
rbuf[y * w + x] = clamp(pixel[0]) * 255 + 0.5;
gbuf[y * w + x] = clamp(pixel[1]) * 255 + 0.5;
bbuf[y * w + x] = clamp(pixel[2]) * 255 + 0.5;
}
}
}
if (GifQuantizeBuffer(w, h, &psize, &rbuf[0], &gbuf[0], &bbuf[0],
&obuf[0], pcols) == GIF_ERROR) {
f.throwErr("quantising image");
}
// Use 255 for transparent colour.
if (frame.image->hasAlpha()) {
const auto &img = *static_cast<const Image<3,1>*>(frame.image);
for (uimglen_t y=0 ; y<h ; ++y) {
for (uimglen_t x=0 ; x<w ; ++x) {
const auto &pixel = img.pixel(x, h-y-1);
if (pixel[3] < 0.5)
obuf[y * w + x] = 255;
}
}
}
GraphicsControlBlock gdb {
DISPOSAL_UNSPECIFIED,
false,
int(frame.delay / 0.01 + 0.5), // Round to nearest multiple of 0.01
frame.image->hasAlpha() ? 255 : NO_TRANSPARENT_COLOR
};
GifByteType ext[4];
EGifGCBToExtension(&gdb, ext);
if (EGifPutExtension(f.file, 0xf9, sizeof ext, ext) == GIF_ERROR)
f.throwErr("writing GCE");
if (EGifPutImageDesc(f.file, 0, 0, w, h, false, palette.palette) == GIF_ERROR)
f.throwErr("writing image desc");
for (uimglen_t y=0 ; y<h ; ++y) {
if (EGifPutLine(f.file, &obuf[y * w], w) == GIF_ERROR)
f.throwErr("writing pixel line");
}
}
}
GifFile gif_open (const std::string &filename)
{
const std::string msg = "while reading \"" + filename + "\": ";
ScopedLoadFile f(filename);
uimglen_t sw = f.file->SWidth;
uimglen_t sh = f.file->SHeight;
GifRecordType record_type;
std::vector<std::unique_ptr<Image<3, 1>>> imgs;
std::vector<double> delays;
int trans_colour = -1;
int bg_colour = f.file->SBackGroundColor;
unsigned loops = 1;
double delay = 1;
do {
if (DGifGetRecordType(f.file, &record_type) == GIF_ERROR)
f.throwErr("getting record type");
switch (record_type) {
case IMAGE_DESC_RECORD_TYPE: {
if (DGifGetImageDesc(f.file) == GIF_ERROR)
f.throwErr("getting image desc");
ColorMapObject *palette =
f.file->Image.ColorMap ? f.file->Image.ColorMap : f.file->SColorMap;
Colour<3, true> bg(0);
if (bg_colour == trans_colour) {
bg[0] = 0;
bg[1] = 0;
bg[2] = 0;
bg[3] = 0;
} else {
bg[0] = palette->Colors[bg_colour].Red / 255.0;
bg[1] = palette->Colors[bg_colour].Green / 255.0;
bg[2] = palette->Colors[bg_colour].Blue / 255.0;
bg[3] = 1;
}
imgs.emplace_back(new Image<3, 1>(sw, sh));
delays.emplace_back(delay);
auto &img = *imgs[imgs.size() - 1];
for (uimglen_t y=0 ; y<sh ; ++y) {
for (uimglen_t x=0 ; x<sw ; ++x) {
img.pixel(x, sh-y-1) = bg;
}
}
// Dimensions of frame
uimglen_t top = f.file->Image.Top;
uimglen_t fh = f.file->Image.Height;
uimglen_t bottom = top + fh;
uimglen_t left = f.file->Image.Left;
uimglen_t fw = f.file->Image.Width;
uimglen_t right = left + fw;
std::vector<GifByteType> line(fw);
if (f.file->Image.Interlace) {
// This is a mechanism for progressive rendering.
const uimglen_t offsets[] = { 0, 4, 2, 1 };
const uimglen_t jumps[] = { 8, 8, 4, 2 };
// Need to perform 4 passes on the images:
for (uimglen_t i = 0; i < 4; i++) {
for (uimglen_t y = top + offsets[i] ; y < top+fh ; y += jumps[i]) {
if (DGifGetLine(f.file, &line[0], fw) == GIF_ERROR)
f.throwErr("getting a line of pixels");
for (uimglen_t x=left ; x<right && x<sw ; ++x) {
auto &pixel = img.pixel(x, sh-y-1);
int colour = line[x - left];
if (colour == trans_colour) {
pixel[0] = 0;
pixel[1] = 0;
pixel[2] = 0;
pixel[3] = 0;
} else {
pixel[0] = palette->Colors[colour].Red / 255.0;
pixel[1] = palette->Colors[colour].Green / 255.0;
pixel[2] = palette->Colors[colour].Blue / 255.0;
pixel[3] = 1;
}
}
}
}
} else {
for (uimglen_t y=top ; y<bottom && y<sh ; ++y) {
if (DGifGetLine(f.file, &line[0], fw) == GIF_ERROR)
f.throwErr("getting a line of pixels");
for (uimglen_t x=left ; x<right && x<sw ; ++x) {
auto &pixel = img.pixel(x, sh-y-1);
int colour = line[x - left];
if (colour == trans_colour) {
pixel[0] = 0;
pixel[1] = 0;
pixel[2] = 0;
pixel[3] = 0;
} else {
pixel[0] = palette->Colors[colour].Red / 255.0;
pixel[1] = palette->Colors[colour].Green / 255.0;
pixel[2] = palette->Colors[colour].Blue / 255.0;
pixel[3] = 1;
}
}
}
}
}
break;
case EXTENSION_RECORD_TYPE: {
int ext_code;
GifByteType *ext;
std::vector<std::vector<GifByteType>> blocks;
if (DGifGetExtension(f.file, &ext_code, &ext) == GIF_ERROR)
f.throwErr("getting extension");
while (ext != NULL) {
unsigned block_sz = ext[0];
blocks.emplace_back(block_sz);
for (unsigned i=0 ; i<block_sz ; ++i)
blocks[blocks.size() - 1][i] = ext[1 + i];
if (DGifGetExtensionNext(f.file, &ext) == GIF_ERROR)
f.throwErr("getting next extension");
}
switch (ext_code) {
case 249: { // Graphics control extension
if (blocks.size() < 1) goto ignore;
if (blocks[0].size() < 4) goto ignore;
int flags = GifByteType(blocks[0][0]);
bool trans = (flags >> 0) & 0x1;
bool ui = (flags >> 1) & 0x2;
unsigned disposal = (flags >> 2) & 0x7;
int block_delay = GifByteType(blocks[0][1]) | GifByteType(blocks[0][2] << 8);
int block_trans_colour = GifByteType(blocks[0][3]);
trans_colour = trans ? block_trans_colour : -1;
delay = block_delay * 0.01;
(void) ui;
(void) disposal;
} break;
case 254: { // Comment extension
for (unsigned i=0 ; i<blocks.size() ; ++i) {
// std::cout << blocks[i] << std::endl;
}
} break;
case 255: { // Application extension
std::vector<GifByteType> netscape2_0 =
{ 'N', 'E', 'T', 'S', 'C', 'A', 'P', 'E', '2', '.', '0' };
if (netscape2_0 == blocks[0]) {
if (blocks.size() < 2) goto ignore;
if (blocks[1].size() < 3) goto ignore;
unsigned repeats = blocks[1][1] | (blocks[1][2] << 8);
loops = repeats == 0 ? 0 : repeats + 1ul;
}
} break;
default: {
std::cout << "Unrecognised extension code: " << ext_code << std::endl;
}
}
ignore:;
}
break;
case TERMINATE_RECORD_TYPE:
goto end;
default:
EXCEPTEX << "Internal error." << ENDL;
}
} while (true);
end:;
GifFile r;
r.loops = loops;
r.frames.resize(imgs.size());
for (unsigned i=0 ; i<imgs.size() ; ++i) {
r.frames[i].image = imgs[i].release();
r.frames[i].delay = delays[i];
}
return r;
}