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 proof of concept for drawing HDR reference images.

Wolthera van Hövell tot Westerflier74e3885b16db
diff --git a/libs/pigment/KoColorSpaceRegistry.cpp b/libs/pigment/KoColorSpaceRegistry.cpp
index 7c5ed639d8..0c5e00258b 100644
--- a/libs/pigment/KoColorSpaceRegistry.cpp
+++ b/libs/pigment/KoColorSpaceRegistry.cpp
@@ -724,6 +724,84 @@ const KoColorProfile *KoColorSpaceRegistry::profileFor(const QVector<double> &co
return nullptr;
}
+#include <QColorSpace>
+
+static QMap<QColorSpace::ColorModel, QString> mapQColorSpaceModelToId {
+ {QColorSpace::ColorModel::Rgb, RGBAColorModelID.id()},
+ {QColorSpace::ColorModel::Cmyk, CMYKAColorModelID.id()},
+ {QColorSpace::ColorModel::Gray, GrayAColorModelID.id()},
+};
+
+static QMap<QColorSpace::Primaries, ColorPrimaries> mapQColorSpaceColorPrimaries {
+ {QColorSpace::Primaries::SRgb, PRIMARIES_ITU_R_BT_709_5},
+ {QColorSpace::Primaries::AdobeRgb, PRIMARIES_ADOBE_RGB_1998},
+ {QColorSpace::Primaries::DciP3D65, PRIMARIES_SMPTE_EG_432_1},
+ {QColorSpace::Primaries::ProPhotoRgb, PRIMARIES_PROPHOTO},
+ {QColorSpace::Primaries::Bt2020, PRIMARIES_ITU_R_BT_2020_2_AND_2100_0},
+ {QColorSpace::Primaries::Custom, PRIMARIES_UNSPECIFIED}
+};
+
+static QMap<QColorSpace::TransferFunction, TransferCharacteristics> mapQColorSpaceColorTransfer {
+ {QColorSpace::TransferFunction::Linear, TRC_LINEAR},
+ {QColorSpace::TransferFunction::SRgb, TRC_IEC_61966_2_1},
+ {QColorSpace::TransferFunction::ProPhotoRgb, TRC_PROPHOTO},
+ {QColorSpace::TransferFunction::Bt2020, TRC_ITU_R_BT_2020_2_12bit},
+#if QT_VERSION >= QT_VERSION_CHECK(6, 8, 0)
+ {QColorSpace::TransferFunction::St2084, TRC_ITU_R_BT_2100_0_PQ},
+ {QColorSpace::TransferFunction::Hlg, TRC_ITU_R_BT_2100_0_HLG},
+#endif
+ {QColorSpace::TransferFunction::Custom, TRC_UNSPECIFIED}
+};
+
+const KoColorProfile *KoColorSpaceRegistry::profileForQColorSpace(const QColorSpace &space)
+{
+ // We want to test RGB before testing wether we are dealing with a LUT, because we want to funnel rec2020 pq into loading our profile.
+ if (space.colorModel() == QColorSpace::ColorModel::Rgb) {
+ if (space.transferFunction() == QColorSpace::TransferFunction::Gamma) {
+ // load icc profile.
+ QByteArray profileData = space.iccProfile();
+ return createColorProfile(RGBAColorModelID.id(), Integer8BitsColorDepthID.id(), profileData);
+ }
+ ColorPrimaries primaries = mapQColorSpaceColorPrimaries.value(space.primaries(), PRIMARIES_UNSPECIFIED);
+ TransferCharacteristics transfer = mapQColorSpaceColorTransfer.value(space.transferFunction(), TRC_UNSPECIFIED);
+ // In qt6.9 we can get the 'primary points'.
+ QVector<double> colorants;
+ if ((!colorants.isEmpty() || primaries != PRIMARIES_UNSPECIFIED) && transfer != TRC_UNSPECIFIED) {
+ return profileFor(colorants, primaries, transfer);
+ }
+ } else if (space.colorModel() == QColorSpace::ColorModel::Gray) {
+ TransferCharacteristics transfer = mapQColorSpaceColorTransfer.value(space.transferFunction(), TRC_UNSPECIFIED);
+ if (space.transferFunction() == QColorSpace::TransferFunction::Gamma) {
+ QByteArray profileData = space.iccProfile();
+ return createColorProfile(RGBAColorModelID.id(), Integer8BitsColorDepthID.id(), profileData);
+
+ }
+ QVector<double> colorants;
+ colorants << space.whitePoint().x() << space.whitePoint().y();
+ if (!colorants.isEmpty() && transfer != TRC_UNSPECIFIED) {
+ return profileFor(colorants, PRIMARIES_UNSPECIFIED, transfer);
+ }
+ }
+ KoColorSpaceEngine *engine = KoColorSpaceEngineRegistry::instance()->get("icc");
+ if (space.isValid() && engine) {
+ // byte array from icc profile.
+ QByteArray profileData = space.iccProfile();
+ return engine->addProfile(profileData);
+ }
+
+ return nullptr;
+}
+
+QColorSpace KoColorSpaceRegistry::QColorSpaceForProfile(const KoColorProfile *profile) const
+{
+#if QT_VERSION >= QT_VERSION_CHECK(6, 8, 0)
+ if (profile == p2020PQProfile()) {
+ return QColorSpace(QColorSpace::Bt2100Pq);
+ }
+#endif
+ return QColorSpace::fromIccProfile(profile->rawData());
+}
+
QList<KoID> KoColorSpaceRegistry::colorModelsList(ColorSpaceListVisibility option) const
{
QReadLocker l(&d->registrylock);
diff --git a/libs/pigment/KoColorSpaceRegistry.h b/libs/pigment/KoColorSpaceRegistry.h
index f5314d4746..5b09aa05e1 100644
--- a/libs/pigment/KoColorSpaceRegistry.h
+++ b/libs/pigment/KoColorSpaceRegistry.h
@@ -23,6 +23,7 @@ class KoColorProfile;
class KoColorConversionSystem;
class KoColorConversionCache;
class KoColorConversionTransformation;
+class QColorSpace;
/**
* The registry for colorspaces and profiles.
@@ -346,6 +347,24 @@ public:
*/
const KoColorProfile *profileFor(const QVector<double> &colorants, ColorPrimaries colorPrimaries, TransferCharacteristics transferFunction) const;
+ /**
+ * @brief profileForQColorSpace
+ * Find a KoColorProfile that matches a given QColorSpace.
+ * This will use the QColorSpace::IccProfile function as a
+ * last resort, preferring to use preexisting profiles in the
+ * registery.
+ * @param space -- space to return the profile for.
+ * @return A Profile for the given QColorSpace, may be null if nothing was found.
+ */
+ const KoColorProfile *profileForQColorSpace(const QColorSpace &space);
+
+ /**
+ * @brief QColorSpaceForProfile
+ * Generate a QColorSpace for the given KoColorProfile.
+ * @param profile -- source profile
+ * @return a QColorSpace loaded from the profile. May not be RGB!
+ */
+ QColorSpace QColorSpaceForProfile(const KoColorProfile *profile) const;
/**
* @return the list of available color models
*/
diff --git a/libs/ui/KisReferenceImage.cpp b/libs/ui/KisReferenceImage.cpp
index 63cb344a92..c7928148f7 100644
--- a/libs/ui/KisReferenceImage.cpp
+++ b/libs/ui/KisReferenceImage.cpp
@@ -5,6 +5,7 @@
*/
#include "KisReferenceImage.h"
+#include "KoColorProfile.h"
#include "KoColorSpaceRegistry.h"
#include <QImage>
@@ -73,7 +74,17 @@ struct KisReferenceImage::Private : public QSharedData
if (image.isNull()) {
KisDocument * doc = KisPart::instance()->createTemporaryDocument();
if (doc->openPath(externalFilename, KisDocument::DontAddToRecent)) {
- image = doc->image()->convertToQImage(doc->image()->bounds(), 0);
+ if (doc->image()->colorSpace()->colorModelId() == RGBAColorModelID) {
+ if (doc->image()->colorSpace()->colorDepthId() == Float32BitsColorDepthID || doc->image()->colorSpace()->colorDepthId() == Float16BitsColorDepthID) {
+ // Because we store as PNG, we'll need to convert to PQ here. One problem: we need to check when saving the reference images.
+ const KoColorSpace *pqCs = KoColorSpaceRegistry::instance()->colorSpace(RGBAColorModelID.id(), Integer16BitsColorDepthID.id(), KoColorSpaceRegistry::instance()->p2020PQProfile());
+ doc->image()->convertImageColorSpace(pqCs, KoColorConversionTransformation::IntentRelativeColorimetric, KoColorConversionTransformation::internalConversionFlags());
+ }
+ image = doc->image()->convertToQImage(doc->image()->bounds(), doc->image()->colorSpace()->profile());
+ image.setColorSpace(KoColorSpaceRegistry::instance()->QColorSpaceForProfile(doc->image()->colorSpace()->profile()));
+ } else {
+ image = doc->image()->convertToQImage(doc->image()->bounds(), 0);
+ }
}
KisPart::instance()->removeDocument(doc);
}
@@ -81,7 +92,9 @@ struct KisReferenceImage::Private : public QSharedData
// See https://bugs.kde.org/show_bug.cgi?id=416515 -- a jpeg image
// loaded into a qimage cannot be saved to png unless we explicitly
// convert the colorspace of the QImage
- image.convertToColorSpace(QColorSpace(QColorSpace::SRgb));
+ if (image.colorSpace().colorModel() != QColorSpace::ColorModel::Rgb || !image.colorSpace().isValid()) {
+ image.convertToColorSpace(QColorSpace(QColorSpace::SRgb));
+ }
return (!image.isNull());
}
@@ -196,7 +209,12 @@ KisReferenceImage::fromPaintDevice(KisPaintDeviceSP src, const KisCoordinatesCon
}
auto *reference = new KisReferenceImage();
- reference->d->image = src->convertToQImage(KoColorSpaceRegistry::instance()->p709SRGBProfile());
+ if (src->colorSpace()->colorModelId() == RGBAColorModelID) {
+ reference->d->image = src->convertToQImage(src->colorSpace()->profile());
+ reference->d->image.setColorSpace(KoColorSpaceRegistry::instance()->QColorSpaceForProfile(src->colorSpace()->profile()));
+ } else {
+ reference->d->image = src->convertToQImage(KoColorSpaceRegistry::instance()->p709SRGBProfile());
+ }
QRect r = QRect(QPoint(), reference->d->image.size());
QSizeF size = converter.imageToDocument(r).size();
@@ -245,6 +263,23 @@ void KisReferenceImage::paint(QPainter &gc) const
QImage prescaled = d->mipmap.getClosestWithoutWorkaroundBorder(transform * devicePixelRatioFTransform * gc.transform(), &scale);
transform.scale(1.0 / scale, 1.0 / scale);
+ // Color conversion.
+ QImage *targetD = dynamic_cast<QImage*>(gc.device());
+ const bool getTargetCs = (targetD && targetD->colorSpace().colorModel() == QColorSpace::ColorModel::Rgb);
+ const KoColorProfile *targetP = getTargetCs? KoColorSpaceRegistry::instance()->profileForQColorSpace(targetD->colorSpace()): KoColorSpaceRegistry::instance()->p709SRGBProfile();
+ const KoColorProfile *sourceP = KoColorSpaceRegistry::instance()->profileForQColorSpace(d->image.colorSpace());
+ if (targetP != sourceP) {
+ KisPaintDeviceSP dev = new KisPaintDevice(KoColorSpaceRegistry::instance()->colorSpace(RGBAColorModelID.id(), Integer8BitsColorDepthID.id(), sourceP));
+ dev->convertFromQImage(prescaled, sourceP);
+ dev->convertTo(KoColorSpaceRegistry::instance()->colorSpace(RGBAColorModelID.id(), Integer8BitsColorDepthID.id(), targetP));
+ prescaled = dev->convertToQImage(targetP);
+ if (getTargetCs) {
+ prescaled.setColorSpace(targetD->colorSpace());
+ } else {
+ prescaled.setColorSpace(QColorSpace(QColorSpace::SRgb));
+ }
+ }
+
if (scale > 1.0) {
// enlarging should be done without smooth transformation
// so the user can see pixels just as they are painted
diff --git a/libs/ui/KisReferenceImagesDecoration.cpp b/libs/ui/KisReferenceImagesDecoration.cpp
index 18d9301666..7b960ec48c 100644
--- a/libs/ui/KisReferenceImagesDecoration.cpp
+++ b/libs/ui/KisReferenceImagesDecoration.cpp
@@ -13,6 +13,7 @@
#include "KisReferenceImagesLayer.h"
#include "kis_layer_utils.h"
#include <KoColorDisplayRendererInterface.h>
+#include <qcolorspace.h>
struct KisReferenceImagesDecoration::Private {
struct Buffer
@@ -67,7 +68,9 @@ private:
buffer.position = widgetRect.topLeft();
// to ensure that buffer is big enough for all the pixels on high dpi displays
// BUG 411118
+ QColorSpace space = buffer.image.colorSpace();
buffer.image = QImage((widgetRect.size()*devicePixelRatioF).toSize(), QImage::Format_ARGB32);
+ buffer.image.setColorSpace(space);
buffer.image.setDevicePixelRatio(devicePixelRatioF);
imageRect = q->view()->viewConverter()->widgetToImage(widgetRect);
@@ -133,7 +136,9 @@ void KisReferenceImagesDecoration::drawDecoration(QPainter &gc, const QRectF &/*
if (d->previousViewSize != viewSize || !KisAlgebra2D::fuzzyMatrixCompare(transform, d->previousTransform, 1e-4)) {
d->previousViewSize = viewSize;
d->previousTransform = transform;
- d->buffer.image = QImage();
+ d->buffer.image = QImage(QSize(1, 1), QImage::Format_ARGB32);
+ // Set this with canvas opengl colorprofile.
+ d->buffer.image.setColorSpace(QColorSpace(QColorSpace::Bt2100Pq));
d->updateBufferByWidgetCoordinates(QRectF(QPointF(0,0), viewSize));
}