KWStyle - itkImage.h
 
Matrix View
Description

1 /*=========================================================================
2
3   Program:   Insight Segmentation & Registration Toolkit
4   Module:    $RCSfile: itkImage.h.html,v $
5   Language:  C++
6   Date:      $Date: 2006/01/17 19:15:36 $
7   Version:   $Revision: 1.4 $
8
9   Copyright (c) Insight Software Consortium. All rights reserved.
10   See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
11
12      This software is distributed WITHOUT ANY WARRANTY; without even
13      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14      PURPOSE.  See the above copyright notices for more information.
15
16 =========================================================================*/
17 #ifndef __itkImage_h
18 #define __itkImage_h
19
20 #include "itkImageBase.h"
21 #include "itkImageRegion.h"
22 #include "itkImportImageContainer.h"
23 #include "itkDefaultPixelAccessor.h"
24 #include "itkDefaultPixelAccessorFunctor.h"
25 #include "itkPoint.h"
26 #include "itkContinuousIndex.h"
27 #include "itkFixedArray.h"
28 #include "itkWeakPointer.h"
29 #include "itkNeighborhoodAccessorFunctor.h"
30
31 namespace itk
32 {
33 /** \class Image
34  *  \brief Templated n-dimensional image class.
35  *
36  * Images are templated over a pixel type (modeling the dependent
37  * variables), and a dimension (number of independent variables).  The
38  * container for the pixel data is the ImportImageContainer.
39  *
40  * Within the pixel container, images are modeled as arrays, defined by a
41  * start index and a size.
42  *
43  * There are three sets of meta-data describing an image. These are "Region"
44  * objects that define a portion of an image via a starting index for the image
45  * array and a size. The ivar LargestPossibleRegion defines the size and
46  * starting index of the image dataset. The entire image dataset, however,
47  * need not be resident in memory. The region of the image that is resident in
48  * memory is defined by the "BufferedRegion". The Buffer is a contiguous block
49  * of memory.  The third set of meta-data defines a region of interest, called
50  * the "RequestedRegion". The RequestedRegion is used by the pipeline
51  * execution model to define what a filter is requested to produce.
52  *
53  * [RegionIndex, RegionSize] C [BufferIndex, BufferSize]
54  *                           C [ImageIndex, ImageSize]
55  *
56  * Pixels can be accessed direcly using the SetPixel() and GetPixel()
57  * methods or can be accessed via iterators.  Begin() creates
58  * an iterator that can walk a specified region of a buffer.
59  *
60  * The pixel type may be one of the native types; a Insight-defined
61  * class type such as Vector; or a user-defined type. Note that
62  * depending on the type of pixel that you use, the process objects
63  * (i.e., those filters processing data objects) may not operate on
64  * the image and/or pixel type. This becomes apparent at compile-time
65  * because operator overloading (for the pixel type) is not supported.
66  *
67  * The data in an image is arranged in a 1D array as [][][][slice][row][col]
68  * with the column index varying most rapidly.  The Index type reverses
69  * the order so that with Index[0] = col, Index[1] = row, Index[2] = slice,
70  * ...
71  *
72  * \sa ImageContainerInterface
73  *
74  * \example DataRepresentation/Image/Image1.cxx
75  * \example DataRepresentation/Image/Image2.cxx
76  * \example DataRepresentation/Image/Image2.cxx
77  * \example DataRepresentation/Image/RGBImage.cxx
78  * \example DataRepresentation/Image/VectorImage.cxx
79  *
80  * \ingroup ImageObjects */
81 template <class TPixel, unsigned int VImageDimension=2>
82 class ITK_EXPORT Image : public ImageBase<VImageDimension>
83 {
84 public:
85   /** Standard class typedefs */
86   typedef Image               Self;
87 TDA   typedef ImageBase<VImageDimension>  Superclass;
88   typedef SmartPointer<Self>  Pointer;
89 TDA   typedef SmartPointer<const Self>  ConstPointer;
90 TDA   typedef WeakPointer<const Self>  ConstWeakPointer;
91
92   /** Method for creation through the object factory. */
93   itkNewMacro(Self);
94
95   /** Run-time type information (and related methods). */
96   itkTypeMacro(Image, ImageBase);
97
98   /** Pixel typedef support. Used to declare pixel type in filters
99    * or other operations. */
100   typedef TPixel PixelType;
101
102   /** Typedef alias for PixelType */
103 SEM   typedef TPixel ValueType ;
104
105   /** Internal Pixel representation. Used to maintain a uniform API
106    * with Image Adaptors and allow to keep a particular internal
107    * representation of data while showing a different external
108    * representation. */
109   typedef TPixel InternalPixelType;
110
111   typedef PixelType IOPixelType;
112
113   /** Accessor type that convert data between internal and external
114    *  representations.  */
115   typedef DefaultPixelAccessor< PixelType > AccessorType;
116 TDA   typedef DefaultPixelAccessorFunctor< Self > AccessorFunctorType;
117
118   /** Tyepdef for the functor used to access a neighborhood of pixel pointers.*/
119   typedef NeighborhoodAccessorFunctor< Self > 
120 IND ********************************************NeighborhoodAccessorFunctorType;
121
122   /** Dimension of the image.  This constant is used by functions that are
123    * templated over image type (as opposed to being templated over pixel type
124    * and dimension) when they need compile time access to the dimension of
125    * the image. */
126   itkStaticConstMacro(ImageDimension, unsigned int, VImageDimension);
127
128   /** Container used to store pixels in the image. */
129   typedef ImportImageContainer<unsigned long, PixelType> PixelContainer;
130
131   /** Index typedef support. An index is used to access pixel values. */
132   typedef typename Superclass::IndexType  IndexType;
133
134   /** Offset typedef support. An offset is used to access pixel values. */
135   typedef typename Superclass::OffsetType OffsetType;
136
137   /** Size typedef support. A size is used to define region bounds. */
138   typedef typename Superclass::SizeType  SizeType;
139
140   /** Direction typedef support. A matrix of direction cosines. */
141   typedef typename Superclass::DirectionType  DirectionType;
142
143 LEN   /** Region typedef support. A region is used to specify a subset of an image. */
144   typedef typename Superclass::RegionType  RegionType;
145
146   /** Spacing typedef support.  Spacing holds the size of a pixel.  The
147    * spacing is the geometric distance between image samples. */
148   typedef typename Superclass::SpacingType SpacingType;
149
150   /** Origin typedef support.  The origin is the geometric coordinates
151    * of the index (0,0). */
152   typedef typename Superclass::PointType PointType;
153
154   /** A pointer to the pixel container. */
155   typedef typename PixelContainer::Pointer PixelContainerPointer;
156 TDA   typedef typename PixelContainer::ConstPointer PixelContainerConstPointer;
157
158   /** Offset typedef (relative position between indices) */
159   typedef typename Superclass::OffsetValueType OffsetValueType;
160
161   /** Allocate the image memory. The size of the image must
162    * already be set, e.g. by calling SetRegions(). */
163   void Allocate();
164
165   /** Convenience methods to set the LargestPossibleRegion,
166    *  BufferedRegion and RequestedRegion. Allocate must still be called.
167    */
168   void SetRegions(RegionType region)
169     {
170     this->SetLargestPossibleRegion(region);
171     this->SetBufferedRegion(region);
172     this->SetRequestedRegion(region);
173     };
174
175   void SetRegions(SizeType size)
176     {
177     RegionType region; region.SetSize(size);
178     this->SetLargestPossibleRegion(region);
179     this->SetBufferedRegion(region);
180     this->SetRequestedRegion(region);
181     };
182
183   /** Restore the data object to its initial state. This means releasing
184    * memory. */
185   virtual void Initialize();
186
187   /** Fill the image buffer with a value.  Be sure to call Allocate()
188    * first. */
189   void FillBuffer (const TPixel& value);
190
191   /** \brief Set a pixel value.
192    *
193    * Allocate() needs to have been called first -- for efficiency,
194    * this function does not check that the image has actually been
195    * allocated yet. */
196   void SetPixel(const IndexType &index, const TPixel& value)
197     {
198     typename Superclass::OffsetValueType offset = this->ComputeOffset(index);
199     (*m_Buffer)[offset] = value;
200     }
201
202   /** \brief Get a pixel (read only version).
203    *
204    * For efficiency, this function does not check that the
205    * image has actually been allocated yet. */
206   const TPixel& GetPixel(const IndexType &index) const
207 IND **{
208     typename Superclass::OffsetValueType offset = this->ComputeOffset(index);
209     return ( (*m_Buffer)[offset] );
210 IND **}
211
212   /** \brief Get a reference to a pixel (e.g. for editing).
213    *
214    * For efficiency, this function does not check that the
215    * image has actually been allocated yet. */
216   TPixel& GetPixel(const IndexType &index)
217     {
218     typename Superclass::OffsetValueType offset = this->ComputeOffset(index);
219     return ( (*m_Buffer)[offset] );
220     }
221
222   /** \brief Access a pixel. This version can be an lvalue.
223    *
224    * For efficiency, this function does not check that the
225    * image has actually been allocated yet. */
226   TPixel & operator[](const IndexType &index)
227 IND *****{ return this->GetPixel(index); }
228
229   /** \brief Access a pixel. This version can only be an rvalue.
230    *
231    * For efficiency, this function does not check that the
232    * image has actually been allocated yet. */
233   const TPixel& operator[](const IndexType &index) const
234 IND *****{ return this->GetPixel(index); }
235
236   /** Return a pointer to the beginning of the buffer.  This is used by
237    * the image iterator class. */
238   TPixel *GetBufferPointer()
239     { return m_Buffer ? m_Buffer->GetBufferPointer() : 0; }
240   const TPixel *GetBufferPointer() const
241     { return m_Buffer ? m_Buffer->GetBufferPointer() : 0; }
242
243   /** Return a pointer to the container. */
244   PixelContainer* GetPixelContainer()
245     { return m_Buffer.GetPointer(); }
246
247   const PixelContainer* GetPixelContainer() const
248     { return m_Buffer.GetPointer(); }
249
250   /** Set the container to use. Note that this does not cause the
251    * DataObject to be modified. */
252   void SetPixelContainer( PixelContainer *container );
253
254   /** Graft the data and information from one image to another. This
255    * is a convenience method to setup a second image with all the meta
256    * information of another image and use the same pixel
257    * container. Note that this method is different than just using two
258    * SmartPointers to the same image since separate DataObjects are
259    * still maintained. This method is similar to
260    * ImageSource::GraftOutput(). The implementation in ImageBase
261    * simply calls CopyInformation() and copies the region ivars.
262    * The implementation here refers to the superclass' implementation
263    * and then copies over the pixel container. */
264   virtual void Graft(const DataObject *data);
265
266   
267   /** Return the Pixel Accessor object */
268   AccessorType GetPixelAccessor( void ) 
269     { return AccessorType(); }
270
271   /** Return the Pixel Accesor object */
272   const AccessorType GetPixelAccessor( void ) const
273     { return AccessorType(); }
274
275   /** Return the NeighborhoodAccessor functor */
276   NeighborhoodAccessorFunctorType GetNeighborhoodAccessor() 
277     { return NeighborhoodAccessorFunctorType(); }
278   
279   /** Return the NeighborhoodAccessor functor */
280   const NeighborhoodAccessorFunctorType GetNeighborhoodAccessor() const
281     { return NeighborhoodAccessorFunctorType(); }
282   
283
284   /** \brief Get the continuous index from a physical point
285    *
286    * Returns true if the resulting index is within the image, false otherwise.
287    * \sa Transform */
288   template<class TCoordRep>
289   bool TransformPhysicalPointToContinuousIndex(
290               const Point<TCoordRep, VImageDimension>& point,
291               ContinuousIndex<TCoordRep, VImageDimension>& index   ) const
292     {
293     // Update the output index
294 SEM,SEM     for (unsigned int i = 0 ; i < VImageDimension ; i++)
295       {
296 LEN       index[i] = static_cast<TCoordRep>( (point[i]- this->m_Origin[i]) / this->m_Spacing[i] );
297       }
298
299     // Now, check to see if the index is within allowed bounds
300     const bool isInside =
301 IND ******this->GetLargestPossibleRegion().IsInside( index );
302
303     return isInside;
304     }
305
306   /** Get the index (discrete) from a physical point.
307    * Floating point index results are truncated to integers.
308    * Returns true if the resulting index is within the image, false otherwise
309    * \sa Transform */
310   template<class TCoordRep>
311   bool TransformPhysicalPointToIndex(
312             const Point<TCoordRep, VImageDimension>& point,
313             IndexType & index                                ) const
314     {
315     typedef typename IndexType::IndexValueType IndexValueType;
316
317     // Update the output index
318 SEM,SEM     for (unsigned int i = 0 ; i < VImageDimension ; i++)
319       {
320 LEN       index[i] = static_cast<IndexValueType>( (point[i]- this->m_Origin[i]) / this->m_Spacing[i] );
321       }
322
323     // Now, check to see if the index is within allowed bounds
324     const bool isInside =
325 IND ******this->GetLargestPossibleRegion().IsInside( index );
326
327     return isInside;
328     }
329
330   /** Get a physical point (in the space which
331    * the origin and spacing infomation comes from)
332    * from a continuous index (in the index space)
333    * \sa Transform */
334   template<class TCoordRep>
335   void TransformContinuousIndexToPhysicalPoint(
336             const ContinuousIndex<TCoordRep, VImageDimension>& index,
337             Point<TCoordRep, VImageDimension>& point        ) const
338     {
339 SEM,SEM     for (unsigned int i = 0 ; i < VImageDimension ; i++)
340       {
341 LEN       point[i] = static_cast<TCoordRep>( this->m_Spacing[i] * index[i] + this->m_Origin[i] );
342       }
343     }
344
345   /** Get a physical point (in the space which
346    * the origin and spacing infomation comes from)
347    * from a discrete index (in the index space)
348    *
349    * \sa Transform */
350   template<class TCoordRep>
351   void TransformIndexToPhysicalPoint(
352                       const IndexType & index,
353                       Point<TCoordRep, VImageDimension>& point ) const
354     {
355 SEM,SEM     for (unsigned int i = 0 ; i < VImageDimension ; i++)
356       {
357       point[i] = static_cast<TCoordRep>( this->m_Spacing[i] *
358         static_cast<double>( index[i] ) + this->m_Origin[i] );
359       }
360     }
361
362 protected:
363   Image();
364   void PrintSelf(std::ostream& os, Indent indent) const;
365   virtual ~Image() {};
366 private:
367   Image(const Self&); //purposely not implemented
368   void operator=(const Self&); //purposely not implemented
369
370   /** Memory for the current buffer. */
371   PixelContainerPointer m_Buffer;
372 };
373 #ifdef ITK_EXPLICIT_INSTANTIATION
374 IND ***extern template class Image<float         ,2>;
375 IND ***extern template class Image<double        ,2>;
376 IND ***extern template class Image<unsigned char ,2>;
377 IND ***extern template class Image<unsigned short,2>;
378 IND ***extern template class Image<unsigned int  ,2>;
379 IND ***extern template class Image<signed char   ,2>;
380 IND ***extern template class Image<signed short  ,2>;
381 IND ***extern template class Image<signed int    ,2>;
382 IND ***extern template class Image<float         ,3>;
383 IND ***extern template class Image<double        ,3>;
384 IND ***extern template class Image<unsigned char ,3>;
385 IND ***extern template class Image<unsigned short,3>;
386 IND ***extern template class Image<unsigned int  ,3>;
387 IND ***extern template class Image<signed char   ,3>;
388 IND ***extern template class Image<signed short  ,3>;
389 IND ***extern template class Image<signed int    ,3>;
390 #endif
391 // end namespace itk
392 #ifndef ITK_MANUAL_INSTANTIATION
393 #include "itkImage.txx"
394 #endif
395
396 #endif
397
398 EOF

Generated by KWStyle 1.0b on Tuesday January,17 at 02:14:13PM
© Kitware Inc.