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.
This commit is contained in:
AnotherTest 2020-07-24 09:59:39 +04:30 committed by Andreas Kling
parent 9d349ac646
commit 9685080bd4
Notes: sideshowbarker 2024-07-19 04:38:27 +09:00
15 changed files with 869 additions and 1 deletions

View file

@ -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

View file

@ -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>;

View file

@ -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&);
};
}

View file

@ -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()
{
}
}

View file

@ -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();
};
}

View file

@ -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>;

View file

@ -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 };
};
}

View file

@ -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) };
}
}

View file

@ -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);
};
}

View file

@ -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) };
}
}

View file

@ -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&);
};
}

View file

@ -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>;

View file

@ -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&);
};
}

View file

@ -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; }

View file

@ -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);