Commit 97796f04 authored by Carl Olsson's avatar Carl Olsson Committed by Boudewijn Rempt

Initial hacked-in gradientmap threshold blending.

parent 103d4b79
......@@ -144,20 +144,20 @@ QGradient* KoStopGradient::toQGradient() const
return gradient;
}
void KoStopGradient::colorAt(KoColor& dst, qreal t) const
bool KoStopGradient::stopsAt(KoGradientStop &leftStop, KoGradientStop &rightStop, qreal t) const
{
KoColor buffer;
if (! m_stops.count())
return;
return false;
if (t <= m_stops.first().first || m_stops.count() == 1) {
// we have only one stop or t is before the first stop
// -> use the color of the first stop
dst.fromKoColor(m_stops.first().second);
leftStop = m_stops.first();
rightStop = KoGradientStop(-std::numeric_limits<double>::infinity(), leftStop.second);
return true;
} else if (t >= m_stops.last().first) {
// t is after the last stop
// -> use the color of the last stop
dst.fromKoColor(m_stops.last().second);
rightStop = m_stops.last();
leftStop = KoGradientStop(std::numeric_limits<double>::infinity(), rightStop.second);
return true;
} else {
// we have at least two color stops
// -> find the two stops which frame our t
......@@ -170,14 +170,20 @@ void KoStopGradient::colorAt(KoColor& dst, qreal t) const
break;
}
//if ( *buffer.colorSpace() != *colorSpace()) {
// buffer = KoColor(colorSpace());
//}
//hack to get a color space with the bitdepth of the gradients(8bit), but with the colour profile of the image//
const KoColorSpace* mixSpace = KoColorSpaceRegistry::instance()->rgb8(dst.colorSpace()->profile());
leftStop = *(stop - 1);
rightStop = *(stop);
return true;
}
}
void KoStopGradient::colorAt(KoColor& dst, qreal t) const
{
KoColor buffer;
const KoGradientStop& leftStop = *(stop - 1);
const KoGradientStop& rightStop = *(stop);
KoGradientStop leftStop, rightStop;
if (!stopsAt(leftStop, rightStop, t)) return;
const KoColorSpace* mixSpace = KoColorSpaceRegistry::instance()->rgb8(dst.colorSpace()->profile());
KoColor startDummy, endDummy;
if (mixSpace){
......@@ -215,8 +221,8 @@ void KoStopGradient::colorAt(KoColor& dst, qreal t) const
colorSpace()->mixColorsOp()->mixColors(colors, colorWeights, 2, buffer.data());
}
dst.fromKoColor(buffer);
}
}
KoStopGradient * KoStopGradient::fromQGradient(const QGradient * gradient)
......
......@@ -51,6 +51,9 @@ public:
/// reimplemented
QGradient* toQGradient() const override;
/// Find stops surrounding position, returns false if position outside gradient
bool stopsAt(KoGradientStop& leftStop, KoGradientStop& rightStop, qreal t) const;
/// reimplemented
void colorAt(KoColor&, qreal t) const override;
......
......@@ -69,13 +69,28 @@ void KritaFilterGradientMap::processImpl(KisPaintDeviceSP device,
}
KoStopGradient gradient = KoStopGradient::fromXML(doc.firstChildElement());
bool threshold = true;
KoColor outColor(Qt::white, device->colorSpace());
KisSequentialIteratorProgress it(device, applyRect, progressUpdater);
quint8 grey;
qreal grey;
const int pixelSize = device->colorSpace()->pixelSize();
while (it.nextPixel()) {
grey = device->colorSpace()->intensity8(it.oldRawData());
gradient.colorAt(outColor,(qreal)grey/255);
grey = qreal(device->colorSpace()->intensity8(it.oldRawData())) / 255;
if (threshold) {
KoGradientStop leftStop, rightStop;
if (!gradient.stopsAt(leftStop, rightStop, grey)) continue;
qreal localT = (grey - leftStop.first) / (rightStop.first - leftStop.first);
if (localT < qreal(rand()) / qreal(RAND_MAX)) {
outColor = leftStop.second;
}
else {
outColor = rightStop.second;
}
}
else {
gradient.colorAt(outColor, grey);
}
outColor.setOpacity(qMin(KoColor(it.oldRawData(), device->colorSpace()).opacityF(), outColor.opacityF()));
outColor.convertTo(device->colorSpace());
memcpy(it.rawData(), outColor.data(), pixelSize);
......
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