Panda3D

openCVTexture.h

00001 // Filename: openCVTexture.h
00002 // Created by:  zacpavlov (19Aug05)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 #ifndef OPENCVTEXTURE_H
00016 #define OPENCVTEXTURE_H
00017 
00018 #include "pandabase.h"
00019 #ifdef HAVE_OPENCV
00020 
00021 #include "videoTexture.h"
00022 
00023 #include <cxcore.h>
00024 #include <cv.h>
00025 #include <highgui.h>
00026 
00027 ////////////////////////////////////////////////////////////////////
00028 //       Class : OpenCVTexture
00029 // Description : A specialization on VideoTexture that takes its input
00030 //               using the CV library, to produce an animated texture,
00031 //               with its source taken from an .avi file or from a
00032 //               camera input.
00033 ////////////////////////////////////////////////////////////////////
00034 class EXPCL_VISION OpenCVTexture : public VideoTexture {
00035 PUBLISHED:
00036   OpenCVTexture(const string &name = string());
00037 protected:
00038   OpenCVTexture(const OpenCVTexture &copy);
00039 PUBLISHED:
00040   virtual ~OpenCVTexture();
00041 
00042   bool from_camera(int camera_index = -1, int z = 0,
00043                    int alpha_file_channel = 0,
00044                    const LoaderOptions &options = LoaderOptions());
00045 
00046 public:
00047   static PT(Texture) make_texture();
00048 
00049 protected:
00050   virtual void consider_update();
00051   virtual PT(Texture) do_make_copy();
00052   void do_assign(const OpenCVTexture &copy);
00053 
00054   virtual void update_frame(int frame);
00055   virtual void update_frame(int frame, int z);
00056 
00057   virtual bool do_read_one(const Filename &fullpath, const Filename &alpha_fullpath,
00058                            int z, int n, int primary_file_num_channels, int alpha_file_channel,
00059                            const LoaderOptions &options,
00060                            bool header_only, BamCacheRecord *record);
00061   virtual bool do_load_one(const PNMImage &pnmimage, const string &name,
00062                            int z, int n, const LoaderOptions &options);
00063 
00064 private:    
00065   class VideoPage;
00066   class VideoStream;
00067 
00068   VideoPage &modify_page(int z);
00069   bool do_reconsider_video_properties(const VideoStream &stream, 
00070                                       int num_components, int z, 
00071                                       const LoaderOptions &options);
00072   void do_update();
00073 
00074   class VideoStream {
00075   public:
00076     VideoStream();
00077     VideoStream(const VideoStream &copy);
00078     ~VideoStream();
00079 
00080     bool read(const Filename &filename);
00081     bool from_camera(int camera_index);
00082     void clear();
00083     INLINE bool is_valid() const;
00084     INLINE bool is_from_file() const;
00085     bool get_frame_data(int frame,
00086                         const unsigned char *&r,
00087                         const unsigned char *&g,
00088                         const unsigned char *&b,
00089                         ssize_t &x_pitch, ssize_t &y_pitch);
00090 
00091     CvCapture *_capture;
00092     Filename _filename;
00093     int _camera_index;
00094     int _next_frame;
00095   };
00096 
00097   class VideoPage {
00098   public:
00099     INLINE VideoPage();
00100     INLINE VideoPage(const VideoPage &copy);
00101     INLINE ~VideoPage();
00102 
00103     VideoStream _color, _alpha;
00104   };
00105 
00106   typedef pvector<VideoPage> Pages;
00107   Pages _pages;
00108 
00109 public:
00110   static void register_with_read_factory();
00111 
00112 public:
00113   static TypeHandle get_class_type() {
00114     return _type_handle;
00115   }
00116   static void init_type() {
00117     VideoTexture::init_type();
00118     register_type(_type_handle, "OpenCVTexture",
00119                   VideoTexture::get_class_type());
00120   }
00121   virtual TypeHandle get_type() const {
00122     return get_class_type();
00123   }
00124   virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
00125 
00126 private:
00127   static TypeHandle _type_handle;
00128 };
00129 
00130 #include "openCVTexture.I"
00131 
00132 #endif  // HAVE_OPENCV
00133 
00134 #endif
 All Classes Functions Variables Enumerations