MeVisLab Toolbox Reference
mlLUTIterator.h
Go to the documentation of this file.
1 /*************************************************************************************
2 **
3 ** Copyright 2007, MeVis Medical Solutions AG
4 **
5 ** The user may use this file in accordance with the license agreement provided with
6 ** the Software or, alternatively, in accordance with the terms contained in a
7 ** written agreement between the user and MeVis Medical Solutions AG.
8 **
9 ** For further information use the contact form at https://www.mevislab.de/contact
10 **
11 **************************************************************************************/
12 
13 #ifndef ML_LUTITERATOR_H
14 #define ML_LUTITERATOR_H
15 
16 
18 
19 #include "mlLUTSystem.h"
20 #include "mlLUTBasic.h"
21 #include "mlLUTData.h"
22 
23 
24 
25 ML_START_NAMESPACE
26 
27 
30 {
31 protected:
32 
34  LUTIteratorBase (LUTVisualType vtFrom, LUTVisualType vtTo, int width, int row, int layer);
35 
36 
37 public:
38 
40  virtual ~LUTIteratorBase () {}
41 
42 
44  enum
45  {
46  L = 0,
47  A_LA = 1,
48  R = 0,
49  G = 1,
50  B = 2,
51  A_RGBA = 3,
52  MAX_CHANNELS = 4,
53  };
54 
55 
57  bool isValid (void) const { return _valid; }
58 
60  LUTVisualType getSourceType (void) const { return _vtFrom; }
61 
63  LUTVisualType getTargetType (void) const { return _vtTo; }
64 
66  bool alphaNeedsInit (void) const { return _initAlpha; }
67 
69  int getWidth (void) const { return _width; }
70 
72  inline int operator () (void) const { return _index; }
73 
75  int getRow (void) const { return _row; }
76 
78  int getLayer (void) const { return _layer; }
79 
81  inline bool atEnd (void) const { return _index >= _width; }
82 
84  inline int getNumChannels (void) const { return LUTNumberOfChannels(_vtFrom); }
85 
86 
87 protected:
88 
90  bool _valid;
91 
94 
97 
99  enum ConvMethod { CONV_NONE = 0, CONV_L_TO_RGB, CONV_RGB_TO_L } _convMethodId;
100 
103 
106 
108  int _width;
109 
111  int _index;
112 
114  int _row, _layer;
115 
116 };
117 
118 
119 
126 template <typename T>
128 {
129 public:
130 
134  LUTIterator (LUTVisualType visualType, LUTData<T> &lutData);
135 
140  LUTIterator (LUTVisualType visualType, LUTData<T> &lutData, int row);
141 
146  LUTIterator (LUTVisualType visualType, LUTData<T> &lutData, int row, int layer);
147 
148 
150  ~LUTIterator () override { clear(); }
151 
152 
154  inline void operator ++ (void) { ++_index; (this->*_inc)(); }
155 
156 
158  T **chn;
159 
160 
161 protected:
162 
164  void init (LUTData<T> &lutData);
165 
167  void clear (void);
168 
170  void inc1 (void) { chn[0] += _dpLut; }
171  void inc2 (void) { chn[0] += _dpLut; chn[1] += _dpLut; }
172  void inc3 (void) { chn[0] += _dpLut; chn[1] += _dpLut; chn[2] += _dpLut; }
173  void inc4 (void) { chn[0] += _dpLut; chn[1] += _dpLut; chn[2] += _dpLut; chn[3] += _dpLut; }
174  void inc5 (void) { chn[0] += _dpLut; chn[3] += _dpLut; }
175 
177  void convertLtoRGB (void) { chn[L][2] = chn[L][1] = chn[L][0]; }
178 
180  void convertRGBtoL (void)
181  { *chn[L] = LUTCastFromDouble<T>(LUTConvertRGBtoL(static_cast<double>(*chn[R]),
182  static_cast<double>(*chn[G]),
183  static_cast<double>(*chn[B]))); }
184 
185 
187  void convertAndInc (void) { (this->*_incConvert)(); (this->*_incPointers)(); }
188 
189 
191  typedef void (LUTIterator<T>::*IncrementFnc) (void);
192 
194  IncrementFnc _inc, _incConvert, _incPointers;
195 
198 
200  int _dpLut;
201 
202 };
203 
204 
208 template <typename T>
210  : LUTIteratorBase(visualType, lutData.getVisualType(), lutData.getWidth(),
211  lutData.getMinRow(), lutData.getMinLayer())
212 { init(lutData); }
213 
214 
219 template <typename T>
220 LUTIterator<T>::LUTIterator (LUTVisualType visualType, LUTData<T> &lutData, int row)
221  : LUTIteratorBase(visualType, lutData.getVisualType(), lutData.getWidth(),
222  row, lutData.getMinLayer())
223 { init(lutData); }
224 
225 
230 template <typename T>
231 LUTIterator<T>::LUTIterator (LUTVisualType visualType, LUTData<T> &lutData, int row, int layer)
232  : LUTIteratorBase(visualType, lutData.getVisualType(), lutData.getWidth(),
233  row, layer)
234 { init(lutData); }
235 
236 
238 template <typename T>
240 {
241  chn = nullptr;
242  _inc = nullptr;
243  _incConvert = nullptr;
244  _incPointers = nullptr;
245  _dummy = nullptr;
246  _dpLut = lutData.getStride(1);
247 
248  if (_valid)
249  {
250  // Allocate dummy channel entries and channel pointers
251  try
252  {
253  _dummy = new T[MAX_CHANNELS];
254  chn = new T* [MAX_CHANNELS];
255  for (int i = 0; i < MAX_CHANNELS; i++)
256  chn[i] = _dummy+i;
257  }
258  catch (...)
259  {
260  clear();
261  }
262 
263  // Allocation successful?
264  if (chn && _dummy)
265  {
266  // Set conversion method
267  switch (_convMethodId)
268  {
269  case CONV_NONE: break;
270  case CONV_L_TO_RGB: _incConvert = &LUTIterator<T>::convertLtoRGB; break;
271  case CONV_RGB_TO_L: _incConvert = &LUTIterator<T>::convertRGBtoL; break;
272  default: _valid = false;
273  }
274  // Set pointer increment method
275  switch (_incMethodId)
276  {
277  case 1: _incPointers = &LUTIterator<T>::inc1; break;
278  case 2: _incPointers = &LUTIterator<T>::inc2; break;
279  case 3: _incPointers = &LUTIterator<T>::inc3; break;
280  case 4: _incPointers = &LUTIterator<T>::inc4; break;
281  case 5: _incPointers = &LUTIterator<T>::inc5; break;
282  default: _valid = false;
283  }
284  // Set main increment method
285  if (_incConvert)
287  else
288  _inc = _incPointers;
289 
290  // Initialize channel pointers
291  T *pLut = lutData.getEntriesAt(lutData.getMinIndex(), _row, _layer);
292  switch (LUTReducedVisualType(getSourceType()))
293  {
294  case LUT_L:
295  chn[L] = pLut+(_vtTo == LUT_ABGR ? 1 : 0);
296  break;
297 
298  case LUT_RGB:
299  switch (lutData.getVisualType())
300  {
301  case LUT_L:
302  case LUT_LA: chn[R] = pLut; break;
303 
304  case LUT_RGB:
305  case LUT_RGBA: chn[R] = pLut; chn[G] = pLut+1; chn[B] = pLut+2; break;
306 
307  case LUT_ABGR: ++pLut; // fall through
308  case LUT_BGR: chn[B] = pLut; chn[G] = pLut+1; chn[R] = pLut+2; break;
309 
310  default: _valid = false;
311  }
312  break;
313 
314  default: _valid = false;
315  }
316 
317  // Alpha channel pointer
318  if (_valid)
319  {
320  // Index at which alpha channel is stored in LUT data object
321  int destAlpha = LUTAlphaChannelIndex(getTargetType());
322  if (destAlpha >= 0)
323  {
324  // Index at which source expects alpha channel pointer
325  int srcAlpha = LUTNumberOfChannels(LUTReducedVisualType(getSourceType()));
326  chn[srcAlpha] = lutData.getEntriesAt(lutData.getMinIndex(), _row, _layer)+destAlpha;
327  }
328  }
329  }
330  }
331 }
332 
333 
335 template <typename T>
337 {
338  if (chn)
339  {
340  delete[] chn;
341  chn = nullptr;
342  }
343  if (_dummy)
344  {
345  delete[] _dummy;
346  _dummy = nullptr;
347  }
348 }
349 
350 
351 
352 ML_END_NAMESPACE
353 
354 
355 #endif
356 // __mlLUTIterator_H
@ R
Definition: SoKeyGrabber.h:69
@ T
Definition: SoKeyGrabber.h:71
@ G
Definition: SoKeyGrabber.h:58
@ B
Definition: SoKeyGrabber.h:53
@ L
Definition: SoKeyGrabber.h:63
LUTVisualType getVisualType(void) const
Get visual type.
Definition: mlLUTData.h:110
int getMinIndex(void) const
Get minimum index value.
Definition: mlLUTData.h:119
int getStride(int dim) const
Get entry stride for index (dim = 1), row (2) and layer (3) dimension, relative to entry size,...
A LUTData object stores an instance of a LUT, rendered for a specified visual type,...
Definition: mlLUTData.h:243
T * getEntriesAt(int index, int row, int layer)
Get LUT entries at given index, row and layer.
Definition: mlLUTData.h:312
LUT iterator base class with type independent functionality and declarations.
Definition: mlLUTIterator.h:30
ConvMethod
Conversion method id.
Definition: mlLUTIterator.h:99
virtual ~LUTIteratorBase()
Destructor (declared here to make it virtual)
Definition: mlLUTIterator.h:40
int _incMethodId
Pointer increment method id (= number of channel pointers to increment)
int _index
Current index.
bool _valid
Iterator valid flag.
Definition: mlLUTIterator.h:90
bool alphaNeedsInit(void) const
Alpha channel needs initialization.
Definition: mlLUTIterator.h:66
int getWidth(void) const
Get LUT width.
Definition: mlLUTIterator.h:69
LUTVisualType getTargetType(void) const
Get visual type used for storing (target type)
Definition: mlLUTIterator.h:63
LUTVisualType _vtFrom
Source visual type, used by the LUT being rendered.
Definition: mlLUTIterator.h:93
int getLayer(void) const
Get current layer.
Definition: mlLUTIterator.h:78
int _width
Width of LUTData object.
bool isValid(void) const
Is iterator valid?
Definition: mlLUTIterator.h:57
bool _initAlpha
Alpha channel needs initialization.
LUTIteratorBase(LUTVisualType vtFrom, LUTVisualType vtTo, int width, int row, int layer)
Constructor (protected, class can only be used by derived classes)
LUTVisualType getSourceType(void) const
Get visual type used for rendering (source type)
Definition: mlLUTIterator.h:60
int getRow(void) const
Get current row.
Definition: mlLUTIterator.h:75
LUTVisualType _vtTo
Target visual type, used by the LUTData object storing the rendered LUT.
Definition: mlLUTIterator.h:96
int getNumChannels(void) const
Get number of (source) channels.
Definition: mlLUTIterator.h:84
bool atEnd(void) const
End of index range reached?
Definition: mlLUTIterator.h:81
The LUTIterator class template is used by a LUTFunction object for rendering into a LUTData object.
void inc3(void)
void convertLtoRGB(void)
Convert Luminance to RGB.
IncrementFnc _inc
Increment method pointers.
void clear(void)
Clean up.
void inc5(void)
T * _dummy
Dummy channel entries.
LUTIterator(LUTVisualType visualType, LUTData< T > &lutData)
Constructor for addressing a 1D-LUT visualType is the type of the LUT being rendered,...
void inc2(void)
T ** chn
Channel pointer array.
void convertAndInc(void)
Increment method including conversion.
~LUTIterator() override
Destructor.
void inc4(void)
void inc1(void)
! Increment methods
void convertRGBtoL(void)
Convert RGB to Luminance.
int _dpLut
Pointer increment.
void init(LUTData< T > &lutData)
Initialize.
#define MLLUT_EXPORT
System specific macro definitions.
Definition: mlLUTSystem.h:25
void init()
Initializes the ML, the runtime type system, the memory manager, fields, static buffers,...
MLLUT_EXPORT double LUTConvertRGBtoL(double r, double g, double b)
Convert RGB to luminance.
MLLUT_EXPORT int LUTNumberOfChannels(LUTVisualType visualType)
Get number of channels for given LUTVisualType, or 0 if unknown.
LUTVisualType
LUT visual type constants to identify the visual interpretation of the individual LUT channels.
Definition: mlLUTBasic.h:30
@ LUT_ABGR
Alpha, Blue, Green, Red.
Definition: mlLUTBasic.h:38
@ LUT_BGR
Blue, Green, Red.
Definition: mlLUTBasic.h:37
@ LUT_RGBA
Red, Green, Blue, Alpha.
Definition: mlLUTBasic.h:34
@ LUT_L
Luminance (Gray)
Definition: mlLUTBasic.h:31
@ LUT_LA
Luminance and Alpha.
Definition: mlLUTBasic.h:32
@ LUT_RGB
Red, Green, Blue.
Definition: mlLUTBasic.h:33
MLLUT_EXPORT int LUTAlphaChannelIndex(LUTVisualType visualType)
Get index of alpha channel, or -1 if none.
MLLUT_EXPORT LUTVisualType LUTReducedVisualType(LUTVisualType visualType)
Get reduced visual type, i.e. ignoring layout variants.