From 81326ac8c79f97d352818ecb4381ba2d167280d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Musab=20K=C4=B1l=C4=B1=C3=A7?= Date: Thu, 2 Sep 2021 21:08:50 +0300 Subject: PixelPaint: Add invert filter --- Userland/Applications/PixelPaint/FilterParams.h | 1 + Userland/Applications/PixelPaint/main.cpp | 10 ++++++ Userland/Libraries/LibGfx/Filters/InvertFilter.h | 45 ++++++++++++++++++++++++ 3 files changed, 56 insertions(+) create mode 100644 Userland/Libraries/LibGfx/Filters/InvertFilter.h 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 #include #include +#include #include #include #include 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ıç + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include + +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); + } + } + } +}; + +} -- cgit v1.2.3