23template <
typename PixelType>
45 [[nodiscard]] PixelType
at(std::size_t i)
const
47 if (
const auto s =
size(); i >= s) {
48 panic(fmt::format(
"index {} out of bounds for image size {}", i, s));
60 [[nodiscard]] PixelType
at(std::size_t row, std::size_t col)
const
63 panic(fmt::format(
"col {} out of bounds for image width {}", col, width_));
66 panic(fmt::format(
"row {} out of bounds for image height {}", row, height_));
68 return pixels_[row * width_ + col];
77 void set(std::size_t i, PixelType pixel)
79 if (
const auto s =
size(); i >= s) {
80 panic(fmt::format(
"index {} out of bounds for image size {}", i, s));
92 void set(std::size_t row, std::size_t col, PixelType pixel)
95 panic(fmt::format(
"col {} out of bounds for image width {}", col, width_));
98 panic(fmt::format(
"row {} out of bounds for image height {}", row, height_));
100 pixels_[row * width_ + col] = pixel;
108 [[nodiscard]] std::size_t
width()
const
128 [[nodiscard]] std::size_t
size()
const
130 return pixels_.size();
133 [[nodiscard]]
const std::optional<std::vector<Rgba32>> &
palette()
const
139 std::vector<PixelType> pixels_;
141 std::optional<std::vector<Rgba32>> palette_;
A template for two-dimensional images with arbitrarily typed pixel values.
std::size_t width() const
Gets the width of this image in pixels.
void set(std::size_t i, PixelType pixel)
Sets the pixel value at a given one-dimensional pixel index.
Image(std::size_t width, std::size_t height, std::vector< Rgba32 > palette)
const std::optional< std::vector< Rgba32 > > & palette() const
void set(std::size_t row, std::size_t col, PixelType pixel)
Sets the pixel value at a given row and column.
PixelType at(std::size_t i) const
Fetches the pixel value at a given one-dimensional pixel index.
std::size_t height() const
Gets the height of this image in pixels.
PixelType at(std::size_t row, std::size_t col) const
Fetches the pixel value at a given row and column.
std::size_t size() const
Gets the size of this image in pixels.
Image(std::size_t width, std::size_t height)
void panic(const StringViewSourceLoc &s)
Unconditionally terminates the program with a panic message.