Ocean
ColorChannelCurve.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_CURVE_H
9 #define META_OCEAN_CV_ADVANCED_COLOR_CHANNEL_CURVE_H
10 
12 
13 #include "ocean/base/Callback.h"
14 
15 #include "ocean/math/Numeric.h"
16 
17 namespace Ocean
18 {
19 
20 namespace CV
21 {
22 
23 namespace Advanced
24 {
25 
26 /**
27  * Implements a look-up table to perform fast transformations on a single color channel.
28  * @ingroup cvadvanced
29  */
30 class OCEAN_CV_ADVANCED_EXPORT ColorChannelCurve
31 {
32  public:
33 
34  /**
35  * Callback function type that returns a floating point value for the specified unsigned char input color value.
36  * Callback functions of this type can be used to define a color channel curve like gamma correction.
37  */
39 
40  /**
41  * Represents a specific color channel transformation
42  */
44  {
45  /// Returns input unmodified
47  /// Delinearization of SRGB values.
48  TT_LINEAR_SRGB
49  };
50 
51  public:
52 
53  /**
54  * Creates a new color channel curve using the specified transformation function preset.
55  * @param type Specifies the type of inbuilt transformation function to use
56  */
58 
59  /**
60  * Creates a new color channel curve using the specified transformation function.
61  * @param transformFunction Transformation function to use
62  */
63  inline ColorChannelCurve(const TransformationFunction& transformFunction);
64 
65  /**
66  * Linearizes a given component of an sRGB triplet.
67  * @param value Red, green or blue component of an sRGB triplet in the interval [0;255]
68  * @return Linearized value in the interval [0;1]
69  */
70  static inline Scalar linearizeSRGB(const unsigned char value);
71 
72  /**
73  * Linearizes a given component of an sRGB triplet.
74  * @param value Red, green or blue component of an sRGB triplet in the interval [0;1]
75  * @return Linearized value in the interval [0;1]
76  */
77  static inline Scalar linearizeSRGB(const Scalar value);
78 
79  /**
80  * Returns the input values unmodified.
81  * @param value Red, green or blue component of an sRGB triplet in the interval [0;1]
82  * @return unmodified value in the interval [0;1]
83  */
84  static inline Scalar identity(const unsigned char value);
85 
86  /**
87  * Delinearizes a given component of an sRGB triplet.
88  * @param value Red, green or blue component of an sRGB triplet, with range [0, 1]
89  * @return Delinearized value, with range [0, 255]
90  */
91  static unsigned char inline delinearizeSRGB(const Scalar value);
92 
93  /**
94  * Transform a byte-sized color component using the transformation function implemented as look-up table.
95  * @param value Input value to be transformed
96  * @return Transformed output value
97  */
98  inline Scalar operator()(const unsigned char value) const;
99 
100  protected:
101 
102  /**
103  * Sets the look-up-table using the specified transformation function.
104  * @param transformFunction Transformation function to use
105  */
106  void setTransformation(const TransformationFunction& transformFunction);
107 
108  protected:
109 
110  /// Maps byte-sized input color values between 0 and 255 to the specified output value.
111  Scalar transformData[256];
112 };
113 
115 {
116  setTransformation(transformFunction);
117 }
118 
119 inline Scalar ColorChannelCurve::operator()(const unsigned char value) const
120 {
121  return transformData[value];
122 }
123 
124 inline unsigned char ColorChannelCurve::delinearizeSRGB(const Scalar value)
125 {
126  ocean_assert(value >= 0 && value <= 1);
127 
128  if (value <= Scalar(0.00304))
129  {
130  const Scalar c = value * Scalar(12.92);
131 
132  ocean_assert(c >= 0 && c <= 1);
133  return (unsigned char)(c * Scalar(255) + Scalar(0.5));
134  }
135  else
136  {
137  const Scalar c = Numeric::pow(value, Scalar(0.4166666666666667)) * Scalar(1.055) - Scalar(0.055); // 0.41666... = 1/2.4
138 
139  ocean_assert(c >= 0 && c <= 1);
140  return (unsigned char)(c * Scalar(255) + Scalar(0.5));
141  }
142 }
143 
144 inline Scalar ColorChannelCurve::identity(const unsigned char value)
145 {
146  return value * Scalar(0.00392156862745098);
147 }
148 
149 inline Scalar ColorChannelCurve::linearizeSRGB(const unsigned char value)
150 {
151  const Scalar c = value * Scalar(0.00392156862745098); // 0.00392... = 1/255.0
152 
153  if (c <= Scalar(0.03928))
154  return c * Scalar(0.07739938080495357); // 0.07739... = 1/12.92
155  else
156  return Numeric::pow((c + Scalar(0.055)) * Scalar(0.9478672985781991), Scalar(2.4)); // 0.94786 = 1/1.055
157 }
158 
160 {
161  if (value <= Scalar(0.03928))
162  return value * Scalar(0.07739938080495357); // 0.07739... = 1/12.92
163  else
164  return Numeric::pow((value + Scalar(0.055)) * Scalar(0.9478672985781991), Scalar(2.4)); // 0.94786 = 1/1.055
165 }
166 
167 }
168 
169 }
170 
171 }
172 
173 #endif // OCEAN_CV_ADVANCED_COLOR_CHANNEL_CURVE_H
Implements a look-up table to perform fast transformations on a single color channel.
Definition: ColorChannelCurve.h:31
static Scalar identity(const unsigned char value)
Returns the input values unmodified.
Definition: ColorChannelCurve.h:144
Scalar transformData[256]
Maps byte-sized input color values between 0 and 255 to the specified output value.
Definition: ColorChannelCurve.h:111
ColorChannelCurve(const TransformationType type)
Creates a new color channel curve using the specified transformation function preset.
Callback< Scalar, unsigned char > TransformationFunction
Callback function type that returns a floating point value for the specified unsigned char input colo...
Definition: ColorChannelCurve.h:38
static unsigned char delinearizeSRGB(const Scalar value)
Delinearizes a given component of an sRGB triplet.
Definition: ColorChannelCurve.h:124
void setTransformation(const TransformationFunction &transformFunction)
Sets the look-up-table using the specified transformation function.
TransformationType
Represents a specific color channel transformation.
Definition: ColorChannelCurve.h:44
@ TT_IDENTITY
Returns input unmodified.
Definition: ColorChannelCurve.h:46
static Scalar linearizeSRGB(const unsigned char value)
Linearizes a given component of an sRGB triplet.
Definition: ColorChannelCurve.h:149
Scalar operator()(const unsigned char value) const
Transform a byte-sized color component using the transformation function implemented as look-up table...
Definition: ColorChannelCurve.h:119
This class implements a container for callback functions.
Definition: Callback.h:3456
static T pow(const T x, const T y)
Returns x raised to the power of y.
Definition: Numeric.h:1860
float Scalar
Definition of a scalar type.
Definition: Math.h:128
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15