From 21129d7fcb6ecd25ffc165e04ee79382b541ca90 Mon Sep 17 00:00:00 2001 From: Martin Pulec Date: Mon, 15 Aug 2022 14:40:26 +0200 Subject: [PATCH] image generators: unified initialization - either nothing or string passed to constructor - disallowed omitting "blank" when color with "blank=" given --- src/utils/video_pattern_generator.cpp | 75 ++++++++++++++------------- 1 file changed, 38 insertions(+), 37 deletions(-) diff --git a/src/utils/video_pattern_generator.cpp b/src/utils/video_pattern_generator.cpp index 4d52ac935..1fb391786 100644 --- a/src/utils/video_pattern_generator.cpp +++ b/src/utils/video_pattern_generator.cpp @@ -280,7 +280,11 @@ class image_pattern_smpte_bars : public image_pattern_ebu_smpte_bars<0xBFU, 7> { class image_pattern_blank : public image_pattern { public: - explicit image_pattern_blank(uint32_t c = 0xFF000000U) : color(c) {} + explicit image_pattern_blank(string const &init) { + if (!init.empty()) { + color = stoi(init, nullptr, 0); + } + } private: enum generator_depth fill(int width, int height, unsigned char *data) override { @@ -289,12 +293,16 @@ class image_pattern_blank : public image_pattern { } return generator_depth::bits8; } - uint32_t color; + uint32_t color = 0xFF000000U; }; class image_pattern_gradient : public image_pattern { public: - explicit image_pattern_gradient(uint32_t c) : color(c) {} + explicit image_pattern_gradient(const string &config) { + if (!config.empty()) { + color = stol(config, nullptr, 0); + } + } static constexpr uint32_t red = 0xFFU; private: enum generator_depth fill(int width, int height, unsigned char *data) override { @@ -310,14 +318,23 @@ class image_pattern_gradient : public image_pattern { } return generator_depth::bits8; } - uint32_t color; + uint32_t color = image_pattern_gradient::red;; }; class image_pattern_gradient2 : public image_pattern { public: - explicit image_pattern_gradient2(long maxval = 0XFFFFU) : val_max(maxval) {} + explicit image_pattern_gradient2(string const &config) { + if (config.empty()) { + return; + } + if (config == "help"s) { + cout << "Testcard gradient2 usage:\n\t-t testcard:gradient2[=maxval] - maxval is 16-bit number\n"; + throw 1; + } + val_max = stol(config, nullptr, 0); + } private: - const unsigned int val_max; + unsigned int val_max = 0XFFFFU; enum generator_depth fill(int width, int height, unsigned char *data) override { assert(width > 1); // avoid division by zero auto *ptr = reinterpret_cast(data); @@ -377,6 +394,9 @@ class image_pattern_raw : public image_pattern { if (config.empty()) { throw ug_runtime_error("Empty raw pattern is not allowed!"); } + if (config.substr(0, "0x"s.length()) == "0x") { // strip optional "0x" + config = config.substr(2); + } while (!config.empty()) { unsigned char byte = 0; if (sscanf(config.c_str(), "%2hhx", &byte) == 1) { @@ -409,52 +429,33 @@ unique_ptr image_pattern::create(string const &config) { pattern = config.substr(0, delim); params = config.substr(delim + 1); } - if (config == "bars") { + if (pattern == "bars") { return make_unique(); } - if (config == "blank") { - return make_unique(); + if (pattern == "blank") { + return make_unique(params); } - if (config == "ebu_bars") { + if (pattern == "ebu_bars") { return make_unique>(); } - if (pattern == "gradient2") { - if (!params.empty()) { - if (params == "help"s) { - cout << "Testcard gradient2 usage:\n\t-t testcard:gradient2[=maxval] - maxval is 16-bit resolution\n"; - return {}; - } - return make_unique(stol(params, nullptr, 0)); - } - return make_unique(); - } if (pattern == "gradient") { - uint32_t color = image_pattern_gradient::red; - if (!params.empty()) { - color = stol(params, nullptr, 0); - } - return make_unique(color); + return make_unique(params); + } + if (pattern == "gradient2") { + return make_unique(params); } if (config == "noise") { return make_unique(); } - if (config.substr(0, "raw=0x"s.length()) == "raw=0x") { - return make_unique(config.substr("raw=0x"s.length())); + if (pattern == "raw") { + return make_unique(params); } - if (config == "smpte_bars") { + if (pattern == "smpte_bars") { return make_unique(); } if (pattern == "uv_plane") { return make_unique(params); } - if (config.substr(0, "0x"s.length()) == "0x") { - uint32_t blank_color = 0U; - if (sscanf(config.substr("0x"s.length()).c_str(), "%x", &blank_color) == 1) { - return make_unique(blank_color); - } else { - LOG(LOG_LEVEL_ERROR) << MOD_NAME << "Wrong color!\n"; - } - } throw ug_runtime_error("Unknown pattern: "s + config + "!"s); } @@ -563,7 +564,7 @@ video_pattern_generator_t video_pattern_generator_create(std::string const & config, int width, int height, codec_t color_spec, int offset) { if (config == "help") { - col() << "Pattern to use, one of: " << TBOLD("bars, blank, ebu_bars, gradient[=0x], gradient2*, gray, noise, raw=0xXX[YYZZ..], smpte_bars, uv_plane[=], 0x\n"); + col() << "Pattern to use, one of: " << TBOLD("bars, blank[=0x], ebu_bars, gradient[=0x], gradient2*, gray, noise, raw=0xXX[YYZZ..], smpte_bars, uv_plane[=]\n"); col() << "\t\t- patterns " TBOLD("'gradient2'") ", " TBOLD("'noise'") " and " TBOLD("'uv_plane'") " generate full bit-depth patterns with"; for (codec_t c = VIDEO_CODEC_FIRST; c != VIDEO_CODEC_COUNT; c = static_cast(static_cast(c) + 1)) { if (get_decoder_from_to(RG48, c) != NULL) {