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
20template <typename PixelType>
21class Image {
22 public:
23 Image() : Image(0, 0) {}
24
25 Image(std::size_t width, std::size_t height) : pixels_(width * height), width_{width}, height_{height} {}
26
27 Image(std::size_t width, std::size_t height, std::vector<Rgba32> palette)
28 : pixels_(width * height), palette_(std::move(palette)), width_{width}, height_{height}
29 {
30 }
31
37 Image(std::size_t width, std::size_t height, PixelType fill_color)
38 : pixels_(width * height, fill_color), width_{width}, height_{height}
39 {
40 }
41
50 [[nodiscard]] PixelType at(std::size_t i) const
51 {
52 if (const auto s = size(); i >= s) {
53 panic(std::format("index {} out of bounds for image size {}", i, s));
54 }
55 return pixels_[i];
56 }
57
63 [[nodiscard]] PixelType at(std::size_t row, std::size_t col) const
64 {
65 if (col >= width_) {
66 panic(std::format("col {} out of bounds for image width {}", col, width_));
67 }
68 if (row >= height_) {
69 panic(std::format("row {} out of bounds for image height {}", row, height_));
70 }
71 return pixels_[row * width_ + col];
72 }
73
78 void set(std::size_t i, PixelType pixel)
79 {
80 if (const auto s = size(); i >= s) {
81 panic(std::format("index {} out of bounds for image size {}", i, s));
82 }
83 pixels_[i] = pixel;
84 }
85
91 void set(std::size_t row, std::size_t col, PixelType pixel)
92 {
93 if (col >= width_) {
94 panic(std::format("col {} out of bounds for image width {}", col, width_));
95 }
96 if (row >= height_) {
97 panic(std::format("row {} out of bounds for image height {}", row, height_));
98 }
99 pixels_[row * width_ + col] = pixel;
100 }
101
102 [[nodiscard]] std::size_t width() const
103 {
104 return width_;
105 }
106
107 [[nodiscard]] std::size_t height() const
108 {
109 return height_;
110 }
111
112 [[nodiscard]] std::size_t size() const
113 {
114 return pixels_.size();
115 }
116
125 [[nodiscard]] std::size_t size_in_tiles() const
126 {
127 if (width_ % 8 != 0) {
128 panic(std::format("image width {} is not divisible by 8", width_));
129 }
130 if (height_ % 8 != 0) {
131 panic(std::format("image height {} is not divisible by 8", height_));
132 }
133 return (width_ / 8) * (height_ / 8);
134 }
135
136 [[nodiscard]] const std::optional<std::vector<Rgba32>> &palette() const
137 {
138 return palette_;
139 }
140
141 void palette(std::vector<Rgba32> palette)
142 {
143 palette_ = std::move(palette);
144 }
145
146 private:
147 std::vector<PixelType> pixels_;
148 std::optional<std::vector<Rgba32>> palette_;
149 std::size_t width_;
150 std::size_t height_;
151};
152
153} // namespace porytiles
A template for two-dimensional images with arbitrarily typed pixel values.
Definition image.hpp:21
void set(std::size_t i, PixelType pixel)
Sets the pixel value at a given one-dimensional pixel index.
Definition image.hpp:78
std::size_t size_in_tiles() const
Gets the number of 8x8 tile regions in this image.
Definition image.hpp:125
std::size_t size() const
Definition image.hpp:112
std::size_t width() const
Definition image.hpp:102
PixelType at(std::size_t row, std::size_t col) const
Fetches the pixel value at a given row and column.
Definition image.hpp:63
const std::optional< std::vector< Rgba32 > > & palette() const
Definition image.hpp:136
std::size_t height() const
Definition image.hpp:107
PixelType at(std::size_t i) const
Fetches the pixel value at a given one-dimensional pixel index.
Definition image.hpp:50
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:37
Image(std::size_t width, std::size_t height, std::vector< Rgba32 > palette)
Definition image.hpp:27
void palette(std::vector< Rgba32 > palette)
Definition image.hpp:141
Image(std::size_t width, std::size_t height)
Definition image.hpp:25
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:91
void panic(const StringViewSourceLoc &s)
Unconditionally terminates the program with a panic message.
Definition panic.cpp:43