|
73 | 73 | #include "video_capture/testcard_common.h" |
74 | 74 | #include "video_pattern_generator.h" |
75 | 75 |
|
76 | | -#define BLANK_USAGE "blank[=0x<AABBGGRR>]" |
| 76 | +#define BLANK_USAGE "blank[=<color>|help]" |
77 | 77 | #define MOD_NAME "[vid. patt. generator] " |
78 | 78 | constexpr size_t headroom = 128; // headroom for cases when dst color_spec has wider block size |
79 | 79 | constexpr int rg48_bpp = 6; |
@@ -101,6 +101,55 @@ enum class generator_depth { |
101 | 101 | bits16 ///< RG48 |
102 | 102 | }; |
103 | 103 |
|
| 104 | +/** |
| 105 | + * @todo move to some of common files (video* ?) and link |
| 106 | + * externally later when there will be more users of the function. |
| 107 | + * @reutrns 4B color (uint32_t) or -1 if error |
| 108 | + */ |
| 109 | +static long long |
| 110 | +get_color_by_name(const char *col) |
| 111 | +{ |
| 112 | + static const struct { |
| 113 | + const char *name; |
| 114 | + uint32_t val; |
| 115 | + } colors[] = { |
| 116 | + { "white", 0xFFFFFFU }, |
| 117 | + { "gray", 0x7F7F7FU }, |
| 118 | + { "black", 0x000000U }, |
| 119 | + { "red", 0x0000FFU }, |
| 120 | + { "green", 0x00FF00U }, |
| 121 | + { "blue", 0xFF0000U }, |
| 122 | + { "yellow", 0x00FFFFU }, |
| 123 | + { "magenta", 0xFF00FFU }, |
| 124 | + { "cyan", 0xFFFF00U }, |
| 125 | + }; |
| 126 | + |
| 127 | + if (strcmp(col, "help") == 0) { |
| 128 | + color_printf("color in format " TBOLD("0x<AABBGGRR>") " or a symbolic name\n"); |
| 129 | + color_printf("Leading zeros can be omitted, eg. " |
| 130 | + "`0xFF` produces a red pattern.\n"); |
| 131 | + color_printf("\nfollowing symbolic names can be also used:\n"); |
| 132 | + for (unsigned i = 0; i < ARR_COUNT(colors); ++i) { |
| 133 | + color_printf("\t- " TBOLD("%s") "\n", colors[i].name); |
| 134 | + } |
| 135 | + color_printf("\n"); |
| 136 | + return -1; |
| 137 | + } |
| 138 | + char *endptr = nullptr; |
| 139 | + errno = 0; |
| 140 | + unsigned long val = strtoul(col, &endptr, 0); |
| 141 | + if (errno == 0 && endptr[0] == '\0') { |
| 142 | + return (uint32_t) val; |
| 143 | + } |
| 144 | + for (unsigned i = 0; i < ARR_COUNT(colors); ++i) { |
| 145 | + if (strcasecmp(col, colors[i].name) == 0) { |
| 146 | + return 0xFFU << 24 | colors[i].val; |
| 147 | + } |
| 148 | + } |
| 149 | + MSG(ERROR, "Unknown color: %s. Use 'help' for possible values.\n", col); |
| 150 | + return -1; |
| 151 | +} |
| 152 | + |
104 | 153 | class image_pattern { |
105 | 154 | public: |
106 | 155 | static unique_ptr<image_pattern> create(string const &pattern, string const ¶ms); |
@@ -327,16 +376,18 @@ class image_pattern_blank : public image_pattern { |
327 | 376 | if (init == "help"s) { |
328 | 377 | color_printf("Testcard " TBOLD("blank") " usage:\n"); |
329 | 378 | color_printf("\t" TRED(TBOLD( |
330 | | - "-t testcard:patt=" BLANK_USAGE)) "\n"); |
331 | | - color_printf( |
332 | | - "\nLeading zeros can be omitted, eg. " |
333 | | - "`0xFF` produces a red pattern.\n"); |
| 379 | + "-t testcard:patt=" BLANK_USAGE)) "\n\n"); |
| 380 | + get_color_by_name("help"); |
334 | 381 | color_printf( |
335 | 382 | "Defaults to 0xFF000000.\n"); |
336 | 383 | throw 1; |
337 | 384 | } |
338 | 385 | if (!init.empty()) { |
339 | | - color = stoll(init, nullptr, 0); |
| 386 | + const long long c = get_color_by_name(init.c_str()); |
| 387 | + if (c == -1) { |
| 388 | + throw 1; |
| 389 | + } |
| 390 | + color = c; |
340 | 391 | } |
341 | 392 | } |
342 | 393 |
|
|
0 commit comments