浏览代码

LibGfx: Implement Grayscale/Invert filters as ColorFilter

Xavier Defrang 3 年之前
父节点
当前提交
2502a88e49
共有 2 个文件被更改,包括 8 次插入50 次删除
  1. 4 25
      Userland/Libraries/LibGfx/Filters/GrayscaleFilter.h
  2. 4 25
      Userland/Libraries/LibGfx/Filters/InvertFilter.h

+ 4 - 25
Userland/Libraries/LibGfx/Filters/GrayscaleFilter.h

@@ -6,40 +6,19 @@
 
 #pragma once
 
-#include <LibGfx/Filters/Filter.h>
+#include <LibGfx/Filters/ColorFilter.h>
 
 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(); };
 };
 
 }

+ 4 - 25
Userland/Libraries/LibGfx/Filters/InvertFilter.h

@@ -6,40 +6,19 @@
 
 #pragma once
 
-#include <LibGfx/Filters/Filter.h>
+#include <LibGfx/Filters/ColorFilter.h>
 
 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(); };
 };
 
 }