hyperion.ng/include/utils/Image.h

210 lines
3.6 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())
{
}
2020-08-08 13:09:15 +02:00
Image(unsigned width, 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
///
2020-08-08 13:09:15 +02:00
Image(unsigned width, 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();
}
2020-08-08 13:09:15 +02:00
uint8_t red(unsigned pixel) const
{
2020-07-19 16:14:54 +02:00
return _d_ptr->red(pixel);
}
2020-08-08 13:09:15 +02:00
uint8_t green(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-08-08 13:09:15 +02:00
uint8_t blue(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-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 _d_ptr->operator()(x, y);
}
///
/// @return reference to specified pixel
///
2020-08-08 13:09:15 +02:00
Pixel_T& operator()(unsigned x, 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
2020-08-08 13:09:15 +02:00
void resize(unsigned width, 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
///
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 _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