PageRenderTime 57ms CodeModel.GetById 10ms app.highlight 43ms RepoModel.GetById 1ms app.codeStats 1ms

/src/pyglue/PyGroupTransform.cpp

http://github.com/imageworks/OpenColorIO
C++ | 251 lines | 190 code | 25 blank | 36 comment | 13 complexity | ac95d6119ef6071743a233c22ba0238d 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#define GetConstGroupTransform(pyobject) GetConstPyOCIO<PyOCIO_Transform, \
 36    ConstGroupTransformRcPtr, GroupTransform>(pyobject, \
 37    PyOCIO_GroupTransformType)
 38
 39#define GetEditableGroupTransform(pyobject) GetEditablePyOCIO<PyOCIO_Transform, \
 40    GroupTransformRcPtr, GroupTransform>(pyobject, \
 41    PyOCIO_GroupTransformType)
 42
 43OCIO_NAMESPACE_ENTER
 44{
 45    
 46    namespace
 47    {
 48        
 49        ///////////////////////////////////////////////////////////////////////
 50        ///
 51        
 52        int PyOCIO_GroupTransform_init(PyOCIO_Transform * self, PyObject * args, PyObject * kwds);
 53        PyObject * PyOCIO_GroupTransform_getTransform(PyObject * self,  PyObject * args);
 54        PyObject * PyOCIO_GroupTransform_getTransforms(PyObject * self);
 55        PyObject * PyOCIO_GroupTransform_setTransforms(PyObject * self,  PyObject * args);
 56        // TODO: make these appear more like a pysequence. .append, len(), etc
 57        PyObject * PyOCIO_GroupTransform_size(PyObject * self);
 58        PyObject * PyOCIO_GroupTransform_push_back(PyObject * self, PyObject * args);
 59        PyObject * PyOCIO_GroupTransform_clear(PyObject * self);
 60        PyObject * PyOCIO_GroupTransform_empty(PyObject * self);
 61        
 62        ///////////////////////////////////////////////////////////////////////
 63        ///
 64        
 65        PyMethodDef PyOCIO_GroupTransform_methods[] = {
 66            { "getTransform",
 67            PyOCIO_GroupTransform_getTransform, METH_VARARGS, GROUPTRANSFORM_GETTRANSFORM__DOC__ },
 68            { "getTransforms",
 69            (PyCFunction) PyOCIO_GroupTransform_getTransforms, METH_NOARGS, GROUPTRANSFORM_GETTRANSFORMS__DOC__ },
 70            { "setTransforms",
 71            PyOCIO_GroupTransform_setTransforms, METH_VARARGS, GROUPTRANSFORM_SETTRANSFORMS__DOC__ },
 72            { "size",
 73            (PyCFunction) PyOCIO_GroupTransform_size, METH_NOARGS, GROUPTRANSFORM_SIZE__DOC__ },
 74            { "push_back",
 75            PyOCIO_GroupTransform_push_back, METH_VARARGS, GROUPTRANSFORM_PUSH_BACK__DOC__ },
 76            { "clear",
 77            (PyCFunction) PyOCIO_GroupTransform_clear, METH_NOARGS, GROUPTRANSFORM_CLEAR__DOC__ },
 78            { "empty",
 79            (PyCFunction) PyOCIO_GroupTransform_empty, METH_NOARGS, GROUPTRANSFORM_EMPTY__DOC__ },
 80            { NULL, NULL, 0, NULL }
 81        };
 82        
 83    }
 84    
 85    ///////////////////////////////////////////////////////////////////////////
 86    ///
 87    
 88    PyTypeObject PyOCIO_GroupTransformType = {
 89        PyVarObject_HEAD_INIT(NULL, 0)
 90        "OCIO.GroupTransform",                    //tp_name
 91        sizeof(PyOCIO_Transform),                   //tp_basicsize
 92        0,                                          //tp_itemsize
 93        0,                                          //tp_dealloc
 94        0,                                          //tp_print
 95        0,                                          //tp_getattr
 96        0,                                          //tp_setattr
 97        0,                                          //tp_compare
 98        0,                                          //tp_repr
 99        0,                                          //tp_as_number
100        0,                                          //tp_as_sequence
101        0,                                          //tp_as_mapping
102        0,                                          //tp_hash 
103        0,                                          //tp_call
104        0,                                          //tp_str
105        0,                                          //tp_getattro
106        0,                                          //tp_setattro
107        0,                                          //tp_as_buffer
108        Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,   //tp_flags
109        GROUPTRANSFORM__DOC__,                      //tp_doc 
110        0,                                          //tp_traverse 
111        0,                                          //tp_clear 
112        0,                                          //tp_richcompare 
113        0,                                          //tp_weaklistoffset 
114        0,                                          //tp_iter 
115        0,                                          //tp_iternext 
116        PyOCIO_GroupTransform_methods,              //tp_methods 
117        0,                                          //tp_members 
118        0,                                          //tp_getset 
119        &PyOCIO_TransformType,                      //tp_base 
120        0,                                          //tp_dict 
121        0,                                          //tp_descr_get 
122        0,                                          //tp_descr_set 
123        0,                                          //tp_dictoffset 
124        (initproc) PyOCIO_GroupTransform_init,      //tp_init 
125        0,                                          //tp_alloc 
126        0,                                          //tp_new 
127        0,                                          //tp_free
128        0,                                          //tp_is_gc
129    };
130    
131    namespace
132    {
133        
134        ///////////////////////////////////////////////////////////////////////
135        ///
136        
137        int PyOCIO_GroupTransform_init(PyOCIO_Transform * self, PyObject * args, PyObject * kwds)
138        {
139            OCIO_PYTRY_ENTER()
140            GroupTransformRcPtr ptr = GroupTransform::Create();
141            int ret = BuildPyTransformObject<GroupTransformRcPtr>(self, ptr);
142            PyObject* pytransforms = Py_None;
143            char* direction = NULL;
144            static const char *kwlist[] = { "transforms", "direction", NULL };
145            if(!PyArg_ParseTupleAndKeywords(args, kwds, "|Os",
146                const_cast<char **>(kwlist),
147                &pytransforms, &direction )) return -1;
148            if(pytransforms != Py_None)
149            {
150                std::vector<ConstTransformRcPtr> data;
151                if(!FillTransformVectorFromPySequence(pytransforms, data))
152                {
153                    PyErr_SetString(PyExc_TypeError,
154                        "Kwarg 'transforms' must be a transform array.");
155                    return -1;
156                }
157                for(unsigned int i=0; i<data.size(); ++i)
158                    ptr->push_back(data[i]);
159            }
160            if(direction)
161                ptr->setDirection(TransformDirectionFromString(direction));
162            return ret;
163            OCIO_PYTRY_EXIT(-1)
164        }
165        
166        PyObject * PyOCIO_GroupTransform_getTransform(PyObject * self, PyObject * args)
167        {
168            OCIO_PYTRY_ENTER()
169            int index = 0;
170            if (!PyArg_ParseTuple(args,"i:getTransform",
171                &index)) return NULL;
172            ConstGroupTransformRcPtr transform = GetConstGroupTransform(self);
173            ConstTransformRcPtr childTransform = transform->getTransform(index);
174            return BuildConstPyTransform(childTransform);
175            OCIO_PYTRY_EXIT(NULL)
176        }
177        
178        PyObject * PyOCIO_GroupTransform_getTransforms(PyObject * self)
179        {
180            OCIO_PYTRY_ENTER()
181            ConstGroupTransformRcPtr transform = GetConstGroupTransform(self);
182            std::vector<ConstTransformRcPtr> transforms;
183            for(int i = 0; i < transform->size(); ++i)
184                transforms.push_back(transform->getTransform(i));
185            return CreatePyListFromTransformVector(transforms);
186            OCIO_PYTRY_EXIT(NULL)
187        }
188        
189        PyObject * PyOCIO_GroupTransform_setTransforms(PyObject * self, PyObject * args)
190        {
191            OCIO_PYTRY_ENTER()
192            PyObject* pytransforms = 0;
193            if (!PyArg_ParseTuple(args,"O:setTransforms",
194                &pytransforms)) return NULL;
195            GroupTransformRcPtr transform = GetEditableGroupTransform(self);
196            std::vector<ConstTransformRcPtr> data;
197            if(!FillTransformVectorFromPySequence(pytransforms, data))
198            {
199                PyErr_SetString(PyExc_TypeError, "First argument must be a transform array.");
200                return 0;
201            }
202            transform->clear();
203            for(unsigned int i = 0; i < data.size(); ++i)
204                transform->push_back(data[i]);
205            Py_RETURN_NONE;
206            OCIO_PYTRY_EXIT(NULL)
207        }
208        
209        PyObject * PyOCIO_GroupTransform_size(PyObject * self)
210        {
211            OCIO_PYTRY_ENTER()
212            ConstGroupTransformRcPtr transform = GetConstGroupTransform(self);
213            return PyInt_FromLong(transform->size());
214            OCIO_PYTRY_EXIT(NULL)
215        }
216        
217        PyObject * PyOCIO_GroupTransform_push_back(PyObject * self, PyObject * args)
218        {
219            OCIO_PYTRY_ENTER()
220            PyObject* pytransform = 0;
221            if (!PyArg_ParseTuple(args,"O:push_back",
222                &pytransform)) return NULL;
223            GroupTransformRcPtr transform = GetEditableGroupTransform(self);
224            if(!IsPyTransform(pytransform))
225                throw Exception("GroupTransform.push_back requires a transform as the first arg.");
226            transform->push_back(GetConstTransform(pytransform, true));
227            Py_RETURN_NONE;
228            OCIO_PYTRY_EXIT(NULL)
229        }
230        
231        PyObject * PyOCIO_GroupTransform_clear(PyObject * self)
232        {
233            OCIO_PYTRY_ENTER()
234            GroupTransformRcPtr transform = GetEditableGroupTransform(self);
235            transform->clear();
236            Py_RETURN_NONE;
237            OCIO_PYTRY_EXIT(NULL)
238        }
239        
240        PyObject * PyOCIO_GroupTransform_empty(PyObject * self)
241        {
242            OCIO_PYTRY_ENTER()
243            ConstGroupTransformRcPtr transform = GetConstGroupTransform(self);
244            return PyBool_FromLong(transform->empty());
245            OCIO_PYTRY_EXIT(NULL)
246        }
247        
248    }
249
250}
251OCIO_NAMESPACE_EXIT