/src/core/ParseUtils.cpp

http://github.com/imageworks/OpenColorIO · C++ · 510 lines · 382 code · 90 blank · 38 comment · 230 complexity · 375bac55a1b3a0a108d30e042d2766b3 MD5 · raw file

  1. /*
  2. Copyright (c) 2003-2010 Sony Pictures Imageworks Inc., et al.
  3. All Rights Reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are
  6. met:
  7. * Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. * Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in the
  11. documentation and/or other materials provided with the distribution.
  12. * Neither the name of Sony Pictures Imageworks nor the names of its
  13. contributors may be used to endorse or promote products derived from
  14. this software without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  16. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  17. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  18. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  19. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  21. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  22. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  23. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include <iostream>
  28. #include <set>
  29. #include <sstream>
  30. #include <OpenColorIO/OpenColorIO.h>
  31. #include "ParseUtils.h"
  32. #include "pystring/pystring.h"
  33. OCIO_NAMESPACE_ENTER
  34. {
  35. const char * BoolToString(bool val)
  36. {
  37. if(val) return "true";
  38. return "false";
  39. }
  40. bool BoolFromString(const char * s)
  41. {
  42. std::string str = pystring::lower(s);
  43. if((str == "true") || (str=="yes")) return true;
  44. return false;
  45. }
  46. const char * LoggingLevelToString(LoggingLevel level)
  47. {
  48. if(level == LOGGING_LEVEL_NONE) return "none";
  49. else if(level == LOGGING_LEVEL_WARNING) return "warning";
  50. else if(level == LOGGING_LEVEL_INFO) return "info";
  51. else if(level == LOGGING_LEVEL_DEBUG) return "debug";
  52. return "unknown";
  53. }
  54. LoggingLevel LoggingLevelFromString(const char * s)
  55. {
  56. std::string str = pystring::lower(s);
  57. if(str == "0" || str == "none") return LOGGING_LEVEL_NONE;
  58. else if(str == "1" || str == "warning") return LOGGING_LEVEL_WARNING;
  59. else if(str == "2" || str == "info") return LOGGING_LEVEL_INFO;
  60. else if(str == "3" || str == "debug") return LOGGING_LEVEL_DEBUG;
  61. return LOGGING_LEVEL_UNKNOWN;
  62. }
  63. const char * TransformDirectionToString(TransformDirection dir)
  64. {
  65. if(dir == TRANSFORM_DIR_FORWARD) return "forward";
  66. else if(dir == TRANSFORM_DIR_INVERSE) return "inverse";
  67. return "unknown";
  68. }
  69. TransformDirection TransformDirectionFromString(const char * s)
  70. {
  71. std::string str = pystring::lower(s);
  72. if(str == "forward") return TRANSFORM_DIR_FORWARD;
  73. else if(str == "inverse") return TRANSFORM_DIR_INVERSE;
  74. return TRANSFORM_DIR_UNKNOWN;
  75. }
  76. TransformDirection CombineTransformDirections(TransformDirection d1,
  77. TransformDirection d2)
  78. {
  79. // Any unknowns always combine to be unknown.
  80. if(d1 == TRANSFORM_DIR_UNKNOWN || d2 == TRANSFORM_DIR_UNKNOWN)
  81. return TRANSFORM_DIR_UNKNOWN;
  82. if(d1 == TRANSFORM_DIR_FORWARD && d2 == TRANSFORM_DIR_FORWARD)
  83. return TRANSFORM_DIR_FORWARD;
  84. if(d1 == TRANSFORM_DIR_INVERSE && d2 == TRANSFORM_DIR_INVERSE)
  85. return TRANSFORM_DIR_FORWARD;
  86. return TRANSFORM_DIR_INVERSE;
  87. }
  88. TransformDirection GetInverseTransformDirection(TransformDirection dir)
  89. {
  90. if(dir == TRANSFORM_DIR_FORWARD) return TRANSFORM_DIR_INVERSE;
  91. else if(dir == TRANSFORM_DIR_INVERSE) return TRANSFORM_DIR_FORWARD;
  92. return TRANSFORM_DIR_UNKNOWN;
  93. }
  94. const char * ColorSpaceDirectionToString(ColorSpaceDirection dir)
  95. {
  96. if(dir == COLORSPACE_DIR_TO_REFERENCE) return "to_reference";
  97. else if(dir == COLORSPACE_DIR_FROM_REFERENCE) return "from_reference";
  98. return "unknown";
  99. }
  100. ColorSpaceDirection ColorSpaceDirectionFromString(const char * s)
  101. {
  102. std::string str = pystring::lower(s);
  103. if(str == "to_reference") return COLORSPACE_DIR_TO_REFERENCE;
  104. else if(str == "from_reference") return COLORSPACE_DIR_FROM_REFERENCE;
  105. return COLORSPACE_DIR_UNKNOWN;
  106. }
  107. const char * BitDepthToString(BitDepth bitDepth)
  108. {
  109. if(bitDepth == BIT_DEPTH_UINT8) return "8ui";
  110. else if(bitDepth == BIT_DEPTH_UINT10) return "10ui";
  111. else if(bitDepth == BIT_DEPTH_UINT12) return "12ui";
  112. else if(bitDepth == BIT_DEPTH_UINT14) return "14ui";
  113. else if(bitDepth == BIT_DEPTH_UINT16) return "16ui";
  114. else if(bitDepth == BIT_DEPTH_UINT32) return "32ui";
  115. else if(bitDepth == BIT_DEPTH_F16) return "16f";
  116. else if(bitDepth == BIT_DEPTH_F32) return "32f";
  117. return "unknown";
  118. }
  119. BitDepth BitDepthFromString(const char * s)
  120. {
  121. std::string str = pystring::lower(s);
  122. if(str == "8ui") return BIT_DEPTH_UINT8;
  123. else if(str == "10ui") return BIT_DEPTH_UINT10;
  124. else if(str == "12ui") return BIT_DEPTH_UINT12;
  125. else if(str == "14ui") return BIT_DEPTH_UINT14;
  126. else if(str == "16ui") return BIT_DEPTH_UINT16;
  127. else if(str == "32ui") return BIT_DEPTH_UINT32;
  128. else if(str == "16f") return BIT_DEPTH_F16;
  129. else if(str == "32f") return BIT_DEPTH_F32;
  130. return BIT_DEPTH_UNKNOWN;
  131. }
  132. bool BitDepthIsFloat(BitDepth bitDepth)
  133. {
  134. if(bitDepth == BIT_DEPTH_F16) return true;
  135. else if(bitDepth == BIT_DEPTH_F32) return true;
  136. return false;
  137. }
  138. int BitDepthToInt(BitDepth bitDepth)
  139. {
  140. if(bitDepth == BIT_DEPTH_UINT8) return 8;
  141. else if(bitDepth == BIT_DEPTH_UINT10) return 10;
  142. else if(bitDepth == BIT_DEPTH_UINT12) return 12;
  143. else if(bitDepth == BIT_DEPTH_UINT14) return 14;
  144. else if(bitDepth == BIT_DEPTH_UINT16) return 16;
  145. else if(bitDepth == BIT_DEPTH_UINT32) return 32;
  146. return 0;
  147. }
  148. const char * AllocationToString(Allocation alloc)
  149. {
  150. if(alloc == ALLOCATION_UNIFORM) return "uniform";
  151. else if(alloc == ALLOCATION_LG2) return "lg2";
  152. return "unknown";
  153. }
  154. Allocation AllocationFromString(const char * s)
  155. {
  156. std::string str = pystring::lower(s);
  157. if(str == "uniform") return ALLOCATION_UNIFORM;
  158. else if(str == "lg2") return ALLOCATION_LG2;
  159. return ALLOCATION_UNKNOWN;
  160. }
  161. const char * InterpolationToString(Interpolation interp)
  162. {
  163. if(interp == INTERP_NEAREST) return "nearest";
  164. else if(interp == INTERP_LINEAR) return "linear";
  165. else if(interp == INTERP_TETRAHEDRAL) return "tetrahedral";
  166. else if(interp == INTERP_BEST) return "best";
  167. return "unknown";
  168. }
  169. Interpolation InterpolationFromString(const char * s)
  170. {
  171. std::string str = pystring::lower(s);
  172. if(str == "nearest") return INTERP_NEAREST;
  173. else if(str == "linear") return INTERP_LINEAR;
  174. else if(str == "tetrahedral") return INTERP_TETRAHEDRAL;
  175. else if(str == "best") return INTERP_BEST;
  176. return INTERP_UNKNOWN;
  177. }
  178. const char * GpuLanguageToString(GpuLanguage language)
  179. {
  180. if(language == GPU_LANGUAGE_CG) return "cg";
  181. else if(language == GPU_LANGUAGE_GLSL_1_0) return "glsl_1.0";
  182. else if(language == GPU_LANGUAGE_GLSL_1_3) return "glsl_1.3";
  183. return "unknown";
  184. }
  185. GpuLanguage GpuLanguageFromString(const char * s)
  186. {
  187. std::string str = pystring::lower(s);
  188. if(str == "cg") return GPU_LANGUAGE_CG;
  189. else if(str == "glsl_1.0") return GPU_LANGUAGE_GLSL_1_0;
  190. else if(str == "glsl_1.3") return GPU_LANGUAGE_GLSL_1_3;
  191. return GPU_LANGUAGE_UNKNOWN;
  192. }
  193. const char * EnvironmentModeToString(EnvironmentMode mode)
  194. {
  195. if(mode == ENV_ENVIRONMENT_LOAD_PREDEFINED) return "loadpredefined";
  196. else if(mode == ENV_ENVIRONMENT_LOAD_ALL) return "loadall";
  197. return "unknown";
  198. }
  199. EnvironmentMode EnvironmentModeFromString(const char * s)
  200. {
  201. std::string str = pystring::lower(s);
  202. if(str == "loadpredefined") return ENV_ENVIRONMENT_LOAD_PREDEFINED;
  203. else if(str == "loadall") return ENV_ENVIRONMENT_LOAD_ALL;
  204. return ENV_ENVIRONMENT_UNKNOWN;
  205. }
  206. const char * ROLE_DEFAULT = "default";
  207. const char * ROLE_REFERENCE = "reference";
  208. const char * ROLE_DATA = "data";
  209. const char * ROLE_COLOR_PICKING = "color_picking";
  210. const char * ROLE_SCENE_LINEAR = "scene_linear";
  211. const char * ROLE_COMPOSITING_LOG = "compositing_log";
  212. const char * ROLE_COLOR_TIMING = "color_timing";
  213. const char * ROLE_TEXTURE_PAINT = "texture_paint";
  214. const char * ROLE_MATTE_PAINT = "matte_paint";
  215. namespace
  216. {
  217. const int FLOAT_DECIMALS = 7;
  218. const int DOUBLE_DECIMALS = 16;
  219. }
  220. std::string FloatToString(float value)
  221. {
  222. std::ostringstream pretty;
  223. pretty.precision(FLOAT_DECIMALS);
  224. pretty << value;
  225. return pretty.str();
  226. }
  227. std::string FloatVecToString(const float * fval, unsigned int size)
  228. {
  229. if(size<=0) return "";
  230. std::ostringstream pretty;
  231. pretty.precision(FLOAT_DECIMALS);
  232. for(unsigned int i=0; i<size; ++i)
  233. {
  234. if(i!=0) pretty << " ";
  235. pretty << fval[i];
  236. }
  237. return pretty.str();
  238. }
  239. bool StringToFloat(float * fval, const char * str)
  240. {
  241. if(!str) return false;
  242. std::istringstream inputStringstream(str);
  243. float x;
  244. if(!(inputStringstream >> x))
  245. {
  246. return false;
  247. }
  248. if(fval) *fval = x;
  249. return true;
  250. }
  251. bool StringToInt(int * ival, const char * str, bool failIfLeftoverChars)
  252. {
  253. if(!str) return false;
  254. if(!ival) return false;
  255. std::istringstream i(str);
  256. char c=0;
  257. if (!(i >> *ival) || (failIfLeftoverChars && i.get(c))) return false;
  258. return true;
  259. }
  260. std::string DoubleToString(double value)
  261. {
  262. std::ostringstream pretty;
  263. pretty.precision(DOUBLE_DECIMALS);
  264. pretty << value;
  265. return pretty.str();
  266. }
  267. bool StringVecToFloatVec(std::vector<float> &floatArray,
  268. const std::vector<std::string> &lineParts)
  269. {
  270. floatArray.resize(lineParts.size());
  271. for(unsigned int i=0; i<lineParts.size(); i++)
  272. {
  273. std::istringstream inputStringstream(lineParts[i]);
  274. float x;
  275. if(!(inputStringstream >> x))
  276. {
  277. return false;
  278. }
  279. floatArray[i] = x;
  280. }
  281. return true;
  282. }
  283. bool StringVecToIntVec(std::vector<int> &intArray,
  284. const std::vector<std::string> &lineParts)
  285. {
  286. intArray.resize(lineParts.size());
  287. for(unsigned int i=0; i<lineParts.size(); i++)
  288. {
  289. std::istringstream inputStringstream(lineParts[i]);
  290. int x;
  291. if(!(inputStringstream >> x))
  292. {
  293. return false;
  294. }
  295. intArray[i] = x;
  296. }
  297. return true;
  298. }
  299. ////////////////////////////////////////////////////////////////////////////
  300. // read the next non empty line, and store it in 'line'
  301. // return 'true' on success
  302. bool nextline(std::istream &istream, std::string &line)
  303. {
  304. while ( istream.good() )
  305. {
  306. std::getline(istream, line);
  307. if(line.size() > 0 && line[line.size() - 1] == '\r')
  308. {
  309. line.resize(line.size() - 1);
  310. }
  311. if(!pystring::strip(line).empty())
  312. {
  313. return true;
  314. }
  315. }
  316. line = "";
  317. return false;
  318. }
  319. bool StrEqualsCaseIgnore(const std::string & a, const std::string & b)
  320. {
  321. return (pystring::lower(a) == pystring::lower(b));
  322. }
  323. // If a ',' is in the string, split on it
  324. // If a ':' is in the string, split on it
  325. // Otherwise, assume a single string.
  326. // Also, strip whitespace from all parts.
  327. void SplitStringEnvStyle(std::vector<std::string> & outputvec, const char * str)
  328. {
  329. if(!str) return;
  330. std::string s = pystring::strip(str);
  331. if(pystring::find(s, ",") > -1)
  332. {
  333. pystring::split(s, outputvec, ",");
  334. }
  335. else if(pystring::find(s, ":") > -1)
  336. {
  337. pystring::split(s, outputvec, ":");
  338. }
  339. else
  340. {
  341. outputvec.push_back(s);
  342. }
  343. for(unsigned int i=0; i<outputvec.size(); ++i)
  344. {
  345. outputvec[i] = pystring::strip(outputvec[i]);
  346. }
  347. }
  348. std::string JoinStringEnvStyle(const std::vector<std::string> & outputvec)
  349. {
  350. return pystring::join(", ", outputvec);
  351. }
  352. // Ordering and capitalization from vec1 is preserved
  353. std::vector<std::string> IntersectStringVecsCaseIgnore(const std::vector<std::string> & vec1,
  354. const std::vector<std::string> & vec2)
  355. {
  356. std::vector<std::string> newvec;
  357. std::set<std::string> allvalues;
  358. // Seed the set with all values from vec2
  359. for(unsigned int i=0; i<vec2.size(); ++i)
  360. {
  361. allvalues.insert(pystring::lower(vec2[i]));
  362. }
  363. for(unsigned int i=0; i<vec1.size(); ++i)
  364. {
  365. std::string key = pystring::lower(vec1[i]);
  366. if(allvalues.find(key) != allvalues.end())
  367. {
  368. newvec.push_back(vec1[i]);
  369. }
  370. }
  371. return newvec;
  372. }
  373. int FindInStringVecCaseIgnore(const std::vector<std::string> & vec, const std::string & str)
  374. {
  375. std::string teststr = pystring::lower(str);
  376. for(unsigned int i=0; i<vec.size(); ++i)
  377. {
  378. if(pystring::lower(vec[i]) == teststr) return static_cast<int>(i);
  379. }
  380. return -1;
  381. }
  382. }
  383. OCIO_NAMESPACE_EXIT
  384. ///////////////////////////////////////////////////////////////////////////////
  385. #ifdef OCIO_UNIT_TEST
  386. OCIO_NAMESPACE_USING
  387. #include "UnitTest.h"
  388. OIIO_ADD_TEST(ParseUtils, StringToInt)
  389. {
  390. int ival = 0;
  391. bool success = false;
  392. success = StringToInt(&ival, "", false);
  393. OIIO_CHECK_EQUAL(success, false);
  394. success = StringToInt(&ival, "9", false);
  395. OIIO_CHECK_EQUAL(success, true);
  396. OIIO_CHECK_EQUAL(ival, 9);
  397. success = StringToInt(&ival, " 10 ", false);
  398. OIIO_CHECK_EQUAL(success, true);
  399. OIIO_CHECK_EQUAL(ival, 10);
  400. success = StringToInt(&ival, " 101", true);
  401. OIIO_CHECK_EQUAL(success, true);
  402. OIIO_CHECK_EQUAL(ival, 101);
  403. success = StringToInt(&ival, " 11x ", false);
  404. OIIO_CHECK_EQUAL(success, true);
  405. OIIO_CHECK_EQUAL(ival, 11);
  406. success = StringToInt(&ival, " 12x ", true);
  407. OIIO_CHECK_EQUAL(success, false);
  408. success = StringToInt(&ival, "13", true);
  409. OIIO_CHECK_EQUAL(success, true);
  410. OIIO_CHECK_EQUAL(ival, 13);
  411. success = StringToInt(&ival, "-14", true);
  412. OIIO_CHECK_EQUAL(success, true);
  413. OIIO_CHECK_EQUAL(ival, -14);
  414. success = StringToInt(&ival, "x-15", false);
  415. OIIO_CHECK_EQUAL(success, false);
  416. success = StringToInt(&ival, "x-16", false);
  417. OIIO_CHECK_EQUAL(success, false);
  418. }
  419. #endif // OCIO_UNIT_TEST