-
Notifications
You must be signed in to change notification settings - Fork 2
/
voiceprintview.cpp
103 lines (92 loc) · 2.66 KB
/
voiceprintview.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
#include "voiceprintview.h"
#include <QPainter>
#include <QtGui>
VoicePrintView::VoicePrintView(QWidget *parent)
: QWidget(parent)
, place(0)
{
img = new QImage(width(), height(), QImage::Format_RGBX8888);
setAttribute(Qt::WA_OpaquePaintEvent, true);
}
void VoicePrintView::deliverSamples(
const std::int16_t *data, std::size_t count)
{
QRgb* line = (QRgb*)img->scanLine(place++);
if (place >= img->height())
place = 0;
// If it is too many, reduce it
if (count > img->width())
count = img->width();
std::transform(data, data + count, line,
[](std::int16_t const& sample) -> QRgb {
int v = int(sample);
int v1 = std::max(0, std::min(0xFF, v));
int v2 = 0xFF - std::max(0, std::min(0xFF, v >> 8));
// why backwards? :(
QRgb result = v2 == 0xFF
? QColor::fromRgb(v1, v1, v1).rgb()
: QColor::fromRgb(v2, v2, 0xFF).rgb();
return result;
});
//update();
}
void VoicePrintView::paintEvent(QPaintEvent *pe)
{
// Image is drawn in two halves:
// place is needs to be at the bottom
//
// +--------+<-- place
// | bottom |
// |--------|<-- 0
// | top |
// +--------+<-- place - 1
//
// bottom:
// position: 0
// start: place
// height: imgHeight - place
// top:
// position: imgHeight - place
// start: 0
// height: place
QPainter *qp;
qp = new QPainter(this);
int rest = img->height() - place;
int width = img->width();
int height = img->height();
// Draw the stuff before place
if (place != 0)
{
qp->drawImage(0, 0, *img,
0, place,
width, rest);
}
qp->drawImage(0, rest, *img,
0, 0,
width, place);
qp->end();
}
void VoicePrintView::resizeEvent(QResizeEvent *re)
{
QSize viewSize = re->size();
QSize imgSize = img->size();
int heightDiff;
if (viewSize.height() != imgSize.height() ||
viewSize.width() != imgSize.width())
{
heightDiff = viewSize.height() - imgSize.height();
place += heightDiff;
while (place < 0 && viewSize.height())
place += viewSize.height();
if (place > viewSize.height())
place = 0;
// Keep bottom left part of old image
QImage *newimage = new QImage(
img->copy(0, imgSize.height() - viewSize.height(),
viewSize.width(), viewSize.height()));
delete img;
img = newimage;
if (place >= viewSize.height())
place = 0;
}
}