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

LibGfx: Add Gfx::Bitmap::apply_mask()

This allows applying an alpha or luminance mask to a bitmap (of the same
size) inplace.
This commit is contained in:
MacDue 2023-09-03 19:24:00 +01:00 committed by Andreas Kling
parent 93e230ba58
commit b69e8ee893
2 changed files with 26 additions and 0 deletions

View file

@ -344,6 +344,25 @@ ErrorOr<NonnullRefPtr<Gfx::Bitmap>> Bitmap::flipped(Gfx::Orientation orientation
return new_bitmap;
}
void Bitmap::apply_mask(Gfx::Bitmap const& mask, MaskKind mask_kind)
{
VERIFY(size() == mask.size());
for (int y = 0; y < height(); y++) {
for (int x = 0; x < width(); x++) {
auto color = get_pixel(x, y);
auto mask_color = mask.get_pixel(x, y);
if (mask_kind == MaskKind::Luminance) {
color = color.with_alpha(color.alpha() * mask_color.alpha() * mask_color.luminosity() / (255 * 255));
} else {
VERIFY(mask_kind == MaskKind::Alpha);
color = color.with_alpha(color.alpha() * mask_color.alpha() / 255);
}
set_pixel(x, y, color);
}
}
}
ErrorOr<NonnullRefPtr<Gfx::Bitmap>> Bitmap::scaled(int sx, int sy) const
{
VERIFY(sx >= 0 && sy >= 0);

View file

@ -133,6 +133,13 @@ public:
ErrorOr<NonnullRefPtr<Gfx::Bitmap>> inverted() const;
enum class MaskKind {
Alpha,
Luminance
};
void apply_mask(Gfx::Bitmap const& mask, MaskKind);
~Bitmap();
[[nodiscard]] u8* scanline_u8(int physical_y);