1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
|
/* $Id$
-----------------------------------------------------------------------------
This source file is part of VideoTexture library
Copyright (c) 2007 The Zdeno Ash Miklas
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any later
version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
Place - Suite 330, Boston, MA 02111-1307, USA, or go to
http://www.gnu.org/copyleft/lesser.txt.
-----------------------------------------------------------------------------
*/
#include <PyObjectPlus.h>
#include <structmember.h>
#include "FilterColor.h"
#include "FilterBase.h"
#include "PyTypeList.h"
// implementation FilterGray
// attributes structure
static PyGetSetDef filterGrayGetSets[] =
{ // attributes from FilterBase class
{"previous", (getter)Filter_getPrevious, (setter)Filter_setPrevious, "previous pixel filter", NULL},
{NULL}
};
// define python type
PyTypeObject FilterGrayType =
{
PyObject_HEAD_INIT(NULL)
0, /*ob_size*/
"VideoTexture.FilterGray", /*tp_name*/
sizeof(PyFilter), /*tp_basicsize*/
0, /*tp_itemsize*/
(destructor)Filter_dealloc,/*tp_dealloc*/
0, /*tp_print*/
0, /*tp_getattr*/
0, /*tp_setattr*/
0, /*tp_compare*/
0, /*tp_repr*/
0, /*tp_as_number*/
0, /*tp_as_sequence*/
0, /*tp_as_mapping*/
0, /*tp_hash */
0, /*tp_call*/
0, /*tp_str*/
0, /*tp_getattro*/
0, /*tp_setattro*/
0, /*tp_as_buffer*/
Py_TPFLAGS_DEFAULT, /*tp_flags*/
"Filter for gray scale effect", /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
NULL, /* tp_methods */
0, /* tp_members */
filterGrayGetSets, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
(initproc)Filter_init<FilterGray>, /* tp_init */
0, /* tp_alloc */
Filter_allocNew, /* tp_new */
};
// implementation FilterColor
// constructor
FilterColor::FilterColor (void)
{
// reset color matrix to identity
for (int r = 0; r < 4; ++r)
for (int c = 0; c < 5; ++c)
m_matrix[r][c] = (r == c) ? 256 : 0;
}
// set color matrix
void FilterColor::setMatrix (ColorMatrix & mat)
{
// copy matrix
for (int r = 0; r < 4; ++r)
for (int c = 0; c < 5; ++c)
m_matrix[r][c] = mat[r][c];
}
// cast Filter pointer to FilterColor
inline FilterColor * getFilterColor (PyFilter * self)
{ return static_cast<FilterColor*>(self->m_filter); }
// python methods and get/sets
// get color matrix
static PyObject * getMatrix (PyFilter * self, void * closure)
{
ColorMatrix & mat = getFilterColor(self)->getMatrix();
return Py_BuildValue("((hhhhh)(hhhhh)(hhhhh)(hhhhh))",
mat[0][0], mat[0][1], mat[0][2], mat[0][3], mat[0][4],
mat[1][0], mat[1][1], mat[1][2], mat[1][3], mat[1][4],
mat[2][0], mat[2][1], mat[2][2], mat[2][3], mat[2][4],
mat[3][0], mat[3][1], mat[3][2], mat[3][3], mat[3][4]);
}
// set color matrix
static int setMatrix (PyFilter * self, PyObject * value, void * closure)
{
// matrix to store items
ColorMatrix mat;
// check validity of parameter
bool valid = value != NULL && PySequence_Check(value)
&& PySequence_Length(value) == 4;
// check rows
for (int r = 0; valid && r < 4; ++r)
{
// get row object
PyObject * row = PySequence_Fast_GET_ITEM(value, r);
// check sequence
valid = PySequence_Check(row) && PySequence_Length(row) == 5;
// check items
for (int c = 0; valid && c < 5; ++c)
{
// item must be int
valid = PyInt_Check(PySequence_Fast_GET_ITEM(row, c));
// if it is valid, save it in matrix
if (valid)
mat[r][c] = short(PyInt_AsLong(PySequence_Fast_GET_ITEM(row, c)));
}
}
// if parameter is not valid, report error
if (!valid)
{
PyErr_SetString(PyExc_TypeError, "The value must be a matrix [4][5] of ints");
return -1;
}
// set color matrix
getFilterColor(self)->setMatrix(mat);
// success
return 0;
}
// attributes structure
static PyGetSetDef filterColorGetSets[] =
{
{"matrix", (getter)getMatrix, (setter)setMatrix, "matrix [4][5] for color calculation", NULL},
// attributes from FilterBase class
{"previous", (getter)Filter_getPrevious, (setter)Filter_setPrevious, "previous pixel filter", NULL},
{NULL}
};
// define python type
PyTypeObject FilterColorType =
{
PyObject_HEAD_INIT(NULL)
0, /*ob_size*/
"VideoTexture.FilterColor", /*tp_name*/
sizeof(PyFilter), /*tp_basicsize*/
0, /*tp_itemsize*/
(destructor)Filter_dealloc,/*tp_dealloc*/
0, /*tp_print*/
0, /*tp_getattr*/
0, /*tp_setattr*/
0, /*tp_compare*/
0, /*tp_repr*/
0, /*tp_as_number*/
0, /*tp_as_sequence*/
0, /*tp_as_mapping*/
0, /*tp_hash */
0, /*tp_call*/
0, /*tp_str*/
0, /*tp_getattro*/
0, /*tp_setattro*/
0, /*tp_as_buffer*/
Py_TPFLAGS_DEFAULT, /*tp_flags*/
"Filter for color calculations", /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
NULL, /* tp_methods */
0, /* tp_members */
filterColorGetSets, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
(initproc)Filter_init<FilterColor>, /* tp_init */
0, /* tp_alloc */
Filter_allocNew, /* tp_new */
};
// implementation FilterLevel
// constructor
FilterLevel::FilterLevel (void)
{
// reset color levels
for (int r = 0; r < 4; ++r)
{
levels[r][0] = 0;
levels[r][1] = 0xFF << (r << 3);
levels[r][2] = 0xFF;
}
}
// set color levels
void FilterLevel::setLevels (ColorLevel & lev)
{
// copy levels
for (int r = 0; r < 4; ++r)
{
for (int c = 0; c < 2; ++c)
levels[r][c] = lev[r][c] << (r << 3);
levels[r][2] = lev[r][0] < lev[r][1] ? lev[r][1] - lev[r][0] : 1;
}
}
// cast Filter pointer to FilterLevel
inline FilterLevel * getFilterLevel (PyFilter * self)
{ return static_cast<FilterLevel*>(self->m_filter); }
// python methods and get/sets
// get color levels
static PyObject * getLevels (PyFilter * self, void * closure)
{
ColorLevel & lev = getFilterLevel(self)->getLevels();
return Py_BuildValue("((kk)(kk)(kk)(kk))",
lev[0][0], lev[0][1], lev[1][0] >> 8, lev[1][1] >> 8,
lev[2][0] >> 16, lev[2][1] >> 16, lev[3][0] >> 24, lev[3][1] >> 24);
}
// set color levels
static int setLevels (PyFilter * self, PyObject * value, void * closure)
{
// matrix to store items
ColorLevel lev;
// check validity of parameter
bool valid = value != NULL && PySequence_Check(value)
&& PySequence_Length(value) == 4;
// check rows
for (int r = 0; valid && r < 4; ++r)
{
// get row object
PyObject * row = PySequence_Fast_GET_ITEM(value, r);
// check sequence
valid = PySequence_Check(row) && PySequence_Length(row) == 2;
// check items
for (int c = 0; valid && c < 2; ++c)
{
// item must be int
valid = PyInt_Check(PySequence_Fast_GET_ITEM(row, c));
// if it is valid, save it in matrix
if (valid)
lev[r][c] = (unsigned long)(PyInt_AsLong(PySequence_Fast_GET_ITEM(row, c)));
}
}
// if parameter is not valid, report error
if (!valid)
{
PyErr_SetString(PyExc_TypeError, "The value must be a matrix [4][2] of ints");
return -1;
}
// set color matrix
getFilterLevel(self)->setLevels(lev);
// success
return 0;
}
// attributes structure
static PyGetSetDef filterLevelGetSets[] =
{
{"levels", (getter)getLevels, (setter)setLevels, "levels matrix [4] (min, max)", NULL},
// attributes from FilterBase class
{"previous", (getter)Filter_getPrevious, (setter)Filter_setPrevious, "previous pixel filter", NULL},
{NULL}
};
// define python type
PyTypeObject FilterLevelType =
{
PyObject_HEAD_INIT(NULL)
0, /*ob_size*/
"VideoTexture.FilterLevel", /*tp_name*/
sizeof(PyFilter), /*tp_basicsize*/
0, /*tp_itemsize*/
(destructor)Filter_dealloc,/*tp_dealloc*/
0, /*tp_print*/
0, /*tp_getattr*/
0, /*tp_setattr*/
0, /*tp_compare*/
0, /*tp_repr*/
0, /*tp_as_number*/
0, /*tp_as_sequence*/
0, /*tp_as_mapping*/
0, /*tp_hash */
0, /*tp_call*/
0, /*tp_str*/
0, /*tp_getattro*/
0, /*tp_setattro*/
0, /*tp_as_buffer*/
Py_TPFLAGS_DEFAULT, /*tp_flags*/
"Filter for levels calculations", /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
NULL, /* tp_methods */
0, /* tp_members */
filterLevelGetSets, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
(initproc)Filter_init<FilterLevel>, /* tp_init */
0, /* tp_alloc */
Filter_allocNew, /* tp_new */
};
|