core/vil/vil_memory_image.cxx
Go to the documentation of this file.
00001 // This is core/vil/vil_memory_image.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 #include "vil_memory_image.h"
00015 #include <vcl_cassert.h>
00016 #include <vcl_cstdlib.h>
00017 #include <vxl_config.h> // for vxl_uint_32 etc.
00018 #include <vil/vil_image_view.h>
00019 #include <vil/vil_copy.h>
00020 #include <vil/vil_pixel_format.h>
00021 
00022 class vil_image_view_base;
00023 
00024 vil_memory_image::vil_memory_image()
00025 : view_(new vil_image_view<vxl_byte>()) {}
00026 
00027 
00028 //: Create an in-memory image of given size and pixel type.
00029 // If not interleaved, pixel type must be scalar or nplanes must be 1.
00030 // If n_interleaved_planes is not 1, pixel type must be scalar, and n_planes must be 1.
00031 vil_memory_image::vil_memory_image(unsigned n_i, unsigned n_j, unsigned n_planes,
00032                                    vil_pixel_format format, unsigned n_interleaved_planes/*=1*/)
00033 {
00034   // Check that only once source of multiple planes is not 1.
00035   assert( ( (n_planes==1?0:1) + (n_interleaved_planes==1?0:1) +
00036     (vil_pixel_format_num_components(format)==1?0:1) ) <= 1) ;
00037 
00038   if (vil_pixel_format_num_components(format)!=1)
00039     n_interleaved_planes = vil_pixel_format_num_components(format);
00040 
00041   switch (vil_pixel_format_component_format(format))
00042   {
00043 #define macro( F , T  ) \
00044    case F : view_ = new vil_image_view<T >(n_i, n_j, n_planes, n_interleaved_planes); \
00045             break;
00046    macro(VIL_PIXEL_FORMAT_BYTE ,   vxl_byte)
00047    macro(VIL_PIXEL_FORMAT_SBYTE ,  vxl_sbyte)
00048 #if VXL_HAS_INT_64
00049    macro(VIL_PIXEL_FORMAT_UINT_64, vxl_uint_64)
00050    macro(VIL_PIXEL_FORMAT_INT_64 , vxl_int_64)
00051 #endif
00052    macro(VIL_PIXEL_FORMAT_UINT_32, vxl_uint_32)
00053    macro(VIL_PIXEL_FORMAT_INT_32 , vxl_int_32)
00054    macro(VIL_PIXEL_FORMAT_UINT_16, vxl_uint_16)
00055    macro(VIL_PIXEL_FORMAT_INT_16 , vxl_int_16)
00056    macro(VIL_PIXEL_FORMAT_BOOL ,   bool)
00057    macro(VIL_PIXEL_FORMAT_FLOAT ,  float)
00058    macro(VIL_PIXEL_FORMAT_DOUBLE , double)
00059    macro(VIL_PIXEL_FORMAT_COMPLEX_FLOAT ,  vcl_complex<float>)
00060    macro(VIL_PIXEL_FORMAT_COMPLEX_DOUBLE , vcl_complex<double>)
00061 #undef macro
00062    default:
00063     vcl_cerr << "ERROR: vil_memory_image::vil_memory_image\n"
00064                 "\t unknown format " << format << vcl_endl;
00065     vcl_abort();
00066   }
00067 }
00068 
00069 
00070 //: Create a wrapper around the given image_view
00071 vil_memory_image::vil_memory_image(vil_image_view_base const &view)
00072 {
00073   switch (vil_pixel_format_component_format(view.pixel_format()))
00074   {
00075 #define macro( F , T ) \
00076    case F :  view_ = new vil_image_view<T >(view); break;
00077    macro(VIL_PIXEL_FORMAT_BYTE ,   vxl_byte )
00078    macro(VIL_PIXEL_FORMAT_SBYTE ,  vxl_sbyte )
00079 #if VXL_HAS_INT_64
00080    macro(VIL_PIXEL_FORMAT_UINT_64, vxl_uint_64 )
00081    macro(VIL_PIXEL_FORMAT_INT_64 , vxl_int_64 )
00082 #endif
00083    macro(VIL_PIXEL_FORMAT_UINT_32, vxl_uint_32 )
00084    macro(VIL_PIXEL_FORMAT_INT_32 , vxl_int_32 )
00085    macro(VIL_PIXEL_FORMAT_UINT_16, vxl_uint_16 )
00086    macro(VIL_PIXEL_FORMAT_INT_16 , vxl_int_16 )
00087    macro(VIL_PIXEL_FORMAT_BOOL ,   bool )
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     vcl_cerr << "ERROR: vil_memory_image::vil_memory_image\n\tunknown format "
00095              << vil_pixel_format_component_format(view.pixel_format()) << '\n';
00096     vcl_abort();
00097   }
00098   assert (view_->ni() == view.ni() && view_->nj() == view.nj());
00099 }
00100 
00101 
00102 //: Create a read/write view of a copy of this data.
00103 // Currently not yet implemented.
00104 // \return 0 if unable to get view of correct size.
00105 vil_image_view_base_sptr vil_memory_image::get_copy_view(unsigned i0, unsigned n_i,
00106                                                          unsigned j0, unsigned n_j) const
00107 {
00108   if (i0 + n_i > view_->ni() || j0 + n_j > view_->nj()) return 0;
00109 
00110   switch (view_->pixel_format())
00111   {
00112 #define macro( F , T ) \
00113    case  F : { \
00114     const vil_image_view< T > &v = static_cast<const vil_image_view< T > &>(*view_); \
00115     vil_image_view< T > w(v.memory_chunk(), &v(i0,j0), \
00116                           n_i, n_j, v.nplanes(), \
00117                           v.istep(), v.jstep(), v.planestep()); \
00118     return new vil_image_view< T >(vil_copy_deep(w)); }
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_BOOL , bool )
00130    macro(VIL_PIXEL_FORMAT_FLOAT , float )
00131    macro(VIL_PIXEL_FORMAT_DOUBLE , double )
00132    macro(VIL_PIXEL_FORMAT_COMPLEX_FLOAT ,  vcl_complex<float>)
00133    macro(VIL_PIXEL_FORMAT_COMPLEX_DOUBLE , vcl_complex<double>)
00134 #undef macro
00135    default:
00136     return 0;
00137   }
00138 }
00139 
00140 //: Create a read/write view of a copy of this data.
00141 // \return 0 if unable to get view of correct size.
00142 vil_image_view_base_sptr vil_memory_image::get_view(unsigned i0, unsigned n_i,
00143                                                     unsigned j0, unsigned n_j) const
00144 {
00145   if (i0 + n_i > view_->ni() || j0 + n_j > view_->nj()) return 0;
00146 
00147   switch (view_->pixel_format())
00148   {
00149 #define macro( F , T ) \
00150    case  F : { \
00151     const vil_image_view< T > &v = static_cast<const vil_image_view< T > &>(*view_); \
00152     return new vil_image_view< T >(v.memory_chunk(), &v(i0,j0), \
00153                                    n_i, n_j, v.nplanes(), \
00154                                    v.istep(), v.jstep(), v.planestep()); }
00155    macro(VIL_PIXEL_FORMAT_BYTE , vxl_byte )
00156    macro(VIL_PIXEL_FORMAT_SBYTE , vxl_sbyte )
00157 #if VXL_HAS_INT_64
00158    macro(VIL_PIXEL_FORMAT_UINT_64 , vxl_uint_64 )
00159    macro(VIL_PIXEL_FORMAT_INT_64 , vxl_int_64 )
00160 #endif
00161    macro(VIL_PIXEL_FORMAT_UINT_32 , vxl_uint_32 )
00162    macro(VIL_PIXEL_FORMAT_INT_32 , vxl_int_32 )
00163    macro(VIL_PIXEL_FORMAT_UINT_16 , vxl_uint_16 )
00164    macro(VIL_PIXEL_FORMAT_INT_16 , vxl_int_16 )
00165    macro(VIL_PIXEL_FORMAT_BOOL , bool )
00166    macro(VIL_PIXEL_FORMAT_FLOAT , float )
00167    macro(VIL_PIXEL_FORMAT_DOUBLE , double )
00168    macro(VIL_PIXEL_FORMAT_COMPLEX_FLOAT ,  vcl_complex<float>)
00169    macro(VIL_PIXEL_FORMAT_COMPLEX_DOUBLE , vcl_complex<double>)
00170 #undef macro
00171    default:
00172     return 0;
00173   }
00174 }
00175 
00176 
00177 //: Put the data in this view back into the image source.
00178 // \return true on success.
00179 bool vil_memory_image::put_view(const vil_image_view_base& im,unsigned i0, unsigned j0)
00180 {
00181   if (view_-> pixel_format() != im.pixel_format()) return false;
00182   if (!view_fits(im, i0, j0)) return false;
00183 
00184   switch (view_->pixel_format())
00185   {
00186 #define macro( F , T ) \
00187    case  F : { \
00188     vil_image_view< T > &v = static_cast<vil_image_view< T > &>(*view_); \
00189     const vil_image_view< T > &w = static_cast<const vil_image_view< T > &>(im); \
00190     if (v.memory_chunk() == w.memory_chunk()) \
00191     { \
00192       if (&v(i0,j0) != w.top_left_ptr()) { \
00193         vcl_cerr << "ERROR: vil_memory_image::put_view()\n" \
00194                  << "different window from that used in get_view()\n"; \
00195         vcl_abort(); } \
00196       else return true; /* The user has already modified the data in place. */ \
00197     } \
00198     vil_copy_to_window(w, v, i0, j0); \
00199     return true; }
00200 
00201    macro(VIL_PIXEL_FORMAT_BYTE , vxl_byte )
00202    macro(VIL_PIXEL_FORMAT_SBYTE , vxl_sbyte )
00203 #if VXL_HAS_INT_64
00204    macro(VIL_PIXEL_FORMAT_UINT_64 , vxl_uint_64 )
00205    macro(VIL_PIXEL_FORMAT_INT_64 , vxl_int_64 )
00206 #endif
00207    macro(VIL_PIXEL_FORMAT_UINT_32 , vxl_uint_32 )
00208    macro(VIL_PIXEL_FORMAT_INT_32 , vxl_int_32 )
00209    macro(VIL_PIXEL_FORMAT_UINT_16 , vxl_uint_16 )
00210    macro(VIL_PIXEL_FORMAT_INT_16 , vxl_int_16 )
00211    macro(VIL_PIXEL_FORMAT_BOOL , bool )
00212    macro(VIL_PIXEL_FORMAT_FLOAT , float )
00213    macro(VIL_PIXEL_FORMAT_DOUBLE , double )
00214    macro(VIL_PIXEL_FORMAT_COMPLEX_FLOAT ,  vcl_complex<float>)
00215    macro(VIL_PIXEL_FORMAT_COMPLEX_DOUBLE , vcl_complex<double>)
00216 #undef macro
00217    default:
00218     vcl_cerr << "WARNING: vil_memory_image::put_view()\n"
00219              << "\t Unexpected pixel type" << view_->pixel_format() << vcl_endl;
00220     return 0;
00221   }
00222 }
00223