diff options
author | AnotherTest <ali.mpfard@gmail.com> | 2020-07-24 09:59:39 +0430 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-07-25 02:13:43 +0200 |
commit | 9685080bd4e44080dbf7569f4b0e42ee890909a2 (patch) | |
tree | 9cc05be979434e99ef3249f14ae422a6e5cf2a67 /Applications/PixelPaint | |
parent | 9d349ac64626af5dbc64c9fafd5e05024ddc2cce (diff) | |
download | serenity-9685080bd4e44080dbf7569f4b0e42ee890909a2.zip |
PixelPaint: Add a bunch of spatial filters
This patchset adds a generic convolution matrix spatial filter, and a
few named derivatives such as BoxBlur, Laplacian, Sharpen and
GaussianBlur.
Diffstat (limited to 'Applications/PixelPaint')
-rw-r--r-- | Applications/PixelPaint/CMakeLists.txt | 6 | ||||
-rw-r--r-- | Applications/PixelPaint/Filters/BoxBlurFilter.cpp | 70 | ||||
-rw-r--r-- | Applications/PixelPaint/Filters/BoxBlurFilter.h | 46 | ||||
-rw-r--r-- | Applications/PixelPaint/Filters/Filter.cpp | 39 | ||||
-rw-r--r-- | Applications/PixelPaint/Filters/Filter.h | 64 | ||||
-rw-r--r-- | Applications/PixelPaint/Filters/GenericConvolutionFilter.cpp | 172 | ||||
-rw-r--r-- | Applications/PixelPaint/Filters/GenericConvolutionFilter.h | 99 | ||||
-rw-r--r-- | Applications/PixelPaint/Filters/LaplacianFilter.cpp | 52 | ||||
-rw-r--r-- | Applications/PixelPaint/Filters/LaplacianFilter.h | 45 | ||||
-rw-r--r-- | Applications/PixelPaint/Filters/SharpenFilter.cpp | 50 | ||||
-rw-r--r-- | Applications/PixelPaint/Filters/SharpenFilter.h | 45 | ||||
-rw-r--r-- | Applications/PixelPaint/Filters/SpatialGaussianBlurFilter.cpp | 69 | ||||
-rw-r--r-- | Applications/PixelPaint/Filters/SpatialGaussianBlurFilter.h | 47 | ||||
-rw-r--r-- | Applications/PixelPaint/Layer.h | 2 | ||||
-rw-r--r-- | Applications/PixelPaint/main.cpp | 64 |
15 files changed, 869 insertions, 1 deletions
diff --git a/Applications/PixelPaint/CMakeLists.txt b/Applications/PixelPaint/CMakeLists.txt index 52c55e48b0..3fb7752ca8 100644 --- a/Applications/PixelPaint/CMakeLists.txt +++ b/Applications/PixelPaint/CMakeLists.txt @@ -3,6 +3,12 @@ set(SOURCES CreateNewLayerDialog.cpp EllipseTool.cpp EraseTool.cpp + Filters/BoxBlurFilter.cpp + Filters/Filter.cpp + Filters/GenericConvolutionFilter.cpp + Filters/LaplacianFilter.cpp + Filters/SharpenFilter.cpp + Filters/SpatialGaussianBlurFilter.cpp Image.cpp ImageEditor.cpp Layer.cpp diff --git a/Applications/PixelPaint/Filters/BoxBlurFilter.cpp b/Applications/PixelPaint/Filters/BoxBlurFilter.cpp new file mode 100644 index 0000000000..4da1c1d158 --- /dev/null +++ b/Applications/PixelPaint/Filters/BoxBlurFilter.cpp @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2020, the SerenityOS developers. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "BoxBlurFilter.h" +#include <LibGUI/BoxLayout.h> +#include <LibGUI/Button.h> +#include <LibGUI/CheckBox.h> +#include <LibGUI/SpinBox.h> + +namespace PixelPaint { + +template<size_t N> +BoxBlurFilter<N>::BoxBlurFilter() +{ +} + +template<size_t N> +BoxBlurFilter<N>::~BoxBlurFilter() +{ +} + +template<size_t N> +void BoxBlurFilter<N>::apply(const Filter::Parameters& parameters) +{ + GenericConvolutionFilter<N>::apply(parameters); +} + +template<size_t N> +typename GenericConvolutionFilter<N>::Parameters BoxBlurFilter<N>::get_parameters(Gfx::Bitmap& bitmap, const Gfx::IntRect& rect) +{ + Matrix<N, float> kernel; + + for (size_t i = 0; i < N; ++i) { + for (size_t j = 0; j < N; ++j) { + kernel.elements()[i][j] = 1; + } + } + + normalize(kernel); + + return { bitmap, rect, kernel }; +} + +} + +template class PixelPaint::BoxBlurFilter<3>; +template class PixelPaint::BoxBlurFilter<5>; diff --git a/Applications/PixelPaint/Filters/BoxBlurFilter.h b/Applications/PixelPaint/Filters/BoxBlurFilter.h new file mode 100644 index 0000000000..760eb17df9 --- /dev/null +++ b/Applications/PixelPaint/Filters/BoxBlurFilter.h @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2020, the SerenityOS developers. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#pragma once + +#include "GenericConvolutionFilter.h" + +namespace PixelPaint { + +template<size_t N> +class BoxBlurFilter : public GenericConvolutionFilter<N> { +public: + BoxBlurFilter(); + virtual ~BoxBlurFilter(); + + virtual const char* class_name() const override { return "BoxBlurFilter"; } + + virtual void apply(const Filter::Parameters&) override; + + typename GenericConvolutionFilter<N>::Parameters get_parameters(Gfx::Bitmap&, const Gfx::IntRect&); +}; + +} diff --git a/Applications/PixelPaint/Filters/Filter.cpp b/Applications/PixelPaint/Filters/Filter.cpp new file mode 100644 index 0000000000..cd21c604e4 --- /dev/null +++ b/Applications/PixelPaint/Filters/Filter.cpp @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2020, the SerenityOS developers. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "Filter.h" + +namespace PixelPaint { + +Filter::Filter() +{ +} + +Filter::~Filter() +{ +} + +} diff --git a/Applications/PixelPaint/Filters/Filter.h b/Applications/PixelPaint/Filters/Filter.h new file mode 100644 index 0000000000..c2146324b1 --- /dev/null +++ b/Applications/PixelPaint/Filters/Filter.h @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2020, the SerenityOS developers. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#pragma once + +#include <LibGUI/Event.h> +#include <LibGUI/Forward.h> +#include <LibGfx/Forward.h> + +namespace PixelPaint { + +class Filter { +public: + class Parameters { + public: + Parameters(Gfx::Bitmap& bitmap, const Gfx::IntRect& rect) + : m_target_bitmap(bitmap) + , m_target_rect(rect) + + { + } + + Gfx::Bitmap& bitmap() const { return m_target_bitmap; } + const Gfx::IntRect& rect() const { return m_target_rect; } + virtual bool is_generic_convolution_filter() const { return false; } + + private: + Gfx::Bitmap& m_target_bitmap; + Gfx::IntRect m_target_rect; + }; + virtual ~Filter(); + + virtual const char* class_name() const = 0; + + virtual void apply(const Parameters&) = 0; + +protected: + Filter(); +}; + +} diff --git a/Applications/PixelPaint/Filters/GenericConvolutionFilter.cpp b/Applications/PixelPaint/Filters/GenericConvolutionFilter.cpp new file mode 100644 index 0000000000..161beef25a --- /dev/null +++ b/Applications/PixelPaint/Filters/GenericConvolutionFilter.cpp @@ -0,0 +1,172 @@ +/* + * Copyright (c) 2020, the SerenityOS developers. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "GenericConvolutionFilter.h" +#include <AK/TemporaryChange.h> +#include <LibGUI/Action.h> +#include <LibGUI/BoxLayout.h> +#include <LibGUI/Button.h> +#include <LibGUI/CheckBox.h> +#include <LibGUI/Dialog.h> +#include <LibGUI/Menu.h> +#include <LibGUI/Painter.h> +#include <LibGUI/TextBox.h> +#include <LibGfx/Bitmap.h> + +namespace PixelPaint { + +template<size_t N> +GenericConvolutionFilter<N>::GenericConvolutionFilter() +{ +} + +template<size_t N> +GenericConvolutionFilter<N>::~GenericConvolutionFilter() +{ +} + +template<size_t N> +void GenericConvolutionFilter<N>::apply(const Filter::Parameters& parameters) +{ + ASSERT(parameters.is_generic_convolution_filter()); + + auto& gcf_params = static_cast<const GenericConvolutionFilter::Parameters&>(parameters); + + auto& source = gcf_params.bitmap(); + const auto& source_rect = gcf_params.rect(); + auto target = Gfx::Bitmap::create(source.format(), parameters.rect().size()); + + // FIXME: Help! I am naive! + for (auto i_ = 0; i_ < source_rect.width(); ++i_) { + auto i = i_ + source_rect.x(); + for (auto j_ = 0; j_ < source_rect.height(); ++j_) { + auto j = j_ + source_rect.y(); + FloatVector3 value(0, 0, 0); + for (auto k = 0; k < 4; ++k) { + auto ki = i + k - 2; + if (ki < 0 || ki >= source.size().width()) { + if (gcf_params.should_wrap()) + ki = (ki + source.size().width()) % source.size().width(); + else + continue; + } + + for (auto l = 0; l < 4; ++l) { + auto lj = j + l - 2; + if (lj < 0 || lj >= source.size().height()) { + if (gcf_params.should_wrap()) + lj = (lj + source.size().height()) % source.size().height(); + else + continue; + } + + auto pixel = source.get_pixel(ki, lj); + FloatVector3 pixel_value(pixel.red(), pixel.green(), pixel.blue()); + + value = value + pixel_value * gcf_params.kernel().elements()[k][l]; + } + } + + // The float->u8 overflow is intentional. + target->set_pixel(i_, j_, Color(value.x(), value.y(), value.z(), source.get_pixel(i, j).alpha())); + } + } + + // FIXME: Substitute for some sort of faster "blit" method. + for (auto i_ = 0; i_ < source_rect.width(); ++i_) { + auto i = i_ + source_rect.x(); + for (auto j_ = 0; j_ < source_rect.height(); ++j_) { + auto j = j_ + source_rect.y(); + source.set_pixel(i, j, target->get_pixel(i_, j_)); + } + } +} + +template<size_t N> +typename GenericConvolutionFilter<N>::Parameters GenericConvolutionFilter<N>::get_parameters(Gfx::Bitmap& bitmap, const Gfx::IntRect& rect, GUI::Window* parent_window) +{ + auto input = GenericConvolutionFilterInputDialog<N>::construct(parent_window); + input->exec(); + + return { bitmap, rect, input->matrix(), input->should_wrap() }; +} + +template<size_t N> +GenericConvolutionFilterInputDialog<N>::GenericConvolutionFilterInputDialog(Window* parent_window) + : Dialog(parent_window) +{ + // FIXME: Help! Make this GUI less ugly. + auto& main_widget = set_main_widget<GUI::Frame>(); + main_widget.set_frame_shape(Gfx::FrameShape::Container); + main_widget.set_frame_shadow(Gfx::FrameShadow::Raised); + main_widget.set_fill_with_background_color(true); + main_widget.template set_layout<GUI::VerticalBoxLayout>(); + + size_t index = 0; + size_t columns = N; + size_t rows = N; + + for (size_t row = 0; row < rows; ++row) { + auto& horizontal_container = main_widget.template add<GUI::Widget>(); + horizontal_container.template set_layout<GUI::HorizontalBoxLayout>(); + for (size_t column = 0; column < columns; ++column) { + if (index < columns * rows) { + auto& textbox = horizontal_container.template add<GUI::TextBox>(); + textbox.set_preferred_size({ 30, 50 }); + textbox.on_change = [&, row = row, column = column] { + auto& element = m_matrix.elements()[row][column]; + char* endptr = nullptr; + auto value = strtof(textbox.text().characters(), &endptr); + if (endptr != nullptr) + element = value; + else + textbox.set_text(""); + }; + } else { + horizontal_container.template add<GUI::Widget>(); + } + } + } + + auto& norm_checkbox = main_widget.template add<GUI::CheckBox>("Normalize"); + norm_checkbox.set_checked(false); + + auto& wrap_checkbox = main_widget.template add<GUI::CheckBox>("Wrap"); + wrap_checkbox.set_checked(m_should_wrap); + + auto& button = main_widget.template add<GUI::Button>("Done"); + button.set_button_style(Gfx::ButtonStyle::CoolBar); + button.on_click = [&](auto) { + m_should_wrap = wrap_checkbox.is_checked(); + if (norm_checkbox.is_checked()) + normalize(m_matrix); + done(ExecOK); + }; +} +} + +template class PixelPaint::GenericConvolutionFilter<3>; +template class PixelPaint::GenericConvolutionFilter<5>; diff --git a/Applications/PixelPaint/Filters/GenericConvolutionFilter.h b/Applications/PixelPaint/Filters/GenericConvolutionFilter.h new file mode 100644 index 0000000000..e32fd5f417 --- /dev/null +++ b/Applications/PixelPaint/Filters/GenericConvolutionFilter.h @@ -0,0 +1,99 @@ +/* + * Copyright (c) 2020, the SerenityOS developers. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#pragma once + +#include "Filter.h" +#include <LibGUI/Dialog.h> +#include <LibGfx/Matrix.h> +#include <LibGfx/Matrix4x4.h> + +namespace PixelPaint { + +template<size_t N, typename T> +inline static constexpr void normalize(Matrix<N, T>& matrix) +{ + auto sum = 0.0f; + for (size_t i = 0; i < matrix.Size; ++i) { + for (size_t j = 0; j < matrix.Size; ++j) { + sum += matrix.elements()[i][j]; + } + } + for (size_t i = 0; i < matrix.Size; ++i) { + for (size_t j = 0; j < matrix.Size; ++j) { + matrix.elements()[i][j] /= sum; + } + } +} + +template<size_t N> +class GenericConvolutionFilter : public Filter { +public: + class Parameters : public Filter::Parameters { + public: + Parameters(Gfx::Bitmap& bitmap, const Gfx::IntRect& rect, Gfx::Matrix<N, float> kernel, bool should_wrap = false) + : Filter::Parameters(bitmap, rect) + , m_kernel(move(kernel)) + , m_should_wrap(should_wrap) + + { + } + + const Gfx::Matrix<N, float>& kernel() const { return m_kernel; } + Gfx::Matrix<N, float>& kernel() { return m_kernel; } + bool should_wrap() const { return m_should_wrap; } + + private: + virtual bool is_generic_convolution_filter() const override { return true; } + Gfx::Matrix<N, float> m_kernel; + bool m_should_wrap { false }; + }; + + GenericConvolutionFilter(); + virtual ~GenericConvolutionFilter(); + + virtual const char* class_name() const override { return "GenericConvolutionFilter"; } + + virtual void apply(const Filter::Parameters&) override; + + Parameters get_parameters(Gfx::Bitmap&, const Gfx::IntRect&, GUI::Window* parent_window); +}; + +template<size_t N> +class GenericConvolutionFilterInputDialog : public GUI::Dialog { + C_OBJECT(GenericConvolutionFilterInputDialog); + +public: + const Matrix<N, float>& matrix() const { return m_matrix; } + bool should_wrap() const { return m_should_wrap; } + +private: + explicit GenericConvolutionFilterInputDialog(GUI::Window*); + Matrix<N, float> m_matrix {}; + bool m_should_wrap { false }; +}; + +} diff --git a/Applications/PixelPaint/Filters/LaplacianFilter.cpp b/Applications/PixelPaint/Filters/LaplacianFilter.cpp new file mode 100644 index 0000000000..e71ca17e1e --- /dev/null +++ b/Applications/PixelPaint/Filters/LaplacianFilter.cpp @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2020, the SerenityOS developers. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "LaplacianFilter.h" + +namespace PixelPaint { + +LaplacianFilter::LaplacianFilter() +{ +} + +LaplacianFilter::~LaplacianFilter() +{ +} + +void LaplacianFilter::apply(const Filter::Parameters& parameters) +{ + GenericConvolutionFilter::apply(parameters); +} + +GenericConvolutionFilter<3>::Parameters LaplacianFilter::get_parameters(Gfx::Bitmap& bitmap, const Gfx::IntRect& rect, bool diagonal) +{ + if (diagonal) + return { bitmap, rect, Matrix<3, float>(-1, -1, -1, -1, 8, -1, -1, -1, -1) }; + + return { bitmap, rect, Matrix<3, float>(0, -1, 0, -1, 4, -1, 0, -1, 0) }; +} + +} diff --git a/Applications/PixelPaint/Filters/LaplacianFilter.h b/Applications/PixelPaint/Filters/LaplacianFilter.h new file mode 100644 index 0000000000..0184c47e55 --- /dev/null +++ b/Applications/PixelPaint/Filters/LaplacianFilter.h @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2020, the SerenityOS developers. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#pragma once + +#include "GenericConvolutionFilter.h" + +namespace PixelPaint { + +class LaplacianFilter : public GenericConvolutionFilter<3> { +public: + LaplacianFilter(); + virtual ~LaplacianFilter(); + + virtual const char* class_name() const override { return "LaplacianFilter"; } + + virtual void apply(const Filter::Parameters&) override; + + GenericConvolutionFilter::Parameters get_parameters(Gfx::Bitmap&, const Gfx::IntRect&, bool diagonal); +}; + +} diff --git a/Applications/PixelPaint/Filters/SharpenFilter.cpp b/Applications/PixelPaint/Filters/SharpenFilter.cpp new file mode 100644 index 0000000000..4d6f34ed43 --- /dev/null +++ b/Applications/PixelPaint/Filters/SharpenFilter.cpp @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2020, the SerenityOS developers. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "SharpenFilter.h" +#include "../ImageEditor.h" + +namespace PixelPaint { + +SharpenFilter::SharpenFilter() +{ +} + +SharpenFilter::~SharpenFilter() +{ +} + +void SharpenFilter::apply(const Filter::Parameters& parameters) +{ + GenericConvolutionFilter::apply(parameters); +} + +GenericConvolutionFilter<3>::Parameters SharpenFilter::get_parameters(Gfx::Bitmap& bitmap, const Gfx::IntRect& rect) +{ + return { bitmap, rect, Matrix<3, float>(0, -1, 0, -1, 5, -1, 0, -1, 0) }; +} + +} diff --git a/Applications/PixelPaint/Filters/SharpenFilter.h b/Applications/PixelPaint/Filters/SharpenFilter.h new file mode 100644 index 0000000000..2fe26bb3fd --- /dev/null +++ b/Applications/PixelPaint/Filters/SharpenFilter.h @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2020, the SerenityOS developers. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#pragma once + +#include "GenericConvolutionFilter.h" + +namespace PixelPaint { + +class SharpenFilter : public GenericConvolutionFilter<3> { +public: + SharpenFilter(); + virtual ~SharpenFilter(); + + virtual const char* class_name() const override { return "SharpenFilter"; } + + virtual void apply(const Filter::Parameters&) override; + + GenericConvolutionFilter::Parameters get_parameters(Gfx::Bitmap&, const Gfx::IntRect&); +}; + +} diff --git a/Applications/PixelPaint/Filters/SpatialGaussianBlurFilter.cpp b/Applications/PixelPaint/Filters/SpatialGaussianBlurFilter.cpp new file mode 100644 index 0000000000..007df1adaa --- /dev/null +++ b/Applications/PixelPaint/Filters/SpatialGaussianBlurFilter.cpp @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2020, the SerenityOS developers. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "SpatialGaussianBlurFilter.h" + +namespace PixelPaint { + +template<size_t N, typename T> +SpatialGaussianBlurFilter<N, T>::SpatialGaussianBlurFilter() +{ +} + +template<size_t N, typename T> +SpatialGaussianBlurFilter<N, T>::~SpatialGaussianBlurFilter() +{ +} + +template<size_t N, typename T> +void SpatialGaussianBlurFilter<N, T>::apply(const Filter::Parameters& parameters) +{ + GenericConvolutionFilter<N>::apply(parameters); +} + +template<size_t N, typename _T> +typename GenericConvolutionFilter<N>::Parameters SpatialGaussianBlurFilter<N, _T>::get_parameters(Gfx::Bitmap& bitmap, const Gfx::IntRect& rect) +{ + Matrix<N, float> kernel; + auto sigma = 1.0f; + auto s = 2.0f * sigma * sigma; + + for (auto x = -(ssize_t)N / 2; x <= (ssize_t)N / 2; x++) { + for (auto y = -(ssize_t)N / 2; y <= (ssize_t)N / 2; y++) { + auto r = sqrt(x * x + y * y); + kernel.elements()[x + 2][y + 2] = (exp(-(r * r) / s)) / (M_PI * s); + } + } + + normalize(kernel); + + return { bitmap, rect, kernel }; +} + +} + +template class PixelPaint::SpatialGaussianBlurFilter<3>; +template class PixelPaint::SpatialGaussianBlurFilter<5>; diff --git a/Applications/PixelPaint/Filters/SpatialGaussianBlurFilter.h b/Applications/PixelPaint/Filters/SpatialGaussianBlurFilter.h new file mode 100644 index 0000000000..38e56d501b --- /dev/null +++ b/Applications/PixelPaint/Filters/SpatialGaussianBlurFilter.h @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2020, the SerenityOS developers. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#pragma once + +#include "GenericConvolutionFilter.h" +#include <AK/StdLibExtras.h> + +namespace PixelPaint { + +template<size_t N, typename = typename AK::EnableIf<N % 2 == 1>::Type> +class SpatialGaussianBlurFilter : public GenericConvolutionFilter<N> { +public: + SpatialGaussianBlurFilter(); + virtual ~SpatialGaussianBlurFilter(); + + virtual const char* class_name() const override { return "SpatialGaussianBlurFilter"; } + + virtual void apply(const Filter::Parameters&) override; + + typename GenericConvolutionFilter<N>::Parameters get_parameters(Gfx::Bitmap&, const Gfx::IntRect&); +}; + +} diff --git a/Applications/PixelPaint/Layer.h b/Applications/PixelPaint/Layer.h index a9aeb75c8f..f7ef404a7a 100644 --- a/Applications/PixelPaint/Layer.h +++ b/Applications/PixelPaint/Layer.h @@ -61,6 +61,8 @@ public: const String& name() const { return m_name; } void set_name(const String&); + void set_bitmap(Gfx::Bitmap& bitmap) { m_bitmap = bitmap; } + void did_modify_bitmap(Image&); void set_selected(bool selected) { m_selected = selected; } diff --git a/Applications/PixelPaint/main.cpp b/Applications/PixelPaint/main.cpp index d5b39f35f8..d54052f612 100644 --- a/Applications/PixelPaint/main.cpp +++ b/Applications/PixelPaint/main.cpp @@ -25,14 +25,19 @@ */ #include "CreateNewLayerDialog.h" +#include "Filters/BoxBlurFilter.h" +#include "Filters/GenericConvolutionFilter.h" +#include "Filters/LaplacianFilter.h" +#include "Filters/SharpenFilter.h" +#include "Filters/SpatialGaussianBlurFilter.h" #include "Image.h" #include "ImageEditor.h" #include "Layer.h" #include "LayerListWidget.h" +#include "LayerPropertiesWidget.h" #include "PaletteWidget.h" #include "Tool.h" #include "ToolboxWidget.h" -#include "LayerPropertiesWidget.h" #include <LibGUI/AboutDialog.h> #include <LibGUI/Action.h> #include <LibGUI/Application.h> @@ -45,6 +50,7 @@ #include <LibGUI/TableView.h> #include <LibGUI/Window.h> #include <LibGfx/Bitmap.h> +#include <LibGfx/Matrix4x4.h> #include <stdio.h> int main(int argc, char** argv) @@ -194,6 +200,62 @@ int main(int argc, char** argv) }, window)); + auto& filter_menu = menubar->add_menu("Filter"); + auto& spatial_filters_menu = filter_menu.add_submenu("Spatial"); + + auto& edge_detect_submenu = spatial_filters_menu.add_submenu("Edge Detect"); + edge_detect_submenu.add_action(GUI::Action::create("Laplacian (cardinal)", [&](auto&) { + if (auto* layer = image_editor.active_layer()) { + PixelPaint::LaplacianFilter filter; + filter.apply(filter.get_parameters(layer->bitmap(), layer->rect(), false)); + } + })); + edge_detect_submenu.add_action(GUI::Action::create("Laplacian (diagonal)", [&](auto&) { + if (auto* layer = image_editor.active_layer()) { + PixelPaint::LaplacianFilter filter; + filter.apply(filter.get_parameters(layer->bitmap(), layer->rect(), true)); + } + })); + auto& blur_submenu = spatial_filters_menu.add_submenu("Blur and Sharpen"); + blur_submenu.add_action(GUI::Action::create("Gaussian Blur (3x3)", [&](auto&) { + if (auto* layer = image_editor.active_layer()) { + PixelPaint::SpatialGaussianBlurFilter<3> filter; + filter.apply(filter.get_parameters(layer->bitmap(), layer->rect())); + } + })); + blur_submenu.add_action(GUI::Action::create("Gaussian Blur (5x5)", [&](auto&) { + if (auto* layer = image_editor.active_layer()) { + PixelPaint::SpatialGaussianBlurFilter<5> filter; + filter.apply(filter.get_parameters(layer->bitmap(), layer->rect())); + } + })); + blur_submenu.add_action(GUI::Action::create("Box Blur (3x3)", [&](auto&) { + if (auto* layer = image_editor.active_layer()) { + PixelPaint::BoxBlurFilter<3> filter; + filter.apply(filter.get_parameters(layer->bitmap(), layer->rect())); + } + })); + blur_submenu.add_action(GUI::Action::create("Box Blur (5x5)", [&](auto&) { + if (auto* layer = image_editor.active_layer()) { + PixelPaint::BoxBlurFilter<5> filter; + filter.apply(filter.get_parameters(layer->bitmap(), layer->rect())); + } + })); + blur_submenu.add_action(GUI::Action::create("Sharpen", [&](auto&) { + if (auto* layer = image_editor.active_layer()) { + PixelPaint::SharpenFilter filter; + filter.apply(filter.get_parameters(layer->bitmap(), layer->rect())); + } + })); + + spatial_filters_menu.add_separator(); + spatial_filters_menu.add_action(GUI::Action::create("Generic 5x5 Convolution", [&](auto&) { + if (auto* layer = image_editor.active_layer()) { + PixelPaint::GenericConvolutionFilter<5> filter; + filter.apply(filter.get_parameters(layer->bitmap(), layer->rect(), window)); + } + })); + auto& help_menu = menubar->add_menu("Help"); help_menu.add_action(GUI::Action::create("About", [&](auto&) { GUI::AboutDialog::show("PixelPaint", app_icon.bitmap_for_size(32), window); |