mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 14:07:46 +00:00
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:
parent
150b3cf378
commit
dcbddb4f8c
2 changed files with 73 additions and 25 deletions
|
@ -16,14 +16,16 @@ BucketTool::~BucketTool()
|
||||||
|
|
||||||
static void flood_fill(GraphicsBitmap& bitmap, const Point& start_position, Color target_color, Color fill_color)
|
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<Point> queue;
|
||||||
queue.enqueue(Point(start_position));
|
queue.enqueue(Point(start_position));
|
||||||
while (!queue.is_empty()) {
|
while (!queue.is_empty()) {
|
||||||
auto position = queue.dequeue();
|
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;
|
continue;
|
||||||
bitmap.set_pixel(position, fill_color);
|
bitmap.set_pixel<GraphicsBitmap::Format::RGB32>(position.x(), position.y(), fill_color);
|
||||||
|
|
||||||
if (position.x() != 0)
|
if (position.x() != 0)
|
||||||
queue.enqueue(position.translated(-1, 0));
|
queue.enqueue(position.translated(-1, 0));
|
||||||
|
|
|
@ -51,41 +51,27 @@ public:
|
||||||
Color palette_color(byte index) const { return Color::from_rgba(m_palette[index]); }
|
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(); }
|
void set_palette_color(byte index, Color color) { m_palette[index] = color.value(); }
|
||||||
|
|
||||||
|
template<Format>
|
||||||
Color get_pixel(int x, int y) const
|
Color get_pixel(int x, int y) const
|
||||||
{
|
{
|
||||||
switch (m_format) {
|
ASSERT_NOT_REACHED();
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Color get_pixel(int x, int y) const;
|
||||||
|
|
||||||
Color get_pixel(const Point& position) const
|
Color get_pixel(const Point& position) const
|
||||||
{
|
{
|
||||||
return get_pixel(position.x(), position.y());
|
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) {
|
ASSERT_NOT_REACHED();
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void set_pixel(int x, int y, Color);
|
||||||
|
|
||||||
void set_pixel(const Point& position, Color color)
|
void set_pixel(const Point& position, Color color)
|
||||||
{
|
{
|
||||||
set_pixel(position.x(), position.y(), color);
|
set_pixel(position.x(), position.y(), color);
|
||||||
|
@ -126,3 +112,63 @@ inline byte* GraphicsBitmap::bits(int y)
|
||||||
{
|
{
|
||||||
return reinterpret_cast<byte*>(scanline(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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue