summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTobias Christiansen <tobyase@serenityos.org>2022-01-02 21:56:31 +0100
committerIdan Horowitz <idan.horowitz@gmail.com>2022-01-04 21:41:14 +0200
commitb0ab97916901fdb744adee308a9141e5109dccdb (patch)
tree2becaa07a4e0d3154c9b0ab40fdef3f977706445
parent902b8f158365e6f36790d0df2c03750c1ecd03bb (diff)
downloadserenity-b0ab97916901fdb744adee308a9141e5109dccdb.zip
LibGfx: Add BitmapFormat::BGRx8888 support to the FastBoxBlurFilter
1268b39ba introduced template specialization when reading/writing to/from the bitmap that increased the speed of the filter. To keep that while supporting more than one BitmapFormat, the appropriate {get,set}_pixel<>() functions are detemined and stored in Function<>s.
-rw-r--r--Userland/Libraries/LibGfx/Filters/FastBoxBlurFilter.cpp28
1 files changed, 23 insertions, 5 deletions
diff --git a/Userland/Libraries/LibGfx/Filters/FastBoxBlurFilter.cpp b/Userland/Libraries/LibGfx/Filters/FastBoxBlurFilter.cpp
index 6a543918dd..10f6181576 100644
--- a/Userland/Libraries/LibGfx/Filters/FastBoxBlurFilter.cpp
+++ b/Userland/Libraries/LibGfx/Filters/FastBoxBlurFilter.cpp
@@ -4,6 +4,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
+#include <AK/Function.h>
#include <AK/Vector.h>
#include <LibGfx/Filters/FastBoxBlurFilter.h>
@@ -31,7 +32,24 @@ FastBoxBlurFilter::FastBoxBlurFilter(Bitmap& bitmap)
void FastBoxBlurFilter::apply_single_pass(int radius)
{
VERIFY(radius >= 0);
- VERIFY(m_bitmap.format() == BitmapFormat::BGRA8888);
+
+ auto format = m_bitmap.format();
+ VERIFY(format == BitmapFormat::BGRA8888 || format == BitmapFormat::BGRx8888);
+
+ Function<Color(int, int)> get_pixel_function;
+ Function<void(int, int, Color)> set_pixel_function;
+ switch (format) {
+ case BitmapFormat::BGRx8888:
+ get_pixel_function = [&](int x, int y) { return m_bitmap.get_pixel<StorageFormat::BGRx8888>(x, y); };
+ set_pixel_function = [&](int x, int y, Color color) { return m_bitmap.set_pixel<StorageFormat::BGRx8888>(x, y, color); };
+ break;
+ case BitmapFormat::BGRA8888:
+ get_pixel_function = [&](int x, int y) { return m_bitmap.get_pixel<StorageFormat::BGRA8888>(x, y); };
+ set_pixel_function = [&](int x, int y, Color color) { return m_bitmap.set_pixel<StorageFormat::BGRA8888>(x, y, color); };
+ break;
+ default:
+ VERIFY_NOT_REACHED();
+ }
int height = m_bitmap.height();
int width = m_bitmap.width();
@@ -57,7 +75,7 @@ void FastBoxBlurFilter::apply_single_pass(int radius)
// Setup sliding window
for (int i = -radius; i <= radius; ++i) {
- auto color_at_px = m_bitmap.get_pixel<StorageFormat::BGRA8888>(clamp(i, 0, width - 1), y);
+ auto color_at_px = get_pixel_function(clamp(i, 0, width - 1), y);
sum_red += red_value(color_at_px);
sum_green += green_value(color_at_px);
sum_blue += blue_value(color_at_px);
@@ -73,8 +91,8 @@ void FastBoxBlurFilter::apply_single_pass(int radius)
auto leftmost_x_coord = max(x - radius, 0);
auto rightmost_x_coord = min(x + radius + 1, width - 1);
- auto leftmost_x_color = m_bitmap.get_pixel<StorageFormat::BGRA8888>(leftmost_x_coord, y);
- auto rightmost_x_color = m_bitmap.get_pixel<StorageFormat::BGRA8888>(rightmost_x_coord, y);
+ auto leftmost_x_color = get_pixel_function(leftmost_x_coord, y);
+ auto rightmost_x_color = get_pixel_function(rightmost_x_coord, y);
sum_red -= red_value(leftmost_x_color);
sum_red += red_value(rightmost_x_color);
@@ -110,7 +128,7 @@ void FastBoxBlurFilter::apply_single_pass(int radius)
sum_blue / div,
sum_alpha / div);
- m_bitmap.set_pixel<StorageFormat::BGRA8888>(x, y, color);
+ set_pixel_function(x, y, color);
auto topmost_y_coord = max(y - radius, 0);
auto bottommost_y_coord = min(y + radius + 1, height - 1);