$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
safe.hh
1 // Copyright (C) 2007, 2008, 2009, 2011, 2012, 2013 EPITA Research and
2 // Development 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_IMORPH_SAFE_HH
28 # define MLN_CORE_IMAGE_IMORPH_SAFE_HH
29 
36 
37 # include <mln/core/internal/image_identity.hh>
38 
39 
40 
41 namespace mln
42 {
43 
44  // Forward declaration.
45  template <typename I> class safe_image;
46 
47 
48  namespace internal
49  {
50 
54  template <typename I>
55  struct data< safe_image<I> >
56  {
57  data(I& ima, const mln_value(I)& default_value);
58 
59  I ima_;
60  mln_value(I) default_value_;
61  };
62 
63  } // end of namespace mln::internal
64 
65 
66  namespace trait
67  {
68 
69  template <typename I>
70  struct image_< safe_image<I> > : image_< I > // Same as I except...
71  {
72  // ...these changes.
73  typedef trait::image::category::identity_morpher category;
74  typedef mln_internal_trait_image_speed_from(I) speed; // Un-fastest.
75  };
76 
77  } // end of namespace mln::trait
78 
79 
80 
84  //
85  template <typename I>
86  class safe_image : public internal::image_identity< I, mln_domain(I), safe_image<I> >
87  {
88  public:
89 
92 
93  safe_image();
94  safe_image(I& ima);
95  safe_image(I& ima, const mln_value(I)& default_value);
96 
98  // Initialize an empty image.
99  void init_(I& ima, const mln_value(I)& default_value);
101 
102  mln_rvalue(I) operator()(const mln_psite(I)& p) const;
103 
104  mln_morpher_lvalue(I) operator()(const mln_psite(I)& p);
105 
107  operator safe_image<const I>() const;
108  };
109 
110 
111 
112  template <typename I>
113  safe_image<I> safe(Image<I>& ima,
114  mln_value(I) default_value = mln_value(I)());
115 
116  template <typename I>
117  safe_image<const I> safe(const Image<I>& ima,
118  mln_value(I) default_value = mln_value(I)());
119 
120 
121 
122 # ifndef MLN_INCLUDE_ONLY
123 
124  // internal::data< safe_image<I,S> >
125 
126  namespace internal
127  {
128 
129  template <typename I>
130  inline
131  data< safe_image<I> >::data(I& ima, const mln_value(I)& default_value)
132  : ima_(ima),
133  default_value_(default_value)
134  {
135  }
136 
137  } // end of namespace mln::internal
138 
139  // safe_image<I>
140 
141  template <typename I>
142  inline
143  safe_image<I>::safe_image()
144  {
145  }
146 
147  template <typename I>
148  inline
149  safe_image<I>::safe_image(I& ima, const mln_value(I)& default_value)
150  {
151  mln_precondition(ima.is_valid());
152  init_(ima, default_value);
153  }
154 
155  template <typename I>
156  inline
157  safe_image<I>::safe_image(I& ima)
158  {
159  mln_precondition(ima.is_valid());
160  init_(ima, mln_value(I)());
161  }
162 
163  template <typename I>
164  inline
165  void
166  safe_image<I>::init_(I& ima, const mln_value(I)& default_value)
167  {
168  mln_precondition(! this->is_valid());
169  mln_precondition(ima.is_valid());
170  this->data_ = new internal::data< safe_image<I> >(ima, default_value);
171  }
172 
173  template <typename I>
174  inline
175  mln_rvalue(I)
176  safe_image<I>::operator()(const mln_psite(I)& p) const
177  {
178  mln_precondition(this->is_valid());
179  if (! this->has(p))
180  return this->data_->default_value_;
181  return this->data_->ima_(p);
182  }
183 
184  template <typename I>
185  inline
186  mln_morpher_lvalue(I)
187  safe_image<I>::operator()(const mln_psite(I)& p)
188  {
189  mln_precondition(this->is_valid());
190  static mln_value(I) forget_it_;
191  if (this->has(p))
192  return this->data_->ima_(p);
193  else
194  // A copy of data_->default_value_ is returned.
195  return forget_it_ = this->data_->default_value_;
196  }
197 
198  template <typename I>
199  inline
200  safe_image<I>::operator safe_image<const I>() const
201  {
202  safe_image<const I> tmp(this->data_->ima_, this->data_->default_value_);
203  return tmp;
204  }
205 
206  // safe
207 
208  template <typename I>
209  inline
210  safe_image<I> safe(Image<I>& ima,
211  mln_value(I) default_value)
212  {
213  safe_image<I> tmp(exact(ima), default_value);
214  return tmp;
215  }
216 
217  template <typename I>
218  inline
219  safe_image<const I> safe(const Image<I>& ima,
220  mln_value(I) default_value)
221  {
222  safe_image<const I> tmp(exact(ima), default_value);
223  return tmp;
224  }
225 
226 # endif // ! MLN_INCLUDE_ONLY
227 
228 } // end of namespace mln
229 
230 
231 #endif // ! MLN_CORE_IMAGE_IMORPH_SAFE_HH