contrib/mul/vil3d/vil3d_memory_image.cxx
Go to the documentation of this file.
00001 // This is mul/vil3d/vil3d_memory_image.cxx
00002 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00003 #pragma implementation
00004 #endif
00005 //:
00006 // \file
00007 // \author Ian Scott
00008 
00009 #include "vil3d_memory_image.h"
00010 #include <vcl_cassert.h>
00011 #include <vcl_cstdlib.h>
00012 #include <vxl_config.h> // for vxl_uint_32 etc.
00013 #include <vil3d/vil3d_image_view.h>
00014 #include <vil3d/vil3d_copy.h>
00015 #include <vil/vil_pixel_format.h>
00016 
00017 class vil3d_image_view_base;
00018 
00019 vil3d_memory_image::vil3d_memory_image():
00020    view_(new vil3d_image_view<vxl_byte>()) {}
00021 
00022 vil3d_memory_image::vil3d_memory_image(unsigned ni, unsigned nj, unsigned nk,
00023                                        unsigned nplanes, vil_pixel_format format)
00024 {
00025   // format should be a scalar type
00026   assert (vil_pixel_format_num_components(format)==1);
00027 
00028   switch (format)
00029   {
00030 #define macro( F , T ) \
00031   case F :     view_ = new vil3d_image_view<T >(ni, nj, nk, nplanes); break;
00032 macro(VIL_PIXEL_FORMAT_BYTE, vxl_byte )
00033 macro(VIL_PIXEL_FORMAT_SBYTE , vxl_sbyte )
00034 macro(VIL_PIXEL_FORMAT_UINT_32 , vxl_uint_32 )
00035 macro(VIL_PIXEL_FORMAT_UINT_16 , vxl_uint_16 )
00036 macro(VIL_PIXEL_FORMAT_INT_32 , vxl_int_32 )
00037 macro(VIL_PIXEL_FORMAT_INT_16 , vxl_int_16 )
00038 macro(VIL_PIXEL_FORMAT_BOOL , bool )
00039 macro(VIL_PIXEL_FORMAT_FLOAT , float )
00040 macro(VIL_PIXEL_FORMAT_DOUBLE , double )
00041 #undef macro
00042   default:
00043     vcl_cerr << "ERROR: vil3d_memory_image::vil3d_memory_image\n"
00044                 "\t unknown format " << format << vcl_endl;
00045     vcl_abort();
00046   }
00047 }
00048 
00049 
00050 //: Create a read/write view of a copy of this data.
00051 // Currently not yet implemented.
00052 // \return 0 if unable to get view of correct size.
00053 vil3d_image_view_base_sptr vil3d_memory_image::get_copy_view(unsigned i0, unsigned ni,
00054                                                              unsigned j0, unsigned nj,
00055                                                              unsigned k0, unsigned nk) const
00056 {
00057   if (i0 + ni > view_->ni() || j0 + nj > view_->nj() ||
00058       k0 + nk > view_->nk()) return 0;
00059 
00060   switch(view_->pixel_format())
00061   {
00062 #define macro( F , T ) \
00063   case  F : { \
00064       const vil3d_image_view< T > &v = \
00065         static_cast<const vil3d_image_view< T > &>(*view_); \
00066       vil3d_image_view< T > w(v.memory_chunk(), &v(i0,j0,k0), \
00067                              ni, nj, nk, v.nplanes(), \
00068                              v.istep(), v.jstep(), v.kstep(), v.planestep()); \
00069       return new vil3d_image_view< T >(vil3d_copy_deep(w)); }
00070 macro(VIL_PIXEL_FORMAT_BYTE, vxl_byte )
00071 macro(VIL_PIXEL_FORMAT_SBYTE , vxl_sbyte )
00072 macro(VIL_PIXEL_FORMAT_UINT_32 , vxl_uint_32 )
00073 macro(VIL_PIXEL_FORMAT_UINT_16 , vxl_uint_16 )
00074 macro(VIL_PIXEL_FORMAT_INT_32 , vxl_int_32 )
00075 macro(VIL_PIXEL_FORMAT_INT_16 , vxl_int_16 )
00076 macro(VIL_PIXEL_FORMAT_BOOL , bool )
00077 macro(VIL_PIXEL_FORMAT_FLOAT , float )
00078 macro(VIL_PIXEL_FORMAT_DOUBLE , double )
00079 #undef macro
00080   default:
00081     return 0;
00082   }
00083 }
00084 
00085 //: Create a read/write view of a copy of this data.
00086 // \return 0 if unable to get view of correct size.
00087 vil3d_image_view_base_sptr vil3d_memory_image::get_view(unsigned i0, unsigned ni,
00088                                                         unsigned j0, unsigned nj,
00089                                                         unsigned k0, unsigned nk) const
00090 {
00091   if (i0 + ni > view_->ni() || j0 + nj > view_->nj() ||
00092       k0 + nk > view_->nk()) return 0;
00093 
00094   switch(view_->pixel_format())
00095   {
00096 #define macro( F , T ) \
00097   case  F : { \
00098       const vil3d_image_view< T > &v = \
00099         static_cast<const vil3d_image_view< T > &>(*view_); \
00100       return new vil3d_image_view< T >(v.memory_chunk(), &v(i0,j0,k0), \
00101                                       ni, nj, nk, v.nplanes(), \
00102                                       v.istep(), v.jstep(), v.kstep(), \
00103                                       v.planestep()); }
00104 macro(VIL_PIXEL_FORMAT_BYTE , vxl_byte )
00105 macro(VIL_PIXEL_FORMAT_SBYTE , vxl_sbyte )
00106 macro(VIL_PIXEL_FORMAT_UINT_32 , vxl_uint_32 )
00107 macro(VIL_PIXEL_FORMAT_UINT_16 , vxl_uint_16 )
00108 macro(VIL_PIXEL_FORMAT_INT_32 , vxl_int_32 )
00109 macro(VIL_PIXEL_FORMAT_INT_16 , vxl_int_16 )
00110 macro(VIL_PIXEL_FORMAT_BOOL , bool )
00111 macro(VIL_PIXEL_FORMAT_FLOAT , float )
00112 macro(VIL_PIXEL_FORMAT_DOUBLE , double )
00113 #undef macro
00114   default:
00115     return 0;
00116   }
00117 }
00118 
00119 
00120 //: Put the data in this view back into the image source.
00121 // Currently not yet implemented.
00122 // \return true on success.
00123 bool vil3d_memory_image::put_view(const vil3d_image_view_base& im,
00124        unsigned i0, unsigned j0, unsigned k0)
00125 {
00126   if (view_-> pixel_format() != im.pixel_format()) return false;
00127   if (!view_fits(im, i0, j0, k0)) return false;
00128 
00129   switch(view_->pixel_format())
00130   {
00131 #define macro( F , T ) \
00132   case  F : { \
00133       vil3d_image_view< T > &v = \
00134         static_cast<vil3d_image_view< T > &>(*view_); \
00135       const vil3d_image_view< T > &w = \
00136         static_cast<const vil3d_image_view< T > &>(im); \
00137       if (v.memory_chunk() == w.memory_chunk()) \
00138       { \
00139         if (&v(i0,j0,k0) != w.origin_ptr()) { \
00140           vcl_cerr << "ERROR: vil_memory_image::put_view()\n" \
00141             "different window from that used in get_view()" << vcl_endl; \
00142           vcl_abort(); } \
00143         else return true; /* The user has already modified the data in place. */ \
00144       } \
00145       vil3d_copy_to_window(w, v, i0, j0, k0); \
00146       return true; }
00147 
00148 macro(VIL_PIXEL_FORMAT_BYTE , vxl_byte )
00149 macro(VIL_PIXEL_FORMAT_SBYTE , vxl_sbyte )
00150 macro(VIL_PIXEL_FORMAT_UINT_32 , vxl_uint_32 )
00151 macro(VIL_PIXEL_FORMAT_UINT_16 , vxl_uint_16 )
00152 macro(VIL_PIXEL_FORMAT_INT_32 , vxl_int_32 )
00153 macro(VIL_PIXEL_FORMAT_INT_16 , vxl_int_16 )
00154 macro(VIL_PIXEL_FORMAT_BOOL , bool )
00155 macro(VIL_PIXEL_FORMAT_FLOAT , float )
00156 macro(VIL_PIXEL_FORMAT_DOUBLE , double )
00157 #undef macro
00158 
00159   default:
00160     vcl_cerr << "WARNING: vil3d_memory_image::put_view()\n"
00161                 "\t Unexpected pixel type" << view_->pixel_format() << vcl_endl;
00162     return 0;
00163   }
00164 }
00165