Ocean
FrameConverterY8.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_Y_8_H
9 #define META_OCEAN_CV_FRAME_CONVERTER_Y_8_H
10 
11 #include "ocean/cv/CV.h"
12 #include "ocean/cv/FrameChannels.h"
14 
15 #include "ocean/base/Memory.h"
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 with Y8 pixel format.
26  * @ingroup cv
27  */
28 class OCEAN_CV_EXPORT FrameConverterY8 : public FrameConverter
29 {
30  protected:
31 
32  /**
33  * This class implements the manager for lookup tables.
34  */
35  class LookupTableManager : public Singleton<LookupTableManager>
36  {
37  protected:
38 
39  /// Definition of a map mapping gamma values to the memory of lookup tables.
40  typedef std::unordered_map<float, Memory> LookupTables;
41 
42  public:
43 
44  /**
45  * Returns the lookup table for a gamma compression/correction function.
46  * The gamma compression/correction is based the following equation
47  * <pre>
48  * Y8 = 255 * (Y8 / 255) ^ gamma
49  * </pre>
50  * @param gamma The gamma value for which the lookup table will be returned, with range (0, 2)
51  * @return The requested lookup table, will be valid
52  */
53  const uint8_t* lookupTable(const float gamma);
54 
55  protected:
56 
57  /// The lookup tables.
59 
60  /// The lock of the manager.
62  };
63 
64  public:
65 
66  /**
67  * Converts a Y 8 bit frame to a BGR 24 bit frame.
68  * @param source The source frame buffer, must be valid
69  * @param target The target frame buffer, must be valid
70  * @param width The width of the frame in pixel, with range [1, infinity)
71  * @param height The height of the frame in pixel, with range [1, infinity)
72  * @param flag Determining the type of conversion
73  * @param sourcePaddingElements The number of padding elements at the end of each source row, in elements, with range [0, infinity)
74  * @param targetPaddingElements The number of padding elements at the end of each target row, in elements, with range [0, infinity)
75  * @param worker Optional worker object to distribute the computational load
76  */
77  static inline void convertY8ToBGR24(const uint8_t* source, uint8_t* target, const unsigned int width, const unsigned int height, const ConversionFlag flag, const unsigned int sourcePaddingElements, const unsigned int targetPaddingElements, Worker* worker = nullptr);
78 
79  /**
80  * Converts a Y 8 bit frame to a RGB 24 bit frame.
81  * @param source The source frame buffer, must be valid
82  * @param target The target frame buffer, must be valid
83  * @param width The width of the frame in pixel, with range [1, infinity)
84  * @param height The height of the frame in pixel, with range [1, infinity)
85  * @param flag Determining the type of conversion
86  * @param sourcePaddingElements The number of padding elements at the end of each source row, in elements, with range [0, infinity)
87  * @param targetPaddingElements The number of padding elements at the end of each target row, in elements, with range [0, infinity)
88  * @param worker Optional worker object to distribute the computational load
89  */
90  static inline void convertY8ToRGB24(const uint8_t* source, uint8_t* target, const unsigned int width, const unsigned int height, const ConversionFlag flag, const unsigned int sourcePaddingElements, const unsigned int targetPaddingElements, Worker* worker = nullptr);
91 
92  /**
93  * Converts a Y 8 bit frame to a RGBA 32 bit frame.
94  * @param source The source frame buffer, must be valid
95  * @param target The target frame buffer, must be valid
96  * @param width The width of the frame in pixel, with range [1, infinity)
97  * @param height The height of the frame in pixel, with range [1, infinity)
98  * @param flag Determining the type of conversion
99  * @param sourcePaddingElements The number of padding elements at the end of each source row, in elements, with range [0, infinity)
100  * @param targetPaddingElements The number of padding elements at the end of each target row, in elements, with range [0, infinity)
101  * @param alphaValue The value of the alpha channel to be set, with range [0, 255]
102  * @param worker Optional worker object to distribute the computational load
103  */
104  static inline void convertY8ToRGBA32(const uint8_t* source, uint8_t* target, const unsigned int width, const unsigned int height, const ConversionFlag flag, const unsigned int sourcePaddingElements, const unsigned int targetPaddingElements, const uint8_t alphaValue = 0xFF, Worker* worker = nullptr);
105 
106  /**
107  * Converts a Y 8 bit frame to a Y 8 bit frame.
108  * @param source The source frame buffer, must be valid
109  * @param target The target frame buffer, must be valid
110  * @param width The width of the frame in pixel, with range [1, infinity)
111  * @param height The height of the frame in pixel, with range [1, infinity)
112  * @param flag Determining the type of conversion
113  * @param sourcePaddingElements The number of padding elements at the end of each source row, in elements, with range [0, infinity)
114  * @param targetPaddingElements The number of padding elements at the end of each target row, in elements, with range [0, infinity)
115  * @param worker Optional worker object to distribute the computational load
116  */
117  static inline void convertY8ToY8(const uint8_t* source, uint8_t* target, const unsigned int width, const unsigned int height, const ConversionFlag flag, const unsigned int sourcePaddingElements, const unsigned int targetPaddingElements, Worker* worker = nullptr);
118 
119  /**
120  * Converts a Y8 frame to a Y8 frame by applying gamma compression/correction using a lookup table.
121  * The gamma compression/correction is based the following equation
122  * <pre>
123  * Y8 = 255 * (Y8 / 255) ^ gamma
124  * </pre>
125  * @param source The source frame buffer, must be valid
126  * @param target The target frame buffer, must be valid
127  * @param width The width of the frame in pixel, with range [1, infinity)
128  * @param height The height of the frame in pixel, with range [1, infinity)
129  * @param flag Determining the type of conversion
130  * @param gamma The gamma value to be applied, with range (0, 2)
131  * @param sourcePaddingElements The number of padding elements at the end of each source row, in elements, with range [0, infinity)
132  * @param targetPaddingElements The number of padding elements at the end of each target row, in elements, with range [0, infinity)
133  * @param worker Optional worker object to distribute the computational load
134  */
135  static inline void convertY8ToY8GammaLUT(const uint8_t* const source, uint8_t* const target, const unsigned int width, const unsigned int height, const ConversionFlag flag, const float gamma, const unsigned int sourcePaddingElements, const unsigned int targetPaddingElements, Worker* worker = nullptr);
136 
137  protected:
138 
139  /**
140  * Converts a Y8 row to a Y8 row by applying gamma compression/correction with a lookup table.
141  * @param source The pointer to the source pixels, must be valid
142  * @param target The pointer to the target pixels receiving the converted pixel data, must be valid
143  * @param size The number of source (and target pixels) to convert, with range [1, infinity)
144  * @param parameters The pointer to the `uint8_t` lookup table to be used, must be valid
145  */
146  static void convertRowY8ToY8GammaLUT(const uint8_t* source, uint8_t* target, const size_t size, const void* parameters);
147 };
148 
149 inline void FrameConverterY8::convertY8ToBGR24(const uint8_t* source, uint8_t* target, const unsigned int width, const unsigned int height, const ConversionFlag flag, const unsigned int sourcePaddingElements, const unsigned int targetPaddingElements, Worker* worker)
150 {
151  ocean_assert(source != nullptr && target != nullptr);
152  ocean_assert(width >= 1u && height >= 1u);
153 
154  // source frame Y
155  // 0
156  // target frame B G R
157  // pattern 0 0 0
158  constexpr unsigned int shufflePattern = 0x000u;
159 
160  FrameChannels::shuffleChannels<uint8_t, 1u, 3u, shufflePattern>(source, target, width, height, flag, sourcePaddingElements, targetPaddingElements, worker);
161 }
162 
163 inline void FrameConverterY8::convertY8ToRGB24(const uint8_t* source, uint8_t* target, const unsigned int width, const unsigned int height, const ConversionFlag flag, const unsigned int sourcePaddingElements, const unsigned int targetPaddingElements, Worker* worker)
164 {
165  ocean_assert(source != nullptr && target != nullptr);
166  ocean_assert(width >= 1u && height >= 1u);
167 
168  // source frame Y
169  // 0
170  // target frame R G B
171  // pattern 0 0 0
172  constexpr unsigned int shufflePattern = 0x000u;
173 
174  FrameChannels::shuffleChannels<uint8_t, 1u, 3u, shufflePattern>(source, target, width, height, flag, sourcePaddingElements, targetPaddingElements, worker);
175 }
176 
177 inline void FrameConverterY8::convertY8ToRGBA32(const uint8_t* source, uint8_t* target, const unsigned int width, const unsigned int height, const ConversionFlag flag, const unsigned int sourcePaddingElements, const unsigned int targetPaddingElements, const uint8_t alphaValue, Worker* worker)
178 {
179  ocean_assert(source != nullptr && target != nullptr);
180  ocean_assert(width >= 1u && height >= 1u);
181 
182  // source frame Y
183  // 0
184  // target frame R G B A
185  // pattern 0 0 0
186  constexpr unsigned int shufflePattern = 0x000u;
187 
188  FrameChannels::shuffleChannelsAndSetLastChannelValue<uint8_t, 1u, 4u, shufflePattern>(source, alphaValue, target, width, height, flag, sourcePaddingElements, targetPaddingElements, worker);
189 }
190 
191 inline void FrameConverterY8::convertY8ToY8(const uint8_t* source, uint8_t* target, const unsigned int width, const unsigned int height, const ConversionFlag flag, const unsigned int sourcePaddingElements, const unsigned int targetPaddingElements, Worker* worker)
192 {
193  ocean_assert(source != nullptr && target != nullptr);
194  ocean_assert(width >= 1u && height >= 1u);
195 
196  FrameChannels::transformGeneric<uint8_t, 1u>(source, target, width, height, flag, sourcePaddingElements, targetPaddingElements, worker);
197 }
198 
199 inline void FrameConverterY8::convertY8ToY8GammaLUT(const uint8_t* source, uint8_t* target, const unsigned int width, const unsigned int height, const ConversionFlag flag, const float gamma, const unsigned int sourcePaddingElements, const unsigned int targetPaddingElements, Worker* worker)
200 {
201  ocean_assert(source != nullptr && target != nullptr);
202  ocean_assert(width >= 1u && height >= 1u);
203 
204  ocean_assert(gamma > 0.0f && gamma < 2.0f);
205 
206  const unsigned int sourceStrideElements = width + sourcePaddingElements;
207  const unsigned int targetStrideElements = width + targetPaddingElements;
208 
209  const void* const options = LookupTableManager::get().lookupTable(gamma);
210 
211  const bool areContinuous = sourcePaddingElements == 0u && targetPaddingElements == 0u;
212 
213  FrameConverter::convertGenericPixelFormat(source, target, width, height, sourceStrideElements, targetStrideElements, flag, convertRowY8ToY8GammaLUT, CV::FrameChannels::reverseRowPixelOrderInPlace<uint8_t, 1u>, areContinuous, options, worker);
214 }
215 
216 }
217 
218 }
219 
220 #endif // META_OCEAN_CV_FRAME_CONVERTER_Y_8_H
This is the base class for all frame converter classes.
Definition: FrameConverter.h:32
ConversionFlag
Definition of individual conversion flags.
Definition: FrameConverter.h:39
static void convertGenericPixelFormat(const TSource *source, TTarget *target, const unsigned int width, const unsigned int height, const unsigned int sourceStrideElements, const unsigned int targetStrideElements, const ConversionFlag flag, const RowConversionFunction< TSource, TTarget > rowConversionFunction, const RowReversePixelOrderInPlaceFunction< TTarget > targetReversePixelOrderInPlaceFunction, const bool areContinuous, const void *options, Worker *worker)
Converts a frame with generic pixel format (e.g., RGBA32, BGR24, YUV24, ...) to a frame with generic ...
Definition: FrameConverter.h:3160
This class implements the manager for lookup tables.
Definition: FrameConverterY8.h:36
LookupTables lookupTables_
The lookup tables.
Definition: FrameConverterY8.h:58
std::unordered_map< float, Memory > LookupTables
Definition of a map mapping gamma values to the memory of lookup tables.
Definition: FrameConverterY8.h:40
const uint8_t * lookupTable(const float gamma)
Returns the lookup table for a gamma compression/correction function.
Lock lock_
The lock of the manager.
Definition: FrameConverterY8.h:61
This class provides functions to convert frames with Y8 pixel format.
Definition: FrameConverterY8.h:29
static void convertY8ToRGB24(const uint8_t *source, uint8_t *target, const unsigned int width, const unsigned int height, const ConversionFlag flag, const unsigned int sourcePaddingElements, const unsigned int targetPaddingElements, Worker *worker=nullptr)
Converts a Y 8 bit frame to a RGB 24 bit frame.
Definition: FrameConverterY8.h:163
static void convertY8ToRGBA32(const uint8_t *source, uint8_t *target, const unsigned int width, const unsigned int height, const ConversionFlag flag, const unsigned int sourcePaddingElements, const unsigned int targetPaddingElements, const uint8_t alphaValue=0xFF, Worker *worker=nullptr)
Converts a Y 8 bit frame to a RGBA 32 bit frame.
Definition: FrameConverterY8.h:177
static void convertY8ToY8(const uint8_t *source, uint8_t *target, const unsigned int width, const unsigned int height, const ConversionFlag flag, const unsigned int sourcePaddingElements, const unsigned int targetPaddingElements, Worker *worker=nullptr)
Converts a Y 8 bit frame to a Y 8 bit frame.
Definition: FrameConverterY8.h:191
static void convertY8ToY8GammaLUT(const uint8_t *const source, uint8_t *const target, const unsigned int width, const unsigned int height, const ConversionFlag flag, const float gamma, const unsigned int sourcePaddingElements, const unsigned int targetPaddingElements, Worker *worker=nullptr)
Converts a Y8 frame to a Y8 frame by applying gamma compression/correction using a lookup table.
Definition: FrameConverterY8.h:199
static void convertRowY8ToY8GammaLUT(const uint8_t *source, uint8_t *target, const size_t size, const void *parameters)
Converts a Y8 row to a Y8 row by applying gamma compression/correction with a lookup table.
static void convertY8ToBGR24(const uint8_t *source, uint8_t *target, const unsigned int width, const unsigned int height, const ConversionFlag flag, const unsigned int sourcePaddingElements, const unsigned int targetPaddingElements, Worker *worker=nullptr)
Converts a Y 8 bit frame to a BGR 24 bit frame.
Definition: FrameConverterY8.h:149
This class implements a recursive lock object.
Definition: Lock.h:31
This template class is the base class for all singleton objects.
Definition: Singleton.h:71
static LookupTableManager & get()
Returns a reference to the unique object.
Definition: Singleton.h:115
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