PageRenderTime 23ms CodeModel.GetById 10ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/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/*
  2Copyright (c) 2003-2010 Sony Pictures Imageworks Inc., et al.
  3All Rights Reserved.
  4
  5Redistribution and use in source and binary forms, with or without
  6modification, are permitted provided that the following conditions are
  7met:
  8* Redistributions of source code must retain the above copyright
  9  notice, this list of conditions and the following disclaimer.
 10* Redistributions in binary form must reproduce the above copyright
 11  notice, this list of conditions and the following disclaimer in the
 12  documentation and/or other materials provided with the distribution.
 13* Neither the name of Sony Pictures Imageworks nor the names of its
 14  contributors may be used to endorse or promote products derived from
 15  this software without specific prior written permission.
 16THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 17"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 18LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 19A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 20OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 21SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 22LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 23DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 24THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 25(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 26OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 27*/
 28
 29#include <Python.h>
 30#include <OpenColorIO/OpenColorIO.h>
 31
 32#include "PyUtil.h"
 33#include "PyDoc.h"
 34
 35// Rarely used. could use a log transform instead. This can sample by log when
 36// doing the offset to make best use of the data.
 37
 38#define GetConstAllocationTransform(pyobject) GetConstPyOCIO<PyOCIO_Transform, \
 39    ConstAllocationTransformRcPtr, AllocationTransform>(pyobject, \
 40    PyOCIO_AllocationTransformType)
 41
 42#define GetEditableAllocationTransform(pyobject) GetEditablePyOCIO<PyOCIO_Transform, \
 43    AllocationTransformRcPtr, AllocationTransform>(pyobject, \
 44    PyOCIO_AllocationTransformType)
 45
 46OCIO_NAMESPACE_ENTER
 47{
 48    
 49    namespace
 50    {
 51        
 52        ///////////////////////////////////////////////////////////////////////
 53        ///
 54        
 55        int PyOCIO_AllocationTransform_init(PyOCIO_Transform * self, PyObject * args, PyObject * kwds);
 56        PyObject * PyOCIO_AllocationTransform_equals(PyObject * self,  PyObject * args);
 57        PyObject * PyOCIO_AllocationTransform_getAllocation(PyObject * self);
 58        PyObject * PyOCIO_AllocationTransform_setAllocation(PyObject * self,  PyObject * args);
 59        PyObject * PyOCIO_AllocationTransform_getNumVars(PyObject * self);
 60        PyObject * PyOCIO_AllocationTransform_getVars(PyObject * self);
 61        PyObject * PyOCIO_AllocationTransform_setVars(PyObject * self,  PyObject * args);
 62        
 63        ///////////////////////////////////////////////////////////////////////
 64        ///
 65        
 66        PyMethodDef PyOCIO_AllocationTransform_methods[] = {
 67            { "getAllocation",
 68            (PyCFunction) PyOCIO_AllocationTransform_getAllocation, METH_NOARGS, ALLOCATIONTRANSFORM_GETALLOCATION__DOC__ },
 69            { "setAllocation",
 70            PyOCIO_AllocationTransform_setAllocation, METH_VARARGS, ALLOCATIONTRANSFORM_SETALLOCATION__DOC__ },
 71            { "getNumVars",
 72            (PyCFunction) PyOCIO_AllocationTransform_getNumVars, METH_VARARGS, ALLOCATIONTRANSFORM_GETNUMVARS__DOC__ },
 73            { "getVars",
 74            (PyCFunction) PyOCIO_AllocationTransform_getVars, METH_NOARGS, ALLOCATIONTRANSFORM_GETVARS__DOC__ },
 75            { "setVars",
 76            PyOCIO_AllocationTransform_setVars, METH_VARARGS, ALLOCATIONTRANSFORM_SETVARS__DOC__ },
 77            { NULL, NULL, 0, NULL }
 78        };
 79        
 80    }
 81    
 82    ///////////////////////////////////////////////////////////////////////////
 83    ///
 84    
 85    PyTypeObject PyOCIO_AllocationTransformType = {
 86        PyVarObject_HEAD_INIT(NULL, 0)              //ob_size
 87        "OCIO.AllocationTransform",                 //tp_name
 88        sizeof(PyOCIO_Transform),                   //tp_basicsize
 89        0,                                          //tp_itemsize
 90        0,                                          //tp_dealloc
 91        0,                                          //tp_print
 92        0,                                          //tp_getattr
 93        0,                                          //tp_setattr
 94        0,                                          //tp_compare
 95        0,                                          //tp_repr
 96        0,                                          //tp_as_number
 97        0,                                          //tp_as_sequence
 98        0,                                          //tp_as_mapping
 99        0,                                          //tp_hash 
100        0,                                          //tp_call
101        0,                                          //tp_str
102        0,                                          //tp_getattro
103        0,                                          //tp_setattro
104        0,                                          //tp_as_buffer
105        Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,   //tp_flags
106        ALLOCATIONTRANSFORM__DOC__,                 //tp_doc 
107        0,                                          //tp_traverse 
108        0,                                          //tp_clear 
109        0,                                          //tp_richcompare 
110        0,                                          //tp_weaklistoffset 
111        0,                                          //tp_iter 
112        0,                                          //tp_iternext 
113        PyOCIO_AllocationTransform_methods,         //tp_methods 
114        0,                                          //tp_members 
115        0,                                          //tp_getset 
116        &PyOCIO_TransformType,                      //tp_base 
117        0,                                          //tp_dict 
118        0,                                          //tp_descr_get 
119        0,                                          //tp_descr_set 
120        0,                                          //tp_dictoffset 
121        (initproc) PyOCIO_AllocationTransform_init, //tp_init 
122        0,                                          //tp_alloc 
123        0,                                          //tp_new 
124        0,                                          //tp_free
125        0,                                          //tp_is_gc
126    };
127    
128    namespace
129    {
130        
131        ///////////////////////////////////////////////////////////////////////
132        ///
133        
134        int PyOCIO_AllocationTransform_init(PyOCIO_Transform * self, PyObject * /*args*/, PyObject * /*kwds*/)
135        {
136            OCIO_PYTRY_ENTER()
137            return BuildPyTransformObject<AllocationTransformRcPtr>(self, AllocationTransform::Create());
138            OCIO_PYTRY_EXIT(-1)
139        }
140        
141        PyObject * PyOCIO_AllocationTransform_getAllocation(PyObject * self)
142        {
143            OCIO_PYTRY_ENTER()
144            ConstAllocationTransformRcPtr transform = GetConstAllocationTransform(self);
145            return PyString_FromString( AllocationToString( transform->getAllocation()) );
146            OCIO_PYTRY_EXIT(NULL)
147        }
148        
149        PyObject * PyOCIO_AllocationTransform_setAllocation(PyObject * self, PyObject * args)
150        {
151            OCIO_PYTRY_ENTER()
152            Allocation hwalloc;
153            if (!PyArg_ParseTuple(args,"O&:setAllocation",
154                ConvertPyObjectToAllocation, &hwalloc)) return NULL;
155            AllocationTransformRcPtr transform = GetEditableAllocationTransform(self);
156            transform->setAllocation(hwalloc);
157            Py_RETURN_NONE;
158            OCIO_PYTRY_EXIT(NULL)
159        }
160        
161        PyObject * PyOCIO_AllocationTransform_getNumVars(PyObject * self)
162        {
163            OCIO_PYTRY_ENTER()
164            ConstAllocationTransformRcPtr transform = GetConstAllocationTransform(self);
165            return PyInt_FromLong(transform->getNumVars());
166            OCIO_PYTRY_EXIT(NULL)
167        }
168        
169        PyObject * PyOCIO_AllocationTransform_getVars(PyObject * self)
170        {
171            OCIO_PYTRY_ENTER()
172            ConstAllocationTransformRcPtr transform = GetConstAllocationTransform(self);
173            std::vector<float> vars(transform->getNumVars());
174            if(!vars.empty()) transform->getVars(&vars[0]);
175            return CreatePyListFromFloatVector(vars);
176            OCIO_PYTRY_EXIT(NULL)
177        }
178        
179        PyObject * PyOCIO_AllocationTransform_setVars(PyObject * self, PyObject * args)
180        {
181            OCIO_PYTRY_ENTER()
182            PyObject * pyvars = 0;
183            if (!PyArg_ParseTuple(args,"O:setVars", &pyvars)) return NULL;
184            std::vector<float> vars;
185            if(!FillFloatVectorFromPySequence(pyvars, vars))
186            {
187                PyErr_SetString(PyExc_TypeError, "First argument must be a float array.");
188                return 0;
189            }
190            AllocationTransformRcPtr transform = GetEditableAllocationTransform(self);
191            if(!vars.empty()) transform->setVars(static_cast<int>(vars.size()), &vars[0]);
192            Py_RETURN_NONE;
193            OCIO_PYTRY_EXIT(NULL)
194        }
195        
196    }
197
198}
199OCIO_NAMESPACE_EXIT