py api cleanup, replace use...
[blender.git] / source / gameengine / VideoTexture / VideoBase.cpp
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software  Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * Copyright (c) 2007 The Zdeno Ash Miklas
19  *
20  * This source file is part of VideoTexture library
21  *
22  * Contributor(s):
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file gameengine/VideoTexture/VideoBase.cpp
28  *  \ingroup bgevideotex
29  */
30
31 #if defined WIN32
32 #define WINDOWS_LEAN_AND_MEAN
33 #include <windows.h>
34 #endif
35
36 #include "VideoBase.h"
37
38 #include "FilterSource.h"
39
40 // VideoBase implementation
41
42
43 // initialize image data
44 void VideoBase::init(short width, short height)
45 {
46         // save original sizes
47         m_orgSize[0] = width;
48         m_orgSize[1] = height;
49         // call base class initialization
50         ImageBase::init(width, height);
51 }
52
53
54 // process video frame
55 void VideoBase::process (BYTE * sample)
56 {
57         // if scale was changed
58         if (m_scaleChange)
59                 // reset image
60                 init(m_orgSize[0], m_orgSize[1]);
61         // if image is allocated and is able to store new image
62         if (m_image != NULL && !m_avail)
63         {
64                 // filters used
65                 // convert video format to image
66                 switch (m_format)
67                 {
68                 case RGBA32:
69                         {
70                                 FilterRGBA32 filtRGBA;
71                                 // use filter object for format to convert image
72                                 filterImage(filtRGBA, sample, m_orgSize);
73                                 // finish
74                                 break;
75                         }
76                 case RGB24:
77                         {
78                                 FilterRGB24 filtRGB;
79                                 // use filter object for format to convert image
80                                 filterImage(filtRGB, sample, m_orgSize);
81                                 // finish
82                                 break;
83                         }
84                 case YV12:
85                         {
86                                 // use filter object for format to convert image
87                                 FilterYV12 filtYUV;
88                                 filtYUV.setBuffs(sample, m_orgSize);
89                                 filterImage(filtYUV, sample, m_orgSize);
90                                 // finish
91                                 break;
92                         }
93                 case None:
94                         break; /* assert? */
95                 }
96         }
97 }
98
99
100 // python functions
101
102
103 // exceptions for video source initialization
104 ExceptionID SourceVideoEmpty, SourceVideoCreation;
105 ExpDesc SourceVideoEmptyDesc (SourceVideoEmpty, "Source Video is empty");
106 ExpDesc SourceVideoCreationDesc (SourceVideoCreation, "SourceVideo object was not created");
107
108 // open video source
109 void Video_open (VideoBase * self, char * file, short captureID)
110 {
111         // if file is empty, throw exception
112         if (file == NULL) THRWEXCP(SourceVideoEmpty, S_OK);
113
114         // open video file or capture device
115         if (captureID >= 0) 
116                 self->openCam(file, captureID);
117         else 
118                 self->openFile(file);
119 }
120
121
122 // play video
123 PyObject *Video_play(PyImage *self)
124 { if (getVideo(self)->play()) Py_RETURN_TRUE; else Py_RETURN_FALSE; }
125
126 // pause video
127 PyObject *Video_pause(PyImage *self)
128 { if (getVideo(self)->pause()) Py_RETURN_TRUE; else Py_RETURN_FALSE; }
129
130 PyObject *Video_stop(PyImage *self)
131 { if (getVideo(self)->stop()) Py_RETURN_TRUE; else Py_RETURN_FALSE; }
132
133 // get status
134 PyObject *Video_getStatus(PyImage *self, void *closure)
135 {
136         return Py_BuildValue("h", getVideo(self)->getStatus());
137 }
138
139 // refresh video
140 PyObject *Video_refresh(PyImage *self)
141 {
142         getVideo(self)->refresh();
143         return Video_getStatus(self, NULL);
144 }
145
146
147 // get range
148 PyObject *Video_getRange(PyImage *self, void *closure)
149 {
150         return Py_BuildValue("[ff]", getVideo(self)->getRange()[0],
151                 getVideo(self)->getRange()[1]);
152 }
153
154 // set range
155 int Video_setRange(PyImage *self, PyObject *value, void *closure)
156 {
157         // check validity of parameter
158         if (value == NULL || !PySequence_Check(value) || PySequence_Size(value) != 2 ||
159             /* XXX - this is incorrect if the sequence is not a list/tuple! */
160             !PyFloat_Check(PySequence_Fast_GET_ITEM(value, 0)) ||
161             !PyFloat_Check(PySequence_Fast_GET_ITEM(value, 1)))
162         {
163                 PyErr_SetString(PyExc_TypeError, "The value must be a sequence of 2 float");
164                 return -1;
165         }
166         // set range
167         getVideo(self)->setRange(PyFloat_AsDouble(PySequence_Fast_GET_ITEM(value, 0)),
168                 PyFloat_AsDouble(PySequence_Fast_GET_ITEM(value, 1)));
169         // success
170         return 0;
171 }
172
173 // get repeat
174 PyObject *Video_getRepeat (PyImage *self, void *closure)
175 { return Py_BuildValue("h", getVideo(self)->getRepeat()); }
176
177 // set repeat
178 int Video_setRepeat(PyImage *self, PyObject *value, void *closure)
179 {
180         // check validity of parameter
181         if (value == NULL || !PyLong_Check(value))
182         {
183                 PyErr_SetString(PyExc_TypeError, "The value must be an int");
184                 return -1;
185         }
186         // set repeat
187         getVideo(self)->setRepeat(int(PyLong_AsLong(value)));
188         // success
189         return 0;
190 }
191
192 // get frame rate
193 PyObject *Video_getFrameRate (PyImage *self, void *closure)
194 { return Py_BuildValue("f", double(getVideo(self)->getFrameRate())); }
195
196 // set frame rate
197 int Video_setFrameRate(PyImage *self, PyObject *value, void *closure)
198 {
199         // check validity of parameter
200         if (value == NULL || !PyFloat_Check(value))
201         {
202                 PyErr_SetString(PyExc_TypeError, "The value must be a float");
203                 return -1;
204         }
205         // set repeat
206         getVideo(self)->setFrameRate(float(PyFloat_AsDouble(value)));
207         // success
208         return 0;
209 }