From 2cf82d4da95802d6a74fe3e8a1b6da73fbf2332d Mon Sep 17 00:00:00 2001 From: Hartmut Seichter Date: Mon, 2 Jun 2025 21:36:18 +0200 Subject: [PATCH] new image class --- src/core/include/pw/core/matrix.hpp | 4 +- src/core/tests/pwcore_test_image.cpp | 69 ++++++++++++++++++++++++---- 2 files changed, 61 insertions(+), 12 deletions(-) diff --git a/src/core/include/pw/core/matrix.hpp b/src/core/include/pw/core/matrix.hpp index 40e3b0e..1111708 100644 --- a/src/core/include/pw/core/matrix.hpp +++ b/src/core/include/pw/core/matrix.hpp @@ -70,8 +70,8 @@ struct matrix final { template auto get(this auto&& self) -> decltype(auto) { static_assert(idx < Rows, "Out of bounds access to a member."); // TODO: use forward_like when clang is catching up - // return std::forward_like(self.m_[idx]); - return std::forward(self).m_[idx]; + return std::forward_like(self.m_[idx]); + // return std::forward(self).m_[idx]; } constexpr auto diagonal() const noexcept -> diag_type { diff --git a/src/core/tests/pwcore_test_image.cpp b/src/core/tests/pwcore_test_image.cpp index 79cf378..d905b2f 100644 --- a/src/core/tests/pwcore_test_image.cpp +++ b/src/core/tests/pwcore_test_image.cpp @@ -6,19 +6,20 @@ #include #include +#include namespace pw { struct image_layout { - enum pixel_layout { RGB8, RGBA8, LUM8, DEPTH }; + enum struct pixel_layout { RGB8, RGBA8, LUM8, DEPTH }; static constexpr auto bits_per_channel(pixel_layout layout) { - constexpr static auto lut = - std::array{std::make_tuple(RGB8, sizeof(std::uint8_t) << 3), - std::make_tuple(RGBA8, sizeof(std::uint8_t) << 3), - std::make_tuple(LUM8, sizeof(std::uint8_t) << 3), - std::make_tuple(DEPTH, sizeof(float) << 3)}; + constexpr static auto lut = std::array{ + std::make_tuple(pixel_layout::RGB8, sizeof(std::uint8_t) << 3), + std::make_tuple(pixel_layout::RGBA8, sizeof(std::uint8_t) << 3), + std::make_tuple(pixel_layout::LUM8, sizeof(std::uint8_t) << 3), + std::make_tuple(pixel_layout::DEPTH, sizeof(float) << 3)}; const auto iter = std::find_if( std::begin(lut), std::end(lut), @@ -26,16 +27,53 @@ struct image_layout { return iter != std::end(lut) ? std::get<1>(*iter) : 0u; } + + static constexpr auto channels(pixel_layout layout) { + constexpr static auto lut = + std::array{std::make_tuple(pixel_layout::RGB8, 3), + std::make_tuple(pixel_layout::RGBA8, 4), + std::make_tuple(pixel_layout::LUM8, 1), + std::make_tuple(pixel_layout::DEPTH, 1)}; + + const auto iter = std::find_if( + std::begin(lut), std::end(lut), + [&layout](const auto& v) { return std::get<0>(v) == layout; }); + + return iter != std::end(lut) ? std::get<1>(*iter) : 0u; + } + + size size_{}; + pixel_layout layout_{pixel_layout::RGB8}; +}; + +struct image_new { + + using buffer_type = std::vector; + using size_type = pw::size; + + image_layout layout; + buffer_type buffer{}; + + static constexpr auto make(const image_new::size_type& size_, + image_layout::pixel_layout layout_) + -> image_new { + + return {.layout{.layout_ = layout_}, + .buffer{buffer_type{ + static_cast( + (image_layout::bits_per_channel(layout_) >> 3) * + size_.area() * image_layout::channels(layout_)), + buffer_type::value_type{0}}}}; + } }; } // namespace pw auto main() -> int { - auto val = pw::image_layout::bits_per_channel( - pw::image_layout::pixel_layout::RGB8); - - std::print("RGB8 = {}bits per channel\n", val); + std::print("RGB8 = {}bits per channel\n", + pw::image_layout::bits_per_channel( + pw::image_layout::pixel_layout::RGB8)); std::print("DEPTH = {}bits per channel\n", pw::image_layout::bits_per_channel( pw::image_layout::pixel_layout::DEPTH)); @@ -43,5 +81,16 @@ auto main() -> int { pw::image_layout::bits_per_channel( pw::image_layout::pixel_layout::LUM8)); + std::print( + "RGB8 = {}channels\n", + pw::image_layout::channels(pw::image_layout::pixel_layout::RGB8)); + std::print( + "DEPTH = {}channels\n", + pw::image_layout::channels(pw::image_layout::pixel_layout::DEPTH)); + std::print( + "LUM8 = {}channels\n", + pw::image_layout::channels(pw::image_layout::pixel_layout::LUM8)); // pw::image + + pw::image_new::make(pw::iamge, image_layout::pixel_layout layout_) }