core/vidl/vidl_frame.h
Go to the documentation of this file.
00001 // This is core/vidl/vidl_frame.h
00002 #ifndef vidl_frame_h_
00003 #define vidl_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 vidl_frame is meant to be an object for transmitting a
00015 //  frame buffer from a vidl_istream to a vidl_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 vidl_convert_to_view
00019 
00020 #include "vidl_pixel_format.h"
00021 #include <vil/vil_memory_chunk.h>
00022 #include <vil/vil_image_view_base.h>
00023 
00024 
00025 //: A ref counted video frame
00026 class vidl_frame
00027 {
00028   public:
00029     //: Destructor
00030     virtual ~vidl_frame() {}
00031 
00032     //: Make the buffer invalid (data()==0 and size()==0)
00033     virtual void invalidate() { ni_=0; nj_=0; format_=VIDL_PIXEL_FORMAT_UNKNOWN; }
00034 
00035     //: Return a pointer to the first element of data
00036     virtual void * data() = 0;
00037     virtual const void * data() const = 0;
00038 
00039     //: The size of the buffer in bytes
00040     virtual unsigned long size() const = 0;
00041 
00042     //: Width
00043     unsigned ni() const { return ni_; }
00044 
00045     //: Height
00046     unsigned nj() const { return nj_; }
00047 
00048     //: Return the pixel format
00049     vidl_pixel_format pixel_format() const { return format_; }
00050 
00051   protected:
00052     //: Constructor
00053     vidl_frame():
00054       ni_(0), nj_(0), format_(VIDL_PIXEL_FORMAT_UNKNOWN), ref_count_(0) {}
00055 
00056     //: Constructor
00057     vidl_frame(unsigned ni, unsigned nj, vidl_pixel_format fmt):
00058       ni_(ni), nj_(nj), format_(fmt), ref_count_(0) {}
00059 
00060     //: frame width
00061     unsigned ni_;
00062     //: frame height
00063     unsigned nj_;
00064     //: frame pixel format
00065     vidl_pixel_format format_;
00066 
00067   //-------------------------------------------------------
00068   // reference counting
00069   public:
00070 
00071     //: Increment reference count
00072     void ref() { ref_count_++; }
00073 
00074     //: Decrement reference count
00075     void unref();
00076 
00077     //: Number of objects referring to this data
00078     int ref_count() const { return ref_count_; }
00079 
00080   private:
00081     int ref_count_;
00082 };
00083 
00084 
00085 //: A frame buffer that shares someone else's data
00086 class vidl_shared_frame : public vidl_frame
00087 {
00088   public:
00089     //: Constructor
00090     vidl_shared_frame():
00091       vidl_frame(), buffer_(NULL) {}
00092 
00093     //: Constructor
00094     vidl_shared_frame(void * buffer, unsigned ni, unsigned nj, vidl_pixel_format fmt):
00095       vidl_frame(ni,nj,fmt), buffer_(buffer) {}
00096 
00097     //: Destructor
00098     virtual ~vidl_shared_frame() {}
00099 
00100     //: Make the buffer invalid (data()==0 and size()==0)
00101     virtual void invalidate() { buffer_ = 0; vidl_frame::invalidate(); }
00102 
00103     //: Return a pointer to the first element of data
00104     virtual void * data() { return buffer_; }
00105     virtual const void * data() const { return buffer_; }
00106 
00107     //: The size of the buffer in bytes
00108     virtual unsigned long size() const { return vidl_pixel_format_buffer_size(ni_,nj_,format_); }
00109 
00110   private:
00111     void * buffer_;
00112 };
00113 
00114 
00115 //: A frame buffer that wraps a vil_memory_chunk
00116 //  This is useful when the frame actually came from a vil_image
00117 class vidl_memory_chunk_frame : public vidl_frame
00118 {
00119   public:
00120     //: Constructor
00121     vidl_memory_chunk_frame() : memory_(NULL) {}
00122 
00123     //: Constructor - from a vil_memory_chunk_sptr
00124     vidl_memory_chunk_frame(unsigned ni, unsigned nj, vidl_pixel_format fmt,
00125                             const vil_memory_chunk_sptr& memory):
00126       vidl_frame(ni,nj,fmt), memory_(memory) {}
00127 
00128     //: Constructor - from a vil_image_view
00129     // return an invalid frame if the image format can not be wrapped
00130     // \param fmt if not UNKNOWN, requires this pixel or fails
00131     vidl_memory_chunk_frame(const vil_image_view_base& image,
00132                             vidl_pixel_format fmt = VIDL_PIXEL_FORMAT_UNKNOWN);
00133 
00134     //: Return the memory chunk
00135     // used in recreating a vil_image_view
00136     inline const vil_memory_chunk_sptr& memory_chunk() const { return memory_; }
00137 
00138     //: Destructor
00139     virtual ~vidl_memory_chunk_frame() {}
00140 
00141     //: Make the buffer invalid (data()==0 and size()==0)
00142     virtual void invalidate() { memory_ = NULL;  vidl_frame::invalidate(); }
00143 
00144     //: Return a pointer to the first element of data
00145     virtual void * data () { return memory_?memory_->data():NULL; }
00146     virtual const void * data () const { return memory_?memory_->data():NULL; }
00147 
00148     //: The size of the buffer in bytes
00149     virtual unsigned long size() const { return (unsigned long)(memory_?memory_->size():0L); }
00150 
00151   private:
00152     vil_memory_chunk_sptr memory_;
00153 };
00154 
00155 
00156 #endif // vidl_frame_h_