Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfif_decoder.h
More file actions
Latest commit
201 lines (183 loc) · 5.98 KB
/
Copy pathfif_decoder.h
File metadata and controls
201 lines (183 loc) · 5.98 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
//C++ FastIF decoder
//Copyright (C) 2020 I.C.
#pragma once
#include<iostream>
#include<cstring>
structFIFrgb {
unsignedchar r = 0;
unsignedchar g = 0;
unsignedchar b = 0;
};
structFIF {
constunsignedchar* data = nullptr;
unsignedint width = 0;
unsignedint height = 0;
unsignedlong datapos = 0;
bool eof = 0;
FIFrgb currentBG;
FIFrgb currentFG;
FIFrgb* decoded_data = nullptr;
};
//Utils
unsignedchar* fif_slice;
char* fif_slicestring;
//Read bytes of data
unsignedchar* fif_readbytes(FIF* fiffile,unsignedlong length) {
delete[] fif_slice;
fif_slice = newunsignedchar[length];
for(unsignedint i=0;i<length;i++) {
fif_slice[i]=fiffile->data[fiffile->datapos++];
}
return fif_slice;
}
//Get slice as a null terminated string
char* fif_readbytesAsString(FIF* fiffile,unsignedlong length) {
unsignedchar slicestr[length+1];
unsignedchar* bytes=fif_readbytes(fiffile,length);
for(unsignedint i=0;i<length;i++) {
slicestr[i]=bytes[i];
if(slicestr[i]=='\0') {slicestr[i]='';}
}
slicestr[length]='\0';
delete[] fif_slice;
fif_slicestring = newchar[length+1];
for(unsignedint i=0;i<length+1;i++) {
fif_slicestring[i]=slicestr[i];
}
return fif_slicestring;
}
//Get slice and convert it to a number
unsignedcharfif_readbyte(FIF* fiffile) {
return fiffile->data[fiffile->datapos++];
}
//Spliy byte into bits
bool fif_bits[8] = {0};
constbool* fif_getbits(unsignedchar b) {
unsignedchar mask=1;
for(unsignedchar i=0;i<8;i++) {
fif_bits[i]=(b & (mask << i)) != 0;
}
return fif_bits;
}
//-----
constunsignedchar fif_bitindex[8] = {0,2,4,1,3,5,6,7};
voidFIF_renderchar(FIF* fiffile, unsignedint x, unsignedint y, unsignedchar c) {
constbool* bits = fif_getbits(c);
unsignedint pos = (x*2)+(y*4)*fiffile->width;
for(unsignedchar i=0;i<8;i++) {
unsignedint wpos = pos + ((fif_bitindex[i] / 2) * fiffile->width) + (fif_bitindex[i] % 2);
if(wpos >= fiffile->width*fiffile->height) {std::cout << "Writer went out of bounds (X: " << x << " Y: " << y << ").\n"; return;};
fiffile->decoded_data[wpos] = bits[i] ? fiffile->currentFG : fiffile->currentBG;
}
}
/*
* Main read function
*
* Returns:
* 0 - success, done decoding file
* >0 - sleep operation, ms to sleepyes
* <0 - error
*/
signedintFIF_read(FIF* fiffile) {
if(fiffile->eof) return0;
if(fiffile->datapos == 0) {
//Magic
char* str = fif_readbytesAsString(fiffile,6);
if(strcmp(str,"FastIF") != 0) {
std::cout << "Invalid FIF file.\n";
return -1;
}
//Width and height
fiffile->width = fif_readbyte(fiffile) * 2;
fiffile->height = fif_readbyte(fiffile) * 4;
//Initialize memory
fiffile->decoded_data = new FIFrgb[fiffile->width*fiffile->height];
fiffile->currentBG.r = 0;
fiffile->currentBG.g = 0;
fiffile->currentBG.b = 0;
fiffile->currentFG.r = 0;
fiffile->currentFG.g = 0;
fiffile->currentFG.b = 0;
return1;
}
//Main reading loop
while(!fiffile->eof) {
unsignedchar opbyte = 0;
switch(opbyte = fif_readbyte(fiffile)) {
//BG color
case0x01: {
fiffile->currentBG.r = fif_readbyte(fiffile);
fiffile->currentBG.g = fif_readbyte(fiffile);
fiffile->currentBG.b = fif_readbyte(fiffile);
break;
}
//FG color
case0x02: {
fiffile->currentFG.r = fif_readbyte(fiffile);
fiffile->currentFG.g = fif_readbyte(fiffile);
fiffile->currentFG.b = fif_readbyte(fiffile);
break;
}
//Horizontal line
case0x10: {
unsignedint x = fif_readbyte(fiffile);
unsignedint y = fif_readbyte(fiffile);
unsignedint s = fif_readbyte(fiffile);
for(unsignedint i=0;i<s;i++) {
FIF_renderchar(fiffile,x+i,y,fif_readbyte(fiffile));
}
break;
}
//Fill
case0x11: {
unsignedint x = fif_readbyte(fiffile);
unsignedint y = fif_readbyte(fiffile);
unsignedint fw = fif_readbyte(fiffile);
unsignedint fh = fif_readbyte(fiffile);
unsignedchar c = fif_readbyte(fiffile);
for(unsignedint ix=0;ix<fw;ix++) {
for(unsignedint iy=0;iy<fh;iy++) {
FIF_renderchar(fiffile,x+ix,y+iy,c);
}
}
break;
}
//Sleep
case0x12: {
returnfif_readbyte(fiffile) * 10;
break;
}
//Vertical line
case0x13: {
unsignedint x = fif_readbyte(fiffile);
unsignedint y = fif_readbyte(fiffile);
unsignedint s = fif_readbyte(fiffile);
for(unsignedint i=0;i<s;i++) {
FIF_renderchar(fiffile,x,y+i,fif_readbyte(fiffile));
}
break;
}
//EOF
case0x20: {
fiffile->eof = 1;
return0;
}
//Unused
case0x40: {
for(unsignedint i=0;i<8;i++) fif_readbyte(fiffile);
break;
}
//Unsupported
default: {
std::cout << "Unsupported opcode " << std::hex << (unsignedint)opbyte << " at offset " << fiffile->datapos-1 << ".\n";
return -1;
}
}
}
return0;
}
//Free FIF struct memory
voidFIF_free(FIF* fiffile) {
delete[] fiffile->data;
delete[] fiffile->decoded_data;
}