MeVisLabToolboxReference
MeVisLab/Standard/Sources/Shared/MLLUT/mlLUTIterator.h
Go to the documentation of this file.
00001 // **InsertLicense** code
00002 //----------------------------------------------------------------------------------
00004 
00010 //----------------------------------------------------------------------------------
00011 
00012 
00013 #ifndef __mlLUTIterator_H
00014 #define __mlLUTIterator_H
00015 
00016 // Local includes
00017 #ifndef __mlLUTSystem_H
00018 #include "mlLUTSystem.h"
00019 #endif
00020 #ifndef __mlLUTBasic_H
00021 #include "mlLUTBasic.h"
00022 #endif
00023 #ifndef __mlLUTData_H
00024 #include "mlLUTData.h"
00025 #endif
00026 
00027 
00028 
00029 ML_START_NAMESPACE
00030 
00031 
00033 class MLLUT_EXPORT LUTIteratorBase
00034 {
00035 protected:
00036 
00038  LUTIteratorBase (LUTVisualType vtFrom, LUTVisualType vtTo, int width, int row, int layer);
00039 
00040 
00041 public:
00042 
00044   virtual ~LUTIteratorBase () {}
00045 
00046 
00048   enum 
00049   {
00050     L = 0,              
00051     A_LA = 1,           
00052     R = 0,              
00053     G = 1,              
00054     B = 2,              
00055     A_RGBA = 3,         
00056     MAX_CHANNELS = 4,   
00057   };
00058 
00059   
00061   bool isValid (void) const { return _valid; }
00062 
00064   LUTVisualType getSourceType (void) const { return _vtFrom; }
00065 
00067   LUTVisualType getTargetType (void) const { return _vtTo; }
00068 
00070   bool alphaNeedsInit (void) const { return _initAlpha; }
00071 
00073   int getWidth (void) const { return _width; }
00074 
00076   inline int operator () (void) const { return _index; }
00077 
00079   int getRow (void) const { return _row; }
00080 
00082   int getLayer (void) const { return _layer; }
00083 
00085   inline bool atEnd (void) const { return _index >= _width; }
00086 
00088   inline int getNumChannels (void) const { return LUTNumberOfChannels(_vtFrom); }
00089 
00090 
00091 protected:
00092 
00094   bool _valid;
00095 
00097   LUTVisualType _vtFrom;
00098 
00100   LUTVisualType _vtTo;
00101 
00103   enum ConvMethod { CONV_NONE = 0, CONV_L_TO_RGB, CONV_RGB_TO_L } _convMethodId;
00104 
00106   int _incMethodId;
00107 
00109   bool _initAlpha;
00110 
00112   int _width;
00113 
00115   int _index;
00116 
00118   int _row, _layer;
00119 
00120 };
00121 
00122 
00123 
00130 template <typename T> 
00131 class LUTIterator : public LUTIteratorBase
00132 {
00133 public:
00134 
00138   LUTIterator (LUTVisualType visualType, LUTData<T> &lutData);
00139 
00144   LUTIterator (LUTVisualType visualType, LUTData<T> &lutData, int row);
00145 
00150   LUTIterator (LUTVisualType visualType, LUTData<T> &lutData, int row, int layer);
00151 
00152 
00154   virtual ~LUTIterator () { clear(); }
00155 
00156 
00158   inline void operator ++ (void) { ++_index; (this->*_inc)(); }
00159 
00160 
00162   T **chn;
00163   
00164 
00165 protected:
00166 
00168   void init (LUTData<T> &lutData);
00169 
00171   void clear (void);
00172 
00174   void inc1 (void) { chn[0] += _dpLut; }
00175   void inc2 (void) { chn[0] += _dpLut; chn[1] += _dpLut; }
00176   void inc3 (void) { chn[0] += _dpLut; chn[1] += _dpLut; chn[2] += _dpLut; }
00177   void inc4 (void) { chn[0] += _dpLut; chn[1] += _dpLut; chn[2] += _dpLut; chn[3] += _dpLut; }
00178   void inc5 (void) { chn[0] += _dpLut; chn[3] += _dpLut; }
00179 
00181   void convertLtoRGB (void) { chn[L][2] = chn[L][1] = chn[L][0]; }
00182 
00184   void convertRGBtoL (void) 
00185   { *chn[L] = LUTCastFromDouble<T>(LUTConvertRGBtoL((double)(*chn[R]), (double)(*chn[G]), (double)(*chn[B]))); }
00186 
00187 
00189   void convertAndInc (void) { (this->*_incConvert)(); (this->*_incPointers)(); }
00190 
00191 
00193   typedef void (LUTIterator<T>::*IncrementFnc) (void);
00194 
00196   IncrementFnc _inc, _incConvert, _incPointers;
00197 
00199   T *_dummy;
00200 
00202   int _dpLut;
00203 
00204 };
00205 
00206 
00210 template <typename T>
00211 LUTIterator<T>::LUTIterator (LUTVisualType visualType, LUTData<T> &lutData)
00212   : LUTIteratorBase(visualType, lutData.getVisualType(), lutData.getWidth(), 
00213                     lutData.getMinRow(), lutData.getMinLayer())
00214 { init(lutData); }
00215 
00216 
00221 template <typename T>
00222 LUTIterator<T>::LUTIterator (LUTVisualType visualType, LUTData<T> &lutData, int row)
00223   : LUTIteratorBase(visualType, lutData.getVisualType(), lutData.getWidth(), 
00224                     row, lutData.getMinLayer())
00225 { init(lutData); }
00226 
00227 
00232 template <typename T>
00233 LUTIterator<T>::LUTIterator (LUTVisualType visualType, LUTData<T> &lutData, int row, int layer)
00234   : LUTIteratorBase(visualType, lutData.getVisualType(), lutData.getWidth(), 
00235                     row, layer)
00236 { init(lutData); }
00237 
00238 
00240 template <typename T>
00241 void LUTIterator<T>::init (LUTData<T> &lutData)
00242 {
00243   chn = 0; 
00244   _inc = 0;
00245   _incConvert = 0;
00246   _incPointers = 0;
00247   _dummy = 0;
00248   _dpLut = lutData.getStride(1);
00249 
00250   if (_valid)
00251   {
00252     // Allocate dummy channel entries and channel pointers
00253     try
00254     {
00255       _dummy = new T[MAX_CHANNELS];
00256       chn = new T* [MAX_CHANNELS];
00257       for (int i = 0; i < MAX_CHANNELS; i++)
00258         chn[i] = _dummy+i;
00259     }
00260     catch (...)
00261     {
00262       clear();
00263     }
00264 
00265     // Allocation successful?
00266     if (chn && _dummy)
00267     {
00268       // Set conversion method
00269       switch (_convMethodId)
00270       {
00271         case CONV_NONE: break;
00272         case CONV_L_TO_RGB: _incConvert = &LUTIterator<T>::convertLtoRGB; break;
00273         case CONV_RGB_TO_L: _incConvert = &LUTIterator<T>::convertRGBtoL; break;
00274         default: _valid = false;
00275       }
00276       // Set pointer increment method
00277       switch (_incMethodId)
00278       {
00279         case 1: _incPointers = &LUTIterator<T>::inc1; break;
00280         case 2: _incPointers = &LUTIterator<T>::inc2; break;
00281         case 3: _incPointers = &LUTIterator<T>::inc3; break;
00282         case 4: _incPointers = &LUTIterator<T>::inc4; break;
00283         case 5: _incPointers = &LUTIterator<T>::inc5; break;
00284         default: _valid = false;
00285       }
00286       // Set main increment method
00287       if (_incConvert)
00288         _inc = &LUTIterator<T>::convertAndInc;
00289       else
00290         _inc = _incPointers;
00291 
00292       // Initialize channel pointers
00293       T *pLut = lutData.getEntriesAt(lutData.getMinIndex(), _row, _layer);
00294       switch (LUTReducedVisualType(getSourceType()))
00295       {
00296         case LUT_L:
00297           chn[L] = pLut+(_vtTo == LUT_ABGR ? 1 : 0);
00298           break;
00299 
00300         case LUT_RGB:
00301           switch (lutData.getVisualType())
00302           {
00303             case LUT_L:    
00304             case LUT_LA:   chn[R] = pLut; break;
00305 
00306             case LUT_RGB:  
00307             case LUT_RGBA: chn[R] = pLut; chn[G] = pLut+1; chn[B] = pLut+2; break;
00308 
00309             case LUT_ABGR: ++pLut; // fall through
00310             case LUT_BGR:  chn[B] = pLut; chn[G] = pLut+1; chn[R] = pLut+2; break;
00311 
00312             default: _valid = false;
00313           }
00314           break;
00315 
00316         default: _valid = false;
00317       }
00318 
00319       // Alpha channel pointer
00320       if (_valid)
00321       {
00322         // Index at which alpha channel is stored in LUT data object
00323         int destAlpha = LUTAlphaChannelIndex(getTargetType());
00324         if (destAlpha >= 0)
00325         {
00326           // Index at which source expects alpha channel pointer
00327           int srcAlpha = LUTNumberOfChannels(LUTReducedVisualType(getSourceType()));
00328           chn[srcAlpha] = lutData.getEntriesAt(lutData.getMinIndex(), _row, _layer)+destAlpha;
00329         }
00330       }
00331     }
00332   }
00333 }
00334 
00335 
00337 template <typename T>
00338 void LUTIterator<T>::clear (void)
00339 {
00340   if (chn)
00341   {
00342     delete[] chn;
00343     chn = 0;
00344   }
00345   if (_dummy)
00346   {
00347     delete[] _dummy;
00348     _dummy = 0;
00349   }
00350 }
00351 
00352 
00353 
00354 ML_END_NAMESPACE
00355 
00356 
00357 #endif
00358 // __mlLUTIterator_H