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

0001 /*
0002  *  SPDX-FileCopyrightText: 2016 Boudewijn Rempt <boud@valdyas.org>
0003  *
0004  *  SPDX-License-Identifier: LGPL-2.1-or-later
0005  */
0006 #ifndef KOALPHACOLORSPACE_H
0007 #define KOALPHACOLORSPACE_H
0008 
0009 #include <QColor>
0010 
0011 #include "DebugPigment.h"
0012 #include "kritapigment_export.h"
0013 
0014 #include "KoColorSpaceAbstract.h"
0015 #include "KoColorSpaceTraits.h"
0016 
0017 #include "KoColorModelStandardIds.h"
0018 #include "KoColorModelStandardIdsUtils.h"
0019 #include "KoSimpleColorSpaceFactory.h"
0020 
0021 #include <KoConfig.h>
0022 #ifdef HAVE_OPENEXR
0023 #include <half.h>
0024 #endif
0025 
0026 
0027 template <typename _T, typename _Tdst>
0028 class KoColorSpaceMaths;
0029 
0030 typedef KoColorSpaceTrait<quint8, 1, 0> AlphaU8Traits;
0031 typedef KoColorSpaceTrait<quint16, 1, 0> AlphaU16Traits;
0032 typedef KoColorSpaceTrait<float, 1, 0> AlphaF32Traits;
0033 
0034 template <typename channel_type> KoID alphaIdFromChannelType();
0035 template <> inline KoID alphaIdFromChannelType<quint8>() { return KoID("ALPHA", i18n("Alpha (8-bit integer)")); }
0036 template <> inline KoID alphaIdFromChannelType<quint16>() { return KoID("ALPHAU16", i18n("Alpha (16-bit integer)")); }
0037 template <> inline KoID alphaIdFromChannelType<float>() { return KoID("ALPHAF32", i18n("Alpha (32-bit floating point)")); }
0038 
0039 #ifdef HAVE_OPENEXR
0040 typedef KoColorSpaceTrait<half, 1, 0> AlphaF16Traits;
0041 template <> inline KoID alphaIdFromChannelType<half>() { return KoID("ALPHAF16", i18n("Alpha (16-bit floating point)")); }
0042 #endif
0043 
0044 class QBitArray;
0045 
0046 /**
0047  * The alpha mask is a special color strategy that treats all pixels as
0048  * alpha value with a color common to the mask. The default color is white.
0049  */
0050 template <class _CSTrait>
0051 class KRITAPIGMENT_EXPORT KoAlphaColorSpaceImpl : public KoColorSpaceAbstract<_CSTrait>
0052 {
0053     typedef typename _CSTrait::channels_type channels_type;
0054     typedef KoColorSpaceMaths<channels_type> _Maths;
0055     typedef KoColorSpaceMaths<channels_type, quint8> _MathsToU8;
0056     typedef KoColorSpaceMaths<quint8, channels_type> _MathsFromU8;
0057 
0058 
0059 public:
0060 
0061     KoAlphaColorSpaceImpl();
0062 
0063     ~KoAlphaColorSpaceImpl() override;
0064 
0065     static QString colorSpaceId() {
0066         return alphaIdFromChannelType<channels_type>().id();
0067     }
0068 
0069     KoID colorModelId() const override {
0070         return AlphaColorModelID;
0071     }
0072 
0073     KoID colorDepthId() const override {
0074         return colorDepthIdForChannelType<channels_type>();
0075     }
0076 
0077     virtual KoColorSpace* clone() const;
0078 
0079     bool willDegrade(ColorSpaceIndependence independence) const override {
0080         Q_UNUSED(independence);
0081         return false;
0082     }
0083 
0084     bool profileIsCompatible(const KoColorProfile* /*profile*/) const override {
0085         return false;
0086     }
0087 
0088     void fromQColor(const QColor& color, quint8 *dst) const override;
0089 
0090     void toQColor(const quint8 *src, QColor *c) const override;
0091 
0092     quint8 difference(const quint8 *src1, const quint8 *src2) const override;
0093     quint8 differenceA(const quint8 *src1, const quint8 *src2) const override;
0094 
0095     quint32 colorChannelCount() const override {
0096         return 0;
0097     }
0098 
0099     QString channelValueText(const quint8 *pixel, quint32 channelIndex) const override;
0100 
0101     QString normalisedChannelValueText(const quint8 *pixel, quint32 channelIndex) const override;
0102 
0103     virtual void convolveColors(quint8** colors, qreal* kernelValues, quint8 *dst, qreal factor, qreal offset, qint32 nColors, const QBitArray & channelFlags) const;
0104 
0105     virtual quint32 colorSpaceType() const {
0106         return 0;
0107     }
0108 
0109     bool hasHighDynamicRange() const override {
0110         return false;
0111     }
0112 
0113     const KoColorProfile* profile() const override {
0114         return m_profile;
0115     }
0116 
0117     QImage convertToQImage(const quint8 *data, qint32 width, qint32 height,
0118                                    const KoColorProfile *  dstProfile,
0119                                    KoColorConversionTransformation::Intent renderingIntent,
0120                                    KoColorConversionTransformation::ConversionFlags conversionFlags) const override;
0121 
0122     void toLabA16(const quint8* src, quint8* dst, quint32 nPixels) const override;
0123     void fromLabA16(const quint8* src, quint8* dst, quint32 nPixels) const override;
0124 
0125     void toRgbA16(const quint8* src, quint8* dst, quint32 nPixels) const override;
0126     void fromRgbA16(const quint8* src, quint8* dst, quint32 nPixels) const override;
0127 
0128     KoColorTransformation* createBrightnessContrastAdjustment(const quint16* transferValues) const override {
0129         Q_UNUSED(transferValues);
0130         warnPigment << i18n("Undefined operation in the alpha color space");
0131         return 0;
0132     }
0133 
0134     KoColorTransformation* createPerChannelAdjustment(const quint16* const*) const override {
0135         warnPigment << i18n("Undefined operation in the alpha color space");
0136         return 0;
0137     }
0138 
0139     KoColorTransformation *createDarkenAdjustment(qint32 , bool , qreal) const override {
0140         warnPigment << i18n("Undefined operation in the alpha color space");
0141         return 0;
0142     }
0143 
0144     virtual void invertColor(quint8*, qint32) const {
0145         warnPigment << i18n("Undefined operation in the alpha color space");
0146     }
0147 
0148     void colorToXML(const quint8* , QDomDocument& , QDomElement&) const override {
0149         warnPigment << i18n("Undefined operation in the alpha color space");
0150     }
0151 
0152     void colorFromXML(quint8* , const QDomElement&) const override {
0153         warnPigment << i18n("Undefined operation in the alpha color space");
0154     }
0155 
0156     void toHSY(const QVector<double> &, qreal *, qreal *, qreal *) const override {
0157         warnPigment << i18n("Undefined operation in the alpha color space");
0158     }
0159 
0160     QVector <double> fromHSY(qreal *, qreal *, qreal *) const override {
0161         warnPigment << i18n("Undefined operation in the alpha color space");
0162         QVector <double> channelValues (1);
0163         channelValues.fill(0.0);
0164         return channelValues;
0165     }
0166 
0167     void toYUV(const QVector<double> &, qreal *, qreal *, qreal *) const override {
0168         warnPigment << i18n("Undefined operation in the alpha color space");
0169     }
0170 
0171     QVector <double> fromYUV(qreal *, qreal *, qreal *) const override {
0172         warnPigment << i18n("Undefined operation in the alpha color space");
0173         QVector <double> channelValues (1);
0174         channelValues.fill(0.0);
0175         return channelValues;
0176     }
0177 
0178 protected:
0179     bool preferCompositionInSourceColorSpace() const override;
0180 
0181 private:
0182     KoColorProfile* m_profile;
0183 };
0184 
0185 typedef KoAlphaColorSpaceImpl<AlphaU8Traits> KoAlphaColorSpace;
0186 typedef KoAlphaColorSpaceImpl<AlphaU16Traits> KoAlphaU16ColorSpace;
0187 #ifdef HAVE_OPENEXR
0188 typedef KoAlphaColorSpaceImpl<AlphaF16Traits> KoAlphaF16ColorSpace;
0189 #endif
0190 typedef KoAlphaColorSpaceImpl<AlphaF32Traits> KoAlphaF32ColorSpace;
0191 
0192 template <class _CSTrait>
0193 class KoAlphaColorSpaceFactoryImpl : public KoSimpleColorSpaceFactory
0194 {
0195     typedef typename _CSTrait::channels_type channels_type;
0196 
0197 public:
0198     KoAlphaColorSpaceFactoryImpl()
0199             : KoSimpleColorSpaceFactory(alphaIdFromChannelType<channels_type>().id(),
0200                                         alphaIdFromChannelType<channels_type>().name(),
0201                                         false,
0202                                         AlphaColorModelID,
0203                                         colorDepthIdForChannelType<channels_type>(),
0204                                         sizeof(channels_type) * 8,
0205                                         100000)
0206     {
0207     }
0208 
0209     KoColorSpace *createColorSpace(const KoColorProfile *) const override {
0210         return new KoAlphaColorSpaceImpl<_CSTrait>();
0211     }
0212 
0213     QList<KoColorConversionTransformationFactory*> colorConversionLinks() const override;
0214 };
0215 
0216 typedef KoAlphaColorSpaceFactoryImpl<AlphaU8Traits> KoAlphaColorSpaceFactory;
0217 typedef KoAlphaColorSpaceFactoryImpl<AlphaU16Traits> KoAlphaU16ColorSpaceFactory;
0218 #ifdef HAVE_OPENEXR
0219 typedef KoAlphaColorSpaceFactoryImpl<AlphaF16Traits> KoAlphaF16ColorSpaceFactory;
0220 #endif
0221 typedef KoAlphaColorSpaceFactoryImpl<AlphaF32Traits> KoAlphaF32ColorSpaceFactory;
0222 
0223 #endif