Ocean
FrameConverterColorMap.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #ifndef META_OCEAN_CV_FRAME_CONVERTER_COLOR_MAP_H
9 #define META_OCEAN_CV_FRAME_CONVERTER_COLOR_MAP_H
10 
11 #include "ocean/cv/CV.h"
13 #include "ocean/cv/FrameChannels.h"
14 #include "ocean/cv/FrameMinMax.h"
15 
16 #include "ocean/base/Worker.h"
17 
18 namespace Ocean
19 {
20 
21 namespace CV
22 {
23 
24 /**
25  * This class provides functions to convert frames based on a color map.
26  * @ingroup cv
27  */
28 class OCEAN_CV_EXPORT FrameConverterColorMap : public FrameConverter
29 {
30  public:
31 
32  /**
33  * Definition of individual color maps.
34  */
35  enum ColorMap : uint32_t
36  {
37  /// An invalid color map
38  CM_INVALID = 0u,
39  /// The Jet color map, as implemented in convertJetFloat1ChannelToRGB24().
41  /// The Turbo color map, as implemented in convertTurboFloat1ChannelToRGB24().
43  /// The Inferno color map, as implemented in convertInfernoFloat1ChannelToRGB24().
45  /// The Spiral color map with 10 spiral rounds, as implemented in convertSpiralFloat1ChannelToRGB24().
47  /// The Spiral color map with 20 spiral rounds, as implemented in convertSpiralFloat1ChannelToRGB24().
49  /// A linear normalization of the input creating an RGB image with values between black and white.
50  CM_LINEAR
51  };
52 
53  /**
54  * Definition of a vector holding color map values.
55  */
56  using ColorMaps = std::vector<ColorMap>;
57 
58  /**
59  * The following comfort class provides comfortable functions simplifying prototyping applications but also increasing binary size of the resulting applications.
60  * Best practice is to avoid using these functions if binary size matters,<br>
61  * as for every comfort function a corresponding function exists with specialized functionality not increasing binary size significantly.<br>
62  */
63  class OCEAN_CV_EXPORT Comfort
64  {
65  public:
66 
67  /**
68  * Converts a 1-channel frame to a RGB24 frame using a specified color map.
69  * This function automatically determines the minimum and maximum value range of the source frame before applying the color map, infinity and NaN values are ignored and set to black.
70  * The source frame can hold 32-bit or 64 bit floating point elements, or 8-bit, 16-bit, 32-bit or 64-bit integer elements.
71  * @param source The source frame, must be valid
72  * @param target The target frame, will be modified in case the target frame type does not fit
73  * @param colorMap The color map to be used when converting the source frame
74  * @param worker Optional worker object to distribute the computational load
75  * @return True, if succeeded
76  */
77  static bool convert1ChannelToRGB24(const Frame& source, Frame& target, const ColorMap colorMap, Worker* worker = nullptr);
78 
79  /**
80  * Converts a 1-channel float frame to a RGB24 frame using a specified color map.
81  * If minValue and/or maxValue are not specified, this function automatically determines the minimum and maximum value range of the source frame before applying the color map, infinity and NaN values are ignored and set to black.
82  * The source frame can hold 32-bit or 64-bit floating point elements.
83  * @param source The source frame, must be valid
84  * @param target The target frame, will be modified in case the target frame type does not fit
85  * @param colorMap The color map to be used when converting the source frame
86  * @param worker Optional worker object to distribute the computational load
87  * @param minValue Optional minimum value used to apply the color map, with range (-infinity, infinity), NumericF::maxValue() to let the function determine the value
88  * @param maxValue Optional maximum value used to apply the color map, with range (minValue, infinity), NumericF::minValue() to let the function determine the value
89  * @return True, if succeeded
90  */
91  static bool convertFloat1ChannelToRGB24(const Frame& source, Frame& target, const ColorMap colorMap, Worker* worker = nullptr, float minValue = NumericF::maxValue(), float maxValue = NumericF::minValue());
92 
93  /**
94  * Converts a 1-channel integer frame to a RGB24 frame using a specified color map.
95  * This function automatically determines the minimum and maximum value range of the source frame before applying the color map, infinity and NaN values are ignored and set to black.
96  * The source frame can hold 8-bit, 16-bit, 32-bit or 64-bit integer elements.
97  * @param source The source frame, must be valid
98  * @param target The target frame, will be modified in case the target frame type does not fit
99  * @param colorMap The color map to be used when converting the source frame
100  * @param worker Optional worker object to distribute the computational load
101  * @return True, if succeeded
102  */
103  static bool convertInteger1ChannelToRGB24(const Frame& source, Frame& target, const ColorMap colorMap, Worker* worker = nullptr);
104  };
105 
106  protected:
107 
108  /**
109  * Helper class for converters with lookup table.
110  */
112  {
113  public:
114 
115  /**
116  * Creates a new lookup object.
117  * @param minValue The minimal source value, with range (-infinity, infinity)
118  * @param maxValue The minimal source value, with range (minValue, infinity)
119  * @param lookupTable The actual lookup table, must be valid
120  */
121  inline LookupData(const float minValue, const float maxValue, const uint8_t* const lookupTable);
122 
123  /**
124  * Return the minimal source value.
125  * @return The minimal source value
126  */
127  inline float minValue() const;
128 
129  /**
130  * Return the maximal source value.
131  * @return The maximal source value
132  */
133  inline float maxValue() const;
134 
135  /**
136  * Returns the actual lookup table.
137  * @return The lookup table
138  */
139  inline const uint8_t* lookupTable() const;
140 
141  protected:
142 
143  /// The minimal source value, with range (-infinity, infinity).
144  const float minValue_;
145 
146  /// The minimal source value, with range (minValue, infinity).
147  const float maxValue_;
148 
149  /// The actual lookup table.
150  const uint8_t* const lookupTable_;
151  };
152 
153  public:
154 
155  /**
156  * Converts a row of a 1-channel float frame to a row of a RGB24 frame using the Jet color map.
157  * Infinity and NaN values are ignored and set to black.
158  * @param source The source frame buffer, must be valid
159  * @param target The target frame buffer, must be valid
160  * @param width The width of the frame in pixel, with range (0, infinity)
161  * @param height The height of the frame in pixel, with range (0, infinity)
162  * @param flag Determining the type of conversion
163  * @param sourcePaddingElements The number of padding elements at the end of each source row, in (uint16_t) elements, with range [0, infinity)
164  * @param targetPaddingElements The number of padding elements at the end of each target row, in (uint8_t) elements, with range [0, infinity)
165  * @param minSource The minimal source value, with range (-infinity, infinity)
166  * @param maxSource The minimal source value, with range (minValue, infinity)
167  * @param worker Optional worker object to distribute the computational load
168  */
169  static inline void convertJetFloat1ChannelToRGB24(const float* const source, uint8_t* const target, const unsigned int width, const unsigned int height, const ConversionFlag flag, const unsigned int sourcePaddingElements, const unsigned int targetPaddingElements, const float minSource = 0.0f, const float maxSource = 1.0f, Worker* worker = nullptr);
170 
171  /**
172  * Converts a row of a 1-channel float frame to a row of a RGB24 frame using the Turbo color map.
173  * Infinity and NaN values are ignored and set to black.
174  * @param source The source frame buffer, must be valid
175  * @param target The target frame buffer, must be valid
176  * @param width The width of the frame in pixel, with range (0, infinity)
177  * @param height The height of the frame in pixel, with range (0, infinity)
178  * @param flag Determining the type of conversion
179  * @param sourcePaddingElements The number of padding elements at the end of each source row, in (uint16_t) elements, with range [0, infinity)
180  * @param targetPaddingElements The number of padding elements at the end of each target row, in (uint8_t) elements, with range [0, infinity)
181  * @param minSource The minimal source value, with range (-infinity, infinity)
182  * @param maxSource The minimal source value, with range (minValue, infinity)
183  * @param worker Optional worker object to distribute the computational load
184  */
185  static void convertTurboFloat1ChannelToRGB24(const float* const source, uint8_t* const target, const unsigned int width, const unsigned int height, const ConversionFlag flag, const unsigned int sourcePaddingElements, const unsigned int targetPaddingElements, const float minSource = 0.0f, const float maxSource = 1.0f, Worker* worker = nullptr);
186 
187  /**
188  * Converts a row of a 1-channel float frame to a row of a RGB24 frame using the Inferno color map.
189  * Infinity and NaN values are ignored and set to black.
190  * @param source The source frame buffer, must be valid
191  * @param target The target frame buffer, must be valid
192  * @param width The width of the frame in pixel, with range (0, infinity)
193  * @param height The height of the frame in pixel, with range (0, infinity)
194  * @param flag Determining the type of conversion
195  * @param sourcePaddingElements The number of padding elements at the end of each source row, in (uint16_t) elements, with range [0, infinity)
196  * @param targetPaddingElements The number of padding elements at the end of each target row, in (uint8_t) elements, with range [0, infinity)
197  * @param minSource The minimal source value, with range (-infinity, infinity)
198  * @param maxSource The minimal source value, with range (minValue, infinity)
199  * @param worker Optional worker object to distribute the computational load
200  */
201  static void convertInfernoFloat1ChannelToRGB24(const float* const source, uint8_t* const target, const unsigned int width, const unsigned int height, const ConversionFlag flag, const unsigned int sourcePaddingElements, const unsigned int targetPaddingElements, const float minSource = 0.0f, const float maxSource = 1.0f, Worker* worker = nullptr);
202 
203  /**
204  * Converts a row of a 1-channel float frame to a row of a RGB24 frame using the Spiral color map.
205  * Infinity and NaN values are ignored and set to black.
206  * @param source The source frame buffer, must be valid
207  * @param target The target frame buffer, must be valid
208  * @param width The width of the frame in pixel, with range (0, infinity)
209  * @param height The height of the frame in pixel, with range (0, infinity)
210  * @param flag Determining the type of conversion
211  * @param sourcePaddingElements The number of padding elements at the end of each source row, in (uint16_t) elements, with range [0, infinity)
212  * @param targetPaddingElements The number of padding elements at the end of each target row, in (uint8_t) elements, with range [0, infinity)
213  * @param minSource The minimal source value, with range (-infinity, infinity)
214  * @param maxSource The minimal source value, with range (minValue, infinity)
215  * @param period The width of one spiral round/period in defined in the domain of `minValue` and `maxValue`, with range (0, infinity)
216  * @param worker Optional worker object to distribute the computational load
217  */
218  static inline void convertSpiralFloat1ChannelToRGB24(const float* const source, uint8_t* const target, const unsigned int width, const unsigned int height, const ConversionFlag flag, const unsigned int sourcePaddingElements, const unsigned int targetPaddingElements, const float minSource = 0.0f, const float maxSource = 1.0f, const float period = 0.1f, Worker* worker = nullptr);
219 
220  /**
221  * Translates the name of a color map to it's value.
222  * @param colorMap The name of the color map, possible values are 'jet', 'turbo', 'inferno', 'spiral10', 'spiral20', 'linear'
223  * @return The resulting value of the color map, CM_INVALID if unknown
224  */
225  static ColorMap translateColorMap(const std::string& colorMap);
226  /**
227  * Translates the values of a color map to a readable string.
228  * @param colorMap The value of the color map
229  * @return The resulting readable string, empty if unknown
230  */
231  static std::string translateColorMap(const ColorMap colorMap);
232 
233  /**
234  * Returns all defined color maps.
235  * @return The definition of all color maps
236  */
237  static const ColorMaps& definedColorMaps();
238 
239  protected:
240 
241  /**
242  * Converts a row of a 1-channel float frame to a row of a RGB24 frame using the Jet color map.
243  * @param source The source row, must be valid
244  * @param target The target row, must be valid
245  * @param width The width of the row in pixel, with range [1, infinity)
246  * @param options The options parameter with two float values, must be valid
247  */
248  static void convertRowJetFloat1ChannelToRGB24(const float* source, uint8_t* target, const size_t width, const void* options);
249 
250  /**
251  * Converts a row of a 1-channel float frame to a row of a RGB24 frame using the Spiral color map.
252  * @param source The source row, must be valid
253  * @param target The target row, must be valid
254  * @param width The width of the row in pixel, with range [1, infinity)
255  * @param options The options parameter with three float values, must be valid
256  */
257  static void convertRowSpiralFloat1ChannelToRGB24(const float* source, uint8_t* target, const size_t width, const void* options);
258 
259  /**
260  * Converts a row of a 1-channel float frame to a row of a RGB24 frame using a lookup table.
261  * @param source The source row, must be valid
262  * @param target The target row, must be valid
263  * @param width The width of the row in pixel, with range [1, infinity)
264  * @param options The lookup data object, must be valid
265  */
266  static void convertRowLookupFloat1ChannelToRGB24(const float* source, uint8_t* target, const size_t width, const void* options);
267 };
268 
269 inline FrameConverterColorMap::LookupData::LookupData(const float minValue, const float maxValue, const uint8_t* const lookupTable) :
270  minValue_(minValue),
271  maxValue_(maxValue),
272  lookupTable_(lookupTable)
273 {
274  // nothing to do here
275 }
276 
278 {
279  return minValue_;
280 }
281 
283 {
284  return maxValue_;
285 }
286 
288 {
289  return lookupTable_;
290 }
291 
292 inline void FrameConverterColorMap::convertJetFloat1ChannelToRGB24(const float* const source, uint8_t* const target, const unsigned int width, const unsigned int height, const ConversionFlag flag, const unsigned int sourcePaddingElements, const unsigned int targetPaddingElements, const float minSource, const float maxSource, Worker* worker)
293 {
294  ocean_assert(source != nullptr && target != nullptr);
295  ocean_assert(width >= 1u && height >= 1u);
296  ocean_assert(minSource < maxSource);
297 
298  const unsigned int sourceStrideElements = width + sourcePaddingElements;
299  const unsigned int targetStrideElements = width * 3u + targetPaddingElements;
300 
301  const float options[2] =
302  {
303  minSource,
304  maxSource
305  };
306 
307  const bool areContinuous = sourcePaddingElements == 0u && targetPaddingElements == 0u;
308 
309  FrameConverter::convertGenericPixelFormat<float, uint8_t>(source, target, width, height, sourceStrideElements, targetStrideElements, flag, CV::FrameConverterColorMap::convertRowJetFloat1ChannelToRGB24, CV::FrameChannels::reverseRowPixelOrderInPlace<uint8_t, 3u>, areContinuous, options, worker);
310 }
311 
312 inline void FrameConverterColorMap::convertSpiralFloat1ChannelToRGB24(const float* const source, uint8_t* const target, const unsigned int width, const unsigned int height, const ConversionFlag flag, const unsigned int sourcePaddingElements, const unsigned int targetPaddingElements, const float minSource, const float maxSource, const float period, Worker* worker)
313 {
314  ocean_assert(source != nullptr && target != nullptr);
315  ocean_assert(width >= 1u && height >= 1u);
316  ocean_assert(minSource < maxSource);
317  ocean_assert(period > 0.0f);
318 
319  const unsigned int sourceStrideElements = width + sourcePaddingElements;
320  const unsigned int targetStrideElements = width * 3u + targetPaddingElements;
321 
322  const float options[3] =
323  {
324  minSource,
325  maxSource,
326  period
327  };
328 
329  const bool areContinuous = sourcePaddingElements == 0u && targetPaddingElements == 0u;
330 
331  FrameConverter::convertGenericPixelFormat<float, uint8_t>(source, target, width, height, sourceStrideElements, targetStrideElements, flag, CV::FrameConverterColorMap::convertRowSpiralFloat1ChannelToRGB24, CV::FrameChannels::reverseRowPixelOrderInPlace<uint8_t, 3u>, areContinuous, options, worker);
332 }
333 
334 }
335 
336 }
337 
338 #endif // META_OCEAN_CV_FRAME_CONVERTER_COLOR_MAP_H
The following comfort class provides comfortable functions simplifying prototyping applications but a...
Definition: FrameConverterColorMap.h:64
static bool convert1ChannelToRGB24(const Frame &source, Frame &target, const ColorMap colorMap, Worker *worker=nullptr)
Converts a 1-channel frame to a RGB24 frame using a specified color map.
static bool convertFloat1ChannelToRGB24(const Frame &source, Frame &target, const ColorMap colorMap, Worker *worker=nullptr, float minValue=NumericF::maxValue(), float maxValue=NumericF::minValue())
Converts a 1-channel float frame to a RGB24 frame using a specified color map.
static bool convertInteger1ChannelToRGB24(const Frame &source, Frame &target, const ColorMap colorMap, Worker *worker=nullptr)
Converts a 1-channel integer frame to a RGB24 frame using a specified color map.
Helper class for converters with lookup table.
Definition: FrameConverterColorMap.h:112
LookupData(const float minValue, const float maxValue, const uint8_t *const lookupTable)
Creates a new lookup object.
Definition: FrameConverterColorMap.h:269
float minValue() const
Return the minimal source value.
Definition: FrameConverterColorMap.h:277
const float minValue_
The minimal source value, with range (-infinity, infinity).
Definition: FrameConverterColorMap.h:144
const uint8_t *const lookupTable_
The actual lookup table.
Definition: FrameConverterColorMap.h:150
const float maxValue_
The minimal source value, with range (minValue, infinity).
Definition: FrameConverterColorMap.h:147
const uint8_t * lookupTable() const
Returns the actual lookup table.
Definition: FrameConverterColorMap.h:287
float maxValue() const
Return the maximal source value.
Definition: FrameConverterColorMap.h:282
This class provides functions to convert frames based on a color map.
Definition: FrameConverterColorMap.h:29
ColorMap
Definition of individual color maps.
Definition: FrameConverterColorMap.h:36
@ CM_JET
The Jet color map, as implemented in convertJetFloat1ChannelToRGB24().
Definition: FrameConverterColorMap.h:40
@ CM_TURBO
The Turbo color map, as implemented in convertTurboFloat1ChannelToRGB24().
Definition: FrameConverterColorMap.h:42
@ CM_SPIRAL_10
The Spiral color map with 10 spiral rounds, as implemented in convertSpiralFloat1ChannelToRGB24().
Definition: FrameConverterColorMap.h:46
@ CM_INFERNO
The Inferno color map, as implemented in convertInfernoFloat1ChannelToRGB24().
Definition: FrameConverterColorMap.h:44
@ CM_SPIRAL_20
The Spiral color map with 20 spiral rounds, as implemented in convertSpiralFloat1ChannelToRGB24().
Definition: FrameConverterColorMap.h:48
static void convertTurboFloat1ChannelToRGB24(const float *const source, uint8_t *const target, const unsigned int width, const unsigned int height, const ConversionFlag flag, const unsigned int sourcePaddingElements, const unsigned int targetPaddingElements, const float minSource=0.0f, const float maxSource=1.0f, Worker *worker=nullptr)
Converts a row of a 1-channel float frame to a row of a RGB24 frame using the Turbo color map.
static ColorMap translateColorMap(const std::string &colorMap)
Translates the name of a color map to it's value.
static void convertRowJetFloat1ChannelToRGB24(const float *source, uint8_t *target, const size_t width, const void *options)
Converts a row of a 1-channel float frame to a row of a RGB24 frame using the Jet color map.
static void convertSpiralFloat1ChannelToRGB24(const float *const source, uint8_t *const target, const unsigned int width, const unsigned int height, const ConversionFlag flag, const unsigned int sourcePaddingElements, const unsigned int targetPaddingElements, const float minSource=0.0f, const float maxSource=1.0f, const float period=0.1f, Worker *worker=nullptr)
Converts a row of a 1-channel float frame to a row of a RGB24 frame using the Spiral color map.
Definition: FrameConverterColorMap.h:312
static void convertJetFloat1ChannelToRGB24(const float *const source, uint8_t *const target, const unsigned int width, const unsigned int height, const ConversionFlag flag, const unsigned int sourcePaddingElements, const unsigned int targetPaddingElements, const float minSource=0.0f, const float maxSource=1.0f, Worker *worker=nullptr)
Converts a row of a 1-channel float frame to a row of a RGB24 frame using the Jet color map.
Definition: FrameConverterColorMap.h:292
static void convertRowLookupFloat1ChannelToRGB24(const float *source, uint8_t *target, const size_t width, const void *options)
Converts a row of a 1-channel float frame to a row of a RGB24 frame using a lookup table.
static void convertRowSpiralFloat1ChannelToRGB24(const float *source, uint8_t *target, const size_t width, const void *options)
Converts a row of a 1-channel float frame to a row of a RGB24 frame using the Spiral color map.
static std::string translateColorMap(const ColorMap colorMap)
Translates the values of a color map to a readable string.
std::vector< ColorMap > ColorMaps
Definition of a vector holding color map values.
Definition: FrameConverterColorMap.h:56
static const ColorMaps & definedColorMaps()
Returns all defined color maps.
static void convertInfernoFloat1ChannelToRGB24(const float *const source, uint8_t *const target, const unsigned int width, const unsigned int height, const ConversionFlag flag, const unsigned int sourcePaddingElements, const unsigned int targetPaddingElements, const float minSource=0.0f, const float maxSource=1.0f, Worker *worker=nullptr)
Converts a row of a 1-channel float frame to a row of a RGB24 frame using the Inferno color map.
This is the base class for all frame converter classes.
Definition: FrameConverter.h:32
ConversionFlag
Definition of individual conversion flags.
Definition: FrameConverter.h:39
This class implements Ocean's image class.
Definition: Frame.h:1792
static constexpr T minValue()
Returns the min scalar value.
Definition: Numeric.h:3250
static constexpr T maxValue()
Returns the max scalar value.
Definition: Numeric.h:3244
This class implements a worker able to distribute function calls over different threads.
Definition: Worker.h:33
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15