mirror of
https://github.com/RGBCube/serenity
synced 2025-07-28 07:57:46 +00:00
PixelPaint: Add invert filter
This commit is contained in:
parent
280cbf2e18
commit
81326ac8c7
3 changed files with 56 additions and 0 deletions
45
Userland/Libraries/LibGfx/Filters/InvertFilter.h
Normal file
45
Userland/Libraries/LibGfx/Filters/InvertFilter.h
Normal file
|
@ -0,0 +1,45 @@
|
|||
/*
|
||||
* Copyright (c) 2021, Musab Kılıç <musabkilic@protonmail.com>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <LibGfx/Filters/Filter.h>
|
||||
|
||||
namespace Gfx {
|
||||
|
||||
class InvertFilter : public Filter {
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue