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
|
#include "../include/Gif.hpp"
#include "../include/FileUtil.hpp"
using namespace std;
namespace dchat
{
void* bitmapCreate(int width, int height)
{
return calloc(width * height, 4);
}
void bitmapDestroy(void *bitmap)
{
free(bitmap);
}
unsigned char* bitmapGetBuffer(void *bitmap)
{
return (unsigned char*)bitmap;
}
void bitmapSetOpaque(void *bitmap, bool opaque)
{
}
bool bitmapTestOpaque(void *bitmap)
{
return false;
}
void bitmapModified(void *bitmap)
{
}
const char* gifResultToString(gif_result code)
{
switch(code)
{
case GIF_INSUFFICIENT_FRAME_DATA:
return "GIF_INSUFFICIENT_FRAME_DATA";
case GIF_FRAME_DATA_ERROR:
return "GIF_FRAME_DATA_ERROR";
case GIF_INSUFFICIENT_DATA:
return "GIF_INSUFFICIENT_DATA";
case GIF_DATA_ERROR:
return "GIF_DATA_ERROR";
case GIF_INSUFFICIENT_MEMORY:
return "GIF_INSUFFICIENT_MEMORY";
default:
return "Unknown gif result code";
}
}
Gif::Gif(const boost::filesystem::path &filepath) :
currentFrame(0),
timeElapsedCs(0.0)
{
try
{
fileContent = getFileContent(filepath);
}
catch(FileException &e)
{
throw GifLoadException(e.what());
}
init();
}
Gif::Gif(StringView &&_fileContent) :
fileContent(move(_fileContent)),
currentFrame(0),
timeElapsedCs(0.0)
{
init();
}
void Gif::init()
{
gif_bitmap_callback_vt bitmapCallbacks =
{
bitmapCreate,
bitmapDestroy,
bitmapGetBuffer,
bitmapSetOpaque,
bitmapTestOpaque,
bitmapModified
};
gif_create(&gif, &bitmapCallbacks);
gif_result code;
do
{
code = gif_initialise(&gif, fileContent.size, (unsigned char*)fileContent.data);
if(code != GIF_OK && code != GIF_WORKING)
{
string errMsg = "Failed to initialize gif, reason: ";
errMsg += gifResultToString(code);
throw GifLoadException(errMsg);
}
}
while(code != GIF_OK);
if(!texture.create(gif.width, gif.height))
throw GifLoadException("Failed to create texture for gif");
texture.setSmooth(true);
sprite.setTexture(texture, true);
}
Gif::~Gif()
{
gif_finalise(&gif);
delete fileContent.data;
}
void Gif::setPosition(const sf::Vector2f &position)
{
sprite.setPosition(position);
}
void Gif::setSize(const sf::Vector2f &size)
{
sf::Vector2u textureSize = sprite.getTexture()->getSize();
sprite.setScale(size.x / (float)textureSize.x, size.y / (float)textureSize.y);
}
void Gif::draw(sf::RenderTarget &target)
{
double frameDeltaCs = (double)frameTimer.getElapsedTime().asMilliseconds() * 0.1; // Centisecond
frameTimer.restart();
timeElapsedCs += frameDeltaCs;
unsigned char *image = nullptr;
u32 startFrame = currentFrame;
while(true)
{
int i = currentFrame % gif.frame_count;
gif_result code = gif_decode_frame(&gif, i);
if(code != GIF_OK)
printf("Warning: gif_decode_frame: %s\n", gifResultToString(code));
gif_frame &frame = gif.frames[i];
// frame_delay is in centiseconds
double frameDelay = (double)frame.frame_delay;
if(timeElapsedCs >= frameDelay)
timeElapsedCs -= frameDelay;
else
break;
image = (unsigned char*)gif.frame_image;
++currentFrame;
}
if(currentFrame != startFrame)
{
texture.update(image);
// TODO: Check if this is too heavy
texture.generateMipmap();
sprite.setTexture(texture, true);
}
target.draw(sprite);
}
bool Gif::isDataGif(const StringView &data)
{
return data.size >= 6 && (memcmp(data.data, "GIF87a", 6) == 0 || memcmp(data.data, "GIF89a", 6) == 0);
}
}
|