core/vil/algo/vil_tile_images.h
Go to the documentation of this file.
00001 // This is core/vil/algo/vil_tile_images.h
00002 #ifndef vil_tile_images_h_
00003 #define vil_tile_images_h_
00004 //:
00005 // \file
00006 // \brief Create a big image by tiling images in patches
00007 // \author Tim Cootes
00008 
00009 #include <vil/vil_image_view.h>
00010 #include <vcl_vector.h>
00011 #include <vcl_cmath.h>
00012 #include <vcl_cassert.h>
00013 
00014 //: Create a big image by tiling images in patches (must be of same size).
00015 //  Creates a large image by putting smaller images into an approximately
00016 //  square grid.
00017 //  If there are n small images, the grid will have sqrt(n) columns.
00018 // \sa contrib/mul/tools/tile_images_2d
00019 // \relatesalso vil_image_view
00020 template<class T>
00021 inline void vil_tile_images(vil_image_view<T>& big_image,
00022                             const vcl_vector<vil_image_view<T> >& patches)
00023 {
00024   unsigned n = patches.size();
00025   unsigned nj = unsigned(vcl_sqrt(float(n)));
00026   unsigned ni = 1+(n-1)/nj;
00027 
00028   unsigned pi = patches[0].ni();
00029   unsigned pj = patches[0].nj();
00030   unsigned np = patches[0].nplanes();
00031 
00032   big_image.set_size(ni*pi,nj*pj);
00033   big_image.fill(vxl_byte(0));
00034   for (unsigned k=0;k<n;++k)
00035   {
00036     assert(patches[k].ni()==pi && patches[k].nj()== pj &&
00037            patches[k].nplanes()== np);
00038 
00039     unsigned i = k%ni;
00040     unsigned j = k/ni;
00041     for (unsigned y=0;y<pj;++y)
00042       for (unsigned x=0;x<pi;++x)
00043         for (unsigned p=0;p<np;++p)
00044           big_image(i*pi+x,j*pj+y,p) = patches[k](x,y,p);
00045   }
00046 }
00047 
00048 #endif // vil_tile_images_h_