34template <SupportsTransparency PixelType,
typename TransparencyPredicate>
38 TransparencyPredicate is_transparent_pred)
41 std::map<ColorIndex, ShapeMask> index_to_mask;
46 const auto pixel = pixel_tile.
at(row, col);
49 if (is_transparent_pred(pixel)) {
56 panic(
"Pixel not found in ColorIndexMap");
62 if (index_to_mask.find(index) == index_to_mask.end()) {
67 index_to_mask[index].
set(row, col);
73 for (
const auto &[index, mask] : index_to_mask) {
74 result.
set(mask, index);
104template <SupportsTransparency ColorType, std::
size_t N = 0,
typename TransparencyPredicate>
110 std::map<ColorType, std::size_t> color_to_index;
112 color_to_index[color] = pal_idx.value();
116 std::array<IndexPixel, tile::size_pix> index_pixels;
119 const auto &pixel = tile.
at(i);
121 if (is_transparent_pred(pixel)) {
127 if (palette.
size() == 0) {
128 panic(
"non-transparent pixel found but palette is empty");
132 auto it = color_to_index.find(pixel);
133 if (it != color_to_index.end()) {
137 panic(
"color not found in palette");
167template <SupportsTransparency ColorType, std::
size_t N = 0>
171 if (palette.
size() == 0) {
172 panic(
"palette is empty");
176 std::array<ColorType, tile::size_pix> color_pixels;
179 const auto &index_pixel = index_tile.
at(i);
183 const std::size_t color_index = index_pixel.color_index();
185 if (color_index == 0) {
187 color_pixels[i] = transparent_color;
191 auto it = index_to_color.find(
PaletteIndex{color_index});
192 if (it == index_to_color.end()) {
194 "color_index " + std::to_string(color_index) +
" out of palette bounds [0, " +
195 std::to_string(palette.
size()) +
")");
197 color_pixels[i] = it->second;
230template <SupportsTransparency PixelType>
231[[nodiscard]] ShapeTile<ColorIndex>
236 pixel_tile, color_index_map, [](
const PixelType &p) {
return p.is_transparent(); });
265template <SupportsTransparency PixelType>
271 pixel_tile, color_index_map, [&extrinsic](
const PixelType &p) {
return p.is_transparent(extrinsic); });
297template <SupportsTransparency PixelType>
298[[nodiscard]] PixelTile<PixelType>
305 std::array<bool, tile::size_pix> pixel_set{};
308 for (
const auto &[mask, index] : shape_tile.
colors()) {
312 panic(
"ColorIndex not found in ColorIndexMap");
315 const PixelType &color = *color_opt;
320 if (mask.get(row, col)) {
324 if (pixel_set[pixel_index]) {
325 panic(
"Overlapping masks detected in ShapeTile - programmer error");
328 result.
set(row, col, color);
329 pixel_set[pixel_index] =
true;
360template <SupportsTransparency PixelType>
361[[nodiscard]] ShapeTile<PixelType>
367 for (
const auto &[mask, index] : shape_tile.
colors()) {
371 panic(
"ColorIndex not found in ColorIndexMap");
374 const PixelType &color = *color_opt;
377 result.
set(mask, color);
400template <SupportsTransparency ColorType, std::
size_t N = 0>
401[[nodiscard]] PixelTile<ColorType>
426template <SupportsTransparency ColorType, std::
size_t N = 0>
450template <SupportsTransparency ColorType, std::
size_t N = 0>
451[[nodiscard]] PixelTile<IndexPixel>
453 requires requires(
const ColorType &c) { c.is_transparent(c); }
456 tile, palette, [](
const ColorType &c) {
return c.is_transparent(); });
476template <SupportsTransparency ColorType, std::
size_t N = 0>
482 tile, palette, [&extrinsic](
const ColorType &c) {
return c.is_transparent(extrinsic); });
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.
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).
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).