svn merge -r 20651:20822 https://svn.blender.org/svnroot/bf-blender/branches/blender2...
[blender.git] / source / gameengine / VideoTexture / ImageMix.cpp
1 /* $Id$
2 -----------------------------------------------------------------------------
3 This source file is part of VideoTexture library
4
5 Copyright (c) 2007 The Zdeno Ash Miklas
6
7 This program is free software; you can redistribute it and/or modify it under
8 the terms of the GNU Lesser General Public License as published by the Free Software
9 Foundation; either version 2 of the License, or (at your option) any later
10 version.
11
12 This program is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License along with
17 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
18 Place - Suite 330, Boston, MA 02111-1307, USA, or go to
19 http://www.gnu.org/copyleft/lesser.txt.
20 -----------------------------------------------------------------------------
21 */
22
23 // implementation
24
25 #include <PyObjectPlus.h>
26 #include <structmember.h>
27
28 #include "ImageMix.h"
29
30 #include "ImageBase.h"
31
32 #include "Exception.h"
33
34
35 // cast ImageSource pointer to ImageSourceMix
36 inline ImageSourceMix * getImageSourceMix (ImageSource * src)
37 { return static_cast<ImageSourceMix*>(src); }
38
39
40 // get weight
41 short ImageMix::getWeight (const char * id)
42 {
43         // find source
44         ImageSourceList::iterator src = findSource(id);
45         // if found, return its weight
46         return src != m_sources.end() ? getImageSourceMix(*src)->getWeight() : 0;
47 }
48
49 // set weight
50 bool ImageMix::setWeight (const char * id, short weight)
51 {
52         // find source
53         ImageSourceList::iterator src = findSource(id);
54         // if source isn't found, report it
55         if (src == m_sources.end()) return false;
56         // set its weight
57         getImageSourceMix(*src)->setWeight(weight);
58         return true;
59 }
60
61 ExceptionID ImageSizesNotMatch;
62
63 ExpDesc ImageSizesNotMatchDesc (ImageSizesNotMatch, "Image sizes of sources are different");
64
65 // calculate image from sources and set its availability
66 void ImageMix::calcImage (unsigned int texId)
67 {
68         // check source sizes
69         if (!checkSourceSizes()) THRWEXCP(ImageSizesNotMatch, S_OK);
70         // set offsets to image buffers
71         for (ImageSourceList::iterator it = m_sources.begin(); it != m_sources.end(); ++it)
72                 // if image buffer is available
73                 if ((*it)->getImageBuf() != NULL)
74                         // set its offset
75                         getImageSourceMix(*it)->setOffset(m_sources[0]->getImageBuf());
76                 // otherwise don't calculate image
77                 else 
78                         return;
79         // if there is only single source
80         if (m_sources.size() == 1)
81         {
82                 // use single filter
83                 FilterBase mixFilt;
84                 // fiter and convert image
85                 filterImage(mixFilt, m_sources[0]->getImageBuf(), m_sources[0]->getSize());
86         }
87         // otherwise use mix filter to merge source images
88         else
89         {
90                 FilterImageMix mixFilt (m_sources);
91                 // fiter and convert image
92                 filterImage(mixFilt, m_sources[0]->getImageBuf(), m_sources[0]->getSize());
93         }
94 }
95
96
97
98 // cast Image pointer to ImageMix
99 inline ImageMix * getImageMix (PyImage * self)
100 { return static_cast<ImageMix*>(self->m_image); }
101
102
103 // python methods
104
105 // get source weight
106 PyObject * getWeight (PyImage * self, PyObject * args)
107 {
108         // weight
109         short weight = 0;
110         // get arguments
111         char * id;
112         if (!PyArg_ParseTuple(args, "s:getWeight", &id))
113                 return NULL;
114         if (self->m_image != NULL)
115                 // get weight
116                 weight = getImageMix(self)->getWeight(id);
117         // return weight
118         return Py_BuildValue("h", weight);
119 }
120
121
122 // set source weight
123 PyObject * setWeight (PyImage * self, PyObject * args)
124 {
125         // get arguments
126         char * id;
127         short weight = 0;
128         if (!PyArg_ParseTuple(args, "sh:setWeight", &id, &weight))
129                 return NULL;
130         if (self->m_image != NULL)
131                 // set weight
132                 if (!getImageMix(self)->setWeight(id, weight))
133                 {
134                         // if not set, report error
135                         PyErr_SetString(PyExc_RuntimeError, "Invalid id of source");;
136                         return NULL;
137                 }
138         // return none
139         Py_RETURN_NONE;
140 }
141
142
143 // methods structure
144 static PyMethodDef imageMixMethods[] =
145
146         {"getSource", (PyCFunction)Image_getSource, METH_VARARGS, "get image source"},
147         {"setSource", (PyCFunction)Image_setSource, METH_VARARGS, "set image source"},
148         {"getWeight", (PyCFunction)getWeight, METH_VARARGS, "get image source weight"},
149         {"setWeight", (PyCFunction)setWeight, METH_VARARGS, "set image source weight"},
150         // methods from ImageBase class
151         {"refresh", (PyCFunction)Image_refresh, METH_NOARGS, "Refresh image - invalidate its current content"},
152         {NULL}
153 };
154 // attributes structure
155 static PyGetSetDef imageMixGetSets[] =
156 { // attributes from ImageBase class
157         {(char*)"image", (getter)Image_getImage, NULL, (char*)"image data", NULL},
158         {(char*)"size", (getter)Image_getSize, NULL, (char*)"image size", NULL},
159         {(char*)"scale", (getter)Image_getScale, (setter)Image_setScale, (char*)"fast scale of image (near neighbour)", NULL},
160         {(char*)"flip", (getter)Image_getFlip, (setter)Image_setFlip, (char*)"flip image vertically", NULL},
161         {(char*)"filter", (getter)Image_getFilter, (setter)Image_setFilter, (char*)"pixel filter", NULL},
162         {NULL}
163 };
164
165
166 // define python type
167 PyTypeObject ImageMixType =
168
169 #if (PY_VERSION_HEX >= 0x02060000)
170         PyVarObject_HEAD_INIT(NULL, 0)
171 #else
172         /* python 2.5 and below */
173         PyObject_HEAD_INIT( NULL )  /* required py macro */
174         0,                         /*ob_size*/
175 #endif
176         "VideoTexture.ImageMix",   /*tp_name*/
177         sizeof(PyImage),          /*tp_basicsize*/
178         0,                         /*tp_itemsize*/
179         (destructor)Image_dealloc, /*tp_dealloc*/
180         0,                         /*tp_print*/
181         0,                         /*tp_getattr*/
182         0,                         /*tp_setattr*/
183         0,                         /*tp_compare*/
184         0,                         /*tp_repr*/
185         0,                         /*tp_as_number*/
186         0,                         /*tp_as_sequence*/
187         0,                         /*tp_as_mapping*/
188         0,                         /*tp_hash */
189         0,                         /*tp_call*/
190         0,                         /*tp_str*/
191         0,                         /*tp_getattro*/
192         0,                         /*tp_setattro*/
193         0,                         /*tp_as_buffer*/
194         Py_TPFLAGS_DEFAULT,        /*tp_flags*/
195         "Image mixer",       /* tp_doc */
196         0,                             /* tp_traverse */
197         0,                             /* tp_clear */
198         0,                             /* tp_richcompare */
199         0,                             /* tp_weaklistoffset */
200         0,                             /* tp_iter */
201         0,                             /* tp_iternext */
202         imageMixMethods,    /* tp_methods */
203         0,                   /* tp_members */
204         imageMixGetSets,          /* tp_getset */
205         0,                         /* tp_base */
206         0,                         /* tp_dict */
207         0,                         /* tp_descr_get */
208         0,                         /* tp_descr_set */
209         0,                         /* tp_dictoffset */
210         (initproc)Image_init<ImageMix>,     /* tp_init */
211         0,                         /* tp_alloc */
212         Image_allocNew,           /* tp_new */
213 };
214