diff options
author | Musab Kılıç <musabkilic@protonmail.com> | 2021-09-02 21:08:50 +0300 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-09-04 03:35:17 +0200 |
commit | 81326ac8c79f97d352818ecb4381ba2d167280d5 (patch) | |
tree | 8680071c8cc3eee01f88aaf2b269b09852340f41 /Userland | |
parent | 280cbf2e1865c9f5d867d3e715fd86de0a0f928e (diff) | |
download | serenity-81326ac8c79f97d352818ecb4381ba2d167280d5.zip |
PixelPaint: Add invert filter
Diffstat (limited to 'Userland')
-rw-r--r-- | Userland/Applications/PixelPaint/FilterParams.h | 1 | ||||
-rw-r--r-- | Userland/Applications/PixelPaint/main.cpp | 10 | ||||
-rw-r--r-- | Userland/Libraries/LibGfx/Filters/InvertFilter.h | 45 |
3 files changed, 56 insertions, 0 deletions
diff --git a/Userland/Applications/PixelPaint/FilterParams.h b/Userland/Applications/PixelPaint/FilterParams.h index f923f90853..6f97466828 100644 --- a/Userland/Applications/PixelPaint/FilterParams.h +++ b/Userland/Applications/PixelPaint/FilterParams.h @@ -17,6 +17,7 @@ #include <LibGfx/Filters/BoxBlurFilter.h> #include <LibGfx/Filters/GenericConvolutionFilter.h> #include <LibGfx/Filters/GrayscaleFilter.h> +#include <LibGfx/Filters/InvertFilter.h> #include <LibGfx/Filters/LaplacianFilter.h> #include <LibGfx/Filters/SharpenFilter.h> #include <LibGfx/Filters/SpatialGaussianBlurFilter.h> diff --git a/Userland/Applications/PixelPaint/main.cpp b/Userland/Applications/PixelPaint/main.cpp index aba8f8b065..3c96565994 100644 --- a/Userland/Applications/PixelPaint/main.cpp +++ b/Userland/Applications/PixelPaint/main.cpp @@ -724,6 +724,16 @@ int main(int argc, char** argv) editor->did_complete_action(); } })); + color_filters_menu.add_action(GUI::Action::create("Invert", [&](auto&) { + auto* editor = current_image_editor(); + if (!editor) + return; + if (auto* layer = editor->active_layer()) { + Gfx::InvertFilter filter; + filter.apply(layer->bitmap(), layer->rect(), layer->bitmap(), layer->rect()); + editor->did_complete_action(); + } + })); auto& help_menu = window->add_menu("&Help"); help_menu.add_action(GUI::CommonActions::make_about_action("Pixel Paint", app_icon, window)); diff --git a/Userland/Libraries/LibGfx/Filters/InvertFilter.h b/Userland/Libraries/LibGfx/Filters/InvertFilter.h new file mode 100644 index 0000000000..dc24501069 --- /dev/null +++ b/Userland/Libraries/LibGfx/Filters/InvertFilter.h @@ -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); + } + } + } +}; + +} |