vil_view_as.h
Go to the documentation of this file.
1 // This is core/vil/vil_view_as.h
2 #ifndef vil_view_as_h_
3 #define vil_view_as_h_
4 //:
5 // \file
6 // \brief Various view conversion functions.
7 //
8 // vil_image_view<T>::operator=() can automatically perform these
9 // conversions for you.
10 // \author Tim Cootes, Ian Scott - Manchester
11 
12 #include <complex>
13 #ifdef _MSC_VER
14 # include <vcl_msvc_warnings.h>
15 #endif
16 #include <vil/vil_image_view.h>
17 #include <vil/vil_rgb.h>
18 #include <vil/vil_rgba.h>
19 
20 //: Return a 3-plane view of an RGB image, or a 4-plane view of an RGBA, or a 2-plane view of a complex image.
21 // Class T must be a compound pixel type.
22 // \return an empty view if it can't do the conversion.
23 // O(1).
24 // \relatesalso vil_image_view
25 template<class T>
27 {
28  typedef typename T::value_type comp_type;
29  if (v.nplanes()!=1) return vil_image_view<T>();
30  const unsigned ncomponents = sizeof(T) / sizeof(comp_type);
31 
32  // An RGB image is RGBRGBRGB, an RGBA image is RGBARGBARGBA, and a
33  // complex image is RCRCRC, so istep = ncomponents*v.istep(), and
34  // jstep = ncomponents*v.jstep().
35 
37  v.memory_chunk(),reinterpret_cast<comp_type const*>(v.top_left_ptr()),
38  v.ni(),v.nj(),ncomponents,
39  v.istep()*ncomponents,v.jstep()*ncomponents,1);
40 }
41 
42 //: Return an RGB component view of a 3-plane image.
43 // \return an empty view if it can't do the conversion (e.g. planestep != 1)
44 // O(1).
45 // \relatesalso vil_image_view
46 template<class T>
48 {
49  if ((v.nplanes()!=3) || (v.planestep()!=1) || (v.istep()!=3 && v.jstep()!=3))
50  return vil_image_view<vil_rgb<T> >();
51 
52  return vil_image_view<vil_rgb<T> >(v.memory_chunk(),
53  reinterpret_cast<vil_rgb<T> const*>( v.top_left_ptr()),
54  v.ni(),v.nj(),1,
55  v.istep()/3,v.jstep()/3,1);
56 }
57 
58 //: Return an RGBA component view of a 4-plane image.
59 // \return an empty view if it can't do the conversion (e.g. planestep != 1)
60 // O(1).
61 // \relatesalso vil_image_view
62 template<class T>
64 {
65  if ((v.nplanes()!=4) || (v.planestep()!=1) || (v.istep()!=4 && v.jstep()!=4))
66  return vil_image_view<vil_rgba<T> >();
67 
68  return vil_image_view<vil_rgba<T> >(v.memory_chunk(),
69  static_cast<vil_rgba<T> const*>( v.top_left_ptr()),
70  v.ni(),v.nj(),1,
71  v.istep()/3,v.jstep()/3,1);
72 }
73 
74 //: Return a complex component view of a 2N-plane image.
75 // \return an empty view if it can't do the conversion (e.g. planestep != 1)
76 // O(1).
77 // \relatesalso vil_image_view
78 // \warning This view translation is a slight bodge. The underlying data is a still a component T.
79 // This means that consistency check between the view and the underlying data will fail.
80 // For example vsl_b_write(os, vil_view_as_complex(img); and vsl_b_read(...) will fail. Simply deep copy the
81 // view before checking (or saving) the image to avoid problems.
82 template<class T>
85 {
86  if ((v.nplanes()%2!=0) || (v.planestep()!=1) || (v.istep()!=2 && v.jstep()!=2))
88 
90  v.memory_chunk(),
91  reinterpret_cast<std::complex<T> const *> (v.top_left_ptr()),
92  v.ni(), v.nj(), v.nplanes()/2,
93  v.istep()/2, v.jstep()/2, 1);
94 }
95 
96 //: Base function to do the work for both vil_view_real/imag_part
97 // O(1).
98 // \relatesalso vil_image_view
99 // \warning This view translation is a slight bodge. The underlying data is a still a scalar complex<T>.
100 // This means that consistency check between the view and the underlying data will fail.
101 // For example vsl_b_write(os, vil_view_part(img, i); and vsl_b_read(...) will fail. Simply deep copy the
102 // view before checking (or saving) the image to avoid problems.
103 template <class T>
104 inline vil_image_view<T>
105 vil_view_part (vil_image_view<std::complex<T> > img, int pt)
106 {
107  return vil_image_view<T> (
108  img.memory_chunk(),
109  reinterpret_cast<T *>(img.top_left_ptr()) + pt,
110  img.ni(), img.nj(), img.nplanes(),
111  2*img.istep(), 2*img.jstep(), 2*img.planestep());
112 }
113 
114 //: Return a view of the real part of a complex image.
115 // O(1).
116 // \relatesalso vil_image_view
117 // \warning This view translation is a slight bodge. The underlying data is a still a scalar complex<T>.
118 // This means that consistency check between the view and the underlying data will fail.
119 // For example vsl_b_write(os, vil_view_real_part(img, i); and vsl_b_read(...) will fail. Simply deep copy the
120 // view before checking (or saving) the image to avoid problems.
121 template <class T>
122 inline vil_image_view<T>
123 vil_view_real_part (vil_image_view<std::complex<T> > img)
124 {
125  return vil_view_part (img, 0);
126 }
127 
128 //: Return a view of the imaginary part of a complex image.
129 // O(1).
130 // \relatesalso vil_image_view
131 // \warning This view translation is a slight bodge. The underlying data is a still a scalar complex<T>.
132 // This means that consistency check between the view and the underlying data will fail.
133 // For example vsl_b_write(os, vil_view_imag_part(img, i); and vsl_b_read(...) will fail. Simply deep copy the
134 // view before checking (or saving) the image to avoid problems.
135 template <class T>
136 inline vil_image_view<T>
137 vil_view_imag_part (vil_image_view<std::complex<T> > img)
138 {
139  return vil_view_part (img, 1);
140 }
141 
142 #endif // vil_view_as_h_
vil_image_view< T > vil_view_imag_part(vil_image_view< std::complex< T > > img)
Return a view of the imaginary part of a complex image.
Definition: vil_view_as.h:137
vil_image_view< std::complex< T > > vil_view_as_complex(const vil_image_view< T > &v)
Return a complex component view of a 2N-plane image.
Definition: vil_view_as.h:84
Pixel type for 24 bit images.
Concrete view of image data of type T held in memory.
Definition: vil_fwd.h:13
#define v
vil_image_view< T > vil_view_part(vil_image_view< std::complex< T > > img, int pt)
Base function to do the work for both vil_view_real/imag_part.
Definition: vil_view_as.h:105
vil_image_view< vil_rgb< T > > vil_view_as_rgb(const vil_image_view< T > &v)
Return an RGB component view of a 3-plane image.
Definition: vil_view_as.h:47
vil_image_view< T > vil_view_real_part(vil_image_view< std::complex< T > > img)
Return a view of the real part of a complex image.
Definition: vil_view_as.h:123
A base class reference-counting view of some image data.
vil_image_view< typename T::value_type > vil_view_as_planes(const vil_image_view< T > &v)
Return a 3-plane view of an RGB image, or a 4-plane view of an RGBA, or a 2-plane view of a complex i...
Definition: vil_view_as.h:26
This is the appropriate pixel type for RGBA colour images.
Definition: vil_fwd.h:15
vil_image_view< vil_rgba< T > > vil_view_as_rgba(const vil_image_view< T > &v)
Return an RGBA component view of a 4-plane image.
Definition: vil_view_as.h:63
Templated four-value colour cell.
This is the appropriate pixel type for 24-bit colour images.
Definition: vil_fwd.h:14