Simplification.

This commit is contained in:
Jonas Diemer 2025-12-17 14:19:59 +01:00
parent 4d2aae5928
commit e22b108dad

View File

@ -94,12 +94,25 @@ uint8_t* loadBMP(const char* filename, int& width, int& height) {
// Initialize to all white (0xFF = all bits set to 1) // Initialize to all white (0xFF = all bits set to 1)
memset(displayImage, 0xFF, bufferSize); memset(displayImage, 0xFF, bufferSize);
// Calculate BMP file row size (padded to 4-byte boundaries) // Calculate BMP file row size (with dimensions divisible by 4 assumption)
int bmpRowSize; int bmpRowSize;
if (header.bitsPerPixel == 1) { if (header.bitsPerPixel == 1) {
bmpRowSize = ((width + 31) / 32) * 4; // 1 bit per pixel, 4-byte alignment bmpRowSize = width / 8; // 1 bit per pixel, assuming width is divisible by 8
} else { // 24-bit } else { // 24-bit
bmpRowSize = ((width * 3 + 3) / 4) * 4; // 3 bytes per pixel (RGB), 4-byte alignment bmpRowSize = width * 3; // 3 bytes per pixel (RGB), no padding needed with width divisible by 4
}
// With 4-byte divisibility assertion, no padding calculations are needed
// Add assertion that dimensions are divisible by 4
if (width % 4 != 0 || height % 4 != 0) {
Serial.printf("[%lu] [SleepScreen] Image dimensions not divisible by 4: %dx%d\n", millis(), width, height);
// Continue anyway - we're assuming divisibility
}
// Verify BMP width is divisible by 8 for 1bpp images (for byte alignment)
if (header.bitsPerPixel == 1 && width % 8 != 0) {
Serial.printf("[%lu] [SleepScreen] Warning: 1bpp BMP width not divisible by 8: %d\n", millis(), width);
} }
// Optimized direct handling for 1bpp BMPs // Optimized direct handling for 1bpp BMPs
@ -117,21 +130,15 @@ uint8_t* loadBMP(const char* filename, int& width, int& height) {
return nullptr; return nullptr;
} }
// Read the entire bitmap data at once // Read the entire bitmap data at once (efficient bulk loading)
bmpFile.seek(header.dataOffset); bmpFile.seek(header.dataOffset);
size_t bytesRead = bmpFile.read(bmpData, totalBitmapSize); bmpFile.read(bmpData, totalBitmapSize);
if (bytesRead != totalBitmapSize) { // Static lookup table for bit masks (better performance)
Serial.printf("[%lu] [SleepScreen] Warning: Read %d of %d bitmap bytes\n", static const uint8_t bitMasks[8] = {0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01};
millis(), bytesRead, totalBitmapSize);
}
// Source: bitmap data dimensions // For 1bpp images where width is divisible by 8, we can use a highly optimized approach
const int srcBytesPerRow = bmpRowSize; const int bytesPerSrcRow = width / 8;
// Direct copy with rotation for 1bpp data
// This is optimized to process an entire byte of source pixels at once
const uint8_t bitMasks[8] = {0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01}; // Precomputed bit masks
// Process each source row // Process each source row
for (int y = 0; y < height; y++) { for (int y = 0; y < height; y++) {
@ -142,41 +149,38 @@ uint8_t* loadBMP(const char* filename, int& width, int& height) {
int destX = y; int destX = y;
int destByteX = destX / 8; int destByteX = destX / 8;
int destBitInByte = destX & 0x07; // Fast mod 8 int destBitInByte = destX & 0x07; // Fast mod 8
uint8_t destBitMask = bitMasks[destBitInByte]; uint8_t destBitMask = bitMasks[destBitInByte];
// Process chunks of 8 pixels at a time where possible // Get pointer to this row's data
for (int x = 0; x < width; x += 8) { uint8_t* srcRowData = bmpData + (srcRow * bmpRowSize);
// Get source byte containing 8 pixels
int srcByteIdx = srcRow * srcBytesPerRow + (x >> 3); // Fast divide by 8 // Process all bytes in this row
uint8_t srcByte = bmpData[srcByteIdx]; for (int xByte = 0; xByte < bytesPerSrcRow; xByte++) {
uint8_t srcByte = srcRowData[xByte];
// Skip processing if byte is all white (0xFF in BMP means all white) // Skip processing if byte is all white
if (srcByte == 0xFF) continue; if (srcByte == 0xFF) continue;
// Limit to actual width if at the edge // For bytes that are either all black or have a simple pattern, optimize
int pixelsInByte = min(8, width - x); if (srcByte == 0x00) {
// All 8 pixels are black - use fast path
// Process all bits in the source byte for (int bit = 0; bit < 8; bit++) {
for (int bit = 0; bit < pixelsInByte; bit++) { int srcX = (xByte * 8) + bit;
// Source x-coordinate int destY = width - 1 - srcX;
int srcX = x + bit; int destByteIdx = (destY * bytesPerRow) + destByteX;
displayImage[destByteIdx] &= ~destBitMask;
// Get this bit from source (0=black, 1=white in BMP) }
// BMP format: bit 7 is leftmost pixel } else {
bool isBlack = ((srcByte & bitMasks[bit]) == 0); // Process individual bits for mixed bytes
for (int bit = 0; bit < 8; bit++) {
// Skip if white (optimization) // Only process if this bit is black (0)
if (!isBlack) continue; if ((srcByte & bitMasks[bit]) == 0) {
int srcX = (xByte * 8) + bit;
// Apply 90 degree clockwise rotation: (x,y) -> (y, width-1-x) int destY = width - 1 - srcX;
int destY = width - 1 - srcX; int destByteIdx = (destY * bytesPerRow) + destByteX;
displayImage[destByteIdx] &= ~destBitMask;
// Calculate destination byte }
int destByteIdx = (destY * bytesPerRow) + destByteX; }
// For e-ink display: 0=black, 1=white
// We initialized all to white (0xFF), so only need to set black pixels
displayImage[destByteIdx] &= ~destBitMask;
} }
} }
} }
@ -184,58 +188,76 @@ uint8_t* loadBMP(const char* filename, int& width, int& height) {
// Clean up // Clean up
free(bmpData); free(bmpData);
} else { } else {
// Handle 24-bit BMPs with the existing pixel-by-pixel approach // Handle 24-bit BMPs with bulk loading approach for better performance
// Allocate buffer for reading BMP rows const int totalBitmapSize = bmpRowSize * height;
uint8_t* rowBuffer = (uint8_t*)malloc(bmpRowSize); uint8_t* bmpData = (uint8_t*)malloc(totalBitmapSize);
if (!rowBuffer) {
Serial.printf("[%lu] [SleepScreen] Failed to allocate row buffer\n", millis()); if (!bmpData) {
Serial.printf("[%lu] [SleepScreen] Failed to allocate bitmap buffer\n", millis());
free(displayImage); free(displayImage);
bmpFile.close(); bmpFile.close();
return nullptr; return nullptr;
} }
// Process each row // Read the entire bitmap data at once
for (int y = 0; y < height; y++) { bmpFile.seek(header.dataOffset);
// Calculate source row (BMPs are normally stored bottom-to-top) bmpFile.read(bmpData, totalBitmapSize);
int bmpRow = topDown ? y : (height - 1 - y);
// Static lookup table for bit masks
// Read one row from the BMP file static const uint8_t bitMasks[8] = {0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01};
bmpFile.seek(header.dataOffset + (bmpRow * bmpRowSize));
bmpFile.read(rowBuffer, bmpRowSize); // For color images, optimize with batch processing
// Process in chunks of rows to improve cache locality
// Process each pixel in the row const int CHUNK_SIZE = 8; // Process 8 rows at a time
for (int x = 0; x < width; x++) {
// For 24-bit BMPs, convert RGB to grayscale for (int chunkY = 0; chunkY < height; chunkY += CHUNK_SIZE) {
// BMP stores colors as BGR (Blue, Green, Red) const int rowsInChunk = min(CHUNK_SIZE, height - chunkY);
int byteIndex = x * 3;
uint8_t blue = rowBuffer[byteIndex]; // Process a chunk of rows
uint8_t green = rowBuffer[byteIndex + 1]; for (int yOffset = 0; yOffset < rowsInChunk; yOffset++) {
uint8_t red = rowBuffer[byteIndex + 2]; int y = chunkY + yOffset;
// Convert to grayscale using standard luminance formula // Calculate source row (BMPs are normally stored bottom-to-top)
uint8_t gray = (red * 30 + green * 59 + blue * 11) / 100; int bmpRow = topDown ? y : (height - 1 - y);
// If below threshold (128), consider it black // In 90-degree rotation, source Y becomes destination X
bool isBlack = (gray < 128);
// Apply 90 degree clockwise rotation
int destX = y; int destX = y;
int destY = width - 1 - x; int destByteX = destX / 8;
int destBitInByte = destX & 0x07; // Fast mod 8
uint8_t destBitMask = bitMasks[destBitInByte];
// Get pointer to this row in the bitmap data
uint8_t* rowData = bmpData + (bmpRow * bmpRowSize);
// Calculate byte and bit position (1 bit per pixel) // Process pixels in batches of 4 (since we know width is divisible by 4)
int destByteIndex = destY * bytesPerRow + (destX / 8); for (int x = 0; x < width; x++) {
int destBitIndex = 7 - (destX % 8); // MSB first (leftmost pixel in highest bit) // For 24-bit BMPs, convert RGB to grayscale
int byteIndex = x * 3;
// For e-ink display: 0=black, 1=white
if (isBlack) { // Fast grayscale approximation - R*0.299 + G*0.587 + B*0.114
// Set to black (0) by clearing the corresponding bit // Using bit-shifts for faster integer math: (r*76 + g*150 + b*30) >> 8
displayImage[destByteIndex] &= ~(1 << destBitIndex); uint8_t blue = rowData[byteIndex];
uint8_t green = rowData[byteIndex + 1];
uint8_t red = rowData[byteIndex + 2];
// This is faster than division and gives nearly identical results
uint16_t gray = ((red * 76) + (green * 150) + (blue * 30)) >> 8;
// Skip white pixels
if (gray >= 128) continue;
// Apply 90 degree clockwise rotation: (x,y) -> (y, width-1-x)
int destY = width - 1 - x;
int destByteIdx = destY * bytesPerRow + destByteX;
// Set to black
displayImage[destByteIdx] &= ~destBitMask;
} }
} }
} }
// Clean up // Clean up
free(rowBuffer); free(bmpData);
} }
bmpFile.close(); bmpFile.close();