00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #ifndef __cmtkTypedArray_h_included_
00034 #define __cmtkTypedArray_h_included_
00035
00036 #include <cmtkconfig.h>
00037
00038 #include <Base/cmtkMacros.h>
00039 #include <Base/cmtkTypes.h>
00040 #include <Base/cmtkMathUtil.h>
00041 #include <Base/cmtkDataTypeTraits.h>
00042 #include <Base/cmtkHistogram.h>
00043 #include <Base/cmtkTypedArrayFunction.h>
00044
00045 #include <System/cmtkSmartPtr.h>
00046 #include <System/cmtkSmartConstPtr.h>
00047 #include <System/cmtkException.h>
00048
00049 #include <math.h>
00050 #include <stdlib.h>
00051 #include <stdio.h>
00052 #include <limits.h>
00053
00054 #include <memory.h>
00055
00056 #include <vector>
00057
00058 #ifdef DEBUG
00059 #define CheckBounds(index,bound) \
00060 if (!(index<bound)) throw( Exception( "Index is outside bounds", this ) );
00061 #else
00062 #define CheckBounds(index,bound)
00063 #endif
00064
00065 namespace
00066 cmtk
00067 {
00068
00071
00076 class TypedArray
00077 {
00079 cmtkGetSetMacro(DataClass,DataClass);
00080
00081 public:
00083 typedef TypedArray Self;
00084
00086 typedef SmartPointer<Self> SmartPtr;
00087
00089 typedef SmartConstPointer<Self> SmartConstPtr;
00090
00102 static Self::SmartPtr Create
00103 ( const ScalarDataType dtype, void *const data, const size_t size, const bool freeArray = true, const bool paddingFlag = false, const void* paddingData = NULL );
00104
00113 static Self::SmartPtr Create( const ScalarDataType dtype, const size_t size );
00114
00116 static void Free( void *const data )
00117 {
00118 free( data );
00119 }
00120
00126 virtual bool Get ( Types::DataItem&, const size_t ) const = 0;
00127
00135 virtual void GetSequence ( Types::DataItem *const values, const size_t index, const size_t length ) const = 0;
00136
00138 virtual void Set ( const Types::DataItem, const size_t ) = 0;
00139
00141 virtual ScalarDataType GetType () const = 0;
00142
00144 virtual size_t GetItemSize () const = 0;
00145
00148 virtual void* GetPaddingPtr () const = 0;
00149
00151 virtual Types::DataItem GetPaddingValue () const = 0;
00152
00154 virtual bool PaddingDataAt ( const size_t index ) const = 0;
00155
00160 virtual Types::DataItem* GetData () const = 0;
00161
00169 virtual void SetData( Types::DataItem *const data ) = 0;
00170
00173 virtual Self::SmartPtr Convert( const ScalarDataType dtype ) const = 0;
00174
00179 virtual void* ConvertSubArray( const ScalarDataType dtype, const size_t fromIdx, const size_t len ) const = 0;
00180
00183 virtual void ConvertSubArray( void *const destination, const ScalarDataType dtype, const size_t fromIdx, const size_t len ) const = 0;
00184
00191 virtual void* ConvertArray ( const ScalarDataType dtype ) const
00192 {
00193 return this->ConvertSubArray( dtype, 0, DataSize );
00194 }
00195
00203 virtual void ClearArray ( const bool usePaddingData = false ) = 0;
00204
00207 virtual void ChangeEndianness() = 0;
00208
00216 virtual void Rescale( const Types::DataItem scale = 1, const Types::DataItem offset = 0 ) = 0;
00217
00220 virtual void RescaleAndShift( const Types::DataItem scale = 1, const Types::DataItem offset = 0, const size_t shiftBits = 0 ) = 0;
00221
00224 virtual void RescaleToRange( const Types::DataItemRange& toRange );
00225
00229 virtual void GammaCorrection( const Types::DataItem gamma ) = 0;
00230
00232 typedef double (*FunctionTypeDouble)(const double);
00233
00235 typedef float (*FunctionTypeFloat)(const float);
00236
00238 virtual void ApplyFunction( Self::FunctionTypeFloat f )
00239 {
00240 this->ApplyFunctionFloat( f );
00241 }
00242
00244 virtual void ApplyFunction( Self::FunctionTypeDouble f )
00245 {
00246 this->ApplyFunctionDouble( f );
00247 }
00248
00251 virtual void ApplyFunctionFloat( Self::FunctionTypeFloat f ) = 0;
00252
00255 virtual void ApplyFunctionDouble( Self::FunctionTypeDouble f ) = 0;
00256
00258 virtual void MakeAbsolute() = 0;
00259
00273 virtual void Rescale( const Types::DataItem scale, const Types::DataItem offset, const Types::DataItem truncLo, const Types::DataItem truncHi = CMTK_ITEM_MAX ) = 0;
00274
00279 virtual void Threshold( const Types::DataItemRange& range ) = 0;
00280
00285 virtual void ThresholdToPadding( const Types::DataItemRange& range ) = 0;
00286
00293 virtual void PruneHistogram( const bool pruneHi, const bool pruneLo, const size_t numberOfBinsTarget, const size_t numberOfBinsInternal = 1024 );
00294
00299 virtual void Binarize( const Types::DataItem threshold = 0 ) = 0;
00300
00306 Self::SmartPtr Clone() const
00307 {
00308 return Self::SmartPtr( this->CloneVirtual() );
00309 }
00310
00312 TypedArray ()
00313 {
00314 DataSize = 0;
00315 PaddingFlag = false;
00316 FreeArray = false;
00317 this->m_DataClass = DATACLASS_GREY;
00318 }
00319
00323 virtual ~TypedArray () {}
00324
00329 virtual void FreeData() = 0;
00330
00334 void ReleaseDataPointer ()
00335 {
00336 FreeArray = false;
00337 }
00338
00342 size_t GetDataSize () const { return DataSize; }
00343
00346 size_t GetDataSizeBytes () const
00347 {
00348 return this->GetItemSize() * DataSize;
00349 }
00350
00354 virtual void* GetDataPtr( const size_t offset = 0) = 0;
00355
00359 virtual const void* GetDataPtr( const size_t offset = 0) const = 0;
00360
00364 virtual Types::DataItem* GetSubArray( Types::DataItem *const, const size_t, const size_t, const Types::DataItem = 0 ) const = 0;
00365
00369 virtual Types::DataItem* GetSubArray( const size_t, const size_t, const Types::DataItem = 0 ) const = 0;
00370
00375 bool GetPaddingFlag () const { return PaddingFlag; }
00376
00378 void ClearPaddingFlag()
00379 {
00380 this->PaddingFlag = false;
00381 }
00382
00384 virtual void SetPaddingAt ( const size_t ) = 0;
00385
00387 virtual void SetPaddingValue ( const Types::DataItem paddingData ) = 0;
00388
00390 virtual void SetPaddingPtr ( const void* paddingData ) = 0;
00391
00393 virtual void ReplacePaddingData ( const Types::DataItem value = 0 ) = 0;
00394
00396 virtual bool IsPaddingAt( const size_t index ) const = 0;
00397
00399 virtual bool IsPaddingOrZeroAt( const size_t index ) const = 0;
00400
00402 virtual const Types::DataItemRange GetRange() const = 0;
00403
00405 virtual double GetEntropy( const bool fractional = CMTK_HISTOGRAM_DISCRETE, const int numberOfBins = 128 ) const = 0;
00406
00418 virtual double GetEntropy( Histogram<unsigned int>& histogram ) const = 0;
00419
00423 virtual double GetEntropy( Histogram<double>& histogram, const bool fractional = CMTK_HISTOGRAM_DISCRETE ) const = 0;
00424
00428 virtual double GetEntropy( Histogram<double>& histogram, const double* kernel, const size_t kernelRadius ) const = 0;
00429
00435 virtual size_t GetStatistics ( Types::DataItem& mean, Types::DataItem& variance ) const = 0;
00436
00439 virtual Types::DataItem GetPercentile
00440 ( const Types::DataItem percentile,
00441 const size_t nBins = 256
00442 ) const;
00443
00450 virtual std::vector<Types::DataItem> GetPercentileList
00451 ( const std::vector<Types::DataItem>& percentileList,
00452 const size_t nBins = 256
00453 ) const;
00454
00459 virtual Histogram<unsigned int>::SmartPtr GetHistogram( const unsigned int numberOfBins ,
00460 const bool centeredBins = false ) const = 0;
00461
00464 virtual void BlockSet( const Types::DataItem value, const size_t fromOffset, const size_t toOffset ) = 0;
00465
00468 virtual void Fill( const Types::DataItem value )
00469 {
00470 this->BlockSet( value, 0, this->GetDataSize() );
00471 }
00472
00476 virtual void BlockCopy( Self& target, const size_t toOffset, const size_t fromOffset, const size_t blockLength ) const
00477 {
00478 this->ConvertSubArray( target.GetDataPtr( toOffset ), target.GetType(), fromOffset, blockLength );
00479 }
00480
00485 virtual void BlockSwap( const size_t fromOffset, const size_t toOffset, const size_t blockLength );
00486
00491 virtual void BlockReverse( const size_t fromOffset, const size_t blockLength );
00492
00495 virtual void ApplyFunctionObject( const TypedArrayFunction& f ) = 0;
00496
00497 protected:
00503 ScalarDataType m_DataType;
00504
00506 bool FreeArray;
00507
00509 size_t DataSize;
00510
00513 bool PaddingFlag;
00514
00516 virtual void Alloc ( const size_t datasize ) = 0;
00517
00519 virtual Self* CloneVirtual() const = 0;
00520 };
00521
00523
00524 }
00525
00526 #endif // #ifndef __cmtkTypedArray_h_included_