Преглед изворни кода

LibGfx: Add LumaFilter

This allows you to specify a luminosity range, all pixels that fall
outside this range are set to black, the others are untouched.
Tobias Christiansen пре 3 година
родитељ
комит
e4b7d38e18

+ 1 - 0
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

+ 33 - 0
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() });
+        }
+    }
+}
+
+}

+ 24 - 0
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;
+};
+
+}