svn merge ^/trunk/blender -r41226:41227 .
[blender.git] / source / gameengine / VideoTexture / FilterBlueScreen.cpp
1 /** \file gameengine/VideoTexture/FilterBlueScreen.cpp
2  *  \ingroup bgevideotex
3  */
4 -----------------------------------------------------------------------------
5 This source file is part of VideoTexture library
6
7 Copyright (c) 2007 The Zdeno Ash Miklas
8
9 This program is free software; you can redistribute it and/or modify it under
10 the terms of the GNU Lesser General Public License as published by the Free Software
11 Foundation; either version 2 of the License, or (at your option) any later
12 version.
13
14 This program is distributed in the hope that it will be useful, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License along with
19 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
20 Place - Suite 330, Boston, MA 02111-1307, USA, or go to
21 http://www.gnu.org/copyleft/lesser.txt.
22 -----------------------------------------------------------------------------
23 */
24
25
26 #include <PyObjectPlus.h>
27 #include <structmember.h>
28
29 #include "FilterBlueScreen.h"
30
31 #include "FilterBase.h"
32 #include "PyTypeList.h"
33
34 // implementation FilterBlueScreen
35
36 // constructor
37 FilterBlueScreen::FilterBlueScreen (void)
38 {
39         // set color to blue
40         setColor(0, 0, 255);
41         // set limits
42         setLimits(64, 64);
43 }
44
45 // set color
46 void FilterBlueScreen::setColor (unsigned char red, unsigned char green, unsigned char blue)
47 {
48         m_color[0] = red;
49         m_color[1] = green;
50         m_color[2] = blue;
51 }
52
53 // set limits for color variation
54 void FilterBlueScreen::setLimits (unsigned short minLimit, unsigned short maxLimit)
55 {
56         m_limits[0] = minLimit;
57         m_limits[1] = maxLimit > minLimit ? maxLimit : minLimit;
58         // calculate square values
59         for (short idx = 0; idx < 2; ++idx)
60                 m_squareLimits[idx] = m_limits[idx] * m_limits[idx];
61         // limits distance
62         m_limitDist = m_squareLimits[1] - m_squareLimits[0];
63 }
64
65
66
67 // cast Filter pointer to FilterBlueScreen
68 inline FilterBlueScreen * getFilter (PyFilter * self)
69 { return static_cast<FilterBlueScreen*>(self->m_filter); }
70
71
72 // python methods and get/sets
73
74 // get color
75 static PyObject * getColor (PyFilter * self, void * closure)
76 {
77         return Py_BuildValue("[BBB]", getFilter(self)->getColor()[0],
78                 getFilter(self)->getColor()[1], getFilter(self)->getColor()[2]);
79 }
80
81 // set color
82 static int setColor (PyFilter * self, PyObject * value, void * closure)
83 {
84         // check validity of parameter
85         if (value == NULL || !PySequence_Check(value) || PySequence_Size(value) != 3
86                 || !PyLong_Check(PySequence_Fast_GET_ITEM(value, 0))
87                 || !PyLong_Check(PySequence_Fast_GET_ITEM(value, 1))
88                 || !PyLong_Check(PySequence_Fast_GET_ITEM(value, 2)))
89         {
90                 PyErr_SetString(PyExc_TypeError, "The value must be a sequence of 3 ints");
91                 return -1;
92         }
93         // set color
94         getFilter(self)->setColor((unsigned char)(PyLong_AsSsize_t(PySequence_Fast_GET_ITEM(value, 0))),
95                 (unsigned char)(PyLong_AsSsize_t(PySequence_Fast_GET_ITEM(value, 1))),
96                 (unsigned char)(PyLong_AsSsize_t(PySequence_Fast_GET_ITEM(value, 2))));
97         // success
98         return 0;
99 }
100
101 // get limits
102 static PyObject * getLimits (PyFilter * self, void * closure)
103 {
104         return Py_BuildValue("[II]", getFilter(self)->getLimits()[0],
105                 getFilter(self)->getLimits()[1]);
106 }
107
108 // set limit
109 static int setLimits (PyFilter * self, PyObject * value, void * closure)
110 {
111         // check validity of parameter
112         if (value == NULL || !PySequence_Check(value) || PySequence_Size(value) != 2
113                 || !PyLong_Check(PySequence_Fast_GET_ITEM(value, 0))
114                 || !PyLong_Check(PySequence_Fast_GET_ITEM(value, 1)))
115         {
116                 PyErr_SetString(PyExc_TypeError, "The value must be a sequence of 2 ints");
117                 return -1;
118         }
119         // set limits
120         getFilter(self)->setLimits((unsigned short)(PyLong_AsSsize_t(PySequence_Fast_GET_ITEM(value, 0))),
121                 (unsigned short)(PyLong_AsSsize_t(PySequence_Fast_GET_ITEM(value, 1))));
122         // success
123         return 0;
124 }
125
126
127 // attributes structure
128 static PyGetSetDef filterBSGetSets[] =
129
130         {(char*)"color", (getter)getColor, (setter)setColor, (char*)"blue screen color", NULL},
131         {(char*)"limits", (getter)getLimits, (setter)setLimits, (char*)"blue screen color limits", NULL},
132         // attributes from FilterBase class
133         {(char*)"previous", (getter)Filter_getPrevious, (setter)Filter_setPrevious, (char*)"previous pixel filter", NULL},
134         {NULL}
135 };
136
137 // define python type
138 PyTypeObject FilterBlueScreenType =
139
140         PyVarObject_HEAD_INIT(NULL, 0)
141         "VideoTexture.FilterBlueScreen",   /*tp_name*/
142         sizeof(PyFilter),          /*tp_basicsize*/
143         0,                         /*tp_itemsize*/
144         (destructor)Filter_dealloc,/*tp_dealloc*/
145         0,                         /*tp_print*/
146         0,                         /*tp_getattr*/
147         0,                         /*tp_setattr*/
148         0,                         /*tp_compare*/
149         0,                         /*tp_repr*/
150         0,                         /*tp_as_number*/
151         0,                         /*tp_as_sequence*/
152         0,                         /*tp_as_mapping*/
153         0,                         /*tp_hash */
154         0,                         /*tp_call*/
155         0,                         /*tp_str*/
156         0,                         /*tp_getattro*/
157         0,                         /*tp_setattro*/
158         0,                         /*tp_as_buffer*/
159         Py_TPFLAGS_DEFAULT,        /*tp_flags*/
160         "Filter for Blue Screen objects",       /* tp_doc */
161         0,                             /* tp_traverse */
162         0,                             /* tp_clear */
163         0,                             /* tp_richcompare */
164         0,                             /* tp_weaklistoffset */
165         0,                             /* tp_iter */
166         0,                             /* tp_iternext */
167         NULL,                /* tp_methods */
168         0,                   /* tp_members */
169         filterBSGetSets,           /* tp_getset */
170         0,                         /* tp_base */
171         0,                         /* tp_dict */
172         0,                         /* tp_descr_get */
173         0,                         /* tp_descr_set */
174         0,                         /* tp_dictoffset */
175         (initproc)Filter_init<FilterBlueScreen>,     /* tp_init */
176         0,                         /* tp_alloc */
177         Filter_allocNew,           /* tp_new */
178 };
179