mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 01:17:34 +00:00
LibGfx: Implement Grayscale/Invert filters as ColorFilter
This commit is contained in:
parent
ff25958b51
commit
2502a88e49
2 changed files with 8 additions and 50 deletions
|
@ -6,40 +6,19 @@
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <LibGfx/Filters/Filter.h>
|
#include <LibGfx/Filters/ColorFilter.h>
|
||||||
|
|
||||||
namespace Gfx {
|
namespace Gfx {
|
||||||
|
|
||||||
class GrayscaleFilter : public Filter {
|
class GrayscaleFilter : public ColorFilter {
|
||||||
public:
|
public:
|
||||||
GrayscaleFilter() { }
|
GrayscaleFilter() { }
|
||||||
virtual ~GrayscaleFilter() { }
|
virtual ~GrayscaleFilter() { }
|
||||||
|
|
||||||
virtual char const* class_name() const override { return "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
|
protected:
|
||||||
{
|
Color convert_color(Color original) override { return original.to_grayscale(); };
|
||||||
// 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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,40 +6,19 @@
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <LibGfx/Filters/Filter.h>
|
#include <LibGfx/Filters/ColorFilter.h>
|
||||||
|
|
||||||
namespace Gfx {
|
namespace Gfx {
|
||||||
|
|
||||||
class InvertFilter : public Filter {
|
class InvertFilter : public ColorFilter {
|
||||||
public:
|
public:
|
||||||
InvertFilter() { }
|
InvertFilter() { }
|
||||||
virtual ~InvertFilter() { }
|
virtual ~InvertFilter() { }
|
||||||
|
|
||||||
virtual char const* class_name() const override { return "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
|
protected:
|
||||||
{
|
Color convert_color(Color original) override { return original.inverted(); };
|
||||||
// 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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue