Optimisation of Bitmap parsing
This commit is contained in:
parent
f72d2c372c
commit
06bb39e03a
@ -99,18 +99,15 @@ BmpReaderError Bitmap::parseHeaders() {
|
|||||||
|
|
||||||
if (width <= 0 || height <= 0) return BmpReaderError::BadDimensions;
|
if (width <= 0 || height <= 0) return BmpReaderError::BadDimensions;
|
||||||
|
|
||||||
// Palette for 8-bit indexed images
|
// Pre-calculate Row Bytes to avoid doing this every row
|
||||||
for (int i = 0; i < 256; i++) paletteLum[i] = static_cast<uint8_t>(i); // default grayscale ramp
|
rowBytes = (width * bpp + 31) / 32 * 4;
|
||||||
|
|
||||||
|
for (int i = 0; i < 256; i++) paletteLum[i] = static_cast<uint8_t>(i);
|
||||||
if (colorsUsed > 0) {
|
if (colorsUsed > 0) {
|
||||||
for (uint32_t i = 0; i < colorsUsed; i++) {
|
for (uint32_t i = 0; i < colorsUsed; i++) {
|
||||||
const int b = file.read();
|
uint8_t rgb[4];
|
||||||
const int g = file.read();
|
file.read(rgb, 4); // Read B, G, R, Reserved in one go
|
||||||
const int r = file.read();
|
paletteLum[i] = (77u * rgb[2] + 150u * rgb[1] + 29u * rgb[0]) >> 8;
|
||||||
file.seek(1, SeekCur); // reserved
|
|
||||||
const auto bb = static_cast<uint8_t>(b < 0 ? 0 : b);
|
|
||||||
const auto gg = static_cast<uint8_t>(g < 0 ? 0 : g);
|
|
||||||
const auto rr = static_cast<uint8_t>(r < 0 ? 0 : r);
|
|
||||||
paletteLum[i] = static_cast<uint8_t>((77u * rr + 150u * gg + 29u * bb) >> 8);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -122,72 +119,64 @@ BmpReaderError Bitmap::parseHeaders() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// packed 2bpp output, 0 = black, 1 = dark gray, 2 = light gray, 3 = white
|
// packed 2bpp output, 0 = black, 1 = dark gray, 2 = light gray, 3 = white
|
||||||
BmpReaderError Bitmap::readRow(uint8_t* data, const size_t dataLen, size_t* read) const {
|
BmpReaderError Bitmap::readRow(uint8_t* data, uint8_t* rowBuffer) const {
|
||||||
// Validate data is long enough to hold a row worth of data
|
// Note: rowBuffer should be pre-allocated by the caller to size 'rowBytes'
|
||||||
const size_t outputBytes = (width + 3) / 4;
|
if (file.read(rowBuffer, rowBytes) != rowBytes) return BmpReaderError::ShortReadRow;
|
||||||
if (dataLen < outputBytes) {
|
|
||||||
*read = 0;
|
|
||||||
return BmpReaderError::BufferTooSmall;
|
|
||||||
}
|
|
||||||
|
|
||||||
// setup data to be all black
|
uint8_t* outPtr = data;
|
||||||
memset(data, 0x00, outputBytes);
|
uint8_t currentOutByte = 0;
|
||||||
|
int bitShift = 6;
|
||||||
|
|
||||||
const size_t rowBytes = (width * bpp + 31) / 32 * 4;
|
// Helper lambda to pack 2bpp color into the output stream
|
||||||
const auto rowBuffer = static_cast<uint8_t*>(malloc(rowBytes));
|
auto packPixel = [&](uint8_t lum) {
|
||||||
if (!rowBuffer) {
|
uint8_t color = (lum >> 6); // Simple 2-bit reduction: 0-255 -> 0-3
|
||||||
*read = 0;
|
currentOutByte |= (color << bitShift);
|
||||||
return BmpReaderError::OomRowBuffer;
|
if (bitShift == 0) {
|
||||||
}
|
*outPtr++ = currentOutByte;
|
||||||
|
currentOutByte = 0;
|
||||||
if (file.read(rowBuffer, rowBytes) != rowBytes) {
|
bitShift = 6;
|
||||||
free(rowBuffer);
|
|
||||||
*read = 0;
|
|
||||||
return BmpReaderError::ShortReadRow;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int bmpX = 0; bmpX < width; bmpX++) {
|
|
||||||
uint8_t lum;
|
|
||||||
if (bpp == 1) {
|
|
||||||
const uint8_t byte = rowBuffer[bmpX / 8];
|
|
||||||
const uint8_t bit = 7 - (bmpX % 8);
|
|
||||||
const bool bitSet = (byte >> bit) & 0x01;
|
|
||||||
// In 1bpp BMPs, palette index 0 is conventionally black and index 1 is white.
|
|
||||||
lum = bitSet ? 0xFF : 0x00;
|
|
||||||
} else if (bpp == 2) {
|
|
||||||
const uint8_t byte = rowBuffer[bmpX / 4];
|
|
||||||
const uint8_t twobit = 6 - ((bmpX * 2) % 8);
|
|
||||||
const uint8_t val = (byte >> twobit) & 0x3;
|
|
||||||
lum = paletteLum[val];
|
|
||||||
} else if (bpp == 8) {
|
|
||||||
const uint8_t idx = rowBuffer[bmpX];
|
|
||||||
lum = paletteLum[idx];
|
|
||||||
} else {
|
} else {
|
||||||
// 24 / 32
|
bitShift -= 2;
|
||||||
const uint8_t* px = &rowBuffer[bmpX * bpp / 8];
|
|
||||||
const uint8_t b = px[0];
|
|
||||||
const uint8_t g = px[1];
|
|
||||||
const uint8_t r = px[2];
|
|
||||||
|
|
||||||
lum = static_cast<uint8_t>((77u * r + 150u * g + 29u * b) >> 8);
|
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
uint8_t color;
|
switch (bpp) {
|
||||||
if (lum >= 192) {
|
case 8: {
|
||||||
color = 0x3; // white
|
for (int x = 0; x < width; x++) {
|
||||||
} else if (lum >= 128) {
|
packPixel(paletteLum[rowBuffer[x]]);
|
||||||
color = 0x2; // light gray
|
}
|
||||||
} else if (lum >= 64) {
|
break;
|
||||||
color = 0x1; // dark gray
|
}
|
||||||
} else {
|
case 24: {
|
||||||
color = 0x0; // black
|
const uint8_t* p = rowBuffer;
|
||||||
|
for (int x = 0; x < width; x++) {
|
||||||
|
uint8_t lum = (77u * p[2] + 150u * p[1] + 29u * p[0]) >> 8;
|
||||||
|
packPixel(lum);
|
||||||
|
p += 3;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 1: {
|
||||||
|
for (int x = 0; x < width; x++) {
|
||||||
|
uint8_t lum = (rowBuffer[x >> 3] & (0x80 >> (x & 7))) ? 0xFF : 0x00;
|
||||||
|
packPixel(lum);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 32: {
|
||||||
|
const uint8_t* p = rowBuffer;
|
||||||
|
for (int x = 0; x < width; x++) {
|
||||||
|
uint8_t lum = (77u * p[2] + 150u * p[1] + 29u * p[0]) >> 8;
|
||||||
|
packPixel(lum);
|
||||||
|
p += 4;
|
||||||
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
data[bmpX / 4] |= (color << (6 - ((bmpX % 4) * 2)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
free(rowBuffer);
|
// Flush remaining bits if width is not a multiple of 4
|
||||||
*read = outputBytes;
|
if (bitShift != 6) *outPtr = currentOutByte;
|
||||||
|
|
||||||
return BmpReaderError::Ok;
|
return BmpReaderError::Ok;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -27,14 +27,15 @@ class Bitmap {
|
|||||||
public:
|
public:
|
||||||
static const char* errorToString(BmpReaderError err);
|
static const char* errorToString(BmpReaderError err);
|
||||||
|
|
||||||
explicit Bitmap(File& file) : file(file) {};
|
explicit Bitmap(File& file) : file(file) {}
|
||||||
BmpReaderError parseHeaders();
|
BmpReaderError parseHeaders();
|
||||||
BmpReaderError readRow(uint8_t* data, size_t dataLen, size_t* read) const;
|
BmpReaderError readRow(uint8_t* data, uint8_t* rowBuffer) const;
|
||||||
BmpReaderError rewindToData() const;
|
BmpReaderError rewindToData() const;
|
||||||
int getWidth() const { return width; }
|
int getWidth() const { return width; }
|
||||||
int getHeight() const { return height; }
|
int getHeight() const { return height; }
|
||||||
bool isTopDown() const { return topDown; }
|
bool isTopDown() const { return topDown; }
|
||||||
bool hasGreyscale() const { return bpp > 1; }
|
bool hasGreyscale() const { return bpp > 1; }
|
||||||
|
int getRowBytes() const { return rowBytes; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static uint16_t readLE16(File& f);
|
static uint16_t readLE16(File& f);
|
||||||
@ -46,5 +47,6 @@ class Bitmap {
|
|||||||
bool topDown = false;
|
bool topDown = false;
|
||||||
uint32_t bfOffBits = 0;
|
uint32_t bfOffBits = 0;
|
||||||
uint16_t bpp = 0;
|
uint16_t bpp = 0;
|
||||||
|
int rowBytes = 0;
|
||||||
uint8_t paletteLum[256] = {};
|
uint8_t paletteLum[256] = {};
|
||||||
};
|
};
|
||||||
|
|||||||
@ -134,6 +134,7 @@ void GfxRenderer::drawBitmap(const Bitmap& bitmap, const int x, const int y, con
|
|||||||
|
|
||||||
const uint8_t outputRowSize = (bitmap.getWidth() + 3) / 4;
|
const uint8_t outputRowSize = (bitmap.getWidth() + 3) / 4;
|
||||||
auto* outputRow = static_cast<uint8_t*>(malloc(outputRowSize));
|
auto* outputRow = static_cast<uint8_t*>(malloc(outputRowSize));
|
||||||
|
auto* rowBytes = static_cast<uint8_t*>(malloc(bitmap.getRowBytes()));
|
||||||
|
|
||||||
for (int bmpY = 0; bmpY < bitmap.getHeight(); bmpY++) {
|
for (int bmpY = 0; bmpY < bitmap.getHeight(); bmpY++) {
|
||||||
// The BMP's (0, 0) is the bottom-left corner (if the height is positive, top-left if negative).
|
// The BMP's (0, 0) is the bottom-left corner (if the height is positive, top-left if negative).
|
||||||
@ -146,18 +147,13 @@ void GfxRenderer::drawBitmap(const Bitmap& bitmap, const int x, const int y, con
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t readBytes;
|
if (bitmap.readRow(outputRow, rowBytes) != BmpReaderError::Ok) {
|
||||||
if (bitmap.readRow(outputRow, outputRowSize, &readBytes) != BmpReaderError::Ok) {
|
Serial.printf("[%lu] [GFX] Failed to read row %d from bitmap\n", millis(), bmpY);
|
||||||
free(outputRow);
|
free(outputRow);
|
||||||
|
free(rowBytes);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (readBytes != outputRowSize) {
|
|
||||||
Serial.printf("[%lu] [GFX] Failed to read BMP row data, got: %d, expected: %d\n", millis(), readBytes,
|
|
||||||
outputRowSize);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int bmpX = 0; bmpX < bitmap.getWidth(); bmpX++) {
|
for (int bmpX = 0; bmpX < bitmap.getWidth(); bmpX++) {
|
||||||
int screenX = x + bmpX;
|
int screenX = x + bmpX;
|
||||||
if (isScaled) {
|
if (isScaled) {
|
||||||
@ -180,6 +176,7 @@ void GfxRenderer::drawBitmap(const Bitmap& bitmap, const int x, const int y, con
|
|||||||
}
|
}
|
||||||
|
|
||||||
free(outputRow);
|
free(outputRow);
|
||||||
|
free(rowBytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
void GfxRenderer::clearScreen(const uint8_t color) const { einkDisplay.clearScreen(color); }
|
void GfxRenderer::clearScreen(const uint8_t color) const { einkDisplay.clearScreen(color); }
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user