displayign images works with some dim

This commit is contained in:
2026-02-05 10:09:00 +01:00
parent cddc24e147
commit 7136ae7385
3 changed files with 48 additions and 20 deletions
+21 -2
View File
@@ -133,14 +133,20 @@ void scrollAllScrollableTexts(bool split_scroll_mode = false)
}
}
void drawImageFromMemoryByIndex(unsigned char image_index, short pos_x, short pos_y)
void drawImageFromMemoryByIndex(unsigned char image_index, short pos_x, short pos_y, unsigned char dim_percentage = 0)
{
Image* img = &saved_images[image_index];
auto dimBy = [dim_percentage](unsigned char color)
{
return color * (100 - dim_percentage) / 100;
};
for (unsigned char y = 0; y < img->height; y++)
{
for (unsigned char x = 0; x < img->width; x++)
{
setPixel(x + pos_x, y + pos_y, pixels.Color(img->pixels[y][x].r, img->pixels[y][x].g, img->pixels[y][x].b));
setPixel(x + pos_x, y + pos_y, pixels.Color(dimBy(img->pixels[y][x].r), dimBy(img->pixels[y][x].g), dimBy(img->pixels[y][x].b)));
}
}
}
@@ -164,6 +170,19 @@ void loop()
{
pixels.clear();
handle_server();
if (saved_images[0].width > 0)
{
drawImageFromMemoryByIndex(0, 0, 0);
}
else
{
if (text_nodes[0].is_deleted && text_nodes[2].is_deleted)
{
}
scrollAllScrollableTexts();
}
pixels.show();
}
+1 -1
View File
@@ -11,7 +11,7 @@ extern TextNode text_nodes[MAX_TEXT_NODES_COUNT];
extern Cursor cursor;
void drawImageFromMemoryByIndex(unsigned char image_index, short pos_x, short pos_y);
void drawImageFromMemoryByIndex(unsigned char image_index, short pos_x, short pos_y, unsigned char dim_percentage = 0);
void setPixel(short x, short y, uint32_t color);
void start_server();
void handle_server();
+22 -13
View File
@@ -13,6 +13,7 @@ WebServer server(80);
Pixel new_image[16][16];
char* fileContent = nullptr;
size_t fileContent_len = 0;
String upload_error_message = "";
void handleRoot()
@@ -29,6 +30,7 @@ void handleUploadPage()
void handleBmpUpload() {
HTTPUpload& upload = server.upload();
if (upload.status == UPLOAD_FILE_START) {
upload_error_message = "";
if (fileContent) {
free(fileContent);
fileContent = nullptr;
@@ -46,11 +48,18 @@ void handleBmpUpload() {
char header[54];
memcpy(header, fileContent, 54);
int dataOffset = *(int*)&header[10];
int width = *(int*)&header[18];
int height = *(int*)&header[22];
short bitsPerPixel = *(short*)&header[28];
if (bitsPerPixel != 24) {
upload_error_message = "Unsupported BMP format: Only 24-bit BMPs are supported";
return;
}
if (width < 1 || width > 48 || height < 1 || height > 16) {
server.send(400, "text/plain", "Invalid image dimensions");
upload_error_message = "Invalid image dimensions";
return;
}
@@ -59,21 +68,17 @@ void handleBmpUpload() {
// Read pixel data
int row_padded = (width*3 + 3) & (~3);
char* pixel_data = fileContent + 54;
char* pixel_data = fileContent + dataOffset;
for (int y = height - 1; y >= 0; y--) {
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
saved_images[0].pixels[y][x].b = pixel_data[y*row_padded + x*3 + 0];
saved_images[0].pixels[y][x].g = pixel_data[y*row_padded + x*3 + 1];
saved_images[0].pixels[y][x].r = pixel_data[y*row_padded + x*3 + 2];
int bmp_row = height - 1 - y;
saved_images[0].pixels[y][x].b = pixel_data[bmp_row*row_padded + x*3 + 0];
saved_images[0].pixels[y][x].g = pixel_data[bmp_row*row_padded + x*3 + 1];
saved_images[0].pixels[y][x].r = pixel_data[bmp_row*row_padded + x*3 + 2];
}
}
pixels.clear();
drawImageFromMemoryByIndex(0, 0, 0);
pixels.show();
server.send(200, "text/plain", "OK");
free(fileContent);
fileContent = nullptr;
fileContent_len = 0;
@@ -86,7 +91,7 @@ void handleBmpUpload() {
void handleShowSaved()
{
pixels.clear();
drawImageFromMemoryByIndex(0, 0, 0);
drawImageFromMemoryByIndex(0, 0, 0, 100);
pixels.show();
server.send(200, "text/plain", "OK");
}
@@ -106,7 +111,11 @@ void start_server()
server.on("/show-saved", handleShowSaved);
server.on("/upload-page", HTTP_GET, handleUploadPage);
server.on("/upload-bmp", HTTP_POST, []() {
server.send(200, "text/plain", "");
if (upload_error_message.length() > 0) {
server.send(400, "text/plain", upload_error_message);
} else {
server.send(200, "text/plain", "Upload OK");
}
}, handleBmpUpload);
server.begin();
}