Skip to content

Commit 2833130

Browse files
committed
testcard pattern blank: supp for sym names
1 parent 41d3e19 commit 2833130

File tree

1 file changed

+57
-6
lines changed

1 file changed

+57
-6
lines changed

src/utils/video_pattern_generator.cpp

Lines changed: 57 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
#include "video_capture/testcard_common.h"
7474
#include "video_pattern_generator.h"
7575

76-
#define BLANK_USAGE "blank[=0x<AABBGGRR>]"
76+
#define BLANK_USAGE "blank[=<color>|help]"
7777
#define MOD_NAME "[vid. patt. generator] "
7878
constexpr size_t headroom = 128; // headroom for cases when dst color_spec has wider block size
7979
constexpr int rg48_bpp = 6;
@@ -101,6 +101,55 @@ enum class generator_depth {
101101
bits16 ///< RG48
102102
};
103103

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+
104153
class image_pattern {
105154
public:
106155
static unique_ptr<image_pattern> create(string const &pattern, string const &params);
@@ -327,16 +376,18 @@ class image_pattern_blank : public image_pattern {
327376
if (init == "help"s) {
328377
color_printf("Testcard " TBOLD("blank") " usage:\n");
329378
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");
334381
color_printf(
335382
"Defaults to 0xFF000000.\n");
336383
throw 1;
337384
}
338385
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;
340391
}
341392
}
342393

0 commit comments

Comments
 (0)