Porytiles
Loading...
Searching...
No Matches
tile_extractors.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <format>
4#include <vector>
5
10
11namespace porytiles {
12
33template <typename PixelType>
34[[nodiscard]] std::vector<PixelTile<PixelType>> extract_tiles_from_image(
35 const Image<PixelType> &img, std::size_t tile_offset, std::size_t tile_count, std::size_t tiles_per_row = 16)
36{
37 if (img.width() % tile::side_length_pix != 0 || img.height() % tile::side_length_pix != 0) {
38 panic(
39 std::format(
40 "image dimensions must be multiples of {}, got {}x{}",
42 img.width(),
43 img.height()));
44 }
45
46 std::vector<PixelTile<PixelType>> result;
47 result.reserve(tile_count);
48
49 for (std::size_t i = 0; i < tile_count; ++i) {
50 const std::size_t tile_idx = tile_offset + i;
51 const std::size_t tile_row = tile_idx / tiles_per_row;
52 const std::size_t tile_col = tile_idx % tiles_per_row;
53
54 const std::size_t pixel_x = tile_col * tile::side_length_pix;
55 const std::size_t pixel_y = tile_row * tile::side_length_pix;
56
57 PixelTile<PixelType> pixel_tile;
58 for (std::size_t row = 0; row < tile::side_length_pix; ++row) {
59 for (std::size_t col = 0; col < tile::side_length_pix; ++col) {
60 pixel_tile.set(row, col, img.at(pixel_y + row, pixel_x + col));
61 }
62 }
63
64 result.push_back(std::move(pixel_tile));
65 }
66
67 return result;
68}
69
85template <typename PixelType>
86[[nodiscard]] std::vector<PixelTile<PixelType>> extract_tiles_from_image(const Image<PixelType> &img)
87{
88 if (img.width() % tile::side_length_pix != 0 || img.height() % tile::side_length_pix != 0) {
89 panic(
90 std::format(
91 "image dimensions must be multiples of {}, got {}x{}",
93 img.width(),
94 img.height()));
95 }
96
97 const std::size_t tiles_per_row = img.width() / tile::side_length_pix;
98 const std::size_t total_tiles = img.size_in_tiles();
99
100 return extract_tiles_from_image(img, 0, total_tiles, tiles_per_row);
101}
102
114template <typename PixelType>
116 const Image<PixelType> &img,
117 std::size_t tile_idx,
118 std::size_t tiles_per_row = metatile::metatiles_per_row * metatile::tiles_per_side)
119{
120 return extract_tiles_from_image(img, tile_idx, 1, tiles_per_row).at(0);
121}
122
123} // namespace porytiles
A template for two-dimensional images with arbitrarily typed pixel values.
Definition image.hpp:21
std::size_t size_in_tiles() const
Gets the number of 8x8 tile regions in this image.
Definition image.hpp:125
std::size_t width() const
Definition image.hpp:102
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
An 8x8 tile backed by literal-array-based per-pixel storage of an arbitrary pixel type.
PixelType at(std::size_t i) const
void set(std::size_t i, const PixelType &p)
constexpr std::size_t tiles_per_side
Definition metatile.hpp:21
constexpr std::size_t metatiles_per_row
Definition metatile.hpp:27
constexpr std::size_t side_length_pix
PixelTile< PixelType > extract_single_tile(const Image< PixelType > &img, std::size_t tile_idx, std::size_t tiles_per_row=metatile::metatiles_per_row *metatile::tiles_per_side)
Extracts a single tile from an image at a given tile index.
void panic(const StringViewSourceLoc &s)
Unconditionally terminates the program with a panic message.
Definition panic.cpp:43
std::vector< PixelTile< PixelType > > extract_tiles_from_image(const Image< PixelType > &img, std::size_t tile_offset, std::size_t tile_count, std::size_t tiles_per_row=16)
Extracts a subset of 8x8 tiles from a tileset image at a specific offset.