core/vil/vil_plane.cxx
Go to the documentation of this file.
00001 // This is core/vil/vil_plane.cxx
00002 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00003 #pragma implementation
00004 #endif
00005 //:
00006 // \file
00007 // \author Ian Scott.
00008 //
00009 // \verbatim
00010 //  Modifications
00011 //   23 Oct.2003 - Peter Vanroose - Added support for 64-bit int pixels
00012 // \endverbatim
00013 //
00014 //-----------------------------------------------------------------------------
00015 
00016 #include "vil_plane.h"
00017 #include <vcl_cassert.h>
00018 
00019 
00020 vil_image_resource_sptr vil_plane(const vil_image_resource_sptr &src, unsigned p)
00021 {
00022   return new vil_plane_image_resource(src, p);
00023 }
00024 
00025 vil_plane_image_resource::vil_plane_image_resource(vil_image_resource_sptr const& gi,
00026                                                    unsigned p):
00027   src_(gi),
00028   plane_(p)
00029 {
00030   assert (p <= src_->nplanes());
00031 }
00032 
00033 vil_image_view_base_sptr vil_plane_image_resource::get_copy_view(unsigned i0, unsigned ni,
00034                                                                  unsigned j0, unsigned nj) const
00035 {
00036   vil_image_view_base_sptr vs = src_->get_copy_view(i0, ni, j0, nj);
00037   if (!vs) return 0;
00038 
00039   switch (vs->pixel_format())
00040   {
00041 #define macro( F , T ) \
00042   case F : \
00043     return new vil_image_view<T > (vil_plane(static_cast<const vil_image_view<T >&>(*vs), plane_));
00044 
00045     macro(VIL_PIXEL_FORMAT_BYTE , vxl_byte )
00046     macro(VIL_PIXEL_FORMAT_SBYTE , vxl_sbyte )
00047 #if VXL_HAS_INT_64
00048     macro(VIL_PIXEL_FORMAT_UINT_64 , vxl_uint_64 )
00049     macro(VIL_PIXEL_FORMAT_INT_64 , vxl_int_64 )
00050 #endif
00051     macro(VIL_PIXEL_FORMAT_UINT_32 , vxl_uint_32 )
00052     macro(VIL_PIXEL_FORMAT_INT_32 , vxl_int_32 )
00053     macro(VIL_PIXEL_FORMAT_UINT_16 , vxl_uint_16 )
00054     macro(VIL_PIXEL_FORMAT_INT_16 , vxl_int_16 )
00055     macro(VIL_PIXEL_FORMAT_FLOAT , float )
00056     macro(VIL_PIXEL_FORMAT_DOUBLE , double )
00057     macro(VIL_PIXEL_FORMAT_COMPLEX_FLOAT ,  vcl_complex<float>)
00058     macro(VIL_PIXEL_FORMAT_COMPLEX_DOUBLE , vcl_complex<double>)
00059 #undef macro
00060   default:
00061     return 0;
00062   }
00063 }
00064 
00065 
00066 vil_image_view_base_sptr vil_plane_image_resource::get_view(unsigned i0, unsigned ni,
00067                                                             unsigned j0, unsigned nj) const
00068 {
00069   vil_image_view_base_sptr vs = src_->get_view(i0, ni, j0, nj);
00070   if (!vs) return 0;
00071 
00072   switch (vs->pixel_format())
00073   {
00074 #define macro( F , T ) \
00075   case F : \
00076     return new vil_image_view<T > (vil_plane(static_cast<const vil_image_view<T >&>(*vs), plane_));
00077 
00078     macro(VIL_PIXEL_FORMAT_BYTE , vxl_byte )
00079     macro(VIL_PIXEL_FORMAT_SBYTE , vxl_sbyte )
00080 #if VXL_HAS_INT_64
00081     macro(VIL_PIXEL_FORMAT_UINT_64 , vxl_uint_64 )
00082     macro(VIL_PIXEL_FORMAT_INT_64 , vxl_int_64 )
00083 #endif
00084     macro(VIL_PIXEL_FORMAT_UINT_32 , vxl_uint_32 )
00085     macro(VIL_PIXEL_FORMAT_INT_32 , vxl_int_32 )
00086     macro(VIL_PIXEL_FORMAT_UINT_16 , vxl_uint_16 )
00087     macro(VIL_PIXEL_FORMAT_INT_16 , vxl_int_16 )
00088     macro(VIL_PIXEL_FORMAT_FLOAT , float )
00089     macro(VIL_PIXEL_FORMAT_DOUBLE , double )
00090     macro(VIL_PIXEL_FORMAT_COMPLEX_FLOAT ,  vcl_complex<float>)
00091     macro(VIL_PIXEL_FORMAT_COMPLEX_DOUBLE , vcl_complex<double>)
00092 #undef macro
00093   default:
00094     return 0;
00095   }
00096 }
00097 
00098 
00099 //: Put the data in this view back into the image source.
00100 bool vil_plane_image_resource::put_view(const vil_image_view_base& im,
00101                                         unsigned i0, unsigned j0)
00102 {
00103   if (im.nplanes() != 1) return false;
00104   vil_image_view_base_sptr vs = src_->get_view(i0, im.ni(), j0, im.nj());
00105   if (!vs || im.pixel_format() != vs->pixel_format()) return false;
00106 
00107   switch (vs->pixel_format())
00108   {
00109 #define macro( F , T ) \
00110   case F : { \
00111     const vil_image_view<T > view = static_cast<const vil_image_view<T >&>(im); \
00112     vil_image_view<T > plane = vil_plane(static_cast<vil_image_view<T >&>(*vs), plane_); \
00113     if (view == plane) return true; /* If we have already modified the data, do nothing */ \
00114     for (unsigned j=0;j<view.nj();++j) \
00115       for (unsigned i=0;i<view.ni();++i) \
00116         plane(i,j) = view(i,j); \
00117     return src_->put_view(*vs, i0, j0); }
00118 
00119     macro(VIL_PIXEL_FORMAT_BYTE , vxl_byte )
00120     macro(VIL_PIXEL_FORMAT_SBYTE , vxl_sbyte )
00121 #if VXL_HAS_INT_64
00122     macro(VIL_PIXEL_FORMAT_UINT_64 , vxl_uint_64 )
00123     macro(VIL_PIXEL_FORMAT_INT_64 , vxl_int_64 )
00124 #endif
00125     macro(VIL_PIXEL_FORMAT_UINT_32 , vxl_uint_32 )
00126     macro(VIL_PIXEL_FORMAT_INT_32 , vxl_int_32 )
00127     macro(VIL_PIXEL_FORMAT_UINT_16 , vxl_uint_16 )
00128     macro(VIL_PIXEL_FORMAT_INT_16 , vxl_int_16 )
00129     macro(VIL_PIXEL_FORMAT_FLOAT , float )
00130     macro(VIL_PIXEL_FORMAT_DOUBLE , double )
00131     macro(VIL_PIXEL_FORMAT_COMPLEX_FLOAT ,  vcl_complex<float>)
00132     macro(VIL_PIXEL_FORMAT_COMPLEX_DOUBLE , vcl_complex<double>)
00133 #undef macro
00134   default:
00135     return false;
00136   }
00137 }
00138