forked from wang-bin/QtAV
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ColorTransform.cpp
287 lines (255 loc) · 7.63 KB
/
ColorTransform.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/******************************************************************************
QtAV: Media play library based on Qt and FFmpeg
Copyright (C) 2012-2014 Wang Bin <[email protected]>
* This file is part of QtAV
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
******************************************************************************/
#include "QtAV/ColorTransform.h"
#include <QtCore/qmath.h>
namespace QtAV {
static const QMatrix4x4 kGBR2RGB = QMatrix4x4(0, 0, 1, 0,
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 0, 1);
static const QMatrix4x4 yuv2rgb_bt601 =
QMatrix4x4(
1.0f, 0.000f, 1.402f, 0.0f,
1.0f, -0.344f, -0.714f, 0.0f,
1.0f, 1.772f, 0.000f, 0.0f,
0.0f, 0.000f, 0.000f, 1.0f)
*
QMatrix4x4(
1.0f, 0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f, -0.5f,
0.0f, 0.0f, 1.0f, -0.5f,
0.0f, 0.0f, 0.0f, 1.0f);
static const QMatrix4x4 yuv2rgb_bt709 =
QMatrix4x4(
1.0f, 0.000f, 1.5701f, 0.0f,
1.0f, -0.187f, -0.4664f, 0.0f,
1.0f, 1.8556f, 0.000f, 0.0f,
0.0f, 0.000f, 0.000f, 1.0f)
*
QMatrix4x4(
1.0f, 0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f, -0.5f,
0.0f, 0.0f, 1.0f, -0.5f,
0.0f, 0.0f, 0.0f, 1.0f);
const QMatrix4x4& ColorTransform::YUV2RGB(ColorSpace cs)
{
switch (cs) {
case BT601:
return yuv2rgb_bt601;
case BT709:
return yuv2rgb_bt709;
default:
return yuv2rgb_bt601;
}
return yuv2rgb_bt601;
}
class ColorTransform::Private : public QSharedData
{
public:
Private()
: recompute(true)
, in(ColorTransform::RGB)
, out(ColorTransform::RGB)
, hue(0)
, saturation(0)
, contrast(0)
, brightness(0)
{}
Private(const Private& other)
: QSharedData(other)
, recompute(true)
, in(ColorTransform::RGB)
, out(ColorTransform::RGB)
, hue(0)
, saturation(0)
, contrast(0)
, brightness(0)
{}
~Private() {}
void reset() {
recompute = true;
//in = out = ColorTransform::RGB; ///
hue = 0;
saturation = 0;
contrast = 0;
brightness = 0;
M.setToIdentity();
}
// TODO: optimize for other color spaces
void compute() const {
recompute = false;
//http://docs.rainmeter.net/tips/colormatrix-guide
//http://www.graficaobscura.com/matrix/index.html
//http://beesbuzz.biz/code/hsv_color_transforms.php
// ??
float b = brightness;
// brightness R,G,B
QMatrix4x4 B(1, 0, 0, b,
0, 1, 0, b,
0, 0, 1, b,
0, 0, 0, 1);
float c = contrast 1.0;
// Contrast (offset) R,G,B
QMatrix4x4 C(c, 0, 0, 0,
0, c, 0, 0,
0, 0, c, 0,
0, 0, 0, 1);
// Saturation
const float wr = 0.3086f;
const float wg = 0.6094f;
const float wb = 0.0820f;
float s = saturation 1.0f;
QMatrix4x4 S(
(1.0f - s)*wr s, (1.0f - s)*wg , (1.0f - s)*wb , 0.0f,
(1.0f - s)*wr , (1.0f - s)*wg s, (1.0f - s)*wb , 0.0f,
(1.0f - s)*wr , (1.0f - s)*wg , (1.0f - s)*wb s, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f
);
// Hue
const float n = 1.0f / sqrtf(3.0f); // normalized hue rotation axis: sqrt(3)*(1 1 1)
const float h = hue*M_PI; // hue rotation angle
const float hc = cosf(h);
const float hs = sinf(h);
QMatrix4x4 H( // conversion of angle/axis representation to matrix representation
n*n*(1.0f - hc) hc , n*n*(1.0f - hc) - n*hs, n*n*(1.0f - hc) n*hs, 0.0f,
n*n*(1.0f - hc) n*hs, n*n*(1.0f - hc) hc , n*n*(1.0f - hc) - n*hs, 0.0f,
n*n*(1.0f - hc) - n*hs, n*n*(1.0f - hc) n*hs, n*n*(1.0f - hc) hc , 0.0f,
0.0f, 0.0f, 0.0f, 1.0f
);
M = B*C*S*H;
// TODO: transform to output color space other than RGB
switch (in) {
case ColorTransform::RGB:
break;
case ColorTransform::GBR:
M *= kGBR2RGB;
break;
default:
M *= YUV2RGB(in);
break;
}
switch (out) {
case ColorTransform::RGB:
break;
case ColorTransform::GBR:
M = kGBR2RGB.inverted() * M;
break;
default:
M = YUV2RGB(out).inverted() * M;
break;
}
}
mutable bool recompute;
ColorTransform::ColorSpace in, out;
qreal hue, saturation, contrast, brightness;
mutable QMatrix4x4 M; // count the transformations between spaces
};
ColorTransform::ColorTransform()
: d(new Private())
{
}
ColorTransform::~ColorTransform()
{
}
ColorTransform::ColorSpace ColorTransform::inputColorSpace() const
{
return d->in;
}
void ColorTransform::setInputColorSpace(ColorSpace cs)
{
if (d->in == cs)
return;
d->in = cs;
d->recompute = true; //TODO: only recompute color space transform
}
ColorTransform::ColorSpace ColorTransform::outputColorSpace() const
{
return d->out;
}
void ColorTransform::setOutputColorSpace(ColorSpace cs)
{
if (d->out == cs)
return;
d->out = cs;
d->recompute = true; //TODO: only recompute color space transform
}
QMatrix4x4 ColorTransform::matrix() const
{
if (d->recompute)
d->compute();
return d->M;
}
const QMatrix4x4& ColorTransform::matrixRef() const
{
if (d->recompute)
d->compute();
return d->M;
}
/*!
* \brief reset
* set to identity
*/
void ColorTransform::reset()
{
d->reset();
}
void ColorTransform::setBrightness(qreal brightness)
{
if (d->brightness == brightness)
return;
d->brightness = brightness;
d->recompute = true;
}
qreal ColorTransform::brightness() const
{
return d->brightness;
}
// -1~1
void ColorTransform::setHue(qreal hue)
{
if (d->hue == hue)
return;
d->hue = hue;
d->recompute = true;
}
qreal ColorTransform::hue() const
{
return d->hue;
}
void ColorTransform::setContrast(qreal contrast)
{
if (d->contrast == contrast)
return;
d->contrast = contrast;
d->recompute = true;
}
qreal ColorTransform::contrast() const
{
return d->contrast;
}
void ColorTransform::setSaturation(qreal saturation)
{
if (d->saturation == saturation)
return;
d->saturation = saturation;
d->recompute = true;
}
qreal ColorTransform::saturation() const
{
return d->saturation;
}
} //namespace QtAV