00001
00002 #ifndef vil_view_as_h_
00003 #define vil_view_as_h_
00004 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00005 #pragma interface
00006 #endif
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include <vcl_complex.h>
00016 #include <vil/vil_image_view.h>
00017 #include <vil/vil_rgb.h>
00018 #include <vil/vil_rgba.h>
00019
00020
00021
00022
00023
00024
00025 template<class T>
00026 inline vil_image_view<typename T::value_type> vil_view_as_planes(const vil_image_view<T >& v)
00027 {
00028 typedef typename T::value_type comp_type;
00029 if (v.nplanes()!=1) return vil_image_view<T>();
00030 const unsigned ncomponents = sizeof(T) / sizeof(comp_type);
00031
00032
00033
00034
00035
00036 return vil_image_view<comp_type>(
00037 v.memory_chunk(),reinterpret_cast<comp_type const*>(v.top_left_ptr()),
00038 v.ni(),v.nj(),ncomponents,
00039 v.istep()*ncomponents,v.jstep()*ncomponents,1);
00040 }
00041
00042
00043
00044
00045
00046 template<class T>
00047 inline vil_image_view<vil_rgb<T> > vil_view_as_rgb(const vil_image_view<T>& v)
00048 {
00049 if ((v.nplanes()!=3) || (v.planestep()!=1) || (v.istep()!=3 && v.jstep()!=3))
00050 return vil_image_view<vil_rgb<T> >();
00051
00052 return vil_image_view<vil_rgb<T> >(v.memory_chunk(),
00053 reinterpret_cast<vil_rgb<T> const*>( v.top_left_ptr()),
00054 v.ni(),v.nj(),1,
00055 v.istep()/3,v.jstep()/3,1);
00056 }
00057
00058
00059
00060
00061
00062 template<class T>
00063 inline vil_image_view<vil_rgba<T> > vil_view_as_rgba(const vil_image_view<T>& v)
00064 {
00065 if ((v.nplanes()!=4) || (v.planestep()!=1) || (v.istep()!=4 && v.jstep()!=4))
00066 return vil_image_view<vil_rgba<T> >();
00067
00068 return vil_image_view<vil_rgba<T> >(v.memory_chunk(),
00069 static_cast<vil_rgba<T> const*>( v.top_left_ptr()),
00070 v.ni(),v.nj(),1,
00071 v.istep()/3,v.jstep()/3,1);
00072 }
00073
00074
00075
00076
00077
00078 template<class T>
00079 inline vil_image_view<vcl_complex<T> >
00080 vil_view_as_complex (const vil_image_view<T> & v)
00081 {
00082 if ((v.nplanes()%2!=0) || (v.planestep()!=1) || (v.istep()!=2 && v.jstep()!=2))
00083 return vil_image_view<vcl_complex<T> >();
00084
00085 return vil_image_view<vcl_complex<T> > (
00086 v.memory_chunk(),
00087 reinterpret_cast<vcl_complex<T> const *> (v.top_left_ptr()),
00088 v.ni(), v.nj(), v.nplanes()/2,
00089 v.istep()/2, v.jstep()/2, 1);
00090 }
00091
00092
00093
00094
00095 template <class T>
00096 inline vil_image_view<T>
00097 vil_view_part (vil_image_view<vcl_complex<T> > img, int pt)
00098 {
00099 return vil_image_view<T> (
00100 img.memory_chunk(),
00101 reinterpret_cast<T *>(img.top_left_ptr()) + pt,
00102 img.ni(), img.nj(), img.nplanes(),
00103 2*img.istep(), 2*img.jstep(), 2*img.planestep());
00104 }
00105
00106
00107
00108
00109 template <class T>
00110 inline vil_image_view<T>
00111 vil_view_real_part (vil_image_view<vcl_complex<T> > img)
00112 {
00113 return vil_view_part (img, 0);
00114 }
00115
00116
00117
00118
00119 template <class T>
00120 inline vil_image_view<T>
00121 vil_view_imag_part (vil_image_view<vcl_complex<T> > img)
00122 {
00123 return vil_view_part (img, 1);
00124 }
00125
00126 #endif // vil_view_as_h_