contrib/mul/vil3d/vil3d_image_view_base.h
Go to the documentation of this file.
00001 // This is mul/vil3d/vil3d_image_view_base.h
00002 #ifndef vil3d_image_view_base_h_
00003 #define vil3d_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_cstddef.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 vil3d_image_view<T>.
00026 
00027 class vil3d_image_view_base
00028 {
00029  protected:
00030   //: Number of columns.
00031   unsigned ni_;
00032   //: Number of rasters.
00033   unsigned nj_;
00034   //: Number of slices.
00035   unsigned nk_;
00036   //: Number of planes.
00037   unsigned nplanes_;
00038 
00039   vil3d_image_view_base(unsigned ni, unsigned nj, unsigned nk, unsigned nplanes):
00040   ni_(ni), nj_(nj), nk_(nk), nplanes_(nplanes), reference_count_(0) {}
00041 
00042   //: Default is an empty one-plane image
00043   //  Don't set nplanes_ to zero as it confuses set_size(nx,ny,nz) later
00044   vil3d_image_view_base(): ni_(0), nj_(0), nk_(0), nplanes_(1), reference_count_(0) {}
00045 
00046  public:
00047   // The destructor must be virtual so that the memory chunk is destroyed.
00048   virtual ~vil3d_image_view_base() {}
00049 
00050   //: Width
00051   unsigned ni()  const {return ni_;}
00052   //: Height
00053   unsigned nj()  const {return nj_;}
00054   //: Depth
00055   unsigned nk()  const {return nk_;}
00056   //: Number of planes
00057   unsigned nplanes() const {return nplanes_;}
00058 
00059   //: The number of pixels.
00060   vcl_size_t size() const { return static_cast<vcl_size_t>(ni_) * nj_ * nk_ * nplanes_; }
00061 
00062   //: resize current planes to ni x nj * nk
00063   // If already correct size, this function returns quickly
00064   virtual void set_size(unsigned ni, unsigned nj, unsigned nk) =0;
00065 
00066   //: resize to ni x nj * nk with nplanes planes.
00067   // If already correct size, this function returns quickly
00068   virtual void set_size(unsigned ni, unsigned nj, unsigned nk, unsigned nplanes) =0;
00069 
00070   //: Print a 1-line summary of contents
00071   virtual void print(vcl_ostream&) const =0;
00072 
00073   //: Return class name
00074   virtual vcl_string is_a() const =0;
00075 
00076   //: Return a description of the concrete data pixel type.
00077   // For example if the value is VIL_PIXEL_FORMAT_BYTE,
00078   // you can safely cast, or assign the base class reference to
00079   // a vil3d_image_view<vxl_byte>.
00080   virtual enum vil_pixel_format pixel_format() const=0;
00081 
00082   //: True if this is (or is derived from) class s
00083   virtual bool is_class(vcl_string const& s) const { return s=="vil3d_image_view_base"; }
00084 
00085  private:
00086   // You probably should not use a vil3d_image_view in a vbl_smart_ptr, so the
00087   // ref functions are private
00088   friend class vil_smart_ptr<vil3d_image_view_base>;
00089   void ref() { ++reference_count_; }
00090   void unref() {
00091     assert(reference_count_>0);
00092     if (--reference_count_<=0) delete this;}
00093   int reference_count_;
00094 };
00095 
00096 //: An interface between vil3d_image_views and vil3d_image_resources
00097 // This object is used internally by vil to provide a type-independent
00098 // transient storage for a view as it is being assigned to a
00099 // vil3d_image_view<T> from a vil3d_image_resource::get_view(),
00100 // vil3d_load() or vil3d_convert_..() function call.
00101 // If you want a type independent image container, you are recommended to
00102 // use a vil3d_image_resource_sptr
00103 typedef vil_smart_ptr<vil3d_image_view_base> vil3d_image_view_base_sptr;
00104 
00105 //: Print a 1-line summary of contents
00106 inline
00107 vcl_ostream& operator<<(vcl_ostream& s, vil3d_image_view_base const& im) {
00108   im.print(s); return s;
00109 }
00110 
00111 #endif // vil3d_image_view_base_h_