diff options
-rw-r--r-- | Userland/Libraries/LibGfx/CMakeLists.txt | 1 | ||||
-rw-r--r-- | Userland/Libraries/LibGfx/Filters/LumaFilter.cpp | 33 | ||||
-rw-r--r-- | Userland/Libraries/LibGfx/Filters/LumaFilter.h | 24 |
3 files changed, 58 insertions, 0 deletions
diff --git a/Userland/Libraries/LibGfx/CMakeLists.txt b/Userland/Libraries/LibGfx/CMakeLists.txt index 8e1663abcd..f4247715e7 100644 --- a/Userland/Libraries/LibGfx/CMakeLists.txt +++ b/Userland/Libraries/LibGfx/CMakeLists.txt @@ -16,6 +16,7 @@ set(SOURCES Emoji.cpp Filters/ColorBlindnessFilter.cpp Filters/FastBoxBlurFilter.cpp + Filters/LumaFilter.cpp FontDatabase.cpp GIFLoader.cpp ICOLoader.cpp diff --git a/Userland/Libraries/LibGfx/Filters/LumaFilter.cpp b/Userland/Libraries/LibGfx/Filters/LumaFilter.cpp new file mode 100644 index 0000000000..bdbbad7da2 --- /dev/null +++ b/Userland/Libraries/LibGfx/Filters/LumaFilter.cpp @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2022, Tobias Christiansen <tobyase@serenityos.org> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include "LumaFilter.h" +namespace Gfx { + +void LumaFilter::apply(u8 lower_bound, u8 upper_bound) +{ + if (upper_bound < lower_bound) + return; + + int height = m_bitmap.height(); + int width = m_bitmap.width(); + + auto format = m_bitmap.format(); + VERIFY(format == BitmapFormat::BGRA8888 || format == BitmapFormat::BGRx8888); + + for (int y = 0; y < height; ++y) { + for (int x = 0; x < width; ++x) { + Color color; + color = m_bitmap.get_pixel(x, y); + + auto luma = color.luminosity(); + if (lower_bound > luma || upper_bound < luma) + m_bitmap.set_pixel(x, y, { 0, 0, 0, color.alpha() }); + } + } +} + +} diff --git a/Userland/Libraries/LibGfx/Filters/LumaFilter.h b/Userland/Libraries/LibGfx/Filters/LumaFilter.h new file mode 100644 index 0000000000..7f907f9ef6 --- /dev/null +++ b/Userland/Libraries/LibGfx/Filters/LumaFilter.h @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2022, Tobias Christiansen <tobyase@serenityos.org> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include <LibGfx/Bitmap.h> + +namespace Gfx { + +class LumaFilter { +public: + LumaFilter(Bitmap& bitmap) + : m_bitmap(bitmap) {}; + + void apply(u8 lower_bound, u8 upper_bound); + +private: + Bitmap& m_bitmap; +}; + +} |