Ocean
MappingI.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_SYNTHESIS_MAPPING_I_H
9 #define META_OCEAN_CV_SYNTHESIS_MAPPING_I_H
10 
13 
14 #include "ocean/cv/PixelPosition.h"
15 
16 namespace Ocean
17 {
18 
19 namespace CV
20 {
21 
22 namespace Synthesis
23 {
24 
25 /**
26  * This class implements a mapping with integer accuracy.
27  * @ingroup cvsynthesis
28  */
29 class OCEAN_CV_SYNTHESIS_EXPORT MappingI : public Mapping
30 {
31  friend class MappingF1;
32 
33  public:
34 
35  /**
36  * Destructs a mapping object.
37  */
38  inline ~MappingI() override;
39 
40  /**
41  * Returns the mapping for a given position.
42  * @param x Horizontal position to return the mapping for, with range [0, width - 1]
43  * @param y Vertical position to return the mapping for, with range [0, height - 1]
44  * @return Current mapping for the specified position
45  */
46  inline const PixelPosition& position(const unsigned int x, const unsigned int y) const;
47 
48  /**
49  * Returns the mapping for a given position.
50  * @param x Horizontal position to return the mapping for, with range [0, width - 1]
51  * @param y Vertical position to return the mapping for, with range [0, height - 1]
52  * @return Current mapping for the specified position
53  */
54  inline PixelPosition& position(const unsigned int x, const unsigned int y);
55 
56  /**
57  * Returns the mapping for a given position.
58  * @param location The location for which the mapping will be returned, with range [0, width-1]x[0, height-1]
59  * @return Current mapping for the specified position
60  */
61  inline const PixelPosition& position(const CV::PixelPosition& location) const;
62 
63  /**
64  * Returns the mapping for a given position.
65  * @param location The location for which the mapping will be returned, with range [0, width-1]x[0, height-1]
66  * @return Current mapping for the specified position
67  */
68  inline PixelPosition& position(const CV::PixelPosition& location);
69 
70  /**
71  * Sets a new mapping for a specified position.
72  * @param x Horizontal position to set the mapping for, with range [0, width - 1]
73  * @param y Vertical position to set the mapping for, with range [0, height - 1]
74  * @param pixelPosition New mapping to be set
75  */
76  inline void setPosition(const unsigned int x, const unsigned int y, const PixelPosition& pixelPosition);
77 
78  /**
79  * Returns the pointer to a mapping row.
80  * @param y The index of the row to return, with range [0, height - 1]
81  * @return The mapping row
82  */
83  inline const PixelPosition* row(const unsigned int y) const;
84 
85  /**
86  * Returns the pointer to a mapping row.
87  * @param y The index of the row to return, with range [0, height - 1]
88  * @return The mapping row
89  */
90  inline PixelPosition* row(const unsigned int y);
91 
92  /**
93  * Resets the stored mapping.
94  */
95  inline void reset();
96 
97  /**
98  * Returns the mappings of this object.
99  * @return All mapping
100  */
101  inline const PixelPosition* operator()() const;
102 
103  /**
104  * Returns the mappings of this object.
105  * @return All mapping
106  */
107  inline PixelPosition* operator()();
108 
109  protected:
110 
111  /**
112  * Creates an empty mapping object.
113  */
114  MappingI() = default;
115 
116  /**
117  * Copies a mapping from a given mapping object.
118  * @param pixelMapping Pixel mapping to be copied
119  */
120  inline MappingI(const MappingI& pixelMapping);
121 
122  /**
123  * Moves a mapping from a given mapping object.
124  * @param pixelMapping Pixel mapping to be moved
125  */
126  inline MappingI(MappingI&& pixelMapping) noexcept;
127 
128  /**
129  * Creates a new mapping object with defined dimension.
130  * Beware: An initial mapping is not provided.<br>
131  * @param width The width of the mapping object in pixel, with range [1, infinity)
132  * @param height The height of the mapping object in pixel, with range [1, infinity)
133  */
134  inline MappingI(const unsigned int width, const unsigned int height);
135 
136  /**
137  * Calculates the sum of square differences between two 5x5 frame regions in two frames with explicit weighted mask pixels.<br>
138  * Compared to the standard ssd calculation this extended version weights the square difference of mask pixels (value not equal to 0xFF) with a given factor, further the center pixel is not considered.
139  * @param frame0 Pointer to the top left position in the 5x5 region in the first frame
140  * @param frame1 Pointer to the top left position in the 5x5 region in the second frame
141  * @param mask0 Pointer to the top left position in the 5x5 region in the mask frame, with 0xFF defining a non-mask pixel
142  * @param width0 Width of the first frame in pixel, with range [5, infinity)
143  * @param width1 Width of the second frame in pixel, with range [5, infinity)
144  * @param frame0PaddingElements The number of padding elements at the end of each first frame row, in elements, with range [0, infinity)
145  * @param frame1PaddingElements The number of padding elements at the end of each second frame row, in elements, with range [0, infinity)
146  * @param mask0PaddingElements The number of padding elements at the end of each mask row, in elements, with range [0, infinity)
147  * @return Resulting sum of squared differences
148  * @tparam tChannels Number of frame channels
149  * @tparam tBorderFactor Multiplication factor for squared differences of border pixels, with range [1, infinity)
150  */
151  template <unsigned int tChannels, unsigned int tBorderFactor>
152  static inline unsigned int ssd5x5MaskNoCenter(const uint8_t* frame0, const uint8_t* frame1, const uint8_t* mask0, const unsigned int width0, const unsigned int width1, const unsigned int frame0PaddingElements, const unsigned int frame1PaddingElements, const unsigned int mask0PaddingElements);
153 
154  /**
155  * Assign operator.
156  * @param pixelMapping Mapping object to be copied
157  * @return Reference to this object
158  */
159  inline MappingI& operator=(const MappingI& pixelMapping);
160 
161  /**
162  * Move operator.
163  * @param pixelMapping Mapping object to be moved
164  * @return Reference to this object
165  */
166  inline MappingI& operator=(MappingI&& pixelMapping) noexcept;
167 
168  protected:
169 
170  /// Pixel mappings for each pixel.
171  PixelPosition* mappingI_ = nullptr;
172 };
173 
174 inline MappingI::MappingI(const MappingI& pixelMapping) :
175  Mapping(pixelMapping.width_, pixelMapping.height_),
176  mappingI_(nullptr)
177 {
178  const unsigned int size = width_ * height_;
179 
180  if (size != 0u)
181  {
182  mappingI_ = (PixelPosition*)malloc(size * sizeof(PixelPosition));
183  ocean_assert(mappingI_ != nullptr);
184 
185  memcpy(mappingI_, pixelMapping.mappingI_, size * sizeof(PixelPosition));
186  }
187 }
188 
189 inline MappingI::MappingI(MappingI&& pixelMapping) noexcept :
190  Mapping(std::move(pixelMapping)),
191  mappingI_(nullptr)
192 {
193  mappingI_ = pixelMapping.mappingI_;
194  pixelMapping.mappingI_ = nullptr;
195 }
196 
197 inline MappingI::MappingI(const unsigned int width, const unsigned int height) :
198  Mapping(width, height),
199  mappingI_(nullptr)
200 {
201  const unsigned int size = width * height;
202 
203  if (size != 0)
204  {
205  mappingI_ = (PixelPosition*)malloc(size * sizeof(PixelPosition));
206  ocean_assert(mappingI_ != nullptr);
207  }
208 }
209 
211 {
212  free(mappingI_);
213 }
214 
215 inline const PixelPosition& MappingI::position(const unsigned int x, const unsigned int y) const
216 {
217  ocean_assert(x < width_ && y < height_);
218  return mappingI_[y * width_ + x];
219 }
220 
221 inline PixelPosition& MappingI::position(const unsigned int x, const unsigned int y)
222 {
223  ocean_assert(x < width_ && y < height_);
224  return mappingI_[y * width_ + x];
225 }
226 
227 inline const PixelPosition& MappingI::position(const CV::PixelPosition& location) const
228 {
229  return position(location.x(), location.y());
230 }
231 
233 {
234  return position(location.x(), location.y());
235 }
236 
237 inline void MappingI::setPosition(const unsigned int x, const unsigned int y, const PixelPosition& pixelPosition)
238 {
239  ocean_assert(x < width_ && y < height_);
240  mappingI_[y * width_ + x] = pixelPosition;
241 }
242 
243 inline const PixelPosition* MappingI::row(const unsigned int y) const
244 {
245  ocean_assert(y < height_);
246 
247  return mappingI_ + y * width_;
248 }
249 
250 inline PixelPosition* MappingI::row(const unsigned int y)
251 {
252  ocean_assert(y < height_);
253 
254  return mappingI_ + y * width_;
255 }
256 
257 inline void MappingI::reset()
258 {
259  ocean_assert(mappingI_);
260  memset(mappingI_, 0xFF, sizeof(PixelPosition) * width_ * height_);
261 }
262 
263 inline const PixelPosition* MappingI::operator()() const
264 {
265  return mappingI_;
266 }
267 
269 {
270  return mappingI_;
271 }
272 
273 template <unsigned int tChannels, unsigned int tBorderFactor>
274 inline unsigned int MappingI::ssd5x5MaskNoCenter(const uint8_t* frame0, const uint8_t* frame1, const uint8_t* mask0, const unsigned int width0, const unsigned int width1, const unsigned int frame0PaddingElements, const unsigned int frame1PaddingElements, const unsigned int mask0PaddingElements)
275 {
276  static_assert(tBorderFactor >= 1u, "Invalid border factor!");
277 
278  ocean_assert(frame0 != nullptr && frame1 != nullptr && mask0 != nullptr);
279  ocean_assert(width0 >= 5u && width1 >= 5u);
280 
281  const unsigned int frame0StrideElements = width0 * tChannels + frame0PaddingElements;
282  const unsigned int frame1StrideElements = width1 * tChannels + frame1PaddingElements;
283  const unsigned int mask0StrideElements = width0 + mask0PaddingElements;
284 
285  unsigned int ssd = 0u;
286 
287  for (unsigned int y = 0u; y < 5u; ++y)
288  {
289  for (unsigned int x = 0u; x < 5u; ++x)
290  {
291  if (x == 2u && y == 2u)
292  {
293  // skip the center pixel
294  continue;
295  }
296 
297  unsigned int local = 0u;
298 
299  for (unsigned int n = 0u; n < tChannels; ++n)
300  {
301  const int value = int(frame0[x * tChannels + n] - frame1[x * tChannels + n]);
302  local += value * value;
303  }
304 
305  if (mask0[x] == 0xFFu)
306  {
307  local *= tBorderFactor;
308  }
309 
310  ssd += local;
311  }
312 
313  frame0 += frame0StrideElements;
314  frame1 += frame1StrideElements;
315  mask0 += mask0StrideElements;
316  }
317 
318  return ssd;
319 }
320 
321 inline MappingI& MappingI::operator=(const MappingI& pixelMapping)
322 {
323  if (this != &pixelMapping)
324  {
325  if (mappingI_)
326  {
327  free(mappingI_);
328  mappingI_ = nullptr;
329  }
330 
331  Mapping::operator=(pixelMapping);
332 
333  const unsigned int size = width_ * height_;
334 
335  if (size != 0u)
336  {
337  mappingI_ = (PixelPosition*)malloc(size * sizeof(PixelPosition));
338  ocean_assert(mappingI_ != nullptr);
339 
340  memcpy(mappingI_, pixelMapping.mappingI_, size * sizeof(PixelPosition));
341  }
342  }
343 
344  return *this;
345 }
346 
347 inline MappingI& MappingI::operator=(MappingI&& pixelMapping) noexcept
348 {
349  if (this != &pixelMapping)
350  {
351  Mapping::operator=(std::move(pixelMapping));
352 
353  mappingI_ = pixelMapping.mappingI_;
354  pixelMapping.mappingI_ = nullptr;
355  }
356 
357  return *this;
358 }
359 
360 }
361 
362 }
363 
364 }
365 
366 #endif // META_OCEAN_CV_SYNTHESIS_MAPPING_I_1_H
T y() const
Returns the vertical coordinate position of this object.
Definition: PixelPosition.h:470
T x() const
Returns the horizontal coordinate position of this object.
Definition: PixelPosition.h:458
Cost function:
Definition: MappingF1.h:52
This class is the base class for all mappings.
Definition: Mapping.h:35
unsigned int width_
Width of this pixel mapping object in pixel.
Definition: Mapping.h:147
unsigned int width() const
Returns the width of this mapping object.
Definition: Mapping.h:282
Mapping & operator=(const Mapping &mapping)
Assign operator.
Definition: Mapping.h:407
unsigned int height_
Height of this pixel mapping object in pixel.
Definition: Mapping.h:150
unsigned int height() const
Returns the height of this mapping object.
Definition: Mapping.h:287
This class implements a mapping with integer accuracy.
Definition: MappingI.h:30
MappingI & operator=(const MappingI &pixelMapping)
Assign operator.
Definition: MappingI.h:321
static unsigned int ssd5x5MaskNoCenter(const uint8_t *frame0, const uint8_t *frame1, const uint8_t *mask0, const unsigned int width0, const unsigned int width1, const unsigned int frame0PaddingElements, const unsigned int frame1PaddingElements, const unsigned int mask0PaddingElements)
Calculates the sum of square differences between two 5x5 frame regions in two frames with explicit we...
Definition: MappingI.h:274
const PixelPosition * row(const unsigned int y) const
Returns the pointer to a mapping row.
Definition: MappingI.h:243
const PixelPosition * operator()() const
Returns the mappings of this object.
Definition: MappingI.h:263
MappingI()=default
Creates an empty mapping object.
const PixelPosition & position(const unsigned int x, const unsigned int y) const
Returns the mapping for a given position.
Definition: MappingI.h:215
void reset()
Resets the stored mapping.
Definition: MappingI.h:257
PixelPosition * mappingI_
Pixel mappings for each pixel.
Definition: MappingI.h:171
void setPosition(const unsigned int x, const unsigned int y, const PixelPosition &pixelPosition)
Sets a new mapping for a specified position.
Definition: MappingI.h:237
~MappingI() override
Destructs a mapping object.
Definition: MappingI.h:210
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15