Some optimizations (#880)

This commit is contained in:
Murat Seker 2020-07-19 16:14:54 +02:00 committed by GitHub
parent 4880e31562
commit 95688c0f1e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 230 additions and 130 deletions

View File

@ -1,51 +1,24 @@
#pragma once #pragma once
// STL includes #include <QSharedDataPointer>
#include <vector>
#include <cstdint>
#include <cstring>
#include <algorithm>
#include <cassert>
#include <utils/ColorRgb.h>
// https://docs.microsoft.com/en-us/windows/win32/winprog/windows-data-types#ssize-t #include <utils/ImageData.h>
#if defined(_MSC_VER)
#include <BaseTsd.h>
typedef SSIZE_T ssize_t;
#endif
template <typename Pixel_T> template <typename Pixel_T>
class Image class Image
{ {
public: public:
typedef Pixel_T pixel_type; typedef Pixel_T pixel_type;
///
/// Default constructor for an image
///
Image() : Image() :
_width(1), Image(1, 1, Pixel_T())
_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) : Image(const unsigned width, const unsigned height) :
_width(width), Image(width, height, Pixel_T())
_height(height),
_pixels(new Pixel_T[width * height + 1]),
_endOfPixels(_pixels + width * height)
{ {
memset(_pixels, 0, (_width*_height+1)*sizeof(Pixel_T));
} }
/// ///
@ -56,52 +29,37 @@ public:
/// @param background The color of the image /// @param background The color of the image
/// ///
Image(const unsigned width, const unsigned height, const Pixel_T background) : Image(const unsigned width, const unsigned height, const Pixel_T background) :
_width(width), _d_ptr(new ImageData<Pixel_T>(width, height, background))
_height(height),
_pixels(new Pixel_T[width * height + 1]),
_endOfPixels(_pixels + width * height)
{ {
std::fill(_pixels, _endOfPixels, background);
} }
/// ///
/// Copy constructor for an image /// Copy constructor for an image
/// @param other The image which will be copied
/// ///
Image(const Image & other) : 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, (long) other._width * other._height * sizeof(Pixel_T)); _d_ptr = other._d_ptr;
} }
// Define assignment operator in terms of the copy constructor
// More to read: https://stackoverflow.com/questions/255612/dynamically-allocating-an-array-of-objects?answertab=active#tab-top
Image& operator=(Image rhs) Image& operator=(Image rhs)
{ {
rhs.swap(*this); // Define assignment operator in terms of the copy constructor
// More to read: https://stackoverflow.com/questions/255612/dynamically-allocating-an-array-of-objects?answertab=active#tab-top
_d_ptr = rhs._d_ptr;
return *this; return *this;
} }
void swap(Image& s) noexcept void swap(Image& s)
{ {
using std::swap; std::swap(this->_d_ptr, s._d_ptr);
swap(this->_width, s._width);
swap(this->_height, s._height);
swap(this->_pixels, s._pixels);
swap(this->_endOfPixels, s._endOfPixels);
} }
// C++11
Image(Image&& src) noexcept Image(Image&& src) noexcept
: _width(0)
, _height(0)
, _pixels(NULL)
, _endOfPixels(NULL)
{ {
src.swap(*this); std::swap(this->_d_ptr, src._d_ptr);
} }
Image& operator=(Image&& src) noexcept Image& operator=(Image&& src) noexcept
{ {
src.swap(*this); src.swap(*this);
@ -113,7 +71,6 @@ public:
/// ///
~Image() ~Image()
{ {
delete[] _pixels;
} }
/// ///
@ -123,7 +80,7 @@ public:
/// ///
inline unsigned width() const inline unsigned width() const
{ {
return _width; return _d_ptr->width();
} }
/// ///
@ -133,22 +90,17 @@ public:
/// ///
inline unsigned height() const inline unsigned height() const
{ {
return _height; return _d_ptr->height();
} }
uint8_t red(const unsigned pixel) const uint8_t red(const unsigned pixel) const
{ {
return (_pixels + pixel)->red; return _d_ptr->red(pixel);
} }
uint8_t green(const unsigned pixel) const uint8_t green(const unsigned pixel) const
{ {
return (_pixels + pixel)->green; return _d_ptr->green(pixel);
}
uint8_t blue(const unsigned pixel) const
{
return (_pixels + pixel)->blue;
} }
/// ///
@ -159,9 +111,9 @@ public:
/// ///
/// @return const reference to specified pixel /// @return const reference to specified pixel
/// ///
const Pixel_T& operator()(const unsigned x, const unsigned y) const uint8_t blue(const unsigned pixel) const
{ {
return _pixels[toIndex(x,y)]; return _d_ptr->blue(pixel);
} }
/// ///
@ -169,12 +121,17 @@ public:
/// ///
/// @param x The x index /// @param x The x index
/// @param y The y index /// @param y The y index
const Pixel_T& operator()(const unsigned x, const unsigned y) const
{
return _d_ptr->operator()(x, y);
}
/// ///
/// @return reference to specified pixel /// @return reference to specified pixel
/// ///
Pixel_T& operator()(const unsigned x, const unsigned y) Pixel_T& operator()(const unsigned x, const unsigned y)
{ {
return _pixels[toIndex(x,y)]; return _d_ptr->operator()(x, y);
} }
/// Resize the image /// Resize the image
@ -182,28 +139,7 @@ public:
/// @param height The height of the image /// @param height The height of the image
void resize(const unsigned width, const unsigned height) void resize(const unsigned width, const unsigned height)
{ {
if ((width*height) > unsigned((_endOfPixels-_pixels))) _d_ptr->resize(width, height);
{
delete[] _pixels;
_pixels = new Pixel_T[width*height + 1];
_endOfPixels = _pixels + width*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);
memcpy(_pixels, other._pixels, _width * _height * sizeof(Pixel_T));
} }
/// ///
@ -212,7 +148,7 @@ public:
/// ///
Pixel_T* memptr() Pixel_T* memptr()
{ {
return _pixels; return _d_ptr->memptr();
} }
/// ///
@ -221,10 +157,9 @@ public:
/// ///
const Pixel_T* memptr() const const Pixel_T* memptr() const
{ {
return _pixels; return _d_ptr->memptr();
} }
/// ///
/// Convert image of any color order to a RGB image. /// Convert image of any color order to a RGB image.
/// ///
@ -232,38 +167,31 @@ public:
/// ///
void toRgb(Image<ColorRgb>& image) void toRgb(Image<ColorRgb>& image)
{ {
image.resize(_width, _height); _d_ptr->toRgb(*(image.imageData()));
const unsigned imageSize = _width * _height;
for (unsigned idx=0; idx<imageSize; idx++)
{
const Pixel_T color = memptr()[idx];
image.memptr()[idx] = ColorRgb{color.red, color.green, color.blue};
}
} }
/// ///
/// get size of buffer /// Get size of buffer
// ///
ssize_t size() const ssize_t size() const
{ {
return (ssize_t) _width * _height * sizeof(Pixel_T); return _d_ptr->size();
} }
///
/// Clear the image /// Clear the image
// ///
void clear() void clear()
{ {
_width = 1; _d_ptr->clear();
_height = 1; }
delete[] _pixels;
_pixels = new Pixel_T[2]; QSharedDataPointer<ImageData<Pixel_T>> imageData() const
_endOfPixels = _pixels + 1; {
memset(_pixels, 0, (unsigned long) _width * _height * sizeof(Pixel_T)); return _d_ptr;
} }
private: private:
/// ///
/// Translate x and y coordinate to index of the underlying vector /// Translate x and y coordinate to index of the underlying vector
/// ///
@ -274,18 +202,10 @@ private:
/// ///
inline unsigned toIndex(const unsigned x, const unsigned y) const inline unsigned toIndex(const unsigned x, const unsigned y) const
{ {
return y*_width + x; return _d_ptr->toIndex(x, y);
} }
private: private:
/// The width of the image QSharedDataPointer<ImageData<Pixel_T>> _d_ptr;
unsigned _width;
/// The height of the image
unsigned _height;
/// The pixels of the image
Pixel_T* _pixels;
/// Pointer to the last(extra) pixel
Pixel_T* _endOfPixels;
}; };

