20template <
typename PixelType>
50 [[nodiscard]] PixelType
at(std::size_t i)
const
52 if (
const auto s =
size(); i >= s) {
53 panic(std::format(
"index {} out of bounds for image size {}", i, s));
63 [[nodiscard]] PixelType
at(std::size_t row, std::size_t col)
const
66 panic(std::format(
"col {} out of bounds for image width {}", col, width_));
69 panic(std::format(
"row {} out of bounds for image height {}", row, height_));
71 return pixels_[row * width_ + col];
78 void set(std::size_t i, PixelType pixel)
80 if (
const auto s =
size(); i >= s) {
81 panic(std::format(
"index {} out of bounds for image size {}", i, s));
91 void set(std::size_t row, std::size_t col, PixelType pixel)
94 panic(std::format(
"col {} out of bounds for image width {}", col, width_));
97 panic(std::format(
"row {} out of bounds for image height {}", row, height_));
99 pixels_[row * width_ + col] = pixel;
102 [[nodiscard]] std::size_t
width()
const
112 [[nodiscard]] std::size_t
size()
const
114 return pixels_.size();
127 if (width_ % 8 != 0) {
128 panic(std::format(
"image width {} is not divisible by 8", width_));
130 if (height_ % 8 != 0) {
131 panic(std::format(
"image height {} is not divisible by 8", height_));
133 return (width_ / 8) * (height_ / 8);
136 [[nodiscard]]
const std::optional<std::vector<Rgba32>> &
palette()
const
147 std::vector<PixelType> pixels_;
148 std::optional<std::vector<Rgba32>> palette_;
A template for two-dimensional images with arbitrarily typed pixel values.
void set(std::size_t i, PixelType pixel)
Sets the pixel value at a given one-dimensional pixel index.
std::size_t size_in_tiles() const
Gets the number of 8x8 tile regions in this image.
std::size_t width() const
PixelType at(std::size_t row, std::size_t col) const
Fetches the pixel value at a given row and column.
const std::optional< std::vector< Rgba32 > > & palette() const
std::size_t height() const
PixelType at(std::size_t i) const
Fetches the pixel value at a given one-dimensional pixel index.
Image(std::size_t width, std::size_t height, PixelType fill_color)
Constructs an image with all pixels set to a specified fill value.
Image(std::size_t width, std::size_t height, std::vector< Rgba32 > palette)
void palette(std::vector< Rgba32 > palette)
Image(std::size_t width, std::size_t height)
void set(std::size_t row, std::size_t col, PixelType pixel)
Sets the pixel value at a given row and column.
void panic(const StringViewSourceLoc &s)
Unconditionally terminates the program with a panic message.