• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List

transform.cc

00001 // Copyright (C) 2007, 2008, 2009 EPITA Research and Development Laboratory (LRDE)
00002 //
00003 // This file is part of Olena.
00004 //
00005 // Olena is free software: you can redistribute it and/or modify it under
00006 // the terms of the GNU General Public License as published by the Free
00007 // Software Foundation, version 2 of the License.
00008 //
00009 // Olena is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012 // General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with Olena.  If not, see <http://www.gnu.org/licenses/>.
00016 //
00017 // As a special exception, you may use this file as part of a free
00018 // software project without restriction.  Specifically, if other files
00019 // instantiate templates or use macros or inline functions from this
00020 // file, or you compile this file and link it with other files to produce
00021 // an executable, this file does not by itself cause the resulting
00022 // executable to be covered by the GNU General Public License.  This
00023 // exception does not however invalidate any other reasons why the
00024 // executable file might be covered by the GNU General Public License.
00025 
00026 #include <cmath>
00027 
00028 #include <mln/core/image/image1d.hh>
00029 #include <mln/core/image/image2d.hh>
00030 #include <mln/core/image/image3d.hh>
00031 #include <mln/pw/image.hh>
00032 #include <mln/core/image/flat_image.hh>
00033 #include <mln/core/image/vmorph/cast_image.hh>
00034 #include <mln/core/image/dmorph/image_if.hh>
00035 #include <mln/core/image/dmorph/sub_image.hh>
00036 #include <mln/core/image/dmorph/extension_val.hh>
00037 
00038 #include <mln/data/fill.hh>
00039 #include <mln/data/transform.hh>
00040 #include <mln/data/paste.hh>
00041 #include <mln/fun/p2b/chess.hh>
00042 #include <mln/fun/p2v/iota.hh>
00043 
00044 
00045 namespace my
00046 {
00047 
00048   template <typename I>
00049   void iota(I& ima)
00050   {
00051     unsigned i = 0;
00052     mln_piter(I) p(ima.domain());
00053     for_all(p)
00054     {
00055       ima(p) = i * i;
00056       i += 1;
00057     }
00058   }
00059 
00060   struct sqrt : mln::Function_v2v<sqrt>
00061   {
00062     typedef unsigned short result;
00063 
00064     template <typename T>
00065     result operator()(T c) const
00066     {
00067       return static_cast<result>( std::sqrt(float(c)) );
00068     }
00069   };
00070 
00071 } // end of namespace mln
00072 
00073 
00074 
00075 int main()
00076 {
00077   using namespace mln;
00078   const unsigned size = 5;
00079   box2d b = make::box2d(1,1, 3,3);
00080 
00081 
00083   {
00084     image1d<unsigned short> ima(size);
00085     image1d<unsigned short> out(size);
00086 
00087     my::iota(ima);
00088     out = data::transform(ima, my::sqrt());
00089 
00090     box_fwd_piter_<point1d> p(out.domain());
00091     for_all(p)
00092       mln_assertion(ima(p) == out(p) * out(p));
00093   }
00094 
00095 
00097   {
00098     image2d<unsigned short> ima(size, size);
00099     image2d<unsigned short> out(size, size);
00100 
00101      my::iota(ima);
00102      out = data::transform(ima, my::sqrt());
00103 
00104      box_fwd_piter_<point2d> p(out.domain());
00105      for_all(p)
00106        mln_assertion(ima(p) == out(p) * out(p));
00107   }
00108 
00110   {
00111      image2d<unsigned short> ima(size, size);
00112 
00113      data::fill_with_value(ima, 51);
00114      data::transform(ima, my::sqrt());
00115 
00116   }
00117 
00119   {
00120     image3d<unsigned short> ima(size, size, size);
00121     image3d<unsigned short> out(size, size, size);
00122 
00123     my::iota(ima);
00124     out = data::transform(ima, my::sqrt());
00125 
00126     box_fwd_piter_<point3d> p(out.domain());
00127     for_all(p)
00128       mln_assertion(ima(p) == out(p) * out(p));
00129   }
00130 
00132   {
00133     fun::p2v::iota f;
00134     const pw::image<fun::p2v::iota, box2d> ima(f, b);
00135     image2d<unsigned short> out(size, size);
00136 
00137     data::fill(out, 0u);
00138     out = data::transform(ima, my::sqrt());
00139   }
00140 
00141   // flat image test
00142   {
00143     flat_image<short, box2d> ima(5, b);
00144     image2d<unsigned short> out(size, size);
00145 
00146     data::fill_with_value(ima, 169);
00147     out = data::transform(ima, my::sqrt());
00148 
00149     box2d::piter p(out.domain());
00150     for_all(p)
00151       mln_assertion(ima(p) == out(p) * out(p));
00152   }
00153 
00154   // image if test
00155   {
00156     typedef image2d<unsigned short> I;
00157     typedef image_if<I, fun::p2b::chess> II;
00158 
00159     I ima(size, size);
00160     II ima_if = ima | fun::p2b::chess();
00161 
00162     data::fill_with_value(ima, 0);
00163     my::iota(ima);
00164     II out = data::transform(ima_if, my::sqrt());
00165 
00166     II::piter p(ima_if.domain());
00167     for_all(p)
00168       mln_assertion(ima_if(p) == out(p) * out(p));
00169   }
00170 
00171   // cast image test
00172   {
00173     typedef image2d<unsigned short> I;
00174     typedef cast_image_<int, I> II;
00175     typedef image2d<unsigned short> III;
00176 
00177     I in(size, size);
00178     II cast(in);
00179     III out(size, size);
00180 
00181     data::fill(in, 169u);
00182     data::fill(out, 81u);
00183 
00184     out = data::transform(cast, my::sqrt());
00185 
00186     II::piter p(cast.domain());
00187     for_all(p)
00188       mln_assertion(cast(p) == out(p) * out(p));
00189   }
00190 
00191   // sub_image test
00192   {
00193     typedef image2d<int> I;
00194     typedef sub_image< image2d<int>, box2d > II;
00195     typedef sub_image< image2d<unsigned short>, box2d > III;
00196 
00197     I ima(size, size);
00198     II sub_ima(ima, b);
00199 
00200     data::fill(ima, 169);
00201     III out = data::transform(sub_ima, my::sqrt());
00202 
00203     II::piter p(sub_ima.domain());
00204     for_all(p)
00205       mln_assertion(sub_ima(p) == out(p) * out(p));
00206   }
00207 
00208   // extended image test
00209   {
00210     typedef image2d<int> I;
00211     typedef extension_val< image2d<int> > II;
00212     typedef extension_val< image2d<unsigned short> > III;
00213 
00214     I ima(size, size);
00215     II extend_ima(ima, 169);
00216 
00217     data::fill(ima, 169);
00218     III out = data::transform(extend_ima, my::sqrt());
00219 
00220     II::piter p(extend_ima.domain());
00221     for_all(p)
00222       mln_assertion(extend_ima(p) == out(p) * out(p));
00223   }
00224 }

Generated on Tue Oct 4 2011 15:24:43 for Milena (Olena) by  doxygen 1.7.1