core/vil/vil_crop.h
Go to the documentation of this file.
00001 // This is core/vil/vil_crop.h
00002 #ifndef vil_crop_h_
00003 #define vil_crop_h_
00004 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00005 #pragma interface
00006 #endif
00007 //:
00008 // \file
00009 // \author Ian Scott.
00010 
00011 #include <vil/vil_image_resource.h>
00012 #include <vil/vil_image_view.h>
00013 #include <vcl_cassert.h>
00014 
00015 
00016 //: Create a view which is a cropped version of src.
00017 // Doesn't modify underlying data. O(1).
00018 // \relatesalso vil_image_view
00019 // \return an n_i x n_j window of im with offset (i0,j0)
00020 template<class T>
00021 inline vil_image_view<T> vil_crop(const vil_image_view<T> &im, unsigned i0,
00022                                   unsigned n_i, unsigned j0, unsigned n_j)
00023 {
00024   assert(i0<im.ni()); assert(i0+n_i<=im.ni());
00025   assert(j0<im.nj()); assert(j0+n_j<=im.nj());
00026   return vil_image_view<T>(im.memory_chunk(), im.top_left_ptr()+ i0*im.istep() + j0*im.jstep(),
00027                            n_i, n_j, im.nplanes(), im.istep(), im.jstep(), im.planestep());
00028 }
00029 
00030 //: Crop to a region of src.
00031 // \relatesalso vil_image_resource
00032 vil_image_resource_sptr vil_crop(const vil_image_resource_sptr &src, unsigned i0,
00033                                  unsigned n_i, unsigned j0, unsigned n_j);
00034 
00035 //: A generic_image adaptor that behaves like a cropped version of its input
00036 class vil_crop_image_resource : public vil_image_resource
00037 {
00038  public:
00039   vil_crop_image_resource(vil_image_resource_sptr const&, unsigned i0, unsigned n_i,
00040                           unsigned j0, unsigned n_j);
00041 
00042   virtual unsigned nplanes() const { return src_->nplanes(); }
00043   virtual unsigned ni() const { return ni_; }
00044   virtual unsigned nj() const { return nj_; }
00045 
00046   virtual enum vil_pixel_format pixel_format() const { return src_->pixel_format(); }
00047 
00048 
00049   virtual vil_image_view_base_sptr get_copy_view(unsigned i0, unsigned n_i,
00050                                                  unsigned j0, unsigned n_j) const;
00051 
00052   virtual vil_image_view_base_sptr get_view(unsigned i0, unsigned n_i,
00053                                             unsigned j0, unsigned n_j) const;
00054 
00055 
00056   //: Put the data in this view back into the image source.
00057   virtual bool put_view(const vil_image_view_base& im, unsigned i0, unsigned j0)
00058   {
00059     return src_->put_view(im, i0+i0_, j0+j0_);
00060   }
00061 
00062   //: Extra property information
00063   virtual bool get_property(char const* tag, void* property_value = 0) const
00064   {
00065     return src_->get_property(tag, property_value);
00066   }
00067 
00068  protected:
00069   vil_image_resource_sptr src_;
00070   int i0_;
00071   int ni_;
00072   int j0_;
00073   int nj_;
00074 };
00075 
00076 #endif // vil_crop_h_