1eb2e830d37410575ebfeaeafc71ee135bb028b1
[blender.git] / source / gameengine / VideoTexture / VideoBase.cpp
1 /*
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 /** \file gameengine/VideoTexture/VideoBase.cpp
24  *  \ingroup bgevideotex
25  */
26
27 #if defined WIN32
28 #define WINDOWS_LEAN_AND_MEAN
29 #include <windows.h>
30 #endif
31
32 #include "VideoBase.h"
33
34 #include "FilterSource.h"
35
36 // VideoBase implementation
37
38
39 // initialize image data
40 void VideoBase::init(short width, short height)
41 {
42         // save original sizes
43         m_orgSize[0] = width;
44         m_orgSize[1] = height;
45         // call base class initialization
46         ImageBase::init(width, height);
47 }
48
49
50 // process video frame
51 void VideoBase::process (BYTE * sample)
52 {
53         // if scale was changed
54         if (m_scaleChange)
55                 // reset image
56                 init(m_orgSize[0], m_orgSize[1]);
57         // if image is allocated and is able to store new image
58         if (m_image != NULL && !m_avail)
59         {
60                 // filters used
61                 // convert video format to image
62                 switch (m_format)
63                 {
64                 case RGBA32:
65                         {
66                                 FilterRGBA32 filtRGBA;
67                                 // use filter object for format to convert image
68                                 filterImage(filtRGBA, sample, m_orgSize);
69                                 // finish
70                                 break;
71                         }
72                 case RGB24:
73                         {
74                                 FilterRGB24 filtRGB;
75                                 // use filter object for format to convert image
76                                 filterImage(filtRGB, sample, m_orgSize);
77                                 // finish
78                                 break;
79                         }
80                 case YV12:
81                         {
82                                 // use filter object for format to convert image
83                                 FilterYV12 filtYUV;
84                                 filtYUV.setBuffs(sample, m_orgSize);
85                                 filterImage(filtYUV, sample, m_orgSize);
86                                 // finish
87                                 break;
88                         }
89                 case None:
90                         break; /* assert? */
91                 }
92         }
93 }
94
95
96 // python functions
97
98
99 // exceptions for video source initialization
100 ExceptionID SourceVideoEmpty, SourceVideoCreation;
101 ExpDesc SourceVideoEmptyDesc (SourceVideoEmpty, "Source Video is empty");
102 ExpDesc SourceVideoCreationDesc (SourceVideoCreation, "SourceVideo object was not created");
103
104 // open video source
105 void Video_open (VideoBase * self, char * file, short captureID)
106 {
107         // if file is empty, throw exception
108         if (file == NULL) THRWEXCP(SourceVideoEmpty, S_OK);
109
110         // open video file or capture device
111         if (captureID >= 0) 
112                 self->openCam(file, captureID);
113         else 
114                 self->openFile(file);
115 }
116
117
118 // play video
119 PyObject *Video_play(PyImage *self)
120 { if (getVideo(self)->play()) Py_RETURN_TRUE; else Py_RETURN_FALSE; }
121
122 // pause video
123 PyObject *Video_pause(PyImage *self)
124 { if (getVideo(self)->pause()) Py_RETURN_TRUE; else Py_RETURN_FALSE; }
125
126 PyObject *Video_stop(PyImage *self)
127 { if (getVideo(self)->stop()) Py_RETURN_TRUE; else Py_RETURN_FALSE; }
128
129 // get status
130 PyObject *Video_getStatus(PyImage *self, void *closure)
131 {
132         return Py_BuildValue("h", getVideo(self)->getStatus());
133 }
134
135 // refresh video
136 PyObject *Video_refresh(PyImage *self)
137 {
138         getVideo(self)->refresh();
139         return Video_getStatus(self, NULL);
140 }
141
142
143 // get range
144 PyObject *Video_getRange(PyImage *self, void *closure)
145 {
146         return Py_BuildValue("[ff]", getVideo(self)->getRange()[0],
147                 getVideo(self)->getRange()[1]);
148 }
149
150 // set range
151 int Video_setRange(PyImage *self, PyObject *value, void *closure)
152 {
153         // check validity of parameter
154         if (value == NULL || !PySequence_Check(value) || PySequence_Size(value) != 2 ||
155             /* XXX - this is incorrect if the sequence is not a list/tuple! */
156             !PyFloat_Check(PySequence_Fast_GET_ITEM(value, 0)) ||
157             !PyFloat_Check(PySequence_Fast_GET_ITEM(value, 1)))
158         {
159                 PyErr_SetString(PyExc_TypeError, "The value must be a sequence of 2 float");
160                 return -1;
161         }
162         // set range
163         getVideo(self)->setRange(PyFloat_AsDouble(PySequence_Fast_GET_ITEM(value, 0)),
164                 PyFloat_AsDouble(PySequence_Fast_GET_ITEM(value, 1)));
165         // success
166         return 0;
167 }
168
169 // get repeat
170 PyObject *Video_getRepeat (PyImage *self, void *closure)
171 { return Py_BuildValue("h", getVideo(self)->getRepeat()); }
172
173 // set repeat
174 int Video_setRepeat(PyImage *self, PyObject *value, void *closure)
175 {
176         // check validity of parameter
177         if (value == NULL || !PyLong_Check(value))
178         {
179                 PyErr_SetString(PyExc_TypeError, "The value must be an int");
180                 return -1;
181         }
182         // set repeat
183         getVideo(self)->setRepeat(int(PyLong_AsSsize_t(value)));
184         // success
185         return 0;
186 }
187
188 // get frame rate
189 PyObject *Video_getFrameRate (PyImage *self, void *closure)
190 { return Py_BuildValue("f", double(getVideo(self)->getFrameRate())); }
191
192 // set frame rate
193 int Video_setFrameRate(PyImage *self, PyObject *value, void *closure)
194 {
195         // check validity of parameter
196         if (value == NULL || !PyFloat_Check(value))
197         {
198                 PyErr_SetString(PyExc_TypeError, "The value must be a float");
199                 return -1;
200         }
201         // set repeat
202         getVideo(self)->setFrameRate(float(PyFloat_AsDouble(value)));
203         // success
204         return 0;
205 }