Commit 3f20831b authored by C. Boemann's avatar C. Boemann
Browse files

Improve painting of gradients by caching the values. It now takes about 40% of what is used to

for a 4000x4000 8bit image
parent 2f0e1efb
......@@ -22,6 +22,7 @@
#include <KoColorSpace.h>
#include <KoAbstractGradient.h>
#include <KoCachedGradient.h>
#include <KoProgressUpdater.h>
#include <KoUpdater.h>
......@@ -469,6 +470,8 @@ KisGradientPainter::KisGradientPainter(KisPaintDeviceSP device, KisSelectionSP s
{
}
#include <QTime>
bool KisGradientPainter::paintGradient(const QPointF& gradientVectorStart,
const QPointF& gradientVectorEnd,
enumGradientShape shape,
......@@ -547,11 +550,10 @@ bool KisGradientPainter::paintGradient(const QPointF& gradientVectorStart,
KisHLineIteratorSP hit = dev->createHLineIteratorNG(startx, starty, width);
KisProgressUpdateHelper progressHelper(progressUpdater(), 100, height);
KoCachedGradient cachedGradient(gradient(), qMax(endy-starty, endx - startx), colorSpace);
for (int y = starty; y <= endy; y++) {
for (int x = startx; x <= endx; x++) {
double t = shapeStrategy->valueAt(x, y);
t = repeatStrategy->valueAt(t);
......@@ -559,8 +561,7 @@ bool KisGradientPainter::paintGradient(const QPointF& gradientVectorStart,
t = 1 - t;
}
gradient()->colorAt(color, t);
memcpy(hit->rawData(), color.data(), pixelSize);
memcpy(hit->rawData(), cachedGradient.cachedAt(t), pixelSize);
hit->nextPixel();
}
......@@ -571,6 +572,5 @@ bool KisGradientPainter::paintGradient(const QPointF& gradientVectorStart,
bitBlt(startx, starty, dev, startx, starty, width, height);
delete shapeStrategy;
return true;
}
......@@ -65,6 +65,7 @@ set(pigmentcms_SRCS
resources/KoResource.cpp
resources/KoStopGradient.cpp
resources/KoSegmentGradient.cpp
resources/KoCachedGradient.cpp
)
set(PIGMENT_INSTALL_FILES
......
/*
Copyright (C) 2005 Tim Beaulen <tbscope@gmail.org>
Copyright (C) 2007 Jan Hambrecht <jaham@gmx.net>
Copyright (c) 2007 Sven Langkamp <sven.langkamp@gmail.com>
Copyright (c) 2013 C. Boemann <cbo@boemann.dk>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "KoCachedGradient.h"
#include <cfloat>
#include <QColor>
#include <klocale.h>
#include <kdebug.h>
#include "KoColorSpaceRegistry.h"
#include <KoColorModelStandardIds.h>
struct KoCachedGradient::Private {
const KoAbstractGradient *subject;
const KoColorSpace *colorSpace;
qint32 max;
KoColor *colors;
};
KoCachedGradient::KoCachedGradient(const KoAbstractGradient *subject, qint32 steps, const KoColorSpace *cs)
: KoAbstractGradient(subject->filename())
, d(new Private)
{
d->subject = subject;
d->max = steps-1;
d->colors = new KoColor[steps];
d->colorSpace = cs;
KoColor tmpColor(d->colorSpace);
for(qint32 i = 0; i < steps; i++) {
d->subject->colorAt(tmpColor, qreal(i) / d->max);
d->colors[i] = tmpColor;
}
}
KoCachedGradient::~KoCachedGradient()
{
delete[] d->colors;
}
QGradient* KoCachedGradient::toQGradient() const
{
return d->subject->toQGradient();
}
quint8 *KoCachedGradient::cachedAt(qreal t) const
{
qint32 tInt = t * d->max + 0.5;
return d->colors[tInt].data();
}
/*
Copyright (c) 2007 Sven Langkamp <sven.langkamp@gmail.com>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef KOCACHEDGRADIENT_H
#define KOCACHEDGRADIENT_H
#include <QGradient>
#include "KoAbstractGradient.h"
#include "KoColor.h"
#include "KoColorSpace.h"
#include "KoResource.h"
#include <pigment_export.h>
/**
* KoAbstractGradient is the base class of all gradient resources
*/
class PIGMENTCMS_EXPORT KoCachedGradient : public KoAbstractGradient
{
public:
explicit KoCachedGradient(const KoAbstractGradient *gradient, qint32 steps, const KoColorSpace *cs);
virtual ~KoCachedGradient();
/**
* Creates a QGradient from the gradient.
* The resulting QGradient might differ from original gradient
*/
virtual QGradient* toQGradient() const;
/// gets the color data at position 0 <= t <= 1
quint8 *cachedAt(qreal t) const;
void setColorSpace(KoColorSpace* colorSpace);
const KoColorSpace * colorSpace() const;
/* void setSpread(QGradient::Spread spreadMethod);
QGradient::Spread spread() const;
void setType(QGradient::Type repeatType);
QGradient::Type type() const;
QImage image() const;
void updatePreview();
QImage generatePreview(int width, int height) const;
*/
private:
struct Private;
Private* const d;
};
#endif // KOCACHEDGRADIENT_H
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