hyperion.ng/include/utils/ImageData.h

183 lines
3.3 KiB
C
Raw Normal View History

2020-07-19 16:14:54 +02:00
#pragma once
// STL includes
#include <vector>
#include <cstdint>
#include <cstring>
#include <algorithm>
#include <cassert>
#include <type_traits>
#include <utils/ColorRgb.h>
// QT includes
#include <QSharedData>
// https://docs.microsoft.com/en-us/windows/win32/winprog/windows-data-types#ssize-t
#if defined(_MSC_VER)
#include <BaseTsd.h>
typedef SSIZE_T ssize_t;
#endif
template <typename Pixel_T>
class ImageData : public QSharedData
{
public:
typedef Pixel_T pixel_type;
2020-08-08 13:09:15 +02:00
ImageData(unsigned width, unsigned height, const Pixel_T background) :
2020-07-19 16:14:54 +02:00
_width(width),
_height(height),
_pixels(new Pixel_T[width * height + 1])
{
std::fill(_pixels, _pixels + width * height, background);
}
ImageData(const ImageData & other) :
QSharedData(other),
_width(other._width),
_height(other._height),
_pixels(new Pixel_T[other._width * other._height + 1])
{
Various Cleanups (#1075) * LedDevice - Address clang findings * Fix Windows Warnings * Ensure newInput is initialised * Clean-up unused elements for Plaform Capture * Fix initialization problem and spellings * Address clang findings and spelling corrections * LedDevice clean-ups * Cleanups * Align that getLedCount is int * Have "display" as default for Grabbers * Fix config during start-up for missing elements * Framegrabber Clean-up - Remove non supported grabbers from selection, filter valid options * Typo * Framegrabber.json - Fix property numbering * Preselect active Grabbertype * Sort Grabbernames * Align options with selected element * Fix deletion of pointer to incomplete type 'BonjourBrowserWrapper' * Address macOS compile warnings * Have default layout = 1 LED only to avoid errors as in #673 * Address lgtm findings * Address finding that params passed to LedDevice discovery were not considered * Cleanups after merging with latest master * Update Changelog * Address lgtm findings * Fix comment * Test Fix * Fix Python Warning * Handle Dummy Device assignment correctly * Address delete called on non-final 'commandline::Option' that has virtual functions but non-virtual destructor * Correct that QTimer.start accepts only int * Have Release Python GIL & reset threat state chnage downward compatible * Correct format specifier * LedDevice - add assertions * Readonly DB - Fix merge issue * Smoothing - Fix wrong defaults * LedDevice - correct assertion * Show smoothing config set# in debug and related values. * Suppress error on windows, if default file is "/dev/null" * CMAKE - Allow to define QT_BASE_DIR dynamically via environment-variable * Ignore Visual Studio specific files Co-authored-by: Paulchen Panther <16664240+Paulchen-Panther@users.noreply.github.com>
2020-11-14 17:58:56 +01:00
memcpy(_pixels, other._pixels, static_cast<ulong>(other._width) * static_cast<ulong>(other._height) * sizeof(Pixel_T));
2020-07-19 16:14:54 +02:00
}
ImageData& operator=(ImageData rhs)
{
rhs.swap(*this);
return *this;
}
void swap(ImageData& s) noexcept
{
using std::swap;
swap(this->_width, s._width);
swap(this->_height, s._height);
swap(this->_pixels, s._pixels);
}
ImageData(ImageData&& src) noexcept
: _width(0)
, _height(0)
, _pixels(NULL)
{
src.swap(*this);
}
ImageData& operator=(ImageData&& src) noexcept
{
src.swap(*this);
return *this;
}
~ImageData()
{
delete[] _pixels;
}
inline unsigned width() const
{
return _width;
}
inline unsigned height() const
{
return _height;
}
2020-08-08 13:09:15 +02:00
uint8_t red(unsigned pixel) const
2020-07-19 16:14:54 +02:00
{
return (_pixels + pixel)->red;
}
2020-08-08 13:09:15 +02:00
uint8_t green(unsigned pixel) const
2020-07-19 16:14:54 +02:00
{
return (_pixels + pixel)->green;
}
2020-08-08 13:09:15 +02:00
uint8_t blue(unsigned pixel) const
2020-07-19 16:14:54 +02:00
{
return (_pixels + pixel)->blue;
}
2020-08-08 13:09:15 +02:00
const Pixel_T& operator()(unsigned x, unsigned y) const
2020-07-19 16:14:54 +02:00
{
return _pixels[toIndex(x,y)];
}
2020-08-08 13:09:15 +02:00
Pixel_T& operator()(unsigned x, unsigned y)
2020-07-19 16:14:54 +02:00
{
return _pixels[toIndex(x,y)];
}
2020-08-08 13:09:15 +02:00
void resize(unsigned width, unsigned height)
2020-07-19 16:14:54 +02:00
{
if (width == _width && height == _height)
return;
if ((width * height) > unsigned((_width * _height)))
{
delete[] _pixels;
_pixels = new Pixel_T[width*height + 1];
}
_width = width;
_height = height;
}
Pixel_T* memptr()
{
return _pixels;
}
const Pixel_T* memptr() const
{
return _pixels;
}
2020-07-27 20:00:36 +02:00
void toRgb(ImageData<ColorRgb>& image) const
2020-07-19 16:14:54 +02:00
{
2020-07-22 16:46:43 +02:00
if (image.width() != _width || image.height() != _height)
image.resize(_width, _height);
2020-07-19 16:14:54 +02:00
const unsigned imageSize = _width * _height;
for (unsigned idx = 0; idx < imageSize; idx++)
{
const Pixel_T & color = _pixels[idx];
image.memptr()[idx] = ColorRgb{color.red, color.green, color.blue};
}
}
ssize_t size() const
{
Various Cleanups (#1075) * LedDevice - Address clang findings * Fix Windows Warnings * Ensure newInput is initialised * Clean-up unused elements for Plaform Capture * Fix initialization problem and spellings * Address clang findings and spelling corrections * LedDevice clean-ups * Cleanups * Align that getLedCount is int * Have "display" as default for Grabbers * Fix config during start-up for missing elements * Framegrabber Clean-up - Remove non supported grabbers from selection, filter valid options * Typo * Framegrabber.json - Fix property numbering * Preselect active Grabbertype * Sort Grabbernames * Align options with selected element * Fix deletion of pointer to incomplete type 'BonjourBrowserWrapper' * Address macOS compile warnings * Have default layout = 1 LED only to avoid errors as in #673 * Address lgtm findings * Address finding that params passed to LedDevice discovery were not considered * Cleanups after merging with latest master * Update Changelog * Address lgtm findings * Fix comment * Test Fix * Fix Python Warning * Handle Dummy Device assignment correctly * Address delete called on non-final 'commandline::Option' that has virtual functions but non-virtual destructor * Correct that QTimer.start accepts only int * Have Release Python GIL & reset threat state chnage downward compatible * Correct format specifier * LedDevice - add assertions * Readonly DB - Fix merge issue * Smoothing - Fix wrong defaults * LedDevice - correct assertion * Show smoothing config set# in debug and related values. * Suppress error on windows, if default file is "/dev/null" * CMAKE - Allow to define QT_BASE_DIR dynamically via environment-variable * Ignore Visual Studio specific files Co-authored-by: Paulchen Panther <16664240+Paulchen-Panther@users.noreply.github.com>
2020-11-14 17:58:56 +01:00
return static_cast<ssize_t>(_width) * static_cast<ssize_t>(_height) * sizeof(Pixel_T);
2020-07-19 16:14:54 +02:00
}
void clear()
{
if (_width != 1 || _height != 1)
{
_width = 1;
_height = 1;
delete[] _pixels;
_pixels = new Pixel_T[2];
}
Various Cleanups (#1075) * LedDevice - Address clang findings * Fix Windows Warnings * Ensure newInput is initialised * Clean-up unused elements for Plaform Capture * Fix initialization problem and spellings * Address clang findings and spelling corrections * LedDevice clean-ups * Cleanups * Align that getLedCount is int * Have "display" as default for Grabbers * Fix config during start-up for missing elements * Framegrabber Clean-up - Remove non supported grabbers from selection, filter valid options * Typo * Framegrabber.json - Fix property numbering * Preselect active Grabbertype * Sort Grabbernames * Align options with selected element * Fix deletion of pointer to incomplete type 'BonjourBrowserWrapper' * Address macOS compile warnings * Have default layout = 1 LED only to avoid errors as in #673 * Address lgtm findings * Address finding that params passed to LedDevice discovery were not considered * Cleanups after merging with latest master * Update Changelog * Address lgtm findings * Fix comment * Test Fix * Fix Python Warning * Handle Dummy Device assignment correctly * Address delete called on non-final 'commandline::Option' that has virtual functions but non-virtual destructor * Correct that QTimer.start accepts only int * Have Release Python GIL & reset threat state chnage downward compatible * Correct format specifier * LedDevice - add assertions * Readonly DB - Fix merge issue * Smoothing - Fix wrong defaults * LedDevice - correct assertion * Show smoothing config set# in debug and related values. * Suppress error on windows, if default file is "/dev/null" * CMAKE - Allow to define QT_BASE_DIR dynamically via environment-variable * Ignore Visual Studio specific files Co-authored-by: Paulchen Panther <16664240+Paulchen-Panther@users.noreply.github.com>
2020-11-14 17:58:56 +01:00
memset(_pixels, 0, static_cast<unsigned long>(_width) * static_cast<unsigned long>(_height) * sizeof(Pixel_T));
2020-07-19 16:14:54 +02:00
}
private:
2020-08-08 13:09:15 +02:00
inline unsigned toIndex(unsigned x, unsigned y) const
2020-07-19 16:14:54 +02:00
{
return y * _width + x;
}
private:
/// The width of the image
unsigned _width;
/// The height of the image
unsigned _height;
/// The pixels of the image
Pixel_T* _pixels;
};