-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathFTOLED_BMP.cpp
279 lines (246 loc) · 8.74 KB
/
FTOLED_BMP.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
/* FTOLED BMP parsing and bitmap display routines for BMP files */
#include <stdint.h>
#include <SD.h>
#include <FTOLED.h>
#ifdef ARDUINO_AVR_YUN
/*
On Arduino Yun, the Bridge library provides a conflicting
implementation of File. Therefore the sketch chooses one or
the other library via including SD.h or Bridge.h. FileIO header
(referenced here below) comes from the Bridge library. Whichever
header is missing as it's not in the include path generates a warning
but warning output is suppressed on Arduino 1.5.6.
See also https://github.com/arduino/Arduino/issues/1942
*/
#include <FileIO.h>
#endif
// Read a little-endian short word from a stream
template<typename T> inline uint16_t readShort(T &s)
{
return (uint16_t)s.read() | (uint16_t)s.read() << 8;
}
// Read a little-endian long from a stream
template<typename T> inline uint32_t readLong(T &s)
{
return (uint32_t)readShort(s) | (uint32_t)readShort(s) << 16;
}
enum BMP_Compression {
BMP_NoCompression = 0,
BMP_RLE8bpp = 1,
BMP_RLE4bpp = 2,
BMP_BITFIELDS = 3,
// ... lots more methods that aren't supported :)
};
const uint8_t OFFS_DIB_HEADER = 0x0e;
// Simple use of _displayBMP uses the SD File object directly
BMP_Status OLED::displayBMP(File &source, const int x, const int y) {
return _displayBMP(source, 0, 0, x, y);
}
BMP_Status OLED::displayBMP(File &source, const int from_x, const int from_y, const int to_x, const int to_y) {
return _displayBMP(source, from_x, from_y, to_x, to_y);
}
// In order to support Progmem we have a wrapper class that implements the seek()
// and read() methods as inline. This allows the _displayBMP template to compile
// against a PROGMEM stored buffer, using the same source as for a BMP file object!
//
// Yes, this is kinda sorta hacky but it means we don't have separate
// code paths for PROGMEM vs SD stored BMPs. Plus we still have fast code paths for
// both.
#ifdef __AVR__
class _Progmem_wrapper {
int base;
int current;
public:
inline _Progmem_wrapper(const uint8_t *base) : base((int)base),current((int)base) { }
inline uint8_t read() { return pgm_read_byte(current++); }
inline void read(void *buf, size_t len) {
uint8_t *tbuf = (uint8_t *)buf;
for(size_t i = 0; i < len; i++) // memcpy_PF not implemented in arduino's version of avrlibc
*tbuf++ = pgm_read_byte(current++);
}
inline boolean seek(uint32_t pos) { current=base+pos; return true; }
};
#else
// ARM version is the same, only we just treat the address like a const uint8_t*
//
// Because the ARM isn't Harvard arch this is a little inefficient, in that we're copying
// memory from one part of the address space to another sometimes. But it's not too bad as
// the implementation avoids this where possible to save RAM on the AVRs.
class _Progmem_wrapper {
const uint8_t *base;
const uint8_t *current;
public:
inline _Progmem_wrapper(const uint8_t *base) : base(base),current(base) { }
inline uint8_t read() { return *current++; }
inline void read(void *buf, size_t len) { memcpy(buf, current, len); current += len; }
inline boolean seek(uint32_t pos) { current=base+pos; return true; }
};
#endif
BMP_Status OLED::displayBMP(const uint8_t *pgm_addr, const int x, const int y) {
_Progmem_wrapper wrapper(pgm_addr);
return _displayBMP(wrapper, 0, 0, x, y);
}
BMP_Status OLED::displayBMP(const uint8_t *pgm_addr, const int from_x, const int from_y, const int to_x, const int to_y) {
_Progmem_wrapper wrapper(pgm_addr);
return _displayBMP(wrapper, from_x, from_y, to_x, to_y);
}
template<typename SourceType> BMP_Status OLED::_displayBMP(SourceType &source, const int from_x, const int from_y, const int to_x, const int to_y)
{
SourceType &f = source;
f.seek(0);
// File header, check magic number 'BM'
if(f.read() != 'B' || f.read() != 'M')
return BMP_INVALID_FORMAT;
// Read DIB header with image properties
f.seek(OFFS_DIB_HEADER);
uint16_t dib_headersize = readLong(f);
uint16_t width, height, bpp, compression;
bool v2header = (dib_headersize == 12); // BMPv2 header, no compression, no additional options
if(v2header) {
width = readShort(f);
height = readShort(f);
if(readShort(f) != 1)
return BMP_UNSUPPORTED_HEADER;
bpp = readShort(f);
compression = BMP_NoCompression;
}
else {
width = readLong(f);
height = readLong(f);
if(readShort(f) != 1)
return BMP_UNSUPPORTED_HEADER;
bpp = readShort(f);
compression = readLong(f);
}
// Verify image properties from header
if(bpp > 24)
return BMP_TOO_MANY_COLOURS;
if(bpp != 1 && bpp != 4 && bpp != 8 && bpp != 16 && bpp != 24)
return BMP_UNSUPPORTED_COLOURS;
if(!(compression == BMP_NoCompression
|| (compression == BMP_BITFIELDS && bpp == 16))) {
return BMP_COMPRESSION_NOT_SUPPORTED;
}
// In case of the bitfields option, determine the pixel format. We support RGB565 & RGB555 only
bool rgb565 = true;
if(compression == BMP_BITFIELDS) {
f.seek(0x36);
uint16_t b = readLong(f);
uint16_t g = readLong(f);
uint16_t r = readLong(f);
if(r == 0x001f && g == 0x07e0 && b == 0xf800)
rgb565 = true;
else if(r != 0x001f && g != 0x03e0 && b != 0x7c00)
return BMP_UNSUPPORTED_COLOURS; // Not RGB555 either
}
if (width < from_x || height < from_y)
return BMP_ORIGIN_OUTSIDE_IMAGE; // source in BMP is offscreen
// Find the starting offset for the data in the first row
f.seek(0x0a);
uint32_t data_offs = readLong(f);
assertCS();
// Trim height to 128, anything bigger gets cut off, then set up row span in memory
height = height - from_y;
if(to_y + height > 128)
height = 128-to_y;
// Calculate outputtable width and set up column span in memory
uint16_t out_width = width - from_x;
if(to_x + out_width > 128)
out_width = 128-to_x;
// Calculate the width in bits of each row (rounded up to nearest byte)
uint16_t row_bits = (width*bpp + 7) & ~7;
// Calculate width in bytes (4-byte boundary aligned)
uint16_t row_bytes = (row_bits/8 + 3) & ~3;
startWrite(to_x,to_y,to_x+out_width-1,to_y+height-1,false);
releaseCS();
// Read colour palette to RAM. It's quite hefty to hold in RAM (512 bytes for a full 8-bit palette)
// but don't have much choice as seeking back and forth on SD is painfully slow
OLED_Colour *palette;
if(bpp < 16) {
uint16_t palette_size = 1<<bpp;
palette = (OLED_Colour *)malloc(sizeof(OLED_Colour)*palette_size);
f.seek(OFFS_DIB_HEADER + dib_headersize);
for(uint16_t i = 0; i < palette_size; i++) {
uint8_t pal[4];
f.read(pal, v2header ? 3 : 4);
palette[i].blue = pal[0] >> 3;
palette[i].green = pal[1] >> 2;
palette[i].red = pal[2] >> 3;
}
}
for(byte row = 0; row < height; row++) {
f.seek(data_offs + (row+from_y)*row_bytes + from_x*bpp/8);
if(bpp > 15) {
OLED_Colour buf[out_width];
if(bpp == 24) {
for(uint16_t col = 0; col < out_width; col++) {
buf[col].blue = f.read() >> 3;
buf[col].green = f.read() >> 2;
buf[col].red = f.read() >> 3;
}
}
else if(rgb565)
for(uint16_t col = 0; col < out_width; col++) {
uint16_t bgr565 = readShort(f);
buf[col].blue = bgr565 & 0x1f;
buf[col].green = (bgr565 >> 5) & 0x3f;
buf[col].red = (bgr565 >> 11);
}
else { // RGB555
for(uint16_t col = 0; col < out_width; col++) {
uint16_t bgr555 = readShort(f);
buf[col].blue = bgr555 & 0x1f;
buf[col].green = ((bgr555 >> 5) & 0x1f) << 1;
buf[col].red = (bgr555 >> 10);
}
}
assertCS();
for(uint16_t col = 0; col < out_width; col++) {
writeData(buf[col]);
}
releaseCS();
}
else if(bpp == 8) {
uint8_t buf[out_width];
f.read(&buf, sizeof(buf));
assertCS();
for(uint16_t col = 0; col < out_width; col++) {
writeData(palette[buf[col]]);
}
releaseCS();
}
else if(bpp == 4) {
uint8_t buf[(out_width+1)/2];
f.read(&buf, sizeof(buf));
assertCS();
for(uint16_t col = 0; col < out_width/2; col++) {
writeData(palette[buf[col] >> 4]);
writeData(palette[buf[col] & 0x0F]);
}
if(out_width % 2) { // Odd width, last pixel comes from here
writeData(palette[buf[sizeof(buf)-1] >> 4]);
}
releaseCS();
}
else if(bpp == 1) {
const size_t row_bytes = (out_width+7)/8;
uint8_t buf[row_bytes];
f.read(&buf, row_bytes);
assertCS();
uint8_t bit = 1<<7;
uint8_t* byte = buf;
for(uint16_t col = 0; col < out_width; col++) {
writeData(*byte & bit ? palette[1] : palette[0]);
bit >>= 1;
if(bit == 0) {
bit = 1<<7;
byte++;
}
}
releaseCS();
}
}
if(bpp < 16)
free(palette);
return BMP_OK;
}