[FFmpeg-devel] [PATCH 2/2] Extend color syntax, make it accept an alpha component specifier.

Stefano Sabatini stefano.sabatini-lala
Fri Jun 11 00:34:21 CEST 2010


---
 libavfilter/parseutils.c |   51 +++++++++++++++++++++++++++++++++++++++++++--
 libavfilter/parseutils.h |    8 ++++++-
 2 files changed, 55 insertions(+), 4 deletions(-)

diff --git a/libavfilter/parseutils.c b/libavfilter/parseutils.c
index 9ac61d8..979f2a8 100644
--- a/libavfilter/parseutils.c
+++ b/libavfilter/parseutils.c
@@ -24,6 +24,7 @@
 
 #include <strings.h>
 #include "libavutil/avutil.h"
+#include "libavutil/avstring.h"
 #include "libavutil/random_seed.h"
 #include "parseutils.h"
 
@@ -215,6 +216,8 @@ static int color_table_compare(const void *lhs, const void *rhs)
     return strcasecmp(lhs, ((const ColorEntry *)rhs)->name);
 }
 
+#define ALPHA_SEP '@'
+
 int av_parse_color(uint8_t *rgba_color, const char *color_string, void *log_ctx)
 {
     if (!strcasecmp(color_string, "random") || !strcasecmp(color_string, "bikeshed")) {
@@ -236,21 +239,50 @@ int av_parse_color(uint8_t *rgba_color, const char *color_string, void *log_ctx)
         if (len == 10) {
             rgba_color[3] = rgba;
             rgba >>= 8;
-        }
+        } else
+            rgba_color[3] = 255;
+
         rgba_color[0] = rgba >> 16;
         rgba_color[1] = rgba >> 8;
         rgba_color[2] = rgba;
     } else {
-        const ColorEntry *entry = bsearch(color_string,
+        char *tail, color_string2[128];
+        const ColorEntry *entry;
+        av_strlcpy(color_string2, color_string, sizeof(color_string2));
+        if ((tail = strchr(color_string2, ALPHA_SEP)))
+            *tail++ = 0;
+        entry = bsearch(color_string2,
                                           color_table,
                                           FF_ARRAY_ELEMS(color_table),
                                           sizeof(ColorEntry),
                                           color_table_compare);
         if (!entry) {
-            av_log(log_ctx, AV_LOG_ERROR, "Cannot find color '%s'\n", color_string);
+            av_log(log_ctx, AV_LOG_ERROR, "Cannot find color '%s'\n", color_string2);
             return AVERROR(EINVAL);
         }
         memcpy(rgba_color, entry->rgba_color, 4);
+        rgba_color[3] = 255;
+
+        if (tail) {
+            unsigned long int alpha;
+            const char *alpha_string = tail;
+            if (!strncmp(alpha_string, "0x", 2)) {
+                alpha = strtoul(alpha_string, &tail, 16);
+            } else {
+                alpha = strtoul(alpha_string, &tail, 10);
+                if (*tail) {
+                    double d = strtod(alpha_string, &tail);
+                    alpha = d * 255;
+                }
+            }
+
+            if (tail == alpha_string || *tail || alpha > 255) {
+                av_log(log_ctx, AV_LOG_ERROR, "Invalid alpha value specifier '%s' in '%s'\n",
+                       alpha_string, color_string);
+                return AVERROR(EINVAL);
+            }
+            rgba_color[3] = alpha;
+        }
     }
 
     return 0;
@@ -420,6 +452,19 @@ int main(void)
             "0xffXXee",
             "0xfoobar",
             "0xffffeeeeeeee",
+            "red at foo",
+            "red@",
+            "red at 0xfff",
+            "red at 0xf",
+            "red at 2",
+            "red at 0.1",
+            "red at -1",
+            "red at 0.5",
+            "red at 1.0",
+            "red at 256",
+            "red at 10foo",
+            "red at -1.0",
+            "red at -0.0",
         };
 
         av_log_set_level(AV_LOG_DEBUG);
diff --git a/libavfilter/parseutils.h b/libavfilter/parseutils.h
index b5b494e..fc0c784 100644
--- a/libavfilter/parseutils.h
+++ b/libavfilter/parseutils.h
@@ -46,7 +46,13 @@ char *av_get_token(const char **buf, const char *term);
  * Puts the RGBA values that correspond to color_string in rgba_color.
  *
  * @param color_string a string specifying a color. It can be the name of
- * a color (case insensitive match) or a 0xRRGGBB[AA] sequence.
+ * a color (case insensitive match) eventually followed by "@" and a
+ * string representing the alpha component, or a 0xRRGGBB[AA]
+ * sequence.
+ * The alpha component may be a string composed by "0x" followed by an
+ * hexadecimal number or a base-10 number between 0 and 255, or a
+ * decimal number between 0.0 and 1.0, which represents the opacity
+ * value. If the alpha component is not specified then 255 is assumed.
  * The string "random" will result in a random color.
  * @return >= 0 in case of success, a negative value in case of
  * failure (for example if color_string cannot be parsed).
-- 
1.7.1




More information about the ffmpeg-devel mailing list