Porytiles
Loading...
Searching...
No Matches
image_tileizer.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cstddef>
4#include <format>
5#include <vector>
6
10
11namespace porytiles {
12
27template <typename T>
29 public:
55 {
56 // Validate that image dimensions are multiples of tile size
57 if (img.width() % tile::side_length_pix != 0 || img.height() % tile::side_length_pix != 0) {
58 return FormattableError{std::format(
59 "image dimensions must be a multiple of {}, got {}x{}",
61 img.width(),
62 img.height())};
63 }
64
65 const std::size_t tiles_per_row = img.width() / tile::side_length_pix;
66 const std::size_t tiles_per_col = img.height() / tile::side_length_pix;
67 const std::size_t total_tiles = tiles_per_row * tiles_per_col;
68
69 std::vector<PixelTile<T>> tiles;
70 tiles.reserve(total_tiles);
71
72 // Process each tile region
73 for (std::size_t tile_row = 0; tile_row < tiles_per_col; ++tile_row) {
74 for (std::size_t tile_col = 0; tile_col < tiles_per_row; ++tile_col) {
75 PixelTile<T> tile;
76
77 // Calculate pixel offsets for this tile
78 const std::size_t pixel_row_offset = tile_row * tile::side_length_pix;
79 const std::size_t pixel_col_offset = tile_col * tile::side_length_pix;
80
81 // Copy pixels from source image to tile
82 for (std::size_t pixel_row = 0; pixel_row < tile::side_length_pix; ++pixel_row) {
83 for (std::size_t pixel_col = 0; pixel_col < tile::side_length_pix; ++pixel_col) {
84 const std::size_t src_row = pixel_row_offset + pixel_row;
85 const std::size_t src_col = pixel_col_offset + pixel_col;
86
87 tile.set(pixel_row, pixel_col, img.at(src_row, src_col));
88 }
89 }
90
91 tiles.push_back(std::move(tile));
92 }
93 }
94
95 return tiles;
96 }
97};
98
99} // 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.
ChainableResult< std::vector< PixelTile< T > > > tileize(const Image< T > &img) const
Converts an image into a vector of 8x8 tiles.
A template for two-dimensional images with arbitrarily typed pixel values.
Definition image.hpp:23
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.
void set(std::size_t i, const PixelType &p)
constexpr std::size_t side_length_pix