DGtal  1.0.0
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  myITKImagePointer->FillBuffer(0);
79  }
80 
81  template <typename TDomain, typename TValue>
82  ImageContainerByITKImage<TDomain, TValue>::ImageContainerByITKImage(const ITKImagePointer &aRef)
83  : myITKImagePointer(aRef)
84  {
85  updateDomain();
86  }
87 
88  ///////////////////////////////////////////////////////////////////////////////
89  // Interface - public :
90 
91  template <typename TDomain, typename TValue>
92  void
93  ImageContainerByITKImage<TDomain, TValue>::updateDomain()
94  {
95  const typename ITKImage::RegionType region = myITKImagePointer->GetLargestPossibleRegion();
96  const typename ITKImage::IndexType start = region.GetIndex();
97  const typename ITKImage::SizeType size = region.GetSize();
98 
99  Point lowerBound;
100  Point upperBound;
101  for (Dimension k = 0; k < dimension; k++)
102  {
103  lowerBound[k] = start[k];
104  upperBound[k] = start[k]+size[k]-1;
105  }
106 
107  myDomain = TDomain(lowerBound, upperBound);
108  }
109 
110  /**
111  * Get the value of an image at a given position.
112  *
113  * @param aPoint position in the image.
114  * @return the value at aPoint.
115  */
116  template <typename TDomain, typename TValue>
117  inline
118  TValue
119  ImageContainerByITKImage<TDomain, TValue>::operator()(const Point &aPoint) const
120  {
121  typename ITKImage::IndexType p;
122  for (Dimension k = 0; k < dimension; k++)
123  p[k] = aPoint[k];
124  return myITKImagePointer->GetPixel(p);
125  }
126 
127  /**
128  * Get the value of an image at a given position.
129  *
130  * @param aPoint position in the image.
131  * @return the value at aPoint.
132  */
133  template <typename TDomain, typename TValue>
134  inline
135  TValue
136  ImageContainerByITKImage<TDomain, TValue>::operator()(const ConstIterator &it) const
137  {
138  return it.Get();
139  }
140 
141  /**
142  * Get the value of an image at a given position.
143  *
144  * @param aPoint position in the image.
145  * @return the value at aPoint.
146  */
147  template <typename TDomain, typename TValue>
148  inline
149  TValue
150  ImageContainerByITKImage<TDomain, TValue>::operator()(const Iterator &it) const
151  {
152  return it.Get();
153  }
154 
155  template <typename TDomain, typename TValue>
156  inline
157  DGtal::ImageContainerByITKImage<TDomain,TValue>&
158  DGtal::ImageContainerByITKImage<TDomain,TValue>
159  ::operator=(const ImageContainerByITKImage& other)
160  {
161  if (this != &other)
162  {
163  myITKImagePointer = other.myITKImagePointer;
164  updateDomain();
165  }
166  return *this;
167  }
168 
169  template <typename Domain, typename T>
170  inline
171  void
172  ImageContainerByITKImage<Domain, T>::setValue(const Point &aPoint, const T &V)
173  {
174  typename ITKImage::IndexType p;
175  for (Dimension k = 0; k < dimension; k++)
176  p[k] = aPoint[k];
177  myITKImagePointer->SetPixel(p, V);
178  }
179 
180  template <typename Domain, typename T>
181  inline
182  void
183  ImageContainerByITKImage<Domain, T>::setValue(Iterator &it, const T &V)
184  {
185  it.Set(V);
186  }
187 
188  /**
189  * Writes/Displays the object on an output stream.
190  * @param out the output stream where the object is written.
191  */
192  template<typename TDomain, typename TValue>
193  inline
194  void
195  ImageContainerByITKImage<TDomain, TValue>::selfDisplay ( std::ostream & out ) const
196  {
197  typename ITKImage::RegionType region = myITKImagePointer->GetLargestPossibleRegion();
198 
199  out << "[ImageContainerByITKImage] valuetype=" << sizeof(TValue) << "bytes "
200  << "domain=" << this->domain() << " "
201  << "refcount=" << myITKImagePointer->GetReferenceCount() << " "
202  << "region=" << region.GetIndex() << "/" << region.GetSize();
203  }
204 
205  ///////////////////////////////////////////////////////////////////////////////
206  // Implementation of inline functions //
207 
208  template<typename TDomain, typename TValue>
209  inline
210  std::ostream&
211  operator<< ( std::ostream & out,
212  const ImageContainerByITKImage<TDomain, TValue> & object )
213  {
214  object.selfDisplay( out );
215  return out;
216  }
217 }
218 // //
219 ///////////////////////////////////////////////////////////////////////////////
220 
221