180
include/utils/ImageData.h Normal file
View File

@ -0,0 +1,180 @@
#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;
ImageData(const unsigned width, const unsigned height, const Pixel_T background) :
_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])
{
memcpy(_pixels, other._pixels, (long) other._width * other._height * sizeof(Pixel_T));
}
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;
}
uint8_t red(const unsigned pixel) const
{
return (_pixels + pixel)->red;
}
uint8_t green(const unsigned pixel) const
{
return (_pixels + pixel)->green;
}
uint8_t blue(const unsigned pixel) const
{
return (_pixels + pixel)->blue;
}
const Pixel_T& operator()(const unsigned x, const unsigned y) const
{
return _pixels[toIndex(x,y)];
}
Pixel_T& operator()(const unsigned x, const unsigned y)
{
return _pixels[toIndex(x,y)];
}
void resize(const unsigned width, const unsigned height)
{
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;
}
void toRgb(ImageData<ColorRgb>& image)
{
image.resize(_width, _height);
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
{
return (ssize_t) _width * _height * sizeof(Pixel_T);
}
void clear()
{
if (_width != 1 || _height != 1)
{
_width = 1;
_height = 1;
delete[] _pixels;
_pixels = new Pixel_T[2];
}
memset(_pixels, 0, (unsigned long) _width * _height * sizeof(Pixel_T));
}
private:
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 pixels of the image
Pixel_T* _pixels;
};

View File

@ -39,6 +39,7 @@
// Boblight // Boblight
#include <boblightserver/BoblightServer.h> #include <boblightserver/BoblightServer.h>
Hyperion::Hyperion(const quint8& instance) Hyperion::Hyperion(const quint8& instance)
: QObject() : QObject()
, _instIndex(instance) , _instIndex(instance)

View File

@ -60,7 +60,6 @@ void ImageResampler::processImage(const uint8_t * data, int width, int height, i
// calculate the output size // calculate the output size
int outputWidth = (width - _cropLeft - cropRight - (_horizontalDecimation >> 1) + _horizontalDecimation - 1) / _horizontalDecimation; int outputWidth = (width - _cropLeft - cropRight - (_horizontalDecimation >> 1) + _horizontalDecimation - 1) / _horizontalDecimation;
int outputHeight = (height - _cropTop - cropBottom - (_verticalDecimation >> 1) + _verticalDecimation - 1) / _verticalDecimation; int outputHeight = (height - _cropTop - cropBottom - (_verticalDecimation >> 1) + _verticalDecimation - 1) / _verticalDecimation;
if ((outputImage.height() != unsigned(outputHeight)) && (outputImage.width() != unsigned(outputWidth)))
outputImage.resize(outputWidth, outputHeight); outputImage.resize(outputWidth, outputHeight);
for (int yDest = 0, ySource = _cropTop + (_verticalDecimation >> 1); yDest < outputHeight; ySource += _verticalDecimation, ++yDest) for (int yDest = 0, ySource = _cropTop + (_verticalDecimation >> 1); yDest < outputHeight; ySource += _verticalDecimation, ++yDest)