1 // This is mul/vil3d/vil3d_image_view_base.h
2 #ifndef vil3d_image_view_base_h_
3 #define vil3d_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 #include <iostream>
17 #include <cstddef>
18 #include <cassert>
19 #ifdef _MSC_VER
20 #  include <vcl_msvc_warnings.h>
21 #endif
22 #include <vil/vil_pixel_format.h>
23 #include <vil/vil_smart_ptr.h>
24 #include <vgl/vgl_box_3d.h>
25 
26 //: An abstract base class of smart pointers to actual image data in memory.
27 // If you want an actual image, try instantiating vil3d_image_view<T>.
28 
29 class vil3d_image_view_base
30 {
31  protected:
32   //: Number of columns.
33    unsigned ni_{0};
34    //: Number of rasters.
35    unsigned nj_{0};
36    //: Number of slices.
37    unsigned nk_{0};
38    //: Number of planes.
39    unsigned nplanes_{1};
40 
vil3d_image_view_base(unsigned ni,unsigned nj,unsigned nk,unsigned nplanes)41    vil3d_image_view_base(unsigned ni, unsigned nj, unsigned nk,
42                          unsigned nplanes)
43        : ni_(ni), nj_(nj), nk_(nk), nplanes_(nplanes) {}
44 
45    //: Default is an empty one-plane image
46    //  Don't set nplanes_ to zero as it confuses set_size(nx,ny,nz) later
47    vil3d_image_view_base() = default;
48 
49  public:
50   // The destructor must be virtual so that the memory chunk is destroyed.
51   virtual ~vil3d_image_view_base() = default;
52 
53   //: Width
ni()54   unsigned ni()  const {return ni_;}
55   //: Height
nj()56   unsigned nj()  const {return nj_;}
57   //: Depth
nk()58   unsigned nk()  const {return nk_;}
59   //: Number of planes
nplanes()60   unsigned nplanes() const {return nplanes_;}
61 
62   //: Return a box describing the voxel region
bounds()63   vgl_box_3d<int> bounds() const
64   {
65     if (size()==0) return {};  // Empty
66     return {0,0,0,static_cast<int>(ni()-1),static_cast<int>(nj()-1),static_cast<int>(nk()-1)};
67   }
68 
69   //: The number of pixels.
size()70   std::size_t size() const { return static_cast<std::size_t>(ni_) * nj_ * nk_ * nplanes_; }
71 
72   //: resize current planes to ni x nj * nk
73   // If already correct size, this function returns quickly
74   virtual void set_size(unsigned ni, unsigned nj, unsigned nk) =0;
75 
76   //: resize to ni x nj * nk with nplanes planes.
77   // If already correct size, this function returns quickly
78   virtual void set_size(unsigned ni, unsigned nj, unsigned nk, unsigned nplanes) =0;
79 
80   //: Print a 1-line summary of contents
81   virtual void print(std::ostream&) const =0;
82 
83   //: Return class name
84   virtual std::string is_a() const =0;
85 
86   //: Return a description of the concrete data pixel type.
87   // For example if the value is VIL_PIXEL_FORMAT_BYTE,
88   // you can safely cast, or assign the base class reference to
89   // a vil3d_image_view<vxl_byte>.
90   virtual enum vil_pixel_format pixel_format() const=0;
91 
92   //: True if this is (or is derived from) class s
is_class(std::string const & s)93   virtual bool is_class(std::string const& s) const { return s=="vil3d_image_view_base"; }
94 
95  private:
96   // You probably should not use a vil3d_image_view in a vbl_smart_ptr, so the
97   // ref functions are private
98   friend class vil_smart_ptr<vil3d_image_view_base>;
ref()99   void ref() { ++reference_count_; }
unref()100   void unref() {
101     assert(reference_count_>0);
102     if (--reference_count_<=0) delete this;}
103   int reference_count_{0};
104 };
105 
106 //: An interface between vil3d_image_views and vil3d_image_resources
107 // This object is used internally by vil to provide a type-independent
108 // transient storage for a view as it is being assigned to a
109 // vil3d_image_view<T> from a vil3d_image_resource::get_view(),
110 // vil3d_load() or vil3d_convert_..() function call.
111 // If you want a type independent image container, you are recommended to
112 // use a vil3d_image_resource_sptr
113 typedef vil_smart_ptr<vil3d_image_view_base> vil3d_image_view_base_sptr;
114 
115 //: Print a 1-line summary of contents
116 inline
117 std::ostream& operator<<(std::ostream& s, vil3d_image_view_base const& im) {
118   im.print(s); return s;
119 }
120 
121 #endif // vil3d_image_view_base_h_
122