Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
cast_image.hh
1 // Copyright (C) 2007, 2009, 2011 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 MLN_CORE_IMAGE_VMORPH_CAST_IMAGE_HH
28 # define MLN_CORE_IMAGE_VMORPH_CAST_IMAGE_HH
29 
36 
37 # include <mln/core/internal/image_value_morpher.hh>
38 # include <mln/value/set.hh>
39 # include <mln/value/cast.hh>
40 
41 
42 namespace mln
43 {
44 
45  // Fwd decl.
46  template <typename T, typename I> struct cast_image_;
47 
48 
49  namespace internal
50  {
52  template <typename T, typename I>
53  struct data< cast_image_<T,I> >
54  {
55  data(const I& ima);
56  const I& ima_;
57  };
58 
59  } // end of namespace mln::internal
60 
61 
62 
63  namespace trait
64  {
65 
66  template <typename T, typename I, typename value_io>
67  struct cast_image_trait_selector :
68  default_image_morpher< I, T, cast_image_<T,I> >
69  {
70  typedef trait::image::vw_io::none vw_io;
71  typedef trait::image::vw_set::none vw_set;
72  };
73 
74  template <typename T, typename I>
75  struct cast_image_trait_selector<T, I, trait::image::vw_io::read> :
76  default_image_morpher< I, T, cast_image_<T,I> >
77  {
78  typedef trait::image::vw_io::read vw_io;
79  };
80 
81  template <typename T, typename I>
82  struct cast_image_trait_selector<T, I, trait::image::vw_io::read_write> :
83  default_image_morpher< I, T, cast_image_<T,I> >
84  {
85  typedef trait::image::vw_io::read vw_io;
86  };
87 
88  template <typename T, typename I>
89  struct image_< cast_image_<T,I> > :
90  cast_image_trait_selector<T, I, mln_trait_image_vw_io(I)>
91  {
92  private:
93  typedef mlc_equal(mln_trait_value_quant(T),
94  trait::value::quant::high) is_high_quant_;
95  public:
96  typedef mlc_if(is_high_quant_,
97  trait::image::quant::high,
98  trait::image::quant::low) quant;
99 
100  typedef trait::image::category::value_morpher category;
101  typedef trait::image::value_io::read_only value_io;
102  typedef trait::image::pw_io::read pw_io;
103  typedef trait::image::value_access::indirect value_access;
104 
106  typedef trait::image::ext_domain::none ext_domain;
107  };
108 
109  } // end of namespace mln::trait
110 
111 
112 
117  template <typename T, typename I>
118  struct cast_image_ :
119  public internal::image_value_morpher< I, T, cast_image_<T,I> >
120  {
122  typedef T value;
123 
125  typedef T rvalue;
126 
128  typedef T lvalue;
129 
131  typedef cast_image_< tag::value_<T>, tag::image_<I> > skeleton;
132 
134  cast_image_(const Image<I>& ima);
135 
137  void init_(const Image<I>& ima);
138 
140  T operator()(const mln_psite(I)& p) const;
141 
143  T operator()(const mln_psite(I)& p);
144  };
145 
146 
147  template <typename T, typename I>
148  cast_image_<T,I>
149  cast_image(const Image<I>& ima)
150  {
151  mln_precondition(exact(ima).is_valid());
152  cast_image_<T,I> tmp(ima);
153  return tmp;
154  }
155 
156 
157 # ifndef MLN_INCLUDE_ONLY
158 
159 
160  // internal::data< cast_image_<T,I> >
161 
162  namespace internal
163  {
164 
165  template <typename T, typename I>
166  inline
167  data< cast_image_<T,I> >::data(const I& ima)
168  : ima_(ima)
169  {
170  }
171 
172  } // end of namespace mln::internal
173 
174  // cast_image_<T,I>
175 
176  template <typename T, typename I>
177  inline
178  cast_image_<T,I>::cast_image_(const Image<I>& ima)
179  {
180  mln_precondition(exact(ima).is_valid());
181  this->data_ = new internal::data< cast_image_<T,I> >(exact(ima));
182  }
183 
184  template <typename T, typename I>
185  inline
186  void
187  cast_image_<T,I>::init_(const Image<I>& ima)
188  {
189  mln_precondition(exact(ima).is_valid());
190  this->data_ = new internal::data<cast_image_<T,I> >(exact(ima));
191  }
192 
193  template <typename T, typename I>
194  inline
195  T
196  cast_image_<T,I>::operator()(const mln_psite(I)& p) const
197  {
198  mln_precondition(this->data_->ima_.has(p));
199  return mln::value::cast<T>( this->data_->ima_(p) );
200  }
201 
202  template <typename T, typename I>
203  inline
204  T
205  cast_image_<T,I>::operator()(const mln_psite(I)& p)
206  {
207  return mln::value::cast<T>( this->data_->ima_(p) );
208  }
209 
210 # endif // ! MLN_INCLUDE_ONLY
211 
212 } // end of namespace mln
213 
214 
215 #endif // ! MLN_CORE_IMAGE_VMORPH_CAST_IMAGE_HH