/src/core/ColorSpaceTransform.cpp

http://github.com/imageworks/OpenColorIO · C++ · 246 lines · 164 code · 41 blank · 41 comment · 19 complexity · b3aa443380294695e3a446f929dd73fb 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 "NoOps.h"
  29. #include "OpBuilders.h"
  30. OCIO_NAMESPACE_ENTER
  31. {
  32. ColorSpaceTransformRcPtr ColorSpaceTransform::Create()
  33. {
  34. return ColorSpaceTransformRcPtr(new ColorSpaceTransform(), &deleter);
  35. }
  36. void ColorSpaceTransform::deleter(ColorSpaceTransform* t)
  37. {
  38. delete t;
  39. }
  40. class ColorSpaceTransform::Impl
  41. {
  42. public:
  43. TransformDirection dir_;
  44. std::string src_;
  45. std::string dst_;
  46. Impl() :
  47. dir_(TRANSFORM_DIR_FORWARD)
  48. { }
  49. ~Impl()
  50. { }
  51. Impl& operator= (const Impl & rhs)
  52. {
  53. dir_ = rhs.dir_;
  54. src_ = rhs.src_;
  55. dst_ = rhs.dst_;
  56. return *this;
  57. }
  58. };
  59. ///////////////////////////////////////////////////////////////////////////
  60. ColorSpaceTransform::ColorSpaceTransform()
  61. : m_impl(new ColorSpaceTransform::Impl)
  62. {
  63. }
  64. TransformRcPtr ColorSpaceTransform::createEditableCopy() const
  65. {
  66. ColorSpaceTransformRcPtr transform = ColorSpaceTransform::Create();
  67. *(transform->m_impl) = *m_impl;
  68. return transform;
  69. }
  70. ColorSpaceTransform::~ColorSpaceTransform()
  71. {
  72. delete m_impl;
  73. m_impl = NULL;
  74. }
  75. ColorSpaceTransform& ColorSpaceTransform::operator= (const ColorSpaceTransform & rhs)
  76. {
  77. *m_impl = *rhs.m_impl;
  78. return *this;
  79. }
  80. TransformDirection ColorSpaceTransform::getDirection() const
  81. {
  82. return getImpl()->dir_;
  83. }
  84. void ColorSpaceTransform::setDirection(TransformDirection dir)
  85. {
  86. getImpl()->dir_ = dir;
  87. }
  88. const char * ColorSpaceTransform::getSrc() const
  89. {
  90. return getImpl()->src_.c_str();
  91. }
  92. void ColorSpaceTransform::setSrc(const char * src)
  93. {
  94. getImpl()->src_ = src;
  95. }
  96. const char * ColorSpaceTransform::getDst() const
  97. {
  98. return getImpl()->dst_.c_str();
  99. }
  100. void ColorSpaceTransform::setDst(const char * dst)
  101. {
  102. getImpl()->dst_ = dst;
  103. }
  104. std::ostream& operator<< (std::ostream& os, const ColorSpaceTransform& t)
  105. {
  106. os << "<ColorSpaceTransform ";
  107. os << "direction=" << TransformDirectionToString(t.getDirection()) << ", ";
  108. os << ">\n";
  109. return os;
  110. }
  111. ///////////////////////////////////////////////////////////////////////////////////////////////////////
  112. void BuildColorSpaceOps(OpRcPtrVec & ops,
  113. const Config& config,
  114. const ConstContextRcPtr & context,
  115. const ColorSpaceTransform & colorSpaceTransform,
  116. TransformDirection dir)
  117. {
  118. TransformDirection combinedDir = CombineTransformDirections(dir,
  119. colorSpaceTransform.getDirection());
  120. ConstColorSpaceRcPtr src, dst;
  121. if(combinedDir == TRANSFORM_DIR_FORWARD)
  122. {
  123. src = config.getColorSpace( colorSpaceTransform.getSrc() );
  124. dst = config.getColorSpace( colorSpaceTransform.getDst() );
  125. }
  126. else if(combinedDir == TRANSFORM_DIR_INVERSE)
  127. {
  128. dst = config.getColorSpace( colorSpaceTransform.getSrc() );
  129. src = config.getColorSpace( colorSpaceTransform.getDst() );
  130. }
  131. BuildColorSpaceOps(ops, config, context, src, dst);
  132. }
  133. namespace
  134. {
  135. bool AreColorSpacesInSameEqualityGroup(const ConstColorSpaceRcPtr & csa,
  136. const ConstColorSpaceRcPtr & csb)
  137. {
  138. std::string a = csa->getEqualityGroup();
  139. std::string b = csb->getEqualityGroup();
  140. if(!a.empty()) return (a==b);
  141. return false;
  142. }
  143. }
  144. void BuildColorSpaceOps(OpRcPtrVec & ops,
  145. const Config & config,
  146. const ConstContextRcPtr & context,
  147. const ConstColorSpaceRcPtr & srcColorSpace,
  148. const ConstColorSpaceRcPtr & dstColorSpace)
  149. {
  150. if(!srcColorSpace)
  151. throw Exception("BuildColorSpaceOps failed, null srcColorSpace.");
  152. if(!dstColorSpace)
  153. throw Exception("BuildColorSpaceOps failed, null dstColorSpace.");
  154. if(AreColorSpacesInSameEqualityGroup(srcColorSpace, dstColorSpace))
  155. return;
  156. if(dstColorSpace->isData() || srcColorSpace->isData())
  157. return;
  158. // Consider dt8 -> vd8?
  159. // One would have to explode the srcColorSpace->getTransform(COLORSPACE_DIR_TO_REFERENCE);
  160. // result, and walk through it step by step. If the dstColorspace family were
  161. // ever encountered in transit, we'd want to short circuit the result.
  162. AllocationData srcAllocation;
  163. srcAllocation.allocation = srcColorSpace->getAllocation();
  164. srcAllocation.vars.resize( srcColorSpace->getAllocationNumVars());
  165. if(srcAllocation.vars.size() > 0)
  166. {
  167. srcColorSpace->getAllocationVars(&srcAllocation.vars[0]);
  168. }
  169. CreateGpuAllocationNoOp(ops, srcAllocation);
  170. // Go to the reference space, either by using
  171. // * cs->ref in the forward direction
  172. // * ref->cs in the inverse direction
  173. if(srcColorSpace->getTransform(COLORSPACE_DIR_TO_REFERENCE))
  174. {
  175. BuildOps(ops, config, context, srcColorSpace->getTransform(COLORSPACE_DIR_TO_REFERENCE), TRANSFORM_DIR_FORWARD);
  176. }
  177. else if(srcColorSpace->getTransform(COLORSPACE_DIR_FROM_REFERENCE))
  178. {
  179. BuildOps(ops, config, context, srcColorSpace->getTransform(COLORSPACE_DIR_FROM_REFERENCE), TRANSFORM_DIR_INVERSE);
  180. }
  181. // Otherwise, both are not defined so its a no-op. This is not an error condition.
  182. // Go from the reference space, either by using
  183. // * ref->cs in the forward direction
  184. // * cs->ref in the inverse direction
  185. if(dstColorSpace->getTransform(COLORSPACE_DIR_FROM_REFERENCE))
  186. {
  187. BuildOps(ops, config, context, dstColorSpace->getTransform(COLORSPACE_DIR_FROM_REFERENCE), TRANSFORM_DIR_FORWARD);
  188. }
  189. else if(dstColorSpace->getTransform(COLORSPACE_DIR_TO_REFERENCE))
  190. {
  191. BuildOps(ops, config, context, dstColorSpace->getTransform(COLORSPACE_DIR_TO_REFERENCE), TRANSFORM_DIR_INVERSE);
  192. }
  193. // Otherwise, both are not defined so its a no-op. This is not an error condition.
  194. AllocationData dstAllocation;
  195. dstAllocation.allocation = dstColorSpace->getAllocation();
  196. dstAllocation.vars.resize( dstColorSpace->getAllocationNumVars());
  197. if(dstAllocation.vars.size() > 0)
  198. {
  199. dstColorSpace->getAllocationVars(&dstAllocation.vars[0]);
  200. }
  201. CreateGpuAllocationNoOp(ops, dstAllocation);
  202. }
  203. }
  204. OCIO_NAMESPACE_EXIT