From 298435488464a92e61cc89816eaf2e06d56a1f86 Mon Sep 17 00:00:00 2001 From: tobid7 Date: Tue, 1 Sep 2026 08:15:03 +0200 Subject: [PATCH] implement Image::Convert and Image::Flip --- CMakeLists.txt | 1 + include/pd/image/convert.hpp | 58 +++++++++++++++++++++++++ include/pd/image/image.hpp | 2 + source/image/convert.cpp | 82 ++++++++++++++++++++++++++++++++++++ source/image/image.cpp | 76 ++++++++++++++++++++++++++++++++- 5 files changed, 218 insertions(+), 1 deletion(-) create mode 100644 include/pd/image/convert.hpp create mode 100644 source/image/convert.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index f577a30..9bf2f30 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -39,6 +39,7 @@ set(PD_SOURCES source/core/timetrace.cpp # Image + source/image/convert.cpp source/image/image.cpp # Drivers diff --git a/include/pd/image/convert.hpp b/include/pd/image/convert.hpp new file mode 100644 index 0000000..96394a6 --- /dev/null +++ b/include/pd/image/convert.hpp @@ -0,0 +1,58 @@ +#pragma once + +/* +MIT License +Copyright (c) 2024 - 2026 René Amthor (tobid7) + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + */ + +#include +#include +#include + +namespace PD { +/** + * Namespace containing function to convert images + */ +namespace ImgConvert { +/** + * Convert RGB24 to RGBA32 by adding a 4th alpha value set to 255 + * to every pixel + * @param out Result List + * @param in Input Buffer List (rgb24) + * @param w width of the image + * @param h height of the image + */ +PD_API +void RGB24toRGBA32(std::vector& out, const std::vector& in, + const int& w, const int& h); +PD_API +void RGB32toRGBA24(std::vector& out, const std::vector& in, + const int& w, const int& h); +/** + * Reverse 32 (RGBA -> ABGR || ABGR -> RGBA) + * @param buf Buffer to convert + * @param w width + * @param h height + */ +PD_API void Reverse32(std::vector& buf, const int& w, const int& h); +PD_API void ReverseBuf(std::vector& buf, size_t bpp, int w, int h); +} // namespace ImgConvert +} // namespace PD \ No newline at end of file diff --git a/include/pd/image/image.hpp b/include/pd/image/image.hpp index 126174b..2c20371 100644 --- a/include/pd/image/image.hpp +++ b/include/pd/image/image.hpp @@ -40,6 +40,8 @@ class PD_API Image { static int Format2Bpp(Format fmt); static Format GuessFmtFromBpp(int bpp); + void Convert(Format dst); + private: std::vector pData; ivec2 pSize; diff --git a/source/image/convert.cpp b/source/image/convert.cpp new file mode 100644 index 0000000..218ea3e --- /dev/null +++ b/source/image/convert.cpp @@ -0,0 +1,82 @@ +/* +MIT License +Copyright (c) 2024 - 2026 René Amthor (tobid7) + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + */ + +#include + +namespace PD::ImgConvert { + +PD_API void RGB24toRGBA32(std::vector& out, const std::vector& in, + const int& w, const int& h) { + // Converts RGB24 to RGBA32 + for (int y = 0; y < h; y++) { + for (int x = 0; x < w; x++) { + int src = (y * w + x) * 3; + int dst = (y * w + x) * 4; + out[dst + 0] = in[src + 0]; + out[dst + 1] = in[src + 1]; + out[dst + 2] = in[src + 2]; + out[dst + 3] = 255; + } + } +} + +PD_API void RGB32toRGBA24(std::vector& out, const std::vector& in, + const int& w, const int& h) { + // Converts RGB24 to RGBA32 + for (int y = 0; y < h; y++) { + for (int x = 0; x < w; x++) { + int src = (y * w + x) * 4; + int dst = (y * w + x) * 3; + out[dst + 0] = in[src + 0]; + out[dst + 1] = in[src + 1]; + out[dst + 2] = in[src + 2]; + } + } +} + +PD_API void Reverse32(std::vector& buf, const int& w, const int& h) { + for (int x = 0; x < w; x++) { + for (int y = 0; y < h; y++) { + int i = y * w + x; + u8 t0 = buf[i + 0]; + u8 t1 = buf[i + 1]; + buf[i + 0] = buf[i + 3]; + buf[i + 1] = buf[i + 2]; + buf[i + 3] = t0; + buf[i + 2] = t1; + } + } +} + +PD_API void ReverseBuf(std::vector& buf, size_t bpp, int w, int h) { + std::vector cpy = buf; + for (int x = 0; x < w; x++) { + for (int y = 0; y < h; y++) { + int pos = (y * w + x) * bpp; + for (size_t i = 0; i < bpp; i++) { + buf[pos + bpp - 1 - i] = cpy[pos + i]; + } + } + } +} +} // namespace PD::ImgConvert \ No newline at end of file diff --git a/source/image/image.cpp b/source/image/image.cpp index 89a32b9..9f1e131 100644 --- a/source/image/image.cpp +++ b/source/image/image.cpp @@ -1,3 +1,4 @@ +#include #include #if defined(PD_INCLUDE_STB_IMAGE) @@ -54,6 +55,53 @@ PD_API void Image::Copy(const std::vector& pixels, int w, int h, int bpp) { pFormat = GuessFmtFromBpp(bpp); } +PD_API void Image::Convert(Format dst) { + if (pFormat == dst) { + return; + } else if (pFormat == Format::RGB && dst == Format::BGR) { + ImgConvert::ReverseBuf(pData, 3, pSize.x, pSize.y); + pFormat = Format::BGR; + } else if (pFormat == Format::RGB && dst == Format::RGBA) { + std::vector cpy = pData; + pData.resize(pSize.x * pSize.y * 4); + ImgConvert::RGB24toRGBA32(pData, cpy, pSize.x, pSize.y); + pFormat = Format::RGBA; + } else if (pFormat == Format::RGBA && dst == Format::RGB) { + std::vector cpy = pData; + pData.resize(pSize.x * pSize.y * 3); + ImgConvert::RGB32toRGBA24(pData, cpy, pSize.x, pSize.y); + pFormat = Format::RGB; + } else if (pFormat == Format::RGBA && dst == Format::BGRA) { + for (int i = 0; i < (pSize.x * pSize.y * 4); i += 4) { + u8 _tmp = pData[i + 0]; + pData[i + 0] = pData[i + 2]; + pData[i + 2] = _tmp; + } + } else if (pFormat == Format::RGBA && dst == Format::RGB565) { + Convert(Format::RGB); + Convert(Format::RGB565); + } else if (pFormat == Format::RGB && dst == Format::RGB565) { + auto f = [](u8 r, u8 g, u8 b) -> u16 { + u16 _r = (r >> 3); + u16 _g = (g >> 2); + u16 _b = (b >> 3); + return (_r << 11) | (_g << 5) | _b; + }; + std::vector cpy = pData; + pData.resize(pSize.x * pSize.y * 2); + for (int y = 0; y < pSize.x; y++) { + for (int x = 0; x < pSize.y; x++) { + int src = (y * pSize.x + x) * 3; + int dst = (y * pSize.x + x) * 2; + u16 new_px = f(cpy[src + 0], cpy[src + 1], cpy[src + 2]); + pData[dst + 0] = new_px >> 8; + pData[dst + 1] = new_px & 0xff; + } + } + pFormat = Format::RGB565; + } +} + PD_API int Image::Format2Bpp(Format fmt) { switch (fmt) { case Format::RGBA: @@ -84,7 +132,33 @@ PD_API Image::Format Image::GuessFmtFromBpp(int bpp) { } PD_API void Image::Flip(bool hz, bool vt) { - // TODO + auto bpp = Format2Bpp(pFormat); + int rlen = pSize.x * bpp; // calculate as less as possible + if (hz) { + for (int j = 0; j < pSize.y; j++) { + int roff = j * rlen; + for (int i = 0; i < pSize.x / 2; i++) { + int src = roff + (i * bpp); + int dst = roff + (pSize.x - 1 - i) * bpp; + for (int k = 0; k < bpp; k++) { + PD::u8 tmp = pData[dst + k]; + pData[dst + k] = pData[src + k]; + pData[src + k] = tmp; + } + } + } + } + if (vt) { + for (int j = 0; j < pSize.y / 2; j++) { + int rsrc = j * rlen; + int rdst = (pSize.y - 1 - j) * rlen; + for (int i = 0; i < rlen; i++) { // swap the entire row + PD::u8 tmp = pData[rdst + i]; + pData[rdst + i] = pData[rsrc + i]; + pData[rsrc + i] = tmp; + } + } + } } } // namespace PD \ No newline at end of file