diff options
author | MacDue <macdue@dueutil.tech> | 2022-06-20 12:23:57 +0100 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2022-06-23 19:13:24 +0100 |
commit | 30a0ed66780250edcb7cb4f2b6bd35a3b08af749 (patch) | |
tree | 590fecf943018553e19e7a12daf3543513801c6e | |
parent | bb48a61d50ed1f1a9c4e0bdec9debfd2d0ad3b22 (diff) | |
download | serenity-30a0ed66780250edcb7cb4f2b6bd35a3b08af749.zip |
LibGfx: Avoid AK::Function indirection in FastBoxBlurFilter + flatten it
This dropped a few percent when profiling Lubrsi's test.
-rw-r--r-- | Userland/Libraries/LibGfx/Filters/FastBoxBlurFilter.cpp | 49 |
1 files changed, 26 insertions, 23 deletions
diff --git a/Userland/Libraries/LibGfx/Filters/FastBoxBlurFilter.cpp b/Userland/Libraries/LibGfx/Filters/FastBoxBlurFilter.cpp index 7cbec6ea7c..66f18136ff 100644 --- a/Userland/Libraries/LibGfx/Filters/FastBoxBlurFilter.cpp +++ b/Userland/Libraries/LibGfx/Filters/FastBoxBlurFilter.cpp @@ -33,30 +33,9 @@ void FastBoxBlurFilter::apply_single_pass(size_t radius) apply_single_pass(radius, radius); } -// Based on the super fast blur algorithm by Quasimondo, explored here: https://stackoverflow.com/questions/21418892/understanding-super-fast-blur-algorithm -void FastBoxBlurFilter::apply_single_pass(size_t radius_x, size_t radius_y) +template<typename GetPixelFunction, typename SetPixelFunction> +static void do_single_pass(int width, int height, size_t radius_x, size_t radius_y, GetPixelFunction get_pixel_function, SetPixelFunction set_pixel_function) { - 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(); - int div_x = 2 * radius_x + 1; int div_y = 2 * radius_y + 1; @@ -149,6 +128,30 @@ void FastBoxBlurFilter::apply_single_pass(size_t radius_x, size_t radius_y) } } +// Based on the super fast blur algorithm by Quasimondo, explored here: https://stackoverflow.com/questions/21418892/understanding-super-fast-blur-algorithm +FLATTEN void FastBoxBlurFilter::apply_single_pass(size_t radius_x, size_t radius_y) +{ + auto format = m_bitmap.format(); + VERIFY(format == BitmapFormat::BGRA8888 || format == BitmapFormat::BGRx8888); + + switch (format) { + case BitmapFormat::BGRx8888: + do_single_pass( + m_bitmap.width(), m_bitmap.height(), radius_x, radius_y, + [&](int x, int y) { return m_bitmap.get_pixel<StorageFormat::BGRx8888>(x, y); }, + [&](int x, int y, Color color) { return m_bitmap.set_pixel<StorageFormat::BGRx8888>(x, y, color); }); + break; + case BitmapFormat::BGRA8888: + do_single_pass( + m_bitmap.width(), m_bitmap.height(), radius_x, radius_y, + [&](int x, int y) { return m_bitmap.get_pixel<StorageFormat::BGRA8888>(x, y); }, + [&](int x, int y, Color color) { return m_bitmap.set_pixel<StorageFormat::BGRA8888>(x, y, color); }); + break; + default: + VERIFY_NOT_REACHED(); + } +} + // Math from here: http://blog.ivank.net/fastest-gaussian-blur.html void FastBoxBlurFilter::apply_three_passes(size_t radius) { |