Porytiles
Loading...
Searching...
No Matches
image.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <format>
4#include <optional>
5#include <vector>
6
9
10namespace porytiles {
11
22template <typename PixelType>
23class Image {
24 public:
25 Image() : Image(0, 0) {}
26
27 Image(std::size_t width, std::size_t height) : pixels_(width * height), width_{width}, height_{height} {}
28
29 Image(std::size_t width, std::size_t height, std::vector<Rgba32> palette)
30 : pixels_(width * height), palette_(std::move(palette)), width_{width}, height_{height}
31 {
32 }
33
41 Image(std::size_t width, std::size_t height, PixelType fill_color)
42 : pixels_(width * height, fill_color), width_{width}, height_{height}
43 {
44 }
45
56 [[nodiscard]] PixelType at(std::size_t i) const
57 {
58 if (const auto s = size(); i >= s) {
59 panic(std::format("index {} out of bounds for image size {}", i, s));
60 }
61 return pixels_[i];
62 }
63
71 [[nodiscard]] PixelType at(std::size_t row, std::size_t col) const
72 {
73 if (col >= width_) {
74 panic(std::format("col {} out of bounds for image width {}", col, width_));
75 }
76 if (row >= height_) {
77 panic(std::format("row {} out of bounds for image height {}", row, height_));
78 }
79 return pixels_[row * width_ + col];
80 }
81
88 void set(std::size_t i, PixelType pixel)
89 {
90 if (const auto s = size(); i >= s) {
91 panic(std::format("index {} out of bounds for image size {}", i, s));
92 }
93 pixels_[i] = pixel;
94 }
95
103 void set(std::size_t row, std::size_t col, PixelType pixel)
104 {
105 if (col >= width_) {
106 panic(std::format("col {} out of bounds for image width {}", col, width_));
107 }
108 if (row >= height_) {
109 panic(std::format("row {} out of bounds for image height {}", row, height_));
110 }
111 pixels_[row * width_ + col] = pixel;
112 }
113
114 [[nodiscard]] std::size_t width() const
115 {
116 return width_;
117 }
118
119 [[nodiscard]] std::size_t height() const
120 {
121 return height_;
122 }
123
124 [[nodiscard]] std::size_t size() const
125 {
126 return pixels_.size();
127 }
128
139 [[nodiscard]] std::size_t size_in_tiles() const
140 {
141 if (width_ % 8 != 0) {
142 panic(std::format("image width {} is not divisible by 8", width_));
143 }
144 if (height_ % 8 != 0) {
145 panic(std::format("image height {} is not divisible by 8", height_));
146 }
147 return (width_ / 8) * (height_ / 8);
148 }
149
150 [[nodiscard]] const std::optional<std::vector<Rgba32>> &palette() const
151 {
152 return palette_;
153 }
154
155 void palette(std::vector<Rgba32> pal)
156 {
157 palette_ = std::move(pal);
158 }
159
160 private:
161 std::vector<PixelType> pixels_;
162 std::optional<std::vector<Rgba32>> palette_;
163 std::size_t width_;
164 std::size_t height_;
165};
166
167} // namespace porytiles
A template for two-dimensional images with arbitrarily typed pixel values.
Definition image.hpp:23
void set(std::size_t i, PixelType pixel)
Sets the pixel value at a given one-dimensional pixel index.
Definition image.hpp:88
std::size_t size_in_tiles() const
Gets the number of 8x8 tile regions in this image.
Definition image.hpp:139
std::size_t size() const
Definition image.hpp:124
std::size_t width() const
Definition image.hpp:114
PixelType at(std::size_t row, std::size_t col) const
Fetches the pixel value at a given row and column.
Definition image.hpp:71
const std::optional< std::vector< Rgba32 > > & palette() const
Definition image.hpp:150
std::size_t height() const
Definition image.hpp:119
PixelType at(std::size_t i) const
Fetches the pixel value at a given one-dimensional pixel index.
Definition image.hpp:56
Image(std::size_t width, std::size_t height, PixelType fill_color)
Constructs an image with all pixels set to a specified fill value.
Definition image.hpp:41
Image(std::size_t width, std::size_t height, std::vector< Rgba32 > palette)
Definition image.hpp:29
void palette(std::vector< Rgba32 > pal)
Definition image.hpp:155
Image(std::size_t width, std::size_t height)
Definition image.hpp:27
void set(std::size_t row, std::size_t col, PixelType pixel)
Sets the pixel value at a given row and column.
Definition image.hpp:103
void panic(const StringViewSourceLoc &s)
Unconditionally terminates the program with a panic message.
Definition panic.cpp:43