File indexing completed on 2024-05-19 04:27:23

0001 /*
0002  *  SPDX-FileCopyrightText: 2005 Boudewijn Rempt <boud@valdyas.org>
0003  *  SPDX-FileCopyrightText: 2006-2007 Cyrille Berger <cberger@cberger.net>
0004  *  SPDX-FileCopyrightText: 2021 L. E. Segovia <amy@amyspark.me>
0005  *
0006  * SPDX-License-Identifier: LGPL-2.1-or-later
0007  */
0008 #ifndef KOCOLORSPACE_H
0009 #define KOCOLORSPACE_H
0010 
0011 #include <climits>
0012 
0013 #include <QImage>
0014 #include <QHash>
0015 #include <QVector>
0016 #include <QList>
0017 
0018 #include <boost/operators.hpp>
0019 #include "KoColorSpaceConstants.h"
0020 #include "KoColorConversionTransformation.h"
0021 #include "KoColorProofingConversionTransformation.h"
0022 #include "KoCompositeOp.h"
0023 #include "KisDitherOp.h"
0024 #include <KoID.h>
0025 #include "kritapigment_export.h"
0026 
0027 class QDomDocument;
0028 class QDomElement;
0029 class KoChannelInfo;
0030 class KoColorProfile;
0031 class KoColorTransformation;
0032 class QBitArray;
0033 
0034 enum Deletability {
0035     OwnedByRegistryDoNotDelete,
0036     OwnedByRegistryRegistryDeletes,
0037     NotOwnedByRegistry
0038 };
0039 
0040 enum ColorSpaceIndependence {
0041     FULLY_INDEPENDENT,
0042     TO_LAB16,
0043     TO_RGBA8,
0044     TO_RGBA16
0045 };
0046 
0047 class KoMixColorsOp;
0048 class KoConvolutionOp;
0049 
0050 /**
0051  * A KoColorSpace is the definition of a certain color space.
0052  *
0053  * A color model and a color space are two related concepts. A color
0054  * model is more general in that it describes the channels involved and
0055  * how they in broad terms combine to describe a color. Examples are
0056  * RGB, HSV, CMYK.
0057  *
0058  * A color space is more specific in that it also describes exactly how
0059  * the channels are combined. So for each color model there can be a
0060  * number of specific color spaces. So RGB is the model and sRGB,
0061  * adobeRGB, etc are colorspaces.
0062  *
0063  * In Pigment KoColorSpace acts as both a color model and a color space.
0064  * You can think of the class definition as the color model, but the
0065  * instance of the class as representing a colorspace.
0066  *
0067  * A third concept is the profile represented by KoColorProfile. It
0068  * represents the info needed to specialize a color model into a color
0069  * space.
0070  *
0071  * KoColorSpace is an abstract class serving as an interface.
0072  *
0073  * Subclasses implement actual color spaces
0074  * Some subclasses implement only some parts and are named Traits
0075  *
0076  */
0077 class KRITAPIGMENT_EXPORT KoColorSpace : public boost::equality_comparable<KoColorSpace>
0078 {
0079     friend class KoColorSpaceRegistry;
0080     friend class KoColorSpaceFactory;
0081 protected:
0082     /// Only for use by classes that serve as baseclass for real color spaces
0083     KoColorSpace();
0084 
0085 public:
0086 
0087     /// Should be called by real color spaces
0088     KoColorSpace(const QString &id, const QString &name, KoMixColorsOp *mixColorsOp, KoConvolutionOp *convolutionOp);
0089 
0090     virtual bool operator==(const KoColorSpace& rhs) const;
0091 protected:
0092     virtual ~KoColorSpace();
0093 
0094 public:
0095     //========== Gamut and other basic info ===================================//
0096     /*
0097      * @returns QPolygonF with 5*channel samples converted to xyY.
0098      * maybe convert to 3d space in future?
0099      */
0100     QPolygonF gamutXYY() const;
0101 
0102     /*
0103      * @returns a polygon with 5 samples per channel converted to xyY, but unlike
0104      * gamutxyY it focuses on the luminance. This then can be used to visualise
0105      * the approximate trc of a given colorspace.
0106      */
0107     QPolygonF estimatedTRCXYY() const;
0108 
0109     QVector <qreal> lumaCoefficients() const;
0110 
0111     //========== Channels =====================================================//
0112 
0113     /// Return a list describing all the channels this color model has. The order
0114     /// of the channels in the list is the order of channels in the pixel. To find
0115     /// out the preferred display position, use KoChannelInfo::displayPosition.
0116     QList<KoChannelInfo *> channels() const;
0117 
0118     /**
0119      * The total number of channels for a single pixel in this color model
0120      */
0121     virtual quint32 channelCount() const = 0;
0122 
0123     /**
0124      * Position of the alpha channel in a pixel
0125      */
0126     virtual quint32 alphaPos() const = 0;
0127 
0128     /**
0129      * The total number of color channels (excludes alpha) for a single
0130      * pixel in this color model.
0131      */
0132     virtual quint32 colorChannelCount() const = 0;
0133 
0134     /**
0135      * returns a QBitArray that contains true for the specified
0136      * channel types:
0137      *
0138      * @param color if true, set all color channels to true
0139      * @param alpha if true, set all alpha channels to true
0140      *
0141      * The order of channels is the colorspace descriptive order,
0142      * not the pixel order.
0143      */
0144     QBitArray channelFlags(bool color = true, bool alpha = false) const;
0145 
0146     /**
0147      * The size in bytes of a single pixel in this color model
0148      */
0149     virtual quint32 pixelSize() const = 0;
0150 
0151     /**
0152      * Return a string with the channel's value suitable for display in the gui.
0153      */
0154     virtual QString channelValueText(const quint8 *pixel, quint32 channelIndex) const = 0;
0155 
0156     /**
0157      * Return a string with the channel's value with integer
0158      * channels normalised to the floating point range 0 to 1, if
0159      * appropriate.
0160      */
0161     virtual QString normalisedChannelValueText(const quint8 *pixel, quint32 channelIndex) const = 0;
0162 
0163     /**
0164      * Return a QVector of floats with channels' values normalized
0165      * to floating point range 0 to 1.
0166      */
0167     virtual void normalisedChannelsValue(const quint8 *pixel, QVector<float> &channels) const = 0;
0168 
0169     /**
0170      * Write in the pixel the value from the normalized vector.
0171      */
0172     virtual void fromNormalisedChannelsValue(quint8 *pixel, const QVector<float> &values) const = 0;
0173 
0174     /**
0175      * Convert the value of the channel at the specified position into
0176      * an 8-bit value. The position is not the number of bytes, but
0177      * the position of the channel as defined in the channel info list.
0178      */
0179     virtual quint8 scaleToU8(const quint8 * srcPixel, qint32 channelPos) const = 0;
0180 
0181     /**
0182      * Set dstPixel to the pixel containing only the given channel of srcPixel. The remaining channels
0183      * should be set to whatever makes sense for 'empty' channels of this color space,
0184      * with the intent being that the pixel should look like it only has the given channel.
0185      */
0186     virtual void singleChannelPixel(quint8 *dstPixel, const quint8 *srcPixel, quint32 channelIndex) const = 0;
0187 
0188     //========== Identification ===============================================//
0189 
0190     /**
0191      * ID for use in files and internally: unchanging name. As the id must be unique
0192      * it is usually the concatenation of the id of the color model and of the color
0193      * depth, for instance "RGBA8" or "CMYKA16" or "XYZA32f".
0194      */
0195     QString id() const;
0196 
0197     /**
0198      * User visible name which contains the name of the color model and of the color depth.
0199      * For instance "RGBA (8-bits)" or "CMYKA (16-bits)".
0200      */
0201     QString name() const;
0202 
0203     /**
0204      * @return a string that identify the color model (for instance "RGB" or "CMYK" ...)
0205      * @see KoColorModelStandardIds.h
0206      */
0207     virtual KoID colorModelId() const = 0;
0208     /**
0209      * @return a string that identify the bit depth (for instance "U8" or "F16" ...)
0210      * @see KoColorModelStandardIds.h
0211      */
0212     virtual KoID colorDepthId() const = 0;
0213 
0214     /**
0215      * @return true if the profile given in argument can be used by this color space
0216      */
0217     virtual bool profileIsCompatible(const KoColorProfile* profile) const = 0;
0218 
0219     /**
0220      * If false, images in this colorspace will degrade considerably by
0221      * functions, tools and filters that have the given measure of colorspace
0222      * independence.
0223      *
0224      * @param independence the measure to which this colorspace will suffer
0225      *                     from the manipulations of the tool or filter asking
0226      * @return false if no degradation will take place, true if degradation will
0227      *         take place
0228      */
0229     virtual bool willDegrade(ColorSpaceIndependence independence) const = 0;
0230 
0231     //========== Capabilities =================================================//
0232 
0233     /**
0234      * Tests if the colorspace offers the specific composite op.
0235      * @param srcSpace optional source color space. Some color spaces prefer blitting in source
0236      *        color space. If already known, additional composite ops may be available.
0237      */
0238     virtual bool hasCompositeOp(const QString & id, const KoColorSpace *srcSpace = nullptr) const;
0239 
0240     /**
0241      * Returns the list of user-visible composite ops supported by this colorspace.
0242      */
0243     virtual QList<KoCompositeOp*> compositeOps() const;
0244 
0245     /**
0246      * Retrieve a single composite op from the ones this colorspace offers.
0247      * If the requested composite op does not exist, COMPOSITE_OVER is returned.
0248      * @param srcSpace optional source color space. Some color spaces prefer blitting in source
0249      *        color space. If already known, additional composite ops may be available.
0250      *        _Note_: if given, the returned op is only safe to use with this exact source color space!
0251      */
0252     const KoCompositeOp * compositeOp(const QString & id, const KoColorSpace *srcSpace = nullptr) const;
0253 
0254     /**
0255      * add a composite op to this colorspace.
0256      */
0257     virtual void addCompositeOp(const KoCompositeOp * op);
0258 
0259     /**
0260      * Returns true if the colorspace supports channel values outside the
0261      * (normalised) range 0 to 1.
0262      */
0263     virtual bool hasHighDynamicRange() const = 0;
0264 
0265 //========== Display profiles =============================================//
0266 
0267     /**
0268      * Return the profile of this color space.
0269      */
0270     virtual const KoColorProfile * profile() const = 0;
0271 
0272 
0273 //================= Conversion functions ==================================//
0274 
0275     /**
0276      * Fills the provided buffer with fully transparent color in this
0277      * color space.
0278      *
0279      * Not all color spaces support conversion from QColor (e.g. InputClass
0280      * spaces), so for them we need to generate this color separately.
0281      */
0282     virtual void transparentColor(quint8 *dst, quint32 nPixels) const;
0283 
0284     /**
0285      * The fromQColor methods take a given color defined as an RGB QColor
0286      * and fills a byte array with the corresponding color in the
0287      * the colorspace managed by this strategy.
0288      *
0289      * @param color the QColor that will be used to fill dst
0290      * @param dst a pointer to a pixel
0291      */
0292     virtual void fromQColor(const QColor& color, quint8 *dst) const = 0;
0293 
0294     /**
0295      * The toQColor methods take a byte array that is at least pixelSize() long
0296      * and converts the contents to a QColor, using the given profile as a source
0297      * profile and the optional profile as a destination profile.
0298      *
0299      * @param src a pointer to the source pixel
0300      * @param c the QColor that will be filled with the color at src
0301      */
0302     virtual void toQColor(const quint8 *src, QColor *c) const = 0;
0303 
0304     /**
0305      * The toQColor16 methods take a byte array that is at least pixelSize() long
0306      * and converts the contents to a 16 bit QColor, using the given profile as a source
0307      * profile and the optional profile as a destination profile.
0308      *
0309      * @param src a pointer to the source pixel
0310      * @param c the QColor that will be filled with the color at src
0311      */
0312     virtual void toQColor16(const quint8 *src, QColor *c) const = 0;
0313 
0314     /**
0315      * Convert the pixels in data to (8-bit BGRA) QImage using the specified profiles.
0316      *
0317      * @param data A pointer to a contiguous memory region containing width * height pixels
0318      * @param width in pixels
0319      * @param height in pixels
0320      * @param dstProfile destination profile
0321      * @param renderingIntent the rendering intent
0322      * @param conversionFlags conversion flags
0323      */
0324     virtual QImage convertToQImage(const quint8 *data, qint32 width, qint32 height,
0325                                    const KoColorProfile *  dstProfile,
0326                                    KoColorConversionTransformation::Intent renderingIntent,
0327                                    KoColorConversionTransformation::ConversionFlags conversionFlags) const;
0328 
0329     /**
0330      * Convert the specified data to Lab (D50). All colorspaces are guaranteed to support this
0331      *
0332      * @param src the source data
0333      * @param dst the destination data
0334      * @param nPixels the number of source pixels
0335      */
0336     virtual void toLabA16(const quint8 * src, quint8 * dst, quint32 nPixels) const;
0337 
0338     /**
0339      * Convert the specified data from Lab (D50). to this colorspace. All colorspaces are
0340      * guaranteed to support this.
0341      *
0342      * @param src the pixels in 16 bit lab format
0343      * @param dst the destination data
0344      * @param nPixels the number of pixels in the array
0345      */
0346     virtual void fromLabA16(const quint8 * src, quint8 * dst, quint32 nPixels) const;
0347 
0348     /**
0349      * Convert the specified data to sRGB 16 bits. All colorspaces are guaranteed to support this
0350      *
0351      * @param src the source data
0352      * @param dst the destination data
0353      * @param nPixels the number of source pixels
0354      */
0355     virtual void toRgbA16(const quint8 * src, quint8 * dst, quint32 nPixels) const;
0356 
0357     /**
0358      * Convert the specified data from sRGB 16 bits. to this colorspace. All colorspaces are
0359      * guaranteed to support this.
0360      *
0361      * @param src the pixels in 16 bit rgb format
0362      * @param dst the destination data
0363      * @param nPixels the number of pixels in the array
0364      */
0365     virtual void fromRgbA16(const quint8 * src, quint8 * dst, quint32 nPixels) const;
0366 
0367     /**
0368      * Create a color conversion transformation.
0369      */
0370     virtual KoColorConversionTransformation* createColorConverter(const KoColorSpace * dstColorSpace,
0371                                                                   KoColorConversionTransformation::Intent renderingIntent,
0372                                                                   KoColorConversionTransformation::ConversionFlags conversionFlags) const;
0373 
0374     /**
0375      * Retrieve the elevate-to-normalized floating point dithering op.
0376      */
0377     virtual const KisDitherOp *ditherOp(const QString &depth, DitherType type) const;
0378 
0379     virtual void addDitherOp(KisDitherOp *op);
0380 
0381     /**
0382      * Convert a byte array of srcLen pixels *src to the specified color space
0383      * and put the converted bytes into the prepared byte array *dst.
0384      *
0385      * Returns false if the conversion failed, true if it succeeded
0386      *
0387      * This function is not thread-safe. If you want to apply multiple conversion
0388      * in different threads at the same time, you need to create one color converter
0389      * per-thread using createColorConverter.
0390      */
0391     virtual bool convertPixelsTo(const quint8 * src,
0392                                  quint8 * dst, const KoColorSpace * dstColorSpace,
0393                                  quint32 numPixels,
0394                                  KoColorConversionTransformation::Intent renderingIntent,
0395                                  KoColorConversionTransformation::ConversionFlags conversionFlags) const;
0396 
0397     virtual KoColorConversionTransformation *createProofingTransform(const KoColorSpace * dstColorSpace,
0398                                                              const KoColorSpace * proofingSpace,
0399                                                              KoColorConversionTransformation::Intent renderingIntent,
0400                                                              KoColorConversionTransformation::Intent proofingIntent,
0401                                                              KoColorConversionTransformation::ConversionFlags conversionFlags,
0402                                                              quint8 *gamutWarning, double adaptationState) const;
0403     /**
0404      * @brief proofPixelsTo
0405      * @param src source
0406      * @param dst destination
0407      * @param numPixels the amount of pixels.
0408      * @param proofingTransform the intent used for proofing.
0409      * @return
0410      */
0411     virtual bool proofPixelsTo(const quint8 * src,
0412                                quint8 * dst,
0413                                quint32 numPixels,
0414                                KoColorConversionTransformation *proofingTransform) const;
0415 
0416     /**
0417      * Convert @p nPixels pixels in @p src into their human-visible
0418      * visual representation. The channel is shown as grayscale.
0419      *
0420      * Both buffers are in the same color space.
0421      *
0422      * @param src source buffer in (*this) color space
0423      * @param dst destination buffer in the same color space as @p src
0424      * @param nPixels length of the buffers in number of pixels
0425      * @param pixelSize stride of each pixel in the destination buffer
0426      * @param selectedChannelIndex Index of the selected channel.
0427      */
0428     virtual void convertChannelToVisualRepresentation(const quint8 *src, quint8 *dst, quint32 nPixels, const qint32 selectedChannelIndex) const = 0;
0429 
0430     /**
0431      * Convert @p nPixels pixels in @p src into their human-visible
0432      * visual representation. The channels are shown as if other channels were null (or, if Lab, L = 1.0, *a = *b = 0.0).
0433      *
0434      * Both buffers are in the same color space.
0435      *
0436      * @param src source buffer in (*this) color space
0437      * @param dst destination buffer in the same color space as @p src
0438      * @param nPixels length of the buffers in number of pixels
0439      * @param pixelSize stride of each pixel in the destination buffer
0440      * @param selectedChannels Bitmap of selected channels
0441      */
0442     virtual void convertChannelToVisualRepresentation(const quint8 *src, quint8 *dst, quint32 nPixels, const QBitArray selectedChannels) const = 0;
0443 
0444 //============================== Manipulation functions ==========================//
0445 
0446 
0447 //
0448 // The manipulation functions have default implementations that _convert_ the pixel
0449 // to a QColor and back. Reimplement these methods in your color strategy!
0450 //
0451 
0452     /**
0453      * Get the alpha value of the given pixel, downscaled to an 8-bit value.
0454      */
0455     virtual quint8 opacityU8(const quint8 * pixel) const = 0;
0456     virtual qreal opacityF(const quint8 * pixel) const = 0;
0457 
0458     /**
0459      * Set the alpha channel of the given run of pixels to the given value.
0460      *
0461      * pixels -- a pointer to the pixels that will have their alpha set to this value
0462      * alpha --  a downscaled 8-bit value for opacity
0463      * nPixels -- the number of pixels
0464      *
0465      */
0466     virtual void setOpacity(quint8 * pixels, quint8 alpha, qint32 nPixels) const = 0;
0467     virtual void setOpacity(quint8 * pixels, qreal alpha, qint32 nPixels) const = 0;
0468 
0469 
0470     /**
0471     * Copy the opacity of a run of pixels.
0472     *
0473     * src -- pixels to get opacity from.  This is in the source colorspace.
0474     * dst -- pixels in Alpha8 colorspace to be assigned the opacity of src pixel
0475     * nPixels -- the number of pixels
0476     *
0477     */
0478     virtual void copyOpacityU8(quint8* src, quint8* dst, qint32 nPixels) const = 0;
0479 
0480     /**
0481      * Multiply the alpha channel of the given run of pixels by the given value.
0482      *
0483      * pixels -- a pointer to the pixels that will have their alpha set to this value
0484      * alpha --  a downscaled 8-bit value for opacity
0485      * nPixels -- the number of pixels
0486      *
0487      */
0488     virtual void multiplyAlpha(quint8 * pixels, quint8 alpha, qint32 nPixels) const = 0;
0489 
0490     /**
0491      * Applies the specified 8-bit alpha mask to the pixels. We assume that there are just
0492      * as many alpha values as pixels but we do not check this; the alpha values
0493      * are assumed to be 8-bits.
0494      */
0495     virtual void applyAlphaU8Mask(quint8 * pixels, const quint8 * alpha, qint32 nPixels) const = 0;
0496 
0497     /**
0498      * Applies the inverted 8-bit alpha mask to the pixels. We assume that there are just
0499      * as many alpha values as pixels but we do not check this; the alpha values
0500      * are assumed to be 8-bits.
0501      */
0502     virtual void applyInverseAlphaU8Mask(quint8 * pixels, const quint8 * alpha, qint32 nPixels) const = 0;
0503 
0504     /**
0505      * Applies the specified float alpha mask to the pixels. We assume that there are just
0506      * as many alpha values as pixels but we do not check this; alpha values have to be between 0.0 and 1.0
0507      */
0508     virtual void applyAlphaNormedFloatMask(quint8 * pixels, const float * alpha, qint32 nPixels) const = 0;
0509 
0510     /**
0511      * Applies the inverted specified float alpha mask to the pixels. We assume that there are just
0512      * as many alpha values as pixels but we do not check this; alpha values have to be between 0.0 and 1.0
0513      */
0514     virtual void applyInverseNormedFloatMask(quint8 * pixels, const float * alpha, qint32 nPixels) const = 0;
0515 
0516     /**
0517      * Fills \p pixels with specified \p brushColor and then applies inverted brush
0518      * mask specified in \p alpha.
0519      */
0520     virtual void fillInverseAlphaNormedFloatMaskWithColor(quint8 * pixels, const float * alpha, const quint8 *brushColor, qint32 nPixels) const = 0;
0521 
0522     /**
0523      * Fills \p dst with specified \p brushColor and then applies inverted brush
0524      * mask specified in \p brush. Premultiplied red channel of the brush is
0525      * used as an alpha channel for destination pixels.
0526      *
0527      * The equation is:
0528      *
0529      *     dstC = colorC;
0530      *     dstA = qAlpha(brush) * (255 - qRed(brush)) / 255;
0531      */
0532     virtual void fillGrayBrushWithColor(quint8 *dst, const QRgb *brush, quint8 *brushColor, qint32 nPixels) const = 0;
0533 
0534     /**
0535      * Fills \p dst with specified \p brushColor and then applies inverted brush
0536      * mask specified in \p brush. Inverted red channel of the brush is used
0537      * as lightness of the destination. Alpha channel of the brush is used as
0538      * alpha of the destination.
0539      *
0540      * The equation is:
0541      *
0542      *     dstL_hsl = preserveLightness(colorL_hsl, lightFactor);
0543      *     dstA = qAlpha(brush);
0544      *
0545      * For details on preserveLightness() formula,
0546      * see KoColorSpacePreserveLightnessUtils.h
0547      */
0548     virtual void fillGrayBrushWithColorAndLightnessOverlay(quint8 *dst, const QRgb *brush, quint8 *brushColor, qint32 nPixels) const;
0549     // Same as above, but with contrast adjusted by strength.  Strength == 1 -> full contrast.  Allows softer lightness adjustments.
0550     virtual void fillGrayBrushWithColorAndLightnessWithStrength(quint8* dst, const QRgb* brush, quint8* brushColor, qreal strength, qint32 nPixels) const;
0551     // Same as above, but applies lightness adjustment to \p dst in-place
0552     virtual void modulateLightnessByGrayBrush(quint8* dst, const QRgb *brush, qreal strength, qint32 nPixels) const;
0553 
0554     /**
0555      * Create an adjustment object for adjusting the brightness and contrast
0556      * transferValues is a 256 bins array with values from 0 to 0xFFFF
0557      * This function is thread-safe, but you need to create one KoColorTransformation per thread.
0558      */
0559     virtual KoColorTransformation *createBrightnessContrastAdjustment(const quint16 *transferValues) const = 0;
0560 
0561     /**
0562      * Create an adjustment object for adjusting individual channels
0563      * transferValues is an array of colorChannelCount number of 256 bins array with values from 0 to 0xFFFF
0564      * This function is thread-safe, but you need to create one KoColorTransformation per thread.
0565      *
0566      * The layout of the channels must be the following:
0567      *
0568      * 0..N-2 - color channels of the pixel;
0569      * N-1 - alpha channel of the pixel (if exists)
0570      */
0571     virtual KoColorTransformation *createPerChannelAdjustment(const quint16 * const* transferValues) const = 0;
0572 
0573     /**
0574      * Darken all color channels with the given amount. If compensate is true,
0575      * the compensation factor will be used to limit the darkening.
0576      *
0577      */
0578     virtual KoColorTransformation *createDarkenAdjustment(qint32 shade, bool compensate, qreal compensation) const = 0;
0579 
0580     /**
0581      * Invert color channels of the given pixels
0582      * This function is thread-safe, but you need to create one KoColorTransformation per thread.
0583      */
0584     virtual KoColorTransformation *createInvertTransformation() const = 0;
0585 
0586     /**
0587      * Get the difference between 2 colors, normalized in the range (0,255). Only completely
0588      * opaque and completely transparent are taken into account when computing the difference;
0589      * other transparency levels are not regarded when finding the difference.
0590      *
0591      * Completely transparent pixels are treated as if they are completely
0592      * different from any non-transparent pixels.
0593      */
0594     virtual quint8 difference(const quint8* src1, const quint8* src2) const = 0;
0595 
0596     /**
0597      * Get the difference between 2 colors, normalized in the range (0,255). This function
0598      * takes the Alpha channel of the pixel into account. Alpha channel has the same
0599      * weight as Lightness channel.
0600      *
0601      * Completely transparent pixels are treated as if their color channels are
0602      * the same as ones of the other pixel. In other words, only alpha channel
0603      * difference is compared.
0604      */
0605     virtual quint8 differenceA(const quint8* src1, const quint8* src2) const = 0;
0606 
0607     /**
0608      * @return the mix color operation of this colorspace (do not delete it locally, it's deleted by the colorspace).
0609      */
0610     virtual KoMixColorsOp* mixColorsOp() const;
0611 
0612     /**
0613      * @return the convolution operation of this colorspace (do not delete it locally, it's deleted by the colorspace).
0614      */
0615     virtual KoConvolutionOp* convolutionOp() const;
0616 
0617     /**
0618      * Calculate the intensity of the given pixel, scaled down to the range 0-255. XXX: Maybe this should be more flexible
0619      */
0620     virtual quint8 intensity8(const quint8 * src) const = 0;
0621 
0622     /**
0623      * Calculate the intensity of the given pixel, scaled down to the range 0-1
0624      */
0625     virtual qreal intensityF(const quint8 * src) const = 0;
0626 
0627     /*
0628      *increase luminosity by step
0629      */
0630     virtual void increaseLuminosity(quint8 * pixel, qreal step) const;
0631     virtual void decreaseLuminosity(quint8 * pixel, qreal step) const;
0632     virtual void increaseSaturation(quint8 * pixel, qreal step) const;
0633     virtual void decreaseSaturation(quint8 * pixel, qreal step) const;
0634     virtual void increaseHue(quint8 * pixel, qreal step) const;
0635     virtual void decreaseHue(quint8 * pixel, qreal step) const;
0636     virtual void increaseRed(quint8 * pixel, qreal step) const;
0637     virtual void increaseGreen(quint8 * pixel, qreal step) const;
0638     virtual void increaseBlue(quint8 * pixel, qreal step) const;
0639     virtual void increaseYellow(quint8 * pixel, qreal step) const;
0640     virtual void toHSY(const QVector<double> &channelValues, qreal *hue, qreal *sat, qreal *luma) const = 0;
0641     virtual QVector <double> fromHSY(qreal *hue, qreal *sat, qreal *luma) const = 0;
0642     virtual void toYUV(const QVector<double> &channelValues, qreal *y, qreal *u, qreal *v) const = 0;
0643     virtual QVector <double> fromYUV(qreal *y, qreal *u, qreal *v) const = 0;
0644     /**
0645      * Compose two arrays of pixels together. If source and target
0646      * are not the same color model, the source pixels will be
0647      * converted to the target model. We're "dst" -- "dst" pixels are always in _this_
0648      * colorspace.
0649      *
0650      * @param srcSpace the colorspace of the source pixels that will be composited onto "us"
0651      * @param params the information needed for blitting e.g. the source and destination pixel data,
0652      *        the opacity and flow, ...
0653      * @param op the composition operator instance to use, e.g. COPY_OVER.
0654      *        This operator must belong to this (dst) color space, UNLESS preferCompositionInSourceColorSpace()
0655      *        returns true. In this case, the operator should be from the source color space. Besides avoiding
0656      *        recurring lookups via ::compositeOp(), this is necessary if this color space does not implement
0657      *        the desired composite op, but srcPace does.
0658      *        Use ::compositeOp() with valid srcSpace to get the appropriate op.
0659      *
0660      * @param renderingIntent the rendering intent
0661      * @param conversionFlags the conversion flags.
0662      *
0663      */
0664     virtual void bitBlt(const KoColorSpace* srcSpace, const KoCompositeOp::ParameterInfo& params, const KoCompositeOp* op,
0665                         KoColorConversionTransformation::Intent renderingIntent,
0666                         KoColorConversionTransformation::ConversionFlags conversionFlags) const;
0667 
0668     /**
0669      * Serialize this color following Create's swatch color specification available
0670      * at https://web.archive.org/web/20110826002520/http://create.freedesktop.org/wiki/Swatches_-_color_file_format/Draft
0671      *
0672      * This function doesn't create the \<color /\> element but rather the \<CMYK /\>,
0673      * \<sRGB /\>, \<RGB /\> ... elements. It is assumed that colorElt is the \<color /\>
0674      * element.
0675      *
0676      * @param pixel buffer to serialized
0677      * @param colorElt root element for the serialization, it is assumed that this
0678      *                 element is \<color /\>
0679      * @param doc is the document containing colorElt
0680      */
0681     virtual void colorToXML(const quint8* pixel, QDomDocument& doc, QDomElement& colorElt) const = 0;
0682 
0683     /**
0684      * Unserialize a color following Create's swatch color specification available
0685      * at https://web.archive.org/web/20110826002520/http://create.freedesktop.org/wiki/Swatches_-_color_file_format/Draft
0686      *
0687      * @param pixel buffer where the color will be unserialized
0688      * @param elt the element to unserialize (\<CMYK /\>, \<sRGB /\>, \<RGB /\>)
0689      * @return the unserialize color, or an empty color object if the function failed
0690      *         to unserialize the color
0691      */
0692     virtual void colorFromXML(quint8* pixel, const QDomElement& elt) const = 0;
0693 
0694     KoColorTransformation* createColorTransformation(const QString & id, const QHash<QString, QVariant> & parameters) const;
0695 
0696 protected:
0697 
0698     /**
0699      * Use this function in the constructor of your colorspace to add the information about a channel.
0700      * @param ci a pointer to the information about a channel
0701      */
0702     virtual void addChannel(KoChannelInfo * ci);
0703     const KoColorConversionTransformation* toLabA16Converter() const;
0704     const KoColorConversionTransformation* fromLabA16Converter() const;
0705     const KoColorConversionTransformation* toRgbA16Converter() const;
0706     const KoColorConversionTransformation* fromRgbA16Converter() const;
0707 
0708     /**
0709      * This function defines the behavior of the bitBlt function
0710      * when the composition of pixels in different colorspaces is
0711      * requested, that is in case:
0712      *
0713      * srcCS == any
0714      * dstCS == this
0715      *
0716      * 1) preferCompositionInSourceColorSpace() == false,
0717      *
0718      *    the source pixels are first converted to *this color space
0719      *    and then composition is performed.
0720      *
0721      * 2)  preferCompositionInSourceColorSpace() == true,
0722      *
0723      *    the destination pixels are first converted into *srcCS color
0724      *    space, then the composition is done, and the result is finally
0725      *    converted into *this colorspace.
0726      *
0727      *    This is used by alpha8() color space mostly, because it has
0728      *    weaker representation of the color, so the composition
0729      *    should be done in CS with richer functionality.
0730      */
0731     virtual bool preferCompositionInSourceColorSpace() const;
0732 
0733 
0734     struct Private;
0735     Private * const d;
0736 
0737 };
0738 
0739 inline QDebug operator<<(QDebug dbg, const KoColorSpace *cs)
0740 {
0741     if (cs) {
0742         dbg.nospace() << cs->name() << " (" << cs->colorModelId().id() << "," << cs->colorDepthId().id() << " )";
0743     } else {
0744         dbg.nospace() << "0x0";
0745     }
0746 
0747     return dbg.space();
0748 }
0749 
0750 
0751 #endif // KOCOLORSPACE_H