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

249 lines
5.3 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"
#include <QDebug>
2014-08-29 19:40:37 +08:00
using namespace cv;
Camera::Camera()
2014-08-29 20:42:38 +08:00
: refcount{0}
{
}
2014-10-07 22:59:21 +08:00
void Camera::subscribe()
{
if (refcount <= 0)
{
refcount = 1;
2014-08-29 20:42:38 +08:00
cam.open(0);
}
else
refcount++;
}
2014-10-07 22:59:21 +08:00
void Camera::unsubscribe()
{
refcount--;
if (refcount <= 0)
{
2014-10-07 22:59:21 +08:00
//cam.release();
refcount = 0;
}
}
2014-08-29 19:40:37 +08:00
Mat Camera::getLastFrame()
{
Mat frame;
2014-08-29 20:42:38 +08:00
cam >> frame;
2014-08-29 19:40:37 +08:00
Mat out;
if (!frame.empty())
cv::cvtColor(frame, out, CV_BGR2RGB);
return out;
}
2014-06-30 20:49:42 +08:00
vpx_image Camera::getLastVPXImage()
{
2014-08-29 20:20:32 +08:00
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
QList<Camera::VideoMode> Camera::getVideoModes()
{
// probe resolutions
QList<QSize> resolutions = {
QSize( 160, 120), // QQVGA
QSize( 320, 240), // HVGA
QSize(1024, 768), // XGA
QSize( 432, 240), // WQVGA
QSize( 640, 360), // nHD
};
QList<VideoMode> modes;
for (QSize res : resolutions)
{
cam.set(CV_CAP_PROP_FRAME_WIDTH, res.width());
cam.set(CV_CAP_PROP_FRAME_HEIGHT, res.height());
double w = cam.get(CV_CAP_PROP_FRAME_WIDTH);
double h = cam.get(CV_CAP_PROP_FRAME_HEIGHT);
2014-10-07 22:59:21 +08:00
qDebug() << "PROBING:" << res << " got " << w << h;
if (w == res.width() && h == res.height())
{
modes.append({res, 60}); // assume 60fps for now
}
}
return modes;
}
Camera::VideoMode Camera::getBestVideoMode()
{
int bestScore = 0;
VideoMode bestMode;
for (VideoMode mode : getVideoModes())
{
int score = mode.res.width() * mode.res.height();
if (score > bestScore)
{
bestScore = score;
bestMode = mode;
}
}
return bestMode;
}
void Camera::setVideoMode(Camera::VideoMode mode)
{
if (cam.isOpened())
{
cam.set(CV_CAP_PROP_FRAME_WIDTH, mode.res.width());
cam.set(CV_CAP_PROP_FRAME_HEIGHT, mode.res.height());
2014-10-07 22:59:21 +08:00
//cam.set(CV_CAP_PROP_SATURATION, 0.5);
}
}
Camera::VideoMode Camera::getVideoMode()
{
return VideoMode{QSize(cam.get(CV_CAP_PROP_FRAME_WIDTH), cam.get(CV_CAP_PROP_FRAME_HEIGHT)), 60};
}
2014-10-07 22:59:21 +08:00
void Camera::setProp(Camera::Prop prop, double val)
{
switch (prop)
{
case BRIGHTNESS:
cam.set(CV_CAP_PROP_BRIGHTNESS, val);
break;
case SATURATION:
cam.set(CV_CAP_PROP_SATURATION, val);
break;
case CONTRAST:
cam.set(CV_CAP_PROP_CONTRAST, val);
break;
case HUE:
cam.set(CV_CAP_PROP_HUE, val);
break;
}
}
double Camera::getProp(Camera::Prop prop)
{
switch (prop)
{
case BRIGHTNESS:
return cam.get(CV_CAP_PROP_BRIGHTNESS);
case SATURATION:
return cam.get(CV_CAP_PROP_SATURATION);
case CONTRAST:
return cam.get(CV_CAP_PROP_CONTRAST);
case HUE:
return cam.get(CV_CAP_PROP_HUE);
}
return 0.0;
}
void *Camera::getData()
{
return currFrame.data;
}
int Camera::getDataSize()
{
return currFrame.total() * currFrame.channels();
}
void Camera::lock()
{
mutex.lock();
currFrame = getLastFrame();
//getLastFrame().copyTo(currFrame);
}
void Camera::unlock()
{
mutex.unlock();
}
QSize Camera::resolution()
{
return getVideoMode().res;
}
double Camera::fps()
{
return getVideoMode().fps;
}
2014-09-11 21:44:34 +08:00
Camera* Camera::getInstance()
{
return Widget::getInstance()->getCamera();
}