1 /* Webcamoid, webcam capture application.
2  * Copyright (C) 2016  Gonzalo Exequiel Pedone
3  *
4  * Webcamoid is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * Webcamoid 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
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with Webcamoid. If not, see <http://www.gnu.org/licenses/>.
16  *
17  * Web-Site: http://webcamoid.github.io/
18  */
19 
20 #include <QImage>
21 #include <QQmlContext>
22 #include <QtMath>
23 #include <QMutex>
24 #include <akpacket.h>
25 #include <akvideopacket.h>
26 
27 #include "waveelement.h"
28 
29 class WaveElementPrivate
30 {
31     public:
32         qreal m_amplitude {0.12};
33         qreal m_frequency {8};
34         qreal m_phase {0.0};
35         QRgb m_background {qRgb(0, 0, 0)};
36         QSize m_frameSize;
37         QVector<int> m_sineMap;
38         QMutex m_mutex;
39 };
40 
WaveElement()41 WaveElement::WaveElement(): AkElement()
42 {
43     this->d = new WaveElementPrivate;
44 
45     QObject::connect(this,
46                      &WaveElement::amplitudeChanged,
47                      this,
48                      &WaveElement::updateSineMap);
49     QObject::connect(this,
50                      &WaveElement::frequencyChanged,
51                      this,
52                      &WaveElement::updateSineMap);
53     QObject::connect(this,
54                      &WaveElement::phaseChanged,
55                      this,
56                      &WaveElement::updateSineMap);
57     QObject::connect(this,
58                      &WaveElement::backgroundChanged,
59                      this,
60                      &WaveElement::updateSineMap);
61     QObject::connect(this,
62                      &WaveElement::frameSizeChanged,
63                      this,
64                      &WaveElement::updateSineMap);
65 }
66 
~WaveElement()67 WaveElement::~WaveElement()
68 {
69 
70     delete this->d;
71 }
72 
amplitude() const73 qreal WaveElement::amplitude() const
74 {
75     return this->d->m_amplitude;
76 }
77 
frequency() const78 qreal WaveElement::frequency() const
79 {
80     return this->d->m_frequency;
81 }
82 
phase() const83 qreal WaveElement::phase() const
84 {
85     return this->d->m_phase;
86 }
87 
background() const88 QRgb WaveElement::background() const
89 {
90     return this->d->m_background;
91 }
92 
controlInterfaceProvide(const QString & controlId) const93 QString WaveElement::controlInterfaceProvide(const QString &controlId) const
94 {
95     Q_UNUSED(controlId)
96 
97     return QString("qrc:/Wave/share/qml/main.qml");
98 }
99 
controlInterfaceConfigure(QQmlContext * context,const QString & controlId) const100 void WaveElement::controlInterfaceConfigure(QQmlContext *context,
101                                             const QString &controlId) const
102 {
103     Q_UNUSED(controlId)
104 
105     context->setContextProperty("Wave", const_cast<QObject *>(qobject_cast<const QObject *>(this)));
106     context->setContextProperty("controlId", this->objectName());
107 }
108 
iVideoStream(const AkVideoPacket & packet)109 AkPacket WaveElement::iVideoStream(const AkVideoPacket &packet)
110 {
111     auto src = packet.toImage();
112 
113     if (src.isNull())
114         return AkPacket();
115 
116     src = src.convertToFormat(QImage::Format_ARGB32);
117     qreal amplitude = this->d->m_amplitude;
118 
119     QImage oFrame(src.width(), src.height(), src.format());
120     oFrame.fill(this->d->m_background);
121 
122     if (amplitude <= 0.0)
123         akSend(packet)
124 
125     if (amplitude >= 1.0) {
126         auto oPacket = AkVideoPacket::fromImage(oFrame, packet);
127         akSend(oPacket)
128     }
129 
130     if (src.size() != this->d->m_frameSize) {
131         this->d->m_frameSize = src.size();
132         emit this->frameSizeChanged(src.size());
133     }
134 
135     this->d->m_mutex.lock();
136     QVector<int> sineMap(this->d->m_sineMap);
137     this->d->m_mutex.unlock();
138 
139     if (sineMap.isEmpty())
140         akSend(packet)
141 
142     for (int y = 0; y < oFrame.height(); y++) {
143         // Get input line.
144         int yi = int(y / (1.0 - amplitude));
145 
146         if (yi < 0 || yi >= src.height())
147             continue;
148 
149         auto iLine = reinterpret_cast<const QRgb *>(src.constScanLine(yi));
150 
151         for (int x = 0; x < oFrame.width(); x++) {
152             // Get output line.
153             int yo = y  + sineMap[x];
154 
155             if (yo < 0
156                 || yo >= src.height())
157                 continue;
158 
159             QRgb *oLine = reinterpret_cast<QRgb *>(oFrame.scanLine(yo));
160             oLine[x] = iLine[x];
161         }
162     }
163 
164     auto oPacket = AkVideoPacket::fromImage(oFrame, packet);
165     akSend(oPacket)
166 }
167 
setAmplitude(qreal amplitude)168 void WaveElement::setAmplitude(qreal amplitude)
169 {
170     if (qFuzzyCompare(amplitude, this->d->m_amplitude))
171         return;
172 
173     this->d->m_amplitude = amplitude;
174     emit this->amplitudeChanged(amplitude);
175 }
176 
setFrequency(qreal frequency)177 void WaveElement::setFrequency(qreal frequency)
178 {
179     if (qFuzzyCompare(frequency, this->d->m_frequency))
180         return;
181 
182     this->d->m_frequency = frequency;
183     emit this->frequencyChanged(frequency);
184 }
185 
setPhase(qreal phase)186 void WaveElement::setPhase(qreal phase)
187 {
188     if (qFuzzyCompare(this->d->m_phase, phase))
189         return;
190 
191     this->d->m_phase = phase;
192     emit this->phaseChanged(phase);
193 }
194 
setBackground(QRgb background)195 void WaveElement::setBackground(QRgb background)
196 {
197     if (background == this->d->m_background)
198         return;
199 
200     this->d->m_background = background;
201     emit this->backgroundChanged(background);
202 }
203 
resetAmplitude()204 void WaveElement::resetAmplitude()
205 {
206     this->setAmplitude(0.12);
207 }
208 
resetFrequency()209 void WaveElement::resetFrequency()
210 {
211     this->setFrequency(8);
212 }
213 
resetPhase()214 void WaveElement::resetPhase()
215 {
216     this->setPhase(0.0);
217 }
218 
resetBackground()219 void WaveElement::resetBackground()
220 {
221     this->setBackground(qRgb(0, 0, 0));
222 }
223 
updateSineMap()224 void WaveElement::updateSineMap()
225 {
226     if (this->d->m_frameSize.isEmpty())
227         return;
228 
229     int width = this->d->m_frameSize.width();
230     int height = this->d->m_frameSize.height();
231     QVector<int> sineMap(width);
232     qreal phase = 2.0 * M_PI * this->d->m_phase;
233 
234     for (int x = 0; x < width; x++)
235         sineMap[x] = int(0.5 * this->d->m_amplitude * height
236                          * (sin(this->d->m_frequency * 2.0 * M_PI * x / width
237                                 + phase)
238                             + 1.0));
239 
240     this->d->m_mutex.lock();
241     this->d->m_sineMap = sineMap;
242     this->d->m_mutex.unlock();
243 }
244 
245 #include "moc_waveelement.cpp"
246