1 // This is core/vil/vil_image_view_base.h
2 #ifndef vil_image_view_base_h_
3 #define vil_image_view_base_h_
4 //:
5 // \file
6 // \brief A base class reference-counting view of some image data.
7 // \author Ian Scott - Manchester
8 //
9 // \verbatim
10 //  Modifications
11 //   10 Sep. 2004 Peter Vanroose  Inlined all 1-line methods in class decl
12 // \endverbatim
13 
14 #include <iosfwd>
15 #include <string>
16 #ifdef _MSC_VER
17 #  include <vcl_msvc_warnings.h>
18 #endif
19 #include <cassert>
20 #include <vcl_atomic_count.h>
21 #include "vil_pixel_format.h"
22 #include "vil_smart_ptr.h"
23 
24 //: An abstract base class of smart pointers to actual image data in memory.
25 // If you want an actual image, try instantiating vil_image_view<T>.
26 
27 class vil_image_view_base
28 {
29  protected:
30   //: Number of columns.
31    unsigned ni_{0};
32    //: Number of rasters.
33    unsigned nj_{0};
34    //: Number of planes.
35    unsigned nplanes_{1};
36 
vil_image_view_base(unsigned n_i,unsigned n_j,unsigned n_planes)37    vil_image_view_base(unsigned n_i, unsigned n_j, unsigned n_planes)
38        : ni_(n_i), nj_(n_j), nplanes_(n_planes), reference_count_(0) {}
39 
40    //: Default is an empty one-plane image
41    //  Don't set nplanes_ to zero as it confuses set_size(nx,ny) later
vil_image_view_base()42    vil_image_view_base() : reference_count_(0) {}
43 
44  public:
45   // The destructor must be virtual so that the memory chunk is destroyed.
~vil_image_view_base()46   virtual ~vil_image_view_base() { assert( reference_count_ == 0 ); }
47 
48   //: Width
ni()49   unsigned ni()  const {return ni_;}
50   //: Height
nj()51   unsigned nj()  const {return nj_;}
52   //: Number of planes
nplanes()53   unsigned nplanes() const {return nplanes_;}
54 
55   //: The number of pixels.
size()56   unsigned long size() const { return ni_ * nj_ * nplanes_; }
57 
58   //: set_size current planes to width x height.
59   // If already correct size, this function returns quickly
60   virtual void set_size(unsigned width, unsigned height) =0;
61 
62   //: resize to width x height x n_planes.
63   // If already correct size, this function returns quickly
64   virtual void set_size(unsigned width, unsigned height, unsigned n_planes) =0;
65 
66   //: Print a 1-line summary of contents
67   virtual void print(std::ostream&) const =0;
68 
69   //: Return class name
70   virtual std::string is_a() const =0;
71 
72   //: Return a description of the concrete data pixel type.
73   // For example if the value is VIL_PIXEL_FORMAT_BYTE,
74   // you can safely cast, or assign the base class reference to
75   // a vil_image_view<vxl_byte>.
76   virtual enum vil_pixel_format pixel_format() const=0;
77 
78   //: True if this is (or is derived from) class s
is_class(std::string const & s)79   virtual bool is_class(std::string const& s) const { return s=="vil_image_view_base"; }
80 
81  private:
82   // You probably should not use a vil_image_view in a vbl_smart_ptr, so the
83   // ref functions are private
84   friend class vil_smart_ptr<vil_image_view_base>;
ref()85   void ref() { ++reference_count_; }
unref()86   void unref() {
87     assert(reference_count_>0);
88     if (--reference_count_<=0) delete this;}
89   vcl_atomic_count reference_count_;
90 };
91 
92 
93 //: An interface between vil_image_views and vil_image_resources
94 // This object is used internally by vil to provide a type-independent
95 // transient storage for a view as it is being assigned to a
96 // vil_image_view<T> from a vil_image_resource::get_view(),
97 // vil_load() or vil_convert_..() function call.
98 // If you want a type independent image container, you are recommended to
99 // use a vil_image_resource_sptr
100 typedef vil_smart_ptr<vil_image_view_base> vil_image_view_base_sptr;
101 
102 //: Print a 1-line summary of contents
103 inline
104 std::ostream& operator<<(std::ostream& s, vil_image_view_base const& im)
105 { im.print(s); return s; }
106 
107 #endif // vil_image_view_base_h_
108