remove $Id: tags after discussion on the mailign list: http://markmail.org/message...
[blender.git] / source / gameengine / VideoTexture / ImageMix.cpp
1 /*
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 /** \file gameengine/VideoTexture/ImageMix.cpp
24  *  \ingroup bgevideotex
25  */
26
27
28 // implementation
29
30 #include <PyObjectPlus.h>
31 #include <structmember.h>
32
33 #include "ImageMix.h"
34
35 #include "ImageBase.h"
36
37 #include "Exception.h"
38
39
40 // cast ImageSource pointer to ImageSourceMix
41 inline ImageSourceMix * getImageSourceMix (ImageSource * src)
42 { return static_cast<ImageSourceMix*>(src); }
43
44
45 // get weight
46 short ImageMix::getWeight (const char * id)
47 {
48         // find source
49         ImageSourceList::iterator src = findSource(id);
50         // if found, return its weight
51         return src != m_sources.end() ? getImageSourceMix(*src)->getWeight() : 0;
52 }
53
54 // set weight
55 bool ImageMix::setWeight (const char * id, short weight)
56 {
57         // find source
58         ImageSourceList::iterator src = findSource(id);
59         // if source isn't found, report it
60         if (src == m_sources.end()) return false;
61         // set its weight
62         getImageSourceMix(*src)->setWeight(weight);
63         return true;
64 }
65
66 ExceptionID ImageSizesNotMatch;
67
68 ExpDesc ImageSizesNotMatchDesc (ImageSizesNotMatch, "Image sizes of sources are different");
69
70 // calculate image from sources and set its availability
71 void ImageMix::calcImage (unsigned int texId, double ts)
72 {
73         // check source sizes
74         if (!checkSourceSizes()) THRWEXCP(ImageSizesNotMatch, S_OK);
75         // set offsets to image buffers
76         for (ImageSourceList::iterator it = m_sources.begin(); it != m_sources.end(); ++it)
77                 // if image buffer is available
78                 if ((*it)->getImageBuf() != NULL)
79                         // set its offset
80                         getImageSourceMix(*it)->setOffset(m_sources[0]->getImageBuf());
81                 // otherwise don't calculate image
82                 else 
83                         return;
84         // if there is only single source
85         if (m_sources.size() == 1)
86         {
87                 // use single filter
88                 FilterBase mixFilt;
89                 // fiter and convert image
90                 filterImage(mixFilt, m_sources[0]->getImageBuf(), m_sources[0]->getSize());
91         }
92         // otherwise use mix filter to merge source images
93         else
94         {
95                 FilterImageMix mixFilt (m_sources);
96                 // fiter and convert image
97                 filterImage(mixFilt, m_sources[0]->getImageBuf(), m_sources[0]->getSize());
98         }
99 }
100
101
102
103 // cast Image pointer to ImageMix
104 inline ImageMix * getImageMix (PyImage * self)
105 { return static_cast<ImageMix*>(self->m_image); }
106
107
108 // python methods
109
110 // get source weight
111 PyObject * getWeight (PyImage * self, PyObject * args)
112 {
113         // weight
114         short weight = 0;
115         // get arguments
116         char * id;
117         if (!PyArg_ParseTuple(args, "s:getWeight", &id))
118                 return NULL;
119         if (self->m_image != NULL)
120                 // get weight
121                 weight = getImageMix(self)->getWeight(id);
122         // return weight
123         return Py_BuildValue("h", weight);
124 }
125
126
127 // set source weight
128 PyObject * setWeight (PyImage * self, PyObject * args)
129 {
130         // get arguments
131         char * id;
132         short weight = 0;
133         if (!PyArg_ParseTuple(args, "sh:setWeight", &id, &weight))
134                 return NULL;
135         if (self->m_image != NULL)
136                 // set weight
137                 if (!getImageMix(self)->setWeight(id, weight))
138                 {
139                         // if not set, report error
140                         PyErr_SetString(PyExc_RuntimeError, "Invalid id of source");
141                         return NULL;
142                 }
143         // return none
144         Py_RETURN_NONE;
145 }
146
147
148 // methods structure
149 static PyMethodDef imageMixMethods[] =
150
151         {"getSource", (PyCFunction)Image_getSource, METH_VARARGS, "get image source"},
152         {"setSource", (PyCFunction)Image_setSource, METH_VARARGS, "set image source"},
153         {"getWeight", (PyCFunction)getWeight, METH_VARARGS, "get image source weight"},
154         {"setWeight", (PyCFunction)setWeight, METH_VARARGS, "set image source weight"},
155         // methods from ImageBase class
156         {"refresh", (PyCFunction)Image_refresh, METH_NOARGS, "Refresh image - invalidate its current content"},
157         {NULL}
158 };
159 // attributes structure
160 static PyGetSetDef imageMixGetSets[] =
161 { // attributes from ImageBase class
162         {(char*)"valid", (getter)Image_valid, NULL, (char*)"bool to tell if an image is available", NULL},
163         {(char*)"image", (getter)Image_getImage, NULL, (char*)"image data", NULL},
164         {(char*)"size", (getter)Image_getSize, NULL, (char*)"image size", NULL},
165         {(char*)"scale", (getter)Image_getScale, (setter)Image_setScale, (char*)"fast scale of image (near neighbour)", NULL},
166         {(char*)"flip", (getter)Image_getFlip, (setter)Image_setFlip, (char*)"flip image vertically", NULL},
167         {(char*)"filter", (getter)Image_getFilter, (setter)Image_setFilter, (char*)"pixel filter", NULL},
168         {NULL}
169 };
170
171
172 // define python type
173 PyTypeObject ImageMixType =
174
175         PyVarObject_HEAD_INIT(NULL, 0)
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         &imageBufferProcs,         /*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