Blender V2.61 - r43446

ImageMix.cpp

Go to the documentation of this file.
00001 /*
00002 -----------------------------------------------------------------------------
00003 This source file is part of VideoTexture library
00004 
00005 Copyright (c) 2007 The Zdeno Ash Miklas
00006 
00007 This program is free software; you can redistribute it and/or modify it under
00008 the terms of the GNU Lesser General Public License as published by the Free Software
00009 Foundation; either version 2 of the License, or (at your option) any later
00010 version.
00011 
00012 This program is distributed in the hope that it will be useful, but WITHOUT
00013 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00014 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
00015 
00016 You should have received a copy of the GNU Lesser General Public License along with
00017 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
00018 Place - Suite 330, Boston, MA 02111-1307, USA, or go to
00019 http://www.gnu.org/copyleft/lesser.txt.
00020 -----------------------------------------------------------------------------
00021 */
00022 
00028 // implementation
00029 
00030 #include <PyObjectPlus.h>
00031 #include <structmember.h>
00032 
00033 #include "ImageMix.h"
00034 
00035 #include "ImageBase.h"
00036 
00037 #include "Exception.h"
00038 
00039 
00040 // cast ImageSource pointer to ImageSourceMix
00041 inline ImageSourceMix * getImageSourceMix (ImageSource * src)
00042 { return static_cast<ImageSourceMix*>(src); }
00043 
00044 
00045 // get weight
00046 short ImageMix::getWeight (const char * id)
00047 {
00048     // find source
00049     ImageSourceList::iterator src = findSource(id);
00050     // if found, return its weight
00051     return src != m_sources.end() ? getImageSourceMix(*src)->getWeight() : 0;
00052 }
00053 
00054 // set weight
00055 bool ImageMix::setWeight (const char * id, short weight)
00056 {
00057     // find source
00058     ImageSourceList::iterator src = findSource(id);
00059     // if source isn't found, report it
00060     if (src == m_sources.end()) return false;
00061     // set its weight
00062     getImageSourceMix(*src)->setWeight(weight);
00063     return true;
00064 }
00065 
00066 ExceptionID ImageSizesNotMatch;
00067 
00068 ExpDesc ImageSizesNotMatchDesc (ImageSizesNotMatch, "Image sizes of sources are different");
00069 
00070 // calculate image from sources and set its availability
00071 void ImageMix::calcImage (unsigned int texId, double ts)
00072 {
00073     // check source sizes
00074     if (!checkSourceSizes()) THRWEXCP(ImageSizesNotMatch, S_OK);
00075     // set offsets to image buffers
00076     for (ImageSourceList::iterator it = m_sources.begin(); it != m_sources.end(); ++it)
00077         // if image buffer is available
00078         if ((*it)->getImageBuf() != NULL)
00079             // set its offset
00080             getImageSourceMix(*it)->setOffset(m_sources[0]->getImageBuf());
00081         // otherwise don't calculate image
00082         else 
00083             return;
00084     // if there is only single source
00085     if (m_sources.size() == 1)
00086     {
00087         // use single filter
00088         FilterBase mixFilt;
00089         // fiter and convert image
00090         filterImage(mixFilt, m_sources[0]->getImageBuf(), m_sources[0]->getSize());
00091     }
00092     // otherwise use mix filter to merge source images
00093     else
00094     {
00095         FilterImageMix mixFilt (m_sources);
00096         // fiter and convert image
00097         filterImage(mixFilt, m_sources[0]->getImageBuf(), m_sources[0]->getSize());
00098     }
00099 }
00100 
00101 
00102 
00103 // cast Image pointer to ImageMix
00104 inline ImageMix * getImageMix (PyImage * self)
00105 { return static_cast<ImageMix*>(self->m_image); }
00106 
00107 
00108 // python methods
00109 
00110 // get source weight
00111 PyObject * getWeight (PyImage * self, PyObject * args)
00112 {
00113     // weight
00114     short weight = 0;
00115     // get arguments
00116     char * id;
00117     if (!PyArg_ParseTuple(args, "s:getWeight", &id))
00118         return NULL;
00119     if (self->m_image != NULL)
00120         // get weight
00121         weight = getImageMix(self)->getWeight(id);
00122     // return weight
00123     return Py_BuildValue("h", weight);
00124 }
00125 
00126 
00127 // set source weight
00128 PyObject * setWeight (PyImage * self, PyObject * args)
00129 {
00130     // get arguments
00131     char * id;
00132     short weight = 0;
00133     if (!PyArg_ParseTuple(args, "sh:setWeight", &id, &weight))
00134         return NULL;
00135     if (self->m_image != NULL)
00136         // set weight
00137         if (!getImageMix(self)->setWeight(id, weight))
00138         {
00139             // if not set, report error
00140             PyErr_SetString(PyExc_RuntimeError, "Invalid id of source");
00141             return NULL;
00142         }
00143     // return none
00144     Py_RETURN_NONE;
00145 }
00146 
00147 
00148 // methods structure
00149 static PyMethodDef imageMixMethods[] =
00150 { 
00151     {"getSource", (PyCFunction)Image_getSource, METH_VARARGS, "get image source"},
00152     {"setSource", (PyCFunction)Image_setSource, METH_VARARGS, "set image source"},
00153     {"getWeight", (PyCFunction)getWeight, METH_VARARGS, "get image source weight"},
00154     {"setWeight", (PyCFunction)setWeight, METH_VARARGS, "set image source weight"},
00155     // methods from ImageBase class
00156     {"refresh", (PyCFunction)Image_refresh, METH_NOARGS, "Refresh image - invalidate its current content"},
00157     {NULL}
00158 };
00159 // attributes structure
00160 static PyGetSetDef imageMixGetSets[] =
00161 { // attributes from ImageBase class
00162     {(char*)"valid", (getter)Image_valid, NULL, (char*)"bool to tell if an image is available", NULL},
00163     {(char*)"image", (getter)Image_getImage, NULL, (char*)"image data", NULL},
00164     {(char*)"size", (getter)Image_getSize, NULL, (char*)"image size", NULL},
00165     {(char*)"scale", (getter)Image_getScale, (setter)Image_setScale, (char*)"fast scale of image (near neighbour)", NULL},
00166     {(char*)"flip", (getter)Image_getFlip, (setter)Image_setFlip, (char*)"flip image vertically", NULL},
00167     {(char*)"filter", (getter)Image_getFilter, (setter)Image_setFilter, (char*)"pixel filter", NULL},
00168     {NULL}
00169 };
00170 
00171 
00172 // define python type
00173 PyTypeObject ImageMixType =
00174 { 
00175     PyVarObject_HEAD_INIT(NULL, 0)
00176     "VideoTexture.ImageMix",   /*tp_name*/
00177     sizeof(PyImage),          /*tp_basicsize*/
00178     0,                         /*tp_itemsize*/
00179     (destructor)Image_dealloc, /*tp_dealloc*/
00180     0,                         /*tp_print*/
00181     0,                         /*tp_getattr*/
00182     0,                         /*tp_setattr*/
00183     0,                         /*tp_compare*/
00184     0,                         /*tp_repr*/
00185     0,                         /*tp_as_number*/
00186     0,                         /*tp_as_sequence*/
00187     0,                         /*tp_as_mapping*/
00188     0,                         /*tp_hash */
00189     0,                         /*tp_call*/
00190     0,                         /*tp_str*/
00191     0,                         /*tp_getattro*/
00192     0,                         /*tp_setattro*/
00193     &imageBufferProcs,         /*tp_as_buffer*/
00194     Py_TPFLAGS_DEFAULT,        /*tp_flags*/
00195     "Image mixer",       /* tp_doc */
00196     0,                     /* tp_traverse */
00197     0,                     /* tp_clear */
00198     0,                     /* tp_richcompare */
00199     0,                     /* tp_weaklistoffset */
00200     0,                     /* tp_iter */
00201     0,                     /* tp_iternext */
00202     imageMixMethods,    /* tp_methods */
00203     0,                   /* tp_members */
00204     imageMixGetSets,          /* tp_getset */
00205     0,                         /* tp_base */
00206     0,                         /* tp_dict */
00207     0,                         /* tp_descr_get */
00208     0,                         /* tp_descr_set */
00209     0,                         /* tp_dictoffset */
00210     (initproc)Image_init<ImageMix>,     /* tp_init */
00211     0,                         /* tp_alloc */
00212     Image_allocNew,           /* tp_new */
00213 };
00214