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

colorize.hh

00001 // Copyright (C) 2008, 2009 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_LABELING_COLORIZE_HH
00028 # define MLN_LABELING_COLORIZE_HH
00029 
00033 
00034 # include <mln/core/concept/image.hh>
00035 # include <mln/fun/i2v/array.hh>
00036 # include <mln/value/rgb8.hh>
00037 # include <mln/literal/black.hh>
00038 # include <mln/data/transform.hh>
00039 # include <mln/data/compute.hh>
00040 # include <mln/accu/stat/max.hh>
00041 
00042 
00043 namespace mln
00044 {
00045 
00046   namespace labeling
00047   {
00048 
00049     // Forward declaration.
00050     namespace colorize_
00051     {
00052       extern unsigned min_value;
00053       extern unsigned max_value;
00054     }
00055 
00056 
00059 
00069     template <typename V, typename L>
00070     mln_ch_value(L, V)
00071     colorize(const V& value,
00072              const Image<L>& labeled_image,
00073              const mln_value(L)& nlabels);
00074 
00075 
00077     //
00078     template <typename V, typename L>
00079     mln_ch_value(L, V)
00080     colorize(const V& value,
00081              const Image<L>& labeled_image);
00082 
00083 
00085     //
00086     template <typename L>
00087     mln_ch_value(L, mln::value::rgb8)
00088     colorize(const Image<L>& input,
00089              const mln_value(L)& nlabels);
00090 
00091 
00092 # ifndef MLN_INCLUDE_ONLY
00093 
00094     namespace colorize_
00095     {
00096       unsigned min_value = 20;
00097       unsigned max_value = 220;
00098     }
00099 
00100 
00101     namespace internal
00102     {
00103 
00104       unsigned random_number()
00105       {
00106         static unsigned last = 1;
00107 
00108         last = (323 * last + 6603) % 1025;
00109 
00110         return colorize_::min_value + last % colorize_::max_value;
00111       }
00112 
00113 
00114       // No random color generator is available for the value type V.
00115       template <typename V>
00116       V random_color(const V&);
00117 
00118 
00119       template <unsigned n>
00120       mln::value::rgb<n>
00121       random_color(const mln::value::rgb<n>&)
00122       {
00123         // Make sure the numbers are generated in the same order
00124         // whatever the compiler used.
00125         // For instance, ICC does not compute function arguments in
00126         // the same order as GCC does.
00127         unsigned
00128           red = random_number(),
00129           green = random_number(),
00130           blue = random_number();
00131         return mln::value::rgb<n>(red,
00132                                   green,
00133                                   blue);
00134       }
00135 
00136     }
00137 
00138     template <typename V, typename L>
00139     inline
00140     mln_ch_value(L, V)
00141     colorize(const V& value,
00142              const Image<L>& input,
00143              const mln_value(L)& nlabels)
00144     {
00145       trace::entering("labeling::colorize");
00146       mln_precondition(exact(input).is_valid());
00147       // FIXME: check that V is a color type.
00148       // FIXME: we want to be sure that this is a label.
00149       // mlc_is_a(mln_value(L), mln::value::Symbolic)::check();
00150       (void) value;
00151 
00152       unsigned label_count = static_cast<unsigned>(nlabels) + 1;
00153       static fun::i2v::array<V> f(0);
00154       int diff_size = f.size() - label_count;
00155       if (diff_size < 0)
00156       {
00157         srand(1);
00158         f.resize(label_count);
00159         unsigned i = f.size() + diff_size;
00160         // We want to treat comp 0 differently since it is the background.
00161         if (i == 0)
00162         {
00163           i = 1;
00164           f(0) = literal::black;
00165         }
00166         for (; i < f.size(); ++i)
00167           f(i) = internal::random_color(value);
00168       }
00169       mln_assertion(f.size() >= (label_count));
00170       mln_ch_value(L, V) output = data::transform(input, f);
00171 
00172       trace::exiting("labeling::colorize");
00173       return output;
00174     }
00175 
00176     template <typename V, typename L>
00177     inline
00178     mln_ch_value(L, V)
00179     colorize(const V& value,
00180              const Image<L>& input)
00181     {
00182       trace::entering("labeling::colorize");
00183       mln_precondition(exact(input).is_valid());
00184 
00185       accu::stat::max<mln_value(L)> accu;
00186       mln_value(L) nlabels = data::compute(accu, input);
00187 
00188       mln_ch_value(L,V) output = colorize(value, input, nlabels);
00189 
00190       trace::exiting("labeling::colorize");
00191       return output;
00192     }
00193 
00194 
00195     template <typename L>
00196     inline
00197     mln_ch_value(L, mln::value::rgb8)
00198     colorize(const Image<L>& input,
00199              const mln_value(L)& nlabels)
00200     {
00201       return colorize(mln::value::rgb8(), input, nlabels);
00202     }
00203 
00204 
00205 # endif // ! MLN_INCLUDE_ONLY
00206 
00207   } // end of namespace mln::labeling
00208 
00209 } // end of namespace mln
00210 
00211 
00212 #endif // ! MLN_LABELING_COLORIZE_HH

Generated on Thu Sep 8 2011 18:31:39 for Milena (Olena) by  doxygen 1.7.1