Skip to content

armgit / krita

Public Imported from GitHub
0

Krita is a free and open source cross-platform application that offers an end-to-end solution for creating digital art files from scratch built on the KDE and Qt frameworks.

C++

Added Bayer dithering option to Gradient Tool

Jakub Nowakowskic9c44bee7713
diff --git a/krita/data/aboutdata/developers.txt b/krita/data/aboutdata/developers.txt
index 40999f8be7..d31703369d 100644
--- a/krita/data/aboutdata/developers.txt
+++ b/krita/data/aboutdata/developers.txt
@@ -92,6 +92,7 @@ Jaime
Jaime Torres
Jaison Lee
Jakob Petsovits
+Jakub Nowakowski
Jakub Stachowski
Jan Hambrecht
Jarosław Staniek
diff --git a/libs/image/kis_gradient_painter.cc b/libs/image/kis_gradient_painter.cc
index 712249778f..d9fb0b6b04 100644
--- a/libs/image/kis_gradient_painter.cc
+++ b/libs/image/kis_gradient_painter.cc
@@ -30,6 +30,7 @@
#include "kis_cached_gradient_shape_strategy.h"
#include "krita_utils.h"
#include "KoMixColorsOp.h"
+#include <KisDitherMaths.h>
#include <KisDitherOp.h>
#include <KoCachedGradient.h>
@@ -361,7 +362,7 @@ public:
double valueAt(double x, double y) const override;
protected:
- double m_vectorAngle;
+ double m_vectorAngle;
double m_radius;
};
@@ -404,6 +405,58 @@ double ReverseSpiralGradientStrategy::valueAt(double x, double y) const
};
+class DitheredGradientStrategy : public KisGradientShapeStrategy
+{
+
+public:
+ DitheredGradientStrategy(KisGradientPainter::enumDither dither, int ditherSteps, QSharedPointer<KisGradientShapeStrategy> shapeStrategy);
+
+ double valueAt(double x, double y) const override;
+
+protected:
+ KisGradientPainter::enumDither m_dither;
+ int m_ditherSteps;
+ QSharedPointer<KisGradientShapeStrategy> m_shapeStrategy;
+};
+
+DitheredGradientStrategy::DitheredGradientStrategy(KisGradientPainter::enumDither dither,
+ int ditherSteps,
+ QSharedPointer<KisGradientShapeStrategy> shapeStrategy)
+ : m_dither(dither)
+ , m_ditherSteps(ditherSteps)
+ , m_shapeStrategy(shapeStrategy)
+{
+}
+
+double DitheredGradientStrategy::valueAt(double x, double y) const
+{
+ double value = m_shapeStrategy->valueAt(x, y);
+ double ditherFactor = 0.0;
+
+ switch (m_dither) {
+ case KisGradientPainter::DitherBayer2:
+ ditherFactor = KisDitherMaths::dither_factor_bayer_2_normalized(x, y);
+ break;
+ case KisGradientPainter::DitherBayer4:
+ ditherFactor = KisDitherMaths::dither_factor_bayer_4_normalized(x, y);
+ break;
+ case KisGradientPainter::DitherBayer8:
+ ditherFactor = KisDitherMaths::dither_factor_bayer_8_normalized(x, y);
+ break;
+ default:
+ return value;
+ }
+
+ // Divide gradient into discrete steps and adjust value with dithering matrix.
+ value *= m_ditherSteps - 1;
+ value = std::floor(value + ditherFactor);
+
+ // Bring value to original scale.
+ value /= m_ditherSteps - 1;
+
+ return value;
+}
+
class GradientRepeatStrategy
{
public:
@@ -1095,7 +1148,8 @@ bool KisGradientPainter::paintGradient(const QPointF& gradientVectorStart,
qint32 starty,
qint32 width,
qint32 height,
- bool useDithering)
+ enumDither dither,
+ int ditherSteps)
{
return paintGradient(gradientVectorStart,
gradientVectorEnd,
@@ -1103,7 +1157,8 @@ bool KisGradientPainter::paintGradient(const QPointF& gradientVectorStart,
antiAliasThreshold,
reverseGradient,
QRect(startx, starty, width, height),
- useDithering);
+ dither,
+ ditherSteps);
}
bool KisGradientPainter::paintGradient(const QPointF& gradientVectorStart,
@@ -1112,7 +1167,8 @@ bool KisGradientPainter::paintGradient(const QPointF& gradientVectorStart,
double antiAliasThreshold,
bool reverseGradient,
const QRect &applyRect,
- bool useDithering)
+ enumDither dither,
+ int ditherSteps)
{
// The following combinations of options have aliasing artifacts
// where the first color meets the last color of the gradient.
@@ -1130,7 +1186,8 @@ bool KisGradientPainter::paintGradient(const QPointF& gradientVectorStart,
repeat,
antiAliasThreshold,
reverseGradient,
- useDithering,
+ dither,
+ ditherSteps,
applyRect,
paintPolicy);
@@ -1141,7 +1198,8 @@ bool KisGradientPainter::paintGradient(const QPointF& gradientVectorStart,
repeat,
antiAliasThreshold,
reverseGradient,
- useDithering,
+ dither,
+ ditherSteps,
applyRect,
paintPolicy);
@@ -1153,7 +1211,8 @@ bool KisGradientPainter::paintGradient(const QPointF& gradientVectorStart,
repeat,
antiAliasThreshold,
reverseGradient,
- useDithering,
+ dither,
+ ditherSteps,
applyRect,
paintPolicy);
}
@@ -1166,7 +1225,8 @@ bool KisGradientPainter::paintGradient(const QPointF& gradientVectorStart,
repeat,
antiAliasThreshold,
reverseGradient,
- useDithering,
+ dither,
+ ditherSteps,
applyRect,
paintPolicy);
}
@@ -1177,7 +1237,8 @@ bool KisGradientPainter::paintGradient(const QPointF& gradientVectorStart,
enumGradientRepeat repeat,
double antiAliasThreshold,
bool reverseGradient,
- bool useDithering,
+ enumDither dither,
+ int ditherSteps,
const QRect &applyRect,
T & paintPolicy)
{
@@ -1255,6 +1316,13 @@ bool KisGradientPainter::paintGradient(const QPointF& gradientVectorStart,
break;
}
+ if (isBayerDither(dither)) {
+ // Wrap each shape strategy in a dithering filter.
+ for (Private::ProcessRegion& region : m_d->processRegions) {
+ region.precalculatedShapeStrategy = toQShared(new DitheredGradientStrategy(dither, ditherSteps, region.precalculatedShapeStrategy));
+ }
+ }
+
GradientRepeatStrategy *repeatStrategy = 0;
switch (repeat) {
@@ -1290,7 +1358,7 @@ bool KisGradientPainter::paintGradient(const QPointF& gradientVectorStart,
tmp->setDefaultBounds(dev->defaultBounds());
tmp->clear();
- const KisDitherOp* op = mixCs->ditherOp(destCs->colorDepthId().id(), useDithering ? DITHER_BEST : DITHER_NONE);
+ const KisDitherOp* op = mixCs->ditherOp(destCs->colorDepthId().id(), dither == DitherBlueNoise ? DITHER_BEST : DITHER_NONE);
Q_FOREACH (const Private::ProcessRegion &r, m_d->processRegions) {
QRect processRect = r.processRect;
@@ -1342,3 +1410,10 @@ bool KisGradientPainter::paintGradient(const QPointF& gradientVectorStart,
return true;
}
+
+bool KisGradientPainter::isBayerDither(enumDither dither)
+{
+ return dither == KisGradientPainter::DitherBayer2 ||
+ dither == KisGradientPainter::DitherBayer4 ||
+ dither == KisGradientPainter::DitherBayer8;
+}
diff --git a/libs/image/kis_gradient_painter.h b/libs/image/kis_gradient_painter.h
index 79142ba70e..038c97a3e7 100644
--- a/libs/image/kis_gradient_painter.h
+++ b/libs/image/kis_gradient_painter.h
@@ -49,6 +49,16 @@ public:
GradientRepeatAlternate
};
+ enum enumDither {
+ DitherNone,
+ DitherBlueNoise,
+ DitherBayer2,
+ DitherBayer4,
+ DitherBayer8
+ };
+
+ static constexpr int defaultDitherSteps = 2;
+
void setGradientShape(enumGradientShape shape);
void precalculateShape();
@@ -65,7 +75,8 @@ public:
qint32 starty,
qint32 width,
qint32 height,
- bool useDithering = false);
+ enumDither dither = DitherNone,
+ int ditherSteps = defaultDitherSteps);
// convenience overload
bool paintGradient(const QPointF& gradientVectorStart,
@@ -74,7 +85,8 @@ public:
double antiAliasThreshold,
bool reverseGradient,
const QRect &applyRect,
- bool useDithering = false);
+ enumDither dither = DitherNone,
+ int ditherSteps = defaultDitherSteps);
template <class T>
bool paintGradient(const QPointF& gradientVectorStart,
@@ -82,10 +94,13 @@ public:
enumGradientRepeat repeat,
double antiAliasThreshold,
bool reverseGradient,
- bool useDithering,
+ enumDither dither,
+ int ditherSteps,
const QRect &applyRect,
T & paintPolicy);
+ static bool isBayerDither(enumDither dither);
+
private:
struct Private;
const QScopedPointer<Private> m_d;
diff --git a/libs/image/layerstyles/kis_ls_utils.cpp b/libs/image/layerstyles/kis_ls_utils.cpp
index d56817b629..3a454e6215 100644
--- a/libs/image/layerstyles/kis_ls_utils.cpp
+++ b/libs/image/layerstyles/kis_ls_utils.cpp
@@ -531,7 +531,7 @@ namespace KisLsUtils
repeat, 0.0,
config->reverse(),
applyRect,
- config->dither());
+ config->dither() ? KisGradientPainter::DitherBlueNoise : KisGradientPainter::DitherNone);
}
}
diff --git a/libs/pigment/KisDitherMaths.h b/libs/pigment/KisDitherMaths.h
index e3c4a872d2..24bd7ca23c 100644
--- a/libs/pigment/KisDitherMaths.h
+++ b/libs/pigment/KisDitherMaths.h
@@ -150,20 +150,55 @@ inline float dither_factor_blue_noise_64(int x, int y)
return m * (1.f / 4096.f) + (1.f / 8192.f);
}
-inline float dither_factor_bayer_8(int x, int y)
+/*
+ * Compute Bayer dithering matrices
+ *
+ * Bayer(n, x, y) = reverse(interleave(xor(x mod n, y mod n), x mod n))
+ *
+ * where
+ *
+ * n - order of square Bayer matrix (n x n)
+ * x, y - matrix coordinates
+ */
+
+inline float dither_factor_bayer_2_normalized(int x, int y)
{
y ^= x;
- /* Compute reverse(interleave(xor(x mod n, y mod n), x mod n))
- * Here n = 8 and `mod n` is the bottom 3 bits.
- */
+ // Here n = 2 and `mod n` is the bottom bit.
+ uint32_t m = ((y & 0x1) << 1) | (x & 0x1);
+
+ // m is in range [0, 3]. We scale it to [0, 3.0f/4.0f].
+ return static_cast<float>(m) * (1.f / 4.f);
+}
+
+inline float dither_factor_bayer_4_normalized(int x, int y)
+{
+ y ^= x;
+
+ // Here n = 4 and `mod n` is the bottom 2 bits.
+ uint32_t m = ((y & 0x1) << 3) | ((x & 0x1) << 2) | (y & 0x2) | ((x & 0x2) >> 1);
+
+ // m is in range [0, 15]. We scale it to [0, 15.0f/16.0f].
+ return static_cast<float>(m) * (1.f / 16.f);
+}
+
+inline float dither_factor_bayer_8_normalized(int x, int y)
+{
+ y ^= x;
+
+ // Here n = 8 and `mod n` is the bottom 3 bits.
uint32_t m = ((y & 0x1) << 5) | ((x & 0x1) << 4) | ((y & 0x2) << 2) | ((x & 0x2) << 1) | ((y & 0x4) >> 1) | ((x & 0x4) >> 2);
- /* m is in range [0, 63]. We scale it to [0, 63.0f/64.0f], then
- * shift it to to [1.0f/128.0f, 127.0f/128.0f] so that 0 < d < 1.
- * This ensures exact values are not changed by dithering.
- */
- return static_cast<float>(m) * (1.f / 64.f) + (1.f / 128.f);
+ // m is in range [0, 63]. We scale it to [0, 63.0f/64.0f].
+ return static_cast<float>(m) * (1.f / 64.f);
+}
+
+inline float dither_factor_bayer_8(int x, int y)
+{
+ // Shift normalized matrix value to [1.0f/128.0f, 127.0f/128.0f] so that 0 < d < 1.
+ // This ensures exact values are not changed by dithering.
+ return dither_factor_bayer_8_normalized(x, y) + (1.f / 128.f);
}
inline float apply_dither(float f, float d, float s)
diff --git a/libs/ui/widgets/kis_scratch_pad.cpp b/libs/ui/widgets/kis_scratch_pad.cpp
index 7633092a0a..bc6cf71490 100644
--- a/libs/ui/widgets/kis_scratch_pad.cpp
+++ b/libs/ui/widgets/kis_scratch_pad.cpp
@@ -1063,7 +1063,7 @@ void KisScratchPad::fillGradient(const QPoint &gradientVectorStart,
reverseGradient,
gradientRect.left(), gradientRect.top(),
gradientRect.width(), gradientRect.height(),
- dither);
+ dither ? KisGradientPainter::DitherBlueNoise : KisGradientPainter::DitherNone);
} else {
painter.paintGradient(gradientVectorStart,
gradientVectorEnd,
@@ -1072,7 +1072,7 @@ void KisScratchPad::fillGradient(const QPoint &gradientVectorStart,
reverseGradient,
gradientRect.left(), gradientRect.top(),
gradientRect.width(), gradientRect.height(),
- dither);
+ dither ? KisGradientPainter::DitherBlueNoise : KisGradientPainter::DitherNone);
}
t.end();
}
diff --git a/plugins/generators/gradient/KisGradientGenerator.cpp b/plugins/generators/gradient/KisGradientGenerator.cpp
index 019d0d84de..741b7b7399 100644
--- a/plugins/generators/gradient/KisGradientGenerator.cpp
+++ b/plugins/generators/gradient/KisGradientGenerator.cpp
@@ -52,7 +52,7 @@ void KisGradientGenerator::generate(KisProcessingInformation dst,
generatorConfiguration->antiAliasThreshold(),
generatorConfiguration->reverse(),
QRect(dst.topLeft(), size),
- generatorConfiguration->dither()
+ generatorConfiguration->dither() ? KisGradientPainter::DitherBlueNoise : KisGradientPainter::DitherNone
);
}
diff --git a/plugins/tools/basictools/kis_tool_gradient.cc b/plugins/tools/basictools/kis_tool_gradient.cc
index 119be91a40..e77ad1114a 100644
--- a/plugins/tools/basictools/kis_tool_gradient.cc
+++ b/plugins/tools/basictools/kis_tool_gradient.cc
@@ -58,7 +58,8 @@ KisToolGradient::KisToolGradient(KoCanvasBase * canvas)
m_startPos = QPointF(0, 0);
m_endPos = QPointF(0, 0);
- m_dither = false;
+ m_dither = KisGradientPainter::DitherNone;
+ m_ditherSteps = KisGradientPainter::defaultDitherSteps;
m_reverse = false;
m_shape = KisGradientPainter::GradientShapeLinear;
m_repeat = KisGradientPainter::GradientRepeatNone;
@@ -164,7 +165,8 @@ void KisToolGradient::endPrimaryAction(KoPointerEvent *event)
new KisCommandUtils::LambdaCommand(
[resources, startPos = m_startPos, endPos = m_endPos,
shape = m_shape, repeat = m_repeat, reverse = m_reverse,
- antiAliasThreshold = m_antiAliasThreshold, dither = m_dither] () mutable {
+ antiAliasThreshold = m_antiAliasThreshold, dither = m_dither,
+ ditherSteps = m_ditherSteps] () mutable {
KisNodeSP node = resources->currentNode();
KisPaintDeviceSP device = node->paintDevice();
@@ -182,7 +184,7 @@ void KisToolGradient::endPrimaryAction(KoPointerEvent *event)
repeat, antiAliasThreshold,
reverse, 0, 0,
bounds.width(), bounds.height(),
- dither);
+ dither, ditherSteps);
return painter.endAndTakeTransaction();
}));
@@ -262,6 +264,23 @@ QWidget* KisToolGradient::createOptionWidget()
addOptionWidgetOption(m_cmbRepeat, m_lbRepeat);
connect(m_cmbRepeat, SIGNAL(currentIndexChanged(int)), this, SLOT(slotSetRepeat(int)));
+ m_lbDither = new QLabel(i18n("Dither:"), widget);
+ m_cmbDither = new KComboBox(widget);
+ m_cmbDither->setObjectName("dither");
+ m_cmbDither->addItem(i18nc("The gradient will not be dithered", "None"));
+ m_cmbDither->addItem(i18nc("The gradient will be dithered with Blue Noise", "Blue Noise"));
+ m_cmbDither->addItem(i18nc("The gradient will be dithered with Bayer Matrix 2x2", "Bayer Matrix 2x2"));
+ m_cmbDither->addItem(i18nc("The gradient will be dithered with Bayer Matrix 4x4", "Bayer Matrix 4x4"));
+ m_cmbDither->addItem(i18nc("The gradient will be dithered with Bayer Matrix 8x8", "Bayer Matrix 8x8"));
+ addOptionWidgetOption(m_cmbDither, m_lbDither);
+ connect(m_cmbDither, SIGNAL(currentIndexChanged(int)), this, SLOT(slotSetDither(int)));
+
+ m_lbDitherSteps = new QLabel(i18n("Dither steps:"), widget);
+ m_sbDitherSteps = new KisIntParseSpinBox(widget);
+ m_sbDitherSteps->setObjectName("dither_steps");
+ m_sbDitherSteps->setRange(2, 10000);
+ addOptionWidgetOption(m_sbDitherSteps, m_lbDitherSteps);
+ connect(m_sbDitherSteps, SIGNAL(valueChanged(int)), this, SLOT(slotSetDitherSteps(int)));
m_lbAntiAliasThreshold = new QLabel(i18n("Anti-alias threshold:"), widget);
m_slAntiAliasThreshold = new KisDoubleSliderSpinBox(widget);
@@ -276,16 +295,10 @@ QWidget* KisToolGradient::createOptionWidget()
connect(m_ckReverse, SIGNAL(toggled(bool)), this, SLOT(slotSetReverse(bool)));
addOptionWidgetOption(m_ckReverse);
- m_ckDither = new QCheckBox(i18nc("the gradient will be dithered", "Dither"), widget);
- m_ckDither->setObjectName("dither_check");
- connect(m_ckDither, SIGNAL(toggled(bool)), this, SLOT(slotSetDither(bool)));
- addOptionWidgetOption(m_ckDither);
-
- widget->setFixedHeight(widget->sizeHint().height());
-
// load configuration settings into widget (updating UI will update internal variables from signals/slots)
- m_ckDither->setChecked(m_configGroup.readEntry<bool>("dither", false));
+ slotSetDither((int)m_configGroup.readEntry("dither", 0));
+ m_sbDitherSteps->setValue((int)m_configGroup.readEntry("ditherSteps", KisGradientPainter::defaultDitherSteps));
m_ckReverse->setChecked((bool)m_configGroup.readEntry("reverse", false));
m_cmbShape->setCurrentIndex((int)m_configGroup.readEntry("shape", 0));
m_cmbRepeat->setCurrentIndex((int)m_configGroup.readEntry("repeat", 0));
@@ -312,10 +325,22 @@ void KisToolGradient::slotSetReverse(bool state)
m_configGroup.writeEntry("reverse", state);
}
-void KisToolGradient::slotSetDither(bool state)
+void KisToolGradient::slotSetDither(int dither)
+{
+ if (m_cmbDither->currentIndex() != dither) {
+ m_cmbDither->setCurrentIndex(dither);
+ }
+
+ m_dither = static_cast<KisGradientPainter::enumDither>(dither);
+ m_configGroup.writeEntry("dither", dither);
+
+ showControl(m_sbDitherSteps, KisGradientPainter::isBayerDither(m_dither));
+}
+
+void KisToolGradient::slotSetDitherSteps(int value)
{
- m_dither = state;
- m_configGroup.writeEntry("dither", state);
+ m_ditherSteps = value;
+ m_configGroup.writeEntry("ditherSteps", value);
}
void KisToolGradient::slotSetAntiAliasThreshold(qreal value)
diff --git a/plugins/tools/basictools/kis_tool_gradient.h b/plugins/tools/basictools/kis_tool_gradient.h
index 2fa8369cee..d2210ad545 100644
--- a/plugins/tools/basictools/kis_tool_gradient.h
+++ b/plugins/tools/basictools/kis_tool_gradient.h
@@ -21,6 +21,7 @@
#include <kis_global.h>
#include <kis_types.h>
#include <kis_gradient_painter.h>
+#include <kis_int_parse_spin_box.h>
#include <flake/kis_node_shape.h>
#include <kis_icon.h>
#include <kconfig.h>
@@ -57,7 +58,8 @@ private Q_SLOTS:
void slotSetShape(int);
void slotSetRepeat(int);
void slotSetReverse(bool);
- void slotSetDither(bool);
+ void slotSetDither(int);
+ void slotSetDitherSteps(int);
void slotSetAntiAliasThreshold(qreal);
protected Q_SLOTS:
void resetCursorStyle() override;
@@ -82,13 +84,17 @@ private:
KisGradientPainter::enumGradientShape m_shape;
KisGradientPainter::enumGradientRepeat m_repeat;
- bool m_dither {false};
+ KisGradientPainter::enumDither m_dither {KisGradientPainter::DitherNone};
+ int m_ditherSteps {KisGradientPainter::defaultDitherSteps};
bool m_reverse {false};
double m_antiAliasThreshold {0.0};
QLabel *m_lbShape {nullptr};
QLabel *m_lbRepeat {nullptr};
- QCheckBox *m_ckDither {nullptr};
+ QLabel *m_lbDither {nullptr};
+ KComboBox *m_cmbDither {nullptr};
+ QLabel *m_lbDitherSteps {nullptr};
+ KisIntParseSpinBox *m_sbDitherSteps {nullptr};
QCheckBox *m_ckReverse {nullptr};
KComboBox *m_cmbShape {nullptr};
KComboBox *m_cmbRepeat {nullptr};