2013-07-26 22:38:34 +02:00
|
|
|
#pragma once
|
|
|
|
|
2013-11-11 10:00:37 +01:00
|
|
|
// STL includes
|
2013-08-03 23:24:22 +02:00
|
|
|
#include <vector>
|
2013-11-11 10:00:37 +01:00
|
|
|
#include <cstdint>
|
|
|
|
#include <cstring>
|
|
|
|
#include <algorithm>
|
2017-08-01 17:29:47 +02:00
|
|
|
#include <cassert>
|
2016-02-19 13:31:08 +01:00
|
|
|
#include <utils/ColorRgb.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;
|
|
|
|
|
|
|
|
///
|
|
|
|
/// Default constructor for an image
|
|
|
|
///
|
|
|
|
Image() :
|
|
|
|
_width(1),
|
|
|
|
_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) :
|
|
|
|
_width(width),
|
|
|
|
_height(height),
|
|
|
|
_pixels(new Pixel_T[width * height + 1]),
|
|
|
|
_endOfPixels(_pixels + width * height)
|
|
|
|
{
|
|
|
|
memset(_pixels, 0, (_width*_height+1)*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
|
|
|
|
/// @param background The color of the image
|
|
|
|
///
|
|
|
|
Image(const unsigned width, const unsigned height, const Pixel_T background) :
|
|
|
|
_width(width),
|
|
|
|
_height(height),
|
|
|
|
_pixels(new Pixel_T[width * height + 1]),
|
|
|
|
_endOfPixels(_pixels + width * height)
|
|
|
|
{
|
|
|
|
std::fill(_pixels, _endOfPixels, background);
|
|
|
|
}
|
|
|
|
|
|
|
|
///
|
|
|
|
/// Copy constructor for an image
|
|
|
|
///
|
|
|
|
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)
|
|
|
|
{
|
2019-06-05 18:19:08 +02:00
|
|
|
memcpy(_pixels, other._pixels, (long) other._width * other._height * sizeof(Pixel_T));
|
2016-05-26 23:44:27 +02:00
|
|
|
}
|
|
|
|
|
2018-12-27 23:11:32 +01: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
|
|
|
|
Image& operator=(Image rhs)
|
|
|
|
{
|
|
|
|
rhs.swap(*this);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void swap(Image& s) noexcept
|
|
|
|
{
|
|
|
|
using std::swap;
|
|
|
|
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
|
|
|
|
: _width(0)
|
|
|
|
, _height(0)
|
|
|
|
, _pixels(NULL)
|
|
|
|
, _endOfPixels(NULL)
|
|
|
|
{
|
|
|
|
src.swap(*this);
|
|
|
|
}
|
|
|
|
Image& operator=(Image&& src) noexcept
|
|
|
|
{
|
|
|
|
src.swap(*this);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2016-05-26 23:44:27 +02:00
|
|
|
///
|
|
|
|
/// Destructor
|
|
|
|
///
|
|
|
|
~Image()
|
|
|
|
{
|
|
|
|
delete[] _pixels;
|
|
|
|
}
|
|
|
|
|
|
|
|
///
|
|
|
|
/// Returns the width of the image
|
|
|
|
///
|
|
|
|
/// @return The width of the image
|
|
|
|
///
|
|
|
|
inline unsigned width() const
|
|
|
|
{
|
|
|
|
return _width;
|
|
|
|
}
|
|
|
|
|
|
|
|
///
|
|
|
|
/// Returns the height of the image
|
|
|
|
///
|
|
|
|
/// @return The height of the image
|
|
|
|
///
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
///
|
|
|
|
/// 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
|
|
|
|
///
|
|
|
|
const Pixel_T& operator()(const unsigned x, const unsigned y) const
|
|
|
|
{
|
|
|
|
return _pixels[toIndex(x,y)];
|
|
|
|
}
|
|
|
|
|
|
|
|
///
|
|
|
|
/// Returns a reference to a specified pixel in the image
|
|
|
|
///
|
|
|
|
/// @param x The x index
|
|
|
|
/// @param y The y index
|
|
|
|
///
|
|
|
|
/// @return reference to specified pixel
|
|
|
|
///
|
|
|
|
Pixel_T& operator()(const unsigned x, const unsigned y)
|
|
|
|
{
|
|
|
|
return _pixels[toIndex(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)
|
|
|
|
{
|
|
|
|
if ((width*height) > unsigned((_endOfPixels-_pixels)))
|
|
|
|
{
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
|
|
|
///
|
|
|
|
/// Returns a memory pointer to the first pixel in the image
|
|
|
|
/// @return The memory pointer to the first pixel
|
|
|
|
///
|
|
|
|
Pixel_T* memptr()
|
|
|
|
{
|
|
|
|
return _pixels;
|
|
|
|
}
|
|
|
|
|
|
|
|
///
|
|
|
|
/// 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
|
|
|
|
{
|
|
|
|
return _pixels;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///
|
|
|
|
/// Convert image of any color order to a RGB image.
|
|
|
|
///
|
|
|
|
/// @param[out] image The image that buffers the output
|
|
|
|
///
|
|
|
|
void toRgb(Image<ColorRgb>& image)
|
|
|
|
{
|
|
|
|
image.resize(_width, _height);
|
|
|
|
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};
|
|
|
|
}
|
|
|
|
}
|
2016-02-19 13:31:08 +01:00
|
|
|
|
2017-09-01 08:50:37 +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
|
|
|
{
|
2019-06-05 18:19:08 +02:00
|
|
|
return (ssize_t) _width * _height * sizeof(Pixel_T);
|
2017-09-01 08:50:37 +02:00
|
|
|
}
|
|
|
|
|
2013-08-03 23:24:22 +02:00
|
|
|
private:
|
2013-08-02 11:54:09 +02:00
|
|
|
|
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
|
|
|
|
{
|
|
|
|
return y*_width + x;
|
|
|
|
}
|
2013-08-02 11:54:09 +02:00
|
|
|
|
2013-07-26 22:38:34 +02:00
|
|
|
private:
|
2016-05-26 23:44:27 +02:00
|
|
|
/// The width of the image
|
|
|
|
unsigned _width;
|
|
|
|
/// The height of the image
|
|
|
|
unsigned _height;
|
2013-07-26 22:38:34 +02:00
|
|
|
|
2016-05-26 23:44:27 +02:00
|
|
|
/// The pixels of the image
|
|
|
|
Pixel_T* _pixels;
|
2013-11-11 10:00:37 +01:00
|
|
|
|
2016-05-26 23:44:27 +02:00
|
|
|
/// Pointer to the last(extra) pixel
|
|
|
|
Pixel_T* _endOfPixels;
|
2013-07-26 22:38:34 +02:00
|
|
|
};
|