/src/pyglue/PyAllocationTransform.cpp

http://github.com/imageworks/OpenColorIO · C++ · 199 lines · 138 code · 24 blank · 37 comment · 5 complexity · 76a351104455aec9207d4e59e6144bc8 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 <Python.h>
  28. #include <OpenColorIO/OpenColorIO.h>
  29. #include "PyUtil.h"
  30. #include "PyDoc.h"
  31. // Rarely used. could use a log transform instead. This can sample by log when
  32. // doing the offset to make best use of the data.
  33. #define GetConstAllocationTransform(pyobject) GetConstPyOCIO<PyOCIO_Transform, \
  34. ConstAllocationTransformRcPtr, AllocationTransform>(pyobject, \
  35. PyOCIO_AllocationTransformType)
  36. #define GetEditableAllocationTransform(pyobject) GetEditablePyOCIO<PyOCIO_Transform, \
  37. AllocationTransformRcPtr, AllocationTransform>(pyobject, \
  38. PyOCIO_AllocationTransformType)
  39. OCIO_NAMESPACE_ENTER
  40. {
  41. namespace
  42. {
  43. ///////////////////////////////////////////////////////////////////////
  44. ///
  45. int PyOCIO_AllocationTransform_init(PyOCIO_Transform * self, PyObject * args, PyObject * kwds);
  46. PyObject * PyOCIO_AllocationTransform_equals(PyObject * self, PyObject * args);
  47. PyObject * PyOCIO_AllocationTransform_getAllocation(PyObject * self);
  48. PyObject * PyOCIO_AllocationTransform_setAllocation(PyObject * self, PyObject * args);
  49. PyObject * PyOCIO_AllocationTransform_getNumVars(PyObject * self);
  50. PyObject * PyOCIO_AllocationTransform_getVars(PyObject * self);
  51. PyObject * PyOCIO_AllocationTransform_setVars(PyObject * self, PyObject * args);
  52. ///////////////////////////////////////////////////////////////////////
  53. ///
  54. PyMethodDef PyOCIO_AllocationTransform_methods[] = {
  55. { "getAllocation",
  56. (PyCFunction) PyOCIO_AllocationTransform_getAllocation, METH_NOARGS, ALLOCATIONTRANSFORM_GETALLOCATION__DOC__ },
  57. { "setAllocation",
  58. PyOCIO_AllocationTransform_setAllocation, METH_VARARGS, ALLOCATIONTRANSFORM_SETALLOCATION__DOC__ },
  59. { "getNumVars",
  60. (PyCFunction) PyOCIO_AllocationTransform_getNumVars, METH_VARARGS, ALLOCATIONTRANSFORM_GETNUMVARS__DOC__ },
  61. { "getVars",
  62. (PyCFunction) PyOCIO_AllocationTransform_getVars, METH_NOARGS, ALLOCATIONTRANSFORM_GETVARS__DOC__ },
  63. { "setVars",
  64. PyOCIO_AllocationTransform_setVars, METH_VARARGS, ALLOCATIONTRANSFORM_SETVARS__DOC__ },
  65. { NULL, NULL, 0, NULL }
  66. };
  67. }
  68. ///////////////////////////////////////////////////////////////////////////
  69. ///
  70. PyTypeObject PyOCIO_AllocationTransformType = {
  71. PyVarObject_HEAD_INIT(NULL, 0) //ob_size
  72. "OCIO.AllocationTransform", //tp_name
  73. sizeof(PyOCIO_Transform), //tp_basicsize
  74. 0, //tp_itemsize
  75. 0, //tp_dealloc
  76. 0, //tp_print
  77. 0, //tp_getattr
  78. 0, //tp_setattr
  79. 0, //tp_compare
  80. 0, //tp_repr
  81. 0, //tp_as_number
  82. 0, //tp_as_sequence
  83. 0, //tp_as_mapping
  84. 0, //tp_hash
  85. 0, //tp_call
  86. 0, //tp_str
  87. 0, //tp_getattro
  88. 0, //tp_setattro
  89. 0, //tp_as_buffer
  90. Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, //tp_flags
  91. ALLOCATIONTRANSFORM__DOC__, //tp_doc
  92. 0, //tp_traverse
  93. 0, //tp_clear
  94. 0, //tp_richcompare
  95. 0, //tp_weaklistoffset
  96. 0, //tp_iter
  97. 0, //tp_iternext
  98. PyOCIO_AllocationTransform_methods, //tp_methods
  99. 0, //tp_members
  100. 0, //tp_getset
  101. &PyOCIO_TransformType, //tp_base
  102. 0, //tp_dict
  103. 0, //tp_descr_get
  104. 0, //tp_descr_set
  105. 0, //tp_dictoffset
  106. (initproc) PyOCIO_AllocationTransform_init, //tp_init
  107. 0, //tp_alloc
  108. 0, //tp_new
  109. 0, //tp_free
  110. 0, //tp_is_gc
  111. };
  112. namespace
  113. {
  114. ///////////////////////////////////////////////////////////////////////
  115. ///
  116. int PyOCIO_AllocationTransform_init(PyOCIO_Transform * self, PyObject * /*args*/, PyObject * /*kwds*/)
  117. {
  118. OCIO_PYTRY_ENTER()
  119. return BuildPyTransformObject<AllocationTransformRcPtr>(self, AllocationTransform::Create());
  120. OCIO_PYTRY_EXIT(-1)
  121. }
  122. PyObject * PyOCIO_AllocationTransform_getAllocation(PyObject * self)
  123. {
  124. OCIO_PYTRY_ENTER()
  125. ConstAllocationTransformRcPtr transform = GetConstAllocationTransform(self);
  126. return PyString_FromString( AllocationToString( transform->getAllocation()) );
  127. OCIO_PYTRY_EXIT(NULL)
  128. }
  129. PyObject * PyOCIO_AllocationTransform_setAllocation(PyObject * self, PyObject * args)
  130. {
  131. OCIO_PYTRY_ENTER()
  132. Allocation hwalloc;
  133. if (!PyArg_ParseTuple(args,"O&:setAllocation",
  134. ConvertPyObjectToAllocation, &hwalloc)) return NULL;
  135. AllocationTransformRcPtr transform = GetEditableAllocationTransform(self);
  136. transform->setAllocation(hwalloc);
  137. Py_RETURN_NONE;
  138. OCIO_PYTRY_EXIT(NULL)
  139. }
  140. PyObject * PyOCIO_AllocationTransform_getNumVars(PyObject * self)
  141. {
  142. OCIO_PYTRY_ENTER()
  143. ConstAllocationTransformRcPtr transform = GetConstAllocationTransform(self);
  144. return PyInt_FromLong(transform->getNumVars());
  145. OCIO_PYTRY_EXIT(NULL)
  146. }
  147. PyObject * PyOCIO_AllocationTransform_getVars(PyObject * self)
  148. {
  149. OCIO_PYTRY_ENTER()
  150. ConstAllocationTransformRcPtr transform = GetConstAllocationTransform(self);
  151. std::vector<float> vars(transform->getNumVars());
  152. if(!vars.empty()) transform->getVars(&vars[0]);
  153. return CreatePyListFromFloatVector(vars);
  154. OCIO_PYTRY_EXIT(NULL)
  155. }
  156. PyObject * PyOCIO_AllocationTransform_setVars(PyObject * self, PyObject * args)
  157. {
  158. OCIO_PYTRY_ENTER()
  159. PyObject * pyvars = 0;
  160. if (!PyArg_ParseTuple(args,"O:setVars", &pyvars)) return NULL;
  161. std::vector<float> vars;
  162. if(!FillFloatVectorFromPySequence(pyvars, vars))
  163. {
  164. PyErr_SetString(PyExc_TypeError, "First argument must be a float array.");
  165. return 0;
  166. }
  167. AllocationTransformRcPtr transform = GetEditableAllocationTransform(self);
  168. if(!vars.empty()) transform->setVars(static_cast<int>(vars.size()), &vars[0]);
  169. Py_RETURN_NONE;
  170. OCIO_PYTRY_EXIT(NULL)
  171. }
  172. }
  173. }
  174. OCIO_NAMESPACE_EXIT