Lagrange
ImageStorage.h
1/*
2 * Copyright 2023 Adobe. All rights reserved.
3 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License. You may obtain a copy
5 * of the License at http://www.apache.org/licenses/LICENSE-2.0
6 *
7 * Unless required by applicable law or agreed to in writing, software distributed under
8 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9 * OF ANY KIND, either express or implied. See the License for the specific language
10 * governing permissions and limitations under the License.
11 */
12#pragma once
13
14#include <lagrange/image/ImageType.h>
15#include <tbb/cache_aligned_allocator.h>
16#include <Eigen/Eigen>
17#include <memory>
18#include <vector>
19
20namespace lagrange {
21namespace image {
22
24{
25public:
26 template <typename T>
27 using aligned_allocator = tbb::cache_aligned_allocator<T>;
28 template <typename T>
29 using aligned_vector = std::vector<T, aligned_allocator<T>>;
30
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);
33
34 inline ImageStorage(const ImageStorage& other) { *this = other; };
35 ImageStorage(ImageStorage&& other) noexcept { *this = other; };
36
37 virtual ~ImageStorage(){};
38
39public:
40 inline ImageStorage& operator=(const ImageStorage& other);
41 inline ImageStorage& operator=(ImageStorage&& other) noexcept;
42 inline void clear_buffer(unsigned char ch);
43
44protected:
45 inline void reset();
46 inline bool resize(size_t width, size_t height, size_t alignment);
47
48public:
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; }
53
54protected:
55 aligned_vector<unsigned char> m_buffer;
56 unsigned char* m_buffer_weak_ptr = nullptr; // for m_buffer does not owned by the storage
57 unsigned char* m_data_ptr = nullptr; // data pointer to either m_buffer or m_buffer_weak_ptr;
58
59 Eigen::Matrix<size_t, 2, 1> m_full_size;
60 size_t m_full_stride;
61};
62
63
64inline ImageStorage::ImageStorage(size_t width, size_t height, size_t alignment)
65{
66 if (!resize(width, height, alignment)) {
67 throw std::runtime_error("ImageStorage::ImageStorage, cannot resize!");
68 }
69}
70inline ImageStorage::ImageStorage(size_t width, size_t height, size_t stride, unsigned char* data)
71{
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!");
75 }
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;
80}
81
82inline ImageStorage& ImageStorage::operator=(const ImageStorage& other)
83{
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;
89 return *this;
90}
91inline ImageStorage& ImageStorage::operator=(ImageStorage&& other) noexcept
92{
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;
98 return *this;
99}
100
101inline void ImageStorage::clear_buffer(unsigned char ch)
102{
103 if (m_data_ptr) {
104 std::fill(m_data_ptr, m_data_ptr + m_full_size(0) * m_full_size(1), ch);
105 }
106}
107
108inline void ImageStorage::reset()
109{
110 m_buffer.clear();
111 m_full_size.setZero();
112 m_full_stride = 0;
113 m_buffer_weak_ptr = nullptr;
114 m_data_ptr = nullptr;
115}
116inline bool ImageStorage::resize(size_t width, size_t height, size_t alignment)
117{
118 if (0 == width || 0 == height || 0 == alignment || 0 != ((alignment - 1) & alignment)) {
119 reset();
120 return false;
121 } else {
122 m_full_size = {width, height};
123 m_full_stride = ((width - 1) / alignment + 1) * alignment;
124 m_buffer.clear();
125 m_buffer.resize(m_full_stride * height);
126 m_buffer_weak_ptr = nullptr;
127 m_data_ptr = m_buffer.data();
128 return true;
129 }
130}
131} // namespace image
132} // namespace lagrange
Definition: ImageStorage.h:24
Main namespace for Lagrange.
Definition: AABBIGL.h:30