Porytiles
Loading...
Searching...
No Matches
image_tileizer.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cstddef>
4#include <vector>
5
6#include "fmt/format.h"
7
11
12namespace porytiles2 {
13
28template <typename T>
30 public:
56 {
57 // Validate that image dimensions are multiples of tile size
58 if (img.width() % tile::side_length_pix != 0 || img.height() % tile::side_length_pix != 0) {
59 return FormattableError{fmt::format(
60 "image dimensions must be a multiple of {}, got {}x{}",
62 img.width(),
63 img.height())};
64 }
65
66 const std::size_t tiles_per_row = img.width() / tile::side_length_pix;
67 const std::size_t tiles_per_col = img.height() / tile::side_length_pix;
68 const std::size_t total_tiles = tiles_per_row * tiles_per_col;
69
70 std::vector<PixelTile<T>> tiles;
71 tiles.reserve(total_tiles);
72
73 // Process each tile region
74 for (std::size_t tile_row = 0; tile_row < tiles_per_col; ++tile_row) {
75 for (std::size_t tile_col = 0; tile_col < tiles_per_row; ++tile_col) {
76 PixelTile<T> tile;
77
78 // Calculate pixel offsets for this tile
79 const std::size_t pixel_row_offset = tile_row * tile::side_length_pix;
80 const std::size_t pixel_col_offset = tile_col * tile::side_length_pix;
81
82 // Copy pixels from source image to tile
83 for (std::size_t pixel_row = 0; pixel_row < tile::side_length_pix; ++pixel_row) {
84 for (std::size_t pixel_col = 0; pixel_col < tile::side_length_pix; ++pixel_col) {
85 const std::size_t src_row = pixel_row_offset + pixel_row;
86 const std::size_t src_col = pixel_col_offset + pixel_col;
87
88 tile.set(pixel_row, pixel_col, img.at(src_row, src_col));
89 }
90 }
91
92 tiles.push_back(std::move(tile));
93 }
94 }
95
96 return tiles;
97 }
98};
99
100} // namespace porytiles2
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:117
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:24
std::size_t width() const
Gets the width of this image in pixels.
Definition image.hpp:108
PixelType at(std::size_t i) const
Fetches the pixel value at a given one-dimensional pixel index.
Definition image.hpp:45
std::size_t height() const
Gets the height of this image in pixels.
Definition image.hpp:118
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