14#include <lagrange/image/ImageType.h>
15#include <tbb/cache_aligned_allocator.h>
27 using aligned_allocator = tbb::cache_aligned_allocator<T>;
29 using aligned_vector = std::vector<T, aligned_allocator<T>>;
31 inline ImageStorage(
size_t width,
size_t height,
size_t alignment);
32 inline ImageStorage(
size_t width,
size_t height,
size_t stride,
unsigned char* data);
42 inline void clear_buffer(
unsigned char ch);
46 inline bool resize(
size_t width,
size_t height,
size_t alignment);
49 auto get_full_size()
const {
return m_full_size; }
50 auto get_full_stride()
const {
return m_full_stride; }
51 const auto* data()
const {
return m_data_ptr; }
52 auto* data() {
return m_data_ptr; }
55 aligned_vector<unsigned char> m_buffer;
56 unsigned char* m_buffer_weak_ptr =
nullptr;
57 unsigned char* m_data_ptr =
nullptr;
59 Eigen::Matrix<size_t, 2, 1> m_full_size;
64inline ImageStorage::ImageStorage(
size_t width,
size_t height,
size_t alignment)
66 if (!resize(width, height, alignment)) {
67 throw std::runtime_error(
"ImageStorage::ImageStorage, cannot resize!");
70inline ImageStorage::ImageStorage(
size_t width,
size_t height,
size_t stride,
unsigned char* data)
72 if (0 == width || 0 == height || 0 == stride || width > stride ||
nullptr == data) {
73 throw std::runtime_error(
74 "ImageStorage::ImageStorage, width or height or stride or data is invalid!");
76 m_full_size = {width, height};
77 m_full_stride = stride;
78 m_buffer_weak_ptr = data;
79 m_data_ptr = m_buffer_weak_ptr;
82inline ImageStorage& ImageStorage::operator=(
const ImageStorage& other)
84 m_full_size = other.m_full_size;
85 m_full_stride = other.m_full_stride;
86 m_buffer = other.m_buffer;
87 m_buffer_weak_ptr = other.m_buffer_weak_ptr;
88 m_data_ptr = other.m_data_ptr;
91inline ImageStorage& ImageStorage::operator=(ImageStorage&& other)
noexcept
93 m_full_size = other.m_full_size;
94 m_full_stride = other.m_full_stride;
95 m_buffer = std::move(other.m_buffer);
96 m_buffer_weak_ptr = other.m_buffer_weak_ptr;
97 m_data_ptr = other.m_data_ptr;
101inline void ImageStorage::clear_buffer(
unsigned char ch)
104 std::fill(m_data_ptr, m_data_ptr + m_full_size(0) * m_full_size(1), ch);
108inline void ImageStorage::reset()
111 m_full_size.setZero();
113 m_buffer_weak_ptr =
nullptr;
114 m_data_ptr =
nullptr;
116inline bool ImageStorage::resize(
size_t width,
size_t height,
size_t alignment)
118 if (0 == width || 0 == height || 0 == alignment || 0 != ((alignment - 1) & alignment)) {
122 m_full_size = {width, height};
123 m_full_stride = ((width - 1) / alignment + 1) * alignment;
125 m_buffer.resize(m_full_stride * height);
126 m_buffer_weak_ptr =
nullptr;
127 m_data_ptr = m_buffer.data();
Definition: ImageStorage.h:24
Main namespace for Lagrange.
Definition: AABBIGL.h:30