Ocean
FrameConverterBGRA32.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_BGRA_32_H
9 #define META_OCEAN_CV_FRAME_CONVERTER_BGRA_32_H
10 
11 #include "ocean/cv/CV.h"
13 #include "ocean/cv/FrameChannels.h"
14 
15 namespace Ocean
16 {
17 
18 // Forward declaration.
19 class Worker;
20 
21 namespace CV
22 {
23 
24 /**
25  * This class provides functions to convert frames with BGRA pixel format.
26  * @ingroup cv
27  */
28 class OCEAN_CV_EXPORT FrameConverterBGRA32 : public FrameConverter
29 {
30  public:
31 
32  /**
33  * Converts a BGRA 32 bit frame to a ARGB 32 bit frame.
34  * @param source The source frame buffer, must be valid
35  * @param target The target frame buffer, must be valid
36  * @param width The width of the frame in pixel, with range [1, infinity)
37  * @param height The height of the frame in pixel, with range [1, infinity)
38  * @param flag Determining the type of conversion
39  * @param sourcePaddingElements The number of padding elements at the end of each source row, in elements, with range [0, infinity)
40  * @param targetPaddingElements The number of padding elements at the end of each target row, in elements, with range [0, infinity)
41  * @param worker Optional worker to distribute the computation to several CPU cores
42  */
43  static inline void convertBGRA32ToARGB32(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);
44 
45  /**
46  * Converts a BGRA 32 bit frame to a BGR 24 bit frame.
47  * @param source The source frame buffer, must be valid
48  * @param target The target frame buffer, must be valid
49  * @param width The width of the frame in pixel, with range [1, infinity)
50  * @param height The height of the frame in pixel, with range [1, infinity)
51  * @param flag Determining the type of conversion
52  * @param sourcePaddingElements The number of padding elements at the end of each source row, in elements, with range [0, infinity)
53  * @param targetPaddingElements The number of padding elements at the end of each target row, in elements, with range [0, infinity)
54  * @param worker Optional worker to distribute the computation to several CPU cores
55  */
56  static inline void convertBGRA32ToBGR24(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);
57 
58  /**
59  * Converts a BGRA 32 bit frame to a BGRA 32 bit frame.
60  * @param source The source frame buffer, must be valid
61  * @param target The target frame buffer, must be valid
62  * @param width The width of the frame in pixel, with range [1, infinity)
63  * @param height The height of the frame in pixel, with range [1, infinity)
64  * @param flag Determining the type of conversion
65  * @param sourcePaddingElements The number of padding elements at the end of each source row, in elements, with range [0, infinity)
66  * @param targetPaddingElements The number of padding elements at the end of each target row, in elements, with range [0, infinity)
67  * @param worker Optional worker to distribute the computation to several CPU cores
68  */
69  static inline void convertBGRA32ToBGRA32(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);
70 
71  /**
72  * Converts a BGRA 32 bit frame to a RGB 24 bit frame.
73  * @param source The source frame buffer, must be valid
74  * @param target The target frame buffer, must be valid
75  * @param width The width of the frame in pixel, with range [1, infinity)
76  * @param height The height of the frame in pixel, with range [1, infinity)
77  * @param flag Determining the type of conversion
78  * @param sourcePaddingElements The number of padding elements at the end of each source row, in elements, with range [0, infinity)
79  * @param targetPaddingElements The number of padding elements at the end of each target row, in elements, with range [0, infinity)
80  * @param worker Optional worker to distribute the computation to several CPU cores
81  */
82  static inline void convertBGRA32ToRGB24(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);
83 
84  /**
85  * Converts a BGRA 32 bit frame to a RGBA 32 bit frame.
86  * @param source The source frame buffer, must be valid
87  * @param target The target frame buffer, must be valid
88  * @param width The width of the frame in pixel, with range [1, infinity)
89  * @param height The height of the frame in pixel, with range [1, infinity)
90  * @param flag Determining the type of conversion
91  * @param sourcePaddingElements The number of padding elements at the end of each source row, in elements, with range [0, infinity)
92  * @param targetPaddingElements The number of padding elements at the end of each target row, in elements, with range [0, infinity)
93  * @param worker Optional worker to distribute the computation to several CPU cores
94  */
95  static inline void convertBGRA32ToRGBA32(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);
96 
97  /**
98  * Converts a BGRA 32 bit frame to a gray scale frame by the exact conversion.
99  * Gray = Red * 0.299 + Green * 0.587 + Blue * 0.114
100  * @param source The source frame buffer, must be valid
101  * @param target The target frame buffer, must be valid
102  * @param width The width of the frame, with range [1, infinity)
103  * @param height The height of the frame, with range [1, infinity)
104  * @param flag Determining the type of conversion
105  * @param sourcePaddingElements The number of padding elements at the end of each source row, in elements, with range [0, infinity)
106  * @param targetPaddingElements The number of padding elements at the end of each target row, in elements, with range [0, infinity)
107  * @param worker Optional worker to distribute the computation to several CPU cores
108  */
109  static inline void convertBGRA32ToY8(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);
110 
111  /**
112  * Converts a BGRA 32 bit frame to a YA 16 bit frame by the exact conversion.
113  * Gray = Red * 0.299 + Green * 0.587 + Blue * 0.114
114  * @param source The source frame buffer, must be valid
115  * @param target The target frame buffer, must be valid
116  * @param width The width of the frame, with range [1, infinity)
117  * @param height The height of the frame, with range [1, infinity)
118  * @param flag Determining the type of conversion
119  * @param sourcePaddingElements The number of padding elements at the end of each source row, in elements, with range [0, infinity)
120  * @param targetPaddingElements The number of padding elements at the end of each target row, in elements, with range [0, infinity)
121  * @param worker Optional worker to distribute the computation to several CPU cores
122  */
123  static inline void convertBGRA32ToYA16(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);
124 
125  /**
126  * Converts a BGRA 32 bit frame to a YUV 24 bit frame by the exact conversion.
127  * @param source The source frame buffer, must be valid
128  * @param target The target frame buffer, must be valid
129  * @param width The width of the frame in pixel, with range [1, infinity)
130  * @param height The height of the frame in pixel, with range [1, infinity)
131  * @param flag Determining the type of conversion
132  * @param sourcePaddingElements The number of padding elements at the end of each source row, in elements, with range [0, infinity)
133  * @param targetPaddingElements The number of padding elements at the end of each target row, in elements, with range [0, infinity)
134  * @param worker Optional worker to distribute the computation to several CPU cores
135  */
136  static inline void convertBGRA32ToYUV24(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);
137 };
138 
139 inline void FrameConverterBGRA32::convertBGRA32ToARGB32(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)
140 {
141  ocean_assert(source != nullptr && target != nullptr);
142  ocean_assert(width >= 1u && height >= 1u);
143 
144  FrameChannels::reverseChannelOrder<uint8_t, 4u>(source, target, width, height, flag, sourcePaddingElements, targetPaddingElements, worker);
145 }
146 
147 inline void FrameConverterBGRA32::convertBGRA32ToBGR24(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)
148 {
149  ocean_assert(source != nullptr && target != nullptr);
150  ocean_assert(width >= 1u && height >= 1u);
151 
152  FrameChannels::removeLastChannel<uint8_t, 4u>(source, target, width, height, flag, sourcePaddingElements, targetPaddingElements, worker);
153 }
154 
155 inline void FrameConverterBGRA32::convertBGRA32ToBGRA32(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)
156 {
157  ocean_assert(source != nullptr && target != nullptr);
158  ocean_assert(width >= 1u && height >= 1u);
159 
160  FrameChannels::transformGeneric<uint8_t, 4u>(source, target, width, height, flag, sourcePaddingElements, targetPaddingElements, worker);
161 }
162 
163 inline void FrameConverterBGRA32::convertBGRA32ToRGB24(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 B G R A
169  // 0 1 2 3
170  // target frame R G B
171  // pattern 2 1 0
172  constexpr unsigned int shufflePattern = 0x012;
173 
174  FrameChannels::shuffleChannels<uint8_t, 4u, 3u, shufflePattern>(source, target, width, height, flag, sourcePaddingElements, targetPaddingElements, worker);
175 }
176 
177 inline void FrameConverterBGRA32::convertBGRA32ToRGBA32(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)
178 {
179  ocean_assert(source != nullptr && target != nullptr);
180  ocean_assert(width >= 1u && height >= 1u);
181 
182  // source frame B G R A
183  // 0 1 2 3
184  // target frame R G B A
185  // pattern 2 1 0 3
186  constexpr unsigned int shufflePattern = 0x3012;
187 
188  FrameChannels::shuffleChannels<uint8_t, 4u, 4u, shufflePattern>(source, target, width, height, flag, sourcePaddingElements, targetPaddingElements, worker);
189 }
190 
191 inline void FrameConverterBGRA32::convertBGRA32ToY8(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  const unsigned int sourceStrideElements = width * 4u + sourcePaddingElements;
197  const unsigned int targetStrideElements = width + targetPaddingElements;
198 
199  constexpr unsigned int channelFactors[4] = {15u, 75u, 38u, 0u};
200 
201  static_assert(channelFactors[0] + channelFactors[1] + channelFactors[2] + channelFactors[3] == 128u, "Invalid factors!");
202 
203  constexpr bool useFactorChannel0 = channelFactors[0] != 0u;
204  constexpr bool useFactorChannel1 = channelFactors[1] != 0u;
205  constexpr bool useFactorChannel2 = channelFactors[2] != 0u;
206  constexpr bool useFactorChannel3 = channelFactors[3] != 0u;
207 
208  const bool areContinuous = sourcePaddingElements == 0u && targetPaddingElements == 0u;
209 
210  FrameConverter::convertGenericPixelFormat(source, target, width, height, sourceStrideElements, targetStrideElements, flag, CV::FrameChannels::convertRow4ChannelsTo1Channel8BitPerChannel7BitPrecision<useFactorChannel0, useFactorChannel1, useFactorChannel2, useFactorChannel3>, CV::FrameChannels::reverseRowPixelOrderInPlace<uint8_t, 1u>, areContinuous, channelFactors, worker);
211 }
212 
213 inline void FrameConverterBGRA32::convertBGRA32ToYA16(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)
214 {
215  ocean_assert(source != nullptr && target != nullptr);
216  ocean_assert(width >= 1u && height >= 1u);
217 
218  const unsigned int sourceStrideElements = width * 4u + sourcePaddingElements;
219  const unsigned int targetStrideElements = width * 2u + targetPaddingElements;
220 
221  // | B |
222  // | Y | | 15 75 38 0 | | G |
223  // | A | = | 0 0 0 128 | * | R |
224  // | A |
225 
226  constexpr unsigned int channelFactors[8] = {15u, 0u, 75u, 0u, 38u, 0u, 0u, 128u};
227 
228  const bool areContinuous = sourcePaddingElements == 0u && targetPaddingElements == 0u;
229 
230  FrameConverter::convertGenericPixelFormat(source, target, width, height, sourceStrideElements, targetStrideElements, flag, CV::FrameChannels::convertRow4ChannelsTo2Channels8BitPerChannel7BitPrecision, CV::FrameChannels::reverseRowPixelOrderInPlace<uint8_t, 2u>, areContinuous, channelFactors, worker);
231 }
232 
233 inline void FrameConverterBGRA32::convertBGRA32ToYUV24(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)
234 {
235  ocean_assert(source != nullptr && target != nullptr);
236  ocean_assert(width >= 1u && height >= 1u);
237 
238  const unsigned int sourceStrideElements = width * 4u + sourcePaddingElements;
239  const unsigned int targetStrideElements = width * 3u + targetPaddingElements;
240 
241  // approximation:
242  // Y = ( 33 * R + 64 * G + 13 * B + 0 * A) / 128 + 16
243  // U = (-19 * R - 37 * G + 56 * B + 0 * A) / 128 + 128
244  // V = ( 56 * R - 47 * G - 9 * B + 0 * A) / 128 + 128
245 
246  const int parameters[15] = {13, 56, -9, 64, -37, -47, 33, -19, 56, 0, 0, 0, 16, 128, 128};
247 
248  const bool areContinuous = sourcePaddingElements == 0u && targetPaddingElements == 0u;
249 
250  FrameConverter::convertGenericPixelFormat(source, target, width, height, sourceStrideElements, targetStrideElements, flag, CV::FrameChannels::convertRow4ChannelsTo3Channels8BitPerChannel7BitPrecision, CV::FrameChannels::reverseRowPixelOrderInPlace<uint8_t, 3u>, areContinuous, parameters, worker);
251 }
252 
253 }
254 
255 }
256 
257 #endif // META_OCEAN_CV_FRAME_CONVERTER_BGRA_32_H
static void convertRow4ChannelsTo2Channels8BitPerChannel7BitPrecision(const uint8_t *source, uint8_t *target, const size_t size, const void *multiplicationFactors_128)
Converts a row of pixels with 4 channels to pixels with two channel by a linear combination of the fo...
static void convertRow4ChannelsTo3Channels8BitPerChannel7BitPrecision(const uint8_t *source, uint8_t *target, const size_t size, const void *parameters)
Converts a row of pixels with 3 channels to pixels with 3 channels by a linear combination of the thr...
This class provides functions to convert frames with BGRA pixel format.
Definition: FrameConverterBGRA32.h:29
static void convertBGRA32ToRGBA32(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 BGRA 32 bit frame to a RGBA 32 bit frame.
Definition: FrameConverterBGRA32.h:177
static void convertBGRA32ToBGRA32(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 BGRA 32 bit frame to a BGRA 32 bit frame.
Definition: FrameConverterBGRA32.h:155
static void convertBGRA32ToRGB24(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 BGRA 32 bit frame to a RGB 24 bit frame.
Definition: FrameConverterBGRA32.h:163
static void convertBGRA32ToBGR24(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 BGRA 32 bit frame to a BGR 24 bit frame.
Definition: FrameConverterBGRA32.h:147
static void convertBGRA32ToY8(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)
Converts a BGRA 32 bit frame to a gray scale frame by the exact conversion.
Definition: FrameConverterBGRA32.h:191
static void convertBGRA32ToARGB32(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 BGRA 32 bit frame to a ARGB 32 bit frame.
Definition: FrameConverterBGRA32.h:139
static void convertBGRA32ToYA16(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 BGRA 32 bit frame to a YA 16 bit frame by the exact conversion.
Definition: FrameConverterBGRA32.h:213
static void convertBGRA32ToYUV24(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 BGRA 32 bit frame to a YUV 24 bit frame by the exact conversion.
Definition: FrameConverterBGRA32.h:233
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:3211
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