From 46eff4429781ecbb25258c419ba2153d6f357692 Mon Sep 17 00:00:00 2001 From: Hartmut Seichter Date: Wed, 16 Jan 2019 11:13:12 +0100 Subject: [PATCH] adding important low-level code for image and matrix manipulations --- src/core/include/pw/core/image.hpp | 32 +++++++++---- src/core/include/pw/core/matrix.hpp | 2 - src/core/include/pw/core/size.hpp | 2 + src/core/src/image.cpp | 71 +++++++++++++++++++++++++++++ 4 files changed, 96 insertions(+), 11 deletions(-) diff --git a/src/core/include/pw/core/image.hpp b/src/core/include/pw/core/image.hpp index 73ff30d..4c50ea6 100644 --- a/src/core/include/pw/core/image.hpp +++ b/src/core/include/pw/core/image.hpp @@ -33,22 +33,36 @@ public: image() = default; - enum image_type { - RGB8, - RGBA8, - LUM - }; + enum pixel_layout { + RGB8, + RGBA8, + LUM + }; - bool create(const sizei& size,); + image(const sizei& s, pixel_layout t, void *ptr = nullptr); + + bool create(const sizei& s, pixel_layout t, void *ptr = nullptr); + + void destroy(bool release_memory = false); const uint8_t *data() const { return _data.data(); } uint8_t *data() { return _data.data(); } + pixel_layout layout() const; + void set_layout(const pixel_layout &layout); + + uint64_t change_count() const; + void set_change_count(const uint64_t &change_count); + + static uint32_t bytes_per_pixel(pixel_layout t); + protected: - sizei _size; - std::vector _data; - std::string _uri; + sizei _size; + pixel_layout _layout; + uint64_t _change_count; + + std::vector _data; }; diff --git a/src/core/include/pw/core/matrix.hpp b/src/core/include/pw/core/matrix.hpp index a2de3b7..2f9a13f 100644 --- a/src/core/include/pw/core/matrix.hpp +++ b/src/core/include/pw/core/matrix.hpp @@ -487,8 +487,6 @@ public: }; -////////////////////////////////////////////////////////////////////////// - // // Specializations // diff --git a/src/core/include/pw/core/size.hpp b/src/core/include/pw/core/size.hpp index 6cf4e9e..69a9324 100644 --- a/src/core/include/pw/core/size.hpp +++ b/src/core/include/pw/core/size.hpp @@ -36,6 +36,8 @@ struct size_ { size_(T_ w,T_ h) : width(w), height(h) {} + T_ area() const { return std::abs(width * height); } + }; typedef size_ size; diff --git a/src/core/src/image.cpp b/src/core/src/image.cpp index e69de29..11c9e0e 100644 --- a/src/core/src/image.cpp +++ b/src/core/src/image.cpp @@ -0,0 +1,71 @@ +#include "pw/core/image.hpp" + +#include + +namespace pw { + +image::image(const sizei &s, image::pixel_layout t, void *ptr) +{ + +} + +bool image::create(const sizei &s, image::pixel_layout t, void *ptr) +{ + auto n = bytes_per_pixel(t) * s.area(); + + if (ptr != nullptr) { + + auto a = reinterpret_cast(ptr); + + _data.assign(a,a + n); + + } else { + + _data.resize(n,0); + } + + return !_data.empty(); +} + +void image::destroy(bool release_memory) +{ + _data.clear(); + + if (release_memory) _data.shrink_to_fit(); +} + +uint32_t image::bytes_per_pixel(image::pixel_layout t) +{ + switch (t) { + case pw::image::RGB8: + return 3; + case pw::image::RGBA8: + return 4; + case pw::image::LUM: + return 1; + } + + return std::numeric_limits::max(); +} + +image::pixel_layout image::layout() const +{ + return _layout; +} + +void image::set_layout(const pixel_layout &layout) +{ + _layout = layout; +} + +uint64_t image::change_count() const +{ + return _change_count; +} + +void image::set_change_count(const uint64_t &change_count) +{ + _change_count = change_count; +} + +}