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