Ocean
FrameOperations.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_OPERATIONS_H
9 #define META_OCEAN_CV_FRAME_OPERATIONS_H
10 
11 #include "ocean/base/Worker.h"
12 #include "ocean/base/Frame.h"
13 
14 #include "ocean/cv/CV.h"
15 #include "ocean/cv/FrameChannels.h"
16 
17 namespace Ocean
18 {
19 
20 namespace CV
21 {
22 
23 /**
24  * Class for commonly used (arithmetic) operators on and between frames.
25  * @ingroup cv
26  */
27 class OCEAN_CV_EXPORT FrameOperations
28 {
29  public:
30 
31  /**
32  * Computes the difference between two frames and stores the difference
33  *
34  * For the pixel-wise subtraction of
35  *
36  * A and B, C_i = A_i - B_i,
37  *
38  * where i is the index of a pixel.
39  *
40  * @note Refrain from using this function, if binary size is important, e.g. for development on mobile platform. Instead use the pointer-based function @c subtract()
41  *
42  * @param source0 First source frame, must be valid
43  * @param source1 Second source frame, must be valid and have the same type as @c source0
44  * @param target Resulting frame, must be valid and have the same type as @c source0
45  * @param worker Optional worker instance for parallelization
46  * @return True on success, otherwise false (e.g., for unsupported pixel formats, or mismatch of pixel formats)
47  */
48  static bool subtract(const Frame& source0, const Frame& source1, Frame& target, Worker* worker = nullptr);
49 
50  /**
51  * Computes the difference between two frames
52  * The pixel-wise subtraction of two frames is defined as `C(y,x) = A(y,x) - B(y,x)`
53  * @param source0 Pointer to the first source, must not be null
54  * @param source1 Pointer to the second source, must not be null
55  * @param target Pointer to the result location, must not be null
56  * @param width The width of the sources and the target (must all be equal)
57  * @param height The height of the sources and the target (must all be equal)
58  * @param source0PaddingElements The number of padding elements at the end of each row of the first source, in elements, with range [0, infinity)
59  * @param source1PaddingElements The number of padding elements at the end of each row of the second source, in elements, with range [0, infinity)
60  * @param targetPaddingElements The number of padding elements at the end of each target row, in elements, with range [0, infinity)
61  * @param conversionFlag Determines the type of conversion
62  * @param worker Optional worker instance for parallelization
63  * @tparam TSource0 Data type of the first source
64  * @tparam TSource1 Data type of the second source
65  * @tparam TTarget Data type of the output
66  * @tparam TIntermediate Data type for the computation of intermediate results (before the conversion to @c TTarget)
67  * @tparam tChannels Number of channels of the sources and the target
68  */
69  template <typename TSource0, typename TSource1, typename TTarget, typename TIntermediate, unsigned int tChannels>
70  static void subtract(const TSource0* source0, const TSource1* source1, TTarget* target, const unsigned int width, const unsigned int height, const unsigned int source0PaddingElements, const unsigned int source1PaddingElements, const unsigned int targetPaddingElements, const FrameChannels::ConversionFlag conversionFlag = FrameChannels::CONVERT_NORMAL, Worker* worker = nullptr);
71 
72  protected:
73 
74  /**
75  * Collections of pixel operations
76  * @tparam tChannels Number of channels of the data to process
77  */
78  template <unsigned int tChannels>
79  struct Operations
80  {
81  /**
82  * Subtraction of frames
83  *
84  * For the pixel-wise subtraction of two frames, C_i = A_i - B_i, where
85  * i is the index of a pixel.
86  *
87  * @param source0 Pointer to the first source, must not be null
88  * @param source1 Pointer to the second source, must not be null
89  * @param target Pointer to the result location, must not be null
90  * @tparam TSource0 Type of the first source
91  * @tparam TSource1 Type of the second source
92  * @tparam TTarget Type of the target
93  */
94  template <typename TSource0, typename TSource1, typename TTarget>
95  static inline void subtractPixel(const TSource0* source0, const TSource1* source1, TTarget* target);
96  };
97 };
98 
99 template <typename TSource0, typename TSource1, typename TTarget, typename TIntermediate, unsigned int tChannels>
100 void FrameOperations::subtract(const TSource0* source0, const TSource1* source1, TTarget* target, const unsigned int width, const unsigned int height, const unsigned int source0PaddingElements, const unsigned int source1PaddingElements, const unsigned int targetPaddingElements, const FrameChannels::ConversionFlag conversionFlag, Worker* worker)
101 {
102  static_assert(tChannels > 0u, "Number of channels must be larger then zero.");
103 
104  ocean_assert(source0 && source1 && target);
105  ocean_assert(width > 0u && height > 0u);
106 
107  FrameChannels::applyBivariateOperator<TSource0, TSource1, TTarget, TIntermediate, tChannels, tChannels, Operations<tChannels>::template subtractPixel<TSource0, TSource1, TTarget>>(source0, source1, target, width, height, source0PaddingElements, source1PaddingElements, targetPaddingElements, conversionFlag, worker);
108 }
109 
110 template <unsigned int tChannels>
111 template <typename TSource0, typename TSource1, typename TTarget>
112 inline void FrameOperations::Operations<tChannels>::subtractPixel(const TSource0* source0, const TSource1* source1, TTarget* target)
113 {
114  static_assert(tChannels > 0u, "Number of channels must be larger then zero.");
115 
116  ocean_assert(source0 && source1 && target);
117 
118  for (unsigned int channel = 0u; channel < tChannels; ++channel)
119  {
120  target[channel] = static_cast<TTarget>(source0[channel] - source1[channel]);
121  }
122 }
123 
124 } // namespace CV
125 
126 } // namespace Ocean
127 
128 #endif // META_OCEAN_CV_FRAME_OPERATIONS_H
ConversionFlag
Definition of individual conversion flags.
Definition: FrameConverter.h:39
@ CONVERT_NORMAL
Normal conversion, neither flips nor mirrors the image.
Definition: FrameConverter.h:49
Class for commonly used (arithmetic) operators on and between frames.
Definition: FrameOperations.h:28
static bool subtract(const Frame &source0, const Frame &source1, Frame &target, Worker *worker=nullptr)
Computes the difference between two frames and stores the difference.
This class implements Ocean's image class.
Definition: Frame.h:1792
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
Collections of pixel operations.
Definition: FrameOperations.h:80
static void subtractPixel(const TSource0 *source0, const TSource1 *source1, TTarget *target)
Subtraction of frames.
Definition: FrameOperations.h:112