diff --git a/panda/src/grutil/aviTexture.I b/panda/src/grutil/aviTexture.I index c67b083afd..81acf3e6f4 100755 --- a/panda/src/grutil/aviTexture.I +++ b/panda/src/grutil/aviTexture.I @@ -1,4 +1,20 @@ -#ifdef HAVE_CV +// Filename: aviTexture.I +// Created by: zacpavlov (19Aug05) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) 2001 - 2004, Disney Enterprises, Inc. All rights reserved +// +// All use of this software is subject to the terms of the Panda 3d +// Software license. You should have received a copy of this license +// along with this source code; you will also find a current copy of +// the license at http://etc.cmu.edu/panda3d/docs/license/ . +// +// To contact the maintainers of this program write to +// panda3d-general@lists.sourceforge.net . +// +//////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////// @@ -7,14 +23,14 @@ // Description: Grabs a fresh ram buffer to mess with // //////////////////////////////////////////////////////////////////// -INLINE bool AviTexture::obtain_ram() -{ - _buf=modify_ram_image(); - if(_buf) - { +INLINE bool AviTexture:: +obtain_ram() { + _buf = modify_ram_image(); + + if (_buf) { return true; } - return 0; + return false; } @@ -25,11 +41,12 @@ INLINE bool AviTexture::obtain_ram() // positioned at. Disabled for camera input // //////////////////////////////////////////////////////////////////// -INLINE float AviTexture::get_time() -{ - if(!_isCamera) +INLINE float AviTexture:: +get_time() { + if (!_isCamera) { return _time; - return 0; + } + return 0.0f; } //////////////////////////////////////////////////////////////////// @@ -39,36 +56,37 @@ INLINE float AviTexture::get_time() // for camera input // //////////////////////////////////////////////////////////////////// -INLINE void AviTexture::set_time(float t) -{ - if(_isCamera) +INLINE void AviTexture:: +set_time(float t) { + if (_isCamera) { return; - if(t>=0 && t<=1) - { + } + if (t >= 0 && t <= 1) { _time=t; } - if(_capture) - { + if (_capture) { cvSetCaptureProperty(_capture,CV_CAP_PROP_POS_AVI_RATIO,t); - _current_frame=cvGetCaptureProperty(_capture,CV_CAP_PROP_POS_FRAMES); + _current_frame = cvGetCaptureProperty(_capture,CV_CAP_PROP_POS_FRAMES); } } //////////////////////////////////////////////////////////////////// // Function: AviTexture::set_fps // Access: Published -// Description: sets the fps for the movie, and affects frame recieves +// Description: sets the fps for the movie, and affects frame receives // from camera. Note, since frame update is controlled // by python, this item is sort of arbitrary //////////////////////////////////////////////////////////////////// -INLINE void AviTexture::set_fps(float fps) -{ - if(fps>=0) - _fps=fps; - else +INLINE void AviTexture:: +set_fps(float fps) { + if (fps >= 0) { + _fps = fps; + } else { return; - if(_isCamera && _capture) + } + if (_isCamera && _capture) { cvSetCaptureProperty(_capture,CV_CAP_PROP_FPS,int(fps)); + } } //////////////////////////////////////////////////////////////////// @@ -76,8 +94,8 @@ INLINE void AviTexture::set_fps(float fps) // Access: Published // Description: returns the fps as recieved from movie //////////////////////////////////////////////////////////////////// -INLINE float AviTexture::get_fps() -{ +INLINE float AviTexture:: +get_fps() { return _fps; } @@ -86,8 +104,8 @@ INLINE float AviTexture::get_fps() // Access: Published // Description: returns the total amount of frames //////////////////////////////////////////////////////////////////// -INLINE int AviTexture::get_total_frames() -{ +INLINE int AviTexture:: +get_total_frames() { return _total_frames; } @@ -96,8 +114,8 @@ INLINE int AviTexture::get_total_frames() // Access: Published // Description: returns current frame (base 0) //////////////////////////////////////////////////////////////////// -INLINE int AviTexture::get_current_frame() -{ +INLINE int AviTexture:: +get_current_frame() { return _current_frame; } @@ -106,15 +124,11 @@ INLINE int AviTexture::get_current_frame() // Access: Published // Description: sets the current frame (base 0) //////////////////////////////////////////////////////////////////// -INLINE void AviTexture::set_current_frame(int frame) -{ - if(frame>=0 && frame<_total_frames) - { - _current_frame=frame; +INLINE void AviTexture:: +set_current_frame(int frame) { + if (frame>=0 && frame<_total_frames) { + _current_frame = frame; cvSetCaptureProperty(_capture,CV_CAP_PROP_POS_FRAMES,_current_frame); - _time=cvGetCaptureProperty(_capture,CV_CAP_PROP_POS_AVI_RATIO); + _time = cvGetCaptureProperty(_capture,CV_CAP_PROP_POS_AVI_RATIO); } } - - -#endif diff --git a/panda/src/grutil/aviTexture.cxx b/panda/src/grutil/aviTexture.cxx index 06d5dc9c6b..6ffad80883 100755 --- a/panda/src/grutil/aviTexture.cxx +++ b/panda/src/grutil/aviTexture.cxx @@ -1,74 +1,97 @@ +// Filename: aviTexture.cxx +// Created by: zacpavlov (19Aug05) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) 2001 - 2004, Disney Enterprises, Inc. All rights reserved +// +// All use of this software is subject to the terms of the Panda 3d +// Software license. You should have received a copy of this license +// along with this source code; you will also find a current copy of +// the license at http://etc.cmu.edu/panda3d/docs/license/ . +// +// To contact the maintainers of this program write to +// panda3d-general@lists.sourceforge.net . +// +//////////////////////////////////////////////////////////////////// + #include "pandabase.h" + #ifdef HAVE_CV -#include "texture.h" #include "aviTexture.h" -#include TypeHandle AviTexture::_type_handle; //////////////////////////////////////////////////////////////////// -// Function: AviTexture::AviTexture +// Function: AviTexture::Constructor // Access: Published // Description: Sets up the texture to read frames from a camera //////////////////////////////////////////////////////////////////// -AviTexture::AviTexture() -{ - _isCamera=true; +AviTexture:: +AviTexture() { + _isCamera = true; _capture = cvCaptureFromCAM(0); - _buf=0; - _magicNum=0; - _time=0; - _fps=30; - _total_frames=0; - _current_frame=0; + _buf = NULL; + _magicNum = 0; + _time = 0.0f; + _fps = 30.0f; + _total_frames = 0; + _current_frame = 0; - if(_capture) - { - cvSetCaptureProperty(_capture,CV_CAP_PROP_FPS,_fps); - _width=cvGetCaptureProperty(_capture,CV_CAP_PROP_FRAME_WIDTH); - _height=cvGetCaptureProperty(_capture,CV_CAP_PROP_FRAME_HEIGHT); - if(_width<_height) + if (_capture) { + cvSetCaptureProperty(_capture, CV_CAP_PROP_FPS,_fps); + _width = cvGetCaptureProperty(_capture, CV_CAP_PROP_FRAME_WIDTH); + _height = cvGetCaptureProperty(_capture, CV_CAP_PROP_FRAME_HEIGHT); + + if (_width < _height) { gen_tex(_width); - else + } else { gen_tex(_height); + } } } //////////////////////////////////////////////////////////////////// -// Function: AviTexture::AviTexture +// Function: AviTexture::Constructor // Access: Published // Description: Defines the texture as a movie texture. // TODO: Make this search the panda paths //////////////////////////////////////////////////////////////////// -AviTexture::AviTexture(const string &filename) -{ - _isCamera=false; - _capture = cvCaptureFromFile(filename.c_str()); - _buf=0; - _magicNum=0; - _current_frame=0; - if(_capture) - { - _fps=cvGetCaptureProperty(_capture,CV_CAP_PROP_FPS); - _total_frames=cvGetCaptureProperty(_capture,CV_CAP_PROP_FRAME_COUNT); - _width=cvGetCaptureProperty(_capture,CV_CAP_PROP_FRAME_WIDTH); - _height=cvGetCaptureProperty(_capture,CV_CAP_PROP_FRAME_HEIGHT); - if(_width<_height) - gen_tex(_width); - else - gen_tex(_height); - } +AviTexture:: +AviTexture(const string &filename) { + _isCamera = false; + _capture = cvCaptureFromFile(filename.c_str()); + _buf = NULL; + _magicNum = 0; + _time = 0.0f; + _fps = 30.0f; + _total_frames = 0; + _current_frame = 0; + + if (_capture) { + _fps = cvGetCaptureProperty(_capture, CV_CAP_PROP_FPS); + _total_frames = cvGetCaptureProperty(_capture, CV_CAP_PROP_FRAME_COUNT); + _width = cvGetCaptureProperty(_capture, CV_CAP_PROP_FRAME_WIDTH); + _height = cvGetCaptureProperty(_capture, CV_CAP_PROP_FRAME_HEIGHT); + + if (_width < _height) { + gen_tex(_width); + } else { + gen_tex(_height); + } + } } //////////////////////////////////////////////////////////////////// -// Function: AviTexture::~AviTexture +// Function: AviTexture::Destructor // Access: Published // Description: Destructor. Release the camera or video stream //////////////////////////////////////////////////////////////////// -AviTexture::~AviTexture() -{ - cvReleaseCapture(&_capture); +AviTexture:: +~AviTexture() { + cvReleaseCapture(&_capture); } //////////////////////////////////////////////////////////////////// @@ -77,36 +100,12 @@ AviTexture::~AviTexture() // Description: Tries to find the largest texture that will fit // inside the video stream. TODO: allow for fit around //////////////////////////////////////////////////////////////////// -void AviTexture::gen_tex(int magicNum) -{ - if(magicNum<32) - { - magicNum=16; - } - else if(magicNum<64) - { - magicNum=32; - } - else if(magicNum<128) - { - magicNum=64; - } - else if(magicNum<256) - { - magicNum=128; - } - else if(magicNum<512) - { - magicNum=256; - } - else if(magicNum<1024) - { - magicNum=512; - } - else - magicNum=1024; - setup_2d_texture(magicNum,magicNum, Texture::T_unsigned_byte,Texture::F_rgb8); - _magicNum=magicNum; +void AviTexture:: +gen_tex(int magicNum) { + int size = down_to_power_2(magicNum); + + setup_2d_texture(size, size, Texture::T_unsigned_byte, Texture::F_rgb8); + _magicNum = size; } //////////////////////////////////////////////////////////////////// @@ -115,45 +114,50 @@ void AviTexture::gen_tex(int magicNum) // Description: Grabs the next frame off the camera or avi file // Returns false if the capture fails or reached EOF //////////////////////////////////////////////////////////////////// +bool AviTexture:: +update() { + int begx, endx, begy, endy; + IplImage *frame = 0; + int xs, ys; -bool AviTexture::update() -{ - int begx,endx,begy,endy; - IplImage* frame = 0; - int xs,ys; - if(_capture) - { - if(_time==1) + if (_capture) { + if (_time == 1.0f) { return false; + } frame = cvQueryFrame( _capture ); - if(frame) - { - _time=cvGetCaptureProperty(_capture,CV_CAP_PROP_POS_AVI_RATIO); - _current_frame=cvGetCaptureProperty(_capture,CV_CAP_PROP_POS_FRAMES); - if(!obtain_ram()) + + if (frame) { + _time = cvGetCaptureProperty(_capture, CV_CAP_PROP_POS_AVI_RATIO); + _current_frame = cvGetCaptureProperty(_capture, CV_CAP_PROP_POS_FRAMES); + if (!obtain_ram()) { return false; - begx=(_width-_magicNum)/2.0; - endx=_width-begx; - begy=(_height-_magicNum)/2.0; - endy=_height-begy; + } + begx = (_width - _magicNum) / 2.0; + endx = _width - begx; + begy = (_height - _magicNum) / 2.0; + endy = _height - begy; - if(_buf) - { + if (_buf) { + xs = get_x_size(); + ys = get_y_size(); - xs=get_x_size(); - ys=get_y_size(); - - if(get_num_components()!=3) + if (get_num_components() != 3) { return false; + } - for(int i=begy;iimageData+i*+_width*3+begx*3,xs*3); + for (int i = begy; i < endy; ++i) { + memcpy(_buf + ((i - begy) * xs * 3), + frame->imageData + i * _width * 3 + begx * 3, + xs * 3); + } return true; } } } + return false; } -#endif +#endif // HAVE_CV + diff --git a/panda/src/grutil/aviTexture.h b/panda/src/grutil/aviTexture.h index 3e744365a4..e371fd66cb 100755 --- a/panda/src/grutil/aviTexture.h +++ b/panda/src/grutil/aviTexture.h @@ -1,3 +1,21 @@ +// Filename: aviTexture.h +// Created by: zacpavlov (19Aug05) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) 2001 - 2004, Disney Enterprises, Inc. All rights reserved +// +// All use of this software is subject to the terms of the Panda 3d +// Software license. You should have received a copy of this license +// along with this source code; you will also find a current copy of +// the license at http://etc.cmu.edu/panda3d/docs/license/ . +// +// To contact the maintainers of this program write to +// panda3d-general@lists.sourceforge.net . +// +//////////////////////////////////////////////////////////////////// + #ifndef AVITEXTURE_H #define AVITEXTURE_H @@ -9,36 +27,47 @@ #include #include - - +//////////////////////////////////////////////////////////////////// +// Class : AviTexture +// Description : A specialization on Texture that takes its input +// using the CV library, to produce an animated texture, +// with its source taken from an .avi file or from a +// camera input. +// +// Presently, it is necessary for the application to +// call update() periodically to advance to the next +// frame. +//////////////////////////////////////////////////////////////////// class EXPCL_PANDA AviTexture : public Texture { PUBLISHED: - AviTexture(); - AviTexture(const string &filename); - INLINE void set_time(float t); - INLINE float get_time(); - INLINE float get_fps(); - INLINE void set_fps(float fps); - INLINE int get_total_frames(); - INLINE int get_current_frame(); - INLINE void set_current_frame(int frame); - bool update(); -public: - ~AviTexture(); -private: - void gen_tex(int magicNum); - INLINE bool obtain_ram(); - int _magicNum; - PTA_uchar _buf; - CvCapture * _capture; - float _time; - bool _isCamera; - float _fps; - int _total_frames; - int _current_frame; - int _width; - int _height; + AviTexture(); + AviTexture(const string &filename); + ~AviTexture(); + INLINE void set_time(float t); + INLINE float get_time(); + INLINE float get_fps(); + INLINE void set_fps(float fps); + INLINE int get_total_frames(); + INLINE int get_current_frame(); + INLINE void set_current_frame(int frame); + bool update(); + +private: + void gen_tex(int magicNum); + INLINE bool obtain_ram(); + + int _magicNum; + PTA_uchar _buf; + CvCapture * _capture; + float _time; + bool _isCamera; + float _fps; + int _total_frames; + int _current_frame; + int _width; + int _height; + public: static TypeHandle get_class_type() { return _type_handle; @@ -60,6 +89,6 @@ private: #include "aviTexture.I" -#endif +#endif // HAVE_CV #endif