mirror of
https://github.com/hyperion-project/hyperion.ng.git
synced 2025-03-01 10:33:28 +00:00
Extract ImageResampler from X11Grabber
Former-commit-id: 63e95fa85fc0ef5a66f6eb8b396cf4b6370cf5dd
This commit is contained in:
@@ -11,119 +11,119 @@
|
||||
// util includes
|
||||
#include <utils/Image.h>
|
||||
#include <utils/ColorRgb.h>
|
||||
#include <utils/PixelFormat.h>
|
||||
#include <utils/VideoMode.h>
|
||||
|
||||
// grabber includes
|
||||
#include <grabber/VideoStandard.h>
|
||||
#include <grabber/PixelFormat.h>
|
||||
|
||||
/// Capture class for V4L2 devices
|
||||
///
|
||||
/// @see http://linuxtv.org/downloads/v4l-dvb-apis/capture-example.html
|
||||
class V4L2Grabber : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
V4L2Grabber(const std::string & device,
|
||||
int input,
|
||||
VideoStandard videoStandard, PixelFormat pixelFormat,
|
||||
int width,
|
||||
int height,
|
||||
int frameDecimation,
|
||||
int horizontalPixelDecimation,
|
||||
int verticalPixelDecimation);
|
||||
virtual ~V4L2Grabber();
|
||||
V4L2Grabber(const std::string & device,
|
||||
int input,
|
||||
VideoStandard videoStandard, PixelFormat pixelFormat,
|
||||
int width,
|
||||
int height,
|
||||
int frameDecimation,
|
||||
int horizontalPixelDecimation,
|
||||
int verticalPixelDecimation);
|
||||
virtual ~V4L2Grabber();
|
||||
|
||||
public slots:
|
||||
void setCropping(int cropLeft,
|
||||
int cropRight,
|
||||
int cropTop,
|
||||
int cropBottom);
|
||||
void setCropping(int cropLeft,
|
||||
int cropRight,
|
||||
int cropTop,
|
||||
int cropBottom);
|
||||
|
||||
void set3D(VideoMode mode);
|
||||
void set3D(VideoMode mode);
|
||||
|
||||
void setSignalThreshold(double redSignalThreshold,
|
||||
double greenSignalThreshold,
|
||||
double blueSignalThreshold,
|
||||
int noSignalCounterThreshold);
|
||||
void setSignalThreshold(double redSignalThreshold,
|
||||
double greenSignalThreshold,
|
||||
double blueSignalThreshold,
|
||||
int noSignalCounterThreshold);
|
||||
|
||||
void start();
|
||||
void start();
|
||||
|
||||
void stop();
|
||||
void stop();
|
||||
|
||||
signals:
|
||||
void newFrame(const Image<ColorRgb> & image);
|
||||
void newFrame(const Image<ColorRgb> & image);
|
||||
|
||||
private slots:
|
||||
int read_frame();
|
||||
int read_frame();
|
||||
|
||||
private:
|
||||
void open_device();
|
||||
void open_device();
|
||||
|
||||
void close_device();
|
||||
void close_device();
|
||||
|
||||
void init_read(unsigned int buffer_size);
|
||||
void init_read(unsigned int buffer_size);
|
||||
|
||||
void init_mmap();
|
||||
void init_mmap();
|
||||
|
||||
void init_userp(unsigned int buffer_size);
|
||||
void init_userp(unsigned int buffer_size);
|
||||
|
||||
void init_device(VideoStandard videoStandard, int input);
|
||||
void init_device(VideoStandard videoStandard, int input);
|
||||
|
||||
void uninit_device();
|
||||
void uninit_device();
|
||||
|
||||
void start_capturing();
|
||||
void start_capturing();
|
||||
|
||||
void stop_capturing();
|
||||
void stop_capturing();
|
||||
|
||||
bool process_image(const void *p, int size);
|
||||
bool process_image(const void *p, int size);
|
||||
|
||||
void process_image(const uint8_t *p);
|
||||
void process_image(const uint8_t *p);
|
||||
|
||||
int xioctl(int request, void *arg);
|
||||
int xioctl(int request, void *arg);
|
||||
|
||||
void throw_exception(const std::string &error);
|
||||
void throw_exception(const std::string &error);
|
||||
|
||||
void throw_errno_exception(const std::string &error);
|
||||
void throw_errno_exception(const std::string &error);
|
||||
|
||||
private:
|
||||
enum io_method {
|
||||
IO_METHOD_READ,
|
||||
IO_METHOD_MMAP,
|
||||
IO_METHOD_USERPTR
|
||||
};
|
||||
enum io_method {
|
||||
IO_METHOD_READ,
|
||||
IO_METHOD_MMAP,
|
||||
IO_METHOD_USERPTR
|
||||
};
|
||||
|
||||
struct buffer {
|
||||
void *start;
|
||||
size_t length;
|
||||
};
|
||||
struct buffer {
|
||||
void *start;
|
||||
size_t length;
|
||||
};
|
||||
|
||||
private:
|
||||
const std::string _deviceName;
|
||||
const io_method _ioMethod;
|
||||
int _fileDescriptor;
|
||||
std::vector<buffer> _buffers;
|
||||
const std::string _deviceName;
|
||||
const io_method _ioMethod;
|
||||
int _fileDescriptor;
|
||||
std::vector<buffer> _buffers;
|
||||
|
||||
PixelFormat _pixelFormat;
|
||||
int _width;
|
||||
int _height;
|
||||
int _frameByteSize;
|
||||
int _cropLeft;
|
||||
int _cropRight;
|
||||
int _cropTop;
|
||||
int _cropBottom;
|
||||
int _frameDecimation;
|
||||
int _horizontalPixelDecimation;
|
||||
int _verticalPixelDecimation;
|
||||
int _noSignalCounterThreshold;
|
||||
PixelFormat _pixelFormat;
|
||||
int _width;
|
||||
int _height;
|
||||
int _frameByteSize;
|
||||
int _cropLeft;
|
||||
int _cropRight;
|
||||
int _cropTop;
|
||||
int _cropBottom;
|
||||
int _frameDecimation;
|
||||
int _horizontalPixelDecimation;
|
||||
int _verticalPixelDecimation;
|
||||
int _noSignalCounterThreshold;
|
||||
|
||||
ColorRgb _noSignalThresholdColor;
|
||||
ColorRgb _noSignalThresholdColor;
|
||||
|
||||
VideoMode _mode3D;
|
||||
VideoMode _mode3D;
|
||||
|
||||
int _currentFrame;
|
||||
int _noSignalCounter;
|
||||
int _currentFrame;
|
||||
int _noSignalCounter;
|
||||
|
||||
QSocketNotifier * _streamNotifier;
|
||||
QSocketNotifier * _streamNotifier;
|
||||
};
|
||||
|
@@ -2,6 +2,7 @@
|
||||
// Hyperion-utils includes
|
||||
#include <utils/Image.h>
|
||||
#include <utils/ColorRgb.h>
|
||||
#include <utils/ImageResampler.h>
|
||||
|
||||
// X11 includes
|
||||
#include <X11/Xlib.h>
|
||||
@@ -10,7 +11,7 @@ class X11Grabber
|
||||
{
|
||||
public:
|
||||
|
||||
X11Grabber(const unsigned cropHorizontal, const unsigned cropVertical, const unsigned pixelDecimation);
|
||||
X11Grabber(int cropLeft, int cropRight, int cropTop, int cropBottom, int horizontalPixelDecimation, int verticalPixelDecimation);
|
||||
|
||||
virtual ~X11Grabber();
|
||||
|
||||
@@ -19,11 +20,12 @@ public:
|
||||
Image<ColorRgb> & grab();
|
||||
|
||||
private:
|
||||
ImageResampler _imageResampler;
|
||||
|
||||
const unsigned _pixelDecimation;
|
||||
|
||||
const unsigned _cropWidth;
|
||||
const unsigned _cropHeight;
|
||||
int _cropLeft;
|
||||
int _cropRight;
|
||||
int _cropTop;
|
||||
int _cropBottom;
|
||||
|
||||
/// Reference to the X11 display (nullptr if not opened)
|
||||
Display * _x11Display;
|
||||
|
@@ -11,207 +11,202 @@ class Image
|
||||
{
|
||||
public:
|
||||
|
||||
typedef Pixel_T pixel_type;
|
||||
typedef Pixel_T pixel_type;
|
||||
|
||||
///
|
||||
/// Default constructor for an image
|
||||
///
|
||||
Image() :
|
||||
_width(1),
|
||||
_height(1),
|
||||
_pixels(new Pixel_T[2]),
|
||||
_endOfPixels(_pixels + 1)
|
||||
{
|
||||
memset(_pixels, 0, 2*sizeof(Pixel_T));
|
||||
}
|
||||
///
|
||||
/// Default constructor for an image
|
||||
///
|
||||
Image() :
|
||||
_width(1),
|
||||
_height(1),
|
||||
_pixels(new Pixel_T[2]),
|
||||
_endOfPixels(_pixels + 1)
|
||||
{
|
||||
memset(_pixels, 0, 2*sizeof(Pixel_T));
|
||||
}
|
||||
|
||||
///
|
||||
/// Constructor for an image with specified width and height
|
||||
///
|
||||
/// @param width The width of the image
|
||||
/// @param height The height of the image
|
||||
///
|
||||
Image(const unsigned width, const unsigned height) :
|
||||
_width(width),
|
||||
_height(height),
|
||||
_pixels(new Pixel_T[width * height + 1]),
|
||||
_endOfPixels(_pixels + width * height)
|
||||
{
|
||||
memset(_pixels, 0, (_width*_height+1)*sizeof(Pixel_T));
|
||||
}
|
||||
///
|
||||
/// Constructor for an image with specified width and height
|
||||
///
|
||||
/// @param width The width of the image
|
||||
/// @param height The height of the image
|
||||
///
|
||||
Image(const unsigned width, const unsigned height) :
|
||||
_width(width),
|
||||
_height(height),
|
||||
_pixels(new Pixel_T[width * height + 1]),
|
||||
_endOfPixels(_pixels + width * height)
|
||||
{
|
||||
memset(_pixels, 0, (_width*_height+1)*sizeof(Pixel_T));
|
||||
}
|
||||
|
||||
///
|
||||
/// Constructor for an image with specified width and height
|
||||
///
|
||||
/// @param width The width of the image
|
||||
/// @param height The height of the image
|
||||
/// @param background The color of the image
|
||||
///
|
||||
Image(const unsigned width, const unsigned height, const Pixel_T background) :
|
||||
_width(width),
|
||||
_height(height),
|
||||
_pixels(new Pixel_T[width * height + 1]),
|
||||
_endOfPixels(_pixels + width * height)
|
||||
{
|
||||
std::fill(_pixels, _endOfPixels, background);
|
||||
}
|
||||
///
|
||||
/// Constructor for an image with specified width and height
|
||||
///
|
||||
/// @param width The width of the image
|
||||
/// @param height The height of the image
|
||||
/// @param background The color of the image
|
||||
///
|
||||
Image(const unsigned width, const unsigned height, const Pixel_T background) :
|
||||
_width(width),
|
||||
_height(height),
|
||||
_pixels(new Pixel_T[width * height + 1]),
|
||||
_endOfPixels(_pixels + width * height)
|
||||
{
|
||||
std::fill(_pixels, _endOfPixels, background);
|
||||
}
|
||||
|
||||
///
|
||||
/// Copy constructor for an image
|
||||
///
|
||||
Image(const Image & other) :
|
||||
_width(other._width),
|
||||
_height(other._height),
|
||||
_pixels(new Pixel_T[other._width * other._height + 1]),
|
||||
_endOfPixels(_pixels + other._width * other._height)
|
||||
{
|
||||
memcpy(_pixels, other._pixels, other._width * other._height * sizeof(Pixel_T));
|
||||
}
|
||||
///
|
||||
/// Copy constructor for an image
|
||||
///
|
||||
Image(const Image & other) :
|
||||
_width(other._width),
|
||||
_height(other._height),
|
||||
_pixels(new Pixel_T[other._width * other._height + 1]),
|
||||
_endOfPixels(_pixels + other._width * other._height)
|
||||
{
|
||||
memcpy(_pixels, other._pixels, other._width * other._height * sizeof(Pixel_T));
|
||||
}
|
||||
|
||||
///
|
||||
/// Destructor
|
||||
///
|
||||
~Image()
|
||||
{
|
||||
delete[] _pixels;
|
||||
}
|
||||
///
|
||||
/// Destructor
|
||||
///
|
||||
~Image()
|
||||
{
|
||||
delete[] _pixels;
|
||||
}
|
||||
|
||||
///
|
||||
/// Returns the width of the image
|
||||
///
|
||||
/// @return The width of the image
|
||||
///
|
||||
inline unsigned width() const
|
||||
{
|
||||
return _width;
|
||||
}
|
||||
///
|
||||
/// Returns the width of the image
|
||||
///
|
||||
/// @return The width of the image
|
||||
///
|
||||
inline unsigned width() const
|
||||
{
|
||||
return _width;
|
||||
}
|
||||
|
||||
///
|
||||
/// Returns the height of the image
|
||||
///
|
||||
/// @return The height of the image
|
||||
///
|
||||
inline unsigned height() const
|
||||
{
|
||||
return _height;
|
||||
}
|
||||
///
|
||||
/// Returns the height of the image
|
||||
///
|
||||
/// @return The height of the image
|
||||
///
|
||||
inline unsigned height() const
|
||||
{
|
||||
return _height;
|
||||
}
|
||||
|
||||
uint8_t alpha(const unsigned pixel) const
|
||||
{
|
||||
return (_pixels + pixel)->red;
|
||||
}
|
||||
uint8_t red(const unsigned pixel) const
|
||||
{
|
||||
return (_pixels + pixel)->red;
|
||||
}
|
||||
|
||||
uint8_t red(const unsigned pixel) const
|
||||
{
|
||||
return (_pixels + pixel)->red;
|
||||
}
|
||||
uint8_t green(const unsigned pixel) const
|
||||
{
|
||||
return (_pixels + pixel)->green;
|
||||
}
|
||||
|
||||
uint8_t green(const unsigned pixel) const
|
||||
{
|
||||
return (_pixels + pixel)->green;
|
||||
}
|
||||
uint8_t blue(const unsigned pixel) const
|
||||
{
|
||||
return (_pixels + pixel)->blue;
|
||||
}
|
||||
|
||||
uint8_t blue(const unsigned pixel) const
|
||||
{
|
||||
return (_pixels + pixel)->blue;
|
||||
}
|
||||
///
|
||||
/// Returns a const reference to a specified pixel in the image
|
||||
///
|
||||
/// @param x The x index
|
||||
/// @param y The y index
|
||||
///
|
||||
/// @return const reference to specified pixel
|
||||
///
|
||||
const Pixel_T& operator()(const unsigned x, const unsigned y) const
|
||||
{
|
||||
return _pixels[toIndex(x,y)];
|
||||
}
|
||||
|
||||
///
|
||||
/// Returns a const reference to a specified pixel in the image
|
||||
///
|
||||
/// @param x The x index
|
||||
/// @param y The y index
|
||||
///
|
||||
/// @return const reference to specified pixel
|
||||
///
|
||||
const Pixel_T& operator()(const unsigned x, const unsigned y) const
|
||||
{
|
||||
return _pixels[toIndex(x,y)];
|
||||
}
|
||||
///
|
||||
/// Returns a reference to a specified pixel in the image
|
||||
///
|
||||
/// @param x The x index
|
||||
/// @param y The y index
|
||||
///
|
||||
/// @return reference to specified pixel
|
||||
///
|
||||
Pixel_T& operator()(const unsigned x, const unsigned y)
|
||||
{
|
||||
return _pixels[toIndex(x,y)];
|
||||
}
|
||||
|
||||
///
|
||||
/// Returns a reference to a specified pixel in the image
|
||||
///
|
||||
/// @param x The x index
|
||||
/// @param y The y index
|
||||
///
|
||||
/// @return reference to specified pixel
|
||||
///
|
||||
Pixel_T& operator()(const unsigned x, const unsigned y)
|
||||
{
|
||||
return _pixels[toIndex(x,y)];
|
||||
}
|
||||
/// Resize the image
|
||||
/// @param width The width of the image
|
||||
/// @param height The height of the image
|
||||
void resize(const unsigned width, const unsigned height)
|
||||
{
|
||||
if ((width*height) > (_endOfPixels-_pixels))
|
||||
{
|
||||
delete[] _pixels;
|
||||
_pixels = new Pixel_T[width*height + 1];
|
||||
_endOfPixels = _pixels + width*height;
|
||||
}
|
||||
|
||||
/// Resize the image
|
||||
/// @param width The width of the image
|
||||
/// @param height The height of the image
|
||||
void resize(const unsigned width, const unsigned height)
|
||||
{
|
||||
if ((width*height) > (_endOfPixels-_pixels))
|
||||
{
|
||||
delete[] _pixels;
|
||||
_pixels = new Pixel_T[width*height + 1];
|
||||
_endOfPixels = _pixels + width*height;
|
||||
}
|
||||
_width = width;
|
||||
_height = height;
|
||||
}
|
||||
|
||||
_width = width;
|
||||
_height = height;
|
||||
}
|
||||
///
|
||||
/// Copies another image into this image. The images should have exactly the same size.
|
||||
///
|
||||
/// @param other The image to copy into this
|
||||
///
|
||||
void copy(const Image<Pixel_T>& other)
|
||||
{
|
||||
assert(other._width == _width);
|
||||
assert(other._height == _height);
|
||||
|
||||
///
|
||||
/// Copies another image into this image. The images should have exactly the same size.
|
||||
///
|
||||
/// @param other The image to copy into this
|
||||
///
|
||||
void copy(const Image<Pixel_T>& other)
|
||||
{
|
||||
assert(other._width == _width);
|
||||
assert(other._height == _height);
|
||||
memcpy(_pixels, other._pixels, _width*_height*sizeof(Pixel_T));
|
||||
}
|
||||
|
||||
memcpy(_pixels, other._pixels, _width*_height*sizeof(Pixel_T));
|
||||
}
|
||||
///
|
||||
/// Returns a memory pointer to the first pixel in the image
|
||||
/// @return The memory pointer to the first pixel
|
||||
///
|
||||
Pixel_T* memptr()
|
||||
{
|
||||
return _pixels;
|
||||
}
|
||||
|
||||
///
|
||||
/// Returns a memory pointer to the first pixel in the image
|
||||
/// @return The memory pointer to the first pixel
|
||||
///
|
||||
Pixel_T* memptr()
|
||||
{
|
||||
return _pixels;
|
||||
}
|
||||
|
||||
///
|
||||
/// Returns a const memory pointer to the first pixel in the image
|
||||
/// @return The const memory pointer to the first pixel
|
||||
///
|
||||
const Pixel_T* memptr() const
|
||||
{
|
||||
return _pixels;
|
||||
}
|
||||
///
|
||||
/// Returns a const memory pointer to the first pixel in the image
|
||||
/// @return The const memory pointer to the first pixel
|
||||
///
|
||||
const Pixel_T* memptr() const
|
||||
{
|
||||
return _pixels;
|
||||
}
|
||||
private:
|
||||
|
||||
///
|
||||
/// Translate x and y coordinate to index of the underlying vector
|
||||
///
|
||||
/// @param x The x index
|
||||
/// @param y The y index
|
||||
///
|
||||
/// @return The index into the underlying data-vector
|
||||
///
|
||||
inline unsigned toIndex(const unsigned x, const unsigned y) const
|
||||
{
|
||||
return y*_width + x;
|
||||
}
|
||||
///
|
||||
/// Translate x and y coordinate to index of the underlying vector
|
||||
///
|
||||
/// @param x The x index
|
||||
/// @param y The y index
|
||||
///
|
||||
/// @return The index into the underlying data-vector
|
||||
///
|
||||
inline unsigned toIndex(const unsigned x, const unsigned y) const
|
||||
{
|
||||
return y*_width + x;
|
||||
}
|
||||
|
||||
private:
|
||||
/// The width of the image
|
||||
unsigned _width;
|
||||
/// The height of the image
|
||||
unsigned _height;
|
||||
/// The width of the image
|
||||
unsigned _width;
|
||||
/// The height of the image
|
||||
unsigned _height;
|
||||
|
||||
/// The pixels of the image
|
||||
Pixel_T* _pixels;
|
||||
/// The pixels of the image
|
||||
Pixel_T* _pixels;
|
||||
|
||||
/// Pointer to the last(extra) pixel
|
||||
Pixel_T* _endOfPixels;
|
||||
/// Pointer to the last(extra) pixel
|
||||
Pixel_T* _endOfPixels;
|
||||
};
|
||||
|
40
include/utils/ImageResampler.h
Normal file
40
include/utils/ImageResampler.h
Normal file
@@ -0,0 +1,40 @@
|
||||
#pragma once
|
||||
|
||||
#include <utils/VideoMode.h>
|
||||
#include <utils/PixelFormat.h>
|
||||
#include <utils/Image.h>
|
||||
#include <utils/ColorRgb.h>
|
||||
|
||||
class ImageResampler
|
||||
{
|
||||
public:
|
||||
ImageResampler();
|
||||
~ImageResampler();
|
||||
|
||||
void setHorizontalPixelDecimation(int decimator);
|
||||
|
||||
void setVerticalPixelDecimation(int decimator);
|
||||
|
||||
void setCropping(int cropLeft,
|
||||
int cropRight,
|
||||
int cropTop,
|
||||
int cropBottom);
|
||||
|
||||
void set3D(VideoMode mode);
|
||||
|
||||
void processImage(const uint8_t * data, int width, int height, int lineLength, PixelFormat pixelFormat,
|
||||
Image<ColorRgb> & outputImage) const;
|
||||
|
||||
private:
|
||||
static inline uint8_t clamp(int x);
|
||||
static void yuv2rgb(uint8_t y, uint8_t u, uint8_t v, uint8_t & r, uint8_t & g, uint8_t & b);
|
||||
|
||||
private:
|
||||
int _horizontalDecimation;
|
||||
int _verticalDecimation;
|
||||
int _cropLeft;
|
||||
int _cropRight;
|
||||
int _cropTop;
|
||||
int _cropBottom;
|
||||
VideoMode _videoMode;
|
||||
};
|
@@ -9,8 +9,9 @@
|
||||
enum PixelFormat {
|
||||
PIXELFORMAT_YUYV,
|
||||
PIXELFORMAT_UYVY,
|
||||
PIXELFORMAT_RGB32,
|
||||
PIXELFORMAT_NO_CHANGE
|
||||
PIXELFORMAT_RGB32,
|
||||
PIXELFORMAT_BGR32,
|
||||
PIXELFORMAT_NO_CHANGE
|
||||
};
|
||||
|
||||
inline PixelFormat parsePixelFormat(std::string pixelFormat)
|
||||
@@ -26,10 +27,14 @@ inline PixelFormat parsePixelFormat(std::string pixelFormat)
|
||||
{
|
||||
return PIXELFORMAT_UYVY;
|
||||
}
|
||||
else if (pixelFormat == "rgb32")
|
||||
{
|
||||
return PIXELFORMAT_RGB32;
|
||||
}
|
||||
else if (pixelFormat == "rgb32")
|
||||
{
|
||||
return PIXELFORMAT_RGB32;
|
||||
}
|
||||
else if (pixelFormat == "bgr32")
|
||||
{
|
||||
return PIXELFORMAT_BGR32;
|
||||
}
|
||||
|
||||
// return the default NO_CHANGE
|
||||
return PIXELFORMAT_NO_CHANGE;
|
Reference in New Issue
Block a user