MeVisLab Toolbox Reference
Loading...
Searching...
No Matches
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
25
26
29{
30protected:
33
34
35public:
38 {}
39
40
42 enum {
43 L = 0,
44 A_LA = 1,
45 R = 0,
46 G = 1,
47 B = 2,
48 A_RGBA = 3,
49 MAX_CHANNELS = 4,
50 };
51
52
54 bool isValid(void) const
55 {
56 return _valid;
57 }
58
61 {
62 return _vtFrom;
63 }
64
67 {
68 return _vtTo;
69 }
70
72 bool alphaNeedsInit(void) const
73 {
74 return _initAlpha;
75 }
76
78 int getWidth(void) const
79 {
80 return _width;
81 }
82
84 inline int operator()(void) const
85 {
86 return _index;
87 }
88
90 int getRow(void) const
91 {
92 return _row;
93 }
94
96 int getLayer(void) const
97 {
98 return _layer;
99 }
100
102 inline bool atEnd(void) const
103 {
104 return _index >= _width;
105 }
106
108 inline int getNumChannels(void) const
109 {
110 return LUTNumberOfChannels(_vtFrom);
111 }
112
113
114protected:
116 bool _valid;
117
120
123
126 CONV_NONE = 0,
128 CONV_RGB_TO_L
129 } _convMethodId;
130
133
136
139
142
144 int _row, _layer;
145};
146
147
154template <typename T> class LUTIterator : public LUTIteratorBase
155{
156public:
161
166 LUTIterator(LUTVisualType visualType, LUTData<T>& lutData, int row);
167
172 LUTIterator(LUTVisualType visualType, LUTData<T>& lutData, int row, int layer);
173
174
176 ~LUTIterator() override
177 {
178 clear();
179 }
180
181
183 inline void operator++(void)
184 {
185 ++_index;
186 (this->*_inc)();
187 }
188
189
192
193
194protected:
196 void init(LUTData<T>& lutData);
197
199 void clear(void);
200
202 void inc1(void)
203 {
204 chn[0] += _dpLut;
205 }
206 void inc2(void)
207 {
208 chn[0] += _dpLut;
209 chn[1] += _dpLut;
210 }
211 void inc3(void)
212 {
213 chn[0] += _dpLut;
214 chn[1] += _dpLut;
215 chn[2] += _dpLut;
216 }
217 void inc4(void)
218 {
219 chn[0] += _dpLut;
220 chn[1] += _dpLut;
221 chn[2] += _dpLut;
222 chn[3] += _dpLut;
223 }
224 void inc5(void)
225 {
226 chn[0] += _dpLut;
227 chn[3] += _dpLut;
228 }
229
231 void convertLtoRGB(void)
232 {
233 chn[L][2] = chn[L][1] = chn[L][0];
234 }
235
237 void convertRGBtoL(void)
238 {
239 *chn[L] = LUTCastFromDouble<T>(
240 LUTConvertRGBtoL(static_cast<double>(*chn[R]), static_cast<double>(*chn[G]), static_cast<double>(*chn[B])));
241 }
242
243
245 void convertAndInc(void)
246 {
247 (this->*_incConvert)();
248 (this->*_incPointers)();
249 }
250
251
253 typedef void (LUTIterator<T>::*IncrementFnc)(void);
254
256 IncrementFnc _inc, _incConvert, _incPointers;
257
260
263};
264
265
270 : LUTIteratorBase(visualType, lutData.getVisualType(), lutData.getWidth(), lutData.getMinRow(), lutData.getMinLayer())
271{
272 init(lutData);
273}
274
275
280template <typename T> LUTIterator<T>::LUTIterator(LUTVisualType visualType, LUTData<T>& lutData, int row)
281 : LUTIteratorBase(visualType, lutData.getVisualType(), lutData.getWidth(), row, lutData.getMinLayer())
282{
283 init(lutData);
284}
285
286
291template <typename T> LUTIterator<T>::LUTIterator(LUTVisualType visualType, LUTData<T>& lutData, int row, int layer)
292 : LUTIteratorBase(visualType, lutData.getVisualType(), lutData.getWidth(), row, layer)
293{
294 init(lutData);
295}
296
297
299template <typename T> void LUTIterator<T>::init(LUTData<T>& lutData)
300{
301 chn = nullptr;
302 _inc = nullptr;
303 _incConvert = nullptr;
304 _incPointers = nullptr;
305 _dummy = nullptr;
306 _dpLut = lutData.getStride(1);
307
308 if (_valid) {
309 // Allocate dummy channel entries and channel pointers
310 try {
311 _dummy = new T[MAX_CHANNELS];
312 chn = new T*[MAX_CHANNELS];
313 for (int i = 0; i < MAX_CHANNELS; i++)
314 chn[i] = _dummy + i;
315 }
316 catch (...) {
317 clear();
318 }
319
320 // Allocation successful?
321 if (chn && _dummy) {
322 // Set conversion method
323 switch (_convMethodId) {
324 case CONV_NONE:
325 break;
326 case CONV_L_TO_RGB:
327 _incConvert = &LUTIterator<T>::convertLtoRGB;
328 break;
329 case CONV_RGB_TO_L:
330 _incConvert = &LUTIterator<T>::convertRGBtoL;
331 break;
332 default:
333 _valid = false;
334 }
335 // Set pointer increment method
336 switch (_incMethodId) {
337 case 1:
338 _incPointers = &LUTIterator<T>::inc1;
339 break;
340 case 2:
341 _incPointers = &LUTIterator<T>::inc2;
342 break;
343 case 3:
344 _incPointers = &LUTIterator<T>::inc3;
345 break;
346 case 4:
347 _incPointers = &LUTIterator<T>::inc4;
348 break;
349 case 5:
350 _incPointers = &LUTIterator<T>::inc5;
351 break;
352 default:
353 _valid = false;
354 }
355 // Set main increment method
356 if (_incConvert)
358 else
359 _inc = _incPointers;
360
361 // Initialize channel pointers
362 T* pLut = lutData.getEntriesAt(lutData.getMinIndex(), _row, _layer);
363 switch (LUTReducedVisualType(getSourceType())) {
364 case LUT_L:
365 chn[L] = pLut + (_vtTo == LUT_ABGR ? 1 : 0);
366 break;
367
368 case LUT_RGB:
369 switch (lutData.getVisualType()) {
370 case LUT_L:
371 case LUT_LA:
372 chn[R] = pLut;
373 break;
374
375 case LUT_RGB:
376 case LUT_RGBA:
377 chn[R] = pLut;
378 chn[G] = pLut + 1;
379 chn[B] = pLut + 2;
380 break;
381
382 case LUT_ABGR:
383 ++pLut; // fall through
384 case LUT_BGR:
385 chn[B] = pLut;
386 chn[G] = pLut + 1;
387 chn[R] = pLut + 2;
388 break;
389
390 default:
391 _valid = false;
392 }
393 break;
394
395 default:
396 _valid = false;
397 }
398
399 // Alpha channel pointer
400 if (_valid) {
401 // Index at which alpha channel is stored in LUT data object
402 int destAlpha = LUTAlphaChannelIndex(getTargetType());
403 if (destAlpha >= 0) {
404 // Index at which source expects alpha channel pointer
405 int srcAlpha = LUTNumberOfChannels(LUTReducedVisualType(getSourceType()));
406 chn[srcAlpha] = lutData.getEntriesAt(lutData.getMinIndex(), _row, _layer) + destAlpha;
407 }
408 }
409 }
410 }
411}
412
413
415template <typename T> void LUTIterator<T>::clear(void)
416{
417 if (chn) {
418 delete[] chn;
419 chn = nullptr;
420 }
421 if (_dummy) {
422 delete[] _dummy;
423 _dummy = nullptr;
424 }
425}
426
427
429
430
431#endif
432// __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:308
LUT iterator base class with type independent functionality and declarations.
ConvMethod
Conversion method id.
int operator()(void) const
Get current index (zero based)
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,...
void operator++(void)
Increment operator.
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.