Scope
lut.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "pixel.h"
4 
5 namespace scope {
6 
11 extern const std::array<BGRA8Pixel,256> RainbowTable;
12 
14 template<int32_t LBound = 0, int32_t UBound = 255, class TResult = uint8_t, class TArg = uint16_t>
15 class LUT_function {
16 
17 protected:
19  std::array<TResult, UBound - LBound + 1> lut_array;
20 
22  typename std::array<TResult, UBound - LBound + 1>::iterator lut;
23 
24 public:
28  explicit LUT_function ( std::function<TResult(double)> f, const double& coeff = 1 ) {
29  lut = lut_array.begin() + LBound;
30  for ( uint32_t i = LBound ; i <= UBound ; ++i )
31  *(lut+i) = f(i*coeff);
32  }
33 
35  const TResult& operator() (TArg i) const {
36  return *(lut+i);
37  }
38 };
39 
41 enum ColorEnum { None, Gray, Red, Green, Blue, Yellow, Rainbow, Limits };
42 
44 extern const std::array<CString, 8> gColorStrings;
45 
47 class ColorProps {
48 
49 protected:
51  mutable std::mutex mutex;
52 
54  ColorEnum col;
55 
57  uint16_t ll;
58 
60  uint16_t ul;
61 
62 protected:
63 
64 public:
67  ColorProps(const ColorEnum& _col = None, const uint16_t& _ll = 0, const uint16_t& _ul = UINT16_MAX >> 1)
68  : col(_col)
69  , ll(_ll)
70  , ul(_ul) {
71  }
72 
74  ColorProps(const ColorProps& cp) {
75  col = cp.col;
76  ll = cp.ll;
77  ul = cp.ul;
78  }
79 
82  col = cp.Color();
83  ll = cp.LowerLimit();
84  ul = cp.UpperLimit();
85  return *this;
86  }
87 
90  uint16_t Range() const { std::lock_guard<std::mutex> lock(mutex); return ul-ll; }
91  ColorEnum Color() const { std::lock_guard<std::mutex> lock(mutex); return col; }
92  uint16_t LowerLimit() const { std::lock_guard<std::mutex> lock(mutex); return ll; }
93  uint16_t UpperLimit() const { std::lock_guard<std::mutex> lock(mutex); return ul; }
98  void SetColor(const ColorEnum& _col) { std::lock_guard<std::mutex> lock(mutex); col = _col; }
99  void SetLowerLimit(const uint16_t& _ll) { std::lock_guard<std::mutex> lock(mutex); ll = _ll; }
100  void SetUpperLimit(const uint16_t& _ul) { std::lock_guard<std::mutex> lock(mutex); ul = _ul; }
104  operator ColorEnum() const { std::lock_guard<std::mutex> lock(mutex); return Color(); }
105 };
106 
108 #define BGRA8BLACK BGRA8Pixel(0)
109 
111 #define BGRA8WHITE BGRA8Pixel(255)
112 
114 inline BGRA8Pixel U16ToBGRA8(const uint16_t& gray, const ColorEnum& pixelcolor) {
115  switch (pixelcolor) {
116  case Gray:
117  return BGRA8Pixel(to8(gray));
118  case Blue:
119  return BGRA8Pixel(to8(gray), 0U, 0U);
120  case Green:
121  return BGRA8Pixel(0U, to8(gray), 0U);
122  case Red:
123  return BGRA8Pixel(0U, 0U, to8(gray));
124  case Yellow:
125  return BGRA8Pixel(0U, to8(gray), to8(gray));
126  case None:
127  return BGRA8Pixel(0U);
128  case Rainbow:
129  return RainbowTable[to8(gray)];
130  case Limits:
131  return ( (to8(gray) == 0)
132  ? BGRA8Pixel(255,0,0)
133  : (to8(gray) == 255)
134  ? BGRA8Pixel(0,0,255)
135  : BGRA8Pixel(to8(gray))
136  );
137  default:
138  return BGRA8Pixel(0U);
139  }
140 }
141 
143 inline BGRA8Pixel U16ToBGRA8Histo(const uint16_t& gray, const ColorEnum& pixelcolor, const uint16_t& l, const uint16_t& u) {
144  const uint8_t tmp8 = to8hist_mod(gray, l, u);
145  switch (pixelcolor) {
146  case Gray:
147  return BGRA8Pixel(tmp8);
148  case Blue:
149  return BGRA8Pixel(tmp8, 0U, 0U);
150  case Green:
151  return BGRA8Pixel(0U, tmp8, 0U);
152  case Red:
153  return BGRA8Pixel(0U, 0U, tmp8);
154  case Yellow:
155  return BGRA8Pixel(0U, tmp8, tmp8);
156  case None:
157  return BGRA8Pixel(0U);
158  case Rainbow:
159  return RainbowTable[tmp8];
160  case Limits:
161  return ( (tmp8 == 0)
162  ? BGRA8Pixel(255,0,0)
163  : (tmp8 == 255)
164  ? BGRA8Pixel(0,0,255)
165  : BGRA8Pixel(tmp8)
166  );
167  default:
168  return BGRA8Pixel(0U);
169  }
170 }
171 
173 inline BGRA8Pixel U16ToBGRA8Histo2(const uint16_t& gray, const ColorEnum& pixelcolor, const uint16_t& l, const uint16_t& u, const uint16_t& scaler) {
174  const uint8_t tmp8 = to8hist_mod2(gray, l, u, scaler);
175  switch (pixelcolor) {
176  case Gray:
177  return BGRA8Pixel(tmp8);
178  case Blue:
179  return BGRA8Pixel(tmp8, 0U, 0U);
180  case Green:
181  return BGRA8Pixel(0U, tmp8, 0U);
182  case Red:
183  return BGRA8Pixel(0U, 0U, tmp8);
184  case Yellow:
185  return BGRA8Pixel(0U, tmp8, tmp8);
186  case None:
187  return BGRA8Pixel(0U);
188  case Rainbow:
189  return RainbowTable[tmp8];
190  case Limits:
191  return ( (tmp8 == 0)
192  ? BGRA8Pixel(255,0,0)
193  : (tmp8 == 255)
194  ? BGRA8Pixel(0,0,255)
195  : BGRA8Pixel(tmp8)
196  );
197  default:
198  return BGRA8Pixel(0U);
199  }
200 }
201 
202 }
std::array< TResult, UBound-LBound+1 >::iterator lut
iterator to beginning of lookup table
Definition: lut.h:22
const TResult & operator()(TArg i) const
Definition: lut.h:35
std::mutex mutex
for protection
Definition: lut.h:51
LUT_function(std::function< TResult(double)> f, const double &coeff=1)
Fills lookup table with function f.
Definition: lut.h:28
The BGRA8Pixel struct and various related helpers for color conversions.
class for generating a lookup table by providing a function to the constructor
Definition: lut.h:15
ColorProps(const ColorEnum &_col=None, const uint16_t &_ll=0, const uint16_t &_ul=UINT16_MAX >> 1)
NI-DAQ devices have range of +-x volts digitized to int16, we read this as uint16, thus only values from 0-32767 are used => maximum pixel value is 65534 / 2 = 32767.
Definition: lut.h:67
std::array< TResult, UBound-LBound+1 > lut_array
the lookup table
Definition: lut.h:19
uint16_t ul
upper limit to display
Definition: lut.h:60
uint16_t ll
lower limit to display
Definition: lut.h:57
ColorProps(const ColorProps &cp)
Safe copy constructor.
Definition: lut.h:74
ColorProps & operator=(const ColorProps &cp)
Safe assignment.
Definition: lut.h:81
ColorEnum col
the color map type
Definition: lut.h:54
Encapsulated lower and upper limit for range adjustment and color for displaying. ...
Definition: lut.h:47