8#include "geometry/axis_aligned_rectangle.hpp"
9#include "geometry/extent.hpp"
22 pixel_row(T *pixels,
ssize_t width) noexcept : _pixels(pixels), _width(width) {}
24 [[nodiscard]]
ssize_t width()
const noexcept
31 [[nodiscard]] T
const *
data() const noexcept
38 [[nodiscard]] T *
data() noexcept
49 return _pixels[columnNr];
58 return _pixels[columnNr];
67 [[nodiscard]] T
const &
at(
ssize_t columnNr)
const noexcept
69 tt_assert(columnNr >= 0 && columnNr < _width);
70 return _pixels[columnNr];
81 tt_assert(columnNr >= 0 && columnNr < _width);
82 return _pixels[columnNr];
104 pixel_map() noexcept : _pixels(
nullptr), _width(0), _height(0), _stride(0), _self_allocated(true) {}
113 _pixels(pixels), _width(width), _height(height), _stride(stride), _self_allocated(
false)
115 tt_assert(_stride >= _width);
116 tt_assert(_width >= 0);
117 tt_assert(_height >= 0);
119 if (pixels ==
nullptr) {
120 _self_allocated =
true;
121 _pixels =
new T[_height * _stride];
149 if (_self_allocated) {
160 if (_self_allocated) {
163 for (
ssize_t y = 0; y != _height; ++y) {
164 ttlet src_row = (*this)[y];
166 for (
ssize_t x = 0; x != _width; ++x) {
167 dst_row[x] = src_row[x];
173 return submap(0, 0, _width, _height);
178 _pixels(other._pixels),
179 _width(other._width),
180 _height(other._height),
181 _stride(other._stride),
182 _self_allocated(other._self_allocated)
184 tt_axiom(
this != &other);
185 other._self_allocated =
false;
188 [[nodiscard]]
operator bool() const noexcept
190 return _pixels !=
nullptr;
193 [[nodiscard]] ssize_t width() const noexcept
198 [[nodiscard]] ssize_t height() const noexcept
203 [[nodiscard]] ssize_t stride() const noexcept
215 if (_self_allocated) {
218 _pixels = other._pixels;
219 _width = other._width;
220 _height = other._height;
221 _stride = other._stride;
222 _self_allocated = other._self_allocated;
223 other._self_allocated =
false;
227 extent2 extent() const noexcept
229 return {narrow_cast<float>(_width), narrow_cast<float>(_height)};
241 tt_axiom((x >= 0) && (y >= 0));
242 tt_assert((x + width <= _width) && (y + height <= _height));
244 ttlet offset = y * _stride + x;
246 return {_pixels + offset, width, height, _stride};
254 narrow_cast<ssize_t>(
rectangle.bottom()),
256 narrow_cast<ssize_t>(
rectangle.height()));
259 pixel_row<T>
const operator[](ssize_t rowNr)
const noexcept
261 return {_pixels + (rowNr * _stride), _width};
264 pixel_row<T> operator[](ssize_t rowNr)
noexcept
266 return {_pixels + (rowNr * _stride), _width};
269 pixel_row<T>
const at(ssize_t rowNr)
const noexcept
271 tt_assert(rowNr < _height);
272 return (*
this)[rowNr];
275 pixel_row<T> at(ssize_t rowNr)
noexcept
277 tt_assert(rowNr < _height);
278 return (*
this)[rowNr];
301 bool _self_allocated;
305void copy(pixel_map<T>
const &src, pixel_map<T> &dst)
noexcept
307 ssize_t width =
std::min(src.width(), dst.width());
308 ssize_t height =
std::min(src.height(), dst.height());
310 for (ssize_t y = 0; y != height; ++y) {
311 ttlet src_row = src[y];
312 auto dst_row = dst[y];
313 for (ssize_t x = 0; x != width; ++x) {
314 dst_row[x] = src_row[x];
319template<
int KERNEL_SIZE,
typename KERNEL>
320void horizontalFilterRow(pixel_row<uint8_t> row, KERNEL kernel)
noexcept;
322template<
int KERNEL_SIZE,
typename T,
typename KERNEL>
323void horizontalFilter(pixel_map<T> &pixels, KERNEL kernel)
noexcept;
328void fill(pixel_map<T> &dst)
noexcept;
333void fill(pixel_map<T> &dst, T color)
noexcept;
338void rotate90(pixel_map<T> &dst, pixel_map<T>
const &src)
noexcept;
343void rotate270(pixel_map<T> &dst, pixel_map<T>
const &src)
noexcept;
347void mergeMaximum(pixel_map<uint8_t> &dst, pixel_map<uint8_t>
const &src)
noexcept;
355inline void makeTransparentBorder(pixel_map<T> &pixel_map)
noexcept;
Class which represents an axis-aligned rectangle.
Definition axis_aligned_rectangle.hpp:18
Class which represents an rectangle.
Definition rectangle.hpp:16
A 2D canvas of pixels.
Definition pixel_map.hpp:100
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:131
pixel_map(ssize_t width, ssize_t height) noexcept
Construct an pixel-map from memory received from an API.
Definition pixel_map.hpp:145
pixel_map() noexcept
Construct an empty pixel-map.
Definition pixel_map.hpp:104
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:138
pixel_map< T > 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:239
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:112
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:20
T const * data() const noexcept
Get a pointer to the pixel data.
Definition pixel_map.hpp:31
T const & operator[](ssize_t columnNr) const noexcept
Get a access to a pixel in the row.
Definition pixel_map.hpp:47
T * data() noexcept
Get a pointer to the pixel data.
Definition pixel_map.hpp:38
T const & at(ssize_t columnNr) const noexcept
Get a access to a pixel in the row.
Definition pixel_map.hpp:67
T & at(ssize_t columnNr) noexcept
Get a access to a pixel in the row.
Definition pixel_map.hpp:79
T & operator[](ssize_t columnNr) noexcept
Get a access to a pixel in the row.
Definition pixel_map.hpp:56