Refactor color utils (#955)

Co-authored-by: Paulchen Panther <16664240+Paulchen-Panther@users.noreply.github.com>
This commit is contained in:
Murat Seker 2020-08-08 13:22:37 +02:00 committed by GitHub
parent 63d95a5a2a
commit a18ccb8b48
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
20 changed files with 181 additions and 120 deletions

View File

@ -6,7 +6,6 @@
struct ColorArgb
{
/// The alpha mask channel
uint8_t alpha;
@ -18,20 +17,19 @@ struct ColorArgb
uint8_t blue;
/// 'Black' RgbColor (255, 0, 0, 0)
static ColorArgb BLACK;
static const ColorArgb BLACK;
/// 'Red' RgbColor (255, 255, 0, 0)
static ColorArgb RED;
static const ColorArgb RED;
/// 'Green' RgbColor (255, 0, 255, 0)
static ColorArgb GREEN;
static const ColorArgb GREEN;
/// 'Blue' RgbColor (255, 0, 0, 255)
static ColorArgb BLUE;
static const ColorArgb BLUE;
/// 'Yellow' RgbColor (255, 255, 255, 0)
static ColorArgb YELLOW;
static const ColorArgb YELLOW;
/// 'White' RgbColor (255, 255, 255, 255)
static ColorArgb WHITE;
static const ColorArgb WHITE;
};
/// Assert to ensure that the size of the structure is 'only' 3 bytes
static_assert(sizeof(ColorArgb) == 4, "Incorrect size of ColorARGB");
@ -44,6 +42,12 @@ static_assert(sizeof(ColorArgb) == 4, "Incorrect size of ColorARGB");
///
inline std::ostream& operator<<(std::ostream& os, const ColorArgb& color)
{
os << "{" << unsigned(color.alpha) << "," << unsigned(color.red) << "," << unsigned(color.green) << "," << unsigned(color.blue) << "}";
os << "{"
<< color.alpha << ","
<< color.red << ","
<< color.green << ","
<< color.blue
<< "}";
return os;
}

View File

@ -17,18 +17,19 @@ struct ColorBgr
/// The red color channel
uint8_t red;
/// 'Black' RgbColor (0, 0, 0)
static ColorBgr BLACK;
static const ColorBgr BLACK;
/// 'Red' RgbColor (255, 0, 0)
static ColorBgr RED;
static const ColorBgr RED;
/// 'Green' RgbColor (0, 255, 0)
static ColorBgr GREEN;
static const ColorBgr GREEN;
/// 'Blue' RgbColor (0, 0, 255)
static ColorBgr BLUE;
static const ColorBgr BLUE;
/// 'Yellow' RgbColor (255, 255, 0)
static ColorBgr YELLOW;
static const ColorBgr YELLOW;
/// 'White' RgbColor (255, 255, 255)
static ColorBgr WHITE;
static const ColorBgr WHITE;
};
/// Assert to ensure that the size of the structure is 'only' 3 bytes
@ -43,19 +44,33 @@ static_assert(sizeof(ColorBgr) == 3, "Incorrect size of ColorBgr");
///
inline std::ostream& operator<<(std::ostream& os, const ColorBgr& color)
{
os << "{" << unsigned(color.red) << "," << unsigned(color.green) << "," << unsigned(color.blue) << "}";
os << "{"
<< color.red << ","
<< color.green << ","
<< color.blue
<< "}";
return os;
}
/// Compare operator to check if a color is 'equal' to another color
inline bool operator==(const ColorBgr & lhs, const ColorBgr & rhs)
{
return (lhs.red == rhs.red) &&
(lhs.green == rhs.green) &&
(lhs.blue == rhs.blue);
}
/// Compare operator to check if a color is 'smaller' than another color
inline bool operator<(const ColorBgr & lhs, const ColorBgr & rhs)
{
return (lhs.red < rhs.red) && (lhs.green < rhs.green) && (lhs.blue < rhs.blue);
return (lhs.red < rhs.red) &&
(lhs.green < rhs.green) &&
(lhs.blue < rhs.blue);
}
/// Compare operator to check if a color is 'smaller' than or 'equal' to another color
inline bool operator<=(const ColorBgr & lhs, const ColorBgr & rhs)
{
return (lhs.red <= rhs.red) && (lhs.green <= rhs.green) && (lhs.blue <= rhs.blue);
return lhs < rhs || lhs == rhs;
}

