Ocean
SumAbsoluteDifferencesBase.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_SUM_ABSOLUTE_DIFFERENCES_BASE_H
9 #define META_OCEAN_CV_SUM_ABSOLUTE_DIFFERENCES_BASE_H
10 
11 #include "ocean/cv/CV.h"
12 #include "ocean/cv/CVUtilities.h"
13 
14 namespace Ocean
15 {
16 
17 namespace CV
18 {
19 
20 /**
21  * This class implements several sum of absolute differences functions.
22  * @ingroup cv
23  */
24 class OCEAN_CV_EXPORT SumAbsoluteDifferencesBase
25 {
26  public:
27 
28  /**
29  * Returns the sum of absolute differences between two square patches.
30  * @param image0 The first image in which the first patch is located, must be valid
31  * @param image1 The second image in which the second patch is located, must be valid
32  * @param width0 Width of the first frame in pixels, with range [tPatchSize, infinity)
33  * @param width1 Width of the second frame in pixels, with range [tPatchSize, infinity)
34  * @param centerX0 Horizontal center position of the (tPatchSize x tPatchSize) block in the first frame, with range [tPatchSize / 2, width0 - tPatchSize / 2 - 1]
35  * @param centerY0 Vertical center position of the (tPatchSize x tPatchSize) block in the first frame, with range [tPatchSize / 2, height0 - tPatchSize / 2 - 1]
36  * @param centerX1 Horizontal center position of the (tPatchSize x tPatchSize) block in the second frame, with range [tPatchSize / 2, width1 - tPatchSize / 2 - 1]
37  * @param centerY1 Vertical center position of the (tPatchSize x tPatchSize) block in the second frame, with range [tPatchSize / 2, height1 - tPatchSize / 2 - 1]
38  * @param image0PaddingElements The number of padding elements at the end of each row of the first image, in elements, with range [0, infinity)
39  * @param image1PaddingElements The number of padding elements at the end of each row of the second image, in elements, with range [0, infinity)
40  * @return The resulting sum of absolute differences for tPatchSize * tPatchSize * tChannels elements
41  * @tparam tChannels Specifies the number of channels for the given frames, with range [1, infinity)
42  * @tparam tPatchSize The size of the square patch (the edge length) in pixel, with range [1, infinity), must be odd
43  */
44  template <unsigned int tChannels, unsigned int tPatchSize>
45  static OCEAN_FORCE_INLINE uint32_t patch8BitPerChannelTemplate(const uint8_t* image0, const uint8_t* image1, const unsigned int width0, const unsigned int width1, const unsigned int centerX0, const unsigned int centerY0, const unsigned int centerX1, const unsigned int centerY1, const unsigned int image0PaddingElements, const unsigned int image1PaddingElements);
46 
47  /**
48  * Returns the sum of absolute differences between two square patches.
49  * @param patch0 The top-left corner of the first image patch, must be valid
50  * @param patch1 The top-left corner of the second image patch, must be valid
51  * @param patch0StrideElements The number of elements between two row starts in the first patch, in elements, with range [tPatchSize * tChannels, infinity)
52  * @param patch1StrideElements The number of elements between two row starts in the second patch, in elements, with range [tPatchSize * tChannels, infinity)
53  * @return The resulting sum of absolute differences for tPatchSize * tPatchSize * tChannels elements
54  * @tparam tChannels Specifies the number of channels for the given frames, with range [1, infinity)
55  * @tparam tPatchSize The size of the square patch (the edge length) in pixel, with range [1, infinity)
56  */
57  template <unsigned int tChannels, unsigned int tPatchSize>
58  static uint32_t patch8BitPerChannelTemplate(const uint8_t* patch0, const uint8_t* patch1, const unsigned int patch0StrideElements, const unsigned int patch1StrideElements);
59 
60  /**
61  * Returns the sum of absolute differences between a square image patch and a buffer.
62  * @param image0 The image in which the image patch is located, must be valid
63  * @param width0 Width of the first frame in pixels, with range [tPatchSize, infinity)
64  * @param centerX0 Horizontal center position of the (tPatchSize x tPatchSize) block in the first frame, with range [tPatchSize / 2, width0 - tPatchSize / 2 - 1]
65  * @param centerY0 Vertical center position of the (tPatchSize x tPatchSize) block in the first frame, with range [tPatchSize / 2, height0 - tPatchSize / 2 - 1]
66  * @param image0PaddingElements The number of padding elements at the end of each row of the first image, in elements, with range [0, infinity)
67  * @param buffer1 The memory buffer with `tChannels * tPatchSize * tPatchSize` elements, must be valid
68  * @return The resulting sum of absolute differences for tPatchSize * tPatchSize * tChannels elements, with range [0, infinity)
69  * @tparam tChannels The number of channels for the given frames, with range [1, infinity)
70  * @tparam tPatchSize The size of the square patch (the edge length) in pixel, with range [1, infinity), must be odd
71  */
72  template <unsigned int tChannels, unsigned int tPatchSize>
73  static OCEAN_FORCE_INLINE uint32_t patchBuffer8BitPerChannelTemplate(const uint8_t* image0, const unsigned int width0, const unsigned int centerX0, const unsigned int centerY0, const unsigned int image0PaddingElements, const uint8_t* buffer1);
74 
75  /**
76  * Returns the sum of absolute differences between a square image patch and a buffer.
77  * @param patch0 The top left start position of the image patch, must be valid
78  * @param buffer1 The memory buffer, must be valid
79  * @param patch0StrideElements The number of elements between two rows for the image patch, in elements, with range [tChannels, tPatchSize, infinity)
80  * @return The resulting sum of absolute differences for tPatchSize * tPatchSize * tChannels elements, with range [0, infinity)
81  * @tparam tChannels The number of channels for the given frames, with range [1, infinity)
82  * @tparam tPatchSize The size of the square patch (the edge length) in pixel, with range [1, infinity), must be odd
83  */
84  template <unsigned int tChannels, unsigned int tPatchSize>
85  static uint32_t patchBuffer8BitPerChannelTemplate(const uint8_t* patch0, const uint8_t* buffer1, const unsigned int patch0StrideElements);
86 
87  /**
88  * Returns the sum of absolute differences between two individual buffers.
89  * @param buffer0 The first buffer, with `tBufferSize` elements, must be valid
90  * @param buffer1 The second buffer, with `tBufferSize` elements, must be valid
91  * @return The resulting sum of absolute differences, with range [0, infinity)
92  * @tparam tBufferSize The number of elements in each buffer, with range [1, infinity)
93  */
94  template <unsigned int tBufferSize>
95  static inline uint32_t buffer8BitPerChannelTemplate(const uint8_t* const buffer0, const uint8_t* const buffer1);
96 
97  /**
98  * Returns the sum of absolute differences between two patches within an image, patch pixels outside the image will be mirrored back into the image.
99  * @param image0 The image in which the first patch is located, must be valid
100  * @param image1 The image in which the second patch is located, must be valid
101  * @param patchSize The size of the square patch (the edge length) in pixel, with range [1, infinity), must be odd
102  * @param width0 The width of the first image, in pixels, with range [tPatchSize, infinity)
103  * @param height0 The height of the first image, in pixels, with range [tPatchSize, infinity)
104  * @param width1 The width of the second image, in pixels, with range [tPatchSize, infinity)
105  * @param height1 The height of the second image, in pixels, with range [tPatchSize, infinity)
106  * @param centerX0 Horizontal center position of the (tPatchSize x tPatchSize) block in the first frame, with range [tPatchSize/2, width - tPatchSize/2 - 1]
107  * @param centerY0 Vertical center position of the (tPatchSize x tPatchSize) block in the first frame, with range [tPatchSize/2, height - tPatchSize/2 - 1]
108  * @param centerX1 Horizontal center position of the (tPatchSize x tPatchSize) block in the second frame, with range [tPatchSize/2, width - tPatchSize/2 - 1]
109  * @param centerY1 Vertical center position of the (tPatchSize x tPatchSize) block in the second frame, with range [tPatchSize/2, height - tPatchSize/2 - 1]
110  * @param image0PaddingElements The number of padding elements at the end of each row of the first image, in elements, with range [0, infinity)
111  * @param image1PaddingElements The number of padding elements at the end of each row of the second image, in elements, with range [0, infinity)
112  * @return The resulting sum of absolute differences, with range [0, infinity)
113  * @tparam tChannels Specifies the number of channels for the given frames
114  */
115  template <unsigned int tChannels>
116  static uint32_t patchMirroredBorder8BitPerChannelTemplate(const uint8_t* image0, const uint8_t* image1, const unsigned int patchSize, const unsigned int width0, const unsigned int height0, const unsigned int width1, const unsigned int height1, const unsigned int centerX0, const unsigned int centerY0, const unsigned int centerX1, const unsigned int centerY1, const unsigned int image0PaddingElements, const unsigned int image1PaddingElements);
117 
118  /**
119  * Returns the sum of absolute differences between two square patches.
120  * @param image0 The first image in which the first patch is located, must be valid
121  * @param image1 The second image in which the second patch is located, must be valid
122  * @param channels The number of channels for the given frames, with range [1, infinity)
123  * @param patchSize The size of the square patch (the edge length) in pixel, with range [1, infinity), must be odd
124  * @param width0 Width of the first frame in pixels, with range [tPatchSize, infinity)
125  * @param width1 Width of the second frame in pixels, with range [tPatchSize, infinity)
126  * @param centerX0 Horizontal center position of the (tPatchSize x tPatchSize) block in the first frame, with range [tPatchSize / 2, width0 - tPatchSize / 2 - 1]
127  * @param centerY0 Vertical center position of the (tPatchSize x tPatchSize) block in the first frame, with range [tPatchSize / 2, height0 - tPatchSize / 2 - 1]
128  * @param centerX1 Horizontal center position of the (tPatchSize x tPatchSize) block in the second frame, with range [tPatchSize / 2, width1 - tPatchSize / 2 - 1]
129  * @param centerY1 Vertical center position of the (tPatchSize x tPatchSize) block in the second frame, with range [tPatchSize / 2, height1 - tPatchSize / 2 - 1]
130  * @param image0PaddingElements The number of padding elements at the end of each row of the first image, in elements, with range [0, infinity)
131  * @param image1PaddingElements The number of padding elements at the end of each row of the second image, in elements, with range [0, infinity)
132  * @return The resulting sum of absolute differences for tPatchSize * tPatchSize * tChannels elements
133  * @tparam tChannels The number of channels for the given frames, with range [1, infinity)
134  * @tparam tPatchSize The size of the square patch (the edge length) in pixel, with range [1, infinity), must be odd
135  */
136  static OCEAN_FORCE_INLINE uint32_t patch8BitPerChannel(const uint8_t* image0, const uint8_t* image1, const unsigned int channels, const unsigned int patchSize, const unsigned int width0, const unsigned int width1, const unsigned int centerX0, const unsigned int centerY0, const unsigned int centerX1, const unsigned int centerY1, const unsigned int image0PaddingElements, const unsigned int image1PaddingElements);
137 
138  /**
139  * Returns the sum of absolute differences between two square patches.
140  * @param patch0 The top-left corner of the first image patch, must be valid
141  * @param patch1 The top-left corner of the second image patch, must be valid
142  * @param channels The number of channels for the given frames, with range [1, infinity)
143  * @param patchSize The size of the square patch (the edge length) in pixel, with range [1, infinity), must be odd
144  * @param patch0StrideElements The number of elements between two row starts in the first patch, in elements, with range [tPatchSize * tChannels, infinity)
145  * @param patch1StrideElements The number of elements between two row starts in the second patch, in elements, with range [tPatchSize * tChannels, infinity)
146  * @return The resulting sum of absolute differences for tPatchSize * tPatchSize * tChannels elements
147  */
148  static uint32_t patch8BitPerChannel(const uint8_t* patch0, const uint8_t* patch1, const unsigned int channels, const unsigned int patchSize, const unsigned int patch0StrideElements, const unsigned int patch1StrideElements);
149 
150  /**
151  * Returns the sum of absolute differences between a square image patch and a buffer.
152  * @param image0 The image in which the image patch is located, must be valid
153  * @param channels The number of channels for the given frames, with range [1, infinity)
154  * @param patchSize The size of the square patch (the edge length) in pixel, with range [1, infinity), must be odd
155  * @param width0 Width of the first frame in pixels, with range [tPatchSize, infinity)
156  * @param centerX0 Horizontal center position of the (tPatchSize x tPatchSize) block in the first frame, with range [tPatchSize / 2, width0 - tPatchSize / 2 - 1]
157  * @param centerY0 Vertical center position of the (tPatchSize x tPatchSize) block in the first frame, with range [tPatchSize / 2, height0 - tPatchSize / 2 - 1]
158  * @param image0PaddingElements The number of padding elements at the end of each row of the first image, in elements, with range [0, infinity)
159  * @param buffer1 The memory buffer with `tChannels * tPatchSize * tPatchSize` elements, must be valid
160  * @return The resulting sum of absolute differences for tPatchSize * tPatchSize * tChannels elements, with range [0, infinity)
161  */
162  static OCEAN_FORCE_INLINE uint32_t patchBuffer8BitPerChannel(const uint8_t* image0, unsigned int channels, unsigned int patchSize, const unsigned int width0, const unsigned int centerX0, const unsigned int centerY0, const unsigned int image0PaddingElements, const uint8_t* buffer1);
163 
164  /**
165  * Returns the sum of absolute differences between a square image patch and a buffer.
166  * @param patch0 The top left start position of the image patch, must be valid
167  * @param buffer1 The memory buffer, must be valid
168  * @param channels The number of channels for the given frames, with range [1, infinity)
169  * @param patchSize The size of the square patch (the edge length) in pixel, with range [1, infinity), must be odd
170  * @param patch0StrideElements The number of elements between two rows for the image patch, in elements, with range [tChannels, tPatchSize, infinity)
171  * @return The resulting sum of absolute differences for tPatchSize * tPatchSize * tChannels elements, with range [0, infinity)
172  */
173  static inline uint32_t patchBuffer8BitPerChannel(const uint8_t* patch0, const uint8_t* buffer1, unsigned int channels, unsigned int patchSize, const unsigned int patch0StrideElements);
174 
175  /**
176  * Returns the sum of absolute differences between two individual buffers.
177  * @param buffer0 The first buffer, with `size` elements, must be valid
178  * @param buffer1 The second buffer, with `size` elements, must be valid
179  * @param bufferSize The number of elements in each buffer, with range [1, infinity)
180  * @return The sum of absolute differences between both buffers, with range [0, infinity)
181  */
182  static uint32_t buffer8BitPerChannel(const uint8_t* const buffer0, const uint8_t* const buffer1, const unsigned int bufferSize);
183 };
184 
185 template <unsigned int tChannels, unsigned int tPatchSize>
186 OCEAN_FORCE_INLINE uint32_t SumAbsoluteDifferencesBase::patch8BitPerChannelTemplate(const uint8_t* const image0, const uint8_t* const image1, const unsigned int width0, const unsigned int width1, const unsigned int centerX0, const unsigned int centerY0, const unsigned int centerX1, const unsigned int centerY1, const unsigned int image0PaddingElements, const unsigned int image1PaddingElements)
187 {
188  static_assert(tChannels >= 1u, "Invalid number of frame channels!");
189  static_assert(tPatchSize % 2u == 1u, "Invalid patch size!");
190 
191  ocean_assert(image0 != nullptr && image1 != nullptr);
192 
193  constexpr unsigned int tPatchSize_2 = tPatchSize / 2u;
194 
195  ocean_assert(centerX0 >= tPatchSize_2 && centerY0 >= tPatchSize_2 && centerX0 < width0 - tPatchSize_2);
196  ocean_assert(centerX1 >= tPatchSize_2 && centerY1 >= tPatchSize_2 && centerX1 < width1 - tPatchSize_2);
197 
198  ocean_assert(width0 >= tPatchSize);
199  ocean_assert(width1 >= tPatchSize);
200 
201  const unsigned int image0StrideElements = width0 * tChannels + image0PaddingElements;
202  const unsigned int image1StrideElements = width1 * tChannels + image1PaddingElements;
203 
204  return patch8BitPerChannelTemplate<tChannels, tPatchSize>(image0 + (centerY0 - tPatchSize_2) * image0StrideElements + (centerX0 - tPatchSize_2) * tChannels, image1 + (centerY1 - tPatchSize_2) * image1StrideElements + (centerX1 - tPatchSize_2) * tChannels, image0StrideElements, image1StrideElements);
205 }
206 
207 template <unsigned int tChannels, unsigned int tPatchSize>
208 uint32_t SumAbsoluteDifferencesBase::patch8BitPerChannelTemplate(const uint8_t* patch0, const uint8_t* patch1, const unsigned int patch0StrideElements, const unsigned int patch1StrideElements)
209 {
210  static_assert(tPatchSize >= 1u, "Invalid image patch size, need an odd value!");
211  static_assert(tChannels > 0u, "Invalid number of frame channels!");
212 
213  ocean_assert(patch0 != nullptr && patch1 != nullptr);
214 
215  ocean_assert(patch0StrideElements >= tPatchSize * tChannels);
216  ocean_assert(patch1StrideElements >= tPatchSize * tChannels);
217 
218  uint32_t result = 0u;
219  int32_t value = 0;
220 
221  for (unsigned int y = 0u; y < tPatchSize; ++y)
222  {
223  for (unsigned int n = 0u; n < tPatchSize * tChannels; ++n)
224  {
225  value = int16_t(patch0[n]) - int16_t(patch1[n]);
226  result += std::abs(value);
227  }
228 
229  patch0 += patch0StrideElements;
230  patch1 += patch1StrideElements;
231  }
232 
233  return result;
234 }
235 
236 template <unsigned int tChannels, unsigned int tPatchSize>
237 OCEAN_FORCE_INLINE uint32_t SumAbsoluteDifferencesBase::patchBuffer8BitPerChannelTemplate(const uint8_t* image0, const unsigned int width0, const unsigned int centerX0, const unsigned int centerY0, const unsigned int image0PaddingElements, const uint8_t* buffer1)
238 {
239  static_assert(tChannels >= 1u, "Invalid number of frame channels!");
240  static_assert(tPatchSize % 2u == 1u, "Invalid patch size!");
241 
242  ocean_assert(image0 != nullptr && buffer1 != nullptr);
243 
244  constexpr unsigned int tPatchSize_2 = tPatchSize / 2u;
245 
246  ocean_assert(centerX0 >= tPatchSize_2 && centerY0 >= tPatchSize_2 && centerX0 < width0 - tPatchSize_2);
247 
248  ocean_assert(width0 >= tPatchSize);
249 
250  const unsigned int image0StrideElements = width0 * tChannels + image0PaddingElements;
251 
252  return patchBuffer8BitPerChannelTemplate<tChannels, tPatchSize>(image0 + (centerY0 - tPatchSize_2) * image0StrideElements + (centerX0 - tPatchSize_2) * tChannels, buffer1, image0StrideElements);
253 }
254 
255 template <unsigned int tChannels, unsigned int tPatchSize>
256 uint32_t SumAbsoluteDifferencesBase::patchBuffer8BitPerChannelTemplate(const uint8_t* patch0, const uint8_t* buffer1, const unsigned int patch0StrideElements)
257 {
258  return patch8BitPerChannelTemplate<tChannels, tPatchSize>(patch0, buffer1, patch0StrideElements, tChannels * tPatchSize);
259 }
260 
261 template <unsigned int tBufferSize>
262 inline uint32_t SumAbsoluteDifferencesBase::buffer8BitPerChannelTemplate(const uint8_t* const buffer0, const uint8_t* const buffer1)
263 {
264  static_assert(tBufferSize != 0u, "Invalid number of frame channels!");
265 
266  ocean_assert(buffer0 != nullptr && buffer1 != nullptr);
267 
268  int16_t difference;
269  uint32_t ssd = 0u;
270 
271  for (unsigned int n = 0u; n < tBufferSize; ++n)
272  {
273  difference = int16_t(buffer0[n]) - int16_t(buffer1[n]);
274  ssd += uint32_t(std::abs(difference));
275  }
276 
277  return ssd;
278 }
279 
280 template <unsigned int tChannels>
281 uint32_t SumAbsoluteDifferencesBase::patchMirroredBorder8BitPerChannelTemplate(const uint8_t* image0, const uint8_t* image1, const unsigned int patchSize, const unsigned int width0, const unsigned int height0, const unsigned int width1, const unsigned int height1, const unsigned int centerX0, const unsigned int centerY0, const unsigned int centerX1, const unsigned int centerY1, const unsigned int image0PaddingElements, const unsigned int image1PaddingElements)
282 {
283  static_assert(tChannels != 0u, "Invalid number of data channels!");
284 
285  ocean_assert(image0 != nullptr && image1 != nullptr);
286  ocean_assert(patchSize % 2u == 1u);
287 
288  const unsigned int patchSize_2 = patchSize / 2u;
289 
290  ocean_assert(width0 >= patchSize_2);
291  ocean_assert(width1 >= patchSize_2);
292 
293  ocean_assert(centerX0 < width0 && centerY0 < height0);
294  ocean_assert(centerX1 < width1 && centerY1 < height1);
295 
296  const int left0 = int(centerX0 - patchSize_2);
297  const int top0 = int(centerY0 - patchSize_2);
298 
299  const int left1 = int(centerX1 - patchSize_2);
300  const int top1 = int(centerY1 - patchSize_2);
301 
302  const unsigned int image0StrideElements = width0 * tChannels + image0PaddingElements;
303  const unsigned int image1StrideElements = width1 * tChannels + image1PaddingElements;
304 
305  const uint8_t* i0 = image0 + top0 * int(image0StrideElements) + left0 * int(tChannels);
306  const uint8_t* i1 = image1 + top1 * int(image1StrideElements) + left1 * int(tChannels);
307 
308  uint32_t sad = 0u;
309 
310  unsigned int y0 = centerY0 - patchSize_2;
311  unsigned int y1 = centerY1 - patchSize_2;
312 
313  const uint8_t* const i0End = i0 + patchSize * image0StrideElements;
314 
315  while (i0 != i0End)
316  {
317  ocean_assert(i0 < i0End);
318 
319  const uint8_t* c0 = i0 + CVUtilities::mirrorOffset(y0, height0) * int(image0StrideElements);
320  const uint8_t* c1 = i1 + CVUtilities::mirrorOffset(y1, height1) * int(image1StrideElements);
321 
322  unsigned int x0 = centerX0 - patchSize_2;
323  unsigned int x1 = centerX1 - patchSize_2;
324 
325  const uint8_t* const c0RowEnd = c0 + patchSize * tChannels;
326 
327  while (c0 != c0RowEnd)
328  {
329  ocean_assert(c0 < c0RowEnd);
330 
331  if (x0 < width0 && x1 < width1)
332  {
333  // both pixels lies inside the frame
334 
335  for (unsigned int n = 0u; n < tChannels; ++n)
336  {
337  sad += abs(*c0++ - *c1++);
338  }
339  }
340  else if (x0 < width0)
341  {
342  // x0 lies inside the frame
343 
344  ocean_assert(x1 >= width1);
345 
346  const uint8_t* m1 = c1 + CVUtilities::mirrorOffset(x1, width1) * int(tChannels);
347 
348  for (unsigned int n = 0u; n < tChannels; ++n)
349  {
350  sad += abs(*c0++ - *m1++);
351  }
352 
353  c1 += tChannels;
354  }
355  else if (x1 < width1)
356  {
357  // x1 lies inside the frame
358 
359  ocean_assert(x0 >= width0);
360 
361  const uint8_t* m0 = c0 + CVUtilities::mirrorOffset(x0, width0) * int(tChannels);
362 
363  for (unsigned int n = 0u; n < tChannels; ++n)
364  {
365  sad += abs(*m0++ - *c1++);
366  }
367 
368  c0 += tChannels;
369  }
370  else
371  {
372  // neither x0 nor x1 lies inside the frame
373 
374  ocean_assert(x0 >= width0 && x1 >= width1);
375 
376  const uint8_t* m1 = c1 + CVUtilities::mirrorOffset(x1, width1) * int(tChannels);
377  const uint8_t* m0 = c0 + CVUtilities::mirrorOffset(x0, width0) * int(tChannels);
378 
379  for (unsigned int n = 0u; n < tChannels; ++n)
380  {
381  sad += abs(*m0++ - *m1++);
382  }
383 
384  c0 += tChannels;
385  c1 += tChannels;
386  }
387 
388  ++x0;
389  ++x1;
390  }
391 
392  i0 += image0StrideElements;
393  i1 += image1StrideElements;
394 
395  ++y0;
396  ++y1;
397  }
398 
399  return sad;
400 }
401 
402 OCEAN_FORCE_INLINE uint32_t SumAbsoluteDifferencesBase::patch8BitPerChannel(const uint8_t* const image0, const uint8_t* const image1, const unsigned int channels, const unsigned int patchSize, const unsigned int width0, const unsigned int width1, const unsigned int centerX0, const unsigned int centerY0, const unsigned int centerX1, const unsigned int centerY1, const unsigned int image0PaddingElements, const unsigned int image1PaddingElements)
403 {
404  ocean_assert(image0 != nullptr && image1 != nullptr);
405 
406  ocean_assert(channels >= 1u);
407  ocean_assert(patchSize % 2u == 1u);
408 
409  const unsigned int patchSize_2 = patchSize / 2u;
410 
411  ocean_assert(centerX0 >= patchSize_2 && centerY0 >= patchSize_2 && centerX0 < width0 - patchSize_2);
412  ocean_assert(centerX1 >= patchSize_2 && centerY1 >= patchSize_2 && centerX1 < width1 - patchSize_2);
413 
414  ocean_assert(width0 >= patchSize);
415  ocean_assert(width1 >= patchSize);
416 
417  const unsigned int image0StrideElements = width0 * channels + image0PaddingElements;
418  const unsigned int image1StrideElements = width1 * channels + image1PaddingElements;
419 
420  return patch8BitPerChannel(image0 + (centerY0 - patchSize_2) * image0StrideElements + (centerX0 - patchSize_2) * channels, image1 + (centerY1 - patchSize_2) * image1StrideElements + (centerX1 - patchSize_2) * channels, channels, patchSize, image0StrideElements, image1StrideElements);
421 }
422 
423 OCEAN_FORCE_INLINE uint32_t SumAbsoluteDifferencesBase::patchBuffer8BitPerChannel(const uint8_t* image0, unsigned int channels, unsigned int patchSize, const unsigned int width0, const unsigned int centerX0, const unsigned int centerY0, const unsigned int image0PaddingElements, const uint8_t* buffer1)
424 {
425  ocean_assert(image0 != nullptr && buffer1 != nullptr);
426 
427  ocean_assert(channels >= 1u);
428  ocean_assert(patchSize % 2u == 1u);
429 
430  const unsigned int patchSize_2 = patchSize / 2u;
431 
432  ocean_assert(centerX0 >= patchSize_2 && centerY0 >= patchSize_2 && centerX0 < width0 - patchSize_2);
433 
434  ocean_assert(width0 >= patchSize);
435 
436  const unsigned int image0StrideElements = width0 * channels + image0PaddingElements;
437 
438  return patchBuffer8BitPerChannel(image0 + (centerY0 - patchSize_2) * image0StrideElements + (centerX0 - patchSize_2) * channels, buffer1, channels, patchSize, image0StrideElements);
439 }
440 
441 inline uint32_t SumAbsoluteDifferencesBase::patchBuffer8BitPerChannel(const uint8_t* patch0, const uint8_t* buffer1, unsigned int channels, unsigned int patchSize, const unsigned int patch0StrideElements)
442 {
443  return patch8BitPerChannel(patch0, buffer1, channels, patchSize, patch0StrideElements, channels * patchSize);
444 }
445 
446 }
447 
448 }
449 
450 #endif // META_OCEAN_CV_SUM_ABSOLUTE_DIFFERENCES_BASE_H
static int mirrorOffset(const unsigned int index, const unsigned int elements)
Deprecated.
Definition: CVUtilities.h:446
This class implements several sum of absolute differences functions.
Definition: SumAbsoluteDifferencesBase.h:25
static OCEAN_FORCE_INLINE uint32_t patch8BitPerChannelTemplate(const uint8_t *image0, const uint8_t *image1, const unsigned int width0, const unsigned int width1, const unsigned int centerX0, const unsigned int centerY0, const unsigned int centerX1, const unsigned int centerY1, const unsigned int image0PaddingElements, const unsigned int image1PaddingElements)
Returns the sum of absolute differences between two square patches.
Definition: SumAbsoluteDifferencesBase.h:186
static OCEAN_FORCE_INLINE uint32_t patch8BitPerChannel(const uint8_t *image0, const uint8_t *image1, const unsigned int channels, const unsigned int patchSize, const unsigned int width0, const unsigned int width1, const unsigned int centerX0, const unsigned int centerY0, const unsigned int centerX1, const unsigned int centerY1, const unsigned int image0PaddingElements, const unsigned int image1PaddingElements)
Returns the sum of absolute differences between two square patches.
Definition: SumAbsoluteDifferencesBase.h:402
static OCEAN_FORCE_INLINE uint32_t patchBuffer8BitPerChannelTemplate(const uint8_t *image0, const unsigned int width0, const unsigned int centerX0, const unsigned int centerY0, const unsigned int image0PaddingElements, const uint8_t *buffer1)
Returns the sum of absolute differences between a square image patch and a buffer.
Definition: SumAbsoluteDifferencesBase.h:237
static uint32_t patchMirroredBorder8BitPerChannelTemplate(const uint8_t *image0, const uint8_t *image1, const unsigned int patchSize, const unsigned int width0, const unsigned int height0, const unsigned int width1, const unsigned int height1, const unsigned int centerX0, const unsigned int centerY0, const unsigned int centerX1, const unsigned int centerY1, const unsigned int image0PaddingElements, const unsigned int image1PaddingElements)
Returns the sum of absolute differences between two patches within an image, patch pixels outside the...
Definition: SumAbsoluteDifferencesBase.h:281
static uint32_t patch8BitPerChannel(const uint8_t *patch0, const uint8_t *patch1, const unsigned int channels, const unsigned int patchSize, const unsigned int patch0StrideElements, const unsigned int patch1StrideElements)
Returns the sum of absolute differences between two square patches.
static OCEAN_FORCE_INLINE uint32_t patchBuffer8BitPerChannel(const uint8_t *image0, unsigned int channels, unsigned int patchSize, const unsigned int width0, const unsigned int centerX0, const unsigned int centerY0, const unsigned int image0PaddingElements, const uint8_t *buffer1)
Returns the sum of absolute differences between a square image patch and a buffer.
Definition: SumAbsoluteDifferencesBase.h:423
static uint32_t buffer8BitPerChannelTemplate(const uint8_t *const buffer0, const uint8_t *const buffer1)
Returns the sum of absolute differences between two individual buffers.
Definition: SumAbsoluteDifferencesBase.h:262
static uint32_t buffer8BitPerChannel(const uint8_t *const buffer0, const uint8_t *const buffer1, const unsigned int bufferSize)
Returns the sum of absolute differences between two individual buffers.
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15