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
25template <typename T>
27 public:
51 {
52 // Validate that image dimensions are multiples of tile size
53 if (img.width() % tile::side_length_pix != 0 || img.height() % tile::side_length_pix != 0) {
54 return FormattableError{std::format(
55 "image dimensions must be a multiple of {}, got {}x{}",
57 img.width(),
58 img.height())};
59 }
60
61 const std::size_t tiles_per_row = img.width() / tile::side_length_pix;
62 const std::size_t tiles_per_col = img.height() / tile::side_length_pix;
63 const std::size_t total_tiles = tiles_per_row * tiles_per_col;
64
65 std::vector<PixelTile<T>> tiles;
66 tiles.reserve(total_tiles);
67
68 // Process each tile region
69 for (std::size_t tile_row = 0; tile_row < tiles_per_col; ++tile_row) {
70 for (std::size_t tile_col = 0; tile_col < tiles_per_row; ++tile_col) {
71 PixelTile<T> tile;
72
73 // Calculate pixel offsets for this tile
74 const std::size_t pixel_row_offset = tile_row * tile::side_length_pix;
75 const std::size_t pixel_col_offset = tile_col * tile::side_length_pix;
76
77 // Copy pixels from source image to tile
78 for (std::size_t pixel_row = 0; pixel_row < tile::side_length_pix; ++pixel_row) {
79 for (std::size_t pixel_col = 0; pixel_col < tile::side_length_pix; ++pixel_col) {
80 const std::size_t src_row = pixel_row_offset + pixel_row;
81 const std::size_t src_col = pixel_col_offset + pixel_col;
82
83 tile.set(pixel_row, pixel_col, img.at(src_row, src_col));
84 }
85 }
86
87 tiles.push_back(std::move(tile));
88 }
89 }
90
91 return tiles;
92 }
93};
94
95} // 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.
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:21
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.
void set(std::size_t i, const PixelType &p)
constexpr std::size_t side_length_pix