Ocean
Loading...
Searching...
No Matches
ColorChannelMapper.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_ADVANCED_COLOR_CHANNEL_MAPPER_H
9#define META_OCEAN_CV_ADVANCED_COLOR_CHANNEL_MAPPER_H
10
12
13#include "ocean/base/Callback.h"
14#include "ocean/base/Frame.h"
16#include "ocean/base/Worker.h"
17
18#include "ocean/math/Vector3.h"
19
20namespace Ocean
21{
22
23namespace CV
24{
25
26namespace Advanced
27{
28
29/**
30 * Provides methods for fast remapping of colors inside frames
31 * @ingroup cvadvanced
32 */
33class OCEAN_CV_ADVANCED_EXPORT ColorChannelMapper
34{
35 public:
36
37 /**
38 * Callback function for color mapping
39 * First parameter: color channel index
40 * Second parameter: color channel input value
41 * Return value: color channel output value
42 */
44
45 public:
46
47 /**
48 * Creates a look-up table using the specified color channel mapping function.
49 * The lookup data is a 2D image using 1 Byte per pixel.
50 * @param pixelFormat Pixel format
51 * @param lookupFrame Lookup table that is filled
52 * @param function The function that defines the color channel mapping
53 * @return True, if succeeded
54 */
55 static bool createLookup8BitsPerChannel(FrameType::PixelFormat pixelFormat, Frame& lookupFrame, const ColorChannelMapFunction& function);
56
57 /**
58 * Maps the colors of the specified frame to new values using the specified look-up table
59 * @param frame Image frame that is modified, must be valid
60 * @param lookupFrame Lookup table that is used, must be valid
61 * @param worker Optional worker object used for load distribution
62 */
63 static void applyLookup8BitsPerChannel(Frame& frame, const Frame& lookupFrame, Worker* worker = nullptr);
64
65 /**
66 * Creates a lookup table for color scaling and offset operation.
67 * This operation is only supported for frames using 8 bit per R, G and B channel and a zipped pixel format.
68 * The values of an optional alpha channel are not modified.
69 * @param pixelFormat Pixel format of image frame
70 * @param lookupFrame Lookup table that is filled
71 * @param scale Color scale factor
72 * @param offset Color offset
73 * @param gamma Exponent for gamma correction
74 */
75 static void createLookupScaleOffset(const FrameType::PixelFormat& pixelFormat, Frame& lookupFrame, const VectorF3& scale, const VectorF3& offset, const VectorF3& gamma);
76
77 /**
78 * Performs a color scaling and offset operation on the specified image frame.
79 * This operation is only supported for frames using 8 bit per R, G and B channel and a zipped pixel format.
80 * The values of an optional alpha channel are not modified.
81 * @param frame Image frame that is modified
82 * @param scale Color scale factor
83 * @param offset Color offset
84 * @param gamma Exponent for gamma correction
85 * @param worker Optional worker object used for load distribution
86 */
87 static void mapScaleOffset(Frame& frame, const VectorF3& scale, const VectorF3& offset, const VectorF3& gamma, Worker* worker = nullptr);
88
89 protected:
90
91 /**
92 * Maps the colors of the specified frame to new values using the specified look-up table for a specified subset of pixel rows.
93 * @param frameData Pointer to image frame data that is modified, must be valid
94 * @param frameWidth Width of frame in pixel, with range [1, infinity)
95 * @param frameHeight Height of frame in pixel, with range [1, infinity)
96 * @param framePaddingElements The number of padding elements at the end of each frame row, in elements, with range [0, infinity)
97 * @param lookupData Pointer to data of lookup table, must be valid
98 * @param firstRow Index of first pixel row, with range [0, frameHeight - 1]
99 * @param numberRows Number of pixel rows that are modified, with range [1, frameHeight - firstRow]
100 * @tparam tChannels The number of channels the frame has, with range [1, infinity)
101 */
102 template <unsigned int tChannels>
103 static void applyLookup8BitsPerChannelSubset(uint8_t* frameData, const unsigned int frameWidth, const unsigned int frameHeight, const unsigned int framePaddingElements, const uint8_t* lookupData, const unsigned int firstRow, const unsigned int numberRows);
104};
105
106/**
107 * Defines a scaling and offset color channel mapping
108 * @tparam tChannels Number of color channels
109 * @ingroup cvadvanced
110 */
111template <unsigned int tChannels>
113{
114
115 public:
116
117 /**
118 * Creates a new instance with an identity mapping
119 */
121
122 /**
123 * Creates a new instance with the specified scaling factor and offset.
124 * @param scale Color scaling factor
125 * @param offset Color offset
126 * @param gamma Exponent for gamma correction, should be greater than zero
127 */
128 ScaleOffsetMapping(const float* scale, const float* offset, const float* gamma = nullptr);
129
130 /**
131 * Maps a color value for the specified intensity index
132 * @param channel Color channel index
133 * @param input Color input value in the range of [0;255]
134 * @return Color output value in the range of [0;255]
135 */
136 uint8_t map(const unsigned int channel, const uint8_t input);
137
138 protected:
139
140 /// Specifies color scale factor for each color channel
141 float gamma_[tChannels];
142
143 /// Specifies color scale factor for each color channel
144 float scale_[tChannels];
145
146 /// Specifies color offset for each color channel
147 float offset_[tChannels];
148};
149
150template <unsigned int tChannels>
152{
153 for (unsigned int c = 0u; c < tChannels; c++)
154 {
155 scale_[c] = 1.0f;
156 offset_[c] = 0.0f;
157 gamma_[c] = 1.0f;
158 }
159}
160
161template <unsigned int tChannels>
162ScaleOffsetMapping<tChannels>::ScaleOffsetMapping(const float* scale, const float* offset, const float* gamma)
163{
164 for (unsigned int c = 0; c < tChannels; c++)
165 {
166 ocean_assert(gamma == nullptr || NumericF::isAbove(gamma[c], 0.0f));
167
168 scale_[c] = scale[c];
169 offset_[c] = offset == nullptr ? 0.0f : offset[c];
170 gamma_[c] = gamma == nullptr ? 1.0f : max(NumericF::eps(), gamma[c]);
171 }
172}
173
174template <unsigned int tChannels>
175uint8_t ScaleOffsetMapping<tChannels>::map(const unsigned int channel, const uint8_t input)
176{
177 if (channel >= tChannels)
178 {
179 return input;
180 }
181
182 constexpr float inv255 = 1.0f / 255.0f;
183
184 return uint8_t(minmax<int>(0u, int(((NumericF::pow(input * inv255, gamma_[channel]) * scale_[channel]) + offset_[channel]) * 255.0f + 0.5f), 255u));
185}
186
187template <unsigned int tChannels>
188void ColorChannelMapper::applyLookup8BitsPerChannelSubset(uint8_t* frameData, const unsigned int frameWidth, const unsigned int frameHeight, const unsigned int framePaddingElements, const uint8_t* lookupData, const unsigned int firstRow, const unsigned int numberRows)
189{
190 ocean_assert(frameData != nullptr && lookupData != nullptr);
191 ocean_assert(frameWidth > 0u && frameHeight > 0u);
192 ocean_assert_and_suppress_unused(numberRows <= frameHeight, frameHeight);
193
194 const unsigned int frameStrideElements = frameWidth * tChannels + framePaddingElements;
195
196 for (unsigned int y = firstRow; y < firstRow + numberRows; ++y)
197 {
198 uint8_t* data = frameData + y * frameStrideElements;
199
200 for (unsigned int x = 0u; x < frameWidth; ++x)
201 {
202 for (unsigned int c = 0u; c < tChannels; c++)
203 {
204 *data = lookupData[*data * tChannels + c];
205 ++data;
206 }
207 }
208 }
209}
210
211}
212
213}
214
215}
216
217#endif // META_OCEAN_CV_ADVANCED_COLOR_CHANNEL_MAPPER_H
Provides methods for fast remapping of colors inside frames.
Definition ColorChannelMapper.h:34
static void applyLookup8BitsPerChannel(Frame &frame, const Frame &lookupFrame, Worker *worker=nullptr)
Maps the colors of the specified frame to new values using the specified look-up table.
static void mapScaleOffset(Frame &frame, const VectorF3 &scale, const VectorF3 &offset, const VectorF3 &gamma, Worker *worker=nullptr)
Performs a color scaling and offset operation on the specified image frame.
Callback< uint8_t, unsigned int, uint8_t > ColorChannelMapFunction
Callback function for color mapping First parameter: color channel index Second parameter: color chan...
Definition ColorChannelMapper.h:43
static void applyLookup8BitsPerChannelSubset(uint8_t *frameData, const unsigned int frameWidth, const unsigned int frameHeight, const unsigned int framePaddingElements, const uint8_t *lookupData, const unsigned int firstRow, const unsigned int numberRows)
Maps the colors of the specified frame to new values using the specified look-up table for a specifie...
Definition ColorChannelMapper.h:188
static void createLookupScaleOffset(const FrameType::PixelFormat &pixelFormat, Frame &lookupFrame, const VectorF3 &scale, const VectorF3 &offset, const VectorF3 &gamma)
Creates a lookup table for color scaling and offset operation.
static bool createLookup8BitsPerChannel(FrameType::PixelFormat pixelFormat, Frame &lookupFrame, const ColorChannelMapFunction &function)
Creates a look-up table using the specified color channel mapping function.
Defines a scaling and offset color channel mapping.
Definition ColorChannelMapper.h:113
float gamma_[tChannels]
Specifies color scale factor for each color channel.
Definition ColorChannelMapper.h:141
float scale_[tChannels]
Specifies color scale factor for each color channel.
Definition ColorChannelMapper.h:144
float offset_[tChannels]
Specifies color offset for each color channel.
Definition ColorChannelMapper.h:147
uint8_t map(const unsigned int channel, const uint8_t input)
Maps a color value for the specified intensity index.
Definition ColorChannelMapper.h:175
ScaleOffsetMapping()
Creates a new instance with an identity mapping.
Definition ColorChannelMapper.h:151
This class implements a container for callback functions.
Definition Callback.h:3456
This class implements Ocean's image class.
Definition Frame.h:1808
PixelFormat
Definition of all pixel formats available in the Ocean framework.
Definition Frame.h:183
static T pow(const T x, const T y)
Returns x raised to the power of y.
Definition Numeric.h:1860
static constexpr bool isAbove(const T value, const T lower, const T epsilon=NumericT< T >::eps())
Returns whether a parameter lies on or above a given border tolerating a small epsilon.
Definition Numeric.h:2937
static constexpr T eps()
Returns a small epsilon.
This class implements a vector with three elements.
Definition Vector3.h:97
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