1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
|
/*
* This file is part of the KDE project
*
* Copyright (c) 2005 Cyrille Berger <cberger@cberger.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "wavefilter.h"
#include <stdlib.h>
#include <vector>
#include <math.h>
#include <QPoint>
#include <kis_debug.h>
#include <kpluginfactory.h>
#include <klocalizedstring.h>
#include <KoUpdater.h>
#include <kis_image.h>
#include <filter/kis_filter_registry.h>
#include <kis_global.h>
#include <kis_layer.h>
#include <kis_random_sub_accessor.h>
#include <kis_selection.h>
#include <kis_types.h>
#include <kis_paint_device.h>
#include <filter/kis_filter_configuration.h>
#include <kis_processing_information.h>
#include "kis_wdg_wave.h"
#include "ui_wdgwaveoptions.h"
#include <kis_iterator_ng.h>
K_PLUGIN_FACTORY_WITH_JSON(KritaWaveFilterFactory, "kritawavefilter.json", registerPlugin<KritaWaveFilter>();)
class KisWaveCurve
{
public:
virtual ~KisWaveCurve() {}
virtual double valueAt(int x, int y) = 0;
};
class KisSinusoidalWaveCurve : public KisWaveCurve
{
public:
KisSinusoidalWaveCurve(int amplitude, int wavelength, int shift) : m_amplitude(amplitude), m_wavelength(wavelength), m_shift(shift) {
}
~KisSinusoidalWaveCurve() override {}
double valueAt(int x, int y) override {
return y + m_amplitude * cos((double)(m_shift + x) / m_wavelength);
}
private:
int m_amplitude, m_wavelength, m_shift;
};
class KisTriangleWaveCurve : public KisWaveCurve
{
public:
KisTriangleWaveCurve(int amplitude, int wavelength, int shift) : m_amplitude(amplitude), m_wavelength(wavelength), m_shift(shift) {
}
~KisTriangleWaveCurve() override {}
double valueAt(int x, int y) override {
return y + m_amplitude * pow(-1.0, (m_shift + x) / m_wavelength) *(0.5 - (double)((m_shift + x) % m_wavelength) / m_wavelength);
}
private:
int m_amplitude, m_wavelength, m_shift;
}; KritaWaveFilter::KritaWaveFilter(QObject *parent, const QVariantList &)
: QObject(parent)
{
KisFilterRegistry::instance()->add(new KisFilterWave());
}
KritaWaveFilter::~KritaWaveFilter()
{
}
KisFilterWave::KisFilterWave() : KisFilter(id(), categoryOther(), i18n("&Wave..."))
{
setColorSpaceIndependence(FULLY_INDEPENDENT);
setSupportsPainting(false);
setSupportsAdjustmentLayers(false);
}
KisFilterConfigurationSP KisFilterWave::factoryConfiguration(const KisPaintDeviceSP) const
{
KisFilterConfigurationSP config = new KisFilterConfiguration("wave", 1);
config->setProperty("horizontalwavelength", 50);
config->setProperty("horizontalshift", 50);
config->setProperty("horizontalamplitude", 4);
config->setProperty("horizontalshape", 0);
config->setProperty("verticalwavelength", 50);
config->setProperty("verticalshift", 50);
config->setProperty("verticalamplitude", 4);
config->setProperty("verticalshape", 0);
return config;
}
KisConfigWidget * KisFilterWave::createConfigurationWidget(QWidget* parent, const KisPaintDeviceSP) const
{
return new KisWdgWave((KisFilter*)this, (QWidget*)parent);
}
void KisFilterWave::processImpl(KisPaintDeviceSP device,
const QRect& applyRect,
const KisFilterConfigurationSP config,
KoUpdater* progressUpdater
) const
{
Q_ASSERT(device.data() != 0);
int cost = (applyRect.width() * applyRect.height()) / 100;
if (cost == 0) cost = 1;
int count = 0;
QVariant value;
int horizontalwavelength = (config && config->getProperty("horizontalwavelength", value)) ? value.toInt() : 50;
int horizontalshift = (config && config->getProperty("horizontalshift", value)) ? value.toInt() : 50;
int horizontalamplitude = (config && config->getProperty("horizontalamplitude", value)) ? value.toInt() : 4;
int horizontalshape = (config && config->getProperty("horizontalshape", value)) ? value.toInt() : 0;
int verticalwavelength = (config && config->getProperty("verticalwavelength", value)) ? value.toInt() : 50;
int verticalshift = (config && config->getProperty("verticalshift", value)) ? value.toInt() : 50;
int verticalamplitude = (config && config->getProperty("verticalamplitude", value)) ? value.toInt() : 4;
int verticalshape = (config && config->getProperty("verticalshape", value)) ? value.toInt() : 0;
KisSequentialIterator dstIt(device, applyRect);
KisWaveCurve* verticalcurve;
if (verticalshape == 1)
verticalcurve = new KisTriangleWaveCurve(verticalamplitude, verticalwavelength, verticalshift);
else
verticalcurve = new KisSinusoidalWaveCurve(verticalamplitude, verticalwavelength, verticalshift);
KisWaveCurve* horizontalcurve;
if (horizontalshape == 1)
horizontalcurve = new KisTriangleWaveCurve(horizontalamplitude, horizontalwavelength, horizontalshift);
else
horizontalcurve = new KisSinusoidalWaveCurve(horizontalamplitude, horizontalwavelength, horizontalshift);
KisRandomSubAccessorSP srcRSA = device->createRandomSubAccessor();
do {
double xv = horizontalcurve->valueAt(dstIt.y(), dstIt.x());
double yv = verticalcurve->valueAt(dstIt.x(), dstIt.y());
srcRSA->moveTo(QPointF(xv, yv));
srcRSA->sampledOldRawData(dstIt.rawData());
if (progressUpdater) progressUpdater->setProgress((++count) / cost);
} while (dstIt.nextPixel());
delete horizontalcurve;
delete verticalcurve;
}
QRect KisFilterWave::neededRect(const QRect& rect, const KisFilterConfigurationSP config, int lod) const
{
Q_UNUSED(lod);
QVariant value;
int horizontalamplitude = (config && config->getProperty("horizontalamplitude", value)) ? value.toInt() : 4;
int verticalamplitude = (config && config->getProperty("verticalamplitude", value)) ? value.toInt() : 4;
return rect.adjusted(-horizontalamplitude, -verticalamplitude, horizontalamplitude, verticalamplitude);
}
#include "wavefilter.moc"
|