Differential D20563 Diff 56262 plugins/color/colorspaceextensions/kis_luminositytoalpha_adjustment.cpp
Changeset View
Changeset View
Standalone View
Standalone View
plugins/color/colorspaceextensions/kis_luminositytoalpha_adjustment.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | * Copyright (c) 2013 Boudewijn Rempt <boud@valdyas.org> | ||||
3 | * | ||||
4 | * This library is free software; you can redistribute it and/or | ||||
5 | * modify it under the terms of the GNU Lesser General Public | ||||
6 | * License as published by the Free Software Foundation; version 2 | ||||
7 | * of the License. | ||||
8 | * | ||||
9 | * This library 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 GNU | ||||
12 | * Lesser General Public License for more details. | ||||
13 | * | ||||
14 | * You should have received a copy of the GNU Lesser General Public License | ||||
15 | * along with this library; see the file COPYING.LIB. If not, write to | ||||
16 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||||
17 | * Boston, MA 02110-1301, USA. | ||||
18 | */ | ||||
19 | | ||||
20 | #include "kis_luminositytoalpha_adjustment.h" | ||||
21 | | ||||
22 | #include <KoConfig.h> | ||||
23 | | ||||
24 | #include <kis_debug.h> | ||||
25 | #include <klocalizedstring.h> | ||||
26 | | ||||
27 | #include <KoColorConversions.h> | ||||
28 | #include <KoColorModelStandardIds.h> | ||||
29 | #include <KoColorSpace.h> | ||||
30 | #include <KoColorSpaceTraits.h> | ||||
31 | #include <KoColorTransformation.h> | ||||
32 | #include <KoID.h> | ||||
33 | | ||||
34 | #define SCALE_TO_FLOAT( v ) KoColorSpaceMaths< _channel_type_, float>::scaleToA( v ) | ||||
35 | #define SCALE_FROM_FLOAT( v ) KoColorSpaceMaths< float, _channel_type_>::scaleToA( v ) | ||||
36 | | ||||
37 | // Based on Kis_Desaturate_Adjustment | ||||
38 | | ||||
39 | template<typename _channel_type_,typename traits> | ||||
40 | class KisLuminosityToAlphaAdjustment : public KoColorTransformation | ||||
41 | { | ||||
42 | typedef traits RGBTrait; | ||||
43 | typedef typename RGBTrait::Pixel RGBPixel; | ||||
44 | | ||||
45 | | ||||
46 | public: | ||||
47 | KisLuminosityToAlphaAdjustment() | ||||
48 | { | ||||
49 | } | ||||
50 | | ||||
51 | public: | ||||
52 | | ||||
53 | void transform(const quint8 *srcU8, quint8 *dstU8, qint32 nPixels) const override | ||||
54 | { | ||||
55 | const RGBPixel* src = reinterpret_cast<const RGBPixel*>(srcU8); | ||||
56 | RGBPixel* dst = reinterpret_cast<RGBPixel*>(dstU8); | ||||
57 | float r, g, b, cl,cll,clll,cd,cdd,cddd,cm,cmm,cmmm,ncl; | ||||
58 | while (nPixels > 0) { | ||||
59 | | ||||
60 | r = SCALE_TO_FLOAT(src->red); | ||||
61 | g = SCALE_TO_FLOAT(src->green); | ||||
62 | b = SCALE_TO_FLOAT(src->blue); | ||||
63 | cl= r * 0.299 + g * 0.587 + b * 0.114; | ||||
64 | cd = 1 - cl; | ||||
65 | cll=(cl-cd<0?0:cl-cd); | ||||
66 | clll=(cll-cd<0?0:cll-cd); | ||||
67 | cdd=(cd-cl<0?0:cd-cl); | ||||
68 | cddd=(cdd-cl<0?0:cdd-cl); | ||||
69 | cm=(cl*cd)*2; | ||||
70 | cmm=(1-cll)*(1-cdd); | ||||
71 | cmmm=(1-clll)*(1-cddd); | ||||
72 | | ||||
73 | | ||||
74 | // https://www.gimp.org/tutorials/Luminosity_Masks/ | ||||
75 | switch(m_type) { | ||||
76 | case 0: // L | ||||
77 | { | ||||
78 | ncl = cl; | ||||
79 | break; | ||||
80 | } | ||||
81 | case 1: // LL | ||||
82 | { | ||||
83 | | ||||
84 | ncl = cll; | ||||
85 | break; | ||||
86 | | ||||
87 | } | ||||
88 | | ||||
89 | case 2: // LLL | ||||
90 | { | ||||
91 | ncl = clll; | ||||
92 | break; | ||||
93 | | ||||
94 | } | ||||
95 | case 3: // M | ||||
96 | { | ||||
97 | ncl = cm; | ||||
98 | break; | ||||
99 | } | ||||
100 | case 4: // MM | ||||
101 | { | ||||
102 | ncl = cmm; | ||||
103 | break; | ||||
104 | } | ||||
105 | case 5: // MMM | ||||
106 | { | ||||
107 | ncl = cmmm; | ||||
108 | break; | ||||
109 | } | ||||
110 | case 6: // D | ||||
111 | { | ||||
112 | ncl = cd; | ||||
113 | break; | ||||
114 | } | ||||
115 | case 7: // DD | ||||
116 | { | ||||
117 | ncl = cdd; | ||||
118 | break; | ||||
119 | } | ||||
120 | case 8: // DDD | ||||
121 | { | ||||
122 | ncl = cddd; | ||||
123 | break; | ||||
124 | } | ||||
125 | | ||||
126 | | ||||
127 | default: | ||||
128 | ncl = 0; | ||||
129 | } | ||||
130 | dst->red = src->red; | ||||
131 | dst->green = src->green; | ||||
132 | dst->blue = src->blue; | ||||
133 | dst->alpha = SCALE_FROM_FLOAT(ncl); | ||||
134 | | ||||
135 | --nPixels; | ||||
136 | ++src; | ||||
137 | ++dst; | ||||
138 | } | ||||
139 | } | ||||
140 | | ||||
141 | QList<QString> parameters() const override | ||||
142 | { | ||||
143 | QList<QString> list; | ||||
144 | list << "type"; | ||||
145 | return list; | ||||
146 | } | ||||
147 | | ||||
148 | int parameterId(const QString& name) const override | ||||
149 | { | ||||
150 | if (name == "type") { | ||||
151 | return 0; | ||||
152 | } | ||||
153 | return -1; | ||||
154 | } | ||||
155 | | ||||
156 | /** | ||||
157 | * name - "type": | ||||
158 | * 0: L | ||||
159 | * 1: LL | ||||
160 | * 2: LLL | ||||
161 | */ | ||||
162 | void setParameter(int id, const QVariant& parameter) override | ||||
163 | { | ||||
164 | switch(id) | ||||
165 | { | ||||
166 | case 0: | ||||
167 | m_type = parameter.toDouble(); | ||||
168 | break; | ||||
169 | default: | ||||
170 | ; | ||||
171 | } | ||||
172 | } | ||||
173 | | ||||
174 | private: | ||||
175 | | ||||
176 | int m_type; | ||||
177 | | ||||
178 | }; | ||||
179 | | ||||
180 | | ||||
181 | KisLuminosityToAlphaAdjustmentFactory::KisLuminosityToAlphaAdjustmentFactory() | ||||
182 | : KoColorTransformationFactory("desaturate_adjustment") | ||||
183 | { | ||||
184 | } | ||||
185 | | ||||
186 | QList< QPair< KoID, KoID > > KisLuminosityToAlphaAdjustmentFactory::supportedModels() const | ||||
187 | { | ||||
188 | QList< QPair< KoID, KoID > > l; | ||||
189 | l.append(QPair< KoID, KoID >(RGBAColorModelID , Integer8BitsColorDepthID)); | ||||
190 | l.append(QPair< KoID, KoID >(RGBAColorModelID , Integer16BitsColorDepthID)); | ||||
191 | l.append(QPair< KoID, KoID >(RGBAColorModelID , Float16BitsColorDepthID)); | ||||
192 | l.append(QPair< KoID, KoID >(RGBAColorModelID , Float32BitsColorDepthID)); | ||||
193 | return l; | ||||
194 | } | ||||
195 | | ||||
196 | KoColorTransformation* KisLuminosityToAlphaAdjustmentFactory::createTransformation(const KoColorSpace* colorSpace, QHash<QString, QVariant> parameters) const | ||||
197 | { | ||||
198 | KoColorTransformation * adj; | ||||
199 | if (colorSpace->colorModelId() != RGBAColorModelID) { | ||||
200 | dbgKrita << "Unsupported color space " << colorSpace->id() << " in KisLuminosityToAlphaAdjustmentFactory::createTransformation"; | ||||
201 | return 0; | ||||
202 | } | ||||
203 | if (colorSpace->colorDepthId() == Integer8BitsColorDepthID) { | ||||
204 | adj = new KisLuminosityToAlphaAdjustment< quint8, KoBgrTraits < quint8 > >(); | ||||
205 | } else if (colorSpace->colorDepthId() == Integer16BitsColorDepthID) { | ||||
206 | adj = new KisLuminosityToAlphaAdjustment< quint16, KoBgrTraits < quint16 > >(); | ||||
207 | } | ||||
208 | #ifdef HAVE_OPENEXR | ||||
209 | else if (colorSpace->colorDepthId() == Float16BitsColorDepthID) { | ||||
210 | adj = new KisLuminosityToAlphaAdjustment< half, KoRgbTraits < half > >(); | ||||
211 | } | ||||
212 | #endif | ||||
213 | else if (colorSpace->colorDepthId() == Float32BitsColorDepthID) { | ||||
214 | adj = new KisLuminosityToAlphaAdjustment< float, KoRgbTraits < float > >(); | ||||
215 | } | ||||
216 | else { | ||||
217 | dbgKrita << "Unsupported color space " << colorSpace->id() << " in KisLuminosityToAlphaAdjustmentFactory::createTransformation"; | ||||
218 | return 0; | ||||
219 | } | ||||
220 | adj->setParameters(parameters); | ||||
221 | return adj; | ||||
222 | | ||||
223 | } |