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
26
27
30{
31protected:
32
35
36
37public:
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
87protected:
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
109
112
114 int _row, _layer;
115
116};
117
118
119
126template <typename T>
128{
129public:
130
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
159
160
161protected:
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
201
202};
203
204
208template <typename T>
210 : LUTIteratorBase(visualType, lutData.getVisualType(), lutData.getWidth(),
211 lutData.getMinRow(), lutData.getMinLayer())
212{ init(lutData); }
213
214
219template <typename T>
221 : LUTIteratorBase(visualType, lutData.getVisualType(), lutData.getWidth(),
222 row, lutData.getMinLayer())
223{ init(lutData); }
224
225
230template <typename T>
232 : LUTIteratorBase(visualType, lutData.getVisualType(), lutData.getWidth(),
233 row, layer)
234{ init(lutData); }
235
236
238template <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
335template <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
353
354
355#endif
356// __mlLUTIterator_H
@ R
@ T
@ G
@ B
@ L
A LUTData object stores an instance of a LUT, rendered for a specified visual type,...
Definition mlLUTData.h:243
LUT iterator base class with type independent functionality and declarations.
ConvMethod
Conversion method id.
virtual ~LUTIteratorBase()
Destructor (declared here to make it virtual)
int _incMethodId
Pointer increment method id (= number of channel pointers to increment)
int _index
Current index.
bool _valid
Iterator valid flag.
bool alphaNeedsInit(void) const
Alpha channel needs initialization.
int getWidth(void) const
Get LUT width.
LUTVisualType getTargetType(void) const
Get visual type used for storing (target type)
LUTVisualType _vtFrom
Source visual type, used by the LUT being rendered.
int getLayer(void) const
Get current layer.
int _width
Width of LUTData object.
bool isValid(void) const
Is iterator valid?
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)
int getRow(void) const
Get current row.
LUTVisualType _vtTo
Target visual type, used by the LUTData object storing the rendered LUT.
int getNumChannels(void) const
Get number of (source) channels.
bool atEnd(void) const
End of index range reached?
The LUTIterator class template is used by a LUTFunction object for rendering into a LUTData object.
void convertLtoRGB(void)
Convert Luminance to RGB.
IncrementFnc _inc
Increment method pointers.
void clear(void)
Clean up.
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,...
T ** chn
Channel pointer array.
void convertAndInc(void)
Increment method including conversion.
~LUTIterator() override
Destructor.
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
Target mlrange_cast(Source arg)
Generic version of checked ML casts.
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.