Ocean
CreatorInformationCost4NeighborhoodI1.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_CREATOR_INFORMATION_COST_4_NEIGHBORHOOD_I_1_H
9 #define META_OCEAN_CV_SYNTHESIS_CREATOR_INFORMATION_COST_4_NEIGHBORHOOD_I_1_H
10 
15 
16 namespace Ocean
17 {
18 
19 namespace CV
20 {
21 
22 namespace Synthesis
23 {
24 
25 /**
26  * This class implements a creator that determines the mapping cost for a four neighborhood for mappings with integer accuracy.
27  * @tparam tWeightFactor Spatial weight impact, with range [0, infinity)
28  * @tparam tBorderFactor Weight factor of border pixels, with range [1, infinity)
29  * @ingroup cvsynthesis
30  */
31 template <unsigned int tWeightFactor, unsigned int tBorderFactor>
33  virtual public CreatorI,
34  virtual public CreatorSubset,
35  virtual public Creator1
36 {
37  protected:
38 
39  /**
40  * Definition of a vector holding costs.
41  */
42  typedef std::vector<uint64_t> Costs;
43 
44  public:
45 
46  /**
47  * Creates a new creator object.
48  * @param layer Synthesis layer that is used for to create the information
49  * @param cost Resulting mapping cost
50  * @param maxSpatialCost Maximal spatial cost
51  */
52  inline CreatorInformationCost4NeighborhoodI1(const LayerI1& layer, uint64_t& cost, const unsigned int maxSpatialCost = (unsigned int)(-1));
53 
54  /**
55  * Invokes the creator.
56  * @see CreatorSubset::invoke().
57  */
58  bool invoke(Worker* worker = nullptr) const override;
59 
60  protected:
61 
62  /**
63  * Creates a subset of the information.
64  * @see CreatorSubsxet::createSubset().
65  */
66  void createSubset(const unsigned int firstColumn, const unsigned int numberColumns, const unsigned int firstRow, const unsigned int numberRows) const override;
67 
68  /**
69  * Specialization of the default function that creates a subset of the information.
70  * @param firstColumn First column to be handled
71  * @param numberColumns Number of columns to be handled
72  * @param firstRow First row to be handled
73  * @param numberRows Number of rows to be handled
74  * @tparam tChannels Number of channels of the target frame
75  */
76  template <unsigned int tChannels>
77  void createSubsetChannels(const unsigned int firstColumn, const unsigned int numberColumns, const unsigned int firstRow, const unsigned int numberRows) const;
78 
79  protected:
80 
81  /// Specialized layer reference.
82  const LayerI1& layerI1_;
83 
84  /// Resulting creator cost.
85  uint64_t& cost_;
86 
87  /// Maximal spatial cost.
88  const unsigned int maxSpatialCost_;
89 
90  /// Intermediate row costs.
91  mutable Costs rowCost_;
92 };
93 
94 template <unsigned int tWeightFactor, unsigned int tBorderFactor>
96  Creator(layer),
97  CreatorI(layer),
98  CreatorSubset(layer),
99  Creator1(layer),
100  layerI1_(layer),
101  cost_(cost),
102  maxSpatialCost_(maxSpatialCost)
103 {
104  // nothing to do here
105 }
106 
107 template <unsigned int tWeightFactor, unsigned int tBorderFactor>
109 {
110  rowCost_ = Costs(layerI1_.height(), 0u);
111  cost_ = 0ull;
112 
113  if (!CreatorSubset::invoke(worker))
114  {
115  return false;
116  }
117 
118  const size_t firstRow = layerI1_.boundingBox() ? layerI1_.boundingBox().top() : 0u;
119  const size_t endRow = layerI1_.boundingBox() ? layerI1_.boundingBox().bottomEnd() : layerI1_.height();
120 
121 #ifdef OCEAN_DEBUG
122 
123  for (size_t n = 0; n < firstRow; ++n)
124  {
125  ocean_assert(rowCost_[n] == 0);
126  }
127  for (size_t n = endRow; n < layerI1_.height(); ++n)
128  {
129  ocean_assert(rowCost_[n] == 0);
130  }
131 
132 #endif // OCEAN_DEBUG
133 
134  for (size_t n = firstRow; n < endRow; ++n)
135  {
136  cost_ += rowCost_[n];
137  }
138 
139  return true;
140 }
141 
142 template <unsigned int tWeightFactor, unsigned int tBorderFactor>
143 void CreatorInformationCost4NeighborhoodI1<tWeightFactor, tBorderFactor>::createSubset(const unsigned int firstColumn, const unsigned int numberColumns, const unsigned int firstRow, const unsigned int numberRows) const
144 {
145  ocean_assert(layerI1_.frame().numberPlanes() == 1u);
146 
147  switch (layerI1_.frame().channels())
148  {
149  case 1u:
150  createSubsetChannels<1u>(firstColumn, numberColumns, firstRow, numberRows);
151  break;
152 
153  case 2u:
154  createSubsetChannels<2u>(firstColumn, numberColumns, firstRow, numberRows);
155  break;
156 
157  case 3u:
158  createSubsetChannels<3u>(firstColumn, numberColumns, firstRow, numberRows);
159  break;
160 
161  case 4u:
162  createSubsetChannels<4u>(firstColumn, numberColumns, firstRow, numberRows);
163  break;
164 
165  default:
166  ocean_assert(false && "Invalid frame type.");
167  }
168 }
169 
170 template <unsigned int tWeightFactor, unsigned int tBorderFactor>
171 template <unsigned int tChannels>
172 void CreatorInformationCost4NeighborhoodI1<tWeightFactor, tBorderFactor>::createSubsetChannels(const unsigned int firstColumn, const unsigned int numberColumns, const unsigned int firstRow, const unsigned int numberRows) const
173 {
174  const uint8_t* layerFrame = layerI1_.frame().template constdata<uint8_t>();
175  const uint8_t* layerMask = layerI1_.mask().template constdata<uint8_t>();
176 
177  const unsigned int layerFramePaddingElements = layerI1_.frame().paddingElements();
178  const unsigned int layerMaskPaddingElements = layerI1_.mask().paddingElements();
179 
180  const MappingI1& layerMapping = layerI1_.mapping();
181 
182 #ifdef OCEAN_DEBUG
183  const PixelBoundingBox& debugLayerBoundingBox = layerI1_.boundingBox();
184  ocean_assert(!debugLayerBoundingBox || firstRow >= debugLayerBoundingBox.top());
185  ocean_assert(!debugLayerBoundingBox || firstRow + numberRows <= debugLayerBoundingBox.bottomEnd());
186 #endif // OCEAN_DEBUG
187 
188  uint64_t rowCost;
189 
190  for (unsigned int y = firstRow; y < firstRow + numberRows; ++y)
191  {
192  rowCost = 0ull;
193 
194  const uint8_t* maskRow = layerI1_.mask().template constrow<uint8_t>(y);
195  const PixelPosition* mappingRow = layerI_.mapping().row(y);
196 
197  for (unsigned int x = firstColumn; x < firstColumn + numberColumns; ++x)
198  {
199  if (maskRow[x] != 0xFFu)
200  {
201  const PixelPosition& mapping = mappingRow[x];
202 
203  const unsigned int spatialCost = layerMapping.spatialCost4Neighborhood<tChannels>(x, y, mapping.x(), mapping.y(), layerMask, layerMaskPaddingElements, maxSpatialCost_);
204  const unsigned int appearanceCost = layerMapping.appearanceCost5x5<tChannels, tBorderFactor>(x, y, mapping.x(), mapping.y(), layerFrame, layerMask, layerFramePaddingElements, layerMaskPaddingElements);
205 
206  rowCost += uint64_t(tWeightFactor) * uint64_t(spatialCost) + uint64_t(appearanceCost);
207  }
208  }
209 
210  ocean_assert(y < rowCost_.size());
211  rowCost_[y] = rowCost;
212  }
213 }
214 
215 }
216 
217 }
218 
219 }
220 
221 #endif // META_OCEAN_CV_SYNTHESIS_CREATOR_INFORMATION_COST_4_NEIGHBORHOOD_I_1_H
T bottomEnd() const
Returns the bottom (excluding) pixel position of this bounding box.
Definition: PixelBoundingBox.h:451
T top() const
Returns the top (including) pixel position of this bounding box.
Definition: PixelBoundingBox.h:423
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
This class implements the base class for all creators that support mappings for one frame.
Definition: Creator1.h:28
This class implements the base class for all creators.
Definition: Creator.h:29
This class is the base class for all creators that support mappings with integer accuracy.
Definition: CreatorI.h:29
This class implements a creator that determines the mapping cost for a four neighborhood for mappings...
Definition: CreatorInformationCost4NeighborhoodI1.h:36
bool invoke(Worker *worker=nullptr) const override
Invokes the creator.
Definition: CreatorInformationCost4NeighborhoodI1.h:108
CreatorInformationCost4NeighborhoodI1(const LayerI1 &layer, uint64_t &cost, const unsigned int maxSpatialCost=(unsigned int)(-1))
Creates a new creator object.
Definition: CreatorInformationCost4NeighborhoodI1.h:95
uint64_t & cost_
Resulting creator cost.
Definition: CreatorInformationCost4NeighborhoodI1.h:85
Costs rowCost_
Intermediate row costs.
Definition: CreatorInformationCost4NeighborhoodI1.h:91
const LayerI1 & layerI1_
Specialized layer reference.
Definition: CreatorInformationCost4NeighborhoodI1.h:82
void createSubsetChannels(const unsigned int firstColumn, const unsigned int numberColumns, const unsigned int firstRow, const unsigned int numberRows) const
Specialization of the default function that creates a subset of the information.
Definition: CreatorInformationCost4NeighborhoodI1.h:172
std::vector< uint64_t > Costs
Definition of a vector holding costs.
Definition: CreatorInformationCost4NeighborhoodI1.h:42
const unsigned int maxSpatialCost_
Maximal spatial cost.
Definition: CreatorInformationCost4NeighborhoodI1.h:88
void createSubset(const unsigned int firstColumn, const unsigned int numberColumns, const unsigned int firstRow, const unsigned int numberRows) const override
Creates a subset of the information.
Definition: CreatorInformationCost4NeighborhoodI1.h:143
This class implements a creator that can be distributed to subsets of the synthesis layer.
Definition: CreatorSubset.h:28
bool invoke(Worker *worker=nullptr) const override
Invokes the creator.
This class implements a single layer for pixel synthesis within one frame and pixel accuracy.
Definition: LayerI1.h:41
This class implements the pixel mapping between source and target frames.
Definition: MappingI1.h:49
unsigned int spatialCost4Neighborhood(const unsigned int xTarget, const unsigned int yTarget, const unsigned int xSource, const unsigned int ySource, const uint8_t *targetMask, const unsigned int targetMaskPaddingElements, const unsigned int maxCost) const
Calculates the smallest/cheapest spatial cost for a given point in a four-neighborhood and normalizes...
Definition: MappingI1.h:212
unsigned int appearanceCost5x5(const unsigned int xTarget, const unsigned int yTarget, const unsigned int xSource, const unsigned int ySource, const uint8_t *frame, const uint8_t *mask, const unsigned int framePaddingElements, const unsigned int maskPaddingElements) const
Calculates the appearance cost for a given point in a given frame.
Definition: MappingI1.h:634
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