Krita Source Code Documentation
Loading...
Searching...
No Matches
kis_desaturate_adjustment.cpp
Go to the documentation of this file.
1/*
2* SPDX-FileCopyrightText: 2013 Boudewijn Rempt <boud@valdyas.org>
3 *
4 * SPDX-License-Identifier: LGPL-2.0-only
5*/
6
8
9#include <KoConfig.h>
10
11#include <kis_debug.h>
12#include <klocalizedstring.h>
13
14#include <KoColorConversions.h>
16#include <KoColorSpace.h>
17#include <KoColorSpaceTraits.h>
19#include <KoID.h>
20
21#define SCALE_TO_FLOAT( v ) KoColorSpaceMaths< _channel_type_, float>::scaleToA( v )
22#define SCALE_FROM_FLOAT( v ) KoColorSpaceMaths< float, _channel_type_>::scaleToA( v )
23
24template<typename _channel_type_,typename traits>
26{
27 typedef traits RGBTrait;
28 typedef typename RGBTrait::Pixel RGBPixel;
29
30public:
34
35public:
36
37 void transform(const quint8 *srcU8, quint8 *dstU8, qint32 nPixels) const override
38 {
39 const RGBPixel* src = reinterpret_cast<const RGBPixel*>(srcU8);
40 RGBPixel* dst = reinterpret_cast<RGBPixel*>(dstU8);
41 float r, g, b, gray;
42 while (nPixels > 0) {
43
44 r = SCALE_TO_FLOAT(src->red);
45 g = SCALE_TO_FLOAT(src->green);
46 b = SCALE_TO_FLOAT(src->blue);
47
48 // https://www.tannerhelland.com/3643/grayscale-image-algorithm-vb6/
49 switch(m_type) {
50 case 0: // lightness
51 {
52 gray = (qMax(qMax(r, g), b) + qMin(qMin(r, g), b)) / 2;
53 break;
54 }
55 case 1: // luminosity BT 709
56 {
57 gray = r * 0.2126 + g * 0.7152 + b * 0.0722;
58 break;
59
60 }
61
62 case 2: // luminosity BT 601
63 {
64 gray = r * 0.299 + g * 0.587 + b * 0.114;
65 break;
66
67 }
68 case 3: // average
69 {
70 gray = (r + g + b) / 3;
71 break;
72 }
73 case 4: // min
74 {
75 gray = qMin(qMin(r, g), b);
76 break;
77 }
78 case 5: // min
79 {
80 gray = qMax(qMax(r, g), b);
81 break;
82 }
83
84 default:
85 gray = 0;
86 }
87 dst->red = SCALE_FROM_FLOAT(gray);
88 dst->green = SCALE_FROM_FLOAT(gray);
89 dst->blue = SCALE_FROM_FLOAT(gray);
90 dst->alpha = src->alpha;
91
92 --nPixels;
93 ++src;
94 ++dst;
95 }
96 }
97
98 QList<QString> parameters() const override
99 {
100 QList<QString> list;
101 list << "type";
102 return list;
103 }
104
105 int parameterId(const QString& name) const override
106 {
107 if (name == "type") {
108 return 0;
109 }
110 return -1;
111 }
112
119 void setParameter(int id, const QVariant& parameter) override
120 {
121 switch(id)
122 {
123 case 0:
124 m_type = parameter.toDouble();
125 break;
126 default:
127 ;
128 }
129 }
130
131private:
132
133 int m_type {0};
134
135};
136
137
142
144{
146 l.append(QPair< KoID, KoID >(RGBAColorModelID , Integer8BitsColorDepthID));
147 l.append(QPair< KoID, KoID >(RGBAColorModelID , Integer16BitsColorDepthID));
148 l.append(QPair< KoID, KoID >(RGBAColorModelID , Float16BitsColorDepthID));
149 l.append(QPair< KoID, KoID >(RGBAColorModelID , Float32BitsColorDepthID));
150 return l;
151}
152
153KoColorTransformation* KisDesaturateAdjustmentFactory::createTransformation(const KoColorSpace* colorSpace, QHash<QString, QVariant> parameters) const
154{
156 if (colorSpace->colorModelId() != RGBAColorModelID) {
157 dbgKrita << "Unsupported color space " << colorSpace->id() << " in KisDesaturateAdjustmentFactory::createTransformation";
158 return 0;
159 }
160 if (colorSpace->colorDepthId() == Integer8BitsColorDepthID) {
162 } else if (colorSpace->colorDepthId() == Integer16BitsColorDepthID) {
164 }
165#ifdef HAVE_OPENEXR
166 else if (colorSpace->colorDepthId() == Float16BitsColorDepthID) {
168 }
169#endif
170 else if (colorSpace->colorDepthId() == Float32BitsColorDepthID) {
172 }
173 else {
174 dbgKrita << "Unsupported color space " << colorSpace->id() << " in KisDesaturateAdjustmentFactory::createTransformation";
175 return 0;
176 }
177 adj->setParameters(parameters);
178 return adj;
179
180}
const KoID Float32BitsColorDepthID("F32", ki18n("32-bit float/channel"))
const KoID Float16BitsColorDepthID("F16", ki18n("16-bit float/channel"))
const KoID Integer8BitsColorDepthID("U8", ki18n("8-bit integer/channel"))
const KoID Integer16BitsColorDepthID("U16", ki18n("16-bit integer/channel"))
const KoID RGBAColorModelID("RGBA", ki18n("RGB/Alpha"))
QList< QPair< KoID, KoID > > supportedModels() const override
KoColorTransformation * createTransformation(const KoColorSpace *colorSpace, QHash< QString, QVariant > parameters) const override
void transform(const quint8 *srcU8, quint8 *dstU8, qint32 nPixels) const override
int parameterId(const QString &name) const override
QList< QString > parameters() const override
void setParameter(int id, const QVariant &parameter) override
virtual KoID colorModelId() const =0
virtual KoID colorDepthId() const =0
void setParameters(const QHash< QString, QVariant > &parameters)
#define dbgKrita
Definition kis_debug.h:45
#define SCALE_FROM_FLOAT(v)
#define SCALE_TO_FLOAT(v)