-
Notifications
You must be signed in to change notification settings - Fork 0
/
BbcImageLoader.cpp
334 lines (277 loc) · 7.41 KB
/
BbcImageLoader.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
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
/*
* Copyright © 2010-2021 Matt Robinson
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
#include <stdexcept>
#include "BbcImageLoader.h"
const int MEMSIZE012 = 20480; // Screen memory size for modes 0, 1 & 2
const int MEMSIZE45 = 10240; // Screen memory size for modes 4 & 5
BbcImageLoader::BbcImageLoader(uint8_t *data, int size)
{
if(size <= 0 || size > BbcScreen::MAX_MEMSIZE)
{
throw std::invalid_argument("size must be greater than 0 and less than BbcScreen::MAX_MEMSIZE");
}
this->data = data;
this->size = size;
}
BbcScreen* BbcImageLoader::LoadAuto()
{
BbcScreen *screen;
// Assume the file is LdPic format, and attempt to load it like that
if((screen = LoadLdPic()) == NULL)
{
// File was not in LdPic format, load it as a memory dump
screen = LoadMemDump();
}
return screen;
}
BbcScreen* BbcImageLoader::LoadMemDump()
{
return new BbcScreen(this->size, this->data);
}
BbcScreen* BbcImageLoader::LoadLdPic()
{
uint8_t outValBitSize;
uint8_t mode;
uint8_t colMapping;
uint8_t stepSize;
uint8_t repCountBits;
uint8_t readMode;
uint8_t repeatCount;
uint8_t valToRepeat;
this->pos = 0;
// Read the number of bits to read for each image byte
if(!getBits(8, &outValBitSize))
{
return NULL;
}
if(outValBitSize == 0 || outValBitSize > 8)
{
// As this is the number of bits to read, it can't be zero, and it can't be
// larger than eight, as that would overflow the single byte storage.
return NULL;
}
// Read the mode from the file
if(!getBits(8, &mode))
{
return NULL;
}
int memSize;
switch(mode % 8)
{
case 0:
case 1:
case 2:
memSize = MEMSIZE012;
break;
case 4:
case 5:
memSize = MEMSIZE45;
break;
default:
// Modes 3, 6 & 7 are not supported
return NULL;
}
// Initialise a new BbcScreen instance and set the mode
BbcScreen *screen = new BbcScreen(memSize);
screen->setMode(mode);
// Read the colour mappings from the file
for(int8_t readPal = BbcScreen::PALETTE_SIZE - 1; readPal >= 0; readPal--)
{
if(!getBits(4, &colMapping))
{
delete screen;
return NULL;
}
screen->setColour(readPal, colMapping);
}
// Read the number of bytes to move forward by after each byte is written to memory
if(!getBits(8, &stepSize))
{
delete screen;
return NULL;
}
if(stepSize == 0)
{
// This can't be zero, as only the first pixel would get written to
delete screen;
return NULL;
}
// Fetch the number of bits to read for each repeat count
if(!getBits(8, &repCountBits))
{
delete screen;
return NULL;
}
if(repCountBits == 0 || repCountBits > 8)
{
// As this is the number of bits to read, it can't be zero, and it can't be
// larger than eight, as that would overflow the single byte storage.
delete screen;
return NULL;
}
// Start in the highest step position and work backwards
int address = stepSize - 1;
int progPos = address;
for(;;)
{
// The next bit of the file shows whether to read just a single
// value, or to read the number of repeats and a value
if(!getBit(&readMode))
{
// Unexpected end of file
delete screen;
return NULL;
}
if(readMode == 0)
{
// Single value mode - only 1 repeat
repeatCount = 1;
}
else
{
// Fetch the number of times the value should be repeated
if(!getBits(repCountBits, &repeatCount))
{
// Unexpected end of file
delete screen;
return NULL;
}
if(repeatCount == 0)
{
// The value must be repeated at least once
delete screen;
return NULL;
}
}
// Now fetch the value itself
if(!getBits(outValBitSize, &valToRepeat))
{
// Unexpected end of file
delete screen;
return NULL;
}
// Output the value(s) to the file
while(repeatCount > 0)
{
screen->setScreenByte(address, valToRepeat);
address += stepSize;
// Reached the end of the address space, wrap around and
// store the previous step's values now
if(address >= memSize)
{
if(progPos == 0)
{
if(repeatCount > 1)
{
// Repeats are still remaining but the memory is full
delete screen;
return NULL;
}
// All of the screen memory has now had data loaded to it
return screen;
}
// Move back to start filling in the next step of values
progPos--;
address = progPos;
}
repeatCount--;
}
}
}
bool BbcImageLoader::getBit(uint8_t *bit)
{
if(bitsLeft == 0)
{
if(this->pos == this->size)
{
// End of data
return false;
}
bitStore = this->data[this->pos++];
bitsLeft = 8;
}
// Fetch the leftmost bit
*bit = (bitStore & 128) >> 7;
// Shift the remaining bits across
bitStore <<= 1;
// Decrement the bytes left counter
bitsLeft --;
return true;
}
bool BbcImageLoader::getBits(int numBits, uint8_t *bits)
{
*bits = 0;
uint8_t addBit;
// Must be between 1 and 8 bits that have been asked for
if(numBits < 1 || numBits > 8)
{
throw std::invalid_argument("numBits must be between 1 and 8");
}
for(int bitCount = 0; bitCount < 8; bitCount++)
{
// Shift the bits in the byte one place to the right
*bits >>= 1;
if(bitCount < numBits)
{
if(!getBit(&addBit))
{
// End of file
return false;
}
// Insert the returned bit as the msb of the byte
*bits |= addBit << 7;
}
}
return true;
}
BbcScreen* BbcImageLoader::LoadScrLoad()
{
this->pos = 0;
int screenSize = 0;
for(;;)
{
if(++this->pos == this->size)
{
// Unexpected EOF
return NULL;
}
int repeatCount = this->data[this->pos++];
if(repeatCount == 0)
{
screenSize += 256;
}
else
{
screenSize += repeatCount;
}
if(this->pos == this->size)
{
// EOF
break;
}
}
BbcScreen *screen = new BbcScreen(screenSize);
int address = 0;
this->pos = 0;
for(;;)
{
uint8_t valToRepeat = this->data[this->pos++];
int repeatCount = this->data[this->pos++];
if(repeatCount == 0)
{
repeatCount = 256;
}
for(int i = 0; i < repeatCount; i++)
{
screen->setScreenByte(address++, valToRepeat);
}
if(this->pos == this->size)
{
break;
}
}
return screen;
}