7#include "geometry/axis_aligned_rectangle.hpp"
8#include "geometry/extent.hpp"
15namespace hi::inline v1 {
33 [[nodiscard]] T
const *
data() const noexcept
40 [[nodiscard]] T *
data() noexcept
51 return _pixels[columnNr];
60 return _pixels[columnNr];
71 hi_assert(columnNr >= 0 && columnNr < _width);
72 return _pixels[columnNr];
83 hi_assert(columnNr >= 0 && columnNr < _width);
84 return _pixels[columnNr];
104 using value_type = T;
108 pixel_map() noexcept : _pixels(
nullptr), _width(0), _height(0), _stride(0), _self_allocated(true) {}
117 _pixels(pixels), _width(width), _height(height), _stride(stride), _self_allocated(
false)
119 hi_assert(_stride >= _width);
120 hi_assert(_width >= 0);
121 hi_assert(_height >= 0);
123 if (pixels ==
nullptr) {
124 _self_allocated =
true;
125 _pixels =
new T[_height * _stride];
153 if (_self_allocated) {
164 _pixels(other._pixels),
165 _width(other._width),
166 _height(other._height),
167 _stride(other._stride),
168 _self_allocated(other._self_allocated)
170 if (_self_allocated) {
171 _pixels =
new T[_height * _stride];
174 hilet src_row = other[y];
175 auto dst_row = (*this)[y];
177 dst_row[x] = src_row[x];
184 _pixels(other._pixels),
185 _width(other._width),
186 _height(other._height),
187 _stride(other._stride),
188 _self_allocated(other._self_allocated)
190 hi_axiom(
this != &other);
191 other._self_allocated =
false;
194 [[nodiscard]]
operator bool() const noexcept
196 return _pixels !=
nullptr;
218 hi_return_on_self_assignment(other);
220 _pixels = other._pixels;
221 _width = other._width;
222 _height = other._height;
223 _stride = other._stride;
224 _self_allocated = other._self_allocated;
226 if (_self_allocated) {
227 _pixels =
new T[_height * _stride];
230 hilet src_row = other[y];
231 auto dst_row = (*this)[y];
233 dst_row[x] = src_row[x];
243 if (_self_allocated) {
246 _pixels = other._pixels;
247 _width = other._width;
248 _height = other._height;
249 _stride = other._stride;
250 _self_allocated = other._self_allocated;
251 other._self_allocated =
false;
255 extent2 extent() const noexcept
257 return {narrow_cast<float>(_width), narrow_cast<float>(_height)};
269 hi_axiom((x >= 0) && (y >= 0));
270 hi_assert((x + width <= _width) && (y + height <= _height));
272 hilet offset = y * _stride + x;
274 return pixel_map{_pixels + offset, width, height, _stride};
281 narrow_cast<std::size_t>(
rectangle.left()),
282 narrow_cast<std::size_t>(
rectangle.bottom()),
287 pixel_row<T>
const operator[](
std::size_t rowNr)
const noexcept
289 return {_pixels + (rowNr * _stride), _width};
292 pixel_row<T> operator[](
std::size_t rowNr)
noexcept
294 return {_pixels + (rowNr * _stride), _width};
297 pixel_row<T>
const at(
std::size_t rowNr)
const noexcept
299 hi_assert(rowNr < _height);
300 return (*
this)[rowNr];
305 hi_assert(rowNr < _height);
306 return (*
this)[rowNr];
329 bool _self_allocated;
333void copy(pixel_map<T>
const &src, pixel_map<T> &dst)
noexcept
339 hilet src_row = src[y];
340 auto dst_row = dst[y];
342 dst_row[x] = src_row[x];
347template<
int KERNEL_SIZE,
typename KERNEL>
348void horizontalFilterRow(pixel_row<uint8_t> row, KERNEL kernel)
noexcept;
350template<
int KERNEL_SIZE,
typename T,
typename KERNEL>
351void horizontalFilter(pixel_map<T> &pixels, KERNEL kernel)
noexcept;
356void fill(pixel_map<T> &dst)
noexcept;
361void fill(pixel_map<T> &dst, T color)
noexcept;
366void rotate90(pixel_map<T> &dst, pixel_map<T>
const &src)
noexcept;
371void rotate270(pixel_map<T> &dst, pixel_map<T>
const &src)
noexcept;
375void mergeMaximum(pixel_map<uint8_t> &dst, pixel_map<uint8_t>
const &src)
noexcept;
383inline 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:102
pixel_map() noexcept
Construct an empty pixel-map.
Definition pixel_map.hpp:108
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:142
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:116
pixel_map(pixel_map const &other) noexcept
Copy constructor of other.
Definition pixel_map.hpp:163
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:149
pixel_map & operator=(pixel_map const &other)
Disallowing copying so that life-time of selfAllocated pixels is easy to understand.
Definition pixel_map.hpp:216
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:267
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:135
A row of pixels.
Definition pixel_map.hpp:20
T * data() noexcept
Get a pointer to the pixel data.
Definition pixel_map.hpp:40
T & operator[](std::size_t columnNr) noexcept
Get a access to a pixel in the row.
Definition pixel_map.hpp:58
T const & at(std::size_t columnNr) const noexcept
Get a access to a pixel in the row.
Definition pixel_map.hpp:69
T const & operator[](std::size_t columnNr) const noexcept
Get a access to a pixel in the row.
Definition pixel_map.hpp:49
T const * data() const noexcept
Get a pointer to the pixel data.
Definition pixel_map.hpp:33
T & at(std::size_t columnNr) noexcept
Get a access to a pixel in the row.
Definition pixel_map.hpp:81