$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
image1d.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_IMAGE1D_HH
28 # define MLN_CORE_IMAGE_IMAGE1D_HH
29 
35 
36 # include <mln/core/internal/fixme.hh>
37 # include <mln/core/internal/image_primary.hh>
38 # include <mln/core/alias/box1d.hh>
39 
40 # include <mln/border/thickness.hh>
41 # include <mln/value/set.hh>
42 # include <mln/fun/i2v/all_to.hh>
43 
44 
45 // FIXME:
46 
47 // # include <mln/core/pixter1d.hh>
48 // # include <mln/core/dpoints_pixter.hh>
49 
50 
51 namespace mln
52 {
53 
54  // Forward declaration.
55  template <typename T> struct image1d;
56 
57 
58  namespace internal
59  {
60 
65  template <typename T>
66  struct data< image1d<T> >
67  {
68  data(const box1d& b, unsigned bdr);
69  ~data();
70 
71  T* buffer_;
72  T* array_;
73 
74  box1d b_; // theoretical box
75  unsigned bdr_;
76  box1d vb_; // virtual box, i.e., box including the virtual border
77 
78  void update_vb_();
79  void allocate_();
80  void deallocate_();
81  void swap_ (data< image1d<T> >& other_);
82  void reallocate_(unsigned new_border);
83  };
84 
85  } // end of namespace mln::internal
86 
87 
88 
89  namespace trait
90  {
91 
92  template <typename T>
93  struct image_< image1d<T> > : default_image_< T, image1d<T> >
94  {
95  // misc
96  typedef trait::image::category::primary category;
97  typedef trait::image::speed::fastest speed;
98  typedef trait::image::size::regular size;
99 
100  // value
101  typedef trait::image::vw_io::none vw_io;
102  typedef trait::image::vw_set::none vw_set;
103  typedef trait::image::value_access::direct value_access;
104  typedef trait::image::value_storage::one_block value_storage;
105  typedef trait::image::value_browsing::site_wise_only value_browsing;
106  typedef trait::image::value_alignment::with_grid value_alignment;
107  typedef trait::image::value_io::read_write value_io;
108 
109  // site / domain
110  typedef trait::image::pw_io::read_write pw_io;
111  typedef trait::image::localization::basic_grid localization;
112  typedef trait::image::dimension::one_d dimension;
113 
114  // extended domain
115  typedef trait::image::ext_domain::extendable ext_domain;
116  typedef trait::image::ext_value::multiple ext_value;
117  typedef trait::image::ext_io::read_write ext_io;
118  };
119 
120  } // end of namespace mln::trait
121 
122 
123 
124  // Forward declaration.
125  template <typename T> struct image1d;
126 
127 
135  //
136  template <typename T>
137  struct image1d :
138  public internal::image_primary< T, box1d, image1d<T> >
139  {
141 
143  typedef T value;
144 
146  typedef const T& rvalue;
147 
149  typedef T& lvalue;
150 
153 
154 
156  image1d();
157 
160  image1d(unsigned ninds, unsigned bdr = border::thickness);
161 
163  image1d(const box1d& b, unsigned bdr = border::thickness);
164 
165 
168  void init_(const box1d& b, unsigned bdr = border::thickness);
170 
171 
173  bool has(const point1d& p) const;
174 
176  const box1d& domain() const;
177 
179  const box1d& bbox() const;
180 
182  const box1d& vbbox() const;
183 
185  unsigned border() const;
186 
188  const T& operator()(const point1d& p) const;
189 
191  T& operator()(const point1d& p);
192 
193 
194  // Specific methods:
195  // -----------------
196 
198 
200  const T& at_(def::coord offset) const;
201 
203  T& at_(def::coord offset);
204 
206  unsigned ninds() const;
207 
209 
210 
212 
213  // Give the offset of a point.
214  using super_::offset_of_point;
215 
217  int delta_offset(const dpoint1d& dp) const;
218 
220  point1d point_at_offset(unsigned i) const;
221 
223  const T* buffer() const;
224 
226  T* buffer();
227 
230  const T& element(unsigned i) const;
231 
234  T& element(unsigned i);
235 
237  unsigned nelements() const;
238 
239 
242  void resize_(unsigned new_border);
244 
245  };
246 
247  template <typename T, typename J>
248  void init_(tag::image_t, mln::image1d<T>& target, const J& model);
249 
250 # ifndef MLN_INCLUDE_ONLY
251 
252  // init_
253  template <typename T>
254  inline
255  void init_(tag::border_t, unsigned& b, const image1d<T>& model)
256  {
257  b = model.border();
258  }
259 
260  template <typename T, typename J>
261  inline
262  void init_(tag::image_t, image1d<T>& target, const J& model)
263  {
264  box1d b;
265  init_(tag::bbox, b, model);
266  unsigned bdr;
267  init_(tag::border, bdr, model);
268  target.init_(b, bdr);
269  }
270 
271  // internal::data< image1d<T> >
272 
273  namespace internal
274  {
275 
276  template <typename T>
277  inline
278  data< image1d<T> >::data(const box1d& b, unsigned bdr)
279  : buffer_(0),
280  array_ (0),
281  b_ (b),
282  bdr_ (bdr)
283  {
284  allocate_();
285  }
286 
287  template <typename T>
288  inline
289  data< image1d<T> >::~data()
290  {
291  deallocate_();
292  }
293 
294  template <typename T>
295  inline
296  void
297  data< image1d<T> >::update_vb_()
298  {
299  dpoint1d dp(all_to(bdr_));
300 
301  vb_.pmin() = b_.pmin() - dp;
302  vb_.pmax() = b_.pmax() + dp;
303  }
304 
305  template <typename T>
306  inline
307  void
308  data< image1d<T> >::allocate_()
309  {
310  update_vb_();
311  unsigned
312  ni = vb_.len(0);
313  buffer_ = new T[ni];
314  array_ = buffer_ - vb_.pmin().ind();
315  mln_postcondition(vb_.len(0) == b_.len(0) + 2 * bdr_);
316  }
317 
318  template <typename T>
319  inline
320  void
321  data< image1d<T> >::deallocate_()
322  {
323  if (buffer_)
324  {
325  delete[] buffer_;
326  buffer_ = 0;
327  }
328  }
329 
330 
331  template <typename T>
332  inline
333  void
334  data< image1d<T> >::swap_(data< image1d<T> >& other_)
335  {
336 
337  data< image1d<T> > self_ = *this;
338  *this = other_;
339  other_ = self_;
340 
341  }
342 
343  template <typename T>
344  inline
345  void
346  data< image1d<T> >::reallocate_(unsigned new_border)
347  {
348  data< image1d<T> >& tmp = *(new data< image1d<T> >(this->b_, new_border));
349  this->swap_(tmp);
350  }
351 
352  } // end of namespace mln::internal
353 
354 
355  // image1d<T>
356 
357  template <typename T>
358  inline
359  image1d<T>::image1d()
360  {
361  }
362 
363  template <typename T>
364  inline
365  image1d<T>::image1d(const box1d& b, unsigned bdr)
366  {
367  init_(b, bdr);
368  }
369 
370  template <typename T>
371  inline
372  image1d<T>::image1d(unsigned ninds, unsigned bdr)
373  {
374  mln_precondition(ninds != 0);
375  init_(make::box1d(ninds), bdr);
376  }
377 
378  template <typename T>
379  inline
380  void
381  image1d<T>::init_(const box1d& b, unsigned bdr)
382  {
383  mln_precondition(! this->is_valid());
384  this->data_ = new internal::data< image1d<T> >(b, bdr);
385  }
386 
387  template <typename T>
388  inline
389  const box1d&
390  image1d<T>::domain() const
391  {
392  mln_precondition(this->is_valid());
393  return this->data_->b_;
394  }
395 
396  template <typename T>
397  inline
398  const box1d&
399  image1d<T>::bbox() const
400  {
401  mln_precondition(this->is_valid());
402  return this->data_->b_;
403  }
404 
405  template <typename T>
406  inline
407  const box1d&
408  image1d<T>::vbbox() const
409  {
410  mln_precondition(this->is_valid());
411  return this->data_->vb_;
412  }
413 
414  template <typename T>
415  inline
416  unsigned
417  image1d<T>::border() const
418  {
419  mln_precondition(this->is_valid());
420  return this->data_->bdr_;
421  }
422 
423  template <typename T>
424  inline
425  unsigned
426  image1d<T>::nelements() const
427  {
428  mln_precondition(this->is_valid());
429  return this->data_->vb_.nsites();
430  }
431 
432  template <typename T>
433  inline
434  bool
435  image1d<T>::has(const point1d& p) const
436  {
437  mln_precondition(this->is_valid());
438  return this->data_->vb_.has(p);
439  }
440 
441  template <typename T>
442  inline
443  const T&
444  image1d<T>::operator()(const point1d& p) const
445  {
446  mln_precondition(this->has(p));
447  return this->data_->array_[p.ind()];
448  }
449 
450  template <typename T>
451  inline
452  T&
453  image1d<T>::operator()(const point1d& p)
454  {
455  mln_precondition(this->has(p));
456  return this->data_->array_[p.ind()];
457  }
458 
459  template <typename T>
460  inline
461  const T&
462  image1d<T>::at_(def::coord offset) const
463  {
464  mln_precondition(this->has(point1d(offset)));
465  return this->data_->array_[offset];
466  }
467 
468  template <typename T>
469  inline
470  unsigned
471  image1d<T>::ninds() const
472  {
473  mln_precondition(this->is_valid());
474  return this->data_->b_.len(0);
475  }
476 
477  template <typename T>
478  inline
479  T&
480  image1d<T>::at_(def::coord offset)
481  {
482  mln_precondition(this->has(point1d(offset)));
483  return this->data_->array_[offset];
484  }
485 
486 
487  template <typename T>
488  inline
489  const T&
490  image1d<T>::element(unsigned i) const
491  {
492  mln_precondition(i < nelements());
493  return this->data_->buffer_[i];
494  }
495 
496  template <typename T>
497  inline
498  T&
499  image1d<T>::element(unsigned i)
500  {
501  mln_precondition(i < nelements());
502  return this->data_->buffer_[i];
503  }
504 
505  template <typename T>
506  inline
507  const T*
508  image1d<T>::buffer() const
509  {
510  mln_precondition(this->is_valid());
511  return this->data_->buffer_;
512  }
513 
514  template <typename T>
515  inline
516  T*
517  image1d<T>::buffer()
518  {
519  mln_precondition(this->is_valid());
520  return this->data_->buffer_;
521  }
522 
523  template <typename T>
524  inline
525  int
526  image1d<T>::delta_offset(const dpoint1d& dp) const
527  {
528  mln_precondition(this->is_valid());
529  int o = dp[0];
530  return o;
531  }
532 
533  template <typename T>
534  inline
535  point1d
536  image1d<T>::point_at_offset(unsigned i) const
537  {
538  mln_precondition(i < nelements());
539  def::coord ind = static_cast<def::coord>(i + this->data_->vb_.min_ind());
540  point1d p = point1d(ind);
541  mln_postcondition(& this->operator()(p) == this->data_->buffer_ + i);
542  return p;
543  }
544 
545  template <typename T>
546  inline
547  void
548  image1d<T>::resize_(unsigned new_border)
549  {
550  this->data_->reallocate_(new_border);
551  }
552 
553 # endif // ! MLN_INCLUDE_ONLY
554 
555 } // end of namespace mln
556 
557 
558 
559 # include <mln/core/trait/pixter.hh>
560 # include <mln/core/dpoints_pixter.hh>
561 # include <mln/core/pixter1d.hh>
562 # include <mln/core/w_window.hh>
563 
564 
565 
566 namespace mln
567 {
568 
569  namespace trait
570  {
571 
572  // pixter
573 
574  template <typename T>
575  struct fwd_pixter< image1d<T> >
576  {
578  };
579 
580  template <typename T>
581  struct fwd_pixter< const image1d<T> >
582  {
584  };
585 
586  template <typename T>
587  struct bkd_pixter< image1d<T> >
588  {
589  typedef bkd_pixter1d< image1d<T> > ret;
590  };
591 
592  template <typename T>
593  struct bkd_pixter< const image1d<T> >
594  {
596  };
597 
598  // qixter
599 
600  template <typename T, typename W>
601  struct fwd_qixter< image1d<T>, W >
602  {
604  };
605 
606  template <typename T, typename W>
607  struct fwd_qixter< const image1d<T>, W >
608  {
610  };
611 
612  template <typename T, typename W>
613  struct bkd_qixter< image1d<T>, W >
614  {
616  };
617 
618  template <typename T, typename W>
619  struct bkd_qixter< const image1d<T>, W >
620  {
622  };
623 
624  // nixter
625 
626  template <typename T, typename W>
627  struct fwd_nixter< image1d<T>, W >
628  {
630  };
631 
632  template <typename T, typename W>
633  struct fwd_nixter< const image1d<T>, W >
634  {
636  };
637 
638  template <typename T, typename W>
639  struct bkd_nixter< image1d<T>, W >
640  {
642  };
643 
644  template <typename T, typename W>
645  struct bkd_nixter< const image1d<T>, W >
646  {
648  };
649 
650  } // end of namespace mln::trait
651 
652 } // end of namespace mln
653 
654 # include <mln/make/image.hh>
655 
656 #endif // ! MLN_CORE_IMAGE_IMAGE1D_HH