mirror of
https://github.com/hyperion-project/hyperion.ng.git
synced 2025-03-01 10:33:28 +00:00
Changed RgbImage to template based Image (with template for pixel type)
Former-commit-id: ef02f164eaf3c2f9dd552c1c17b525cf6eed499c
This commit is contained in:
52
include/utils/ColorArgb.h
Normal file
52
include/utils/ColorArgb.h
Normal file
@@ -0,0 +1,52 @@
|
||||
#pragma once
|
||||
|
||||
// STL includes
|
||||
#include <cstdint>
|
||||
#include <ostream>
|
||||
|
||||
struct ColorArgb;
|
||||
|
||||
struct ColorArgb
|
||||
{
|
||||
|
||||
/// The alpha mask channel
|
||||
uint8_t alpha;
|
||||
|
||||
/// The red color channel
|
||||
uint8_t red;
|
||||
/// The green color channel
|
||||
uint8_t green;
|
||||
/// The blue color channel
|
||||
uint8_t blue;
|
||||
|
||||
/// 'Black' RgbColor (255, 0, 0, 0)
|
||||
static ColorArgb BLACK;
|
||||
/// 'Red' RgbColor (255, 255, 0, 0)
|
||||
static ColorArgb RED;
|
||||
/// 'Green' RgbColor (255, 0, 255, 0)
|
||||
static ColorArgb GREEN;
|
||||
/// 'Blue' RgbColor (255, 0, 0, 255)
|
||||
static ColorArgb BLUE;
|
||||
/// 'Yellow' RgbColor (255, 255, 255, 0)
|
||||
static ColorArgb YELLOW;
|
||||
/// 'White' RgbColor (255, 255, 255, 255)
|
||||
static ColorArgb WHITE;
|
||||
};
|
||||
|
||||
|
||||
/// Assert to ensure that the size of the structure is 'only' 3 bytes
|
||||
static_assert(sizeof(ColorArgb) == 4, "Incorrect size of ColorARGB");
|
||||
|
||||
///
|
||||
/// Stream operator to write ColorRgb to an outputstream (format "'{'[alpha]', '[red]','[green]','[blue]'}'")
|
||||
///
|
||||
/// @param os The output stream
|
||||
/// @param color The color to write
|
||||
/// @return The output stream (with the color written to it)
|
||||
///
|
||||
inline std::ostream& operator<<(std::ostream& os, const ColorArgb& color)
|
||||
{
|
||||
os << "{" << unsigned(color.alpha) << "," << unsigned(color.red) << "," << unsigned(color.green) << "," << unsigned(color.blue) << "}";
|
||||
return os;
|
||||
}
|
||||
|
@@ -1,18 +1,16 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
// STL includes
|
||||
#include <stdint.h>
|
||||
#include <cstdint>
|
||||
#include <iostream>
|
||||
|
||||
// Forward class declaration
|
||||
struct RgbColor;
|
||||
struct ColorRgb;
|
||||
|
||||
///
|
||||
/// Plain-Old-Data structure containing the red-green-blue color specification. Size of the
|
||||
/// structure is exactly 3-bytes for easy writing to led-device
|
||||
///
|
||||
struct RgbColor
|
||||
struct ColorRgb
|
||||
{
|
||||
/// The red color channel
|
||||
uint8_t red;
|
||||
@@ -22,42 +20,30 @@ struct RgbColor
|
||||
uint8_t blue;
|
||||
|
||||
/// 'Black' RgbColor (0, 0, 0)
|
||||
static RgbColor BLACK;
|
||||
static ColorRgb BLACK;
|
||||
/// 'Red' RgbColor (255, 0, 0)
|
||||
static RgbColor RED;
|
||||
static ColorRgb RED;
|
||||
/// 'Green' RgbColor (0, 255, 0)
|
||||
static RgbColor GREEN;
|
||||
static ColorRgb GREEN;
|
||||
/// 'Blue' RgbColor (0, 0, 255)
|
||||
static RgbColor BLUE;
|
||||
static ColorRgb BLUE;
|
||||
/// 'Yellow' RgbColor (255, 255, 0)
|
||||
static RgbColor YELLOW;
|
||||
static ColorRgb YELLOW;
|
||||
/// 'White' RgbColor (255, 255, 255)
|
||||
static RgbColor WHITE;
|
||||
|
||||
///
|
||||
/// Checks is this exactly matches another color
|
||||
///
|
||||
/// @param other The other color
|
||||
///
|
||||
/// @return True if the colors are identical
|
||||
///
|
||||
inline bool operator==(const RgbColor& other) const
|
||||
{
|
||||
return red == other.red && green == other.green && blue == other.blue;
|
||||
}
|
||||
static ColorRgb WHITE;
|
||||
};
|
||||
|
||||
/// Assert to ensure that the size of the structure is 'only' 3 bytes
|
||||
static_assert(sizeof(RgbColor) == 3, "Incorrect size of RgbColor");
|
||||
static_assert(sizeof(ColorRgb) == 3, "Incorrect size of ColorRgb");
|
||||
|
||||
///
|
||||
/// Stream operator to write RgbColor to an outputstream (format "'{'[red]','[green]','[blue]'}'")
|
||||
/// Stream operator to write ColorRgb to an outputstream (format "'{'[red]','[green]','[blue]'}'")
|
||||
///
|
||||
/// @param os The output stream
|
||||
/// @param color The color to write
|
||||
/// @return The output stream (with the color written to it)
|
||||
///
|
||||
inline std::ostream& operator<<(std::ostream& os, const RgbColor& color)
|
||||
inline std::ostream& operator<<(std::ostream& os, const ColorRgb& color)
|
||||
{
|
||||
os << "{" << unsigned(color.red) << "," << unsigned(color.green) << "," << unsigned(color.blue) << "}";
|
||||
return os;
|
52
include/utils/ColorRgba.h
Normal file
52
include/utils/ColorRgba.h
Normal file
@@ -0,0 +1,52 @@
|
||||
#pragma once
|
||||
|
||||
// STL includes
|
||||
#include <cstdint>
|
||||
#include <ostream>
|
||||
|
||||
struct ColorRgba;
|
||||
|
||||
struct ColorRgba
|
||||
{
|
||||
|
||||
/// The red color channel
|
||||
uint8_t red;
|
||||
/// The green color channel
|
||||
uint8_t green;
|
||||
/// The blue color channel
|
||||
uint8_t blue;
|
||||
|
||||
/// The alpha mask channel
|
||||
uint8_t alpha;
|
||||
|
||||
/// 'Black' RgbColor (0, 0, 0, 255)
|
||||
static ColorRgba BLACK;
|
||||
/// 'Red' RgbColor (255, 0, 0, 255)
|
||||
static ColorRgba RED;
|
||||
/// 'Green' RgbColor (0, 255, 0, 255)
|
||||
static ColorRgba GREEN;
|
||||
/// 'Blue' RgbColor (0, 0, 255, 255)
|
||||
static ColorRgba BLUE;
|
||||
/// 'Yellow' RgbColor (255, 255, 0, 255)
|
||||
static ColorRgba YELLOW;
|
||||
/// 'White' RgbColor (255, 255, 255, 255
|
||||
static ColorRgba WHITE;
|
||||
};
|
||||
|
||||
|
||||
/// Assert to ensure that the size of the structure is 'only' 3 bytes
|
||||
static_assert(sizeof(ColorRgba) == 4, "Incorrect size of ColorARGB");
|
||||
|
||||
///
|
||||
/// Stream operator to write ColorRgb to an outputstream (format "'{'[alpha]', '[red]','[green]','[blue]'}'")
|
||||
///
|
||||
/// @param os The output stream
|
||||
/// @param color The color to write
|
||||
/// @return The output stream (with the color written to it)
|
||||
///
|
||||
inline std::ostream& operator<<(std::ostream& os, const ColorRgba& color)
|
||||
{
|
||||
os << "{" << unsigned(color.alpha) << "," << unsigned(color.red) << "," << unsigned(color.green) << "," << unsigned(color.blue) << "}";
|
||||
return os;
|
||||
}
|
||||
|
@@ -1,34 +1,56 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cassert>
|
||||
#include <cstring>
|
||||
// STL includes
|
||||
#include <vector>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <algorithm>
|
||||
|
||||
// Local includes
|
||||
#include "RgbColor.h"
|
||||
|
||||
///
|
||||
/// The RgbImage holds a 2D matrix of RgbColors's (or image). Width and height of the image are
|
||||
/// fixed at construction.
|
||||
///
|
||||
class RgbImage
|
||||
template <typename Pixel_T>
|
||||
class Image
|
||||
{
|
||||
public:
|
||||
|
||||
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
|
||||
/// @param background The color of the image (default = BLACK)
|
||||
///
|
||||
RgbImage(const unsigned width, const unsigned height, const RgbColor background = RgbColor::BLACK);
|
||||
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);
|
||||
}
|
||||
|
||||
///
|
||||
/// Destructor
|
||||
///
|
||||
~RgbImage();
|
||||
~Image()
|
||||
{
|
||||
delete[] _pixels;
|
||||
}
|
||||
|
||||
///
|
||||
/// Returns the width of the image
|
||||
@@ -50,14 +72,25 @@ public:
|
||||
return _height;
|
||||
}
|
||||
|
||||
///
|
||||
/// 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);
|
||||
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;
|
||||
}
|
||||
|
||||
///
|
||||
/// Returns a const reference to a specified pixel in the image
|
||||
@@ -67,7 +100,10 @@ public:
|
||||
///
|
||||
/// @return const reference to specified pixel
|
||||
///
|
||||
const RgbColor& operator()(const unsigned x, const unsigned y) const;
|
||||
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
|
||||
@@ -77,37 +113,40 @@ public:
|
||||
///
|
||||
/// @return reference to specified pixel
|
||||
///
|
||||
RgbColor& operator()(const unsigned x, const unsigned y);
|
||||
Pixel_T& operator()(const unsigned x, const unsigned y)
|
||||
{
|
||||
return _pixels[toIndex(x,y)];
|
||||
}
|
||||
|
||||
///
|
||||
/// 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)
|
||||
void copy(const Image<Pixel_T>& other)
|
||||
{
|
||||
assert(other._width == _width);
|
||||
assert(other._height == _height);
|
||||
|
||||
memcpy(mColors, other.mColors, _width*_height*sizeof(RgbColor));
|
||||
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
|
||||
///
|
||||
RgbColor* memptr()
|
||||
Pixel_T* memptr()
|
||||
{
|
||||
return mColors;
|
||||
return _pixels;
|
||||
}
|
||||
|
||||
///
|
||||
/// 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
|
||||
const Pixel_T* memptr() const
|
||||
{
|
||||
return mColors;
|
||||
return _pixels;
|
||||
}
|
||||
private:
|
||||
|
||||
@@ -130,6 +169,9 @@ private:
|
||||
/// The height of the image
|
||||
const unsigned _height;
|
||||
|
||||
/** The colors of the image */
|
||||
RgbColor* mColors;
|
||||
/// The pixels of the image
|
||||
Pixel_T* _pixels;
|
||||
|
||||
/// Pointer to the last(extra) pixel
|
||||
Pixel_T* _endOfPixels;
|
||||
};
|
Reference in New Issue
Block a user