From 549c347d6feb2e94a810a720c97a8bf0f57317a1 Mon Sep 17 00:00:00 2001 From: Tomi Valkeinen Date: Mon, 4 May 2026 16:19:04 +0300 Subject: kms++util: Replace test-pattern generator with pixpat Switch draw_test_pattern() to call libpixpat (linked statically into libkms++util.so) instead of the in-tree pattern generator. Pixpat covers every pattern (kmstest, smpte, solid colors) and every pixel format the previous generator handled, so behavior is unchanged for all callers. Drop the now-unused machinery: conv.h and conv-*.h template writers, color16.h (RGB16/YUV16 plus conversions), the *_old / _single / _multi declarations that had no definitions or callers, and the c_draw_test_pattern C ABI which had no callers anywhere in the tree. RecStandard and ColorRange move from color16.h directly into kms++util.h, since they are still part of the public TestPatternOptions struct. --- kms++util/inc/kms++util/color16.h | 184 ------------------------------------ kms++util/inc/kms++util/kms++util.h | 30 +----- 2 files changed, 3 insertions(+), 211 deletions(-) delete mode 100644 kms++util/inc/kms++util/color16.h (limited to 'kms++util/inc') diff --git a/kms++util/inc/kms++util/color16.h b/kms++util/inc/kms++util/color16.h deleted file mode 100644 index 340ce6a..0000000 --- a/kms++util/inc/kms++util/color16.h +++ /dev/null @@ -1,184 +0,0 @@ -#pragma once - -#include -#include -#include - -namespace kms -{ - -enum class ColorRange { Limited, Full }; - -enum class RecStandard { BT601, BT709, BT2020 }; - -class YUV16; -class RGB16; - -// Conversion coefficients for different standards -struct ConversionCoefficients { - double kr; - double kg; - double kb; - - static constexpr ConversionCoefficients get(RecStandard rec) noexcept - { - switch (rec) { - case RecStandard::BT601: - return { 0.299, 0.587, 0.114 }; - default: // Default to BT709 - case RecStandard::BT709: - return { 0.2126, 0.7152, 0.0722 }; - case RecStandard::BT2020: - return { 0.2627, 0.6780, 0.0593 }; - } - } -}; - -// Range scaling factors -struct RangeScaling { - double y_min; - double y_max; - double c_min; - double c_max; - - static constexpr RangeScaling get(ColorRange range) noexcept - { - switch (range) { - case ColorRange::Limited: - return { - 16.0 / 255.0, // y_min - 235.0 / 255.0, // y_max - 16.0 / 255.0, // c_min - 240.0 / 255.0 // c_max - }; - case ColorRange::Full: - default: - return { 0.0, 1.0, 0.0, 1.0 }; - } - } -}; - -class RGB16 -{ -public: - uint16_t r = 0; - uint16_t g = 0; - uint16_t b = 0; - uint16_t a = 0; - - constexpr RGB16() noexcept = default; - constexpr RGB16(uint16_t r, uint16_t g, uint16_t b) noexcept : r(r), g(g), b(b), a(max_value) {} - constexpr RGB16(uint16_t r, uint16_t g, uint16_t b, uint16_t a) noexcept : r(r), g(g), b(b), a(a) {} - - static constexpr RGB16 from_8(uint8_t r, uint8_t g, uint8_t b) - { - return RGB16 { - static_cast((r << 8) | r), - static_cast((g << 8) | g), - static_cast((b << 8) | b), - }; - } - - [[nodiscard]] - constexpr YUV16 to_yuv(RecStandard rec = RecStandard::BT709, - ColorRange range = ColorRange::Limited) const noexcept; - - static constexpr uint16_t max_value = 0xffff; -}; - -class YUV16 -{ -public: - uint16_t y = 0; - uint16_t u = 0; - uint16_t v = 0; - uint16_t a = 0; - - constexpr YUV16() noexcept = default; - constexpr YUV16(uint16_t y, uint16_t u, uint16_t v) noexcept : y(y), u(u), v(v), a(max_value) {} - constexpr YUV16(uint16_t y, uint16_t u, uint16_t v, uint16_t a) noexcept : y(y), u(u), v(v), a(a) {} - - [[nodiscard]] - constexpr RGB16 to_rgb(RecStandard rec = RecStandard::BT709, - ColorRange range = ColorRange::Limited) const noexcept; - - - static constexpr YUV16 from_10(uint16_t r, uint16_t g, uint16_t b) - { - return YUV16 { - static_cast((r << 6) | (r >> 4)), - static_cast((g << 6) | (g >> 4)), - static_cast((b << 6) | (b >> 4)), - }; - } - - static constexpr YUV16 from_12(uint16_t r, uint16_t g, uint16_t b) - { - return YUV16 { - static_cast((r << 4) | (r >> 8)), - static_cast((g << 4) | (g >> 8)), - static_cast((b << 4) | (b >> 8)), - }; - } - - static constexpr uint16_t max_value = 0xffff; -}; - -constexpr YUV16 RGB16::to_yuv(RecStandard rec, ColorRange range) const noexcept -{ - const auto coeff = ConversionCoefficients::get(rec); - const auto scaling = RangeScaling::get(range); - - // Normalize RGB values to [0,1] - const double r_norm = static_cast(r) / max_value; - const double g_norm = static_cast(g) / max_value; - const double b_norm = static_cast(b) / max_value; - - // Calculate Y (unscaled) - double y_unscaled = coeff.kr * r_norm + coeff.kg * g_norm + coeff.kb * b_norm; - - // Calculate U and V using unscaled Y - double u = (b_norm - y_unscaled) / (2.0 * (1.0 - coeff.kb)); - double v = (r_norm - y_unscaled) / (2.0 * (1.0 - coeff.kr)); - - // Scale Y to target range - double y = y_unscaled * (scaling.y_max - scaling.y_min) + scaling.y_min; - - // Scale U and V to target range - u = u * (scaling.c_max - scaling.c_min) + (scaling.c_max + scaling.c_min) / 2.0; - v = v * (scaling.c_max - scaling.c_min) + (scaling.c_max + scaling.c_min) / 2.0; - - // Convert back to 16-bit values - return YUV16(static_cast(std::round(y * max_value)), - static_cast(std::round(u * max_value)), - static_cast(std::round(v * max_value))); -} - -constexpr RGB16 YUV16::to_rgb(RecStandard rec, ColorRange range) const noexcept -{ - const auto coeff = ConversionCoefficients::get(rec); - const auto scaling = RangeScaling::get(range); - - // Normalize YUV values to [0,1] - double y_norm = static_cast(y) / max_value; - double u_norm = static_cast(u) / max_value; - double v_norm = static_cast(v) / max_value; - - // Rescale from target range to [0,1] - y_norm = (y_norm - scaling.y_min) / (scaling.y_max - scaling.y_min); - u_norm = (u_norm - (scaling.c_max + scaling.c_min) / 2.0) / (scaling.c_max - scaling.c_min); - v_norm = (v_norm - (scaling.c_max + scaling.c_min) / 2.0) / (scaling.c_max - scaling.c_min); - - // Convert to RGB - const double r = y_norm + 2.0 * v_norm * (1.0 - coeff.kr); - const double g = y_norm - 2.0 * u_norm * (1.0 - coeff.kb) * coeff.kb / coeff.kg - - 2.0 * v_norm * (1.0 - coeff.kr) * coeff.kr / coeff.kg; - const double b = y_norm + 2.0 * u_norm * (1.0 - coeff.kb); - - // Clamp and convert back to 16-bit values - return RGB16(static_cast(std::round(std::clamp(r, 0.0, 1.0) * max_value)), - static_cast(std::round(std::clamp(g, 0.0, 1.0) * max_value)), - static_cast(std::round(std::clamp(b, 0.0, 1.0) * max_value))); -} - -} // namespace kms diff --git a/kms++util/inc/kms++util/kms++util.h b/kms++util/inc/kms++util/kms++util.h index 396d18c..be06cab 100644 --- a/kms++util/inc/kms++util/kms++util.h +++ b/kms++util/inc/kms++util/kms++util.h @@ -3,7 +3,6 @@ #include #include -#include #include #include #include @@ -18,6 +17,9 @@ namespace kms { class IFramebuffer; +enum class ColorRange { Limited, Full }; +enum class RecStandard { BT601, BT709, BT2020 }; + void draw_rgb_pixel(IFramebuffer& buf, unsigned x, unsigned y, RGB color); void draw_yuv444_pixel(IFramebuffer& buf, unsigned x, unsigned y, YUV yuv); void draw_yuv422_macropixel(IFramebuffer& buf, unsigned x, unsigned y, YUV yuv1, YUV yuv2); @@ -29,10 +31,6 @@ void draw_text(IFramebuffer& buf, uint32_t x, uint32_t y, const std::string& str void draw_color_bar(IFramebuffer& buf, int old_xpos, int xpos, int width); -void draw_test_pattern_old(IFramebuffer& fb, YUVType yuvt = YUVType::BT601_Lim); -void draw_test_pattern_single_old(IFramebuffer& fb, YUVType yuvt = YUVType::BT601_Lim); -void draw_test_pattern_multi_old(IFramebuffer& fb, YUVType yuvt = YUVType::BT601_Lim); - struct TestPatternOptions { std::string pattern; RecStandard rec = RecStandard::BT709; @@ -40,8 +38,6 @@ struct TestPatternOptions { }; void draw_test_pattern(IFramebuffer& fb, const TestPatternOptions& options = {}); -void draw_test_pattern_single(IFramebuffer& fb, const TestPatternOptions& options = {}); -void draw_test_pattern_multi(IFramebuffer& fb, const TestPatternOptions& options = {}); } // namespace kms #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0])) @@ -84,23 +80,3 @@ void draw_test_pattern_multi(IFramebuffer& fb, const TestPatternOptions& options fprintf(stderr, fmt "\n", ##__VA_ARGS__); \ exit(-1); \ } - -extern "C" { - -struct CDrawTestPatternParameters { - uint32_t width; - uint32_t height; - const char* format_name; - uint8_t* buffers[4]; - uint32_t sizes[4]; - uint32_t pitches[4]; - uint32_t offsets[4]; - - const char* pattern; - uint32_t rec_standard; - bool full_range; -}; - -int c_draw_test_pattern(struct CDrawTestPatternParameters* params); - -} -- cgit v1.2.3