From 6dfcdaa0d2866e1e6615390fe761391d6e2f192e Mon Sep 17 00:00:00 2001 From: Pavel Shliak Date: Tue, 3 Dec 2024 23:47:27 +0400 Subject: [PATCH] LibGfx: Unify bitmap set_pixel() using templates --- Libraries/LibGfx/Bitmap.h | 52 ++++++++++++--------------------------- 1 file changed, 16 insertions(+), 36 deletions(-) diff --git a/Libraries/LibGfx/Bitmap.h b/Libraries/LibGfx/Bitmap.h index 63b55049f7d..ef92c4bb5c2 100644 --- a/Libraries/LibGfx/Bitmap.h +++ b/Libraries/LibGfx/Bitmap.h @@ -263,40 +263,21 @@ ALWAYS_INLINE Color Bitmap::get_pixel(int x, int y) const } } -template<> -ALWAYS_INLINE void Bitmap::set_pixel(int x, int y, Color color) +template +ALWAYS_INLINE void Bitmap::set_pixel(int x, int y, Color color) { VERIFY(x >= 0); VERIFY(x < width()); - scanline(y)[x] = color.value(); -} -template<> -ALWAYS_INLINE void Bitmap::set_pixel(int x, int y, Color color) -{ - VERIFY(x >= 0); - VERIFY(x < width()); - scanline(y)[x] = color.value(); // drop alpha -} - -template<> -ALWAYS_INLINE void Bitmap::set_pixel(int x, int y, Color color) -{ - VERIFY(x >= 0); - VERIFY(x < width()); - // FIXME: There's a lot of inaccurately named functions in the Color class right now (RGBA vs BGRA), - // clear those up and then make this more convenient. - auto rgba = (color.alpha() << 24) | (color.blue() << 16) | (color.green() << 8) | color.red(); - scanline(y)[x] = rgba; -} - -template<> -ALWAYS_INLINE void Bitmap::set_pixel(int x, int y, Color color) -{ - VERIFY(x >= 0); - VERIFY(x < width()); - auto rgb = (color.blue() << 16) | (color.green() << 8) | color.red(); - scanline(y)[x] = rgb; + if constexpr (storage_format == StorageFormat::BGRx8888 || storage_format == StorageFormat::BGRA8888) { + scanline(y)[x] = color.value(); + } else if constexpr (storage_format == StorageFormat::RGBA8888) { + scanline(y)[x] = (color.alpha() << 24) | (color.blue() << 16) | (color.green() << 8) | color.red(); + } else if constexpr (storage_format == StorageFormat::RGBx8888) { + scanline(y)[x] = (color.blue() << 16) | (color.green() << 8) | color.red(); + } else { + static_assert(false, "There's a new storage format not in Bitmap::set_pixel"); + } } ALWAYS_INLINE void Bitmap::set_pixel(int x, int y, Color color) @@ -304,19 +285,18 @@ ALWAYS_INLINE void Bitmap::set_pixel(int x, int y, Color color) switch (determine_storage_format(m_format)) { case StorageFormat::BGRx8888: set_pixel(x, y, color); - break; + return; case StorageFormat::BGRA8888: set_pixel(x, y, color); - break; + return; case StorageFormat::RGBA8888: set_pixel(x, y, color); - break; + return; case StorageFormat::RGBx8888: set_pixel(x, y, color); - break; - default: - VERIFY_NOT_REACHED(); + return; } + VERIFY_NOT_REACHED(); } }