$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
bilinear.hh
1 // Copyright (C) 2010, 2011, 2013 EPITA Research and Development
2 // Laboratory (LRDE)
3 //
4 // This file is part of Olena.
5 //
6 // Olena is free software: you can redistribute it and/or modify it under
7 // the terms of the GNU General Public License as published by the Free
8 // Software Foundation, version 2 of the License.
9 //
10 // Olena is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with Olena. If not, see <http://www.gnu.org/licenses/>.
17 //
18 // As a special exception, you may use this file as part of a free
19 // software project without restriction. Specifically, if other files
20 // instantiate templates or use macros or inline functions from this
21 // file, or you compile this file and link it with other files to produce
22 // an executable, this file does not by itself cause the resulting
23 // executable to be covered by the GNU General Public License. This
24 // exception does not however invalidate any other reasons why the
25 // executable file might be covered by the GNU General Public License.
26 
27 #ifndef SCRIBO_SUBSAMPLING_BILINEAR_HH
28 # define SCRIBO_SUBSAMPLING_BILINEAR_HH
29 
33 
34 # include <mln/core/concept/image.hh>
35 # include <mln/core/alias/box2d.hh>
36 # include <mln/geom/max_col.hh>
37 # include <mln/geom/max_row.hh>
38 
39 # include <mln/opt/at.hh>
40 
41 
42 namespace scribo
43 {
44 
45  namespace subsampling
46  {
47 
48  using namespace mln;
49 
51 
55  template <typename I>
56  mln_concrete(I)
57  bilinear(const Image<I>& input, int sub_ratio);
58 
59 
60 # ifndef MLN_INCLUDE_ONLY
61 
62 
63  template <typename I>
64  mln_concrete(I)
65  bilinear(const Image<I>& input_, int sub_ratio)
66  {
67  mln_trace("scribo::subsampling::bilinear");
68 
69  const I& input = exact(input_);
70  mln_precondition(input.is_valid());
71  mlc_is(mln_domain(I), box2d)::check();
72 
73  mln_concrete(I) output(input.domain().nrows() / sub_ratio,
74  input.domain().ncols() / sub_ratio);
75 
76  mln_piter(I) p(output.domain());
77  mln_value(I) pixels[4];
78 
79  mln::def::coord
80  x_offset = input.domain().pmin().col(),
81  y_offset = input.domain().pmin().row();
82 
83  for_all(p)
84  {
85  int x = int(round(-0.5 + p.col() * sub_ratio));
86  int y = int(round(-0.5 + p.row() * sub_ratio));
87 
88  if (x < 0)
89  x = 0;
90  else if (x >= geom::max_col(input))
91  x = input.ncols() - 1;
92 
93  if (y < 0)
94  y = 0;
95  else if (y >= geom::max_row(input))
96  y = input.nrows() - 1;
97 
98  double dx = (p.col() * sub_ratio) - x;
99  double dy = (p.row() * sub_ratio) - y;
100 
101  pixels[0] = opt::at(input, y + y_offset, x + x_offset);
102  pixels[1] = opt::at(input, y + y_offset, x + x_offset + 1);
103  pixels[2] = opt::at(input, y + y_offset + 1, x + x_offset);
104  pixels[3] = opt::at(input, y + y_offset + 1, x + x_offset + 1);
105 
106  output(p) = pixels[0] * (1 - dx) * (1 - dy) + pixels[1] * dx * (1 - dy) +
107  pixels[2] * (1 - dx) * dy + pixels[3] * dx * dy;
108 
109  }
110 
111  return output;
112  }
113 
114 # endif // ! MLN_INCLUDE_ONLY
115 
116 
117  } // end of namespace subsampling
118 
119 } // end of namespace scribo
120 
121 
122 #endif // SCRIBO_SUBSAMPLING_BILINEAR_HH