hyperion.ng/include/utils/RgbImage.h

136 lines
2.7 KiB
C
Raw Normal View History

#pragma once
#include <cassert>
#include <cstring>
#include <vector>
// 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.
///
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)
///
RgbImage(const unsigned width, const unsigned height, const RgbColor background = RgbColor::BLACK);
2013-09-09 04:54:13 +02:00
///
/// Destructor
///
~RgbImage();
2013-09-09 04:54:13 +02:00
///
/// Returns the width of the image
///
/// @return The width of the image
///
inline unsigned width() const
{
2013-09-09 04:54:13 +02:00
return _width;
}
2013-09-09 04:54:13 +02:00
///
/// Returns the height of the image
///
/// @return The height of the image
///
inline unsigned height() const
{
2013-09-09 04:54:13 +02:00
return _height;
}
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
///
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
///
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
///
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
///
inline void copy(const RgbImage& other)
{
2013-09-09 04:54:13 +02:00
assert(other._width == _width);
assert(other._height == _height);
2013-09-09 04:54:13 +02:00
memcpy(mColors, other.mColors, _width*_height*sizeof(RgbColor));
}
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
///
RgbColor* memptr()
{
return mColors;
}
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
///
const RgbColor* memptr() const
{
return mColors;
}
private:
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
///
inline unsigned toIndex(const unsigned x, const unsigned y) const
{
2013-09-09 04:54:13 +02:00
return y*_width + x;
}
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;
/** The colors of the image */
RgbColor* mColors;
};