-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGif.cpp
263 lines (209 loc) · 7.07 KB
/
Gif.cpp
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
#include "Gif.h"
#include "GifException.h"
#include "Color.h"
#include "Image.h"
#include "Animation.h"
#include <gif_lib.h>
#include <cstddef>
#include <stdexcept>
#include <string>
#include <vector>
#include <chrono>
#include <memory>
#include <utility>
using namespace Gif2UnicornHat;
using namespace std;
using namespace std::chrono;
namespace {
int combineBytesToInt(uint8_t loByte, uint8_t hiByte)
{
return loByte + hiByte*0xFFu;
}
Color lookupColor(const ColorMapObject& colorMap, int colorIndex) noexcept
{
auto& entry = colorMap.Colors[colorIndex];
return Color(entry.Red, entry.Green, entry.Blue);
}
void drawImageRecordToCanvas(GifFileType& file, Image& canvas, int transparentIndex = -1)
{
if (DGifGetImageDesc(&file) == GIF_ERROR) {
throw GifException(file.Error);
}
auto& img = file.Image;
if (img.ColorMap == nullptr && file.SColorMap == nullptr) {
throw runtime_error("Missing color map.");
}
// Use the local colour map if present. Otherwise, use the global one.
auto& colorMap = (img.ColorMap != nullptr) ? *img.ColorMap : *file.SColorMap;
if ((img.Left + img.Width) > file.SWidth || (img.Top + img.Height) > file.SHeight) {
throw runtime_error("Image extends outside of canvas dimensions.");
}
// Place one line at a time on the canvas.
vector<GifPixelType> imgLine(img.Width);
auto copyLineToCanvas = [&] (int targetLine) {
if (DGifGetLine(&file, imgLine.data(), img.Width) == GIF_ERROR) {
throw GifException(file.Error);
}
// Copy the line into the canvas.
for (int j = 0; j < img.Width; ++j) {
if (imgLine[j] == transparentIndex)
continue; //< Don't overwrite what's currently in the canvas when transparent.
canvas[img.Left+j][img.Top+targetLine] = lookupColor(colorMap, imgLine[j]);
}
};
// Read each line into the canvas.
if (file.Image.Interlace) {
// 4-pass interlacing.
int interlaceOffset[] = { 0, 4, 2, 1 };
int interlaceStep[] = { 8, 8, 4, 2 };
for (int pass = 0; pass < 4; ++pass) {
for (int i = interlaceOffset[pass]; i < (img.Top + img.Height); i += interlaceStep[pass]) {
copyLineToCanvas(i);
}
}
} else {
for (int i = 0; i < img.Height; ++i) {
copyLineToCanvas(i);
}
}
}
}
namespace Gif2UnicornHat {
struct GraphicsControlBlock {
enum DisposalMethod {
NotSpecified = 0, DoNotDispose, RestoreToBackground, RestoreToPrevious
};
DisposalMethod disposalMethod;
bool userInputFlag;
bool transparentColorFlag;
milliseconds delayTime;
int transparentColorIndex;
static GraphicsControlBlock fromBytes(GifByteType* extData);
};
GraphicsControlBlock GraphicsControlBlock::fromBytes(GifByteType* extData)
{
GraphicsControlBlock gcb;
if (extData[0] != 4) {
throw runtime_error("Unexpected size of graphics control block.");
}
gcb.disposalMethod = static_cast<GraphicsControlBlock::DisposalMethod>((extData[1] & 0x1C) >> 2);
gcb.userInputFlag = (extData[1] & 0x2);
gcb.transparentColorFlag = (extData[1] & 0x1);
gcb.delayTime = milliseconds(combineBytesToInt(extData[2], extData[3])*10);
gcb.transparentColorIndex = extData[4];
return gcb;
}
void closeGifFile(GifFileType* f)
{
int err = 0;
if (f) DGifCloseFile(f, &err);
}
unique_ptr<GifFileType, void(*)(GifFileType*)> openGifFile(const string& path)
{
int err = 0;
auto rawGifFile = DGifOpenFileName(path.c_str(), &err);
if (err)
throw GifException(err);
return {rawGifFile, closeGifFile};
}
Gif Gif::fromFile(const std::string& path, Color defaultBackground /*= Color()*/)
{
// Open the file.
auto file = openGifFile(path);
// Figure out what background colour to use.
Color backgroundColor = defaultBackground;
if (file->SColorMap != nullptr && file->SBackGroundColor > 0) {
backgroundColor = lookupColor(*file->SColorMap, file->SBackGroundColor);
}
// A place to store the last frame and the next control block.
Image lastCanvas(file->SWidth, file->SHeight, backgroundColor);
GraphicsControlBlock gcb;
Animation animation;
while (true) {
GifRecordType recordType;
if (DGifGetRecordType(file.get(), &recordType) == GIF_ERROR) {
throw GifException(file->Error);
}
if (recordType == IMAGE_DESC_RECORD_TYPE) {
Image canvas(file->SWidth, file->SHeight, backgroundColor);
if (gcb.disposalMethod == GraphicsControlBlock::DoNotDispose ||
gcb.disposalMethod == GraphicsControlBlock::NotSpecified) { //< Most encoders use NotSpecified to mean DoNotDispose.
// Copy in the last canvas and use it as the starting point.
canvas = lastCanvas;
}
drawImageRecordToCanvas(*file, canvas, (gcb.transparentColorFlag ? gcb.transparentColorIndex : -1));
if (gcb.delayTime == milliseconds(0) && !animation.frames().empty()) {
// Replace the last frame when no delay is specified.
animation.frames().back().image = canvas;
} else {
// Append a new frame.
animation.appendFrame({canvas, gcb.delayTime});
}
if (gcb.disposalMethod != GraphicsControlBlock::RestoreToPrevious) {
// Store the canvas for the next frame, when RestoreToPrevious isn't set.
lastCanvas = move(canvas);
}
// Clear the GCB so it doesn't apply to the next frame.
gcb = GraphicsControlBlock();
} else if (recordType == EXTENSION_RECORD_TYPE) {
int extCode;
GifByteType* extData;
if (DGifGetExtension(file.get(), &extCode, &extData) == GIF_ERROR) {
throw GifException(file->Error);
}
if (extCode == APPLICATION_EXT_FUNC_CODE) //< Stores loop count animated images.
{
auto extString = string(extData+1, extData+12);
if (extString == "NETSCAPE2.0" || extString == "ANIMEXTS1.0") { //< Is this the animation extension record?
// The number of loops is in the next extension record, but don't fail if it isn't there.
if (DGifGetExtensionNext(file.get(), &extData) != GIF_ERROR) {
if (extData[0] == 3 && extData[1] == 1) {
animation.setNumLoops(combineBytesToInt(extData[2], extData[3]));
}
}
}
} else if (extCode == GRAPHICS_EXT_FUNC_CODE) { //< Stores frame delay for each image.
gcb = GraphicsControlBlock::fromBytes(extData);
}
// Skip any other extension blocks.
while (extData != nullptr) {
if (DGifGetExtensionNext(file.get(), &extData) == GIF_ERROR) {
throw GifException(file->Error);
}
}
} else if (recordType == TERMINATE_RECORD_TYPE) {
break;
}
}
return Gif(file->SWidth, file->SHeight, animation);
}
Gif::Gif(Image::Dimension width, Image::Dimension height, Animation animation)
: width_(width),
height_(height),
animation_(animation)
{
}
Image::Dimension Gif::height() const
{
return height_;
}
Image::Dimension Gif::width() const
{
return width_;
}
bool Gif::isAnimated() const
{
return animation_.numFrames() > 0;
}
const Image& Gif::getStaticImage() const
{
if (animation_.numFrames() == 0) {
throw runtime_error("Gif contains no image records.");
}
return animation_.frames().front().image;
}
const Animation& Gif::getAnimation() const
{
return animation_;
}
}