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 <cstdint>
#include <cstring>
#include <algorithm>
#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;
2024-01-13 17:04:45 +01:00
ImageData(int width, int height, const Pixel_T background) :
2020-07-19 16:14:54 +02:00
_width(width),
_height(height),
2024-01-13 17:04:45 +01:00
_pixels(new Pixel_T[static_cast<size_t>(width) * static_cast<size_t>(height)])
2020-07-19 16:14:54 +02:00
{
std::fill(_pixels, _pixels + width * height, background);
}
ImageData(const ImageData & other) :
QSharedData(other),
_width(other._width),
_height(other._height),
2024-01-13 17:04:45 +01:00
_pixels(new Pixel_T[static_cast<size_t>(other._width) * static_cast<size_t>(other._height)])
2020-07-19 16:14:54 +02:00
{
2024-01-13 17:04:45 +01:00
memcpy(_pixels, other._pixels, static_cast<size_t>(other._width) * static_cast<size_t>(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;
}
2024-01-13 17:04:45 +01:00
inline int width() const
2020-07-19 16:14:54 +02:00
{
return _width;
}
2024-01-13 17:04:45 +01:00
inline int height() const
2020-07-19 16:14:54 +02:00
{
return _height;
}
2024-01-13 17:04:45 +01:00
uint8_t red(int pixel) const
2020-07-19 16:14:54 +02:00
{
return (_pixels + pixel)->red;
}
2024-01-13 17:04:45 +01:00
uint8_t green(int pixel) const
2020-07-19 16:14:54 +02:00
{
return (_pixels + pixel)->green;
}
2024-01-13 17:04:45 +01:00
uint8_t blue(int pixel) const
2020-07-19 16:14:54 +02:00
{
return (_pixels + pixel)->blue;
}
2024-01-13 17:04:45 +01:00
const Pixel_T& operator()(int x, int y) const
2020-07-19 16:14:54 +02:00
{
return _pixels[toIndex(x,y)];
}
2024-01-13 17:04:45 +01:00
Pixel_T& operator()(int x, int y)
2020-07-19 16:14:54 +02:00
{
return _pixels[toIndex(x,y)];
}
2024-01-13 17:04:45 +01:00
void resize(int width, int height)
2020-07-19 16:14:54 +02:00
{
if (width == _width && height == _height)
{
2024-01-13 17:04:45 +01:00
return;
2020-07-19 16:14:54 +02:00
}
2024-01-13 17:04:45 +01:00
// Allocate a new buffer without initializing the content
Pixel_T* newPixels = new Pixel_T[static_cast<size_t>(width) * static_cast<size_t>(height)];
// Release the old buffer without copying data
delete[] _pixels;
// Update the pointer to the new buffer
_pixels = newPixels;
2020-07-19 16:14:54 +02:00
_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)
2024-01-13 17:04:45 +01:00
{
2020-07-22 16:46:43 +02:00
image.resize(_width, _height);
2024-01-13 17:04:45 +01:00
}
2020-07-22 16:46:43 +02:00
2024-01-13 17:04:45 +01:00
const int imageSize = _width * _height;
2020-07-19 16:14:54 +02:00
2024-01-13 17:04:45 +01:00
for (int idx = 0; idx < imageSize; idx++)
2020-07-19 16:14:54 +02:00
{
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)
{
2024-01-13 17:04:45 +01:00
resize(1,1);
2020-07-19 16:14:54 +02:00
}
2024-01-13 17:04:45 +01:00
// Set the single pixel to the default background
_pixels[0] = Pixel_T();
2020-07-19 16:14:54 +02:00
}
private:
2024-01-13 17:04:45 +01:00
inline int toIndex(int x, int y) const
2020-07-19 16:14:54 +02:00
{
return y * _width + x;
}
/// The width of the image
2024-01-13 17:04:45 +01:00
int _width;
2020-07-19 16:14:54 +02:00
/// The height of the image
2024-01-13 17:04:45 +01:00
int _height;
2020-07-19 16:14:54 +02:00
/// The pixels of the image
Pixel_T* _pixels;
};