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