core/vil/vil_image_view_base.h
Go to the documentation of this file.
00001 // This is core/vil/vil_image_view_base.h
00002 #ifndef vil_image_view_base_h_
00003 #define vil_image_view_base_h_
00004 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00005 #pragma interface
00006 #endif
00007 //:
00008 // \file
00009 // \brief A base class reference-counting view of some image data.
00010 // \author Ian Scott - Manchester
00011 //
00012 // \verbatim
00013 //  Modifications
00014 //   10 Sep. 2004 Peter Vanroose  Inlined all 1-line methods in class decl
00015 // \endverbatim
00016 
00017 #include <vcl_iosfwd.h>
00018 #include <vcl_string.h>
00019 #include <vcl_cassert.h>
00020 #include <vcl_atomic_count.h>
00021 #include <vil/vil_pixel_format.h>
00022 #include <vil/vil_smart_ptr.h>
00023 
00024 //: An abstract base class of smart pointers to actual image data in memory.
00025 // If you want an actual image, try instantiating vil_image_view<T>.
00026 
00027 class vil_image_view_base
00028 {
00029  protected:
00030   //: Number of columns.
00031   unsigned ni_;
00032   //: Number of rasters.
00033   unsigned nj_;
00034   //: Number of planes.
00035   unsigned nplanes_;
00036 
00037   vil_image_view_base(unsigned n_i, unsigned n_j, unsigned n_planes):
00038   ni_(n_i), nj_(n_j), nplanes_(n_planes), reference_count_(0) {}
00039 
00040   //: Default is an empty one-plane image
00041   //  Don't set nplanes_ to zero as it confuses set_size(nx,ny) later
00042   vil_image_view_base(): ni_(0), nj_(0), nplanes_(1), reference_count_(0) {}
00043 
00044  public:
00045   // The destructor must be virtual so that the memory chunk is destroyed.
00046   virtual ~vil_image_view_base() { assert( reference_count_ == 0 ); }
00047 
00048   //: Width
00049   unsigned ni()  const {return ni_;}
00050   //: Height
00051   unsigned nj()  const {return nj_;}
00052   //: Number of planes
00053   unsigned nplanes() const {return nplanes_;}
00054 
00055   //: The number of pixels.
00056   unsigned long size() const { return ni_ * nj_ * nplanes_; }
00057 
00058   //: set_size current planes to width x height.
00059   // If already correct size, this function returns quickly
00060   virtual void set_size(unsigned width, unsigned height) =0;
00061 
00062   //: resize to width x height x n_planes.
00063   // If already correct size, this function returns quickly
00064   virtual void set_size(unsigned width, unsigned height, unsigned n_planes) =0;
00065 
00066   //: Print a 1-line summary of contents
00067   virtual void print(vcl_ostream&) const =0;
00068 
00069   //: Return class name
00070   virtual vcl_string is_a() const =0;
00071 
00072   //: Return a description of the concrete data pixel type.
00073   // For example if the value is VIL_PIXEL_FORMAT_BYTE,
00074   // you can safely cast, or assign the base class reference to
00075   // a vil_image_view<vxl_byte>.
00076   virtual enum vil_pixel_format pixel_format() const=0;
00077 
00078   //: True if this is (or is derived from) class s
00079   virtual bool is_class(vcl_string const& s) const { return s=="vil_image_view_base"; }
00080 
00081  private:
00082   // You probably should not use a vil_image_view in a vbl_smart_ptr, so the
00083   // ref functions are private
00084   friend class vil_smart_ptr<vil_image_view_base>;
00085   void ref() { ++reference_count_; }
00086   void unref() {
00087     assert(reference_count_>0);
00088     if (--reference_count_<=0) delete this;}
00089   vcl_atomic_count reference_count_;
00090 };
00091 
00092 
00093 //: An interface between vil_image_views and vil_image_resources
00094 // This object is used internally by vil to provide a type-independent
00095 // transient storage for a view as it is being assigned to a
00096 // vil_image_view<T> from a vil_image_resource::get_view(),
00097 // vil_load() or vil_convert_..() function call.
00098 // If you want a type independent image container, you are recommended to
00099 // use a vil_image_resource_sptr
00100 typedef vil_smart_ptr<vil_image_view_base> vil_image_view_base_sptr;
00101 
00102 //: Print a 1-line summary of contents
00103 inline
00104 vcl_ostream& operator<<(vcl_ostream& s, vil_image_view_base const& im)
00105 { im.print(s); return s; }
00106 
00107 #endif // vil_image_view_base_h_