From 75c359ef31931caca1f2efbcab68783448bdb929 Mon Sep 17 00:00:00 2001 From: Timothy Slater Date: Tue, 1 Nov 2022 17:28:05 -0500 Subject: [PATCH] PixelPaint: Make filters apply to a selection if one is present This changes ImageProcessor to use the scratch bitmap of the layer which will cause the changes to only be applied inside the active selection (if there is one). This also updates the FilterPreviewWidget to show the filter preview with active selection taken into account. --- .../Applications/PixelPaint/FilterGallery.cpp | 1 + .../PixelPaint/FilterPreviewWidget.cpp | 23 +++++++++++++++++-- .../PixelPaint/FilterPreviewWidget.h | 2 ++ .../PixelPaint/ImageProcessor.cpp | 2 +- 4 files changed, 25 insertions(+), 3 deletions(-) diff --git a/Userland/Applications/PixelPaint/FilterGallery.cpp b/Userland/Applications/PixelPaint/FilterGallery.cpp index 20ab3c32d2..4fd9f3dad8 100644 --- a/Userland/Applications/PixelPaint/FilterGallery.cpp +++ b/Userland/Applications/PixelPaint/FilterGallery.cpp @@ -65,6 +65,7 @@ FilterGallery::FilterGallery(GUI::Window* parent_window, ImageEditor* editor) m_config_widget->add_child(*m_selected_filter_config_widget); }; + m_preview_widget->set_layer(editor->active_layer()); m_preview_widget->set_bitmap(editor->active_layer()->content_bitmap().clone().release_value()); apply_button->on_click = [this](auto) { diff --git a/Userland/Applications/PixelPaint/FilterPreviewWidget.cpp b/Userland/Applications/PixelPaint/FilterPreviewWidget.cpp index aff498e971..0f2cfb7546 100644 --- a/Userland/Applications/PixelPaint/FilterPreviewWidget.cpp +++ b/Userland/Applications/PixelPaint/FilterPreviewWidget.cpp @@ -21,6 +21,11 @@ FilterPreviewWidget::~FilterPreviewWidget() { } +void FilterPreviewWidget::set_layer(RefPtr layer) +{ + m_layer = layer; +} + void FilterPreviewWidget::set_bitmap(RefPtr const& bitmap) { m_bitmap = bitmap; @@ -29,10 +34,24 @@ void FilterPreviewWidget::set_bitmap(RefPtr const& bitmap) void FilterPreviewWidget::set_filter(Filter* filter) { - if (filter) + if (filter) { filter->apply(*m_filtered_bitmap, *m_bitmap); - else + + // If we have a layer set and the image has an active selection we only want the filter to apply to the + // selected region. This will walk the image and for every pixel that is outside the selection, restore it + // from the original bitmap. + if (m_layer && !m_layer->image().selection().is_empty()) { + for (int y = 0; y < m_filtered_bitmap->height(); ++y) { + for (int x = 0; x < m_filtered_bitmap->width(); ++x) { + if (!m_layer->image().selection().is_selected(m_layer->location().translated(x, y))) { + m_filtered_bitmap->set_pixel(x, y, m_bitmap->get_pixel(x, y)); + } + } + } + } + } else { m_filtered_bitmap = m_bitmap->clone().release_value(); + } repaint(); } diff --git a/Userland/Applications/PixelPaint/FilterPreviewWidget.h b/Userland/Applications/PixelPaint/FilterPreviewWidget.h index 4753c693ef..ba074188c1 100644 --- a/Userland/Applications/PixelPaint/FilterPreviewWidget.h +++ b/Userland/Applications/PixelPaint/FilterPreviewWidget.h @@ -19,6 +19,7 @@ class FilterPreviewWidget final : public GUI::Frame { public: virtual ~FilterPreviewWidget() override; + void set_layer(RefPtr layer); void set_bitmap(RefPtr const& bitmap); void set_filter(Filter* filter); void clear_filter(); @@ -26,6 +27,7 @@ public: private: explicit FilterPreviewWidget(); + RefPtr m_layer; RefPtr m_bitmap; RefPtr m_filtered_bitmap; diff --git a/Userland/Applications/PixelPaint/ImageProcessor.cpp b/Userland/Applications/PixelPaint/ImageProcessor.cpp index 7e5c31eef8..e826c21853 100644 --- a/Userland/Applications/PixelPaint/ImageProcessor.cpp +++ b/Userland/Applications/PixelPaint/ImageProcessor.cpp @@ -16,7 +16,7 @@ FilterApplicationCommand::FilterApplicationCommand(NonnullRefPtr filter, void FilterApplicationCommand::execute() { - m_filter->apply(m_target_layer->content_bitmap(), m_target_layer->content_bitmap()); + m_filter->apply(m_target_layer->get_scratch_edited_bitmap(), m_target_layer->get_scratch_edited_bitmap()); m_filter->m_editor->gui_event_loop().deferred_invoke([strong_this = NonnullRefPtr(*this)]() { // HACK: we can't tell strong_this to not be const (*const_cast*>(&strong_this->m_target_layer))->did_modify_bitmap(strong_this->m_target_layer->rect());