1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00
qTox/widget/camera.cpp

243 lines
5.4 KiB
C++
Raw Normal View History

2014-07-07 00:19:45 +08:00
/*
Copyright (C) 2014 by Project Tox <https://tox.im>
This file is part of qTox, a Qt-based graphical interface for Tox.
This program is libre software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the COPYING file for more details.
*/
#include "camera.h"
2014-09-11 21:44:34 +08:00
#include "widget.h"
2014-10-08 22:37:42 +08:00
#include "cameraworker.h"
#include <QDebug>
2014-10-08 20:25:32 +08:00
#include <QThread>
2014-10-08 20:25:32 +08:00
Camera* Camera::instance = nullptr;
2014-08-29 19:40:37 +08:00
Camera::Camera()
2014-10-08 20:25:32 +08:00
: refcount(0)
, workerThread(nullptr)
, worker(nullptr)
{
2014-10-08 22:37:42 +08:00
worker = new CameraWorker(0);
2014-10-08 20:25:32 +08:00
workerThread = new QThread();
worker->moveToThread(workerThread);
2014-10-08 22:37:42 +08:00
connect(workerThread, &QThread::started, worker, &CameraWorker::onStart);
connect(workerThread, &QThread::finished, worker, &CameraWorker::deleteLater);
connect(workerThread, &QThread::deleteLater, worker, &CameraWorker::deleteLater);
connect(worker, &CameraWorker::started, this, &Camera::onWorkerStarted);
connect(worker, &CameraWorker::newFrameAvailable, this, &Camera::onNewFrameAvailable);
connect(worker, &CameraWorker::resProbingFinished, this, &Camera::onResProbingFinished);
2014-10-08 20:25:32 +08:00
workerThread->start();
}
2014-10-08 21:48:02 +08:00
void Camera::onWorkerStarted()
{
worker->probeResolutions();
}
2014-10-08 20:25:32 +08:00
Camera::~Camera()
{
workerThread->exit();
workerThread->deleteLater();
}
2014-10-07 22:59:21 +08:00
void Camera::subscribe()
{
if (refcount <= 0)
2014-10-08 20:25:32 +08:00
worker->resume();
2014-10-08 21:48:02 +08:00
refcount++;
}
2014-10-07 22:59:21 +08:00
void Camera::unsubscribe()
{
refcount--;
if (refcount <= 0)
{
2014-10-08 20:25:32 +08:00
worker->suspend();
refcount = 0;
}
}
2014-10-08 20:25:32 +08:00
cv::Mat Camera::getLastFrame()
2014-08-29 19:40:37 +08:00
{
2014-10-08 21:48:02 +08:00
return currFrame;
}
2014-06-30 20:49:42 +08:00
vpx_image Camera::getLastVPXImage()
{
2014-10-08 20:25:32 +08:00
cv::Mat3b frame = getLastFrame();
2014-07-01 05:48:12 +08:00
vpx_image img;
2014-08-29 20:20:32 +08:00
int w = frame.size().width, h = frame.size().height;
2014-06-30 20:49:42 +08:00
vpx_img_alloc(&img, VPX_IMG_FMT_I420, w, h, 1); // I420 == YUV420P, same as YV12 with U and V switched
2014-08-29 20:20:32 +08:00
size_t i=0, j=0;
for( int line = 0; line < h; ++line )
{
const cv::Vec3b *srcrow = frame[line];
if( !(line % 2) )
{
2014-08-29 20:20:32 +08:00
for( int x = 0; x < w; x += 2 )
{
2014-08-29 20:20:32 +08:00
uint8_t r = srcrow[x][2];
uint8_t g = srcrow[x][1];
uint8_t b = srcrow[x][0];
img.planes[VPX_PLANE_Y][i] = ((66*r + 129*g + 25*b) >> 8) + 16;
2014-08-29 20:33:19 +08:00
img.planes[VPX_PLANE_V][j] = ((-38*r + -74*g + 112*b) >> 8) + 128;
img.planes[VPX_PLANE_U][j] = ((112*r + -94*g + -18*b) >> 8) + 128;
2014-08-29 20:20:32 +08:00
i++;
j++;
r = srcrow[x+1][2];
g = srcrow[x+1][1];
b = srcrow[x+1][0];
img.planes[VPX_PLANE_Y][i] = ((66*r + 129*g + 25*b) >> 8) + 16;
i++;
}
2014-08-29 20:20:32 +08:00
}
else
{
for( int x = 0; x < w; x += 1 )
{
2014-08-29 20:20:32 +08:00
uint8_t r = srcrow[x][2];
uint8_t g = srcrow[x][1];
uint8_t b = srcrow[x][0];
2014-08-29 20:20:32 +08:00
img.planes[VPX_PLANE_Y][i] = ((66*r + 129*g + 25*b) >> 8) + 16;
i++;
}
}
2014-06-30 20:49:42 +08:00
}
return img;
}
2014-09-11 21:44:34 +08:00
2014-10-08 21:48:02 +08:00
QList<QSize> Camera::getSupportedResolutions()
{
2014-10-08 21:48:02 +08:00
return resolutions;
}
2014-10-08 21:48:02 +08:00
QSize Camera::getBestVideoMode()
{
int bestScore = 0;
2014-10-08 21:48:02 +08:00
QSize bestRes;
2014-10-08 21:48:02 +08:00
for (QSize res : getSupportedResolutions())
{
2014-10-08 21:48:02 +08:00
int score = res.width() * res.height();
if (score > bestScore)
{
bestScore = score;
2014-10-08 21:48:02 +08:00
bestRes = res;
}
}
2014-10-08 21:48:02 +08:00
return bestRes;
}
2014-10-08 21:48:02 +08:00
void Camera::setResolution(QSize res)
{
2014-10-08 21:48:02 +08:00
worker->setProp(CV_CAP_PROP_FRAME_WIDTH, res.width());
worker->setProp(CV_CAP_PROP_FRAME_HEIGHT, res.height());
}
2014-10-08 21:48:02 +08:00
QSize Camera::getResolution()
{
2014-10-08 21:48:02 +08:00
return QSize(worker->getProp(CV_CAP_PROP_FRAME_WIDTH), worker->getProp(CV_CAP_PROP_FRAME_HEIGHT));
}
2014-10-07 22:59:21 +08:00
void Camera::setProp(Camera::Prop prop, double val)
{
switch (prop)
{
case BRIGHTNESS:
2014-10-08 21:48:02 +08:00
worker->setProp(CV_CAP_PROP_BRIGHTNESS, val);
2014-10-07 22:59:21 +08:00
break;
case SATURATION:
2014-10-08 21:48:02 +08:00
worker->setProp(CV_CAP_PROP_SATURATION, val);
2014-10-07 22:59:21 +08:00
break;
case CONTRAST:
2014-10-08 21:48:02 +08:00
worker->setProp(CV_CAP_PROP_CONTRAST, val);
2014-10-07 22:59:21 +08:00
break;
case HUE:
2014-10-08 21:48:02 +08:00
worker->setProp(CV_CAP_PROP_HUE, val);
2014-10-07 22:59:21 +08:00
break;
}
}
double Camera::getProp(Camera::Prop prop)
{
switch (prop)
{
case BRIGHTNESS:
2014-10-08 21:48:02 +08:00
return worker->getProp(CV_CAP_PROP_BRIGHTNESS);
2014-10-07 22:59:21 +08:00
case SATURATION:
2014-10-08 21:48:02 +08:00
return worker->getProp(CV_CAP_PROP_SATURATION);
2014-10-07 22:59:21 +08:00
case CONTRAST:
2014-10-08 21:48:02 +08:00
return worker->getProp(CV_CAP_PROP_CONTRAST);
2014-10-07 22:59:21 +08:00
case HUE:
2014-10-08 21:48:02 +08:00
return worker->getProp(CV_CAP_PROP_HUE);
2014-10-07 22:59:21 +08:00
}
return 0.0;
}
2014-10-08 20:25:32 +08:00
void Camera::onNewFrameAvailable()
{
emit frameAvailable();
}
2014-10-08 21:48:02 +08:00
void Camera::onResProbingFinished(QList<QSize> res)
{
resolutions = res;
}
2014-10-07 22:59:21 +08:00
void *Camera::getData()
{
return currFrame.data;
}
int Camera::getDataSize()
{
return currFrame.total() * currFrame.channels();
}
void Camera::lock()
{
mutex.lock();
2014-10-08 20:25:32 +08:00
if (worker->hasFrame())
2014-10-08 22:37:42 +08:00
currFrame = worker->dequeueFrame();
2014-10-07 22:59:21 +08:00
}
void Camera::unlock()
{
mutex.unlock();
}
QSize Camera::resolution()
{
2014-10-08 20:25:32 +08:00
return QSize(currFrame.cols, currFrame.rows);
}
Camera* Camera::getInstance()
{
if (!instance)
instance = new Camera();
return instance;
2014-10-07 22:59:21 +08:00
}