Refactor V4L2 and X11 grabbers to share more code

Former-commit-id: 46176e53d1acf39f9bd0c0ecbb8e5fb5ab4d45be
This commit is contained in:
poljvd
2014-11-21 21:24:33 +01:00
parent 01854a471e
commit d89f504d83
22 changed files with 724 additions and 748 deletions

View File

@@ -1,4 +1,3 @@
if (ENABLE_DISPMANX)
add_subdirectory(dispmanx)
endif (ENABLE_DISPMANX)
@@ -6,3 +5,7 @@ endif (ENABLE_DISPMANX)
if (ENABLE_V4L2)
add_subdirectory(v4l2)
endif (ENABLE_V4L2)
if (ENABLE_X11)
add_subdirectory(x11)
endif()

View File

@@ -0,0 +1,37 @@
# Define the current source locations
SET(CURRENT_HEADER_DIR ${CMAKE_SOURCE_DIR}/include/grabber)
SET(CURRENT_SOURCE_DIR ${CMAKE_SOURCE_DIR}/libsrc/grabber/x11)
# Find X11
find_package(X11 REQUIRED)
include_directories(
${QT_INCLUDES}
${X11_INCLUDES}
)
SET(X11_QT_HEADERS
${CURRENT_HEADER_DIR}/X11Grabber.h
)
SET(X11_HEADERS
${CURRENT_HEADER_DIR}/X11Grabber.h
)
SET(X11_SOURCES
${CURRENT_SOURCE_DIR}/X11Grabber.cpp
)
QT4_WRAP_CPP(X11_HEADERS_MOC ${X11_QT_HEADERS})
add_library(x11-grabber
${X11_HEADERS}
${X11_SOURCES}
${X11_QT_HEADERS}
${X11_HEADERS_MOC}
)
target_link_libraries(x11-grabber
hyperion
${QT_LIBRARIES}
)

View File

@@ -0,0 +1,115 @@
// STL includes
#include <iostream>
#include <cstdint>
// X11 includes
#include <X11/Xutil.h>
// X11Grabber includes
#include <grabber/X11Grabber.h>
X11Grabber::X11Grabber(const unsigned cropHorizontal, const unsigned cropVertical, const unsigned pixelDecimation) :
_pixelDecimation(pixelDecimation),
_cropWidth(cropHorizontal),
_cropHeight(cropVertical),
_x11Display(nullptr),
_screenWidth(0),
_screenHeight(0),
_image(0,0)
{
// empty
}
X11Grabber::~X11Grabber()
{
if (_x11Display != nullptr)
{
XCloseDisplay(_x11Display);
}
}
int X11Grabber::open()
{
const char * display_name = nullptr;
_x11Display = XOpenDisplay(display_name);
if (_x11Display == nullptr)
{
std::cerr << "Failed to open the default X11-display" << std::endl;
return -1;
}
return 0;
}
Image<ColorRgb> & X11Grabber::grab()
{
if (_x11Display == nullptr)
{
open();
}
updateScreenDimensions();
const int croppedWidth = _screenWidth - 2*_cropWidth;
const int croppedHeight = _screenHeight - 2*_cropHeight;
// Capture the current screen
XImage * xImage = XGetImage(_x11Display, DefaultRootWindow(_x11Display), _cropWidth, _cropHeight, croppedWidth, croppedHeight, AllPlanes, ZPixmap);
if (xImage == nullptr)
{
std::cerr << "Grab failed" << std::endl;
return _image;
}
// Copy the capture XImage to the local image (and apply required decimation)
ColorRgb * outputPtr = _image.memptr();
for (int iY=(_pixelDecimation/2); iY<croppedHeight; iY+=_pixelDecimation)
{
for (int iX=(_pixelDecimation/2); iX<croppedWidth; iX+=_pixelDecimation)
{
// Extract the pixel from the X11-image
const uint32_t pixel = uint32_t(XGetPixel(xImage, iX, iY));
// Assign the color value
outputPtr->red = uint8_t((pixel >> 16) & 0xff);
outputPtr->green = uint8_t((pixel >> 8) & 0xff);
outputPtr->blue = uint8_t((pixel >> 0) & 0xff);
// Move to the next output pixel
++outputPtr;
}
}
// Cleanup allocated resources of the X11 grab
XDestroyImage(xImage);
return _image;
}
int X11Grabber::updateScreenDimensions()
{
XWindowAttributes window_attributes_return;
const Status status = XGetWindowAttributes(_x11Display, DefaultRootWindow(_x11Display), &window_attributes_return);
if (status == 0)
{
std::cerr << "Failed to obtain window attributes" << std::endl;
return -1;
}
if (_screenWidth == unsigned(window_attributes_return.width) && _screenHeight == unsigned(window_attributes_return.height))
{
// No update required
return 0;
}
std::cout << "Update of screen resolution: [" << _screenWidth << "x" << _screenHeight <<"] => ";
_screenWidth = window_attributes_return.width;
_screenHeight = window_attributes_return.height;
std::cout << "[" << _screenWidth << "x" << _screenHeight <<"]" << std::endl;
// Update the size of the buffer used to transfer the screenshot
int width = (_screenWidth - 2 * _cropWidth + _pixelDecimation/2) / _pixelDecimation;
int height = (_screenHeight - 2 * _cropHeight + _pixelDecimation/2) / _pixelDecimation;
_image.resize(width, height);
return 0;
}