core/vil/vil_view_as.h
Go to the documentation of this file.
00001 // This is core/vil/vil_view_as.h
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 // \file
00009 // \brief Various view conversion functions.
00010 //
00011 // vil_image_view<T>::operator=() can automatically perform these
00012 // conversions for you.
00013 // \author Tim Cootes, Ian Scott - Manchester
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 //: 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.
00021 // Class T must be a compound pixel type.
00022 // \return an empty view if it can't do the conversion.
00023 // O(1).
00024 // \relatesalso vil_image_view
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   // An RGB image is RGBRGBRGB, an RGBA image is RGBARGBARGBA, and a
00033   // complex image is RCRCRC, so istep = ncomponents*v.istep(), and
00034   // jstep = ncomponents*v.jstep().
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 //: Return an RGB component view of a 3-plane image.
00043 // \return an empty view if it can't do the conversion (e.g. planestep != 1)
00044 // O(1).
00045 // \relatesalso vil_image_view
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 //: Return an RGBA component view of a 4-plane image.
00059 // \return an empty view if it can't do the conversion (e.g. planestep != 1)
00060 // O(1).
00061 // \relatesalso vil_image_view
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 //: Return a complex component view of a 2N-plane image.
00075 // \return an empty view if it can't do the conversion (e.g. planestep != 1)
00076 // O(1).
00077 // \relatesalso vil_image_view
00078 // \warning This view translation is a slight bodge. The underlying data is a still a component T.
00079 // This means that consistency check between the view and the underlying data will fail.
00080 // For example vsl_b_write(os, vil_view_as_complex(img); and vsl_b_read(...) will fail. Simply deep copy the
00081 // view before checking (or saving) the image to avoid problems.
00082 template<class T>
00083 inline vil_image_view<vcl_complex<T> >
00084 vil_view_as_complex (const vil_image_view<T> & v)
00085 {
00086   if ((v.nplanes()%2!=0) || (v.planestep()!=1) || (v.istep()!=2 && v.jstep()!=2))
00087       return vil_image_view<vcl_complex<T> >();
00088 
00089   return vil_image_view<vcl_complex<T> > (
00090       v.memory_chunk(),
00091       reinterpret_cast<vcl_complex<T> const *> (v.top_left_ptr()),
00092       v.ni(), v.nj(), v.nplanes()/2,
00093       v.istep()/2, v.jstep()/2, 1);
00094 }
00095 
00096 //: Base function to do the work for both vil_view_real/imag_part
00097 // O(1).
00098 // \relatesalso vil_image_view
00099 // \warning This view translation is a slight bodge. The underlying data is a still a scalar complex<T>.
00100 // This means that consistency check between the view and the underlying data will fail.
00101 // For example vsl_b_write(os, vil_view_part(img, i); and vsl_b_read(...) will fail. Simply deep copy the
00102 // view before checking (or saving) the image to avoid problems.
00103 template <class T>
00104 inline vil_image_view<T>
00105 vil_view_part (vil_image_view<vcl_complex<T> > img, int pt)
00106 {
00107   return vil_image_view<T> (
00108       img.memory_chunk(),
00109       reinterpret_cast<T *>(img.top_left_ptr()) + pt,
00110       img.ni(), img.nj(), img.nplanes(),
00111       2*img.istep(), 2*img.jstep(), 2*img.planestep());
00112 }
00113 
00114 //: Return a view of the real part of a complex image.
00115 // O(1).
00116 // \relatesalso vil_image_view
00117 // \warning This view translation is a slight bodge. The underlying data is a still a scalar complex<T>.
00118 // This means that consistency check between the view and the underlying data will fail.
00119 // For example vsl_b_write(os, vil_view_real_part(img, i); and vsl_b_read(...) will fail. Simply deep copy the
00120 // view before checking (or saving) the image to avoid problems.
00121 template <class T>
00122 inline vil_image_view<T>
00123 vil_view_real_part (vil_image_view<vcl_complex<T> > img)
00124 {
00125   return vil_view_part (img, 0);
00126 }
00127 
00128 //: Return a view of the imaginary part of a complex image.
00129 // O(1).
00130 // \relatesalso vil_image_view
00131 // \warning This view translation is a slight bodge. The underlying data is a still a scalar complex<T>.
00132 // This means that consistency check between the view and the underlying data will fail.
00133 // For example vsl_b_write(os, vil_view_imag_part(img, i); and vsl_b_read(...) will fail. Simply deep copy the
00134 // view before checking (or saving) the image to avoid problems.
00135 template <class T>
00136 inline vil_image_view<T>
00137 vil_view_imag_part (vil_image_view<vcl_complex<T> > img)
00138 {
00139   return vil_view_part (img, 1);
00140 }
00141 
00142 #endif // vil_view_as_h_