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
35template <typename PixelType>
36[[nodiscard]] std::vector<PixelTile<PixelType>> extract_tiles_from_image(
37 const Image<PixelType> &img, std::size_t tile_offset, std::size_t tile_count, std::size_t tiles_per_row = 16)
38{
39 if (img.width() % tile::side_length_pix != 0 || img.height() % tile::side_length_pix != 0) {
40 panic(
41 std::format(
42 "image dimensions must be multiples of {}, got {}x{}",
44 img.width(),
45 img.height()));
46 }
47
48 std::vector<PixelTile<PixelType>> result;
49 result.reserve(tile_count);
50
51 for (std::size_t i = 0; i < tile_count; ++i) {
52 const std::size_t tile_idx = tile_offset + i;
53 const std::size_t tile_row = tile_idx / tiles_per_row;
54 const std::size_t tile_col = tile_idx % tiles_per_row;
55
56 const std::size_t pixel_x = tile_col * tile::side_length_pix;
57 const std::size_t pixel_y = tile_row * tile::side_length_pix;
58
59 PixelTile<PixelType> pixel_tile;
60 for (std::size_t row = 0; row < tile::side_length_pix; ++row) {
61 for (std::size_t col = 0; col < tile::side_length_pix; ++col) {
62 pixel_tile.set(row, col, img.at(pixel_y + row, pixel_x + col));
63 }
64 }
65
66 result.push_back(std::move(pixel_tile));
67 }
68
69 return result;
70}
71
89template <typename PixelType>
90[[nodiscard]] std::vector<PixelTile<PixelType>> extract_tiles_from_image(const Image<PixelType> &img)
91{
92 if (img.width() % tile::side_length_pix != 0 || img.height() % tile::side_length_pix != 0) {
93 panic(
94 std::format(
95 "image dimensions must be multiples of {}, got {}x{}",
97 img.width(),
98 img.height()));
99 }
100
101 const std::size_t tiles_per_row = img.width() / tile::side_length_pix;
102 const std::size_t total_tiles = img.size_in_tiles();
103
104 return extract_tiles_from_image(img, 0, total_tiles, tiles_per_row);
105}
106
120template <typename PixelType>
122 const Image<PixelType> &img,
123 std::size_t tile_idx,
124 std::size_t tiles_per_row = metatile::metatiles_per_row * metatile::tiles_per_side)
125{
126 return extract_tiles_from_image(img, tile_idx, 1, tiles_per_row).at(0);
127}
128
129} // namespace porytiles
A template for two-dimensional images with arbitrarily typed pixel values.
Definition image.hpp:23
std::size_t size_in_tiles() const
Gets the number of 8x8 tile regions in this image.
Definition image.hpp:139
std::size_t width() const
Definition image.hpp:114
std::size_t height() const
Definition image.hpp:119
PixelType at(std::size_t i) const
Fetches the pixel value at a given one-dimensional pixel index.
Definition image.hpp:56
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:16
constexpr std::size_t metatiles_per_row
Definition metatile.hpp:22
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.