Revision a52e2c3a

View differences:

libavfilter/parseutils.c
24 24

  
25 25
#include <strings.h>
26 26
#include "libavutil/avutil.h"
27
#include "libavutil/avstring.h"
27 28
#include "libavutil/random_seed.h"
28 29
#include "parseutils.h"
29 30

  
......
216 217
    return strcasecmp(lhs, ((const ColorEntry *)rhs)->name);
217 218
}
218 219

  
220
#define ALPHA_SEP '@'
221

  
219 222
int av_parse_color(uint8_t *rgba_color, const char *color_string, void *log_ctx)
220 223
{
221
    if (!strcasecmp(color_string, "random") || !strcasecmp(color_string, "bikeshed")) {
224
    char *tail, color_string2[128];
225
    const ColorEntry *entry;
226
    av_strlcpy(color_string2, color_string, sizeof(color_string2));
227
    if ((tail = strchr(color_string2, ALPHA_SEP)))
228
        *tail++ = 0;
229
    rgba_color[3] = 255;
230

  
231
    if (!strcasecmp(color_string2, "random") || !strcasecmp(color_string2, "bikeshed")) {
222 232
        int rgba = av_get_random_seed();
223 233
        rgba_color[0] = rgba >> 24;
224 234
        rgba_color[1] = rgba >> 16;
225 235
        rgba_color[2] = rgba >> 8;
226 236
        rgba_color[3] = rgba;
227 237
    } else
228
    if (!strncmp(color_string, "0x", 2)) {
238
    if (!strncmp(color_string2, "0x", 2)) {
229 239
        char *tail;
230
        int len = strlen(color_string);
231
        unsigned int rgba = strtoul(color_string, &tail, 16);
240
        int len = strlen(color_string2);
241
        unsigned int rgba = strtoul(color_string2, &tail, 16);
232 242

  
233 243
        if (*tail || (len != 8 && len != 10)) {
234
            av_log(log_ctx, AV_LOG_ERROR, "Invalid 0xRRGGBB[AA] color string: '%s'\n", color_string);
244
            av_log(log_ctx, AV_LOG_ERROR, "Invalid 0xRRGGBB[AA] color string: '%s'\n", color_string2);
235 245
            return AVERROR(EINVAL);
236 246
        }
237 247
        if (len == 10) {
......
242 252
        rgba_color[1] = rgba >> 8;
243 253
        rgba_color[2] = rgba;
244 254
    } else {
245
        const ColorEntry *entry = bsearch(color_string,
255
        entry = bsearch(color_string2,
246 256
                                          color_table,
247 257
                                          FF_ARRAY_ELEMS(color_table),
248 258
                                          sizeof(ColorEntry),
249 259
                                          color_table_compare);
250 260
        if (!entry) {
251
            av_log(log_ctx, AV_LOG_ERROR, "Cannot find color '%s'\n", color_string);
261
            av_log(log_ctx, AV_LOG_ERROR, "Cannot find color '%s'\n", color_string2);
252 262
            return AVERROR(EINVAL);
253 263
        }
254 264
        memcpy(rgba_color, entry->rgb_color, 3);
255 265
    }
256 266

  
267
    if (tail) {
268
        unsigned long int alpha;
269
        const char *alpha_string = tail;
270
        if (!strncmp(alpha_string, "0x", 2)) {
271
            alpha = strtoul(alpha_string, &tail, 16);
272
        } else {
273
            alpha = strtoul(alpha_string, &tail, 10);
274
            if (*tail) {
275
                double d = strtod(alpha_string, &tail);
276
                alpha = d * 255;
277
            }
278
        }
279

  
280
        if (tail == alpha_string || *tail || alpha > 255) {
281
            av_log(log_ctx, AV_LOG_ERROR, "Invalid alpha value specifier '%s' in '%s'\n",
282
                   alpha_string, color_string);
283
            return AVERROR(EINVAL);
284
        }
285
        rgba_color[3] = alpha;
286
    }
287

  
257 288
    return 0;
258 289
}
259 290

  
......
421 452
            "0xffXXee",
422 453
            "0xfoobar",
423 454
            "0xffffeeeeeeee",
455
            "red@foo",
456
            "random@10",
457
            "0xff0000@1.0",
458
            "red@",
459
            "red@0xfff",
460
            "red@0xf",
461
            "red@2",
462
            "red@0.1",
463
            "red@-1",
464
            "red@0.5",
465
            "red@1.0",
466
            "red@256",
467
            "red@10foo",
468
            "red@-1.0",
469
            "red@-0.0",
424 470
        };
425 471

  
426 472
        av_log_set_level(AV_LOG_DEBUG);
libavfilter/parseutils.h
46 46
 * Puts the RGBA values that correspond to color_string in rgba_color.
47 47
 *
48 48
 * @param color_string a string specifying a color. It can be the name of
49
 * a color (case insensitive match) or a 0xRRGGBB[AA] sequence.
49
 * a color (case insensitive match) or a 0xRRGGBB[AA] sequence,
50
 * possibly followed by "@" and a string representing the alpha
51
 * component.
52
 * The alpha component may be a string composed by "0x" followed by an
53
 * hexadecimal number or a base-10 number between 0 and 255, or a
54
 * decimal number between 0.0 and 1.0, which represents the opacity
55
 * value (0/0x00/0.0 means completely transparent, 255/0xff/1.0
56
 * completely opaque).
57
 * If the alpha component is not specified then 255 is assumed.
50 58
 * The string "random" will result in a random color.
51 59
 * @return >= 0 in case of success, a negative value in case of
52 60
 * failure (for example if color_string cannot be parsed).

Also available in: Unified diff