Porytiles
Loading...
Searching...
No Matches
layer_image_metatileizer.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cstddef>
4#include <format>
5#include <tuple>
6#include <vector>
7
14
15namespace porytiles {
16
29template <typename T>
31 public:
51 metatileize(const Image<T> &bottom, const Image<T> &middle, const Image<T> &top) const
52 {
53 // Validate that all images have the same dimensions
54 if (bottom.width() != middle.width() || bottom.height() != middle.height() || bottom.width() != top.width() ||
55 bottom.height() != top.height()) {
56 return FormattableError{std::format(
57 "layer images have mismatched dimensions: bottom={}x{}, middle={}x{}, top={}x{}",
58 bottom.width(),
59 bottom.height(),
60 middle.width(),
61 middle.height(),
62 top.width(),
63 top.height())};
64 }
65
66 // Tileize each layer image
67 const auto bottom_tiles_result = tileizer_.tileize(bottom);
68 if (!bottom_tiles_result.has_value()) {
70 FormattableError{"Failed to tileize bottom layer."}, bottom_tiles_result};
71 }
72 const auto &bottom_tiles = bottom_tiles_result.value();
73
74 const auto middle_tiles_result = tileizer_.tileize(middle);
75 if (!middle_tiles_result.has_value()) {
77 FormattableError{"Failed to tileize middle layer."}, middle_tiles_result};
78 }
79 const auto &middle_tiles = middle_tiles_result.value();
80
81 const auto top_tiles_result = tileizer_.tileize(top);
82 if (!top_tiles_result.has_value()) {
84 FormattableError{"Failed to tileize top layer."}, top_tiles_result};
85 }
86 const auto &top_tiles = top_tiles_result.value();
87
88 /*
89 * Validate that dimensions are multiples of metatile size. We already validated that all image dimensions are
90 * identical, so we can just check bottom here as a surrogate for the other two layers. Additionally, we already
91 * checked in the tileization step if the image dimensions were a multiple of 8. Now, we check that the image
92 * dimensions are a multiple of 16 to confirm that it can be correctly metatileized.
93 */
94 if (bottom.width() % metatile::side_length_pix != 0 || bottom.height() % metatile::side_length_pix != 0) {
95 return FormattableError{std::format(
96 "image dimensions must be multiples of {}, got {}x{}",
98 bottom.width(),
99 bottom.height())};
100 }
101
102 const std::size_t metatiles_per_row = bottom.width() / metatile::side_length_pix;
103 const std::size_t metatiles_per_col = bottom.height() / metatile::side_length_pix;
104 const std::size_t total_metatiles = metatiles_per_row * metatiles_per_col;
105
106 std::vector<Metatile<T>> metatiles;
107 metatiles.reserve(total_metatiles);
108
109 const std::size_t tiles_per_image_row = bottom.width() / tile::side_length_pix;
110
111 // Process each 16x16 metatile region
112 for (std::size_t metatile_row = 0; metatile_row < metatiles_per_col; ++metatile_row) {
113 for (std::size_t metatile_col = 0; metatile_col < metatiles_per_row; ++metatile_col) {
114 Metatile<T> metatile;
115 populate_metatile_at_position(
116 metatile, bottom_tiles, middle_tiles, top_tiles, metatile_row, metatile_col, tiles_per_image_row);
117 metatiles.push_back(std::move(metatile));
118 }
119 }
120
121 return metatiles;
122 }
123
142 demetatileize(const std::vector<Metatile<T>> &metatiles, std::size_t metatiles_per_row) const
143 {
144 // Validate input parameters
145 if (metatiles_per_row == 0) {
146 panic("metatiles_per_row must be greater than zero");
147 }
148
149 if (metatiles.empty()) {
150 return FormattableError{"Input metatiles vector was empty."};
151 }
152
153 // Compute metatiles_per_col using ceiling division
154 const std::size_t metatiles_per_col = (metatiles.size() + metatiles_per_row - 1) / metatiles_per_row;
155
156 // Calculate image dimensions
157 const std::size_t image_width = metatiles_per_row * metatile::side_length_pix;
158 const std::size_t image_height = metatiles_per_col * metatile::side_length_pix;
159
160 // Create the three layer images
161 Image<T> bottom_image{image_width, image_height};
162 Image<T> middle_image{image_width, image_height};
163 Image<T> top_image{image_width, image_height};
164
165 // Process each metatile position in the grid
166 for (std::size_t metatile_row = 0; metatile_row < metatiles_per_col; ++metatile_row) {
167 for (std::size_t metatile_col = 0; metatile_col < metatiles_per_row; ++metatile_col) {
168 const std::size_t metatile_idx = metatile_row * metatiles_per_row + metatile_col;
169
170 // Check if we have a metatile for this position, or if we need to pad with empty pixels
171 if (metatile_idx < metatiles.size()) {
172 const auto &metatile = metatiles[metatile_idx];
173 copy_metatile_to_images(
174 metatile, bottom_image, middle_image, top_image, metatile_row, metatile_col);
175 }
176 else {
177 fill_region_with_transparent(bottom_image, middle_image, top_image, metatile_row, metatile_col);
178 }
179 }
180 }
181
182 return std::make_tuple(std::move(bottom_image), std::move(middle_image), std::move(top_image));
183 }
184
185 private:
186 ImageTileizer<T> tileizer_;
187
188 static void populate_metatile_at_position(
189 Metatile<T> &metatile,
190 const std::vector<PixelTile<T>> &bottom_tiles,
191 const std::vector<PixelTile<T>> &middle_tiles,
192 const std::vector<PixelTile<T>> &top_tiles,
193 std::size_t metatile_row,
194 std::size_t metatile_col,
195 std::size_t tiles_per_image_row)
196 {
197 for (std::size_t tile_idx = 0; tile_idx < metatile::tiles_per_metatile_layer; ++tile_idx) {
198 // Calculate tile position within the metatile
199 const std::size_t tile_row = tile_idx / metatile::tiles_per_side;
200 const std::size_t tile_col = tile_idx % metatile::tiles_per_side;
201
202 // Calculate which tile index we need from the tileized arrays
203 const std::size_t global_tile_row = metatile_row * metatile::tiles_per_side + tile_row;
204 const std::size_t global_tile_col = metatile_col * metatile::tiles_per_side + tile_col;
205 const std::size_t global_tile_idx = global_tile_row * tiles_per_image_row + global_tile_col;
206
207 // Set tiles in the metatile from the tileized arrays
208 metatile.set_bottom(tile_idx, bottom_tiles[global_tile_idx]);
209 metatile.set_middle(tile_idx, middle_tiles[global_tile_idx]);
210 metatile.set_top(tile_idx, top_tiles[global_tile_idx]);
211 }
212 }
213
214 static void copy_metatile_to_images(
215 const Metatile<T> &metatile,
216 Image<T> &bottom_image,
217 Image<T> &middle_image,
218 Image<T> &top_image,
219 std::size_t metatile_row,
220 std::size_t metatile_col)
221 {
222 // Extract tiles from this metatile and place them in the appropriate image positions
223 for (std::size_t tile_idx = 0; tile_idx < metatile::tiles_per_metatile_layer; ++tile_idx) {
224 // Calculate tile position within the metatile
225 const std::size_t tile_row = tile_idx / metatile::tiles_per_side;
226 const std::size_t tile_col = tile_idx % metatile::tiles_per_side;
227
228 // Calculate the starting pixel position for this tile in the image
229 const std::size_t start_pixel_row =
230 metatile_row * metatile::side_length_pix + tile_row * tile::side_length_pix;
231 const std::size_t start_pixel_col =
232 metatile_col * metatile::side_length_pix + tile_col * tile::side_length_pix;
233
234 // Copy pixels from each layer's tile to the corresponding image
235 const auto &bottom_tile = metatile.bottom(tile_idx);
236 const auto &middle_tile = metatile.middle(tile_idx);
237 const auto &top_tile = metatile.top(tile_idx);
238
239 for (std::size_t pixel_row = 0; pixel_row < tile::side_length_pix; ++pixel_row) {
240 for (std::size_t pixel_col = 0; pixel_col < tile::side_length_pix; ++pixel_col) {
241 const std::size_t image_row = start_pixel_row + pixel_row;
242 const std::size_t image_col = start_pixel_col + pixel_col;
243
244 bottom_image.set(image_row, image_col, bottom_tile.at(pixel_row, pixel_col));
245 middle_image.set(image_row, image_col, middle_tile.at(pixel_row, pixel_col));
246 top_image.set(image_row, image_col, top_tile.at(pixel_row, pixel_col));
247 }
248 }
249 }
250 }
251
252 static void fill_region_with_transparent(
253 Image<T> &bottom_image,
254 Image<T> &middle_image,
255 Image<T> &top_image,
256 std::size_t metatile_row,
257 std::size_t metatile_col)
258 {
259 const T transparent_pixel{};
260
261 // Fill the entire 16x16 region for this metatile position with transparent pixels
262 for (std::size_t pixel_row = 0; pixel_row < metatile::side_length_pix; ++pixel_row) {
263 for (std::size_t pixel_col = 0; pixel_col < metatile::side_length_pix; ++pixel_col) {
264 const std::size_t image_row = metatile_row * metatile::side_length_pix + pixel_row;
265 const std::size_t image_col = metatile_col * metatile::side_length_pix + pixel_col;
266
267 bottom_image.set(image_row, image_col, transparent_pixel);
268 middle_image.set(image_row, image_col, transparent_pixel);
269 top_image.set(image_row, image_col, transparent_pixel);
270 }
271 }
272 }
273};
274
275} // namespace porytiles
A result type that maintains a chainable sequence of errors for debugging and error reporting.
General-purpose error implementation with formatted message support.
Definition error.hpp:63
Service for converting images into collections of 8x8 tiles.
A template for two-dimensional images with arbitrarily typed pixel values.
Definition image.hpp:23
Service for converting layer images into collections of metatiles.
ChainableResult< std::vector< Metatile< T > > > metatileize(const Image< T > &bottom, const Image< T > &middle, const Image< T > &top) const
Converts three layer images into a vector of metatiles.
ChainableResult< std::tuple< Image< T >, Image< T >, Image< T > > > demetatileize(const std::vector< Metatile< T > > &metatiles, std::size_t metatiles_per_row) const
Converts a vector of metatiles back into three separate layer images.
The core tileset entity - a 2x2 grid of PixelTile objects arranged into three layers.
Definition metatile.hpp:186
void set_bottom(std::size_t i, PixelTile< PixelType > tile)
Set a PixelTile in the bottom layer.
Definition metatile.hpp:366
void set_top(std::size_t i, PixelTile< PixelType > tile)
Set a Tile in the top layer.
Definition metatile.hpp:460
void set_middle(std::size_t i, PixelTile< PixelType > tile)
Set a PixelTile in the middle layer.
Definition metatile.hpp:413
An 8x8 tile backed by literal-array-based per-pixel storage of an arbitrary pixel type.
constexpr std::size_t side_length_pix
Definition metatile.hpp:19
constexpr std::size_t tiles_per_side
Definition metatile.hpp:16
constexpr std::size_t tiles_per_metatile_layer
Definition metatile.hpp:17
constexpr std::size_t side_length_pix
void panic(const StringViewSourceLoc &s)
Unconditionally terminates the program with a panic message.
Definition panic.cpp:43