DGtal  0.9.2
ImageContainerByITKImage.ih
1 /**
2  * This program is free software: you can redistribute it and/or modify
3  * it under the terms of the GNU Lesser General Public License as
4  * published by the Free Software Foundation, either version 3 of the
5  * License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program. If not, see <http://www.gnu.org/licenses/>.
14  *
15  **/
16 
17 /**
18  * @file ImageContainerByITKImage.ih
19  * @author David Coeurjolly (\c david.coeurjolly@liris.cnrs.fr )
20  * @author Pierre Gueth (\c pierre.gueth@liris.cnrs.fr )
21  * Laboratoire d'InfoRmatique en Image et Systèmes d'information - LIRIS (CNRS, UMR 5205), CNRS, France
22  *
23  * @date 2013/10/23
24  *
25  * Implementation of inline methods defined in ImageContainerByITKImage.h
26  *
27  * This file is part of the DGtal library.
28  */
29 
30 
31 //////////////////////////////////////////////////////////////////////////////
32 #include <cstdlib>
33 //////////////////////////////////////////////////////////////////////////////
34 
35 ///////////////////////////////////////////////////////////////////////////////
36 // IMPLEMENTATION of inline methods.
37 ///////////////////////////////////////////////////////////////////////////////
38 
39 ///////////////////////////////////////////////////////////////////////////////
40 // ----------------------- Standard services ------------------------------
41 
42 namespace DGtal
43 {
44  template<typename TDomain, typename TValue>
45  ImageContainerByITKImage<TDomain, TValue>::~ImageContainerByITKImage()
46  {
47  }
48 
49  template <typename TDomain, typename TValue>
50  ImageContainerByITKImage<TDomain, TValue>::ImageContainerByITKImage(const ImageContainerByITKImage& other)
51  : myITKImagePointer(ITKImage::New()), myDomain()
52  {
53  myITKImagePointer = other.getITKImagePointer();
54  updateDomain();
55  }
56 
57  template <typename TDomain, typename TValue>
58  ImageContainerByITKImage<TDomain, TValue>::ImageContainerByITKImage(const TDomain& aDomain)
59  : myITKImagePointer(ITKImage::New()), myDomain(aDomain)
60  {
61  typename ITKImage::IndexType start;
62  typename ITKImage::SizeType size;
63 
64  const Point lowerBound = aDomain.lowerBound();
65  const Point upperBound = aDomain.upperBound();
66  const Point dec = upperBound-lowerBound;
67 
68  for (Dimension k = 0; k < dimension; k++)
69  {
70  start[k] = lowerBound[k];
71  size[k] = dec[k]+1;
72  }
73 
74  const typename ITKImage::RegionType region(start,size);
75 
76  myITKImagePointer->SetRegions(region);
77  myITKImagePointer->Allocate();
78  }
79 
80  template <typename TDomain, typename TValue>
81  ImageContainerByITKImage<TDomain, TValue>::ImageContainerByITKImage(const ITKImagePointer &aRef)
82  : myITKImagePointer(aRef)
83  {
84  updateDomain();
85  }
86 
87  ///////////////////////////////////////////////////////////////////////////////
88  // Interface - public :
89 
90  template <typename TDomain, typename TValue>
91  void
92  ImageContainerByITKImage<TDomain, TValue>::updateDomain()
93  {
94  const typename ITKImage::RegionType region = myITKImagePointer->GetLargestPossibleRegion();
95  const typename ITKImage::IndexType start = region.GetIndex();
96  const typename ITKImage::SizeType size = region.GetSize();
97 
98  Point lowerBound;
99  Point upperBound;
100  for (Dimension k = 0; k < dimension; k++)
101  {
102  lowerBound[k] = start[k];
103  upperBound[k] = start[k]+size[k]-1;
104  }
105 
106  myDomain = TDomain(lowerBound, upperBound);
107  }
108 
109  /**
110  * Get the value of an image at a given position.
111  *
112  * @param aPoint position in the image.
113  * @return the value at aPoint.
114  */
115  template <typename TDomain, typename TValue>
116  inline
117  TValue
118  ImageContainerByITKImage<TDomain, TValue>::operator()(const Point &aPoint) const
119  {
120  typename ITKImage::IndexType p;
121  for (Dimension k = 0; k < dimension; k++)
122  p[k] = aPoint[k];
123  return myITKImagePointer->GetPixel(p);
124  }
125 
126  /**
127  * Get the value of an image at a given position.
128  *
129  * @param aPoint position in the image.
130  * @return the value at aPoint.
131  */
132  template <typename TDomain, typename TValue>
133  inline
134  TValue
135  ImageContainerByITKImage<TDomain, TValue>::operator()(const ConstIterator &it) const
136  {
137  return it.Get();
138  }
139 
140  /**
141  * Get the value of an image at a given position.
142  *
143  * @param aPoint position in the image.
144  * @return the value at aPoint.
145  */
146  template <typename TDomain, typename TValue>
147  inline
148  TValue
149  ImageContainerByITKImage<TDomain, TValue>::operator()(const Iterator &it) const
150  {
151  return it.Get();
152  }
153 
154  template <typename Domain, typename T>
155  inline
156  void
157  ImageContainerByITKImage<Domain, T>::setValue(const Point &aPoint, const T &V)
158  {
159  typename ITKImage::IndexType p;
160  for (Dimension k = 0; k < dimension; k++)
161  p[k] = aPoint[k];
162  myITKImagePointer->SetPixel(p, V);
163  }
164 
165  template <typename Domain, typename T>
166  inline
167  void
168  ImageContainerByITKImage<Domain, T>::setValue(Iterator &it, const T &V)
169  {
170  it.Set(V);
171  }
172 
173  /**
174  * Writes/Displays the object on an output stream.
175  * @param out the output stream where the object is written.
176  */
177  template<typename TDomain, typename TValue>
178  inline
179  void
180  ImageContainerByITKImage<TDomain, TValue>::selfDisplay ( std::ostream & out ) const
181  {
182  typename ITKImage::RegionType region = myITKImagePointer->GetLargestPossibleRegion();
183 
184  out << "[ImageContainerByITKImage] valuetype=" << sizeof(TValue) << "bytes "
185  << "domain=" << this->domain() << " "
186  << "refcount=" << myITKImagePointer->GetReferenceCount() << " "
187  << "region=" << region.GetIndex() << "/" << region.GetSize();
188  }
189 
190  ///////////////////////////////////////////////////////////////////////////////
191  // Implementation of inline functions //
192 
193  template<typename TDomain, typename TValue>
194  inline
195  std::ostream&
196  operator<< ( std::ostream & out,
197  const ImageContainerByITKImage<TDomain, TValue> & object )
198  {
199  object.selfDisplay( out );
200  return out;
201  }
202 }
203 // //
204 ///////////////////////////////////////////////////////////////////////////////
205 
206