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