2013-07-26 22:38:34 +02:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <cassert>
|
|
|
|
#include <cstring>
|
2013-08-03 23:24:22 +02:00
|
|
|
#include <vector>
|
2013-07-26 22:38:34 +02:00
|
|
|
|
|
|
|
// Local includes
|
|
|
|
#include "RgbColor.h"
|
|
|
|
|
2013-09-09 04:54:13 +02:00
|
|
|
///
|
|
|
|
/// The RgbImage holds a 2D matrix of RgbColors's (or image). Width and height of the image are
|
|
|
|
/// fixed at construction.
|
|
|
|
///
|
2013-07-26 22:38:34 +02:00
|
|
|
class RgbImage
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
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
|
|
|
|
/// @param background The color of the image (default = BLACK)
|
|
|
|
///
|
2013-07-26 22:38:34 +02:00
|
|
|
RgbImage(const unsigned width, const unsigned height, const RgbColor background = RgbColor::BLACK);
|
|
|
|
|
2013-09-09 04:54:13 +02:00
|
|
|
///
|
|
|
|
/// Destructor
|
|
|
|
///
|
2013-07-26 22:38:34 +02:00
|
|
|
~RgbImage();
|
|
|
|
|
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-09-09 04:54:13 +02:00
|
|
|
///
|
|
|
|
/// Sets the color of a specific pixel in the image
|
|
|
|
///
|
|
|
|
/// @param x The x index
|
|
|
|
/// @param y The y index
|
|
|
|
/// @param color The new color
|
|
|
|
///
|
2013-07-26 22:38:34 +02:00
|
|
|
void setPixel(const unsigned x, const unsigned y, const RgbColor color);
|
|
|
|
|
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-07-26 22:38:34 +02:00
|
|
|
const RgbColor& operator()(const unsigned x, const unsigned y) const;
|
|
|
|
|
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-07-26 22:38:34 +02:00
|
|
|
RgbColor& operator()(const unsigned x, const unsigned y);
|
|
|
|
|
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-07-26 22:38:34 +02:00
|
|
|
inline void copy(const RgbImage& other)
|
|
|
|
{
|
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-09-09 04:54:13 +02:00
|
|
|
memcpy(mColors, other.mColors, _width*_height*sizeof(RgbColor));
|
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-08-03 23:24:22 +02:00
|
|
|
RgbColor* memptr()
|
2013-07-26 22:38:34 +02:00
|
|
|
{
|
2013-08-03 23:24:22 +02:00
|
|
|
return mColors;
|
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-08-14 17:02:09 +02:00
|
|
|
const RgbColor* memptr() const
|
|
|
|
{
|
|
|
|
return mColors;
|
|
|
|
}
|
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
|
|
|
|
const unsigned _width;
|
|
|
|
/// The height of the image
|
|
|
|
const unsigned _height;
|
2013-07-26 22:38:34 +02:00
|
|
|
|
|
|
|
/** The colors of the image */
|
|
|
|
RgbColor* mColors;
|
|
|
|
};
|