code cleanup:
[blender.git] / source / gameengine / VideoTexture / FilterBlueScreen.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/FilterBlueScreen.cpp
28  *  \ingroup bgevideotex
29  */
30
31 #include "PyObjectPlus.h"
32 #include <structmember.h>
33
34 #include "FilterBlueScreen.h"
35
36 #include "FilterBase.h"
37 #include "PyTypeList.h"
38
39 // implementation FilterBlueScreen
40
41 // constructor
42 FilterBlueScreen::FilterBlueScreen (void)
43 {
44         // set color to blue
45         setColor(0, 0, 255);
46         // set limits
47         setLimits(64, 64);
48 }
49
50 // set color
51 void FilterBlueScreen::setColor (unsigned char red, unsigned char green, unsigned char blue)
52 {
53         m_color[0] = red;
54         m_color[1] = green;
55         m_color[2] = blue;
56 }
57
58 // set limits for color variation
59 void FilterBlueScreen::setLimits (unsigned short minLimit, unsigned short maxLimit)
60 {
61         m_limits[0] = minLimit;
62         m_limits[1] = maxLimit > minLimit ? maxLimit : minLimit;
63         // calculate square values
64         for (short idx = 0; idx < 2; ++idx)
65                 m_squareLimits[idx] = m_limits[idx] * m_limits[idx];
66         // limits distance
67         m_limitDist = m_squareLimits[1] - m_squareLimits[0];
68 }
69
70
71
72 // cast Filter pointer to FilterBlueScreen
73 inline FilterBlueScreen * getFilter (PyFilter *self)
74 { return static_cast<FilterBlueScreen*>(self->m_filter); }
75
76
77 // python methods and get/sets
78
79 // get color
80 static PyObject *getColor (PyFilter *self, void *closure)
81 {
82         return Py_BuildValue("[BBB]", getFilter(self)->getColor()[0],
83                 getFilter(self)->getColor()[1], getFilter(self)->getColor()[2]);
84 }
85
86 // set color
87 static int setColor(PyFilter *self, PyObject *value, void *closure)
88 {
89         // check validity of parameter
90         if (value == NULL ||
91             !(PyTuple_Check(value) || PyList_Check(value)) ||
92             PySequence_Fast_GET_SIZE(value) != 3 ||
93             !PyLong_Check(PySequence_Fast_GET_ITEM(value, 0)) ||
94             !PyLong_Check(PySequence_Fast_GET_ITEM(value, 1)) ||
95             !PyLong_Check(PySequence_Fast_GET_ITEM(value, 2)))
96         {
97                 PyErr_SetString(PyExc_TypeError, "The value must be a sequence of 3 ints");
98                 return -1;
99         }
100         // set color
101         getFilter(self)->setColor(
102                 (unsigned char)(PyLong_AsLong(PySequence_Fast_GET_ITEM(value, 0))),
103                 (unsigned char)(PyLong_AsLong(PySequence_Fast_GET_ITEM(value, 1))),
104                 (unsigned char)(PyLong_AsLong(PySequence_Fast_GET_ITEM(value, 2))));
105         // success
106         return 0;
107 }
108
109 // get limits
110 static PyObject *getLimits (PyFilter *self, void *closure)
111 {
112         return Py_BuildValue("[II]", getFilter(self)->getLimits()[0],
113                 getFilter(self)->getLimits()[1]);
114 }
115
116 // set limit
117 static int setLimits(PyFilter *self, PyObject *value, void *closure)
118 {
119         // check validity of parameter
120         if (value == NULL ||
121             !(PyTuple_Check(value) || PyList_Check(value)) ||
122             PySequence_Fast_GET_SIZE(value) != 2 ||
123             !PyLong_Check(PySequence_Fast_GET_ITEM(value, 0)) ||
124             !PyLong_Check(PySequence_Fast_GET_ITEM(value, 1)))
125         {
126                 PyErr_SetString(PyExc_TypeError, "The value must be a sequence of 2 ints");
127                 return -1;
128         }
129         // set limits
130         getFilter(self)->setLimits(
131                 (unsigned short)(PyLong_AsLong(PySequence_Fast_GET_ITEM(value, 0))),
132                 (unsigned short)(PyLong_AsLong(PySequence_Fast_GET_ITEM(value, 1))));
133         // success
134         return 0;
135 }
136
137
138 // attributes structure
139 static PyGetSetDef filterBSGetSets[] =
140
141         {(char*)"color", (getter)getColor, (setter)setColor, (char*)"blue screen color", NULL},
142         {(char*)"limits", (getter)getLimits, (setter)setLimits, (char*)"blue screen color limits", NULL},
143         // attributes from FilterBase class
144         {(char*)"previous", (getter)Filter_getPrevious, (setter)Filter_setPrevious, (char*)"previous pixel filter", NULL},
145         {NULL}
146 };
147
148 // define python type
149 PyTypeObject FilterBlueScreenType =
150
151         PyVarObject_HEAD_INIT(NULL, 0)
152         "VideoTexture.FilterBlueScreen",   /*tp_name*/
153         sizeof(PyFilter),          /*tp_basicsize*/
154         0,                         /*tp_itemsize*/
155         (destructor)Filter_dealloc,/*tp_dealloc*/
156         0,                         /*tp_print*/
157         0,                         /*tp_getattr*/
158         0,                         /*tp_setattr*/
159         0,                         /*tp_compare*/
160         0,                         /*tp_repr*/
161         0,                         /*tp_as_number*/
162         0,                         /*tp_as_sequence*/
163         0,                         /*tp_as_mapping*/
164         0,                         /*tp_hash */
165         0,                         /*tp_call*/
166         0,                         /*tp_str*/
167         0,                         /*tp_getattro*/
168         0,                         /*tp_setattro*/
169         0,                         /*tp_as_buffer*/
170         Py_TPFLAGS_DEFAULT,        /*tp_flags*/
171         "Filter for Blue Screen objects",       /* tp_doc */
172         0,                         /* tp_traverse */
173         0,                         /* tp_clear */
174         0,                         /* tp_richcompare */
175         0,                         /* tp_weaklistoffset */
176         0,                         /* tp_iter */
177         0,                         /* tp_iternext */
178         NULL,                      /* tp_methods */
179         0,                         /* tp_members */
180         filterBSGetSets,           /* tp_getset */
181         0,                         /* tp_base */
182         0,                         /* tp_dict */
183         0,                         /* tp_descr_get */
184         0,                         /* tp_descr_set */
185         0,                         /* tp_dictoffset */
186         (initproc)Filter_init<FilterBlueScreen>,     /* tp_init */
187         0,                         /* tp_alloc */
188         Filter_allocNew,           /* tp_new */
189 };
190