Porytiles
Loading...
Searching...
No Matches
image.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <optional>
4#include <vector>
5
6#include "fmt/format.h"
7
10
11namespace porytiles2 {
12
23template <typename PixelType>
24class Image {
25 public:
26 Image() : Image(0, 0) {}
27
28 explicit Image(std::size_t width, std::size_t height) : pixels_(width * height), width_{width}, height_{height} {}
29
30 Image(std::size_t width, std::size_t height, std::vector<Rgba32> palette)
31 : pixels_(width * height), palette_(std::move(palette)), width_{width}, height_{height}
32 {
33 }
34
45 [[nodiscard]] PixelType at(std::size_t i) const
46 {
47 if (const auto s = size(); i >= s) {
48 panic(fmt::format("index {} out of bounds for image size {}", i, s));
49 }
50 return pixels_[i];
51 }
52
60 [[nodiscard]] PixelType at(std::size_t row, std::size_t col) const
61 {
62 if (col >= width_) {
63 panic(fmt::format("col {} out of bounds for image width {}", col, width_));
64 }
65 if (row >= height_) {
66 panic(fmt::format("row {} out of bounds for image height {}", row, height_));
67 }
68 return pixels_[row * width_ + col];
69 }
70
77 void set(std::size_t i, PixelType pixel)
78 {
79 if (const auto s = size(); i >= s) {
80 panic(fmt::format("index {} out of bounds for image size {}", i, s));
81 }
82 pixels_[i] = pixel;
83 }
84
92 void set(std::size_t row, std::size_t col, PixelType pixel)
93 {
94 if (col >= width_) {
95 panic(fmt::format("col {} out of bounds for image width {}", col, width_));
96 }
97 if (row >= height_) {
98 panic(fmt::format("row {} out of bounds for image height {}", row, height_));
99 }
100 pixels_[row * width_ + col] = pixel;
101 }
102
108 [[nodiscard]] std::size_t width() const
109 {
110 return width_;
111 }
112
118 [[nodiscard]] std::size_t height() const
119 {
120 return height_;
121 }
122
128 [[nodiscard]] std::size_t size() const
129 {
130 return pixels_.size();
131 }
132
133 [[nodiscard]] const std::optional<std::vector<Rgba32>> &palette() const
134 {
135 return palette_;
136 }
137
138 private:
139 std::vector<PixelType> pixels_;
140 // TODO: is there a better way to handle this?
141 std::optional<std::vector<Rgba32>> palette_;
142 std::size_t width_;
143 std::size_t height_;
144};
145
146} // namespace porytiles2
A template for two-dimensional images with arbitrarily typed pixel values.
Definition image.hpp:24
std::size_t width() const
Gets the width of this image in pixels.
Definition image.hpp:108
void set(std::size_t i, PixelType pixel)
Sets the pixel value at a given one-dimensional pixel index.
Definition image.hpp:77
Image(std::size_t width, std::size_t height, std::vector< Rgba32 > palette)
Definition image.hpp:30
const std::optional< std::vector< Rgba32 > > & palette() const
Definition image.hpp:133
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:92
PixelType at(std::size_t i) const
Fetches the pixel value at a given one-dimensional pixel index.
Definition image.hpp:45
std::size_t height() const
Gets the height of this image in pixels.
Definition image.hpp:118
PixelType at(std::size_t row, std::size_t col) const
Fetches the pixel value at a given row and column.
Definition image.hpp:60
std::size_t size() const
Gets the size of this image in pixels.
Definition image.hpp:128
Image(std::size_t width, std::size_t height)
Definition image.hpp:28
void panic(const StringViewSourceLoc &s)
Unconditionally terminates the program with a panic message.
Definition panic.hpp:53