implement xflip in the UI

This commit is contained in:
Arisotura
2022-09-26 21:13:58 +02:00
parent 7c9e37b156
commit 308ea6289a
5 changed files with 54 additions and 22 deletions

View File

@ -22,7 +22,7 @@
CameraFrameDumper::CameraFrameDumper(QObject* parent) : QAbstractVideoSurface(parent) CameraFrameDumper::CameraFrameDumper(QObject* parent) : QAbstractVideoSurface(parent)
{ {
camList.append((CameraManager*)parent); cam = (CameraManager*)parent;
} }
bool CameraFrameDumper::present(const QVideoFrame& _frame) bool CameraFrameDumper::present(const QVideoFrame& _frame)
@ -33,8 +33,7 @@ bool CameraFrameDumper::present(const QVideoFrame& _frame)
if (!frame.isReadable()) if (!frame.isReadable())
return false; return false;
for (CameraManager* cam : camList) cam->feedFrame((u32*)frame.bits(), frame.width(), frame.height(), frame.pixelFormat() == QVideoFrame::Format_YUYV);
cam->feedFrame((u32*)frame.bits(), frame.width(), frame.height(), frame.pixelFormat() == QVideoFrame::Format_YUYV);
frame.unmap(); frame.unmap();
@ -71,6 +70,7 @@ CameraManager::CameraManager(int num, int width, int height, bool yuv) : QObject
frameBuffer = new u32[fbsize]; frameBuffer = new u32[fbsize];
inputType = -1; inputType = -1;
xFlip = false;
init(); init();
} }
@ -120,13 +120,14 @@ void CameraManager::init()
if (frameFormatYUV) if (frameFormatYUV)
{ {
copyFrame_RGBtoYUV((u32*)img.bits(), img.width(), img.height(), copyFrame_RGBtoYUV((u32*)img.bits(), img.width(), img.height(),
frameBuffer, frameWidth, frameHeight); frameBuffer, frameWidth, frameHeight,
false);
} }
else else
{ {
copyFrame_Straight((u32*)img.bits(), img.width(), img.height(), copyFrame_Straight((u32*)img.bits(), img.width(), img.height(),
frameBuffer, frameWidth, frameHeight, frameBuffer, frameWidth, frameHeight,
false); false, false);
} }
} }
} }
@ -205,11 +206,19 @@ void CameraManager::camStop()
camDevice->stop(); camDevice->stop();
} }
void CameraManager::setXFlip(bool flip)
{
xFlip = flip;
}
void CameraManager::captureFrame(u32* frame, int width, int height, bool yuv) void CameraManager::captureFrame(u32* frame, int width, int height, bool yuv)
{ {
frameMutex.lock(); frameMutex.lock();
if (width == frameWidth && height == frameHeight && yuv == frameFormatYUV) if ((width == frameWidth) &&
(height == frameHeight) &&
(yuv == frameFormatYUV) &&
(!xFlip))
{ {
int len = width * height; int len = width * height;
if (yuv) len /= 2; if (yuv) len /= 2;
@ -221,17 +230,19 @@ void CameraManager::captureFrame(u32* frame, int width, int height, bool yuv)
{ {
copyFrame_Straight(frameBuffer, frameWidth, frameHeight, copyFrame_Straight(frameBuffer, frameWidth, frameHeight,
frame, width, height, frame, width, height,
yuv); xFlip, yuv);
} }
else if (yuv) else if (yuv)
{ {
copyFrame_RGBtoYUV(frameBuffer, frameWidth, frameHeight, copyFrame_RGBtoYUV(frameBuffer, frameWidth, frameHeight,
frame, width, height); frame, width, height,
xFlip);
} }
else else
{ {
copyFrame_YUVtoRGB(frameBuffer, frameWidth, frameHeight, copyFrame_YUVtoRGB(frameBuffer, frameWidth, frameHeight,
frame, width, height); frame, width, height,
xFlip);
} }
} }
@ -254,24 +265,26 @@ void CameraManager::feedFrame(u32* frame, int width, int height, bool yuv)
{ {
copyFrame_Straight(frame, width, height, copyFrame_Straight(frame, width, height,
frameBuffer, frameWidth, frameHeight, frameBuffer, frameWidth, frameHeight,
yuv); false, yuv);
} }
else if (yuv) else if (yuv)
{ {
copyFrame_RGBtoYUV(frame, width, height, copyFrame_RGBtoYUV(frame, width, height,
frameBuffer, frameWidth, frameHeight); frameBuffer, frameWidth, frameHeight,
false);
} }
else else
{ {
copyFrame_YUVtoRGB(frame, width, height, copyFrame_YUVtoRGB(frame, width, height,
frameBuffer, frameWidth, frameHeight); frameBuffer, frameWidth, frameHeight,
false);
} }
} }
frameMutex.unlock(); frameMutex.unlock();
} }
void CameraManager::copyFrame_Straight(u32* src, int swidth, int sheight, u32* dst, int dwidth, int dheight, bool yuv) void CameraManager::copyFrame_Straight(u32* src, int swidth, int sheight, u32* dst, int dwidth, int dheight, bool xflip, bool yuv)
{ {
if (yuv) if (yuv)
{ {
@ -286,13 +299,14 @@ void CameraManager::copyFrame_Straight(u32* src, int swidth, int sheight, u32* d
for (int dx = 0; dx < dwidth; dx++) for (int dx = 0; dx < dwidth; dx++)
{ {
int sx = (dx * swidth) / dwidth; int sx = (dx * swidth) / dwidth;
if (xflip) sx = swidth-1 - sx;
dst[(dy * dwidth) + dx] = src[(sy * swidth) + sx]; dst[(dy * dwidth) + dx] = src[(sy * swidth) + sx];
} }
} }
} }
void CameraManager::copyFrame_RGBtoYUV(u32* src, int swidth, int sheight, u32* dst, int dwidth, int dheight) void CameraManager::copyFrame_RGBtoYUV(u32* src, int swidth, int sheight, u32* dst, int dwidth, int dheight, bool xflip)
{ {
for (int dy = 0; dy < dheight; dy++) for (int dy = 0; dy < dheight; dy++)
{ {
@ -303,9 +317,13 @@ void CameraManager::copyFrame_RGBtoYUV(u32* src, int swidth, int sheight, u32* d
int sx; int sx;
sx = (dx * swidth) / dwidth; sx = (dx * swidth) / dwidth;
if (xflip) sx = swidth-1 - sx;
u32 pixel1 = src[sy*swidth + sx]; u32 pixel1 = src[sy*swidth + sx];
sx = ((dx+1) * swidth) / dwidth; sx = ((dx+1) * swidth) / dwidth;
if (xflip) sx = swidth-1 - sx;
u32 pixel2 = src[sy*swidth + sx]; u32 pixel2 = src[sy*swidth + sx];
int r1 = (pixel1 >> 16) & 0xFF; int r1 = (pixel1 >> 16) & 0xFF;
@ -339,7 +357,7 @@ void CameraManager::copyFrame_RGBtoYUV(u32* src, int swidth, int sheight, u32* d
} }
} }
void CameraManager::copyFrame_YUVtoRGB(u32* src, int swidth, int sheight, u32* dst, int dwidth, int dheight) void CameraManager::copyFrame_YUVtoRGB(u32* src, int swidth, int sheight, u32* dst, int dwidth, int dheight, bool xflip)
{ {
for (int dy = 0; dy < dheight; dy++) for (int dy = 0; dy < dheight; dy++)
{ {
@ -348,6 +366,8 @@ void CameraManager::copyFrame_YUVtoRGB(u32* src, int swidth, int sheight, u32* d
for (int dx = 0; dx < dwidth; dx+=2) for (int dx = 0; dx < dwidth; dx+=2)
{ {
int sx = (dx * swidth) / dwidth; int sx = (dx * swidth) / dwidth;
if (xflip) sx = swidth-1 - sx;
u32 val = src[(sy*swidth + sx) / 2]; u32 val = src[(sy*swidth + sx) / 2];
int y1 = val & 0xFF; int y1 = val & 0xFF;

View File

@ -40,7 +40,7 @@ public:
QList<QVideoFrame::PixelFormat> supportedPixelFormats(QAbstractVideoBuffer::HandleType type = QAbstractVideoBuffer::NoHandle) const override; QList<QVideoFrame::PixelFormat> supportedPixelFormats(QAbstractVideoBuffer::HandleType type = QAbstractVideoBuffer::NoHandle) const override;
private: private:
QList<CameraManager*> camList; CameraManager* cam;
}; };
class CameraManager : public QObject class CameraManager : public QObject
@ -58,6 +58,8 @@ public:
void stop(); void stop();
bool isStarted(); bool isStarted();
void setXFlip(bool flip);
void captureFrame(u32* frame, int width, int height, bool yuv); void captureFrame(u32* frame, int width, int height, bool yuv);
void feedFrame(u32* frame, int width, int height, bool yuv); void feedFrame(u32* frame, int width, int height, bool yuv);
@ -87,9 +89,11 @@ private:
u32* frameBuffer; u32* frameBuffer;
QMutex frameMutex; QMutex frameMutex;
void copyFrame_Straight(u32* src, int swidth, int sheight, u32* dst, int dwidth, int dheight, bool yuv); bool xFlip;
void copyFrame_RGBtoYUV(u32* src, int swidth, int sheight, u32* dst, int dwidth, int dheight);
void copyFrame_YUVtoRGB(u32* src, int swidth, int sheight, u32* dst, int dwidth, int dheight); void copyFrame_Straight(u32* src, int swidth, int sheight, u32* dst, int dwidth, int dheight, bool xflip, bool yuv);
void copyFrame_RGBtoYUV(u32* src, int swidth, int sheight, u32* dst, int dwidth, int dheight, bool xflip);
void copyFrame_YUVtoRGB(u32* src, int swidth, int sheight, u32* dst, int dwidth, int dheight, bool xflip);
}; };
#endif // CAMERAMANAGER_H #endif // CAMERAMANAGER_H

View File

@ -270,3 +270,11 @@ void CameraSettingsDialog::populateCamControls(int id)
ui->chkFlipPicture->setChecked(cfg.XFlip); ui->chkFlipPicture->setChecked(cfg.XFlip);
} }
void CameraSettingsDialog::on_chkFlipPicture_clicked()
{
if (!currentCfg) return;
currentCfg->XFlip = ui->chkFlipPicture->isChecked();
if (currentCam) currentCam->setXFlip(currentCfg->XFlip);
}

View File

@ -79,9 +79,6 @@ public:
currentDlg = nullptr; currentDlg = nullptr;
} }
signals:
//
private slots: private slots:
void on_CameraSettingsDialog_accepted(); void on_CameraSettingsDialog_accepted();
void on_CameraSettingsDialog_rejected(); void on_CameraSettingsDialog_rejected();
@ -91,6 +88,7 @@ private slots:
void on_txtSrcImagePath_textChanged(); void on_txtSrcImagePath_textChanged();
void on_btnSrcImageBrowse_clicked(); void on_btnSrcImageBrowse_clicked();
void on_cbPhysicalCamera_currentIndexChanged(int id); void on_cbPhysicalCamera_currentIndexChanged(int id);
void on_chkFlipPicture_clicked();
private: private:
Ui::CameraSettingsDialog* ui; Ui::CameraSettingsDialog* ui;

View File

@ -3233,6 +3233,8 @@ int main(int argc, char** argv)
camStarted[1] = false; camStarted[1] = false;
camManager[0] = new CameraManager(0, 640, 480, true); camManager[0] = new CameraManager(0, 640, 480, true);
camManager[1] = new CameraManager(1, 640, 480, true); camManager[1] = new CameraManager(1, 640, 480, true);
camManager[0]->setXFlip(Config::Camera[0].XFlip);
camManager[1]->setXFlip(Config::Camera[1].XFlip);
ROMManager::EnableCheats(Config::EnableCheats != 0); ROMManager::EnableCheats(Config::EnableCheats != 0);