View File

@ -20,17 +20,17 @@ struct ColorRgb
uint8_t blue;
/// 'Black' RgbColor (0, 0, 0)
static ColorRgb BLACK;
static const ColorRgb BLACK;
/// 'Red' RgbColor (255, 0, 0)
static ColorRgb RED;
static const ColorRgb RED;
/// 'Green' RgbColor (0, 255, 0)
static ColorRgb GREEN;
static const ColorRgb GREEN;
/// 'Blue' RgbColor (0, 0, 255)
static ColorRgb BLUE;
static const ColorRgb BLUE;
/// 'Yellow' RgbColor (255, 255, 0)
static ColorRgb YELLOW;
static const ColorRgb YELLOW;
/// 'White' RgbColor (255, 255, 255)
static ColorRgb WHITE;
static const ColorRgb WHITE;
};
/// Assert to ensure that the size of the structure is 'only' 3 bytes
@ -45,7 +45,12 @@ static_assert(sizeof(ColorRgb) == 3, "Incorrect size of ColorRgb");
///
inline std::ostream& operator<<(std::ostream& os, const ColorRgb& color)
{
os << "{" << unsigned(color.red) << "," << unsigned(color.green) << "," << unsigned(color.blue) << "}";
os << "{"
<< color.red << ","
<< color.green << ","
<< color.blue
<< "}";
return os;
}
@ -58,30 +63,51 @@ inline std::ostream& operator<<(std::ostream& os, const ColorRgb& color)
///
inline QTextStream& operator<<(QTextStream &os, const ColorRgb& color)
{
os << "{" << unsigned(color.red) << "," << unsigned(color.green) << "," << unsigned(color.blue) << "}";
os << "{"
<< color.red << ","
<< color.green << ","
<< color.blue
<< "}";
return os;
}
/// Compare operator to check if a color is 'equal' to another color
inline bool operator==(const ColorRgb & lhs, const ColorRgb & rhs)
{
return lhs.red == rhs.red &&
lhs.green == rhs.green &&
lhs.blue == rhs.blue;
}
/// Compare operator to check if a color is 'smaller' than another color
inline bool operator<(const ColorRgb & lhs, const ColorRgb & rhs)
{
return (lhs.red < rhs.red) && (lhs.green < rhs.green) && (lhs.blue < rhs.blue);
return lhs.red < rhs.red &&
lhs.green < rhs.green &&
lhs.blue < rhs.blue;
}
/// Compare operator to check if a color is 'not equal' to another color
inline bool operator!=(const ColorRgb & lhs, const ColorRgb & rhs)
{
return !(lhs == rhs);
}
/// Compare operator to check if a color is 'smaller' than or 'equal' to another color
inline bool operator<=(const ColorRgb & lhs, const ColorRgb & rhs)
{
return (lhs.red <= rhs.red) && (lhs.green <= rhs.green) && (lhs.blue <= rhs.blue);
return lhs < rhs || lhs == rhs;
}
/// Compare operator to check if a color is 'greater' to another color
inline bool operator>(const ColorRgb & lhs, const ColorRgb & rhs)
{
return (lhs.red > rhs.red) && (lhs.green > rhs.green) && (lhs.blue > rhs.blue);
return !(lhs < rhs) && lhs != rhs;
}
/// Compare operator to check if a color is 'greater' than or 'equal' to another color
inline bool operator>=(const ColorRgb & lhs, const ColorRgb & rhs)
{
return (lhs.red >= rhs.red) && (lhs.green >= rhs.green) && (lhs.blue >= rhs.blue);
return lhs > rhs || lhs == rhs;
}

View File

