image_array1d.hh

00001 // Copyright (C) 2001, 2003, 2004  EPITA Research and Development Laboratory
00002 //
00003 // This file is part of the Olena Library.  This library is free
00004 // software; you can redistribute it and/or modify it under the terms
00005 // of the GNU General Public License version 2 as published by the
00006 // Free Software Foundation.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU General Public License
00014 // along with this library; see the file COPYING.  If not, write to
00015 // the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
00016 // MA 02111-1307, USA.
00017 //
00018 // As a special exception, you may use this file as part of a free
00019 // software library 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
00022 // produce an executable, this file does not by itself cause the
00023 // resulting executable to be covered by the GNU General Public
00024 // License.  This exception does not however invalidate any other
00025 // reasons why the executable file might be covered by the GNU General
00026 // Public License.
00027 
00028 #ifndef OLENA_CORE_IMPL_IMAGE_ARRAY1D_HH
00029 # define OLENA_CORE_IMPL_IMAGE_ARRAY1D_HH
00030 
00031 # include <oln/core/impl/image_array.hh>
00032 # include <oln/core/image1d_size.hh>
00033 # include <oln/core/point1d.hh>
00034 
00035 namespace oln {
00036 
00037 
00039 
00040   template<class T>
00041   void
00042   pretreat_1d_data_(T*& buffer, T*& buffer_, const image1d_size& s)
00043   {
00044     precondition(s.ncols() > 0 && s.border() >= 0);
00045     buffer_ = buffer + s.border();
00046   }
00047 
00048 
00049   namespace impl {
00050     template<class T>
00051     class image_array1d;
00052   } // end of impl
00053 
00060   template<class T>
00061   struct impl_traits<impl::image_array1d<T> >: public impl_traits<impl::image_array<T, impl::image_array1d<T> > >
00062   {
00063     enum { dim = 1 };
00064     typedef point1d point_type;
00065     typedef image1d_size size_type;
00066     typedef T value_type;
00067   };
00068 
00069   namespace impl {
00070 
00071 
00077     template<class T>
00078     class image_array1d :
00079       public image_array<T, image_array1d<T> >
00080     {
00081 
00082     public:
00083 
00084       typedef image_array1d<T> self_type;
00085       typedef image_array1d<T> exact_type;
00086 
00087       typedef typename impl_traits<exact_type>::point_type point_type;
00088       typedef typename impl_traits<exact_type>::value_type value_type;
00089       typedef typename impl_traits<exact_type>::size_type size_type;
00090 
00091       typedef image_array<T, image_array1d<T> > super_type;
00092 
00093 
00094       friend class image_impl<image_array1d<T> >;
00095       friend class image_array<T, image_array1d<T> >;
00096 
00097       image_array1d(const size_type& s): super_type(s)
00098       {
00099         pretreat_1d_data_(this->buffer_, buffer__, s);
00100       }
00101 
00102       image_array1d() {}
00103 
00104       ~image_array1d() {}
00105 
00106     protected:
00107 
00109 
00110       bool
00111       hold_(const point_type& p) const
00112       {
00113         return
00114           p.col() >= 0 &&
00115           p.col() < this->size_.ncols();
00116       }
00117 
00119 
00120       bool
00121       hold_large_(const point_type& p) const
00122       {
00123         return
00124           p.col() >= - this->size_.border() &&
00125           p.col() < this->size_.ncols() + this->size_.border();
00126       }
00127 
00128 
00130       value_type&
00131       at_(const point_type& p)
00132       {
00133         return at_(p.col());
00134       }
00135 
00137       value_type&
00138       at_(coord col)
00139       {
00140         invariant(this->buffer_ != 0);
00141         precondition_hold_large(point_type(col));
00142         return buffer__[col];
00143       }
00144 
00145 
00147       size_t
00148       len_(const size_type& s) const
00149       {
00150         coord ncols_eff = s.ncols() + 2 * s.border();
00151         return size_t(ncols_eff);
00152       }
00153 
00154       // borders
00155 
00159       void
00160       border_reallocate_and_copy_(coord new_border, bool copy_border)
00161       {
00162         T* buffer = 0;
00163         // first allocate
00164         allocate_data_(buffer, len_(size_type(this->size_.ncols(), new_border)));
00165         // move data
00166         coord border = this->size_.border();
00167         if (border > new_border)
00168           border = new_border;
00169         coord src_min_col = copy_border ? -border : 0;
00170         coord src_ncols = this->size_.ncols() + (copy_border ? (border * 2) : 0);
00171         memcpy(buffer + new_border, &at_(src_min_col),
00172                src_ncols * sizeof(T));
00173 
00174         // then replace
00175         this->size_.border() = new_border;
00176         pretreat_1d_data_(buffer, buffer__,
00177                           size_type(this->size_.ncols(), new_border));
00178         desallocate_data_(this->buffer_);
00179         this->buffer_ = buffer;
00180 
00181       }
00182 
00188       void
00189       border_replicate_(void)
00190       {
00191         for (coord j = - this->size_.border(); j; ++j)
00192           {
00193             at_(j) = at_(0);
00194             at_(this->size_.ncols() - j - 1) = at_(this->size_.ncols() - 1);
00195           }
00196       }
00197 
00202       void
00203       border_mirror_(void)
00204       {
00205         for (coord j = - this->size_.border(); j; ++j)
00206           {
00207             at_(j) = at_(-j);
00208             at_(this->size_.ncols() - j - 1) = at_(this->size_.ncols() + j - 1);
00209           }
00210       }
00211 
00213 
00214       void
00215       border_assign_(value_type val)
00216       {
00217         for (coord j = - this->size_.border(); j; ++j)
00218           {
00219             at_(j) = val;
00220             at_(this->size_.ncols() - j - 1) = val;
00221           }
00222       }
00223 
00224     private:
00225 
00226       T* buffer__;
00227 
00228     };
00229 
00230   } // end of namespace impl
00231 
00232 } // end of namespace oln
00233 
00234 #endif // ! OLENA_CORE_IMPL_IMAGE_ARRAY1D_HH

Generated on Thu Apr 15 20:13:11 2004 for Olena by doxygen 1.3.6-20040222