hypnoticelement.cpp
7.8 KB
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
/* Webcamoid, webcam capture application.
* Copyright (C) 2016 Gonzalo Exequiel Pedone
*
* Webcamoid is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Webcamoid 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Webcamoid. If not, see <http://www.gnu.org/licenses/>.
*
* Web-Site: http://webcamoid.github.io/
*/
#include <QDataStream>
#include <QQmlContext>
#include <QSize>
#include <QtMath>
#include <qrgb.h>
#include <akfrac.h>
#include <akpacket.h>
#include <akvideocaps.h>
#include <akvideoconverter.h>
#include <akvideopacket.h>
#include "hypnoticelement.h"
class HypnoticElementPrivate
{
public:
HypnoticElement::OpticMode m_mode {HypnoticElement::OpticModeSpiral1};
HypnoticElement::OpticMode m_currentMode {HypnoticElement::OpticModeSpiral1};
int m_speedInc {0};
int m_threshold {127};
QSize m_frameSize;
QRgb m_palette[256];
AkVideoPacket m_opticalMap;
quint8 m_speed {16};
quint8 m_phase {0};
AkVideoConverter m_videoConverter {{AkVideoCaps::Format_argbpack, 0, 0, {}}};
inline void createPalette();
inline AkVideoPacket createOpticalMap(const QSize &size,
HypnoticElement::OpticMode mode) const;
inline AkVideoPacket imageThreshold(const AkVideoPacket &src,
int threshold) const;
};
HypnoticElement::HypnoticElement(): AkElement()
{
this->d = new HypnoticElementPrivate;
this->d->createPalette();
}
HypnoticElement::~HypnoticElement()
{
delete this->d;
}
HypnoticElement::OpticMode HypnoticElement::mode() const
{
return this->d->m_mode;
}
int HypnoticElement::speedInc() const
{
return this->d->m_speedInc;
}
int HypnoticElement::threshold() const
{
return this->d->m_threshold;
}
QString HypnoticElement::controlInterfaceProvide(const QString &controlId) const
{
Q_UNUSED(controlId)
return QString("qrc:/Hypnotic/share/qml/main.qml");
}
void HypnoticElement::controlInterfaceConfigure(QQmlContext *context,
const QString &controlId) const
{
Q_UNUSED(controlId)
context->setContextProperty("Hypnotic", const_cast<QObject *>(qobject_cast<const QObject *>(this)));
context->setContextProperty("controlId", this->objectName());
}
AkPacket HypnoticElement::iVideoStream(const AkVideoPacket &packet)
{
this->d->m_videoConverter.begin();
auto src = this->d->m_videoConverter.convert(packet);
this->d->m_videoConverter.end();
if (!src)
return {};
AkVideoPacket dst(src.caps());
dst.copyMetadata(src);
QSize frameSize(src.caps().width(), src.caps().height());
if (frameSize != this->d->m_frameSize
|| this->d->m_currentMode != this->d->m_mode) {
this->d->m_speed = 16;
this->d->m_phase = 0;
this->d->m_opticalMap = this->d->createOpticalMap(frameSize, this->d->m_mode);
this->d->m_frameSize = frameSize;
this->d->m_currentMode = this->d->m_mode;
}
this->d->m_speed += this->d->m_speedInc;
this->d->m_phase -= this->d->m_speed;
auto diff = this->d->imageThreshold(src, this->d->m_threshold);
for (int i = 0, y = 0; y < src.caps().height(); y++) {
auto optLine = this->d->m_opticalMap.constLine(0, y);
auto diffLine = diff.constLine(0, y);
auto dstLine = reinterpret_cast<QRgb *>(dst.line(0, y));
for (int x = 0; x < src.caps().width(); i++, x++)
dstLine[x] = this->d->m_palette[(quint8(int(optLine[x]) + int(this->d->m_phase)) ^ diffLine[x]) & 255];
}
if (dst)
emit this->oStream(dst);
return dst;
}
void HypnoticElement::setMode(OpticMode mode)
{
if (this->d->m_mode == mode)
return;
this->d->m_mode = mode;
emit this->modeChanged(mode);
}
void HypnoticElement::setSpeedInc(int speedInc)
{
if (this->d->m_speedInc == speedInc)
return;
this->d->m_speedInc = speedInc;
emit this->speedIncChanged(speedInc);
}
void HypnoticElement::setThreshold(int threshold)
{
if (this->d->m_threshold == threshold)
return;
this->d->m_threshold = threshold;
emit this->thresholdChanged(threshold);
}
void HypnoticElement::resetMode()
{
this->setMode(HypnoticElement::OpticModeSpiral1);
}
void HypnoticElement::resetSpeedInc()
{
this->setSpeedInc(0);
}
void HypnoticElement::resetThreshold()
{
this->setThreshold(127);
}
QDataStream &operator >>(QDataStream &istream, HypnoticElement::OpticMode &mode)
{
int modeInt;
istream >> modeInt;
mode = static_cast<HypnoticElement::OpticMode>(modeInt);
return istream;
}
QDataStream &operator <<(QDataStream &ostream, HypnoticElement::OpticMode mode)
{
ostream << static_cast<int>(mode);
return ostream;
}
void HypnoticElementPrivate::createPalette()
{
for (int i = 0; i < 112; i++) {
this->m_palette[i] = qRgb(0, 0, 0);
this->m_palette[i + 128] = qRgb(255, 255, 255);
}
for (int i = 0; i < 16; i++) {
auto color = QRgb(16 * (i + 1) - 1);
this->m_palette[i + 112] = qRgb(qRed(color), qGreen(color), qBlue(color));
color = 255 - color;
this->m_palette[i + 240] = qRgb(qRed(color), qGreen(color), qBlue(color));
}
}
AkVideoPacket HypnoticElementPrivate::createOpticalMap(const QSize &size,
HypnoticElement::OpticMode mode) const
{
AkVideoPacket opticalMap({AkVideoCaps::Format_y8,
size.width(),
size.height(),
{}});
for (int y = 0; y < size.height(); y++) {
auto line = opticalMap.line(0, y);
auto yy = qreal(2 * y - size.height()) / (2 * size.width());
for (int x = 0; x < size.width(); x++) {
auto xx = qreal(2 * x - size.width()) / (2 * size.width());
qreal r = sqrt(xx * xx + yy * yy);
qreal at = atan2(xx, yy);
switch (mode) {
case HypnoticElement::OpticModeSpiral1:
line[x] = quint8(256.0 * at / M_PI + 4000.0 * r);
break;
case HypnoticElement::OpticModeSpiral2: {
int j = int(300.0 * r / 32.0);
qreal rr = 300.0 * r - 32.0 * j;
j *= 64;
j += rr > 28.0? qRound(16.0 * (rr - 28.0)): 0;
line[x] = quint8(4096.0 * at / M_PI + 1600.0 * r - j);
break;
}
case HypnoticElement::OpticModeParabola:
line[x] = quint8(400.0 * yy / (0.3 * xx * xx + 0.1));
break;
case HypnoticElement::OpticModeHorizontalStripe:
line[x] = quint8(5120 * x / size.width());
break;
}
}
}
return opticalMap;
}
AkVideoPacket HypnoticElementPrivate::imageThreshold(const AkVideoPacket &src,
int threshold) const
{
auto ocaps = src.caps();
ocaps.setFormat(AkVideoCaps::Format_y8);
AkVideoPacket diff(ocaps);
diff.copyMetadata(src);
for (int y = 0; y < src.caps().height(); y++) {
auto srcBits = reinterpret_cast<const QRgb *>(src.constLine(0, y));
auto diffBits = diff.line(0, y);
for (int x = 0; x < src.caps().width(); x++)
diffBits[x] = qGray(srcBits[x]) >= threshold? 255: 0;
}
return diff;
}
#include "moc_hypnoticelement.cpp"