YCbCrU8ColorSpace.cpp 3.87 KB
Newer Older
1
/*
2
 *  SPDX-FileCopyrightText: 2007 Cyrille Berger (cberger@cberger.net)
Amy spark's avatar
Amy spark committed
3
 *  SPDX-FileCopyrightText: 2021 L. E. Segovia <amy@amyspark.me>
4
 *
Samuel Gaist's avatar
Samuel Gaist committed
5
 * SPDX-License-Identifier: LGPL-2.1-or-later
Amy spark's avatar
Amy spark committed
6
 */
7
8
9
10

#include "YCbCrU8ColorSpace.h"
#include <QDomElement>

11
#include <QDebug>
12
#include <klocalizedstring.h>
13
14

#include "compositeops/KoCompositeOps.h"
Amy spark's avatar
Amy spark committed
15
#include "dithering/KisYCbCrDitherOpFactory.h"
16
17
18
#include <KoColorConversions.h>

#include <kis_dom_utils.h>
19
20

YCbCrU8ColorSpace::YCbCrU8ColorSpace(const QString &name, KoColorProfile *p)
21
    : LcmsColorSpace<KoYCbCrU8Traits>(colorSpaceId(), name, TYPE_YCbCrA_8, cmsSigYCbCrData, p)
22
23
24
25
26
27
28
29
30
{
    addChannel(new KoChannelInfo(i18n("Y"),     KoYCbCrU8Traits::Y_pos     * sizeof(quint8), KoYCbCrU8Traits::Y_pos,     KoChannelInfo::COLOR, KoChannelInfo::UINT8, sizeof(quint8), Qt::cyan));
    addChannel(new KoChannelInfo(i18n("Cb"),    KoYCbCrU8Traits::Cb_pos    * sizeof(quint8), KoYCbCrU8Traits::Cb_pos,    KoChannelInfo::COLOR, KoChannelInfo::UINT8, sizeof(quint8), Qt::magenta));
    addChannel(new KoChannelInfo(i18n("Cr"),    KoYCbCrU8Traits::Cr_pos    * sizeof(quint8), KoYCbCrU8Traits::Cr_pos,    KoChannelInfo::COLOR, KoChannelInfo::UINT8, sizeof(quint8), Qt::yellow));
    addChannel(new KoChannelInfo(i18n("Alpha"), KoYCbCrU8Traits::alpha_pos * sizeof(quint8), KoYCbCrU8Traits::alpha_pos, KoChannelInfo::ALPHA, KoChannelInfo::UINT8, sizeof(quint8)));

    init();

    addStandardCompositeOps<KoYCbCrU8Traits>(this);
Amy spark's avatar
Amy spark committed
31
    addStandardDitherOps<KoYCbCrU8Traits>(this);
32
33
34
35
36
37
38
}

bool YCbCrU8ColorSpace::willDegrade(ColorSpaceIndependence /*independence*/) const
{
    return false;
}

39
KoColorSpace *YCbCrU8ColorSpace::clone() const
40
41
42
43
{
    return new YCbCrU8ColorSpace(name(), profile()->clone());
}

44
void YCbCrU8ColorSpace::colorToXML(const quint8 *pixel, QDomDocument &doc, QDomElement &colorElt) const
45
{
46
    const KoYCbCrU8Traits::Pixel *p = reinterpret_cast<const KoYCbCrU8Traits::Pixel *>(pixel);
47
    QDomElement labElt = doc.createElement("YCbCr");
48
49
50
    labElt.setAttribute("Y",  KisDomUtils::toString(KoColorSpaceMaths< KoYCbCrU8Traits::channels_type, qreal>::scaleToA(p->Y)));
    labElt.setAttribute("Cb", KisDomUtils::toString(KoColorSpaceMaths< KoYCbCrU8Traits::channels_type, qreal>::scaleToA(p->Cb)));
    labElt.setAttribute("Cr", KisDomUtils::toString(KoColorSpaceMaths< KoYCbCrU8Traits::channels_type, qreal>::scaleToA(p->Cr)));
51
52
53
54
    labElt.setAttribute("space", profile()->name());
    colorElt.appendChild(labElt);
}

55
void YCbCrU8ColorSpace::colorFromXML(quint8 *pixel, const QDomElement &elt) const
56
{
57
    KoYCbCrU8Traits::Pixel *p = reinterpret_cast<KoYCbCrU8Traits::Pixel *>(pixel);
58
59
60
    p->Y = KoColorSpaceMaths< qreal, KoYCbCrU8Traits::channels_type >::scaleToA(KisDomUtils::toDouble(elt.attribute("Y")));
    p->Cb = KoColorSpaceMaths< qreal, KoYCbCrU8Traits::channels_type >::scaleToA(KisDomUtils::toDouble(elt.attribute("Cb")));
    p->Cr = KoColorSpaceMaths< qreal, KoYCbCrU8Traits::channels_type >::scaleToA(KisDomUtils::toDouble(elt.attribute("Cr")));
61
62
63
    p->alpha = KoColorSpaceMathsTraits<quint8>::max;
}

64
void YCbCrU8ColorSpace::toHSY(const QVector<double> &channelValues, qreal *hue, qreal *sat, qreal *luma) const
65
66
67
68
69
70
71
72
73
74
75
{
    LabToLCH(channelValues[0],channelValues[1],channelValues[2], luma, sat, hue);
}

QVector <double> YCbCrU8ColorSpace::fromHSY(qreal *hue, qreal *sat, qreal *luma) const
{
    QVector <double> channelValues(4);
    LCHToLab(*luma, *sat, *hue, &channelValues[0],&channelValues[1],&channelValues[2]);
    channelValues[3]=1.0;
    return channelValues;
}
76
void YCbCrU8ColorSpace::toYUV(const QVector<double> &channelValues, qreal *y, qreal *u, qreal *v) const
77
78
79
80
81
82
83
84
85
86
87
88
89
90
{
    *y =channelValues[0];
    *u=channelValues[1];
    *v=channelValues[2];
}

QVector <double> YCbCrU8ColorSpace::fromYUV(qreal *y, qreal *u, qreal *v) const
{
    QVector <double> channelValues(4);
    channelValues[0]=*y;
    channelValues[1]=*u;
    channelValues[2]=*v;
    channelValues[3]=1.0;
    return channelValues;
91
}