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:
@@ -5,8 +5,9 @@
|
||||
#include <QTimer>
|
||||
|
||||
// Utils includes
|
||||
#include <utils/RgbColor.h>
|
||||
#include <utils/RgbImage.h>
|
||||
#include <utils/Image.h>
|
||||
#include <utils/ColorRgb.h>
|
||||
#include <utils/ColorRgba.h>
|
||||
#include <utils/GrabbingMode.h>
|
||||
|
||||
// Forward class declaration
|
||||
@@ -15,8 +16,8 @@ class Hyperion;
|
||||
class ImageProcessor;
|
||||
|
||||
///
|
||||
/// The DispmanxWrapper uses an instance of the DispmanxFrameGrabber to obtain RgbImage's from the
|
||||
/// displayed content. This RgbImage is processed to a RgbColor for each led and commmited to the
|
||||
/// The DispmanxWrapper uses an instance of the DispmanxFrameGrabber to obtain ImageRgb's from the
|
||||
/// displayed content. This ImageRgb is processed to a ColorRgb for each led and commmited to the
|
||||
/// attached Hyperion.
|
||||
///
|
||||
class DispmanxWrapper: public QObject
|
||||
@@ -72,14 +73,14 @@ private:
|
||||
QTimer _timer;
|
||||
|
||||
/// The image used for grabbing frames
|
||||
RgbImage _image;
|
||||
Image<ColorRgba> _image;
|
||||
/// The actual grabber
|
||||
DispmanxFrameGrabber * _frameGrabber;
|
||||
/// The processor for transforming images to led colors
|
||||
ImageProcessor * _processor;
|
||||
|
||||
/// The list with computed led colors
|
||||
std::vector<RgbColor> _ledColors;
|
||||
std::vector<ColorRgb> _ledColors;
|
||||
|
||||
/// Pointer to Hyperion for writing led values
|
||||
Hyperion * _hyperion;
|
||||
|
131
include/hyperion/BlackBorderDetector.h
Normal file
131
include/hyperion/BlackBorderDetector.h
Normal file
@@ -0,0 +1,131 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
// Utils includes
|
||||
#include <utils/Image.h>
|
||||
|
||||
namespace hyperion
|
||||
{
|
||||
///
|
||||
/// Result structure of the detected blackborder.
|
||||
///
|
||||
struct BlackBorder
|
||||
{
|
||||
/// Falg indicating if the border is unknown
|
||||
bool unknown;
|
||||
|
||||
/// The size of the detected horizontal border
|
||||
int horizontalSize;
|
||||
|
||||
/// The size of the detected vertical border
|
||||
int verticalSize;
|
||||
|
||||
///
|
||||
/// Compares this BlackBorder to the given other BlackBorder
|
||||
///
|
||||
/// @param[in] other The other BlackBorder
|
||||
///
|
||||
/// @return True if this is the same border as other
|
||||
///
|
||||
inline bool operator== (const BlackBorder& other) const
|
||||
{
|
||||
if (unknown)
|
||||
{
|
||||
return other.unknown;
|
||||
}
|
||||
|
||||
return other.unknown==false && horizontalSize==other.horizontalSize && verticalSize==other.verticalSize;
|
||||
}
|
||||
};
|
||||
|
||||
///
|
||||
/// The BlackBorderDetector performs detection of black-borders on a single image.
|
||||
/// The detector will search for the upper left corner of the picture in the frame.
|
||||
/// Based on detected black pixels it will give an estimate of the black-border.
|
||||
///
|
||||
class BlackBorderDetector
|
||||
{
|
||||
public:
|
||||
///
|
||||
/// Constructs a black-border detector
|
||||
///
|
||||
BlackBorderDetector();
|
||||
|
||||
///
|
||||
/// Performs the actual black-border detection on the given image
|
||||
///
|
||||
/// @param[in] image The image on which detection is performed
|
||||
///
|
||||
/// @return The detected (or not detected) black border info
|
||||
///
|
||||
template <typename Pixel_T>
|
||||
BlackBorder process(const Image<Pixel_T> & image)
|
||||
{
|
||||
// only test the topleft third of the image
|
||||
int width = image.width() /3;
|
||||
int height = image.height() / 3;
|
||||
int maxSize = std::max(width, height);
|
||||
|
||||
int firstNonBlackXPixelIndex = -1;
|
||||
int firstNonBlackYPixelIndex = -1;
|
||||
|
||||
// find some pixel of the image
|
||||
for (int i = 0; i < maxSize; ++i)
|
||||
{
|
||||
int x = std::min(i, width);
|
||||
int y = std::min(i, height);
|
||||
|
||||
const Pixel_T & color = image(x, y);
|
||||
if (!isBlack(color))
|
||||
{
|
||||
firstNonBlackXPixelIndex = x;
|
||||
firstNonBlackYPixelIndex = y;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// expand image to the left
|
||||
for(; firstNonBlackXPixelIndex > 0; --firstNonBlackXPixelIndex)
|
||||
{
|
||||
const Pixel_T & color = image(firstNonBlackXPixelIndex-1, firstNonBlackYPixelIndex);
|
||||
if (isBlack(color))
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// expand image to the top
|
||||
for(; firstNonBlackYPixelIndex > 0; --firstNonBlackYPixelIndex)
|
||||
{
|
||||
const Pixel_T & color = image(firstNonBlackXPixelIndex, firstNonBlackYPixelIndex-1);
|
||||
if (isBlack(color))
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Construct result
|
||||
BlackBorder detectedBorder;
|
||||
detectedBorder.unknown = firstNonBlackXPixelIndex == -1 || firstNonBlackYPixelIndex == -1;
|
||||
detectedBorder.horizontalSize = firstNonBlackYPixelIndex;
|
||||
detectedBorder.verticalSize = firstNonBlackXPixelIndex;
|
||||
return detectedBorder;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
///
|
||||
/// Checks if a given color is considered black and therefor could be part of the border.
|
||||
///
|
||||
/// @param[in] color The color to check
|
||||
///
|
||||
/// @return True if the color is considered black else false
|
||||
///
|
||||
template <typename Pixel_T>
|
||||
inline bool isBlack(const Pixel_T & color)
|
||||
{
|
||||
// Return the simple compare of the color against black
|
||||
return color.red+color.green+color.green == 0;
|
||||
}
|
||||
};
|
||||
} // end namespace hyperion
|
96
include/hyperion/BlackBorderProcessor.h
Normal file
96
include/hyperion/BlackBorderProcessor.h
Normal file
@@ -0,0 +1,96 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
// Local Hyperion includes
|
||||
#include "BlackBorderDetector.h"
|
||||
|
||||
namespace hyperion
|
||||
{
|
||||
///
|
||||
/// The BlackBorder processor is a wrapper around the black-border detector for keeping track of
|
||||
/// detected borders and count of the type and size of detected borders.
|
||||
///
|
||||
class BlackBorderProcessor
|
||||
{
|
||||
public:
|
||||
///
|
||||
/// Constructor for the BlackBorderProcessor
|
||||
/// @param unknownFrameCnt The number of frames(images) that need to contain an unknown
|
||||
/// border before the current border is set to unknown
|
||||
/// @param borderFrameCnt The number of frames(images) that need to contain a vertical or
|
||||
/// horizontal border becomes the current border
|
||||
/// @param blurRemoveCnt The size to add to a horizontal or vertical border (because the
|
||||
/// outer pixels is blurred (black and color combined due to image scaling))
|
||||
///
|
||||
BlackBorderProcessor(
|
||||
const unsigned unknownFrameCnt,
|
||||
const unsigned borderFrameCnt,
|
||||
const unsigned blurRemoveCnt);
|
||||
|
||||
///
|
||||
/// Return the current (detected) border
|
||||
/// @return The current border
|
||||
///
|
||||
BlackBorder getCurrentBorder() const;
|
||||
|
||||
///
|
||||
/// Processes the image. This performs detecion of black-border on the given image and
|
||||
/// updates the current border accordingly. If the current border is updated the method call
|
||||
/// will return true else false
|
||||
///
|
||||
/// @param image The image to process
|
||||
///
|
||||
/// @return True if a different border was detected than the current else false
|
||||
///
|
||||
template <typename Pixel_T>
|
||||
bool process(const Image<Pixel_T> & image)
|
||||
{
|
||||
// get the border for the single image
|
||||
BlackBorder imageBorder = _detector.process(image);
|
||||
// add blur to the border
|
||||
if (imageBorder.horizontalSize > 0)
|
||||
{
|
||||
imageBorder.horizontalSize += _blurRemoveCnt;
|
||||
}
|
||||
if (imageBorder.verticalSize > 0)
|
||||
{
|
||||
imageBorder.verticalSize += _blurRemoveCnt;
|
||||
}
|
||||
|
||||
const bool borderUpdated = updateBorder(imageBorder);
|
||||
return borderUpdated;
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
///
|
||||
/// Updates the current border based on the newly detected border. Returns true if the
|
||||
/// current border has changed.
|
||||
///
|
||||
/// @param newDetectedBorder The newly detected border
|
||||
/// @return True if the current border changed else false
|
||||
///
|
||||
bool updateBorder(const BlackBorder & newDetectedBorder);
|
||||
|
||||
/// The number of unknown-borders detected before it becomes the current border
|
||||
const unsigned _unknownSwitchCnt;
|
||||
|
||||
/// The number of horizontal/vertical borders detected before it becomes the current border
|
||||
const unsigned _borderSwitchCnt;
|
||||
|
||||
/// The number of pixels to increase a detected border for removing blury pixels
|
||||
unsigned _blurRemoveCnt;
|
||||
|
||||
/// The blackborder detector
|
||||
BlackBorderDetector _detector;
|
||||
|
||||
/// The current detected border
|
||||
BlackBorder _currentBorder;
|
||||
|
||||
/// The border detected in the previous frame
|
||||
BlackBorder _previousDetectedBorder;
|
||||
|
||||
/// The number of frame the previous detected border matched the incomming border
|
||||
unsigned _consistentCnt;
|
||||
};
|
||||
} // end namespace hyperion
|
@@ -8,7 +8,7 @@
|
||||
#include <QTimer>
|
||||
|
||||
// hyperion-utils includes
|
||||
#include <utils/RgbImage.h>
|
||||
#include <utils/Image.h>
|
||||
|
||||
// Hyperion includes
|
||||
#include <hyperion/LedString.h>
|
||||
@@ -76,7 +76,7 @@ public:
|
||||
/// @param[in] ledColor The color to write to the leds
|
||||
/// @param[in] timeout_ms The time the leds are set to the given color [ms]
|
||||
///
|
||||
void setColor(int priority, const RgbColor &ledColor, const int timeout_ms);
|
||||
void setColor(int priority, const ColorRgb &ledColor, const int timeout_ms);
|
||||
|
||||
///
|
||||
/// Writes the given colors to all leds for the given time and priority
|
||||
@@ -85,7 +85,7 @@ public:
|
||||
/// @param[in] ledColors The colors to write to the leds
|
||||
/// @param[in] timeout_ms The time the leds are set to the given colors [ms]
|
||||
///
|
||||
void setColors(int priority, const std::vector<RgbColor> &ledColors, const int timeout_ms);
|
||||
void setColors(int priority, const std::vector<ColorRgb> &ledColors, const int timeout_ms);
|
||||
|
||||
///
|
||||
/// Sets/Updates a part of the color transformation.
|
||||
@@ -162,7 +162,7 @@ private:
|
||||
///
|
||||
/// @param colors The colors to be transformed
|
||||
///
|
||||
void applyTransform(std::vector<RgbColor>& colors) const;
|
||||
void applyTransform(std::vector<ColorRgb>& colors) const;
|
||||
|
||||
/// The specifiation of the led frame construction and picture integration
|
||||
LedString _ledString;
|
||||
|
@@ -2,17 +2,13 @@
|
||||
#pragma once
|
||||
|
||||
// Utils includes
|
||||
#include <utils/RgbImage.h>
|
||||
#include <utils/Image.h>
|
||||
|
||||
// Hyperion includes
|
||||
#include <hyperion/ImageProcessorFactory.h>
|
||||
#include <hyperion/LedString.h>
|
||||
|
||||
// Forward class declaration
|
||||
namespace hyperion {
|
||||
class ImageToLedsMap;
|
||||
class BlackBorderProcessor;
|
||||
}
|
||||
#include <hyperion/ImageToLedsMap.h>
|
||||
#include <hyperion/BlackBorderProcessor.h>
|
||||
|
||||
///
|
||||
/// The ImageProcessor translates an RGB-image to RGB-values for the leds. The processing is
|
||||
@@ -42,7 +38,21 @@ public:
|
||||
///
|
||||
/// @return The color value per led
|
||||
///
|
||||
std::vector<RgbColor> process(const RgbImage& image);
|
||||
template <typename Pixel_T>
|
||||
std::vector<ColorRgb> process(const Image<Pixel_T>& image)
|
||||
{
|
||||
// Ensure that the buffer-image is the proper size
|
||||
setSize(image.width(), image.height());
|
||||
|
||||
// Check black border detection
|
||||
verifyBorder(image);
|
||||
|
||||
// Create a result vector and call the 'in place' functionl
|
||||
std::vector<ColorRgb> colors = mImageToLeds->getMeanLedColor(image);
|
||||
|
||||
// return the computed colors
|
||||
return colors;
|
||||
}
|
||||
|
||||
///
|
||||
/// Determines the led colors of the image in the buffer.
|
||||
@@ -50,7 +60,18 @@ public:
|
||||
/// @param[in] image The image to translate to led values
|
||||
/// @param[out] ledColors The color value per led
|
||||
///
|
||||
void process(const RgbImage& image, std::vector<RgbColor>& ledColors);
|
||||
template <typename Pixel_T>
|
||||
void process(const Image<Pixel_T>& image, std::vector<ColorRgb>& ledColors)
|
||||
{
|
||||
// Ensure that the buffer-image is the proper size
|
||||
setSize(image.width(), image.height());
|
||||
|
||||
// Check black border detection
|
||||
verifyBorder(image);
|
||||
|
||||
// Determine the mean-colors of each led (using the existing mapping)
|
||||
mImageToLeds->getMeanLedColor(image, ledColors);
|
||||
}
|
||||
|
||||
///
|
||||
/// Get the hscan and vscan parameters for a single led
|
||||
@@ -80,7 +101,33 @@ private:
|
||||
///
|
||||
/// @param[in] image The image to perform black-border detection on
|
||||
///
|
||||
void verifyBorder(const RgbImage& image);
|
||||
template <typename Pixel_T>
|
||||
void verifyBorder(const Image<Pixel_T> & image)
|
||||
{
|
||||
if(_enableBlackBorderRemoval && _borderProcessor->process(image))
|
||||
{
|
||||
std::cout << "BORDER SWITCH REQUIRED!!" << std::endl;
|
||||
|
||||
const hyperion::BlackBorder border = _borderProcessor->getCurrentBorder();
|
||||
|
||||
// Clean up the old mapping
|
||||
delete mImageToLeds;
|
||||
|
||||
if (border.unknown)
|
||||
{
|
||||
// Construct a new buffer and mapping
|
||||
mImageToLeds = new hyperion::ImageToLedsMap(image.width(), image.height(), 0, 0, mLedString.leds());
|
||||
}
|
||||
else
|
||||
{
|
||||
// Construct a new buffer and mapping
|
||||
mImageToLeds = new hyperion::ImageToLedsMap(image.width(), image.height(), border.horizontalSize, border.verticalSize, mLedString.leds());
|
||||
}
|
||||
|
||||
std::cout << "CURRENT BORDER TYPE: unknown=" << border.unknown << " hor.size=" << border.horizontalSize << " vert.size=" << border.verticalSize << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
/// The Led-string specification
|
||||
const LedString mLedString;
|
||||
@@ -89,7 +136,7 @@ private:
|
||||
bool _enableBlackBorderRemoval;
|
||||
|
||||
/// The processor for black border detection
|
||||
hyperion::BlackBorderProcessor* _borderProcessor;
|
||||
hyperion::BlackBorderProcessor * _borderProcessor;
|
||||
|
||||
/// The mapping of image-pixels to leds
|
||||
hyperion::ImageToLedsMap* mImageToLeds;
|
||||
|
144
include/hyperion/ImageToLedsMap.h
Normal file
144
include/hyperion/ImageToLedsMap.h
Normal file
@@ -0,0 +1,144 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
// STL includes
|
||||
#include <cassert>
|
||||
#include <sstream>
|
||||
|
||||
// hyperion-utils includes
|
||||
#include <utils/Image.h>
|
||||
|
||||
// hyperion includes
|
||||
#include <hyperion/LedString.h>
|
||||
|
||||
namespace hyperion
|
||||
{
|
||||
|
||||
///
|
||||
/// The ImageToLedsMap holds a mapping of indices into an image to leds. It can be used to
|
||||
/// calculate the average (or mean) color per led for a specific region.
|
||||
///
|
||||
class ImageToLedsMap
|
||||
{
|
||||
public:
|
||||
|
||||
///
|
||||
/// Constructs an mapping from the absolute indices in an image to each led based on the border
|
||||
/// definition given in the list of leds. The map holds absolute indices to any given image,
|
||||
/// provided that it is row-oriented.
|
||||
/// The mapping is created purely on size (width and height). The given borders are excluded
|
||||
/// from indexing.
|
||||
///
|
||||
/// @param[in] width The width of the indexed image
|
||||
/// @param[in] height The width of the indexed image
|
||||
/// @param[in] horizontalBorder The size of the horizontal border (0=no border)
|
||||
/// @param[in] verticalBorder The size of the vertical border (0=no border)
|
||||
/// @param[in] leds The list with led specifications
|
||||
///
|
||||
ImageToLedsMap(
|
||||
const unsigned width,
|
||||
const unsigned height,
|
||||
const unsigned horizontalBorder,
|
||||
const unsigned verticalBorder,
|
||||
const std::vector<Led> & leds);
|
||||
|
||||
///
|
||||
/// Returns the width of the indexed image
|
||||
///
|
||||
/// @return The width of the indexed image [pixels]
|
||||
///
|
||||
unsigned width() const;
|
||||
|
||||
///
|
||||
/// Returns the height of the indexed image
|
||||
///
|
||||
/// @return The height of the indexed image [pixels]
|
||||
///
|
||||
unsigned height() const;
|
||||
|
||||
///
|
||||
/// Determines the mean-color for each led using the mapping the image given
|
||||
/// at construction.
|
||||
///
|
||||
/// @param[in] image The image from which to extract the led colors
|
||||
///
|
||||
/// @return ledColors The vector containing the output
|
||||
///
|
||||
template <typename Pixel_T>
|
||||
std::vector<ColorRgb> getMeanLedColor(const Image<Pixel_T> & image) const
|
||||
{
|
||||
std::vector<ColorRgb> colors(mColorsMap.size(), ColorRgb{0,0,0});
|
||||
getMeanLedColor(image, colors);
|
||||
return colors;
|
||||
}
|
||||
|
||||
///
|
||||
/// Determines the mean color for each led using the mapping the image given
|
||||
/// at construction.
|
||||
///
|
||||
/// @param[in] image The image from which to extract the led colors
|
||||
/// @param[out] ledColors The vector containing the output
|
||||
///
|
||||
template <typename Pixel_T>
|
||||
void getMeanLedColor(const Image<Pixel_T> & image, std::vector<ColorRgb> & ledColors) const
|
||||
{
|
||||
// Sanity check for the number of leds
|
||||
assert(mColorsMap.size() == ledColors.size());
|
||||
|
||||
// Iterate each led and compute the mean
|
||||
auto led = ledColors.begin();
|
||||
for (auto ledColors = mColorsMap.begin(); ledColors != mColorsMap.end(); ++ledColors, ++led)
|
||||
{
|
||||
const ColorRgb color = calcMeanColor(image, *ledColors);
|
||||
*led = color;
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
/// The width of the indexed image
|
||||
const unsigned _width;
|
||||
/// The height of the indexed image
|
||||
const unsigned _height;
|
||||
/// The absolute indices into the image for each led
|
||||
std::vector<std::vector<unsigned>> mColorsMap;
|
||||
|
||||
///
|
||||
/// Calculates the 'mean color' of the given list. This is the mean over each color-channel
|
||||
/// (red, green, blue)
|
||||
///
|
||||
/// @param[in] image The image a section from which an average color must be computed
|
||||
/// @param[in] colors The list with colors
|
||||
///
|
||||
/// @return The mean of the given list of colors (or black when empty)
|
||||
///
|
||||
template <typename Pixel_T>
|
||||
ColorRgb calcMeanColor(const Image<Pixel_T> & image, const std::vector<unsigned> & colors) const
|
||||
{
|
||||
if (colors.size() == 0)
|
||||
{
|
||||
return ColorRgb::BLACK;
|
||||
}
|
||||
|
||||
// Accumulate the sum of each seperate color channel
|
||||
uint_fast16_t cummRed = 0;
|
||||
uint_fast16_t cummGreen = 0;
|
||||
uint_fast16_t cummBlue = 0;
|
||||
for (const unsigned colorOffset : colors)
|
||||
{
|
||||
const Pixel_T& pixel = image.memptr()[colorOffset];
|
||||
cummRed += pixel.red;
|
||||
cummGreen += pixel.green;
|
||||
cummBlue += pixel.blue;
|
||||
}
|
||||
|
||||
// Compute the average of each color channel
|
||||
const uint8_t avgRed = uint8_t(cummRed/colors.size());
|
||||
const uint8_t avgGreen = uint8_t(cummGreen/colors.size());
|
||||
const uint8_t avgBlue = uint8_t(cummBlue/colors.size());
|
||||
|
||||
// Return the computed color
|
||||
return {avgRed, avgGreen, avgBlue};
|
||||
}
|
||||
};
|
||||
|
||||
} // end namespace hyperion
|
@@ -4,7 +4,7 @@
|
||||
#include <vector>
|
||||
|
||||
// Utility includes
|
||||
#include <utils/RgbColor.h>
|
||||
#include <utils/ColorRgb.h>
|
||||
|
||||
///
|
||||
/// Interface (pure virtual base class) for LedDevices.
|
||||
@@ -28,7 +28,7 @@ public:
|
||||
///
|
||||
/// @return Zero on success else negative
|
||||
///
|
||||
virtual int write(const std::vector<RgbColor>& ledValues) = 0;
|
||||
virtual int write(const std::vector<ColorRgb>& ledValues) = 0;
|
||||
|
||||
/// Switch the leds off
|
||||
virtual int switchOff() = 0;
|
||||
|
@@ -7,7 +7,7 @@
|
||||
#include <vector>
|
||||
|
||||
// Local includes
|
||||
#include <utils/RgbColor.h>
|
||||
#include <utils/ColorRgb.h>
|
||||
|
||||
// Forward class declarations
|
||||
namespace Json { class Value; }
|
||||
|
@@ -10,7 +10,7 @@
|
||||
#include <QMap>
|
||||
|
||||
// Utils includes
|
||||
#include <utils/RgbColor.h>
|
||||
#include <utils/ColorRgb.h>
|
||||
|
||||
// Hyperion includes
|
||||
#include <hyperion/LedDevice.h>
|
||||
@@ -34,7 +34,7 @@ public:
|
||||
/// The absolute timeout of the channel
|
||||
int64_t timeoutTime_ms;
|
||||
/// The colors for each led of the channel
|
||||
std::vector<RgbColor> ledColors;
|
||||
std::vector<ColorRgb> ledColors;
|
||||
};
|
||||
|
||||
///
|
||||
@@ -89,7 +89,7 @@ public:
|
||||
/// @param[in] ledColors The led colors of the priority channel
|
||||
/// @param[in] timeoutTime_ms The absolute timeout time of the channel
|
||||
///
|
||||
void setInput(const int priority, const std::vector<RgbColor>& ledColors, const int64_t timeoutTime_ms=-1);
|
||||
void setInput(const int priority, const std::vector<ColorRgb>& ledColors, const int64_t timeoutTime_ms=-1);
|
||||
|
||||
///
|
||||
/// Clears the specified priority channel
|
||||
|
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