File indexing completed on 2024-06-09 04:23:28

0001 /*
0002  *  SPDX-FileCopyrightText: 2004 Boudewijn Rempt <boud@valdyas.org>
0003  *  SPDX-FileCopyrightText: 2006 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 #include "KoRgbU8ColorSpace.h"
0009 
0010 #include <limits.h>
0011 #include <stdlib.h>
0012 
0013 #include <QImage>
0014 #include <QBitArray>
0015 
0016 #include <klocalizedstring.h>
0017 
0018 #include "KoChannelInfo.h"
0019 #include "KoID.h"
0020 #include "KoIntegerMaths.h"
0021 #include "compositeops/KoCompositeOps.h"
0022 #include "dithering/KisRgbDitherOpFactory.h"
0023 
0024 #include "KoColorConversions.h"
0025 #include <KoColorSpacePreserveLightnessUtils.h>
0026 
0027 KoRgbU8ColorSpace::KoRgbU8ColorSpace() :
0028 
0029         KoSimpleColorSpace<KoBgrU8Traits>(colorSpaceId(),
0030                                           i18n("RGB (8-bit integer/channel, unmanaged)"),
0031                                           RGBAColorModelID,
0032                                           Integer8BitsColorDepthID)
0033 {
0034     
0035     addChannel(new KoChannelInfo(i18n("Blue"),  0, 2, KoChannelInfo::COLOR, KoChannelInfo::UINT8, 1, QColor(0, 0, 255)));
0036     addChannel(new KoChannelInfo(i18n("Green"), 1, 1, KoChannelInfo::COLOR, KoChannelInfo::UINT8, 1, QColor(0, 255, 0)));
0037     addChannel(new KoChannelInfo(i18n("Red"),   2, 0, KoChannelInfo::COLOR, KoChannelInfo::UINT8, 1, QColor(255, 0, 0)));
0038     addChannel(new KoChannelInfo(i18n("Alpha"), 3, 3, KoChannelInfo::ALPHA, KoChannelInfo::UINT8));
0039 
0040     // ADD, ALPHA_DARKEN, BURN, DIVIDE, DODGE, ERASE, MULTIPLY, OVER, OVERLAY, SCREEN, SUBTRACT
0041     addStandardCompositeOps<KoBgrU8Traits>(this);
0042     addStandardDitherOps<KoBgrU8Traits>(this);
0043 }
0044 
0045 KoRgbU8ColorSpace::~KoRgbU8ColorSpace()
0046 {
0047 }
0048 
0049 
0050 QString KoRgbU8ColorSpace::colorSpaceId()
0051 {
0052     return QStringLiteral("RGBA");
0053 }
0054 
0055 
0056 KoColorSpace* KoRgbU8ColorSpace::clone() const
0057 {
0058     return new KoRgbU8ColorSpace();
0059 }
0060 
0061 
0062 void KoRgbU8ColorSpace::fromQColor(const QColor& c, quint8 *dst) const
0063 {
0064     QVector<float> channelValues;
0065     channelValues << c.blueF() << c.greenF() << c.redF() << c.alphaF();
0066     fromNormalisedChannelsValue(dst, channelValues);
0067 }
0068 
0069 void KoRgbU8ColorSpace::toQColor(const quint8 * src, QColor *c) const
0070 {
0071     QVector<float> channelValues(4);
0072     normalisedChannelsValue(src, channelValues);
0073     c->setRgbF(channelValues[2], channelValues[1], channelValues[0], channelValues[3]);
0074 }
0075 
0076 void KoRgbU8ColorSpace::toHSY(const QVector<double> &channelValues, qreal *hue, qreal *sat, qreal *luma) const
0077 {
0078     
0079     RGBToHSY(channelValues[0],channelValues[1],channelValues[2], hue, sat, luma);
0080 }
0081 
0082 QVector <double> KoRgbU8ColorSpace::fromHSY(qreal *hue, qreal *sat, qreal *luma) const
0083 {
0084     QVector <double> channelValues(4);
0085     HSYToRGB(*hue, *sat, *luma, &channelValues[0],&channelValues[1],&channelValues[2]);
0086     channelValues[3]=1.0;
0087     return channelValues;
0088 }
0089 
0090 void KoRgbU8ColorSpace::toYUV(const QVector<double> &channelValues, qreal *y, qreal *u, qreal *v) const
0091 {
0092     RGBToYUV(channelValues[0],channelValues[1],channelValues[2], y, u, v);
0093 }
0094 
0095 QVector <double> KoRgbU8ColorSpace::fromYUV(qreal *y, qreal *u, qreal *v) const
0096 {
0097     QVector <double> channelValues(4);
0098     YUVToRGB(*y, *u, *v, &channelValues[0],&channelValues[1],&channelValues[2]);
0099     channelValues[3]=1.0;
0100     return channelValues;
0101 }
0102 
0103 void KoRgbU8ColorSpace::fillGrayBrushWithColorAndLightnessOverlay(quint8 *dst, const QRgb *brush, quint8 *brushColor, qint32 nPixels) const
0104 {
0105     fillGrayBrushWithColorPreserveLightnessRGB<KoBgrU8Traits>(dst, brush, brushColor, 1.0, nPixels);
0106 }
0107 
0108 void KoRgbU8ColorSpace::fillGrayBrushWithColorAndLightnessWithStrength(quint8* dst, const QRgb* brush, quint8* brushColor, qreal strength, qint32 nPixels) const
0109 {
0110     fillGrayBrushWithColorPreserveLightnessRGB<KoBgrU8Traits>(dst, brush, brushColor, strength, nPixels);
0111 }
0112 
0113 void KoRgbU8ColorSpace::modulateLightnessByGrayBrush(quint8 *dst, const QRgb *brush, qreal strength, qint32 nPixels) const
0114 {
0115    modulateLightnessByGrayBrushRGB<KoBgrU8Traits>(dst, brush, strength, nPixels);
0116 }