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++

Implement KisColorPairSampler to sample colors for blending

Dmitry Kazakov6f9007b0e805
diff --git a/libs/pigment/tests/KisColorPairSampler.h b/libs/pigment/tests/KisColorPairSampler.h
new file mode 100644
index 0000000000..b19f07f47b
--- /dev/null
+++ b/libs/pigment/tests/KisColorPairSampler.h
@@ -0,0 +1,174 @@
+/*
+ * SPDX-FileCopyrightText: 2024 Dmitry Kazakov <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+#ifndef KISCOLORPAIRSAMPLER_H
+#define KISCOLORPAIRSAMPLER_H
+
+#include <boost/iterator/iterator_facade.hpp>
+
+#include <KoColor.h>
+#include <KoColorModelStandardIds.h>
+
+struct KisColorPairSampler
+{
+ std::vector<qreal> colorValues;
+ std::vector<qreal> alphaValues;
+
+ size_t numSamples() const {
+ return
+ alphaValues.size() *
+ alphaValues.size() *
+ alphaValues.size() *
+ colorValues.size() *
+ colorValues.size();
+ }
+
+ struct SampleIndex {
+ SampleIndex(size_t index, const KisColorPairSampler &sampler)
+ {
+ KIS_ASSERT(index >= 0);
+ KIS_ASSERT(index < sampler.numSamples());
+
+ size_t rest = index;
+
+ dstColorIndex = rest % sampler.colorValues.size();
+ rest /= sampler.colorValues.size();
+
+ srcColorIndex = rest % sampler.colorValues.size();
+ rest /= sampler.colorValues.size();
+
+ dstAlphaIndex = rest % sampler.alphaValues.size();
+ rest /= sampler.alphaValues.size();
+
+ srcAlphaIndex = rest % sampler.alphaValues.size();
+ rest /= sampler.alphaValues.size();
+
+ KIS_ASSERT(rest < sampler.alphaValues.size());
+ opacityIndex = rest;
+ }
+
+ size_t opacityIndex = 0;
+ size_t srcAlphaIndex = 0;
+ size_t dstAlphaIndex = 0;
+ size_t srcColorIndex = 0;
+ size_t dstColorIndex = 0;
+ };
+
+ class const_iterator
+ : public boost::iterator_facade <const_iterator,
+ boost::none_t,
+ boost::random_access_traversal_tag,
+ boost::none_t>
+ {
+ public:
+ const_iterator()
+ : m_sampler(0),
+ m_index(0) {}
+
+ const_iterator(const KisColorPairSampler *sampler, int index)
+ : m_sampler(sampler),
+ m_index(index) {}
+
+ KoColor srcColor(const KoColorSpace *colorSpace) const {
+ SampleIndex idx(m_index, *m_sampler);
+ return createColor(m_sampler->colorValues[idx.srcColorIndex],
+ m_sampler->alphaValues[idx.srcAlphaIndex],
+ colorSpace);
+
+ }
+
+ KoColor dstColor(const KoColorSpace *colorSpace) const {
+ SampleIndex idx(m_index, *m_sampler);
+ return createColor(m_sampler->colorValues[idx.dstColorIndex],
+ m_sampler->alphaValues[idx.dstAlphaIndex],
+ colorSpace);
+ }
+
+ qreal opacity() const {
+ SampleIndex idx(m_index, *m_sampler);
+ return m_sampler->alphaValues[idx.opacityIndex];
+ }
+
+ private:
+ KoColor createColorU (qreal colorF, qreal alphaF, const KoColorSpace *colorSpace) const {
+ using namespace Arithmetic;
+
+ /**
+ * Some composite ops reply on the halfValue to decide about the
+ * processing they do, so we should make sure that float and uint16
+ * modes use the same half-point in the test
+ */
+ const quint16 color = qFuzzyCompare(colorF, 0.5) ?
+ KoColorSpaceMathsTraits<quint16>::halfValue :
+ qRound(qBound(0.0, colorF, 1.0) * unitValue<quint16>());
+ const quint16 alpha = qRound(qBound(0.0, alphaF, 1.0) * unitValue<quint16>());
+
+ KoColor c(colorSpace);
+ quint16 *ptr = reinterpret_cast<quint16*>(c.data());
+ ptr[2] = color;
+ ptr[3] = alpha;
+ return c;
+ };
+
+ KoColor createColorF(qreal color, qreal alpha, const KoColorSpace *colorSpace) const {
+ KoColor c(colorSpace);
+ float *ptr = reinterpret_cast<float*>(c.data());
+ ptr[0] = color;
+ ptr[3] = alpha;
+ return c;
+ };
+
+ KoColor createColor(qreal color, qreal alpha, const KoColorSpace *colorSpace) const {
+ KoColor result;
+
+ if (colorSpace->colorDepthId() == Float32BitsColorDepthID) {
+ result = createColorF(color, alpha, colorSpace);
+ } else if (colorSpace->colorDepthId() == Integer16BitsColorDepthID) {
+ result = createColorU(color, alpha, colorSpace);
+ } else {
+ qFatal("bit depth is not implemented");
+ }
+ return result;
+ };
+
+ private:
+ friend class boost::iterator_core_access;
+
+ void increment() {
+ m_index++;
+ }
+
+ bool equal(const_iterator const& other) const {
+ return m_index == other.m_index &&
+ m_sampler == other.m_sampler;
+ }
+
+ void decrement() {
+ m_index--;
+ }
+
+ void advance(difference_type n) {
+ m_index += n;
+ }
+
+ difference_type distance_to(const const_iterator &rhs) {
+ return rhs.m_index - m_index;
+ }
+
+ private:
+ const KisColorPairSampler *m_sampler;
+ size_t m_index;
+ };
+
+ const_iterator begin() const {
+ return const_iterator(this, 0);
+ }
+ const_iterator end() const {
+ return const_iterator(this, numSamples());
+ }
+};
+
+
+#endif // KISCOLORPAIRSAMPLER_H
diff --git a/libs/pigment/tests/TestCompositeOpInversion.cpp b/libs/pigment/tests/TestCompositeOpInversion.cpp
index fc4813ed7f..aef109b6d6 100644
--- a/libs/pigment/tests/TestCompositeOpInversion.cpp
+++ b/libs/pigment/tests/TestCompositeOpInversion.cpp
@@ -228,6 +228,31 @@ void TestCompositeOpInversion::testBurnInF32Mode()
}
+#include <KisColorPairSampler.h>
+
+void TestCompositeOpInversion::testColorPairSampler()
+{
+ const KoColorSpace* csU = KoColorSpaceRegistry::instance()->colorSpace(RGBAColorModelID.id(), Integer16BitsColorDepthID.id(), 0);
+ const KoColorSpace* csF = KoColorSpaceRegistry::instance()->colorSpace(RGBAColorModelID.id(), Float32BitsColorDepthID.id(), 0);
+
+ KisColorPairSampler sampler;
+
+ sampler.alphaValues = {0.0, 0.3, 0.9};
+ sampler.colorValues = {0.1, 0.4, 0.6, 1.0};
+
+ KisColorPairSampler::const_iterator it = sampler.begin();
+ KisColorPairSampler::const_iterator end = sampler.end();
+
+ QCOMPARE(sampler.numSamples(), 3 * 3 * 3 * 4 * 4);
+ QCOMPARE(std::distance(it, end), sampler.numSamples());
+
+ for (; it != end; ++it) {
+ qDebug() << ppVar(it.opacity()) << ppVar(it.srcColor(csF)) << ppVar(it.dstColor(csF));
+ }
+}
+
+
+
namespace {
struct Wrapper {
KoColor color;
diff --git a/libs/pigment/tests/TestCompositeOpInversion.h b/libs/pigment/tests/TestCompositeOpInversion.h
index 9e4bfffd5c..c85e74d151 100644
--- a/libs/pigment/tests/TestCompositeOpInversion.h
+++ b/libs/pigment/tests/TestCompositeOpInversion.h
@@ -18,6 +18,8 @@ private Q_SLOTS:
void testBurnInF32Mode();
+ void testColorPairSampler();
+
void testFloatModes();
void testFloatModes_data();
};
diff --git a/libs/pigment/tests/TestKoColorSpaceSanity.cpp b/libs/pigment/tests/TestKoColorSpaceSanity.cpp
index fc133d886f..71876d3b13 100644
--- a/libs/pigment/tests/TestKoColorSpaceSanity.cpp
+++ b/libs/pigment/tests/TestKoColorSpaceSanity.cpp
@@ -50,4 +50,12 @@ void TestKoColorSpaceSanity::testChannelsInfo()
}
}
+
+
+
+void TestKoColorSpaceSanity::testIterator()
+{
+
+}
+
KISTEST_MAIN(TestKoColorSpaceSanity)
diff --git a/libs/pigment/tests/TestKoColorSpaceSanity.h b/libs/pigment/tests/TestKoColorSpaceSanity.h
index 7c6f2a9a54..a449d77363 100644
--- a/libs/pigment/tests/TestKoColorSpaceSanity.h
+++ b/libs/pigment/tests/TestKoColorSpaceSanity.h
@@ -15,6 +15,7 @@ class TestKoColorSpaceSanity : public QObject
private Q_SLOTS:
void testChannelsInfo();
+ void testIterator();
};
#endif