Porytiles
Loading...
Searching...
No Matches
tile_validator.cpp
Go to the documentation of this file.
2
3#include <unordered_set>
4
9
10namespace porytiles2 {
11
13{
14 bool hit_error = false;
15 std::size_t tile_index = 0;
16 for (const auto &tile : tiles) {
17 for (std::size_t row = 0; row < tile::side_length_pix; ++row) {
18 for (std::size_t col = 0; col < tile::side_length_pix; ++col) {
19 const auto &pixel = tile.at(row, col);
20 if (pixel.alpha() != Rgba32::alpha_opaque && pixel.alpha() != Rgba32::alpha_transparent) {
21 hit_error = true;
22 auto [metatile_index, layer, subtile] = metatile::from_tile_index(tile_index);
23 std::vector errors = {format_->format(
24 "{}: invalid alpha channel: {}",
25 FormatParam{metatile::message_header(metatile_index, layer, subtile, row, col, *format_)},
26 FormatParam{std::to_string(pixel.alpha()), Style::bold})};
27 std::vector highlight = tile_printer_->print_metatile_highlight(subtile, row, col, Style::red);
28 std::ranges::copy(highlight, std::back_inserter(errors));
29 diag_->err("alpha-channel-validation", errors);
30 }
31 }
32 }
33 tile_index++;
34 }
35
36 if (hit_error) {
37 return FormattableError{"alpha channel validation failed"};
38 }
39
40 return {};
41}
42
44TileValidator::validate_unique_color_count(const std::vector<PixelTile<Rgba32>> &tiles, const Rgba32 &extrinsic) const
45{
46 bool hit_error = false;
47 std::size_t tile_index = 0;
48 for (const auto &tile : tiles) {
49 std::unordered_set<Rgba32> unique_colors;
50 for (std::size_t row = 0; row < tile::side_length_pix; ++row) {
51 for (std::size_t col = 0; col < tile::side_length_pix; ++col) {
52 const auto &pixel = tile.at(row, col);
53 if (pixel.alpha() != Rgba32::alpha_transparent && pixel != extrinsic) {
54 unique_colors.insert(pixel);
55 }
56
57 if (unique_colors.size() > pal::max_size - 1) {
58 hit_error = true;
59 auto [metatile_index, layer, subtile] = metatile::from_tile_index(tile_index);
60 std::vector errors = {format_->format(
61 "{}: found {}th unique color: {}",
62 FormatParam{metatile::message_header(metatile_index, layer, subtile, row, col, *format_)},
64 FormatParam{pixel.to_jasc_str(), Style::bold})};
65 std::vector highlight = tile_printer_->print_metatile_highlight(subtile, row, col, Style::red);
66 std::ranges::copy(highlight, std::back_inserter(errors));
67 diag_->err("color-count-validation", errors);
68 goto next_tile;
69 }
70 }
71 }
72 next_tile:
73 tile_index++;
74 }
75
76 if (hit_error) {
77 return FormattableError{
78 "unique color constraint violation: tile had more than {} unique non-transparent pixels",
80 }
81
82 return {};
83}
84
86{
87 // TODO: implement
88 return {};
89}
90
91} // namespace porytiles2
A result type that maintains a chainable sequence of errors for debugging and error reporting.
A text parameter with associated styling for formatted output.
General-purpose error implementation with formatted message support.
Definition error.hpp:117
An 8x8 tile backed by literal-array-based per-pixel storage of an arbitrary pixel type.
Represents a 32-bit RGBA color.
Definition rgba32.hpp:21
static constexpr std::uint8_t alpha_opaque
Definition rgba32.hpp:24
static constexpr std::uint8_t alpha_transparent
Definition rgba32.hpp:23
virtual std::string format(const std::string &format_str, const std::vector< FormatParam > &params) const
Formats a string with styled parameters using fmtlib syntax.
virtual std::vector< std::string > print_metatile_highlight(metatile::Subtile subtile, std::size_t row, std::size_t col, Style color) const =0
ChainableResult< void > validate_alpha_channels(const std::vector< PixelTile< Rgba32 > > &tiles) const
ChainableResult< void > generate_precision_loss_warnings(const std::vector< PixelTile< Rgba32 > > &tiles) const
ChainableResult< void > validate_unique_color_count(const std::vector< PixelTile< Rgba32 > > &tiles, const Rgba32 &extrinsic) const
void err(const std::string &tag, const std::string &msg) const
Display a tagged error message.
std::string message_header(std::size_t index, Layer layer, Subtile subtile, std::size_t subtile_row, std::size_t subtile_col, const TextFormatter &format)
Definition metatile.hpp:62
std::tuple< std::size_t, Layer, Subtile > from_tile_index(std::size_t tile_index)
Definition metatile.hpp:52
constexpr std::size_t max_size
Definition palette.hpp:14
constexpr std::size_t side_length_pix
@ bold
Bold text formatting.
@ red
Red text color.