-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviewer.cpp
481 lines (409 loc) · 14.5 KB
/
viewer.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
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
#include "debug.h"
#include "viewer.h"
#include "camera.h"
#include "imgui.h"
// for PushItemFlag(), ImGuiItemFlags_Disabled
#include "imgui_internal.h"
#include <assert.h>
#include <stdlib.h>
Viewer::Viewer() {
camera = NULL;
selectedCamera = NULL;
cameras.clear();
image = new Image();
numImages = 0;
numAllImages = 0;
numErrors = 0;
numBytes = 0;
scaleWidth = 1.0f;
scaleHeight = 1.0f;
paletteCurrentIndex = -1;
genicam = new Genicam();
}
Viewer::~Viewer() {
delete genicam;
delete image;
if (camera) {
camera->stop();
}
clearCameraList();
arv_shutdown();
}
void Viewer::clearCameraList(void) {
for (size_t i = 0; i < cameras.size(); i++) {
delete cameras[i];
}
cameras.clear();
camera = NULL;
free(selectedCamera);
selectedCamera = NULL;
}
void Viewer::selectCamera(const unsigned int _index) {
Camera *cam = NULL;
bool found = false;
for (size_t i = 0; i < cameras.size(); i++) {
cam = cameras[i];
if (cam->index == _index) {
found = true;
break;
}
}
assert(found == true);
D("selected camera index %d\n", cam->index);
if (camera && (camera->index == cam->index)) {
// already selected
return;
}
if (selectedCamera) {
free(selectedCamera);
}
selectedCamera = (char *)calloc(1, strlen(cam->deviceId) + strlen(cam->serialNumber) + 5);
sprintf(selectedCamera, "%s - %s", cam->deviceId, cam->serialNumber);
// stop the current device
stopCamera();
camera = cam;
// start selected the current device
startCamera();
}
void Viewer::startCamera(void) {
D("\n");
if (camera == NULL) {
E("no camera.. nothing to do..\n");
return;
}
camera->start();
genicam->initialize(camera->camera);
}
void Viewer::stopCamera(void) {
D("\n");
if (camera == NULL) {
D("no camera.. nothing to do..\n");
return;
}
genicam->destroy();
camera->stop();
}
void Viewer::showCameraList(void) {
ImGui::Begin("Camera list");
// generate list of cameras
if (ImGui::Button("update device list")) {
clearCameraList();
arv_update_device_list();
unsigned int n_devices = arv_get_n_devices();
for (unsigned int i = 0; i < n_devices; i++) {
cameras.push_back(new Camera(i));
}
}
// show list of cameras
if (! cameras.empty()) {
ImGui::Text("Found %zu devices", cameras.size());
ImGui::Columns(7, "cameras");
ImGui::Separator();
ImGui::Text("ID"); ImGui::NextColumn();
ImGui::Text("Device"); ImGui::NextColumn();
ImGui::Text("Vendor"); ImGui::NextColumn();
ImGui::Text("Model"); ImGui::NextColumn();
ImGui::Text("Serial nr."); ImGui::NextColumn();
ImGui::Text("Protocol"); ImGui::NextColumn();
ImGui::Text("Physical ID"); ImGui::NextColumn();
ImGui::Separator();
unsigned int selected = (camera != NULL) ? camera->index : -1;
for (size_t i = 0; i < cameras.size(); i++) {
Camera *cam = cameras[i];
char label[32];
sprintf(label, "%04u", cam->index);
if (ImGui::Selectable(label, selected == cam->index, ImGuiSelectableFlags_SpanAllColumns)){
D("clicked on index %d\n", cam->index);
selectCamera(cam->index);
}
ImGui::NextColumn();
ImGui::Text("%s", cam->deviceId); ImGui::NextColumn();
ImGui::Text("%s", cam->vendor); ImGui::NextColumn();
ImGui::Text("%s", cam->model); ImGui::NextColumn();
ImGui::Text("%s", cam->serialNumber); ImGui::NextColumn();
ImGui::Text("%s", cam->protocol); ImGui::NextColumn();
ImGui::Text("%s", cam->physicalId); ImGui::NextColumn();
}
ImGui::Columns(1);
} else {
ImGui::Text("No cameras.");
}
// ImGui::Separator();
ImGui::End();
}
void Viewer::showCameraInfo(void) {
// show this window only if a camera is selected
if (! selectedCamera) {
return;
}
ImGui::Begin(selectedCamera);
if (! camera->connected) {
ImGui::Text("Camera %s not connected.", camera->deviceId);
return;
}
ImGui::Text("Device name %s", camera->deviceId);
ImGui::Text("Image payload %d bytes", camera->imagePayload);
handleImageSize();
handleImageOffset();
handleImageBinning();
handleImagePixelFormat();
ImGui::Separator();
handleImageFrameRate();
handleExposure();
handleGain();
ImGui::Separator();
if (ImGui::Button("Start")) {
camera->startVideo();
}
ImGui::SameLine();
if (ImGui::Button("Stop")) {
camera->stopVideo();
}
ImGui::SameLine();
if (ImGui::Button("Reset stats")) {
numImages = 0;
numAllImages = 0;
numErrors = 0;
numBytes = 0;
}
ImGui::AlignTextToFramePadding();
ImGui::SetNextItemWidth(200);
const char * paletteCurrent = NULL;
if (paletteCurrentIndex != -1) {
paletteCurrent = paletteList.getName(paletteCurrentIndex);
}
if (ImGui::BeginCombo("Palette", paletteCurrent)) {
for (unsigned int n = 0; n < paletteList.getCount(); n++) {
const bool isSelected = (paletteCurrentIndex == (int)n);
if (ImGui::Selectable(paletteList.getName(n), isSelected)) {
paletteCurrentIndex = n;
paletteCurrent = paletteList.getName(paletteCurrentIndex);
D("palette changed to %d = %s\n", paletteCurrentIndex, paletteCurrent);
assert((camera->imageDepth == 8) || (camera->imageDepth == 16));
unsigned char *map = paletteList.generate(paletteCurrent, camera->imageDepth);
assert(map != NULL);
image->updatePalette(camera->imageDepth, map);
}
// set the initial focus when opening the combo
if (isSelected) {
ImGui::SetItemDefaultFocus();
}
}
ImGui::EndCombo();
}
ImGui::Separator();
if (ImGui::SliderFloat("Width scale", &scaleWidth, 0.0f, 1.0f)) {
image->updateScale(scaleWidth, scaleHeight);
}
if (ImGui::SliderFloat("Height scale", &scaleHeight, 0.0f, 1.0f)) {
image->updateScale(scaleWidth, scaleHeight);
}
ImGui::Separator();
// statistics are updated once per second
if (timeout <= ImGui::GetTime()) {
numImages = camera->numImages;
numAllImages += numImages;
numErrors += camera->numErrors;
numBytes = (double) camera->numBytes / 1e6;
// reset the stats until next timeout (1 s)
camera->numImages = 0;
camera->numErrors = 0;
camera->numBytes = 0;
timeout = ImGui::GetTime() + 1.0;
}
ImGui::Text("rate %d images/s", numImages);
ImGui::Text("transferred %.3g MiB/s", numBytes);
ImGui::Text("# OK images %d", numAllImages);
ImGui::Text("# error images %d", numErrors);
ImGui::Separator();
ImGui::End();
}
void Viewer::showCameraImage(void) {
// show this window only if a camera is selected
if (! selectedCamera) {
return;
}
ImGui::Begin("Image Window");
if (camera->imageUpdate) {
image->updateImage(camera->imageWidth, camera->imageHeight, camera->imageDepth, camera->imageData);
camera->imageUpdate = false;
}
image->render();
ImGui::End();
}
void Viewer::showCameraFeatures(void) {
// show this window only if a camera is selected
if (! selectedCamera) {
return;
}
ImGui::Begin("Features Window");
// if (camera->imageUpdate) {
// image->updateImage(camera->imageWidth, camera->imageHeight, camera->imageData);
// camera->imageUpdate = false;
// }
// image->render();
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(2, 2));
ImGui::Columns(2);
ImGui::Separator();
genicam->listFeatures();
ImGui::Columns(1);
ImGui::Separator();
ImGui::PopStyleVar();
ImGui::End();
}
void Viewer::handleImageSize(void) {
ImGui::AlignTextToFramePadding();
ImGui::SetNextItemWidth(200);
int x = camera->xSize.value;
if (ImGui::SliderInt("Size X", &x, camera->xSize.min, camera->xSize.max, "%u", ImGuiSliderFlags_AlwaysClamp)) {
D("X size changed to %d\n", x);
camera->setImageSize(x, -1);
}
ImGui::SameLine();
ImGui::Text("(%u - %u, +/- %u)", camera->xSize.min, camera->xSize.max, camera->xSize.step);
ImGui::AlignTextToFramePadding();
ImGui::SetNextItemWidth(200);
int y = camera->ySize.value;
if (ImGui::SliderInt("Size Y", &y, camera->ySize.min, camera->ySize.max, "%u", ImGuiSliderFlags_AlwaysClamp)) {
D("Y size changed to %d\n", y);
camera->setImageSize(-1, y);
}
ImGui::SameLine();
ImGui::Text("(%u - %u, +/- %u)", camera->ySize.min, camera->ySize.max, camera->ySize.step);
}
void Viewer::handleImageOffset(void) {
ImGui::AlignTextToFramePadding();
ImGui::SetNextItemWidth(200);
int x = camera->xOffset.value;
if (ImGui::SliderInt("Offset X", &x, camera->xOffset.min, camera->xOffset.max, "%u", ImGuiSliderFlags_AlwaysClamp)) {
D("X offset changed to %d\n", x);
camera->setImageOffset(x, -1);
}
ImGui::SameLine();
ImGui::Text("(%u - %u, +/- %u)", camera->xOffset.min, camera->xOffset.max, camera->xOffset.step);
ImGui::AlignTextToFramePadding();
ImGui::SetNextItemWidth(200);
int y = camera->yOffset.value;
if (ImGui::SliderInt("Offset Y", &y, camera->yOffset.min, camera->yOffset.max, "%u", ImGuiSliderFlags_AlwaysClamp)) {
D("Y offset changed to %d\n", y);
camera->setImageOffset(-1, y);
}
ImGui::SameLine();
ImGui::Text("(%u - %u, +/- %u)", camera->yOffset.min, camera->yOffset.max, camera->yOffset.step);
}
void Viewer::handleImageBinning(void) {
if (! camera->binningAvailable) {
ImGui::Text("Binning is not supported");
return;
}
ImGui::AlignTextToFramePadding();
ImGui::SetNextItemWidth(200);
int x = camera->xBinning.value;
if (ImGui::SliderInt("Binning X", &x, camera->xBinning.min, camera->xBinning.max, "%u", ImGuiSliderFlags_AlwaysClamp)) {
D("X binning changed to %d\n", x);
camera->setImageBinning(x, -1);
}
ImGui::SameLine();
ImGui::Text("(%u - %u, +/- %u)", camera->xBinning.min, camera->xBinning.max, camera->xBinning.step);
ImGui::AlignTextToFramePadding();
ImGui::SetNextItemWidth(200);
int y = camera->yBinning.value;
if (ImGui::SliderInt("Binning Y", &y, camera->yBinning.min, camera->yBinning.max, "%u", ImGuiSliderFlags_AlwaysClamp)) {
D("Y binning changed to %d\n", y);
camera->setImageBinning(-1, y);
}
ImGui::SameLine();
ImGui::Text("(%u - %u, +/- %u)", camera->yBinning.min, camera->yBinning.max, camera->yBinning.step);
}
void Viewer::handleImagePixelFormat(void) {
// ImGui::Text("Number of pixels formats %d", camera->numPixelFormats);
ImGui::AlignTextToFramePadding();
ImGui::SetNextItemWidth(200);
int value = camera->pixelFormatCurrent;
// XXX: Use ImGui::BeginCombo()
if (ImGui::Combo("Pixel format", &value, camera->pixelFormatStrings, camera->numPixelFormats)) {
camera->setPixelFormat(value);
D("pixel format changed to %s (0x%08X)\n", camera->pixelFormatString, camera->pixelFormat);
}
}
void Viewer::handleImageFrameRate(void) {
if (! camera->frameRateAvailable) {
ImGui::Text("Frame rate control is not supported");
return;
}
ImGui::AlignTextToFramePadding();
ImGui::SetNextItemWidth(200);
float value = camera->frameRate.value;
if (ImGui::SliderFloat("Frame rate", &value, camera->frameRate.min, camera->frameRate.max, "%.3f", ImGuiSliderFlags_AlwaysClamp)) {
D("frame rate changed to %f\n", value);
camera->setFrameRate(value);
}
ImGui::SameLine();
ImGui::Text("(%.3f - %.3f, +/- %.3f)", camera->frameRate.min, camera->frameRate.max, camera->frameRate.step);
}
void Viewer::handleGain(void) {
if (! camera->gainAvailable) {
ImGui::Text("Gain control is not supported");
return;
}
// manual gain control
ImGui::AlignTextToFramePadding();
ImGui::SetNextItemWidth(200);
if (camera->gainAutoAvailable && camera->gainAuto) {
ImGui::PushItemFlag(ImGuiItemFlags_Disabled, true);
ImGui::PushStyleVar(ImGuiStyleVar_Alpha, ImGui::GetStyle().Alpha * 0.5f);
}
float value = camera->gain.value;
if (ImGui::SliderFloat("Gain", &value, camera->gain.min, camera->gain.max, "%.3f", ImGuiSliderFlags_AlwaysClamp)) {
D("gain changed to %f\n", value);
camera->setGain(value);
}
ImGui::SameLine();
ImGui::Text("(%.3f - %.3f, +/- %.3f)", camera->gain.min, camera->gain.max, camera->gain.step);
if (camera->gainAutoAvailable && camera->gainAuto) {
ImGui::PopItemFlag();
ImGui::PopStyleVar();
}
// auto gain control
if (camera->gainAutoAvailable) {
ImGui::SameLine();
bool value = camera->gainAuto;
if (ImGui::Checkbox("Gain auto", &value)) {
D("auto gain changed to %d\n", value);
camera->setGainAuto(value);
}
}
}
void Viewer::handleExposure(void) {
if (! camera->exposureAvailable) {
ImGui::Text("Exposure time control is not supported");
return;
}
// manual exposure time control
ImGui::AlignTextToFramePadding();
ImGui::SetNextItemWidth(200);
if (camera->exposureAutoAvailable && camera->exposureAuto) {
ImGui::PushItemFlag(ImGuiItemFlags_Disabled, true);
ImGui::PushStyleVar(ImGuiStyleVar_Alpha, ImGui::GetStyle().Alpha * 0.5f);
}
float value = camera->exposure.value;
if (ImGui::SliderFloat("Exposure", &value, camera->exposure.min, camera->exposure.max, "%.3f ms", ImGuiSliderFlags_AlwaysClamp)) {
D("exposure changed to %f\n", value);
camera->setExposure(value);
}
ImGui::SameLine();
ImGui::Text("(%.3f - %.3f, +/- %.3f)", camera->exposure.min, camera->exposure.max, camera->exposure.step);
if (camera->exposureAutoAvailable && camera->exposureAuto) {
ImGui::PopItemFlag();
ImGui::PopStyleVar();
}
// auto exposure time control
if (camera->exposureAutoAvailable) {
ImGui::SameLine();
bool value = camera->exposureAuto;
if (ImGui::Checkbox("Exposure auto", &value)) {
D("auto exposure changed to %d\n", value);
camera->setExposureAuto(value);
}
}
}