mirror of
https://github.com/RGBCube/serenity
synced 2025-06-01 12:08:14 +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
14 changed files with 96 additions and 363 deletions
42
Libraries/LibGfx/Filters/BoxBlurFilter.h
Normal file
42
Libraries/LibGfx/Filters/BoxBlurFilter.h
Normal file
|
@ -0,0 +1,42 @@
|
|||
/*
|
||||
* 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 Gfx {
|
||||
|
||||
template<size_t N>
|
||||
class BoxBlurFilter : public GenericConvolutionFilter<N> {
|
||||
public:
|
||||
BoxBlurFilter() { }
|
||||
virtual ~BoxBlurFilter() { }
|
||||
|
||||
virtual const char* class_name() const override { return "BoxBlurFilter"; }
|
||||
};
|
||||
|
||||
}
|
65
Libraries/LibGfx/Filters/Filter.h
Normal file
65
Libraries/LibGfx/Filters/Filter.h
Normal file
|
@ -0,0 +1,65 @@
|
|||
/*
|
||||
* 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 <LibGfx/Bitmap.h>
|
||||
#include <LibGfx/Rect.h>
|
||||
|
||||
namespace Gfx {
|
||||
|
||||
class Filter {
|
||||
public:
|
||||
class Parameters {
|
||||
public:
|
||||
Parameters(Bitmap& bitmap, const IntRect& rect)
|
||||
: m_target_bitmap(bitmap)
|
||||
, m_target_rect(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:
|
||||
Bitmap& m_target_bitmap;
|
||||
IntRect m_target_rect;
|
||||
};
|
||||
virtual ~Filter() { }
|
||||
|
||||
virtual const char* class_name() const = 0;
|
||||
|
||||
virtual void apply(const Parameters&) = 0;
|
||||
|
||||
protected:
|
||||
Filter() { }
|
||||
};
|
||||
|
||||
}
|
136
Libraries/LibGfx/Filters/GenericConvolutionFilter.h
Normal file
136
Libraries/LibGfx/Filters/GenericConvolutionFilter.h
Normal file
|
@ -0,0 +1,136 @@
|
|||
/*
|
||||
* 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 <LibGfx/Matrix.h>
|
||||
#include <LibGfx/Matrix4x4.h>
|
||||
|
||||
namespace Gfx {
|
||||
|
||||
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& 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_));
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
}
|
41
Libraries/LibGfx/Filters/LaplacianFilter.h
Normal file
41
Libraries/LibGfx/Filters/LaplacianFilter.h
Normal file
|
@ -0,0 +1,41 @@
|
|||
/*
|
||||
* 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 Gfx {
|
||||
|
||||
class LaplacianFilter : public GenericConvolutionFilter<3> {
|
||||
public:
|
||||
LaplacianFilter() { }
|
||||
virtual ~LaplacianFilter() { }
|
||||
|
||||
virtual const char* class_name() const override { return "LaplacianFilter"; }
|
||||
};
|
||||
|
||||
}
|
41
Libraries/LibGfx/Filters/SharpenFilter.h
Normal file
41
Libraries/LibGfx/Filters/SharpenFilter.h
Normal file
|
@ -0,0 +1,41 @@
|
|||
/*
|
||||
* 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 Gfx {
|
||||
|
||||
class SharpenFilter : public GenericConvolutionFilter<3> {
|
||||
public:
|
||||
SharpenFilter() { }
|
||||
virtual ~SharpenFilter() { }
|
||||
|
||||
virtual const char* class_name() const override { return "SharpenFilter"; }
|
||||
};
|
||||
|
||||
}
|
43
Libraries/LibGfx/Filters/SpatialGaussianBlurFilter.h
Normal file
43
Libraries/LibGfx/Filters/SpatialGaussianBlurFilter.h
Normal file
|
@ -0,0 +1,43 @@
|
|||
/*
|
||||
* 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 Gfx {
|
||||
|
||||
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"; }
|
||||
};
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue