Milena (Olena)
User documentation 2.0a Id
|
00001 // Copyright (C) 2009, 2011 EPITA Research and Development Laboratory 00002 // (LRDE) 00003 // 00004 // This file is part of Olena. 00005 // 00006 // Olena is free software: you can redistribute it and/or modify it under 00007 // the terms of the GNU General Public License as published by the Free 00008 // Software Foundation, version 2 of the License. 00009 // 00010 // Olena is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 // General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with Olena. If not, see <http://www.gnu.org/licenses/>. 00017 // 00018 // As a special exception, you may use this file as part of a free 00019 // software project without restriction. Specifically, if other files 00020 // instantiate templates or use macros or inline functions from this 00021 // file, or you compile this file and link it with other files to produce 00022 // an executable, this file does not by itself cause the resulting 00023 // executable to be covered by the GNU General Public License. This 00024 // exception does not however invalidate any other reasons why the 00025 // executable file might be covered by the GNU General Public License. 00026 00027 #ifndef MLN_WORLD_BINARY_2D_SUBSAMPLE_HH 00028 # define MLN_WORLD_BINARY_2D_SUBSAMPLE_HH 00029 00036 00037 # include <mln/core/image/image2d.hh> 00038 # include <mln/core/alias/dpoint2d.hh> 00039 # include <mln/data/convert.hh> 00040 # include <mln/value/int_u8.hh> 00041 00042 namespace mln 00043 { 00044 00045 namespace world 00046 { 00047 00048 namespace binary_2d 00049 { 00050 00057 // 00058 image2d<value::int_u8> 00059 subsample(image2d<bool>& input, unsigned n); 00060 00061 00062 # ifndef MLN_INCLUDE_ONLY 00063 00064 inline 00065 image2d<value::int_u8> 00066 subsample(image2d<bool>& input, unsigned n) 00067 { 00068 trace::entering("world::binary_2d::subsample"); 00069 00070 mln_precondition(input.is_valid()); 00071 00072 using value::int_u8; 00073 00074 if (n == 0) 00075 { 00076 image2d<value::int_u8> 00077 output = data::convert(int_u8(), input); 00078 00079 trace::exiting("world::binary_2d::subsample"); 00080 return output; 00081 } 00082 00083 const bool** ptr = new const bool*[n]; 00084 const unsigned nrows = input.nrows() / n; 00085 const unsigned ncols = input.ncols() / n; 00086 algebra::vec<2, unsigned int> vmin; 00087 algebra::vec<2, unsigned int> vmax; 00088 vmin[0] = 0; 00089 vmin[1] = 0; 00090 vmax[0] = nrows - 1; 00091 vmax[1] = ncols - 1; 00092 point2d pmin(vmin); 00093 point2d pmax(vmax); 00094 image2d<int_u8> output(box<point2d>(pmin, pmax)); 00095 00096 dpoint2d dp_row(1, 0); 00097 const unsigned delta_row = input.delta_index(dp_row); 00098 unsigned count = 0; 00099 00100 for (unsigned row = 0; row < nrows; ++row) 00101 { 00102 ptr[0] = & input(point2d(n * row, 0)); 00103 for (unsigned i = 1; i < n; ++i) 00104 ptr[i] = ptr[i - 1] + delta_row; 00105 for (unsigned col = 0; col < ncols; ++col) 00106 { 00107 count = 0; 00108 for (unsigned i = 0; i < n; ++i) 00109 { 00110 for (unsigned j = 0; j < n; ++j, ++(ptr[i])) 00111 { 00112 if (*(ptr[i])) 00113 ++count; 00114 } 00115 } 00116 output(point2d(row, col)) = count * 255 / n / n; 00117 } 00118 } 00119 00120 trace::exiting("world::binary_2d::subsample"); 00121 return output; 00122 } 00123 00124 # endif // ! MLN_INCLUDE_ONLY 00125 00126 } // end of namespace mln::world::binary_2d 00127 00128 } // end of namspace mln::world 00129 00130 } // end of namespace mln 00131 00132 #endif // ! MLN_WORLD_BINARY_2D_SUBSAMPLE_HH