@ -6,7 +6,6 @@
struct ColorRgba
{
/// The red color channel
uint8_t red;
/// The green color channel
@ -18,20 +17,19 @@ struct ColorRgba
uint8_t alpha;
/// 'Black' RgbColor (0, 0, 0, 255)
static ColorRgba BLACK;
static const ColorRgba BLACK;
/// 'Red' RgbColor (255, 0, 0, 255)
static ColorRgba RED;
static const ColorRgba RED;
/// 'Green' RgbColor (0, 255, 0, 255)
static ColorRgba GREEN;
static const ColorRgba GREEN;
/// 'Blue' RgbColor (0, 0, 255, 255)
static ColorRgba BLUE;
static const ColorRgba BLUE;
/// 'Yellow' RgbColor (255, 255, 0, 255)
static ColorRgba YELLOW;
static const ColorRgba YELLOW;
/// 'White' RgbColor (255, 255, 255, 255
static ColorRgba WHITE;
static const ColorRgba WHITE;
};
/// Assert to ensure that the size of the structure is 'only' 3 bytes
static_assert(sizeof(ColorRgba) == 4, "Incorrect size of ColorARGB");
@ -44,6 +42,12 @@ static_assert(sizeof(ColorRgba) == 4, "Incorrect size of ColorARGB");
///
inline std::ostream& operator<<(std::ostream& os, const ColorRgba& color)
{
os << "{" << unsigned(color.alpha) << "," << unsigned(color.red) << "," << unsigned(color.green) << "," << unsigned(color.blue) << "}";
os << "{"
<< color.alpha << ","
<< color.red << ","
<< color.green << ","
<< color.blue
<< "}";
return os;
}

View File

@ -20,17 +20,17 @@ struct ColorRgbw
uint8_t white;
/// 'Black' RgbColor (0, 0, 0, 0)
static ColorRgbw BLACK;
static const ColorRgbw BLACK;
/// 'Red' RgbColor (255, 0, 0, 0)
static ColorRgbw RED;
static const ColorRgbw RED;
/// 'Green' RgbColor (0, 255, 0, 0)
static ColorRgbw GREEN;
static const ColorRgbw GREEN;
/// 'Blue' RgbColor (0, 0, 255, 0)
static ColorRgbw BLUE;
static const ColorRgbw BLUE;
/// 'Yellow' RgbColor (255, 255, 0, 0)
static ColorRgbw YELLOW;
static const ColorRgbw YELLOW;
/// 'White' RgbColor (0, 0, 0, 255)
static ColorRgbw WHITE;
static const ColorRgbw WHITE;
};
/// Assert to ensure that the size of the structure is 'only' 4 bytes
@ -45,19 +45,36 @@ static_assert(sizeof(ColorRgbw) == 4, "Incorrect size of ColorRgbw");
///
inline std::ostream& operator<<(std::ostream& os, const ColorRgbw& color)
{
os << "{" << unsigned(color.red) << "," << unsigned(color.green) << "," << unsigned(color.blue) << "," << unsigned(color.white) << "}";
os << "{"
<< color.red << ","
<< color.green << ","
<< color.blue << ","
<< color.white <<
"}";
return os;
}
/// Compare operator to check if a color is 'equal' than another color
inline bool operator==(const ColorRgbw & lhs, const ColorRgbw & rhs)
{
return lhs.red == rhs.red &&
lhs.green == rhs.green &&
lhs.blue == rhs.blue &&
lhs.white == rhs.white;
}
/// Compare operator to check if a color is 'smaller' than another color
inline bool operator<(const ColorRgbw & lhs, const ColorRgbw & rhs)
{
return (lhs.red < rhs.red) && (lhs.green < rhs.green) && (lhs.blue < rhs.blue) && (lhs.white < rhs.white);
return lhs.red < rhs.red &&
lhs.green < rhs.green &&
lhs.blue < rhs.blue &&
lhs.white < rhs.white;
}
/// Compare operator to check if a color is 'smaller' than or 'equal' to another color
inline bool operator<=(const ColorRgbw & lhs, const ColorRgbw & rhs)
{
return (lhs.red <= rhs.red) && (lhs.green <= rhs.green) && (lhs.blue <= rhs.blue) && (lhs.white < rhs.white);
return lhs < rhs || lhs == rhs;
}

