first sweeping through size and image

This commit is contained in:
Hartmut Seichter 2024-07-16 18:51:14 +02:00
parent c58f81d640
commit 86f45a084a
6 changed files with 54 additions and 63 deletions

View file

@ -7,11 +7,13 @@ cmake_minimum_required(VERSION 3.28)
project(pixwerx)
#
# pixwerx ist C++23
# pixwerx is C++23
#
set (CMAKE_CXX_STANDARD 23)
#
# internal cmake modules
#
set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/etc/cmake ${CMAKE_MODULE_PATH})
# boilerplate locations for build

View file

@ -23,12 +23,9 @@
#ifndef PW_CORE_AABB_HPP
#define PW_CORE_AABB_HPP
#include "vector.hpp"
#include <cstddef>
#include <numeric>
#include <pw/core/vector.hpp>
#include <algorithm>
#include <numeric>
#include <vector>
namespace pw {

View file

@ -25,6 +25,7 @@
#include <cstddef>
#include <cstdint>
#include <cstdlib>
#include <memory>
#include <string>

View file

@ -8,8 +8,8 @@
* 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 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,
@ -29,58 +29,55 @@
namespace pw {
class image {
public:
public:
using size_type = pw::size<std::size_t>;
image() = default;
using data_t = std::byte;
using data_t = std::byte;
enum pixel_layout {
RGB8,
RGBA8,
LUM,
DEPTH,
HDR
};
enum pixel_layout { RGB8, RGBA8, LUM, DEPTH, HDR };
image(const size_type& s, pixel_layout t,
const data_t* ptr = nullptr);
image(const size& s, pixel_layout t, const data_t *ptr = nullptr);
bool create(const size_type& s, pixel_layout t,
const data_t* ptr = nullptr);
bool create(const size& s, pixel_layout t, const data_t *ptr = nullptr);
void release(bool release_memory = false);
void release(bool release_memory = false);
const data_t* data() const { return _data.data(); }
data_t* data() { return _data.data(); }
const data_t *data() const { return _data.data(); }
data_t *data() { return _data.data(); }
const float *data_float() const { return reinterpret_cast<const float*>(_data.data()); }
float *data_float() { return reinterpret_cast<float*>(_data.data());}
const float* data_float() const {
return reinterpret_cast<const float*>(_data.data());
}
float* data_float() { return reinterpret_cast<float*>(_data.data()); }
pixel_layout layout() const;
void set_layout(const pixel_layout &layout);
void set_layout(const pixel_layout& layout);
uint64_t change_count() const;
void set_change_count(const uint64_t &change_count);
void set_change_count(const uint64_t& change_count);
static uint32_t bytes_per_pixel(pixel_layout t);
static uint32_t components(pixel_layout t);
::pw::size size() const;
size_type size() const { return _size; }
void generate_noise();
bool is_valid() const;
protected:
protected:
::pw::size _size { 0, 0 };
pixel_layout _layout { pixel_layout::RGB8 };
uint64_t _change_count { 0 };
std::vector<data_t> _data;
size_type _size{.width = 0, .height = 0};
pixel_layout _layout{pixel_layout::RGB8};
uint64_t _change_count{0};
std::vector<data_t> _data;
};
}
} // namespace pw
#endif

View file

@ -8,8 +8,8 @@
* 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 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,
@ -23,33 +23,30 @@
#ifndef PW_CORE_SIZE_HPP
#define PW_CORE_SIZE_HPP
#include <asm-generic/errno.h>
#include <pw/core/globals.hpp>
#include <type_traits>
namespace pw {
template <typename T_>
struct size_ {
template <typename Scalar> struct size {
using value_type = T_;
using value_type = Scalar;
Scalar width, height;
T_ width,height;
size_() = default;
size_(T_ w,T_ h) : width(w), height(h) {}
T_ area() const { return std::abs(width * height); }
template <typename To_>
size_<To_> cast() const { return size_<To_>(static_cast<To_>(width),static_cast<To_>(height)); }
constexpr auto area() const noexcept -> Scalar {
if constexpr (std::is_unsigned_v<Scalar>) {
return width * height;
} else {
return std::abs(width * height);
}
}
template <typename Other> constexpr auto operator()() const noexcept {
return Other{width, height};
}
};
typedef size_<int> size;
typedef size_<float> sizef;
typedef size_<double> sized;
}
} // namespace pw
#endif

View file

@ -1,13 +1,14 @@
#include "pw/core/image.hpp"
#include "pw/core/debug.hpp"
#include <cstddef>
#include <limits>
#include <random>
#include <algorithm>
namespace pw {
image::image(const ::pw::size &s,
image::image(const ::pw::size<std::size_t> &s,
image::pixel_layout t,
const data_t *ptr)
{
@ -16,11 +17,11 @@ image::image(const ::pw::size &s,
}
}
bool image::create(const ::pw::size &s,
bool image::create(const ::pw::size<std::size_t> &s,
image::pixel_layout t,
const data_t *ptr)
{
size_t n = bytes_per_pixel(t) * s.area();
const auto n = bytes_per_pixel(t) * s.area();
_size = s;
_layout = t;
@ -52,7 +53,7 @@ void image::generate_noise()
void image::release(bool release_memory)
{
_data.clear();
_size = pw::size();
_size = size_type{};
if (release_memory) _data.shrink_to_fit();
}
@ -95,10 +96,6 @@ uint32_t image::components(image::pixel_layout t)
return 0;
}
::pw::size image::size() const
{
return _size;
}
image::pixel_layout image::layout() const
{