8#include "geometry/axis_aligned_rectangle.hpp"
9#include "geometry/extent.hpp"
23 pixel_row(T *pixels,
ssize_t width) noexcept : _pixels(pixels), _width(width) {}
25 [[nodiscard]]
ssize_t width()
const noexcept
32 [[nodiscard]] T
const *
data() const noexcept
39 [[nodiscard]] T *
data() noexcept
50 return _pixels[columnNr];
59 return _pixels[columnNr];
68 [[nodiscard]] T
const &
at(
ssize_t columnNr)
const noexcept
70 tt_assert(columnNr >= 0 && columnNr < _width);
71 return _pixels[columnNr];
82 tt_assert(columnNr >= 0 && columnNr < _width);
83 return _pixels[columnNr];
105 pixel_map() noexcept : _pixels(
nullptr), _width(0), _height(0), _stride(0), _hash(0), _self_allocated(true) {}
114 _pixels(pixels), _width(width), _height(height), _stride(stride), _hash(0), _self_allocated(
false)
116 tt_assert(_stride >= _width);
117 tt_assert(_width >= 0);
118 tt_assert(_height >= 0);
120 if (pixels ==
nullptr) {
121 _self_allocated =
true;
122 _pixels =
new T[_height * _stride];
150 if (_self_allocated) {
159 [[nodiscard]]
pixel_map copy() const noexcept
161 if (_self_allocated) {
164 for (
ssize_t y = 0; y != _height; ++y) {
165 ttlet src_row = (*this)[y];
167 for (
ssize_t x = 0; x != _width; ++x) {
168 dst_row[x] = src_row[x];
175 return submap(0, 0, _width, _height);
180 _pixels(other._pixels),
181 _width(other._width),
182 _height(other._height),
183 _stride(other._stride),
185 _self_allocated(other._self_allocated)
187 tt_axiom(
this != &other);
188 other._self_allocated =
false;
191 [[nodiscard]]
operator bool() const noexcept
193 return _pixels !=
nullptr;
196 [[nodiscard]] ssize_t width() const noexcept
201 [[nodiscard]] ssize_t height() const noexcept
206 [[nodiscard]] ssize_t stride() const noexcept
211 [[nodiscard]]
size_t hash() const noexcept
223 if (_self_allocated) {
226 _pixels = other._pixels;
227 _width = other._width;
228 _height = other._height;
229 _stride = other._stride;
231 _self_allocated = other._self_allocated;
232 other._self_allocated =
false;
236 extent2 extent() const noexcept
238 return {narrow_cast<float>(_width), narrow_cast<float>(_height)};
250 tt_axiom((x >= 0) && (y >= 0));
251 tt_assert((x + width <= _width) && (y + height <= _height));
253 ttlet offset = y * _stride + x;
255 auto r =
pixel_map{_pixels + offset, width, height, _stride};
256 r._hash = (width == _width && height == _height) ? _hash : 0;
265 narrow_cast<ssize_t>(
rectangle.bottom()),
267 narrow_cast<ssize_t>(
rectangle.height()));
270 pixel_row<T>
const operator[](ssize_t rowNr)
const noexcept
272 return {_pixels + (rowNr * _stride), _width};
275 pixel_row<T> operator[](ssize_t rowNr)
noexcept
277 return {_pixels + (rowNr * _stride), _width};
280 pixel_row<T>
const at(ssize_t rowNr)
const noexcept
282 tt_assert(rowNr < _height);
283 return (*
this)[rowNr];
286 pixel_row<T> at(ssize_t rowNr)
noexcept
288 tt_assert(rowNr < _height);
289 return (*
this)[rowNr];
297 size_t h = hash_mix(_width, _height);
298 for (
ssize_t row_nr = 0; row_nr != _height; ++row_nr) {
299 ttlet &row = (*this)[row_nr];
300 for (
ssize_t col_nr = 0; col_nr != _width; ++col_nr) {
301 h = hash_mix(h, row[col_nr]);
331 bool _self_allocated;
335void copy(pixel_map<T>
const &src, pixel_map<T> &dst)
noexcept
337 ssize_t width =
std::min(src.width(), dst.width());
338 ssize_t height =
std::min(src.height(), dst.height());
340 for (ssize_t y = 0; y != height; ++y) {
341 ttlet src_row = src[y];
342 auto dst_row = dst[y];
343 for (ssize_t x = 0; x != width; ++x) {
344 dst_row[x] = src_row[x];
349template<
int KERNEL_SIZE,
typename KERNEL>
350void horizontalFilterRow(pixel_row<uint8_t> row, KERNEL kernel)
noexcept;
352template<
int KERNEL_SIZE,
typename T,
typename KERNEL>
353void horizontalFilter(pixel_map<T> &pixels, KERNEL kernel)
noexcept;
358void fill(pixel_map<T> &dst)
noexcept;
363void fill(pixel_map<T> &dst, T color)
noexcept;
368void rotate90(pixel_map<T> &dst, pixel_map<T>
const &src)
noexcept;
373void rotate270(pixel_map<T> &dst, pixel_map<T>
const &src)
noexcept;
377void mergeMaximum(pixel_map<uint8_t> &dst, pixel_map<uint8_t>
const &src)
noexcept;
385inline void makeTransparentBorder(pixel_map<T> &pixel_map)
noexcept;
Class which represents an axis-aligned rectangle.
Definition axis_aligned_rectangle.hpp:20
Class which represents an rectangle.
Definition rectangle.hpp:16
A 2D canvas of pixels.
Definition pixel_map.hpp:101
pixel_map submap(ssize_t x, ssize_t y, ssize_t width, ssize_t height) const noexcept
Get a (smaller) view of the map.
Definition pixel_map.hpp:248
pixel_map(T *pixels, ssize_t width, ssize_t height) noexcept
Construct an pixel-map from memory received from an API.
Definition pixel_map.hpp:132
pixel_map(ssize_t width, ssize_t height) noexcept
Construct an pixel-map from memory received from an API.
Definition pixel_map.hpp:146
void update_hash() noexcept
Update the hash value of the pixmap.
Definition pixel_map.hpp:295
pixel_map() noexcept
Construct an empty pixel-map.
Definition pixel_map.hpp:105
pixel_map(ssize_t width, ssize_t height, ssize_t stride) noexcept
Construct an pixel-map from memory received from an API.
Definition pixel_map.hpp:139
pixel_map & operator=(pixel_map const &other)=delete
Disallowing copying so that life-time of selfAllocated pixels is easy to understand.
pixel_map(T *pixels, ssize_t width, ssize_t height, ssize_t stride) noexcept
Construct an pixel-map from memory received from an API.
Definition pixel_map.hpp:113
pixel_map(pixel_map const &other)=delete
Disallowing copying so that life-time of selfAllocated pixels is easy to understand.
A row of pixels.
Definition pixel_map.hpp:21
T const * data() const noexcept
Get a pointer to the pixel data.
Definition pixel_map.hpp:32
T const & operator[](ssize_t columnNr) const noexcept
Get a access to a pixel in the row.
Definition pixel_map.hpp:48
T * data() noexcept
Get a pointer to the pixel data.
Definition pixel_map.hpp:39
T const & at(ssize_t columnNr) const noexcept
Get a access to a pixel in the row.
Definition pixel_map.hpp:68
T & at(ssize_t columnNr) noexcept
Get a access to a pixel in the row.
Definition pixel_map.hpp:80
T & operator[](ssize_t columnNr) noexcept
Get a access to a pixel in the row.
Definition pixel_map.hpp:57