Blender  V2.59
FilterColor.cpp
Go to the documentation of this file.
00001 
00004 /* $Id: FilterColor.cpp 35176 2011-02-25 13:39:34Z jesterking $
00005 -----------------------------------------------------------------------------
00006 This source file is part of VideoTexture library
00007 
00008 Copyright (c) 2007 The Zdeno Ash Miklas
00009 
00010 This program is free software; you can redistribute it and/or modify it under
00011 the terms of the GNU Lesser General Public License as published by the Free Software
00012 Foundation; either version 2 of the License, or (at your option) any later
00013 version.
00014 
00015 This program is distributed in the hope that it will be useful, but WITHOUT
00016 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00017 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
00018 
00019 You should have received a copy of the GNU Lesser General Public License along with
00020 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
00021 Place - Suite 330, Boston, MA 02111-1307, USA, or go to
00022 http://www.gnu.org/copyleft/lesser.txt.
00023 -----------------------------------------------------------------------------
00024 */
00025 
00026 
00027 #include <PyObjectPlus.h>
00028 #include <structmember.h>
00029 
00030 #include "FilterColor.h"
00031 
00032 #include "FilterBase.h"
00033 #include "PyTypeList.h"
00034 
00035 // implementation FilterGray
00036 
00037 // attributes structure
00038 static PyGetSetDef filterGrayGetSets[] =
00039 { // attributes from FilterBase class
00040         {(char*)"previous", (getter)Filter_getPrevious, (setter)Filter_setPrevious, (char*)"previous pixel filter", NULL},
00041         {NULL}
00042 };
00043 
00044 // define python type
00045 PyTypeObject FilterGrayType =
00046 { 
00047         PyVarObject_HEAD_INIT(NULL, 0)
00048         "VideoTexture.FilterGray",   /*tp_name*/
00049         sizeof(PyFilter),          /*tp_basicsize*/
00050         0,                         /*tp_itemsize*/
00051         (destructor)Filter_dealloc,/*tp_dealloc*/
00052         0,                         /*tp_print*/
00053         0,                         /*tp_getattr*/
00054         0,                         /*tp_setattr*/
00055         0,                         /*tp_compare*/
00056         0,                         /*tp_repr*/
00057         0,                         /*tp_as_number*/
00058         0,                         /*tp_as_sequence*/
00059         0,                         /*tp_as_mapping*/
00060         0,                         /*tp_hash */
00061         0,                         /*tp_call*/
00062         0,                         /*tp_str*/
00063         0,                         /*tp_getattro*/
00064         0,                         /*tp_setattro*/
00065         0,                         /*tp_as_buffer*/
00066         Py_TPFLAGS_DEFAULT,        /*tp_flags*/
00067         "Filter for gray scale effect",       /* tp_doc */
00068         0,                             /* tp_traverse */
00069         0,                             /* tp_clear */
00070         0,                             /* tp_richcompare */
00071         0,                             /* tp_weaklistoffset */
00072         0,                             /* tp_iter */
00073         0,                             /* tp_iternext */
00074         NULL,                /* tp_methods */
00075         0,                   /* tp_members */
00076         filterGrayGetSets,           /* tp_getset */
00077         0,                         /* tp_base */
00078         0,                         /* tp_dict */
00079         0,                         /* tp_descr_get */
00080         0,                         /* tp_descr_set */
00081         0,                         /* tp_dictoffset */
00082         (initproc)Filter_init<FilterGray>,     /* tp_init */
00083         0,                         /* tp_alloc */
00084         Filter_allocNew,           /* tp_new */
00085 };
00086 
00087 
00088 // implementation FilterColor
00089 
00090 // constructor
00091 FilterColor::FilterColor (void)
00092 {
00093         // reset color matrix to identity
00094         for (int r = 0; r < 4; ++r)
00095                 for (int c = 0; c < 5; ++c)
00096                         m_matrix[r][c] = (r == c) ? 256 : 0; 
00097 }
00098 
00099 // set color matrix
00100 void FilterColor::setMatrix (ColorMatrix & mat)
00101 {
00102         // copy matrix
00103         for (int r = 0; r < 4; ++r)
00104                 for (int c = 0; c < 5; ++c)
00105                         m_matrix[r][c] = mat[r][c]; 
00106 }
00107 
00108 
00109 
00110 // cast Filter pointer to FilterColor
00111 inline FilterColor * getFilterColor (PyFilter * self)
00112 { return static_cast<FilterColor*>(self->m_filter); }
00113 
00114 
00115 // python methods and get/sets
00116 
00117 // get color matrix
00118 static PyObject * getMatrix (PyFilter * self, void * closure)
00119 {
00120         ColorMatrix & mat = getFilterColor(self)->getMatrix();
00121         return Py_BuildValue("((hhhhh)(hhhhh)(hhhhh)(hhhhh))",
00122                 mat[0][0], mat[0][1], mat[0][2], mat[0][3], mat[0][4],
00123                 mat[1][0], mat[1][1], mat[1][2], mat[1][3], mat[1][4],
00124                 mat[2][0], mat[2][1], mat[2][2], mat[2][3], mat[2][4],
00125                 mat[3][0], mat[3][1], mat[3][2], mat[3][3], mat[3][4]);
00126 }
00127 
00128 // set color matrix
00129 static int setMatrix (PyFilter * self, PyObject * value, void * closure)
00130 {
00131         // matrix to store items
00132         ColorMatrix mat;
00133         // check validity of parameter
00134         bool valid = value != NULL && PySequence_Check(value)
00135                 && PySequence_Size(value) == 4;
00136         // check rows
00137         for (int r = 0; valid && r < 4; ++r)
00138         {
00139                 // get row object
00140                 PyObject * row = PySequence_Fast_GET_ITEM(value, r);
00141                 // check sequence
00142                 valid = PySequence_Check(row) && PySequence_Size(row) == 5;
00143                 // check items
00144                 for (int c = 0; valid && c < 5; ++c)
00145                 {
00146                         // item must be int
00147                         valid = PyLong_Check(PySequence_Fast_GET_ITEM(row, c));
00148                         // if it is valid, save it in matrix
00149                         if (valid)
00150                                 mat[r][c] = short(PyLong_AsSsize_t(PySequence_Fast_GET_ITEM(row, c)));
00151                 }
00152         }
00153         // if parameter is not valid, report error
00154         if (!valid)
00155         {
00156                 PyErr_SetString(PyExc_TypeError, "The value must be a matrix [4][5] of ints");
00157                 return -1;
00158         }
00159         // set color matrix
00160         getFilterColor(self)->setMatrix(mat);
00161         // success
00162         return 0;
00163 }
00164 
00165 
00166 // attributes structure
00167 static PyGetSetDef filterColorGetSets[] =
00168 { 
00169         {(char*)"matrix", (getter)getMatrix, (setter)setMatrix, (char*)"matrix [4][5] for color calculation", NULL},
00170         // attributes from FilterBase class
00171         {(char*)"previous", (getter)Filter_getPrevious, (setter)Filter_setPrevious, (char*)"previous pixel filter", NULL},
00172         {NULL}
00173 };
00174 
00175 // define python type
00176 PyTypeObject FilterColorType =
00177 { 
00178         PyVarObject_HEAD_INIT(NULL, 0)
00179         "VideoTexture.FilterColor",   /*tp_name*/
00180         sizeof(PyFilter),          /*tp_basicsize*/
00181         0,                         /*tp_itemsize*/
00182         (destructor)Filter_dealloc,/*tp_dealloc*/
00183         0,                         /*tp_print*/
00184         0,                         /*tp_getattr*/
00185         0,                         /*tp_setattr*/
00186         0,                         /*tp_compare*/
00187         0,                         /*tp_repr*/
00188         0,                         /*tp_as_number*/
00189         0,                         /*tp_as_sequence*/
00190         0,                         /*tp_as_mapping*/
00191         0,                         /*tp_hash */
00192         0,                         /*tp_call*/
00193         0,                         /*tp_str*/
00194         0,                         /*tp_getattro*/
00195         0,                         /*tp_setattro*/
00196         0,                         /*tp_as_buffer*/
00197         Py_TPFLAGS_DEFAULT,        /*tp_flags*/
00198         "Filter for color calculations",       /* tp_doc */
00199         0,                             /* tp_traverse */
00200         0,                             /* tp_clear */
00201         0,                             /* tp_richcompare */
00202         0,                             /* tp_weaklistoffset */
00203         0,                             /* tp_iter */
00204         0,                             /* tp_iternext */
00205         NULL,                /* tp_methods */
00206         0,                   /* tp_members */
00207         filterColorGetSets,           /* tp_getset */
00208         0,                         /* tp_base */
00209         0,                         /* tp_dict */
00210         0,                         /* tp_descr_get */
00211         0,                         /* tp_descr_set */
00212         0,                         /* tp_dictoffset */
00213         (initproc)Filter_init<FilterColor>,     /* tp_init */
00214         0,                         /* tp_alloc */
00215         Filter_allocNew,           /* tp_new */
00216 };
00217 
00218 // implementation FilterLevel
00219 
00220 // constructor
00221 FilterLevel::FilterLevel (void)
00222 {
00223         // reset color levels
00224         for (int r = 0; r < 4; ++r)
00225         {
00226                 levels[r][0] = 0;
00227                 levels[r][1] = 0xFF;
00228                 levels[r][2] = 0xFF;
00229         }
00230 }
00231 
00232 // set color levels
00233 void FilterLevel::setLevels (ColorLevel & lev)
00234 {
00235         // copy levels
00236         for (int r = 0; r < 4; ++r)
00237         {
00238                 for (int c = 0; c < 2; ++c)
00239                         levels[r][c] = lev[r][c];
00240                 levels[r][2] = lev[r][0] < lev[r][1] ? lev[r][1] - lev[r][0] : 1;
00241         }
00242 }
00243 
00244 
00245 // cast Filter pointer to FilterLevel
00246 inline FilterLevel * getFilterLevel (PyFilter * self)
00247 { return static_cast<FilterLevel*>(self->m_filter); }
00248 
00249 
00250 // python methods and get/sets
00251 
00252 // get color levels
00253 static PyObject * getLevels (PyFilter * self, void * closure)
00254 {
00255         ColorLevel & lev = getFilterLevel(self)->getLevels();
00256         return Py_BuildValue("((HH)(HH)(HH)(HH))",
00257                 lev[0][0], lev[0][1], lev[1][0], lev[1][1],
00258                 lev[2][0], lev[2][1], lev[3][0], lev[3][1]);
00259 }
00260 
00261 // set color levels
00262 static int setLevels (PyFilter * self, PyObject * value, void * closure)
00263 {
00264         // matrix to store items
00265         ColorLevel lev;
00266         // check validity of parameter
00267         bool valid = value != NULL && PySequence_Check(value)
00268                 && PySequence_Size(value) == 4;
00269         // check rows
00270         for (int r = 0; valid && r < 4; ++r)
00271         {
00272                 // get row object
00273                 PyObject * row = PySequence_Fast_GET_ITEM(value, r);
00274                 // check sequence
00275                 valid = PySequence_Check(row) && PySequence_Size(row) == 2;
00276                 // check items
00277                 for (int c = 0; valid && c < 2; ++c)
00278                 {
00279                         // item must be int
00280                         valid = PyLong_Check(PySequence_Fast_GET_ITEM(row, c));
00281                         // if it is valid, save it in matrix
00282                         if (valid)
00283                                 lev[r][c] = (unsigned short)(PyLong_AsSsize_t(PySequence_Fast_GET_ITEM(row, c)));
00284                 }
00285         }
00286         // if parameter is not valid, report error
00287         if (!valid)
00288         {
00289                 PyErr_SetString(PyExc_TypeError, "The value must be a matrix [4][2] of ints");
00290                 return -1;
00291         }
00292         // set color matrix
00293         getFilterLevel(self)->setLevels(lev);
00294         // success
00295         return 0;
00296 }
00297 
00298 
00299 // attributes structure
00300 static PyGetSetDef filterLevelGetSets[] =
00301 { 
00302         {(char*)"levels", (getter)getLevels, (setter)setLevels, (char*)"levels matrix [4] (min, max)", NULL},
00303         // attributes from FilterBase class
00304         {(char*)"previous", (getter)Filter_getPrevious, (setter)Filter_setPrevious, (char*)"previous pixel filter", NULL},
00305         {NULL}
00306 };
00307 
00308 // define python type
00309 PyTypeObject FilterLevelType =
00310 { 
00311         PyVarObject_HEAD_INIT(NULL, 0)
00312         "VideoTexture.FilterLevel",   /*tp_name*/
00313         sizeof(PyFilter),          /*tp_basicsize*/
00314         0,                         /*tp_itemsize*/
00315         (destructor)Filter_dealloc,/*tp_dealloc*/
00316         0,                         /*tp_print*/
00317         0,                         /*tp_getattr*/
00318         0,                         /*tp_setattr*/
00319         0,                         /*tp_compare*/
00320         0,                         /*tp_repr*/
00321         0,                         /*tp_as_number*/
00322         0,                         /*tp_as_sequence*/
00323         0,                         /*tp_as_mapping*/
00324         0,                         /*tp_hash */
00325         0,                         /*tp_call*/
00326         0,                         /*tp_str*/
00327         0,                         /*tp_getattro*/
00328         0,                         /*tp_setattro*/
00329         0,                         /*tp_as_buffer*/
00330         Py_TPFLAGS_DEFAULT,        /*tp_flags*/
00331         "Filter for levels calculations",       /* tp_doc */
00332         0,                             /* tp_traverse */
00333         0,                             /* tp_clear */
00334         0,                             /* tp_richcompare */
00335         0,                             /* tp_weaklistoffset */
00336         0,                             /* tp_iter */
00337         0,                             /* tp_iternext */
00338         NULL,                /* tp_methods */
00339         0,                   /* tp_members */
00340         filterLevelGetSets,           /* tp_getset */
00341         0,                         /* tp_base */
00342         0,                         /* tp_dict */
00343         0,                         /* tp_descr_get */
00344         0,                         /* tp_descr_set */
00345         0,                         /* tp_dictoffset */
00346         (initproc)Filter_init<FilterLevel>,     /* tp_init */
00347         0,                         /* tp_alloc */
00348         Filter_allocNew,           /* tp_new */
00349 };
00350