Ocean
InitializerCoarserMappingAdaptionI1.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_INITIALIZER_COARSER_MAPPING_ADAPTION_I_1_H
9 #define META_OCEAN_CV_SYNTHESIS_INITIALIZER_COARSER_MAPPING_ADAPTION_I_1_H
10 
16 
17 namespace Ocean
18 {
19 
20 namespace CV
21 {
22 
23 namespace Synthesis
24 {
25 
26 /**
27  * This initializer creates an initial mapping by the adaption of an already existing mapping of a coarser synthesis layer.<br>
28  * The initializer supports mapping with integer accuracy.<br>
29  * The coarser mapping is upsampled and adjusted to the synthesis mask.
30  * @tparam tFactor The template parameter defines the dimension increase factor between the synthesis layer and the given coarser layer. A factor of 2 means that the width and height of the synthesis layer is two times larger than the width and height of the given coarser layer, with range [2, infinity)
31  * @see MappingI, LayerI1, InitializerCoarserMappingAdaptionF1, InitializerAreaConstrainedCoarserMappingAdaptionI1.
32  * @ingroup cvsynthesis
33  */
34 template <unsigned int tFactor>
36  virtual public InitializerI,
37  virtual public InitializerRandomized,
38  virtual public InitializerSubset,
39  virtual public Initializer1
40 {
41  public:
42 
43  /**
44  * Creates a new initializer object.
45  * @param layer The layer for that the initial mapping has to be provided
46  * @param randomGenerator Random number generator
47  * @param coarserLayer The coarser synthesis layer from that the mapping will be adapted
48  */
49  inline InitializerCoarserMappingAdaptionI1(LayerI1& layer, RandomGenerator& randomGenerator, const LayerI1& coarserLayer);
50 
51  private:
52 
53  /**
54  * Initializes a subset of the entire mapping area.
55  * @see InitializerSubset::initializeSubset().
56  */
57  void initializeSubset(const unsigned int firstColumn, const unsigned int numberColumns, const unsigned int firstRow, const unsigned int numberRows) const override;
58 
59  private:
60 
61  /// Coarser synthesis layer from that the mapping will be adapted.
63 };
64 
65 template <unsigned int tFactor>
67  Initializer(layer),
68  InitializerI(layer),
69  InitializerRandomized(layer, randomGenerator),
70  InitializerSubset(layer),
71  Initializer1(layer),
72  coarserLayerI_(coarserLayer)
73 {
74  // nothing to do here
75 }
76 
77 template <unsigned int tFactor>
78 void InitializerCoarserMappingAdaptionI1<tFactor>::initializeSubset(const unsigned int firstColumn, const unsigned int numberColumns, const unsigned int firstRow, const unsigned int numberRows) const
79 {
80  static_assert(tFactor >= 2u, "Invalid factor!");
81 
82  const unsigned int width = layerI_.width();
83  const unsigned int height = layerI_.height();
84 
85  const unsigned int coarserWidth = coarserLayerI_.width();
86  const unsigned int coarserHeight = coarserLayerI_.height();
87 
88  ocean_assert(width / tFactor == coarserWidth);
89  ocean_assert(height / tFactor == coarserHeight);
90 
91  MappingI& mapping = layerI_.mapping();
92  const MappingI& coarserMapping = coarserLayerI_.mapping();
93 
94  RandomGenerator randomGenerator(randomGenerator_);
95 
96  const uint8_t* const mask = layerI_.mask().template constdata<uint8_t>();
97  const uint8_t* const coarserMask = coarserLayerI_.mask().template constdata<uint8_t>();
98 
99  const unsigned int maskStrideElements = layerI_.mask().strideElements();
100  const unsigned int coarserMaskStrideElements = coarserLayerI_.mask().strideElements();
101 
102  for (unsigned int y = firstRow; y < firstRow + numberRows; ++y)
103  {
104  const uint8_t* maskRow = mask + y * maskStrideElements;
105  PixelPosition* positionRow = mapping.row(y);
106 
107  const unsigned int yCoarser = min(y / tFactor, coarserHeight - 1u);
108 
109  const uint8_t* coarserMaskRow = coarserMask + yCoarser * coarserMaskStrideElements;
110  const PixelPosition* coarserPositionRow = coarserMapping.row(yCoarser);
111 
112  for (unsigned int x = firstColumn; x < firstColumn + numberColumns; ++x)
113  {
114  if (maskRow[x] != 0xFFu)
115  {
116  const unsigned int xCoarser = min(x / tFactor, coarserWidth - 1u);
117 
118  // if the corresponding coarser layer pixel is a mask pixel
119  if (coarserMaskRow[xCoarser] != 0xFFu)
120  {
121  const PixelPosition& coarserPosition = coarserPositionRow[xCoarser];
122  ocean_assert(coarserPosition.x() < coarserLayerI_.width());
123  ocean_assert(coarserPosition.y() < coarserLayerI_.height());
124 
125  const unsigned int candidateX = (unsigned int)(int(x) + (int(coarserPosition.x()) - int(xCoarser)) * int(tFactor));
126  const unsigned int candidateY = (unsigned int)(int(y) + (int(coarserPosition.y()) - int(yCoarser)) * int(tFactor));
127 
128  ocean_assert(candidateX < width);
129  ocean_assert(candidateY < height);
130 
131  if (mask[candidateY * maskStrideElements + candidateX] == 0xFFu)
132  {
133  positionRow[x] = CV::PixelPosition(candidateX, candidateY);
134  continue;
135  }
136  }
137 
138  unsigned int candidateX, candidateY;
139  do
140  {
141  candidateX = RandomI::random(randomGenerator, width - 1u);
142  candidateY = RandomI::random(randomGenerator, height - 1u);
143  }
144  while (mask[candidateY * maskStrideElements + candidateX] != 0xFFu);
145 
146  positionRow[x] = CV::PixelPosition(candidateX, candidateY);
147  }
148  }
149  }
150 }
151 
152 }
153 
154 }
155 
156 }
157 
158 #endif // META_OCEAN_CV_SYNTHESIS_INITIALIZER_COARSER_MAPPING_ADAPTION_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
This class is the base class for all initializers that are provided for a single frame only.
Definition: Initializer1.h:29
This initializer creates an initial mapping by the adaption of an already existing mapping of a coars...
Definition: InitializerCoarserMappingAdaptionI1.h:40
void initializeSubset(const unsigned int firstColumn, const unsigned int numberColumns, const unsigned int firstRow, const unsigned int numberRows) const override
Initializes a subset of the entire mapping area.
Definition: InitializerCoarserMappingAdaptionI1.h:78
InitializerCoarserMappingAdaptionI1(LayerI1 &layer, RandomGenerator &randomGenerator, const LayerI1 &coarserLayer)
Creates a new initializer object.
Definition: InitializerCoarserMappingAdaptionI1.h:66
const LayerI1 & coarserLayerI_
Coarser synthesis layer from that the mapping will be adapted.
Definition: InitializerCoarserMappingAdaptionI1.h:62
This class implements the base class for all synthesis initializers.
Definition: Initializer.h:34
This class implements the base class for all initializer objects that are applied for mappings with i...
Definition: InitializerI.h:30
This class is the base class for all initializers that mainly initialize the synthesis mapping by a h...
Definition: InitializerRandomized.h:30
This class is the base class for all initializer objects that can separate the initialization process...
Definition: InitializerSubset.h:29
This class implements a single layer for pixel synthesis within one frame and pixel accuracy.
Definition: LayerI1.h:41
This class implements a mapping with integer accuracy.
Definition: MappingI.h:30
const PixelPosition * row(const unsigned int y) const
Returns the pointer to a mapping row.
Definition: MappingI.h:243
This class implements a generator for random numbers.
Definition: RandomGenerator.h:42
static unsigned int random(const unsigned int maxValue)
Returns one random integer value with specified maximum value.
PixelPositionT< unsigned int > PixelPosition
Definition of the default PixelPosition object with a data type allowing only positive coordinate val...
Definition: PixelPosition.h:27
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15