From a18ccb8b4827aed77b279a5fdfeb5c38cbbe032a Mon Sep 17 00:00:00 2001 From: Murat Seker Date: Sat, 8 Aug 2020 13:22:37 +0200 Subject: [PATCH] Refactor color utils (#955) Co-authored-by: Paulchen Panther <16664240+Paulchen-Panther@users.noreply.github.com> --- include/utils/ColorArgb.h | 22 +++++++++------ include/utils/ColorBgr.h | 33 ++++++++++++++++------ include/utils/ColorRgb.h | 50 +++++++++++++++++++++++++-------- include/utils/ColorRgba.h | 22 +++++++++------ include/utils/ColorRgbw.h | 35 +++++++++++++++++------ include/utils/ColorSys.h | 11 ++++++++ include/utils/GlobalSignals.h | 1 + include/utils/ImageResampler.h | 14 ++------- include/utils/JsonUtils.h | 2 +- include/utils/Profiler.h | 1 - include/utils/RgbToRgbw.h | 2 +- include/utils/SysInfo.h | 1 - libsrc/utils/ColorArgb.cpp | 13 ++++----- libsrc/utils/ColorBgr.cpp | 13 ++++----- libsrc/utils/ColorRgb.cpp | 13 ++++----- libsrc/utils/ColorRgba.cpp | 13 ++++----- libsrc/utils/ColorRgbw.cpp | 13 ++++----- libsrc/utils/ColorSys.cpp | 17 +++++++++++ libsrc/utils/ImageResampler.cpp | 23 ++------------- libsrc/utils/RgbToRgbw.cpp | 2 +- 20 files changed, 181 insertions(+), 120 deletions(-) diff --git a/include/utils/ColorArgb.h b/include/utils/ColorArgb.h index 43c32a9e..c14a2ba2 100644 --- a/include/utils/ColorArgb.h +++ b/include/utils/ColorArgb.h @@ -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; } diff --git a/include/utils/ColorBgr.h b/include/utils/ColorBgr.h index 5cc19bb7..30f2bae9 100644 --- a/include/utils/ColorBgr.h +++ b/include/utils/ColorBgr.h @@ -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; } diff --git a/include/utils/ColorRgb.h b/include/utils/ColorRgb.h index 0ec41c71..7d4bba28 100644 --- a/include/utils/ColorRgb.h +++ b/include/utils/ColorRgb.h @@ -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; } diff --git a/include/utils/ColorRgba.h b/include/utils/ColorRgba.h index e02cc796..63afdc5a 100644 --- a/include/utils/ColorRgba.h +++ b/include/utils/ColorRgba.h @@ -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; } diff --git a/include/utils/ColorRgbw.h b/include/utils/ColorRgbw.h index 9316f94f..c8ba82fc 100644 --- a/include/utils/ColorRgbw.h +++ b/include/utils/ColorRgbw.h @@ -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; } diff --git a/include/utils/ColorSys.h b/include/utils/ColorSys.h index b094a0d6..7aa1a065 100644 --- a/include/utils/ColorSys.h +++ b/include/utils/ColorSys.h @@ -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); }; diff --git a/include/utils/GlobalSignals.h b/include/utils/GlobalSignals.h index adcd2506..213ab955 100644 --- a/include/utils/GlobalSignals.h +++ b/include/utils/GlobalSignals.h @@ -20,6 +20,7 @@ public: static GlobalSignals instance; return & instance; } + private: GlobalSignals() = default; diff --git a/include/utils/ImageResampler.h b/include/utils/ImageResampler.h index 847db2bb..62adb2b9 100644 --- a/include/utils/ImageResampler.h +++ b/include/utils/ImageResampler.h @@ -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 & 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; }; + diff --git a/include/utils/JsonUtils.h b/include/utils/JsonUtils.h index c35f8c4f..694e279f 100644 --- a/include/utils/JsonUtils.h +++ b/include/utils/JsonUtils.h @@ -5,7 +5,7 @@ #include #include -namespace JsonUtils{ +namespace JsonUtils { /// /// @brief read a json file and get the parsed result on success /// @param[in] path The file path to read diff --git a/include/utils/Profiler.h b/include/utils/Profiler.h index 96aec725..236dad7b 100644 --- a/include/utils/Profiler.h +++ b/include/utils/Profiler.h @@ -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: diff --git a/include/utils/RgbToRgbw.h b/include/utils/RgbToRgbw.h index 0c6c9aee..ecfa78af 100644 --- a/include/utils/RgbToRgbw.h +++ b/include/utils/RgbToRgbw.h @@ -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); } diff --git a/include/utils/SysInfo.h b/include/utils/SysInfo.h index eb632e69..c8dca5d7 100644 --- a/include/utils/SysInfo.h +++ b/include/utils/SysInfo.h @@ -5,7 +5,6 @@ class SysInfo : public QObject { - public: struct HyperionSysInfo { diff --git a/libsrc/utils/ColorArgb.cpp b/libsrc/utils/ColorArgb.cpp index 5a9f0c05..4d4c8e82 100644 --- a/libsrc/utils/ColorArgb.cpp +++ b/libsrc/utils/ColorArgb.cpp @@ -1,10 +1,9 @@ - // Utils includes #include -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 }; diff --git a/libsrc/utils/ColorBgr.cpp b/libsrc/utils/ColorBgr.cpp index 3167925e..54f0ab58 100644 --- a/libsrc/utils/ColorBgr.cpp +++ b/libsrc/utils/ColorBgr.cpp @@ -1,11 +1,10 @@ - // Local includes #include -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 }; diff --git a/libsrc/utils/ColorRgb.cpp b/libsrc/utils/ColorRgb.cpp index 7e8c8769..157fcf29 100644 --- a/libsrc/utils/ColorRgb.cpp +++ b/libsrc/utils/ColorRgb.cpp @@ -1,10 +1,9 @@ - // Local includes #include -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 }; diff --git a/libsrc/utils/ColorRgba.cpp b/libsrc/utils/ColorRgba.cpp index a14f1e4c..5736d799 100644 --- a/libsrc/utils/ColorRgba.cpp +++ b/libsrc/utils/ColorRgba.cpp @@ -1,10 +1,9 @@ - // Utils includes #include -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 }; diff --git a/libsrc/utils/ColorRgbw.cpp b/libsrc/utils/ColorRgbw.cpp index af7cf9da..dd8e072c 100644 --- a/libsrc/utils/ColorRgbw.cpp +++ b/libsrc/utils/ColorRgbw.cpp @@ -1,10 +1,9 @@ - // Local includes #include -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 }; diff --git a/libsrc/utils/ColorSys.cpp b/libsrc/utils/ColorSys.cpp index 5ba00254..bf3278fd 100644 --- a/libsrc/utils/ColorSys.cpp +++ b/libsrc/utils/ColorSys.cpp @@ -2,6 +2,11 @@ #include +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); +} diff --git a/libsrc/utils/ImageResampler.cpp b/libsrc/utils/ImageResampler.cpp index 83aa137d..faa5f575 100644 --- a/libsrc/utils/ImageResampler.cpp +++ b/libsrc/utils/ImageResampler.cpp @@ -1,5 +1,5 @@ #include "utils/ImageResampler.h" - +#include #include 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); -} diff --git a/libsrc/utils/RgbToRgbw.cpp b/libsrc/utils/RgbToRgbw.cpp index 23a9257b..a329439e 100644 --- a/libsrc/utils/RgbToRgbw.cpp +++ b/libsrc/utils/RgbToRgbw.cpp @@ -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) {