View File

@ -65,4 +65,15 @@ public:
/// number and scaled between 0 and 360
///
static void hsv2rgb(uint16_t hue, uint8_t saturation, uint8_t value, uint8_t & red, uint8_t & green, uint8_t & blue);
///
/// Translates a YUV (luminance, chrominance, chrominance) color to an RGB (red, green, blue) color
///
/// @param[in] y The luminance YUV-component
/// @param[in] u The chrominance YUV-component
/// @param[in] v The chrominance YUV-component
/// @param[out] red The red RGB-component
/// @param[out] green The green RGB-component
/// @param[out] blue The blue RGB-component
static void yuv2rgb(uint8_t y, uint8_t u, uint8_t v, uint8_t & r, uint8_t & g, uint8_t & b);
};

View File

@ -20,6 +20,7 @@ public:
static GlobalSignals instance;
return & instance;
}
private:
GlobalSignals() = default;

View File

@ -12,22 +12,11 @@ public:
~ImageResampler();
void setHorizontalPixelDecimation(int decimator);
void setVerticalPixelDecimation(int decimator);
void setCropping(int cropLeft,
int cropRight,
int cropTop,
int cropBottom);
void setCropping(int cropLeft, int cropRight, int cropTop, int cropBottom);
void setVideoMode(VideoMode mode);
void processImage(const uint8_t * data, int width, int height, int lineLength, PixelFormat pixelFormat, Image<ColorRgb> & outputImage) const;
private:
static inline uint8_t clamp(int x);
static void yuv2rgb(uint8_t y, uint8_t u, uint8_t v, uint8_t & r, uint8_t & g, uint8_t & b);
private:
int _horizontalDecimation;
int _verticalDecimation;
@ -37,3 +26,4 @@ private:
int _cropBottom;
VideoMode _videoMode;
};

View File

@ -30,7 +30,6 @@ For more profiler function see the macros listed below
#define PROFILER_TIMER_GET(stopWatchName) Profiler::TimerGetTime(stopWatchName, __FILE__, __FUNCTION__, __LINE__);
#define PROFILER_TIMER_GET_IF(condition, stopWatchName) { if (condition) {Profiler::TimerGetTime(stopWatchName, __FILE__, __FUNCTION__, __LINE__);} }
class Profiler
{
public:

View File

@ -15,6 +15,6 @@ namespace RGBW {
};
WhiteAlgorithm stringToWhiteAlgorithm(QString str);
void Rgb_to_Rgbw(ColorRgb input, ColorRgbw * output, const WhiteAlgorithm algorithm);
void Rgb_to_Rgbw(ColorRgb input, ColorRgbw * output, WhiteAlgorithm algorithm);
}

View File

