Imagine++
Image.h
1 // ===========================================================================
2 // Imagine++ Libraries
3 // Copyright (C) Imagine
4 // For detailed information: http://imagine.enpc.fr/software
5 // ===========================================================================
6 
7 #include "PixelTraits.h"
8 #include "Interpol.h"
9 #include "Border.h"
10 
11 namespace Imagine {
14 
24  template <typename T, int dim=2> class Image : public MultiArray<T,dim> {
25  typedef MultiArray<T,dim> Base;
26 
27  public:
32  typedef typename PixelTraits<T>::scalar_type scalar_type;
34  typedef typename Base::iterator iterator;
37 
43  Image() : Base() {}
50  explicit Image(const Coords<dim>& sz) : Base(sz) {}
57  Image(int w, int h) : Base(w,h) {}
64  Image(int w, int h, int d) : Base(w,h,d) {}
76  Image(T* ptr, const Coords<dim>& sz,bool handleDelete=false) : Base(ptr,sz,handleDelete) {}
88  Image(T* ptr, int w,int h,bool handleDelete=false) : Base(ptr,w,h,handleDelete) {}
99  Image(T* ptr, int w,int h,int d,bool handleDelete=false) : Base(ptr,w,h,d,handleDelete) {}
106  Image(const Base& I) : Base(I) {}
115  template <typename T2> Image(const MultiArray<T2,dim> &I) : Base(I) {}
118  virtual ~Image() {}
126  Image& operator=(const Base& I) { Base::operator = (I); return *this; }
136  template <typename T2> Image& operator= (const MultiArray<T2,dim> &I) {
138  return *this;
139  }
146  Image clone() const { return Base::clone(); }
154  Image& fill(T x) { Base::fill(x); return *this; }
163  Image getSubImage(const Coords<dim> &offset, const Coords<dim> &sz) const {
164  return this->getSubArray(offset,sz);
165  }
166 
175  Image getSubImage(int x,int y,int w,int h) const {
176  return this->getSubArray(Coords<2>(x,y),Coords<2>(w,h));
177  }
186  Image getSubImage(int x,int y,int z,int w,int h,int d) const {
187  return this->getSubArray(Coords<3>(x,y,z),Coords<3>(w,h,d));
188  }
198  template <typename V> typename PixelTraits<T>::template CastPixel<V>::value_type interpolate(const FVector<V,dim>& c) const { return Imagine::interpolate(*this,c); }
200  template <typename V> typename PixelTraits<T>::template CastPixel<V>::value_type interpolate(V x, V y) const { return Imagine::interpolate(*this,FVector<V,2>(x,y)); }
202  template <typename V> typename PixelTraits<T>::template CastPixel<V>::value_type interpolate(V x, V y, V z) const { return Imagine::interpolate(*this,FVector<V,3>(x,y,z)); }
210  T neumann(const Coords<dim>& c) const { return NeumannBorder<T,dim>()(*this,c); }
212  T neumann(int x, int y) const { return NeumannBorder<T,2>()(*this,Coords<2>(x,y)); }
214  T neumann(int x, int y, int z) const { return NeumannBorder<T,3>()(*this,Coords<3>(x,y,z)); }
223  T dirichlet(const Coords<dim>& c, T out= T(0)) const { return DirichletBorder<T,dim>(out)(*this,c); }
225  T dirichlet(int x, int y, T out= T(0)) const { return DirichletBorder<T,2>(out)(*this,Coords<2>(x,y)); }
227  T dirichlet(int x, int y, int z, T out= T(0)) const { return DirichletBorder<T,3>(out)(*this,Coords<3>(x,y,z)); }
235  T mirror(const Coords<dim>& c) const { return MirrorBorder<T,dim>()(*this,c); }
237  T mirror(int x, int y) const { return MirrorBorder<T,2>()(*this,Coords<2>(x,y)); }
239  T mirror(int x, int y, int z) const { return MirrorBorder<T,3>()(*this,Coords<3>(x,y,z)); }
247  T invMirror(const Coords<dim>& c) const { return InvMirrorBorder<T,dim>()(*this,c); }
249  T invMirror(int x, int y) const { return InvMirrorBorder<T,2>()(*this,Coords<2>(x,y)); }
251  T invMirror(int x, int y, int z) const { return InvMirrorBorder<T,3>()(*this,Coords<3>(x,y,z)); }
252 
253 
255 
263  friend inline T sum(const Image& I) {
264  return std::accumulate(I.begin(), I.end(), T(scalar_type(0)));
265  }
273  friend inline scalar_type norm2(const Image& I) {
274  scalar_type n = 0;
275  for (const_iterator it = I.begin() ; it != I.end() ; ++it) {
276  n += (*it) * (*it);
277  }
278  return n;
279  }
287  friend inline scalar_type norm(const Image& I) {
288  assert( !std::numeric_limits<T>::is_integer );
289  return ::sqrt(norm2(I));
290  }
297  Image operator-() const {
298  Image C(this->sizes());
299  iterator it3 = C.begin();
300  for (const_iterator it1 = this->begin() ; it1 != this->end() ; ++it1, ++it3)
301  *it3 = - *it1;
302  return C;
303  }
311  Image operator+(const Image& B) const {
312  assert(this->sizes()==B.sizes());
313  Image C(this->sizes());
314  iterator it3 = C.begin();
315  for (const_iterator it1 = this->begin(), it2 = B.begin() ; it1 != this->end() ; ++it1, ++it2, ++it3)
316  *it3 = *it1 + *it2;
317  return C;
318  }
326  Image& operator+=(const Image& B) {
327  assert(this->sizes()==B.sizes());
328  iterator it1 = this->begin();
329  for (const_iterator it2 = B.begin() ; it2 != B.end() ; ++it1, ++it2)
330  *it1+=*it2;
331  return *this;
332  }
340  Image operator-(const Image& B) const {
341  assert(this->sizes()==B.sizes());
342  Image C(this->sizes());
343  iterator it3 = C.begin();
344  for (const_iterator it1 = this->begin(), it2 = B.begin() ; it1 != this->end() ; ++it1, ++it2, ++it3)
345  *it3 = *it1 - *it2;
346  return C;
347  }
355  Image& operator-=(const Image& B) {
356  assert(this->sizes()==B.sizes());
357  iterator it1 = this->begin();
358  for (const_iterator it2 = B.begin() ; it2 != B.end() ; ++it1, ++it2)
359  *it1-=*it2;
360  return *this;
361  }
369  Image operator+(T v) const {
370  Image C(this->sizes());
371  iterator it3 = C.begin();
372  for (const_iterator it1 = this->begin() ; it1 != this->end() ; ++it1, ++it3)
373  *it3 = *it1 + v;
374  return C;
375  }
384  friend inline Image operator+(T v, const Image& I) { return I+v; }
393  for (iterator it = this->begin() ; it != this->end() ; ++it)
394  *it+=v;
395  return *this;
396  }
404  Image operator-(T v) const {
405  Image C(this->sizes());
406  iterator it3 = C.begin();
407  for (const_iterator it1 = this->begin() ; it1 != this->end() ; ++it1, ++it3)
408  *it3 = *it1 - v;
409  return C;
410  }
419  friend inline Image operator-(T v, const Image& I) { return (-I)+v; }
428  for (iterator it = this->begin() ; it != this->end() ; ++it)
429  *it-=v;
430  return *this;
431  }
439  Image operator*(scalar_type s) const {
440  Image C(this->sizes());
441  iterator it3 = C.begin();
442  for (const_iterator it1 = this->begin() ; it1 != this->end() ; ++it1, ++it3)
443  *it3 = *it1 * s;
444  return C;
445  }
454  friend inline Image operator*(scalar_type s, const Image& I) { return I*s; }
462  Image& operator*=(scalar_type s){
463  for (iterator it = this->begin() ; it != this->end() ; ++it)
464  *it*=s;
465  return *this;
466  }
474  Image operator/(scalar_type s) const {
475  Image C(this->sizes());
476  iterator it3 = C.begin();
477  for (const_iterator it1 = this->begin() ; it1 != this->end() ; ++it1, ++it3)
478  *it3 = *it1 / s;
479  return C;
480  }
488  Image& operator/=(scalar_type s){
489  for (iterator it = this->begin() ; it != this->end() ; ++it)
490  *it/=s;
491  return *this;
492  }
501  assert(this->sizes()==B.sizes());
502  Image C(this->sizes());
503  iterator it3 = C.begin();
505  for (const_iterator it1 = this->begin() ; it1 != this->end() ; ++it1, ++it2, ++it3)
506  *it3 = *it1 * *it2;
507  return C;
508  }
517  assert(this->sizes()==B.sizes());
518  iterator it1 = this->begin();
519  for (typename Image<scalar_type,dim>::const_iterator it2 = B.begin() ; it2 != B.end() ; ++it1, ++it2)
520  *it1*=*it2;
521  return *this;
522  }
531  assert(this->sizes()==B.sizes());
532  Image C(this->sizes());
533  iterator it3 = C.begin();
535  for (const_iterator it1 = this->begin() ; it1 != this->end() ; ++it1, ++it2, ++it3)
536  *it3 = *it1 / *it2;
537  return C;
538  }
547  assert(this->sizes()==B.sizes());
548  iterator it1 = this->begin();
549  for (typename Image<scalar_type,dim>::const_iterator it2 = B.begin() ; it2 != B.end() ; ++it1, ++it2)
550  *it1/=*it2;
551  return *this;
552  }
553  };
554 
556 }
T mirror(const Coords< dim > &c) const
Mirror access.
Definition: Image.h:235
Coordinates.
Definition: Coords.h:16
Image operator*(scalar_type s) const
Scalar multiplication.
Definition: Image.h:439
Image & operator/=(scalar_type s)
In place scalar division.
Definition: Image.h:488
Vector of fixed size.
Definition: FVector.h:17
Image(const Coords< dim > &sz)
Constructor (known size).
Definition: Image.h:50
Image operator-(T v) const
Constant substraction.
Definition: Image.h:404
Image(int w, int h, int d)
Constructor (known size, 3D).
Definition: Image.h:64
Image operator+(const Image &B) const
Addition.
Definition: Image.h:311
PixelTraits< T >::scalar_type scalar_type
Scalar type.
Definition: Image.h:32
T mirror(int x, int y, int z) const
Mirror access (3D alias).
Definition: Image.h:239
Image operator-(const Image &B) const
Substraction.
Definition: Image.h:340
Base::const_iterator const_iterator
Const iterator type.
Definition: Image.h:36
PixelTraits< T >::template CastPixel< V >::value_type interpolate(V x, V y, V z) const
Interpolation with automated return type, 3D alias.
Definition: Image.h:202
Image & operator*=(scalar_type s)
In place scalar multiplication.
Definition: Image.h:462
Image & operator-=(const Image &B)
In place substraction.
Definition: Image.h:355
Image & operator*=(const Image< scalar_type, dim > &B)
In place pointwise scalar multiplication.
Definition: Image.h:516
T dirichlet(int x, int y, T out=T(0)) const
Dirichlet (2D alias).
Definition: Image.h:225
Image & fill(T x)
Filling.
Definition: Image.h:154
Image operator/(scalar_type s) const
Scalar division.
Definition: Image.h:474
MultiArray clone() const
Cloning.
Definition: MultiArray.h:191
Array of variable size.
Definition: Array.h:20
Image(int w, int h)
Constructor (known size, 2D).
Definition: Image.h:57
friend scalar_type norm(const Image &I)
Euclidean norm.
Definition: Image.h:287
Image & operator=(const Base &I)
Assignment.
Definition: Image.h:126
Image & operator-=(T v)
In place constant substraction.
Definition: Image.h:427
MultiArray & operator=(const MultiArray &A)
Assignment.
Definition: MultiArray.h:175
Image(T *ptr, int w, int h, int d, bool handleDelete=false)
Constructor (pre-allocated 3D).
Definition: Image.h:99
T invMirror(int x, int y, int z) const
Inversed mirror access (3D alias).
Definition: Image.h:251
Image getSubImage(int x, int y, int w, int h) const
Sub image (2D).
Definition: Image.h:175
Image()
Empty constructor.
Definition: Image.h:43
MultiArray getSubArray(const Coords< dim > &offset, const Coords< dim > &sz) const
Sub array.
Definition: MultiArray.h:269
friend T sum(const Image &I)
Sum of pixels.
Definition: Image.h:263
T dirichlet(int x, int y, int z, T out=T(0)) const
Dirichlet (3D alias).
Definition: Image.h:227
Image getSubImage(int x, int y, int z, int w, int h, int d) const
Sub image (3D).
Definition: Image.h:186
Coords< dim > sizes() const
Sizes.
Definition: MultiArray.h:211
friend Image operator-(T v, const Image &I)
Constant substraction.
Definition: Image.h:419
T invMirror(const Coords< dim > &c) const
Inversed mirror access.
Definition: Image.h:247
Image.
Definition: Image.h:24
size_t offset(const Coords< dim > &c) const
Offset.
Definition: MultiArray.h:283
Image(const Base &I)
Copy constructor.
Definition: Image.h:106
Image operator/(const Image< scalar_type, dim > &B) const
Pointwise scalar division.
Definition: Image.h:530
friend scalar_type norm2(const Image &I)
Squared Euclidean norm.
Definition: Image.h:273
friend Image operator+(T v, const Image &I)
Constant addition.
Definition: Image.h:384
Image operator+(T v) const
Constant addition.
Definition: Image.h:369
Image operator-() const
Opposite.
Definition: Image.h:297
Image getSubImage(const Coords< dim > &offset, const Coords< dim > &sz) const
Sub image.
Definition: Image.h:163
friend Image operator*(scalar_type s, const Image &I)
Scalar multiplication.
Definition: Image.h:454
Image(const MultiArray< T2, dim > &I)
Constructor (different type).
Definition: Image.h:115
Image operator*(const Image< scalar_type, dim > &B) const
Pointwise scalar multiplication.
Definition: Image.h:500
T neumann(int x, int y) const
Neumann (2D alias).
Definition: Image.h:212
nD array of variable size.
Definition: MultiArray.h:21
Image & operator+=(T v)
In place constant addition.
Definition: Image.h:392
PixelTraits< T >::template CastPixel< V >::value_type interpolate(V x, V y) const
Interpolation with automated return type, 2D alias.
Definition: Image.h:200
MultiArray & fill(T x)
Filling.
Definition: MultiArray.h:204
T mirror(int x, int y) const
Mirror access (2D alias).
Definition: Image.h:237
T neumann(int x, int y, int z) const
Neumann (3D alias).
Definition: Image.h:214
T invMirror(int x, int y) const
Inversed mirror access (2D alias).
Definition: Image.h:249
Base::iterator iterator
Iterator type.
Definition: Image.h:34
T dirichlet(const Coords< dim > &c, T out=T(0)) const
Dirichlet access.
Definition: Image.h:223
iterator begin()
Begin iterator.
Definition: Array.h:231
Imagine++ namespace.
Definition: Array.h:7
virtual ~Image()
Destructor.
Definition: Image.h:118
Image(T *ptr, const Coords< dim > &sz, bool handleDelete=false)
Constructor (pre-allocated).
Definition: Image.h:76
RED GREEN BLUE color.
Definition: Color.h:26
const T * const_iterator
Const iterator type.
Definition: Array.h:73
Image(T *ptr, int w, int h, bool handleDelete=false)
Constructor (pre-allocated 2D).
Definition: Image.h:88
iterator end()
End iterator.
Definition: Array.h:245
T neumann(const Coords< dim > &c) const
Neumann access.
Definition: Image.h:210
Image & operator+=(const Image &B)
In place Addition.
Definition: Image.h:326
Image & operator/=(const Image< scalar_type, dim > &B)
In place pointwise scalar division.
Definition: Image.h:546
Image clone() const
Cloning.
Definition: Image.h:146
PixelTraits< T >::template CastPixel< V >::value_type interpolate(const FVector< V, dim > &c) const
Interpolation.
Definition: Image.h:198