From 7da6c89604176173d0dd5c9680c3cbcd21d27fa1 Mon Sep 17 00:00:00 2001 From: Charles <37224242+Charlito33@users.noreply.github.com> Date: Fri, 23 Aug 2024 18:12:47 +0200 Subject: [PATCH] Update image loading to support scaling Added width and height parameters to the drawImage function to enable image scaling. Adjusted Surface class to pass these parameters appropriately. Enhanced image loading logic to handle different image types with scaling. --- lib/graphics/src/Surface.cpp | 17 ++++++++++------- lib/graphics/src/Surface.hpp | 2 +- lib/gui/src/elements/Image.cpp | 34 +++++++++++++++++----------------- 3 files changed, 28 insertions(+), 25 deletions(-) diff --git a/lib/graphics/src/Surface.cpp b/lib/graphics/src/Surface.cpp index bc19d8df..72dd79b1 100644 --- a/lib/graphics/src/Surface.cpp +++ b/lib/graphics/src/Surface.cpp @@ -249,19 +249,22 @@ namespace graphics color); } - void Surface::drawImage(const SImage &image, const int16_t x, const int16_t y) + void Surface::drawImage(const SImage &image, const int16_t x, const int16_t y, const uint16_t w, const uint16_t h) { + float scaleX = static_cast(w) / static_cast(image.getWidth()); + float scaleY = static_cast(h) / static_cast(image.getHeight()); + #ifdef ESP_PLATFORM switch (image.getType()) // image size with right format { case BMP: - m_sprite.drawBmpFile(image.getPath().str().c_str(), x, y); + m_sprite.drawBmpFile(image.getPath().str().c_str(), x, y, 0, 0, 0, 0, scaleX, scaleY); break; case PNG: - m_sprite.drawPngFile(image.getPath().str().c_str(), x, y); + m_sprite.drawPngFile(image.getPath().str().c_str(), x, y, 0, 0, 0, 0, scaleX, scaleY); break; case JPG: - m_sprite.drawJpgFile(image.getPath().str().c_str(), x, y); + m_sprite.drawJpgFile(image.getPath().str().c_str(), x, y, 0, 0, 0, 0, scaleX, scaleY); break; }; @@ -271,13 +274,13 @@ namespace graphics switch (image.getType()) { case BMP: - m_sprite.drawBmpFile(&file, image.getPath().str().c_str(), x, y); + m_sprite.drawBmpFile(&file, image.getPath().str().c_str(), x, y, 0, 0, 0, 0, scaleX, scaleY); break; case PNG: - m_sprite.drawPngFile(&file, image.getPath().str().c_str(), x, y); + m_sprite.drawPngFile(&file, image.getPath().str().c_str(), x, y, 0, 0, 0, 0, scaleX, scaleY); break; case JPG: - m_sprite.drawJpgFile(&file, image.getPath().str().c_str(), x, y); + m_sprite.drawJpgFile(&file, image.getPath().str().c_str(), x, y, 0, 0, 0, 0, scaleX, scaleY); break; }; #endif diff --git a/lib/graphics/src/Surface.hpp b/lib/graphics/src/Surface.hpp index c09f7552..47813b58 100644 --- a/lib/graphics/src/Surface.hpp +++ b/lib/graphics/src/Surface.hpp @@ -67,7 +67,7 @@ namespace graphics void drawLine(int16_t x1, int16_t y1, int16_t x2, int16_t y2, color_t color); - void drawImage(const SImage &image, int16_t x, int16_t y); + void drawImage(const SImage &image, int16_t x, int16_t y, uint16_t w = 0, uint16_t h = 0); void setFont(EFont font); void setFontSize(float fontSize); diff --git a/lib/gui/src/elements/Image.cpp b/lib/gui/src/elements/Image.cpp index 7bc45585..f78cfe90 100644 --- a/lib/gui/src/elements/Image.cpp +++ b/lib/gui/src/elements/Image.cpp @@ -20,32 +20,32 @@ namespace gui::ImagesList std::vector images; - std::shared_ptr loadImage(storage::Path path, uint16_t width, uint16_t height, color_t backgroundColor = 0xFFFF) - { - for (const auto& image : images) - { - if (image.path.str() == path.str() && image.width == width && image.height == height) - { - //std::cout << "image already loaded" << std::endl; + std::shared_ptr loadImage(storage::Path path, uint16_t width, uint16_t height, const color_t backgroundColor = 0xFFFF) { + // ReSharper disable once CppUseStructuredBinding + for (const auto& image : images) { + if (image.path.str() == path.str() && image.width == width && image.height == height) { return image.surface; } } - //std::cout << "image not loaded" << std::endl; + const auto i = graphics::SImage(path); - graphics::SImage i = graphics::SImage(path); + // libsystem::log("Image: " + std::to_string(i.getType()) + ", " + std::to_string(i.getWidth()) + ", " + std::to_string(i.getHeight()) + ", " + i.getPath().str()); - libsystem::log("Image: " + std::to_string(i.getType()) + ", " + std::to_string(i.getWidth()) + ", " + std::to_string(i.getHeight()) + ", " + i.getPath().str()); + ImageLoaded img = { + path, + i.getWidth(), + i.getHeight(), + std::make_shared(width, height) + }; - ImageLoaded img = { path, i.getWidth(), i.getHeight(), std::make_shared(i.getWidth(), i.getHeight()) }; - - uint16_t m_width = i.getWidth(); - uint16_t m_height = i.getHeight(); - - if(i.getType() != graphics::ImageType::BMP) + // Clear the background if it's a transparent image ? + // I guess so ? + if(i.getType() != graphics::ImageType::BMP) { img.surface->clear(backgroundColor); + } - img.surface->drawImage(i, 0, 0); + img.surface->drawImage(i, 0, 0, width, height); images.push_back(img);