contrib/mul/vil3d/vil3d_crop.h
Go to the documentation of this file.
00001 // This is mul/vil3d/vil3d_crop.h
00002 #ifndef vil3d_crop_h_
00003 #define vil3d_crop_h_
00004 //:
00005 // \file
00006 
00007 
00008 #include <vil3d/vil3d_image_view.h>
00009 #include <vil3d/vil3d_image_resource.h>
00010 #include <vcl_cassert.h>
00011 
00012 
00013 //: Create a view that is a window onto an existing image.
00014 // O(1).
00015 template <class T>
00016 vil3d_image_view<T> vil3d_crop(const vil3d_image_view<T>& im,
00017                                unsigned i0, unsigned ni, unsigned j0, unsigned nj,
00018                                unsigned k0, unsigned nk)
00019 {
00020   assert(i0+ni<=im.ni());
00021   assert(j0+nj<=im.nj());
00022   assert(k0+nk<=im.nk());
00023 
00024   // Have to force the cast to avoid compiler warnings about const
00025   return vil3d_image_view<T>(im.memory_chunk(), &im(i0,j0,k0),
00026                              ni, nj, nk, im.nplanes(),
00027                              im.istep(), im.jstep(), im.kstep(), im.planestep());
00028 }
00029 
00030 //: Crop to a region of src.
00031 // \relatesalso vil3d_image_resource
00032 vil3d_image_resource_sptr vil3d_crop(const vil3d_image_resource_sptr &src,
00033                                      unsigned i0, unsigned ni, 
00034                                      unsigned j0, unsigned nj,
00035                                      unsigned k0, unsigned nk);
00036 
00037 //: A generic_image adaptor that behaves like a cropped version of its input
00038 class vil3d_crop_image_resource : public vil3d_image_resource
00039 {
00040  public:
00041   vil3d_crop_image_resource(vil3d_image_resource_sptr const&, 
00042                             unsigned i0, unsigned ni, 
00043                             unsigned j0, unsigned nj,
00044                             unsigned k0, unsigned nk);
00045 
00046   virtual unsigned nplanes() const { return src_->nplanes(); }
00047   virtual unsigned ni() const { return ni_; }
00048   virtual unsigned nj() const { return nj_; }
00049   virtual unsigned nk() const { return nk_; }
00050 
00051   virtual enum vil_pixel_format pixel_format() const { return src_->pixel_format(); }
00052 
00053 
00054   virtual vil3d_image_view_base_sptr get_copy_view(unsigned i0, unsigned n_i, 
00055                                                    unsigned j0, unsigned n_j,
00056                                                    unsigned k0, unsigned n_k) const
00057   {
00058     if (i0 + n_i > ni() || j0 + n_j > nj() || k0 + n_k > nk()) return 0;
00059     return src_->get_copy_view(i0+i0_, n_i, j0+j0_, n_j, k0+k0_, n_k);
00060   }
00061 
00062   virtual vil3d_image_view_base_sptr get_view(unsigned i0, unsigned n_i, 
00063                                               unsigned j0, unsigned n_j,
00064                                               unsigned k0, unsigned n_k) const
00065   {
00066     if (i0 + n_i > ni() || j0 + n_j > nj() || k0 + n_k > nk()) return 0;
00067     return src_->get_view(i0+i0_, n_i, j0+j0_, n_j, k0+k0_, n_k);
00068   }
00069 
00070 
00071   //: Put the data in this view back into the image source.
00072   virtual bool put_view(const vil3d_image_view_base& im, unsigned i0, unsigned j0, unsigned k0)
00073   {
00074     return src_->put_view(im, i0+i0_, j0+j0_, k0+k0_);
00075   }
00076 
00077   //: Extra property information
00078   virtual bool get_property(char const* tag, void* property_value = 0) const
00079   {
00080     return src_->get_property(tag, property_value);
00081   }
00082 
00083  protected:
00084   vil3d_image_resource_sptr src_;
00085   int i0_;
00086   int ni_;
00087   int j0_;
00088   int nj_;
00089   int k0_;
00090   int nk_;
00091 };
00092 
00093 
00094 #endif // vil3d_crop_h_