| 1 |
|
/*========================================================================= |
| 2 |
|
|
| 3 |
|
Program: Insight Segmentation & Registration Toolkit |
| 4 |
|
Module: $RCSfile: itkVectorImage.h.html,v $ |
| 5 |
|
Language: C++ |
| 6 |
|
Date: $Date: 2006/01/17 19:15:49 $ |
| 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 __itkVectorImage_h |
| 18 |
|
#define __itkVectorImage_h |
| 19 |
|
|
| 20 |
|
#include "itkImage.h" |
| 21 |
|
#include "itkImageRegion.h" |
| 22 |
|
#include "itkImportImageContainer.h" |
| 23 |
|
#include "itkDefaultVectorPixelAccessor.h" |
| 24 |
|
#include "itkDefaultVectorPixelAccessorFunctor.h" |
| 25 |
|
#include "itkVectorImageNeighborhoodAccessorFunctor.h" |
| 26 |
|
#include "itkPoint.h" |
| 27 |
|
#include "itkContinuousIndex.h" |
| 28 |
|
#include "itkVariableLengthVector.h" |
| 29 |
|
#include "itkWeakPointer.h" |
| 30 |
|
|
| 31 |
|
namespace itk |
| 32 |
|
{ |
| 33 |
|
|
| 34 |
|
/** \class VectorImage |
| 35 |
|
* \brief Templated n-dimensional vector image class. |
| 36 |
|
* |
| 37 |
|
* This class differs from Image in that it is intended to represent multiple |
| 38 |
|
* images. Each pixel represents \e k measurements, each of datatype \e TPixel. |
| 39 |
|
* The memory organization of the resulting image is as follows: |
| 40 |
|
* ... Pi0 Pi1 Pi2 Pi3 P(i+1)0 P(i+1)1 P(i+1)2 P(i+1)3 P(i+2)0 ... |
| 41 |
|
* where Pi0 represents the 0th measurement of the pixel at index i. |
| 42 |
|
* |
| 43 |
|
* Conceptually, a <tt>VectorImage< double, 3 ></tt> is the same as a |
| 44 |
LEN |
* <tt>Image< VariableLengthVector< double >, 3 ></tt>. The difference lies in the memory |
| 45 |
|
* organization. The latter results in a fragmented |
| 46 |
LEN |
* organization with each location in the Image holding a pointer to an \c VariableLengthVector |
| 47 |
|
* holding the actual pixel. The former stores the \e k pixels instead of a |
| 48 |
LEN |
* pointer reference, which apart from avoiding fragmentation of memory also avoids |
| 49 |
|
* storing a 8 bytes of pointer reference for each pixel. |
| 50 |
|
* The parameter \e k can be set using \c SetVectorLength. |
| 51 |
|
* |
| 52 |
LEN |
* The API of the class is such that it returns a pixeltype VariableLengthVector< double > when |
| 53 |
LEN |
* queried, with the data internally pointing to the buffer. (the container does not |
| 54 |
LEN |
* manage the memory). Similarly SetPixel calls can be made with VariableLengthVector< double >. |
| 55 |
|
* |
| 56 |
|
* The API of this class is similar to Image. |
| 57 |
|
* |
| 58 |
|
* \note |
| 59 |
|
* This work is part of the National Alliance for Medical Image Computing |
| 60 |
|
* (NAMIC), funded by the National Institutes of Health through the NIH Roadmap |
| 61 |
|
* for Medical Research, Grant U54 EB005149. |
| 62 |
|
* |
| 63 |
|
* \sa DefaultVectorPixelAccessor |
| 64 |
|
* \sa DefaultVectorPixelAccessorFunctor |
| 65 |
|
* \sa VectorImageToImagePixelAccessor |
| 66 |
|
* \sa VectorImageToImageAdaptor |
| 67 |
|
* \sa Image |
| 68 |
|
* \sa ImportImageContainer |
| 69 |
|
* |
| 70 |
|
* \example Testing/Code/Common/itkVectorImageTest.cxx |
| 71 |
|
* |
| 72 |
|
* \ingroup ImageObjects |
| 73 |
|
*/ |
| 74 |
|
template <class TPixel, unsigned int VImageDimension=3 > |
| 75 |
|
class ITK_EXPORT VectorImage : |
| 76 |
IND |
****public Image< VariableLengthVector< TPixel >, VImageDimension > |
| 77 |
|
{ |
| 78 |
|
public: |
| 79 |
|
/** Standard class typedefs */ |
| 80 |
|
typedef VectorImage Self; |
| 81 |
|
typedef ImageBase<VImageDimension> Superclass; |
| 82 |
|
typedef SmartPointer<Self> Pointer; |
| 83 |
|
typedef SmartPointer<const Self> ConstPointer; |
| 84 |
|
typedef WeakPointer<const Self> ConstWeakPointer; |
| 85 |
|
|
| 86 |
|
/** Method for creation through the object factory. */ |
| 87 |
|
itkNewMacro(Self); |
| 88 |
|
|
| 89 |
|
/** Run-time type information (and related methods). */ |
| 90 |
|
itkTypeMacro(VectorImage, Image); |
| 91 |
|
|
| 92 |
|
/** Pixel typedef support. Used to declare pixel type in filters |
| 93 |
|
* or other operations. This is not the actual pixel type contained in |
| 94 |
|
* the buffer, ie m_Buffer. The image exhibits an external API of an |
| 95 |
|
* VariableLengthVector< T > and internally stores its data as type T. */ |
| 96 |
|
typedef VariableLengthVector< TPixel > PixelType; |
| 97 |
|
|
| 98 |
|
/** This is the actual pixel type contained in the buffer. Each vector |
| 99 |
|
* pixel is composed of 'm_VectorLength' contiguous InternalPixelType. |
| 100 |
|
*/ |
| 101 |
|
typedef TPixel InternalPixelType; |
| 102 |
|
|
| 103 |
|
/** Typedef alias for PixelType */ |
| 104 |
SEM |
typedef TPixel ValueType ; |
| 105 |
|
|
| 106 |
|
typedef InternalPixelType IOPixelType; |
| 107 |
|
|
| 108 |
|
/** Accessor type that convert data between internal and external |
| 109 |
|
* representations. */ |
| 110 |
|
typedef DefaultVectorPixelAccessor< InternalPixelType > AccessorType; |
| 111 |
|
|
| 112 |
|
/** Functor to provide a common API between DefaultPixelAccessor and |
| 113 |
|
* DefaultVectorPixelAccessor */ |
| 114 |
|
typedef DefaultVectorPixelAccessorFunctor< Self > AccessorFunctorType; |
| 115 |
|
|
| 116 |
|
/** Tyepdef for the functor used to access a neighborhood of pixel pointers.*/ |
| 117 |
|
typedef VectorImageNeighborhoodAccessorFunctor< |
| 118 |
|
Self > NeighborhoodAccessorFunctorType; |
| 119 |
|
|
| 120 |
|
/** Container used to store pixels in the image. */ |
| 121 |
|
typedef ImportImageContainer<unsigned long, InternalPixelType> PixelContainer; |
| 122 |
|
|
| 123 |
|
/** Index typedef support. An index is used to access pixel values. */ |
| 124 |
|
typedef typename Superclass::IndexType IndexType; |
| 125 |
|
|
| 126 |
|
/** Offset typedef support. An offset is used to access pixel values. */ |
| 127 |
|
typedef typename Superclass::OffsetType OffsetType; |
| 128 |
|
|
| 129 |
|
/** Size typedef support. A size is used to define region bounds. */ |
| 130 |
|
typedef typename Superclass::SizeType SizeType; |
| 131 |
|
|
| 132 |
|
/** Direction typedef support. A matrix of direction cosines. */ |
| 133 |
|
typedef typename Superclass::DirectionType DirectionType; |
| 134 |
|
|
| 135 |
LEN |
/** Region typedef support. A region is used to specify a subset of an image. */ |
| 136 |
|
typedef typename Superclass::RegionType RegionType; |
| 137 |
|
|
| 138 |
|
/** Spacing typedef support. Spacing holds the size of a pixel. The |
| 139 |
|
* spacing is the geometric distance between image samples. */ |
| 140 |
|
typedef typename Superclass::SpacingType SpacingType; |
| 141 |
|
|
| 142 |
|
/** Origin typedef support. The origin is the geometric coordinates |
| 143 |
|
* of the index (0,0). */ |
| 144 |
|
typedef typename Superclass::PointType PointType; |
| 145 |
|
|
| 146 |
|
/** A pointer to the pixel container. */ |
| 147 |
|
typedef typename PixelContainer::Pointer PixelContainerPointer; |
| 148 |
TDA |
typedef typename PixelContainer::ConstPointer PixelContainerConstPointer; |
| 149 |
|
|
| 150 |
|
/** Offset typedef (relative position between indices) */ |
| 151 |
|
typedef typename Superclass::OffsetValueType OffsetValueType; |
| 152 |
|
|
| 153 |
|
typedef unsigned int VectorLengthType; |
| 154 |
|
|
| 155 |
|
/** Allocate the image memory. The size of the image must |
| 156 |
|
* already be set, e.g. by calling SetRegions(). */ |
| 157 |
|
void Allocate(); |
| 158 |
|
|
| 159 |
|
/** Restore the data object to its initial state. This means releasing |
| 160 |
|
* memory. */ |
| 161 |
|
virtual void Initialize(); |
| 162 |
|
|
| 163 |
|
/** Fill the image buffer with a value. Be sure to call Allocate() |
| 164 |
|
* first. */ |
| 165 |
|
void FillBuffer(const PixelType& value); |
| 166 |
|
|
| 167 |
|
void SetPixel( const IndexType &index, const PixelType& value ) |
| 168 |
|
{ |
| 169 |
|
OffsetValueType offset = m_VectorLength * this->ComputeOffset(index); |
| 170 |
|
for( VectorLengthType i = 0; i < m_VectorLength; i++ ) |
| 171 |
|
{ |
| 172 |
|
(*m_Buffer)[offset + i] = value[i]; |
| 173 |
|
} |
| 174 |
|
} |
| 175 |
|
|
| 176 |
|
/** \brief Get a pixel (read only version). |
| 177 |
|
* |
| 178 |
|
* For efficiency, this function does not check that the |
| 179 |
|
* image has actually been allocated yet. */ |
| 180 |
|
const PixelType GetPixel(const IndexType &index) const |
| 181 |
|
{ |
| 182 |
|
OffsetValueType offset = m_VectorLength * this->ComputeOffset(index); |
| 183 |
|
PixelType p( &((*m_Buffer)[offset]), m_VectorLength ); |
| 184 |
|
return p; |
| 185 |
|
} |
| 186 |
|
|
| 187 |
|
/** \brief Get a reference to a pixel (e.g. for editing). |
| 188 |
|
* |
| 189 |
|
* For efficiency, this function does not check that the |
| 190 |
|
* image has actually been allocated yet. */ |
| 191 |
|
PixelType GetPixel(const IndexType &index ) |
| 192 |
|
{ |
| 193 |
|
OffsetValueType offset = m_VectorLength * this->ComputeOffset(index); |
| 194 |
|
PixelType p( &((*m_Buffer)[offset]), m_VectorLength ); |
| 195 |
|
return p; |
| 196 |
|
} |
| 197 |
|
|
| 198 |
|
/** Return a pointer to the beginning of the buffer. This is used by |
| 199 |
|
* the image iterator class. */ |
| 200 |
|
InternalPixelType *GetBufferPointer() |
| 201 |
|
{ return m_Buffer ? m_Buffer->GetBufferPointer() : 0; } |
| 202 |
|
const InternalPixelType *GetBufferPointer() const |
| 203 |
|
{ return m_Buffer ? m_Buffer->GetBufferPointer() : 0; } |
| 204 |
|
|
| 205 |
|
/** Return a pointer to the container. */ |
| 206 |
|
PixelContainer* GetPixelContainer() |
| 207 |
|
{ return m_Buffer.GetPointer(); } |
| 208 |
|
|
| 209 |
|
/** Return a pointer to the container. */ |
| 210 |
|
const PixelContainer* GetPixelContainer() const |
| 211 |
|
{ return m_Buffer.GetPointer(); } |
| 212 |
|
|
| 213 |
|
/** Set the container to use. Note that this does not cause the |
| 214 |
|
* DataObject to be modified. */ |
| 215 |
|
void SetPixelContainer( PixelContainer *container ); |
| 216 |
|
|
| 217 |
|
/** Graft the data and information from one image to another. This |
| 218 |
|
* is a convenience method to setup a second image with all the meta |
| 219 |
|
* information of another image and use the same pixel |
| 220 |
|
* container. Note that this method is different than just using two |
| 221 |
|
* SmartPointers to the same image since separate DataObjects are |
| 222 |
|
* still maintained. This method is similar to |
| 223 |
|
* ImageSource::GraftOutput(). The implementation in ImageBase |
| 224 |
|
* simply calls CopyInformation() and copies the region ivars. |
| 225 |
|
* The implementation here refers to the superclass' implementation |
| 226 |
|
* and then copies over the pixel container. */ |
| 227 |
|
virtual void Graft(const DataObject *data); |
| 228 |
|
|
| 229 |
|
/** Return the Pixel Accessor object */ |
| 230 |
|
AccessorType GetPixelAccessor( void ) |
| 231 |
|
{ return AccessorType( m_VectorLength ); } |
| 232 |
|
|
| 233 |
|
/** Return the Pixel Accesor object */ |
| 234 |
|
const AccessorType GetPixelAccessor( void ) const |
| 235 |
|
{ return AccessorType( m_VectorLength ); } |
| 236 |
|
|
| 237 |
|
/** Return the NeighborhoodAccessor functor */ |
| 238 |
|
NeighborhoodAccessorFunctorType GetNeighborhoodAccessor() |
| 239 |
|
{ return NeighborhoodAccessorFunctorType( m_VectorLength ); } |
| 240 |
|
|
| 241 |
|
/** Return the NeighborhoodAccessor functor */ |
| 242 |
|
const NeighborhoodAccessorFunctorType GetNeighborhoodAccessor() const |
| 243 |
|
{ return NeighborhoodAccessorFunctorType(m_VectorLength); } |
| 244 |
|
|
| 245 |
|
|
| 246 |
|
/** Set/Get macros for the length of each vector in the vector image */ |
| 247 |
|
itkSetMacro( VectorLength, VectorLengthType ); |
| 248 |
|
itkGetConstReferenceMacro( VectorLength, VectorLengthType ); |
| 249 |
|
|
| 250 |
|
protected: |
| 251 |
|
VectorImage(); |
| 252 |
|
void PrintSelf( std::ostream& os, Indent indent ) const; |
| 253 |
|
virtual ~VectorImage() {}; |
| 254 |
|
|
| 255 |
|
private: |
| 256 |
|
VectorImage( const Self & ); // purposely not implementated |
| 257 |
|
void operator=(const Self&); //purposely not implemented |
| 258 |
|
|
| 259 |
|
/** Get/Set the number of components each pixel has, ie the VectorLength */ |
| 260 |
|
virtual unsigned int GetNumberOfComponentsPerPixel() const; |
| 261 |
|
virtual void SetNumberOfComponentsPerPixel( unsigned int n ); |
| 262 |
|
|
| 263 |
|
/** Length of the "vector pixel" */ |
| 264 |
|
VectorLengthType m_VectorLength; |
| 265 |
|
|
| 266 |
|
/** Memory for the current buffer. */ |
| 267 |
|
PixelContainerPointer m_Buffer; |
| 268 |
|
}; |
| 269 |
|
|
| 270 |
|
|
| 271 |
|
} // end namespace itk |
| 272 |
|
#ifndef ITK_MANUAL_INSTANTIATION |
| 273 |
|
#include "itkVectorImage.txx" |
| 274 |
|
#endif |
| 275 |
|
|
| 276 |
|
#endif |
| 277 |
|
|