-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDisplayThread.cpp
More file actions
121 lines (103 loc) · 2.79 KB
/
DisplayThread.cpp
File metadata and controls
121 lines (103 loc) · 2.79 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
#include "DisplayThread.h"
#include <algorithm>
#define MAX_FRAMES_PER_SECOND 30
DisplayThread::DisplayThread(unsigned long long bufferCount, MicroscopeManager* mm, QObject* mainWindow, int* targetFrameInfo) :
bufferCount_(bufferCount),
mm_(mm),
mainWindow_(mainWindow),
targetFrameInfo_(targetFrameInfo),
frameCount_(targetFrameInfo[0]),
volumeCount_(targetFrameInfo[2]),
currentFrame_(targetFrameInfo[1])
{
width = mm->GetCameraIntParameter(STREAM_MODULE, "Width");
height = mm->GetCameraIntParameter(STREAM_MODULE, "Height");
if (mm->GetCameraStringParameter(REMOTE_MODULE, "AcquisitionMode") == "SingleFrame")
{
framerate = frameCount_ * volumeCount_;
}
else
{
framerate = mm->GetCameraFloatParameter(REMOTE_MODULE, "AcquisitionFrameRate");
}
framesToDrop_ = ceilf(framerate / (frameCount_ * MAX_FRAMES_PER_SECOND)) - 1;
buf_ = new unsigned char[width * height];
pix_ = new PixmapReadyObject();
frameThd_ = std::thread(&DisplayThread::CheckFrameInfo, this);
disThd_ = std::thread(&DisplayThread::Display, this);
}
DisplayThread::~DisplayThread()
{
delete[] buf_;
delete pix_;
}
void DisplayThread::CheckFrameInfo() //This is super questionable, redesign at some point?
{
while (active)
{
if (frameCount_ != targetFrameInfo_[0])
{
frameCount_ = targetFrameInfo_[0];
if (mm_->GetCameraStringParameter(REMOTE_MODULE, "AcquisitionMode") == "SingleFrame")
{
framerate = frameCount_ * volumeCount_;
}
framesToDrop_ = ceilf(framerate / (frameCount_ * MAX_FRAMES_PER_SECOND)) - 1;
}
if (currentFrame_ != targetFrameInfo_[1])
{
currentFrame_ = targetFrameInfo_[1];
}
if (volumeCount_ != targetFrameInfo_[2])
{
volumeCount_ = targetFrameInfo_[2];
if (mm_->GetCameraStringParameter(REMOTE_MODULE, "AcquisitionMode") == "SingleFrame")
{
framerate = frameCount_ * volumeCount_;
}
}
}
}
void DisplayThread::Display()
{
QObject::connect(pix_, SIGNAL(pixmapReady(const QPixmap&, bool)), mainWindow_, SLOT(updateDisplayFrame(const QPixmap&, bool)));
unsigned long long bufferSize = std::min(mm_->GetImageBufferSize(), width * height);
int framesDropped = 0;
while (active)
{
mm_->GetImage();
//mm_->ApplyCameraMask();
if (bufferCount_ % frameCount_ == currentFrame_)
{
if (framesDropped >= framesToDrop_)
{
framesDropped = 0;
try
{
std::memcpy(buf_, mm_->GetImageBuffer(), bufferSize);
QImage img = QImage(buf_, width, height, width, QImage::Format_Grayscale8);
const QPixmap pixmap = QPixmap::fromImage(img);
pixmapProcessed = false;
pix_->sendSignal(pixmap, false);
}
catch (...)
{
active = false;
}
}
else
{
++framesDropped;
}
}
if (--bufferCount_ == 0)
{
active = false;
}
}
}
void DisplayThread::WaitForThread()
{
frameThd_.join();
disThd_.join();
}