22template <
typename PixelType>
56 [[nodiscard]] PixelType
at(std::size_t i)
const
58 if (
const auto s =
size(); i >= s) {
59 panic(std::format(
"index {} out of bounds for image size {}", i, s));
71 [[nodiscard]] PixelType
at(std::size_t row, std::size_t col)
const
74 panic(std::format(
"col {} out of bounds for image width {}", col, width_));
77 panic(std::format(
"row {} out of bounds for image height {}", row, height_));
79 return pixels_[row * width_ + col];
88 void set(std::size_t i, PixelType pixel)
90 if (
const auto s =
size(); i >= s) {
91 panic(std::format(
"index {} out of bounds for image size {}", i, s));
103 void set(std::size_t row, std::size_t col, PixelType pixel)
106 panic(std::format(
"col {} out of bounds for image width {}", col, width_));
108 if (row >= height_) {
109 panic(std::format(
"row {} out of bounds for image height {}", row, height_));
111 pixels_[row * width_ + col] = pixel;
114 [[nodiscard]] std::size_t
width()
const
124 [[nodiscard]] std::size_t
size()
const
126 return pixels_.size();
141 if (width_ % 8 != 0) {
142 panic(std::format(
"image width {} is not divisible by 8", width_));
144 if (height_ % 8 != 0) {
145 panic(std::format(
"image height {} is not divisible by 8", height_));
147 return (width_ / 8) * (height_ / 8);
150 [[nodiscard]]
const std::optional<std::vector<Rgba32>> &
palette()
const
157 palette_ = std::move(pal);
161 std::vector<PixelType> pixels_;
162 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 > pal)
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.