a0c6e1f6f744561886cc9c77cf0439bf24fa0d9e
[blender.git] / source / gameengine / VideoTexture / ImageBuff.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 // implementation
24
25 #include <PyObjectPlus.h>
26 #include <structmember.h>
27
28 #include "ImageBuff.h"
29
30 #include "ImageBase.h"
31 #include "FilterSource.h"
32
33
34 // default filter
35 FilterBGR24 defFilter;
36
37
38 // load image from buffer
39 void ImageBuff::load (unsigned char * img, short width, short height)
40 {
41         // initialize image buffer
42         init(width, height);
43         // original size
44         short orgSize[2] = {width, height};
45         // is filter available
46         if (m_pyfilter != NULL)
47                 // use it to process image
48                 convImage(*(m_pyfilter->m_filter), img, orgSize);
49         else
50                 // otherwise use default filter
51                 convImage(defFilter, img, orgSize);
52         // image is available
53         m_avail = true;
54 }
55
56
57
58 // cast Image pointer to ImageBuff
59 inline ImageBuff * getImageBuff (PyImage * self)
60 { return static_cast<ImageBuff*>(self->m_image); }
61
62
63 // python methods
64
65 // load image
66 static PyObject * load (PyImage * self, PyObject * args)
67 {
68         // parameters: string image buffer, its size, width, height
69         unsigned char * buff;
70         unsigned int buffSize;
71         short width;
72         short height;
73         // parse parameters
74         if (!PyArg_ParseTuple(args, "s#hh", &buff, &buffSize, &width, &height))
75         {
76                 // report error
77                 PyErr_SetString(PyExc_TypeError, "Parameters are not correct");
78                 return NULL;
79         }
80         // else check buffer size
81         else
82         {
83                 // calc proper buffer size
84                 unsigned int propSize = width * height;
85                 // use pixel size from filter
86                 if (self->m_image->getFilter() != NULL)
87                         propSize *= self->m_image->getFilter()->m_filter->firstPixelSize();
88                 else
89                         propSize *= defFilter.firstPixelSize();
90                 // check if buffer size is correct
91                 if (propSize != buffSize)
92                 {
93                         // if not, report error
94                         PyErr_SetString(PyExc_TypeError, "Buffer hasn't correct size");
95                         return NULL;
96                 }
97                 else
98                         // if correct, load image
99                         getImageBuff(self)->load(buff, width, height);
100         }
101         Py_RETURN_NONE; 
102 }
103
104
105 // methods structure
106 static PyMethodDef imageBuffMethods[] =
107
108         {"load", (PyCFunction)load, METH_VARARGS, "Load image from buffer"},
109         {NULL}
110 };
111 // attributes structure
112 static PyGetSetDef imageBuffGetSets[] =
113 {       // attributes from ImageBase class
114         {"image", (getter)Image_getImage, NULL, "image data", NULL},
115         {"size", (getter)Image_getSize, NULL, "image size", NULL},
116         {"scale", (getter)Image_getScale, (setter)Image_setScale, "fast scale of image (near neighbour)", NULL},
117         {"flip", (getter)Image_getFlip, (setter)Image_setFlip, "flip image vertically", NULL},
118         {"filter", (getter)Image_getFilter, (setter)Image_setFilter, "pixel filter", NULL},
119         {NULL}
120 };
121
122
123 // define python type
124 PyTypeObject ImageBuffType =
125
126         PyObject_HEAD_INIT(NULL)
127         0,                         /*ob_size*/
128         "VideoTexture.ImageBuff",   /*tp_name*/
129         sizeof(PyImage),          /*tp_basicsize*/
130         0,                         /*tp_itemsize*/
131         (destructor)Image_dealloc, /*tp_dealloc*/
132         0,                         /*tp_print*/
133         0,                         /*tp_getattr*/
134         0,                         /*tp_setattr*/
135         0,                         /*tp_compare*/
136         0,                         /*tp_repr*/
137         0,                         /*tp_as_number*/
138         0,                         /*tp_as_sequence*/
139         0,                         /*tp_as_mapping*/
140         0,                         /*tp_hash */
141         0,                         /*tp_call*/
142         0,                         /*tp_str*/
143         0,                         /*tp_getattro*/
144         0,                         /*tp_setattro*/
145         0,                         /*tp_as_buffer*/
146         Py_TPFLAGS_DEFAULT,        /*tp_flags*/
147         "Image source from image buffer",       /* tp_doc */
148         0,                             /* tp_traverse */
149         0,                             /* tp_clear */
150         0,                             /* tp_richcompare */
151         0,                             /* tp_weaklistoffset */
152         0,                             /* tp_iter */
153         0,                             /* tp_iternext */
154         imageBuffMethods,    /* tp_methods */
155         0,                   /* tp_members */
156         imageBuffGetSets,          /* tp_getset */
157         0,                         /* tp_base */
158         0,                         /* tp_dict */
159         0,                         /* tp_descr_get */
160         0,                         /* tp_descr_set */
161         0,                         /* tp_dictoffset */
162         (initproc)Image_init<ImageBuff>,     /* tp_init */
163         0,                         /* tp_alloc */
164         Image_allocNew,           /* tp_new */
165 };
166