GraphicsBitmap: Provide templated versions of get_pixel() and set_pixel().

If we already know the bitmap format used, we can use these functions to
bypass the format checks and go straight to pixel manipulation.
This commit is contained in:
Andreas Kling 2019-06-15 11:06:02 +02:00
parent 150b3cf378
commit dcbddb4f8c
Notes: sideshowbarker 2024-07-19 13:36:01 +09:00
2 changed files with 73 additions and 25 deletions

View file

@ -16,14 +16,16 @@ BucketTool::~BucketTool()
static void flood_fill(GraphicsBitmap& bitmap, const Point& start_position, Color target_color, Color fill_color)
{
ASSERT(bitmap.format() == GraphicsBitmap::Format::RGB32);
Queue<Point> queue;
queue.enqueue(Point(start_position));
while (!queue.is_empty()) {
auto position = queue.dequeue();
if (bitmap.get_pixel(position) != target_color)
if (bitmap.get_pixel<GraphicsBitmap::Format::RGB32>(position.x(), position.y()) != target_color)
continue;
bitmap.set_pixel(position, fill_color);
bitmap.set_pixel<GraphicsBitmap::Format::RGB32>(position.x(), position.y(), fill_color);
if (position.x() != 0)
queue.enqueue(position.translated(-1, 0));

View file

@ -51,41 +51,27 @@ public:
Color palette_color(byte index) const { return Color::from_rgba(m_palette[index]); }
void set_palette_color(byte index, Color color) { m_palette[index] = color.value(); }
template<Format>
Color get_pixel(int x, int y) const
{
switch (m_format) {
case Format::RGB32:
return Color::from_rgb(scanline(y)[x]);
case Format::RGBA32:
return Color::from_rgba(scanline(y)[x]);
case Format::Indexed8:
return Color::from_rgba(m_palette[bits(y)[x]]);
default:
ASSERT_NOT_REACHED();
}
ASSERT_NOT_REACHED();
}
Color get_pixel(int x, int y) const;
Color get_pixel(const Point& position) const
{
return get_pixel(position.x(), position.y());
}
void set_pixel(int x, int y, Color color)
template<Format>
void set_pixel(int x, int y, Color)
{
switch (m_format) {
case Format::RGB32:
scanline(y)[x] = color.value();
break;
case Format::RGBA32:
scanline(y)[x] = color.value();
break;
case Format::Indexed8:
ASSERT_NOT_REACHED();
default:
ASSERT_NOT_REACHED();
}
ASSERT_NOT_REACHED();
}
void set_pixel(int x, int y, Color);
void set_pixel(const Point& position, Color color)
{
set_pixel(position.x(), position.y(), color);
@ -126,3 +112,63 @@ inline byte* GraphicsBitmap::bits(int y)
{
return reinterpret_cast<byte*>(scanline(y));
}
template<>
inline Color GraphicsBitmap::get_pixel<GraphicsBitmap::Format::RGB32>(int x, int y) const
{
return Color::from_rgb(scanline(y)[x]);
}
template<>
inline Color GraphicsBitmap::get_pixel<GraphicsBitmap::Format::RGBA32>(int x, int y) const
{
return Color::from_rgba(scanline(y)[x]);
}
template<>
inline Color GraphicsBitmap::get_pixel<GraphicsBitmap::Format::Indexed8>(int x, int y) const
{
return Color::from_rgba(m_palette[bits(y)[x]]);
}
inline Color GraphicsBitmap::get_pixel(int x, int y) const
{
switch (m_format) {
case Format::RGB32:
return get_pixel<Format::RGB32>(x, y);
case Format::RGBA32:
return get_pixel<Format::RGBA32>(x, y);
case Format::Indexed8:
return get_pixel<Format::Indexed8>(x, y);
default:
ASSERT_NOT_REACHED();
}
}
template<>
inline void GraphicsBitmap::set_pixel<GraphicsBitmap::Format::RGB32>(int x, int y, Color color)
{
scanline(y)[x] = color.value();
}
template<>
inline void GraphicsBitmap::set_pixel<GraphicsBitmap::Format::RGBA32>(int x, int y, Color color)
{
scanline(y)[x] = color.value();
}
inline void GraphicsBitmap::set_pixel(int x, int y, Color color)
{
switch (m_format) {
case Format::RGB32:
set_pixel<Format::RGB32>(x, y, color);
break;
case Format::RGBA32:
set_pixel<Format::RGBA32>(x, y, color);
break;
case Format::Indexed8:
ASSERT_NOT_REACHED();
default:
ASSERT_NOT_REACHED();
}
}