@ -5,7 +5,6 @@
class SysInfo : public QObject
{
public:
struct HyperionSysInfo
{

View File

@ -1,10 +1,9 @@
// Utils includes
#include <utils/ColorArgb.h>
ColorArgb ColorArgb::BLACK = { 255, 0, 0, 0 };
ColorArgb ColorArgb::RED = { 255, 255, 0, 0 };
ColorArgb ColorArgb::GREEN = { 255, 0, 255, 0 };
ColorArgb ColorArgb::BLUE = { 255, 0, 0, 255 };
ColorArgb ColorArgb::YELLOW= { 255, 255, 255, 0 };
ColorArgb ColorArgb::WHITE = { 255, 255, 255, 255 };
const ColorArgb ColorArgb::BLACK = { 255, 0, 0, 0 };
const ColorArgb ColorArgb::RED = { 255, 255, 0, 0 };
const ColorArgb ColorArgb::GREEN = { 255, 0, 255, 0 };
const ColorArgb ColorArgb::BLUE = { 255, 0, 0, 255 };
const ColorArgb ColorArgb::YELLOW = { 255, 255, 255, 0 };
const ColorArgb ColorArgb::WHITE = { 255, 255, 255, 255 };

View File

@ -1,11 +1,10 @@
// Local includes
#include <utils/ColorBgr.h>
ColorBgr ColorBgr::BLACK = { 0, 0, 0 };
ColorBgr ColorBgr::RED = { 0, 0, 255 };
ColorBgr ColorBgr::GREEN = { 0, 255, 0 };
ColorBgr ColorBgr::BLUE = { 255, 0, 0 };
ColorBgr ColorBgr::YELLOW= { 0, 255, 255 };
ColorBgr ColorBgr::WHITE = { 255, 255, 255 };
const ColorBgr ColorBgr::BLACK = { 0, 0, 0 };
const ColorBgr ColorBgr::RED = { 0, 0, 255 };
const ColorBgr ColorBgr::GREEN = { 0, 255, 0 };
const ColorBgr ColorBgr::BLUE = { 255, 0, 0 };
const ColorBgr ColorBgr::YELLOW = { 0, 255, 255 };
const ColorBgr ColorBgr::WHITE = { 255, 255, 255 };

View File

@ -1,10 +1,9 @@
// Local includes
#include <utils/ColorRgb.h>
ColorRgb ColorRgb::BLACK = { 0, 0, 0 };
ColorRgb ColorRgb::RED = { 255, 0, 0 };
ColorRgb ColorRgb::GREEN = { 0, 255, 0 };
ColorRgb ColorRgb::BLUE = { 0, 0, 255 };
ColorRgb ColorRgb::YELLOW= { 255, 255, 0 };
ColorRgb ColorRgb::WHITE = { 255, 255, 255 };
const ColorRgb ColorRgb::BLACK = { 0, 0, 0 };
const ColorRgb ColorRgb::RED = { 255, 0, 0 };
const ColorRgb ColorRgb::GREEN = { 0, 255, 0 };
const ColorRgb ColorRgb::BLUE = { 0, 0, 255 };
const ColorRgb ColorRgb::YELLOW = { 255, 255, 0 };
const ColorRgb ColorRgb::WHITE = { 255, 255, 255 };

View File

@ -1,10 +1,9 @@
// Utils includes
#include <utils/ColorRgba.h>
ColorRgba ColorRgba::BLACK = { 0, 0, 0, 255 };
ColorRgba ColorRgba::RED = { 255, 0, 0, 255 };
ColorRgba ColorRgba::GREEN = { 0, 255, 0, 255 };
ColorRgba ColorRgba::BLUE = { 0, 0, 255, 255 };
ColorRgba ColorRgba::YELLOW= { 255, 255, 0, 255 };
ColorRgba ColorRgba::WHITE = { 255, 255, 255, 255 };
const ColorRgba ColorRgba::BLACK = { 0, 0, 0, 255 };
const ColorRgba ColorRgba::RED = { 255, 0, 0, 255 };
const ColorRgba ColorRgba::GREEN = { 0, 255, 0, 255 };
const ColorRgba ColorRgba::BLUE = { 0, 0, 255, 255 };
const ColorRgba ColorRgba::YELLOW = { 255, 255, 0, 255 };
const ColorRgba ColorRgba::WHITE = { 255, 255, 255, 255 };

View File

@ -1,10 +1,9 @@
// Local includes
#include <utils/ColorRgbw.h>
ColorRgbw ColorRgbw::BLACK = { 0, 0, 0, 0 };
ColorRgbw ColorRgbw::RED = { 255, 0, 0, 0 };
ColorRgbw ColorRgbw::GREEN = { 0, 255, 0, 0 };
ColorRgbw ColorRgbw::BLUE = { 0, 0, 255, 0 };
ColorRgbw ColorRgbw::YELLOW= { 255, 255, 0, 0 };
ColorRgbw ColorRgbw::WHITE = { 0, 0, 0, 255 };
const ColorRgbw ColorRgbw::BLACK = { 0, 0, 0, 0 };
const ColorRgbw ColorRgbw::RED = { 255, 0, 0, 0 };
const ColorRgbw ColorRgbw::GREEN = { 0, 255, 0, 0 };
const ColorRgbw ColorRgbw::BLUE = { 0, 0, 255, 0 };
const ColorRgbw ColorRgbw::YELLOW = { 255, 255, 0, 0 };
const ColorRgbw ColorRgbw::WHITE = { 0, 0, 0, 255 };

View File

@ -2,6 +2,11 @@
#include <QColor>
inline uint8_t clamp(int x)
{
return (x<0) ? 0 : ((x>255) ? 255 : uint8_t(x));
}
void ColorSys::rgb2hsl(uint8_t red, uint8_t green, uint8_t blue, uint16_t & hue, float & saturation, float & luminance)
{
QColor color(red,green,blue);
@ -35,3 +40,15 @@ void ColorSys::hsv2rgb(uint16_t hue, uint8_t saturation, uint8_t value, uint8_t
green = (uint8_t)color.green();
blue = (uint8_t)color.blue();
}
void ColorSys::yuv2rgb(uint8_t y, uint8_t u, uint8_t v, uint8_t &r, uint8_t &g, uint8_t &b)
{
// see: http://en.wikipedia.org/wiki/YUV#Y.27UV444_to_RGB888_conversion
int c = y - 16;
int d = u - 128;
int e = v - 128;
r = clamp((298 * c + 409 * e + 128) >> 8);
g = clamp((298 * c - 100 * d - 208 * e + 128) >> 8);
b = clamp((298 * c + 516 * d + 128) >> 8);
}

View File

@ -1,5 +1,5 @@
#include "utils/ImageResampler.h"
#include <utils/ColorSys.h>
#include <utils/Logger.h>
ImageResampler::ImageResampler()
@ -80,7 +80,7 @@ void ImageResampler::processImage(const uint8_t * data, int width, int height, i
uint8_t y = data[index+1];
uint8_t u = ((xSource&1) == 0) ? data[index ] : data[index-2];
uint8_t v = ((xSource&1) == 0) ? data[index+2] : data[index ];
yuv2rgb(y, u, v, rgb.red, rgb.green, rgb.blue);
ColorSys::yuv2rgb(y, u, v, rgb.red, rgb.green, rgb.blue);
}
break;
case PixelFormat::YUYV:
@ -89,7 +89,7 @@ void ImageResampler::processImage(const uint8_t * data, int width, int height, i
uint8_t y = data[index];
uint8_t u = ((xSource&1) == 0) ? data[index+1] : data[index-1];
uint8_t v = ((xSource&1) == 0) ? data[index+3] : data[index+1];
yuv2rgb(y, u, v, rgb.red, rgb.green, rgb.blue);
ColorSys::yuv2rgb(y, u, v, rgb.red, rgb.green, rgb.blue);
}
break;
case PixelFormat::BGR16:
@ -135,20 +135,3 @@ void ImageResampler::processImage(const uint8_t * data, int width, int height, i
}
}
}
uint8_t ImageResampler::clamp(int x)
{
return (x<0) ? 0 : ((x>255) ? 255 : uint8_t(x));
}
void ImageResampler::yuv2rgb(uint8_t y, uint8_t u, uint8_t v, uint8_t &r, uint8_t &g, uint8_t &b)
{
// see: http://en.wikipedia.org/wiki/YUV#Y.27UV444_to_RGB888_conversion
int c = y - 16;
int d = u - 128;
int e = v - 128;
r = clamp((298 * c + 409 * e + 128) >> 8);
g = clamp((298 * c - 100 * d - 208 * e + 128) >> 8);
b = clamp((298 * c + 516 * d + 128) >> 8);
}

View File

@ -14,7 +14,7 @@ WhiteAlgorithm stringToWhiteAlgorithm(QString str)
return WhiteAlgorithm::INVALID;
}
void Rgb_to_Rgbw(ColorRgb input, ColorRgbw * output, const WhiteAlgorithm algorithm)
void Rgb_to_Rgbw(ColorRgb input, ColorRgbw * output, WhiteAlgorithm algorithm)
{
switch (algorithm)
{