Blender  V2.59
VideoBase.cpp
Go to the documentation of this file.
00001 
00004 /* $Id: VideoBase.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 #if defined WIN32
00027 #define WINDOWS_LEAN_AND_MEAN
00028 #include <windows.h>
00029 #endif
00030 
00031 #include "VideoBase.h"
00032 
00033 #include "FilterSource.h"
00034 
00035 // VideoBase implementation
00036 
00037 
00038 // initialize image data
00039 void VideoBase::init(short width, short height)
00040 {
00041         // save original sizes
00042         m_orgSize[0] = width;
00043         m_orgSize[1] = height;
00044         // call base class initialization
00045         ImageBase::init(width, height);
00046 }
00047 
00048 
00049 // process video frame
00050 void VideoBase::process (BYTE * sample)
00051 {
00052         // if scale was changed
00053         if (m_scaleChange)
00054                 // reset image
00055                 init(m_orgSize[0], m_orgSize[1]);
00056         // if image is allocated and is able to store new image
00057         if (m_image != NULL && !m_avail)
00058         {
00059                 // filters used
00060                 // convert video format to image
00061                 switch (m_format)
00062                 {
00063                 case RGBA32:
00064                         {
00065                                 FilterRGBA32 filtRGBA;
00066                                 // use filter object for format to convert image
00067                                 filterImage(filtRGBA, sample, m_orgSize);
00068                                 // finish
00069                                 break;
00070                         }
00071                 case RGB24:
00072                         {
00073                                 FilterRGB24 filtRGB;
00074                                 // use filter object for format to convert image
00075                                 filterImage(filtRGB, sample, m_orgSize);
00076                                 // finish
00077                                 break;
00078                         }
00079                 case YV12:
00080                         {
00081                                 // use filter object for format to convert image
00082                                 FilterYV12 filtYUV;
00083                                 filtYUV.setBuffs(sample, m_orgSize);
00084                                 filterImage(filtYUV, sample, m_orgSize);
00085                                 // finish
00086                                 break;
00087                         }
00088                 case None:
00089                         break; /* assert? */
00090                 }
00091         }
00092 }
00093 
00094 
00095 // python functions
00096 
00097 
00098 // exceptions for video source initialization
00099 ExceptionID SourceVideoEmpty, SourceVideoCreation;
00100 ExpDesc SourceVideoEmptyDesc (SourceVideoEmpty, "Source Video is empty");
00101 ExpDesc SourceVideoCreationDesc (SourceVideoCreation, "SourceVideo object was not created");
00102 
00103 // open video source
00104 void Video_open (VideoBase * self, char * file, short captureID)
00105 {
00106         // if file is empty, throw exception
00107         if (file == NULL) THRWEXCP(SourceVideoEmpty, S_OK);
00108 
00109         // open video file or capture device
00110         if (captureID >= 0) 
00111                 self->openCam(file, captureID);
00112         else 
00113                 self->openFile(file);
00114 }
00115 
00116 
00117 // play video
00118 PyObject * Video_play (PyImage * self)
00119 { if (getVideo(self)->play()) Py_RETURN_TRUE; else Py_RETURN_FALSE; }
00120 
00121 // pause video
00122 PyObject * Video_pause (PyImage * self)
00123 { if (getVideo(self)->pause()) Py_RETURN_TRUE; else Py_RETURN_FALSE; }
00124 
00125 PyObject * Video_stop (PyImage * self)
00126 { if (getVideo(self)->stop()) Py_RETURN_TRUE; else Py_RETURN_FALSE; }
00127 
00128 // get status
00129 PyObject * Video_getStatus (PyImage * self, void * closure)
00130 {
00131         return Py_BuildValue("h", getVideo(self)->getStatus());
00132 }
00133 
00134 // refresh video
00135 PyObject * Video_refresh (PyImage * self)
00136 {
00137         getVideo(self)->refresh();
00138         return Video_getStatus(self, NULL);
00139 }
00140 
00141 
00142 // get range
00143 PyObject * Video_getRange (PyImage * self, void * closure)
00144 {
00145         return Py_BuildValue("[ff]", getVideo(self)->getRange()[0],
00146                 getVideo(self)->getRange()[1]);
00147 }
00148 
00149 // set range
00150 int Video_setRange (PyImage * self, PyObject * value, void * closure)
00151 {
00152         // check validity of parameter
00153         if (value == NULL || !PySequence_Check(value) || PySequence_Size(value) != 2
00154                 || !PyFloat_Check(PySequence_Fast_GET_ITEM(value, 0))
00155                 || !PyFloat_Check(PySequence_Fast_GET_ITEM(value, 1)))
00156         {
00157                 PyErr_SetString(PyExc_TypeError, "The value must be a sequence of 2 float");
00158                 return -1;
00159         }
00160         // set range
00161         getVideo(self)->setRange(PyFloat_AsDouble(PySequence_Fast_GET_ITEM(value, 0)),
00162                 PyFloat_AsDouble(PySequence_Fast_GET_ITEM(value, 1)));
00163         // success
00164         return 0;
00165 }
00166 
00167 // get repeat
00168 PyObject * Video_getRepeat (PyImage * self, void * closure)
00169 { return Py_BuildValue("h", getVideo(self)->getRepeat()); }
00170 
00171 // set repeat
00172 int Video_setRepeat (PyImage * self, PyObject * value, void * closure)
00173 {
00174         // check validity of parameter
00175         if (value == NULL || !PyLong_Check(value))
00176         {
00177                 PyErr_SetString(PyExc_TypeError, "The value must be an int");
00178                 return -1;
00179         }
00180         // set repeat
00181         getVideo(self)->setRepeat(int(PyLong_AsSsize_t(value)));
00182         // success
00183         return 0;
00184 }
00185 
00186 // get frame rate
00187 PyObject * Video_getFrameRate (PyImage * self, void * closure)
00188 { return Py_BuildValue("f", double(getVideo(self)->getFrameRate())); }
00189 
00190 // set frame rate
00191 int Video_setFrameRate (PyImage * self, PyObject * value, void * closure)
00192 {
00193         // check validity of parameter
00194         if (value == NULL || !PyFloat_Check(value))
00195         {
00196                 PyErr_SetString(PyExc_TypeError, "The value must be a float");
00197                 return -1;
00198         }
00199         // set repeat
00200         getVideo(self)->setFrameRate(float(PyFloat_AsDouble(value)));
00201         // success
00202         return 0;
00203 }