-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
509 lines (452 loc) · 17.2 KB
/
mainwindow.cpp
File metadata and controls
509 lines (452 loc) · 17.2 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
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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
#include "mainwindow.h"
// YUV to RGB conversion helpers
static inline int clamp(int value) {
if (value < 0) return 0;
if (value > 255) return 255;
return value;
}
// YUV420P (I420/YU12): Y plane, then U plane, then V plane
void MainWindow::i420_to_rgb24(const uchar *yuv, uchar *rgb, int width, int height, int linesize)
{
const uchar *y_plane = yuv;
const uchar *u_plane = yuv + linesize * height;
const uchar *v_plane = u_plane + (linesize / 2) * (height / 2);
int uv_linesize = linesize / 2;
for (int i = 0; i < height; ++i) {
for (int j = 0; j < width; ++j) {
int y = y_plane[i * linesize + j];
int u = u_plane[(i / 2) * uv_linesize + (j / 2)];
int v = v_plane[(i / 2) * uv_linesize + (j / 2)];
int c = y - 16;
int d = u - 128;
int e = v - 128;
int r = clamp((298 * c + 409 * e + 128) >> 8);
int g = clamp((298 * c - 100 * d - 208 * e + 128) >> 8);
int b = clamp((298 * c + 516 * d + 128) >> 8);
int rgb_index = (i * width + j) * 3;
rgb[rgb_index] = r;
rgb[rgb_index + 1] = g;
rgb[rgb_index + 2] = b;
}
}
}
// YUV420P (YV12): Y plane, then V plane, then U plane
void MainWindow::yv12_to_rgb24(const uchar *yuv, uchar *rgb, int width, int height, int linesize)
{
const uchar *y_plane = yuv;
const uchar *v_plane = yuv + linesize * height;
const uchar *u_plane = v_plane + (linesize / 2) * (height / 2);
int uv_linesize = linesize / 2;
for (int i = 0; i < height; ++i) {
for (int j = 0; j < width; ++j) {
int y = y_plane[i * linesize + j];
int u = u_plane[(i / 2) * uv_linesize + (j / 2)];
int v = v_plane[(i / 2) * uv_linesize + (j / 2)];
int c = y - 16;
int d = u - 128;
int e = v - 128;
int r = clamp((298 * c + 409 * e + 128) >> 8);
int g = clamp((298 * c - 100 * d - 208 * e + 128) >> 8);
int b = clamp((298 * c + 516 * d + 128) >> 8);
int rgb_index = (i * width + j) * 3;
rgb[rgb_index] = r;
rgb[rgb_index + 1] = g;
rgb[rgb_index + 2] = b;
}
}
}
// YUV420SP (NV12): Y plane, then interleaved UV plane
void MainWindow::nv12_to_rgb24(const uchar *yuv, uchar *rgb, int width, int height, int linesize)
{
const uchar *y_plane = yuv;
const uchar *uv_plane = yuv + linesize * height;
for (int i = 0; i < height; ++i) {
for (int j = 0; j < width; ++j) {
int y = y_plane[i * linesize + j];
int u = uv_plane[(i / 2) * linesize + (j / 2) * 2];
int v = uv_plane[(i / 2) * linesize + (j / 2) * 2 + 1];
int c = y - 16;
int d = u - 128;
int e = v - 128;
int r = clamp((298 * c + 409 * e + 128) >> 8);
int g = clamp((298 * c - 100 * d - 208 * e + 128) >> 8);
int b = clamp((298 * c + 516 * d + 128) >> 8);
int rgb_index = (i * width + j) * 3;
rgb[rgb_index] = r;
rgb[rgb_index + 1] = g;
rgb[rgb_index + 2] = b;
}
}
}
// YUV420SP (NV21): Y plane, then interleaved VU plane
void MainWindow::nv21_to_rgb24(const uchar *yuv, uchar *rgb, int width, int height, int linesize)
{
const uchar *y_plane = yuv;
const uchar *vu_plane = yuv + linesize * height;
for (int i = 0; i < height; ++i) {
for (int j = 0; j < width; ++j) {
int y = y_plane[i * linesize + j];
int v = vu_plane[(i / 2) * linesize + (j / 2) * 2];
int u = vu_plane[(i / 2) * linesize + (j / 2) * 2 + 1];
int c = y - 16;
int d = u - 128;
int e = v - 128;
int r = clamp((298 * c + 409 * e + 128) >> 8);
int g = clamp((298 * c - 100 * d - 208 * e + 128) >> 8);
int b = clamp((298 * c + 516 * d + 128) >> 8);
int rgb_index = (i * width + j) * 3;
rgb[rgb_index] = r;
rgb[rgb_index + 1] = g;
rgb[rgb_index + 2] = b;
}
}
}
// YUV422P (I422)
void MainWindow::i422_to_rgb24(const uchar *yuv, uchar *rgb, int width, int height, int linesize)
{
const uchar *y_plane = yuv;
const uchar *u_plane = yuv + linesize * height;
const uchar *v_plane = u_plane + (linesize / 2) * height;
int uv_linesize = linesize / 2;
for (int i = 0; i < height; ++i) {
for (int j = 0; j < width; ++j) {
int y = y_plane[i * linesize + j];
int u = u_plane[i * uv_linesize + (j / 2)];
int v = v_plane[i * uv_linesize + (j / 2)];
int c = y - 16;
int d = u - 128;
int e = v - 128;
int r = clamp((298 * c + 409 * e + 128) >> 8);
int g = clamp((298 * c - 100 * d - 208 * e + 128) >> 8);
int b = clamp((298 * c + 516 * d + 128) >> 8);
int rgb_index = (i * width + j) * 3;
rgb[rgb_index] = r;
rgb[rgb_index + 1] = g;
rgb[rgb_index + 2] = b;
}
}
}
void MainWindow::bgra_to_rgba(const unsigned int *bgra, unsigned int *rgba, int width, int height, int linesize)
{
for (int i = 0; i < height; ++i) {
for (int j = 0; j < width; ++j) {
int index = i * linesize + j;
unsigned char a = (bgra[index] & 0xff000000) >> 24;
unsigned char r = (bgra[index] & 0x00ff0000) >> 16;
unsigned char g = (bgra[index] & 0x0000ff00) >> 8;
unsigned char b = (bgra[index] & 0x000000ff);
unsigned int result =a << 24 | b << 16 | g << 8 | r;
rgba[index] = result;
}
}
}
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), mappedData(nullptr), mappedDataSize(0), currentFrame(0), totalFrames(0), image(nullptr)
{
// Central widget for image display
imageLabel = new QLabel(this);
imageLabel->setAlignment(Qt::AlignCenter);
imageLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
imageLabel->setScaledContents(true);
setCentralWidget(imageLabel);
createActions();
createMenus();
createSettingsPanel();
// Debouncing timer for UI updates
updateTimer = new QTimer(this);
updateTimer->setSingleShot(true);
updateTimer->setInterval(250); // 250ms delay
connect(updateTimer, &QTimer::timeout, this, &MainWindow::updateImage);
// Playback timer
playbackTimer = new QTimer(this);
connect(playbackTimer, &QTimer::timeout, this, &MainWindow::playNextFrame);
setWindowTitle(tr("Raw Pixel Viewer"));
resize(800, 600);
}
MainWindow::~MainWindow()
{
closeCurrentImageFile();
delete image;
}
void MainWindow::closeCurrentImageFile()
{
playbackTimer->stop();
if (mappedData) {
currentImageFile.unmap(mappedData);
mappedData = nullptr;
mappedDataSize = 0;
}
if (currentImageFile.isOpen()) {
currentImageFile.close();
}
}
void MainWindow::openFile()
{
QString filePath = QFileDialog::getOpenFileName(this, tr("Open Image File"), "", tr("All Files (*.*);;YUV Files (*.yuv);;RGB Files (*.rgb)"));
if (filePath.isEmpty()) {
return;
}
closeCurrentImageFile(); // Close any previously opened file
currentImageFile.setFileName(filePath);
if (!currentImageFile.open(QIODevice::ReadOnly)) {
QMessageBox::warning(this, tr("Error"), tr("Could not open file."));
return;
}
mappedDataSize = currentImageFile.size();
if (mappedDataSize == 0) {
QMessageBox::information(this, tr("Info"), tr("File is empty."));
closeCurrentImageFile();
return;
}
mappedData = currentImageFile.map(0, mappedDataSize);
if (!mappedData) {
QMessageBox::warning(this, tr("Error"), tr("Could not map file to memory."));
closeCurrentImageFile();
return;
}
currentFrame = 0;
updateImage();
}
void MainWindow::updateImage()
{
if (!mappedData) {
return;
}
int width = widthSpinBox->value();
int height = heightSpinBox->value();
QString format = pixelFormatComboBox->currentText();
int linesize = linesizeSpinBox->value();
int frameSize = 0;
if (format.startsWith("YUV420")) {
frameSize = linesize * height * 3 / 2;
} else if (format.startsWith("YUV422")) {
frameSize = linesize * height * 2;
} else if (format == "RGB888") {
frameSize = linesize * height * 3;
} else if (format == "RGBA8888") {
frameSize = linesize * height * 4; // RGBX is 4 bytes per pixel, linesize is in bytes
} else if (format == "BGRA8888") {
frameSize = linesize * height * 4;
}
if (frameSize <= 0) {
QMessageBox::warning(this, tr("Error"), tr("Invalid format or parameters."));
return;
}
totalFrames = mappedDataSize / frameSize;
if (totalFrames == 0) {
imageLabel->clear();
imageLabel->setText(tr("No valid frames found. Check parameters."));
return;
}
if (currentFrame >= totalFrames) {
currentFrame = totalFrames - 1;
}
int frameOffset = currentFrame * frameSize;
const uchar *frameData = mappedData + frameOffset;
QImage *newImage = nullptr;
if (format == "YUV420P (I420/YU12)") {
newImage = new QImage(width, height, QImage::Format_RGB888);
i420_to_rgb24(frameData, newImage->bits(), width, height, linesize);
} else if (format == "YUV420P (YV12)") {
newImage = new QImage(width, height, QImage::Format_RGB888);
yv12_to_rgb24(frameData, newImage->bits(), width, height, linesize);
} else if (format == "YUV420SP (NV12)") {
newImage = new QImage(width, height, QImage::Format_RGB888);
nv12_to_rgb24(frameData, newImage->bits(), width, height, linesize);
} else if (format == "YUV420SP (NV21)") {
newImage = new QImage(width, height, QImage::Format_RGB888);
nv21_to_rgb24(frameData, newImage->bits(), width, height, linesize);
} else if (format == "YUV422P (I422)") {
newImage = new QImage(width, height, QImage::Format_RGB888);
i422_to_rgb24(frameData, newImage->bits(), width, height, linesize);
} else if (format == "RGB888") {
newImage = new QImage(width, height, QImage::Format_RGB888);
int copy_size = width*height*3;
if((mappedDataSize - frameOffset) < width*height*3)
copy_size = mappedDataSize - frameOffset;
memcpy(newImage->bits(), frameData, copy_size);
} else if (format == "RGBA8888") {
newImage = new QImage(width, height, QImage::Format_RGBA8888);
int copy_size = width*height*4;
if((mappedDataSize - frameOffset) < width*height*4)
copy_size = mappedDataSize - frameOffset;
memcpy(newImage->bits(), frameData, copy_size);
} else if (format == "BGRA8888") {
newImage = new QImage(width, height, QImage::Format_RGBA8888);
bgra_to_rgba((const unsigned int *)frameData, (unsigned int *)newImage->bits(), width, height, linesize);
}
if (newImage && !newImage->isNull()) {
delete image; // Delete the old image
image = newImage; // Assign the new one
imageLabel->setPixmap(QPixmap::fromImage(*image));
} else {
delete newImage; // Clean up if image creation failed
}
updateFrameNavigation();
}
void MainWindow::requestUpdate()
{
updateTimer->start();
}
void MainWindow::togglePlayback()
{
if (playbackTimer->isActive()) {
playbackTimer->stop();
playButton->setText(tr("Play"));
} else {
if (totalFrames > 1) {
int fps = fpsSpinBox->value();
if (fps > 0) {
playbackTimer->start(1000 / fps);
playButton->setText(tr("Pause"));
}
}
}
updateFrameNavigation();
}
void MainWindow::playNextFrame()
{
if (currentFrame < totalFrames - 1) {
currentFrame++;
updateImage();
} else {
// Stop at the last frame
playbackTimer->stop();
playButton->setText(tr("Play"));
updateFrameNavigation();
}
}
void MainWindow::jumpToFrame()
{
if (totalFrames > 0) {
int frame = frameSpinBox->value() - 1; // Convert 1-based to 0-based index
if (frame >= 0 && frame < totalFrames) {
currentFrame = frame;
updateImage();
}
}
}
void MainWindow::prevFrame()
{
if (currentFrame > 0) {
currentFrame--;
updateImage();
}
}
void MainWindow::nextFrame()
{
if (currentFrame < totalFrames - 1) {
currentFrame++;
updateImage();
}
}
void MainWindow::createActions()
{
openAction = new QAction(tr("&Open..."), this);
openAction->setShortcut(QKeySequence::Open);
connect(openAction, &QAction::triggered, this, &MainWindow::openFile);
}
void MainWindow::createMenus()
{
fileMenu = menuBar()->addMenu(tr("&File"));
fileMenu->addAction(openAction);
}
void MainWindow::createSettingsPanel()
{
QDockWidget *dockWidget = new QDockWidget(tr("Settings"), this);
addDockWidget(Qt::RightDockWidgetArea, dockWidget);
QWidget *settingsWidget = new QWidget;
QFormLayout *formLayout = new QFormLayout(settingsWidget);
// Resolution
widthSpinBox = new QSpinBox;
widthSpinBox->setRange(1, 8192);
widthSpinBox->setValue(1920);
heightSpinBox = new QSpinBox;
heightSpinBox->setRange(1, 8192);
heightSpinBox->setValue(1080);
QHBoxLayout *resolutionLayout = new QHBoxLayout;
resolutionLayout->addWidget(widthSpinBox);
resolutionLayout->addWidget(new QLabel(tr("x")));
resolutionLayout->addWidget(heightSpinBox);
formLayout->addRow(tr("Resolution:"), resolutionLayout);
// Pixel Format
pixelFormatComboBox = new QComboBox;
pixelFormatComboBox->addItem("YUV420P (I420/YU12)");
pixelFormatComboBox->addItem("YUV420P (YV12)");
pixelFormatComboBox->addItem("YUV420SP (NV12)");
pixelFormatComboBox->addItem("YUV420SP (NV21)");
pixelFormatComboBox->addItem("YUV422P (I422)");
pixelFormatComboBox->addItem("RGB888");
pixelFormatComboBox->addItem("RGBA8888");
pixelFormatComboBox->addItem("BGRA8888");
formLayout->addRow(tr("Pixel Format:"), pixelFormatComboBox);
// Linesize
linesizeSpinBox = new QSpinBox;
linesizeSpinBox->setRange(1, 16384);
linesizeSpinBox->setValue(1920);
formLayout->addRow(tr("Linesize:"), linesizeSpinBox);
// Frame Navigation
prevFrameButton = new QPushButton(tr("Previous Frame"));
nextFrameButton = new QPushButton(tr("Next Frame"));
currentFrameLabel = new QLabel(tr("Frame: 0 / 0"));
currentFrameLabel->setAlignment(Qt::AlignCenter);
QHBoxLayout *frameLayout = new QHBoxLayout;
frameLayout->addWidget(prevFrameButton);
frameLayout->addWidget(nextFrameButton);
formLayout->addRow(frameLayout);
formLayout->addRow(currentFrameLabel);
// Jump to Frame
frameSpinBox = new QSpinBox;
frameSpinBox->setRange(1, 1); // Will be updated dynamically
jumpButton = new QPushButton(tr("Jump"));
QHBoxLayout *jumpLayout = new QHBoxLayout;
jumpLayout->addWidget(new QLabel(tr("Go to Frame:")));
jumpLayout->addWidget(frameSpinBox);
jumpLayout->addWidget(jumpButton);
formLayout->addRow(jumpLayout);
// Playback
fpsSpinBox = new QSpinBox;
fpsSpinBox->setRange(1, 120);
fpsSpinBox->setValue(30);
playButton = new QPushButton(tr("Play"));
QHBoxLayout *playbackLayout = new QHBoxLayout;
playbackLayout->addWidget(new QLabel(tr("FPS:")));
playbackLayout->addWidget(fpsSpinBox);
playbackLayout->addWidget(playButton);
formLayout->addRow(playbackLayout);
dockWidget->setWidget(settingsWidget);
// Connect signals
connect(widthSpinBox, QOverload<int>::of(&QSpinBox::valueChanged), this, &MainWindow::requestUpdate);
connect(heightSpinBox, QOverload<int>::of(&QSpinBox::valueChanged), this, &MainWindow::requestUpdate);
connect(pixelFormatComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &MainWindow::requestUpdate);
connect(linesizeSpinBox, QOverload<int>::of(&QSpinBox::valueChanged), this, &MainWindow::requestUpdate);
connect(prevFrameButton, &QPushButton::clicked, this, &MainWindow::prevFrame);
connect(nextFrameButton, &QPushButton::clicked, this, &MainWindow::nextFrame);
connect(jumpButton, &QPushButton::clicked, this, &MainWindow::jumpToFrame);
connect(playButton, &QPushButton::clicked, this, &MainWindow::togglePlayback);
}
void MainWindow::updateFrameNavigation()
{
bool isPlaying = playbackTimer->isActive();
if (totalFrames > 0) {
currentFrameLabel->setText(QString("Frame: %1 / %2").arg(currentFrame + 1).arg(totalFrames));
frameSpinBox->setRange(1, totalFrames);
frameSpinBox->setValue(currentFrame + 1);
}
else {
currentFrameLabel->setText(tr("Frame: 0 / 0"));
frameSpinBox->setRange(1, 1);
frameSpinBox->setValue(1);
}
prevFrameButton->setEnabled(!isPlaying && currentFrame > 0);
nextFrameButton->setEnabled(!isPlaying && currentFrame < totalFrames - 1);
jumpButton->setEnabled(!isPlaying && totalFrames > 1);
frameSpinBox->setEnabled(!isPlaying && totalFrames > 1);
playButton->setEnabled(totalFrames > 1);
fpsSpinBox->setEnabled(!isPlaying);
if (isPlaying) {
playButton->setText(tr("Pause"));
} else {
playButton->setText(tr("Play"));
}
}