vil_checker_board.hxx
Go to the documentation of this file.
1 // This is core/vil/algo/vil_checker_board.hxx
2 #ifndef vil_checker_board_hxx_
3 #define vil_checker_board_hxx_
4 //:
5 // \file
6 // \brief Build checker board image from two source
7 // \author Gehua Yang
8 
9 #include "vil_checker_board.h"
10 
11 #include <vil/vil_image_view.h>
12 #include <cassert>
13 #ifdef _MSC_VER
14 # include <vcl_msvc_warnings.h>
15 #endif
16 
17 template< typename DEST, typename SRC >
18 void
20  vil_image_view< SRC > const& src1,
21  vil_image_view< SRC > const& src2,
22  vil_checker_board_params const& params )
23 {
24  assert( src1.ni() == src2.ni() && src1.nj() == src2.nj() &&
25  src1.nplanes() == src2.nplanes() );
26  // has to be signed because the offset can be negative
27  //
28  const int ni=(signed)src1.ni();
29  const int nj=(signed)src1.nj();
30  const int np=(signed)src1.nplanes();
31 
32  // If desc image is empty, create one
33  if ( !des ) {
34  des.set_size( ni, nj, np );
35  des.fill( DEST(0) );
36  }
37  assert( des.ni() == src1.ni() && des.nj() == src1.nj() &&
38  des.nplanes() == src1.nplanes() );
39 
40  int xsize=params.xsize_;
41  int ysize=params.ysize_;
42  if ( xsize<0 || ysize<0 )
43  {
44  if ( !params.xnum_blks_ || !params.ynum_blks_ ) {
45  // no enough information
46  return;
47  }
48 
49  xsize = ni / params.xnum_blks_;
50  ysize = nj / params.ynum_blks_;
51  }
52 
53  for ( int i=0; i<ni; ++i )
54  {
55  // block on i index
56  int iblock = ( (i+params.xoff_) / xsize ) % 2;
57 
58  for ( int j=0; j<nj; ++j )
59  {
60  // block on j index
61  int jblock = ( (j+params.yoff_) / ysize + iblock ) % 2;
62 
63  // check the pixel availability
64  bool p1 = true, p2 = true;
65 
66  if ( params.not_use_color_flag_ )
67  {
68  p1 = false; p2 = false;
69  for (int p=0; p<np; ++p ){
70  p1 = p1 || ( src1(i, j, p ) != params.not_use_color_ );
71  p2 = p2 || ( src2(i, j, p ) != params.not_use_color_ );
72  }
73  }
74 
75  if ( params.use_alpha_plane_ )
76  {
77  p1 = p1 && ( src1(i, j, np-1) );
78  p2 = p2 && ( src2(i, j, np-1) );
79  }
80 
81  // if pixel on (jblock) image is not available,
82  // use the other
83  if ( jblock ) { // == 1
84  if ( p1 && !p2 )
85  jblock = 0;
86  }
87  else { // == 0
88  if ( !p1 && p2 )
89  jblock = 1;
90  }
91 
92  // fill pixel
93  if ( !jblock ) // == 0
94  for ( int p=0; p<np; ++p )
95  des(i, j, p) = src1(i, j, p);
96  else // == 0
97  for ( int p=0; p<np; ++p )
98  des(i, j, p) = src2(i, j, p);
99  }
100  }
101 }
102 
103 #undef VIL_CHECKER_BOARD_INSTANTIATE
104 #define VIL_CHECKER_BOARD_INSTANTIATE(srcT, desT) \
105 template void \
106 vil_checker_board( vil_image_view< desT >& des, \
107  vil_image_view< srcT > const& src1, \
108  vil_image_view< srcT > const& src2, \
109  vil_checker_board_params const& params )
110 
111 #endif // vil_checker_board_hxx_
void vil_checker_board(vil_image_view< DEST > &des, vil_image_view< SRC > const &src1, vil_image_view< SRC > const &src2, vil_checker_board_params const &params)
build a checker board image from two images.
Concrete view of image data of type T held in memory.
Definition: vil_fwd.h:13
void fill(T value)
Fill view with given value.
void set_size(unsigned ni, unsigned nj) override
resize current planes to ni x nj.
unsigned ni() const
Width.
unsigned nj() const
Height.
parameters for building checkboard.
A base class reference-counting view of some image data.
unsigned nplanes() const
Number of planes.
Build checker board image from two source.