/src/core/AllocationTransform.cpp

http://github.com/imageworks/OpenColorIO · C++ · 182 lines · 120 code · 33 blank · 29 comment · 3 complexity · d5ed982e48ca11b9f35cd5930fbe71d9 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 <cstring>
  28. #include <sstream>
  29. #include <vector>
  30. #include <OpenColorIO/OpenColorIO.h>
  31. #include "AllocationOp.h"
  32. #include "OpBuilders.h"
  33. OCIO_NAMESPACE_ENTER
  34. {
  35. AllocationTransformRcPtr AllocationTransform::Create()
  36. {
  37. return AllocationTransformRcPtr(new AllocationTransform(), &deleter);
  38. }
  39. void AllocationTransform::deleter(AllocationTransform* t)
  40. {
  41. delete t;
  42. }
  43. class AllocationTransform::Impl
  44. {
  45. public:
  46. TransformDirection dir_;
  47. Allocation allocation_;
  48. std::vector<float> vars_;
  49. Impl() :
  50. dir_(TRANSFORM_DIR_FORWARD),
  51. allocation_(ALLOCATION_UNIFORM)
  52. { }
  53. ~Impl()
  54. { }
  55. Impl& operator= (const Impl & rhs)
  56. {
  57. dir_ = rhs.dir_;
  58. allocation_ = rhs.allocation_;
  59. vars_ = rhs.vars_;
  60. return *this;
  61. }
  62. };
  63. ///////////////////////////////////////////////////////////////////////////
  64. AllocationTransform::AllocationTransform()
  65. : m_impl(new AllocationTransform::Impl)
  66. {
  67. }
  68. TransformRcPtr AllocationTransform::createEditableCopy() const
  69. {
  70. AllocationTransformRcPtr transform = AllocationTransform::Create();
  71. *(transform->m_impl) = *m_impl;
  72. return transform;
  73. }
  74. AllocationTransform::~AllocationTransform()
  75. {
  76. delete m_impl;
  77. m_impl = NULL;
  78. }
  79. AllocationTransform& AllocationTransform::operator= (const AllocationTransform & rhs)
  80. {
  81. *m_impl = *rhs.m_impl;
  82. return *this;
  83. }
  84. TransformDirection AllocationTransform::getDirection() const
  85. {
  86. return getImpl()->dir_;
  87. }
  88. void AllocationTransform::setDirection(TransformDirection dir)
  89. {
  90. getImpl()->dir_ = dir;
  91. }
  92. Allocation AllocationTransform::getAllocation() const
  93. {
  94. return getImpl()->allocation_;
  95. }
  96. void AllocationTransform::setAllocation(Allocation allocation)
  97. {
  98. getImpl()->allocation_ = allocation;
  99. }
  100. int AllocationTransform::getNumVars() const
  101. {
  102. return static_cast<int>(getImpl()->vars_.size());
  103. }
  104. void AllocationTransform::getVars(float * vars) const
  105. {
  106. if(!getImpl()->vars_.empty())
  107. {
  108. memcpy(vars,
  109. &getImpl()->vars_[0],
  110. getImpl()->vars_.size()*sizeof(float));
  111. }
  112. }
  113. void AllocationTransform::setVars(int numvars, const float * vars)
  114. {
  115. getImpl()->vars_.resize(numvars);
  116. if(!getImpl()->vars_.empty())
  117. {
  118. memcpy(&getImpl()->vars_[0],
  119. vars,
  120. numvars*sizeof(float));
  121. }
  122. }
  123. std::ostream& operator<< (std::ostream& os, const AllocationTransform& t)
  124. {
  125. os << "<AllocationTransform ";
  126. os << "direction=" << TransformDirectionToString(t.getDirection()) << ", ";
  127. os << ">\n";
  128. return os;
  129. }
  130. ///////////////////////////////////////////////////////////////////////////
  131. void BuildAllocationOps(OpRcPtrVec & ops,
  132. const Config& /*config*/,
  133. const AllocationTransform& allocationTransform,
  134. TransformDirection dir)
  135. {
  136. TransformDirection combinedDir = CombineTransformDirections(dir,
  137. allocationTransform.getDirection());
  138. AllocationData data;
  139. data.allocation = allocationTransform.getAllocation();
  140. data.vars.resize(allocationTransform.getNumVars());
  141. if(!data.vars.empty())
  142. {
  143. allocationTransform.getVars(&data.vars[0]);
  144. }
  145. CreateAllocationOps(ops, data, combinedDir);
  146. }
  147. }
  148. OCIO_NAMESPACE_EXIT