PageRenderTime 38ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/src/core/ColorSpace.cpp

http://github.com/imageworks/OpenColorIO
C++ | 270 lines | 195 code | 47 blank | 28 comment | 17 complexity | 53c85540876cccbd40f6f0029a3435d3 MD5 | raw file
Possible License(s): BSD-3-Clause
  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 <cstring>
  28. #include <sstream>
  29. #include <vector>
  30. #include <OpenColorIO/OpenColorIO.h>
  31. OCIO_NAMESPACE_ENTER
  32. {
  33. ColorSpaceRcPtr ColorSpace::Create()
  34. {
  35. return ColorSpaceRcPtr(new ColorSpace(), &deleter);
  36. }
  37. void ColorSpace::deleter(ColorSpace* c)
  38. {
  39. delete c;
  40. }
  41. class ColorSpace::Impl
  42. {
  43. public:
  44. std::string name_;
  45. std::string family_;
  46. std::string equalityGroup_;
  47. std::string description_;
  48. BitDepth bitDepth_;
  49. bool isData_;
  50. Allocation allocation_;
  51. std::vector<float> allocationVars_;
  52. TransformRcPtr toRefTransform_;
  53. TransformRcPtr fromRefTransform_;
  54. bool toRefSpecified_;
  55. bool fromRefSpecified_;
  56. Impl() :
  57. bitDepth_(BIT_DEPTH_UNKNOWN),
  58. isData_(false),
  59. allocation_(ALLOCATION_UNIFORM),
  60. toRefSpecified_(false),
  61. fromRefSpecified_(false)
  62. { }
  63. ~Impl()
  64. { }
  65. Impl& operator= (const Impl & rhs)
  66. {
  67. name_ = rhs.name_;
  68. family_ = rhs.family_;
  69. equalityGroup_ = rhs.equalityGroup_;
  70. description_ = rhs.description_;
  71. bitDepth_ = rhs.bitDepth_;
  72. isData_ = rhs.isData_;
  73. allocation_ = rhs.allocation_;
  74. allocationVars_ = rhs.allocationVars_;
  75. toRefTransform_ = rhs.toRefTransform_;
  76. if(toRefTransform_) toRefTransform_ = toRefTransform_->createEditableCopy();
  77. fromRefTransform_ = rhs.fromRefTransform_;
  78. if(fromRefTransform_) fromRefTransform_ = fromRefTransform_->createEditableCopy();
  79. toRefSpecified_ = rhs.toRefSpecified_;
  80. fromRefSpecified_ = rhs.fromRefSpecified_;
  81. return *this;
  82. }
  83. };
  84. ///////////////////////////////////////////////////////////////////////////
  85. ColorSpace::ColorSpace()
  86. : m_impl(new ColorSpace::Impl)
  87. {
  88. }
  89. ColorSpace::~ColorSpace()
  90. {
  91. delete m_impl;
  92. m_impl = NULL;
  93. }
  94. ColorSpaceRcPtr ColorSpace::createEditableCopy() const
  95. {
  96. ColorSpaceRcPtr cs = ColorSpace::Create();
  97. *cs->m_impl = *m_impl;
  98. return cs;
  99. }
  100. const char * ColorSpace::getName() const
  101. {
  102. return getImpl()->name_.c_str();
  103. }
  104. void ColorSpace::setName(const char * name)
  105. {
  106. getImpl()->name_ = name;
  107. }
  108. const char * ColorSpace::getFamily() const
  109. {
  110. return getImpl()->family_.c_str();
  111. }
  112. void ColorSpace::setFamily(const char * family)
  113. {
  114. getImpl()->family_ = family;
  115. }
  116. const char * ColorSpace::getEqualityGroup() const
  117. {
  118. return getImpl()->equalityGroup_.c_str();
  119. }
  120. void ColorSpace::setEqualityGroup(const char * equalityGroup)
  121. {
  122. getImpl()->equalityGroup_ = equalityGroup;
  123. }
  124. const char * ColorSpace::getDescription() const
  125. {
  126. return getImpl()->description_.c_str();
  127. }
  128. void ColorSpace::setDescription(const char * description)
  129. {
  130. getImpl()->description_ = description;
  131. }
  132. BitDepth ColorSpace::getBitDepth() const
  133. {
  134. return getImpl()->bitDepth_;
  135. }
  136. void ColorSpace::setBitDepth(BitDepth bitDepth)
  137. {
  138. getImpl()->bitDepth_ = bitDepth;
  139. }
  140. bool ColorSpace::isData() const
  141. {
  142. return getImpl()->isData_;
  143. }
  144. void ColorSpace::setIsData(bool val)
  145. {
  146. getImpl()->isData_ = val;
  147. }
  148. Allocation ColorSpace::getAllocation() const
  149. {
  150. return getImpl()->allocation_;
  151. }
  152. void ColorSpace::setAllocation(Allocation allocation)
  153. {
  154. getImpl()->allocation_ = allocation;
  155. }
  156. int ColorSpace::getAllocationNumVars() const
  157. {
  158. return static_cast<int>(getImpl()->allocationVars_.size());
  159. }
  160. void ColorSpace::getAllocationVars(float * vars) const
  161. {
  162. if(!getImpl()->allocationVars_.empty())
  163. {
  164. memcpy(vars,
  165. &getImpl()->allocationVars_[0],
  166. getImpl()->allocationVars_.size()*sizeof(float));
  167. }
  168. }
  169. void ColorSpace::setAllocationVars(int numvars, const float * vars)
  170. {
  171. getImpl()->allocationVars_.resize(numvars);
  172. if(!getImpl()->allocationVars_.empty())
  173. {
  174. memcpy(&getImpl()->allocationVars_[0],
  175. vars,
  176. numvars*sizeof(float));
  177. }
  178. }
  179. ConstTransformRcPtr ColorSpace::getTransform(ColorSpaceDirection dir) const
  180. {
  181. if(dir == COLORSPACE_DIR_TO_REFERENCE)
  182. return getImpl()->toRefTransform_;
  183. else if(dir == COLORSPACE_DIR_FROM_REFERENCE)
  184. return getImpl()->fromRefTransform_;
  185. throw Exception("Unspecified ColorSpaceDirection");
  186. }
  187. void ColorSpace::setTransform(const ConstTransformRcPtr & transform,
  188. ColorSpaceDirection dir)
  189. {
  190. TransformRcPtr transformCopy;
  191. if(transform) transformCopy = transform->createEditableCopy();
  192. if(dir == COLORSPACE_DIR_TO_REFERENCE)
  193. getImpl()->toRefTransform_ = transformCopy;
  194. else if(dir == COLORSPACE_DIR_FROM_REFERENCE)
  195. getImpl()->fromRefTransform_ = transformCopy;
  196. else
  197. throw Exception("Unspecified ColorSpaceDirection");
  198. }
  199. std::ostream& operator<< (std::ostream& os, const ColorSpace& cs)
  200. {
  201. os << "<ColorSpace ";
  202. os << "name=" << cs.getName() << ", ";
  203. os << "family=" << cs.getFamily() << ", ";
  204. os << "equalityGroup=" << cs.getEqualityGroup() << ", ";
  205. os << "bitDepth=" << BitDepthToString(cs.getBitDepth()) << ", ";
  206. os << "isData=" << BoolToString(cs.isData()) << ", ";
  207. os << "allocation=" << AllocationToString(cs.getAllocation()) << ", ";
  208. os << ">\n";
  209. if(cs.getTransform(COLORSPACE_DIR_TO_REFERENCE))
  210. {
  211. os << "\t" << cs.getName() << " --> Reference\n";
  212. os << cs.getTransform(COLORSPACE_DIR_TO_REFERENCE);
  213. }
  214. if(cs.getTransform(COLORSPACE_DIR_FROM_REFERENCE))
  215. {
  216. os << "\tReference --> " << cs.getName() << "\n";
  217. os << cs.getTransform(COLORSPACE_DIR_FROM_REFERENCE);
  218. }
  219. return os;
  220. }
  221. }
  222. OCIO_NAMESPACE_EXIT