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
27template <typename T>
29 public:
47 metatileize(const Image<T> &bottom, const Image<T> &middle, const Image<T> &top) const
48 {
49 // Validate that all images have the same dimensions
50 if (bottom.width() != middle.width() || bottom.height() != middle.height() || bottom.width() != top.width() ||
51 bottom.height() != top.height()) {
52 return FormattableError{std::format(
53 "layer images have mismatched dimensions: bottom={}x{}, middle={}x{}, top={}x{}",
54 bottom.width(),
55 bottom.height(),
56 middle.width(),
57 middle.height(),
58 top.width(),
59 top.height())};
60 }
61
62 // Tileize each layer image
63 const auto bottom_tiles_result = tileizer_.tileize(bottom);
64 if (!bottom_tiles_result.has_value()) {
66 FormattableError{"Failed to tileize bottom layer."}, bottom_tiles_result};
67 }
68 const auto &bottom_tiles = bottom_tiles_result.value();
69
70 const auto middle_tiles_result = tileizer_.tileize(middle);
71 if (!middle_tiles_result.has_value()) {
73 FormattableError{"Failed to tileize middle layer."}, middle_tiles_result};
74 }
75 const auto &middle_tiles = middle_tiles_result.value();
76
77 const auto top_tiles_result = tileizer_.tileize(top);
78 if (!top_tiles_result.has_value()) {
80 FormattableError{"Failed to tileize top layer."}, top_tiles_result};
81 }
82 const auto &top_tiles = top_tiles_result.value();
83
84 // Validate that dimensions are multiples of metatile size. We already validated that all image dimensions are
85 // identical, so we can just check bottom here as a surrogate for the other two layers. Additionally, we already
86 // checked in the tileization step if the image dimensions were a multiple of 8. Now, we check that the image
87 // dimensions are a multiple of 16 to confirm that it can be correctly metatileized.
88 if (bottom.width() % metatile::side_length_pix != 0 || bottom.height() % metatile::side_length_pix != 0) {
89 return FormattableError{std::format(
90 "image dimensions must be multiples of {}, got {}x{}",
92 bottom.width(),
93 bottom.height())};
94 }
95
96 const std::size_t metatiles_per_row = bottom.width() / metatile::side_length_pix;
97 const std::size_t metatiles_per_col = bottom.height() / metatile::side_length_pix;
98 const std::size_t total_metatiles = metatiles_per_row * metatiles_per_col;
99
100 std::vector<Metatile<T>> metatiles;
101 metatiles.reserve(total_metatiles);
102
103 const std::size_t tiles_per_image_row = bottom.width() / tile::side_length_pix;
104
105 // Process each 16x16 metatile region
106 for (std::size_t metatile_row = 0; metatile_row < metatiles_per_col; ++metatile_row) {
107 for (std::size_t metatile_col = 0; metatile_col < metatiles_per_row; ++metatile_col) {
108 Metatile<T> metatile;
109 populate_metatile_at_position(
110 metatile, bottom_tiles, middle_tiles, top_tiles, metatile_row, metatile_col, tiles_per_image_row);
111 metatiles.push_back(std::move(metatile));
112 }
113 }
114
115 return metatiles;
116 }
117
134 demetatileize(const std::vector<Metatile<T>> &metatiles, std::size_t metatiles_per_row) const
135 {
136 // Validate input parameters
137 if (metatiles_per_row == 0) {
138 panic("metatiles_per_row must be greater than zero");
139 }
140
141 if (metatiles.empty()) {
142 return FormattableError{"Input metatiles vector was empty."};
143 }
144
145 // Compute metatiles_per_col using ceiling division
146 const std::size_t metatiles_per_col = (metatiles.size() + metatiles_per_row - 1) / metatiles_per_row;
147
148 // Calculate image dimensions
149 const std::size_t image_width = metatiles_per_row * metatile::side_length_pix;
150 const std::size_t image_height = metatiles_per_col * metatile::side_length_pix;
151
152 // Create the three layer images
153 Image<T> bottom_image{image_width, image_height};
154 Image<T> middle_image{image_width, image_height};
155 Image<T> top_image{image_width, image_height};
156
157 // Process each metatile position in the grid
158 for (std::size_t metatile_row = 0; metatile_row < metatiles_per_col; ++metatile_row) {
159 for (std::size_t metatile_col = 0; metatile_col < metatiles_per_row; ++metatile_col) {
160 const std::size_t metatile_idx = metatile_row * metatiles_per_row + metatile_col;
161
162 // Check if we have a metatile for this position, or if we need to pad with empty pixels
163 if (metatile_idx < metatiles.size()) {
164 const auto &metatile = metatiles[metatile_idx];
165 copy_metatile_to_images(
166 metatile, bottom_image, middle_image, top_image, metatile_row, metatile_col);
167 }
168 else {
169 fill_region_with_transparent(bottom_image, middle_image, top_image, metatile_row, metatile_col);
170 }
171 }
172 }
173
174 return std::make_tuple(std::move(bottom_image), std::move(middle_image), std::move(top_image));
175 }
176
177 private:
178 ImageTileizer<T> tileizer_;
179
180 static void populate_metatile_at_position(
181 Metatile<T> &metatile,
182 const std::vector<PixelTile<T>> &bottom_tiles,
183 const std::vector<PixelTile<T>> &middle_tiles,
184 const std::vector<PixelTile<T>> &top_tiles,
185 std::size_t metatile_row,
186 std::size_t metatile_col,
187 std::size_t tiles_per_image_row)
188 {
189 for (std::size_t tile_idx = 0; tile_idx < metatile::tiles_per_metatile_layer; ++tile_idx) {
190 // Calculate tile position within the metatile
191 const std::size_t tile_row = tile_idx / metatile::tiles_per_side;
192 const std::size_t tile_col = tile_idx % metatile::tiles_per_side;
193
194 // Calculate which tile index we need from the tileized arrays
195 const std::size_t global_tile_row = metatile_row * metatile::tiles_per_side + tile_row;
196 const std::size_t global_tile_col = metatile_col * metatile::tiles_per_side + tile_col;
197 const std::size_t global_tile_idx = global_tile_row * tiles_per_image_row + global_tile_col;
198
199 // Set tiles in the metatile from the tileized arrays
200 metatile.set_bottom(tile_idx, bottom_tiles[global_tile_idx]);
201 metatile.set_middle(tile_idx, middle_tiles[global_tile_idx]);
202 metatile.set_top(tile_idx, top_tiles[global_tile_idx]);
203 }
204 }
205
206 static void copy_metatile_to_images(
207 const Metatile<T> &metatile,
208 Image<T> &bottom_image,
209 Image<T> &middle_image,
210 Image<T> &top_image,
211 std::size_t metatile_row,
212 std::size_t metatile_col)
213 {
214 // Extract tiles from this metatile and place them in the appropriate image positions
215 for (std::size_t tile_idx = 0; tile_idx < metatile::tiles_per_metatile_layer; ++tile_idx) {
216 // Calculate tile position within the metatile
217 const std::size_t tile_row = tile_idx / metatile::tiles_per_side;
218 const std::size_t tile_col = tile_idx % metatile::tiles_per_side;
219
220 // Calculate the starting pixel position for this tile in the image
221 const std::size_t start_pixel_row =
222 metatile_row * metatile::side_length_pix + tile_row * tile::side_length_pix;
223 const std::size_t start_pixel_col =
224 metatile_col * metatile::side_length_pix + tile_col * tile::side_length_pix;
225
226 // Copy pixels from each layer's tile to the corresponding image
227 const auto &bottom_tile = metatile.bottom(tile_idx);
228 const auto &middle_tile = metatile.middle(tile_idx);
229 const auto &top_tile = metatile.top(tile_idx);
230
231 for (std::size_t pixel_row = 0; pixel_row < tile::side_length_pix; ++pixel_row) {
232 for (std::size_t pixel_col = 0; pixel_col < tile::side_length_pix; ++pixel_col) {
233 const std::size_t image_row = start_pixel_row + pixel_row;
234 const std::size_t image_col = start_pixel_col + pixel_col;
235
236 bottom_image.set(image_row, image_col, bottom_tile.at(pixel_row, pixel_col));
237 middle_image.set(image_row, image_col, middle_tile.at(pixel_row, pixel_col));
238 top_image.set(image_row, image_col, top_tile.at(pixel_row, pixel_col));
239 }
240 }
241 }
242 }
243
244 static void fill_region_with_transparent(
245 Image<T> &bottom_image,
246 Image<T> &middle_image,
247 Image<T> &top_image,
248 std::size_t metatile_row,
249 std::size_t metatile_col)
250 {
251 const T transparent_pixel{};
252
253 // Fill the entire 16x16 region for this metatile position with transparent pixels
254 for (std::size_t pixel_row = 0; pixel_row < metatile::side_length_pix; ++pixel_row) {
255 for (std::size_t pixel_col = 0; pixel_col < metatile::side_length_pix; ++pixel_col) {
256 const std::size_t image_row = metatile_row * metatile::side_length_pix + pixel_row;
257 const std::size_t image_col = metatile_col * metatile::side_length_pix + pixel_col;
258
259 bottom_image.set(image_row, image_col, transparent_pixel);
260 middle_image.set(image_row, image_col, transparent_pixel);
261 top_image.set(image_row, image_col, transparent_pixel);
262 }
263 }
264 }
265};
266
267} // 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:57
Service for converting images into collections of 8x8 tiles.
A template for two-dimensional images with arbitrarily typed pixel values.
Definition image.hpp:21
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:224
void set_bottom(std::size_t i, PixelTile< PixelType > tile)
Set a PixelTile in the bottom layer.
Definition metatile.hpp:384
void set_top(std::size_t i, PixelTile< PixelType > tile)
Set a Tile in the top layer.
Definition metatile.hpp:466
void set_middle(std::size_t i, PixelTile< PixelType > tile)
Set a PixelTile in the middle layer.
Definition metatile.hpp:425
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:24
constexpr std::size_t tiles_per_side
Definition metatile.hpp:21
constexpr std::size_t tiles_per_metatile_layer
Definition metatile.hpp:22
constexpr std::size_t side_length_pix
void panic(const StringViewSourceLoc &s)
Unconditionally terminates the program with a panic message.
Definition panic.cpp:43