| 1 | /*
|
|---|
| 2 | * Copyright (C) 2004, 2005, 2006, 2007 Nikolas Zimmermann <zimmermann@kde.org>
|
|---|
| 3 | * Copyright (C) 2004, 2005 Rob Buis <buis@kde.org>
|
|---|
| 4 | * Copyright (C) 2005 Eric Seidel <eric@webkit.org>
|
|---|
| 5 | * Copyright (C) 2009 Dirk Schulze <krit@webkit.org>
|
|---|
| 6 | * Copyright (C) 2010 Zoltan Herczeg <zherczeg@webkit.org>
|
|---|
| 7 | * Copyright (C) 2021-2022 Apple Inc. All rights reserved.
|
|---|
| 8 | *
|
|---|
| 9 | * This library is free software; you can redistribute it and/or
|
|---|
| 10 | * modify it under the terms of the GNU Library General Public
|
|---|
| 11 | * License as published by the Free Software Foundation; either
|
|---|
| 12 | * version 2 of the License, or (at your option) any later version.
|
|---|
| 13 | *
|
|---|
| 14 | * This library is distributed in the hope that it will be useful,
|
|---|
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|---|
| 17 | * Library General Public License for more details.
|
|---|
| 18 | *
|
|---|
| 19 | * You should have received a copy of the GNU Library General Public License
|
|---|
| 20 | * along with this library; see the file COPYING.LIB. If not, write to
|
|---|
| 21 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|---|
| 22 | * Boston, MA 02110-1301, USA.
|
|---|
| 23 | */
|
|---|
| 24 |
|
|---|
| 25 | #include "config.h"
|
|---|
| 26 | #include "FEConvolveMatrix.h"
|
|---|
| 27 |
|
|---|
| 28 | #include "FEConvolveMatrixSoftwareApplier.h"
|
|---|
| 29 | #include "Filter.h"
|
|---|
| 30 | #include <wtf/text/TextStream.h>
|
|---|
| 31 |
|
|---|
| 32 | namespace WebCore {
|
|---|
| 33 |
|
|---|
| 34 | Ref<FEConvolveMatrix> FEConvolveMatrix::create(const IntSize& kernelSize, float divisor, float bias, const IntPoint& targetOffset, EdgeModeType edgeMode, const FloatPoint& kernelUnitLength, bool preserveAlpha, const Vector<float>& kernelMatrix)
|
|---|
| 35 | {
|
|---|
| 36 | return adoptRef(*new FEConvolveMatrix(kernelSize, divisor, bias, targetOffset, edgeMode, kernelUnitLength, preserveAlpha, kernelMatrix));
|
|---|
| 37 | }
|
|---|
| 38 |
|
|---|
| 39 | FEConvolveMatrix::FEConvolveMatrix(const IntSize& kernelSize, float divisor, float bias, const IntPoint& targetOffset, EdgeModeType edgeMode, const FloatPoint& kernelUnitLength, bool preserveAlpha, const Vector<float>& kernelMatrix)
|
|---|
| 40 | : FilterEffect(FilterEffect::Type::FEConvolveMatrix)
|
|---|
| 41 | , m_kernelSize(kernelSize)
|
|---|
| 42 | , m_divisor(divisor)
|
|---|
| 43 | , m_bias(bias)
|
|---|
| 44 | , m_targetOffset(targetOffset)
|
|---|
| 45 | , m_edgeMode(edgeMode)
|
|---|
| 46 | , m_kernelUnitLength(kernelUnitLength)
|
|---|
| 47 | , m_preserveAlpha(preserveAlpha)
|
|---|
| 48 | , m_kernelMatrix(kernelMatrix)
|
|---|
| 49 | {
|
|---|
| 50 | ASSERT(m_kernelSize.width() > 0);
|
|---|
| 51 | ASSERT(m_kernelSize.height() > 0);
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | void FEConvolveMatrix::setKernelSize(const IntSize& kernelSize)
|
|---|
| 55 | {
|
|---|
| 56 | ASSERT(kernelSize.width() > 0);
|
|---|
| 57 | ASSERT(kernelSize.height() > 0);
|
|---|
| 58 | m_kernelSize = kernelSize;
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | void FEConvolveMatrix::setKernel(const Vector<float>& kernel)
|
|---|
| 62 | {
|
|---|
| 63 | m_kernelMatrix = kernel;
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | bool FEConvolveMatrix::setDivisor(float divisor)
|
|---|
| 67 | {
|
|---|
| 68 | ASSERT(divisor);
|
|---|
| 69 | if (m_divisor == divisor)
|
|---|
| 70 | return false;
|
|---|
| 71 | m_divisor = divisor;
|
|---|
| 72 | return true;
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | bool FEConvolveMatrix::setBias(float bias)
|
|---|
| 76 | {
|
|---|
| 77 | if (m_bias == bias)
|
|---|
| 78 | return false;
|
|---|
| 79 | m_bias = bias;
|
|---|
| 80 | return true;
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | bool FEConvolveMatrix::setTargetOffset(const IntPoint& targetOffset)
|
|---|
| 84 | {
|
|---|
| 85 | if (m_targetOffset == targetOffset)
|
|---|
| 86 | return false;
|
|---|
| 87 | m_targetOffset = targetOffset;
|
|---|
| 88 | return true;
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | bool FEConvolveMatrix::setEdgeMode(EdgeModeType edgeMode)
|
|---|
| 92 | {
|
|---|
| 93 | if (m_edgeMode == edgeMode)
|
|---|
| 94 | return false;
|
|---|
| 95 | m_edgeMode = edgeMode;
|
|---|
| 96 | return true;
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | bool FEConvolveMatrix::setKernelUnitLength(const FloatPoint& kernelUnitLength)
|
|---|
| 100 | {
|
|---|
| 101 | ASSERT(kernelUnitLength.x() > 0);
|
|---|
| 102 | ASSERT(kernelUnitLength.y() > 0);
|
|---|
| 103 | if (m_kernelUnitLength == kernelUnitLength)
|
|---|
| 104 | return false;
|
|---|
| 105 | m_kernelUnitLength = kernelUnitLength;
|
|---|
| 106 | return true;
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | bool FEConvolveMatrix::setPreserveAlpha(bool preserveAlpha)
|
|---|
| 110 | {
|
|---|
| 111 | if (m_preserveAlpha == preserveAlpha)
|
|---|
| 112 | return false;
|
|---|
| 113 | m_preserveAlpha = preserveAlpha;
|
|---|
| 114 | return true;
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | FloatRect FEConvolveMatrix::calculateImageRect(const Filter& filter, const FilterImageVector&, const FloatRect& primitiveSubregion) const
|
|---|
| 118 | {
|
|---|
| 119 | return filter.maxEffectRect(primitiveSubregion);
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | std::unique_ptr<FilterEffectApplier> FEConvolveMatrix::createSoftwareApplier() const
|
|---|
| 123 | {
|
|---|
| 124 | return FilterEffectApplier::create<FEConvolveMatrixSoftwareApplier>(*this);
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | static TextStream& operator<<(TextStream& ts, const EdgeModeType& type)
|
|---|
| 128 | {
|
|---|
| 129 | switch (type) {
|
|---|
| 130 | case EdgeModeType::Unknown:
|
|---|
| 131 | ts << "UNKNOWN";
|
|---|
| 132 | break;
|
|---|
| 133 | case EdgeModeType::Duplicate:
|
|---|
| 134 | ts << "DUPLICATE";
|
|---|
| 135 | break;
|
|---|
| 136 | case EdgeModeType::Wrap:
|
|---|
| 137 | ts << "WRAP";
|
|---|
| 138 | break;
|
|---|
| 139 | case EdgeModeType::None:
|
|---|
| 140 | ts << "NONE";
|
|---|
| 141 | break;
|
|---|
| 142 | }
|
|---|
| 143 | return ts;
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| 146 | TextStream& FEConvolveMatrix::externalRepresentation(TextStream& ts, FilterRepresentation representation) const
|
|---|
| 147 | {
|
|---|
| 148 | ts << indent << "[feConvolveMatrix";
|
|---|
| 149 | FilterEffect::externalRepresentation(ts, representation);
|
|---|
| 150 |
|
|---|
| 151 | ts << " order=\"" << m_kernelSize << "\"";
|
|---|
| 152 | ts << " kernelMatrix=\"" << m_kernelMatrix << "\"";
|
|---|
| 153 | ts << " divisor=\"" << m_divisor << "\"";
|
|---|
| 154 | ts << " bias=\"" << m_bias << "\"";
|
|---|
| 155 | ts << " target=\"" << m_targetOffset << "\"";
|
|---|
| 156 | ts << " edgeMode=\"" << m_edgeMode << "\"";
|
|---|
| 157 | ts << " kernelUnitLength=\"" << m_kernelUnitLength << "\"";
|
|---|
| 158 | ts << " preserveAlpha=\"" << m_preserveAlpha << "\"";
|
|---|
| 159 |
|
|---|
| 160 | ts << "]\n";
|
|---|
| 161 | return ts;
|
|---|
| 162 | }
|
|---|
| 163 |
|
|---|
| 164 | }; // namespace WebCore
|
|---|