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>
|
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:
|
|
|
|
|
2013-11-11 10:00:37 +01:00
|
|
|
typedef Pixel_T pixel_type;
|
|
|
|
|
|
|
|
///
|
|
|
|
/// 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));
|
|
|
|
}
|
|
|
|
|
2013-09-09 04:54:13 +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
|
2013-11-11 10:00:37 +01:00
|
|
|
/// @param background The color of the image
|
2013-09-09 04:54:13 +02:00
|
|
|
///
|
2013-11-11 10:00:37 +01:00
|
|
|
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);
|
|
|
|
}
|
2013-07-26 22:38:34 +02:00
|
|
|
|
2013-09-09 04:54:13 +02:00
|
|
|
///
|
|
|
|
/// Destructor
|
|
|
|
///
|
2013-11-11 10:00:37 +01:00
|
|
|
~Image()
|
|
|
|
{
|
|
|
|
delete[] _pixels;
|
|
|
|
}
|
2013-07-26 22:38:34 +02:00
|
|
|
|
2013-09-09 04:54:13 +02:00
|
|
|
///
|
|
|
|
/// Returns the width of the image
|
|
|
|
///
|
|
|
|
/// @return The width of the image
|
|
|
|
///
|
2013-07-26 22:38:34 +02:00
|
|
|
inline unsigned width() const
|
|
|
|
{
|
2013-09-09 04:54:13 +02:00
|
|
|
return _width;
|
2013-07-26 22:38:34 +02:00
|
|
|
}
|
|
|
|
|
2013-09-09 04:54:13 +02:00
|
|
|
///
|
|
|
|
/// Returns the height of the image
|
|
|
|
///
|
|
|
|
/// @return The height of the image
|
|
|
|
///
|
2013-07-26 22:38:34 +02:00
|
|
|
inline unsigned height() const
|
|
|
|
{
|
2013-09-09 04:54:13 +02:00
|
|
|
return _height;
|
2013-07-26 22:38:34 +02:00
|
|
|
}
|
|
|
|
|
2013-11-11 10:00:37 +01:00
|
|
|
uint8_t alpha(const unsigned pixel) const
|
|
|
|
{
|
|
|
|
return (_pixels + pixel)->red;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2013-07-26 22:38:34 +02:00
|
|
|
|
2013-09-09 04:54:13 +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
|
|
|
|
///
|
2013-11-11 10:00:37 +01:00
|
|
|
const Pixel_T& operator()(const unsigned x, const unsigned y) const
|
|
|
|
{
|
|
|
|
return _pixels[toIndex(x,y)];
|
|
|
|
}
|
2013-07-26 22:38:34 +02:00
|
|
|
|
2013-09-09 04:54:13 +02:00
|
|
|
///
|
|
|
|
/// 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
|
|
|
|
///
|
2013-11-11 10:00:37 +01:00
|
|
|
Pixel_T& operator()(const unsigned x, const unsigned y)
|
|
|
|
{
|
|
|
|
return _pixels[toIndex(x,y)];
|
|
|
|
}
|
2013-07-26 22:38:34 +02:00
|
|
|
|
2013-12-21 14:32:30 +01: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)
|
|
|
|
{
|
|
|
|
if ((width*height) > (_endOfPixels-_pixels))
|
|
|
|
{
|
|
|
|
delete[] _pixels;
|
|
|
|
_pixels = new Pixel_T[width*height + 1];
|
|
|
|
_endOfPixels = _pixels + width*height;
|
|
|
|
}
|
|
|
|
|
|
|
|
_width = width;
|
|
|
|
_height = height;
|
|
|
|
}
|
|
|
|
|
2013-09-09 04:54:13 +02:00
|
|
|
///
|
|
|
|
/// Copies another image into this image. The images should have exactly the same size.
|
|
|
|
///
|
|
|
|
/// @param other The image to copy into this
|
|
|
|
///
|
2013-11-11 10:00:37 +01:00
|
|
|
void copy(const Image<Pixel_T>& other)
|
2013-07-26 22:38:34 +02:00
|
|
|
{
|
2013-09-09 04:54:13 +02:00
|
|
|
assert(other._width == _width);
|
|
|
|
assert(other._height == _height);
|
2013-07-26 22:38:34 +02:00
|
|
|
|
2013-11-11 10:00:37 +01:00
|
|
|
memcpy(_pixels, other._pixels, _width*_height*sizeof(Pixel_T));
|
2013-07-26 22:38:34 +02:00
|
|
|
}
|
|
|
|
|
2013-09-09 04:54:13 +02:00
|
|
|
///
|
|
|
|
/// Returns a memory pointer to the first pixel in the image
|
|
|
|
/// @return The memory pointer to the first pixel
|
|
|
|
///
|
2013-11-11 10:00:37 +01:00
|
|
|
Pixel_T* memptr()
|
2013-07-26 22:38:34 +02:00
|
|
|
{
|
2013-11-11 10:00:37 +01:00
|
|
|
return _pixels;
|
2013-07-26 22:38:34 +02:00
|
|
|
}
|
|
|
|
|
2013-09-09 04:54:13 +02:00
|
|
|
///
|
|
|
|
/// Returns a const memory pointer to the first pixel in the image
|
|
|
|
/// @return The const memory pointer to the first pixel
|
|
|
|
///
|
2013-11-11 10:00:37 +01:00
|
|
|
const Pixel_T* memptr() const
|
2013-08-14 17:02:09 +02:00
|
|
|
{
|
2013-11-11 10:00:37 +01:00
|
|
|
return _pixels;
|
2013-08-14 17:02:09 +02:00
|
|
|
}
|
2013-08-03 23:24:22 +02:00
|
|
|
private:
|
2013-08-02 11:54:09 +02:00
|
|
|
|
2013-09-09 04:54:13 +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
|
|
|
|
///
|
2013-08-03 23:24:22 +02:00
|
|
|
inline unsigned toIndex(const unsigned x, const unsigned y) const
|
2013-08-02 11:54:09 +02:00
|
|
|
{
|
2013-09-09 04:54:13 +02:00
|
|
|
return y*_width + x;
|
2013-08-02 11:54:09 +02:00
|
|
|
}
|
|
|
|
|
2013-07-26 22:38:34 +02:00
|
|
|
private:
|
2013-09-09 04:54:13 +02:00
|
|
|
/// The width of the image
|
2013-12-21 14:32:30 +01:00
|
|
|
unsigned _width;
|
2013-09-09 04:54:13 +02:00
|
|
|
/// The height of the image
|
2013-12-21 14:32:30 +01:00
|
|
|
unsigned _height;
|
2013-07-26 22:38:34 +02:00
|
|
|
|
2013-11-11 10:00:37 +01:00
|
|
|
/// The pixels of the image
|
|
|
|
Pixel_T* _pixels;
|
|
|
|
|
|
|
|
/// Pointer to the last(extra) pixel
|
|
|
|
Pixel_T* _endOfPixels;
|
2013-07-26 22:38:34 +02:00
|
|
|
};
|