core/vidl/vidl_istream_image_resource.cxx
Go to the documentation of this file.
00001 // This is core/vidl/vidl_istream_image_resource.cxx
00002 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00003 #pragma implementation
00004 #endif
00005 //:
00006 // \file
00007 
00008 #include "vidl_istream_image_resource.h"
00009 #include <vidl/vidl_istream.h>
00010 #include "vidl_convert.h"
00011 
00012 #include <vcl_cassert.h>
00013 #include <vcl_iostream.h>
00014 
00015 #include <vil/vil_image_view.h>
00016 
00017 
00018 vidl_istream_image_resource::
00019     vidl_istream_image_resource(const vidl_istream_sptr& i_stream, int frame,
00020                                 unsigned int ni, unsigned int nj, unsigned int np,
00021                                 vil_pixel_format format)
00022   : istream_(i_stream),
00023     frame_number_(frame),
00024     ni_(ni), nj_(nj), np_(np),
00025     format_(format)
00026 {
00027 }
00028 
00029 
00030 //: try to find the image format and size from the current frame
00031 bool
00032 vidl_istream_image_resource::find_image_properties()
00033 {
00034   vidl_frame_sptr frame = istream_->current_frame();
00035   if (!frame)
00036     return false;
00037 
00038   vidl_pixel_format fmt = frame->pixel_format();
00039   unsigned int bpp = vidl_pixel_format_bpp(fmt);
00040   ni_ = frame->ni();
00041   nj_ = frame->nj();
00042   np_ = vidl_pixel_format_num_channels(fmt);
00043   if (bpp / np_ < 16)
00044     format_ = VIL_PIXEL_FORMAT_BYTE;
00045   else
00046     format_ = VIL_PIXEL_FORMAT_UINT_16;
00047 
00048   return true;
00049 }
00050 
00051 
00052 vidl_istream_image_resource::
00053     vidl_istream_image_resource(const vidl_istream_sptr& i_stream, int frame)
00054   : istream_(i_stream),
00055     frame_number_(frame)
00056 {
00057   assert(istream_);
00058   bool init = find_image_properties();
00059   assert(init);
00060 }
00061 
00062 
00063 vidl_istream_image_resource::
00064     vidl_istream_image_resource(const vidl_istream_sptr& i_stream)
00065   : istream_(i_stream),
00066     frame_number_(0)
00067 {
00068   assert(istream_);
00069   frame_number_ = istream_->frame_number();
00070   bool init = find_image_properties();
00071   assert(init);
00072 }
00073 
00074 
00075 vidl_istream_image_resource::~vidl_istream_image_resource()
00076 {
00077 }
00078 
00079 
00080 unsigned
00081 vidl_istream_image_resource::nplanes() const
00082 {
00083   return np_;
00084 }
00085 
00086 
00087 unsigned
00088 vidl_istream_image_resource::ni() const
00089 {
00090   return ni_;
00091 }
00092 
00093 
00094 unsigned
00095 vidl_istream_image_resource::nj() const
00096 {
00097   return nj_;
00098 }
00099 
00100 
00101 enum vil_pixel_format
00102 vidl_istream_image_resource::pixel_format() const
00103 {
00104   return format_;
00105 }
00106 
00107 
00108 bool
00109 vidl_istream_image_resource::get_property(char const *key, void * value) const
00110 {
00111   return false;
00112 }
00113 
00114 
00115 vil_image_view_base_sptr
00116 vidl_istream_image_resource::get_copy_view(unsigned i0, unsigned ni,
00117                                            unsigned j0, unsigned nj) const
00118 {
00119   if (!istream_)
00120     return NULL;
00121 
00122   int curr_frame = istream_->frame_number();
00123   vidl_frame_sptr frame = NULL;
00124   if (curr_frame == frame_number_)
00125     frame = istream_->current_frame();
00126   if (curr_frame + 1 == frame_number_) {
00127     if (istream_->advance())
00128       frame = istream_->current_frame();
00129   }
00130   else {
00131     if (istream_->is_seekable() && istream_->seek_frame(frame_number_))
00132       frame = istream_->current_frame();
00133   }
00134 
00135   if (!frame)
00136     return NULL;
00137 
00138   // try the wrap the frame in an image view
00139   vil_image_view_base_sptr view = vidl_convert_wrap_in_view(*frame);
00140 
00141   if (!view) {
00142     // try to convert the frame data to the expected view
00143     view = create_empty_view();
00144     vidl_convert_to_view(*frame,*view);
00145   }
00146   if (!view)
00147     return NULL;
00148 
00149   if (i0 == 0 && j0 == 0 && ni == view->ni() && nj == view->nj())
00150     return view;
00151 
00152   if (i0 + ni > view->ni() || j0 + nj > view->nj()) return NULL;
00153 
00154   switch (view->pixel_format())
00155   {
00156 #define macro( F , T ) \
00157    case  F : { \
00158     const vil_image_view< T > &v = static_cast<const vil_image_view< T > &>(*view); \
00159     return new vil_image_view< T >(v.memory_chunk(), &v(i0,j0), \
00160                                    ni, nj, v.nplanes(), \
00161                                    v.istep(), v.jstep(), v.planestep()); }
00162    macro(VIL_PIXEL_FORMAT_BYTE , vxl_byte )
00163    macro(VIL_PIXEL_FORMAT_SBYTE , vxl_sbyte )
00164 #if VXL_HAS_INT_64
00165    macro(VIL_PIXEL_FORMAT_UINT_64 , vxl_uint_64 )
00166    macro(VIL_PIXEL_FORMAT_INT_64 , vxl_int_64 )
00167 #endif
00168    macro(VIL_PIXEL_FORMAT_UINT_32 , vxl_uint_32 )
00169    macro(VIL_PIXEL_FORMAT_INT_32 , vxl_int_32 )
00170    macro(VIL_PIXEL_FORMAT_UINT_16 , vxl_uint_16 )
00171    macro(VIL_PIXEL_FORMAT_INT_16 , vxl_int_16 )
00172    macro(VIL_PIXEL_FORMAT_BOOL , bool )
00173    macro(VIL_PIXEL_FORMAT_FLOAT , float )
00174    macro(VIL_PIXEL_FORMAT_DOUBLE , double )
00175    macro(VIL_PIXEL_FORMAT_COMPLEX_FLOAT ,  vcl_complex<float>)
00176    macro(VIL_PIXEL_FORMAT_COMPLEX_DOUBLE , vcl_complex<double>)
00177 #undef macro
00178    default:
00179     break;
00180   }
00181 
00182   return NULL;
00183 }
00184 
00185 
00186 bool
00187 vidl_istream_image_resource::put_view(const vil_image_view_base &view,
00188                                       unsigned x0, unsigned y0)
00189 {
00190   vcl_cerr << "vidl_istream_image_resource::put_view not supported\n";
00191   return false;
00192 }
00193 
00194 
00195 //: create an empty image of the appropriate type and size
00196 vil_image_view_base_sptr
00197 vidl_istream_image_resource::create_empty_view() const
00198 {
00199   switch (format_)
00200   {
00201 #define macro( F , T ) \
00202    case  F : { \
00203     return new vil_image_view< T >(ni_,nj_,np_); }
00204    macro(VIL_PIXEL_FORMAT_BYTE , vxl_byte )
00205    macro(VIL_PIXEL_FORMAT_SBYTE , vxl_sbyte )
00206 #if VXL_HAS_INT_64
00207    macro(VIL_PIXEL_FORMAT_UINT_64 , vxl_uint_64 )
00208    macro(VIL_PIXEL_FORMAT_INT_64 , vxl_int_64 )
00209 #endif
00210    macro(VIL_PIXEL_FORMAT_UINT_32 , vxl_uint_32 )
00211    macro(VIL_PIXEL_FORMAT_INT_32 , vxl_int_32 )
00212    macro(VIL_PIXEL_FORMAT_UINT_16 , vxl_uint_16 )
00213    macro(VIL_PIXEL_FORMAT_INT_16 , vxl_int_16 )
00214    macro(VIL_PIXEL_FORMAT_BOOL , bool )
00215    macro(VIL_PIXEL_FORMAT_FLOAT , float )
00216    macro(VIL_PIXEL_FORMAT_DOUBLE , double )
00217    macro(VIL_PIXEL_FORMAT_COMPLEX_FLOAT ,  vcl_complex<float>)
00218    macro(VIL_PIXEL_FORMAT_COMPLEX_DOUBLE , vcl_complex<double>)
00219 #undef macro
00220    default:
00221     break;
00222   }
00223   return NULL;
00224 }
00225