9 #include "internal/Pixel.h"
10 #include "internal/PixelConverter.h"
13 template <
typename PixelT>
16 "Image<PixelT>: PixelT must derive from Pixel<NumberT>");
19 Image() : width(0), height(0) {
21 Image(
int width,
int height, PixelT fill = PixelT())
22 : width(width), height(height), data(width * height, fill) {
25 template <
typename ConverterT>
26 void loadFromBuffer(
const uint8_t *buffer,
int width,
int height,
29 static_assert(std::is_same_v<decltype(converter.convert((
const uint8_t *)
nullptr)), PixelT>,
30 "Converter return type must match PixelT");
33 this->height = height;
34 const int pixelCount = getPixelCount();
35 data.resize(pixelCount);
36 for (
int i = 0; i < pixelCount; ++i) {
37 data[i] = converter.convert(&buffer[i * converter.bytesPerPixel]);
41 void fill(
const PixelT &color) {
42 std::fill(data.begin(), data.end(), color);
45 using iter =
typename std::vector<PixelT>::iterator;
52 using const_iter =
typename std::vector<PixelT>::const_iterator;
53 const_iter begin()
const {
56 const_iter end()
const {
60 int getWidth()
const {
63 int getHeight()
const {
66 int getPixelCount()
const {
67 return width * height;
70 return getPixelCount();
73 const std::vector<PixelT> &getData()
const {
77 const PixelT &getPixel(
int x,
int y)
const {
78 return data[index(x, y)];
80 PixelT &getPixel(
int x,
int y) {
81 return data[index(x, y)];
84 void setPixel(
int x,
int y,
const PixelT &p) {
85 data[index(x, y)] = p;
89 PixelT &operator[](
int idx) {
92 const PixelT &operator[](
int idx)
const {
97 PixelT &operator()(
int x,
int y) {
98 return data.at(index(x, y));
100 const PixelT &operator()(
int x,
int y)
const {
101 return data.at(index(x, y));
105 std::vector<PixelT> data;
108 int index(
int x,
int y)
const {
109 if (x < 0 || y < 0 || x >= width || y >= height)
110 throw std::out_of_range(
"Pixel coordinates out of bounds");
111 return y * width + x;