-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathdecoder.cpp
More file actions
350 lines (302 loc) · 8.08 KB
/
decoder.cpp
File metadata and controls
350 lines (302 loc) · 8.08 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
// Aseprite FLIC Library
// Copyright (c) 2019-2025 Igara Studio S.A.
// Copyright (c) 2015 David Capello
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
#include "flic.h"
#include "flic_details.h"
#include <limits>
#undef assert
#define assert(...)
namespace flic {
Decoder::Decoder(FileInterface* file)
: m_file(file)
, m_frameCount(0)
, m_offsetFrame1(0)
, m_offsetFrame2(0)
{
}
bool Decoder::readHeader(Header& header)
{
read32(); // file size
uint16_t magic = read16();
assert(magic == FLI_MAGIC_NUMBER || magic == FLC_MAGIC_NUMBER);
if (magic != FLI_MAGIC_NUMBER &&
magic != FLC_MAGIC_NUMBER)
return false;
header.frames = read16();
header.width = read16();
header.height = read16();
read16(); // Color depth (it is interpreted as 8bpp anyway)
read16(); // Skip flags
header.speed = read32();
if (magic == FLI_MAGIC_NUMBER) {
if (header.speed == 0)
header.speed = 70;
else
header.speed = 1000 * header.speed / 70;
}
if (magic == FLC_MAGIC_NUMBER) {
// Offset to the first and second frame
m_file->seek(80);
m_offsetFrame1 = read32();
m_offsetFrame2 = read32();
}
if (header.width == 0) header.width = 320;
if (header.height == 0) header.height = 200;
m_width = header.width;
m_height = header.height;
// Skip padding
m_file->seek(128);
return true;
}
bool Decoder::readFrame(Frame& frame)
{
switch (m_frameCount) {
case 0:
if (m_offsetFrame1)
m_file->seek(m_offsetFrame1);
break;
case 1:
if (m_offsetFrame2)
m_file->seek(m_offsetFrame2);
break;
}
uint32_t frameStartPos = m_file->tell();
uint32_t frameSize = read32();
uint16_t magic = read16();
assert(magic == FLI_FRAME_MAGIC_NUMBER);
(void)magic;
uint16_t chunks = read16();
for (int i=0; i<8; ++i) // Padding
m_file->read8();
for (uint16_t i=0; i!=chunks; ++i)
readChunk(frame);
m_file->seek(frameStartPos+frameSize);
++m_frameCount;
return true;
}
void Decoder::readChunk(Frame& frame)
{
uint32_t chunkStartPos = m_file->tell();
uint32_t chunkSize = read32();
uint16_t type = read16();
switch (type) {
case FLI_COLOR_256_CHUNK: readColorChunk(frame, false); break;
case FLI_DELTA_CHUNK: readDeltaChunk(frame); break;
case FLI_COLOR_64_CHUNK: readColorChunk(frame, true); break;
case FLI_LC_CHUNK: readLcChunk(frame); break;
case FLI_BLACK_CHUNK: readBlackChunk(frame); break;
case FLI_BRUN_CHUNK: readBrunChunk(frame); break;
case FLI_COPY_CHUNK: readCopyChunk(frame); break;
default:
// Ignore all other kind of chunks
break;
}
m_file->seek(chunkStartPos+chunkSize);
}
void Decoder::readBlackChunk(Frame& frame)
{
std::fill(frame.pixels,
frame.pixels+frame.rowstride*m_height, 0);
}
void Decoder::readCopyChunk(Frame& frame)
{
assert(m_width == 320 && m_height == 200);
if (m_width == 320 && m_height == 200) {
for (int y=0; y<200; ++y) {
uint8_t* it = frame.pixels + y*frame.rowstride;
for (int x=0; x<320; ++x, ++it)
*it = m_file->read8();
}
}
}
void Decoder::readColorChunk(Frame& frame, bool oldColorChunk)
{
int npackets = read16();
// For each packet
int i = 0;
while (npackets--) {
i += m_file->read8(); // Colors to skip
int colors = m_file->read8();
if (colors == 0)
colors = 256;
for (int j=0; j<colors
// If i+j >= 256 it means that the color chunk is invalid,
// we check this to avoid an buffer overflow of frame.colormap[]
&& i+j < 256;
++j) {
Color& color = frame.colormap[i+j];
color.r = m_file->read8();
color.g = m_file->read8();
color.b = m_file->read8();
if (oldColorChunk) {
color.r = 255 * int(color.r) / 63;
color.g = 255 * int(color.g) / 63;
color.b = 255 * int(color.b) / 63;
}
}
}
}
void Decoder::readBrunChunk(Frame& frame)
{
for (int y=0; y<m_height; ++y) {
uint8_t* it = frame.pixels+frame.rowstride*y;
int x = 0;
int npackets = m_file->read8(); // Use the number of packet to check integrity
if (npackets == 0) {
// If npackets is 0, we are in a FLC file (not FLI) and there
// can be more than 255 packets.
npackets = std::numeric_limits<int>::max();
}
while (m_file->ok() && npackets-- != 0 && x < m_width) {
int count = int(int8_t(m_file->read8()));
if (count >= 0) {
uint8_t color = m_file->read8();
while (count-- != 0 && x < m_width) {
*it = color;
++it;
++x;
}
}
else {
while (count++ != 0 && x < m_width) {
*it = m_file->read8();
++it;
++x;
}
}
}
}
}
void Decoder::readLcChunk(Frame& frame)
{
int skipLines = read16();
int nlines = read16();
for (int y=skipLines; y<skipLines+nlines; ++y) {
// Break in case of invalid data
if (y < 0 || y >= m_height)
break;
uint8_t* it = frame.pixels+frame.rowstride*y;
int x = 0;
int npackets = m_file->read8();
while (npackets-- && x < m_width) {
int skip = m_file->read8();
x += skip;
it += skip;
int count = int(int8_t(m_file->read8()));
if (count >= 0) {
uint8_t* end = frame.pixels+frame.rowstride*m_height;
while (count-- != 0 && it < end) {
*it = m_file->read8();
++it;
++x;
}
// Broken file? More bytes than available buffer
if (it == end)
return;
}
else {
uint8_t color = m_file->read8();
while (count++ != 0 && x < m_width) {
*it = color;
++it;
++x;
}
}
}
}
}
void Decoder::readDeltaChunk(Frame& frame)
{
int nlines = read16();
int y = 0;
while (nlines-- != 0) {
int npackets = 0;
while (m_file->ok()) {
int16_t word = read16();
if (word < 0) { // Has bit 15 (0x8000)
if (word & 0x4000) { // Has bit 14 (0x4000)
y += -word; // Skip lines
}
// The last pixel of the current line has changed
// This code exists for animations with an odd column count. The changes at the other positions follow.
else {
assert(y >= 0 && y < m_height);
if (y >= 0 && y < m_height) {
uint8_t* it = frame.pixels + y*frame.rowstride + m_width - 1;
*it = (word & 0xff);
}
}
}
else {
npackets = word;
break;
}
}
// Avoid invalid data to skip more lines than the availables.
if (y >= m_height)
break;
int x = 0;
while (npackets-- != 0) {
x += m_file->read8(); // Skip pixels
int8_t count = m_file->read8(); // Number of words
assert(y >= 0 && y < m_height && x >= 0 && x < m_width);
uint8_t* it = frame.pixels + y*frame.rowstride + x;
if (count >= 0) {
while (count-- != 0 && x < m_width) {
int color1 = m_file->read8();
int color2 = m_file->read8();
*it = color1;
++it;
++x;
if (x < m_width) {
*it = color2;
++it;
++x;
}
}
}
else {
int color1 = m_file->read8();
int color2 = m_file->read8();
while (count++ != 0 && x < m_width) {
*it = color1;
++it;
++x;
if (x < m_width) {
*it = color2;
++it;
++x;
}
}
}
}
++y;
}
}
uint16_t Decoder::read16()
{
int b1 = m_file->read8();
int b2 = m_file->read8();
if (m_file->ok()) {
return ((b2 << 8) | b1); // Little endian
}
else
return 0;
}
uint32_t Decoder::read32()
{
int b1 = m_file->read8();
int b2 = m_file->read8();
int b3 = m_file->read8();
int b4 = m_file->read8();
if (m_file->ok()) {
// Little endian
return ((b4 << 24) | (b3 << 16) | (b2 << 8) | b1);
}
else
return 0;
}
} // namespace flic