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