/vol/vipdata/irtk/image++/include/irtkGenericImage.h

00001 /*=========================================================================
00002 
00003   Library   : Image Registration Toolkit (IRTK)
00004   Module    : $Id: irtkGenericImage.h 8 2009-03-02 16:12:58Z dr $
00005   Copyright : Imperial College, Department of Computing
00006               Visual Information Processing (VIP), 2008 onwards
00007   Date      : $Date: 2009-03-02 16:12:58 +0000 (Mon, 02 Mar 2009) $
00008   Version   : $Revision: 8 $
00009   Changes   : $Author: dr $
00010 
00011 =========================================================================*/
00012 
00013 #ifndef _IRTKGENERICIMAGE_H
00014 
00015 #define _IRTKGENERICIMAGE_H
00016 
00017 #ifdef HAS_VTK
00018 
00019 #include <vtkStructuredPoints.h>
00020 
00021 #endif
00022 
00031 template <class VoxelType> class irtkGenericImage : public irtkBaseImage
00032 {
00033 
00034 protected:
00035 
00037   VoxelType ****_matrix;
00038 
00039 public:
00040 
00042   irtkGenericImage();
00043 
00045   irtkGenericImage(char *);
00046 
00048   irtkGenericImage(int, int, int, int = 1);
00049 
00051   irtkGenericImage(const irtkGenericImage &);
00052 
00054   irtkGenericImage(const irtkImageAttributes &);
00055 
00057   template <class T> irtkGenericImage(const irtkGenericImage<T> &);
00058 
00060   ~irtkGenericImage(void);
00061 
00063   void Initialize(const irtkImageAttributes &);
00064 
00066   void Read (const char *);
00067 
00069   void Write(const char *);
00070 
00072   void GetMinMax(VoxelType *, VoxelType *) const;
00073 
00075   void GetMinMaxPad(VoxelType *, VoxelType *, VoxelType) const;
00076 
00078   void PutMinMax(VoxelType, VoxelType);
00079 
00081   VoxelType *GetPointerToVoxels(int = 0, int = 0, int = 0, int = 0) const;
00082 
00084   int VoxelToIndex(int, int, int, int = 0) const;
00085 
00087   VoxelType   Get(int, int, int, int = 0) const;
00088 
00090   void   Put(int, int, int, VoxelType);
00091 
00093   void   Put(int, int, int, int, VoxelType);
00094 
00096   VoxelType& operator()(int, int, int, int = 0);
00097 
00099   irtkGenericImage GetRegion(int z, int t) const;
00100 
00102   irtkGenericImage GetRegion(int x1, int y1, int z1, int x2, int y2, int z2) const;
00103 
00105   irtkGenericImage GetRegion(int x1, int y1, int z1, int t1, int x2, int y2, int z2, int t2) const;
00106 
00108   irtkGenericImage GetFrame(int t) const;
00109 
00110   //
00111   // Operators for image arithmetics
00112   //
00113 
00115   irtkGenericImage<VoxelType>& operator= (const irtkGenericImage &);
00116   
00118   template <class T> irtkGenericImage<VoxelType>& operator= (const irtkGenericImage<T> &);
00119   
00121   irtkGenericImage  operator+ (const irtkGenericImage &);
00122 
00124   irtkGenericImage& operator+=(const irtkGenericImage &);
00125 
00127   irtkGenericImage  operator- (const irtkGenericImage &);
00128 
00130   irtkGenericImage& operator-=(const irtkGenericImage &);
00131 
00133   irtkGenericImage  operator* (const irtkGenericImage &);
00134 
00136   irtkGenericImage& operator*=(const irtkGenericImage &);
00137 
00139   irtkGenericImage  operator/ (const irtkGenericImage &);
00140 
00142   irtkGenericImage& operator/=(const irtkGenericImage &);
00143 
00144   //
00145   // Operators for image and Type arithmetics
00146   //
00147 
00149   irtkGenericImage  operator+ (VoxelType);
00151   irtkGenericImage& operator+=(VoxelType);
00153   irtkGenericImage  operator- (VoxelType);
00155   irtkGenericImage& operator-=(VoxelType);
00157   irtkGenericImage  operator* (VoxelType);
00159   irtkGenericImage& operator*=(VoxelType);
00161   irtkGenericImage  operator/ (VoxelType);
00163   irtkGenericImage& operator/=(VoxelType);
00164 
00165   //
00166   // Operators for image thresholding
00167   //
00168 
00170   irtkGenericImage  operator> (VoxelType);
00172   irtkGenericImage& operator>=(VoxelType);
00174   irtkGenericImage  operator< (VoxelType);
00176   irtkGenericImage& operator<=(VoxelType);
00177 
00179   Bool operator==(const irtkGenericImage &);
00182 
00183   //
00184   // Reflections and axis flipping
00185   //
00186 
00188   void ReflectX();
00190   void ReflectY();
00192   void ReflectZ();
00194   void FlipXY();
00196   void FlipXZ();
00198   void FlipYZ();
00200   void FlipXT();
00202   void FlipYT();
00204   void FlipZT();
00205 
00206   //
00207   // Conversions from and to VTK
00208   //
00209 
00210 #ifdef HAS_VTK
00211 
00213   int ImageToVTKScalarType();
00214 
00216   void ImageToVTK(vtkStructuredPoints *);
00217 
00219   void VTKToImage(vtkStructuredPoints *);
00220 #endif
00221 
00223   double GetAsDouble(int, int, int, int = 0) const;
00224 
00226   void   PutAsDouble(int, int, int, double);
00227 
00229   void   PutAsDouble(int, int, int, int, double);
00230 
00232   const char *NameOfClass();
00233   
00235   void *GetScalarPointer(int = 0, int = 0, int = 0, int = 0) const;
00236 
00238   virtual int GetScalarType() const;
00239   
00241   virtual double GetScalarTypeMin() const;
00242 
00244   virtual double GetScalarTypeMax() const;
00245 
00246 };
00247 
00248 template <class VoxelType> inline void irtkGenericImage<VoxelType>::Put(int x, int y, int z, VoxelType val)
00249 {
00250 #ifdef NO_BOUNDS
00251   _matrix[0][z][y][x] = val;
00252 #else
00253   if ((x >= _x) || (x < 0) || (y >= _y) || (y < 0) || (z >= _z) || (z < 0) || (_t != 0)) {
00254     cout << "irtkGenericImage<VoxelType>::Put: parameter out of range\n";
00255   } else {
00256     _matrix[0][z][y][x] = static_cast<VoxelType>(val)
00257   }
00258 #endif
00259 }
00260 
00261 template <class VoxelType> inline void irtkGenericImage<VoxelType>::Put(int x, int y, int z, int t, VoxelType val)
00262 {
00263 #ifdef NO_BOUNDS
00264   _matrix[t][z][y][x] = val;
00265 #else
00266   if ((x >= _x) || (x < 0) || (y >= _y) || (y < 0) || (z >= _z) || (z < 0) || (t >= _t) || (t < 0)) {
00267     cout << "irtkGenericImage<VoxelType>::Put: parameter out of range\n";
00268   } else {
00269     _matrix[t][z][y][x] = val;
00270   }
00271 #endif
00272 }
00273 
00274 template <class VoxelType> inline void irtkGenericImage<VoxelType>::PutAsDouble(int x, int y, int z, double val)
00275 {
00276   if (val > voxel_limits<VoxelType>::max()) val = voxel_limits<VoxelType>::max();
00277   if (val < voxel_limits<VoxelType>::min()) val = voxel_limits<VoxelType>::min();  
00278 
00279 #ifdef NO_BOUNDS
00280   _matrix[0][z][y][x] = static_cast<VoxelType>(val);
00281 #else
00282   if ((x >= _x) || (x < 0) || (y >= _y) || (y < 0) || (z >= _z) || (z < 0) || (_t > 0) {
00283     cout << "irtkGenericImage<Type>::PutAsDouble: parameter out of range\n";
00284   } else {
00285     _matrix[0][z][y][x] = static_cast<VoxelType>(val);
00286   }
00287 #endif
00288 }
00289 
00290 template <class VoxelType> inline void irtkGenericImage<VoxelType>::PutAsDouble(int x, int y, int z, int t, double val)
00291 {
00292   if (val > voxel_limits<VoxelType>::max()) val = voxel_limits<VoxelType>::max();
00293   if (val < voxel_limits<VoxelType>::min()) val = voxel_limits<VoxelType>::min();  
00294 
00295 #ifdef NO_BOUNDS
00296   _matrix[t][z][y][x] = static_cast<VoxelType>(val);
00297 #else
00298   if ((x >= _x) || (x < 0) || (y >= _y) || (y < 0) || (z >= _z) || (z < 0) || (t >= _t) || (t < 0)) {
00299     cout << "irtkGenericImage<Type>::PutAsDouble: parameter out of range\n";
00300   } else {
00301     _matrix[t][z][y][x] = static_cast<VoxelType>(val);
00302   }
00303 #endif
00304 }
00305 
00306 template <class VoxelType> inline VoxelType irtkGenericImage<VoxelType>::Get(int x, int y, int z, int t) const
00307 {
00308 #ifdef NO_BOUNDS
00309   return (_matrix[t][z][y][x]);
00310 #else
00311   if ((x >= _x) || (x < 0) || (y >= _y) || (y < 0) || (z >= _z) || (z < 0) || (t >= _t) || (z < t)) {
00312     cout << "irtkGenericImage<Type>::Get: parameter out of range\n";
00313     return 0;
00314   } else {
00315     return(_matrix[t][z][y][x]);
00316   }
00317 #endif
00318 }
00319 
00320 template <class VoxelType> inline double irtkGenericImage<VoxelType>::GetAsDouble(int x, int y, int z, int t) const
00321 {
00322 #ifdef NO_BOUNDS
00323   return (static_cast<double>(_matrix[t][z][y][x]));
00324 #else
00325   if ((x >= _x) || (x < 0) || (y >= _y) || (y < 0) || (z >= _z) || (z < 0) || (t >= _t) || (t < 0)) {
00326     cout << "irtkGenericImage<Type>::GetAsDouble: parameter out of range\n";
00327     return 0;
00328   } else {
00329     return (static_cast<double>(_matrix[t][z][y][x]));
00330   }
00331 #endif
00332 
00333 }
00334 
00335 template <class VoxelType> inline VoxelType& irtkGenericImage<VoxelType>::operator()(int x, int y, int z, int t)
00336 {
00337 #ifdef NO_BOUNDS
00338   return (_matrix[t][z][y][x]);
00339 #else
00340   if ((x >= _x) || (x < 0) || (y >= _y) || (y < 0) || (z >= _z) || (z < 0) || (t >= _t) || (t < 0)) {
00341     cout << "irtkGenericImage<Type>::(): parameter out of range\n";
00342     return _matrix[0][0][0][0];
00343   } else {
00344     return (_matrix[t][z][y][x]);
00345   }
00346 #endif
00347 }
00348 
00349 template <class VoxelType> inline int irtkGenericImage<VoxelType>::VoxelToIndex(int x, int y, int z, int t) const
00350 {
00351 #ifdef NO_BOUNDS
00352   return (&(_matrix[t][z][y][x]) - &(_matrix[0][0][0][0]));
00353 #else
00354   if ((x >= _x) || (x < 0) || (y >= _y) || (y < 0) || (z >= _z) || (z < 0) || (t >= _t) || (t < 0)) {
00355     cout << "irtkGenericImage<Type>::VoxelToIndex: parameter out of range\n";
00356     return 0;
00357   } else {
00358     return (&(_matrix[t][z][y][x]) - &(_matrix[0][0][0][0]));
00359   }
00360 #endif
00361 }
00362 
00363 template <class VoxelType> inline VoxelType *irtkGenericImage<VoxelType>::GetPointerToVoxels(int x, int y, int z, int t) const
00364 {
00365 #ifdef NO_BOUNDS
00366   return &(_matrix[t][z][y][x]);
00367 #else
00368   if ((x >= _x) || (x < 0) || (y >= _y) || (y < 0) || (z >= _z) || (z < 0) || (t >= _t) || (t < 0)) {
00369     cout << "irtkGenericImage<Type>::GetPointerToVoxels: parameter out of range\n";
00370     cout << x << " " << y << " " << z << " " << t << endl;
00371     return NULL;
00372   } else {
00373     return &(_matrix[t][z][y][x]);
00374   }
00375 #endif
00376 }
00377 
00378 template <class VoxelType> inline void *irtkGenericImage<VoxelType>::GetScalarPointer(int x, int y, int z, int t) const
00379 {
00380 #ifdef NO_BOUNDS
00381   return &(_matrix[t][z][y][x]);
00382 #else
00383   if ((x >= _x) || (x < 0) || (y >= _y) || (y < 0) || (z >= _z) || (z < 0) || (t >= _t) || (t < 0)) {
00384     cout << "irtkGenericImage<Type>::GetScalarPointer: parameter out of range\n";
00385     cout << x << " " << y << " " << z << " " << t << endl;
00386     return NULL;
00387   } else {
00388     return &(_matrix[t][z][y][x]);
00389   }
00390 #endif
00391 }
00392 
00393 template <> inline int irtkGenericImage<char>::GetScalarType() const
00394 {
00395    return IRTK_VOXEL_CHAR;
00396 }
00397 
00398 template <> inline int irtkGenericImage<unsigned char>::GetScalarType() const
00399 {
00400    return IRTK_VOXEL_UNSIGNED_CHAR;
00401 }
00402 
00403 template <> inline int irtkGenericImage< unsigned short>::GetScalarType() const
00404 {
00405    return IRTK_VOXEL_UNSIGNED_SHORT;
00406 }
00407 
00408 template <> inline int irtkGenericImage<short>::GetScalarType() const
00409 {
00410    return IRTK_VOXEL_SHORT;
00411 }
00412 
00413 template <> inline int irtkGenericImage<float>::GetScalarType() const
00414 {
00415    return IRTK_VOXEL_FLOAT;
00416 }
00417 
00418 template <> inline int irtkGenericImage<double>::GetScalarType() const
00419 {
00420    return IRTK_VOXEL_DOUBLE;
00421 }
00422 
00423 template <class VoxelType> inline double irtkGenericImage<VoxelType>::GetScalarTypeMin() const
00424 {
00425    return std::numeric_limits<VoxelType>::min();
00426 }
00427 
00428 template <class VoxelType> inline double irtkGenericImage<VoxelType>::GetScalarTypeMax() const
00429 {
00430    return std::numeric_limits<VoxelType>::max();
00431 }
00432 
00433 #endif
00434