You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

335 lines
12KB

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