hyperion.ng/include/utils/Image.h

210 lines
3.7 KiB
C
Raw Normal View History

#pragma once
2020-07-19 16:14:54 +02:00
#include <QSharedDataPointer>
2020-07-19 16:14:54 +02:00
#include <utils/ImageData.h>
template <typename Pixel_T>
class Image
{
public:
typedef Pixel_T pixel_type;
Image() :
2020-07-19 16:14:54 +02:00
Image(1, 1, Pixel_T())
{
}
Image(const unsigned width, const unsigned height) :
2020-07-19 16:14:54 +02:00
Image(width, height, 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) :
2020-07-19 16:14:54 +02:00
_d_ptr(new ImageData<Pixel_T>(width, height, background))
{
}
///
/// Copy constructor for an image
2020-07-19 16:14:54 +02:00
/// @param other The image which will be copied
///
2020-07-19 16:14:54 +02:00
Image(const Image & other)
{
2020-07-19 16:14:54 +02:00
_d_ptr = other._d_ptr;
}
2018-12-27 23:11:32 +01:00
Image& operator=(Image rhs)
{
2020-07-19 16:14:54 +02:00
// 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;
2018-12-27 23:11:32 +01:00
return *this;
}
2020-07-19 16:14:54 +02:00
void swap(Image& s)
2018-12-27 23:11:32 +01:00
{
2020-07-19 16:14:54 +02:00
std::swap(this->_d_ptr, s._d_ptr);
2018-12-27 23:11:32 +01:00
}
Image(Image&& src) noexcept
{
2020-07-19 16:14:54 +02:00
std::swap(this->_d_ptr, src._d_ptr);
2018-12-27 23:11:32 +01:00
}
2020-07-19 16:14:54 +02:00
2018-12-27 23:11:32 +01:00
Image& operator=(Image&& src) noexcept
{
src.swap(*this);
return *this;
}
///
/// Destructor
///
~Image()
{
}
///
/// Returns the width of the image
///
/// @return The width of the image
///
inline unsigned width() const
{
2020-07-19 16:14:54 +02:00
return _d_ptr->width();
}
///
/// Returns the height of the image
///
/// @return The height of the image
///
inline unsigned height() const
{
2020-07-19 16:14:54 +02:00
return _d_ptr->height();
}
uint8_t red(const unsigned pixel) const
{
2020-07-19 16:14:54 +02:00
return _d_ptr->red(pixel);
}
uint8_t green(const unsigned pixel) const
{
2020-07-19 16:14:54 +02:00
return _d_ptr->green(pixel);
}
///
/// 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
///
2020-07-19 16:14:54 +02:00
uint8_t blue(const unsigned pixel) const
{
2020-07-19 16:14:54 +02:00
return _d_ptr->blue(pixel);
}
///
/// Returns a reference to a specified pixel in the image
///
/// @param x The x index
/// @param y The y index
2020-07-19 16:14:54 +02:00
const Pixel_T& operator()(const unsigned x, const unsigned y) const
{
return _d_ptr->operator()(x, y);
}
///
/// @return reference to specified pixel
///
Pixel_T& operator()(const unsigned x, const unsigned y)
{
2020-07-19 16:14:54 +02:00
return _d_ptr->operator()(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)
{
2020-07-19 16:14:54 +02:00
_d_ptr->resize(width, height);
}
///
/// Returns a memory pointer to the first pixel in the image
/// @return The memory pointer to the first pixel
///
Pixel_T* memptr()
{
2020-07-19 16:14:54 +02:00
return _d_ptr->memptr();
}
///
/// 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
{
2020-07-19 16:14:54 +02:00
return _d_ptr->memptr();
}
///
/// Convert image of any color order to a RGB image.
///
/// @param[out] image The image that buffers the output
///
2020-07-27 20:00:36 +02:00
void toRgb(Image<ColorRgb>& image) const
{
2020-07-27 20:00:36 +02:00
_d_ptr->toRgb(*image._d_ptr);
}
///
2020-07-19 16:14:54 +02:00
/// Get size of buffer
///
2018-12-30 22:07:53 +01:00
ssize_t size() const
{
2020-07-19 16:14:54 +02:00
return _d_ptr->size();
}
2020-07-19 16:14:54 +02:00
///
/// Clear the image
2020-07-19 16:14:54 +02:00
///
void clear()
{
2020-07-19 16:14:54 +02:00
_d_ptr->clear();
}
2020-07-19 16:14:54 +02:00
private:
2020-07-27 20:00:36 +02:00
template<class T>
friend class Image;
///
/// 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
{
2020-07-19 16:14:54 +02:00
return _d_ptr->toIndex(x, y);
}
private:
2020-07-19 16:14:54 +02:00
QSharedDataPointer<ImageData<Pixel_T>> _d_ptr;
};
2020-07-19 16:14:54 +02:00