contrib/mul/vimt/vimt_image_pyramid.txx
Go to the documentation of this file.
00001 // This is mul/vimt/vimt_image_pyramid.txx
00002 #ifndef vimt_image_pyramid_txx_
00003 #define vimt_image_pyramid_txx_
00004 //:
00005 // \file
00006 
00007 #include "vimt_image_pyramid.h"
00008 #include <vimt/vimt_image.h>
00009 #include <vimt/vimt_image_2d.h>
00010 
00011 #include <vcl_cstdlib.h> // for abort()
00012 #include <vcl_iostream.h>
00013 #include <vcl_cassert.h>
00014 
00015 //------------------------------------------------------------------------
00016 //: Convert an image pyramid into a flat image containing each layer.
00017 template <class T> void vimt_image_pyramid_flatten(T& out, const vimt_image_pyramid & in)
00018 {
00019   if (!out.is_class("vimt_image_2d"))
00020   {
00021     vcl_cerr<< "ERROR: vimt_image_pyramid_flatten(const vimt_image_pyramid &)\n"
00022             << "Don't know how to deal with image type "<< in(0).is_a() << '\n';
00023     vcl_abort();
00024   }
00025 
00026   const int n_levels = in.n_levels();
00027   assert(n_levels > 0);
00028   assert(out.is_a() == in(0).is_a());
00029 
00030   unsigned width = 0;
00031   for (int i =0; i<n_levels; ++i)
00032     width += static_cast<const vimt_image_2d&>(in(i)).image_base().ni();
00033 
00034   const unsigned nplanes = static_cast<const vimt_image_2d&>(in(0)).image_base().nplanes();
00035   unsigned height = static_cast<const vimt_image_2d&>(in(0)).image_base().nj();
00036   out.image().set_size(width, height, nplanes);
00037   out.image().fill(0);
00038 
00039   for (int i=0, offset=0; i<n_levels; ++i)
00040   {
00041     const T& im_i = static_cast<const T&>(in(i));
00042     const int ni = im_i.image().ni();
00043     const int nj = im_i.image().nj();
00044 
00045     for (unsigned int p=0;p<nplanes;++p)
00046       for (int y=0;y<nj;++y)
00047         for (int x=0;x<ni;++x)
00048           out.image()(x+offset, y, p) = im_i.image()(x, y, p);
00049 
00050     offset+=ni;
00051   }
00052 }
00053 
00054 
00055 #undef VIMT_IMAGE_PYRAMID_INSTANTIATE
00056 #define VIMT_IMAGE_PYRAMID_INSTANTIATE(T) \
00057 template void vimt_image_pyramid_flatten(T &, const vimt_image_pyramid &)
00058 
00059 #endif //vimt_image_pyramid_txx_