23 const std::size_t tiles_per_row = 16;
26 const std::size_t tiles_per_col = (workspace.
capacity() + tiles_per_row - 1) / tiles_per_row;
36 for (std::size_t tile_idx = 0; tile_idx < workspace.
capacity(); ++tile_idx) {
38 const std::size_t tile_row = tile_idx / tiles_per_row;
39 const std::size_t tile_col = tile_idx % tiles_per_row;
46 const auto &canonical_tile = workspace.
tile_at(tile_idx);
55 tile_to_export = canonical_base.
flip(canonical_tile.h_flip(), canonical_tile.v_flip());
59 tile_to_export = canonical_base;
65 const std::size_t dest_row = pixel_row_offset + pixel_row;
66 const std::size_t dest_col = pixel_col_offset + pixel_col;
67 img.
set(dest_row, dest_col, tile_to_export.
at(pixel_row, pixel_col));
86 tiles_.resize(
capacity, transparent_tile);
90 : cursor_{1}, capacity_{capacity}
103 const auto msg = formatter.
format(
111 const std::size_t total_tiles = tiles_per_row * tiles_per_col;
115 const auto msg = formatter.format(
"Image contains {} tiles but capacity is only {}", total_tiles,
capacity);
123 for (std::size_t tile_row = 0; tile_row < tiles_per_col; ++tile_row) {
124 for (std::size_t tile_col = 0; tile_col < tiles_per_row; ++tile_col) {
135 const std::size_t src_row = pixel_row_offset + pixel_row;
136 const std::size_t src_col = pixel_col_offset + pixel_col;
137 pixel_tile.
set(pixel_row, pixel_col, img.
at(src_row, src_col));
143 tiles_.push_back(canonical_tile);
146 if (!canonical_tile.is_transparent()) {
147 const std::size_t tile_index = tiles_.size() - 1;
149 canonical_forms_[base_tile].push_back(tile_index);
157 tiles_.resize(
capacity, transparent_tile);
161 while (cursor_ <
capacity && !tiles_[cursor_].is_transparent()) {
169 if (cursor_ >= capacity_) {
179 tiles_[cursor_] = tile;
183 canonical_forms_[base_tile].push_back(cursor_);
187 while (cursor_ < capacity_ && !tiles_[cursor_].is_transparent()) {
197 auto it = canonical_forms_.find(base_tile);
198 if (it != canonical_forms_.end() && !it->second.empty()) {
199 return it->second.front();
206 if (index >= tiles_.size()) {
207 panic(
"index " + std::to_string(index) +
" >= size " + std::to_string(tiles_.size()));
209 return tiles_.at(index);
214 return export_image_helper(*
this,
false);
219 return export_image_helper(*
this,
true);
224 return cursor_ == capacity_;
A PixelTile representation that stores the canonical (lexicographically minimal) orientation among al...
A template for two-dimensional images with arbitrarily typed pixel values.
std::size_t width() const
Gets the width of this image in pixels.
PixelType at(std::size_t i) const
Fetches the pixel value at a given one-dimensional pixel index.
std::size_t height() const
Gets the height of this image in pixels.
An 8x8 tile backed by literal-array-based per-pixel storage of an arbitrary pixel type.
PixelTile flip(bool h_flip, bool v_flip) const
Creates a flipped copy of this PixelTile.
void set(std::size_t i, const PixelType &p)
PixelType at(std::size_t i) const
bool is_transparent() const
Checks if this entire PixelTile is transparent (intrinsic transparency only).
TextFormatter implementation that strips all styling from text.
virtual std::string format(const std::string &format_str, const std::vector< FormatParam > ¶ms) const
Formats a string with styled parameters using fmtlib syntax.
A workspace for managing canonical IndexPixel tiles destined for tiles.png output.
bool insert_tile(const CanonicalPixelTile< IndexPixel > &tile)
Attempts to insert a non-transparent tile into the workspace at the current cursor position.
bool at_capacity() const
Checks if the workspace has reached capacity and can no longer accept new tile insertions.
std::optional< std::size_t > first_occurrence_of(const CanonicalPixelTile< IndexPixel > &tile) const
Finds the first occurrence index of a given canonical tile in the workspace.
Image< IndexPixel > export_canonical_image() const
Exports the workspace tiles to an Image<IndexPixel> in canonical form (tiles.png format).
CanonicalPixelTile< IndexPixel > tile_at(std::size_t index) const
Retrieves the canonical tile at the specified index in the workspace.
TilesPngWorkspace(std::size_t capacity)
Constructs a TilesPngWorkspace with a specified capacity, initializing all slots with transparent til...
Image< IndexPixel > export_original_image() const
Exports the workspace tiles to an Image<IndexPixel> in original (pre-canonicalization) form.
std::size_t capacity() const
Returns the maximum number of tiles this workspace can hold.
constexpr std::size_t side_length_pix
void panic(const StringViewSourceLoc &s)
Unconditionally terminates the program with a panic message.