Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members | Related Pages

vidl2_frame.h

Go to the documentation of this file.
00001 // This is brl/bbas/vidl2/vidl2_frame.h
00002 #ifndef vidl2_frame_h_
00003 #define vidl2_frame_h_
00004 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00005 #pragma interface
00006 #endif
00007 //:
00008 // \file
00009 // \brief A ref counted video frame
00010 //
00011 // \author Matt Leotta
00012 // \date 13 Jan 2006
00013 //
00014 //  The vidl2_frame is meant to be an object for transmitting a
00015 //  frame buffer from a vidl2_istream to a vidl2_ostream.  It
00016 //  is possible that the actual image buffer cannot be wrapped
00017 //  by a vil_image_view in a meaningful way (i.e. YUV 4:2:2).
00018 //  To work with a frame as an image use vidl2_convert_to_view
00019 
00020 #include "vidl2_pixel_format.h"
00021 #include "vidl2_frame_sptr.h"
00022 #include <vil/vil_memory_chunk.h>
00023 #include <vil/vil_image_view_base.h>
00024 
00025 
00026 //: A ref counted video frame
00027 class vidl2_frame
00028 {
00029   public:
00030     //: Destructor
00031     virtual ~vidl2_frame() {}
00032 
00033     //: Make the buffer invalid (data()==0 and size()==0)
00034     virtual void invalidate() { ni_=0; nj_=0; format_=VIDL2_PIXEL_FORMAT_UNKNOWN; }
00035 
00036     //: Return a pointer to the first element of data
00037     virtual void * data() = 0;
00038     virtual const void * data() const = 0;
00039 
00040     //: The size of the buffer in bytes
00041     virtual unsigned long size() const = 0;
00042 
00043     //: Width
00044     unsigned ni() const { return ni_; }
00045 
00046     //: Height
00047     unsigned nj() const { return nj_; }
00048 
00049     //: Return the pixel format
00050     vidl2_pixel_format pixel_format() const { return format_; }
00051 
00052   protected:
00053     //: Constructor
00054     vidl2_frame():
00055       ni_(0), nj_(0), format_(VIDL2_PIXEL_FORMAT_UNKNOWN), ref_count_(0) {}
00056 
00057     //: Constructor
00058     vidl2_frame(unsigned ni, unsigned nj, vidl2_pixel_format fmt):
00059       ni_(ni), nj_(nj), format_(fmt), ref_count_(0) {}
00060 
00061     //: frame width
00062     unsigned ni_;
00063     //: frame height
00064     unsigned nj_;
00065     //: frame pixel format
00066     vidl2_pixel_format format_;
00067 
00068   //-------------------------------------------------------
00069   // reference counting
00070   public:
00071 
00072     //: Increment reference count
00073     void ref() { ref_count_++; }
00074 
00075     //: Decrement reference count
00076     void unref();
00077 
00078     //: Number of objects referring to this data
00079     int ref_count() const { return ref_count_; }
00080 
00081   private:
00082     int ref_count_;
00083 };
00084 
00085 
00086 //: A frame buffer that shares someone else's data
00087 class vidl2_shared_frame : public vidl2_frame
00088 {
00089   public:
00090     //: Constructor
00091     vidl2_shared_frame():
00092       vidl2_frame(), buffer_(NULL) {}
00093 
00094     //: Constructor
00095     vidl2_shared_frame(void * buffer, unsigned ni, unsigned nj, vidl2_pixel_format fmt):
00096       vidl2_frame(ni,nj,fmt), buffer_(buffer) {}
00097 
00098     //: Destructor
00099     virtual ~vidl2_shared_frame() {}
00100 
00101     //: Make the buffer invalid (data()==0 and size()==0)
00102     virtual void invalidate() { buffer_ = 0; vidl2_frame::invalidate(); }
00103 
00104     //: Return a pointer to the first element of data
00105     virtual void * data() { return buffer_; }
00106     virtual const void * data() const { return buffer_; }
00107 
00108     //: The size of the buffer in bytes
00109     virtual unsigned long size() const { return vidl2_pixel_format_buffer_size(ni_,nj_,format_); }
00110 
00111   private:
00112     void * buffer_;
00113 };
00114 
00115 
00116 //: A frame buffer that wraps a vil_memory_chunk
00117 //  This is useful when the frame actually came from a vil_image
00118 class vidl2_memory_chunk_frame : public vidl2_frame
00119 {
00120   public:
00121     //: Constructor
00122     vidl2_memory_chunk_frame() : memory_(NULL) {}
00123 
00124     //: Constructor - from a vil_memory_chunk_sptr
00125     vidl2_memory_chunk_frame(unsigned ni, unsigned nj, vidl2_pixel_format fmt,
00126                              const vil_memory_chunk_sptr& memory):
00127       vidl2_frame(ni,nj,fmt), memory_(memory) {}
00128 
00129     //: Constructor - from a vil_image_view
00130     // return an invalid frame if the image format can not be wrapped
00131     // \param fmt if not UNKNOWN, requires this pixel or fails
00132     vidl2_memory_chunk_frame(const vil_image_view_base& image,
00133                              vidl2_pixel_format fmt = VIDL2_PIXEL_FORMAT_UNKNOWN);
00134 
00135     //: Return the memory chunk
00136     // used in recreating a vil_image_view
00137     inline const vil_memory_chunk_sptr& memory_chunk() const { return memory_; }
00138 
00139     //: Destructor
00140     virtual ~vidl2_memory_chunk_frame() {}
00141 
00142     //: Make the buffer invalid (data()==0 and size()==0)
00143     virtual void invalidate() { memory_ = NULL;  vidl2_frame::invalidate(); }
00144 
00145     //: Return a pointer to the first element of data
00146     virtual void * data () { return memory_?memory_->data():NULL; }
00147     virtual const void * data () const { return memory_?memory_->data():NULL; }
00148 
00149     //: The size of the buffer in bytes
00150     virtual unsigned long size() const { return memory_?memory_->size():0; }
00151 
00152   private:
00153     vil_memory_chunk_sptr memory_;
00154 };
00155 
00156 
00157 #endif // vidl2_frame_h_

Generated on Thu Jan 10 14:51:31 2008 for contrib/brl/bbas/vidl2 by  doxygen 1.4.4