Commit 3dc39758 authored by Wolthera van Hövell's avatar Wolthera van Hövell 🛍

Simplify Gradient Map filter and make it usable as a filter layer.

There's some awkwardness with the first two generic gradients as the color isn't stored, but this is good for now.

Fixes T1837
parent 4a4f704e
set(kritagradientmap_SOURCES gradientmap.cpp krita_gradient_map_color_transformation.cpp krita_filter_gradient_map.cpp)
set(kritagradientmap_SOURCES gradientmap.cpp
krita_filter_gradient_map.cpp)
ki18n_wrap_ui(kritagradientmap_SOURCES wdg_gradientmap.ui)
......
......@@ -22,7 +22,7 @@
#include <kpluginfactory.h>
#include <filter/kis_filter_registry.h>
#include "krita_filter_gradient_map.h"
#include "krita_gradient_map_color_transformation.h"
#include "KoResourceServerProvider.h"
#include "kis_config_widget.h"
......@@ -51,16 +51,12 @@ void KritaGradientMapConfigWidget::gradientResourceChanged(KoResource* gradient)
Q_UNUSED(gradient)
}
KritaGradientMapFilterConfiguration* KritaGradientMapConfigWidget::configuration() const
KisFilterConfiguration* KritaGradientMapConfigWidget::configuration() const
{
KritaGradientMapFilterConfiguration* cfg = new KritaGradientMapFilterConfiguration();
const KoResource* gradient;
if (!(gradient = m_page->gradientchooser->currentResource()))
{
m_page->gradientchooser->setCurrentItem(0, 0);
gradient = m_page->gradientchooser->currentResource();
}
cfg->setGradient(gradient);
KisFilterConfiguration* cfg = new KisFilterConfiguration("gradientmap", 1);
QString gradientName;
gradientName = m_page->gradientchooser->currentResource()->name();
cfg->setProperty("gradientName", gradientName);
return cfg;
}
......@@ -69,40 +65,16 @@ KritaGradientMapFilterConfiguration* KritaGradientMapConfigWidget::configuration
void KritaGradientMapConfigWidget::setConfiguration(const KisPropertiesConfiguration* config)
{
const KritaGradientMapFilterConfiguration* cfg = dynamic_cast<const KritaGradientMapFilterConfiguration* > (config);
Q_ASSERT(cfg);
Q_UNUSED(cfg);
m_page->gradientchooser->setCurrentResource(KoResourceServerProvider::instance()->gradientServer(false)->resourceByName(config->getString("gradientName")));
Q_ASSERT(config);
Q_UNUSED(config);
}
void KritaGradientMapConfigWidget::setView(KisViewManager *view)
{
Q_UNUSED(view)
}
//-----------------------------
KritaGradientMapFilterConfiguration::KritaGradientMapFilterConfiguration()
: KisColorTransformationConfiguration("gradientmap", 1)
{
}
KritaGradientMapFilterConfiguration::~KritaGradientMapFilterConfiguration()
{
}
void KritaGradientMapFilterConfiguration::setGradient(const KoResource* gradient)
{
m_gradient = gradient;
}
const KoResource* KritaGradientMapFilterConfiguration::gradient() const
{
return m_gradient;
}
//-----------------------------
//------------------------------
KritaGradientMap::KritaGradientMap(QObject *parent, const QVariantList &)
: QObject(parent)
{
......
......@@ -36,20 +36,6 @@ public:
}
};
class KritaGradientMapFilterConfiguration : public KisColorTransformationConfiguration
{
public:
KritaGradientMapFilterConfiguration();
virtual ~KritaGradientMapFilterConfiguration();
virtual void setGradient(const KoResource* gradient);
virtual const KoResource* gradient() const;
private:
KoResource const* m_gradient;
};
class KritaGradientMapConfigWidget : public KisConfigWidget
{
Q_OBJECT
......@@ -57,7 +43,7 @@ public:
KritaGradientMapConfigWidget(QWidget *parent, KisPaintDeviceSP dev, Qt::WFlags f = 0);
virtual ~KritaGradientMapConfigWidget();
virtual KritaGradientMapFilterConfiguration* configuration() const;
virtual KisFilterConfiguration *configuration() const;
virtual void setConfiguration(const KisPropertiesConfiguration* config);
WdgGradientMap * m_page;
void setView(KisViewManager *view);
......
......@@ -18,28 +18,78 @@
*/
#include "krita_filter_gradient_map.h"
#include "KoColorSpace.h"
#include <KoColorSpace.h>
#include <KoColor.h>
#include <kis_paint_device.h>
#include <kis_global.h>
#include <kis_types.h>
#include "kis_config_widget.h"
#include "filter/kis_color_transformation_filter.h"
#include <KoResourceServerProvider.h>
#include <KoResourceServer.h>
#include <KoAbstractGradient.h>
#include <KoColorSet.h>
#include "gradientmap.h"
#include "krita_gradient_map_color_transformation.h"
#include <kis_sequential_iterator.h>
KritaFilterGradientMap::KritaFilterGradientMap() : KisColorTransformationFilter(id(), categoryMap(), i18n("&Gradient Map"))
KritaFilterGradientMap::KritaFilterGradientMap() : KisFilter(id(), categoryMap(), i18n("&Gradient Map"))
{
setColorSpaceIndependence(TO_LAB16);
setColorSpaceIndependence(FULLY_INDEPENDENT);
setShowConfigurationWidget(true);
// don't support anything just yet because of thread safety problems
setSupportsPainting(false);
setSupportsAdjustmentLayers(false);
setSupportsThreading(false);
setSupportsLevelOfDetail(true);
setSupportsPainting(true);
setSupportsAdjustmentLayers(true);
setSupportsThreading(true);
}
KoColorTransformation* KritaFilterGradientMap::createTransformation(const KoColorSpace* cs, const KisFilterConfiguration* config) const
void KritaFilterGradientMap::processImpl(KisPaintDeviceSP device,
const QRect& applyRect,
const KisFilterConfiguration *config,
KoUpdater *progressUpdater) const
{
auto gradient = static_cast<const KoAbstractGradient *>(dynamic_cast<const KritaGradientMapFilterConfiguration *>(config)->gradient());
Q_ASSERT(!device.isNull());
if (progressUpdater) {
progressUpdater->setRange(0, applyRect.height() * applyRect.width());
}
KoAbstractGradient *gradient = KoResourceServerProvider::instance()->gradientServer(false)->resourceByName(config->getString("gradientName"));
KoColorSet *gradientCache = new KoColorSet();
for (int i=0; i<256; i++) {
KoColor gc;
gradient->colorAt(gc, ((qreal)i/255.0));
KoColorSetEntry col;
col.color = gc;
gradientCache->add(col);
}
return new KritaGradientMapColorTransformation(gradient, cs);
KoColor outColor(Qt::white, device->colorSpace());
KisSequentialIterator it(device, applyRect);
int p = 0;
quint8 grey;
const int pixelSize = device->colorSpace()->pixelSize();
do {
grey = device->colorSpace()->intensity8(it.oldRawData());
outColor = gradientCache->getColor((quint32)grey).color;
outColor.convertTo(device->colorSpace());
memcpy(it.rawData(), outColor.data(), pixelSize);
if (progressUpdater) progressUpdater->setValue(p++);
} while (it.nextPixel());
}
KisFilterConfiguration *KritaFilterGradientMap::factoryConfiguration(const KisPaintDeviceSP) const
{
KisFilterConfiguration *config = new KisFilterConfiguration("gradientmap", 1);
KoAbstractGradient *gradient = KoResourceServerProvider::instance()->gradientServer(false)->resources().first();
config->setProperty("gradientName", gradient->name());
return config;
}
KisConfigWidget * KritaFilterGradientMap::createConfigurationWidget(QWidget * parent, const KisPaintDeviceSP dev) const
......
......@@ -18,24 +18,36 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#pragma once
#ifndef KIS_GRADIENT_MAP_H
#define KIS_GRADIENT_MAP_H
#include "kis_paint_device.h"
#include "filter/kis_filter.h"
#include "filter/kis_color_transformation_filter.h"
#include <kis_global.h>
#include <kis_types.h>
#include <kis_paint_device.h>
#include <filter/kis_filter.h>
#include <kis_filter_configuration.h>
#include <kis_config_widget.h>
#include <KoUpdater.h>
class KritaFilterGradientMap : public KisColorTransformationFilter
class KritaFilterGradientMap : public KisFilter
{
public:
KritaFilterGradientMap();
public:
virtual KoColorTransformation* createTransformation(const KoColorSpace* cs, const KisFilterConfiguration* config) const;
static inline KoID id() {
return KoID("gradientmap", i18n("Gradient Map"));
}
virtual void processImpl(KisPaintDeviceSP device,
const QRect& applyRect,
const KisFilterConfiguration* config,
KoUpdater *progressUpdater) const;
virtual KisFilterConfiguration *factoryConfiguration(const KisPaintDeviceSP) const;
virtual KisConfigWidget* createConfigurationWidget(QWidget* parent, const KisPaintDeviceSP dev) const;
};
#endif
/*
* This file is part of the KDE project
*
* Copyright (c) 2016 Spencer Brown <sbrown655@gmail.com>
* 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 "krita_gradient_map_color_transformation.h"
#include "KoColor.h"
#include "KoColorSpaceMaths.h"
KritaGradientMapColorTransformation::KritaGradientMapColorTransformation(const KoAbstractGradient * gradient, const KoColorSpace * cs)
: m_gradient(gradient),
m_colorSpace(cs),
m_psize(cs->pixelSize())
{
}
void KritaGradientMapColorTransformation::transform(const quint8 * src, quint8 * dst, qint32 nPixels) const
{
quint16 srcPixel[4];
qreal grey;
KoColor outColor(m_colorSpace);
while (nPixels--)
{
m_colorSpace->toLabA16(src,reinterpret_cast<quint8 *>(srcPixel),1);
grey = qreal(srcPixel[0]) / KoColorSpaceMathsTraits<quint16>::max;
m_gradient->colorAt(outColor, grey);
memcpy(dst, outColor.data(), m_psize);
src += m_psize;
dst += m_psize;
}
}
/*
* This file is part of Krita
*
* Copyright (c) 2016 Spencer Brown <sbrown655@gmail.com>
*
* 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.
*/
#pragma once
#include "KoColorTransformation.h"
#include "KoAbstractGradient.h"
class KritaGradientMapColorTransformation : public KoColorTransformation
{
public:
KritaGradientMapColorTransformation(const KoAbstractGradient * gradient, const KoColorSpace* cs);
virtual void transform(const quint8* src, quint8* dst, qint32 nPixels) const;
private:
const KoAbstractGradient* m_gradient;
const KoColorSpace* m_colorSpace;
const size_t m_psize;
};
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment