/src/core/LookTransform.cpp

http://github.com/imageworks/OpenColorIO · C++ · 405 lines · 306 code · 60 blank · 39 comment · 38 complexity · 7b816b11a8effe7358c5f2f7c429983b 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 <OpenColorIO/OpenColorIO.h>
  28. #include <algorithm>
  29. #include <iterator>
  30. #include "LookParse.h"
  31. #include "NoOps.h"
  32. #include "OpBuilders.h"
  33. #include "ParseUtils.h"
  34. #include "pystring/pystring.h"
  35. OCIO_NAMESPACE_ENTER
  36. {
  37. LookTransformRcPtr LookTransform::Create()
  38. {
  39. return LookTransformRcPtr(new LookTransform(), &deleter);
  40. }
  41. void LookTransform::deleter(LookTransform* t)
  42. {
  43. delete t;
  44. }
  45. class LookTransform::Impl
  46. {
  47. public:
  48. TransformDirection dir_;
  49. std::string src_;
  50. std::string dst_;
  51. std::string looks_;
  52. Impl() :
  53. dir_(TRANSFORM_DIR_FORWARD)
  54. { }
  55. ~Impl()
  56. { }
  57. Impl& operator= (const Impl & rhs)
  58. {
  59. dir_ = rhs.dir_;
  60. src_ = rhs.src_;
  61. dst_ = rhs.dst_;
  62. looks_ = rhs.looks_;
  63. return *this;
  64. }
  65. };
  66. ///////////////////////////////////////////////////////////////////////////
  67. LookTransform::LookTransform()
  68. : m_impl(new LookTransform::Impl)
  69. {
  70. }
  71. TransformRcPtr LookTransform::createEditableCopy() const
  72. {
  73. LookTransformRcPtr transform = LookTransform::Create();
  74. *(transform->m_impl) = *m_impl;
  75. return transform;
  76. }
  77. LookTransform::~LookTransform()
  78. {
  79. delete m_impl;
  80. m_impl = NULL;
  81. }
  82. LookTransform& LookTransform::operator= (const LookTransform & rhs)
  83. {
  84. *m_impl = *rhs.m_impl;
  85. return *this;
  86. }
  87. TransformDirection LookTransform::getDirection() const
  88. {
  89. return getImpl()->dir_;
  90. }
  91. void LookTransform::setDirection(TransformDirection dir)
  92. {
  93. getImpl()->dir_ = dir;
  94. }
  95. const char * LookTransform::getSrc() const
  96. {
  97. return getImpl()->src_.c_str();
  98. }
  99. void LookTransform::setSrc(const char * src)
  100. {
  101. getImpl()->src_ = src;
  102. }
  103. const char * LookTransform::getDst() const
  104. {
  105. return getImpl()->dst_.c_str();
  106. }
  107. void LookTransform::setDst(const char * dst)
  108. {
  109. getImpl()->dst_ = dst;
  110. }
  111. void LookTransform::setLooks(const char * looks)
  112. {
  113. getImpl()->looks_ = looks;
  114. }
  115. const char * LookTransform::getLooks() const
  116. {
  117. return getImpl()->looks_.c_str();
  118. }
  119. std::ostream& operator<< (std::ostream& os, const LookTransform& t)
  120. {
  121. os << "<LookTransform ";
  122. os << "src=" << t.getSrc() << ", ";
  123. os << "dst=" << t.getDst() << ", ";
  124. os << "looks=" << t.getLooks() << ", ";
  125. os << "direction=" << TransformDirectionToString(t.getDirection()) << ", ";
  126. os << ">\n";
  127. return os;
  128. }
  129. ////////////////////////////////////////////////////////////////////////////
  130. namespace
  131. {
  132. void RunLookTokens(OpRcPtrVec & ops,
  133. ConstColorSpaceRcPtr & currentColorSpace,
  134. bool skipColorSpaceConversions,
  135. const Config& config,
  136. const ConstContextRcPtr & context,
  137. const LookParseResult::Tokens & lookTokens)
  138. {
  139. if(lookTokens.empty()) return;
  140. for(unsigned int i=0; i<lookTokens.size(); ++i)
  141. {
  142. const std::string & lookName = lookTokens[i].name;
  143. if(lookName.empty()) continue;
  144. ConstLookRcPtr look = config.getLook(lookName.c_str());
  145. if(!look)
  146. {
  147. std::ostringstream os;
  148. os << "RunLookTokens error. ";
  149. os << "The specified look, '" << lookName;
  150. os << "', cannot be found. ";
  151. if(config.getNumLooks() == 0)
  152. {
  153. os << " (No looks defined in config)";
  154. }
  155. else
  156. {
  157. os << " (looks: ";
  158. for(int ii=0; ii<config.getNumLooks(); ++ii)
  159. {
  160. if(ii != 0) os << ", ";
  161. os << config.getLookNameByIndex(ii);
  162. }
  163. os << ")";
  164. }
  165. throw Exception(os.str().c_str());
  166. }
  167. // Put the new ops into a temp array, to see if it's a no-op
  168. // If it is a no-op, dont bother doing the colorspace conversion.
  169. OpRcPtrVec tmpOps;
  170. if(lookTokens[i].dir == TRANSFORM_DIR_FORWARD)
  171. {
  172. CreateLookNoOp(tmpOps, lookName);
  173. if(look->getTransform())
  174. {
  175. BuildOps(tmpOps, config, context, look->getTransform(), TRANSFORM_DIR_FORWARD);
  176. }
  177. else if(look->getInverseTransform())
  178. {
  179. BuildOps(tmpOps, config, context, look->getInverseTransform(), TRANSFORM_DIR_INVERSE);
  180. }
  181. }
  182. else if(lookTokens[i].dir == TRANSFORM_DIR_INVERSE)
  183. {
  184. CreateLookNoOp(tmpOps, std::string("-") + lookName);
  185. if(look->getInverseTransform())
  186. {
  187. BuildOps(tmpOps, config, context, look->getInverseTransform(), TRANSFORM_DIR_FORWARD);
  188. }
  189. else if(look->getTransform())
  190. {
  191. BuildOps(tmpOps, config, context, look->getTransform(), TRANSFORM_DIR_INVERSE);
  192. }
  193. }
  194. else
  195. {
  196. std::ostringstream os;
  197. os << "BuildLookOps error. ";
  198. os << "The specified look, '" << lookTokens[i].name;
  199. os << "' has an ill-defined transform direction.";
  200. throw Exception(os.str().c_str());
  201. }
  202. if(!IsOpVecNoOp(tmpOps))
  203. {
  204. if(!skipColorSpaceConversions)
  205. {
  206. ConstColorSpaceRcPtr processColorSpace = config.getColorSpace(look->getProcessSpace());
  207. if(!processColorSpace)
  208. {
  209. std::ostringstream os;
  210. os << "RunLookTokens error. ";
  211. os << "The specified look, '" << lookTokens[i].name;
  212. os << "', requires processing in the ColorSpace, '";
  213. os << look->getProcessSpace() << "' which is not defined.";
  214. throw Exception(os.str().c_str());
  215. }
  216. BuildColorSpaceOps(ops, config, context,
  217. currentColorSpace,
  218. processColorSpace);
  219. currentColorSpace = processColorSpace;
  220. }
  221. std::copy(tmpOps.begin(), tmpOps.end(), std::back_inserter(ops));
  222. }
  223. }
  224. }
  225. } // anon namespace
  226. ////////////////////////////////////////////////////////////////////////////
  227. void BuildLookOps(OpRcPtrVec & ops,
  228. const Config& config,
  229. const ConstContextRcPtr & context,
  230. const LookTransform & lookTransform,
  231. TransformDirection dir)
  232. {
  233. ConstColorSpaceRcPtr src, dst;
  234. src = config.getColorSpace( lookTransform.getSrc() );
  235. dst = config.getColorSpace( lookTransform.getDst() );
  236. if(!src)
  237. {
  238. std::ostringstream os;
  239. os << "BuildLookOps error.";
  240. os << "The specified lookTransform specifies a src colorspace, '";
  241. os << lookTransform.getSrc() << "', which is not defined.";
  242. throw Exception(os.str().c_str());
  243. }
  244. if(!dst)
  245. {
  246. std::ostringstream os;
  247. os << "BuildLookOps error.";
  248. os << "The specified lookTransform specifies a dst colorspace, '";
  249. os << lookTransform.getDst() << "', which is not defined.";
  250. throw Exception(os.str().c_str());
  251. }
  252. LookParseResult looks;
  253. looks.parse(lookTransform.getLooks());
  254. // We must handle the inverse src/dst colorspace transformation explicitly.
  255. if(dir == TRANSFORM_DIR_INVERSE)
  256. {
  257. std::swap(src, dst);
  258. looks.reverse();
  259. }
  260. else if(dir == TRANSFORM_DIR_UNKNOWN)
  261. {
  262. std::ostringstream os;
  263. os << "BuildLookOps error. A valid transform direction must be specified.";
  264. throw Exception(os.str().c_str());
  265. }
  266. ConstColorSpaceRcPtr currentColorSpace = src;
  267. BuildLookOps(ops,
  268. currentColorSpace,
  269. false,
  270. config,
  271. context,
  272. looks);
  273. BuildColorSpaceOps(ops, config, context,
  274. currentColorSpace,
  275. dst);
  276. }
  277. void BuildLookOps(OpRcPtrVec & ops,
  278. ConstColorSpaceRcPtr & currentColorSpace,
  279. bool skipColorSpaceConversions,
  280. const Config& config,
  281. const ConstContextRcPtr & context,
  282. const LookParseResult & looks)
  283. {
  284. const LookParseResult::Options & options = looks.getOptions();
  285. if(options.empty())
  286. {
  287. // Do nothing
  288. }
  289. else if(options.size() == 1)
  290. {
  291. // As an optimization, if we only have a single look option,
  292. // just push back onto the final location
  293. RunLookTokens(ops,
  294. currentColorSpace,
  295. skipColorSpaceConversions,
  296. config,
  297. context,
  298. options[0]);
  299. }
  300. else
  301. {
  302. // If we have multiple look options, try each one in order,
  303. // and if we can create the ops without a missing file exception,
  304. // push back it's results and return
  305. bool success = false;
  306. std::ostringstream os;
  307. OpRcPtrVec tmpOps;
  308. ConstColorSpaceRcPtr cs;
  309. for(unsigned int i=0; i<options.size(); ++i)
  310. {
  311. cs = currentColorSpace;
  312. tmpOps.clear();
  313. try
  314. {
  315. RunLookTokens(tmpOps,
  316. cs,
  317. skipColorSpaceConversions,
  318. config,
  319. context,
  320. options[i]);
  321. success = true;
  322. break;
  323. }
  324. catch(ExceptionMissingFile & e)
  325. {
  326. if(i != 0) os << " ... ";
  327. os << "(";
  328. LookParseResult::serialize(os, options[i]);
  329. os << ") " << e.what();
  330. }
  331. }
  332. if(success)
  333. {
  334. currentColorSpace = cs;
  335. std::copy(tmpOps.begin(), tmpOps.end(), std::back_inserter(ops));
  336. }
  337. else
  338. {
  339. throw ExceptionMissingFile(os.str().c_str());
  340. }
  341. }
  342. }
  343. }
  344. OCIO_NAMESPACE_EXIT