forked from wang-bin/QtAV
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathColorTransform.cpp
206 lines (180 loc) · 5.35 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
/******************************************************************************
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 {
// TODO: type bt601 etc
QMatrix4x4 ColorTransform::YUV2RGB()
{
return 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);
}
class ColorTransform::Private : public QSharedData
{
public:
Private()
: recompute(true)
, hue(0)
, saturation(0)
, contrast(0)
, brightness(0)
{}
Private(const Private& other)
: QSharedData(other)
, recompute(true)
, hue(0)
, saturation(0)
, contrast(0)
, brightness(0)
{}
~Private() {}
void reset() {
recompute = true;
hue = 0;
saturation = 0;
contrast = 0;
brightness = 0;
M.setToIdentity();
}
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;
}
mutable bool recompute;
qreal hue, saturation, contrast, brightness;
mutable QMatrix4x4 M;
};
ColorTransform::ColorTransform()
: d(new Private())
{
}
ColorTransform::~ColorTransform()
{
}
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