From dcbddb4f8cfc73b0e101d30a5f417fa635c53d33 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sat, 15 Jun 2019 11:06:02 +0200 Subject: [PATCH] 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. --- Applications/PaintBrush/BucketTool.cpp | 6 +- SharedGraphics/GraphicsBitmap.h | 92 +++++++++++++++++++------- 2 files changed, 73 insertions(+), 25 deletions(-) diff --git a/Applications/PaintBrush/BucketTool.cpp b/Applications/PaintBrush/BucketTool.cpp index e3fdad5e16..c2b6353418 100644 --- a/Applications/PaintBrush/BucketTool.cpp +++ b/Applications/PaintBrush/BucketTool.cpp @@ -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 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(position.x(), position.y()) != target_color) continue; - bitmap.set_pixel(position, fill_color); + bitmap.set_pixel(position.x(), position.y(), fill_color); if (position.x() != 0) queue.enqueue(position.translated(-1, 0)); diff --git a/SharedGraphics/GraphicsBitmap.h b/SharedGraphics/GraphicsBitmap.h index c73dca70dd..162517de2e 100644 --- a/SharedGraphics/GraphicsBitmap.h +++ b/SharedGraphics/GraphicsBitmap.h @@ -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 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 + 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(scanline(y)); } + +template<> +inline Color GraphicsBitmap::get_pixel(int x, int y) const +{ + return Color::from_rgb(scanline(y)[x]); +} + +template<> +inline Color GraphicsBitmap::get_pixel(int x, int y) const +{ + return Color::from_rgba(scanline(y)[x]); +} + +template<> +inline Color GraphicsBitmap::get_pixel(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(x, y); + case Format::RGBA32: + return get_pixel(x, y); + case Format::Indexed8: + return get_pixel(x, y); + default: + ASSERT_NOT_REACHED(); + } +} + +template<> +inline void GraphicsBitmap::set_pixel(int x, int y, Color color) +{ + scanline(y)[x] = color.value(); +} + +template<> +inline void GraphicsBitmap::set_pixel(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(x, y, color); + break; + case Format::RGBA32: + set_pixel(x, y, color); + break; + case Format::Indexed8: + ASSERT_NOT_REACHED(); + default: + ASSERT_NOT_REACHED(); + } +}