2 -----------------------------------------------------------------------------
3 This source file is part of VideoTexture library
5 Copyright (c) 2007 The Zdeno Ash Miklas
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
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.
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 -----------------------------------------------------------------------------
24 #include <PyObjectPlus.h>
25 #include <structmember.h>
27 #include "FilterColor.h"
29 #include "FilterBase.h"
30 #include "PyTypeList.h"
32 // implementation FilterGray
34 // attributes structure
35 static PyGetSetDef filterGrayGetSets[] =
36 { // attributes from FilterBase class
37 {"previous", (getter)Filter_getPrevious, (setter)Filter_setPrevious, "previous pixel filter", NULL},
42 PyTypeObject FilterGrayType =
44 PyObject_HEAD_INIT(NULL)
46 "VideoTexture.FilterGray", /*tp_name*/
47 sizeof(PyFilter), /*tp_basicsize*/
49 (destructor)Filter_dealloc,/*tp_dealloc*/
64 Py_TPFLAGS_DEFAULT, /*tp_flags*/
65 "Filter for gray scale effect", /* tp_doc */
68 0, /* tp_richcompare */
69 0, /* tp_weaklistoffset */
72 NULL, /* tp_methods */
74 filterGrayGetSets, /* tp_getset */
79 0, /* tp_dictoffset */
80 (initproc)Filter_init<FilterGray>, /* tp_init */
82 Filter_allocNew, /* tp_new */
86 // implementation FilterColor
89 FilterColor::FilterColor (void)
91 // reset color matrix to identity
92 for (int r = 0; r < 4; ++r)
93 for (int c = 0; c < 5; ++c)
94 m_matrix[r][c] = (r == c) ? 256 : 0;
98 void FilterColor::setMatrix (ColorMatrix & mat)
101 for (int r = 0; r < 4; ++r)
102 for (int c = 0; c < 5; ++c)
103 m_matrix[r][c] = mat[r][c];
108 // cast Filter pointer to FilterColor
109 inline FilterColor * getFilterColor (PyFilter * self)
110 { return static_cast<FilterColor*>(self->m_filter); }
113 // python methods and get/sets
116 static PyObject * getMatrix (PyFilter * self, void * closure)
118 ColorMatrix & mat = getFilterColor(self)->getMatrix();
119 return Py_BuildValue("((hhhhh)(hhhhh)(hhhhh)(hhhhh))",
120 mat[0][0], mat[0][1], mat[0][2], mat[0][3], mat[0][4],
121 mat[1][0], mat[1][1], mat[1][2], mat[1][3], mat[1][4],
122 mat[2][0], mat[2][1], mat[2][2], mat[2][3], mat[2][4],
123 mat[3][0], mat[3][1], mat[3][2], mat[3][3], mat[3][4]);
127 static int setMatrix (PyFilter * self, PyObject * value, void * closure)
129 // matrix to store items
131 // check validity of parameter
132 bool valid = value != NULL && PySequence_Check(value)
133 && PySequence_Length(value) == 4;
135 for (int r = 0; valid && r < 4; ++r)
138 PyObject * row = PySequence_Fast_GET_ITEM(value, r);
140 valid = PySequence_Check(row) && PySequence_Length(row) == 5;
142 for (int c = 0; valid && c < 5; ++c)
145 valid = PyInt_Check(PySequence_Fast_GET_ITEM(row, c));
146 // if it is valid, save it in matrix
148 mat[r][c] = short(PyInt_AsLong(PySequence_Fast_GET_ITEM(row, c)));
151 // if parameter is not valid, report error
154 PyErr_SetString(PyExc_TypeError, "The value must be a matrix [4][5] of ints");
158 getFilterColor(self)->setMatrix(mat);
164 // attributes structure
165 static PyGetSetDef filterColorGetSets[] =
167 {"matrix", (getter)getMatrix, (setter)setMatrix, "matrix [4][5] for color calculation", NULL},
168 // attributes from FilterBase class
169 {"previous", (getter)Filter_getPrevious, (setter)Filter_setPrevious, "previous pixel filter", NULL},
173 // define python type
174 PyTypeObject FilterColorType =
176 PyObject_HEAD_INIT(NULL)
178 "VideoTexture.FilterColor", /*tp_name*/
179 sizeof(PyFilter), /*tp_basicsize*/
181 (destructor)Filter_dealloc,/*tp_dealloc*/
188 0, /*tp_as_sequence*/
196 Py_TPFLAGS_DEFAULT, /*tp_flags*/
197 "Filter for color calculations", /* tp_doc */
200 0, /* tp_richcompare */
201 0, /* tp_weaklistoffset */
204 NULL, /* tp_methods */
206 filterColorGetSets, /* tp_getset */
209 0, /* tp_descr_get */
210 0, /* tp_descr_set */
211 0, /* tp_dictoffset */
212 (initproc)Filter_init<FilterColor>, /* tp_init */
214 Filter_allocNew, /* tp_new */
217 // implementation FilterLevel
220 FilterLevel::FilterLevel (void)
222 // reset color levels
223 for (int r = 0; r < 4; ++r)
226 levels[r][1] = 0xFF << (r << 3);
232 void FilterLevel::setLevels (ColorLevel & lev)
235 for (int r = 0; r < 4; ++r)
237 for (int c = 0; c < 2; ++c)
238 levels[r][c] = lev[r][c] << (r << 3);
239 levels[r][2] = lev[r][0] < lev[r][1] ? lev[r][1] - lev[r][0] : 1;
244 // cast Filter pointer to FilterLevel
245 inline FilterLevel * getFilterLevel (PyFilter * self)
246 { return static_cast<FilterLevel*>(self->m_filter); }
249 // python methods and get/sets
252 static PyObject * getLevels (PyFilter * self, void * closure)
254 ColorLevel & lev = getFilterLevel(self)->getLevels();
255 return Py_BuildValue("((kk)(kk)(kk)(kk))",
256 lev[0][0], lev[0][1], lev[1][0] >> 8, lev[1][1] >> 8,
257 lev[2][0] >> 16, lev[2][1] >> 16, lev[3][0] >> 24, lev[3][1] >> 24);
261 static int setLevels (PyFilter * self, PyObject * value, void * closure)
263 // matrix to store items
265 // check validity of parameter
266 bool valid = value != NULL && PySequence_Check(value)
267 && PySequence_Length(value) == 4;
269 for (int r = 0; valid && r < 4; ++r)
272 PyObject * row = PySequence_Fast_GET_ITEM(value, r);
274 valid = PySequence_Check(row) && PySequence_Length(row) == 2;
276 for (int c = 0; valid && c < 2; ++c)
279 valid = PyInt_Check(PySequence_Fast_GET_ITEM(row, c));
280 // if it is valid, save it in matrix
282 lev[r][c] = (unsigned long)(PyInt_AsLong(PySequence_Fast_GET_ITEM(row, c)));
285 // if parameter is not valid, report error
288 PyErr_SetString(PyExc_TypeError, "The value must be a matrix [4][2] of ints");
292 getFilterLevel(self)->setLevels(lev);
298 // attributes structure
299 static PyGetSetDef filterLevelGetSets[] =
301 {"levels", (getter)getLevels, (setter)setLevels, "levels matrix [4] (min, max)", NULL},
302 // attributes from FilterBase class
303 {"previous", (getter)Filter_getPrevious, (setter)Filter_setPrevious, "previous pixel filter", NULL},
307 // define python type
308 PyTypeObject FilterLevelType =
310 PyObject_HEAD_INIT(NULL)
312 "VideoTexture.FilterLevel", /*tp_name*/
313 sizeof(PyFilter), /*tp_basicsize*/
315 (destructor)Filter_dealloc,/*tp_dealloc*/
322 0, /*tp_as_sequence*/
330 Py_TPFLAGS_DEFAULT, /*tp_flags*/
331 "Filter for levels calculations", /* tp_doc */
334 0, /* tp_richcompare */
335 0, /* tp_weaklistoffset */
338 NULL, /* tp_methods */
340 filterLevelGetSets, /* tp_getset */
343 0, /* tp_descr_get */
344 0, /* tp_descr_set */
345 0, /* tp_dictoffset */
346 (initproc)Filter_init<FilterLevel>, /* tp_init */
348 Filter_allocNew, /* tp_new */