code cleanup: gpl header update (formatting)
[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 || !PySequence_Check(value) || PySequence_Size(value) != 3
91                 || !PyLong_Check(PySequence_Fast_GET_ITEM(value, 0))
92                 || !PyLong_Check(PySequence_Fast_GET_ITEM(value, 1))
93                 || !PyLong_Check(PySequence_Fast_GET_ITEM(value, 2)))
94         {
95                 PyErr_SetString(PyExc_TypeError, "The value must be a sequence of 3 ints");
96                 return -1;
97         }
98         // set color
99         getFilter(self)->setColor((unsigned char)(PyLong_AsSsize_t(PySequence_Fast_GET_ITEM(value, 0))),
100                 (unsigned char)(PyLong_AsSsize_t(PySequence_Fast_GET_ITEM(value, 1))),
101                 (unsigned char)(PyLong_AsSsize_t(PySequence_Fast_GET_ITEM(value, 2))));
102         // success
103         return 0;
104 }
105
106 // get limits
107 static PyObject *getLimits (PyFilter *self, void *closure)
108 {
109         return Py_BuildValue("[II]", getFilter(self)->getLimits()[0],
110                 getFilter(self)->getLimits()[1]);
111 }
112
113 // set limit
114 static int setLimits (PyFilter *self, PyObject *value, void *closure)
115 {
116         // check validity of parameter
117         if (value == NULL || !PySequence_Check(value) || PySequence_Size(value) != 2
118                 || !PyLong_Check(PySequence_Fast_GET_ITEM(value, 0))
119                 || !PyLong_Check(PySequence_Fast_GET_ITEM(value, 1)))
120         {
121                 PyErr_SetString(PyExc_TypeError, "The value must be a sequence of 2 ints");
122                 return -1;
123         }
124         // set limits
125         getFilter(self)->setLimits((unsigned short)(PyLong_AsSsize_t(PySequence_Fast_GET_ITEM(value, 0))),
126                 (unsigned short)(PyLong_AsSsize_t(PySequence_Fast_GET_ITEM(value, 1))));
127         // success
128         return 0;
129 }
130
131
132 // attributes structure
133 static PyGetSetDef filterBSGetSets[] =
134
135         {(char*)"color", (getter)getColor, (setter)setColor, (char*)"blue screen color", NULL},
136         {(char*)"limits", (getter)getLimits, (setter)setLimits, (char*)"blue screen color limits", NULL},
137         // attributes from FilterBase class
138         {(char*)"previous", (getter)Filter_getPrevious, (setter)Filter_setPrevious, (char*)"previous pixel filter", NULL},
139         {NULL}
140 };
141
142 // define python type
143 PyTypeObject FilterBlueScreenType =
144
145         PyVarObject_HEAD_INIT(NULL, 0)
146         "VideoTexture.FilterBlueScreen",   /*tp_name*/
147         sizeof(PyFilter),          /*tp_basicsize*/
148         0,                         /*tp_itemsize*/
149         (destructor)Filter_dealloc,/*tp_dealloc*/
150         0,                         /*tp_print*/
151         0,                         /*tp_getattr*/
152         0,                         /*tp_setattr*/
153         0,                         /*tp_compare*/
154         0,                         /*tp_repr*/
155         0,                         /*tp_as_number*/
156         0,                         /*tp_as_sequence*/
157         0,                         /*tp_as_mapping*/
158         0,                         /*tp_hash */
159         0,                         /*tp_call*/
160         0,                         /*tp_str*/
161         0,                         /*tp_getattro*/
162         0,                         /*tp_setattro*/
163         0,                         /*tp_as_buffer*/
164         Py_TPFLAGS_DEFAULT,        /*tp_flags*/
165         "Filter for Blue Screen objects",       /* tp_doc */
166         0,                             /* tp_traverse */
167         0,                             /* tp_clear */
168         0,                             /* tp_richcompare */
169         0,                             /* tp_weaklistoffset */
170         0,                             /* tp_iter */
171         0,                             /* tp_iternext */
172         NULL,                /* tp_methods */
173         0,                   /* tp_members */
174         filterBSGetSets,           /* tp_getset */
175         0,                         /* tp_base */
176         0,                         /* tp_dict */
177         0,                         /* tp_descr_get */
178         0,                         /* tp_descr_set */
179         0,                         /* tp_dictoffset */
180         (initproc)Filter_init<FilterBlueScreen>,     /* tp_init */
181         0,                         /* tp_alloc */
182         Filter_allocNew,           /* tp_new */
183 };
184