From d70ddc89611ced0ec641b5c0d91c7c6146d29398 Mon Sep 17 00:00:00 2001 From: Nico Weber Date: Tue, 13 Jun 2023 19:23:38 -0400 Subject: [PATCH] LibGfx: Add Bitmap::strip_alpha_channel() Sets all alpha values to 0xff and sets the bitmap's format to BGRx8888. Currently only implemented for BGRA8888 and BGRx8888. --- Userland/Libraries/LibGfx/Bitmap.cpp | 8 ++++++++ Userland/Libraries/LibGfx/Bitmap.h | 3 +++ 2 files changed, 11 insertions(+) diff --git a/Userland/Libraries/LibGfx/Bitmap.cpp b/Userland/Libraries/LibGfx/Bitmap.cpp index 7133e3ff50..30d1fa5ce8 100644 --- a/Userland/Libraries/LibGfx/Bitmap.cpp +++ b/Userland/Libraries/LibGfx/Bitmap.cpp @@ -546,6 +546,14 @@ Bitmap::~Bitmap() delete[] m_palette; } +void Bitmap::strip_alpha_channel() +{ + VERIFY(m_format == BitmapFormat::BGRA8888 || m_format == BitmapFormat::BGRx8888); + for (ARGB32& pixel : *this) + pixel = 0xff000000 | (pixel & 0xffffff); + m_format = BitmapFormat::BGRx8888; +} + void Bitmap::set_mmap_name([[maybe_unused]] DeprecatedString const& name) { VERIFY(m_needs_munmap); diff --git a/Userland/Libraries/LibGfx/Bitmap.h b/Userland/Libraries/LibGfx/Bitmap.h index 1243636abd..128015047c 100644 --- a/Userland/Libraries/LibGfx/Bitmap.h +++ b/Userland/Libraries/LibGfx/Bitmap.h @@ -212,6 +212,9 @@ public: [[nodiscard]] bool has_alpha_channel() const { return m_format == BitmapFormat::BGRA8888 || m_format == BitmapFormat::RGBA8888; } [[nodiscard]] BitmapFormat format() const { return m_format; } + // Call only for BGRx8888 and BGRA8888 bitmaps. + void strip_alpha_channel(); + void set_mmap_name(DeprecatedString const&); [[nodiscard]] static constexpr size_t size_in_bytes(size_t pitch, int physical_height) { return pitch * physical_height; }