Debian Codegraph @

libs/img/src/color_maps.cpp

raw
1 /* +------------------------------------------------------------------------+
2 | Mobile Robot Programming Toolkit (MRPT) |
3 | https://www.mrpt.org/ |
4 | |
5 | Copyright (c) 2005-2026, Individual contributors, see AUTHORS file |
6 | See: https://www.mrpt.org/Authors - All rights reserved. |
7 | Released under BSD License. See: https://www.mrpt.org/License |
8 +------------------------------------------------------------------------+ */
9
10 #include "img-precomp.h" // Precompiled headers
11 //
12 #include <mrpt/img/color_maps.h>
13
14 #include <Eigen/Dense>
15 #include <mrpt/math/interp_fit.hpp>
16
17 using namespace mrpt;
18 using namespace mrpt::img;
19 using namespace std;
20
21 /*-------------------------------------------------------------
22 hsv2rgb
23 -------------------------------------------------------------*/
24 void mrpt::img::hsv2rgb(float h, float s, float v, float& r, float& g, float& b)
25 {
26 // See: http://en.wikipedia.org/wiki/HSV_color_space
27 h = max(0.0f, min(1.0f, h));
28 s = max(0.0f, min(1.0f, s));
29 v = max(0.0f, min(1.0f, v));
30
31 int Hi = ((int)floor(h * 6)) % 6;
32 float f = (h * 6) - Hi;
33 float p = v * (1 - s);
34 float q = v * (1 - f * s);
35 float t = v * (1 - (1 - f) * s);
36
37 switch (Hi)
38 {
39 case 0:
40 r = v;
41 g = t;
42 b = p;
43 break;
44 case 1:
45 r = q;
46 g = v;
47 b = p;
48 break;
49 case 2:
50 r = p;
51 g = v;
52 b = t;
53 break;
54 case 3:
55 r = p;
56 g = q;
57 b = v;
58 break;
59 case 4:
60 r = t;
61 g = p;
62 b = v;
63 break;
64 case 5:
65 r = v;
66 g = p;
67 b = q;
68 break;
69 }
70 }
71
72 /*-------------------------------------------------------------
73 rgb2hsv
74 -------------------------------------------------------------*/
75 void mrpt::img::rgb2hsv(float r, float g, float b, float& h, float& s, float& v)
76 {
77 // See: http://en.wikipedia.org/wiki/HSV_color_space
78 r = max(0.0f, min(1.0f, r));
79 g = max(0.0f, min(1.0f, g));
80 b = max(0.0f, min(1.0f, b));
81
82 float Max = max3(r, g, b);
83 float Min = min3(r, g, b);
84
85 if (Max == Min)
86 {
87 h = 0;
88 }
89 else
90 {
91 if (Max == r)
92 {
93 if (g >= b)
94 h = (g - b) / (6 * (Max - Min));
95 else
96 h = 1 - (g - b) / (6 * (Max - Min));
97 }
98 else if (Max == g)
99 h = 1 / 3.0f + (b - r) / (6 * (Max - Min));
100 else
101 h = 2 / 3.0f + (r - g) / (6 * (Max - Min));
102 }
103
104 if (Max == 0)
105 s = 0;
106 else
107 s = 1 - Min / Max;
108
109 v = Max;
110 }
111
112 /*-------------------------------------------------------------
113 colormap
114 -------------------------------------------------------------*/
115 void mrpt::img::colormap(
116 const TColormap& color_map, const float col_indx_in, float& r, float& g, float& b)
117 {
118 MRPT_START
119
120 const float color_index = std::min(1.0f, std::max(.0f, col_indx_in));
121
122 switch (color_map)
123 {
124 case cmJET:
125 jet2rgb(color_index, r, g, b);
126 break;
127 case cmGRAYSCALE:
128 r = g = b = color_index;
129 break;
130 case cmHOT:
131 hot2rgb(color_index, r, g, b);
132 break;
133 default:
134 THROW_EXCEPTION("Invalid color_map");
135 };
136 MRPT_END
137 }
138
139 /*-------------------------------------------------------------
140 jet2rgb
141 -------------------------------------------------------------*/
142 void mrpt::img::jet2rgb(const float color_index, float& r, float& g, float& b)
143 {
144 static bool jet_table_done = false;
145 static Eigen::VectorXf jet_r, jet_g, jet_b;
146
147 // Initialize tables
148 if (!jet_table_done)
149 {
150 jet_table_done = true;
151
152 // Refer to source code of "jet" in MATLAB:
153 float JET_R[] = {0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
154 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
155 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
156 0.0625f, 0.125f, 0.1875f, 0.250f, 0.3125f, 0.375f, 0.4375f, 0.5f,
157 0.5625f, 0.625f, 0.6875f, 0.750f, 0.8125f, 0.875f, 0.9375f, 1.0f,
158 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
159 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
160 0.9375f, 0.875f, 0.8125f, 0.750f, 0.6875f, 0.625f, 0.5625f, 0.500000};
161 float JET_G[] = {0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
162 0.0625f, 0.125f, 0.1875f, 0.250f, 0.3125f, 0.375f, 0.4375f, 0.5f,
163 0.5625f, 0.625f, 0.6875f, 0.750f, 0.8125f, 0.875f, 0.9375f, 1.0f,
164 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
165 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
166 0.9375f, 0.875f, 0.8125f, 0.750f, 0.6875f, 0.625f, 0.5625f, 0.5f,
167 0.4375f, 0.375f, 0.3125f, 0.250f, 0.1875f, 0.125f, 0.0625f, 0.0f,
168 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.000000};
169 float JET_B[] = {0.5625f, 0.625f, 0.6875f, 0.750f, 0.8125f, 0.875f, 0.9375f, 1.0f,
170 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
171 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
172 0.9375f, 0.875f, 0.8125f, 0.750f, 0.6875f, 0.625f, 0.5625f, 0.5f,
173 0.4375f, 0.375f, 0.3125f, 0.250f, 0.1875f, 0.125f, 0.0625f, 0.0f,
174 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
175 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
176 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.000000};
177 const int N = sizeof(JET_B) / sizeof(JET_B[0]);
178
179 jet_r.resize(N);
180 jet_g.resize(N);
181 jet_b.resize(N);
182 for (int i = 0; i < N; i++)
183 {
184 jet_r[i] = JET_R[i];
185 jet_g[i] = JET_G[i];
186 jet_b[i] = JET_B[i];
187 }
188 }
189
190 // Return interpolate value:
191 r = math::interpolate(color_index, jet_r, 0.0f, 1.0f);
192 g = math::interpolate(color_index, jet_g, 0.0f, 1.0f);
193 b = math::interpolate(color_index, jet_b, 0.0f, 1.0f);
194 }
195
196 void mrpt::img::hot2rgb(const float color_index, float& r, float& g, float& b)
197 {
198 static bool table_done = false;
199 static Eigen::VectorXf hot_r, hot_g, hot_b;
200
201 // Initialize tables
202 if (!table_done)
203 {
204 table_done = true;
205
206 // Refer to source code of "hot" in MATLAB:
207 float HOT_R[] = {
208 0.041667f, 0.0833f, 0.125f, 0.166667f, 0.2083f, 0.250f, 0.291667f, 0.3333f,
209 0.375f, 0.416667f, 0.4583f, 0.5f, 0.541667f, 0.5833f, 0.625f, 0.666667f,
210 0.7083f, 0.750f, 0.791667f, 0.8333f, 0.875f, 0.916667f, 0.9583f, 1.0f,
211 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
212 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
213 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
214 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
215 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f};
216 float HOT_G[] = {
217 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
218 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
219 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
220 0.041667f, 0.0833f, 0.125f, 0.166667f, 0.2083f, 0.250f, 0.291667f, 0.3333f,
221 0.375f, 0.416667f, 0.4583f, 0.5f, 0.541667f, 0.5833f, 0.625f, 0.666667f,
222 0.7083f, 0.750f, 0.791667f, 0.8333f, 0.875f, 0.916667f, 0.9583f, 1.0f,
223 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
224 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f};
225 float HOT_B[] = {0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
226 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
227 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
228 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
229 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
230 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
231 0.0625f, 0.125f, 0.1875f, 0.250f, 0.3125f, 0.375f, 0.4375f, 0.5f,
232 0.5625f, 0.625f, 0.6875f, 0.750f, 0.8125f, 0.875f, 0.9375f, 1.0f};
233 const int N = sizeof(HOT_B) / sizeof(HOT_B[0]);
234
235 hot_r.resize(N);
236 hot_g.resize(N);
237 hot_b.resize(N);
238 for (int i = 0; i < N; i++)
239 {
240 hot_r[i] = HOT_R[i];
241 hot_g[i] = HOT_G[i];
242 hot_b[i] = HOT_B[i];
243 }
244 }
245
246 // Return interpolate value:
247 r = math::interpolate(color_index, hot_r, 0.0f, 1.0f);
248 g = math::interpolate(color_index, hot_g, 0.0f, 1.0f);
249 b = math::interpolate(color_index, hot_b, 0.0f, 1.0f);
250 }
251
252 mrpt::img::TColor mrpt::img::colormap(const TColormap& color_map, const float color_index)
253 {
254 float r, g, b;
255 colormap(color_map, color_index, r, g, b);
256 return mrpt::img::TColorf(r, g, b).asTColor();
257 }