From 2502a88e49a886458366145e99d8ac3d0ab0293c Mon Sep 17 00:00:00 2001 From: Xavier Defrang Date: Fri, 7 Jan 2022 23:45:15 +0100 Subject: [PATCH] LibGfx: Implement Grayscale/Invert filters as ColorFilter --- .../LibGfx/Filters/GrayscaleFilter.h | 29 +++---------------- .../Libraries/LibGfx/Filters/InvertFilter.h | 29 +++---------------- 2 files changed, 8 insertions(+), 50 deletions(-) diff --git a/Userland/Libraries/LibGfx/Filters/GrayscaleFilter.h b/Userland/Libraries/LibGfx/Filters/GrayscaleFilter.h index c5c626fb78..8671570c2c 100644 --- a/Userland/Libraries/LibGfx/Filters/GrayscaleFilter.h +++ b/Userland/Libraries/LibGfx/Filters/GrayscaleFilter.h @@ -6,40 +6,19 @@ #pragma once -#include +#include namespace Gfx { -class GrayscaleFilter : public Filter { +class GrayscaleFilter : public ColorFilter { public: GrayscaleFilter() { } virtual ~GrayscaleFilter() { } virtual char const* class_name() const override { return "GrayscaleFilter"; } - virtual void apply(Bitmap& target_bitmap, IntRect const& target_rect, Bitmap const& source_bitmap, IntRect const& source_rect) override - { - // source_rect should be describing the pixels that can be accessed - // to apply this filter, while target_rect should describe the area - // where to apply the filter on. - VERIFY(source_rect.size() == target_rect.size()); - VERIFY(target_bitmap.rect().contains(target_rect)); - VERIFY(source_bitmap.rect().contains(source_rect)); - - for (auto y = 0; y < source_rect.height(); ++y) { - ssize_t source_y = y + source_rect.y(); - ssize_t target_y = y + target_rect.y(); - for (auto x = 0; x < source_rect.width(); ++x) { - ssize_t source_x = x + source_rect.x(); - ssize_t target_x = x + target_rect.x(); - - auto source_pixel = source_bitmap.get_pixel(source_x, source_y); - auto target_color = source_pixel.to_grayscale(); - - target_bitmap.set_pixel(target_x, target_y, target_color); - } - } - } +protected: + Color convert_color(Color original) override { return original.to_grayscale(); }; }; } diff --git a/Userland/Libraries/LibGfx/Filters/InvertFilter.h b/Userland/Libraries/LibGfx/Filters/InvertFilter.h index dc24501069..5c50dc0098 100644 --- a/Userland/Libraries/LibGfx/Filters/InvertFilter.h +++ b/Userland/Libraries/LibGfx/Filters/InvertFilter.h @@ -6,40 +6,19 @@ #pragma once -#include +#include namespace Gfx { -class InvertFilter : public Filter { +class InvertFilter : public ColorFilter { public: InvertFilter() { } virtual ~InvertFilter() { } virtual char const* class_name() const override { return "InvertFilter"; } - virtual void apply(Bitmap& target_bitmap, IntRect const& target_rect, Bitmap const& source_bitmap, IntRect const& source_rect) override - { - // source_rect should be describing the pixels that can be accessed - // to apply this filter, while target_rect should describe the area - // where to apply the filter on. - VERIFY(source_rect.size() == target_rect.size()); - VERIFY(target_bitmap.rect().contains(target_rect)); - VERIFY(source_bitmap.rect().contains(source_rect)); - - for (auto y = 0; y < source_rect.height(); ++y) { - ssize_t source_y = y + source_rect.y(); - ssize_t target_y = y + target_rect.y(); - for (auto x = 0; x < source_rect.width(); ++x) { - ssize_t source_x = x + source_rect.x(); - ssize_t target_x = x + target_rect.x(); - - auto source_pixel = source_bitmap.get_pixel(source_x, source_y); - auto target_color = source_pixel.inverted(); - - target_bitmap.set_pixel(target_x, target_y, target_color); - } - } - } +protected: + Color convert_color(Color original) override { return original.inverted(); }; }; }