mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-22 15:40:19 +00:00
LibGfx: Move filters from PixelPaint into LibGfx
This allows re-using the same filters outside of PixelPaint.
This commit is contained in:
parent
f9700ffb41
commit
8af02fc8b3
Notes:
sideshowbarker
2024-07-19 01:55:26 +09:00
Author: https://github.com/tomuta Commit: https://github.com/SerenityOS/serenity/commit/8af02fc8b3d Pull-request: https://github.com/SerenityOS/serenity/pull/3751
14 changed files with 96 additions and 363 deletions
|
@ -3,12 +3,6 @@ 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
|
||||
|
|
|
@ -1,48 +0,0 @@
|
|||
/*
|
||||
* 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 class PixelPaint::BoxBlurFilter<3>;
|
||||
template class PixelPaint::BoxBlurFilter<5>;
|
|
@ -1,39 +0,0 @@
|
|||
/*
|
||||
* 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()
|
||||
{
|
||||
}
|
||||
|
||||
}
|
|
@ -1,103 +0,0 @@
|
|||
/*
|
||||
* 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 <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 class PixelPaint::GenericConvolutionFilter<3>;
|
||||
template class PixelPaint::GenericConvolutionFilter<5>;
|
|
@ -1,39 +0,0 @@
|
|||
/*
|
||||
* 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()
|
||||
{
|
||||
}
|
||||
|
||||
}
|
|
@ -1,40 +0,0 @@
|
|||
/*
|
||||
* 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()
|
||||
{
|
||||
}
|
||||
|
||||
}
|
|
@ -1,44 +0,0 @@
|
|||
/*
|
||||
* 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 class PixelPaint::SpatialGaussianBlurFilter<3>;
|
||||
template class PixelPaint::SpatialGaussianBlurFilter<5>;
|
|
@ -218,51 +218,51 @@ int main(int argc, char** argv)
|
|||
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;
|
||||
if (auto parameters = PixelPaint::FilterParameters<PixelPaint::LaplacianFilter>::get(layer->bitmap(), layer->rect(), false))
|
||||
Gfx::LaplacianFilter filter;
|
||||
if (auto parameters = PixelPaint::FilterParameters<Gfx::LaplacianFilter>::get(layer->bitmap(), layer->rect(), false))
|
||||
filter.apply(*parameters);
|
||||
}
|
||||
}));
|
||||
edge_detect_submenu.add_action(GUI::Action::create("Laplacian (diagonal)", [&](auto&) {
|
||||
if (auto* layer = image_editor.active_layer()) {
|
||||
PixelPaint::LaplacianFilter filter;
|
||||
if (auto parameters = PixelPaint::FilterParameters<PixelPaint::LaplacianFilter>::get(layer->bitmap(), layer->rect(), true))
|
||||
Gfx::LaplacianFilter filter;
|
||||
if (auto parameters = PixelPaint::FilterParameters<Gfx::LaplacianFilter>::get(layer->bitmap(), layer->rect(), true))
|
||||
filter.apply(*parameters);
|
||||
}
|
||||
}));
|
||||
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;
|
||||
if (auto parameters = PixelPaint::FilterParameters<PixelPaint::SpatialGaussianBlurFilter<3>>::get(layer->bitmap(), layer->rect()))
|
||||
Gfx::SpatialGaussianBlurFilter<3> filter;
|
||||
if (auto parameters = PixelPaint::FilterParameters<Gfx::SpatialGaussianBlurFilter<3>>::get(layer->bitmap(), layer->rect()))
|
||||
filter.apply(*parameters);
|
||||
}
|
||||
}));
|
||||
blur_submenu.add_action(GUI::Action::create("Gaussian Blur (5x5)", [&](auto&) {
|
||||
if (auto* layer = image_editor.active_layer()) {
|
||||
PixelPaint::SpatialGaussianBlurFilter<5> filter;
|
||||
if (auto parameters = PixelPaint::FilterParameters<PixelPaint::SpatialGaussianBlurFilter<5>>::get(layer->bitmap(), layer->rect()))
|
||||
Gfx::SpatialGaussianBlurFilter<5> filter;
|
||||
if (auto parameters = PixelPaint::FilterParameters<Gfx::SpatialGaussianBlurFilter<5>>::get(layer->bitmap(), layer->rect()))
|
||||
filter.apply(*parameters);
|
||||
}
|
||||
}));
|
||||
blur_submenu.add_action(GUI::Action::create("Box Blur (3x3)", [&](auto&) {
|
||||
if (auto* layer = image_editor.active_layer()) {
|
||||
PixelPaint::BoxBlurFilter<3> filter;
|
||||
if (auto parameters = PixelPaint::FilterParameters<PixelPaint::BoxBlurFilter<3>>::get(layer->bitmap(), layer->rect()))
|
||||
Gfx::BoxBlurFilter<3> filter;
|
||||
if (auto parameters = PixelPaint::FilterParameters<Gfx::BoxBlurFilter<3>>::get(layer->bitmap(), layer->rect()))
|
||||
filter.apply(*parameters);
|
||||
}
|
||||
}));
|
||||
blur_submenu.add_action(GUI::Action::create("Box Blur (5x5)", [&](auto&) {
|
||||
if (auto* layer = image_editor.active_layer()) {
|
||||
PixelPaint::BoxBlurFilter<5> filter;
|
||||
if (auto parameters = PixelPaint::FilterParameters<PixelPaint::BoxBlurFilter<5>>::get(layer->bitmap(), layer->rect()))
|
||||
Gfx::BoxBlurFilter<5> filter;
|
||||
if (auto parameters = PixelPaint::FilterParameters<Gfx::BoxBlurFilter<5>>::get(layer->bitmap(), layer->rect()))
|
||||
filter.apply(*parameters);
|
||||
}
|
||||
}));
|
||||
blur_submenu.add_action(GUI::Action::create("Sharpen", [&](auto&) {
|
||||
if (auto* layer = image_editor.active_layer()) {
|
||||
PixelPaint::SharpenFilter filter;
|
||||
if (auto parameters = PixelPaint::FilterParameters<PixelPaint::SharpenFilter>::get(layer->bitmap(), layer->rect()))
|
||||
Gfx::SharpenFilter filter;
|
||||
if (auto parameters = PixelPaint::FilterParameters<Gfx::SharpenFilter>::get(layer->bitmap(), layer->rect()))
|
||||
filter.apply(*parameters);
|
||||
}
|
||||
}));
|
||||
|
@ -270,8 +270,8 @@ int main(int argc, char** argv)
|
|||
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;
|
||||
if (auto parameters = PixelPaint::FilterParameters<PixelPaint::GenericConvolutionFilter<5>>::get(layer->bitmap(), layer->rect(), window))
|
||||
Gfx::GenericConvolutionFilter<5> filter;
|
||||
if (auto parameters = PixelPaint::FilterParameters<Gfx::GenericConvolutionFilter<5>>::get(layer->bitmap(), layer->rect(), window))
|
||||
filter.apply(*parameters);
|
||||
}
|
||||
}));
|
||||
|
|
|
@ -28,13 +28,13 @@
|
|||
|
||||
#include "GenericConvolutionFilter.h"
|
||||
|
||||
namespace PixelPaint {
|
||||
namespace Gfx {
|
||||
|
||||
template<size_t N>
|
||||
class BoxBlurFilter : public GenericConvolutionFilter<N> {
|
||||
public:
|
||||
BoxBlurFilter();
|
||||
virtual ~BoxBlurFilter();
|
||||
BoxBlurFilter() { }
|
||||
virtual ~BoxBlurFilter() { }
|
||||
|
||||
virtual const char* class_name() const override { return "BoxBlurFilter"; }
|
||||
};
|
|
@ -26,41 +26,40 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <LibGUI/Event.h>
|
||||
#include <LibGUI/Forward.h>
|
||||
#include <LibGfx/Forward.h>
|
||||
#include <LibGfx/Bitmap.h>
|
||||
#include <LibGfx/Rect.h>
|
||||
|
||||
namespace PixelPaint {
|
||||
namespace Gfx {
|
||||
|
||||
class Filter {
|
||||
public:
|
||||
class Parameters {
|
||||
public:
|
||||
Parameters(Gfx::Bitmap& bitmap, const Gfx::IntRect& rect)
|
||||
Parameters(Bitmap& bitmap, const 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; }
|
||||
Bitmap& bitmap() const { return m_target_bitmap; }
|
||||
const IntRect& rect() const { return m_target_rect; }
|
||||
virtual bool is_generic_convolution_filter() const { return false; }
|
||||
|
||||
virtual ~Parameters() { }
|
||||
|
||||
private:
|
||||
Gfx::Bitmap& m_target_bitmap;
|
||||
Gfx::IntRect m_target_rect;
|
||||
Bitmap& m_target_bitmap;
|
||||
IntRect m_target_rect;
|
||||
};
|
||||
virtual ~Filter();
|
||||
virtual ~Filter() { }
|
||||
|
||||
virtual const char* class_name() const = 0;
|
||||
|
||||
virtual void apply(const Parameters&) = 0;
|
||||
|
||||
protected:
|
||||
Filter();
|
||||
Filter() { }
|
||||
};
|
||||
|
||||
}
|
|
@ -27,11 +27,10 @@
|
|||
#pragma once
|
||||
|
||||
#include "Filter.h"
|
||||
#include <LibGUI/Dialog.h>
|
||||
#include <LibGfx/Matrix.h>
|
||||
#include <LibGfx/Matrix4x4.h>
|
||||
|
||||
namespace PixelPaint {
|
||||
namespace Gfx {
|
||||
|
||||
template<size_t N, typename T>
|
||||
inline static constexpr void normalize(Matrix<N, T>& matrix)
|
||||
|
@ -72,12 +71,66 @@ public:
|
|||
bool m_should_wrap { false };
|
||||
};
|
||||
|
||||
GenericConvolutionFilter();
|
||||
virtual ~GenericConvolutionFilter();
|
||||
GenericConvolutionFilter() { }
|
||||
virtual ~GenericConvolutionFilter() { }
|
||||
|
||||
virtual const char* class_name() const override { return "GenericConvolutionFilter"; }
|
||||
|
||||
virtual void apply(const Filter::Parameters&) override;
|
||||
virtual void 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_));
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
}
|
|
@ -28,12 +28,12 @@
|
|||
|
||||
#include "GenericConvolutionFilter.h"
|
||||
|
||||
namespace PixelPaint {
|
||||
namespace Gfx {
|
||||
|
||||
class LaplacianFilter : public GenericConvolutionFilter<3> {
|
||||
public:
|
||||
LaplacianFilter();
|
||||
virtual ~LaplacianFilter();
|
||||
LaplacianFilter() { }
|
||||
virtual ~LaplacianFilter() { }
|
||||
|
||||
virtual const char* class_name() const override { return "LaplacianFilter"; }
|
||||
};
|
|
@ -28,12 +28,12 @@
|
|||
|
||||
#include "GenericConvolutionFilter.h"
|
||||
|
||||
namespace PixelPaint {
|
||||
namespace Gfx {
|
||||
|
||||
class SharpenFilter : public GenericConvolutionFilter<3> {
|
||||
public:
|
||||
SharpenFilter();
|
||||
virtual ~SharpenFilter();
|
||||
SharpenFilter() { }
|
||||
virtual ~SharpenFilter() { }
|
||||
|
||||
virtual const char* class_name() const override { return "SharpenFilter"; }
|
||||
};
|
|
@ -29,13 +29,13 @@
|
|||
#include "GenericConvolutionFilter.h"
|
||||
#include <AK/StdLibExtras.h>
|
||||
|
||||
namespace PixelPaint {
|
||||
namespace Gfx {
|
||||
|
||||
template<size_t N, typename = typename AK::EnableIf<N % 2 == 1>::Type>
|
||||
class SpatialGaussianBlurFilter : public GenericConvolutionFilter<N> {
|
||||
public:
|
||||
SpatialGaussianBlurFilter();
|
||||
virtual ~SpatialGaussianBlurFilter();
|
||||
SpatialGaussianBlurFilter() { }
|
||||
virtual ~SpatialGaussianBlurFilter() { }
|
||||
|
||||
virtual const char* class_name() const override { return "SpatialGaussianBlurFilter"; }
|
||||
};
|
Loading…
Reference in a new issue