File indexing completed on 2024-12-22 04:11:38

0001 /*
0002  *  SPDX-FileCopyrightText: 2004-2009 Boudewijn Rempt <boud@valdyas.org>
0003  *  SPDX-FileCopyrightText: 2006 Cyrille Berger <cberger@cberger.net>
0004  *
0005  *  SPDX-License-Identifier: LGPL-2.1-or-later
0006  */
0007 #ifndef KOSIMPLECOLORSPACE_H
0008 #define KOSIMPLECOLORSPACE_H
0009 
0010 #include <QColor>
0011 
0012 #include "DebugPigment.h"
0013 
0014 #include "KoColorSpaceAbstract.h"
0015 #include "KoSimpleColorSpaceFactory.h"
0016 #include "KoColorModelStandardIds.h"
0017 #include "colorprofiles/KoDummyColorProfile.h"
0018 
0019 template<class _CSTraits>
0020 class KoSimpleColorSpace : public KoColorSpaceAbstract<_CSTraits>
0021 {
0022 
0023 public:
0024 
0025     KoSimpleColorSpace(const QString& id,
0026                        const QString& name,
0027                        const KoID& colorModelId,
0028                        const KoID& colorDepthId)
0029             : KoColorSpaceAbstract<_CSTraits>(id, name)
0030             , m_name(name)
0031             , m_colorModelId(colorModelId)
0032             , m_colorDepthId(colorDepthId)
0033             , m_profile(new KoDummyColorProfile) {
0034     }
0035 
0036     ~KoSimpleColorSpace() override {
0037         delete m_profile;
0038     }
0039 
0040     KoID colorModelId() const override {
0041         return m_colorModelId;
0042     }
0043 
0044     KoID colorDepthId() const override {
0045         return m_colorDepthId;
0046     }
0047 
0048     bool willDegrade(ColorSpaceIndependence independence) const override {
0049         Q_UNUSED(independence);
0050         return false;
0051     }
0052 
0053     bool profileIsCompatible(const KoColorProfile* /*profile*/) const override {
0054         return true;
0055     }
0056 
0057     quint8 difference(const quint8 *src1, const quint8 *src2) const override {
0058         Q_UNUSED(src1);
0059         Q_UNUSED(src2);
0060         warnPigment << i18n("Undefined operation in the %1 space", m_name);
0061         return 0;
0062     }
0063 
0064     quint8 differenceA(const quint8 *src1, const quint8 *src2) const override {
0065         Q_UNUSED(src1);
0066         Q_UNUSED(src2);
0067         warnPigment << i18n("Undefined operation in the %1 space", m_name);
0068         return 0;
0069     }
0070 
0071     virtual quint32 colorSpaceType() const {
0072         return 0;
0073     }
0074 
0075     bool hasHighDynamicRange() const override {
0076         return false;
0077     }
0078 
0079     const KoColorProfile* profile() const override {
0080         return m_profile;
0081     }
0082 
0083     KoColorTransformation* createBrightnessContrastAdjustment(const quint16*) const override {
0084         warnPigment << i18n("Undefined operation in the %1 color space", m_name);
0085         return 0;
0086     }
0087 
0088     virtual KoColorTransformation* createDesaturateAdjustment() const {
0089         warnPigment << i18n("Undefined operation in the %1 color space", m_name);
0090         return 0;
0091     }
0092 
0093     KoColorTransformation* createPerChannelAdjustment(const quint16* const*) const override {
0094         warnPigment << i18n("Undefined operation in the %1 color space", m_name);
0095         return 0;
0096     }
0097 
0098     KoColorTransformation *createDarkenAdjustment(qint32 , bool , qreal) const override {
0099         warnPigment << i18n("Undefined operation in the %1 color space", m_name);
0100         return 0;
0101     }
0102 
0103     virtual void invertColor(quint8*, qint32) const {
0104         warnPigment << i18n("Undefined operation in the %1 color space", m_name);
0105     }
0106 
0107     void colorToXML(const quint8* , QDomDocument& , QDomElement&) const override {
0108         warnPigment << i18n("Undefined operation in the %1 color space", m_name);
0109     }
0110 
0111     void colorFromXML(quint8* , const QDomElement&) const override {
0112         warnPigment << i18n("Undefined operation in the %1 color space", m_name);
0113     }
0114     void toHSY(const QVector<double> &, qreal *, qreal *, qreal *) const override {
0115         warnPigment << i18n("Undefined operation in the %1 color space", m_name);
0116     }
0117     QVector <double> fromHSY(qreal *, qreal *, qreal *) const override {
0118         warnPigment << i18n("Undefined operation in the %1 color space", m_name);
0119         QVector <double> channelValues (2);
0120         channelValues.fill(0.0);
0121         return channelValues;
0122     }
0123     void toYUV(const QVector<double> &, qreal *, qreal *, qreal *) const override {
0124         warnPigment << i18n("Undefined operation in the %1 color space", m_name);
0125     }
0126     QVector <double> fromYUV(qreal *, qreal *, qreal *) const override {
0127         warnPigment << i18n("Undefined operation in the %1 color space", m_name);
0128         QVector <double> channelValues (2);
0129         channelValues.fill(0.0);
0130         return channelValues;
0131     }
0132 
0133     void toLabA16(const quint8* src, quint8* dst, quint32 nPixels) const override {
0134         if (colorDepthId() == Integer16BitsColorDepthID && colorModelId() == LABAColorModelID) {
0135             memcpy(dst, src, nPixels * 2);
0136         } else {
0137             const KoColorSpace* dstCs = KoColorSpaceRegistry::instance()->lab16();
0138             convertPixelsTo(src, dst, dstCs, nPixels,
0139                             KoColorConversionTransformation::internalRenderingIntent(),
0140                             KoColorConversionTransformation::internalConversionFlags());
0141         }
0142     }
0143 
0144     void fromLabA16(const quint8* src, quint8* dst, quint32 nPixels) const override {
0145         if (colorDepthId() == Integer16BitsColorDepthID && colorModelId() == LABAColorModelID) {
0146             memcpy(dst, src, nPixels * 2);
0147         } else {
0148             const KoColorSpace* srcCs = KoColorSpaceRegistry::instance()->lab16();
0149             srcCs->convertPixelsTo(src, dst, this, nPixels,
0150                                    KoColorConversionTransformation::internalRenderingIntent(),
0151                                    KoColorConversionTransformation::internalConversionFlags());
0152         }
0153     }
0154 
0155     void toRgbA16(const quint8* src, quint8* dst, quint32 nPixels) const override {
0156         if (colorDepthId() == Integer16BitsColorDepthID && colorModelId() == RGBAColorModelID) {
0157             memcpy(dst, src, nPixels * 2);
0158         } else {
0159             const KoColorSpace* dstCs = KoColorSpaceRegistry::instance()->rgb16();
0160             convertPixelsTo(src, dst, dstCs, nPixels,
0161                             KoColorConversionTransformation::internalRenderingIntent(),
0162                             KoColorConversionTransformation::internalConversionFlags());
0163         }
0164     }
0165 
0166     void fromRgbA16(const quint8* src, quint8* dst, quint32 nPixels) const override {
0167         if (colorDepthId() == Integer16BitsColorDepthID && colorModelId() == RGBAColorModelID) {
0168             memcpy(dst, src, nPixels * 2);
0169         } else {
0170             const KoColorSpace* srcCs = KoColorSpaceRegistry::instance()->rgb16();
0171             srcCs->convertPixelsTo(src, dst, this, nPixels,
0172                                    KoColorConversionTransformation::internalRenderingIntent(),
0173                                    KoColorConversionTransformation::internalConversionFlags());
0174         }
0175     }
0176 
0177     bool convertPixelsTo(const quint8 *src,
0178                                  quint8 *dst, const KoColorSpace * dstColorSpace,
0179                                  quint32 numPixels,
0180                                  KoColorConversionTransformation::Intent renderingIntent,
0181                                  KoColorConversionTransformation::ConversionFlags conversionFlags) const override
0182     {
0183         Q_UNUSED(renderingIntent);
0184         Q_UNUSED(conversionFlags);
0185 
0186         QColor c;
0187         quint32 srcPixelsize = this->pixelSize();
0188         quint32 dstPixelsize = dstColorSpace->pixelSize();
0189 
0190         while (numPixels > 0) {
0191 
0192             this->toQColor(src, &c);
0193             dstColorSpace->fromQColor(c, dst);
0194 
0195             src += srcPixelsize;
0196             dst += dstPixelsize;
0197 
0198             --numPixels;
0199         }
0200         return true;
0201     }
0202 
0203 
0204     virtual QString colorSpaceEngine() const {
0205         return "simple";
0206     }
0207 
0208 private:
0209     QString m_name;
0210     KoID m_colorModelId;
0211     KoID m_colorDepthId;
0212     KoColorProfile* m_profile;
0213 
0214 };
0215 
0216 
0217 #endif // KOSIMPLECOLORSPACE_H