core/vil/algo/vil_checker_board.txx
Go to the documentation of this file.
00001 // This is core/vil/algo/vil_checker_board.txx
00002 #ifndef vil_checker_board_txx_
00003 #define vil_checker_board_txx_
00004 //:
00005 // \file
00006 // \brief Build checker board image from two source
00007 // \author Gehua Yang
00008 
00009 #include "vil_checker_board.h"
00010 
00011 #include <vil/vil_image_view.h>
00012 #include <vcl_cassert.h>
00013 
00014 template< typename DEST, typename SRC >
00015 void
00016 vil_checker_board( vil_image_view< DEST >& des,
00017                    vil_image_view< SRC > const& src1,
00018                    vil_image_view< SRC > const& src2,
00019                    vil_checker_board_params const& params )
00020 {
00021   assert( src1.ni() == src2.ni() && src1.nj() == src2.nj() &&
00022           src1.nplanes() == src2.nplanes() );
00023   // has to be signed because the offset can be negative
00024   //
00025   const int ni=(signed)src1.ni();
00026   const int nj=(signed)src1.nj();
00027   const int np=(signed)src1.nplanes();
00028 
00029   // If desc image is empty, create one
00030   if ( !des ) {
00031     des.set_size( ni, nj, np );
00032     des.fill( DEST(0) );
00033   }
00034   assert( des.ni() == src1.ni() && des.nj() == src1.nj() &&
00035           des.nplanes() == src1.nplanes() );
00036 
00037   int xsize=params.xsize_;
00038   int ysize=params.ysize_;
00039   if ( xsize<0 || ysize<0 )
00040   {
00041     if ( !params.xnum_blks_ || !params.ynum_blks_ ) {
00042       // no enough information
00043       return;
00044     }
00045 
00046     xsize = ni / params.xnum_blks_;
00047     ysize = nj / params.ynum_blks_;
00048   }
00049 
00050   for ( int i=0; i<ni; ++i )
00051   {
00052     // block on i index
00053     int iblock = ( (i+params.xoff_) / xsize ) % 2;
00054 
00055     for ( int j=0; j<nj; ++j )
00056     {
00057       // block on j index
00058       int jblock = ( (j+params.yoff_) / ysize + iblock ) % 2;
00059 
00060       // check the pixel availability
00061       bool p1 = true, p2 = true;
00062 
00063       if ( params.not_use_color_flag_ )
00064       {
00065         p1 = false; p2 = false;
00066         for (int p=0; p<np; ++p ){
00067           p1 = p1 || ( src1(i, j, p ) != params.not_use_color_ );
00068           p2 = p2 || ( src2(i, j, p ) != params.not_use_color_ );
00069         }
00070       }
00071 
00072       if ( params.use_alpha_plane_ )
00073       {
00074         p1 = p1 && ( src1(i, j, np-1) );
00075         p2 = p2 && ( src2(i, j, np-1) );
00076       }
00077 
00078       // if pixel on (jblock) image is not available,
00079       // use the other
00080       if ( jblock ) {      // == 1
00081         if ( p1 && !p2 )
00082           jblock = 0;
00083       }
00084       else {             // == 0
00085         if ( !p1 && p2 )
00086           jblock = 1;
00087       }
00088 
00089       // fill pixel
00090       if ( !jblock )     // == 0
00091         for ( int p=0; p<np; ++p )
00092           des(i, j, p) = src1(i, j, p);
00093       else              // == 0
00094         for ( int p=0; p<np; ++p )
00095           des(i, j, p) = src2(i, j, p);
00096     }
00097   }
00098 }
00099 
00100 #undef VIL_CHECKER_BOARD_INSTANTIATE
00101 #define VIL_CHECKER_BOARD_INSTANTIATE(srcT, desT) \
00102 template void  \
00103 vil_checker_board( vil_image_view< desT >& des,   \
00104                    vil_image_view< srcT > const& src1, \
00105                    vil_image_view< srcT > const& src2, \
00106                    vil_checker_board_params const& params )
00107 
00108 #endif // vil_checker_board_txx_