]> rtime.felk.cvut.cz Git - frescor/ffmpeg.git/blob - libavfilter/parseutils.c
Implement av_parse_color().
[frescor/ffmpeg.git] / libavfilter / parseutils.c
1 /*
2  * copyright (c) 2009 Stefano Sabatini
3  * This file is part of FFmpeg.
4  *
5  * FFmpeg is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * FFmpeg is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with FFmpeg; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19
20 /**
21  * @file libavfilter/parseutils.c
22  * parsing utils
23  */
24
25 #include <string.h>
26 #include "libavutil/avutil.h"
27 #include "parseutils.h"
28
29 #define WHITESPACES " \n\t"
30
31 char *av_get_token(const char **buf, const char *term)
32 {
33     char *out = av_malloc(strlen(*buf) + 1);
34     char *ret= out, *end= out;
35     const char *p = *buf;
36     p += strspn(p, WHITESPACES);
37
38     while(*p && !strspn(p, term)) {
39         char c = *p++;
40         if(c == '\\' && *p){
41             *out++ = *p++;
42             end= out;
43         }else if(c == '\''){
44             while(*p && *p != '\'')
45                 *out++ = *p++;
46             if(*p){
47                 p++;
48                 end= out;
49             }
50         }else{
51             *out++ = c;
52         }
53     }
54
55     do{
56         *out-- = 0;
57     }while(out >= end && strspn(out, WHITESPACES));
58
59     *buf = p;
60
61     return ret;
62 }
63
64 typedef struct {
65     const char *name;            ///< a string representing the name of the color
66     uint8_t     rgba_color[4];   ///< RGBA values for the color
67 } ColorEntry;
68
69 static ColorEntry color_table[] = {
70     { "AliceBlue",            { 0xF0, 0xF8, 0xFF } },
71     { "AntiqueWhite",         { 0xFA, 0xEB, 0xD7 } },
72     { "Aqua",                 { 0x00, 0xFF, 0xFF } },
73     { "Aquamarine",           { 0x7F, 0xFF, 0xD4 } },
74     { "Azure",                { 0xF0, 0xFF, 0xFF } },
75     { "Beige",                { 0xF5, 0xF5, 0xDC } },
76     { "Bisque",               { 0xFF, 0xE4, 0xC4 } },
77     { "Black",                { 0x00, 0x00, 0x00 } },
78     { "BlanchedAlmond",       { 0xFF, 0xEB, 0xCD } },
79     { "Blue",                 { 0x00, 0x00, 0xFF } },
80     { "BlueViolet",           { 0x8A, 0x2B, 0xE2 } },
81     { "Brown",                { 0xA5, 0x2A, 0x2A } },
82     { "BurlyWood",            { 0xDE, 0xB8, 0x87 } },
83     { "CadetBlue",            { 0x5F, 0x9E, 0xA0 } },
84     { "Chartreuse",           { 0x7F, 0xFF, 0x00 } },
85     { "Chocolate",            { 0xD2, 0x69, 0x1E } },
86     { "Coral",                { 0xFF, 0x7F, 0x50 } },
87     { "CornflowerBlue",       { 0x64, 0x95, 0xED } },
88     { "Cornsilk",             { 0xFF, 0xF8, 0xDC } },
89     { "Crimson",              { 0xDC, 0x14, 0x3C } },
90     { "Cyan",                 { 0x00, 0xFF, 0xFF } },
91     { "DarkBlue",             { 0x00, 0x00, 0x8B } },
92     { "DarkCyan",             { 0x00, 0x8B, 0x8B } },
93     { "DarkGoldenRod",        { 0xB8, 0x86, 0x0B } },
94     { "DarkGray",             { 0xA9, 0xA9, 0xA9 } },
95     { "DarkGreen",            { 0x00, 0x64, 0x00 } },
96     { "DarkKhaki",            { 0xBD, 0xB7, 0x6B } },
97     { "DarkMagenta",          { 0x8B, 0x00, 0x8B } },
98     { "DarkOliveGreen",       { 0x55, 0x6B, 0x2F } },
99     { "Darkorange",           { 0xFF, 0x8C, 0x00 } },
100     { "DarkOrchid",           { 0x99, 0x32, 0xCC } },
101     { "DarkRed",              { 0x8B, 0x00, 0x00 } },
102     { "DarkSalmon",           { 0xE9, 0x96, 0x7A } },
103     { "DarkSeaGreen",         { 0x8F, 0xBC, 0x8F } },
104     { "DarkSlateBlue",        { 0x48, 0x3D, 0x8B } },
105     { "DarkSlateGray",        { 0x2F, 0x4F, 0x4F } },
106     { "DarkTurquoise",        { 0x00, 0xCE, 0xD1 } },
107     { "DarkViolet",           { 0x94, 0x00, 0xD3 } },
108     { "DeepPink",             { 0xFF, 0x14, 0x93 } },
109     { "DeepSkyBlue",          { 0x00, 0xBF, 0xFF } },
110     { "DimGray",              { 0x69, 0x69, 0x69 } },
111     { "DodgerBlue",           { 0x1E, 0x90, 0xFF } },
112     { "FireBrick",            { 0xB2, 0x22, 0x22 } },
113     { "FloralWhite",          { 0xFF, 0xFA, 0xF0 } },
114     { "ForestGreen",          { 0x22, 0x8B, 0x22 } },
115     { "Fuchsia",              { 0xFF, 0x00, 0xFF } },
116     { "Gainsboro",            { 0xDC, 0xDC, 0xDC } },
117     { "GhostWhite",           { 0xF8, 0xF8, 0xFF } },
118     { "Gold",                 { 0xFF, 0xD7, 0x00 } },
119     { "GoldenRod",            { 0xDA, 0xA5, 0x20 } },
120     { "Gray",                 { 0x80, 0x80, 0x80 } },
121     { "Green",                { 0x00, 0x80, 0x00 } },
122     { "GreenYellow",          { 0xAD, 0xFF, 0x2F } },
123     { "HoneyDew",             { 0xF0, 0xFF, 0xF0 } },
124     { "HotPink",              { 0xFF, 0x69, 0xB4 } },
125     { "IndianRed",            { 0xCD, 0x5C, 0x5C } },
126     { "Indigo",               { 0x4B, 0x00, 0x82 } },
127     { "Ivory",                { 0xFF, 0xFF, 0xF0 } },
128     { "Khaki",                { 0xF0, 0xE6, 0x8C } },
129     { "Lavender",             { 0xE6, 0xE6, 0xFA } },
130     { "LavenderBlush",        { 0xFF, 0xF0, 0xF5 } },
131     { "LawnGreen",            { 0x7C, 0xFC, 0x00 } },
132     { "LemonChiffon",         { 0xFF, 0xFA, 0xCD } },
133     { "LightBlue",            { 0xAD, 0xD8, 0xE6 } },
134     { "LightCoral",           { 0xF0, 0x80, 0x80 } },
135     { "LightCyan",            { 0xE0, 0xFF, 0xFF } },
136     { "LightGoldenRodYellow", { 0xFA, 0xFA, 0xD2 } },
137     { "LightGrey",            { 0xD3, 0xD3, 0xD3 } },
138     { "LightGreen",           { 0x90, 0xEE, 0x90 } },
139     { "LightPink",            { 0xFF, 0xB6, 0xC1 } },
140     { "LightSalmon",          { 0xFF, 0xA0, 0x7A } },
141     { "LightSeaGreen",        { 0x20, 0xB2, 0xAA } },
142     { "LightSkyBlue",         { 0x87, 0xCE, 0xFA } },
143     { "LightSlateGray",       { 0x77, 0x88, 0x99 } },
144     { "LightSteelBlue",       { 0xB0, 0xC4, 0xDE } },
145     { "LightYellow",          { 0xFF, 0xFF, 0xE0 } },
146     { "Lime",                 { 0x00, 0xFF, 0x00 } },
147     { "LimeGreen",            { 0x32, 0xCD, 0x32 } },
148     { "Linen",                { 0xFA, 0xF0, 0xE6 } },
149     { "Magenta",              { 0xFF, 0x00, 0xFF } },
150     { "Maroon",               { 0x80, 0x00, 0x00 } },
151     { "MediumAquaMarine",     { 0x66, 0xCD, 0xAA } },
152     { "MediumBlue",           { 0x00, 0x00, 0xCD } },
153     { "MediumOrchid",         { 0xBA, 0x55, 0xD3 } },
154     { "MediumPurple",         { 0x93, 0x70, 0xD8 } },
155     { "MediumSeaGreen",       { 0x3C, 0xB3, 0x71 } },
156     { "MediumSlateBlue",      { 0x7B, 0x68, 0xEE } },
157     { "MediumSpringGreen",    { 0x00, 0xFA, 0x9A } },
158     { "MediumTurquoise",      { 0x48, 0xD1, 0xCC } },
159     { "MediumVioletRed",      { 0xC7, 0x15, 0x85 } },
160     { "MidnightBlue",         { 0x19, 0x19, 0x70 } },
161     { "MintCream",            { 0xF5, 0xFF, 0xFA } },
162     { "MistyRose",            { 0xFF, 0xE4, 0xE1 } },
163     { "Moccasin",             { 0xFF, 0xE4, 0xB5 } },
164     { "NavajoWhite",          { 0xFF, 0xDE, 0xAD } },
165     { "Navy",                 { 0x00, 0x00, 0x80 } },
166     { "OldLace",              { 0xFD, 0xF5, 0xE6 } },
167     { "Olive",                { 0x80, 0x80, 0x00 } },
168     { "OliveDrab",            { 0x6B, 0x8E, 0x23 } },
169     { "Orange",               { 0xFF, 0xA5, 0x00 } },
170     { "OrangeRed",            { 0xFF, 0x45, 0x00 } },
171     { "Orchid",               { 0xDA, 0x70, 0xD6 } },
172     { "PaleGoldenRod",        { 0xEE, 0xE8, 0xAA } },
173     { "PaleGreen",            { 0x98, 0xFB, 0x98 } },
174     { "PaleTurquoise",        { 0xAF, 0xEE, 0xEE } },
175     { "PaleVioletRed",        { 0xD8, 0x70, 0x93 } },
176     { "PapayaWhip",           { 0xFF, 0xEF, 0xD5 } },
177     { "PeachPuff",            { 0xFF, 0xDA, 0xB9 } },
178     { "Peru",                 { 0xCD, 0x85, 0x3F } },
179     { "Pink",                 { 0xFF, 0xC0, 0xCB } },
180     { "Plum",                 { 0xDD, 0xA0, 0xDD } },
181     { "PowderBlue",           { 0xB0, 0xE0, 0xE6 } },
182     { "Purple",               { 0x80, 0x00, 0x80 } },
183     { "Red",                  { 0xFF, 0x00, 0x00 } },
184     { "RosyBrown",            { 0xBC, 0x8F, 0x8F } },
185     { "RoyalBlue",            { 0x41, 0x69, 0xE1 } },
186     { "SaddleBrown",          { 0x8B, 0x45, 0x13 } },
187     { "Salmon",               { 0xFA, 0x80, 0x72 } },
188     { "SandyBrown",           { 0xF4, 0xA4, 0x60 } },
189     { "SeaGreen",             { 0x2E, 0x8B, 0x57 } },
190     { "SeaShell",             { 0xFF, 0xF5, 0xEE } },
191     { "Sienna",               { 0xA0, 0x52, 0x2D } },
192     { "Silver",               { 0xC0, 0xC0, 0xC0 } },
193     { "SkyBlue",              { 0x87, 0xCE, 0xEB } },
194     { "SlateBlue",            { 0x6A, 0x5A, 0xCD } },
195     { "SlateGray",            { 0x70, 0x80, 0x90 } },
196     { "Snow",                 { 0xFF, 0xFA, 0xFA } },
197     { "SpringGreen",          { 0x00, 0xFF, 0x7F } },
198     { "SteelBlue",            { 0x46, 0x82, 0xB4 } },
199     { "Tan",                  { 0xD2, 0xB4, 0x8C } },
200     { "Teal",                 { 0x00, 0x80, 0x80 } },
201     { "Thistle",              { 0xD8, 0xBF, 0xD8 } },
202     { "Tomato",               { 0xFF, 0x63, 0x47 } },
203     { "Turquoise",            { 0x40, 0xE0, 0xD0 } },
204     { "Violet",               { 0xEE, 0x82, 0xEE } },
205     { "Wheat",                { 0xF5, 0xDE, 0xB3 } },
206     { "White",                { 0xFF, 0xFF, 0xFF } },
207     { "WhiteSmoke",           { 0xF5, 0xF5, 0xF5 } },
208     { "Yellow",               { 0xFF, 0xFF, 0x00 } },
209     { "YellowGreen",          { 0x9A, 0xCD, 0x32 } },
210 };
211
212 static int color_table_compare(const void *lhs, const void *rhs)
213 {
214     return strcmp(lhs, ((const ColorEntry *)rhs)->name);
215 }
216
217 int av_parse_color(uint8_t *rgba_color, const char *color_string, void *log_ctx)
218 {
219     if (!strncmp(color_string, "0x", 2)) {
220         char *tail;
221         int len = strlen(color_string);
222         int rgba = strtol(color_string, &tail, 16);
223
224         if (*tail || (len != 8 && len != 10)) {
225             av_log(log_ctx, AV_LOG_ERROR, "Invalid 0xRRGGBB[AA] color string: '%s'\n", color_string);
226             return -1;
227         }
228         if (len == 10) {
229             rgba_color[3] = rgba;
230             rgba >>= 8;
231         }
232         rgba_color[0] = rgba >> 16;
233         rgba_color[1] = rgba >> 8;
234         rgba_color[2] = rgba;
235     } else {
236         const ColorEntry *entry = bsearch(color_string,
237                                           color_table,
238                                           FF_ARRAY_ELEMS(color_table),
239                                           sizeof(ColorEntry),
240                                           color_table_compare);
241         if (!entry) {
242             av_log(log_ctx, AV_LOG_DEBUG, "Cannot find color '%s'\n", color_string);
243             return -1;
244         }
245         memcpy(rgba_color, entry->rgba_color, 4);
246     }
247
248     return 0;
249 }
250
251 #ifdef TEST
252
253 #undef printf
254
255 int main(void)
256 {
257     int i;
258
259     const char *strings[] = {
260         "''",
261         "",
262         ":",
263         "\\",
264         "'",
265         "    ''    :",
266         "    ''  ''  :",
267         "foo   '' :",
268         "'foo'",
269         "foo     ",
270         "foo\\",
271         "foo':  blah:blah",
272         "foo\\:  blah:blah",
273         "foo\'",
274         "'foo :  '  :blahblah",
275         "\\ :blah",
276         "     foo",
277         "      foo       ",
278         "      foo     \\ ",
279         "foo ':blah",
280         " foo   bar    :   blahblah",
281         "\\f\\o\\o",
282         "'foo : \\ \\  '   : blahblah",
283         "'\\fo\\o:': blahblah",
284         "\\'fo\\o\\:':  foo  '  :blahblah"
285     };
286
287     for (i=0; i < FF_ARRAY_ELEMS(strings); i++) {
288         const char *p= strings[i];
289         printf("|%s|", p);
290         printf(" -> |%s|", av_get_token(&p, ":"));
291         printf(" + |%s|\n", p);
292     }
293
294     printf("\nTesting av_parse_color()\n");
295     {
296         uint8_t rgba[4];
297         const char *color_names[] = {
298             "foo",
299             "red",
300             "Red ",
301             "RED",
302             "Violet",
303             "Yellow",
304             "Red",
305             "0x000000",
306             "0x0000000",
307             "0x3e34ff",
308             "0x3e34ffaa",
309             "0xffXXee",
310             "0xfoobar",
311             "0xffffeeeeeeee",
312         };
313
314         av_log_set_level(AV_LOG_DEBUG);
315
316         for (int i = 0;  i < FF_ARRAY_ELEMS(color_names); i++) {
317             if (av_parse_color(rgba, color_names[i], NULL) >= 0)
318                 printf("%s -> R(%d) G(%d) B(%d) A(%d)\n", color_names[i], rgba[0], rgba[1], rgba[2], rgba[3]);
319         }
320     }
321
322     return 0;
323 }
324
325 #endif