33template <SupportsTransparency PixelType,
typename TransparencyPredicate>
37 TransparencyPredicate is_transparent_pred)
40 std::map<ColorIndex, ShapeMask> index_to_mask;
45 const auto pixel = pixel_tile.
at(row, col);
48 if (is_transparent_pred(pixel)) {
55 panic(
"Pixel not found in ColorIndexMap");
61 if (index_to_mask.find(index) == index_to_mask.end()) {
66 index_to_mask[index].
set(row, col);
72 for (
const auto &[index, mask] : index_to_mask) {
73 result.
set(mask, index);
101template <SupportsTransparency ColorType, std::
size_t N = 0,
typename TransparencyPredicate>
107 std::map<ColorType, std::size_t> color_to_index;
109 color_to_index[color] = palette_idx.value();
113 std::array<IndexPixel, tile::size_pix> index_pixels;
116 const auto &pixel = tile.
at(i);
118 if (is_transparent_pred(pixel)) {
124 if (palette.
size() == 0) {
125 panic(
"non-transparent pixel found but palette is empty");
129 auto it = color_to_index.find(pixel);
130 if (it != color_to_index.end()) {
134 panic(
"color not found in palette");
162template <SupportsTransparency ColorType, std::
size_t N = 0>
166 if (palette.
size() == 0) {
167 panic(
"palette is empty");
171 std::array<ColorType, tile::size_pix> color_pixels;
174 const auto &index_pixel = index_tile.
at(i);
178 const std::size_t color_index = index_pixel.color_index();
180 if (color_index == 0) {
182 color_pixels[i] = transparent_color;
186 auto it = index_to_color.find(
PaletteIndex{color_index});
187 if (it == index_to_color.end()) {
189 "color_index " + std::to_string(color_index) +
" out of palette bounds [0, " +
190 std::to_string(palette.
size()) +
")");
192 color_pixels[i] = it->second;
223template <SupportsTransparency PixelType>
224[[nodiscard]] ShapeTile<ColorIndex>
229 pixel_tile, color_index_map, [](
const PixelType &p) {
return p.is_transparent(); });
256template <SupportsTransparency PixelType>
262 pixel_tile, color_index_map, [&extrinsic](
const PixelType &p) {
return p.is_transparent(extrinsic); });
286template <SupportsTransparency PixelType>
287[[nodiscard]] PixelTile<PixelType>
294 std::array<bool, tile::size_pix> pixel_set{};
297 for (
const auto &[mask, index] : shape_tile.
colors()) {
301 panic(
"ColorIndex not found in ColorIndexMap");
304 const PixelType &color = *color_opt;
309 if (mask.get(row, col)) {
313 if (pixel_set[pixel_index]) {
314 panic(
"Overlapping masks detected in ShapeTile - programmer error");
317 result.
set(row, col, color);
318 pixel_set[pixel_index] =
true;
347template <SupportsTransparency PixelType>
348[[nodiscard]] ShapeTile<PixelType>
354 for (
const auto &[mask, index] : shape_tile.
colors()) {
358 panic(
"ColorIndex not found in ColorIndexMap");
361 const PixelType &color = *color_opt;
364 result.
set(mask, color);
385template <SupportsTransparency ColorType, std::
size_t N = 0>
386[[nodiscard]] PixelTile<ColorType>
409template <SupportsTransparency ColorType, std::
size_t N = 0>
431template <SupportsTransparency ColorType, std::
size_t N = 0>
432[[nodiscard]] PixelTile<IndexPixel>
434 requires requires(
const ColorType &c) { c.is_transparent(c); }
437 tile, palette, [](
const ColorType &c) {
return c.is_transparent(); });
455template <SupportsTransparency ColorType, std::
size_t N = 0>
461 tile, palette, [&extrinsic](
const ColorType &c) {
return c.is_transparent(extrinsic); });
478template <SupportsTransparency ColorType, std::
size_t N = 0>
484 return static_cast<const PixelTile<ColorType> &
>(
canonical);
A bidirectional mapping between pixel color values and sequential integer indices.
std::optional< ColorIndex > index_at_color(const PixelType &color) const
Retrieves the index associated with a given color.
std::optional< PixelType > color_at_index(ColorIndex index) const
Retrieves the color associated with a given index.
Represents a color index value for palette operations.
Represents an indexed color pixel.
Represents a palette index value within a particular palette.
A generic palette container for colors that support transparency checking.
std::map< PaletteIndex, ColorType > index_to_color_map() const
Creates a map from palette indices to their colors.
std::size_t size() const
Returns the number of slots in the palette.
std::map< ColorType, PaletteIndex > color_to_index_map() const
Creates a map from colors to their palette indices.
An 8x8 tile backed by literal-array-based per-pixel storage of an arbitrary pixel type.
bool is_transparent() const
Checks if this entire PixelTile is transparent (intrinsic transparency only).
PixelType at(std::size_t i) const
void set(std::size_t i, const PixelType &p)
Represents which pixels in an 8x8 tile are non-transparent.
void set(std::size_t row, std::size_t col)
Sets the bit at the specified row and column to 1.
An 8x8 tile backed by mask-based storage that maps shape regions to pixel values.
void set(const ShapeMask &mask, const PixelType &color)
Sets or updates the pixel value for a specific shape mask.
bool is_transparent() const
Checks if this entire ShapeTile is transparent.
const std::map< ShapeMask, PixelType > & colors() const
Returns the internal map of shape masks to pixel values.
PixelTile< IndexPixel > index_tile_from_color_tile_impl(const PixelTile< ColorType > &tile, const Palette< ColorType, N > &palette, TransparencyPredicate is_transparent_pred)
Helper function implementing the core color-to-index tile conversion logic.
ShapeTile< ColorIndex > from_pixel_tile_impl(const PixelTile< PixelType > &pixel_tile, const ColorIndexMap< PixelType > &color_index_map, TransparencyPredicate is_transparent_pred)
Helper function implementing the core PixelTile to ShapeTile conversion logic.
PixelTile< ColorType > color_tile_from_index_tile_impl(const PixelTile< IndexPixel > &index_tile, const Palette< ColorType, N > &palette, const ColorType &transparent_color)
Helper function implementing the core index-to-color tile conversion logic.
constexpr std::size_t side_length_pix
constexpr std::size_t size_pix
void panic(const StringViewSourceLoc &s)
Unconditionally terminates the program with a panic message.
PixelTile< PixelType > from_shape_tile(const ShapeTile< ColorIndex > &shape_tile, const ColorIndexMap< PixelType > &color_index_map)
Converts a ShapeTile<ColorIndex> to a PixelTile using a ColorIndexMap.
@ canonical
Export tiles in canonical form without applying flip transformations.
PixelTile< IndexPixel > index_tile_from_color_tile(const PixelTile< ColorType > &tile, const Palette< ColorType, N > &palette)
Converts a PixelTile<ColorType> to indexed form using a palette (intrinsic transparency only).
PixelTile< ColorType > canonical_color_tile_from_index_tile(const PixelTile< IndexPixel > &index_tile, const Palette< ColorType, N > &palette, const ColorType &extrinsic)
Converts a PixelTile<IndexPixel> to its canonical color form using a palette (extrinsic transparency)...
ShapeTile< ColorIndex > from_pixel_tile(const PixelTile< PixelType > &pixel_tile, const ColorIndexMap< PixelType > &color_index_map)
Converts a PixelTile to a ShapeTile<ColorIndex> using a ColorIndexMap (intrinsic transparency).
ShapeTile< PixelType > shape_tile_to_pixel_colors(const ShapeTile< ColorIndex > &shape_tile, const ColorIndexMap< PixelType > &color_index_map)
Converts a ShapeTile<ColorIndex> to a ShapeTile<PixelType> using a ColorIndexMap.
PixelTile< ColorType > color_tile_from_index_tile(const PixelTile< IndexPixel > &index_tile, const Palette< ColorType, N > &palette)
Converts a PixelTile<IndexPixel> to a PixelTile<ColorType> using a palette (intrinsic transparency).