-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfli.cpp
More file actions
250 lines (213 loc) · 4.09 KB
/
fli.cpp
File metadata and controls
250 lines (213 loc) · 4.09 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
#include "fli.h"
FLI_Player::FLI_Player()
{
}
FLI_Player::FLI_Player(char *filename)
{
Open(filename);
}
FLI_Player::~FLI_Player()
{
if(IsOpen) Close();
}
bool FLI_Player::Open(char *filename)
{
if(IsOpen) Close();
FLIFile = fopen(filename,"rb");
if(FLIFile == NULL) return false;
IsOpen = true;
// Initialize Palette
for(int i = 0; i < 256; i++)
{
Pal.Colors[i].R = i;
Pal.Colors[i].G = i;
Pal.Colors[i].B = i;
}
// Read Header
fread(&Header,128,1,FLIFile);
if(Header.magic != 0xAF11)
{
Close();
return false;
}
CurrentFrame = new unsigned char[Header.width * Header.height];
FLI_Chunk dummy;
Process_FLI_BLACK(dummy);
AnimFrame = 0;
}
void FLI_Player::Process_FLI_COLOR(FLI_Chunk chunk)
{
uint16_t cnt;
uint8_t skip;
uint8_t chg;
uint16_t change;
FLI_Color col;
int palpos = 0;
if(fread(&cnt,2,1,FLIFile))
{
for(int packet = 0; packet < cnt; packet++)
{
fread(&skip,1,1,FLIFile);
fread(&chg,1,1,FLIFile);
palpos += skip;
change = chg;
if(!change) change = 256;
for(int j = 0; j < change; j++)
{
fread(&col,3,1,FLIFile);
col.R *= 4;
col.G *= 4;
col.B *= 4;
Pal.Colors[palpos] = col;
palpos++;
}
}
}
}
void FLI_Player::Process_FLI_LC(FLI_Chunk chunk)
{
uint16_t y;
uint16_t lineoffset;
uint16_t offset;
uint16_t change;
uint8_t packets;
uint8_t skip;
int8_t size;
uint8_t pixel;
uint8_t buf[256];
fread(&y,2,1,FLIFile);
lineoffset = y * Header.width;
fread(&change,2,1,FLIFile);
for(int line = 0; line < change; line++)
{
offset = lineoffset;
fread(&packets,1,1,FLIFile);
for(int p = 0; p < packets; p++)
{
fread(&skip,1,1,FLIFile);
fread(&size,1,1,FLIFile);
offset += skip;
if(size != 0)
{
if(size > 0)
{
fread(buf,size,1,FLIFile);
for(int n = 0; n < size; n++)
CurrentFrame[offset++] = buf[n];
}
else
{
fread(&pixel,1,1,FLIFile);
for(int n = 0; n < -size; n++)
CurrentFrame[offset++] = pixel;
}
}
}
lineoffset += Header.width;
}
}
void FLI_Player::Process_FLI_BLACK(FLI_Chunk chunk)
{
int size = Header.width * Header.height;
for(int i = 0; i < size; i++)
CurrentFrame[i] = 0;
}
void FLI_Player::Process_FLI_BRUN(FLI_Chunk chunk)
{
uint16_t lineoffset = 0;
uint16_t offset;
uint16_t change;
uint8_t packets;
uint8_t skip;
int8_t size;
uint8_t pixel;
uint8_t buf[256];
change = Header.height;
for(int line = 0; line < change; line++)
{
offset = lineoffset;
fread(&packets,1,1,FLIFile);
for(int p = 0; p < packets; p++)
{
fread(&size,1,1,FLIFile);
if(size != 0)
{
if(size > 0)
{
fread(&pixel,1,1,FLIFile);
for(int n = 0; n < size; n++)
CurrentFrame[offset++] = pixel;
}
else
{
fread(buf,-size,1,FLIFile);
for(int n = 0; n < -size; n++)
CurrentFrame[offset++] = buf[n];
}
}
}
lineoffset += Header.width;
}
}
void FLI_Player::Process_FLI_COPY(FLI_Chunk chunk)
{
int size = Header.width * Header.height;
fread(CurrentFrame,size,1,FLIFile);
}
bool FLI_Player::NextFrame()
{
if(AnimFrame == Header.frames) return false;
char dummy;
int i;
FLI_Frame frame;
FLI_Chunk chunk;
uint16_t cursize;
if(!fread(&frame,16,1,FLIFile)) return false;
if(frame.size == 0) return true;
for(int n = 0; n < frame.chunks; n++)
{
if(!fread(&chunk,6,1,FLIFile)) return false;
switch(chunk.type)
{
case FLI_COLOR:
Process_FLI_COLOR(chunk);
break;
case FLI_LC:
Process_FLI_LC(chunk);
break;
case FLI_BLACK:
Process_FLI_BLACK(chunk);
break;
case FLI_BRUN:
Process_FLI_BRUN(chunk);
break;
case FLI_COPY:
Process_FLI_COPY(chunk);
break;
}
i = ftell(FLIFile);
if(i%2) fread(&dummy,1,1,FLIFile);
}
AnimFrame++;
return true;
}
void FLI_Player::RenderFrame(uint8_t *buffer) {
int idx;
for(int n = 0; n < 64000; n++)
{
idx = CurrentFrame[n];
buffer[n * 3] = Pal.Colors[idx].R;
buffer[n * 3 + 1] = Pal.Colors[idx].G;
buffer[n * 3 + 2] = Pal.Colors[idx].B;
}
}
int FLI_Player::GetPos()
{
return AnimFrame;
}
void FLI_Player::Close()
{
fclose(FLIFile);
delete CurrentFrame;
IsOpen = false;
}