1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 03:37:45 +00:00

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.
This commit is contained in:
Tobias Christiansen 2022-01-03 13:13:22 +01:00 committed by Idan Horowitz
parent 06ae5b3536
commit e4b7d38e18
3 changed files with 58 additions and 0 deletions

View file

@ -16,6 +16,7 @@ set(SOURCES
Emoji.cpp
Filters/ColorBlindnessFilter.cpp
Filters/FastBoxBlurFilter.cpp
Filters/LumaFilter.cpp
FontDatabase.cpp
GIFLoader.cpp
ICOLoader.cpp

View file

@ -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() });
}
}
}
}

View file

@ -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;
};
}