-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmicroBmp.c
More file actions
275 lines (239 loc) · 9 KB
/
microBmp.c
File metadata and controls
275 lines (239 loc) · 9 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
#include <stdlib.h>
#include <stdbool.h>
#include "microBmp.h"
typedef struct bmp_RGB {
uint8_t r;
uint8_t g;
uint8_t b;
}bmp_RGB;
inline static uint8_t trailingZeros(uint32_t v)
{
unsigned int c = 32; // c will be the number of zero bits on the right
v &= -((int32_t)(v));
if (v) c--;
if (v & 0x0000FFFF) c -= 16;
if (v & 0x00FF00FF) c -= 8;
if (v & 0x0F0F0F0F) c -= 4;
if (v & 0x33333333) c -= 2;
if (v & 0x55555555) c -= 1;
return (uint8_t)c;
}
inline static uint8_t popcount(uint32_t v)
{
v = v - ((v >> 1) & 0x55555555); // reuse input as temporary
v = (v & 0x33333333) + ((v >> 2) & 0x33333333); // temp
return (((v + (v >> 4)) & 0xF0F0F0F) * 0x1010101) >> 24; // count
}
inline static uint8_t stretchTo8bit(uint8_t v, uint8_t mask)
{
// 0001'1111 | pop = 5
// 1111'1000 | << 8-5
// (bits - (8-bits)) = bits - 8 + bits = 2*bits -8
// 0000'0011 2
// 1100'0000 6
//
uint8_t bits = popcount(mask);
if (bits < 8) {
v = (v << (8 - bits));
//if (bits| (v >> (8 - ())
// todo: bill empty bits
}
return v;
}
static inline size_t calc_row_size(const microBmp_BmpInfo * dibHeader) {
/* Weird formula because BMP row sizes are padded up to a multiple of 4 bytes. */
return (((dibHeader->bitsPerPixel * dibHeader->imageWidth) + 31) / 32) * 4;
}
static bool microBmp_checkSupportedCompression(const microBmp_BmpInfo* dibHeader) {
return (dibHeader->compressionMethod == 0)
|| ( (dibHeader->compressionMethod == 3)
&& (dibHeader->bitsPerPixel == 16))
|| ( (dibHeader->compressionMethod == 3)
&& ((dibHeader->bitsPerPixel == 32) || (dibHeader->bitsPerPixel == 24))
&& (dibHeader->maskR == 0x00FF0000)
&& (dibHeader->maskG == 0x0000FF00)
&& (dibHeader->maskB == 0x000000FF)
)
;
}
microBmpStatus microBmp_init(microBmp_State* o_this, uint8_t* io_buffer, size_t i_buffersize, microBmp_loadDataFunc i_loadDataFunc, void* i_userData)
{
if (i_buffersize < sizeof(microBmp_FileMetaData)){
return MBMP_STATUS_CACHE_BUFFER_TOO_SMALL;
}
o_this->loadDataFunc = i_loadDataFunc;
o_this->loadDataUserData = i_userData;
/* load BMP meta data */
if (i_loadDataFunc) {
i_loadDataFunc(io_buffer, sizeof(microBmp_FileMetaData), 0, i_userData);
}
const microBmp_FileHeader* fileheader = &((microBmp_FileMetaData*)io_buffer)->fileHeader;
const microBmp_BmpInfo* dibHeader = &((microBmp_FileMetaData*)io_buffer)->bmpInfo;
uint32_t imgDataOffset = fileheader->imageDataOffset;
if (fileheader->fileIdentifier != 19778) {
return MBMP_STATUS_UNSUPPORTED_FILE_TYPE;
}
if ( ( (dibHeader->bitsPerPixel != 1)
&& (dibHeader->bitsPerPixel != 4)
&& (dibHeader->bitsPerPixel != 8)
&& (dibHeader->bitsPerPixel != 16)
&& (dibHeader->bitsPerPixel != 24)
&& (dibHeader->bitsPerPixel != 32))
|| !microBmp_checkSupportedCompression(dibHeader)
|| (dibHeader->colorPlanes != 1)
)
{
return MBMP_STATUS_UNSUPPORTED_BMP_FORMAT;
}
o_this->imageWidth = (uint16_t)dibHeader->imageWidth;
o_this->imageHeight = (uint16_t)dibHeader->imageHeight;
o_this->bitsPerPixel = (uint8_t)dibHeader->bitsPerPixel;
o_this->bytesPerPixel = o_this->bitsPerPixel / 8;
o_this->colorsInPalette = (uint16_t)dibHeader->colorsInPalette;
o_this->palette = NULL;
o_this->bytesPerRow = calc_row_size(dibHeader);
o_this->endOfImage = (imgDataOffset + (uint32_t)o_this->bytesPerRow * dibHeader->imageHeight);
/* Calculating file constants */
if (o_this->bitsPerPixel == 16) {
if (dibHeader->compressionMethod == 3) {
o_this->shiftR = trailingZeros(dibHeader->maskR);
o_this->shiftG = trailingZeros(dibHeader->maskG);
o_this->shiftB = trailingZeros(dibHeader->maskB);
o_this->maskR = (uint8_t)(dibHeader->maskR >> o_this->shiftR);
o_this->maskG = (uint8_t)(dibHeader->maskG >> o_this->shiftG);
o_this->maskB = (uint8_t)(dibHeader->maskB >> o_this->shiftB);
} else {
o_this->shiftR = 10;
o_this->shiftG = 5;
o_this->shiftB = 0;
o_this->maskR = 0x1F;
o_this->maskG = 0x1F;
o_this->maskB = 0x1F;
}
}
o_this->currentRow = 0;
if (o_this->bitsPerPixel <= 8) { // palette image use part of buffer as palette buffer and rest as data cache
uint32_t paletteOffset = sizeof(microBmp_FileHeader) + dibHeader->headerSize;
if ( (o_this->bitsPerPixel == 1)
&& (o_this->colorsInPalette == 0)
&& (imgDataOffset > paletteOffset)) { // for some strange reason color count is zero in 1 bit bmps, even if there is a palette
o_this->colorsInPalette = 2;
}
uint32_t paletteSize = o_this->colorsInPalette * 4;
uint32_t reqMinBuffersize = paletteSize + o_this->bytesPerRow;
if (reqMinBuffersize <= i_buffersize) {
if (i_loadDataFunc) {
o_this->palette = io_buffer;
io_buffer += paletteSize;
i_buffersize -= paletteSize;
i_loadDataFunc(o_this->palette, paletteSize, paletteOffset, i_userData);
} else {
o_this->palette = io_buffer + paletteOffset;
}
} else {
return MBMP_STATUS_CACHE_BUFFER_TOO_SMALL;
}
}
o_this->cachedRows = 0;
o_this->cacheSizeRows = (uint16_t)( i_buffersize/o_this->bytesPerRow);
o_this->cacheSizeBytes = o_this->cacheSizeRows * o_this->bytesPerRow;
o_this->imageData = io_buffer;
if (o_this->cacheSizeRows == 0 || o_this->imageData == NULL) {
return MBMP_STATUS_CACHE_BUFFER_TOO_SMALL;
}
return MBMP_STATUS_OK;
}
const uint8_t* microBmp_getNextRow(microBmp_State * io_this)
{
if (io_this->currentRow == io_this->imageHeight) {
return NULL;
}
if(io_this->cachedRows == 0)
{
if (io_this->loadDataFunc) {
/* BMP stores image data backwards, counting back to the row we want to start the read at. */
uint32_t offset = io_this->endOfImage - (uint32_t)io_this->bytesPerRow * (uint32_t)(io_this->currentRow + io_this->cacheSizeRows);
io_this->loadDataFunc(io_this->imageData, io_this->cacheSizeBytes, offset, io_this->loadDataUserData);
io_this->cachedRows = io_this->cacheSizeRows;
/* Move the row pointer forward to the start of the last row **/
io_this->rowData = io_this->imageData + (io_this->bytesPerRow) * (io_this->cacheSizeRows - 1);
} else {
uint32_t offset = io_this->endOfImage - (uint32_t)io_this->bytesPerRow * (uint32_t)(io_this->currentRow+1);
io_this->rowData = io_this->imageData + offset;
io_this->cachedRows = 1;
}
}
else
{
/* Moving down a row (which is backwards in memory)
RowData is in 2 byte chunks (uint16), rowSize in bytes. Divide by 2. */
io_this->rowData -= io_this->bytesPerRow;
}
--io_this->cachedRows;
io_this->currentRow += 1;
return io_this->rowData;
}
void microBmp_setNextRow(microBmp_State* io_this, uint16_t row)
{
/** \todo do not invalidate all cash rows if not necessary */
io_this->currentRow = row;
io_this->cachedRows = 0;
}
static bmp_RGB microBmp_getColorAt(const microBmp_State* i_this, uint16_t x)
{
bmp_RGB col;
const uint8_t* coldata;
if (i_this->palette) {
uint32_t bitOff = x * i_this->bitsPerPixel;
uint32_t byteOff = bitOff / 8;
uint32_t idx = i_this->rowData[byteOff];
if (i_this->bitsPerPixel == 4) { // multiple pixel per byte - refine index
if (x & 1) {
idx = idx & 0xf;
}else{
idx = idx >> 4;
}
} else if (i_this->bitsPerPixel == 1) { // multiple pixel per byte - refine index
idx = ((idx << (bitOff % 8)) & 0x80)?1:0;
}
coldata = &i_this->palette[idx * 4];
col.b = coldata[0];
col.g = coldata[1];
col.r = coldata[2];
} else if(i_this->bytesPerPixel == 2) {
const uint16_t* u16Row = (const uint16_t*)i_this->rowData;
uint16_t c16 = u16Row[x];
col.r = stretchTo8bit((c16 >> i_this->shiftR)& i_this->maskR, i_this->maskR);
col.g = stretchTo8bit((c16 >> i_this->shiftG)& i_this->maskG, i_this->maskG);
col.b = stretchTo8bit((c16 >> i_this->shiftB)& i_this->maskB, i_this->maskB);
} else {
uint32_t byteOff = x * i_this->bytesPerPixel;
coldata = &i_this->rowData[byteOff];
col.b = coldata[0];
col.g = coldata[1];
col.r = coldata[2];
}
return col;
}
void microBmp_convertRowToRGB(const microBmp_State* i_this, uint8_t* o_targetBuf, uint16_t x1, uint16_t x2) {
while (x1 < x2) {
bmp_RGB c = microBmp_getColorAt(i_this, x1);
o_targetBuf[0] = c.r;
o_targetBuf[1] = c.g;
o_targetBuf[2] = c.b;
o_targetBuf += 3;
++x1;
}
}
void microBmp_convertRowTo565(const microBmp_State* i_this, uint16_t* o_targetBuf, uint16_t x1, uint16_t x2) {
/// \todo make efficient by dedicated implemtentation instead of converting to rgb and then back to 565
while (x1 < x2) {
bmp_RGB c = microBmp_getColorAt(i_this, x1);
*o_targetBuf = ( ((uint16_t)c.r & 0xF8) << 8)
| ( ((uint16_t)c.g & 0xFC) << 3)
| ( (uint16_t)c.b >> 3 )
;
o_targetBuf++;
++x1;
}
}