mirror of
https://github.com/hyperion-project/hyperion.ng.git
synced 2025-03-01 10:33:28 +00:00
- refactored framebuffer grabber to use ImageResampler
- added OsxGrabber for OSX - added binary for imx6 (cubox-i) and updated install script Former-commit-id: 2c55e292c842c67e54ce36bd91e4f6303b98687a
This commit is contained in:
@@ -13,3 +13,7 @@ endif (ENABLE_V4L2)
|
||||
if (ENABLE_X11)
|
||||
add_subdirectory(x11)
|
||||
endif()
|
||||
|
||||
if (ENABLE_OSX)
|
||||
add_subdirectory(osx)
|
||||
endif()
|
||||
|
@@ -5,10 +5,8 @@
|
||||
#include <linux/fb.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <math.h>
|
||||
|
||||
// STL includes
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
|
||||
// Local includes
|
||||
@@ -20,59 +18,48 @@ FramebufferFrameGrabber::FramebufferFrameGrabber(const std::string & device, con
|
||||
_fbDevice(device),
|
||||
_width(width),
|
||||
_height(height),
|
||||
_xScale(1),
|
||||
_yScale(1)
|
||||
_imgResampler(new ImageResampler())
|
||||
{
|
||||
int result;
|
||||
struct fb_var_screeninfo vinfo;
|
||||
|
||||
// Check if the framebuffer device can be opened and display the current resolution
|
||||
_fbfd = open(_fbDevice.c_str(), O_RDONLY);
|
||||
assert(_fbfd > 0);
|
||||
|
||||
// get variable screen information
|
||||
result = ioctl (_fbfd, FBIOGET_VSCREENINFO, &vinfo);
|
||||
assert(result == 0);
|
||||
|
||||
std::cout << "Framebuffer opened with resolution: " << vinfo.xres << "x" << vinfo.yres << "@" << vinfo.bits_per_pixel << "bit" << std::endl;
|
||||
|
||||
close(_fbfd);
|
||||
if (_fbfd == 0)
|
||||
{
|
||||
std::cerr << "Error openning " << _fbDevice << std::endl;
|
||||
}
|
||||
else
|
||||
{
|
||||
// get variable screen information
|
||||
result = ioctl (_fbfd, FBIOGET_VSCREENINFO, &vinfo);
|
||||
if (result != 0)
|
||||
{
|
||||
std::cerr << "Could not get screen information" << std::endl;
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout << "Framebuffer opened with resolution: " << vinfo.xres << "x" << vinfo.yres << "@" << vinfo.bits_per_pixel << "bit" << std::endl;
|
||||
}
|
||||
close(_fbfd);
|
||||
}
|
||||
}
|
||||
|
||||
FramebufferFrameGrabber::~FramebufferFrameGrabber()
|
||||
{
|
||||
delete _imgResampler;
|
||||
}
|
||||
|
||||
void FramebufferFrameGrabber::setVideoMode(const VideoMode videoMode)
|
||||
{
|
||||
switch (videoMode) {
|
||||
case VIDEO_3DSBS:
|
||||
_xScale = 2;
|
||||
_yScale = 1;
|
||||
break;
|
||||
case VIDEO_3DTAB:
|
||||
_xScale = 1;
|
||||
_yScale = 2;
|
||||
break;
|
||||
case VIDEO_2D:
|
||||
default:
|
||||
_xScale = 1;
|
||||
_yScale = 1;
|
||||
break;
|
||||
}
|
||||
_imgResampler->set3D(videoMode);
|
||||
}
|
||||
|
||||
void FramebufferFrameGrabber::grabFrame(Image<ColorRgba> & image)
|
||||
void FramebufferFrameGrabber::grabFrame(Image<ColorRgb> & image)
|
||||
{
|
||||
struct fb_var_screeninfo vinfo;
|
||||
unsigned capSize, px, py, index, bytesPerPixel;
|
||||
float x_scale, y_scale;
|
||||
|
||||
/* resize the given image if needed */
|
||||
if (image.width() != _width || image.height() != _height)
|
||||
{
|
||||
image.resize(_width, _height);
|
||||
}
|
||||
unsigned capSize, bytesPerPixel;
|
||||
PixelFormat pixelFormat;
|
||||
|
||||
/* Open the framebuffer device */
|
||||
_fbfd = open(_fbDevice.c_str(), O_RDONLY);
|
||||
@@ -82,41 +69,37 @@ void FramebufferFrameGrabber::grabFrame(Image<ColorRgba> & image)
|
||||
|
||||
bytesPerPixel = vinfo.bits_per_pixel / 8;
|
||||
capSize = vinfo.xres * vinfo.yres * bytesPerPixel;
|
||||
|
||||
|
||||
if (vinfo.bits_per_pixel == 16)
|
||||
{
|
||||
pixelFormat = PIXELFORMAT_BGR16;
|
||||
}
|
||||
else if (vinfo.bits_per_pixel == 24)
|
||||
{
|
||||
pixelFormat = PIXELFORMAT_BGR24;
|
||||
}
|
||||
else if (vinfo.bits_per_pixel == 32)
|
||||
{
|
||||
pixelFormat = PIXELFORMAT_BGR32;
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cerr << "Unknown pixel format: " << vinfo.bits_per_pixel << " bits per pixel" << std::endl;
|
||||
close(_fbfd);
|
||||
return;
|
||||
}
|
||||
|
||||
/* map the device to memory */
|
||||
_fbp = (unsigned char*)mmap(0, capSize, PROT_READ, MAP_PRIVATE | MAP_NORESERVE, _fbfd, 0);
|
||||
|
||||
/* nearest neighbor downscaling */
|
||||
x_scale = (vinfo.xres / float(_xScale)) / float(_width);
|
||||
y_scale = (vinfo.yres / float(_yScale)) / float(_height);
|
||||
|
||||
ColorRgba *pPixel = image.memptr();
|
||||
for (unsigned i=0; i < _height; i++) {
|
||||
for (unsigned j=0; j < _width; j++) {
|
||||
px = floor(j * x_scale);
|
||||
py = floor(i * y_scale);
|
||||
index = (py * vinfo.xres + px) * bytesPerPixel;
|
||||
|
||||
if (vinfo.bits_per_pixel == 16) {
|
||||
pPixel->blue = (_fbp[index] & 0x1f) << 3;
|
||||
pPixel->green = (((_fbp[index + 1] & 0x7) << 3) | (_fbp[index] & 0xE0) >> 5) << 2;
|
||||
pPixel->red = (_fbp[index + 1] & 0xF8);
|
||||
pPixel->alpha = 255;
|
||||
} else if(vinfo.bits_per_pixel == 24) {
|
||||
pPixel->blue = _fbp[index];
|
||||
pPixel->green = _fbp[index + 1];
|
||||
pPixel->red = _fbp[index + 2];
|
||||
pPixel->alpha = 255;
|
||||
} else if(vinfo.bits_per_pixel == 32) {
|
||||
pPixel->blue = _fbp[index];
|
||||
pPixel->green = _fbp[index + 1];
|
||||
pPixel->red = _fbp[index + 2];
|
||||
pPixel->alpha = _fbp[index + 3];
|
||||
}
|
||||
|
||||
pPixel++;
|
||||
}
|
||||
}
|
||||
_imgResampler->setHorizontalPixelDecimation(vinfo.xres/_width);
|
||||
_imgResampler->setVerticalPixelDecimation(vinfo.yres/_height);
|
||||
_imgResampler->processImage(_fbp,
|
||||
vinfo.xres,
|
||||
vinfo.yres,
|
||||
vinfo.xres * bytesPerPixel,
|
||||
pixelFormat,
|
||||
image);
|
||||
|
||||
munmap(_fbp, capSize);
|
||||
close(_fbfd);
|
||||
|
@@ -2,8 +2,9 @@
|
||||
|
||||
// Utils includes
|
||||
#include <utils/Image.h>
|
||||
#include <utils/ColorRgba.h>
|
||||
#include <utils/ColorRgb.h>
|
||||
#include <utils/VideoMode.h>
|
||||
#include <utils/ImageResampler.h>
|
||||
|
||||
///
|
||||
/// The FramebufferFrameGrabber is used for creating snapshots of the display (screenshots)
|
||||
@@ -35,7 +36,7 @@ public:
|
||||
/// @param[out] image The snapped screenshot (should be initialized with correct width and
|
||||
/// height)
|
||||
///
|
||||
void grabFrame(Image<ColorRgba> & image);
|
||||
void grabFrame(Image<ColorRgb> & image);
|
||||
|
||||
private:
|
||||
/// Framebuffer file descriptor
|
||||
@@ -53,9 +54,6 @@ private:
|
||||
/// Height of the captured snapshot [pixels]
|
||||
const unsigned _height;
|
||||
|
||||
/// width scale factor for 3D image processing
|
||||
float _xScale;
|
||||
|
||||
/// height scale factor for 3D image processing
|
||||
float _yScale;
|
||||
/// Image resampler for downscaling the image
|
||||
ImageResampler * _imgResampler;
|
||||
};
|
||||
|
30
libsrc/grabber/osx/CMakeLists.txt
Normal file
30
libsrc/grabber/osx/CMakeLists.txt
Normal file
@@ -0,0 +1,30 @@
|
||||
# Define the current source locations
|
||||
SET(CURRENT_HEADER_DIR ${CMAKE_SOURCE_DIR}/include/grabber)
|
||||
SET(CURRENT_SOURCE_DIR ${CMAKE_SOURCE_DIR}/libsrc/grabber/osx)
|
||||
|
||||
# Group the headers that go through the MOC compiler
|
||||
SET(OsxGrabberQT_HEADERS
|
||||
${CURRENT_HEADER_DIR}/OsxWrapper.h
|
||||
)
|
||||
|
||||
SET(OsxGrabberHEADERS
|
||||
${CURRENT_SOURCE_DIR}/OsxFrameGrabber.h
|
||||
)
|
||||
|
||||
SET(OsxGrabberSOURCES
|
||||
${CURRENT_SOURCE_DIR}/OsxWrapper.cpp
|
||||
${CURRENT_SOURCE_DIR}/OsxFrameGrabber.cpp
|
||||
)
|
||||
|
||||
QT4_WRAP_CPP(OsxGrabberHEADERS_MOC ${OsxGrabberQT_HEADERS})
|
||||
|
||||
add_library(osx-grabber
|
||||
${OsxGrabberHEADERS}
|
||||
${OsxGrabberQT_HEADERS}
|
||||
${OsxGrabberHEADERS_MOC}
|
||||
${OsxGrabberSOURCES}
|
||||
)
|
||||
|
||||
target_link_libraries(osx-grabber
|
||||
hyperion
|
||||
${QT_LIBRARIES})
|
82
libsrc/grabber/osx/OsxFrameGrabber.cpp
Executable file
82
libsrc/grabber/osx/OsxFrameGrabber.cpp
Executable file
@@ -0,0 +1,82 @@
|
||||
// STL includes
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
|
||||
// Local includes
|
||||
#include "OsxFrameGrabber.h"
|
||||
|
||||
OsxFrameGrabber::OsxFrameGrabber(const unsigned display, const unsigned width, const unsigned height) :
|
||||
_screenIndex(display),
|
||||
_width(width),
|
||||
_height(height),
|
||||
_imgResampler(new ImageResampler())
|
||||
{
|
||||
CGImageRef image;
|
||||
CGDisplayCount displayCount;
|
||||
CGDirectDisplayID displays[8];
|
||||
|
||||
// get list of displays
|
||||
CGGetActiveDisplayList(8, displays, &displayCount);
|
||||
if (_screenIndex + 1 > displayCount)
|
||||
{
|
||||
std::cerr << "OSX display with index " << _screenIndex << " is not available. Using main display" << std::endl;
|
||||
_display = kCGDirectMainDisplay;
|
||||
} else {
|
||||
_display = displays[_screenIndex];
|
||||
}
|
||||
|
||||
image = CGDisplayCreateImage(_display);
|
||||
assert(image != NULL);
|
||||
|
||||
std::cout << "OSX display opened with resolution: " << CGImageGetWidth(image) << "x" << CGImageGetHeight(image) << "@" << CGImageGetBitsPerPixel(image) << "bit" << std::endl;
|
||||
|
||||
CGImageRelease(image);
|
||||
}
|
||||
|
||||
OsxFrameGrabber::~OsxFrameGrabber()
|
||||
{
|
||||
delete _imgResampler;
|
||||
}
|
||||
|
||||
void OsxFrameGrabber::setVideoMode(const VideoMode videoMode)
|
||||
{
|
||||
_imgResampler->set3D(videoMode);
|
||||
}
|
||||
|
||||
void OsxFrameGrabber::grabFrame(Image<ColorRgb> & image)
|
||||
{
|
||||
CGImageRef dispImage;
|
||||
CFDataRef imgData;
|
||||
unsigned char * pImgData;
|
||||
unsigned dspWidth, dspHeight;
|
||||
|
||||
dispImage = CGDisplayCreateImage(_display);
|
||||
|
||||
// dsiplay lost, use main
|
||||
if (dispImage == NULL && _display)
|
||||
{
|
||||
dispImage = CGDisplayCreateImage(kCGDirectMainDisplay);
|
||||
// no displays connected, return
|
||||
if (dispImage == NULL)
|
||||
{
|
||||
std::cerr << "OSX no display connected..." << std::endl;
|
||||
return;
|
||||
}
|
||||
}
|
||||
imgData = CGDataProviderCopyData(CGImageGetDataProvider(dispImage));
|
||||
pImgData = (unsigned char*) CFDataGetBytePtr(imgData);
|
||||
dspWidth = CGImageGetWidth(dispImage);
|
||||
dspHeight = CGImageGetHeight(dispImage);
|
||||
|
||||
_imgResampler->setHorizontalPixelDecimation(dspWidth/_width);
|
||||
_imgResampler->setVerticalPixelDecimation(dspHeight/_height);
|
||||
_imgResampler->processImage( pImgData,
|
||||
dspWidth,
|
||||
dspHeight,
|
||||
CGImageGetBytesPerRow(dispImage),
|
||||
PIXELFORMAT_BGR32,
|
||||
image);
|
||||
|
||||
CFRelease(imgData);
|
||||
CGImageRelease(dispImage);
|
||||
}
|
59
libsrc/grabber/osx/OsxFrameGrabber.h
Normal file
59
libsrc/grabber/osx/OsxFrameGrabber.h
Normal file
@@ -0,0 +1,59 @@
|
||||
#pragma once
|
||||
|
||||
// OSX includes
|
||||
#include <CoreGraphics/CoreGraphics.h>
|
||||
|
||||
// Utils includes
|
||||
#include <utils/Image.h>
|
||||
#include <utils/ColorRgb.h>
|
||||
#include <utils/VideoMode.h>
|
||||
#include <utils/ImageResampler.h>
|
||||
|
||||
///
|
||||
/// The OsxFrameGrabber is used for creating snapshots of the display (screenshots)
|
||||
///
|
||||
class OsxFrameGrabber
|
||||
{
|
||||
public:
|
||||
///
|
||||
/// Construct a OsxFrameGrabber that will capture snapshots with specified dimensions.
|
||||
///
|
||||
/// @param[in] display The index of the display to capture
|
||||
/// @param[in] width The width of the captured screenshot
|
||||
/// @param[in] height The heigth of the captured screenshot
|
||||
///
|
||||
OsxFrameGrabber(const unsigned display, const unsigned width, const unsigned height);
|
||||
~OsxFrameGrabber();
|
||||
|
||||
///
|
||||
/// Set the video mode (2D/3D)
|
||||
/// @param[in] mode The new video mode
|
||||
///
|
||||
void setVideoMode(const VideoMode videoMode);
|
||||
|
||||
///
|
||||
/// Captures a single snapshot of the display and writes the data to the given image. The
|
||||
/// provided image should have the same dimensions as the configured values (_width and
|
||||
/// _height)
|
||||
///
|
||||
/// @param[out] image The snapped screenshot (should be initialized with correct width and
|
||||
/// height)
|
||||
///
|
||||
void grabFrame(Image<ColorRgb> & image);
|
||||
|
||||
private:
|
||||
/// display
|
||||
const unsigned _screenIndex;
|
||||
|
||||
/// With of the captured snapshot [pixels]
|
||||
const unsigned _width;
|
||||
|
||||
/// Height of the captured snapshot [pixels]
|
||||
const unsigned _height;
|
||||
|
||||
/// Reference to the captured diaplay
|
||||
CGDirectDisplayID _display;
|
||||
|
||||
/// Image resampler for downscaling the image
|
||||
ImageResampler * _imgResampler;
|
||||
};
|
79
libsrc/grabber/osx/OsxWrapper.cpp
Normal file
79
libsrc/grabber/osx/OsxWrapper.cpp
Normal file
@@ -0,0 +1,79 @@
|
||||
// Hyperion includes
|
||||
#include <hyperion/Hyperion.h>
|
||||
#include <hyperion/ImageProcessorFactory.h>
|
||||
#include <hyperion/ImageProcessor.h>
|
||||
|
||||
// Osx grabber includes
|
||||
#include <grabber/OsxWrapper.h>
|
||||
#include "OsxFrameGrabber.h"
|
||||
|
||||
OsxWrapper::OsxWrapper(const unsigned display, const unsigned grabWidth, const unsigned grabHeight, const unsigned updateRate_Hz, Hyperion * hyperion) :
|
||||
_updateInterval_ms(1000/updateRate_Hz),
|
||||
_timeout_ms(2 * _updateInterval_ms),
|
||||
_priority(1000),
|
||||
_timer(),
|
||||
_image(grabWidth, grabHeight),
|
||||
_frameGrabber(new OsxFrameGrabber(display, grabWidth, grabHeight)),
|
||||
_processor(ImageProcessorFactory::getInstance().newImageProcessor()),
|
||||
_ledColors(hyperion->getLedCount(), ColorRgb{0,0,0}),
|
||||
_hyperion(hyperion)
|
||||
{
|
||||
// Configure the timer to generate events every n milliseconds
|
||||
_timer.setInterval(_updateInterval_ms);
|
||||
_timer.setSingleShot(false);
|
||||
|
||||
_processor->setSize(grabWidth, grabHeight);
|
||||
|
||||
// Connect the QTimer to this
|
||||
QObject::connect(&_timer, SIGNAL(timeout()), this, SLOT(action()));
|
||||
}
|
||||
|
||||
OsxWrapper::~OsxWrapper()
|
||||
{
|
||||
// Cleanup used resources (ImageProcessor and FrameGrabber)
|
||||
delete _processor;
|
||||
delete _frameGrabber;
|
||||
}
|
||||
|
||||
void OsxWrapper::start()
|
||||
{
|
||||
// Start the timer with the pre configured interval
|
||||
_timer.start();
|
||||
}
|
||||
|
||||
void OsxWrapper::action()
|
||||
{
|
||||
// Grab frame into the allocated image
|
||||
_frameGrabber->grabFrame(_image);
|
||||
|
||||
_processor->process(_image, _ledColors);
|
||||
|
||||
_hyperion->setColors(_priority, _ledColors, _timeout_ms);
|
||||
}
|
||||
void OsxWrapper::stop()
|
||||
{
|
||||
// Stop the timer, effectivly stopping the process
|
||||
_timer.stop();
|
||||
}
|
||||
|
||||
void OsxWrapper::setGrabbingMode(const GrabbingMode mode)
|
||||
{
|
||||
switch (mode)
|
||||
{
|
||||
case GRABBINGMODE_VIDEO:
|
||||
case GRABBINGMODE_AUDIO:
|
||||
case GRABBINGMODE_PHOTO:
|
||||
case GRABBINGMODE_MENU:
|
||||
case GRABBINGMODE_INVALID:
|
||||
start();
|
||||
break;
|
||||
case GRABBINGMODE_OFF:
|
||||
stop();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void OsxWrapper::setVideoMode(const VideoMode mode)
|
||||
{
|
||||
_frameGrabber->setVideoMode(mode);
|
||||
}
|
Reference in New Issue
Block a user