1 /*
2  *  Copyright (c) 2006 Cyrille Berger <cberger@cberger.net>
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18 
19 #include "GrayF16ColorSpace.h"
20 
21 #include <QDomElement>
22 
23 #include <klocalizedstring.h>
24 
25 #include <KoIntegerMaths.h>
26 #include <KoColorSpaceRegistry.h>
27 
28 #include "compositeops/KoCompositeOps.h"
29 #include <kis_dom_utils.h>
30 
GrayF16ColorSpace(const QString & name,KoColorProfile * p)31 GrayF16ColorSpace::GrayF16ColorSpace(const QString &name, KoColorProfile *p)
32     : LcmsColorSpace<KoGrayF16Traits>(colorSpaceId(), name,  TYPE_GRAYA_HALF_FLT, cmsSigGrayData, p)
33 {
34     const IccColorProfile *icc_p = dynamic_cast<const IccColorProfile *>(p);
35     Q_ASSERT(icc_p);
36     Q_UNUSED(icc_p);
37     addChannel(new KoChannelInfo(i18n("Gray"), 0 * sizeof(half), 0, KoChannelInfo::COLOR, KoChannelInfo::FLOAT16, 2, Qt::gray));
38     addChannel(new KoChannelInfo(i18n("Alpha"), 1 * sizeof(half), 1, KoChannelInfo::ALPHA, KoChannelInfo::FLOAT16, 2));
39 
40     init();
41 
42     addStandardCompositeOps<KoGrayF16Traits>(this);
43 }
44 
clone() const45 KoColorSpace *GrayF16ColorSpace::clone() const
46 {
47     return new GrayF16ColorSpace(name(), profile()->clone());
48 }
49 
colorToXML(const quint8 * pixel,QDomDocument & doc,QDomElement & colorElt) const50 void GrayF16ColorSpace::colorToXML(const quint8 *pixel, QDomDocument &doc, QDomElement &colorElt) const
51 {
52     const KoGrayF16Traits::channels_type *p = reinterpret_cast<const KoGrayF16Traits::channels_type *>(pixel);
53     QDomElement labElt = doc.createElement("Gray");
54     labElt.setAttribute("g", KisDomUtils::toString(KoColorSpaceMaths< KoGrayF16Traits::channels_type, qreal>::scaleToA(p[0])));
55     labElt.setAttribute("space", profile()->name());
56     colorElt.appendChild(labElt);
57 }
58 
colorFromXML(quint8 * pixel,const QDomElement & elt) const59 void GrayF16ColorSpace::colorFromXML(quint8 *pixel, const QDomElement &elt) const
60 {
61     KoGrayF16Traits::channels_type *p = reinterpret_cast<KoGrayF16Traits::channels_type *>(pixel);
62     p[0] = KoColorSpaceMaths< qreal, KoGrayF16Traits::channels_type >::scaleToA(KisDomUtils::toDouble(elt.attribute("g")));
63     p[1] = 1.0;
64 }
65 
toHSY(const QVector<double> & channelValues,qreal *,qreal *,qreal * luma) const66 void GrayF16ColorSpace::toHSY(const QVector<double> &channelValues, qreal *, qreal *, qreal *luma) const
67 {
68     *luma = channelValues[0];
69 }
70 
fromHSY(qreal *,qreal *,qreal * luma) const71 QVector <double> GrayF16ColorSpace::fromHSY(qreal *, qreal *, qreal *luma) const
72 {
73     QVector <double> channelValues(2);
74     channelValues.fill(*luma);
75     channelValues[1]=1.0;
76     return channelValues;
77 }
78 
toYUV(const QVector<double> & channelValues,qreal * y,qreal *,qreal *) const79 void GrayF16ColorSpace::toYUV(const QVector<double> &channelValues, qreal *y, qreal *, qreal *) const
80 {
81     *y = channelValues[0];
82 }
83 
fromYUV(qreal * y,qreal *,qreal *) const84 QVector <double> GrayF16ColorSpace::fromYUV(qreal *y, qreal *, qreal *) const
85 {
86     QVector <double> channelValues(2);
87     channelValues.fill(*y);
88     channelValues[1]=1.0;
89     return channelValues;
90 }
91