/src/core/ExponentOps.cpp

http://github.com/imageworks/OpenColorIO · C++ · 395 lines · 289 code · 76 blank · 30 comment · 19 complexity · 2133667433f653dcf2f0665b2121b683 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 <cmath>
  28. #include <cstring>
  29. #include <sstream>
  30. #include <OpenColorIO/OpenColorIO.h>
  31. #include "ExponentOps.h"
  32. #include "GpuShaderUtils.h"
  33. #include "MathUtils.h"
  34. OCIO_NAMESPACE_ENTER
  35. {
  36. namespace
  37. {
  38. void ApplyClampExponent(float* rgbaBuffer, long numPixels,
  39. const float* exp4)
  40. {
  41. for(long pixelIndex=0; pixelIndex<numPixels; ++pixelIndex)
  42. {
  43. rgbaBuffer[0] = powf( std::max(0.0f, rgbaBuffer[0]), exp4[0]);
  44. rgbaBuffer[1] = powf( std::max(0.0f, rgbaBuffer[1]), exp4[1]);
  45. rgbaBuffer[2] = powf( std::max(0.0f, rgbaBuffer[2]), exp4[2]);
  46. rgbaBuffer[3] = powf( std::max(0.0f, rgbaBuffer[3]), exp4[3]);
  47. rgbaBuffer += 4;
  48. }
  49. }
  50. const int FLOAT_DECIMALS = 7;
  51. }
  52. namespace
  53. {
  54. class ExponentOp : public Op
  55. {
  56. public:
  57. ExponentOp(const double * exp4,
  58. TransformDirection direction);
  59. virtual ~ExponentOp();
  60. virtual OpRcPtr clone() const;
  61. virtual std::string getInfo() const;
  62. virtual std::string getCacheID() const;
  63. virtual bool isNoOp() const;
  64. virtual bool isSameType(const OpRcPtr & op) const;
  65. virtual bool isInverse(const OpRcPtr & op) const;
  66. virtual bool canCombineWith(const OpRcPtr & op) const;
  67. virtual void combineWith(OpRcPtrVec & ops, const OpRcPtr & secondOp) const;
  68. virtual bool hasChannelCrosstalk() const;
  69. virtual void finalize();
  70. virtual void apply(float* rgbaBuffer, long numPixels) const;
  71. virtual bool supportsGpuShader() const;
  72. virtual void writeGpuShader(std::ostream & shader,
  73. const std::string & pixelName,
  74. const GpuShaderDesc & shaderDesc) const;
  75. private:
  76. double m_exp4[4];
  77. // Set in finalize
  78. std::string m_cacheID;
  79. };
  80. typedef OCIO_SHARED_PTR<ExponentOp> ExponentOpRcPtr;
  81. ExponentOp::ExponentOp(const double * exp4,
  82. TransformDirection direction):
  83. Op()
  84. {
  85. if(direction == TRANSFORM_DIR_UNKNOWN)
  86. {
  87. throw Exception("Cannot create ExponentOp with unspecified transform direction.");
  88. }
  89. if(direction == TRANSFORM_DIR_INVERSE)
  90. {
  91. for(int i=0; i<4; ++i)
  92. {
  93. if(!IsScalarEqualToZeroFlt(exp4[i]))
  94. {
  95. m_exp4[i] = 1.0 / exp4[i];
  96. }
  97. else
  98. {
  99. throw Exception("Cannot apply ExponentOp op, Cannot apply 0.0 exponent in the inverse.");
  100. }
  101. }
  102. }
  103. else
  104. {
  105. memcpy(m_exp4, exp4, 4*sizeof(double));
  106. }
  107. }
  108. OpRcPtr ExponentOp::clone() const
  109. {
  110. OpRcPtr op = OpRcPtr(new ExponentOp(m_exp4, TRANSFORM_DIR_FORWARD));
  111. return op;
  112. }
  113. ExponentOp::~ExponentOp()
  114. { }
  115. std::string ExponentOp::getInfo() const
  116. {
  117. return "<ExponentOp>";
  118. }
  119. std::string ExponentOp::getCacheID() const
  120. {
  121. return m_cacheID;
  122. }
  123. bool ExponentOp::isNoOp() const
  124. {
  125. return IsVecEqualToOneFlt(m_exp4, 4);
  126. }
  127. bool ExponentOp::isSameType(const OpRcPtr & op) const
  128. {
  129. ExponentOpRcPtr typedRcPtr = DynamicPtrCast<ExponentOp>(op);
  130. if(!typedRcPtr) return false;
  131. return true;
  132. }
  133. bool ExponentOp::isInverse(const OpRcPtr & op) const
  134. {
  135. ExponentOpRcPtr typedRcPtr = DynamicPtrCast<ExponentOp>(op);
  136. if(!typedRcPtr) return false;
  137. double combined[4] = { m_exp4[0]*typedRcPtr->m_exp4[0],
  138. m_exp4[1]*typedRcPtr->m_exp4[1],
  139. m_exp4[2]*typedRcPtr->m_exp4[2],
  140. m_exp4[3]*typedRcPtr->m_exp4[3] };
  141. return IsVecEqualToOneFlt(combined, 4);
  142. }
  143. bool ExponentOp::canCombineWith(const OpRcPtr & op) const
  144. {
  145. return isSameType(op);
  146. }
  147. void ExponentOp::combineWith(OpRcPtrVec & ops, const OpRcPtr & secondOp) const
  148. {
  149. ExponentOpRcPtr typedRcPtr = DynamicPtrCast<ExponentOp>(secondOp);
  150. if(!typedRcPtr)
  151. {
  152. std::ostringstream os;
  153. os << "ExponentOp can only be combined with other ";
  154. os << "ExponentOps. secondOp:" << secondOp->getInfo();
  155. throw Exception(os.str().c_str());
  156. }
  157. double combined[4] = { m_exp4[0]*typedRcPtr->m_exp4[0],
  158. m_exp4[1]*typedRcPtr->m_exp4[1],
  159. m_exp4[2]*typedRcPtr->m_exp4[2],
  160. m_exp4[3]*typedRcPtr->m_exp4[3] };
  161. if(!IsVecEqualToOneFlt(combined, 4))
  162. {
  163. ops.push_back(
  164. ExponentOpRcPtr(new ExponentOp(combined,
  165. TRANSFORM_DIR_FORWARD)) );
  166. }
  167. }
  168. bool ExponentOp::hasChannelCrosstalk() const
  169. {
  170. return false;
  171. }
  172. void ExponentOp::finalize()
  173. {
  174. // Create the cacheID
  175. std::ostringstream cacheIDStream;
  176. cacheIDStream << "<ExponentOp ";
  177. cacheIDStream.precision(FLOAT_DECIMALS);
  178. for(int i=0; i<4; ++i)
  179. {
  180. cacheIDStream << m_exp4[i] << " ";
  181. }
  182. cacheIDStream << ">";
  183. m_cacheID = cacheIDStream.str();
  184. }
  185. void ExponentOp::apply(float* rgbaBuffer, long numPixels) const
  186. {
  187. if(!rgbaBuffer) return;
  188. float exp[4] = { float(m_exp4[0]), float(m_exp4[1]),
  189. float(m_exp4[2]), float(m_exp4[3]) };
  190. ApplyClampExponent(rgbaBuffer, numPixels, exp);
  191. }
  192. bool ExponentOp::supportsGpuShader() const
  193. {
  194. return true;
  195. }
  196. void ExponentOp::writeGpuShader(std::ostream & shader,
  197. const std::string & pixelName,
  198. const GpuShaderDesc & shaderDesc) const
  199. {
  200. float exp[4] = { float(m_exp4[0]), float(m_exp4[1]),
  201. float(m_exp4[2]), float(m_exp4[3]) };
  202. GpuLanguage lang = shaderDesc.getLanguage();
  203. float zerovec[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
  204. shader << pixelName << " = pow(";
  205. shader << "max(" << pixelName << ", " << GpuTextHalf4(zerovec, lang) << ")";
  206. shader << ", " << GpuTextHalf4(exp, lang) << ");\n";
  207. }
  208. } // Anon namespace
  209. void CreateExponentOp(OpRcPtrVec & ops,
  210. const float * exp4,
  211. TransformDirection direction)
  212. {
  213. bool expIsIdentity = IsVecEqualToOne(exp4, 4);
  214. if(expIsIdentity) return;
  215. double d_exp[4] = { double(exp4[0]), double(exp4[1]),
  216. double(exp4[2]), double(exp4[3]) };
  217. ops.push_back( ExponentOpRcPtr(new ExponentOp(d_exp, direction)) );
  218. }
  219. }
  220. OCIO_NAMESPACE_EXIT
  221. ///////////////////////////////////////////////////////////////////////////////
  222. #ifdef OCIO_UNIT_TEST
  223. #include "UnitTest.h"
  224. OCIO_NAMESPACE_USING
  225. OIIO_ADD_TEST(ExponentOps, Value)
  226. {
  227. float exp1[4] = { 1.2f, 1.3f, 1.4f, 1.5f };
  228. OpRcPtrVec ops;
  229. CreateExponentOp(ops, exp1, TRANSFORM_DIR_FORWARD);
  230. CreateExponentOp(ops, exp1, TRANSFORM_DIR_INVERSE);
  231. OIIO_CHECK_EQUAL(ops.size(), 2);
  232. for(unsigned int i=0; i<ops.size(); ++i)
  233. {
  234. ops[i]->finalize();
  235. }
  236. float error = 1e-6f;
  237. const float source[] = { 0.5f, 0.5f, 0.5f, 0.5f, };
  238. const float result1[] = { 0.43527528164806206f, 0.40612619817811774f,
  239. 0.37892914162759955f, 0.35355339059327379f };
  240. float tmp[4];
  241. memcpy(tmp, source, 4*sizeof(float));
  242. ops[0]->apply(tmp, 1);
  243. for(unsigned int i=0; i<4; ++i)
  244. {
  245. OIIO_CHECK_CLOSE(tmp[i], result1[i], error);
  246. }
  247. ops[1]->apply(tmp, 1);
  248. for(unsigned int i=0; i<4; ++i)
  249. {
  250. OIIO_CHECK_CLOSE(tmp[i], source[i], error);
  251. }
  252. }
  253. OIIO_ADD_TEST(ExponentOps, Inverse)
  254. {
  255. float exp1[4] = { 2.0f, 1.02345f, 5.651321f, 0.12345678910f };
  256. float exp2[4] = { 2.0f, 2.0f, 2.0f, 2.0f };
  257. OpRcPtrVec ops;
  258. CreateExponentOp(ops, exp1, TRANSFORM_DIR_FORWARD);
  259. CreateExponentOp(ops, exp1, TRANSFORM_DIR_INVERSE);
  260. CreateExponentOp(ops, exp2, TRANSFORM_DIR_FORWARD);
  261. CreateExponentOp(ops, exp2, TRANSFORM_DIR_INVERSE);
  262. OIIO_CHECK_EQUAL(ops.size(), 4);
  263. OIIO_CHECK_ASSERT(ops[0]->isSameType(ops[1]));
  264. OIIO_CHECK_ASSERT(ops[0]->isSameType(ops[2]));
  265. OIIO_CHECK_ASSERT(ops[0]->isSameType(ops[3]->clone()));
  266. OIIO_CHECK_EQUAL(ops[0]->isInverse(ops[0]), false);
  267. OIIO_CHECK_EQUAL(ops[0]->isInverse(ops[1]), true);
  268. OIIO_CHECK_EQUAL(ops[1]->isInverse(ops[0]), true);
  269. OIIO_CHECK_EQUAL(ops[0]->isInverse(ops[2]), false);
  270. OIIO_CHECK_EQUAL(ops[0]->isInverse(ops[3]), false);
  271. OIIO_CHECK_EQUAL(ops[3]->isInverse(ops[0]), false);
  272. OIIO_CHECK_EQUAL(ops[2]->isInverse(ops[3]), true);
  273. OIIO_CHECK_EQUAL(ops[3]->isInverse(ops[2]), true);
  274. OIIO_CHECK_EQUAL(ops[3]->isInverse(ops[3]), false);
  275. }
  276. OIIO_ADD_TEST(ExponentOps, Combining)
  277. {
  278. {
  279. float exp1[4] = { 2.0f, 2.0f, 2.0f, 1.0f };
  280. float exp2[4] = { 1.2f, 1.2f, 1.2f, 1.0f };
  281. OpRcPtrVec ops;
  282. CreateExponentOp(ops, exp1, TRANSFORM_DIR_FORWARD);
  283. CreateExponentOp(ops, exp2, TRANSFORM_DIR_FORWARD);
  284. OIIO_CHECK_EQUAL(ops.size(), 2);
  285. float error = 1e-6f;
  286. const float source[] = { 0.5f, 0.5f, 0.5f, 0.5f, };
  287. const float result[] = { 0.18946457081379978f, 0.18946457081379978f,
  288. 0.18946457081379978f, 0.5f };
  289. float tmp[4];
  290. memcpy(tmp, source, 4*sizeof(float));
  291. ops[0]->apply(tmp, 1);
  292. ops[1]->apply(tmp, 1);
  293. for(unsigned int i=0; i<4; ++i)
  294. {
  295. OIIO_CHECK_CLOSE(tmp[i], result[i], error);
  296. }
  297. OpRcPtrVec combined;
  298. ops[0]->combineWith(combined, ops[1]);
  299. OIIO_CHECK_EQUAL(combined.size(), 1);
  300. float tmp2[4];
  301. memcpy(tmp2, source, 4*sizeof(float));
  302. combined[0]->apply(tmp2, 1);
  303. for(unsigned int i=0; i<4; ++i)
  304. {
  305. OIIO_CHECK_CLOSE(tmp2[i], result[i], error);
  306. }
  307. }
  308. {
  309. float exp1[4] = {1.037289f, 1.019015f, 0.966082f, 1.0f};
  310. OpRcPtrVec ops;
  311. CreateExponentOp(ops, exp1, TRANSFORM_DIR_FORWARD);
  312. CreateExponentOp(ops, exp1, TRANSFORM_DIR_INVERSE);
  313. OIIO_CHECK_EQUAL(ops.size(), 2);
  314. bool isInverse = ops[0]->isInverse(ops[1]);
  315. OIIO_CHECK_EQUAL(isInverse, true);
  316. }
  317. }
  318. #endif // OCIO_UNIT_TEST