Panda3D
 All Classes Functions Variables Enumerations
openCVTexture.h
1 // Filename: openCVTexture.h
2 // Created by: zacpavlov (19Aug05)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #ifndef OPENCVTEXTURE_H
16 #define OPENCVTEXTURE_H
17 
18 #include "pandabase.h"
19 #ifdef HAVE_OPENCV
20 
21 #include "videoTexture.h"
22 
23 // This symbol is predefined by the Panda3D build system to select
24 // whether we are using the OpenCV 2.3 or later interface, or if it is
25 // not defined, we are using the original interface.
26 #ifdef OPENCV_VER_23
27 
28 #include <opencv2/core/core.hpp>
29 //#include <opencv2/video/video.hpp>
30 #include <opencv2/highgui/highgui.hpp>
31 
32 #else
33 #include <cv.h>
34 #include <cxcore.h>
35 #include <highgui.h>
36 
37 #endif // OPENCV_VER_23
38 
39 ////////////////////////////////////////////////////////////////////
40 // Class : OpenCVTexture
41 // Description : A specialization on VideoTexture that takes its input
42 // using the CV library, to produce an animated texture,
43 // with its source taken from an .avi file or from a
44 // camera input.
45 ////////////////////////////////////////////////////////////////////
46 class EXPCL_VISION OpenCVTexture : public VideoTexture {
47 PUBLISHED:
48  OpenCVTexture(const string &name = string());
49 protected:
50  OpenCVTexture(const OpenCVTexture &copy);
51 PUBLISHED:
52  virtual ~OpenCVTexture();
53 
54  bool from_camera(int camera_index = -1, int z = 0,
55  int alpha_file_channel = 0,
56  const LoaderOptions &options = LoaderOptions());
57 
58 public:
59  static PT(Texture) make_texture();
60 
61 protected:
62  virtual void consider_update();
63  virtual PT(Texture) make_copy_impl();
64  void do_assign(Texture::CData *cdata_tex, const OpenCVTexture *copy,
65  const Texture::CData *cdata_copy_tex);
66 
67  virtual void do_update_frame(Texture::CData *cdata_tex, int frame);
68  virtual void do_update_frame(Texture::CData *cdata_tex, int frame, int z);
69 
70  virtual bool do_read_one(Texture::CData *cdata,
71  const Filename &fullpath, const Filename &alpha_fullpath,
72  int z, int n, int primary_file_num_channels, int alpha_file_channel,
73  const LoaderOptions &options,
74  bool header_only, BamCacheRecord *record);
75  virtual bool do_load_one(Texture::CData *cdata,
76  const PNMImage &pnmimage, const string &name,
77  int z, int n, const LoaderOptions &options);
78 
79 private:
80  class VideoPage;
81  class VideoStream;
82 
83  VideoPage &do_modify_page(const Texture::CData *cdata, int z);
84  bool do_reconsider_video_properties(Texture::CData *cdata,
85  const VideoStream &stream,
86  int num_components, int z,
87  const LoaderOptions &options);
88  void do_update();
89 
90  class VideoStream {
91  public:
92  VideoStream();
93  VideoStream(const VideoStream &copy);
94  ~VideoStream();
95 
96  bool read(const Filename &filename);
97  bool from_camera(int camera_index);
98  void clear();
99  INLINE bool is_valid() const;
100  INLINE bool is_from_file() const;
101  bool get_frame_data(int frame,
102  const unsigned char *&r,
103  const unsigned char *&g,
104  const unsigned char *&b,
105  int &x_pitch, int &y_pitch);
106 
107  CvCapture *_capture;
108  Filename _filename;
109  int _camera_index;
110  int _next_frame;
111  };
112 
113  class VideoPage {
114  public:
115  INLINE VideoPage();
116  INLINE VideoPage(const VideoPage &copy);
117  INLINE ~VideoPage();
118 
119  VideoStream _color, _alpha;
120  };
121 
122  typedef pvector<VideoPage> Pages;
123  Pages _pages;
124 
125 public:
126  static void register_with_read_factory();
127 
128 public:
129  static TypeHandle get_class_type() {
130  return _type_handle;
131  }
132  static void init_type() {
133  VideoTexture::init_type();
134  register_type(_type_handle, "OpenCVTexture",
135  VideoTexture::get_class_type());
136  }
137  virtual TypeHandle get_type() const {
138  return get_class_type();
139  }
140  virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
141 
142 private:
143  static TypeHandle _type_handle;
144 };
145 
146 #include "openCVTexture.I"
147 
148 #endif // HAVE_OPENCV
149 
150 #endif
void clear()
Reinitializes the texture to its default, empty state (except for the name).
Definition: texture.I:47
The name of this class derives from the fact that we originally implemented it as a layer on top of t...
Definition: pnmImage.h:68
Specifies parameters that may be passed to the loader.
Definition: loaderOptions.h:26
static void register_with_read_factory()
Factory method to generate a Texture object.
Definition: texture.cxx:7750
Represents a texture object, which is typically a single 2-d image but may also represent a 1-d or 3-...
Definition: texture.h:75
The base class for a family of animated Textures that take their input from a video source...
Definition: videoTexture.h:31
This is our own Panda specialization on the default STL vector.
Definition: pvector.h:39
The name of a file, such as a texture file or an Egg file.
Definition: filename.h:44
An instance of this class is written to the front of a Bam or Txo file to make the file a cached inst...
bool read(const Filename &fullpath, const LoaderOptions &options=LoaderOptions())
Reads the named filename into the texture.
Definition: texture.cxx:338
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85