PageRenderTime 66ms CodeModel.GetById 32ms app.highlight 29ms RepoModel.GetById 1ms app.codeStats 0ms

/src/pyglue/PyMain.cpp

http://github.com/imageworks/OpenColorIO
C++ | 205 lines | 145 code | 27 blank | 33 comment | 5 complexity | 791d6bb0136e9af4a6c2480d4c99a069 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
 31#include <OpenColorIO/OpenColorIO.h>
 32namespace OCIO = OCIO_NAMESPACE;
 33
 34#include "PyUtil.h"
 35#include "PyDoc.h"
 36
 37namespace
 38{
 39    
 40    PyObject * PyOCIO_ClearAllCaches(PyObject * /* self */)
 41    {
 42        OCIO_PYTRY_ENTER()
 43        OCIO::ClearAllCaches();
 44        Py_RETURN_NONE;
 45        OCIO_PYTRY_EXIT(NULL)
 46    }
 47    
 48    PyObject * PyOCIO_GetLoggingLevel(PyObject * /* self */)
 49    {
 50        OCIO_PYTRY_ENTER()
 51        return PyString_FromString(
 52            OCIO::LoggingLevelToString(OCIO::GetLoggingLevel()));
 53        OCIO_PYTRY_EXIT(NULL)
 54    }
 55    
 56    PyObject * PyOCIO_SetLoggingLevel(PyObject * /*self*/, PyObject * args)
 57    {
 58        OCIO_PYTRY_ENTER()
 59        PyObject* pylevel;
 60        if (!PyArg_ParseTuple(args, "O:SetLoggingLevel",
 61            &pylevel)) return NULL;
 62        // We explicitly cast to a str to handle both the str and int cases.
 63        PyObject* pystr = PyObject_Str(pylevel);
 64        if(!pystr) throw OCIO::Exception("Fist argument must be a LOGGING_LEVEL");
 65        OCIO::LoggingLevel level = OCIO::LoggingLevelFromString(PyString_AsString(pystr));
 66        OCIO::SetLoggingLevel(level);
 67        Py_DECREF(pystr);
 68        Py_RETURN_NONE;
 69        OCIO_PYTRY_EXIT(NULL)
 70    }
 71    
 72    PyObject * PyOCIO_GetCurrentConfig(PyObject * /* self */)
 73    {
 74        OCIO_PYTRY_ENTER()
 75        return OCIO::BuildConstPyConfig(OCIO::GetCurrentConfig());
 76        OCIO_PYTRY_EXIT(NULL)
 77    }
 78    
 79    PyObject * PyOCIO_SetCurrentConfig(PyObject * /*self*/, PyObject * args)
 80    {
 81        OCIO_PYTRY_ENTER()
 82        PyObject * pyconfig;
 83        if (!PyArg_ParseTuple(args, "O!:SetCurrentConfig",
 84            &OCIO::PyOCIO_ConfigType, &pyconfig)) return NULL;
 85        OCIO::ConstConfigRcPtr c = OCIO::GetConstConfig(pyconfig, true);
 86        OCIO::SetCurrentConfig(c);
 87        Py_RETURN_NONE;
 88        OCIO_PYTRY_EXIT(NULL)
 89    }
 90    
 91    PyMethodDef PyOCIO_methods[] = {
 92        { "ClearAllCaches",
 93        (PyCFunction) PyOCIO_ClearAllCaches, METH_NOARGS, OCIO::OPENCOLORIO_CLEARALLCACHES__DOC__ },
 94        { "GetLoggingLevel",
 95        (PyCFunction) PyOCIO_GetLoggingLevel, METH_NOARGS, OCIO::OPENCOLORIO_GETLOGGINGLEVEL__DOC__ },
 96        { "SetLoggingLevel",
 97        (PyCFunction) PyOCIO_SetLoggingLevel, METH_VARARGS, OCIO::OPENCOLORIO_SETLOGGINGLEVEL__DOC__ },
 98        { "GetCurrentConfig",
 99        (PyCFunction) PyOCIO_GetCurrentConfig, METH_NOARGS, OCIO::OPENCOLORIO_GETCURRENTCONFIG__DOC__ },
100        { "SetCurrentConfig",
101        (PyCFunction) PyOCIO_SetCurrentConfig, METH_VARARGS, OCIO::OPENCOLORIO_SETCURRENTCONFIG__DOC__ },
102        { NULL, NULL, 0, NULL } /* Sentinel */
103    };
104    
105}
106
107OCIO_NAMESPACE_ENTER
108{
109    namespace
110    {
111        PyObject * g_exceptionType = NULL;
112        PyObject * g_exceptionMissingFileType = NULL;
113    }
114    
115    // These are explicitly initialized in the init function
116    // to make sure they're not initialized until after the module is
117    
118    PyObject * GetExceptionPyType()
119    {
120        return g_exceptionType;
121    }
122    
123    void SetExceptionPyType(PyObject * pytypeobj)
124    {
125        g_exceptionType = pytypeobj;
126    }
127    
128    PyObject * GetExceptionMissingFilePyType()
129    {
130        return g_exceptionMissingFileType;
131    }
132    
133    void SetExceptionMissingFilePyType(PyObject * pytypeobj)
134    {
135        g_exceptionMissingFileType = pytypeobj;
136    }
137    
138    inline bool AddObjectToModule(PyTypeObject& o, const char* n, PyObject* m)
139    {
140        o.tp_new = PyType_GenericNew;
141        if(PyType_Ready(&o) < 0) return false;
142        Py_INCREF(&o);
143        PyModule_AddObject(m, n, (PyObject *)&o);
144        return true;
145    }
146    
147    // fwd declare
148    void AddConstantsModule(PyObject *enclosingModule);
149    
150}
151OCIO_NAMESPACE_EXIT
152
153MOD_INIT(PyOpenColorIO)
154{
155    PyObject * m;
156    MOD_DEF(m, "PyOpenColorIO", OCIO::OPENCOLORIO__DOC__, PyOCIO_methods);
157    
158    PyModule_AddStringConstant(m, "version", OCIO::GetVersion());
159    PyModule_AddIntConstant(m, "hexversion", OCIO::GetVersionHex());
160    
161    // Create Exceptions, and add to the module
162    char Exception[] = "PyOpenColorIO.Exception";
163    char ExceptionMissingFile[] = "PyOpenColorIO.ExceptionMissingFile";
164    
165#if PY_MAJOR_VERSION >= 2 && PY_MINOR_VERSION >= 7
166    OCIO::SetExceptionPyType(PyErr_NewExceptionWithDoc(Exception,
167        (char*)OCIO::EXCEPTION__DOC__, OCIO::GetExceptionPyType(), NULL));
168    OCIO::SetExceptionMissingFilePyType(PyErr_NewExceptionWithDoc(ExceptionMissingFile,
169        (char*)OCIO::EXCEPTIONMISSINGFILE__DOC__, OCIO::GetExceptionMissingFilePyType(), NULL));
170#else
171    OCIO::SetExceptionPyType(PyErr_NewException(Exception,
172        OCIO::GetExceptionPyType(), NULL));
173    OCIO::SetExceptionMissingFilePyType(PyErr_NewException(ExceptionMissingFile,
174        OCIO::GetExceptionMissingFilePyType(), NULL));
175#endif
176    
177    PyModule_AddObject(m, "Exception", OCIO::GetExceptionPyType());
178    PyModule_AddObject(m, "ExceptionMissingFile", OCIO::GetExceptionMissingFilePyType());
179    
180    // Register Classes
181    OCIO::AddObjectToModule(OCIO::PyOCIO_ColorSpaceType, "ColorSpace", m);
182    OCIO::AddObjectToModule(OCIO::PyOCIO_ConfigType, "Config", m);
183    OCIO::AddConstantsModule(m);
184    OCIO::AddObjectToModule(OCIO::PyOCIO_ContextType, "Context", m);
185    OCIO::AddObjectToModule(OCIO::PyOCIO_LookType, "Look", m);
186    OCIO::AddObjectToModule(OCIO::PyOCIO_ProcessorType, "Processor", m);
187    OCIO::AddObjectToModule(OCIO::PyOCIO_ProcessorMetadataType, "ProcessorMetadata", m);
188    OCIO::AddObjectToModule(OCIO::PyOCIO_GpuShaderDescType, "GpuShaderDesc", m);
189    OCIO::AddObjectToModule(OCIO::PyOCIO_BakerType, "Baker", m);
190    OCIO::AddObjectToModule(OCIO::PyOCIO_TransformType, "Transform", m);
191    {
192        OCIO::AddObjectToModule(OCIO::PyOCIO_AllocationTransformType, "AllocationTransform", m);
193        OCIO::AddObjectToModule(OCIO::PyOCIO_CDLTransformType, "CDLTransform", m);
194        OCIO::AddObjectToModule(OCIO::PyOCIO_ColorSpaceTransformType, "ColorSpaceTransform", m);
195        OCIO::AddObjectToModule(OCIO::PyOCIO_DisplayTransformType, "DisplayTransform", m);
196        OCIO::AddObjectToModule(OCIO::PyOCIO_ExponentTransformType, "ExponentTransform", m);
197        OCIO::AddObjectToModule(OCIO::PyOCIO_FileTransformType, "FileTransform", m);
198        OCIO::AddObjectToModule(OCIO::PyOCIO_GroupTransformType, "GroupTransform", m);
199        OCIO::AddObjectToModule(OCIO::PyOCIO_LogTransformType, "LogTransform", m);
200        OCIO::AddObjectToModule(OCIO::PyOCIO_LookTransformType, "LookTransform", m);
201        OCIO::AddObjectToModule(OCIO::PyOCIO_MatrixTransformType, "MatrixTransform", m);
202    }
203
204    return MOD_SUCCESS_VAL(m);
205}