2013-07-26 22:38:34 +02:00
|
|
|
#pragma once
|
|
|
|
|
2020-07-19 16:14:54 +02:00
|
|
|
#include <QSharedDataPointer>
|
2016-02-19 13:31:08 +01:00
|
|
|
|
2020-07-19 16:14:54 +02:00
|
|
|
#include <utils/ImageData.h>
|
2013-07-26 22:38:34 +02:00
|
|
|
|
2013-11-11 10:00:37 +01:00
|
|
|
template <typename Pixel_T>
|
|
|
|
class Image
|
2013-07-26 22:38:34 +02:00
|
|
|
{
|
|
|
|
public:
|
2016-05-26 23:44:27 +02:00
|
|
|
typedef Pixel_T pixel_type;
|
|
|
|
|
|
|
|
Image() :
|
2020-07-19 16:14:54 +02:00
|
|
|
Image(1, 1, Pixel_T())
|
2016-05-26 23:44:27 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Image(const unsigned width, const unsigned height) :
|
2020-07-19 16:14:54 +02:00
|
|
|
Image(width, height, Pixel_T())
|
|
|
|
|
2016-05-26 23:44:27 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
///
|
|
|
|
/// 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))
|
2016-05-26 23:44:27 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
///
|
|
|
|
/// Copy constructor for an image
|
2020-07-19 16:14:54 +02:00
|
|
|
/// @param other The image which will be copied
|
2016-05-26 23:44:27 +02:00
|
|
|
///
|
2020-07-19 16:14:54 +02:00
|
|
|
Image(const Image & other)
|
2016-05-26 23:44:27 +02:00
|
|
|
{
|
2020-07-19 16:14:54 +02:00
|
|
|
_d_ptr = other._d_ptr;
|
2016-05-26 23:44:27 +02:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-05-26 23:44:27 +02:00
|
|
|
///
|
|
|
|
/// 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();
|
2016-05-26 23:44:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
///
|
|
|
|
/// 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();
|
2016-05-26 23:44:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t red(const unsigned pixel) const
|
|
|
|
{
|
2020-07-19 16:14:54 +02:00
|
|
|
return _d_ptr->red(pixel);
|
2016-05-26 23:44:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t green(const unsigned pixel) const
|
|
|
|
{
|
2020-07-19 16:14:54 +02:00
|
|
|
return _d_ptr->green(pixel);
|
2016-05-26 23:44:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
///
|
|
|
|
/// 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
|
2016-05-26 23:44:27 +02:00
|
|
|
{
|
2020-07-19 16:14:54 +02:00
|
|
|
return _d_ptr->blue(pixel);
|
2016-05-26 23:44:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
///
|
|
|
|
/// 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);
|
|
|
|
}
|
|
|
|
|
2016-05-26 23:44:27 +02:00
|
|
|
///
|
|
|
|
/// @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);
|
2016-05-26 23:44:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// 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);
|
2016-05-26 23:44:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
///
|
|
|
|
/// 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();
|
2016-05-26 23:44:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
///
|
|
|
|
/// 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();
|
2016-05-26 23:44:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
///
|
|
|
|
/// Convert image of any color order to a RGB image.
|
|
|
|
///
|
|
|
|
/// @param[out] image The image that buffers the output
|
|
|
|
///
|
|
|
|
void toRgb(Image<ColorRgb>& image)
|
|
|
|
{
|
2020-07-19 16:14:54 +02:00
|
|
|
_d_ptr->toRgb(*(image.imageData()));
|
2016-05-26 23:44:27 +02:00
|
|
|
}
|
2016-02-19 13:31:08 +01:00
|
|
|
|
2017-09-01 08:50:37 +02:00
|
|
|
///
|
2020-07-19 16:14:54 +02:00
|
|
|
/// Get size of buffer
|
|
|
|
///
|
2018-12-30 22:07:53 +01:00
|
|
|
ssize_t size() const
|
2017-09-01 08:50:37 +02:00
|
|
|
{
|
2020-07-19 16:14:54 +02:00
|
|
|
return _d_ptr->size();
|
2017-09-01 08:50:37 +02:00
|
|
|
}
|
|
|
|
|
2020-07-19 16:14:54 +02:00
|
|
|
///
|
2019-08-24 22:53:30 +02:00
|
|
|
/// Clear the image
|
2020-07-19 16:14:54 +02:00
|
|
|
///
|
2019-08-24 22:53:30 +02:00
|
|
|
void clear()
|
|
|
|
{
|
2020-07-19 16:14:54 +02:00
|
|
|
_d_ptr->clear();
|
2019-08-24 22:53:30 +02:00
|
|
|
}
|
|
|
|
|
2020-07-19 16:14:54 +02:00
|
|
|
QSharedDataPointer<ImageData<Pixel_T>> imageData() const
|
|
|
|
{
|
|
|
|
return _d_ptr;
|
|
|
|
}
|
2013-08-02 11:54:09 +02:00
|
|
|
|
2020-07-19 16:14:54 +02:00
|
|
|
private:
|
2016-05-26 23:44:27 +02:00
|
|
|
///
|
|
|
|
/// 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);
|
2016-05-26 23:44:27 +02:00
|
|
|
}
|
2013-08-02 11:54:09 +02:00
|
|
|
|
2013-07-26 22:38:34 +02:00
|
|
|
private:
|
2020-07-19 16:14:54 +02:00
|
|
|
QSharedDataPointer<ImageData<Pixel_T>> _d_ptr;
|
2013-07-26 22:38:34 +02:00
|
|
|
};
|
2020-07-19 16:14:54 +02:00
|
|
|
|