7#include "geometry/axis_aligned_rectangle.hpp"
8#include "geometry/extent.hpp"
18hi_warning_ignore_msvc(26401);
21hi_warning_ignore_msvc(26409);
23namespace hi::inline v1 {
41 [[nodiscard]] T
const *
data() const noexcept
48 [[nodiscard]] T *
data() noexcept
59 return _pixels[columnNr];
68 return _pixels[columnNr];
79 hi_assert(columnNr >= 0 && columnNr < _width);
80 return _pixels[columnNr];
91 hi_assert(columnNr >= 0 && columnNr < _width);
92 return _pixels[columnNr];
112 using value_type = T;
116 pixel_map() noexcept : _pixels(
nullptr), _width(0), _height(0), _stride(0), _self_allocated(true) {}
125 _pixels(pixels), _width(width), _height(height), _stride(stride), _self_allocated(
false)
127 hi_assert(_stride >= _width);
128 hi_assert(_width >= 0);
129 hi_assert(_height >= 0);
131 if (pixels ==
nullptr) {
132 _self_allocated =
true;
133 _pixels =
new T[_height * _stride];
161 if (_self_allocated) {
172 _pixels(other._pixels),
173 _width(other._width),
174 _height(other._height),
175 _stride(other._stride),
176 _self_allocated(other._self_allocated)
178 if (_self_allocated) {
179 _pixels =
new T[_height * _stride];
182 hilet src_row = other[y];
183 auto dst_row = (*this)[y];
185 dst_row[x] = src_row[x];
192 _pixels(other._pixels),
193 _width(other._width),
194 _height(other._height),
195 _stride(other._stride),
196 _self_allocated(other._self_allocated)
198 hi_axiom(
this != &other);
199 other._self_allocated =
false;
202 [[nodiscard]]
operator bool() const noexcept
204 return _pixels !=
nullptr;
226 hi_return_on_self_assignment(other);
228 _pixels = other._pixels;
229 _width = other._width;
230 _height = other._height;
231 _stride = other._stride;
232 _self_allocated = other._self_allocated;
234 if (_self_allocated) {
235 _pixels =
new T[_height * _stride];
238 hilet src_row = other[y];
239 auto dst_row = (*this)[y];
241 dst_row[x] = src_row[x];
251 if (_self_allocated) {
254 _pixels = other._pixels;
255 _width = other._width;
256 _height = other._height;
257 _stride = other._stride;
258 _self_allocated = other._self_allocated;
259 other._self_allocated =
false;
263 extent2 extent() const noexcept
265 return {narrow_cast<float>(_width), narrow_cast<float>(_height)};
277 hi_axiom((x >= 0) && (y >= 0));
278 hi_assert((x + width <= _width) && (y + height <= _height));
280 hilet offset = y * _stride + x;
282 return pixel_map{_pixels + offset, width, height, _stride};
289 narrow_cast<std::size_t>(
rectangle.left()),
290 narrow_cast<std::size_t>(
rectangle.bottom()),
295 pixel_row<T>
const operator[](
std::size_t rowNr)
const noexcept
297 return {_pixels + (rowNr * _stride), _width};
300 pixel_row<T> operator[](
std::size_t rowNr)
noexcept
302 return {_pixels + (rowNr * _stride), _width};
305 pixel_row<T>
const at(
std::size_t rowNr)
const noexcept
307 hi_assert(rowNr < _height);
308 return (*
this)[rowNr];
313 hi_assert(rowNr < _height);
314 return (*
this)[rowNr];
337 bool _self_allocated;
341void copy(pixel_map<T>
const &src, pixel_map<T> &dst)
noexcept
347 hilet src_row = src[y];
348 auto dst_row = dst[y];
350 dst_row[x] = src_row[x];
355template<
int KERNEL_SIZE,
typename KERNEL>
356void horizontalFilterRow(pixel_row<uint8_t> row, KERNEL kernel)
noexcept;
358template<
int KERNEL_SIZE,
typename T,
typename KERNEL>
359void horizontalFilter(pixel_map<T> &pixels, KERNEL kernel)
noexcept;
364void fill(pixel_map<T> &dst)
noexcept;
369void fill(pixel_map<T> &dst, T color)
noexcept;
374void rotate90(pixel_map<T> &dst, pixel_map<T>
const &src)
noexcept;
379void rotate270(pixel_map<T> &dst, pixel_map<T>
const &src)
noexcept;
383void mergeMaximum(pixel_map<uint8_t> &dst, pixel_map<uint8_t>
const &src)
noexcept;
391inline void makeTransparentBorder(pixel_map<T> &pixel_map)
noexcept;
This file includes required definitions.
#define hilet
Invariant should be the default for variables.
Definition required.hpp:23
Class which represents an axis-aligned rectangle.
Definition axis_aligned_rectangle.hpp:20
A rectangle / parallelogram in 3D space.
Definition rectangle.hpp:20
constexpr float height() const noexcept
The height, or length of the up vector.
Definition rectangle.hpp:147
constexpr float width() const noexcept
The width, or length of the right vector.
Definition rectangle.hpp:140
A 2D canvas of pixels.
Definition pixel_map.hpp:110
pixel_map() noexcept
Construct an empty pixel-map.
Definition pixel_map.hpp:116
pixel_map(std::size_t width, std::size_t height, std::size_t stride) noexcept
Construct an pixel-map from memory received from an API.
Definition pixel_map.hpp:150
pixel_map(T *pixels, std::size_t width, std::size_t height, std::size_t stride) noexcept
Construct an pixel-map from memory received from an API.
Definition pixel_map.hpp:124
pixel_map(pixel_map const &other) noexcept
Copy constructor of other.
Definition pixel_map.hpp:171
pixel_map(std::size_t width, std::size_t height) noexcept
Construct an pixel-map from memory received from an API.
Definition pixel_map.hpp:157
pixel_map & operator=(pixel_map const &other)
Disallowing copying so that life-time of selfAllocated pixels is easy to understand.
Definition pixel_map.hpp:224
pixel_map submap(std::size_t x, std::size_t y, std::size_t width, std::size_t height) const noexcept
Get a (smaller) view of the map.
Definition pixel_map.hpp:275
pixel_map(T *pixels, std::size_t width, std::size_t height) noexcept
Construct an pixel-map from memory received from an API.
Definition pixel_map.hpp:143
A row of pixels.
Definition pixel_map.hpp:28
T * data() noexcept
Get a pointer to the pixel data.
Definition pixel_map.hpp:48
T & operator[](std::size_t columnNr) noexcept
Get a access to a pixel in the row.
Definition pixel_map.hpp:66
T const & at(std::size_t columnNr) const noexcept
Get a access to a pixel in the row.
Definition pixel_map.hpp:77
T const & operator[](std::size_t columnNr) const noexcept
Get a access to a pixel in the row.
Definition pixel_map.hpp:57
T const * data() const noexcept
Get a pointer to the pixel data.
Definition pixel_map.hpp:41
T & at(std::size_t columnNr) noexcept
Get a access to a pixel in the row.
Definition pixel_map.hpp:89